summaryrefslogtreecommitdiff
path: root/indra/newview/llexpandabletextbox.cpp
blob: 935dcb74b0f8370c8df57fb3c0db77862f5ddb76 (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
/** 
 * @file llexpandabletextbox.cpp
 * @brief LLExpandableTextBox and related class implementations
 *
 * $LicenseInfo:firstyear=2004&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"
#include "llexpandabletextbox.h"

#include "llscrollcontainer.h"
#include "lltrans.h"
#include "llwindow.h"
#include "llviewerwindow.h"

static LLDefaultChildRegistry::Register<LLExpandableTextBox> t1("expandable_text");

class LLExpanderSegment : public LLTextSegment
{
public:
	LLExpanderSegment(const LLStyleSP& style, S32 start, S32 end, const std::string& more_text, LLTextBase& editor )
	:	LLTextSegment(start, end),
		mEditor(editor),
		mStyle(style),
		mExpanderLabel(more_text)
	{}

	/*virtual*/ bool	getDimensions(S32 first_char, S32 num_chars, S32& width, S32& height) const 
	{
		// more label always spans width of text box
		if (num_chars == 0)
		{
			width = 0; 
			height = 0;
		}
		else
		{
			width = mEditor.getDocumentView()->getRect().getWidth() - mEditor.getHPad(); 
			height = mStyle->getFont()->getLineHeight();
		}
		return true;
	}
	/*virtual*/ S32		getOffset(S32 segment_local_x_coord, S32 start_offset, S32 num_chars, bool round) const 
	{ 
		return start_offset;
	}
	/*virtual*/ S32		getNumChars(S32 num_pixels, S32 segment_offset, S32 line_offset, S32 max_chars) const 
	{ 
		// require full line to ourselves
		if (line_offset == 0) 
		{
			// print all our text
			return getEnd() - getStart(); 
		}
		else
		{
			// wait for next line
			return 0;
		}
	}
	/*virtual*/ F32		draw(S32 start, S32 end, S32 selection_start, S32 selection_end, const LLRect& draw_rect)
	{
		F32 right_x;
		mStyle->getFont()->renderUTF8(mExpanderLabel, start, 
									draw_rect.mRight, draw_rect.mTop, 
									mStyle->getColor(), 
									LLFontGL::RIGHT, LLFontGL::TOP, 
									0, 
									mStyle->getShadowType(), 
									end - start, draw_rect.getWidth(), 
									&right_x, 
									mEditor.getUseEllipses());
		return right_x;
	}
	/*virtual*/ bool	canEdit() const { return false; }
	// eat handleMouseDown event so we get the mouseup event
	/*virtual*/ BOOL	handleMouseDown(S32 x, S32 y, MASK mask) { return TRUE; }
	/*virtual*/ BOOL	handleMouseUp(S32 x, S32 y, MASK mask) { mEditor.onCommit(); return TRUE; }
	/*virtual*/ BOOL	handleHover(S32 x, S32 y, MASK mask) 
	{
		LLUI::getWindow()->setCursor(UI_CURSOR_HAND);
		return TRUE; 
	}
private:
	LLTextBase& mEditor;
	LLStyleSP	mStyle;
	std::string	mExpanderLabel;
};

LLExpandableTextBox::LLTextBoxEx::Params::Params()
{
}

LLExpandableTextBox::LLTextBoxEx::LLTextBoxEx(const Params& p)
:	LLTextEditor(p),
	mExpanderLabel(p.label.isProvided() ? p.label : LLTrans::getString("More")),
	mExpanderVisible(false)
{
	setIsChrome(TRUE);

}

void LLExpandableTextBox::LLTextBoxEx::reshape(S32 width, S32 height, BOOL called_from_parent)
{
	LLTextEditor::reshape(width, height, called_from_parent);

	hideOrShowExpandTextAsNeeded();
}

void LLExpandableTextBox::LLTextBoxEx::setText(const LLStringExplicit& text,const LLStyle::Params& input_params)
{
	// LLTextBox::setText will obliterate the expander segment, so make sure
	// we generate it again by clearing mExpanderVisible
	mExpanderVisible = false;
	LLTextEditor::setText(text, input_params);

	hideOrShowExpandTextAsNeeded();
}


void LLExpandableTextBox::LLTextBoxEx::showExpandText()
{
	if (!mExpanderVisible)
	{
		// make sure we're scrolled to top when collapsing
		if (mScroller)
		{
			mScroller->goToTop();
		}
		// get fully visible lines
		std::pair<S32, S32> visible_lines = getVisibleLines(true);
		S32 last_line = visible_lines.second - 1;

		LLStyle::Params expander_style(getDefaultStyleParams());
		expander_style.font.style = "UNDERLINE";
		expander_style.color = LLUIColorTable::instance().getColor("HTMLLinkColor");
		LLExpanderSegment* expanderp = new LLExpanderSegment(new LLStyle(expander_style), getLineStart(last_line), getLength() + 1, mExpanderLabel, *this);
		insertSegment(expanderp);
		mExpanderVisible = true;
	}

}

