The Battle for Wesnoth  1.19.0-dev
menu_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 
22 
23 namespace gui2
24 {
25 namespace implementation
26 {
27  struct builder_menu_button;
28 }
29 
30 // ------------ WIDGET -----------{
31 
32 /**
33  * @ingroup GUIWidgetWML
34  *
35  * A menu_button is a styled_widget to choose an element from a list of elements.
36  *
37  * When a menu_button has a return value it sets the return value for the window.
38  * Normally this closes the window and returns this value to the caller.
39  * The return value can either be defined by the user or determined from the id of the menu_button.
40  * The return value has a higher precedence as the one defined by the id.
41  * (Of course it's weird to give a menu_button an id and then override its return value.)
42  *
43  * 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.
44  * This has a higher precedence as return_value.
45  *
46  * List with the menu_button specific variables:
47  * Key |Type |Default |Description
48  * ---------------|------------------------------------|---------|-----------
49  * return_value_id| @ref guivartype_string "string" |"" |The return value id.
50  * return_value | @ref guivartype_int "int" |0 |The return value.
51  *
52  * The following states exist:
53  * * state_enabled - the menu_button is enabled.
54  * * state_disabled - the menu_button is disabled.
55  * * state_pressed - the left mouse menu_button is down.
56  * * state_focused - the mouse is over the menu_button.
57  */
59 {
60 public:
61  explicit menu_button(const implementation::builder_menu_button& builder);
62 
63  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
64 
65  /** See @ref styled_widget::set_active. */
66  virtual void set_active(const bool active) override;
67 
68  /** See @ref styled_widget::get_active. */
69  virtual bool get_active() const override;
70 
71  /** See @ref styled_widget::get_state. */
72  virtual unsigned get_state() const override;
73 
74  /***** ***** ***** setters / getters for members ***** ****** *****/
75 
76  void set_values(const std::vector<::config>& values, unsigned selected = 0);
77 
78  void set_selected(unsigned selected, bool fire_event = true);
79 
80  /** Inherited from selectable_item */
81  virtual unsigned get_value() const override { return selected_; }
82 
83  /** Inherited from selectable_item */
84  virtual void set_value(unsigned value, bool fire_event = false) override { set_selected(value, fire_event); }
85 
86  /** Inherited from selectable_item */
87  virtual unsigned num_states() const override { return values_.size(); }
88 
89  /** Returns the value of the selected row */
90  std::string get_value_string() const
91  {
92  return values_[selected_]["label"];
93  }
94 
95  /** Returns the entire config object for the selected row. */
96  const ::config& get_value_config() const
97  {
98  return values_[selected_];
99  }
100 
101  void set_keep_open(const bool keep_open)
102  {
103  keep_open_ = keep_open;
104  }
105 
106 private:
107  /**
108  * Possible states of the widget.
109  *
110  * Note the order of the states must be the same as defined in settings.hpp.
111  */
112  enum state_t {
117  };
118 
119  void set_state(const state_t state);
120 
121  /**
122  * Current state of the widget.
123  *
124  * The state of the widget determines what to render and how the widget
125  * reacts to certain 'events'.
126  */
128 
129  std::vector<::config> values_;
130 
131  unsigned selected_;
132 
134 
135 public:
136  /** Static type getter that does not rely on the widget being constructed. */
137  static const std::string& type();
138 
139 private:
140  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
141  virtual const std::string& get_control_type() const override;
142 
143  /***** ***** ***** signal handlers ***** ****** *****/
144 
145  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
146 
147  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
148 
149  void signal_handler_left_button_down(const event::ui_event event, bool& handled);
150 
151  void signal_handler_left_button_up(const event::ui_event event, bool& handled);
152 
153  void signal_handler_left_button_click(const event::ui_event event, bool& handled);
154 
155  void signal_handler_sdl_wheel_up(const event::ui_event event, bool& handled);
156 
157  void signal_handler_sdl_wheel_down(const event::ui_event event, bool& handled);
158 };
159 
160 // }---------- DEFINITION ---------{
161 
163 {
164  explicit menu_button_definition(const config& cfg);
165 
167  {
168  explicit resolution(const config& cfg);
169  };
170 };
171 
172 // }---------- BUILDER -----------{
173 
174 namespace implementation
175 {
176 
178 {
179 public:
180  explicit builder_menu_button(const config& cfg);
181 
183 
184  virtual std::unique_ptr<widget> build() const override;
185 
186 private:
187  std::vector<::config> options_;
188 };
189 
190 } // namespace implementation
191 
192 // }------------ END --------------
193 
194 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
A menu_button is a styled_widget to choose an element from a list of elements.
Definition: menu_button.hpp:59
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:91
const ::config & get_value_config() const
Returns the entire config object for the selected row.
Definition: menu_button.hpp:96
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:84
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:90
void set_values(const std::vector<::config > &values, unsigned selected=0)
void signal_handler_mouse_enter(const event::ui_event event, bool &handled)
Definition: menu_button.cpp:99
menu_button(const implementation::builder_menu_button &builder)
Definition: menu_button.cpp:40
virtual unsigned get_value() const override
Inherited from selectable_item.
Definition: menu_button.hpp:81
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: menu_button.cpp:86
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: menu_button.cpp:74
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:87
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:81
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.