GG
Button.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 
31 #ifndef _GG_Button_h_
32 #define _GG_Button_h_
33 
34 #include <GG/ClrConstants.h>
35 #include <GG/TextControl.h>
36 
37 
38 namespace GG {
39 
47 class GG_API Button : public TextControl
48 {
49 public:
51  enum ButtonState {
54  BN_ROLLOVER
55  };
56 
58  typedef boost::signal<void ()> ClickedSignalType;
59 
60 
62  Button(X x, Y y, X w, Y h, const std::string& str, const boost::shared_ptr<Font>& font, Clr color,
63  Clr text_color = CLR_BLACK, Flags<WndFlag> flags = INTERACTIVE);
64 
65 
67 
68  ButtonState State() const;
69 
70  const SubTexture& UnpressedGraphic() const;
71  const SubTexture& PressedGraphic() const;
72  const SubTexture& RolloverGraphic() const;
73 
75 
76 
78  virtual void Render();
79 
80  virtual void SetColor(Clr c);
81 
83  void SetState(ButtonState state);
84 
85  void SetUnpressedGraphic(const SubTexture& st);
86  void SetPressedGraphic(const SubTexture& st);
87  void SetRolloverGraphic(const SubTexture& st);
88 
89 
90 protected:
92  Button();
93 
94 
96  virtual void LButtonDown(const Pt& pt, Flags<ModKey> mod_keys);
97  virtual void LDrag(const Pt& pt, const Pt& move, Flags<ModKey> mod_keys);
98  virtual void LButtonUp(const Pt& pt, Flags<ModKey> mod_keys);
99  virtual void LClick(const Pt& pt, Flags<ModKey> mod_keys);
100  virtual void MouseHere(const Pt& pt, Flags<ModKey> mod_keys);
101  virtual void MouseLeave();
102 
103  virtual void RenderUnpressed();
104  virtual void RenderPressed();
105  virtual void RenderRollover();
106 
107 
108 private:
109  void RenderDefault();
110 
111  ButtonState m_state;
112 
113  SubTexture m_unpressed_graphic;
114  SubTexture m_pressed_graphic;
115  SubTexture m_rollover_graphic;
116 };
117 
118 // define EnumMap and stream operators for Button::ButtonState
120  GG_ENUM_MAP_INSERT(Button::BN_PRESSED)
121  GG_ENUM_MAP_INSERT(Button::BN_UNPRESSED)
122  GG_ENUM_MAP_INSERT(Button::BN_ROLLOVER)
124 
125 GG_ENUM_STREAM_IN(Button::ButtonState)
126 GG_ENUM_STREAM_OUT(Button::ButtonState)
127 
128 
140 class GG_API StateButton : public TextControl
141 {
142 public:
144  typedef boost::signal<void (bool)> CheckedSignalType;
145 
146 
148  StateButton(X x, Y y, X w, Y h, const std::string& str, const boost::shared_ptr<Font>& font, Flags<TextFormat> format,
149  Clr color, Clr text_color = CLR_BLACK, Clr interior = CLR_ZERO, StateButtonStyle style = SBSTYLE_3D_XBOX,
150  Flags<WndFlag> flags = INTERACTIVE);
151 
152 
154  virtual Pt MinUsableSize() const;
155 
156  bool Checked() const;
157  Clr InteriorColor() const;
158 
160  StateButtonStyle Style() const;
161 
162  mutable CheckedSignalType CheckedSignal;
163 
164 
166  virtual void Render();
167  virtual void SizeMove(const Pt& ul, const Pt& lr);
168 
169  void Reset();
170  void SetCheck(bool b = true);
171  void SetButtonPosition(const Pt& ul, const Pt& lr);
172  void SetDefaultButtonPosition();
173  virtual void SetColor(Clr c);
174  void SetInteriorColor(Clr c);
175 
177  void SetStyle(StateButtonStyle bs);
179 
180 protected:
182  StateButton();
183 
184 
186  Pt ButtonUpperLeft() const;
187  Pt ButtonLowerRight() const;
188  Pt TextUpperLeft() const;
189 
190 
192  virtual void LClick(const Pt& pt, Flags<ModKey> mod_keys);
193 
194  void RepositionButton();
195 
196 
197 private:
198  bool m_checked;
199  Clr m_int_color;
200  StateButtonStyle m_style;
201 
202  Pt m_button_ul;
203  Pt m_button_lr;
204  Pt m_text_ul;
205 };
206 
207 
216 class GG_API RadioButtonGroup : public Control
217 {
218 public:
220  typedef boost::signal<void (std::size_t)> ButtonChangedSignalType;
221 
222 
224  RadioButtonGroup(X x, Y y, X w, Y h, Orientation orientation);
225 
226 
228  virtual Pt MinUsableSize() const;
229 
231  Orientation GetOrientation() const;
232 
234  bool Empty() const;
235 
237  std::size_t NumButtons() const;
238 
241  std::size_t CheckedButton() const;
242 
247  bool ExpandButtons() const;
248 
253  bool ExpandButtonsProportionally() const;
254 
257  bool RenderOutline() const;
258 
260 
261 
263  virtual void Render();
264 
268  void SetCheck(std::size_t index);
269 
274  void DisableButton(std::size_t index, bool b = true);
275 
277  void AddButton(StateButton* bn);
278 
281  void AddButton(const std::string& text, const boost::shared_ptr<Font>& font, Flags<TextFormat> format,
282  Clr color, Clr text_color = CLR_BLACK, Clr interior = CLR_ZERO,
284 
287  void InsertButton(std::size_t index, StateButton* bn);
288 
292  void InsertButton(std::size_t index, const std::string& text, const boost::shared_ptr<Font>& font, Flags<TextFormat> format,
293  Clr color, Clr text_color = CLR_BLACK, Clr interior = CLR_ZERO,
295 
302  void RemoveButton(StateButton* button);
303 
308  void ExpandButtons(bool expand);
309 
314  void ExpandButtonsProportionally(bool proportional);
315 
318  void RenderOutline(bool render_outline);
319 
322  void RaiseCheckedButton();
323 
326  static const std::size_t NO_BUTTON;
328 
329 protected:
332  struct GG_API ButtonSlot
333  {
334  ButtonSlot();
335  ButtonSlot(StateButton* button_);
336  StateButton* button;
337  boost::signals::connection connection;
338  };
339 
341  RadioButtonGroup();
342 
343 
345  const std::vector<ButtonSlot>& ButtonSlots() const;
346 
347 
348 private:
349  class ButtonClickedFunctor // for catching button-click signals from the contained buttons
350  {
351  public:
352  ButtonClickedFunctor(RadioButtonGroup* group, StateButton* button, std::size_t index);
353  void operator()(bool checked);
354  private:
355  RadioButtonGroup* m_group;
356  StateButton* m_button;
357  std::size_t m_index;
358  };
359 
360  void ConnectSignals();
361  void SetCheckImpl(std::size_t index, bool signal);
362  void Reconnect();
363 
364  const Orientation m_orientation;
365  std::vector<ButtonSlot> m_button_slots;
366  std::size_t m_checked_button;
367  bool m_expand_buttons;
368  bool m_expand_buttons_proportionally;
369  bool m_render_outline;
370 
371  friend class ButtonClickedFunctor;
372 };
373 
374 } // namespace GG
375 
376 #endif