|
@@ -136,6 +136,17 @@ void UITextEdit::update(bool focusCursor)
|
136
|
136
|
const Size *glyphsSize = m_font->getGlyphsSize();
|
137
|
137
|
int glyph;
|
138
|
138
|
|
|
139
|
+ // update rect size
|
|
140
|
+ if(!m_rect.isValid() || m_textAutoResize) {
|
|
141
|
+ textBoxSize += Size(m_padding.left + m_padding.right, m_padding.top + m_padding.bottom) + m_textOffset.toSize();
|
|
142
|
+ Size size = getSize();
|
|
143
|
+ if(size.width() <= 0 || (m_textAutoResize && !m_textWrap))
|
|
144
|
+ size.setWidth(textBoxSize.width());
|
|
145
|
+ if(size.height() <= 0 || m_textAutoResize)
|
|
146
|
+ size.setHeight(textBoxSize.height());
|
|
147
|
+ setSize(size);
|
|
148
|
+ }
|
|
149
|
+
|
139
|
150
|
// resize just on demand
|
140
|
151
|
if(textLength > (int)m_glyphsCoords.size()) {
|
141
|
152
|
m_glyphsCoords.resize(textLength);
|
|
@@ -144,6 +155,11 @@ void UITextEdit::update(bool focusCursor)
|
144
|
155
|
|
145
|
156
|
Point oldTextAreaOffset = m_textVirtualOffset;
|
146
|
157
|
|
|
158
|
+ if(textBoxSize.width() <= getPaddingRect().width())
|
|
159
|
+ m_textVirtualOffset.x = 0;
|
|
160
|
+ if(textBoxSize.height() <= getPaddingRect().height())
|
|
161
|
+ m_textVirtualOffset.y = 0;
|
|
162
|
+
|
147
|
163
|
// readjust start view area based on cursor position
|
148
|
164
|
m_cursorInRange = false;
|
149
|
165
|
if(focusCursor) {
|
|
@@ -331,8 +347,6 @@ void UITextEdit::setSelection(int start, int end)
|
331
|
347
|
|
332
|
348
|
m_selectionStart = std::min(std::max(start, 0), (int)m_text.length());
|
333
|
349
|
m_selectionEnd = std::min(std::max(end, 0), (int)m_text.length());
|
334
|
|
-
|
335
|
|
- onSelectionChange(getSelection(), m_selectionStart, m_selectionEnd);
|
336
|
350
|
}
|
337
|
351
|
|
338
|
352
|
void UITextEdit::setTextHidden(bool hidden)
|
|
@@ -355,8 +369,9 @@ void UITextEdit::appendText(std::string text)
|
355
|
369
|
if(m_cursorPos >= 0) {
|
356
|
370
|
// replace characters that are now allowed
|
357
|
371
|
if(!m_multiline)
|
358
|
|
- stdext::replace_all(text, "\n", "");
|
359
|
|
- stdext::replace_all(text, "\r", " ");
|
|
372
|
+ stdext::replace_all(text, "\n", " ");
|
|
373
|
+ stdext::replace_all(text, "\r", "");
|
|
374
|
+ stdext::replace_all(text, "\t", " ");
|
360
|
375
|
|
361
|
376
|
if(text.length() > 0) {
|
362
|
377
|
// only add text if textedit can add it
|
|
@@ -371,10 +386,10 @@ void UITextEdit::appendText(std::string text)
|
371
|
386
|
}
|
372
|
387
|
}
|
373
|
388
|
|
374
|
|
- std::string oldText = m_text;
|
375
|
|
- m_text.insert(m_cursorPos, text);
|
|
389
|
+ std::string tmp = m_text;
|
|
390
|
+ tmp.insert(m_cursorPos, text);
|
376
|
391
|
m_cursorPos += text.length();
|
377
|
|
- onTextChange(m_text, oldText);
|
|
392
|
+ setText(tmp);
|
378
|
393
|
}
|
379
|
394
|
}
|
380
|
395
|
}
|
|
@@ -396,26 +411,26 @@ void UITextEdit::appendCharacter(char c)
|
396
|
411
|
|
397
|
412
|
std::string tmp;
|
398
|
413
|
tmp = c;
|
399
|
|
- std::string oldText = m_text;
|
400
|
|
- m_text.insert(m_cursorPos, tmp);
|
|
414
|
+ std::string tmp2 = m_text;
|
|
415
|
+ tmp2.insert(m_cursorPos, tmp);
|
401
|
416
|
m_cursorPos++;
|
402
|
|
- onTextChange(m_text, oldText);
|
|
417
|
+ setText(tmp2);
|
403
|
418
|
}
|
404
|
419
|
}
|
405
|
420
|
|
406
|
421
|
void UITextEdit::removeCharacter(bool right)
|
407
|
422
|
{
|
408
|
|
- std::string oldText = m_text;
|
409
|
|
- if(m_cursorPos >= 0 && m_text.length() > 0) {
|
410
|
|
- if((uint)m_cursorPos >= m_text.length()) {
|
411
|
|
- m_text.erase(m_text.begin() + (--m_cursorPos));
|
|
423
|
+ std::string tmp = m_text;
|
|
424
|
+ if(m_cursorPos >= 0 && tmp.length() > 0) {
|
|
425
|
+ if((uint)m_cursorPos >= tmp.length()) {
|
|
426
|
+ tmp.erase(tmp.begin() + (--m_cursorPos));
|
412
|
427
|
} else {
|
413
|
428
|
if(right)
|
414
|
|
- m_text.erase(m_text.begin() + m_cursorPos);
|
|
429
|
+ tmp.erase(tmp.begin() + m_cursorPos);
|
415
|
430
|
else if(m_cursorPos > 0)
|
416
|
|
- m_text.erase(m_text.begin() + --m_cursorPos);
|
|
431
|
+ tmp.erase(tmp.begin() + --m_cursorPos);
|
417
|
432
|
}
|
418
|
|
- onTextChange(m_text, oldText);
|
|
433
|
+ setText(tmp);
|
419
|
434
|
}
|
420
|
435
|
}
|
421
|
436
|
|
|
@@ -428,12 +443,12 @@ void UITextEdit::blinkCursor()
|
428
|
443
|
void UITextEdit::del(bool right)
|
429
|
444
|
{
|
430
|
445
|
if(hasSelection()) {
|
431
|
|
- std::string oldText = m_text;
|
432
|
|
- m_text.erase(m_selectionStart, m_selectionEnd - m_selectionStart);
|
|
446
|
+ std::string tmp = m_text;
|
|
447
|
+ tmp.erase(m_selectionStart, m_selectionEnd - m_selectionStart);
|
433
|
448
|
|
434
|
449
|
setCursorPos(m_selectionStart);
|
435
|
450
|
clearSelection();
|
436
|
|
- onTextChange(m_text, oldText);
|
|
451
|
+ setText(tmp);
|
437
|
452
|
} else
|
438
|
453
|
removeCharacter(right);
|
439
|
454
|
}
|
|
@@ -494,28 +509,51 @@ int UITextEdit::getTextPos(Point pos)
|
494
|
509
|
|
495
|
510
|
// find any glyph that is actually on the
|
496
|
511
|
int candidatePos = -1;
|
|
512
|
+ Rect firstGlyphRect, lastGlyphRect;
|
497
|
513
|
for(int i=0;i<textLength;++i) {
|
498
|
514
|
Rect clickGlyphRect = m_glyphsCoords[i];
|
|
515
|
+ if(!clickGlyphRect.isValid())
|
|
516
|
+ continue;
|
|
517
|
+ if(!firstGlyphRect.isValid())
|
|
518
|
+ firstGlyphRect = clickGlyphRect;
|
|
519
|
+ lastGlyphRect = clickGlyphRect;
|
499
|
520
|
clickGlyphRect.expandTop(m_font->getYOffset() + m_font->getGlyphSpacing().height());
|
500
|
521
|
clickGlyphRect.expandLeft(m_font->getGlyphSpacing().width()+1);
|
501
|
|
- if(clickGlyphRect.contains(pos))
|
502
|
|
- return i;
|
|
522
|
+ if(clickGlyphRect.contains(pos)) {
|
|
523
|
+ candidatePos = i;
|
|
524
|
+ break;
|
|
525
|
+ }
|
503
|
526
|
else if(pos.y >= clickGlyphRect.top() && pos.y <= clickGlyphRect.bottom()) {
|
504
|
|
- if(pos.x <= clickGlyphRect.left())
|
|
527
|
+ if(pos.x <= clickGlyphRect.left()) {
|
505
|
528
|
candidatePos = i;
|
506
|
|
- else if(pos.x >= clickGlyphRect.right())
|
|
529
|
+ break;
|
|
530
|
+ } else if(pos.x >= clickGlyphRect.right())
|
507
|
531
|
candidatePos = i+1;
|
508
|
532
|
}
|
509
|
533
|
}
|
|
534
|
+
|
|
535
|
+ if(textLength > 0) {
|
|
536
|
+ if(pos.y < firstGlyphRect.top())
|
|
537
|
+ return 0;
|
|
538
|
+ else if(pos.y > lastGlyphRect.bottom())
|
|
539
|
+ return textLength;
|
|
540
|
+ }
|
|
541
|
+
|
510
|
542
|
return candidatePos;
|
511
|
543
|
}
|
512
|
544
|
|
513
|
545
|
std::string UITextEdit::getDisplayedText()
|
514
|
546
|
{
|
|
547
|
+ std::string text;
|
515
|
548
|
if(m_textHidden)
|
516
|
|
- return std::string(m_text.length(), '*');
|
|
549
|
+ text = std::string(m_text.length(), '*');
|
517
|
550
|
else
|
518
|
|
- return m_text;
|
|
551
|
+ text = m_text;
|
|
552
|
+
|
|
553
|
+ if(m_textWrap && m_rect.isValid())
|
|
554
|
+ text = m_font->wrapText(text, getWidth() - m_textOffset.x);
|
|
555
|
+
|
|
556
|
+ return text;
|
519
|
557
|
}
|
520
|
558
|
|
521
|
559
|
std::string UITextEdit::getSelection()
|
|
@@ -525,35 +563,27 @@ std::string UITextEdit::getSelection()
|
525
|
563
|
return m_text.substr(m_selectionStart, m_selectionEnd - m_selectionStart);
|
526
|
564
|
}
|
527
|
565
|
|
528
|
|
-void UITextEdit::onHoverChange(bool hovered)
|
|
566
|
+void UITextEdit::updateText()
|
529
|
567
|
{
|
530
|
|
- if(hovered)
|
531
|
|
- g_mouse.setTextCursor();
|
532
|
|
- else
|
533
|
|
- g_mouse.restoreCursor();
|
534
|
|
-}
|
535
|
|
-
|
536
|
|
-void UITextEdit::onTextChange(const std::string& text, const std::string& oldText)
|
537
|
|
-{
|
538
|
|
- if(m_cursorPos > (int)text.length())
|
539
|
|
- m_cursorPos = text.length();
|
|
568
|
+ if(m_cursorPos > (int)m_text.length())
|
|
569
|
+ m_cursorPos = m_text.length();
|
540
|
570
|
|
541
|
571
|
// any text changes reset the selection
|
542
|
572
|
if(m_selectable) {
|
543
|
573
|
m_selectionEnd = 0;
|
544
|
574
|
m_selectionStart = 0;
|
545
|
|
- onSelectionChange(std::string(), 0, 0);
|
546
|
575
|
}
|
547
|
576
|
|
548
|
577
|
blinkCursor();
|
549
|
578
|
update(true);
|
550
|
|
- UIWidget::onTextChange(text, oldText);
|
551
|
579
|
}
|
552
|
580
|
|
553
|
|
-void UITextEdit::onFontChange(const std::string& font)
|
|
581
|
+void UITextEdit::onHoverChange(bool hovered)
|
554
|
582
|
{
|
555
|
|
- update(true);
|
556
|
|
- UIWidget::onFontChange(font);
|
|
583
|
+ if(hovered)
|
|
584
|
+ g_mouse.setTextCursor();
|
|
585
|
+ else
|
|
586
|
+ g_mouse.restoreCursor();
|
557
|
587
|
}
|
558
|
588
|
|
559
|
589
|
void UITextEdit::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode)
|
|
@@ -615,11 +645,15 @@ bool UITextEdit::onKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeat
|
615
|
645
|
|
616
|
646
|
if(keyboardModifiers == Fw::KeyboardNoModifier) {
|
617
|
647
|
if(keyCode == Fw::KeyDelete && m_editable) { // erase right character
|
618
|
|
- del(true);
|
619
|
|
- return true;
|
620
|
|
- } else if(keyCode == Fw::KeyBackspace && m_editable) { // erase left character {
|
621
|
|
- del(false);
|
622
|
|
- return true;
|
|
648
|
+ if(hasSelection() || !m_text.empty()) {
|
|
649
|
+ del(true);
|
|
650
|
+ return true;
|
|
651
|
+ }
|
|
652
|
+ } else if(keyCode == Fw::KeyBackspace && m_editable) { // erase left character
|
|
653
|
+ if(hasSelection() || !m_text.empty()) {
|
|
654
|
+ del(false);
|
|
655
|
+ return true;
|
|
656
|
+ }
|
623
|
657
|
} else if(keyCode == Fw::KeyRight && !m_shiftNavigation) { // move cursor right
|
624
|
658
|
clearSelection();
|
625
|
659
|
moveCursorHorizontally(true);
|
|
@@ -629,13 +663,17 @@ bool UITextEdit::onKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeat
|
629
|
663
|
moveCursorHorizontally(false);
|
630
|
664
|
return true;
|
631
|
665
|
} else if(keyCode == Fw::KeyHome) { // move cursor to first character
|
632
|
|
- clearSelection();
|
633
|
|
- setCursorPos(0);
|
634
|
|
- return true;
|
|
666
|
+ if(m_cursorPos != 0) {
|
|
667
|
+ clearSelection();
|
|
668
|
+ setCursorPos(0);
|
|
669
|
+ return true;
|
|
670
|
+ }
|
635
|
671
|
} else if(keyCode == Fw::KeyEnd) { // move cursor to last character
|
636
|
|
- clearSelection();
|
637
|
|
- setCursorPos(m_text.length());
|
638
|
|
- return true;
|
|
672
|
+ if(m_cursorPos != (int)m_text.length()) {
|
|
673
|
+ clearSelection();
|
|
674
|
+ setCursorPos(m_text.length());
|
|
675
|
+ return true;
|
|
676
|
+ }
|
639
|
677
|
} else if(keyCode == Fw::KeyTab && !m_shiftNavigation) {
|
640
|
678
|
clearSelection();
|
641
|
679
|
if(UIWidgetPtr parent = getParent())
|
|
@@ -656,14 +694,20 @@ bool UITextEdit::onKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeat
|
656
|
694
|
paste(g_window.getClipboardText());
|
657
|
695
|
return true;
|
658
|
696
|
} else if(keyCode == Fw::KeyX && m_editable && m_selectable) {
|
659
|
|
- cut();
|
660
|
|
- return true;
|
|
697
|
+ if(hasSelection()) {
|
|
698
|
+ cut();
|
|
699
|
+ return true;
|
|
700
|
+ }
|
661
|
701
|
} else if(keyCode == Fw::KeyC && m_selectable) {
|
662
|
|
- copy();
|
663
|
|
- return true;
|
|
702
|
+ if(hasSelection()) {
|
|
703
|
+ copy();
|
|
704
|
+ return true;
|
|
705
|
+ }
|
664
|
706
|
} else if(keyCode == Fw::KeyA && m_selectable) {
|
665
|
|
- selectAll();
|
666
|
|
- return true;
|
|
707
|
+ if(m_text.length() > 0) {
|
|
708
|
+ selectAll();
|
|
709
|
+ return true;
|
|
710
|
+ }
|
667
|
711
|
}
|
668
|
712
|
} else if(keyboardModifiers == Fw::KeyboardShiftModifier) {
|
669
|
713
|
if(keyCode == Fw::KeyTab && !m_shiftNavigation) {
|
|
@@ -708,6 +752,9 @@ bool UITextEdit::onKeyText(const std::string& keyText)
|
708
|
752
|
|
709
|
753
|
bool UITextEdit::onMousePress(const Point& mousePos, Fw::MouseButton button)
|
710
|
754
|
{
|
|
755
|
+ if(UIWidget::onMousePress(mousePos, button))
|
|
756
|
+ return true;
|
|
757
|
+
|
711
|
758
|
if(button == Fw::MouseLeftButton) {
|
712
|
759
|
int pos = getTextPos(mousePos);
|
713
|
760
|
if(pos >= 0) {
|
|
@@ -720,7 +767,8 @@ bool UITextEdit::onMousePress(const Point& mousePos, Fw::MouseButton button)
|
720
|
767
|
}
|
721
|
768
|
return true;
|
722
|
769
|
}
|
723
|
|
- return UIWidget::onMousePress(mousePos, button);
|
|
770
|
+
|
|
771
|
+ return false;
|
724
|
772
|
}
|
725
|
773
|
|
726
|
774
|
bool UITextEdit::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
|
|
@@ -741,13 +789,16 @@ bool UITextEdit::onMouseMove(const Point& mousePos, const Point& mouseMoved)
|
741
|
789
|
return UIWidget::onMouseMove(mousePos, mouseMoved);
|
742
|
790
|
}
|
743
|
791
|
|
744
|
|
-void UITextEdit::onTextAreaUpdate(const Point& offset, const Size& visibleSize, const Size& totalSize)
|
|
792
|
+bool UITextEdit::onDoubleClick(const Point& mousePos)
|
745
|
793
|
{
|
746
|
|
- callLuaField("onTextAreaUpdate", offset, visibleSize, totalSize);
|
|
794
|
+ if(m_selectable && m_text.length() > 0) {
|
|
795
|
+ selectAll();
|
|
796
|
+ return true;
|
|
797
|
+ }
|
|
798
|
+ return UIWidget::onDoubleClick(mousePos);
|
747
|
799
|
}
|
748
|
800
|
|
749
|
|
-void UITextEdit::onSelectionChange(const std::string& text, int start, int end)
|
|
801
|
+void UITextEdit::onTextAreaUpdate(const Point& offset, const Size& visibleSize, const Size& totalSize)
|
750
|
802
|
{
|
751
|
|
- update(true);
|
752
|
|
- callLuaField("onSelectionChange", text, start, end);
|
|
803
|
+ callLuaField("onTextAreaUpdate", offset, visibleSize, totalSize);
|
753
|
804
|
}
|