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