//NOTE: obliterates existing styles (including hyperlinks)
void LLExpandableTextBox::LLTextBoxEx::hideExpandText() 
{ 
	if (mExpanderVisible)
	{
		// this will overwrite the expander segment and all text styling with a single style
		LLStyleConstSP sp(new LLStyle(getDefaultStyleParams()));
		LLNormalTextSegment* segmentp = new LLNormalTextSegment(sp, 0, getLength() + 1, *this);
		insertSegment(segmentp);
		
		mExpanderVisible = false;
	}
}

S32 LLExpandableTextBox::LLTextBoxEx::getVerticalTextDelta()
{
	S32 text_height = getTextPixelHeight();
	S32 textbox_height = getRect().getHeight();

	return text_height - textbox_height;
}

S32 LLExpandableTextBox::LLTextBoxEx::getTextPixelHeight()
{
	return getTextBoundingRect().getHeight();
}

void LLExpandableTextBox::LLTextBoxEx::hideOrShowExpandTextAsNeeded()
{
	// Restore the text box contents to calculate the text height properly,
	// otherwise if a part of the text is hidden under "More" link
	// getTextPixelHeight() returns only the height of currently visible text
	// including the "More" link. See STORM-250.
	hideExpandText();

	// Show the expander a.k.a. "More" link if we need it, depending on text
	// contents height. If not, keep it hidden.
	if (getTextPixelHeight() > getRect().getHeight())
	{
		showExpandText();
	}
}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

LLExpandableTextBox::Params::Params()
:	textbox("textbox"),
	scroll("scroll"),
	max_height("max_height", 0),
	bg_visible("bg_visible", false),
	expanded_bg_visible("expanded_bg_visible", true),
	bg_color("bg_color", LLColor4::black),
	expanded_bg_color("expanded_bg_color", LLColor4::black)
{
}

LLExpandableTextBox::LLExpandableTextBox(const Params& p)
:	LLUICtrl(p),
	mMaxHeight(p.max_height),
	mBGVisible(p.bg_visible),
	mExpandedBGVisible(p.expanded_bg_visible),
	mBGColor(p.bg_color),
	mExpandedBGColor(p.expanded_bg_color),
	mExpanded(false)
{
	LLRect rc = getLocalRect();

	LLScrollContainer::Params scroll_params = p.scroll;
	scroll_params.rect(rc);
	mScroll = LLUICtrlFactory::create<LLScrollContainer>(scroll_params);
	addChild(mScroll);

	LLTextBoxEx::Params textbox_params = p.textbox;
	textbox_params.rect(rc);
	mTextBox = LLUICtrlFactory::create<LLTextBoxEx>(textbox_params);
	mScroll->addChild(mTextBox);

	updateTextBoxRect();

	mTextBox->setCommitCallback(boost::bind(&LLExpandableTextBox::onExpandClicked, this));
}

void LLExpandableTextBox::draw()
{
	if(mBGVisible && !mExpanded)
	{
		gl_rect_2d(getLocalRect(), mBGColor.get(), TRUE);
	}
	if(mExpandedBGVisible && mExpanded)
	{
		gl_rect_2d(getLocalRect(), mExpandedBGColor.get(), TRUE);
	}

	collapseIfPosChanged();

	LLUICtrl::draw();
}

void LLExpandableTextBox::collapseIfPosChanged()
{
	if(mExpanded)
	{
		LLView* parentp = getParent();
		LLRect parent_rect = parentp->getRect();
		parentp->localRectToOtherView(parent_rect, &parent_rect, getRootView());

		if(parent_rect.mLeft != mParentRect.mLeft 
			|| parent_rect.mTop != mParentRect.mTop)
		{
			collapseTextBox();
		}
	}
}

void LLExpandableTextBox::onExpandClicked()
{
	expandTextBox();
}

void LLExpandableTextBox::updateTextBoxRect()
{
	LLRect rc = getLocalRect();

	rc.mLeft += mScroll->getBorderWidth();
	rc.mRight -= mScroll->getBorderWidth();
	rc.mTop -= mScroll->getBorderWidth();
	rc.mBottom += mScroll->getBorderWidth();

	mTextBox->reshape(rc.getWidth(), rc.getHeight());
	mTextBox->setRect(rc);
}

