The Battle for Wesnoth  1.17.21+dev
menu_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 
23 
24 class config;
25 
26 namespace gui2
27 {
28 namespace implementation
29 {
30  struct builder_menu_button;
31 }
32 
33 // ------------ WIDGET -----------{
34 
35 /**
36  * @ingroup GUIWidgetWML
37  *
38  * A menu_button is a styled_widget to choose an element from a list of elements.
39  *
40  * When a menu_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 menu_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 menu_button an id and then override its return value.)
45  *
46  * When the menu_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 menu_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  *
55  * The following states exist:
56  * * state_enabled - the menu_button is enabled.
57  * * state_disabled - the menu_button is disabled.
58  * * state_pressed - the left mouse menu_button is down.
59  * * state_focused - the mouse is over the menu_button.
60  */
62 {
63 public:
64  explicit menu_button(const implementation::builder_menu_button& builder);
65 
66  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
67 
68  /** See @ref styled_widget::set_active. */
69  virtual void set_active(const bool active) override;
70 
71  /** See @ref styled_widget::get_active. */
72  virtual bool get_active() const override;
73 
74  /** See @ref styled_widget::get_state. */
75  virtual unsigned get_state() const override;
76 
77  /***** ***** ***** setters / getters for members ***** ****** *****/
78 
79  void set_values(const std::vector<::config>& values, unsigned selected = 0);
80 
81  void set_selected(unsigned selected, bool fire_event = true);
82 
83  /** Inherited from selectable_item */
84  virtual unsigned get_value() const override { return selected_; }
85 
86  /** Inherited from selectable_item */
87  virtual void set_value(unsigned value, bool fire_event = false) override { set_selected(value, fire_event); }
88 
89  /** Inherited from selectable_item */
90  virtual unsigned num_states() const override { return values_.size(); }
91 
92  /** Returns the value of the selected row */
93  std::string get_value_string() const
94  {
95  return values_[selected_]["label"];
96  }
97 
98  /** Returns the entire config object for the selected row. */
99  const ::config& get_value_config() const
100  {
101  return values_[selected_];
102  }
103 
104  void set_keep_open(const bool keep_open)
105  {
106  keep_open_ = keep_open;
107  }
108 
109 private:
110  /**
111  * Possible states of the widget.
112  *
113  * Note the order of the states must be the same as defined in settings.hpp.
114  */
115  enum state_t {
120  };
121 
122  void set_state(const state_t state);
123 
124  /**
125  * Current state of the widget.
126  *
127  * The state of the widget determines what to render and how the widget
128  * reacts to certain 'events'.
129  */
131 
132  std::vector<::config> values_;
133 
134  unsigned selected_;
135 
137 
138 public:
139  /** Static type getter that does not rely on the widget being constructed. */
140  static const std::string& type();
141 
142 private:
143  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
144  virtual const std::string& get_control_type() const override;
145 
146  /***** ***** ***** signal handlers ***** ****** *****/
147 
148  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
149 
150  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
151 
152  void signal_handler_left_button_down(const event::ui_event event, bool& handled);
153 
154  void signal_handler_left_button_up(const event::ui_event event, bool& handled);
155 
156  void signal_handler_left_button_click(const event::ui_event event, bool& handled);
157 
158  void signal_handler_sdl_wheel_up(const event::ui_event event, bool& handled);
159 
160  void signal_handler_sdl_wheel_down(const event::ui_event event, bool& handled);
161 };
162 
163 // }---------- DEFINITION ---------{
164 
166 {
167  explicit menu_button_definition(const config& cfg);
168 
170  {
171  explicit resolution(const config& cfg);
172  };
173 };
174 
175 // }---------- BUILDER -----------{
176 
177 class styled_widget;
178 
179 namespace implementation
180 {
181 
183 {
184 public:
185  explicit builder_menu_button(const config& cfg);
186 
188 
189  virtual std::unique_ptr<widget> build() const override;
190 
191 private:
192  std::vector<::config> options_;
193 };
194 
195 } // namespace implementation
196 
197 // }------------ END --------------
198 
199 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
A menu_button is a styled_widget to choose an element from a list of elements.
Definition: menu_button.hpp:62
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
void set_state(const state_t state)
Definition: menu_button.cpp:94
const ::config & get_value_config() const
Returns the entire config object for the selected row.
Definition: menu_button.hpp:99
void signal_handler_left_button_click(const event::ui_event event, bool &handled)
state_t state_
Current state of the widget.
void set_selected(unsigned selected, bool fire_event=true)
void signal_handler_left_button_down(const event::ui_event event, bool &handled)
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
void signal_handler_mouse_leave(const event::ui_event event, bool &handled)
virtual void set_value(unsigned value, bool fire_event=false) override
Inherited from selectable_item.
Definition: menu_button.hpp:87
void signal_handler_left_button_up(const event::ui_event event, bool &handled)
std::string get_value_string() const
Returns the value of the selected row.
Definition: menu_button.hpp:93
void set_values(const std::vector<::config > &values, unsigned selected=0)
void signal_handler_mouse_enter(const event::ui_event event, bool &handled)
menu_button(const implementation::builder_menu_button &builder)
Definition: menu_button.cpp:43
virtual unsigned get_value() const override
Inherited from selectable_item.
Definition: menu_button.hpp:84
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: menu_button.cpp:89
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: menu_button.cpp:77
void signal_handler_sdl_wheel_up(const event::ui_event event, bool &handled)
void signal_handler_sdl_wheel_down(const event::ui_event event, bool &handled)
virtual unsigned num_states() const override
Inherited from selectable_item.
Definition: menu_button.hpp:90
state_t
Possible states of the widget.
void set_keep_open(const bool keep_open)
virtual bool get_active() const override
See styled_widget::get_active.
Definition: menu_button.cpp:84
std::vector<::config > values_
Small abstract helper class.
Base class for all visible items.
std::string selected
bool fire_event(const ui_event event, const std::vector< std::pair< widget *, ui_event >> &event_chain, widget *dispatcher, widget *w, F &&... params)
Helper function for fire_event.
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
menu_button_definition(const config &cfg)
Base class of a resolution, contains the common keys for a resolution.