The Battle for Wesnoth  1.15.2+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 
20 #include <boost/dynamic_bitset.hpp>
21 
22 #include <functional>
23 
24 class config;
25 
26 namespace gui2
27 {
28 namespace dialogs
29 {
30 ///Used by the menu_button widget.
32 {
33 public:
34  drop_down_menu(SDL_Rect button_pos, const std::vector<config>& items, int selected_item, bool use_markup, bool keep_open, std::function<void()> callback_toggle_state_change = nullptr)
35  : items_(items)
36  , button_pos_(button_pos)
37  , selected_item_(selected_item)
38  , use_markup_(use_markup)
39  , keep_open_(keep_open)
40  , mouse_down_happened_(false)
41  , callback_toggle_state_change_(callback_toggle_state_change)
42  {
43  set_restore(true);
44  }
45 
46  int selected_item() const
47  {
48  return selected_item_;
49  }
50 
51  /** If a toggle button widget is present, returns the toggled state of each row's button. */
52  boost::dynamic_bitset<> get_toggle_states() const;
53 
54 private:
55  /** Configuration of each row. */
56  std::vector<config> items_;
57 
58  /**
59  * The screen location of the menu_button button that triggered this droplist.
60  * Note: we don't adjust the location of this dialog to when resizing the window.
61  * Instead this dialog automatically closes itself on resizing.
62  */
63  SDL_Rect button_pos_;
64 
66 
68 
69  /**
70  * Whether to keep this dialog open after a click occurs not handled by special exceptions
71  * such as scrollbars and toggle buttons.
72  */
73  bool keep_open_;
74 
75  /**
76  * When menu is invoked on a long-touch timer, a following mouse-up event will close it.
77  * This flag prevents that: the menu will only be closed on a mouse-up that follows a mouse-down.
78  * */
80 
81  /**
82  * If toggle buttons are used, this callback is called whenever the state of any toggle
83  * button changes.
84  */
85  std::function<void()> callback_toggle_state_change_;
86 
87  /** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
88  virtual const std::string& window_id() const override;
89 
90  /** Inherited from modal_dialog. */
91  virtual void pre_show(window& window) override;
92 
93  /** Inherited from modal_dialog. */
94  virtual void post_show(window& window) override;
95 
96  void mouse_up_callback(window& window, bool&, bool&, const point& coordinate);
97 
98  void mouse_down_callback();
99 };
100 
101 } // namespace dialogs
102 } // namespace gui2
bool keep_open_
Whether to keep this dialog open after a click occurs not handled by special exceptions such as scrol...
const std::vector< std::string > items
drop_down_menu(SDL_Rect button_pos, const std::vector< config > &items, int selected_item, bool use_markup, bool keep_open, std::function< void()> callback_toggle_state_change=nullptr)
Generic file dialog.
Definition: field-fwd.hpp:22
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
Inherited from modal_dialog, implemented by REGISTER_DIALOG.
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
Inherited from modal_dialog.
std::function< void()> callback_toggle_state_change_
If toggle buttons are used, this callback is called whenever the state of any toggle button changes...
Holds a 2D point.
Definition: point.hpp:23
void mouse_up_callback(window &window, bool &, bool &, const point &coordinate)
Contains the SDL_Rect helper code.
Abstract base class for all modal dialogs.
std::vector< config > items_
Configuration of each row.
virtual void post_show(window &window) override
Inherited from modal_dialog.
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:68
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
SDL_Rect button_pos_
The screen location of the menu_button button that triggered this droplist.
void set_restore(const bool restore)