S32 LLExpandableTextBox::recalculateTextDelta(S32 text_delta)
{
	LLRect expanded_rect = getLocalRect();
	LLView* root_view = getRootView();
	LLRect window_rect = root_view->getRect();

	LLRect expanded_screen_rect;
	localRectToOtherView(expanded_rect, &expanded_screen_rect, root_view);

	// don't allow expanded text box bottom go off screen
	if(expanded_screen_rect.mBottom - text_delta < window_rect.mBottom)
	{
		text_delta = expanded_screen_rect.mBottom - window_rect.mBottom;
	}
	// show scroll bar if max_height is valid 
	// and expanded size is greater that max_height
	else if(mMaxHeight > 0 && expanded_rect.getHeight() + text_delta > mMaxHeight)
	{
		text_delta = mMaxHeight - expanded_rect.getHeight();
	}

	return text_delta;
}

void LLExpandableTextBox::expandTextBox()
{
	// hide "more" link, and show full text contents
	mTextBox->hideExpandText();

	// *HACK dz
	// hideExpandText brakes text styles (replaces hyper-links with plain text), see ticket EXT-3290
	// Set text again to make text box re-apply styles.
	// *TODO Find proper solution to fix this issue.
	// Maybe add removeSegment to LLTextBase
	mTextBox->setTextBase(mText);

	S32 text_delta = mTextBox->getVerticalTextDelta();
	text_delta += mTextBox->getVPad() * 2;
	text_delta += mScroll->getBorderWidth() * 2;
	// no need to expand
	if(text_delta <= 0)
	{
		return;
	}

	saveCollapsedState();

	LLRect expanded_rect = getLocalRect();
	LLRect expanded_screen_rect;

	S32 updated_text_delta = recalculateTextDelta(text_delta);
	// actual expand
	expanded_rect.mBottom -= updated_text_delta;

	LLRect text_box_rect = mTextBox->getRect();

	// check if we need to show scrollbar
	if(text_delta != updated_text_delta)
	{
		static LLUICachedControl<S32> scrollbar_size ("UIScrollbarSize", 0);

		// disable horizontal scrollbar
		text_box_rect.mRight -= scrollbar_size;

		// text box size has changed - redo text wrap
		// Should be handled automatically in reshape() below. JC
		//mTextBox->setWrappedText(mText, text_box_rect.getWidth());

		// recalculate text delta since text wrap changed text height
		text_delta = mTextBox->getVerticalTextDelta() + mTextBox->getVPad() * 2;
	}

	// expand text
	text_box_rect.mBottom -= text_delta;
	mTextBox->reshape(text_box_rect.getWidth(), text_box_rect.getHeight());
	mTextBox->setRect(text_box_rect);

	// expand text box
	localRectToOtherView(expanded_rect, &expanded_screen_rect, getParent());
	reshape(expanded_screen_rect.getWidth(), expanded_screen_rect.getHeight(), FALSE);
	setRect(expanded_screen_rect);

	setFocus(TRUE);
	// this lets us receive top_lost event(needed to collapse text box)
	// it also draws text box above all other ui elements
	gViewerWindow->addPopup(this);

	mExpanded = true;
}

void LLExpandableTextBox::collapseTextBox()
{
	if(!mExpanded)
	{
		return;
	}

	mExpanded = false;

	reshape(mCollapsedRect.getWidth(), mCollapsedRect.getHeight(), FALSE);
	setRect(mCollapsedRect);

	updateTextBoxRect();

	gViewerWindow->removePopup(this);
}

void LLExpandableTextBox::onFocusLost()
{
	collapseTextBox();

	LLUICtrl::onFocusLost();
}

void LLExpandableTextBox::onTopLost()
{
	collapseTextBox();

	LLUICtrl::onTopLost();
}

void LLExpandableTextBox::updateTextShape()
{
	// I guess this should be done on every reshape(),
	// but adding this code to reshape() currently triggers bug VWR-26455,
	// which makes the text virtually unreadable.
	llassert(!mExpanded);
	updateTextBoxRect();
}

void LLExpandableTextBox::setValue(const LLSD& value)
{
	collapseTextBox();
	mText = value.asString();
	mTextBox->setValue(value);
}

void LLExpandableTextBox::setText(const std::string& str)
{
	collapseTextBox();
	mText = str;
	mTextBox->setText(str);
}

void LLExpandableTextBox::saveCollapsedState()
{
	mCollapsedRect = getRect();

	mParentRect = getParent()->getRect();
	// convert parent rect to screen coordinates, 
	// this will allow to track parent's position change
	getParent()->localRectToOtherView(mParentRect, &mParentRect, getRootView());
}