The Battle for Wesnoth  1.17.17+dev
multimenu_button.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2023
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
20 
22 
25 
26 #include <boost/dynamic_bitset.hpp>
27 
28 class config;
29 
30 namespace gui2
31 {
32 namespace implementation
33 {
34 struct builder_multimenu_button;
35 }
36 
37 // ------------ WIDGET -----------{
38 
39 /**
40  * @ingroup GUIWidgetWML
41  *
42  * A multimenu_button is a styled_widget to choose an element from a list of elements.
43  *
44  * When a multimenu_button has a return value it sets the return value for the window.
45  * Normally this closes the window and returns this value to the caller.
46  * The return value can either be defined by the user or determined from the id of the multimenu_button.
47  * The return value has a higher precedence as the one defined by the id.
48  * (Of course it's weird to give a multimenu_button an id and then override its return value.)
49  *
50  * 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.
51  * This has a higher precedence as return_value.
52  *
53  * List with the multimenu_button specific variables:
54  * Key |Type |Default |Description
55  * ---------------|------------------------------------|---------|-----------
56  * return_value_id| @ref guivartype_string "string" |"" |The return value id.
57  * return_value | @ref guivartype_int "int" |0 |The return value.
58  * maximum_shown | @ref guivartype_int "int" |-1 |The maximum number of currently selected values to list on the button.
59  *
60  * The following states exist:
61  * * state_enabled - the multimenu_button is enabled.
62  * * state_disabled - the multimenu_button is disabled.
63  * * state_pressed - the left mouse multimenu_button is down.
64  * * state_focused - the mouse is over the multimenu_button.
65  */
67 {
68 public:
70 
71  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
72 
73  /** See @ref styled_widget::set_active. */
74  virtual void set_active(const bool active) override;
75 
76  /** See @ref styled_widget::get_active. */
77  virtual bool get_active() const override;
78 
79  /** See @ref styled_widget::get_state. */
80  virtual unsigned get_state() const override;
81 
82  /***** ***** ***** setters / getters for members ***** ****** *****/
83 
84  /**
85  * Sets the maximum number of selected elements shown on the label.
86  * If more are selected, the label will say "and N others".
87  *
88  * @param max The maximum number of elements to show
89  */
90  void set_max_shown(const unsigned max)
91  {
92  max_shown_ = max;
93  }
94 
95  /**
96  * Get the maximum number of selected elements shown on the label.
97  *
98  * @returns The maximum number of elements to show
99  */
100  unsigned get_max_shown()
101  {
102  return max_shown_;
103  }
104 
105  /**
106  * Get the number of options available in the menu
107  *
108  * @returns The number of options in the menu
109  */
110  unsigned num_options()
111  {
112  return values_.size();
113  }
114 
115  /**
116  * Select an option in the menu
117  *
118  * @param option The option to select
119  * @param selected True to select it, or false to deselect it
120  */
121  void select_option(const unsigned option, const bool selected = true);
122 
123  /**
124  * Set the options selected in the menu.
125  *
126  * @param states A mask specifying which options to select and deselect
127  */
128  void select_options(boost::dynamic_bitset<> states);
129 
130  /**
131  * Set the available menu options.
132  *
133  * @param values A list of options to show in the menu
134  */
135  void set_values(const std::vector<::config>& values);
136 
137  /**
138  * Get the current state of the menu options.
139  *
140  * @returns A mask specifying which options are selected
141  */
142  boost::dynamic_bitset<> get_toggle_states() const
143  {
144  return toggle_states_;
145  }
146 
147  /**
148  * Deselect all the menu options.
149  */
150  void reset_toggle_states();
151 
152 private:
153  /**
154  * Possible states of the widget.
155  *
156  * Note the order of the states must be the same as defined in settings.hpp.
157  */
158  enum state_t {
163  };
164 
165  void set_state(const state_t state);
166 
167  /**
168  * Current state of the widget.
169  *
170  * The state of the widget determines what to render and how the widget
171  * reacts to certain 'events'.
172  */
174 
175  /**
176  * The maximum number of selected states to list in the label
177  */
178  unsigned max_shown_;
179 
180  std::vector<::config> values_;
181 
182  boost::dynamic_bitset<> toggle_states_;
183 
185 
187  void update_label();
188 
189 public:
190  /** Static type getter that does not rely on the widget being constructed. */
191  static const std::string& type();
192 
193 private:
194  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
195  virtual const std::string& get_control_type() const override;
196 
197  /***** ***** ***** signal handlers ***** ****** *****/
198 
199  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
200 
201  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
202 
203  void signal_handler_left_button_down(const event::ui_event event, bool& handled);
204 
205  void signal_handler_left_button_up(const event::ui_event event, bool& handled);
206 
207  void signal_handler_left_button_click(const event::ui_event event, bool& handled);
208 
210 };
211 
212 // }---------- DEFINITION ---------{
213 
215 {
216  explicit multimenu_button_definition(const config& cfg);
217 
219  {
220  explicit resolution(const config& cfg);
221  };
222 };
223 
224 // }---------- BUILDER -----------{
225 
226 class styled_widget;
227 
228 namespace implementation
229 {
230 
232 {
233 public:
234  explicit builder_multimenu_button(const config& cfg);
235 
237 
238  virtual std::unique_ptr<widget> build() const override;
239 
240 private:
241  unsigned max_shown_;
242  std::vector<::config> options_;
243 };
244 
245 } // namespace implementation
246 
247 // }------------ END --------------
248 
249 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
Used by the menu_button widget.
A multimenu_button is a styled_widget to choose an element from a list of elements.
boost::dynamic_bitset toggle_states_
void signal_handler_mouse_enter(const event::ui_event event, bool &handled)
state_t state_
Current state of the widget.
void select_options(boost::dynamic_bitset<> states)
Set the options selected in the menu.
void set_max_shown(const unsigned max)
Sets the maximum number of selected elements shown on the label.
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
void signal_handler_left_button_click(const event::ui_event event, bool &handled)
void reset_toggle_states()
Deselect all the menu options.
unsigned get_max_shown()
Get the maximum number of selected elements shown on the label.
void set_values(const std::vector<::config > &values)
Set the available menu options.
unsigned num_options()
Get the number of options available in the menu.
void signal_handler_left_button_up(const event::ui_event event, bool &handled)
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
std::vector<::config > values_
virtual bool get_active() const override
See styled_widget::get_active.
state_t
Possible states of the widget.
virtual void set_active(const bool active) override
See styled_widget::set_active.
void signal_handler_left_button_down(const event::ui_event event, bool &handled)
void select_option(const unsigned option, const bool selected=true)
Select an option in the menu.
dialogs::drop_down_menu * droplist_
void signal_handler_mouse_leave(const event::ui_event event, bool &handled)
multimenu_button(const implementation::builder_multimenu_button &builder)
boost::dynamic_bitset get_toggle_states() const
Get the current state of the menu options.
unsigned max_shown_
The maximum number of selected states to list in the label.
void set_state(const state_t state)
virtual unsigned get_state() const override
See styled_widget::get_state.
Base class for all visible items.
std::string selected
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
Generic file dialog.
Contains the implementation details for lexical_cast and shouldn't be used directly.
virtual std::unique_ptr< widget > build() const override
virtual std::unique_ptr< widget > build() const=0
multimenu_button_definition(const config &cfg)
Base class of a resolution, contains the common keys for a resolution.