The Battle for Wesnoth  1.15.2+dev
multimenu_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_multimenu_button;
34 }
35 
36 // ------------ WIDGET -----------{
37 
38 /**
39  * Simple push button.
40  */
42 {
43 public:
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  /**
60  * Sets the maximum number of selected elements shown on the label.
61  * If more are selected, the label will say "and N others".
62  *
63  * @param max The maximum number of elements to show
64  */
65  void set_max_shown(const unsigned max)
66  {
67  max_shown_ = max;
68  }
69 
70  /**
71  * Get the maximum number of selected elements shown on the label.
72  *
73  * @returns The maximum number of elements to show
74  */
75  unsigned get_max_shown()
76  {
77  return max_shown_;
78  }
79 
80  /**
81  * Get the number of options available in the menu
82  *
83  * @returns The number of options in the menu
84  */
85  unsigned num_options()
86  {
87  return values_.size();
88  }
89 
90  /**
91  * Select an option in the menu
92  *
93  * @param option The option to select
94  * @param selected True to select it, or false to deselect it
95  */
96  void select_option(const unsigned option, const bool selected = true);
97 
98  /**
99  * Set the options selected in the menu.
100  *
101  * @param states A mask specifying which options to select and deselect
102  */
103  void select_options(boost::dynamic_bitset<> states);
104 
105  /**
106  * Set the available menu options.
107  *
108  * @param values A list of options to show in the menu
109  */
110  void set_values(const std::vector<::config>& values);
111 
112  /**
113  * Get the current state of the menu options.
114  *
115  * @returns A mask specifying which options are selected
116  */
117  boost::dynamic_bitset<> get_toggle_states() const
118  {
119  return toggle_states_;
120  }
121 
122  /**
123  * Deselect all the menu options.
124  */
125  void reset_toggle_states();
126 
127 private:
128  /**
129  * Possible states of the widget.
130  *
131  * Note the order of the states must be the same as defined in settings.hpp.
132  */
133  enum state_t {
138  };
139 
140  void set_state(const state_t state);
141 
142  /**
143  * Current state of the widget.
144  *
145  * The state of the widget determines what to render and how the widget
146  * reacts to certain 'events'.
147  */
149 
150  /**
151  * The maximum number of selected states to list in the label
152  */
153  unsigned max_shown_;
154 
155  std::vector<::config> values_;
156 
157  boost::dynamic_bitset<> toggle_states_;
158 
160 
161  void update_config_from_toggle_states();
162  void update_label();
163 
164 public:
165  /** Static type getter that does not rely on the widget being constructed. */
166  static const std::string& type();
167 
168 private:
169  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
170  virtual const std::string& get_control_type() const override;
171 
172  /***** ***** ***** signal handlers ***** ****** *****/
173 
174  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
175 
176  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
177 
178  void signal_handler_left_button_down(const event::ui_event event, bool& handled);
179 
180  void signal_handler_left_button_up(const event::ui_event event, bool& handled);
181 
182  void signal_handler_left_button_click(const event::ui_event event, bool& handled);
183 
184  void toggle_state_changed();
185 };
186 
187 // }---------- DEFINITION ---------{
188 
190 {
191  explicit multimenu_button_definition(const config& cfg);
192 
194  {
195  explicit resolution(const config& cfg);
196  };
197 };
198 
199 // }---------- BUILDER -----------{
200 
201 class styled_widget;
202 
203 namespace implementation
204 {
205 
207 {
208 public:
209  explicit builder_multimenu_button(const config& cfg);
210 
212 
213  widget* build() const;
214 
215 private:
216  unsigned max_shown_;
217  std::vector<::config> options_;
218 };
219 
220 } // namespace implementation
221 
222 // }------------ END --------------
223 
224 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
void set_max_shown(const unsigned max)
Sets the maximum number of selected elements shown on the label.
virtual widget * build() const =0
dialogs::drop_down_menu * droplist_
unsigned num_options()
Get the number of options available in the menu.
Base class for all widgets.
Definition: widget.hpp:47
Simple push button.
Generic file dialog.
Definition: field-fwd.hpp:22
state_t state_
Current state of the widget.
state_t
Possible states of the widget.
unsigned max_shown_
The maximum number of selected states to list in the label.
unsigned get_max_shown()
Get the maximum number of selected elements shown on the label.
Used by the menu_button widget.
std::string selected
boost::dynamic_bitset toggle_states_
boost::dynamic_bitset get_toggle_states() const
Get the current state of the menu options.
window * build(const builder_window::window_resolution *definition)
Builds a window.
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:68
std::vector<::config > values_
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