The Battle for Wesnoth  1.17.0-dev
drop_down_menu.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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 "sdl/rect.hpp"
20 #include <optional>
21 
22 #include <boost/dynamic_bitset.hpp>
23 
24 #include <functional>
25 
26 class config;
27 
28 namespace gui2
29 {
30 class styled_widget;
31 
32 namespace dialogs
33 {
34 /** Used by the menu_button widget. */
36 {
37 public:
38  /** Menu was invoked from a widget (currently a [multi]menu_button). Its position and markup settings will be derived from there. */
39  drop_down_menu(styled_widget* parent, const std::vector<config>& items, int selected_item, bool keep_open);
40 
41  /** Menu was invoked manually. Position and markup settings must be provided here. */
42  drop_down_menu(SDL_Rect button_pos, const std::vector<config>& items, int selected_item, bool use_markup, bool keep_open);
43 
44  int selected_item() const
45  {
46  return selected_item_;
47  }
48 
49  /** If a toggle button widget is present, returns the toggled state of each row's button. */
50  boost::dynamic_bitset<> get_toggle_states() const;
51 
52 private:
53  // TODO: evaluate exposing this publically via the [multi]menu_button widgets
54  struct entry_data
55  {
56  entry_data(const config& cfg);
57 
58  /** If present, column 1 will have a toggle button. The value indicates its initial state. */
59  std::optional<bool> checkbox;
60 
61  /** If no checkbox is present, the icon at this path will be shown in column 1. */
62  std::string icon;
63 
64  /** Is present, column 2 will display the image at this path. */
65  std::optional<std::string> image;
66 
67  /** If no image is present, this text will be shown in column 2. */
69 
70  /** If present, this text will be shown in column 3. */
71  std::optional<t_string> details;
72 
73  /** Tooltip text for the entire row. */
75  };
76 
77  /** The widget that invoked this dialog, if applicable. */
79 
80  /** Configuration of each row. */
81  std::vector<entry_data> items_;
82 
83  /**
84  * The screen location of the menu_button button that triggered this droplist.
85  * Note: we don't adjust the location of this dialog to when resizing the window.
86  * Instead this dialog automatically closes itself on resizing.
87  */
88  SDL_Rect button_pos_;
89 
91 
93 
94  /**
95  * Whether to keep this dialog open after a click occurs not handled by special exceptions
96  * such as scrollbars and toggle buttons.
97  */
98  bool keep_open_;
99 
100  /**
101  * When menu is invoked on a long-touch timer, a following mouse-up event will close it.
102  * This flag prevents that: the menu will only be closed on a mouse-up that follows a mouse-down.
103  * */
105 
106  virtual const std::string& window_id() const override;
107 
108  virtual void pre_show(window& window) override;
109 
110  virtual void post_show(window& window) override;
111 
112  void mouse_up_callback(bool&, bool&, const point& coordinate);
113 
114  void mouse_down_callback();
115 };
116 
117 } // namespace dialogs
118 } // namespace gui2
std::optional< t_string > details
If present, this text will be shown in column 3.
std::optional< bool > checkbox
If present, column 1 will have a toggle button.
void mouse_up_callback(bool &, bool &, const point &coordinate)
bool keep_open_
Whether to keep this dialog open after a click occurs not handled by special exceptions such as scrol...
std::optional< std::string > image
Is present, column 2 will display the image at this path.
styled_widget * parent_
The widget that invoked this dialog, if applicable.
const std::vector< std::string > items
Generic file dialog.
Definition: field-fwd.hpp:23
t_string tooltip
Tooltip text for the entire row.
t_string label
If no image is present, this text will be shown in column 2.
Used by the menu_button widget.
bool mouse_down_happened_
When menu is invoked on a long-touch timer, a following mouse-up event will close it...
virtual const std::string & window_id() const override
The id of the window to build.
Various uncategorised dialogs.
boost::dynamic_bitset get_toggle_states() const
If a toggle button widget is present, returns the toggled state of each row&#39;s button.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Holds a 2D point.
Definition: point.hpp:24
Base class for all visible items.
std::vector< entry_data > items_
Configuration of each row.
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).
std::string icon
If no checkbox is present, the icon at this path will be shown in column 1.
Contains the SDL_Rect helper code.
Abstract base class for all modal dialogs.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65
SDL_Rect button_pos_
The screen location of the menu_button button that triggered this droplist.