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 /**
32  * @ingroup GUIWidgetWML
33  *
34  * A label displays text that can be wrapped but no scrollbars are provided.
35  *
36  * A label has two states because labels are often used as visual indication of the state of the widget it labels.
37  *
38  * The following states exist:
39  * * state_enabled - the label is enabled.
40  * * state_disabled - the label is disabled.
41  *
42  * Key |Type |Default |Description
43  * -------------------|------------------------------------|--------|-------------
44  * link_color | @ref guivartype_string "string" |\#ffff00|The color to render links with. This string will be used verbatim in pango markup for each link.
45  *
46  * The label specific variables:
47  * Key |Type |Default|Description
48  * -------------------|------------------------------------|-------|-------------
49  * wrap | @ref guivartype_bool "bool" |false |Is wrapping enabled for the label.
50  * characters_per_line| @ref guivartype_unsigned "unsigned"|0 |Sets the maximum number of characters per line. The amount is an approximate since the width of a character differs. E.g. iii is smaller than MMM. When the value is non-zero it also implies can_wrap is true. When having long strings wrapping them can increase readability, often 66 characters per line is considered the optimum for a one column text.
51  * text_alignment | @ref guivartype_h_align "h_align" |left |The way the text is aligned inside the canvas.
52  * can_shrink | @ref guivartype_bool "bool" |false |Whether the label can shrink past its optimal size.
53  * link_aware | @ref guivartype_bool "bool" |false |Whether the label is link aware. This means it is rendered with links highlighted, and responds to click events on those links.
54  */
55 class label : public styled_widget
56 {
58 
59 public:
60  explicit label(const implementation::builder_label& builder);
61 
62  /** See @ref widget::can_wrap. */
63  virtual bool can_wrap() const override
64  {
65  return can_wrap_ || characters_per_line_ != 0;
66  }
67 
68  /** See @ref styled_widget::get_characters_per_line. */
69  virtual unsigned get_characters_per_line() const override
70  {
71  return characters_per_line_;
72  }
73 
74  /** See @ref styled_widget::get_link_aware. */
75  virtual bool get_link_aware() const override
76  {
77  return link_aware_;
78  }
79 
80  /** See @ref styled_widget::get_link_aware. */
81  virtual color_t get_link_color() const override
82  {
83  return link_color_;
84  }
85 
86  /** See @ref styled_widget::set_active. */
87  virtual void set_active(const bool active) override;
88 
89  /** See @ref styled_widget::get_active. */
90  virtual bool get_active() const override
91  {
92  return state_ != DISABLED;
93  }
94 
95  /** See @ref styled_widget::get_state. */
96  virtual unsigned get_state() const override
97  {
98  return state_;
99  }
100 
101  /** See @ref widget::disable_click_dismiss. */
102  bool disable_click_dismiss() const override
103  {
104  return false;
105  }
106 
107  /** See @ref widget::can_mouse_focus. */
108  virtual bool can_mouse_focus() const override
109  {
110  return !tooltip().empty() || get_link_aware();
111  }
112 
113  /** See @ref styled_widget::update_canvas. */
114  virtual void update_canvas() override;
115 
116  /***** ***** ***** setters / getters for members ***** ****** *****/
117 
118  void set_can_wrap(const bool wrap)
119  {
120  can_wrap_ = wrap;
121  }
122 
123  void set_characters_per_line(const unsigned characters_per_line)
124  {
125  characters_per_line_ = characters_per_line;
126  }
127 
128  void set_link_aware(bool l);
129 
130  void set_link_color(const color_t& color);
131 
132  void set_can_shrink(bool can_shrink)
133  {
134  can_shrink_ = can_shrink;
135  }
136 
137  void set_text_alpha(unsigned short alpha);
138 
139 private:
140  /**
141  * Possible states of the widget.
142  *
143  * Note the order of the states must be the same as defined in settings.hpp.
144  */
145  enum state_t {
148  };
149 
150  void set_state(const state_t state);
151 
152  /**
153  * Current state of the widget.
154  *
155  * The state of the widget determines what to render and how the widget
156  * reacts to certain 'events'.
157  */
159 
160  /** Holds the label can wrap or not. */
161  bool can_wrap_;
162 
163  /**
164  * The maximum number of characters per line.
165  *
166  * The maximum is not an exact maximum, it uses the average character width.
167  */
169 
170  /**
171  * Whether the label is link aware, rendering links with special formatting
172  * and handling click events.
173  */
175 
176  /**
177  * What color links will be rendered in.
178  */
180 
182 
183  unsigned short text_alpha_;
184 
185  /** Inherited from styled_widget. */
186  virtual bool text_can_shrink() override
187  {
188  return can_shrink_;
189  }
190 
191 public:
192  /** Static type getter that does not rely on the widget being constructed. */
193  static const std::string& type();
194 
195 private:
196  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
197  virtual const std::string& get_control_type() const override;
198 
199  /***** ***** ***** signal handlers ***** ****** *****/
200 
201  /**
202  * Left click signal handler: checks if we clicked on a hyperlink
203  */
204  void signal_handler_left_button_click(bool& handled);
205 
206  /**
207  * Right click signal handler: checks if we clicked on a hyperlink, copied to clipboard
208  */
209  void signal_handler_right_button_click(bool& handled);
210 
211  /**
212  * Mouse motion signal handler: checks if the cursor is on a hyperlink
213  */
214  void signal_handler_mouse_motion(bool& handled, const point& coordinate);
215 
216  /**
217  * Mouse leave signal handler: checks if the cursor left a hyperlink
218  */
219  void signal_handler_mouse_leave(bool& handled);
220 
221  /**
222  * Implementation detail for (re)setting the hyperlink cursor.
223  */
224  void update_mouse_cursor(bool enable);
225 };
226 
227 // }---------- DEFINITION ---------{
228 
230 {
231 
232  explicit label_definition(const config& cfg);
233 
235  {
236  explicit resolution(const config& cfg);
237 
239  };
240 };
241 
242 // }---------- BUILDER -----------{
243 
244 namespace implementation
245 {
246 
248 {
249  builder_label(const config& cfg);
250 
252 
253  virtual std::unique_ptr<widget> build() const override;
254 
255  bool wrap;
256 
258 
259  PangoAlignment text_alignment;
260 
263 };
264 
265 } // namespace implementation
266 
267 // }------------ END --------------
268 
269 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
A label displays text that can be wrapped but no scrollbars are provided.
Definition: label.hpp:56
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:132
unsigned short text_alpha_
Definition: label.hpp:183
bool can_shrink_
Definition: label.hpp:181
virtual bool get_link_aware() const override
See styled_widget::get_link_aware.
Definition: label.hpp:75
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:186
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:179
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:102
bool link_aware_
Whether the label is link aware, rendering links with special formatting and handling click events.
Definition: label.hpp:174
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: label.hpp:96
void set_can_wrap(const bool wrap)
Definition: label.hpp:118
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:123
state_t
Possible states of the widget.
Definition: label.hpp:145
virtual bool get_active() const override
See styled_widget::get_active.
Definition: label.hpp:90
state_t state_
Current state of the widget.
Definition: label.hpp:158
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:69
unsigned characters_per_line_
The maximum number of characters per line.
Definition: label.hpp:168
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:81
virtual bool can_mouse_focus() const override
See widget::can_mouse_focus.
Definition: label.hpp:108
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:63
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:161
Base class for all visible items.
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
Base class of a resolution, contains the common keys for a resolution.
Holds a 2D point.
Definition: point.hpp:25