The Battle for Wesnoth  1.19.4+dev
combobox.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2024
3  by Subhraman Sarkar (babaissarkar) <suvrax@gmail.com>
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 
20 
21 namespace gui2
22 {
23 namespace implementation
24 {
25 struct builder_combobox;
26 }
27 
28 // ------------ WIDGET -----------{
29 
30 /**
31  * Class for a combobox.
32  * A widget that allows the user to input text or to select predefined options from a list
33  * accessed by clicking the dropdown arrow.
34  */
35 
36 class combobox : public text_box_base
37 {
39 
40 public:
41  explicit combobox(const implementation::builder_styled_widget& builder);
42 
43  void set_max_input_length(const std::size_t length)
44  {
45  max_input_length_ = length;
46  }
47 
48  void set_hint_data(const std::string& text, const std::string& image)
49  {
50  hint_text_ = text;
52 
53  update_canvas();
54  }
55 
56  void clear()
57  {
58  set_value("");
59  }
60 
61  void set_values(const std::vector<::config>& values, unsigned selected = 0);
62  void set_selected(unsigned selected, bool fire_event = true);
63  unsigned get_selected() const { return selected_; }
64 
65 protected:
66  /* **** ***** ***** ***** layout functions ***** ***** ***** **** */
67 
68  virtual void place(const point& origin, const point& size) override;
69 
70  /* **** ***** ***** ***** Inherited ***** ***** ***** **** */
71 
72  virtual void update_canvas() override;
73 
74  void goto_end_of_line(const bool select = false) override
75  {
76  goto_end_of_data(select);
77  }
78 
79  void goto_start_of_line(const bool select = false) override
80  {
81  goto_start_of_data(select);
82  }
83 
84  void delete_char(const bool before_cursor) override;
85 
86  void delete_selection() override;
87 
88  void handle_mouse_selection(point mouse, const bool start_selection);
89 
90 private:
91 
92  /** The maximum length of the text input. */
93  std::size_t max_input_length_;
94 
95  /** Size of the dropdown icon
96  * TODO : Should be dynamically loaded from image
97  */
98  unsigned const ICON_SIZE = 25;
99 
100  /**
101  * The x offset in the widget where the text starts.
102  *
103  * This value is needed to translate a location in the widget to a location
104  * in the text.
105  */
106  unsigned text_x_offset_;
107 
108  /**
109  * The y offset in the widget where the text starts.
110  *
111  * Needed to determine whether a click is on the text.
112  */
113  unsigned text_y_offset_;
114 
115  /**
116  * The height of the text itself.
117  *
118  * Needed to determine whether a click is on the text.
119  */
120  unsigned text_height_;
121 
122  /** Updates text_x_offset_ and text_y_offset_. */
123  void update_offsets();
124 
125  /** Is the mouse in dragging mode, this affects selection in mouse move */
126  bool dragging_;
127 
128  /** Helper text to display (such as "Search") if the combo box is empty. */
129  std::string hint_text_;
130 
131  /** Image (such as a magnifying glass) that accompanies the help text. */
132  std::string hint_image_;
133 
134  std::vector<::config> values_;
135 
136  unsigned selected_;
137 
138  /**
139  * Inherited from text_box_base.
140  *
141  * Unmodified Handled.
142  * Control Ignored.
143  * Shift Ignored.
144  * Alt Ignored.
145  */
146  void handle_key_up_arrow(SDL_Keymod /*modifier*/, bool& handled) override;
147 
148  /**
149  * Inherited from text_box_base.
150  *
151  * Unmodified Handled.
152  * Control Ignored.
153  * Shift Ignored.
154  * Alt Ignored.
155  */
156  void handle_key_down_arrow(SDL_Keymod /*modifier*/, bool& handled) override;
157 
158 
159  /** Inherited from text_box_base. */
160  void handle_key_clear_line(SDL_Keymod modifier, bool& handled) override;
161 
162  /** Update the mouse cursor based on whether it is over button area or text area */
163  void update_mouse_cursor();
164 
165 public:
166  /** Static type getter that does not rely on the widget being constructed. */
167  static const std::string& type();
168 
169 private:
170  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
171  virtual const std::string& get_control_type() const override;
172 
173  /* **** ***** ***** signal handlers ***** ****** **** */
174 
175  void signal_handler_mouse_enter(const event::ui_event /*event*/, bool& /*handled*/);
177  bool& handled,
178  const point& coordinate);
179 
181  bool& handled);
182 
184  bool& handled);
185 
187  bool& handled);
188 };
189 
190 // }---------- DEFINITION ---------{
191 
193 {
194  explicit combobox_definition(const config& cfg);
195 
197  {
198  explicit resolution(const config& cfg);
199 
202  };
203 };
204 
205 // }---------- BUILDER -----------{
206 
207 namespace implementation
208 {
209 
211 {
212 public:
213  explicit builder_combobox(const config& cfg);
214 
216 
217  virtual std::unique_ptr<widget> build() const override;
218 
219  std::size_t max_input_length;
220 
222  std::string hint_image;
223 
224 private:
225  std::vector<::config> options_;
226 };
227 
228 } // namespace implementation
229 
230 // }------------ END --------------
231 
232 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:163
Class for a combobox.
Definition: combobox.hpp:37
void set_hint_data(const std::string &text, const std::string &image)
Definition: combobox.hpp:48
void signal_handler_mouse_enter(const event::ui_event, bool &)
Definition: combobox.cpp:316
void signal_handler_left_button_up(const event::ui_event event, bool &handled)
Definition: combobox.cpp:375
std::size_t max_input_length_
The maximum length of the text input.
Definition: combobox.hpp:93
void update_mouse_cursor()
Update the mouse cursor based on whether it is over button area or text area.
Definition: combobox.cpp:304
unsigned selected_
Definition: combobox.hpp:136
void delete_selection() override
Deletes the current selection.
Definition: combobox.cpp:179
void goto_start_of_line(const bool select=false) override
Moves the cursor to the beginning of the line.
Definition: combobox.hpp:79
void update_offsets()
Updates text_x_offset_ and text_y_offset_.
Definition: combobox.cpp:223
void goto_end_of_line(const bool select=false) override
Moves the cursor to the end of the line.
Definition: combobox.hpp:74
bool dragging_
Is the mouse in dragging mode, this affects selection in mouse move.
Definition: combobox.hpp:126
void set_max_input_length(const std::size_t length)
Definition: combobox.hpp:43
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
void signal_handler_mouse_motion(const event::ui_event event, bool &handled, const point &coordinate)
Definition: combobox.cpp:322
void handle_mouse_selection(point mouse, const bool start_selection)
Definition: combobox.cpp:199
void clear()
Definition: combobox.hpp:56
virtual void update_canvas() override
Updates the canvas(ses).
Definition: combobox.cpp:89
unsigned const ICON_SIZE
Size of the dropdown icon TODO : Should be dynamically loaded from image.
Definition: combobox.hpp:98
void handle_key_clear_line(SDL_Keymod modifier, bool &handled) override
Inherited from text_box_base.
Definition: combobox.cpp:248
std::string hint_image_
Image (such as a magnifying glass) that accompanies the help text.
Definition: combobox.hpp:132
void handle_key_down_arrow(SDL_Keymod, bool &handled) override
Inherited from text_box_base.
Definition: combobox.cpp:263
void set_values(const std::vector<::config > &values, unsigned selected=0)
Definition: combobox.cpp:272
unsigned text_y_offset_
The y offset in the widget where the text starts.
Definition: combobox.hpp:113
void signal_handler_left_button_down(const event::ui_event event, bool &handled)
Definition: combobox.cpp:337
void delete_char(const bool before_cursor) override
Deletes the character.
Definition: combobox.cpp:168
std::vector<::config > values_
Definition: combobox.hpp:134
void signal_handler_left_button_double_click(const event::ui_event event, bool &handled)
Definition: combobox.cpp:385
void set_selected(unsigned selected, bool fire_event=true)
Definition: combobox.cpp:287
unsigned text_x_offset_
The x offset in the widget where the text starts.
Definition: combobox.hpp:106
void handle_key_up_arrow(SDL_Keymod, bool &handled) override
Inherited from text_box_base.
Definition: combobox.cpp:254
std::string hint_text_
Helper text to display (such as "Search") if the combo box is empty.
Definition: combobox.hpp:129
unsigned text_height_
The height of the text itself.
Definition: combobox.hpp:120
unsigned get_selected() const
Definition: combobox.hpp:63
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: combobox.cpp:76
combobox(const implementation::builder_styled_widget &builder)
Definition: combobox.cpp:42
Abstract base class for text items.
const std::string & text() const
virtual void set_value(const std::string &text)
The set_value is virtual for the password_box class.
virtual void goto_end_of_data(const bool select=false)
Moves the cursor to the end of all text.
virtual void goto_start_of_data(const bool select=false)
Moves the cursor to the beginning of the data.
std::string selected
bool fire_event(const ui_event event, const std::vector< std::pair< widget *, ui_event >> &event_chain, widget *dispatcher, widget *w, F &&... params)
Helper function for fire_event.
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
Generic file dialog.
Functions to load and save images from/to disk.
Contains the implementation details for lexical_cast and shouldn't be used directly.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
typed_formula< unsigned > text_y_offset
Definition: combobox.hpp:201
typed_formula< unsigned > text_x_offset
Definition: combobox.hpp:200
combobox_definition(const config &cfg)
Definition: combobox.cpp:396
std::vector<::config > options_
Definition: combobox.hpp:225
virtual std::unique_ptr< widget > build() const override
Definition: combobox.cpp:433
virtual std::unique_ptr< widget > build() const=0
Holds a 2D point.
Definition: point.hpp:25