Menu.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_Menu_h_
00031 #define _GG_Menu_h_
00032 
00033 #include <GG/Control.h>
00034 
00035 
00036 namespace GG {
00037 
00038 class Font;
00039 class TextControl;
00040 
00046 struct GG_API MenuItem
00047 { 
00049     typedef boost::signal<void (int)> SelectedIDSignalType; 
00050     typedef boost::signal<void ()>    SelectedSignalType;   
00051 
00052  
00054     typedef SelectedIDSignalType::slot_type SelectedIDSlotType; 
00055     typedef SelectedSignalType::slot_type   SelectedSlotType;   
00056 
00057  
00059     MenuItem(); 
00060     MenuItem(const std::string& str, int id, bool disable, bool check); 
00061 
00063     MenuItem(const std::string& str, int id, bool disable, bool check, const SelectedIDSlotType& slot);
00064 
00066     MenuItem(const std::string& str, int id, bool disable, bool check, const SelectedSlotType& slot);
00067 
00069     template <class T1, class T2>
00070     MenuItem(const std::string& str, int id, bool disable, bool check, void (T1::* slot)(int), T2* obj);
00071 
00073     template <class T1, class T2>
00074     MenuItem(const std::string& str, int id, bool disable, bool check, void (T1::* slot)(), T2* obj);
00075 
00076     virtual ~MenuItem(); 
00077 
00078  
00080     mutable boost::shared_ptr<SelectedIDSignalType> SelectedIDSignal; 
00081     mutable boost::shared_ptr<SelectedSignalType>   SelectedSignal;   
00082 
00083 
00084     std::string           label;      
00085     int                   item_ID;    
00086     bool                  disabled;   
00087     bool                  checked;    
00088     std::vector<MenuItem> next_level; 
00089 
00090 private:
00091     friend class boost::serialization::access;
00092     template <class Archive>
00093     void serialize(Archive& ar, const unsigned int version);
00094 };
00095 
00096 
00097 struct SetFontAction;
00098 struct SetTextColorAction;
00099 
00111 class GG_API MenuBar : public Control
00112 {
00113 public: 
00115     typedef boost::signal<void (int)> BrowsedSignalType; 
00116 
00117  
00119     typedef BrowsedSignalType::slot_type  BrowsedSlotType;   
00120 
00121  
00123 
00124     MenuBar(int x, int y, int w, const boost::shared_ptr<Font>& font, Clr text_color = CLR_WHITE, Clr color = CLR_BLACK, Clr interior = CLR_SHADOW); 
00125     MenuBar(int x, int y, int w, const boost::shared_ptr<Font>& font, const MenuItem& m, Clr text_color = CLR_WHITE, Clr color = CLR_BLACK, Clr interior = CLR_SHADOW); 
00126 
00127  
00129     virtual Pt        MinUsableSize() const;
00130 
00131     const MenuItem&   AllMenus() const;                           
00132     bool              ContainsMenu(const std::string& str) const; 
00133     int               NumMenus() const;                           
00134 
00136     const MenuItem&   GetMenu(const std::string& str) const;
00137 
00138     const MenuItem&   GetMenu(int n) const;      
00139 
00140     Clr               BorderColor() const;       
00141     Clr               InteriorColor() const;     
00142     Clr               TextColor() const;         
00143     Clr               HiliteColor() const;       
00144     Clr               SelectedTextColor() const; 
00145 
00146     mutable BrowsedSignalType BrowsedSignal; 
00147 
00148  
00150     virtual void   Render();
00151     virtual void   LButtonDown(const Pt& pt, Flags<ModKey> mod_keys);
00152     virtual void   MouseHere(const Pt& pt, Flags<ModKey> mod_keys);
00153     virtual void   MouseLeave();
00154 
00155     virtual void   SizeMove(const Pt& ul, const Pt& lr);
00156 
00157     MenuItem&      AllMenus();                    
00158 
00160     MenuItem&      GetMenu(const std::string& str);
00161 
00162     MenuItem&      GetMenu(int n);                
00163     void           AddMenu(const MenuItem& menu); 
00164 
00165     void           SetBorderColor(Clr clr);       
00166     void           SetInteriorColor(Clr clr);     
00167     void           SetTextColor(Clr clr);         
00168     void           SetHiliteColor(Clr clr);       
00169     void           SetSelectedTextColor(Clr clr); 
00170 
00171     virtual void   DefineAttributes(WndEditor* editor);
00173 
00174 protected: 
00176     MenuBar(); 
00177 
00178  
00180     const boost::shared_ptr<Font>&   GetFont() const;    
00181     const std::vector<TextControl*>& MenuLabels() const; 
00182     int                              Caret() const;      
00183 
00184 
00185 private:
00187     void AdjustLayout(bool reset = false);
00188 
00189     void BrowsedSlot(int n); 
00190 
00191     boost::shared_ptr<Font>   m_font;           
00192     Clr                       m_border_color;   
00193     Clr                       m_int_color;      
00194     Clr                       m_text_color;     
00195     Clr                       m_hilite_color;   
00196     Clr                       m_sel_text_color; 
00197 
00198     MenuItem                  m_menu_data;      
00199     std::vector<TextControl*> m_menu_labels;    
00200     int                       m_caret;          
00201 
00202     friend struct SetFontAction;
00203     friend struct SetTextColorAction;
00204 
00205     friend class boost::serialization::access;
00206     template <class Archive>
00207     void serialize(Archive& ar, const unsigned int version);
00208 };
00209 
00210 
00223 class GG_API PopupMenu : public Wnd
00224 {
00225 public: 
00227     typedef boost::signal<void (int)> BrowsedSignalType; 
00228 
00229  
00231     typedef BrowsedSignalType::slot_type  BrowsedSlotType;   
00232 
00233  
00235 
00236     PopupMenu(int x, int y, const boost::shared_ptr<Font>& font, const MenuItem& m, Clr text_color = CLR_WHITE, Clr color = CLR_BLACK, Clr interior = CLR_SHADOW);
00238  
00240     virtual Pt  ClientUpperLeft() const;
00241 
00242     int         MenuID() const;            
00243     Clr         BorderColor() const;       
00244     Clr         InteriorColor() const;     
00245     Clr         TextColor() const;         
00246     Clr         HiliteColor() const;       
00247     Clr         SelectedTextColor() const; 
00248 
00249     mutable BrowsedSignalType BrowsedSignal; 
00250 
00251  
00253     virtual void   Render();
00254     virtual void   LButtonUp(const Pt& pt, Flags<ModKey> mod_keys);
00255     virtual void   LClick(const Pt& pt, Flags<ModKey> mod_keys);
00256     virtual void   LDrag(const Pt& pt, const Pt& move, Flags<ModKey> mod_keys);
00257     virtual void   RButtonUp(const Pt& pt, Flags<ModKey> mod_keys);
00258     virtual void   RClick(const Pt& pt, Flags<ModKey> mod_keys);
00259     virtual void   MouseHere(const Pt& pt, Flags<ModKey> mod_keys);
00260 
00261     virtual int    Run();
00262 
00263     void           SetBorderColor(Clr clr);       
00264     void           SetInteriorColor(Clr clr);     
00265     void           SetTextColor(Clr clr);         
00266     void           SetHiliteColor(Clr clr);       
00267     void           SetSelectedTextColor(Clr clr); 
00268 
00269 
00270 protected: 
00272     const boost::shared_ptr<Font>&
00273                              GetFont() const;      
00274     const MenuItem&          MenuData() const;     
00275     const std::vector<Rect>& OpenLevels() const;   
00276     const std::vector<int>&  Caret() const;        
00277     const MenuItem*          ItemSelected() const; 
00278 
00279 
00280 private:
00281     boost::shared_ptr<Font>
00282                       m_font;           
00283     Clr               m_border_color;   
00284     Clr               m_int_color;      
00285     Clr               m_text_color;     
00286     Clr               m_hilite_color;   
00287     Clr               m_sel_text_color; 
00288 
00289     MenuItem          m_menu_data;   
00290 
00291     std::vector<Rect> m_open_levels; 
00292     std::vector<int>  m_caret;       
00293 
00294     const Pt          m_origin;         
00295     MenuItem*         m_item_selected;  
00296 };
00297 
00298 } // namespace GG
00299 
00300 // template implemetations
00301 template <class T1, class T2>
00302 GG::MenuItem::MenuItem(const std::string& str, int id, bool disable, bool check, void (T1::* slot)(int), T2* obj) :
00303     SelectedIDSignal(new SelectedIDSignalType()),
00304     SelectedSignal(new SelectedSignalType()),
00305     label(str), 
00306     item_ID(id), 
00307     disabled(disable), 
00308     checked(check)
00309 {
00310     SelectedIDSignal->connect(boost::bind(slot, obj, _1));
00311 }
00312 
00313 template <class T1, class T2>
00314 GG::MenuItem::MenuItem(const std::string& str, int id, bool disable, bool check, void (T1::* slot)(), T2* obj) :
00315     SelectedIDSignal(new SelectedIDSignalType()),
00316     SelectedSignal(new SelectedSignalType()),
00317     label(str), 
00318     item_ID(id), 
00319     disabled(disable), 
00320     checked(check)
00321 {
00322     SelectedSignal->connect(boost::bind(slot, obj));
00323 }
00324 
00325 template <class Archive>
00326 void GG::MenuItem::serialize(Archive& ar, const unsigned int version)
00327 {
00328     ar  & BOOST_SERIALIZATION_NVP(label)
00329         & BOOST_SERIALIZATION_NVP(item_ID)
00330         & BOOST_SERIALIZATION_NVP(disabled)
00331         & BOOST_SERIALIZATION_NVP(checked)
00332         & BOOST_SERIALIZATION_NVP(next_level);
00333 }
00334 
00335 template <class Archive>
00336 void GG::MenuBar::serialize(Archive& ar, const unsigned int version)
00337 {
00338     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Control)
00339         & BOOST_SERIALIZATION_NVP(m_font)
00340         & BOOST_SERIALIZATION_NVP(m_border_color)
00341         & BOOST_SERIALIZATION_NVP(m_int_color)
00342         & BOOST_SERIALIZATION_NVP(m_text_color)
00343         & BOOST_SERIALIZATION_NVP(m_hilite_color)
00344         & BOOST_SERIALIZATION_NVP(m_sel_text_color)
00345         & BOOST_SERIALIZATION_NVP(m_menu_data)
00346         & BOOST_SERIALIZATION_NVP(m_menu_labels)
00347         & BOOST_SERIALIZATION_NVP(m_caret);
00348 }
00349 
00350 #endif // _GG_Menu_h_

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