The Battle for Wesnoth  1.19.5+dev
drop_down_menu.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 #include "utils/optional_fwd.hpp"
20 
21 #include <boost/dynamic_bitset.hpp>
22 
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(SDL_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 
47  /** If a toggle button widget is present, returns the toggled state of each row's button. */
48  boost::dynamic_bitset<> get_toggle_states() const;
49 
50 private:
51  // TODO: evaluate exposing this publically via the [multi]menu_button widgets
52  struct entry_data
53  {
54  entry_data(const config& cfg);
55 
56  /** If present, column 1 will have a toggle button. The value indicates its initial state. */
57  utils::optional<bool> checkbox;
58 
59  /** If no checkbox is present, the icon at this path will be shown in column 1. */
60  std::string icon;
61 
62  /** Is present, column 2 will display the image at this path. */
63  utils::optional<std::string> image;
64 
65  /** If no image is present, this text will be shown in column 2. */
67 
68  /** If present, this text will be shown in column 3. */
69  utils::optional<t_string> details;
70 
71  /** Tooltip text for the entire row. */
73  };
74 
75  /** The widget that invoked this dialog, if applicable. */
77 
78  /** Configuration of each row. */
79  std::vector<entry_data> items_;
80 
81  /**
82  * The screen location of the menu_button button that triggered this droplist.
83  * Note: we don't adjust the location of this dialog to when resizing the window.
84  * Instead this dialog automatically closes itself on resizing.
85  */
86  SDL_Rect button_pos_;
87 
89 
91 
92  /**
93  * Whether to keep this dialog open after a click occurs not handled by special exceptions
94  * such as scrollbars and toggle buttons.
95  */
96  bool keep_open_;
97 
98  /**
99  * When menu is invoked on a long-touch timer, a following mouse-up event will close it.
100  * This flag prevents that: the menu will only be closed on a mouse-up that follows a mouse-down.
101  * */
103 
104  virtual const std::string& window_id() const override;
105 
106  virtual void pre_show() override;
107 
108  virtual void post_show() override;
109 
110  void mouse_up_callback(bool&, bool&, const point& coordinate);
111 
112  void mouse_down_callback();
113 };
114 
115 } // namespace dialogs
116 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
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.
SDL_Rect button_pos_
The screen location of the menu_button button that triggered this droplist.
bool keep_open_
Whether to keep this dialog open after a click occurs not handled by special exceptions such as scrol...
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.
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.
styled_widget * parent_
The widget that invoked this dialog, if applicable.
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.
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