The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
combobox.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2024 - 2025
3 by Subhraman Sarkar (babaissarkar) <sbmskmm@protonmail.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
21namespace gui2
22{
23namespace implementation
24{
25struct 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
36class combobox : public text_box_base
37{
39
40public:
41 explicit combobox(const implementation::builder_combobox& builder);
42
43 void set_max_input_length(const std::size_t length)
44 {
45 max_input_length_ = length;
46 }
47
48 std::size_t get_max_input_length() const
49 {
50 return max_input_length_;
51 }
52
53 void set_hint_text(const std::string& text)
54 {
57 }
58
59 std::string get_hint_text() const
60 {
61 return hint_text_;
62 }
63
64 void set_hint_image(const std::string& image)
65 {
68 }
69
70 std::string get_hint_image() const
71 {
72 return hint_image_;
73 }
74
75 void set_hint_data(const std::string& text, const std::string& image)
76 {
79
81 }
82
83 void clear()
84 {
85 set_value("");
86 }
87
88 int get_item_count() const
89 {
90 return values_.size();
91 }
92
93 void set_values(const std::vector<::config>& values, unsigned selected = 0);
94 void set_selected(unsigned selected, bool fire_event = true);
95
96 /**
97 * Returned last selected entry's index or
98 * -1 if typed value that does not matches any preset value.
99 */
100 int get_selected() const;
101
102protected:
103 /* **** ***** ***** ***** layout functions ***** ***** ***** **** */
104
105 virtual void place(const point& origin, const point& size) override;
106
107 /* **** ***** ***** ***** Inherited ***** ***** ***** **** */
108
109 virtual void update_canvas() override;
110
111 void goto_end_of_line(const bool select = false) override
112 {
113 goto_end_of_data(select);
114 }
115
116 void goto_start_of_line(const bool select = false) override
117 {
118 goto_start_of_data(select);
119 }
120
121 void delete_char(const bool before_cursor) override;
122
123 void delete_selection() override;
124
125 void handle_mouse_selection(point mouse, const bool start_selection);
126
127private:
128
129 /** The maximum length of the text input. */
130 std::size_t max_input_length_;
131
132 /** Size of the dropdown icon
133 * TODO : Should be dynamically loaded from image
134 */
135 unsigned const ICON_SIZE = 25;
136
137 /**
138 * The x, y offset in the widget where the text starts.
139 *
140 * This value is needed to translate a location in the widget to a location
141 * in the text.
142 */
144
145 /**
146 * The height of the text itself.
147 *
148 * Needed to determine whether a click is on the text.
149 */
150 unsigned text_height_;
151
152 /** Updates text_x_offset_ and text_y_offset_. */
153 void update_offsets();
154
155 /** Is the mouse in dragging mode, this affects selection in mouse move */
157
158 /** Helper text to display (such as "Search") if the combo box is empty. */
159 std::string hint_text_;
160
161 /** Image (such as a magnifying glass) that accompanies the help text. */
162 std::string hint_image_;
163
164 std::vector<::config> values_;
165
166 unsigned selected_;
167
168 /**
169 * Get the `index`-th value from the preset values that area
170 * shown in the dropdown.
171 */
172 std::string get_preset_value(const size_t index) const;
173
174 /**
175 * Inherited from text_box_base.
176 *
177 * Unmodified Handled.
178 * Control Ignored.
179 * Shift Ignored.
180 * Alt Ignored.
181 */
182 void handle_key_up_arrow(SDL_Keymod /*modifier*/, bool& handled) override;
183
184 /**
185 * Inherited from text_box_base.
186 *
187 * Unmodified Handled.
188 * Control Ignored.
189 * Shift Ignored.
190 * Alt Ignored.
191 */
192 void handle_key_down_arrow(SDL_Keymod /*modifier*/, bool& handled) override;
193
194
195 /** Inherited from text_box_base. */
196 void handle_key_clear_line(SDL_Keymod modifier, bool& handled) override;
197
198 /** Update the mouse cursor based on whether it is over button area or text area */
199 void update_mouse_cursor();
200
201public:
202 /** Static type getter that does not rely on the widget being constructed. */
203 static const std::string& type();
204
205private:
206 /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
207 virtual const std::string& get_control_type() const override;
208
209 /* **** ***** ***** signal handlers ***** ****** **** */
210
211 void signal_handler_mouse_enter(const event::ui_event /*event*/, bool& /*handled*/);
213 bool& handled,
214 const point& coordinate);
215
217 bool& handled);
218
220 bool& handled);
221
223 bool& handled);
224};
225
226// }---------- DEFINITION ---------{
227
240
241// }---------- BUILDER -----------{
242
243namespace implementation
244{
245
247{
248public:
249 explicit builder_combobox(const config& cfg);
250
252
253 virtual std::unique_ptr<widget> build() const override;
254
255 std::size_t max_input_length;
256
258 std::string hint_image;
259
260private:
261 std::vector<::config> options_;
262};
263
264} // namespace implementation
265
266// }------------ END --------------
267
268} // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
Class for a combobox.
Definition combobox.hpp:37
void set_hint_data(const std::string &text, const std::string &image)
Definition combobox.hpp:75
void signal_handler_mouse_enter(const event::ui_event, bool &)
Definition combobox.cpp:329
void signal_handler_left_button_up(const event::ui_event event, bool &handled)
Definition combobox.cpp:388
std::size_t max_input_length_
The maximum length of the text input.
Definition combobox.hpp:130
std::string get_preset_value(const size_t index) const
Get the index-th value from the preset values that area shown in the dropdown.
Definition combobox.cpp:272
void update_mouse_cursor()
Update the mouse cursor based on whether it is over button area or text area.
Definition combobox.cpp:317
unsigned selected_
Definition combobox.hpp:166
void delete_selection() override
Deletes the current selection.
Definition combobox.cpp:178
void goto_start_of_line(const bool select=false) override
Moves the cursor to the beginning of the line.
Definition combobox.hpp:116
void update_offsets()
Updates text_x_offset_ and text_y_offset_.
Definition combobox.cpp:221
void goto_end_of_line(const bool select=false) override
Moves the cursor to the end of the line.
Definition combobox.hpp:111
std::string get_hint_text() const
Definition combobox.hpp:59
bool dragging_
Is the mouse in dragging mode, this affects selection in mouse move.
Definition combobox.hpp:156
void set_max_input_length(const std::size_t length)
Definition combobox.hpp:43
void signal_handler_mouse_motion(const event::ui_event event, bool &handled, const point &coordinate)
Definition combobox.cpp:335
void handle_mouse_selection(point mouse, const bool start_selection)
Definition combobox.cpp:198
std::string get_hint_image() const
Definition combobox.hpp:70
virtual void update_canvas() override
Updates the canvas(ses).
Definition combobox.cpp:88
unsigned const ICON_SIZE
Size of the dropdown icon TODO : Should be dynamically loaded from image.
Definition combobox.hpp:135
void handle_key_clear_line(SDL_Keymod modifier, bool &handled) override
Inherited from text_box_base.
Definition combobox.cpp:248
std::size_t get_max_input_length() const
Definition combobox.hpp:48
std::string hint_image_
Image (such as a magnifying glass) that accompanies the help text.
Definition combobox.hpp:162
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
void handle_key_down_arrow(SDL_Keymod, bool &handled) override
Inherited from text_box_base.
Definition combobox.cpp:263
void set_hint_image(const std::string &image)
Definition combobox.hpp:64
void set_values(const std::vector<::config > &values, unsigned selected=0)
Definition combobox.cpp:278
int get_selected() const
Returned last selected entry's index or -1 if typed value that does not matches any preset value.
Definition combobox.cpp:293
void set_hint_text(const std::string &text)
Definition combobox.hpp:53
void signal_handler_left_button_down(const event::ui_event event, bool &handled)
Definition combobox.cpp:350
void delete_char(const bool before_cursor) override
Deletes the character.
Definition combobox.cpp:167
std::vector<::config > values_
Definition combobox.hpp:164
int get_item_count() const
Definition combobox.hpp:88
point text_offset_
The x, y offset in the widget where the text starts.
Definition combobox.hpp:143
void signal_handler_left_button_double_click(const event::ui_event event, bool &handled)
Definition combobox.cpp:398
void set_selected(unsigned selected, bool fire_event=true)
Definition combobox.cpp:300
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:159
unsigned text_height_
The height of the text itself.
Definition combobox.hpp:150
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition combobox.cpp:75
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
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.
Template class can hold a value or a formula to calculate the value.
const config * cfg
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.
typed_formula< unsigned > text_y_offset
Definition combobox.hpp:237
typed_formula< unsigned > text_x_offset
Definition combobox.hpp:236
virtual std::unique_ptr< widget > build() const override
Definition combobox.cpp:446
virtual std::unique_ptr< widget > build() const=0
Holds a 2D point.
Definition point.hpp:25