/indra/llui/llcheckboxctrl.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 252 lines · 161 code · 41 blank · 50 comment · 6 complexity · 7c859a97aa9f8ba59bc9df39468adede MD5 · raw file

  1. /**
  2. * @file llcheckboxctrl.cpp
  3. * @brief LLCheckBoxCtrl base class
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. // The mutants are coming!
  27. #include "linden_common.h"
  28. #define LLCHECKBOXCTRL_CPP
  29. #include "llcheckboxctrl.h"
  30. #include "llgl.h"
  31. #include "llui.h"
  32. #include "lluiconstants.h"
  33. #include "lluictrlfactory.h"
  34. #include "llcontrol.h"
  35. #include "llstring.h"
  36. #include "llfontgl.h"
  37. #include "lltextbox.h"
  38. #include "llkeyboard.h"
  39. const U32 MAX_STRING_LENGTH = 10;
  40. static LLDefaultChildRegistry::Register<LLCheckBoxCtrl> r("check_box");
  41. // Compiler optimization, generate extern template
  42. template class LLCheckBoxCtrl* LLView::getChild<class LLCheckBoxCtrl>(
  43. const std::string& name, BOOL recurse) const;
  44. LLCheckBoxCtrl::Params::Params()
  45. : initial_value("initial_value", false),
  46. label_text("label_text"),
  47. check_button("check_button"),
  48. radio_style("radio_style")
  49. {}
  50. LLCheckBoxCtrl::LLCheckBoxCtrl(const LLCheckBoxCtrl::Params& p)
  51. : LLUICtrl(p),
  52. mTextEnabledColor(p.label_text.text_color()),
  53. mTextDisabledColor(p.label_text.text_readonly_color()),
  54. mFont(p.font())
  55. {
  56. mViewModel->setValue(LLSD(p.initial_value));
  57. mViewModel->resetDirty();
  58. static LLUICachedControl<S32> llcheckboxctrl_spacing ("UICheckboxctrlSpacing", 0);
  59. static LLUICachedControl<S32> llcheckboxctrl_hpad ("UICheckboxctrlHPad", 0);
  60. static LLUICachedControl<S32> llcheckboxctrl_vpad ("UICheckboxctrlVPad", 0);
  61. static LLUICachedControl<S32> llcheckboxctrl_btn_size ("UICheckboxctrlBtnSize", 0);
  62. // must be big enough to hold all children
  63. setUseBoundingRect(TRUE);
  64. // *HACK Get rid of this with SL-55508...
  65. // this allows blank check boxes and radio boxes for now
  66. std::string local_label = p.label;
  67. if(local_label.empty())
  68. {
  69. local_label = " ";
  70. }
  71. LLTextBox::Params tbparams = p.label_text;
  72. tbparams.initial_value(local_label);
  73. if (p.font.isProvided())
  74. {
  75. tbparams.font(p.font);
  76. }
  77. mLabel = LLUICtrlFactory::create<LLTextBox> (tbparams);
  78. mLabel->reshapeToFitText();
  79. addChild(mLabel);
  80. LLRect label_rect = mLabel->getRect();
  81. // Button
  82. // Note: button cover the label by extending all the way to the right.
  83. LLRect btn_rect = p.check_button.rect();
  84. btn_rect.setOriginAndSize(
  85. btn_rect.mLeft,
  86. btn_rect.mBottom,
  87. llmax(btn_rect.mRight, label_rect.mRight - btn_rect.mLeft),
  88. llmax( label_rect.getHeight(), btn_rect.mTop));
  89. std::string active_true_id, active_false_id;
  90. std::string inactive_true_id, inactive_false_id;
  91. LLButton::Params params = p.check_button;
  92. params.rect(btn_rect);
  93. //params.control_name(p.control_name);
  94. params.click_callback.function(boost::bind(&LLCheckBoxCtrl::onButtonPress, this, _2));
  95. params.commit_on_return(false);
  96. // Checkboxes only allow boolean initial values, but buttons can
  97. // take any LLSD.
  98. params.initial_value(LLSD(p.initial_value));
  99. params.follows.flags(FOLLOWS_LEFT | FOLLOWS_BOTTOM);
  100. mButton = LLUICtrlFactory::create<LLButton>(params);
  101. addChild(mButton);
  102. }
  103. LLCheckBoxCtrl::~LLCheckBoxCtrl()
  104. {
  105. // Children all cleaned up by default view destructor.
  106. }
  107. // static
  108. void LLCheckBoxCtrl::onButtonPress( const LLSD& data )
  109. {
  110. //if (mRadioStyle)
  111. //{
  112. // setValue(TRUE);
  113. //}
  114. onCommit();
  115. }
  116. void LLCheckBoxCtrl::onCommit()
  117. {
  118. if( getEnabled() )
  119. {
  120. setTentative(FALSE);
  121. setControlValue(getValue());
  122. LLUICtrl::onCommit();
  123. }
  124. }
  125. void LLCheckBoxCtrl::setEnabled(BOOL b)
  126. {
  127. LLView::setEnabled(b);
  128. if (b)
  129. {
  130. mLabel->setColor( mTextEnabledColor.get() );
  131. }
  132. else
  133. {
  134. mLabel->setColor( mTextDisabledColor.get() );
  135. }
  136. }
  137. void LLCheckBoxCtrl::clear()
  138. {
  139. setValue( FALSE );
  140. }
  141. void LLCheckBoxCtrl::reshape(S32 width, S32 height, BOOL called_from_parent)
  142. {
  143. mLabel->reshapeToFitText();
  144. LLRect label_rect = mLabel->getRect();
  145. // Button
  146. // Note: button cover the label by extending all the way to the right.
  147. LLRect btn_rect = mButton->getRect();
  148. btn_rect.setOriginAndSize(
  149. btn_rect.mLeft,
  150. btn_rect.mBottom,
  151. llmax(btn_rect.getWidth(), label_rect.mRight - btn_rect.mLeft),
  152. llmax(label_rect.mTop - btn_rect.mBottom, btn_rect.getHeight()));
  153. mButton->setShape(btn_rect);
  154. }
  155. //virtual
  156. void LLCheckBoxCtrl::setValue(const LLSD& value )
  157. {
  158. mButton->setValue( value );
  159. }
  160. //virtual
  161. LLSD LLCheckBoxCtrl::getValue() const
  162. {
  163. return mButton->getValue();
  164. }
  165. //virtual
  166. void LLCheckBoxCtrl::setTentative(BOOL b)
  167. {
  168. mButton->setTentative(b);
  169. }
  170. //virtual
  171. BOOL LLCheckBoxCtrl::getTentative() const
  172. {
  173. return mButton->getTentative();
  174. }
  175. void LLCheckBoxCtrl::setLabel( const LLStringExplicit& label )
  176. {
  177. mLabel->setText( label );
  178. reshape(getRect().getWidth(), getRect().getHeight(), FALSE);
  179. }
  180. std::string LLCheckBoxCtrl::getLabel() const
  181. {
  182. return mLabel->getText();
  183. }
  184. BOOL LLCheckBoxCtrl::setLabelArg( const std::string& key, const LLStringExplicit& text )
  185. {
  186. BOOL res = mLabel->setTextArg(key, text);
  187. reshape(getRect().getWidth(), getRect().getHeight(), FALSE);
  188. return res;
  189. }
  190. // virtual
  191. void LLCheckBoxCtrl::setControlName(const std::string& control_name, LLView* context)
  192. {
  193. mButton->setControlName(control_name, context);
  194. }
  195. // virtual Returns TRUE if the user has modified this control.
  196. BOOL LLCheckBoxCtrl::isDirty() const
  197. {
  198. if ( mButton )
  199. {
  200. return mButton->isDirty();
  201. }
  202. return FALSE; // Shouldn't get here
  203. }
  204. // virtual Clear dirty state
  205. void LLCheckBoxCtrl::resetDirty()
  206. {
  207. if ( mButton )
  208. {
  209. mButton->resetDirty();
  210. }
  211. }