Button.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /* GG is a GUI for SDL and OpenGL.
00003    Copyright (C) 2003-2008 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@gmail.com */
00025 
00031 #ifndef _GG_Button_h_
00032 #define _GG_Button_h_
00033 
00034 #include <GG/ClrConstants.h>
00035 #include <GG/TextControl.h>
00036 
00037 #include <boost/serialization/version.hpp>
00038 
00039 
00040 namespace GG {
00041 
00049 class GG_API Button : public TextControl
00050 {
00051 public:
00053     enum ButtonState {
00054         BN_PRESSED,    
00055         BN_UNPRESSED,  
00056         BN_ROLLOVER    
00057     };
00058  
00060     typedef boost::signal<void ()> ClickedSignalType; 
00061 
00062  
00064     Button(X x, Y y, X w, Y h, const std::string& str, const boost::shared_ptr<Font>& font, Clr color,
00065            Clr text_color = CLR_BLACK, Flags<WndFlag> flags = INTERACTIVE); 
00066 
00067  
00069 
00070     ButtonState       State() const;
00071 
00072     const SubTexture& UnpressedGraphic() const; 
00073     const SubTexture& PressedGraphic() const;   
00074     const SubTexture& RolloverGraphic() const;  
00075 
00076     mutable ClickedSignalType ClickedSignal; 
00077 
00078  
00080     virtual void   Render();
00081 
00082     virtual void   SetColor(Clr c); 
00083 
00085     void           SetState(ButtonState state);
00086 
00087     void           SetUnpressedGraphic(const SubTexture& st); 
00088     void           SetPressedGraphic(const SubTexture& st);   
00089     void           SetRolloverGraphic(const SubTexture& st);  
00090 
00091     virtual void   DefineAttributes(WndEditor* editor);
00093 
00094 protected: 
00096     Button(); 
00097 
00098  
00100     virtual void   LButtonDown(const Pt& pt, Flags<ModKey> mod_keys);
00101     virtual void   LDrag(const Pt& pt, const Pt& move, Flags<ModKey> mod_keys);
00102     virtual void   LButtonUp(const Pt& pt, Flags<ModKey> mod_keys);
00103     virtual void   LClick(const Pt& pt, Flags<ModKey> mod_keys);
00104     virtual void   MouseHere(const Pt& pt, Flags<ModKey> mod_keys);
00105     virtual void   MouseLeave();
00106 
00107     virtual void   RenderUnpressed();   
00108     virtual void   RenderPressed();     
00109     virtual void   RenderRollover();    
00110 
00111 
00112 private:
00113     void           RenderDefault();     
00114 
00115     ButtonState    m_state;             
00116 
00117     SubTexture     m_unpressed_graphic; 
00118     SubTexture     m_pressed_graphic;   
00119     SubTexture     m_rollover_graphic;  
00120 
00121     friend class boost::serialization::access;
00122     template <class Archive>
00123     void serialize(Archive& ar, const unsigned int version);
00124 };
00125 
00126 // define EnumMap and stream operators for Button::ButtonState
00127 GG_ENUM_MAP_BEGIN(Button::ButtonState)
00128     GG_ENUM_MAP_INSERT(Button::BN_PRESSED)
00129     GG_ENUM_MAP_INSERT(Button::BN_UNPRESSED)
00130     GG_ENUM_MAP_INSERT(Button::BN_ROLLOVER)
00131 GG_ENUM_MAP_END
00132 
00133 GG_ENUM_STREAM_IN(Button::ButtonState)
00134 GG_ENUM_STREAM_OUT(Button::ButtonState)
00135 
00136 
00152 class GG_API StateButton : public TextControl
00153 {
00154 public: 
00156     typedef boost::signal<void (bool)> CheckedSignalType; 
00157 
00158  
00160     StateButton(X x, Y y, X w, Y h, const std::string& str, const boost::shared_ptr<Font>& font, Flags<TextFormat> format, 
00161                 Clr color, Clr text_color = CLR_BLACK, Clr interior = CLR_ZERO, StateButtonStyle style = SBSTYLE_3D_XBOX,
00162                 Flags<WndFlag> flags = INTERACTIVE); 
00163 
00164  
00166     virtual Pt       MinUsableSize() const;
00167 
00168     bool             Checked() const;       
00169     Clr              InteriorColor() const; 
00170 
00172     StateButtonStyle Style() const;
00173 
00174     mutable CheckedSignalType CheckedSignal; 
00175 
00176  
00178     virtual void     Render();
00179     virtual void     SizeMove(const Pt& ul, const Pt& lr);
00180 
00181     void             Reset();                 
00182     void             SetCheck(bool b = true); 
00183     void             SetButtonPosition(const Pt& ul, const Pt& lr); 
00184     void             SetDefaultButtonPosition(); 
00185     virtual void     SetColor(Clr c);         
00186     void             SetInteriorColor(Clr c); 
00187 
00189     void             SetStyle(StateButtonStyle bs);
00190 
00191     virtual void     DefineAttributes(WndEditor* editor);
00193 
00194 protected: 
00196     StateButton(); 
00197 
00198  
00200     Pt  ButtonUpperLeft() const;  
00201     Pt  ButtonLowerRight() const; 
00202     Pt  TextUpperLeft() const;    
00203 
00204  
00206     virtual void LClick(const Pt& pt, Flags<ModKey> mod_keys);
00207 
00208     void RepositionButton();      
00209 
00210 
00211 private:
00212     bool              m_checked;     
00213     Clr               m_int_color;   
00214     StateButtonStyle  m_style;       
00215 
00216     Pt                m_button_ul;
00217     Pt                m_button_lr;
00218     Pt                m_text_ul;
00219 
00220     friend class boost::serialization::access;
00221     template <class Archive>
00222     void serialize(Archive& ar, const unsigned int version);
00223 };
00224 
00225 
00236 class GG_API RadioButtonGroup : public Control
00237 {
00238 public: 
00240     typedef boost::signal<void (std::size_t)> ButtonChangedSignalType; 
00241 
00242  
00244     RadioButtonGroup(X x, Y y, X w, Y h, Orientation orientation); 
00245 
00246  
00248     virtual Pt       MinUsableSize() const;
00249 
00251     Orientation      GetOrientation() const;
00252 
00254     bool             Empty() const;
00255 
00257     std::size_t      NumButtons() const;
00258 
00261     std::size_t      CheckedButton() const;
00262 
00267     bool             ExpandButtons() const;
00268 
00273     bool             ExpandButtonsProportionally() const;
00274 
00277     bool             RenderOutline() const;
00278 
00279     mutable ButtonChangedSignalType ButtonChangedSignal; 
00280 
00281  
00283     virtual void Render();
00284 
00288     void SetCheck(std::size_t index);
00289 
00294     void DisableButton(std::size_t index, bool b = true); 
00295 
00297     void AddButton(StateButton* bn);
00298 
00301     void AddButton(const std::string& text, const boost::shared_ptr<Font>& font, Flags<TextFormat> format,
00302                    Clr color, Clr text_color = CLR_BLACK, Clr interior = CLR_ZERO,
00303                    StateButtonStyle style = SBSTYLE_3D_RADIO);
00304 
00307     void InsertButton(std::size_t index, StateButton* bn);
00308 
00312     void InsertButton(std::size_t index, const std::string& text, const boost::shared_ptr<Font>& font, Flags<TextFormat> format,
00313                       Clr color, Clr text_color = CLR_BLACK, Clr interior = CLR_ZERO,
00314                       StateButtonStyle style = SBSTYLE_3D_RADIO);
00315 
00322     void RemoveButton(StateButton* button);
00323 
00328     void ExpandButtons(bool expand);
00329 
00334     void ExpandButtonsProportionally(bool proportional);
00335 
00338     void RenderOutline(bool render_outline);
00339 
00342     void RaiseCheckedButton();
00343 
00344     virtual void DefineAttributes(WndEditor* editor);
00345 
00348     static const std::size_t NO_BUTTON;
00350 
00351 protected:
00354     struct GG_API ButtonSlot
00355     {
00356         ButtonSlot();
00357         ButtonSlot(StateButton* button_);
00358         StateButton*               button;
00359         boost::signals::connection connection;
00360 
00361         template <class Archive>
00362         void serialize(Archive& ar, const unsigned int version);
00363     };
00364  
00366     RadioButtonGroup(); 
00367 
00368  
00370     const std::vector<ButtonSlot>& ButtonSlots() const; 
00371 
00372 
00373 private:
00374     class ButtonClickedFunctor // for catching button-click signals from the contained buttons
00375     {
00376     public:
00377         ButtonClickedFunctor(RadioButtonGroup* group, StateButton* button, std::size_t index);
00378         void operator()(bool checked);
00379     private:
00380         RadioButtonGroup* m_group;
00381         StateButton*      m_button;
00382         std::size_t       m_index;
00383     };
00384 
00385     void ConnectSignals();
00386     void SetCheckImpl(std::size_t index, bool signal);
00387     void Reconnect();
00388 
00389     const Orientation       m_orientation;
00390     std::vector<ButtonSlot> m_button_slots;
00391     std::size_t             m_checked_button; 
00392     bool                    m_expand_buttons;
00393     bool                    m_expand_buttons_proportionally;
00394     bool                    m_render_outline;
00395 
00396     friend class ButtonClickedFunctor;
00397 
00398     friend class boost::serialization::access;
00399     template <class Archive>
00400     void serialize(Archive& ar, const unsigned int version);
00401 };
00402 
00403 } // namespace GG
00404 
00405 
00406 // template implementations
00407 template <class Archive>
00408 void GG::Button::serialize(Archive& ar, const unsigned int version)
00409 {
00410     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(TextControl)
00411         & BOOST_SERIALIZATION_NVP(m_state)
00412         & BOOST_SERIALIZATION_NVP(m_unpressed_graphic)
00413         & BOOST_SERIALIZATION_NVP(m_pressed_graphic)
00414         & BOOST_SERIALIZATION_NVP(m_rollover_graphic);
00415 }
00416 
00417 template <class Archive>
00418 void GG::StateButton::serialize(Archive& ar, const unsigned int version)
00419 {
00420     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(TextControl)
00421         & BOOST_SERIALIZATION_NVP(m_checked)
00422         & BOOST_SERIALIZATION_NVP(m_int_color)
00423         & BOOST_SERIALIZATION_NVP(m_style)
00424         & BOOST_SERIALIZATION_NVP(m_button_ul)
00425         & BOOST_SERIALIZATION_NVP(m_button_lr)
00426         & BOOST_SERIALIZATION_NVP(m_text_ul);
00427 }
00428 
00429 template <class Archive>
00430 void GG::RadioButtonGroup::ButtonSlot::serialize(Archive& ar, const unsigned int version)
00431 {
00432     ar  & BOOST_SERIALIZATION_NVP(button);
00433 }
00434 
00435 template <class Archive>
00436 void GG::RadioButtonGroup::serialize(Archive& ar, const unsigned int version)
00437 {
00438     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Control)
00439         & boost::serialization::make_nvp("m_orientation", const_cast<Orientation&>(m_orientation))
00440         & BOOST_SERIALIZATION_NVP(m_button_slots)
00441         & BOOST_SERIALIZATION_NVP(m_expand_buttons)
00442         & BOOST_SERIALIZATION_NVP(m_expand_buttons_proportionally)
00443         & BOOST_SERIALIZATION_NVP(m_checked_button)
00444         & BOOST_SERIALIZATION_NVP(m_render_outline);
00445 
00446     if (Archive::is_loading::value)
00447         ConnectSignals();
00448 }
00449 
00450 #endif // _GG_Button_h_

Generated on Sat Mar 26 07:08:37 2011 for GG by  doxygen 1.5.9