summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Controls/TextEditInPlace.cpp
blob: 145df14004147b1f09ea39273ecc762c6122402f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/****************************************************************************
**
** Copyright (C) 2002 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

//==============================================================================
//	Prefix
//==============================================================================
#include "stdafx.h"

// using namespace Q3DStudio;  <-- Do not do this here because it will conflict with CList and make
// the template generator go blah

//==============================================================================
//	Includes
//==============================================================================
#include "TextEditInPlace.h"
#include "Renderer.h"

//==============================================================================
//	Static class constants
//==============================================================================
const long CTextEditInPlace::s_RightBuffer =
    6; ///< Extra space allotted on the right side of the text

//==============================================================================
/**
 * Constructor
 */
CTextEditInPlace::CTextEditInPlace()
    : CStringEdit()
    , m_IsInEditMode(false)
    , m_IsEditable(true)
    , m_AllowSingleClickEdit(true)
{
    SetAlignment(LEFT);
    SetReadOnly(true);
    SetFillBackground(false);
}

//==============================================================================
/**
 * Destructor
 */
CTextEditInPlace::~CTextEditInPlace()
{
}

//==============================================================================
/**
 * Handles the mouse double-click event.  Enters the text into edit mode so that
 * the user can change its value.
 * @param inPoint location of the mouse
 * @param inFlags modifier flags for the mouse
 * @return true if this message should not be passed to any other children, otherwise false
 */
bool CTextEditInPlace::OnMouseDoubleClick(CPt inPoint, Qt::KeyboardModifiers inFlags)
{
    bool theMessageWasHandled = false;
    if (m_IsEditable) {
        SetEditMode(true);
        SetSelection(0, GetString().Length());
        m_Caret.show = true;
        theMessageWasHandled = true;
    } else
        theMessageWasHandled = CStringEdit::OnMouseDoubleClick(inPoint, inFlags);
    return theMessageWasHandled;
}

void CTextEditInPlace::OnMouseClick(CPt inPoint, Qt::KeyboardModifiers inFlags)
{
    CStringEdit::OnMouseClick(inPoint, inFlags);
    if (IsInFocus() && !(inFlags & CHotKeys::MOUSE_RBUTTON) && !m_IsInEditMode && m_IsEditable
        && m_AllowSingleClickEdit) {
        SetEditMode(true);
        SelectAllText();
    }
}

//==============================================================================
/**
 * Called when this control loses focus.  Turns off edit mode and redraws the
 * control.
 */
void CTextEditInPlace::OnLoseFocus()
{
    SetEditMode(false);
    CStringEdit::OnLoseFocus();
    Invalidate();
}

//==============================================================================
/**
 * Handles most of the drawing, with some help from the parent class.
 * @param inRenderer Renderer to draw to
 */
void CTextEditInPlace::Draw(CRenderer *inRenderer)
{
    CRct theRect(GetSize()); // CRct( CPt( ::dtol( CalculateCharWidths( inRenderer ) + s_RightBuffer
                             // ), GetSize( ).y ) );

    inRenderer->PushClippingRect(theRect);
    CStringEdit::Draw(inRenderer);
    inRenderer->PopClippingRect();
}

//==============================================================================
/**
 * Enables or disables this control from being able to enter "Edit Mode" when
 * SetEditMode() is called.
 * @param inIsEditable true if you want the control to be editable when double-clicked, otherwise
 * false
 */
void CTextEditInPlace::SetEditable(bool inIsEditable)
{
    m_IsEditable = inIsEditable;

    if (!m_IsEditable && m_IsInEditMode)
        SetEditMode(false);
}

//==============================================================================
/**
 * Starts or stops "Edit Mode".  While in Edit Mode, the user can change the
 * text, move the caret, and highlight the text.  An edit box is also drawn
 * around the text.
 * @param inEditMode true to turn on Edit Mode, false to turn off Edit Mode
 */
void CTextEditInPlace::SetEditMode(bool inEditMode)
{
    if (m_IsEditable && (inEditMode != m_IsInEditMode)) {
        m_IsInEditMode = inEditMode;
        SetReadOnly(!m_IsInEditMode);

        // If we are in edit mode
        if (m_IsInEditMode) {
            m_PreviousValue = GetString();
        }
        // If we are not in edit mode
        else {
            // Restore the text color and commit the changes
            FireCommitEvent();
        }
    }
}

//==============================================================================
/**
 * Overriden from the parent to cause this control to lose focus when the Enter
 * button is pressed on the keyboard.
 * @param inHighlight true to highlight all of the text, false to just change the string
 */
