diff options
author | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
commit | 38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch) | |
tree | adca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llui/llcheckboxctrl.cpp | |
parent | README.txt (diff) | |
download | meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2 meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz |
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llui/llcheckboxctrl.cpp')
-rw-r--r-- | linden/indra/llui/llcheckboxctrl.cpp | 337 |
1 files changed, 337 insertions, 0 deletions
diff --git a/linden/indra/llui/llcheckboxctrl.cpp b/linden/indra/llui/llcheckboxctrl.cpp new file mode 100644 index 0000000..9f11ff8 --- /dev/null +++ b/linden/indra/llui/llcheckboxctrl.cpp | |||
@@ -0,0 +1,337 @@ | |||
1 | /** | ||
2 | * @file llcheckboxctrl.cpp | ||
3 | * @brief LLCheckBoxCtrl base class | ||
4 | * | ||
5 | * Copyright (c) 2001-2007, Linden Research, Inc. | ||
6 | * | ||
7 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
8 | * to you under the terms of the GNU General Public License, version 2.0 | ||
9 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
10 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
11 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
12 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
13 | * | ||
14 | * There are special exceptions to the terms and conditions of the GPL as | ||
15 | * it is applied to this Source Code. View the full text of the exception | ||
16 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
17 | * online at http://secondlife.com/developers/opensource/flossexception | ||
18 | * | ||
19 | * By copying, modifying or distributing this software, you acknowledge | ||
20 | * that you have read and understood your obligations described above, | ||
21 | * and agree to abide by those obligations. | ||
22 | * | ||
23 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
24 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
25 | * COMPLETENESS OR PERFORMANCE. | ||
26 | */ | ||
27 | |||
28 | // The mutants are coming! | ||
29 | |||
30 | #include "linden_common.h" | ||
31 | |||
32 | #include "llcheckboxctrl.h" | ||
33 | |||
34 | #include "llgl.h" | ||
35 | #include "llui.h" | ||
36 | #include "lluiconstants.h" | ||
37 | #include "lluictrlfactory.h" | ||
38 | #include "llcontrol.h" | ||
39 | |||
40 | #include "llstring.h" | ||
41 | #include "llfontgl.h" | ||
42 | #include "lltextbox.h" | ||
43 | #include "llkeyboard.h" | ||
44 | #include "llviewborder.h" | ||
45 | |||
46 | const U32 MAX_STRING_LENGTH = 10; | ||
47 | |||
48 | |||
49 | LLCheckBoxCtrl::LLCheckBoxCtrl(const LLString& name, const LLRect& rect, | ||
50 | const LLString& label, | ||
51 | const LLFontGL* font, | ||
52 | void (*commit_callback)(LLUICtrl* ctrl, void* userdata), | ||
53 | void* callback_user_data, | ||
54 | BOOL initial_value, | ||
55 | BOOL use_radio_style, | ||
56 | const LLString& control_which) | ||
57 | : LLUICtrl(name, rect, TRUE, commit_callback, callback_user_data, FOLLOWS_LEFT | FOLLOWS_TOP), | ||
58 | mTextEnabledColor( LLUI::sColorsGroup->getColor( "LabelTextColor" ) ), | ||
59 | mTextDisabledColor( LLUI::sColorsGroup->getColor( "LabelDisabledColor" ) ), | ||
60 | mRadioStyle( use_radio_style ), | ||
61 | mInitialValue( initial_value ) | ||
62 | { | ||
63 | if (font) | ||
64 | { | ||
65 | mFont = font; | ||
66 | } | ||
67 | else | ||
68 | { | ||
69 | mFont = LLFontGL::sSansSerifSmall; | ||
70 | } | ||
71 | |||
72 | // must be big enough to hold all children | ||
73 | setSpanChildren(TRUE); | ||
74 | |||
75 | mKeyboardFocusOnClick = TRUE; | ||
76 | |||
77 | // Label (add a little space to make sure text actually renders) | ||
78 | const S32 FUDGE = 10; | ||
79 | S32 text_width = mFont->getWidth( label ) + FUDGE; | ||
80 | S32 text_height = llround(mFont->getLineHeight()); | ||
81 | LLRect label_rect; | ||
82 | label_rect.setOriginAndSize( | ||
83 | LLCHECKBOXCTRL_HPAD + LLCHECKBOXCTRL_BTN_SIZE + LLCHECKBOXCTRL_SPACING, | ||
84 | LLCHECKBOXCTRL_VPAD + 1, // padding to get better alignment | ||
85 | text_width + LLCHECKBOXCTRL_HPAD, | ||
86 | text_height ); | ||
87 | mLabel = new LLTextBox( "CheckboxCtrl Label", label_rect, label.c_str(), mFont ); | ||
88 | mLabel->setFollowsLeft(); | ||
89 | mLabel->setFollowsBottom(); | ||
90 | addChild(mLabel); | ||
91 | |||
92 | // Button | ||
93 | // Note: button cover the label by extending all the way to the right. | ||
94 | LLRect btn_rect; | ||
95 | btn_rect.setOriginAndSize( | ||
96 | LLCHECKBOXCTRL_HPAD, | ||
97 | LLCHECKBOXCTRL_VPAD, | ||
98 | LLCHECKBOXCTRL_BTN_SIZE + LLCHECKBOXCTRL_SPACING + text_width + LLCHECKBOXCTRL_HPAD, | ||
99 | llmax( text_height, LLCHECKBOXCTRL_BTN_SIZE ) + LLCHECKBOXCTRL_VPAD); | ||
100 | LLString active_true_id, active_false_id; | ||
101 | LLString inactive_true_id, inactive_false_id; | ||
102 | if (mRadioStyle) | ||
103 | { | ||
104 | active_true_id = "UIImgRadioActiveSelectedUUID"; | ||
105 | active_false_id = "UIImgRadioActiveUUID"; | ||
106 | inactive_true_id = "UIImgRadioInactiveSelectedUUID"; | ||
107 | inactive_false_id = "UIImgRadioInactiveUUID"; | ||
108 | mButton = new LLButton( | ||
109 | "Radio control button", btn_rect, | ||
110 | active_false_id, active_true_id, control_which, | ||
111 | &LLCheckBoxCtrl::onButtonPress, this, LLFontGL::sSansSerif ); | ||
112 | mButton->setDisabledImages( inactive_false_id, inactive_true_id ); | ||
113 | mButton->setHoverGlowStrength(0.35f); | ||
114 | } | ||
115 | else | ||
116 | { | ||
117 | active_false_id = "UIImgCheckboxActiveUUID"; | ||
118 | active_true_id = "UIImgCheckboxActiveSelectedUUID"; | ||
119 | inactive_true_id = "UIImgCheckboxInactiveSelectedUUID"; | ||
120 | inactive_false_id = "UIImgCheckboxInactiveUUID"; | ||
121 | mButton = new LLButton( | ||
122 | "Checkbox control button", btn_rect, | ||
123 | active_false_id, active_true_id, control_which, | ||
124 | &LLCheckBoxCtrl::onButtonPress, this, LLFontGL::sSansSerif ); | ||
125 | mButton->setDisabledImages( inactive_false_id, inactive_true_id ); | ||
126 | mButton->setHoverGlowStrength(0.35f); | ||
127 | } | ||
128 | mButton->setToggleState( initial_value ); | ||
129 | mButton->setFollowsLeft(); | ||
130 | mButton->setFollowsBottom(); | ||
131 | mButton->setCommitOnReturn(FALSE); | ||
132 | addChild(mButton); | ||
133 | } | ||
134 | |||
135 | LLCheckBoxCtrl::~LLCheckBoxCtrl() | ||
136 | { | ||
137 | // Children all cleaned up by default view destructor. | ||
138 | } | ||
139 | |||
140 | |||
141 | // static | ||
142 | void LLCheckBoxCtrl::onButtonPress( void *userdata ) | ||
143 | { | ||
144 | LLCheckBoxCtrl* self = (LLCheckBoxCtrl*) userdata; | ||
145 | |||
146 | if (self->mRadioStyle) | ||
147 | { | ||
148 | if (!self->getValue()) | ||
149 | { | ||
150 | self->setValue(TRUE); | ||
151 | } | ||
152 | } | ||
153 | else | ||
154 | { | ||
155 | self->toggle(); | ||
156 | } | ||
157 | self->setControlValue(self->getValue()); | ||
158 | self->onCommit(); | ||
159 | |||
160 | if (self->mKeyboardFocusOnClick) | ||
161 | { | ||
162 | self->setFocus( TRUE ); | ||
163 | self->onFocusReceived(); | ||
164 | } | ||
165 | } | ||
166 | |||
167 | void LLCheckBoxCtrl::onCommit() | ||
168 | { | ||
169 | if( getEnabled() ) | ||
170 | { | ||
171 | setTentative(FALSE); | ||
172 | LLUICtrl::onCommit(); | ||
173 | } | ||
174 | } | ||
175 | |||
176 | void LLCheckBoxCtrl::setEnabled(BOOL b) | ||
177 | { | ||
178 | LLUICtrl::setEnabled(b); | ||
179 | mButton->setEnabled(b); | ||
180 | } | ||
181 | |||
182 | void LLCheckBoxCtrl::clear() | ||
183 | { | ||
184 | setValue( FALSE ); | ||
185 | } | ||
186 | |||
187 | void LLCheckBoxCtrl::reshape(S32 width, S32 height, BOOL called_from_parent) | ||
188 | { | ||
189 | //stretch or shrink bounding rectangle of label when rebuilding UI at new scale | ||
190 | const S32 FUDGE = 10; | ||
191 | S32 text_width = mFont->getWidth( mLabel->getText() ) + FUDGE; | ||
192 | S32 text_height = llround(mFont->getLineHeight()); | ||
193 | LLRect label_rect; | ||
194 | label_rect.setOriginAndSize( | ||
195 | LLCHECKBOXCTRL_HPAD + LLCHECKBOXCTRL_BTN_SIZE + LLCHECKBOXCTRL_SPACING, | ||
196 | LLCHECKBOXCTRL_VPAD, | ||
197 | text_width, | ||
198 | text_height ); | ||
199 | mLabel->setRect(label_rect); | ||
200 | |||
201 | LLRect btn_rect; | ||
202 | btn_rect.setOriginAndSize( | ||
203 | LLCHECKBOXCTRL_HPAD, | ||
204 | LLCHECKBOXCTRL_VPAD, | ||
205 | LLCHECKBOXCTRL_BTN_SIZE + LLCHECKBOXCTRL_SPACING + text_width, | ||
206 | llmax( text_height, LLCHECKBOXCTRL_BTN_SIZE ) ); | ||
207 | mButton->setRect( btn_rect ); | ||
208 | |||
209 | LLUICtrl::reshape(width, height, called_from_parent); | ||
210 | } | ||
211 | |||
212 | void LLCheckBoxCtrl::draw() | ||
213 | { | ||
214 | if (mEnabled) | ||
215 | { | ||
216 | mLabel->setColor( mTextEnabledColor ); | ||
217 | } | ||
218 | else | ||
219 | { | ||
220 | mLabel->setColor( mTextDisabledColor ); | ||
221 | } | ||
222 | |||
223 | // Draw children | ||
224 | LLUICtrl::draw(); | ||
225 | } | ||
226 | |||
227 | //virtual | ||
228 | void LLCheckBoxCtrl::setValue(const LLSD& value ) | ||
229 | { | ||
230 | mButton->setToggleState( value.asBoolean() ); | ||
231 | } | ||
232 | |||
233 | //virtual | ||
234 | LLSD LLCheckBoxCtrl::getValue() const | ||
235 | { | ||
236 | return mButton->getToggleState(); | ||
237 | } | ||
238 | |||
239 | void LLCheckBoxCtrl::setLabel( const LLString& label ) | ||
240 | { | ||
241 | mLabel->setText( label ); | ||
242 | reshape(getRect().getWidth(), getRect().getHeight(), FALSE); | ||
243 | } | ||
244 | |||
245 | LLString LLCheckBoxCtrl::getLabel() const | ||
246 | { | ||
247 | return mLabel->getText(); | ||
248 | } | ||
249 | |||
250 | BOOL LLCheckBoxCtrl::setLabelArg( const LLString& key, const LLString& text ) | ||
251 | { | ||
252 | BOOL res = mLabel->setTextArg(key, text); | ||
253 | reshape(getRect().getWidth(), getRect().getHeight(), FALSE); | ||
254 | return res; | ||
255 | } | ||
256 | |||
257 | //virtual | ||
258 | LLString LLCheckBoxCtrl::getControlName() const | ||
259 | { | ||
260 | return mButton->getControlName(); | ||
261 | } | ||
262 | |||
263 | // virtual | ||
264 | void LLCheckBoxCtrl::setControlName(const LLString& control_name, LLView* context) | ||
265 | { | ||
266 | mButton->setControlName(control_name, context); | ||
267 | } | ||
268 | |||
269 | // virtual | ||
270 | LLXMLNodePtr LLCheckBoxCtrl::getXML(bool save_children) const | ||
271 | { | ||
272 | LLXMLNodePtr node = LLUICtrl::getXML(); | ||
273 | |||
274 | node->createChild("label", TRUE)->setStringValue(mLabel->getText()); | ||
275 | |||
276 | LLString control_name = mButton->getControlName(); | ||
277 | |||
278 | node->createChild("initial_value", TRUE)->setBoolValue(mInitialValue); | ||
279 | |||
280 | node->createChild("font", TRUE)->setStringValue(LLFontGL::nameFromFont(mFont)); | ||
281 | |||
282 | node->createChild("radio_style", TRUE)->setBoolValue(mRadioStyle); | ||
283 | |||
284 | return node; | ||
285 | } | ||
286 | |||
287 | // static | ||
288 | LLView* LLCheckBoxCtrl::fromXML(LLXMLNodePtr node, LLView *parent, LLUICtrlFactory *factory) | ||
289 | { | ||
290 | LLString name("checkbox"); | ||
291 | node->getAttributeString("name", name); | ||
292 | |||
293 | LLString label(""); | ||
294 | node->getAttributeString("label", label); | ||
295 | |||
296 | LLFontGL* font = LLView::selectFont(node); | ||
297 | |||
298 | BOOL radio_style = FALSE; | ||
299 | node->getAttributeBOOL("radio_style", radio_style); | ||
300 | |||
301 | LLUICtrlCallback callback = NULL; | ||
302 | |||
303 | if (label.empty()) | ||
304 | { | ||
305 | label.assign(node->getTextContents()); | ||
306 | } | ||
307 | |||
308 | LLRect rect; | ||
309 | createRect(node, rect, parent, LLRect()); | ||
310 | |||
311 | LLCheckBoxCtrl* checkbox = new LLCheckboxCtrl(name, | ||
312 | rect, | ||
313 | label, | ||
314 | font, | ||
315 | callback, | ||
316 | NULL, | ||
317 | FALSE, | ||
318 | radio_style); // if true, draw radio button style icons | ||
319 | |||
320 | BOOL initial_value = checkbox->getValue().asBoolean(); | ||
321 | node->getAttributeBOOL("initial_value", initial_value); | ||
322 | |||
323 | LLColor4 color; | ||
324 | color = LLUI::sColorsGroup->getColor( "LabelTextColor" ); | ||
325 | LLUICtrlFactory::getAttributeColor(node,"text_enabled_color", color); | ||
326 | checkbox->setEnabledColor(color); | ||
327 | |||
328 | color = LLUI::sColorsGroup->getColor( "LabelDisabledColor" ); | ||
329 | LLUICtrlFactory::getAttributeColor(node,"text_disabled_color", color); | ||
330 | checkbox->setDisabledColor(color); | ||
331 | |||
332 | checkbox->setValue(initial_value); | ||
333 | |||
334 | checkbox->initFromXML(node, parent); | ||
335 | |||
336 | return checkbox; | ||
337 | } | ||