PageRenderTime 39ms CodeModel.GetById 15ms app.highlight 19ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 27// The mutants are coming!
 28#include "linden_common.h"
 29
 30#define LLCHECKBOXCTRL_CPP
 31#include "llcheckboxctrl.h"
 32
 33#include "llgl.h"
 34#include "llui.h"
 35#include "lluiconstants.h"
 36#include "lluictrlfactory.h"
 37#include "llcontrol.h"
 38
 39#include "llstring.h"
 40#include "llfontgl.h"
 41#include "lltextbox.h"
 42#include "llkeyboard.h"
 43
 44const U32 MAX_STRING_LENGTH = 10;
 45
 46static LLDefaultChildRegistry::Register<LLCheckBoxCtrl> r("check_box");
 47
 48// Compiler optimization, generate extern template
 49template class LLCheckBoxCtrl* LLView::getChild<class LLCheckBoxCtrl>(
 50	const std::string& name, BOOL recurse) const;
 51
 52LLCheckBoxCtrl::Params::Params()
 53:	initial_value("initial_value", false),
 54	label_text("label_text"),
 55	check_button("check_button"),
 56	radio_style("radio_style")
 57{}
 58
 59
 60LLCheckBoxCtrl::LLCheckBoxCtrl(const LLCheckBoxCtrl::Params& p)
 61:	LLUICtrl(p),
 62	mTextEnabledColor(p.label_text.text_color()),
 63	mTextDisabledColor(p.label_text.text_readonly_color()),
 64	mFont(p.font())
 65{
 66	mViewModel->setValue(LLSD(p.initial_value));
 67	mViewModel->resetDirty();
 68	static LLUICachedControl<S32> llcheckboxctrl_spacing ("UICheckboxctrlSpacing", 0);
 69	static LLUICachedControl<S32> llcheckboxctrl_hpad ("UICheckboxctrlHPad", 0);
 70	static LLUICachedControl<S32> llcheckboxctrl_vpad ("UICheckboxctrlVPad", 0);
 71	static LLUICachedControl<S32> llcheckboxctrl_btn_size ("UICheckboxctrlBtnSize", 0);
 72
 73	// must be big enough to hold all children
 74	setUseBoundingRect(TRUE);
 75
 76	// *HACK Get rid of this with SL-55508... 
 77	// this allows blank check boxes and radio boxes for now
 78	std::string local_label = p.label;
 79	if(local_label.empty())
 80	{
 81		local_label = " ";
 82	}
 83
 84	LLTextBox::Params tbparams = p.label_text;
 85	tbparams.initial_value(local_label);
 86	if (p.font.isProvided())
 87	{
 88		tbparams.font(p.font);
 89	}
 90	mLabel = LLUICtrlFactory::create<LLTextBox> (tbparams);
 91	mLabel->reshapeToFitText();
 92	addChild(mLabel);
 93
 94	LLRect label_rect = mLabel->getRect();
 95
 96	// Button
 97	// Note: button cover the label by extending all the way to the right.
 98	LLRect btn_rect = p.check_button.rect();
 99	btn_rect.setOriginAndSize(
100		btn_rect.mLeft,
101		btn_rect.mBottom,
102		llmax(btn_rect.mRight, label_rect.mRight - btn_rect.mLeft),
103		llmax( label_rect.getHeight(), btn_rect.mTop));
104	std::string active_true_id, active_false_id;
105	std::string inactive_true_id, inactive_false_id;
106
107	LLButton::Params params = p.check_button;
108	params.rect(btn_rect);
109	//params.control_name(p.control_name);
110	params.click_callback.function(boost::bind(&LLCheckBoxCtrl::onButtonPress, this, _2));
111	params.commit_on_return(false);
112	// Checkboxes only allow boolean initial values, but buttons can
113	// take any LLSD.
114	params.initial_value(LLSD(p.initial_value));
115	params.follows.flags(FOLLOWS_LEFT | FOLLOWS_BOTTOM);
116
117	mButton = LLUICtrlFactory::create<LLButton>(params);
118	addChild(mButton);
119}
120
121LLCheckBoxCtrl::~LLCheckBoxCtrl()
122{
123	// Children all cleaned up by default view destructor.
124}
125
126
127// static
128void LLCheckBoxCtrl::onButtonPress( const LLSD& data )
129{
130	//if (mRadioStyle)
131	//{
132	//	setValue(TRUE);
133	//}
134
135	onCommit();
136}
137
138void LLCheckBoxCtrl::onCommit()
139{
140	if( getEnabled() )
141	{
142		setTentative(FALSE);
143		setControlValue(getValue());
144		LLUICtrl::onCommit();
145	}
146}
147
148void LLCheckBoxCtrl::setEnabled(BOOL b)
149{
150	LLView::setEnabled(b);
151
152	if (b)
153	{
154		mLabel->setColor( mTextEnabledColor.get() );
155	}
156	else
157	{
158		mLabel->setColor( mTextDisabledColor.get() );
159	}
160}
161
162void LLCheckBoxCtrl::clear()
163{
164	setValue( FALSE );
165}
166
167void LLCheckBoxCtrl::reshape(S32 width, S32 height, BOOL called_from_parent)
168{
169
170	mLabel->reshapeToFitText();
171
172	LLRect label_rect = mLabel->getRect();
173
174	// Button
175	// Note: button cover the label by extending all the way to the right.
176	LLRect btn_rect = mButton->getRect();
177	btn_rect.setOriginAndSize(
178		btn_rect.mLeft,
179		btn_rect.mBottom,
180		llmax(btn_rect.getWidth(), label_rect.mRight - btn_rect.mLeft),
181		llmax(label_rect.mTop - btn_rect.mBottom, btn_rect.getHeight()));
182	mButton->setShape(btn_rect);
183}
184
185//virtual
186void LLCheckBoxCtrl::setValue(const LLSD& value )
187{
188	mButton->setValue( value );
189}
190
191//virtual
192LLSD LLCheckBoxCtrl::getValue() const
193{
194	return mButton->getValue();
195}
196
197//virtual
198void LLCheckBoxCtrl::setTentative(BOOL b)
199{
200	mButton->setTentative(b);
201}
202
203//virtual
204BOOL LLCheckBoxCtrl::getTentative() const
205{
206	return mButton->getTentative();
207}
208
209void LLCheckBoxCtrl::setLabel( const LLStringExplicit& label )
210{
211	mLabel->setText( label );
212	reshape(getRect().getWidth(), getRect().getHeight(), FALSE);
213}
214
215std::string LLCheckBoxCtrl::getLabel() const
216{
217	return mLabel->getText();
218}
219
220BOOL LLCheckBoxCtrl::setLabelArg( const std::string& key, const LLStringExplicit& text )
221{
222	BOOL res = mLabel->setTextArg(key, text);
223	reshape(getRect().getWidth(), getRect().getHeight(), FALSE);
224	return res;
225}
226
227// virtual
228void LLCheckBoxCtrl::setControlName(const std::string& control_name, LLView* context)
229{
230	mButton->setControlName(control_name, context);
231}
232
233
234// virtual		Returns TRUE if the user has modified this control.
235BOOL	 LLCheckBoxCtrl::isDirty() const
236{
237	if ( mButton )
238	{
239		return mButton->isDirty();
240	}
241	return FALSE;		// Shouldn't get here
242}
243
244
245// virtual			Clear dirty state
246void	LLCheckBoxCtrl::resetDirty()
247{
248	if ( mButton )
249	{
250		mButton->resetDirty();
251	}
252}