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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
/**
* @file lltextbox.cpp
* @brief A text display widget
*
* $LicenseInfo:firstyear=2001&license=viewergpl$
*
* Copyright (c) 2001-2009, Linden Research, Inc.
*
* Second Life Viewer Source Code
* The source code in this file ("Source Code") is provided by Linden Lab
* to you under the terms of the GNU General Public License, version 2.0
* ("GPL"), unless you have obtained a separate licensing agreement
* ("Other License"), formally executed by you and Linden Lab. Terms of
* the GPL can be found in doc/GPL-license.txt in this distribution, or
* online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution, or
* online at
* http://secondlifegrid.net/programs/open_source/licensing/flossexception
*
* By copying, modifying or distributing this software, you acknowledge
* that you have read and understood your obligations described above,
* and agree to abide by those obligations.
*
* ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
* WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
* COMPLETENESS OR PERFORMANCE.
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "lltextbox.h"
#include "lluictrlfactory.h"
#include "llfocusmgr.h"
#include "llwindow.h"
#include "llurlregistry.h"
#include "llstyle.h"
static LLDefaultChildRegistry::Register<LLTextBox> r("text");
LLTextBox::Params::Params()
: text_color("text_color"),
length("length"),
type("type"),
border_visible("border_visible", false),
border_drop_shadow_visible("border_drop_shadow_visible", false),
bg_visible("bg_visible", false),
use_ellipses("use_ellipses"),
word_wrap("word_wrap", false),
drop_shadow_visible("drop_shadow_visible"),
disabled_color("disabled_color"),
background_color("background_color"),
v_pad("v_pad", 0),
h_pad("h_pad", 0),
line_spacing("line_spacing", 0),
text("text"),
font_shadow("font_shadow", LLFontGL::NO_SHADOW)
{}
LLTextBox::LLTextBox(const LLTextBox::Params& p)
: LLUICtrl(p),
LLTextBase(p),
mBackgroundVisible( p.bg_visible ),
mBorderVisible( p.border_visible ),
mShadowType( p.font_shadow ),
mBorderDropShadowVisible( p.border_drop_shadow_visible ),
mUseEllipses( p.use_ellipses ),
mHPad(p.h_pad),
mVPad(p.v_pad),
mVAlign( LLFontGL::TOP ),
mClickedCallback(NULL),
mTextColor(p.text_color()),
mDisabledColor(p.disabled_color()),
mBackgroundColor(p.background_color()),
mHAlign(p.font_halign),
mLineSpacing(p.line_spacing),
mDidWordWrap(FALSE)
{
mWordWrap = p.word_wrap;
setText( p.text() );
}
BOOL LLTextBox::handleMouseDown(S32 x, S32 y, MASK mask)
{
BOOL handled = FALSE;
// HACK: Only do this if there actually is something to click, so that
// overly large text boxes in the older UI won't start eating clicks.
if (isClickable())
{
handled = TRUE;
// Route future Mouse messages here preemptively. (Release on mouse up.)
gFocusMgr.setMouseCapture( this );
if (getSoundFlags() & MOUSE_DOWN)
{
make_ui_sound("UISndClick");
}
}
return handled;
}
BOOL LLTextBox::handleMouseUp(S32 x, S32 y, MASK mask)
{
BOOL handled = FALSE;
// We only handle the click if the click both started and ended within us
// HACK: Only do this if there actually is something to click, so that
// overly large text boxes in the older UI won't start eating clicks.
if (isClickable() && hasMouseCapture())
{
handled = TRUE;
// Release the mouse
gFocusMgr.setMouseCapture( NULL );
if (getSoundFlags() & MOUSE_UP)
{
make_ui_sound("UISndClickRelease");
}
// handle clicks on Urls in the textbox first
if (! handleMouseUpOverUrl(x, y))
{
// DO THIS AT THE VERY END to allow the button to be destroyed
// as a result of being clicked. If mouseup in the widget,
// it's been clicked
if (mClickedCallback && ! handled)
{
mClickedCallback();
}
}
}
return handled;
}
BOOL LLTextBox::handleRightMouseDown(S32 x, S32 y, MASK mask)
{
// pop up a context menu for any Url under the cursor
return handleRightMouseDownOverUrl(this, x, y);
}
BOOL LLTextBox::handleHover(S32 x, S32 y, MASK mask)
{
// Check to see if we're over an HTML-style link
if (handleHoverOverUrl(x, y))
{
lldebugst(LLERR_USER_INPUT) << "hover handled by " << getName() << llendl;
getWindow()->setCursor(UI_CURSOR_HAND);
return TRUE;
}
return LLView::handleHover(x,y,mask);
}
BOOL LLTextBox::handleToolTip(S32 x, S32 y, std::string& msg, LLRect& sticky_rect_screen)
{
if (handleToolTipForUrl(this, x, y, msg, sticky_rect_screen))
{
return TRUE;
}
return LLUICtrl::handleToolTip(x, y, msg, sticky_rect_screen);
}
void LLTextBox::setText(const LLStringExplicit& text)
{
if(mWordWrap && !mDidWordWrap)
{
setWrappedText(text);
}
else
{
mText.assign(text);
updateDisplayTextAndSegments();
}
}
void LLTextBox::setLineLengths()
{
mLineLengthList.clear();
std::string::size_type cur = 0;
std::string::size_type len = mDisplayText.size();
while (cur < len)
{
std::string::size_type end = mDisplayText.find('\n', cur);
std::string::size_type runLen;
if (end == std::string::npos)
{
runLen = len - cur;
cur = len;
}
else
{
runLen = end - cur;
cur = end + 1; // skip the new line character
}
mLineLengthList.push_back( (S32)runLen );
}
}
LLWString LLTextBox::wrapText(const LLWString &wtext, S32 &hoffset, S32 &line_num, F32 max_width)
{
LLWString final_wtext;
LLWString::size_type cur = 0;
LLWString::size_type len = wtext.size();
while (cur < len)
{
LLWString::size_type end = wtext.find('\n', cur);
if (end == LLWString::npos)
{
end = len;
}
bool charsRemaining = true;
LLWString::size_type runLen = end - cur;
if (runLen > 0)
{
// work out how many chars can fit onto the current line
LLWString run(wtext, cur, runLen);
LLWString::size_type useLen =
mDefaultFont->maxDrawableChars(run.c_str(), max_width-hoffset, runLen, TRUE);
charsRemaining = (cur + useLen < len);
// try to break lines on word boundaries
if (useLen < run.size())
{
LLWString::size_type prev_use_len = useLen;
while (useLen > 0 && ! isspace(run[useLen-1]) && ! ispunct(run[useLen-1]))
{
--useLen;
}
if (useLen == 0)
{
useLen = prev_use_len;
}
}
// add the chars that could fit onto one line to our result
final_wtext.append(wtext, cur, useLen);
cur += useLen;
hoffset += mDefaultFont->getWidth(run.substr(0, useLen).c_str());
// abort if not enough room to add any more characters
if (useLen == 0)
{
break;
}
}
if (charsRemaining)
{
if (wtext[cur] == '\n')
{
cur += 1;
}
final_wtext += '\n';
hoffset = 0;
line_num += 1;
}
}
return final_wtext;
}
void LLTextBox::setWrappedText(const LLStringExplicit& in_text, F32 max_width)
{
mDidWordWrap = TRUE;
setText(wstring_to_utf8str(getWrappedText(in_text, max_width)));
}
LLWString LLTextBox::getWrappedText(const LLStringExplicit& in_text, F32 max_width)
{
//
// we don't want to wrap Urls otherwise we won't be able to detect their
// presence for hyperlinking. So we look for all Urls, and then word wrap
// the text before and after, but never break a Url in the middle. We
// also need to consider that the Url will be displayed as a label (not
// necessary the actual Url string).
//
if (max_width < 0.0f)
{
max_width = (F32)getRect().getWidth();
}
LLWString wtext = utf8str_to_wstring(in_text);
LLWString final_wtext;
S32 line_num = 1;
S32 hoffset = 0;
// find the next Url in the text string
LLUrlMatch match;
while ( LLUrlRegistry::instance().findUrl(wtext, match))
{
S32 start = match.getStart();
S32 end = match.getEnd() + 1;
// perform word wrap on the text before the Url
final_wtext += wrapText(wtext.substr(0, start), hoffset, line_num, max_width);
// add the Url (but compute width based on its label)
S32 label_width = mDefaultFont->getWidth(match.getLabel());
if (hoffset > 0 && hoffset + label_width > max_width)
{
final_wtext += '\n';
line_num++;
hoffset = 0;
}
final_wtext += wtext.substr(start, end-start);
hoffset += label_width;
if (hoffset > max_width)
{
final_wtext += '\n';
line_num++;
hoffset = 0;
// eat any leading whitespace on the next line
while (isspace(wtext[end]) && end < (S32)wtext.size())
{
end++;
}
}
// move on to the rest of the text after the Url
wtext = wtext.substr(end, wtext.size() - end + 1);
}
final_wtext += wrapText(wtext, hoffset, line_num, max_width);
return final_wtext;
}
S32 LLTextBox::getTextPixelWidth()
{
S32 max_line_width = 0;
if( mLineLengthList.size() > 0 )
{
S32 cur_pos = 0;
for (std::vector<S32>::iterator iter = mLineLengthList.begin();
iter != mLineLengthList.end(); ++iter)
{
S32 line_length = *iter;
S32 line_width = mDefaultFont->getWidth( mDisplayText.c_str(), cur_pos, line_length );
if( line_width > max_line_width )
{
max_line_width = line_width;
}
cur_pos += line_length+1;
}
}
else
{
max_line_width = mDefaultFont->getWidth(mDisplayText.c_str());
}
return max_line_width;
}
S32 LLTextBox::getTextPixelHeight()
{
S32 num_lines = mLineLengthList.size();
if( num_lines < 1 )
{
num_lines = 1;
}
return (S32)(num_lines * mDefaultFont->getLineHeight());
}
void LLTextBox::setValue(const LLSD& value )
{
mDidWordWrap = FALSE;
setText(value.asString());
}
BOOL LLTextBox::setTextArg( const std::string& key, const LLStringExplicit& text )
{
mText.setArg(key, text);
updateDisplayTextAndSegments();
return TRUE;
}
void LLTextBox::draw()
{
F32 alpha = getDrawContext().mAlpha;
if (mBorderVisible)
{
gl_rect_2d_offset_local(getLocalRect(), 2, FALSE);
}
if( mBorderDropShadowVisible )
{
static LLUIColor color_drop_shadow = LLUIColorTable::instance().getColor("ColorDropShadow");
static LLUICachedControl<S32> drop_shadow_tooltip ("DropShadowTooltip", 0);
gl_drop_shadow(0, getRect().getHeight(), getRect().getWidth(), 0,
color_drop_shadow % alpha, drop_shadow_tooltip);
}
if (mBackgroundVisible)
{
LLRect r( 0, getRect().getHeight(), getRect().getWidth(), 0 );
gl_rect_2d( r, mBackgroundColor.get() % alpha );
}
S32 text_x = 0;
switch( mHAlign )
{
case LLFontGL::LEFT:
text_x = mHPad;
break;
case LLFontGL::HCENTER:
text_x = getRect().getWidth() / 2;
break;
case LLFontGL::RIGHT:
text_x = getRect().getWidth() - mHPad;
break;
}
S32 text_y = getRect().getHeight() - mVPad;
if ( getEnabled() )
{
drawText( text_x, text_y, mDisplayText, mTextColor.get() );
}
else
{
drawText( text_x, text_y, mDisplayText, mDisabledColor.get() );
}
if (sDebugRects)
{
drawDebugRect();
}
//// *HACK: also draw debug rectangles around currently-being-edited LLView, and any elements that are being highlighted by GUI preview code (see LLFloaterUIPreview)
//std::set<LLView*>::iterator iter = std::find(sPreviewHighlightedElements.begin(), sPreviewHighlightedElements.end(), this);
//if ((sEditingUI && this == sEditingUIView) || (iter != sPreviewHighlightedElements.end() && sDrawPreviewHighlights))
//{
// drawDebugRect();
//}
}
void LLTextBox::reshape(S32 width, S32 height, BOOL called_from_parent)
{
// reparse line lengths (don't need to recalculate the display text)
setLineLengths();
LLView::reshape(width, height, called_from_parent);
}
void LLTextBox::drawText( S32 x, S32 y, const LLWString &text, const LLColor4& color )
{
F32 alpha = getDrawContext().mAlpha;
if (mSegments.size() > 1)
{
// we have Urls (or other multi-styled segments)
drawTextSegments(x, y, text);
}
else if( mLineLengthList.empty() )
{
// simple case of 1 line of text in one style
mDefaultFont->render(text, 0, (F32)x, (F32)y, color % alpha,
mHAlign, mVAlign,
0,
mShadowType,
S32_MAX, getRect().getWidth(), NULL, mUseEllipses);
}
else
{
// simple case of multiple lines of text, all in the same style
S32 cur_pos = 0;
for (std::vector<S32>::iterator iter = mLineLengthList.begin();
iter != mLineLengthList.end(); ++iter)
{
S32 line_length = *iter;
mDefaultFont->render(text, cur_pos, (F32)x, (F32)y, color % alpha,
mHAlign, mVAlign,
0,
mShadowType,
line_length, getRect().getWidth(), NULL, mUseEllipses );
cur_pos += line_length + 1;
S32 line_height = llfloor(mDefaultFont->getLineHeight()) + mLineSpacing;
y -= line_height;
if(y < line_height)
break;
}
}
}
void LLTextBox::reshapeToFitText()
{
// wrap remaining lines that did not fit on call to setWrappedText()
setLineLengths();
S32 width = getTextPixelWidth();
S32 height = getTextPixelHeight();
reshape( width + 2 * mHPad, height + 2 * mVPad );
}
S32 LLTextBox::getDocIndexFromLocalCoord( S32 local_x, S32 local_y, BOOL round ) const
{
// Returns the character offset for the character under the local (x, y) coordinate.
// When round is true, if the position is on the right half of a character, the cursor
// will be put to its right. If round is false, the cursor will always be put to the
// character's left.
LLRect rect = getLocalRect();
rect.mLeft += mHPad;
rect.mRight -= mHPad;
rect.mTop += mVPad;
rect.mBottom -= mVPad;
// Figure out which line we're nearest to.
S32 total_lines = getLineCount();
S32 line_height = llround( mDefaultFont->getLineHeight() ) + mLineSpacing;
S32 line = (rect.mTop - 1 - local_y) / line_height;
if (line >= total_lines)
{
return getLength(); // past the end
}
line = llclamp( line, 0, total_lines );
S32 line_start = getLineStart(line);
S32 next_start = getLineStart(line+1);
S32 line_end = (next_start != line_start) ? next_start - 1 : getLength();
if (line_start == -1)
{
return 0;
}
S32 line_len = line_end - line_start;
S32 pos = mDefaultFont->charFromPixelOffset(mDisplayText.c_str(), line_start,
(F32)(local_x - rect.mLeft),
(F32)rect.getWidth(),
line_len, round);
return line_start + pos;
}
S32 LLTextBox::getLineStart( S32 line ) const
{
line = llclamp(line, 0, getLineCount()-1);
S32 result = 0;
for (int i = 0; i < line; i++)
{
result += mLineLengthList[i] + 1 /* add newline */;
}
return result;
}
void LLTextBox::updateDisplayTextAndSegments()
{
// remove any previous segment list
clearSegments();
// if URL parsing is turned off, then not much to bo
if (! mParseHTML)
{
mDisplayText = mText.getWString();
setLineLengths();
return;
}
// create unique text segments for Urls
mDisplayText.clear();
S32 end = 0;
LLUrlMatch match;
LLWString text = mText.getWString();
// find the next Url in the text string
while ( LLUrlRegistry::instance().findUrl(text, match,
boost::bind(&LLTextBox::onUrlLabelUpdated, this, _1, _2)) )
{
// work out the char offset for the start/end of the url
S32 url_start = match.getStart();
S32 url_end = match.getEnd();
// and the char offset for the label in the display text
S32 seg_start = mDisplayText.size();
S32 start = seg_start + url_start;
S32 end = start + match.getLabel().size();
// create a segment for the text before the Url
mSegments.insert(new LLNormalTextSegment(new LLStyle(), seg_start, start, *this));
mDisplayText += text.substr(0, url_start);
// create a segment for the Url text
LLStyleSP html(new LLStyle);
html->setVisible(true);
html->setColor(mLinkColor);
html->mUnderline = TRUE;
html->setLinkHREF(match.getUrl());
LLNormalTextSegment *html_seg = new LLNormalTextSegment(html, start, end, *this);
html_seg->setToolTip(match.getTooltip());
mSegments.insert(html_seg);
mDisplayText += utf8str_to_wstring(match.getLabel());
// move on to the rest of the text after the Url
text = text.substr(url_end+1, text.size() - url_end);
}
// output a segment for the remaining text
if (text.size() > 0)
{
mSegments.insert(new LLNormalTextSegment(new LLStyle(), end, end + text.size(), *this));
mDisplayText += text;
}
// strip whitespace from the end of the text
while (mDisplayText.size() > 0 && isspace(mDisplayText[mDisplayText.size()-1]))
{
mDisplayText = mDisplayText.substr(0, mDisplayText.size() - 1);
segment_set_t::iterator it = getSegIterContaining(mDisplayText.size());
if (it != mSegments.end())
{
LLTextSegmentPtr seg = *it;
seg->setEnd(seg->getEnd()-1);
}
}
// we may have changed the line lengths, so recalculate them
setLineLengths();
}
void LLTextBox::onUrlLabelUpdated(const std::string &url, const std::string &label)
{
if (mDidWordWrap)
{
// re-word wrap as the url label lengths may have changed
setWrappedText(mText.getString());
}
else
{
// or just update the display text with the latest Url labels
updateDisplayTextAndSegments();
}
}
bool LLTextBox::isClickable() const
{
// return true if we have been given a click callback
if (mClickedCallback)
{
return true;
}
// also return true if we have a clickable Url in the text
segment_set_t::const_iterator it;
for (it = mSegments.begin(); it != mSegments.end(); ++it)
{
LLTextSegmentPtr segmentp = *it;
if (segmentp)
{
const LLStyleSP style = segmentp->getStyle();
if (style && style->isLink())
{
return true;
}
}
}
// otherwise there is nothing clickable here
return false;
}
void LLTextBox::drawTextSegments(S32 init_x, S32 init_y, const LLWString &text)
{
F32 alpha = getDrawContext().mAlpha;
const S32 text_len = text.length();
if (text_len <= 0)
{
return;
}
S32 cur_line = 0;
S32 num_lines = getLineCount();
S32 line_start = getLineStart(cur_line);
S32 line_height = llround( mDefaultFont->getLineHeight() ) + mLineSpacing;
F32 text_y = (F32) init_y;
segment_set_t::iterator cur_seg = mSegments.begin();
// render a line of text at a time
const LLRect textRect = getLocalRect();
while((textRect.mBottom <= text_y) && (cur_line < num_lines))
{
S32 next_start = -1;
S32 line_end = text_len;
if ((cur_line + 1) < num_lines)
{
next_start = getLineStart(cur_line + 1);
line_end = next_start;
}
if ( text[line_end-1] == '\n' )
{
--line_end;
}
// render all segments on this line
F32 text_x = init_x;
S32 seg_start = line_start;
while (seg_start < line_end && cur_seg != mSegments.end())
{
// move to the next segment (or continue the previous one)
LLTextSegment *cur_segment = *cur_seg;
while (cur_segment->getEnd() <= seg_start)
{
if (++cur_seg == mSegments.end())
{
return;
}
cur_segment = *cur_seg;
}
// Draw a segment within the line
S32 clipped_end = llmin( line_end, cur_segment->getEnd() );
S32 clipped_len = clipped_end - seg_start;
if( clipped_len > 0 )
{
LLStyleSP style = cur_segment->getStyle();
if (style && style->isVisible())
{
// work out the color for the segment
LLColor4 color ;
if (getEnabled())
{
color = style->isLink() ? mLinkColor.get() : mTextColor.get();
}
else
{
color = mDisabledColor.get();
}
color = color % alpha;
// render a single line worth for this segment
mDefaultFont->render(text, seg_start, text_x, text_y, color,
mHAlign, mVAlign, 0, mShadowType, clipped_len,
textRect.getWidth(), &text_x, mUseEllipses);
}
seg_start += clipped_len;
}
}
// move down one line
text_y -= (F32)line_height;
line_start = next_start;
cur_line++;
}
}
|