GG
Menu.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /* GG is a GUI for SDL and OpenGL.
3  Copyright (C) 2003-2008 T. Zachary Laine
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public License
7  as published by the Free Software Foundation; either version 2.1
8  of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free
17  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18  02111-1307 USA
19 
20  If you do not wish to comply with the terms of the LGPL please
21  contact the author as other terms are available for a fee.
22 
23  Zach Laine
24  whatwasthataddress@gmail.com */
25 
30 #ifndef _GG_Menu_h_
31 #define _GG_Menu_h_
32 
33 #include <GG/ClrConstants.h>
34 #include <GG/Control.h>
35 
36 
37 namespace GG {
38 
39 class Font;
40 class TextControl;
41 
50 struct GG_API MenuItem
51 {
53  typedef boost::signal<void (int)> SelectedIDSignalType;
54  typedef boost::signal<void ()> SelectedSignalType;
55 
56 
58  typedef SelectedIDSignalType::slot_type SelectedIDSlotType;
59  typedef SelectedSignalType::slot_type SelectedSlotType;
60 
61 
63  MenuItem();
64  MenuItem(const std::string& str, int id, bool disable, bool check);
65 
68  MenuItem(const std::string& str, int id, bool disable, bool check, const SelectedIDSlotType& slot);
69 
72  MenuItem(const std::string& str, int id, bool disable, bool check, const SelectedSlotType& slot);
73 
76  template <class T1, class T2>
77  MenuItem(const std::string& str, int id, bool disable, bool check, void (T1::* slot)(int), T2* obj);
78 
81  template <class T1, class T2>
82  MenuItem(const std::string& str, int id, bool disable, bool check, void (T1::* slot)(), T2* obj);
83 
84  virtual ~MenuItem();
85 
86 
88  mutable boost::shared_ptr<SelectedIDSignalType> SelectedIDSignal;
89  mutable boost::shared_ptr<SelectedSignalType> SelectedSignal;
90 
91 
92  std::string label;
93  int item_ID;
94  bool disabled;
95  bool checked;
96  std::vector<MenuItem> next_level;
97 };
98 
99 
100 struct SetFontAction;
101 struct SetTextColorAction;
102 
122 class GG_API MenuBar : public Control
123 {
124 public:
126  typedef boost::signal<void (int)> BrowsedSignalType;
127 
128 
130 
132  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);
133  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);
134 
135 
137  virtual Pt MinUsableSize() const;
138 
139  const MenuItem& AllMenus() const;
140  bool ContainsMenu(const std::string& str) const;
141  std::size_t NumMenus() const;
142 
144  const MenuItem& GetMenu(const std::string& str) const;
145 
146  const MenuItem& GetMenu(std::size_t n) const;
147 
148  Clr BorderColor() const;
149  Clr InteriorColor() const;
150  Clr TextColor() const;
151  Clr HiliteColor() const;
152  Clr SelectedTextColor() const;
153 
155 
156 
158  virtual void Render();
159  virtual void LButtonDown(const Pt& pt, Flags<ModKey> mod_keys);
160  virtual void MouseHere(const Pt& pt, Flags<ModKey> mod_keys);
161  virtual void MouseLeave();
162 
163  virtual void SizeMove(const Pt& ul, const Pt& lr);
164 
165  MenuItem& AllMenus();
166 
169  MenuItem& GetMenu(const std::string& str);
170 
171  MenuItem& GetMenu(int n);
172  void AddMenu(const MenuItem& menu);
173 
174  void SetBorderColor(Clr clr);
175  void SetInteriorColor(Clr clr);
176  void SetTextColor(Clr clr);
177  void SetHiliteColor(Clr clr);
178  void SetSelectedTextColor(Clr clr);
179 
180 
181  static const std::size_t INVALID_CARET;
182 
183 protected:
185  MenuBar();
186 
187 
189  const boost::shared_ptr<Font>& GetFont() const;
190  const std::vector<TextControl*>& MenuLabels() const;
191  std::size_t Caret() const;
192 
193 
194 private:
197  void AdjustLayout(bool reset = false);
198 
199  boost::shared_ptr<Font> m_font;
200  Clr m_border_color;
201  Clr m_int_color;
202  Clr m_text_color;
203  Clr m_hilite_color;
204  Clr m_sel_text_color;
205 
206  MenuItem m_menu_data;
207  std::vector<TextControl*> m_menu_labels;
208  std::size_t m_caret;
209 
210  friend struct SetFontAction;
211  friend struct SetTextColorAction;
212 };
213 
214 
231 class GG_API PopupMenu : public Wnd
232 {
233 public:
235  typedef boost::signal<void (int)> BrowsedSignalType;
236 
237 
239 
241  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);
243 
245  virtual Pt ClientUpperLeft() const;
246 
247  int MenuID() const;
248  Clr BorderColor() const;
249  Clr InteriorColor() const;
250  Clr TextColor() const;
251  Clr HiliteColor() const;
252  Clr SelectedTextColor() const;
253 
255 
256 
258  virtual void Render();
259  virtual void LButtonUp(const Pt& pt, Flags<ModKey> mod_keys);
260  virtual void LClick(const Pt& pt, Flags<ModKey> mod_keys);
261  virtual void LDrag(const Pt& pt, const Pt& move, Flags<ModKey> mod_keys);
262  virtual void RButtonUp(const Pt& pt, Flags<ModKey> mod_keys);
263  virtual void RClick(const Pt& pt, Flags<ModKey> mod_keys);
264  virtual void MouseHere(const Pt& pt, Flags<ModKey> mod_keys);
265 
266  virtual bool Run();
267 
268  void SetBorderColor(Clr clr);
269  void SetInteriorColor(Clr clr);
270  void SetTextColor(Clr clr);
271  void SetHiliteColor(Clr clr);
272  void SetSelectedTextColor(Clr clr);
273 
274 
275  static const std::size_t INVALID_CARET;
276 
277 protected:
279  const boost::shared_ptr<Font>& GetFont() const;
280  const MenuItem& MenuData() const;
281  const std::vector<Rect>& OpenLevels() const;
282  const std::vector<std::size_t>& Caret() const;
283  const MenuItem* ItemSelected() const;
284 
285 
286 private:
287  boost::shared_ptr<Font>
288  m_font;
289  Clr m_border_color;
290  Clr m_int_color;
291  Clr m_text_color;
292  Clr m_hilite_color;
293  Clr m_sel_text_color;
294 
295  MenuItem m_menu_data;
296 
297  std::vector<Rect> m_open_levels;
298  std::vector<std::size_t>
299  m_caret;
300 
301  const Pt m_origin;
302  MenuItem* m_item_selected;
303 };
304 
305 } // namespace GG
306 
307 // template implemetations
308 template <class T1, class T2>
309 GG::MenuItem::MenuItem(const std::string& str, int id, bool disable, bool check, void (T1::* slot)(int), T2* obj) :
310  SelectedIDSignal(new SelectedIDSignalType()),
311  SelectedSignal(new SelectedSignalType()),
312  label(str),
313  item_ID(id),
314  disabled(disable),
315  checked(check)
316 { SelectedIDSignal->connect(boost::bind(slot, obj, _1)); }
317 
318 template <class T1, class T2>
319 GG::MenuItem::MenuItem(const std::string& str, int id, bool disable, bool check, void (T1::* slot)(), T2* obj) :
320  SelectedIDSignal(new SelectedIDSignalType()),
321  SelectedSignal(new SelectedSignalType()),
322  label(str),
323  item_ID(id),
324  disabled(disable),
325  checked(check)
326 { SelectedSignal->connect(boost::bind(slot, obj)); }
327 
328 #endif