summaryrefslogtreecommitdiff
path: root/indra/llui/lltextbox.cpp
blob: 1c3c8329e60f0fc232c6d9c4b8ff544ddc37f869 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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
/** 
 * @file lltextbox.cpp
 * @brief A text display widget
 *
 * $LicenseInfo:firstyear=2001&license=viewergpl$
 * 
 * Copyright (c) 2001-2007, 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://secondlife.com/developers/opensource/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://secondlife.com/developers/opensource/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"

static LLRegisterWidget<LLTextBox> r("text");

LLTextBox::LLTextBox(const LLString& name, const LLRect& rect, const LLString& text,
					 const LLFontGL* font, BOOL mouse_opaque)
:	LLUICtrl(name, rect, mouse_opaque, NULL, NULL, FOLLOWS_LEFT | FOLLOWS_TOP ),
	mFontGL(font ? font : LLFontGL::sSansSerifSmall),
	mTextColor(			LLUI::sColorsGroup->getColor( "LabelTextColor" ) ),
	mDisabledColor(		LLUI::sColorsGroup->getColor( "LabelDisabledColor" ) ),
	mBackgroundColor(	LLUI::sColorsGroup->getColor( "DefaultBackgroundColor" ) ),
	mBorderColor(		LLUI::sColorsGroup->getColor( "DefaultHighlightLight" ) ),
	mHoverColor(		LLUI::sColorsGroup->getColor( "LabelSelectedColor" ) ),
	mHoverActive( FALSE ),
	mHasHover( FALSE ),
	mBackgroundVisible( FALSE ),
	mBorderVisible( FALSE ),
	mFontStyle(LLFontGL::DROP_SHADOW_SOFT),
	mBorderDropShadowVisible( FALSE ),
	mUseEllipses( FALSE ),
	mHPad(0),
	mVPad(0),
	mHAlign( LLFontGL::LEFT ),
	mVAlign( LLFontGL::TOP ),
	mClickedCallback(NULL),
	mCallbackUserData(NULL)
{
	setText( text );
	setTabStop(FALSE);
}

LLTextBox::LLTextBox(const LLString& name, const LLString& text, F32 max_width,
					 const LLFontGL* font, BOOL mouse_opaque) :
	LLUICtrl(name, LLRect(0, 0, 1, 1), mouse_opaque, NULL, NULL, FOLLOWS_LEFT | FOLLOWS_TOP),	
	mFontGL(font ? font : LLFontGL::sSansSerifSmall),
	mTextColor(LLUI::sColorsGroup->getColor("LabelTextColor")),
	mDisabledColor(LLUI::sColorsGroup->getColor("LabelDisabledColor")),
	mBackgroundColor(LLUI::sColorsGroup->getColor("DefaultBackgroundColor")),
	mBorderColor(LLUI::sColorsGroup->getColor("DefaultHighlightLight")),
	mHoverColor( LLUI::sColorsGroup->getColor( "LabelSelectedColor" ) ),
	mHoverActive( FALSE ),
	mHasHover( FALSE ),
	mBackgroundVisible(FALSE),
	mBorderVisible(FALSE),
	mFontStyle(LLFontGL::DROP_SHADOW_SOFT),
	mBorderDropShadowVisible(FALSE),
	mUseEllipses( FALSE ),
	mHPad(0),
	mVPad(0),
	mHAlign(LLFontGL::LEFT),
	mVAlign( LLFontGL::TOP ),
	mClickedCallback(NULL),
	mCallbackUserData(NULL)
{
	setWrappedText(text, max_width);
	reshapeToFitText();
	setTabStop(FALSE);
}

LLTextBox::LLTextBox(const LLString& name_and_label, const LLRect& rect) :
	LLUICtrl(name_and_label, rect, TRUE, NULL, NULL, FOLLOWS_LEFT | FOLLOWS_TOP),	
	mFontGL(LLFontGL::sSansSerifSmall),
	mTextColor(LLUI::sColorsGroup->getColor("LabelTextColor")),
	mDisabledColor(LLUI::sColorsGroup->getColor("LabelDisabledColor")),
	mBackgroundColor(LLUI::sColorsGroup->getColor("DefaultBackgroundColor")),
	mBorderColor(LLUI::sColorsGroup->getColor("DefaultHighlightLight")),
	mBackgroundVisible(FALSE),
	mBorderVisible(FALSE),
	mFontStyle(LLFontGL::DROP_SHADOW_SOFT),
	mBorderDropShadowVisible(FALSE),
	mHPad(0),
	mVPad(0),
	mHAlign(LLFontGL::LEFT),
	mVAlign( LLFontGL::TOP ),
	mClickedCallback(NULL),
	mCallbackUserData(NULL)
{
	setText( name_and_label );
	setTabStop(FALSE);
}

LLTextBox::LLTextBox(const LLString& name_and_label) :
	LLUICtrl(name_and_label, LLRect(0, 0, 1, 1), TRUE, NULL, NULL, FOLLOWS_LEFT | FOLLOWS_TOP),	
	mFontGL(LLFontGL::sSansSerifSmall),
	mTextColor(LLUI::sColorsGroup->getColor("LabelTextColor")),
	mDisabledColor(LLUI::sColorsGroup->getColor("LabelDisabledColor")),
	mBackgroundColor(LLUI::sColorsGroup->getColor("DefaultBackgroundColor")),
	mBorderColor(LLUI::sColorsGroup->getColor("DefaultHighlightLight")),
	mBackgroundVisible(FALSE),
	mBorderVisible(FALSE),
	mFontStyle(LLFontGL::DROP_SHADOW_SOFT),
	mBorderDropShadowVisible(FALSE),
	mHPad(0),
	mVPad(0),
	mHAlign(LLFontGL::LEFT),
	mVAlign( LLFontGL::TOP ),
	mClickedCallback(NULL),
	mCallbackUserData(NULL)
{
	setWrappedText(name_and_label);
	reshapeToFitText();
	setTabStop(FALSE);
}

BOOL LLTextBox::handleMouseDown(S32 x, S32 y, MASK mask)
{
	BOOL	handled = FALSE;

	// HACK: Only do this if there actually is a click callback, so that
	// overly large text boxes in the older UI won't start eating clicks.
	if (mClickedCallback)
	{
		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 a click callback, so that
	// overly large text boxes in the older UI won't start eating clicks.
	if (mClickedCallback
		&& hasMouseCapture())
	{
		handled = TRUE;

		// Release the mouse
		gFocusMgr.setMouseCapture( NULL );

		if (getSoundFlags() & MOUSE_UP)
		{
			make_ui_sound("UISndClickRelease");
		}

		// 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)
		{
			(*mClickedCallback)( mCallbackUserData );
		}
	}

	return handled;
}

BOOL LLTextBox::handleHover(S32 x, S32 y, MASK mask)
{
	if(mHoverActive)
	{
		mHasHover = TRUE; // This should be set every frame during a hover.
		return TRUE;
	}
	return LLView::handleHover(x,y,mask);
}

void LLTextBox::setText(const LLStringExplicit& text)
{
	mText.assign(text);
	setLineLengths();
}

void LLTextBox::setLineLengths()
{
	mLineLengthList.clear();
	
	LLString::size_type  cur = 0;
	LLString::size_type  len = mText.getWString().size();

	while (cur < len) 
	{
		LLString::size_type end = mText.getWString().find('\n', cur);
		LLString::size_type runLen;
		
		if (end == LLString::npos)
		{
			runLen = len - cur;
			cur = len;
		}
		else
		{
			runLen = end - cur;
			cur = end + 1; // skip the new line character
		}

		mLineLengthList.push_back( (S32)runLen );
	}
}

void LLTextBox::setWrappedText(const LLStringExplicit& in_text, F32 max_width)
{
	if (max_width < 0.0)
	{
		max_width = (F32)getRect().getWidth();
	}

	LLWString wtext = utf8str_to_wstring(in_text);
	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;
		}
		
		LLWString::size_type runLen = end - cur;
		if (runLen > 0)
		{
			LLWString run(wtext, cur, runLen);
			LLWString::size_type useLen =
				mFontGL->maxDrawableChars(run.c_str(), max_width, runLen, TRUE);

			final_wtext.append(wtext, cur, useLen);
			cur += useLen;
		}

		if (cur < len)
		{
			if (wtext[cur] == '\n')
			{
				cur += 1;
			}
			final_wtext += '\n';
		}
	}

	LLString final_text = wstring_to_utf8str(final_wtext);
	setText(final_text);
}

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 = mFontGL->getWidth( mText.getWString().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 = mFontGL->getWidth(mText.getWString().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 * mFontGL->getLineHeight());
}


BOOL LLTextBox::setTextArg( const LLString& key, const LLStringExplicit& text )
{
	mText.setArg(key, text);
	setLineLengths();
	return TRUE;
}

void LLTextBox::draw()
{
	if (mBorderVisible)
	{
		gl_rect_2d_offset_local(getLocalRect(), 2, FALSE);
	}

	if( mBorderDropShadowVisible )
	{
		static LLColor4 color_drop_shadow = LLUI::sColorsGroup->getColor("ColorDropShadow");
		static S32 drop_shadow_tooltip = LLUI::sConfigGroup->getS32("DropShadowTooltip");
		gl_drop_shadow(0, getRect().getHeight(), getRect().getWidth(), 0,
			color_drop_shadow, drop_shadow_tooltip);
	}

	if (mBackgroundVisible)
	{
		LLRect r( 0, getRect().getHeight(), getRect().getWidth(), 0 );
		gl_rect_2d( r, mBackgroundColor );
	}

	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() )
	{
		if(mHasHover)
		{
			drawText( text_x, text_y, mHoverColor );
		}
		else
		{
			drawText( text_x, text_y, mTextColor );
		}				
	}
	else
	{
		drawText( text_x, text_y, mDisabledColor );
	}

	if (sDebugRects)
	{
		drawDebugRect();
	}

	mHasHover = FALSE; // This is reset every frame.
}

void LLTextBox::reshape(S32 width, S32 height, BOOL called_from_parent)
{
	// reparse line lengths
	setLineLengths();
	LLView::reshape(width, height, called_from_parent);
}

void LLTextBox::drawText( S32 x, S32 y, const LLColor4& color )
{
	if( mLineLengthList.empty() )
	{
		mFontGL->render(mText.getWString(), 0, (F32)x, (F32)y, color,
						mHAlign, mVAlign, 
						mFontStyle,
						S32_MAX, getRect().getWidth(), NULL, TRUE, mUseEllipses);
	}
	else
	{
		S32 cur_pos = 0;
		for (std::vector<S32>::iterator iter = mLineLengthList.begin();
			iter != mLineLengthList.end(); ++iter)
		{
			S32 line_length = *iter;
			mFontGL->render(mText.getWString(), cur_pos, (F32)x, (F32)y, color,
							mHAlign, mVAlign,
							mFontStyle,
							line_length, getRect().getWidth(), NULL, TRUE, mUseEllipses );
			cur_pos += line_length + 1;
			y -= llfloor(mFontGL->getLineHeight());
		}
	}
}

void LLTextBox::reshapeToFitText()
{
	S32 width = getTextPixelWidth();
	S32 height = getTextPixelHeight();
	reshape( width + 2 * mHPad, height + 2 * mVPad );
}

// virtual
LLXMLNodePtr LLTextBox::getXML(bool save_children) const
{
	LLXMLNodePtr node = LLUICtrl::getXML();

	// Attributes
	node->createChild("font", TRUE)->setStringValue(LLFontGL::nameFromFont(mFontGL));
	node->createChild("halign", TRUE)->setStringValue(LLFontGL::nameFromHAlign(mHAlign));
	addColorXML(node, mTextColor, "text_color", "LabelTextColor");
	addColorXML(node, mDisabledColor, "disabled_color", "LabelDisabledColor");
	addColorXML(node, mBackgroundColor, "bg_color", "DefaultBackgroundColor");
	addColorXML(node, mBorderColor, "border_color", "DefaultHighlightLight");
	node->createChild("bg_visible", TRUE)->setBoolValue(mBackgroundVisible);
	node->createChild("border_visible", TRUE)->setBoolValue(mBorderVisible);
	node->createChild("border_drop_shadow_visible", TRUE)->setBoolValue(mBorderDropShadowVisible);
	node->createChild("h_pad", TRUE)->setIntValue(mHPad);
	node->createChild("v_pad", TRUE)->setIntValue(mVPad);

	// Contents
	node->setStringValue(mText);

	return node;
}

// static
LLView* LLTextBox::fromXML(LLXMLNodePtr node, LLView *parent, LLUICtrlFactory *factory)
{
	LLString name("text_box");
	node->getAttributeString("name", name);
	LLFontGL* font = LLView::selectFont(node);

	LLString text = node->getTextContents();

	LLTextBox* text_box = new LLTextBox(name,
		LLRect(),
		text,
		font,
		FALSE);
		

	LLFontGL::HAlign halign = LLView::selectFontHAlign(node);
	text_box->setHAlign(halign);

	text_box->initFromXML(node, parent);

	LLString font_style;
	if (node->getAttributeString("font-style", font_style))
	{
		text_box->mFontStyle = LLFontGL::getStyleFromString(font_style);
	}
	
	BOOL mouse_opaque = text_box->getMouseOpaque();
	if (node->getAttributeBOOL("mouse_opaque", mouse_opaque))
	{
		text_box->setMouseOpaque(mouse_opaque);
	}	

	if(node->hasAttribute("text_color"))
	{
		LLColor4 color;
		LLUICtrlFactory::getAttributeColor(node, "text_color", color);
		text_box->setColor(color);
	}

	if(node->hasAttribute("hover_color"))
	{
		LLColor4 color;
		LLUICtrlFactory::getAttributeColor(node, "hover_color", color);
		text_box->setHoverColor(color);
		text_box->setHoverActive(true);
	}

	BOOL hover_active = FALSE;
	if(node->getAttributeBOOL("hover", hover_active))
	{
		text_box->setHoverActive(hover_active);
	}

	return text_box;
}