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 
36 {
37 public:
39 
40  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
41 
42  /** See @ref styled_widget::set_active. */
43  virtual void set_active(const bool active) override;
44 
45  /** See @ref styled_widget::get_active. */
46  virtual bool get_active() const override;
47 
48  /** See @ref styled_widget::get_state. */
49  virtual unsigned get_state() const override;
50 
51  /***** ***** ***** setters / getters for members ***** ****** *****/
52 
53  /**
54  * Sets the maximum number of selected elements shown on the label.
55  * If more are selected, the label will say "and N others".
56  *
57  * @param max The maximum number of elements to show
58  */
59  void set_max_shown(const unsigned max)
60  {
61  max_shown_ = max;
62  }
63 
64  /**
65  * Get the maximum number of selected elements shown on the label.
66  *
67  * @returns The maximum number of elements to show
68  */
69  unsigned get_max_shown()
70  {
71  return max_shown_;
72  }
73 
74  /**
75  * Get the number of options available in the menu
76  *
77  * @returns The number of options in the menu
78  */
79  unsigned num_options()
80  {
81  return values_.size();
82  }
83 
84  /**
85  * Select an option in the menu
86  *
87  * @param option The option to select
88  * @param selected True to select it, or false to deselect it
89  */
90  void select_option(const unsigned option, const bool selected = true);
91 
92  /**
93  * Set the options selected in the menu.
94  *
95  * @param states A mask specifying which options to select and deselect
96  */
97  void select_options(boost::dynamic_bitset<> states);
98 
99  /**
100  * Set the available menu options.
101  *
102  * @param values A list of options to show in the menu
103  */
104  void set_values(const std::vector<::config>& values);
105 
106  /**
107  * Get the current state of the menu options.
108  *
109  * @returns A mask specifying which options are selected
110  */
111  boost::dynamic_bitset<> get_toggle_states() const
112  {
113  return toggle_states_;
114  }
115 
116  /**
117  * Deselect all the menu options.
118  */
119  void reset_toggle_states();
120 
121 private:
122  /**
123  * Possible states of the widget.
124  *
125  * Note the order of the states must be the same as defined in settings.hpp.
126  */
127  enum state_t {
132  };
133 
134  void set_state(const state_t state);
135 
136  /**
137  * Current state of the widget.
138  *
139  * The state of the widget determines what to render and how the widget
140  * reacts to certain 'events'.
141  */
143 
144  /**
145  * The maximum number of selected states to list in the label
146  */
147  unsigned max_shown_;
148 
149  std::vector<::config> values_;
150 
151  boost::dynamic_bitset<> toggle_states_;
152 
154 
156  void update_label();
157 
158 public:
159  /** Static type getter that does not rely on the widget being constructed. */
160  static const std::string& type();
161 
162 private:
163  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
164  virtual const std::string& get_control_type() const override;
165 
166  /***** ***** ***** signal handlers ***** ****** *****/
167 
168  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
169 
170  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
171 
172  void signal_handler_left_button_down(const event::ui_event event, bool& handled);
173 
174  void signal_handler_left_button_up(const event::ui_event event, bool& handled);
175 
176  void signal_handler_left_button_click(const event::ui_event event, bool& handled);
177 
179 };
180 
181 // }---------- DEFINITION ---------{
182 
184 {
185  explicit multimenu_button_definition(const config& cfg);
186 
188  {
189  explicit resolution(const config& cfg);
190  };
191 };
192 
193 // }---------- BUILDER -----------{
194 
195 
196 namespace implementation
197 {
198 
200 {
201 public:
202  explicit builder_multimenu_button(const config& cfg);
203 
205 
206  virtual std::unique_ptr<widget> build() const override;
207 
208 private:
209  unsigned max_shown_;
210  std::vector<::config> options_;
211 };
212 
213 } // namespace implementation
214 
215 // }------------ END --------------
216 
217 } // 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.
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.
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)