The Battle for Wesnoth  1.15.12+dev
drop_down_menu.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
18 #include "sdl/rect.hpp"
19 #include <optional>
20 
21 #include <boost/dynamic_bitset.hpp>
22 
23 #include <functional>
24 
25 class config;
26 
27 namespace gui2
28 {
29 class styled_widget;
30 
31 namespace dialogs
32 {
33 /** Used by the menu_button widget. */
35 {
36 public:
37  /** Menu was invoked from a widget (currently a [multi]menu_button). Its position and markup settings will be derived from there. */
38  drop_down_menu(styled_widget* parent, const std::vector<config>& items, int selected_item, bool keep_open);
39 
40  /** Menu was invoked manually. Position and markup settings must be provided here. */
41  drop_down_menu(SDL_Rect button_pos, const std::vector<config>& items, int selected_item, bool use_markup, bool keep_open);
42 
43  int selected_item() const
44  {
45  return selected_item_;
46  }
47 
48  /** If a toggle button widget is present, returns the toggled state of each row's button. */
49  boost::dynamic_bitset<> get_toggle_states() const;
50 
51 private:
52  // TODO: evaluate exposing this publically via the [multi]menu_button widgets
53  struct entry_data
54  {
55  entry_data(const config& cfg);
56 
57  /** If present, column 1 will have a toggle button. The value indicates its initial state. */
58  std::optional<bool> checkbox;
59 
60  /** If no checkbox is present, the icon at this path will be shown in column 1. */
61  std::string icon;
62 
63  /** Is present, column 2 will display the image at this path. */
64  std::optional<std::string> image;
65 
66  /** If no image is present, this text will be shown in column 2. */
68 
69  /** If present, this text will be shown in column 3. */
70  std::optional<t_string> details;
71 
72  /** Tooltip text for the entire row. */
74  };
75 
76  /** The widget that invoked this dialog, if applicable. */
78 
79  /** Configuration of each row. */
80  std::vector<entry_data> items_;
81 
82  /**
83  * The screen location of the menu_button button that triggered this droplist.
84  * Note: we don't adjust the location of this dialog to when resizing the window.
85  * Instead this dialog automatically closes itself on resizing.
86  */
87  SDL_Rect button_pos_;
88 
90 
92 
93  /**
94  * Whether to keep this dialog open after a click occurs not handled by special exceptions
95  * such as scrollbars and toggle buttons.
96  */
97  bool keep_open_;
98 
99  /**
100  * When menu is invoked on a long-touch timer, a following mouse-up event will close it.
101  * This flag prevents that: the menu will only be closed on a mouse-up that follows a mouse-down.
102  * */
104 
105  virtual const std::string& window_id() const override;
106 
107  virtual void pre_show(window& window) override;
108 
109  virtual void post_show(window& window) override;
110 
111  void mouse_up_callback(bool&, bool&, const point& coordinate);
112 
113  void mouse_down_callback();
114 };
115 
116 } // namespace dialogs
117 } // 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:22
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:23
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:59
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
SDL_Rect button_pos_
The screen location of the menu_button button that triggered this droplist.