The Battle for Wesnoth  1.19.2+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 
33 {
34 public:
35  explicit menu_button(const implementation::builder_menu_button& builder);
36 
37  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
38 
39  /** See @ref styled_widget::set_active. */
40  virtual void set_active(const bool active) override;
41 
42  /** See @ref styled_widget::get_active. */
43  virtual bool get_active() const override;
44 
45  /** See @ref styled_widget::get_state. */
46  virtual unsigned get_state() const override;
47 
48  /***** ***** ***** setters / getters for members ***** ****** *****/
49 
50  void set_values(const std::vector<::config>& values, unsigned selected = 0);
51 
52  void set_selected(unsigned selected, bool fire_event = true);
53 
54  /** Inherited from selectable_item */
55  virtual unsigned get_value() const override { return selected_; }
56 
57  /** Inherited from selectable_item */
58  virtual void set_value(unsigned value, bool fire_event = false) override { set_selected(value, fire_event); }
59 
60  /** Inherited from selectable_item */
61  virtual unsigned num_states() const override { return values_.size(); }
62 
63  /** Returns the value of the selected row */
64  std::string get_value_string() const
65  {
66  return values_[selected_]["label"];
67  }
68 
69  /** Returns the entire config object for the selected row. */
70  const ::config& get_value_config() const
71  {
72  return values_[selected_];
73  }
74 
75  void set_keep_open(const bool keep_open)
76  {
77  keep_open_ = keep_open;
78  }
79 
80 private:
81  /**
82  * Possible states of the widget.
83  *
84  * Note the order of the states must be the same as defined in settings.hpp.
85  */
86  enum state_t {
91  };
92 
93  void set_state(const state_t state);
94 
95  /**
96  * Current state of the widget.
97  *
98  * The state of the widget determines what to render and how the widget
99  * reacts to certain 'events'.
100  */
102 
103  std::vector<::config> values_;
104 
105  unsigned selected_;
106 
108 
109 public:
110  /** Static type getter that does not rely on the widget being constructed. */
111  static const std::string& type();
112 
113 private:
114  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
115  virtual const std::string& get_control_type() const override;
116 
117  /***** ***** ***** signal handlers ***** ****** *****/
118 
119  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
120 
121  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
122 
123  void signal_handler_left_button_down(const event::ui_event event, bool& handled);
124 
125  void signal_handler_left_button_up(const event::ui_event event, bool& handled);
126 
127  void signal_handler_left_button_click(const event::ui_event event, bool& handled);
128 
129  void signal_handler_sdl_wheel_up(const event::ui_event event, bool& handled);
130 
131  void signal_handler_sdl_wheel_down(const event::ui_event event, bool& handled);
132 };
133 
134 // }---------- DEFINITION ---------{
135 
137 {
138  explicit menu_button_definition(const config& cfg);
139 
141  {
142  explicit resolution(const config& cfg);
143  };
144 };
145 
146 // }---------- BUILDER -----------{
147 
148 namespace implementation
149 {
150 
152 {
153 public:
154  explicit builder_menu_button(const config& cfg);
155 
157 
158  virtual std::unique_ptr<widget> build() const override;
159 
160 private:
161  std::vector<::config> options_;
162 };
163 
164 } // namespace implementation
165 
166 // }------------ END --------------
167 
168 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
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:70
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:58
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:64
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:55
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:61
state_t
Possible states of the widget.
Definition: menu_button.hpp:86
void set_keep_open(const bool keep_open)
Definition: menu_button.hpp:75
virtual bool get_active() const override
See styled_widget::get_active.
Definition: menu_button.cpp:81
std::vector<::config > values_
Small abstract helper class.
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)