The Battle for Wesnoth  1.15.12+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  * @ingroup GUIWidgetWML
40  *
41  * A multimenu_button is a styled_widget to choose an element from a list of elements.
42  *
43  * When a multimenu_button has a return value it sets the return value for the window.
44  * Normally this closes the window and returns this value to the caller.
45  * The return value can either be defined by the user or determined from the id of the multimenu_button.
46  * The return value has a higher precedence as the one defined by the id.
47  * (Of course it's weird to give a multimenu_button an id and then override its return value.)
48  *
49  * When the multimenu_button doesn't have a standard id, but you still want to use the return value of that id, use return_value_id instead.
50  * This has a higher precedence as return_value.
51  *
52  * List with the multimenu_button specific variables:
53  * Key |Type |Default |Description
54  * ---------------|------------------------------------|---------|-----------
55  * return_value_id| @ref guivartype_string "string" |"" |The return value id.
56  * return_value | @ref guivartype_int "int" |0 |The return value.
57  * maximum_shown | @ref guivartype_int "int" |-1 |The maximum number of currently selected values to list on the button.
58  *
59  * The following states exist:
60  * * state_enabled - the multimenu_button is enabled.
61  * * state_disabled - the multimenu_button is disabled.
62  * * state_pressed - the left mouse multimenu_button is down.
63  * * state_focused - the mouse is over the multimenu_button.
64  */
66 {
67 public:
69 
70  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
71 
72  /** See @ref styled_widget::set_active. */
73  virtual void set_active(const bool active) override;
74 
75  /** See @ref styled_widget::get_active. */
76  virtual bool get_active() const override;
77 
78  /** See @ref styled_widget::get_state. */
79  virtual unsigned get_state() const override;
80 
81  /***** ***** ***** setters / getters for members ***** ****** *****/
82 
83  /**
84  * Sets the maximum number of selected elements shown on the label.
85  * If more are selected, the label will say "and N others".
86  *
87  * @param max The maximum number of elements to show
88  */
89  void set_max_shown(const unsigned max)
90  {
91  max_shown_ = max;
92  }
93 
94  /**
95  * Get the maximum number of selected elements shown on the label.
96  *
97  * @returns The maximum number of elements to show
98  */
99  unsigned get_max_shown()
100  {
101  return max_shown_;
102  }
103 
104  /**
105  * Get the number of options available in the menu
106  *
107  * @returns The number of options in the menu
108  */
109  unsigned num_options()
110  {
111  return values_.size();
112  }
113 
114  /**
115  * Select an option in the menu
116  *
117  * @param option The option to select
118  * @param selected True to select it, or false to deselect it
119  */
120  void select_option(const unsigned option, const bool selected = true);
121 
122  /**
123  * Set the options selected in the menu.
124  *
125  * @param states A mask specifying which options to select and deselect
126  */
127  void select_options(boost::dynamic_bitset<> states);
128 
129  /**
130  * Set the available menu options.
131  *
132  * @param values A list of options to show in the menu
133  */
134  void set_values(const std::vector<::config>& values);
135 
136  /**
137  * Get the current state of the menu options.
138  *
139  * @returns A mask specifying which options are selected
140  */
141  boost::dynamic_bitset<> get_toggle_states() const
142  {
143  return toggle_states_;
144  }
145 
146  /**
147  * Deselect all the menu options.
148  */
149  void reset_toggle_states();
150 
151 private:
152  /**
153  * Possible states of the widget.
154  *
155  * Note the order of the states must be the same as defined in settings.hpp.
156  */
157  enum state_t {
162  };
163 
164  void set_state(const state_t state);
165 
166  /**
167  * Current state of the widget.
168  *
169  * The state of the widget determines what to render and how the widget
170  * reacts to certain 'events'.
171  */
173 
174  /**
175  * The maximum number of selected states to list in the label
176  */
177  unsigned max_shown_;
178 
179  std::vector<::config> values_;
180 
181  boost::dynamic_bitset<> toggle_states_;
182 
184 
185  void update_config_from_toggle_states();
186  void update_label();
187 
188 public:
189  /** Static type getter that does not rely on the widget being constructed. */
190  static const std::string& type();
191 
192 private:
193  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
194  virtual const std::string& get_control_type() const override;
195 
196  /***** ***** ***** signal handlers ***** ****** *****/
197 
198  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
199 
200  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
201 
202  void signal_handler_left_button_down(const event::ui_event event, bool& handled);
203 
204  void signal_handler_left_button_up(const event::ui_event event, bool& handled);
205 
206  void signal_handler_left_button_click(const event::ui_event event, bool& handled);
207 
208  void signal_handler_notify_changed();
209 };
210 
211 // }---------- DEFINITION ---------{
212 
214 {
215  explicit multimenu_button_definition(const config& cfg);
216 
218  {
219  explicit resolution(const config& cfg);
220  };
221 };
222 
223 // }---------- BUILDER -----------{
224 
225 class styled_widget;
226 
227 namespace implementation
228 {
229 
231 {
232 public:
233  explicit builder_multimenu_button(const config& cfg);
234 
236 
237  virtual widget* build() const override;
238 
239 private:
240  unsigned max_shown_;
241  std::vector<::config> options_;
242 };
243 
244 } // namespace implementation
245 
246 // }------------ END --------------
247 
248 } // 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:49
A multimenu_button is a styled_widget to choose an element from a list of elements.
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.
Base class for all visible items.
point resolution()
Definition: general.cpp:387
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
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:47