PageRenderTime 40ms CodeModel.GetById 15ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llui/llscrolllistcell.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 437 lines | 313 code | 61 blank | 63 comment | 24 complexity | 607601c4c78a9d08f2217fa488f6910b MD5 | raw file
  1/** 
  2 * @file llscrolllistcell.cpp
  3 * @brief Scroll lists are composed of rows (items), each of which 
  4 * contains columns (cells).
  5 *
  6 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 * 
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 */
 27
 28#include "linden_common.h"
 29
 30#include "llscrolllistcell.h"
 31
 32#include "llcheckboxctrl.h"
 33#include "llui.h"	// LLUIImage
 34#include "lluictrlfactory.h"
 35
 36//static 
 37LLScrollListCell* LLScrollListCell::create(const LLScrollListCell::Params& cell_p)
 38{
 39	LLScrollListCell* cell = NULL;
 40
 41	if (cell_p.type() == "icon")
 42	{
 43		cell = new LLScrollListIcon(cell_p);
 44	}
 45	else if (cell_p.type() == "checkbox")
 46	{
 47		cell = new LLScrollListCheck(cell_p);
 48	}
 49	else if (cell_p.type() == "date")
 50	{
 51		cell = new LLScrollListDate(cell_p);
 52	}
 53	else	// default is "text"
 54	{
 55		cell = new LLScrollListText(cell_p);
 56	}
 57
 58	if (cell_p.value.isProvided())
 59	{
 60		cell->setValue(cell_p.value);
 61	}
 62
 63	return cell;
 64}
 65
 66
 67LLScrollListCell::LLScrollListCell(const LLScrollListCell::Params& p)
 68:	mWidth(p.width), 
 69	mToolTip(p.tool_tip)
 70{}
 71
 72// virtual
 73const LLSD LLScrollListCell::getValue() const
 74{
 75	return LLStringUtil::null;
 76}
 77
 78//
 79// LLScrollListIcon
 80//
 81LLScrollListIcon::LLScrollListIcon(const LLScrollListCell::Params& p)
 82:	LLScrollListCell(p),
 83	mIcon(LLUI::getUIImage(p.value().asString())),
 84	mColor(p.color),
 85	mAlignment(p.font_halign)
 86{}
 87
 88LLScrollListIcon::~LLScrollListIcon()
 89{
 90}
 91
 92/*virtual*/
 93S32		LLScrollListIcon::getHeight() const
 94{ return mIcon ? mIcon->getHeight() : 0; }
 95
 96/*virtual*/
 97const LLSD		LLScrollListIcon::getValue() const
 98{ return mIcon.isNull() ? LLStringUtil::null : mIcon->getName(); }
 99