void CTextEditInPlace::EnterText(bool inHighlight)
{
    CStringEdit::EnterText(inHighlight);
    OnLoseFocus();
}

//==============================================================================
/**
 * Overriden to also invalidate/validate the parent.
 * @param inInvalidate true to invalidate this control and the parent control
 */
void CTextEditInPlace::Invalidate(bool inInvalidate)
{
    if (inInvalidate && GetParent())
        GetParent()->Invalidate(inInvalidate);

    CStringEdit::Invalidate(inInvalidate);
}

//==============================================================================
/**
 * Override to avoid selecting all the text unless in edit mode.
 */
void CTextEditInPlace::OnGainFocus()
{
    if (m_IsInEditMode && m_IsEditable)
        CStringEdit::OnGainFocus();
}

bool CTextEditInPlace::CanGainFocus()
{
    return true;
}

//==============================================================================
/**
 * Returns the edit mode.
 * @return true if the control is currently in edit mode meaning that the user
 * can enter text into it, otherwise false
 */
bool CTextEditInPlace::GetEditMode()
{
    return m_IsInEditMode;
}

long CTextEditInPlace::GetRightBuffer()
{
    return s_RightBuffer;
}

//==============================================================================
/**
 * Handles the Right mouse button down command.  Only shows the cut/copy/paste
 * context menu if the control is currently being edited.  You can't change a
 * string that's not currently being edited
 * @param inPoint The mouse position at the time of the event
 * @param inFlags The state of the modifier keys at the time of the event
 * @return true if this function handled the character, otherwise false
 */

bool CTextEditInPlace::OnMouseRDown(CPt inPoint, Qt::KeyboardModifiers inFlags)
{
    bool theMessageWasHandled = false;

    // The base class shows the cut/copy/paste menu, so only do it if we are in edit mode
    if (m_IsInEditMode)
        theMessageWasHandled = CStringEdit::OnMouseRDown(inPoint, inFlags);

    return theMessageWasHandled;
}

//=============================================================================
/**
 * Handles character input from the keyboard.
 *
 * @param inChar Character that was pressed
 * @return true if the character was handled, false if this control does not
 * care about the character that was pressed
 */
bool CTextEditInPlace::OnChar(const QString &inChar, Qt::KeyboardModifiers inModifiers)
{
    bool theMessageWasHandled = false;
    if (GetEditMode())
        theMessageWasHandled = CStringEdit::OnChar(inChar, inModifiers);
    return theMessageWasHandled;
}

//==============================================================================
/**
 * By default, clicking on a CTextEditInPlace control that already has focus, will
 * cause the control to enter edit mode (if SetEditable( true ) has been called).
 * This is referred to as single-click editing.  This function allows you to
 * enable or disable this functionality.  Even if single-click editing is
 * disabled, the user can still edit the text control by double-clicking on it.
 * @param inAllow true to enable single-click editing, false to prevent it
 */
void CTextEditInPlace::SetSingleClickEdit(bool inAllow)
{
    m_AllowSingleClickEdit = inAllow;
}

//==============================================================================
/**
 * Reverts the displayed text to the previous text.
 */
void CTextEditInPlace::RevertText()
{
    if (m_IsInEditMode)
        CStringEdit::RevertText();
}

//==============================================================================
/**
 * Override the base class's version
 */
void CTextEditInPlace::DoFillBackground(CRenderer *inRenderer)
{
    if (m_FillBackground && !m_IsInEditMode)
        inRenderer->FillSolidRect(QRect(0, 0, GetSize().x, GetSize().y), m_BackgroundColorNoFocus);
}

//==============================================================================
/**
 * Handles any non-character keys that were pressed and need to be handled.
 *
 * @param inChar The key that was pressed
 * @param inFlags Indicates which modifier keys were down when event occurred
 * @return true if this function handled the character, otherwise false
 */
bool CTextEditInPlace::HandleSpecialChar(unsigned int inChar, Qt::KeyboardModifiers inFlags)
{
    bool theMessageWasHandled = false;

    switch (inChar) {
    // We're overwriting the Escape hotkey sequence from StringEdit because we need to lose focus
    // in TextEditInPlace, StringEdit basically does the same thing for Escape and Ctrl + Z.
    case Qt::Key_Escape:
        RevertText();
        OnLoseFocus();
        theMessageWasHandled = true;
        break;

    default:
        theMessageWasHandled = CStringEdit::HandleSpecialChar(inChar, inFlags);
    }

    return theMessageWasHandled;
}