Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "CInput.h"
21 :
22 : #include "graphics/Canvas2D.h"
23 : #include "graphics/FontMetrics.h"
24 : #include "graphics/TextRenderer.h"
25 : #include "gui/CGUI.h"
26 : #include "gui/CGUIScrollBarVertical.h"
27 : #include "gui/IGUIScrollBar.h"
28 : #include "gui/SGUIMessage.h"
29 : #include "gui/SettingTypes/EAlign.h"
30 : #include "lib/debug.h"
31 : #include "lib/external_libraries/libsdl.h"
32 : #include "lib/timer.h"
33 : #include "lib/utf8.h"
34 : #include "maths/Size2D.h"
35 : #include "maths/Vector2D.h"
36 : #include "ps/CStrIntern.h"
37 : #include "ps/ConfigDB.h"
38 : #include "ps/Globals.h"
39 : #include "ps/Hotkey.h"
40 : #include "ps/Input.h"
41 :
42 : #include <SDL_clipboard.h>
43 : #include <SDL_events.h>
44 : #include <SDL_keyboard.h>
45 : #include <SDL_mouse.h>
46 : #include <SDL_rect.h>
47 : #include <SDL_scancode.h>
48 : #include <SDL_stdinc.h>
49 : #include <algorithm>
50 : #include <cstring>
51 : #include <cwchar>
52 : #include <memory>
53 : #include <string>
54 : #include <unordered_map>
55 : #include <utility>
56 :
57 : extern int g_yres;
58 :
59 : const CStr CInput::EventNameTextEdit = "TextEdit";
60 : const CStr CInput::EventNamePress = "Press";
61 : const CStr CInput::EventNameTab = "Tab";
62 :
63 0 : CInput::CInput(CGUI& pGUI)
64 : :
65 : IGUIObject(pGUI),
66 : IGUIScrollBarOwner(*static_cast<IGUIObject*>(this)),
67 0 : m_iBufferPos(-1),
68 0 : m_iBufferPos_Tail(-1),
69 0 : m_SelectingText(),
70 0 : m_HorizontalScroll(),
71 0 : m_PrevTime(),
72 0 : m_CursorVisState(true),
73 0 : m_CursorBlinkRate{g_ConfigDB.Get("gui.cursorblinkrate", 0.5)},
74 0 : m_ComposingText(),
75 0 : m_GeneratedPlaceholderTextValid(false),
76 0 : m_iComposedLength(),
77 0 : m_iComposedPos(),
78 0 : m_iInsertPos(),
79 0 : m_BufferPosition(this, "buffer_position"),
80 0 : m_BufferZone(this, "buffer_zone"),
81 0 : m_Caption(this, "caption"),
82 0 : m_Font(this, "font"),
83 0 : m_MaskChar(this, "mask_char"),
84 0 : m_Mask(this, "mask"),
85 0 : m_MaxLength(this, "max_length"),
86 0 : m_MultiLine(this, "multiline"),
87 0 : m_Readonly(this, "readonly"),
88 0 : m_ScrollBar(this, "scrollbar"),
89 0 : m_ScrollBarStyle(this, "scrollbar_style"),
90 0 : m_Sprite(this, "sprite"),
91 0 : m_SpriteOverlay(this, "sprite_overlay"),
92 0 : m_SpriteSelectArea(this, "sprite_selectarea"),
93 0 : m_TextColor(this, "textcolor"),
94 0 : m_TextColorSelected(this, "textcolor_selected"),
95 0 : m_PlaceholderText(this, "placeholder_text"),
96 0 : m_PlaceholderColor(this, "placeholder_color")
97 : {
98 0 : auto bar = std::make_unique<CGUIScrollBarVertical>(pGUI);
99 0 : bar->SetRightAligned(true);
100 0 : AddScrollBar(std::move(bar));
101 0 : }
102 :
103 0 : void CInput::UpdateBufferPositionSetting()
104 : {
105 0 : m_BufferPosition.Set(m_iBufferPos, false);
106 0 : }
107 :
108 0 : void CInput::ClearComposedText()
109 : {
110 0 : m_Caption.GetMutable().erase(m_iInsertPos, m_iComposedLength);
111 0 : m_iBufferPos = m_iInsertPos;
112 0 : UpdateBufferPositionSetting();
113 0 : m_iComposedLength = 0;
114 0 : m_iComposedPos = 0;
115 0 : }
116 :
117 0 : Input::Reaction CInput::ManuallyHandleKeys(const SDL_Event& ev)
118 : {
119 0 : ENSURE(m_iBufferPos != -1);
120 :
121 : // Get direct access to silently mutate m_Caption.
122 : // (Messages don't currently need to be sent)
123 0 : CStrW& caption = m_Caption.GetMutable();
124 :
125 0 : switch (ev.type)
126 : {
127 0 : case SDL_HOTKEYDOWN:
128 0 : {
129 0 : if (m_ComposingText)
130 : return Input::Reaction::HANDLED;
131 :
132 0 : return ManuallyHandleHotkeyEvent(ev);
133 : }
134 : // SDL2 has a new method of text input that better supports Unicode and CJK
135 : // see https://wiki.libsdl.org/Tutorials/TextInput
136 0 : case SDL_TEXTINPUT:
137 0 : {
138 0 : if (m_Readonly)
139 : return Input::Reaction::PASS;
140 :
141 : // Text has been committed, either single key presses or through an IME
142 0 : std::wstring text = wstring_from_utf8(ev.text.text);
143 :
144 : // Check max length
145 0 : if (m_MaxLength != 0 && caption.length() + text.length() > static_cast<size_t>(m_MaxLength))
146 : return Input::Reaction::HANDLED;
147 :
148 0 : m_WantedX = 0.0f;
149 :
150 0 : if (SelectingText())
151 0 : DeleteCurSelection();
152 :
153 0 : if (m_ComposingText)
154 : {
155 0 : ClearComposedText();
156 0 : m_ComposingText = false;
157 : }
158 :
159 0 : if (m_iBufferPos == static_cast<int>(caption.length()))
160 0 : caption.append(text);
161 : else
162 0 : caption.insert(m_iBufferPos, text);
163 :
164 0 : UpdateText(m_iBufferPos, m_iBufferPos, m_iBufferPos+1);
165 :
166 0 : m_iBufferPos += text.length();
167 0 : UpdateBufferPositionSetting();
168 0 : m_iBufferPos_Tail = -1;
169 :
170 0 : UpdateAutoScroll();
171 0 : SendEvent(GUIM_TEXTEDIT, EventNameTextEdit);
172 :
173 : return Input::Reaction::HANDLED;
174 0 : }
175 0 : case SDL_TEXTEDITING:
176 0 : {
177 0 : if (m_Readonly)
178 : return Input::Reaction::PASS;
179 :
180 : // Text is being composed with an IME
181 : // TODO: indicate this by e.g. underlining the uncommitted text
182 0 : const char* rawText = ev.edit.text;
183 0 : int rawLength = strlen(rawText);
184 0 : std::wstring wtext = wstring_from_utf8(rawText);
185 :
186 0 : m_WantedX = 0.0f;
187 :
188 0 : if (SelectingText())
189 0 : DeleteCurSelection();
190 :
191 : // Remember cursor position when text composition begins
192 0 : if (!m_ComposingText)
193 0 : m_iInsertPos = m_iBufferPos;
194 : else
195 : {
196 : // Composed text is replaced each time
197 0 : ClearComposedText();
198 : }
199 :
200 0 : m_ComposingText = ev.edit.start != 0 || rawLength != 0;
201 0 : if (m_ComposingText)
202 : {
203 0 : caption.insert(m_iInsertPos, wtext);
204 :
205 : // The text buffer is limited to SDL_TEXTEDITINGEVENT_TEXT_SIZE bytes, yet start
206 : // increases without limit, so don't let it advance beyond the composed text length
207 0 : m_iComposedLength = wtext.length();
208 0 : m_iComposedPos = ev.edit.start < m_iComposedLength ? ev.edit.start : m_iComposedLength;
209 0 : m_iBufferPos = m_iInsertPos + m_iComposedPos;
210 :
211 : // TODO: composed text selection - what does ev.edit.length do?
212 0 : m_iBufferPos_Tail = -1;
213 : }
214 :
215 0 : UpdateBufferPositionSetting();
216 0 : UpdateText(m_iBufferPos, m_iBufferPos, m_iBufferPos+1);
217 :
218 0 : UpdateAutoScroll();
219 0 : SendEvent(GUIM_TEXTEDIT, EventNameTextEdit);
220 :
221 0 : return Input::Reaction::HANDLED;
222 0 : }
223 0 : case SDL_KEYDOWN:
224 0 : case SDL_KEYUP:
225 0 : {
226 : // Since the GUI framework doesn't handle to set settings
227 : // in Unicode (CStrW), we'll simply retrieve the actual
228 : // pointer and edit that.
229 0 : SDL_Keycode keyCode = ev.key.keysym.sym;
230 :
231 : // We have a probably printable key - we should return HANDLED so it can't trigger hotkeys.
232 : // However, if Ctrl/Meta modifiers are active, just pass it through instead,
233 : // assuming that we are indeed trying to trigger hotkeys (e.g. copy/paste).
234 : // Escape & the "cancel" hotkey are also passed through to allow closing dialogs easily.
235 : // See also similar logic in CConsole.cpp
236 : // NB: this assumes that Ctrl/GUI aren't used in the Manually* functions below,
237 : // as those code paths would obviously never be taken.
238 0 : if (keyCode == SDLK_ESCAPE || EventWillFireHotkey(ev, "cancel") ||
239 0 : g_scancodes[SDL_SCANCODE_LCTRL] || g_scancodes[SDL_SCANCODE_RCTRL] ||
240 0 : g_scancodes[SDL_SCANCODE_LGUI] || g_scancodes[SDL_SCANCODE_RGUI])
241 : return Input::Reaction::PASS;
242 :
243 0 : if (m_ComposingText)
244 : return Input::Reaction::HANDLED;
245 :
246 0 : if (ev.type == SDL_KEYDOWN)
247 : {
248 0 : ManuallyImmutableHandleKeyDownEvent(keyCode);
249 0 : ManuallyMutableHandleKeyDownEvent(keyCode);
250 :
251 0 : UpdateBufferPositionSetting();
252 : }
253 : return Input::Reaction::HANDLED;
254 : }
255 : default:
256 : {
257 : return Input::Reaction::PASS;
258 : }
259 : }
260 : }
261 :
262 0 : void CInput::ManuallyMutableHandleKeyDownEvent(const SDL_Keycode keyCode)
263 : {
264 0 : if (m_Readonly)
265 : return;
266 :
267 0 : wchar_t cooked = 0;
268 :
269 : // Get direct access to silently mutate m_Caption.
270 : // (Messages don't currently need to be sent)
271 0 : CStrW& caption = m_Caption.GetMutable();
272 :
273 0 : switch (keyCode)
274 : {
275 0 : case SDLK_TAB:
276 0 : {
277 0 : SendEvent(GUIM_TAB, EventNameTab);
278 : // Don't send a textedit event, because it should only
279 : // be sent if the GUI control changes the text
280 0 : break;
281 : }
282 0 : case SDLK_BACKSPACE:
283 0 : {
284 0 : m_WantedX = 0.0f;
285 :
286 0 : if (SelectingText())
287 0 : DeleteCurSelection();
288 : else
289 : {
290 0 : m_iBufferPos_Tail = -1;
291 :
292 0 : if (caption.empty() || m_iBufferPos == 0)
293 : break;
294 :
295 0 : if (m_iBufferPos == static_cast<int>(caption.length()))
296 0 : caption = caption.substr(0, static_cast<long>(caption.length()) - 1);
297 : else
298 0 : caption = caption.substr(0, m_iBufferPos - 1) + caption.substr(m_iBufferPos);
299 :
300 0 : --m_iBufferPos;
301 :
302 0 : UpdateText(m_iBufferPos, m_iBufferPos + 1, m_iBufferPos);
303 : }
304 :
305 0 : UpdateAutoScroll();
306 0 : SendEvent(GUIM_TEXTEDIT, EventNameTextEdit);
307 0 : break;
308 : }
309 0 : case SDLK_DELETE:
310 0 : {
311 0 : m_WantedX = 0.0f;
312 :
313 0 : if (SelectingText())
314 0 : DeleteCurSelection();
315 : else
316 : {
317 0 : if (caption.empty() || m_iBufferPos == static_cast<int>(caption.length()))
318 : break;
319 :
320 0 : caption = caption.substr(0, m_iBufferPos) + caption.substr(m_iBufferPos + 1);
321 0 : UpdateText(m_iBufferPos, m_iBufferPos + 1, m_iBufferPos);
322 : }
323 :
324 0 : UpdateAutoScroll();
325 0 : SendEvent(GUIM_TEXTEDIT, EventNameTextEdit);
326 0 : break;
327 : }
328 0 : case SDLK_KP_ENTER:
329 0 : case SDLK_RETURN:
330 0 : {
331 : // 'Return' should do a Press event for single liners (e.g. submitting forms)
332 : // otherwise a '\n' character will be added.
333 0 : if (!m_MultiLine)
334 : {
335 0 : SendEvent(GUIM_PRESSED, EventNamePress);
336 0 : break;
337 : }
338 :
339 : cooked = '\n'; // Change to '\n' and do default:
340 0 : [[fallthrough]];
341 : }
342 0 : default: // Insert a character
343 0 : {
344 : // Regular input is handled via SDL_TEXTINPUT, so we should ignore it here.
345 0 : if (cooked == 0)
346 : return;
347 :
348 : // Check max length
349 0 : if (m_MaxLength != 0 && caption.length() >= static_cast<size_t>(m_MaxLength))
350 : break;
351 :
352 0 : m_WantedX = 0.0f;
353 :
354 0 : if (SelectingText())
355 0 : DeleteCurSelection();
356 0 : m_iBufferPos_Tail = -1;
357 :
358 0 : if (m_iBufferPos == static_cast<int>(caption.length()))
359 0 : caption += cooked;
360 : else
361 0 : caption = caption.substr(0, m_iBufferPos) + cooked + caption.substr(m_iBufferPos);
362 :
363 0 : UpdateText(m_iBufferPos, m_iBufferPos, m_iBufferPos + 1);
364 :
365 0 : ++m_iBufferPos;
366 :
367 0 : UpdateAutoScroll();
368 0 : SendEvent(GUIM_TEXTEDIT, EventNameTextEdit);
369 0 : break;
370 : }
371 : }
372 : }
373 :
374 0 : void CInput::ManuallyImmutableHandleKeyDownEvent(const SDL_Keycode keyCode)
375 : {
376 0 : bool shiftKeyPressed = g_scancodes[SDL_SCANCODE_LSHIFT] || g_scancodes[SDL_SCANCODE_RSHIFT];
377 :
378 0 : const CStrW& caption = *m_Caption;
379 :
380 0 : switch (keyCode)
381 : {
382 0 : case SDLK_HOME:
383 0 : {
384 : // If there's not a selection, we should create one now
385 0 : if (!shiftKeyPressed)
386 : {
387 : // Make sure a selection isn't created.
388 0 : m_iBufferPos_Tail = -1;
389 : }
390 0 : else if (!SelectingText())
391 : {
392 : // Place tail at the current point:
393 0 : m_iBufferPos_Tail = m_iBufferPos;
394 : }
395 :
396 0 : m_iBufferPos = 0;
397 0 : m_WantedX = 0.0f;
398 :
399 0 : UpdateAutoScroll();
400 0 : break;
401 : }
402 0 : case SDLK_END:
403 0 : {
404 : // If there's not a selection, we should create one now
405 0 : if (!shiftKeyPressed)
406 : {
407 : // Make sure a selection isn't created.
408 0 : m_iBufferPos_Tail = -1;
409 : }
410 0 : else if (!SelectingText())
411 : {
412 : // Place tail at the current point:
413 0 : m_iBufferPos_Tail = m_iBufferPos;
414 : }
415 :
416 0 : m_iBufferPos = static_cast<long>(caption.length());
417 0 : m_WantedX = 0.0f;
418 :
419 0 : UpdateAutoScroll();
420 0 : break;
421 : }
422 : /**
423 : * Conventions for Left/Right when text is selected:
424 : *
425 : * References:
426 : *
427 : * Visual Studio
428 : * Visual Studio has the 'newer' approach, used by newer versions of
429 : * things, and in newer applications. A left press will always place
430 : * the pointer on the left edge of the selection, and then of course
431 : * remove the selection. Right will do the exact same thing.
432 : * If you have the pointer on the right edge and press right, it will
433 : * in other words just remove the selection.
434 : *
435 : * Windows (eg. Notepad)
436 : * A left press always takes the pointer a step to the left and
437 : * removes the selection as if it were never there in the first place.
438 : * Right of course does the same thing but to the right.
439 : *
440 : * I chose the Visual Studio convention. Used also in Word, gtk 2.0, MSN
441 : * Messenger.
442 : */
443 0 : case SDLK_LEFT:
444 0 : {
445 0 : m_WantedX = 0.f;
446 :
447 0 : if (shiftKeyPressed || !SelectingText())
448 : {
449 0 : if (!shiftKeyPressed)
450 0 : m_iBufferPos_Tail = -1;
451 0 : else if (!SelectingText())
452 0 : m_iBufferPos_Tail = m_iBufferPos;
453 :
454 0 : if (m_iBufferPos > 0)
455 0 : --m_iBufferPos;
456 : }
457 : else
458 : {
459 0 : if (m_iBufferPos_Tail < m_iBufferPos)
460 0 : m_iBufferPos = m_iBufferPos_Tail;
461 :
462 0 : m_iBufferPos_Tail = -1;
463 : }
464 :
465 0 : UpdateAutoScroll();
466 0 : break;
467 : }
468 0 : case SDLK_RIGHT:
469 0 : {
470 0 : m_WantedX = 0.0f;
471 :
472 0 : if (shiftKeyPressed || !SelectingText())
473 : {
474 0 : if (!shiftKeyPressed)
475 0 : m_iBufferPos_Tail = -1;
476 0 : else if (!SelectingText())
477 0 : m_iBufferPos_Tail = m_iBufferPos;
478 :
479 0 : if (m_iBufferPos < static_cast<int>(caption.length()))
480 0 : ++m_iBufferPos;
481 : }
482 : else
483 : {
484 0 : if (m_iBufferPos_Tail > m_iBufferPos)
485 0 : m_iBufferPos = m_iBufferPos_Tail;
486 :
487 0 : m_iBufferPos_Tail = -1;
488 : }
489 :
490 0 : UpdateAutoScroll();
491 0 : break;
492 : }
493 : /**
494 : * Conventions for Up/Down when text is selected:
495 : *
496 : * References:
497 : *
498 : * Visual Studio
499 : * Visual Studio has a very strange approach, down takes you below the
500 : * selection to the next row, and up to the one prior to the whole
501 : * selection. The weird part is that it is always aligned as the
502 : * 'pointer'. I decided this is to much work for something that is
503 : * a bit arbitrary
504 : *
505 : * Windows (eg. Notepad)
506 : * Just like with left/right, the selection is destroyed and it moves
507 : * just as if there never were a selection.
508 : *
509 : * I chose the Notepad convention even though I use the VS convention with
510 : * left/right.
511 : */
512 0 : case SDLK_UP:
513 0 : {
514 0 : if (!shiftKeyPressed)
515 0 : m_iBufferPos_Tail = -1;
516 0 : else if (!SelectingText())
517 0 : m_iBufferPos_Tail = m_iBufferPos;
518 :
519 0 : std::list<SRow>::iterator current = m_CharacterPositions.begin();
520 0 : while (current != m_CharacterPositions.end())
521 : {
522 0 : if (m_iBufferPos >= current->m_ListStart &&
523 0 : m_iBufferPos <= current->m_ListStart + (int)current->m_ListOfX.size())
524 : break;
525 :
526 0 : ++current;
527 : }
528 :
529 0 : float pos_x;
530 0 : if (m_iBufferPos - current->m_ListStart == 0)
531 0 : pos_x = 0.f;
532 : else
533 0 : pos_x = current->m_ListOfX[m_iBufferPos - current->m_ListStart - 1];
534 :
535 0 : if (m_WantedX > pos_x)
536 0 : pos_x = m_WantedX;
537 :
538 : // Now change row:
539 0 : if (current != m_CharacterPositions.begin())
540 : {
541 0 : --current;
542 :
543 : // Find X-position:
544 0 : m_iBufferPos = current->m_ListStart + GetXTextPosition(current, pos_x, m_WantedX);
545 : }
546 : // else we can't move up
547 :
548 0 : UpdateAutoScroll();
549 0 : break;
550 : }
551 0 : case SDLK_DOWN:
552 0 : {
553 0 : if (!shiftKeyPressed)
554 0 : m_iBufferPos_Tail = -1;
555 0 : else if (!SelectingText())
556 0 : m_iBufferPos_Tail = m_iBufferPos;
557 :
558 0 : std::list<SRow>::iterator current = m_CharacterPositions.begin();
559 0 : while (current != m_CharacterPositions.end())
560 : {
561 0 : if (m_iBufferPos >= current->m_ListStart &&
562 0 : m_iBufferPos <= current->m_ListStart + (int)current->m_ListOfX.size())
563 : break;
564 :
565 0 : ++current;
566 : }
567 :
568 0 : float pos_x;
569 :
570 0 : if (m_iBufferPos - current->m_ListStart == 0)
571 0 : pos_x = 0.f;
572 : else
573 0 : pos_x = current->m_ListOfX[m_iBufferPos - current->m_ListStart - 1];
574 :
575 0 : if (m_WantedX > pos_x)
576 0 : pos_x = m_WantedX;
577 :
578 : // Now change row:
579 : // Add first, so we can check if it's .end()
580 0 : ++current;
581 0 : if (current != m_CharacterPositions.end())
582 : {
583 : // Find X-position:
584 0 : m_iBufferPos = current->m_ListStart + GetXTextPosition(current, pos_x, m_WantedX);
585 : }
586 : // else we can't move up
587 :
588 0 : UpdateAutoScroll();
589 0 : break;
590 : }
591 0 : case SDLK_PAGEUP:
592 0 : {
593 0 : GetScrollBar(0).ScrollMinusPlenty();
594 0 : UpdateAutoScroll();
595 0 : break;
596 : }
597 0 : case SDLK_PAGEDOWN:
598 0 : {
599 0 : GetScrollBar(0).ScrollPlusPlenty();
600 0 : UpdateAutoScroll();
601 0 : break;
602 : }
603 : default:
604 : {
605 : break;
606 : }
607 : }
608 0 : }
609 :
610 0 : void CInput::SetupGeneratedPlaceholderText()
611 : {
612 0 : m_GeneratedPlaceholderText = CGUIText(m_pGUI, m_PlaceholderText, m_Font, 0, m_BufferZone, EAlign::LEFT, this);
613 0 : m_GeneratedPlaceholderTextValid = true;
614 0 : }
615 :
616 0 : Input::Reaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event& ev)
617 : {
618 0 : bool shiftKeyPressed = g_scancodes[SDL_SCANCODE_LSHIFT] || g_scancodes[SDL_SCANCODE_RSHIFT];
619 :
620 0 : std::string hotkey = static_cast<const char*>(ev.user.data1);
621 :
622 : // Get direct access to silently mutate m_Caption.
623 : // (Messages don't currently need to be sent)
624 0 : CStrW& caption = m_Caption.GetMutable();
625 :
626 0 : if (hotkey == "paste")
627 : {
628 0 : if (m_Readonly)
629 : return Input::Reaction::PASS;
630 :
631 0 : m_WantedX = 0.0f;
632 :
633 0 : char* utf8_text = SDL_GetClipboardText();
634 0 : if (!utf8_text)
635 : return Input::Reaction::HANDLED;
636 :
637 0 : std::wstring text = wstring_from_utf8(utf8_text);
638 0 : SDL_free(utf8_text);
639 :
640 : // Check max length
641 0 : if (m_MaxLength != 0 && caption.length() + text.length() > static_cast<size_t>(m_MaxLength))
642 0 : text.erase(static_cast<size_t>(m_MaxLength) - caption.length());
643 :
644 0 : if (SelectingText())
645 0 : DeleteCurSelection();
646 :
647 0 : if (m_iBufferPos == static_cast<int>(caption.length()))
648 0 : caption += text;
649 : else
650 0 : caption = caption.substr(0, m_iBufferPos) + text + caption.substr(m_iBufferPos);
651 :
652 0 : UpdateText(m_iBufferPos, m_iBufferPos, m_iBufferPos+1);
653 :
654 0 : m_iBufferPos += static_cast<int>(text.size());
655 0 : UpdateAutoScroll();
656 0 : UpdateBufferPositionSetting();
657 :
658 0 : SendEvent(GUIM_TEXTEDIT, EventNameTextEdit);
659 :
660 0 : return Input::Reaction::HANDLED;
661 0 : }
662 0 : else if (hotkey == "copy" || hotkey == "cut")
663 : {
664 0 : if (m_Readonly && hotkey == "cut")
665 : return Input::Reaction::PASS;
666 :
667 0 : m_WantedX = 0.0f;
668 :
669 0 : if (SelectingText())
670 : {
671 0 : int virtualFrom;
672 0 : int virtualTo;
673 :
674 0 : if (m_iBufferPos_Tail >= m_iBufferPos)
675 : {
676 : virtualFrom = m_iBufferPos;
677 : virtualTo = m_iBufferPos_Tail;
678 : }
679 : else
680 : {
681 0 : virtualFrom = m_iBufferPos_Tail;
682 0 : virtualTo = m_iBufferPos;
683 : }
684 :
685 0 : CStrW text = caption.substr(virtualFrom, virtualTo - virtualFrom);
686 :
687 0 : SDL_SetClipboardText(text.ToUTF8().c_str());
688 :
689 0 : if (hotkey == "cut")
690 : {
691 0 : DeleteCurSelection();
692 0 : UpdateAutoScroll();
693 0 : SendEvent(GUIM_TEXTEDIT, EventNameTextEdit);
694 : }
695 0 : }
696 :
697 0 : return Input::Reaction::HANDLED;
698 : }
699 0 : else if (hotkey == "text.delete.left")
700 : {
701 0 : if (m_Readonly)
702 : return Input::Reaction::PASS;
703 :
704 0 : m_WantedX = 0.0f;
705 :
706 0 : if (SelectingText())
707 0 : DeleteCurSelection();
708 :
709 0 : if (!caption.empty() && m_iBufferPos != 0)
710 : {
711 0 : m_iBufferPos_Tail = m_iBufferPos;
712 0 : CStrW searchString = caption.substr(0, m_iBufferPos);
713 :
714 : // If we are starting in whitespace, adjust position until we get a non whitespace
715 0 : while (m_iBufferPos > 0)
716 : {
717 0 : if (!iswspace(searchString[m_iBufferPos - 1]))
718 : break;
719 :
720 0 : m_iBufferPos--;
721 : }
722 :
723 : // If we end up on a punctuation char we just delete it (treat punct like a word)
724 0 : if (iswpunct(searchString[m_iBufferPos - 1]))
725 0 : m_iBufferPos--;
726 : else
727 : {
728 : // Now we are on a non white space character, adjust position to char after next whitespace char is found
729 0 : while (m_iBufferPos > 0)
730 : {
731 0 : if (iswspace(searchString[m_iBufferPos - 1]) || iswpunct(searchString[m_iBufferPos - 1]))
732 : break;
733 :
734 0 : m_iBufferPos--;
735 : }
736 : }
737 :
738 0 : UpdateBufferPositionSetting();
739 0 : DeleteCurSelection();
740 0 : SendEvent(GUIM_TEXTEDIT, EventNameTextEdit);
741 0 : }
742 0 : UpdateAutoScroll();
743 : return Input::Reaction::HANDLED;
744 : }
745 0 : else if (hotkey == "text.delete.right")
746 : {
747 0 : if (m_Readonly)
748 : return Input::Reaction::PASS;
749 :
750 0 : m_WantedX = 0.0f;
751 :
752 0 : if (SelectingText())
753 0 : DeleteCurSelection();
754 :
755 0 : if (!caption.empty() && m_iBufferPos < static_cast<int>(caption.length()))
756 : {
757 : // Delete the word to the right of the cursor
758 0 : m_iBufferPos_Tail = m_iBufferPos;
759 :
760 : // Delete chars to the right unit we hit whitespace
761 0 : while (++m_iBufferPos < static_cast<int>(caption.length()))
762 : {
763 0 : if (iswspace(caption[m_iBufferPos]) || iswpunct(caption[m_iBufferPos]))
764 : break;
765 : }
766 :
767 : // Eliminate any whitespace behind the word we just deleted
768 0 : while (m_iBufferPos < static_cast<int>(caption.length()))
769 : {
770 0 : if (!iswspace(caption[m_iBufferPos]))
771 : break;
772 :
773 0 : ++m_iBufferPos;
774 : }
775 0 : UpdateBufferPositionSetting();
776 0 : DeleteCurSelection();
777 : }
778 0 : UpdateAutoScroll();
779 0 : SendEvent(GUIM_TEXTEDIT, EventNameTextEdit);
780 : return Input::Reaction::HANDLED;
781 : }
782 0 : else if (hotkey == "text.move.left")
783 : {
784 0 : m_WantedX = 0.0f;
785 :
786 0 : if (shiftKeyPressed || !SelectingText())
787 : {
788 0 : if (!shiftKeyPressed)
789 0 : m_iBufferPos_Tail = -1;
790 0 : else if (!SelectingText())
791 0 : m_iBufferPos_Tail = m_iBufferPos;
792 :
793 0 : if (!caption.empty() && m_iBufferPos != 0)
794 : {
795 0 : CStrW searchString = caption.substr(0, m_iBufferPos);
796 :
797 : // If we are starting in whitespace, adjust position until we get a non whitespace
798 0 : while (m_iBufferPos > 0)
799 : {
800 0 : if (!iswspace(searchString[m_iBufferPos - 1]))
801 : break;
802 :
803 0 : m_iBufferPos--;
804 : }
805 :
806 : // If we end up on a puctuation char we just select it (treat punct like a word)
807 0 : if (iswpunct(searchString[m_iBufferPos - 1]))
808 0 : m_iBufferPos--;
809 : else
810 : {
811 : // Now we are on a non white space character, adjust position to char after next whitespace char is found
812 0 : while (m_iBufferPos > 0)
813 : {
814 0 : if (iswspace(searchString[m_iBufferPos - 1]) || iswpunct(searchString[m_iBufferPos - 1]))
815 : break;
816 :
817 0 : m_iBufferPos--;
818 : }
819 : }
820 0 : }
821 : }
822 : else
823 : {
824 0 : if (m_iBufferPos_Tail < m_iBufferPos)
825 0 : m_iBufferPos = m_iBufferPos_Tail;
826 :
827 0 : m_iBufferPos_Tail = -1;
828 : }
829 :
830 0 : UpdateBufferPositionSetting();
831 0 : UpdateAutoScroll();
832 :
833 : return Input::Reaction::HANDLED;
834 : }
835 0 : else if (hotkey == "text.move.right")
836 : {
837 0 : m_WantedX = 0.0f;
838 :
839 0 : if (shiftKeyPressed || !SelectingText())
840 : {
841 0 : if (!shiftKeyPressed)
842 0 : m_iBufferPos_Tail = -1;
843 0 : else if (!SelectingText())
844 0 : m_iBufferPos_Tail = m_iBufferPos;
845 :
846 0 : if (!caption.empty() && m_iBufferPos < static_cast<int>(caption.length()))
847 : {
848 : // Select chars to the right until we hit whitespace
849 0 : while (++m_iBufferPos < static_cast<int>(caption.length()))
850 : {
851 0 : if (iswspace(caption[m_iBufferPos]) || iswpunct(caption[m_iBufferPos]))
852 : break;
853 : }
854 :
855 : // Also select any whitespace following the word we just selected
856 0 : while (m_iBufferPos < static_cast<int>(caption.length()))
857 : {
858 0 : if (!iswspace(caption[m_iBufferPos]))
859 : break;
860 :
861 0 : ++m_iBufferPos;
862 : }
863 : }
864 : }
865 : else
866 : {
867 0 : if (m_iBufferPos_Tail > m_iBufferPos)
868 0 : m_iBufferPos = m_iBufferPos_Tail;
869 :
870 0 : m_iBufferPos_Tail = -1;
871 : }
872 :
873 0 : UpdateBufferPositionSetting();
874 0 : UpdateAutoScroll();
875 :
876 : return Input::Reaction::HANDLED;
877 : }
878 :
879 : return Input::Reaction::PASS;
880 0 : }
881 :
882 0 : void CInput::ResetStates()
883 : {
884 0 : IGUIObject::ResetStates();
885 0 : IGUIScrollBarOwner::ResetStates();
886 0 : }
887 :
888 0 : void CInput::HandleMessage(SGUIMessage& Message)
889 : {
890 0 : IGUIObject::HandleMessage(Message);
891 0 : if (m_ScrollBar)
892 0 : IGUIScrollBarOwner::HandleMessage(Message);
893 :
894 : // Cleans up operator[] usage.
895 0 : const CStrW& caption = *m_Caption;
896 :
897 0 : switch (Message.type)
898 : {
899 0 : case GUIM_SETTINGS_UPDATED:
900 0 : {
901 : // Update scroll-bar
902 : // TODO Gee: (2004-09-01) Is this really updated each time it should?
903 0 : if (m_ScrollBar &&
904 0 : (Message.value == "size" ||
905 0 : Message.value == "z" ||
906 0 : Message.value == "absolute"))
907 : {
908 0 : GetScrollBar(0).SetX(GetActualSize().right);
909 0 : GetScrollBar(0).SetY(GetActualSize().top);
910 0 : GetScrollBar(0).SetZ(GetBufferedZ());
911 0 : GetScrollBar(0).SetLength(GetActualSize().bottom - GetActualSize().top);
912 : }
913 :
914 : // Update scrollbar
915 0 : if (Message.value == "scrollbar_style")
916 0 : GetScrollBar(0).SetScrollBarStyle(m_ScrollBarStyle);
917 :
918 0 : if (Message.value == "buffer_position")
919 : {
920 0 : m_iBufferPos = m_MaxLength != 0 ? std::min(m_MaxLength, m_BufferPosition) : m_BufferPosition;
921 0 : m_iBufferPos_Tail = -1; // position change resets selection
922 : }
923 :
924 0 : if (Message.value == "size" ||
925 0 : Message.value == "z" ||
926 0 : Message.value == "font" ||
927 0 : Message.value == "absolute" ||
928 0 : Message.value == "caption" ||
929 0 : Message.value == "scrollbar" ||
930 0 : Message.value == "scrollbar_style")
931 : {
932 0 : UpdateText();
933 : }
934 :
935 0 : if (Message.value == "multiline")
936 : {
937 0 : if (!m_MultiLine)
938 0 : GetScrollBar(0).SetLength(0.f);
939 : else
940 0 : GetScrollBar(0).SetLength(GetActualSize().bottom - GetActualSize().top);
941 :
942 0 : UpdateText();
943 : }
944 :
945 0 : if (Message.value == "placeholder_text" ||
946 0 : Message.value == "size" ||
947 0 : Message.value == "font" ||
948 0 : Message.value == "z" ||
949 0 : Message.value == "text_valign")
950 : {
951 0 : m_GeneratedPlaceholderTextValid = false;
952 : }
953 :
954 0 : UpdateAutoScroll();
955 :
956 0 : break;
957 : }
958 0 : case GUIM_MOUSE_PRESS_LEFT:
959 0 : {
960 : // Check if we're selecting the scrollbar
961 0 : if (m_ScrollBar &&
962 0 : m_MultiLine &&
963 0 : GetScrollBar(0).GetStyle())
964 : {
965 0 : if (m_pGUI.GetMousePos().X > GetActualSize().right - GetScrollBar(0).GetStyle()->m_Width)
966 : break;
967 : }
968 :
969 0 : if (m_ComposingText)
970 : break;
971 :
972 : // Okay, this section is about pressing the mouse and
973 : // choosing where the point should be placed. For
974 : // instance, if we press between a and b, the point
975 : // should of course be placed accordingly. Other
976 : // special cases are handled like the input box norms.
977 0 : if (g_scancodes[SDL_SCANCODE_LSHIFT] || g_scancodes[SDL_SCANCODE_RSHIFT])
978 0 : m_iBufferPos = GetMouseHoveringTextPosition();
979 : else
980 0 : m_iBufferPos = m_iBufferPos_Tail = GetMouseHoveringTextPosition();
981 :
982 0 : m_SelectingText = true;
983 :
984 0 : UpdateAutoScroll();
985 :
986 : // If we immediately release the button it will just be seen as a click
987 : // for the user though.
988 0 : break;
989 : }
990 0 : case GUIM_MOUSE_DBLCLICK_LEFT:
991 0 : {
992 0 : if (m_ComposingText)
993 : break;
994 :
995 0 : if (caption.empty())
996 : break;
997 :
998 0 : m_iBufferPos = m_iBufferPos_Tail = GetMouseHoveringTextPosition();
999 :
1000 0 : if (m_iBufferPos >= (int)caption.length())
1001 0 : m_iBufferPos = m_iBufferPos_Tail = caption.length() - 1;
1002 :
1003 : // See if we are clicking over whitespace
1004 0 : if (iswspace(caption[m_iBufferPos]))
1005 : {
1006 : // see if we are in a section of whitespace greater than one character
1007 0 : if ((m_iBufferPos + 1 < (int) caption.length() && iswspace(caption[m_iBufferPos + 1])) ||
1008 0 : (m_iBufferPos - 1 > 0 && iswspace(caption[m_iBufferPos - 1])))
1009 : {
1010 : //
1011 : // We are clicking in an area with more than one whitespace character
1012 : // so we select both the word to the left and then the word to the right
1013 : //
1014 : // [1] First the left
1015 : // skip the whitespace
1016 0 : while (m_iBufferPos > 0)
1017 : {
1018 0 : if (!iswspace(caption[m_iBufferPos - 1]))
1019 : break;
1020 :
1021 0 : m_iBufferPos--;
1022 : }
1023 : // now go until we hit white space or punctuation
1024 0 : while (m_iBufferPos > 0)
1025 : {
1026 0 : if (iswspace(caption[m_iBufferPos - 1]))
1027 : break;
1028 :
1029 0 : m_iBufferPos--;
1030 :
1031 0 : if (iswpunct(caption[m_iBufferPos]))
1032 : break;
1033 : }
1034 :
1035 : // [2] Then the right
1036 : // go right until we are not in whitespace
1037 0 : while (++m_iBufferPos_Tail < static_cast<int>(caption.length()))
1038 : {
1039 0 : if (!iswspace(caption[m_iBufferPos_Tail]))
1040 : break;
1041 : }
1042 :
1043 0 : if (m_iBufferPos_Tail == static_cast<int>(caption.length()))
1044 : break;
1045 :
1046 : // now go to the right until we hit whitespace or punctuation
1047 0 : while (++m_iBufferPos_Tail < static_cast<int>(caption.length()))
1048 : {
1049 0 : if (iswspace(caption[m_iBufferPos_Tail]) || iswpunct(caption[m_iBufferPos_Tail]))
1050 : break;
1051 : }
1052 : }
1053 : else
1054 : {
1055 : // single whitespace so select word to the right
1056 0 : while (++m_iBufferPos_Tail < static_cast<int>(caption.length()))
1057 : {
1058 0 : if (!iswspace(caption[m_iBufferPos_Tail]))
1059 : break;
1060 : }
1061 :
1062 0 : if (m_iBufferPos_Tail == static_cast<int>(caption.length()))
1063 : break;
1064 :
1065 : // Don't include the leading whitespace
1066 0 : m_iBufferPos = m_iBufferPos_Tail;
1067 :
1068 : // now go to the right until we hit whitespace or punctuation
1069 0 : while (++m_iBufferPos_Tail < static_cast<int>(caption.length()))
1070 : {
1071 0 : if (iswspace(caption[m_iBufferPos_Tail]) || iswpunct(caption[m_iBufferPos_Tail]))
1072 : break;
1073 : }
1074 : }
1075 : }
1076 : else
1077 : {
1078 : // clicked on non-whitespace so select current word
1079 : // go until we hit white space or punctuation
1080 0 : while (m_iBufferPos > 0)
1081 : {
1082 0 : if (iswspace(caption[m_iBufferPos - 1]))
1083 : break;
1084 :
1085 0 : m_iBufferPos--;
1086 :
1087 0 : if (iswpunct(caption[m_iBufferPos]))
1088 : break;
1089 : }
1090 : // go to the right until we hit whitespace or punctuation
1091 0 : while (++m_iBufferPos_Tail < static_cast<int>(caption.length()))
1092 0 : if (iswspace(caption[m_iBufferPos_Tail]) || iswpunct(caption[m_iBufferPos_Tail]))
1093 : break;
1094 : }
1095 0 : UpdateAutoScroll();
1096 0 : break;
1097 : }
1098 0 : case GUIM_MOUSE_RELEASE_LEFT:
1099 0 : {
1100 0 : if (m_SelectingText)
1101 0 : m_SelectingText = false;
1102 : break;
1103 : }
1104 0 : case GUIM_MOUSE_MOTION:
1105 0 : {
1106 : // If we just pressed down and started to move before releasing
1107 : // this is one way of selecting larger portions of text.
1108 0 : if (m_SelectingText)
1109 : {
1110 : // Actually, first we need to re-check that the mouse button is
1111 : // really pressed (it can be released while outside the control.
1112 0 : if (!g_mouse_buttons[SDL_BUTTON_LEFT])
1113 0 : m_SelectingText = false;
1114 : else
1115 0 : m_iBufferPos = GetMouseHoveringTextPosition();
1116 0 : UpdateAutoScroll();
1117 : }
1118 : break;
1119 : }
1120 0 : case GUIM_LOAD:
1121 0 : {
1122 0 : GetScrollBar(0).SetX(GetActualSize().right);
1123 0 : GetScrollBar(0).SetY(GetActualSize().top);
1124 0 : GetScrollBar(0).SetZ(GetBufferedZ());
1125 0 : GetScrollBar(0).SetLength(GetActualSize().bottom - GetActualSize().top);
1126 0 : GetScrollBar(0).SetScrollBarStyle(m_ScrollBarStyle);
1127 :
1128 0 : UpdateText();
1129 0 : UpdateAutoScroll();
1130 :
1131 0 : break;
1132 : }
1133 0 : case GUIM_GOT_FOCUS:
1134 0 : {
1135 0 : m_iBufferPos = 0;
1136 0 : m_PrevTime = 0.0;
1137 0 : m_CursorVisState = false;
1138 :
1139 0 : ResetActiveHotkeys();
1140 :
1141 : // Tell the IME where to draw the candidate list
1142 0 : SDL_Rect rect;
1143 0 : rect.h = GetActualSize().GetSize().Height;
1144 0 : rect.w = GetActualSize().GetSize().Width;
1145 0 : rect.x = GetActualSize().TopLeft().X;
1146 0 : rect.y = GetActualSize().TopLeft().Y;
1147 0 : SDL_SetTextInputRect(&rect);
1148 0 : SDL_StartTextInput();
1149 0 : break;
1150 : }
1151 0 : case GUIM_LOST_FOCUS:
1152 0 : {
1153 0 : if (m_ComposingText)
1154 : {
1155 : // Simulate a final text editing event to clear the composition
1156 0 : SDL_Event ev{};
1157 0 : ev.type = SDL_TEXTEDITING;
1158 0 : ev.edit.length = 0;
1159 0 : ev.edit.start = 0;
1160 0 : ev.edit.text[0] = '\0';
1161 0 : ManuallyHandleKeys(ev);
1162 : }
1163 0 : SDL_StopTextInput();
1164 :
1165 0 : m_iBufferPos = -1;
1166 0 : m_iBufferPos_Tail = -1;
1167 0 : break;
1168 : }
1169 : default:
1170 : {
1171 : break;
1172 : }
1173 : }
1174 0 : UpdateBufferPositionSetting();
1175 0 : }
1176 :
1177 0 : void CInput::HandleSizeChanged()
1178 : {
1179 : // If an ancestor's size changed, this will let us intercept the change and
1180 : // update our scrollbar positions
1181 :
1182 0 : IGUIObject::HandleSizeChanged();
1183 :
1184 0 : if (m_ScrollBar)
1185 : {
1186 0 : GetScrollBar(0).SetX(GetActualSize().right);
1187 0 : GetScrollBar(0).SetY(GetActualSize().top);
1188 0 : GetScrollBar(0).SetZ(GetBufferedZ());
1189 0 : GetScrollBar(0).SetLength(GetActualSize().bottom - GetActualSize().top);
1190 : }
1191 :
1192 0 : m_GeneratedPlaceholderTextValid = false;
1193 0 : }
1194 :
1195 0 : void CInput::DrawContent(CCanvas2D& canvas)
1196 : {
1197 0 : if (m_CursorBlinkRate > 0.0)
1198 : {
1199 : // check if the cursor visibility state needs to be changed
1200 0 : double currTime = timer_Time();
1201 0 : if (currTime - m_PrevTime >= m_CursorBlinkRate)
1202 : {
1203 0 : m_CursorVisState = !m_CursorVisState;
1204 0 : m_PrevTime = currTime;
1205 : }
1206 : }
1207 : else
1208 : // should always be visible
1209 0 : m_CursorVisState = true;
1210 :
1211 0 : CStrIntern font_name(m_Font->ToUTF8());
1212 :
1213 0 : wchar_t mask_char = L'*';
1214 0 : if (m_Mask && m_MaskChar->length() > 0)
1215 0 : mask_char = (*m_MaskChar)[0];
1216 :
1217 0 : m_pGUI.DrawSprite(m_Sprite, canvas, GetActualSize());
1218 :
1219 0 : float scroll = 0.f;
1220 0 : if (m_ScrollBar && m_MultiLine)
1221 0 : scroll = GetScrollBar(0).GetPos();
1222 :
1223 0 : CFontMetrics font(font_name);
1224 :
1225 : // These are useful later.
1226 0 : int VirtualFrom, VirtualTo;
1227 :
1228 0 : if (m_iBufferPos_Tail >= m_iBufferPos)
1229 : {
1230 : VirtualFrom = m_iBufferPos;
1231 : VirtualTo = m_iBufferPos_Tail;
1232 : }
1233 : else
1234 : {
1235 0 : VirtualFrom = m_iBufferPos_Tail;
1236 0 : VirtualTo = m_iBufferPos;
1237 : }
1238 :
1239 : // Get the height of this font.
1240 0 : const float h{font.GetCapHeight()};
1241 0 : const float ls{font.GetHeight()};
1242 :
1243 0 : CTextRenderer textRenderer;
1244 0 : textRenderer.SetCurrentFont(font_name, CStrIntern{});
1245 :
1246 0 : textRenderer.Translate(
1247 0 : (float)(int)(GetActualSize().left) + m_BufferZone,
1248 0 : (float)(int)(GetActualSize().top + h) + m_BufferZone);
1249 :
1250 : // U+FE33: PRESENTATION FORM FOR VERTICAL LOW LINE
1251 : // (sort of like a | which is aligned to the left of most characters)
1252 :
1253 0 : float buffered_y = -scroll + m_BufferZone;
1254 :
1255 : // When selecting larger areas, we need to draw a rectangle box
1256 : // around it, and this is to keep track of where the box
1257 : // started, because we need to follow the iteration until we
1258 : // reach the end, before we can actually draw it.
1259 0 : bool drawing_box = false;
1260 0 : float box_x = 0.f;
1261 :
1262 0 : float x_pointer = 0.f;
1263 :
1264 : // If we have a selecting box (i.e. when you have selected letters, not just when
1265 : // the pointer is between two letters) we need to process all letters once
1266 : // before we do it the second time and render all the text. We can't do it
1267 : // in the same loop because text will have been drawn, so it will disappear when
1268 : // drawn behind the text that has already been drawn. Confusing, well it's necessary
1269 : // (I think).
1270 :
1271 0 : if (SelectingText())
1272 : {
1273 : // Now m_iBufferPos_Tail can be of both sides of m_iBufferPos,
1274 : // just like you can select from right to left, as you can
1275 : // left to right. Is there a difference? Yes, the pointer
1276 : // be placed accordingly, so that if you select shift and
1277 : // expand this selection, it will expand on appropriate side.
1278 : // Anyway, since the drawing procedure needs "To" to be
1279 : // greater than from, we need virtual values that might switch
1280 : // place.
1281 0 : int virtualFrom = 0;
1282 0 : int virtualTo = 0;
1283 :
1284 0 : if (m_iBufferPos_Tail >= m_iBufferPos)
1285 : {
1286 : virtualFrom = m_iBufferPos;
1287 : virtualTo = m_iBufferPos_Tail;
1288 : }
1289 : else
1290 : {
1291 0 : virtualFrom = m_iBufferPos_Tail;
1292 0 : virtualTo = m_iBufferPos;
1293 : }
1294 :
1295 :
1296 0 : bool done = false;
1297 0 : for (std::list<SRow>::const_iterator it = m_CharacterPositions.begin();
1298 0 : it != m_CharacterPositions.end();
1299 0 : ++it, buffered_y += ls, x_pointer = 0.f)
1300 : {
1301 0 : if (m_MultiLine && buffered_y > GetActualSize().GetHeight())
1302 : break;
1303 :
1304 : // We might as well use 'i' here to iterate, because we need it
1305 : // (often compared against ints, so don't make it size_t)
1306 0 : for (int i = 0; i < (int)it->m_ListOfX.size() + 2; ++i)
1307 : {
1308 0 : if (it->m_ListStart + i == virtualFrom)
1309 : {
1310 : // we won't actually draw it now, because we don't
1311 : // know the width of each glyph to that position.
1312 : // we need to go along with the iteration, and
1313 : // make a mark where the box started:
1314 0 : drawing_box = true; // will turn false when finally rendered.
1315 :
1316 : // Get current x position
1317 0 : box_x = x_pointer;
1318 : }
1319 :
1320 0 : const bool at_end = (i == (int)it->m_ListOfX.size() + 1);
1321 :
1322 0 : if (drawing_box && (it->m_ListStart + i == virtualTo || at_end))
1323 : {
1324 : // Depending on if it's just a row change, or if it's
1325 : // the end of the select box, do slightly different things.
1326 0 : if (at_end)
1327 : {
1328 0 : if (it->m_ListStart + i != virtualFrom)
1329 : // and actually add a white space! yes, this is done in any common input
1330 0 : x_pointer += font.GetCharacterWidth(L' ');
1331 : }
1332 : else
1333 : {
1334 : drawing_box = false;
1335 : done = true;
1336 : }
1337 :
1338 0 : CRect rect;
1339 : // Set 'rect' depending on if it's a multiline control, or a one-line control
1340 0 : if (m_MultiLine)
1341 : {
1342 0 : rect = CRect(
1343 0 : GetActualSize().left + box_x + m_BufferZone,
1344 0 : GetActualSize().top + buffered_y + (h - ls) / 2,
1345 0 : GetActualSize().left + x_pointer + m_BufferZone,
1346 0 : GetActualSize().top + buffered_y + (h + ls) / 2);
1347 :
1348 0 : if (rect.bottom < GetActualSize().top)
1349 0 : continue;
1350 :
1351 0 : if (rect.top < GetActualSize().top)
1352 0 : rect.top = GetActualSize().top;
1353 :
1354 0 : if (rect.bottom > GetActualSize().bottom)
1355 0 : rect.bottom = GetActualSize().bottom;
1356 : }
1357 : else // if one-line
1358 : {
1359 0 : rect = CRect(
1360 0 : GetActualSize().left + box_x + m_BufferZone - m_HorizontalScroll,
1361 0 : GetActualSize().top + buffered_y + (h - ls) / 2,
1362 0 : GetActualSize().left + x_pointer + m_BufferZone - m_HorizontalScroll,
1363 0 : GetActualSize().top + buffered_y + (h + ls) / 2);
1364 :
1365 0 : if (rect.left < GetActualSize().left)
1366 0 : rect.left = GetActualSize().left;
1367 :
1368 0 : if (rect.right > GetActualSize().right)
1369 0 : rect.right = GetActualSize().right;
1370 : }
1371 :
1372 0 : m_pGUI.DrawSprite(m_SpriteSelectArea, canvas, rect);
1373 : }
1374 :
1375 0 : if (i < (int)it->m_ListOfX.size())
1376 : {
1377 0 : if (!m_Mask)
1378 0 : x_pointer += font.GetCharacterWidth((*m_Caption)[it->m_ListStart + i]);
1379 : else
1380 0 : x_pointer += font.GetCharacterWidth(mask_char);
1381 : }
1382 : }
1383 :
1384 0 : if (done)
1385 : break;
1386 :
1387 : // If we're about to draw a box, and all of a sudden changes
1388 : // line, we need to draw that line's box, and then reset
1389 : // the box drawing to the beginning of the new line.
1390 0 : if (drawing_box)
1391 0 : box_x = 0.f;
1392 : }
1393 : }
1394 :
1395 : // Reset some from previous run
1396 0 : buffered_y = -scroll;
1397 :
1398 : // Setup initial color (then it might change and change back, when drawing selected area)
1399 0 : textRenderer.SetCurrentColor(m_TextColor);
1400 :
1401 0 : bool using_selected_color = false;
1402 :
1403 0 : for (std::list<SRow>::const_iterator it = m_CharacterPositions.begin();
1404 0 : it != m_CharacterPositions.end();
1405 0 : ++it, buffered_y += ls)
1406 : {
1407 0 : if (buffered_y + m_BufferZone >= -ls || !m_MultiLine)
1408 : {
1409 0 : if (m_MultiLine && buffered_y + m_BufferZone > GetActualSize().GetHeight())
1410 : break;
1411 :
1412 0 : const CVector2D savedTranslate = textRenderer.GetTranslate();
1413 :
1414 : // Text must always be drawn in integer values. So we have to convert scroll
1415 0 : if (m_MultiLine)
1416 0 : textRenderer.Translate(0.f, -(float)(int)scroll);
1417 : else
1418 0 : textRenderer.Translate(-(float)(int)m_HorizontalScroll, 0.f);
1419 :
1420 : // We might as well use 'i' here, because we need it
1421 : // (often compared against ints, so don't make it size_t)
1422 0 : for (int i = 0; i < (int)it->m_ListOfX.size() + 1; ++i)
1423 : {
1424 0 : if (!m_MultiLine && i < (int)it->m_ListOfX.size())
1425 : {
1426 0 : if (it->m_ListOfX[i] - m_HorizontalScroll < -m_BufferZone)
1427 : {
1428 : // We still need to translate the OpenGL matrix
1429 0 : if (i == 0)
1430 0 : textRenderer.Translate(it->m_ListOfX[i], 0.f);
1431 : else
1432 0 : textRenderer.Translate(it->m_ListOfX[i] - it->m_ListOfX[i - 1], 0.f);
1433 :
1434 0 : continue;
1435 : }
1436 : }
1437 :
1438 : // End of selected area, change back color
1439 0 : if (SelectingText() && it->m_ListStart + i == VirtualTo)
1440 : {
1441 0 : using_selected_color = false;
1442 0 : textRenderer.SetCurrentColor(m_TextColor);
1443 : }
1444 :
1445 : // selecting only one, then we need only to draw a cursor.
1446 0 : if (i != (int)it->m_ListOfX.size() && it->m_ListStart + i == m_iBufferPos && m_CursorVisState && !m_Readonly)
1447 0 : textRenderer.Put(0.0f, 0.0f, L"_");
1448 :
1449 : // Drawing selected area
1450 0 : if (SelectingText() &&
1451 0 : it->m_ListStart + i >= VirtualFrom &&
1452 0 : it->m_ListStart + i < VirtualTo &&
1453 : !using_selected_color)
1454 : {
1455 0 : using_selected_color = true;
1456 0 : textRenderer.SetCurrentColor(m_TextColorSelected);
1457 : }
1458 :
1459 0 : if (i != (int)it->m_ListOfX.size())
1460 : {
1461 0 : if (!m_Mask)
1462 0 : textRenderer.PrintfAdvance(L"%lc", (*m_Caption)[it->m_ListStart + i]);
1463 : else
1464 0 : textRenderer.PrintfAdvance(L"%lc", mask_char);
1465 : }
1466 :
1467 : // check it's now outside a one-liner, then we'll break
1468 0 : if (!m_MultiLine && i < (int)it->m_ListOfX.size() &&
1469 0 : it->m_ListOfX[i] - m_HorizontalScroll > GetActualSize().GetWidth() - m_BufferZone)
1470 : break;
1471 : }
1472 :
1473 0 : if (it->m_ListStart + (int)it->m_ListOfX.size() == m_iBufferPos)
1474 : {
1475 0 : textRenderer.SetCurrentColor(m_TextColor);
1476 0 : if (m_CursorVisState && !m_Readonly)
1477 0 : textRenderer.PutAdvance(L"_");
1478 :
1479 0 : if (using_selected_color)
1480 0 : textRenderer.SetCurrentColor(m_TextColorSelected);
1481 : }
1482 :
1483 0 : textRenderer.ResetTranslate(savedTranslate);
1484 : }
1485 :
1486 0 : textRenderer.Translate(0.f, ls);
1487 : }
1488 :
1489 0 : canvas.DrawText(textRenderer);
1490 0 : }
1491 :
1492 0 : void CInput::Draw(CCanvas2D& canvas)
1493 : {
1494 : // We'll have to setup clipping manually, since we're doing the rendering manually.
1495 0 : CRect cliparea = m_VisibleArea ? m_VisibleArea : GetActualSize();
1496 :
1497 : // First we'll figure out the clipping area, which is the cached actual size
1498 : // substracted by an optional scrollbar
1499 0 : if (m_ScrollBar)
1500 : {
1501 : // substract scrollbar from cliparea
1502 0 : if (cliparea.right > GetScrollBar(0).GetOuterRect().left &&
1503 0 : cliparea.right <= GetScrollBar(0).GetOuterRect().right)
1504 0 : cliparea.right = GetScrollBar(0).GetOuterRect().left;
1505 :
1506 0 : if (cliparea.left >= GetScrollBar(0).GetOuterRect().left &&
1507 0 : cliparea.left < GetScrollBar(0).GetOuterRect().right)
1508 0 : cliparea.left = GetScrollBar(0).GetOuterRect().right;
1509 : }
1510 :
1511 0 : const bool isClipped = cliparea != CRect();
1512 0 : if (isClipped)
1513 : {
1514 0 : if (cliparea.GetWidth() <= 0.0f || cliparea.GetHeight() <= 0.0f)
1515 0 : return;
1516 0 : CCanvas2D::ScopedScissor scopedScissor(canvas, cliparea);
1517 0 : DrawContent(canvas);
1518 0 : }
1519 : else
1520 0 : DrawContent(canvas);
1521 :
1522 0 : if (m_Caption->empty() && !m_PlaceholderText->GetRawString().empty())
1523 0 : DrawPlaceholderText(canvas, cliparea);
1524 :
1525 : // Draw scrollbars on top of the content
1526 0 : if (m_ScrollBar && m_MultiLine)
1527 0 : IGUIScrollBarOwner::Draw(canvas);
1528 :
1529 : // Draw the overlays last
1530 0 : m_pGUI.DrawSprite(m_SpriteOverlay, canvas, GetActualSize(), m_VisibleArea);
1531 : }
1532 :
1533 0 : void CInput::DrawPlaceholderText(CCanvas2D& canvas, const CRect& clipping)
1534 : {
1535 0 : if (!m_GeneratedPlaceholderTextValid)
1536 0 : SetupGeneratedPlaceholderText();
1537 :
1538 0 : m_GeneratedPlaceholderText.Draw(m_pGUI, canvas, m_PlaceholderColor, GetActualSize().TopLeft(), clipping);
1539 0 : }
1540 :
1541 0 : void CInput::UpdateText(int from, int to_before, int to_after)
1542 : {
1543 0 : CStrW& caption = m_Caption.GetMutable();
1544 :
1545 0 : if (m_MaxLength != 0 && caption.length() > static_cast<size_t>(m_MaxLength))
1546 0 : caption.erase(m_MaxLength);
1547 :
1548 0 : CStrIntern font_name(m_Font->ToUTF8());
1549 :
1550 0 : wchar_t mask_char = L'*';
1551 0 : if (m_Mask && m_MaskChar->length() > 0)
1552 0 : mask_char = (*m_MaskChar)[0];
1553 :
1554 : // Ensure positions are valid after caption changes
1555 0 : m_iBufferPos = std::min(m_iBufferPos, static_cast<int>(caption.size()));
1556 0 : m_iBufferPos_Tail = std::min(m_iBufferPos_Tail, static_cast<int>(caption.size()));
1557 0 : UpdateBufferPositionSetting();
1558 :
1559 0 : if (font_name.empty())
1560 : {
1561 : // Destroy everything stored, there's no font, so there can be no data.
1562 0 : m_CharacterPositions.clear();
1563 0 : return;
1564 : }
1565 :
1566 0 : SRow row;
1567 0 : row.m_ListStart = 0;
1568 :
1569 0 : int to = 0; // make sure it's initialized
1570 :
1571 0 : if (to_before == -1)
1572 0 : to = static_cast<int>(caption.length());
1573 :
1574 0 : CFontMetrics font(font_name);
1575 :
1576 0 : std::list<SRow>::iterator current_line;
1577 :
1578 : // Used to ... TODO
1579 0 : int check_point_row_start = -1;
1580 0 : int check_point_row_end = -1;
1581 :
1582 : // Reset
1583 0 : if (from == 0 && to_before == -1)
1584 : {
1585 0 : m_CharacterPositions.clear();
1586 0 : current_line = m_CharacterPositions.begin();
1587 : }
1588 : else
1589 : {
1590 0 : ENSURE(to_before != -1);
1591 :
1592 0 : std::list<SRow>::iterator destroy_row_from;
1593 0 : std::list<SRow>::iterator destroy_row_to;
1594 : // Used to check if the above has been set to anything,
1595 : // previously a comparison like:
1596 : // destroy_row_from == std::list<SRow>::iterator()
1597 : // ... was used, but it didn't work with GCC.
1598 0 : bool destroy_row_from_used = false;
1599 0 : bool destroy_row_to_used = false;
1600 :
1601 : // Iterate, and remove everything between 'from' and 'to_before'
1602 : // actually remove the entire lines they are on, it'll all have
1603 : // to be redone. And when going along, we'll delete a row at a time
1604 : // when continuing to see how much more after 'to' we need to remake.
1605 0 : for (std::list<SRow>::iterator it = m_CharacterPositions.begin();
1606 0 : it != m_CharacterPositions.end();
1607 0 : ++it)
1608 : {
1609 0 : if (!destroy_row_from_used && it->m_ListStart > from)
1610 : {
1611 : // Destroy the previous line, and all to 'to_before'
1612 0 : destroy_row_from = it;
1613 0 : --destroy_row_from;
1614 :
1615 0 : destroy_row_from_used = true;
1616 :
1617 : // For the rare case that we might remove characters to a word
1618 : // so that it suddenly fits on the previous row,
1619 : // we need to by standards re-do the whole previous line too
1620 : // (if one exists)
1621 0 : if (destroy_row_from != m_CharacterPositions.begin())
1622 0 : --destroy_row_from;
1623 : }
1624 :
1625 0 : if (!destroy_row_to_used && it->m_ListStart > to_before)
1626 : {
1627 0 : destroy_row_to = it;
1628 0 : destroy_row_to_used = true;
1629 :
1630 : // If it isn't the last row, we'll add another row to delete,
1631 : // just so we can see if the last restorted line is
1632 : // identical to what it was before. If it isn't, then we'll
1633 : // have to continue.
1634 : // 'check_point_row_start' is where we store how the that
1635 : // line looked.
1636 0 : if (destroy_row_to != m_CharacterPositions.end())
1637 : {
1638 0 : check_point_row_start = destroy_row_to->m_ListStart;
1639 0 : check_point_row_end = check_point_row_start + (int)destroy_row_to->m_ListOfX.size();
1640 0 : if (destroy_row_to->m_ListOfX.empty())
1641 0 : ++check_point_row_end;
1642 : }
1643 :
1644 0 : ++destroy_row_to;
1645 : break;
1646 : }
1647 : }
1648 :
1649 0 : if (!destroy_row_from_used)
1650 : {
1651 0 : destroy_row_from = m_CharacterPositions.end();
1652 0 : --destroy_row_from;
1653 :
1654 : // As usual, let's destroy another row back
1655 0 : if (destroy_row_from != m_CharacterPositions.begin())
1656 0 : --destroy_row_from;
1657 :
1658 0 : current_line = destroy_row_from;
1659 : }
1660 :
1661 0 : if (!destroy_row_to_used)
1662 : {
1663 0 : destroy_row_to = m_CharacterPositions.end();
1664 0 : check_point_row_start = -1;
1665 : }
1666 :
1667 : // set 'from' to the row we'll destroy from
1668 : // and 'to' to the row we'll destroy to
1669 0 : from = destroy_row_from->m_ListStart;
1670 :
1671 0 : if (destroy_row_to != m_CharacterPositions.end())
1672 0 : to = destroy_row_to->m_ListStart; // notice it will iterate [from, to), so it will never reach to.
1673 : else
1674 0 : to = static_cast<int>(caption.length());
1675 :
1676 :
1677 : // Setup the first row
1678 0 : row.m_ListStart = destroy_row_from->m_ListStart;
1679 :
1680 0 : std::list<SRow>::iterator temp_it = destroy_row_to;
1681 0 : --temp_it;
1682 :
1683 0 : current_line = m_CharacterPositions.erase(destroy_row_from, destroy_row_to);
1684 :
1685 : // If there has been a change in number of characters
1686 : // we need to change all m_ListStart that comes after
1687 : // the interval we just destroyed. We'll change all
1688 : // values with the delta change of the string length.
1689 0 : int delta = to_after - to_before;
1690 0 : if (delta != 0)
1691 : {
1692 0 : for (std::list<SRow>::iterator it = current_line;
1693 0 : it != m_CharacterPositions.end();
1694 0 : ++it)
1695 0 : it->m_ListStart += delta;
1696 :
1697 : // Update our check point too!
1698 0 : check_point_row_start += delta;
1699 0 : check_point_row_end += delta;
1700 :
1701 0 : if (to != static_cast<int>(caption.length()))
1702 0 : to += delta;
1703 : }
1704 : }
1705 :
1706 0 : int last_word_started = from;
1707 0 : float x_pos = 0.f;
1708 :
1709 : //if (to_before != -1)
1710 : // return;
1711 :
1712 0 : for (int i = from; i < to; ++i)
1713 : {
1714 0 : if (caption[i] == L'\n' && m_MultiLine)
1715 : {
1716 0 : if (i == to-1 && to != static_cast<int>(caption.length()))
1717 : break; // it will be added outside
1718 :
1719 0 : current_line = m_CharacterPositions.insert(current_line, row);
1720 0 : ++current_line;
1721 :
1722 : // Setup the next row:
1723 0 : row.m_ListOfX.clear();
1724 0 : row.m_ListStart = i+1;
1725 0 : x_pos = 0.f;
1726 : }
1727 : else
1728 : {
1729 0 : if (caption[i] == L' '/* || TODO Gee (2004-10-13): the '-' disappears, fix.
1730 : caption[i] == L'-'*/)
1731 0 : last_word_started = i+1;
1732 :
1733 0 : if (!m_Mask)
1734 0 : x_pos += font.GetCharacterWidth(caption[i]);
1735 : else
1736 0 : x_pos += font.GetCharacterWidth(mask_char);
1737 :
1738 0 : if (x_pos >= GetTextAreaWidth() && m_MultiLine)
1739 : {
1740 : // The following decides whether it will word-wrap a word,
1741 : // or if it's only one word on the line, where it has to
1742 : // break the word apart.
1743 0 : if (last_word_started == row.m_ListStart)
1744 : {
1745 0 : last_word_started = i;
1746 0 : row.m_ListOfX.resize(row.m_ListOfX.size() - (i-last_word_started));
1747 : //row.m_ListOfX.push_back(x_pos);
1748 : //continue;
1749 : }
1750 : else
1751 : {
1752 : // regular word-wrap
1753 0 : row.m_ListOfX.resize(row.m_ListOfX.size() - (i-last_word_started+1));
1754 : }
1755 :
1756 : // Now, create a new line:
1757 : // notice: when we enter a newline, you can stand with the cursor
1758 : // both before and after that character, being on different
1759 : // rows. With automatic word-wrapping, that is not possible. Which
1760 : // is intuitively correct.
1761 :
1762 0 : current_line = m_CharacterPositions.insert(current_line, row);
1763 0 : ++current_line;
1764 :
1765 : // Setup the next row:
1766 0 : row.m_ListOfX.clear();
1767 0 : row.m_ListStart = last_word_started;
1768 :
1769 0 : i = last_word_started-1;
1770 :
1771 0 : x_pos = 0.f;
1772 : }
1773 : else
1774 : // Get width of this character:
1775 0 : row.m_ListOfX.push_back(x_pos);
1776 : }
1777 :
1778 : // Check if it's the last iteration, and we're not revising the whole string
1779 : // because in that case, more word-wrapping might be needed.
1780 : // also check if the current line isn't the end
1781 0 : if (to_before != -1 && i == to-1 && current_line != m_CharacterPositions.end())
1782 : {
1783 : // check all rows and see if any existing
1784 0 : if (row.m_ListStart != check_point_row_start)
1785 : {
1786 0 : std::list<SRow>::iterator destroy_row_from;
1787 0 : std::list<SRow>::iterator destroy_row_to;
1788 : // Are used to check if the above has been set to anything,
1789 : // previously a comparison like:
1790 : // destroy_row_from == std::list<SRow>::iterator()
1791 : // was used, but it didn't work with GCC.
1792 0 : bool destroy_row_from_used = false;
1793 0 : bool destroy_row_to_used = false;
1794 :
1795 : // Iterate, and remove everything between 'from' and 'to_before'
1796 : // actually remove the entire lines they are on, it'll all have
1797 : // to be redone. And when going along, we'll delete a row at a time
1798 : // when continuing to see how much more after 'to' we need to remake.
1799 :
1800 0 : for (std::list<SRow>::iterator it = m_CharacterPositions.begin();
1801 0 : it != m_CharacterPositions.end();
1802 0 : ++it)
1803 : {
1804 0 : if (!destroy_row_from_used && it->m_ListStart > check_point_row_start)
1805 : {
1806 : // Destroy the previous line, and all to 'to_before'
1807 : //if (i >= 2)
1808 : // destroy_row_from = it-2;
1809 : //else
1810 : // destroy_row_from = it-1;
1811 : destroy_row_from = it;
1812 : destroy_row_from_used = true;
1813 : //--destroy_row_from;
1814 : }
1815 :
1816 0 : if (!destroy_row_to_used && it->m_ListStart > check_point_row_end)
1817 : {
1818 0 : destroy_row_to = it;
1819 0 : destroy_row_to_used = true;
1820 :
1821 : // If it isn't the last row, we'll add another row to delete,
1822 : // just so we can see if the last restorted line is
1823 : // identical to what it was before. If it isn't, then we'll
1824 : // have to continue.
1825 : // 'check_point_row_start' is where we store how the that
1826 : // line looked.
1827 0 : if (destroy_row_to != m_CharacterPositions.end())
1828 : {
1829 0 : check_point_row_start = destroy_row_to->m_ListStart;
1830 0 : check_point_row_end = check_point_row_start + (int)destroy_row_to->m_ListOfX.size();
1831 0 : if (destroy_row_to->m_ListOfX.empty())
1832 0 : ++check_point_row_end;
1833 : }
1834 : else
1835 : check_point_row_start = check_point_row_end = -1;
1836 :
1837 0 : ++destroy_row_to;
1838 : break;
1839 : }
1840 : }
1841 :
1842 0 : if (!destroy_row_from_used)
1843 : {
1844 0 : destroy_row_from = m_CharacterPositions.end();
1845 0 : --destroy_row_from;
1846 :
1847 0 : current_line = destroy_row_from;
1848 : }
1849 :
1850 0 : if (!destroy_row_to_used)
1851 : {
1852 0 : destroy_row_to = m_CharacterPositions.end();
1853 0 : check_point_row_start = check_point_row_end = -1;
1854 : }
1855 :
1856 0 : if (destroy_row_to != m_CharacterPositions.end())
1857 0 : to = destroy_row_to->m_ListStart; // notice it will iterate [from, to[, so it will never reach to.
1858 : else
1859 0 : to = static_cast<int>(caption.length());
1860 :
1861 :
1862 : // Set current line, new rows will be added before current_line, so
1863 : // we'll choose the destroy_row_to, because it won't be deleted
1864 : // in the coming erase.
1865 0 : current_line = destroy_row_to;
1866 :
1867 0 : m_CharacterPositions.erase(destroy_row_from, destroy_row_to);
1868 : }
1869 : // else, the for loop will end naturally.
1870 : }
1871 : }
1872 : // This is kind of special, when we renew a some lines, then the last
1873 : // one will sometimes end with a space (' '), that really should
1874 : // be omitted when word-wrapping. So we'll check if the last row
1875 : // we'll add has got the same value as the next row.
1876 0 : if (current_line != m_CharacterPositions.end())
1877 : {
1878 0 : if (row.m_ListStart + (int)row.m_ListOfX.size() == current_line->m_ListStart)
1879 0 : row.m_ListOfX.resize(row.m_ListOfX.size()-1);
1880 : }
1881 :
1882 : // add the final row (even if empty)
1883 0 : m_CharacterPositions.insert(current_line, row);
1884 :
1885 0 : if (m_ScrollBar)
1886 : {
1887 0 : GetScrollBar(0).SetScrollRange(m_CharacterPositions.size() * font.GetHeight() + m_BufferZone * 2.f);
1888 0 : GetScrollBar(0).SetScrollSpace(GetActualSize().GetHeight());
1889 : }
1890 0 : }
1891 :
1892 0 : int CInput::GetMouseHoveringTextPosition() const
1893 : {
1894 0 : if (m_CharacterPositions.empty())
1895 : return 0;
1896 :
1897 : // Return position
1898 0 : int retPosition;
1899 :
1900 0 : std::list<SRow>::const_iterator current = m_CharacterPositions.begin();
1901 :
1902 0 : CVector2D mouse = m_pGUI.GetMousePos();
1903 :
1904 0 : if (m_MultiLine)
1905 : {
1906 0 : float scroll = 0.f;
1907 0 : if (m_ScrollBar)
1908 0 : scroll = GetScrollBarPos(0);
1909 :
1910 0 : const CFontMetrics font{CStrIntern{m_Font->ToUTF8()}};
1911 0 : const float spacing{font.GetHeight()};
1912 :
1913 : // Change mouse position relative to text.
1914 0 : mouse -= GetActualSize().TopLeft();
1915 0 : mouse.X -= m_BufferZone;
1916 0 : mouse.Y += scroll - m_BufferZone;
1917 :
1918 0 : int row = (int)((mouse.Y) / spacing);
1919 :
1920 0 : if (row < 0)
1921 : row = 0;
1922 :
1923 0 : if (row > (int)m_CharacterPositions.size()-1)
1924 0 : row = (int)m_CharacterPositions.size()-1;
1925 :
1926 : // TODO Gee (2004-11-21): Okay, I need a 'std::list' for some reasons, but I would really like to
1927 : // be able to get the specific element here. This is hopefully a temporary hack.
1928 :
1929 0 : for (int i = 0; i < row; ++i)
1930 0 : ++current;
1931 : }
1932 : else
1933 : {
1934 : // current is already set to begin,
1935 : // but we'll change the mouse.x to fit our horizontal scrolling
1936 0 : mouse -= GetActualSize().TopLeft();
1937 0 : mouse.X -= m_BufferZone - m_HorizontalScroll;
1938 : // mouse.y is moot
1939 : }
1940 :
1941 0 : retPosition = current->m_ListStart;
1942 :
1943 : // Okay, now loop through the glyphs to find the appropriate X position
1944 0 : float dummy;
1945 0 : retPosition += GetXTextPosition(current, mouse.X, dummy);
1946 :
1947 0 : return retPosition;
1948 : }
1949 :
1950 : // Does not process horizontal scrolling, 'x' must be modified before inputted.
1951 0 : int CInput::GetXTextPosition(const std::list<SRow>::const_iterator& current, const float& x, float& wanted) const
1952 : {
1953 0 : int ret = 0;
1954 0 : float previous = 0.f;
1955 0 : int i = 0;
1956 :
1957 0 : for (std::vector<float>::const_iterator it = current->m_ListOfX.begin();
1958 0 : it != current->m_ListOfX.end();
1959 0 : ++it, ++i)
1960 : {
1961 0 : if (*it >= x)
1962 : {
1963 0 : if (x - previous >= *it - x)
1964 0 : ret += i+1;
1965 : else
1966 : ret += i;
1967 :
1968 : break;
1969 : }
1970 0 : previous = *it;
1971 : }
1972 : // If a position wasn't found, we will assume the last
1973 : // character of that line.
1974 0 : if (i == (int)current->m_ListOfX.size())
1975 : {
1976 0 : ret += i;
1977 0 : wanted = x;
1978 : }
1979 : else
1980 0 : wanted = 0.f;
1981 :
1982 0 : return ret;
1983 : }
1984 :
1985 0 : void CInput::DeleteCurSelection()
1986 : {
1987 0 : int virtualFrom;
1988 0 : int virtualTo;
1989 :
1990 0 : if (m_iBufferPos_Tail >= m_iBufferPos)
1991 : {
1992 : virtualFrom = m_iBufferPos;
1993 : virtualTo = m_iBufferPos_Tail;
1994 : }
1995 : else
1996 : {
1997 0 : virtualFrom = m_iBufferPos_Tail;
1998 0 : virtualTo = m_iBufferPos;
1999 : }
2000 :
2001 : // Silently change.
2002 0 : m_Caption.Set(m_Caption->substr(0, virtualFrom) + m_Caption->substr(virtualTo), false);
2003 :
2004 0 : UpdateText(virtualFrom, virtualTo, virtualFrom);
2005 :
2006 : // Remove selection
2007 0 : m_iBufferPos_Tail = -1;
2008 0 : m_iBufferPos = virtualFrom;
2009 0 : UpdateBufferPositionSetting();
2010 0 : }
2011 :
2012 0 : bool CInput::SelectingText() const
2013 : {
2014 0 : return m_iBufferPos_Tail != -1 &&
2015 0 : m_iBufferPos_Tail != m_iBufferPos;
2016 : }
2017 :
2018 0 : float CInput::GetTextAreaWidth()
2019 : {
2020 0 : if (m_ScrollBar && GetScrollBar(0).GetStyle())
2021 0 : return GetActualSize().GetWidth() - m_BufferZone * 2.f - GetScrollBar(0).GetStyle()->m_Width;
2022 :
2023 0 : return GetActualSize().GetWidth() - m_BufferZone * 2.f;
2024 : }
2025 :
2026 0 : void CInput::UpdateAutoScroll()
2027 : {
2028 : // Autoscrolling up and down
2029 0 : if (m_MultiLine)
2030 : {
2031 0 : if (!m_ScrollBar)
2032 0 : return;
2033 :
2034 0 : const float scroll = GetScrollBar(0).GetPos();
2035 :
2036 0 : const CFontMetrics font{CStrIntern{m_Font->ToUTF8()}};
2037 0 : const float spacing{font.GetHeight()};
2038 :
2039 : // TODO Gee (2004-11-21): Okay, I need a 'std::list' for some reasons, but I would really like to
2040 : // be able to get the specific element here. This is hopefully a temporary hack.
2041 :
2042 0 : std::list<SRow>::iterator current = m_CharacterPositions.begin();
2043 0 : int row = 0;
2044 0 : while (current != m_CharacterPositions.end())
2045 : {
2046 0 : if (m_iBufferPos >= current->m_ListStart &&
2047 0 : m_iBufferPos <= current->m_ListStart + (int)current->m_ListOfX.size())
2048 : break;
2049 :
2050 0 : ++current;
2051 0 : ++row;
2052 : }
2053 :
2054 : // If scrolling down
2055 0 : if (-scroll + static_cast<float>(row + 1) * spacing + m_BufferZone * 2.f > GetActualSize().GetHeight())
2056 : {
2057 : // Scroll so the selected row is shown completely, also with m_BufferZone length to the edge.
2058 0 : GetScrollBar(0).SetPos(static_cast<float>(row + 1) * spacing - GetActualSize().GetHeight() + m_BufferZone * 2.f);
2059 : }
2060 : // If scrolling up
2061 0 : else if (-scroll + (float)row * spacing < 0.f)
2062 : {
2063 : // Scroll so the selected row is shown completely, also with m_BufferZone length to the edge.
2064 0 : GetScrollBar(0).SetPos((float)row * spacing);
2065 : }
2066 : }
2067 : else // autoscrolling left and right
2068 : {
2069 : // Get X position of position:
2070 0 : if (m_CharacterPositions.empty())
2071 : return;
2072 :
2073 0 : float x_position = 0.f;
2074 0 : float x_total = 0.f;
2075 0 : if (!m_CharacterPositions.begin()->m_ListOfX.empty())
2076 : {
2077 :
2078 : // Get position of m_iBufferPos
2079 0 : if ((int)m_CharacterPositions.begin()->m_ListOfX.size() >= m_iBufferPos &&
2080 : m_iBufferPos > 0)
2081 0 : x_position = m_CharacterPositions.begin()->m_ListOfX[m_iBufferPos-1];
2082 :
2083 : // Get complete length:
2084 0 : x_total = m_CharacterPositions.begin()->m_ListOfX[m_CharacterPositions.begin()->m_ListOfX.size()-1];
2085 : }
2086 :
2087 : // Check if outside to the right
2088 0 : if (x_position - m_HorizontalScroll + m_BufferZone * 2.f > GetActualSize().GetWidth())
2089 0 : m_HorizontalScroll = x_position - GetActualSize().GetWidth() + m_BufferZone * 2.f;
2090 :
2091 : // Check if outside to the left
2092 0 : if (x_position - m_HorizontalScroll < 0.f)
2093 0 : m_HorizontalScroll = x_position;
2094 :
2095 : // Check if the text doesn't even fill up to the right edge even though scrolling is done.
2096 0 : if (m_HorizontalScroll != 0.f &&
2097 0 : x_total - m_HorizontalScroll + m_BufferZone * 2.f < GetActualSize().GetWidth())
2098 0 : m_HorizontalScroll = x_total - GetActualSize().GetWidth() + m_BufferZone * 2.f;
2099 :
2100 : // Now this is the fail-safe, if x_total isn't even the length of the control,
2101 : // remove all scrolling
2102 0 : if (x_total + m_BufferZone * 2.f < GetActualSize().GetWidth())
2103 0 : m_HorizontalScroll = 0.f;
2104 : }
2105 : }
|