Button.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /* GG is a GUI for SDL and OpenGL.
00003    Copyright (C) 2003 T. Zachary Laine
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public License
00007    as published by the Free Software Foundation; either version 2.1
00008    of the License, or (at your option) any later version.
00009    
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014     
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA
00019 
00020    If you do not wish to comply with the terms of the LGPL please
00021    contact the author as other terms are available for a fee.
00022     
00023    Zach Laine
00024    whatwasthataddress@hotmail.com */
00025 
00030 #ifndef _GG_Button_h_
00031 #define _GG_Button_h_
00032 
00033 #include <GG/TextControl.h>
00034 
00035 #include <boost/serialization/version.hpp>
00036 
00037 
00038 namespace GG {
00039 
00044 class GG_API Button : public TextControl
00045 {
00046 public:
00048     enum ButtonState {
00049         BN_PRESSED,    
00050         BN_UNPRESSED,  
00051         BN_ROLLOVER    
00052     };
00053  
00055     typedef boost::signal<void ()> ClickedSignalType; 
00056 
00057  
00059     typedef ClickedSignalType::slot_type ClickedSlotType; 
00060 
00061  
00063     Button(int x, int y, int w, int h, const std::string& str, const boost::shared_ptr<Font>& font, Clr color,
00064            Clr text_color = CLR_BLACK, Flags<WndFlag> flags = CLICKABLE); 
00065 
00066  
00068 
00069     ButtonState       State() const;
00070 
00071     const SubTexture& UnpressedGraphic() const; 
00072     const SubTexture& PressedGraphic() const;   
00073     const SubTexture& RolloverGraphic() const;  
00074 
00075     mutable ClickedSignalType ClickedSignal; 
00076 
00077  
00079     virtual void   Render();
00080     virtual void   LButtonDown(const Pt& pt, Flags<ModKey> mod_keys);
00081     virtual void   LDrag(const Pt& pt, const Pt& move, Flags<ModKey> mod_keys);
00082     virtual void   LButtonUp(const Pt& pt, Flags<ModKey> mod_keys);
00083     virtual void   LClick(const Pt& pt, Flags<ModKey> mod_keys);
00084     virtual void   MouseHere(const Pt& pt, Flags<ModKey> mod_keys);
00085     virtual void   MouseLeave();
00086 
00087     virtual void   SetColor(Clr c); 
00088 
00090     void           SetState(ButtonState state);
00091 
00092     void           SetUnpressedGraphic(const SubTexture& st); 
00093     void           SetPressedGraphic(const SubTexture& st);   
00094     void           SetRolloverGraphic(const SubTexture& st);  
00095 
00096     virtual void   DefineAttributes(WndEditor* editor);
00098 
00099 protected: 
00101     Button(); 
00102 
00103  
00105     virtual void   RenderUnpressed();   
00106     virtual void   RenderPressed();     
00107     virtual void   RenderRollover();    
00108 
00109 
00110 private:
00111     void           RenderDefault();     
00112 
00113     ButtonState    m_state;             
00114 
00115     SubTexture     m_unpressed_graphic; 
00116     SubTexture     m_pressed_graphic;   
00117     SubTexture     m_rollover_graphic;  
00118 
00119     friend class boost::serialization::access;
00120     template <class Archive>
00121     void serialize(Archive& ar, const unsigned int version);
00122 };
00123 
00124 // define EnumMap and stream operators for Button::ButtonState
00125 GG_ENUM_MAP_BEGIN(Button::ButtonState)
00126     GG_ENUM_MAP_INSERT(Button::BN_PRESSED)
00127     GG_ENUM_MAP_INSERT(Button::BN_UNPRESSED)
00128     GG_ENUM_MAP_INSERT(Button::BN_ROLLOVER)
00129 GG_ENUM_MAP_END
00130 
00131 GG_ENUM_STREAM_IN(Button::ButtonState)
00132 GG_ENUM_STREAM_OUT(Button::ButtonState)
00133 
00134 
00144 class GG_API StateButton : public TextControl
00145 {
00146 public: 
00148     typedef boost::signal<void (bool)> CheckedSignalType; 
00149 
00150  
00152     typedef CheckedSignalType::slot_type CheckedSlotType; 
00153 
00154  
00156     StateButton(int x, int y, int w, int h, const std::string& str, const boost::shared_ptr<Font>& font, Flags<TextFormat> format, 
00157                 Clr color, Clr text_color = CLR_BLACK, Clr interior = CLR_ZERO, StateButtonStyle style = SBSTYLE_3D_XBOX,
00158                 Flags<WndFlag> flags = CLICKABLE); 
00159 
00160  
00162     virtual Pt       MinUsableSize() const;
00163 
00164     bool             Checked() const;       
00165     Clr              InteriorColor() const; 
00166 
00168     StateButtonStyle Style() const;
00169 
00170     mutable CheckedSignalType CheckedSignal; 
00171 
00172  
00174     virtual void     Render();
00175     virtual void     LClick(const Pt& pt, Flags<ModKey> mod_keys);
00176     virtual void     SizeMove(const Pt& ul, const Pt& lr);
00177 
00178     void             Reset();                 
00179     void             SetCheck(bool b = true); 
00180     void             SetButtonPosition(const Pt& ul, const Pt& lr); 
00181     void             SetDefaultButtonPosition(); 
00182     virtual void     SetColor(Clr c);         
00183     void             SetInteriorColor(Clr c); 
00184 
00186     void             SetStyle(StateButtonStyle bs);
00187 
00188     virtual void     DefineAttributes(WndEditor* editor);
00190 
00191 protected: 
00193     StateButton(); 
00194 
00195  
00197     Pt  ButtonUpperLeft() const;  
00198     Pt  ButtonLowerRight() const; 
00199     Pt  TextUpperLeft() const;    
00200 
00201  
00203     void RepositionButton();      
00204 
00205 
00206 private:
00207     bool              m_checked;     
00208     Clr               m_int_color;   
00209     StateButtonStyle  m_style;       
00210 
00211     Pt                m_button_ul;
00212     Pt                m_button_lr;
00213     Pt                m_text_ul;
00214 
00215     friend class boost::serialization::access;
00216     template <class Archive>
00217     void serialize(Archive& ar, const unsigned int version);
00218 };
00219 
00220 
00227 class GG_API RadioButtonGroup : public Control
00228 {
00229 public: 
00231     typedef boost::signal<void (int)> ButtonChangedSignalType; 
00232 
00233  
00235     typedef ButtonChangedSignalType::slot_type ButtonChangedSlotType; 
00236 
00237  
00239     RadioButtonGroup(int x, int y, int w, int h, Orientation orientation); 
00240 
00241  
00243     virtual Pt       MinUsableSize() const;
00244 
00246     Orientation      GetOrientation() const;
00247 
00249     int              NumButtons() const;
00250 
00252     int              CheckedButton() const;
00253 
00257     bool             ExpandButtons() const;
00258 
00262     bool             ExpandButtonsProportionally() const;
00263 
00266     bool             RenderOutline() const;
00267 
00268     mutable ButtonChangedSignalType ButtonChangedSignal; 
00269 
00270  
00272     virtual void Render();
00273 
00276     void SetCheck(int index);
00277 
00280     void DisableButton(int index, bool b = true); 
00281 
00283     void AddButton(StateButton* bn);
00284 
00286     void AddButton(const std::string& text, const boost::shared_ptr<Font>& font, Flags<TextFormat> format,
00287                    Clr color, Clr text_color = CLR_BLACK, Clr interior = CLR_ZERO,
00288                    StateButtonStyle style = SBSTYLE_3D_RADIO);
00289 
00291     void InsertButton(int index, StateButton* bn);
00292 
00295     void InsertButton(int index, const std::string& text, const boost::shared_ptr<Font>& font, Flags<TextFormat> format,
00296                       Clr color, Clr text_color = CLR_BLACK, Clr interior = CLR_ZERO,
00297                       StateButtonStyle style = SBSTYLE_3D_RADIO);
00298 
00303     void RemoveButton(StateButton* button);
00304 
00307     void ExpandButtons(bool expand);
00308 
00312     void ExpandButtonsProportionally(bool proportional);
00313 
00316     void RenderOutline(bool render_outline);
00317 
00320     void RaiseCheckedButton();
00321 
00322     virtual void DefineAttributes(WndEditor* editor);
00323 
00325     static const int NO_BUTTON;
00327 
00328 protected:
00330     struct GG_API ButtonSlot
00331     {
00332         ButtonSlot();
00333         ButtonSlot(StateButton* button_);
00334         StateButton*               button;
00335         boost::signals::connection connection;
00336 
00337         template <class Archive>
00338         void serialize(Archive& ar, const unsigned int version);
00339     };
00340  
00342     RadioButtonGroup(); 
00343 
00344  
00346     const std::vector<ButtonSlot>& ButtonSlots() const; 
00347 
00348 
00349 private:
00350     class ButtonClickedFunctor // for catching button-click signals from the contained buttons
00351     {
00352     public:
00353         ButtonClickedFunctor(RadioButtonGroup* group, StateButton* button, int index);
00354         void operator()(bool checked);
00355     private:
00356         RadioButtonGroup* m_group;
00357         StateButton*      m_button;
00358         int               m_index;
00359         bool              m_ignore_clicks;
00360     };
00361 
00362     void ConnectSignals();
00363     void HandleRadioClick(int index, bool set_check);
00364     void Reconnect();
00365 
00366     const Orientation       m_orientation;
00367     std::vector<ButtonSlot> m_button_slots;
00368     int                     m_checked_button; 
00369     bool                    m_expand_buttons;
00370     bool                    m_expand_buttons_proportionally;
00371     bool                    m_render_outline;
00372 
00373     friend class ButtonClickedFunctor;
00374 
00375     friend class boost::serialization::access;
00376     template <class Archive>
00377     void serialize(Archive& ar, const unsigned int version);
00378 };
00379 
00380 } // namespace GG
00381 
00382 BOOST_CLASS_VERSION(GG::RadioButtonGroup, 1)
00383 
00384 // template implementations
00385 template <class Archive>
00386 void GG::Button::serialize(Archive& ar, const unsigned int version)
00387 {
00388     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(TextControl)
00389         & BOOST_SERIALIZATION_NVP(m_state)
00390         & BOOST_SERIALIZATION_NVP(m_unpressed_graphic)
00391         & BOOST_SERIALIZATION_NVP(m_pressed_graphic)
00392         & BOOST_SERIALIZATION_NVP(m_rollover_graphic);
00393 }
00394 
00395 template <class Archive>
00396 void GG::StateButton::serialize(Archive& ar, const unsigned int version)
00397 {
00398     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(TextControl)
00399         & BOOST_SERIALIZATION_NVP(m_checked)
00400         & BOOST_SERIALIZATION_NVP(m_int_color)
00401         & BOOST_SERIALIZATION_NVP(m_style)
00402         & BOOST_SERIALIZATION_NVP(m_button_ul)
00403         & BOOST_SERIALIZATION_NVP(m_button_lr)
00404         & BOOST_SERIALIZATION_NVP(m_text_ul);
00405 }
00406 
00407 template <class Archive>
00408 void GG::RadioButtonGroup::ButtonSlot::serialize(Archive& ar, const unsigned int version)
00409 {
00410     ar  & BOOST_SERIALIZATION_NVP(button);
00411 }
00412 
00413 template <class Archive>
00414 void GG::RadioButtonGroup::serialize(Archive& ar, const unsigned int version)
00415 {
00416     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Control)
00417         & boost::serialization::make_nvp("m_orientation", const_cast<Orientation&>(m_orientation));
00418 
00419     if (version == 0) {
00420         std::vector<StateButton*> m_buttons;
00421         if (Archive::is_saving::value) {
00422             m_buttons.resize(m_button_slots.size());
00423             for (unsigned int i = 0; i < m_button_slots.size(); ++i) {
00424                 m_buttons[i] = m_button_slots[i].button;
00425             }
00426         }
00427         ar  & BOOST_SERIALIZATION_NVP(m_buttons);
00428         if (Archive::is_loading::value) {
00429             m_button_slots.resize(m_buttons.size());
00430             for (unsigned int i = 0; i < m_buttons.size(); ++i) {
00431                 m_button_slots[i].button = m_buttons[i];
00432             }
00433         }
00434     } else {
00435         ar  & BOOST_SERIALIZATION_NVP(m_button_slots)
00436             & BOOST_SERIALIZATION_NVP(m_expand_buttons)
00437             & BOOST_SERIALIZATION_NVP(m_expand_buttons_proportionally);
00438     }
00439 
00440     ar  & BOOST_SERIALIZATION_NVP(m_checked_button)
00441         & BOOST_SERIALIZATION_NVP(m_render_outline);
00442 
00443     if (Archive::is_loading::value)
00444         ConnectSignals();
00445 }
00446 
00447 #endif // _GG_Button_h_

Generated on Wed Mar 26 14:35:41 2008 for GG by  doxygen 1.5.2