llcheckboxctrl.cpp

Go to the documentation of this file.
00001 
00032 // The mutants are coming!
00033 
00034 #include "linden_common.h"
00035 
00036 #include "llcheckboxctrl.h"
00037 
00038 #include "llgl.h"
00039 #include "llui.h"
00040 #include "lluiconstants.h"
00041 #include "lluictrlfactory.h"
00042 #include "llcontrol.h"
00043 
00044 #include "llstring.h"
00045 #include "llfontgl.h"
00046 #include "lltextbox.h"
00047 #include "llkeyboard.h"
00048 #include "llviewborder.h"
00049 
00050 const U32 MAX_STRING_LENGTH = 10;
00051 
00052  
00053 LLCheckBoxCtrl::LLCheckBoxCtrl(const LLString& name, const LLRect& rect, 
00054                                                             const LLString& label, 
00055                                                                 const LLFontGL* font,
00056                                                                 void (*commit_callback)(LLUICtrl* ctrl, void* userdata),
00057                                                                 void* callback_user_data,
00058                                                                 BOOL initial_value,
00059                                                                 BOOL use_radio_style,
00060                                                                 const LLString& control_which)
00061 :       LLUICtrl(name, rect, TRUE, commit_callback, callback_user_data, FOLLOWS_LEFT | FOLLOWS_TOP),
00062         mTextEnabledColor( LLUI::sColorsGroup->getColor( "LabelTextColor" ) ),
00063         mTextDisabledColor( LLUI::sColorsGroup->getColor( "LabelDisabledColor" ) ),
00064         mRadioStyle( use_radio_style ),
00065         mInitialValue( initial_value ),
00066         mSetValue( initial_value )
00067 {
00068         if (font)
00069         {
00070                 mFont = font;
00071         }
00072         else
00073         {
00074                 mFont = LLFontGL::sSansSerifSmall;
00075         }
00076 
00077         // must be big enough to hold all children
00078         setSpanChildren(TRUE);
00079 
00080         mKeyboardFocusOnClick = TRUE;
00081 
00082         // Label (add a little space to make sure text actually renders)
00083         const S32 FUDGE = 10;
00084         S32 text_width = mFont->getWidth( label ) + FUDGE;
00085         S32 text_height = llround(mFont->getLineHeight());
00086         LLRect label_rect;
00087         label_rect.setOriginAndSize(
00088                 LLCHECKBOXCTRL_HPAD + LLCHECKBOXCTRL_BTN_SIZE + LLCHECKBOXCTRL_SPACING,
00089                 LLCHECKBOXCTRL_VPAD + 1, // padding to get better alignment
00090                 text_width + LLCHECKBOXCTRL_HPAD,
00091                 text_height );
00092         mLabel = new LLTextBox( "CheckboxCtrl Label", label_rect, label.c_str(), mFont );
00093         mLabel->setFollowsLeft();
00094         mLabel->setFollowsBottom();
00095         addChild(mLabel);
00096 
00097         // Button
00098         // Note: button cover the label by extending all the way to the right.
00099         LLRect btn_rect;
00100         btn_rect.setOriginAndSize(
00101                 LLCHECKBOXCTRL_HPAD,
00102                 LLCHECKBOXCTRL_VPAD,
00103                 LLCHECKBOXCTRL_BTN_SIZE + LLCHECKBOXCTRL_SPACING + text_width + LLCHECKBOXCTRL_HPAD,
00104                 llmax( text_height, LLCHECKBOXCTRL_BTN_SIZE ) + LLCHECKBOXCTRL_VPAD);
00105         LLString active_true_id, active_false_id;
00106         LLString inactive_true_id, inactive_false_id;
00107         if (mRadioStyle)
00108         {
00109                 active_true_id = "UIImgRadioActiveSelectedUUID";
00110                 active_false_id = "UIImgRadioActiveUUID";
00111                 inactive_true_id = "UIImgRadioInactiveSelectedUUID";
00112                 inactive_false_id = "UIImgRadioInactiveUUID";
00113                 mButton = new LLButton(
00114                         "Radio control button", btn_rect,
00115                         active_false_id, active_true_id, control_which,
00116                         &LLCheckBoxCtrl::onButtonPress, this, LLFontGL::sSansSerif ); 
00117                 mButton->setDisabledImages( inactive_false_id, inactive_true_id );
00118                 mButton->setHoverGlowStrength(0.35f);
00119         }
00120         else
00121         {
00122                 active_false_id = "UIImgCheckboxActiveUUID";
00123                 active_true_id = "UIImgCheckboxActiveSelectedUUID";
00124                 inactive_true_id = "UIImgCheckboxInactiveSelectedUUID";
00125                 inactive_false_id = "UIImgCheckboxInactiveUUID";
00126                 mButton = new LLButton(
00127                         "Checkbox control button", btn_rect,
00128                         active_false_id, active_true_id, control_which,
00129                         &LLCheckBoxCtrl::onButtonPress, this, LLFontGL::sSansSerif ); 
00130                 mButton->setDisabledImages( inactive_false_id, inactive_true_id );
00131                 mButton->setHoverGlowStrength(0.35f);
00132         }
00133         mButton->setToggleState( initial_value );
00134         mButton->setFollowsLeft();
00135         mButton->setFollowsBottom();
00136         mButton->setCommitOnReturn(FALSE);
00137         addChild(mButton);
00138 }
00139 
00140 LLCheckBoxCtrl::~LLCheckBoxCtrl()
00141 {
00142         // Children all cleaned up by default view destructor.
00143 }
00144 
00145 
00146 // static
00147 void LLCheckBoxCtrl::onButtonPress( void *userdata )
00148 {
00149         LLCheckBoxCtrl* self = (LLCheckBoxCtrl*) userdata;
00150 
00151         if (self->mRadioStyle)
00152         {
00153                 if (!self->getValue())
00154                 {
00155                         self->setValue(TRUE);
00156                 }
00157         }
00158         else
00159         {
00160                 self->toggle();
00161         }
00162         self->setControlValue(self->getValue());
00163         self->onCommit();
00164 
00165         if (self->mKeyboardFocusOnClick)
00166         {
00167                 self->setFocus( TRUE );
00168                 self->onFocusReceived();
00169         }
00170 }
00171 
00172 void LLCheckBoxCtrl::onCommit()
00173 {
00174         if( getEnabled() )
00175         {
00176                 setTentative(FALSE);
00177                 LLUICtrl::onCommit();
00178         }
00179 }
00180 
00181 void LLCheckBoxCtrl::setEnabled(BOOL b)
00182 {
00183         LLView::setEnabled(b);
00184         mButton->setEnabled(b);
00185 }
00186 
00187 void LLCheckBoxCtrl::clear()
00188 {
00189         setValue( FALSE );
00190 }
00191 
00192 void LLCheckBoxCtrl::reshape(S32 width, S32 height, BOOL called_from_parent)
00193 {
00194         //stretch or shrink bounding rectangle of label when rebuilding UI at new scale
00195         const S32 FUDGE = 10;
00196         S32 text_width = mFont->getWidth( mLabel->getText() ) + FUDGE;
00197         S32 text_height = llround(mFont->getLineHeight());
00198         LLRect label_rect;
00199         label_rect.setOriginAndSize(
00200                 LLCHECKBOXCTRL_HPAD + LLCHECKBOXCTRL_BTN_SIZE + LLCHECKBOXCTRL_SPACING,
00201                 LLCHECKBOXCTRL_VPAD,
00202                 text_width,
00203                 text_height );
00204         mLabel->setRect(label_rect);
00205 
00206         LLRect btn_rect;
00207         btn_rect.setOriginAndSize(
00208                 LLCHECKBOXCTRL_HPAD,
00209                 LLCHECKBOXCTRL_VPAD,
00210                 LLCHECKBOXCTRL_BTN_SIZE + LLCHECKBOXCTRL_SPACING + text_width,
00211                 llmax( text_height, LLCHECKBOXCTRL_BTN_SIZE ) );
00212         mButton->setRect( btn_rect );
00213         
00214         LLUICtrl::reshape(width, height, called_from_parent);
00215 }
00216 
00217 void LLCheckBoxCtrl::draw()
00218 {
00219         if (mEnabled)
00220         {
00221                 mLabel->setColor( mTextEnabledColor );
00222         }
00223         else
00224         {
00225                 mLabel->setColor( mTextDisabledColor );
00226         }
00227 
00228         // Draw children
00229         LLUICtrl::draw();
00230 }
00231 
00232 //virtual
00233 void LLCheckBoxCtrl::setValue(const LLSD& value )
00234 {
00235         mSetValue = value.asBoolean();
00236         mButton->setToggleState( mSetValue );
00237 }
00238 
00239 //virtual
00240 LLSD LLCheckBoxCtrl::getValue() const
00241 {
00242         return mButton->getToggleState();
00243 }
00244 
00245 void LLCheckBoxCtrl::setLabel( const LLStringExplicit& label )
00246 {
00247         mLabel->setText( label );
00248         reshape(getRect().getWidth(), getRect().getHeight(), FALSE);
00249 }
00250 
00251 LLString LLCheckBoxCtrl::getLabel() const
00252 {
00253         return mLabel->getText();
00254 }
00255 
00256 BOOL LLCheckBoxCtrl::setLabelArg( const LLString& key, const LLStringExplicit& text )
00257 {
00258         BOOL res = mLabel->setTextArg(key, text);
00259         reshape(getRect().getWidth(), getRect().getHeight(), FALSE);
00260         return res;
00261 }
00262 
00263 //virtual
00264 LLString LLCheckBoxCtrl::getControlName() const
00265 {
00266         return mButton->getControlName();
00267 }
00268 
00269 // virtual
00270 void LLCheckBoxCtrl::setControlName(const LLString& control_name, LLView* context)
00271 {
00272         mButton->setControlName(control_name, context);
00273 }
00274 
00275 
00276 // virtual              Returns TRUE if the user has modified this control.
00277 BOOL     LLCheckBoxCtrl::isDirty() const
00278 {
00279         if ( mButton )
00280         {
00281                 return (mSetValue != mButton->getToggleState());
00282         }
00283         return FALSE;           // Shouldn't get here
00284 }
00285 
00286 
00287 // virtual                      Clear dirty state
00288 void    LLCheckBoxCtrl::resetDirty()
00289 {
00290         if ( mButton )
00291         {
00292                 mSetValue = mButton->getToggleState();
00293         }
00294 }
00295 
00296 
00297 
00298 // virtual
00299 LLXMLNodePtr LLCheckBoxCtrl::getXML(bool save_children) const
00300 {
00301         LLXMLNodePtr node = LLUICtrl::getXML();
00302 
00303         node->createChild("label", TRUE)->setStringValue(mLabel->getText());
00304 
00305         LLString control_name = mButton->getControlName();
00306         
00307         node->createChild("initial_value", TRUE)->setBoolValue(mInitialValue);
00308 
00309         node->createChild("font", TRUE)->setStringValue(LLFontGL::nameFromFont(mFont));
00310 
00311         node->createChild("radio_style", TRUE)->setBoolValue(mRadioStyle);
00312 
00313         return node;
00314 }
00315 
00316 // static
00317 LLView* LLCheckBoxCtrl::fromXML(LLXMLNodePtr node, LLView *parent, LLUICtrlFactory *factory)
00318 {
00319         LLString name("checkbox");
00320         node->getAttributeString("name", name);
00321 
00322         LLString label("");
00323         node->getAttributeString("label", label);
00324 
00325         LLFontGL* font = LLView::selectFont(node);
00326 
00327         BOOL radio_style = FALSE;
00328         node->getAttributeBOOL("radio_style", radio_style);
00329 
00330         LLUICtrlCallback callback = NULL;
00331 
00332         if (label.empty())
00333         {
00334                 label.assign(node->getTextContents());
00335         }
00336 
00337         LLRect rect;
00338         createRect(node, rect, parent, LLRect());
00339 
00340         LLCheckBoxCtrl* checkbox = new LLCheckboxCtrl(name, 
00341                 rect, 
00342                 label,
00343                 font,
00344                 callback,
00345                 NULL,
00346                 FALSE,
00347                 radio_style); // if true, draw radio button style icons
00348 
00349         BOOL initial_value = checkbox->getValue().asBoolean();
00350         node->getAttributeBOOL("initial_value", initial_value);
00351 
00352         LLColor4 color;
00353         color = LLUI::sColorsGroup->getColor( "LabelTextColor" );
00354         LLUICtrlFactory::getAttributeColor(node,"text_enabled_color", color);
00355         checkbox->setEnabledColor(color);
00356 
00357         color = LLUI::sColorsGroup->getColor( "LabelDisabledColor" );
00358         LLUICtrlFactory::getAttributeColor(node,"text_disabled_color", color);
00359         checkbox->setDisabledColor(color);
00360 
00361         checkbox->setValue(initial_value);
00362 
00363         checkbox->initFromXML(node, parent);
00364 
00365         return checkbox;
00366 }

Generated on Thu Jul 1 06:08:21 2010 for Second Life Viewer by  doxygen 1.4.7