The Battle for Wesnoth  1.19.13+dev
drop_down_menu.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2025
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 #include "gui/widgets/group.hpp"
20 #include "utils/optional_fwd.hpp"
21 
22 #include <boost/dynamic_bitset.hpp>
23 
24 class config;
25 
26 namespace gui2
27 {
28 class styled_widget;
29 
30 namespace dialogs
31 {
32 /** Used by the menu_button widget. */
34 {
35 public:
36  /** Menu was invoked from a widget (currently a [multi]menu_button). Its position and markup settings will be derived from there. */
37  drop_down_menu(styled_widget* parent, const std::vector<config>& items, int selected_item, bool keep_open);
38 
39  /** Menu was invoked manually. Position and markup settings must be provided here. */
40  drop_down_menu(rect button_pos, const std::vector<config>& items, int selected_item, bool use_markup, bool keep_open);
41 
42  int selected_item() const
43  {
44  return selected_item_;
45  }
46 
48  {
49  return selected_item_pos_;
50  }
51 
52  /** If a toggle button widget is present, returns the toggled state of each row's button. */
53  boost::dynamic_bitset<> get_toggle_states() const;
54 
55 private:
56  // TODO: evaluate exposing this publicly via the [multi]menu_button widgets
57  struct entry_data
58  {
59  entry_data(const config& cfg);
60 
61  /** If present, column 1 will have a toggle button. The value indicates its initial state. */
62  utils::optional<bool> checkbox;
63 
64  /** If present, column 1 will have a radio toggle button. The value indicates its initial state. */
65  utils::optional<bool> radio;
66 
67  /** If no checkbox is present, the icon at this path will be shown in column 1. */
68  std::string icon;
69 
70  /** Is present, column 2 will display the image at this path. */
71  utils::optional<std::string> image;
72 
73  /** If no image is present, this text will be shown in column 2. */
75 
76  /** If present, this text will be shown in column 3. */
77  utils::optional<t_string> details;
78 
79  /** Tooltip text for the entire row. */
81  };
82 
83  /** The widget that invoked this dialog, if applicable. */
85 
86  /** Configuration of each row. */
87  std::vector<entry_data> items_;
88 
89  /** Manages radio buttons, if present. */
90  utils::optional<gui2::group<int>> radio_manager_;
91 
92  /**
93  * The screen location of the menu_button button that triggered this droplist.
94  * Note: we don't adjust the location of this dialog to when resizing the window.
95  * Instead this dialog automatically closes itself on resizing.
96  */
98 
101 
103 
104  /**
105  * Whether to keep this dialog open after a click occurs not handled by special exceptions
106  * such as scrollbars and toggle buttons.
107  */
109 
110  /**
111  * When menu is invoked on a long-touch timer, a following mouse-up event will close it.
112  * This flag prevents that: the menu will only be closed on a mouse-up that follows a mouse-down.
113  * */
115 
116  virtual const std::string& window_id() const override;
117 
118  virtual void pre_show() override;
119 
120  virtual void post_show() override;
121 
122  void mouse_up_callback(bool&, bool&, const point& coordinate);
123 
124  void mouse_down_callback();
125 
126  //
127  // ThemeWML transition API. Don't use for anything else!!!
128  //
129 
130  /** If true, enables special handling of embedded toggle buttons. */
131  bool legacy_menu_mode_ = false;
132 
133  /** Helper toggle callback, only to be used for legacy theme handling. */
134  std::function<void(int)> theme_transition_toggle_callback_ = nullptr;
135 
136 public:
137  void set_legacy_menu_mode(bool value)
138  {
139  legacy_menu_mode_ = value;
140  }
141 
142  template<typename Func>
143  void set_legacy_toggle_callback(const Func& f)
144  {
146  }
147 };
148 
149 } // namespace dialogs
150 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
Used by the menu_button widget.
std::vector< entry_data > items_
Configuration of each row.
virtual void post_show() override
Actions to be taken after the window has been shown.
void set_legacy_toggle_callback(const Func &f)
bool keep_open_
Whether to keep this dialog open after a click occurs not handled by special exceptions such as scrol...
utils::optional< gui2::group< int > > radio_manager_
Manages radio buttons, if present.
virtual void pre_show() override
Actions to be taken before showing the window.
void mouse_up_callback(bool &, bool &, const point &coordinate)
drop_down_menu(styled_widget *parent, const std::vector< config > &items, int selected_item, bool keep_open)
Menu was invoked from a widget (currently a [multi]menu_button).
boost::dynamic_bitset get_toggle_states() const
If a toggle button widget is present, returns the toggled state of each row's button.
rect button_pos_
The screen location of the menu_button button that triggered this droplist.
virtual const std::string & window_id() const override
The ID of the window to build.
bool mouse_down_happened_
When menu is invoked on a long-touch timer, a following mouse-up event will close it.
std::function< void(int)> theme_transition_toggle_callback_
Helper toggle callback, only to be used for legacy theme handling.
styled_widget * parent_
The widget that invoked this dialog, if applicable.
void set_legacy_menu_mode(bool value)
bool legacy_menu_mode_
If true, enables special handling of embedded toggle buttons.
Abstract base class for all modal dialogs.
widget * parent()
Definition: widget.cpp:170
Various uncategorised dialogs.
Generic file dialog.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
t_string tooltip
Tooltip text for the entire row.
utils::optional< t_string > details
If present, this text will be shown in column 3.
utils::optional< std::string > image
Is present, column 2 will display the image at this path.
t_string label
If no image is present, this text will be shown in column 2.
utils::optional< bool > radio
If present, column 1 will have a radio toggle button.
std::string icon
If no checkbox is present, the icon at this path will be shown in column 1.
utils::optional< bool > checkbox
If present, column 1 will have a toggle button.
Holds a 2D point.
Definition: point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:49
#define f