PageRenderTime 945ms CodeModel.GetById 929ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  28. #include "llscrolllistcell.h"
  29. #include "llcheckboxctrl.h"
  30. #include "llui.h" // LLUIImage
  31. #include "lluictrlfactory.h"
  32. //static
  33. LLScrollListCell* LLScrollListCell::create(const LLScrollListCell::Params& cell_p)
  34. {
  35. LLScrollListCell* cell = NULL;
  36. if (cell_p.type() == "icon")
  37. {
  38. cell = new LLScrollListIcon(cell_p);
  39. }
  40. else if (cell_p.type() == "checkbox")
  41. {
  42. cell = new LLScrollListCheck(cell_p);
  43. }
  44. else if (cell_p.type() == "date")
  45. {
  46. cell = new LLScrollListDate(cell_p);
  47. }
  48. else // default is "text"
  49. {
  50. cell = new LLScrollListText(cell_p);
  51. }
  52. if (cell_p.value.isProvided())
  53. {
  54. cell->setValue(cell_p.value);
  55. }
  56. return cell;
  57. }
  58. LLScrollListCell::LLScrollListCell(const LLScrollListCell::Params& p)
  59. : mWidth(p.width),
  60. mToolTip(p.tool_tip)
  61. {}
  62. // virtual
  63. const LLSD LLScrollListCell::getValue() const
  64. {
  65. return LLStringUtil::null;
  66. }
  67. //
  68. // LLScrollListIcon
  69. //
  70. LLScrollListIcon::LLScrollListIcon(const LLScrollListCell::Params& p)
  71. : LLScrollListCell(p),
  72. mIcon(LLUI::getUIImage(p.value().asString())),
  73. mColor(p.color),
  74. mAlignment(p.font_halign)
  75. {}
  76. LLScrollListIcon::~LLScrollListIcon()
  77. {
  78. }
  79. /*virtual*/
  80. S32 LLScrollListIcon::getHeight() const
  81. { return mIcon ? mIcon->getHeight() : 0; }
  82. /*virtual*/
  83. const LLSD LLScrollListIcon::getValue() const
  84. { return mIcon.isNull() ? LLStringUtil::null : mIcon->getName(); }
  85. void LLScrollListIcon::setValue(const LLSD& value)
  86. {
  87. if (value.isUUID())
  88. {
  89. // don't use default image specified by LLUUID::null, use no image in that case
  90. LLUUID image_id = value.asUUID();
  91. mIcon = image_id.notNull() ? LLUI::getUIImageByID(image_id) : LLUIImagePtr(NULL);
  92. }
  93. else
  94. {
  95. std::string value_string = value.asString();
  96. if (LLUUID::validate(value_string))
  97. {
  98. setValue(LLUUID(value_string));
  99. }
  100. else if (!value_string.empty())
  101. {
  102. mIcon = LLUI::getUIImage(value.asString());
  103. }
  104. else
  105. {
  106. mIcon = NULL;
  107. }
  108. }
  109. }
  110. void LLScrollListIcon::setColor(const LLColor4& color)
  111. {
  112. mColor = color;
  113. }
  114. S32 LLScrollListIcon::getWidth() const
  115. {
  116. // if no specified fix width, use width of icon
  117. if (LLScrollListCell::getWidth() == 0 && mIcon.notNull())
  118. {
  119. return mIcon->getWidth();
  120. }
  121. return LLScrollListCell::getWidth();
  122. }
  123. void LLScrollListIcon::draw(const LLColor4& color, const LLColor4& highlight_color) const
  124. {
  125. if (mIcon)
  126. {
  127. switch(mAlignment)
  128. {
  129. case LLFontGL::LEFT:
  130. mIcon->draw(0, 0, mColor);
  131. break;
  132. case LLFontGL::RIGHT:
  133. mIcon->draw(getWidth() - mIcon->getWidth(), 0, mColor);
  134. break;
  135. case LLFontGL::HCENTER:
  136. mIcon->draw((getWidth() - mIcon->getWidth()) / 2, 0, mColor);
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. }
  143. //
  144. // LLScrollListText
  145. //
  146. U32 LLScrollListText::sCount = 0;
  147. LLScrollListText::LLScrollListText(const LLScrollListCell::Params& p)
  148. : LLScrollListCell(p),
  149. mText(p.value().asString()),
  150. mFont(p.font),
  151. mColor(p.color),
  152. mUseColor(p.color.isProvided()),
  153. mFontAlignment(p.font_halign),
  154. mVisible(p.visible),
  155. mHighlightCount( 0 ),
  156. mHighlightOffset( 0 )
  157. {
  158. sCount++;
  159. mTextWidth = getWidth();
  160. // initialize rounded rect image
  161. if (!mRoundedRectImage)
  162. {
  163. mRoundedRectImage = LLUI::getUIImage("Rounded_Square");
  164. }
  165. }
  166. //virtual
  167. void LLScrollListText::highlightText(S32 offset, S32 num_chars)
  168. {
  169. mHighlightOffset = offset;
  170. mHighlightCount = num_chars;
  171. }
  172. //virtual
  173. BOOL LLScrollListText::isText() const
  174. {
  175. return TRUE;
  176. }
  177. // virtual
  178. const std::string &LLScrollListText::getToolTip() const
  179. {
  180. // If base class has a tooltip, return that
  181. if (! LLScrollListCell::getToolTip().empty())
  182. return LLScrollListCell::getToolTip();
  183. // ...otherwise, return the value itself as the tooltip
  184. return mText.getString();
  185. }
  186. // virtual
  187. BOOL LLScrollListText::needsToolTip() const
  188. {
  189. // If base class has a tooltip, return that
  190. if (LLScrollListCell::needsToolTip())
  191. return LLScrollListCell::needsToolTip();
  192. // ...otherwise, show tooltips for truncated text
  193. return mFont->getWidth(mText.getString()) > getWidth();
  194. }
  195. //virtual
  196. BOOL LLScrollListText::getVisible() const
  197. {
  198. return mVisible;
  199. }
  200. //virtual
  201. S32 LLScrollListText::getHeight() const
  202. {
  203. return llround(mFont->getLineHeight());
  204. }
  205. LLScrollListText::~LLScrollListText()
  206. {
  207. sCount--;
  208. }
  209. S32 LLScrollListText::getContentWidth() const
  210. {
  211. return mFont->getWidth(mText.getString());
  212. }
  213. void LLScrollListText::setColor(const LLColor4& color)
  214. {
  215. mColor = color;
  216. mUseColor = TRUE;
  217. }
  218. void LLScrollListText::setText(const LLStringExplicit& text)
  219. {
  220. mText = text;
  221. }
  222. void LLScrollListText::setFontStyle(const U8 font_style)
  223. {
  224. LLFontDescriptor new_desc(mFont->getFontDesc());
  225. new_desc.setStyle(font_style);
  226. mFont = LLFontGL::getFont(new_desc);
  227. }
  228. //virtual
  229. void LLScrollListText::setValue(const LLSD& text)
  230. {
  231. setText(text.asString());
  232. }
  233. //virtual
  234. const LLSD LLScrollListText::getValue() const
  235. {
  236. return LLSD(mText.getString());
  237. }
  238. void LLScrollListText::draw(const LLColor4& color, const LLColor4& highlight_color) const
  239. {
  240. LLColor4 display_color;
  241. if (mUseColor)
  242. {
  243. display_color = mColor;
  244. }
  245. else
  246. {
  247. display_color = color;
  248. }
  249. if (mHighlightCount > 0)
  250. {
  251. S32 left = 0;
  252. switch(mFontAlignment)
  253. {
  254. case LLFontGL::LEFT:
  255. left = mFont->getWidth(mText.getString(), 0, mHighlightOffset);
  256. break;
  257. case LLFontGL::RIGHT:
  258. left = getWidth() - mFont->getWidth(mText.getString(), mHighlightOffset, S32_MAX);
  259. break;
  260. case LLFontGL::HCENTER:
  261. left = (getWidth() - mFont->getWidth(mText.getString())) / 2;
  262. break;
  263. }
  264. LLRect highlight_rect(left - 2,
  265. llround(mFont->getLineHeight()) + 1,
  266. left + mFont->getWidth(mText.getString(), mHighlightOffset, mHighlightCount) + 1,
  267. 1);
  268. mRoundedRectImage->draw(highlight_rect, highlight_color);
  269. }
  270. // Try to draw the entire string
  271. F32 right_x;
  272. U32 string_chars = mText.length();
  273. F32 start_x = 0.f;
  274. switch(mFontAlignment)
  275. {
  276. case LLFontGL::LEFT:
  277. start_x = 0.f;
  278. break;
  279. case LLFontGL::RIGHT:
  280. start_x = (F32)getWidth();
  281. break;
  282. case LLFontGL::HCENTER:
  283. start_x = (F32)getWidth() * 0.5f;
  284. break;
  285. }
  286. mFont->render(mText.getWString(), 0,
  287. start_x, 2.f,
  288. display_color,
  289. mFontAlignment,
  290. LLFontGL::BOTTOM,
  291. 0,
  292. LLFontGL::NO_SHADOW,
  293. string_chars,
  294. getTextWidth(),
  295. &right_x,
  296. TRUE);
  297. }
  298. //
  299. // LLScrollListCheck
  300. //
  301. LLScrollListCheck::LLScrollListCheck(const LLScrollListCell::Params& p)
  302. : LLScrollListCell(p)
  303. {
  304. LLCheckBoxCtrl::Params checkbox_p;
  305. checkbox_p.name("checkbox");
  306. checkbox_p.rect = LLRect(0, p.width, p.width, 0);
  307. checkbox_p.enabled(p.enabled);
  308. checkbox_p.initial_value(p.value());
  309. mCheckBox = LLUICtrlFactory::create<LLCheckBoxCtrl>(checkbox_p);
  310. LLRect rect(mCheckBox->getRect());
  311. if (p.width)
  312. {
  313. rect.mRight = rect.mLeft + p.width;
  314. mCheckBox->setRect(rect);
  315. setWidth(p.width);
  316. }
  317. else
  318. {
  319. setWidth(rect.getWidth()); //check_box->getWidth();
  320. }
  321. mCheckBox->setColor(p.color);
  322. }
  323. LLScrollListCheck::~LLScrollListCheck()
  324. {
  325. delete mCheckBox;
  326. mCheckBox = NULL;
  327. }
  328. void LLScrollListCheck::draw(const LLColor4& color, const LLColor4& highlight_color) const
  329. {
  330. mCheckBox->draw();
  331. }
  332. BOOL LLScrollListCheck::handleClick()
  333. {
  334. if (mCheckBox->getEnabled())
  335. {
  336. mCheckBox->toggle();
  337. }
  338. // don't change selection when clicking on embedded checkbox
  339. return TRUE;
  340. }
  341. /*virtual*/
  342. const LLSD LLScrollListCheck::getValue() const
  343. {
  344. return mCheckBox->getValue();
  345. }
  346. /*virtual*/
  347. void LLScrollListCheck::setValue(const LLSD& value)
  348. {
  349. mCheckBox->setValue(value);
  350. }
  351. /*virtual*/
  352. void LLScrollListCheck::onCommit()
  353. {
  354. mCheckBox->onCommit();
  355. }
  356. /*virtual*/
  357. void LLScrollListCheck::setEnabled(BOOL enable)
  358. {
  359. mCheckBox->setEnabled(enable);
  360. }
  361. //
  362. // LLScrollListDate
  363. //
  364. LLScrollListDate::LLScrollListDate( const LLScrollListCell::Params& p)
  365. : LLScrollListText(p),
  366. mDate(p.value().asDate())
  367. {}
  368. void LLScrollListDate::setValue(const LLSD& value)
  369. {
  370. mDate = value.asDate();
  371. LLScrollListText::setValue(mDate.asRFC1123());
  372. }
  373. const LLSD LLScrollListDate::getValue() const
  374. {
  375. return mDate;
  376. }