100void LLScrollListIcon::setValue(const LLSD& value)
101{
102	if (value.isUUID())
103	{
104		// don't use default image specified by LLUUID::null, use no image in that case
105		LLUUID image_id = value.asUUID();
106		mIcon = image_id.notNull() ? LLUI::getUIImageByID(image_id) : LLUIImagePtr(NULL);
107	}
108	else
109	{
110		std::string value_string = value.asString();
111		if (LLUUID::validate(value_string))
112		{
113			setValue(LLUUID(value_string));
114		}
115		else if (!value_string.empty())
116		{
117			mIcon = LLUI::getUIImage(value.asString());
118		}
119		else
120		{
121			mIcon = NULL;
122		}
123	}
124}
125
126
127void LLScrollListIcon::setColor(const LLColor4& color)
128{
129	mColor = color;
130}
131
132S32	LLScrollListIcon::getWidth() const 
133{
134	// if no specified fix width, use width of icon
135	if (LLScrollListCell::getWidth() == 0 && mIcon.notNull())
136	{
137		return mIcon->getWidth();
138	}
139	return LLScrollListCell::getWidth();
140}
141
142
143void LLScrollListIcon::draw(const LLColor4& color, const LLColor4& highlight_color)	 const
144{
145	if (mIcon)
146	{
147		switch(mAlignment)
148		{
149		case LLFontGL::LEFT:
150			mIcon->draw(0, 0, mColor);
151			break;
152		case LLFontGL::RIGHT:
153			mIcon->draw(getWidth() - mIcon->getWidth(), 0, mColor);
154			break;
155		case LLFontGL::HCENTER:
156			mIcon->draw((getWidth() - mIcon->getWidth()) / 2, 0, mColor);
157			break;
158		default:
159			break;
160		}
161	}
162}
163
164//
165// LLScrollListText
166//
167U32 LLScrollListText::sCount = 0;
168
169LLScrollListText::LLScrollListText(const LLScrollListCell::Params& p)
170:	LLScrollListCell(p),
171	mText(p.value().asString()),
172	mFont(p.font),
173	mColor(p.color),
174	mUseColor(p.color.isProvided()),
175	mFontAlignment(p.font_halign),
176	mVisible(p.visible),
177	mHighlightCount( 0 ),
178	mHighlightOffset( 0 )
179{
180	sCount++;
181
182	mTextWidth = getWidth();
183
184	// initialize rounded rect image
185	if (!mRoundedRectImage)
186	{
187		mRoundedRectImage = LLUI::getUIImage("Rounded_Square");
188	}
189}
190
191//virtual 
192void LLScrollListText::highlightText(S32 offset, S32 num_chars)
193{
194	mHighlightOffset = offset;
195	mHighlightCount = num_chars;
196}
197
198//virtual 
199BOOL LLScrollListText::isText() const
200{
201	return TRUE;
202}
203
204// virtual
205const std::string &LLScrollListText::getToolTip() const
206{
207	// If base class has a tooltip, return that
208	if (! LLScrollListCell::getToolTip().empty())
209		return LLScrollListCell::getToolTip();
210	
211	// ...otherwise, return the value itself as the tooltip
212	return mText.getString();
213}
214
215// virtual
216BOOL LLScrollListText::needsToolTip() const
217{
218	// If base class has a tooltip, return that
219	if (LLScrollListCell::needsToolTip())
220		return LLScrollListCell::needsToolTip();
221	
222	// ...otherwise, show tooltips for truncated text
223	return mFont->getWidth(mText.getString()) > getWidth();
224}
225
226//virtual 
227BOOL LLScrollListText::getVisible() const
228{
229	return mVisible;
230}
231
232//virtual 
233S32 LLScrollListText::getHeight() const
234{
235	return llround(mFont->getLineHeight());
236}
237
238
239LLScrollListText::~LLScrollListText()
240{
241	sCount--;
242}
243
244S32	LLScrollListText::getContentWidth() const
245{
246	return mFont->getWidth(mText.getString());
247}
248
249
250void LLScrollListText::setColor(const LLColor4& color)
251{
252	mColor = color;
253	mUseColor = TRUE;
254}
255
256void LLScrollListText::setText(const LLStringExplicit& text)
257{
258	mText = text;
259}
260
261void LLScrollListText::setFontStyle(const U8 font_style)
262{
263	LLFontDescriptor new_desc(mFont->getFontDesc());
264	new_desc.setStyle(font_style);
265	mFont = LLFontGL::getFont(new_desc);
266}
267
268//virtual
269void LLScrollListText::setValue(const LLSD& text)
270{
271	setText(text.asString());
272}
273
274//virtual 
275const LLSD LLScrollListText::getValue() const		
276{ 
277	return LLSD(mText.getString()); 
278}
279
280
281void LLScrollListText::draw(const LLColor4& color, const LLColor4& highlight_color) const
282{
283	LLColor4 display_color;
284	if (mUseColor)
285	{
286		display_color = mColor;
287	}
288	else
289	{
290		display_color = color;
291	}
292
293	if (mHighlightCount > 0)
294	{
295		S32 left = 0;
296		switch(mFontAlignment)
297		{
298		case LLFontGL::LEFT:
299			left = mFont->getWidth(mText.getString(), 0, mHighlightOffset);
300			break;
301		case LLFontGL::RIGHT:
302			left = getWidth() - mFont->getWidth(mText.getString(), mHighlightOffset, S32_MAX);
303			break;
304		case LLFontGL::HCENTER:
305			left = (getWidth() - mFont->getWidth(mText.getString())) / 2;
306			break;
307		}
308		LLRect highlight_rect(left - 2, 
309				llround(mFont->getLineHeight()) + 1, 
310				left + mFont->getWidth(mText.getString(), mHighlightOffset, mHighlightCount) + 1, 
311				1);
312		mRoundedRectImage->draw(highlight_rect, highlight_color);
313	}
314
315	// Try to draw the entire string
316	F32 right_x;
317	U32 string_chars = mText.length();
318	F32 start_x = 0.f;
319	switch(mFontAlignment)
320	{
321	case LLFontGL::LEFT:
322		start_x = 0.f;
323		break;
324	case LLFontGL::RIGHT:
325		start_x = (F32)getWidth();
326		break;
327	case LLFontGL::HCENTER:
328		start_x = (F32)getWidth() * 0.5f;
329		break;
330	}
331	mFont->render(mText.getWString(), 0, 
332					start_x, 2.f,
333					display_color,
334					mFontAlignment,
335					LLFontGL::BOTTOM, 
336					0,
337					LLFontGL::NO_SHADOW,
338					string_chars, 
339					getTextWidth(),
340					&right_x, 
341					TRUE);
342}
343
344//
345// LLScrollListCheck
346//
347LLScrollListCheck::LLScrollListCheck(const LLScrollListCell::Params& p)
348:	LLScrollListCell(p)
349{
350	LLCheckBoxCtrl::Params checkbox_p;
351	checkbox_p.name("checkbox");
352	checkbox_p.rect = LLRect(0, p.width, p.width, 0);
353	checkbox_p.enabled(p.enabled);
354	checkbox_p.initial_value(p.value());
355
356	mCheckBox = LLUICtrlFactory::create<LLCheckBoxCtrl>(checkbox_p);
357	 
358	LLRect rect(mCheckBox->getRect());
359	if (p.width)
360	{
361		rect.mRight = rect.mLeft + p.width;
362		mCheckBox->setRect(rect);
363		setWidth(p.width);
364	}
365	else
366	{
367		setWidth(rect.getWidth()); //check_box->getWidth();
368	}
369
370	mCheckBox->setColor(p.color);
371}
372
373
374LLScrollListCheck::~LLScrollListCheck()
375{
376	delete mCheckBox;
377	mCheckBox = NULL;
378}
379
380void LLScrollListCheck::draw(const LLColor4& color, const LLColor4& highlight_color) const
381{
382	mCheckBox->draw();
383}
384
385BOOL LLScrollListCheck::handleClick()
386{ 
387	if (mCheckBox->getEnabled())
388	{
389		mCheckBox->toggle();
390	}
391	// don't change selection when clicking on embedded checkbox
392	return TRUE; 
393}
394
395/*virtual*/
396const LLSD LLScrollListCheck::getValue() const
397{
398	return mCheckBox->getValue();
399}
400
401/*virtual*/
402void LLScrollListCheck::setValue(const LLSD& value)
403{
404	mCheckBox->setValue(value);
405}
406
407/*virtual*/
408void LLScrollListCheck::onCommit()
409{
410	mCheckBox->onCommit();
411}
412
413/*virtual*/
414void LLScrollListCheck::setEnabled(BOOL enable)
415{
416	mCheckBox->setEnabled(enable);
417}
418
419//
420// LLScrollListDate
421//
422
423LLScrollListDate::LLScrollListDate( const LLScrollListCell::Params& p)
424:	LLScrollListText(p),
425	mDate(p.value().asDate())
426{}
427
428void LLScrollListDate::setValue(const LLSD& value)
429{
430	mDate = value.asDate();
431	LLScrollListText::setValue(mDate.asRFC1123());
432}
433
434const LLSD LLScrollListDate::getValue() const
435{
436	return mDate;
437}