The Battle for Wesnoth  1.15.1+dev
menu_button.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
19 
21 
24 
25 #include <boost/dynamic_bitset.hpp>
26 
27 class config;
28 
29 namespace gui2
30 {
31 namespace implementation
32 {
33  struct builder_menu_button;
34 }
35 
36 // ------------ WIDGET -----------{
37 
38 /**
39  * Simple push button.
40  */
42 {
43 public:
44  explicit menu_button(const implementation::builder_menu_button& builder);
45 
46  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
47 
48  /** See @ref styled_widget::set_active. */
49  virtual void set_active(const bool active) override;
50 
51  /** See @ref styled_widget::get_active. */
52  virtual bool get_active() const override;
53 
54  /** See @ref styled_widget::get_state. */
55  virtual unsigned get_state() const override;
56 
57  /***** ***** ***** setters / getters for members ***** ****** *****/
58 
59  void set_values(const std::vector<::config>& values, unsigned selected = 0);
60 
61  void set_selected(unsigned selected, bool fire_event = true);
62 
63  /** Inherited from selectable_item */
64  virtual unsigned get_value() const override { return selected_; }
65 
66  /** Inherited from selectable_item */
67  virtual void set_value(unsigned value, bool fire_event = false) override { set_selected(value, fire_event); }
68 
69  /** Inherited from selectable_item */
70  virtual unsigned num_states() const override { return values_.size(); }
71 
72  /** Returns the value of the selected row */
73  std::string get_value_string() const
74  {
75  return values_[selected_]["label"];
76  }
77 
78  /** Returns the entire config object for the selected row. */
79  const ::config& get_value_config() const
80  {
81  return values_[selected_];
82  }
83 
84  void set_keep_open(const bool keep_open)
85  {
86  keep_open_ = keep_open;
87  }
88 
89 private:
90  /**
91  * Possible states of the widget.
92  *
93  * Note the order of the states must be the same as defined in settings.hpp.
94  */
95  enum state_t {
100  };
101 
102  void set_state(const state_t state);
103 
104  /**
105  * Current state of the widget.
106  *
107  * The state of the widget determines what to render and how the widget
108  * reacts to certain 'events'.
109  */
111 
112  std::vector<::config> values_;
113 
114  unsigned selected_;
115 
117 
118 public:
119  /** Static type getter that does not rely on the widget being constructed. */
120  static const std::string& type();
121 
122 private:
123  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
124  virtual const std::string& get_control_type() const override;
125 
126  /***** ***** ***** signal handlers ***** ****** *****/
127 
128  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
129 
130  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
131 
132  void signal_handler_left_button_down(const event::ui_event event, bool& handled);
133 
134  void signal_handler_left_button_up(const event::ui_event event, bool& handled);
135 
136  void signal_handler_left_button_click(const event::ui_event event, bool& handled);
137 
138  void signal_handler_sdl_wheel_up(const event::ui_event event, bool& handled);
139 
140  void signal_handler_sdl_wheel_down(const event::ui_event event, bool& handled);
141 };
142 
143 // }---------- DEFINITION ---------{
144 
146 {
147  explicit menu_button_definition(const config& cfg);
148 
150  {
151  explicit resolution(const config& cfg);
152  };
153 };
154 
155 // }---------- BUILDER -----------{
156 
157 class styled_widget;
158 
159 namespace implementation
160 {
161 
163 {
164 public:
165  explicit builder_menu_button(const config& cfg);
166 
168 
169  widget* build() const;
170 
171 private:
172  std::vector<::config> options_;
173 };
174 
175 } // namespace implementation
176 
177 // }------------ END --------------
178 
179 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
Small abstract helper class.
std::string get_value_string() const
Returns the value of the selected row.
Definition: menu_button.hpp:73
virtual widget * build() const =0
const ::config & get_value_config() const
Returns the entire config object for the selected row.
Definition: menu_button.hpp:79
Simple push button.
Definition: menu_button.hpp:41
Base class for all widgets.
Definition: widget.hpp:47
virtual void set_value(unsigned value, bool fire_event=false) override
Inherited from selectable_item.
Definition: menu_button.hpp:67
Generic file dialog.
Definition: field-fwd.hpp:22
virtual unsigned get_value() const override
Inherited from selectable_item.
Definition: menu_button.hpp:64
std::string selected
state_t
Possible states of the widget.
Definition: menu_button.hpp:95
state_t state_
Current state of the widget.
bool fire_event(const ui_event event, std::vector< std::pair< widget *, ui_event >> &event_chain, widget *dispatcher, widget *w, F &&... params)
Helper function for fire_event.
window * build(const builder_window::window_resolution *definition)
Builds a window.
std::vector<::config > values_
void set_keep_open(const bool keep_open)
Definition: menu_button.hpp:84
Base class for all visible items.
point resolution()
Definition: general.cpp:373
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
virtual unsigned num_states() const override
Inherited from selectable_item.
Definition: menu_button.hpp:70
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
ui_event
The event send to the dispatcher.
Definition: handler.hpp:55