The Battle for Wesnoth  1.19.0-dev
label.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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 
21 
22 namespace gui2
23 {
24 namespace implementation
25 {
26  struct builder_label;
27 }
28 
29 // ------------ WIDGET -----------{
30 
31 class label : public styled_widget
32 {
34 
35 public:
36  explicit label(const implementation::builder_label& builder);
37 
38  /** See @ref widget::can_wrap. */
39  virtual bool can_wrap() const override
40  {
41  return can_wrap_ || characters_per_line_ != 0;
42  }
43 
44  /** See @ref styled_widget::get_characters_per_line. */
45  virtual unsigned get_characters_per_line() const override
46  {
47  return characters_per_line_;
48  }
49 
50  /** See @ref styled_widget::get_link_aware. */
51  virtual bool get_link_aware() const override
52  {
53  return link_aware_;
54  }
55 
56  /** See @ref styled_widget::get_link_aware. */
57  virtual color_t get_link_color() const override
58  {
59  return link_color_;
60  }
61 
62  /** See @ref styled_widget::set_active. */
63  virtual void set_active(const bool active) override;
64 
65  /** See @ref styled_widget::get_active. */
66  virtual bool get_active() const override
67  {
68  return state_ != DISABLED;
69  }
70 
71  /** See @ref styled_widget::get_state. */
72  virtual unsigned get_state() const override
73  {
74  return state_;
75  }
76 
77  /** See @ref widget::disable_click_dismiss. */
78  bool disable_click_dismiss() const override
79  {
80  return false;
81  }
82 
83  /** See @ref widget::can_mouse_focus. */
84  virtual bool can_mouse_focus() const override
85  {
86  return !tooltip().empty() || get_link_aware();
87  }
88 
89  /** See @ref styled_widget::update_canvas. */
90  virtual void update_canvas() override;
91 
92  /***** ***** ***** setters / getters for members ***** ****** *****/
93 
94  void set_can_wrap(const bool wrap)
95  {
96  can_wrap_ = wrap;
97  }
98 
99  void set_characters_per_line(const unsigned characters_per_line)
100  {
101  characters_per_line_ = characters_per_line;
102  }
103 
104  void set_link_aware(bool l);
105 
106  void set_link_color(const color_t& color);
107 
108  void set_can_shrink(bool can_shrink)
109  {
110  can_shrink_ = can_shrink;
111  }
112 
113  void set_text_alpha(unsigned short alpha);
114 
115 private:
116  /**
117  * Possible states of the widget.
118  *
119  * Note the order of the states must be the same as defined in settings.hpp.
120  */
121  enum state_t {
124  };
125 
126  void set_state(const state_t state);
127 
128  /**
129  * Current state of the widget.
130  *
131  * The state of the widget determines what to render and how the widget
132  * reacts to certain 'events'.
133  */
135 
136  /** Holds the label can wrap or not. */
137  bool can_wrap_;
138 
139  /**
140  * The maximum number of characters per line.
141  *
142  * The maximum is not an exact maximum, it uses the average character width.
143  */
145 
146  /**
147  * Whether the label is link aware, rendering links with special formatting
148  * and handling click events.
149  */
151 
152  /**
153  * What color links will be rendered in.
154  */
156 
158 
159  unsigned short text_alpha_;
160 
161  /** Inherited from styled_widget. */
162  virtual bool text_can_shrink() override
163  {
164  return can_shrink_;
165  }
166 
167 public:
168  /** Static type getter that does not rely on the widget being constructed. */
169  static const std::string& type();
170 
171 private:
172  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
173  virtual const std::string& get_control_type() const override;
174 
175  /***** ***** ***** signal handlers ***** ****** *****/
176 
177  /**
178  * Left click signal handler: checks if we clicked on a hyperlink
179  */
180  void signal_handler_left_button_click(bool& handled);
181 
182  /**
183  * Right click signal handler: checks if we clicked on a hyperlink, copied to clipboard
184  */
185  void signal_handler_right_button_click(bool& handled);
186 
187  /**
188  * Mouse motion signal handler: checks if the cursor is on a hyperlink
189  */
190  void signal_handler_mouse_motion(bool& handled, const point& coordinate);
191 
192  /**
193  * Mouse leave signal handler: checks if the cursor left a hyperlink
194  */
195  void signal_handler_mouse_leave(bool& handled);
196 
197  /**
198  * Implementation detail for (re)setting the hyperlink cursor.
199  */
200  void update_mouse_cursor(bool enable);
201 };
202 
203 // }---------- DEFINITION ---------{
204 
206 {
207 
208  explicit label_definition(const config& cfg);
209 
211  {
212  explicit resolution(const config& cfg);
213 
215  };
216 };
217 
218 // }---------- BUILDER -----------{
219 
220 namespace implementation
221 {
222 
224 {
225  builder_label(const config& cfg);
226 
228 
229  virtual std::unique_ptr<widget> build() const override;
230 
231  bool wrap;
232 
234 
235  PangoAlignment text_alignment;
236 
239 };
240 
241 } // namespace implementation
242 
243 // }------------ END --------------
244 
245 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
void set_can_shrink(bool can_shrink)
Definition: label.hpp:108
unsigned short text_alpha_
Definition: label.hpp:159
bool can_shrink_
Definition: label.hpp:157
virtual bool get_link_aware() const override
See styled_widget::get_link_aware.
Definition: label.hpp:51
void signal_handler_left_button_click(bool &handled)
Left click signal handler: checks if we clicked on a hyperlink.
Definition: label.cpp:114
virtual bool text_can_shrink() override
Inherited from styled_widget.
Definition: label.hpp:162
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
void set_link_color(const color_t &color)
Definition: label.cpp:97
void signal_handler_mouse_leave(bool &handled)
Mouse leave signal handler: checks if the cursor left a hyperlink.
Definition: label.cpp:195
color_t link_color_
What color links will be rendered in.
Definition: label.hpp:155
void signal_handler_right_button_click(bool &handled)
Right click signal handler: checks if we clicked on a hyperlink, copied to clipboard.
Definition: label.cpp:149
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: label.hpp:78
bool link_aware_
Whether the label is link aware, rendering links with special formatting and handling click events.
Definition: label.hpp:150
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: label.hpp:72
void set_can_wrap(const bool wrap)
Definition: label.hpp:94
virtual void update_canvas() override
See styled_widget::update_canvas.
Definition: label.cpp:62
void set_state(const state_t state)
Definition: label.cpp:106
void set_characters_per_line(const unsigned characters_per_line)
Definition: label.hpp:99
state_t
Possible states of the widget.
Definition: label.hpp:121
virtual bool get_active() const override
See styled_widget::get_active.
Definition: label.hpp:66
state_t state_
Current state of the widget.
Definition: label.hpp:134
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: label.cpp:81
virtual unsigned get_characters_per_line() const override
See styled_widget::get_characters_per_line.
Definition: label.hpp:45
unsigned characters_per_line_
The maximum number of characters per line.
Definition: label.hpp:144
void update_mouse_cursor(bool enable)
Implementation detail for (re)setting the hyperlink cursor.
Definition: label.cpp:209
virtual color_t get_link_color() const override
See styled_widget::get_link_aware.
Definition: label.hpp:57
virtual bool can_mouse_focus() const override
See widget::can_mouse_focus.
Definition: label.hpp:84
label(const implementation::builder_label &builder)
Definition: label.cpp:42
void set_link_aware(bool l)
Definition: label.cpp:88
void set_text_alpha(unsigned short alpha)
Definition: label.cpp:72
virtual bool can_wrap() const override
See widget::can_wrap.
Definition: label.hpp:39
void signal_handler_mouse_motion(bool &handled, const point &coordinate)
Mouse motion signal handler: checks if the cursor is on a hyperlink.
Definition: label.cpp:177
bool can_wrap_
Holds the label can wrap or not.
Definition: label.hpp:137
const t_string & tooltip() const
bool empty() const
Definition: tstring.hpp:186
Generic file dialog.
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.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
virtual std::unique_ptr< widget > build() const override
Definition: label.cpp:256
builder_label(const config &cfg)
Definition: label.cpp:246
virtual std::unique_ptr< widget > build() const=0
resolution(const config &cfg)
Definition: label.cpp:232
label_definition(const config &cfg)
Definition: label.cpp:224
Holds a 2D point.
Definition: point.hpp:25