GG
|
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 00030 #ifndef _GG_Menu_h_ 00031 #define _GG_Menu_h_ 00032 00033 #include <GG/ClrConstants.h> 00034 #include <GG/Control.h> 00035 00036 00037 namespace GG { 00038 00039 class Font; 00040 class TextControl; 00041 00051 struct GG_API MenuItem 00052 { 00054 typedef boost::signal<void (int)> SelectedIDSignalType; 00055 typedef boost::signal<void ()> SelectedSignalType; 00056 00057 00059 typedef SelectedIDSignalType::slot_type SelectedIDSlotType; 00060 typedef SelectedSignalType::slot_type SelectedSlotType; 00061 00062 00064 MenuItem(); 00065 MenuItem(const std::string& str, int id, bool disable, bool check); 00066 00069 MenuItem(const std::string& str, int id, bool disable, bool check, const SelectedIDSlotType& slot); 00070 00073 MenuItem(const std::string& str, int id, bool disable, bool check, const SelectedSlotType& slot); 00074 00077 template <class T1, class T2> 00078 MenuItem(const std::string& str, int id, bool disable, bool check, void (T1::* slot)(int), T2* obj); 00079 00082 template <class T1, class T2> 00083 MenuItem(const std::string& str, int id, bool disable, bool check, void (T1::* slot)(), T2* obj); 00084 00085 virtual ~MenuItem(); 00086 00087 00089 mutable boost::shared_ptr<SelectedIDSignalType> SelectedIDSignal; 00090 mutable boost::shared_ptr<SelectedSignalType> SelectedSignal; 00091 00092 00093 std::string label; 00094 int item_ID; 00095 bool disabled; 00096 bool checked; 00097 std::vector<MenuItem> next_level; 00098 00099 private: 00100 friend class boost::serialization::access; 00101 template <class Archive> 00102 void serialize(Archive& ar, const unsigned int version); 00103 }; 00104 00105 00106 struct SetFontAction; 00107 struct SetTextColorAction; 00108 00128 class GG_API MenuBar : public Control 00129 { 00130 public: 00132 typedef boost::signal<void (int)> BrowsedSignalType; 00133 00134 00136 00138 MenuBar(X x, Y y, X w, const boost::shared_ptr<Font>& font, Clr text_color = CLR_WHITE, Clr color = CLR_BLACK, Clr interior = CLR_SHADOW); 00139 MenuBar(X x, Y y, X w, const boost::shared_ptr<Font>& font, const MenuItem& m, Clr text_color = CLR_WHITE, Clr color = CLR_BLACK, Clr interior = CLR_SHADOW); 00140 00141 00143 virtual Pt MinUsableSize() const; 00144 00145 const MenuItem& AllMenus() const; 00146 bool ContainsMenu(const std::string& str) const; 00147 std::size_t NumMenus() const; 00148 00150 const MenuItem& GetMenu(const std::string& str) const; 00151 00152 const MenuItem& GetMenu(std::size_t n) const; 00153 00154 Clr BorderColor() const; 00155 Clr InteriorColor() const; 00156 Clr TextColor() const; 00157 Clr HiliteColor() const; 00158 Clr SelectedTextColor() const; 00159 00160 mutable BrowsedSignalType BrowsedSignal; 00161 00162 00164 virtual void Render(); 00165 virtual void LButtonDown(const Pt& pt, Flags<ModKey> mod_keys); 00166 virtual void MouseHere(const Pt& pt, Flags<ModKey> mod_keys); 00167 virtual void MouseLeave(); 00168 00169 virtual void SizeMove(const Pt& ul, const Pt& lr); 00170 00171 MenuItem& AllMenus(); 00172 00175 MenuItem& GetMenu(const std::string& str); 00176 00177 MenuItem& GetMenu(int n); 00178 void AddMenu(const MenuItem& menu); 00179 00180 void SetBorderColor(Clr clr); 00181 void SetInteriorColor(Clr clr); 00182 void SetTextColor(Clr clr); 00183 void SetHiliteColor(Clr clr); 00184 void SetSelectedTextColor(Clr clr); 00185 00186 virtual void DefineAttributes(WndEditor* editor); 00188 00189 static const std::size_t INVALID_CARET; 00190 00191 protected: 00193 MenuBar(); 00194 00195 00197 const boost::shared_ptr<Font>& GetFont() const; 00198 const std::vector<TextControl*>& MenuLabels() const; 00199 std::size_t Caret() const; 00200 00201 00202 private: 00205 void AdjustLayout(bool reset = false); 00206 00207 boost::shared_ptr<Font> m_font; 00208 Clr m_border_color; 00209 Clr m_int_color; 00210 Clr m_text_color; 00211 Clr m_hilite_color; 00212 Clr m_sel_text_color; 00213 00214 MenuItem m_menu_data; 00215 std::vector<TextControl*> m_menu_labels; 00216 std::size_t m_caret; 00217 00218 friend struct SetFontAction; 00219 friend struct SetTextColorAction; 00220 00221 friend class boost::serialization::access; 00222 template <class Archive> 00223 void serialize(Archive& ar, const unsigned int version); 00224 }; 00225 00226 00247 class GG_API PopupMenu : public Wnd 00248 { 00249 public: 00251 typedef boost::signal<void (int)> BrowsedSignalType; 00252 00253 00255 00257 PopupMenu(X x, Y y, const boost::shared_ptr<Font>& font, const MenuItem& m, Clr text_color = CLR_WHITE, Clr color = CLR_BLACK, Clr interior = CLR_SHADOW); 00259 00261 virtual Pt ClientUpperLeft() const; 00262 00263 int MenuID() const; 00264 Clr BorderColor() const; 00265 Clr InteriorColor() const; 00266 Clr TextColor() const; 00267 Clr HiliteColor() const; 00268 Clr SelectedTextColor() const; 00269 00270 mutable BrowsedSignalType BrowsedSignal; 00271 00272 00274 virtual void Render(); 00275 virtual void LButtonUp(const Pt& pt, Flags<ModKey> mod_keys); 00276 virtual void LClick(const Pt& pt, Flags<ModKey> mod_keys); 00277 virtual void LDrag(const Pt& pt, const Pt& move, Flags<ModKey> mod_keys); 00278 virtual void RButtonUp(const Pt& pt, Flags<ModKey> mod_keys); 00279 virtual void RClick(const Pt& pt, Flags<ModKey> mod_keys); 00280 virtual void MouseHere(const Pt& pt, Flags<ModKey> mod_keys); 00281 00282 virtual bool Run(); 00283 00284 void SetBorderColor(Clr clr); 00285 void SetInteriorColor(Clr clr); 00286 void SetTextColor(Clr clr); 00287 void SetHiliteColor(Clr clr); 00288 void SetSelectedTextColor(Clr clr); 00289 00290 00291 static const std::size_t INVALID_CARET; 00292 00293 protected: 00295 const boost::shared_ptr<Font>& GetFont() const; 00296 const MenuItem& MenuData() const; 00297 const std::vector<Rect>& OpenLevels() const; 00298 const std::vector<std::size_t>& Caret() const; 00299 const MenuItem* ItemSelected() const; 00300 00301 00302 private: 00303 boost::shared_ptr<Font> 00304 m_font; 00305 Clr m_border_color; 00306 Clr m_int_color; 00307 Clr m_text_color; 00308 Clr m_hilite_color; 00309 Clr m_sel_text_color; 00310 00311 MenuItem m_menu_data; 00312 00313 std::vector<Rect> m_open_levels; 00314 std::vector<std::size_t> 00315 m_caret; 00316 00317 const Pt m_origin; 00318 MenuItem* m_item_selected; 00319 }; 00320 00321 } // namespace GG 00322 00323 // template implemetations 00324 template <class T1, class T2> 00325 GG::MenuItem::MenuItem(const std::string& str, int id, bool disable, bool check, void (T1::* slot)(int), T2* obj) : 00326 SelectedIDSignal(new SelectedIDSignalType()), 00327 SelectedSignal(new SelectedSignalType()), 00328 label(str), 00329 item_ID(id), 00330 disabled(disable), 00331 checked(check) 00332 { SelectedIDSignal->connect(boost::bind(slot, obj, _1)); } 00333 00334 template <class T1, class T2> 00335 GG::MenuItem::MenuItem(const std::string& str, int id, bool disable, bool check, void (T1::* slot)(), T2* obj) : 00336 SelectedIDSignal(new SelectedIDSignalType()), 00337 SelectedSignal(new SelectedSignalType()), 00338 label(str), 00339 item_ID(id), 00340 disabled(disable), 00341 checked(check) 00342 { SelectedSignal->connect(boost::bind(slot, obj)); } 00343 00344 template <class Archive> 00345 void GG::MenuItem::serialize(Archive& ar, const unsigned int version) 00346 { 00347 ar & BOOST_SERIALIZATION_NVP(label) 00348 & BOOST_SERIALIZATION_NVP(item_ID) 00349 & BOOST_SERIALIZATION_NVP(disabled) 00350 & BOOST_SERIALIZATION_NVP(checked) 00351 & BOOST_SERIALIZATION_NVP(next_level); 00352 } 00353 00354 template <class Archive> 00355 void GG::MenuBar::serialize(Archive& ar, const unsigned int version) 00356 { 00357 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Control) 00358 & BOOST_SERIALIZATION_NVP(m_font) 00359 & BOOST_SERIALIZATION_NVP(m_border_color) 00360 & BOOST_SERIALIZATION_NVP(m_int_color) 00361 & BOOST_SERIALIZATION_NVP(m_text_color) 00362 & BOOST_SERIALIZATION_NVP(m_hilite_color) 00363 & BOOST_SERIALIZATION_NVP(m_sel_text_color) 00364 & BOOST_SERIALIZATION_NVP(m_menu_data) 00365 & BOOST_SERIALIZATION_NVP(m_menu_labels) 00366 & BOOST_SERIALIZATION_NVP(m_caret); 00367 } 00368 00369 #endif // _GG_Menu_h_