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

Generated on Fri May 16 08:32:51 2008 for SecondLife by  doxygen 1.5.5