-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
scroll_label.cpp
303 lines (248 loc) · 8.36 KB
/
scroll_label.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
Copyright (C) 2008 - 2017 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/widgets/scroll_label.hpp"
#include "gui/widgets/label.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/core/log.hpp"
#include "gui/core/window_builder/helper.hpp"
#include "gui/core/register_widget.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/scrollbar.hpp"
#include "gui/widgets/spacer.hpp"
#include "gui/widgets/window.hpp"
#include "gettext.hpp"
#include "utils/functional.hpp"
#define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
#define LOG_HEADER LOG_SCOPE_HEADER + ':'
namespace gui2
{
// ------------ WIDGET -----------{
REGISTER_WIDGET(scroll_label)
scroll_label::scroll_label(bool wrap, const PangoAlignment text_alignment)
: scrollbar_container()
, state_(ENABLED)
, wrap_on_(wrap)
, text_alignment_(text_alignment)
{
connect_signal<event::LEFT_BUTTON_DOWN>(
std::bind(&scroll_label::signal_handler_left_button_down, this, _2),
event::dispatcher::back_pre_child);
}
label* scroll_label::get_internal_label()
{
if(content_grid()) {
return dynamic_cast<label*>(content_grid()->find("_label", false));
}
return nullptr;
}
void scroll_label::set_label(const t_string& lbl)
{
// Inherit.
styled_widget::set_label(lbl);
if(label* widget = get_internal_label()) {
widget->set_label(lbl);
bool resize_needed = !content_resize_request();
if(resize_needed && get_size() != point()) {
place(get_origin(), get_size());
}
}
}
void scroll_label::set_text_alignment(const PangoAlignment text_alignment)
{
// Inherit.
styled_widget::set_text_alignment(text_alignment);
text_alignment_ = text_alignment;
if(label* widget = get_internal_label()) {
widget->set_text_alignment(text_alignment_);
}
}
void scroll_label::set_use_markup(bool use_markup)
{
// Inherit.
styled_widget::set_use_markup(use_markup);
if(label* widget = get_internal_label()) {
widget->set_use_markup(use_markup);
}
}
void scroll_label::set_text_alpha(unsigned short alpha)
{
if(label* widget = get_internal_label()) {
widget->set_text_alpha(alpha);
}
}
void scroll_label::set_link_aware(bool l)
{
if(label* widget = get_internal_label()) {
widget->set_link_aware(l);
}
}
void scroll_label::set_self_active(const bool active)
{
state_ = active ? ENABLED : DISABLED;
}
bool scroll_label::get_active() const
{
return state_ != DISABLED;
}
unsigned scroll_label::get_state() const
{
return state_;
}
void scroll_label::finalize_subclass()
{
label* lbl = get_internal_label();
assert(lbl);
lbl->set_label(get_label());
lbl->set_can_wrap(wrap_on_);
lbl->set_text_alignment(text_alignment_);
lbl->set_use_markup(get_use_markup());
}
void scroll_label::set_can_wrap(bool can_wrap)
{
label* lbl = get_internal_label();
assert(lbl);
wrap_on_ = can_wrap;
lbl->set_can_wrap(wrap_on_);
}
bool scroll_label::can_wrap() const
{
return wrap_on_;
}
const std::string& scroll_label::get_control_type() const
{
static const std::string type = "scroll_label";
return type;
}
void scroll_label::signal_handler_left_button_down(const event::ui_event event)
{
DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
get_window()->keyboard_capture(this);
}
// }---------- DEFINITION ---------{
scroll_label_definition::scroll_label_definition(const config& cfg)
: styled_widget_definition(cfg)
{
DBG_GUI_P << "Parsing scroll label " << id << '\n';
load_resolutions<resolution>(cfg);
}
/*WIKI
* @page = GUIWidgetDefinitionWML
* @order = 1_scroll_label
*
* == Scroll label ==
*
* @macro = scroll_label_description
*
* @begin{parent}{name="gui/"}
* This widget is slower as a normal label widget so only use this widget
* when the scrollbar is required (or expected to become required).
* @begin{tag}{name="scroll_label_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
* @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
* @begin{table}{config}
* grid & grid & & A grid containing the widgets for main
* widget. $
* @end{table}
* @allow{link}{name="gui/window/resolution/grid"}
* TODO we need one definition for a vertical scrollbar since this is the second
* time we use it.
*
* @begin{table}{dialog_widgets}
* _content_grid & & grid & m & A grid which should only contain one
* label widget. $
* _scrollbar_grid & & grid & m & A grid for the scrollbar
* (Merge with listbox info.) $
* @end{table}
* @begin{tag}{name="content_grid"}{min=0}{max=1}{super="gui/window/resolution/grid"}
* @end{tag}{name="content_grid"}
* @begin{tag}{name="scrollbar_grid"}{min=0}{max=1}{super="gui/window/resolution/grid"}
* @end{tag}{name="scrollbar_grid"}
* The following states exist:
* * state_enabled, the scroll label is enabled.
* * state_disabled, the scroll label is disabled.
* @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
* @end{tag}{name="state_enabled"}
* @begin{tag}{name="state_disabled"}{min=0}{max=1}{super="generic/state"}
* @end{tag}{name="state_disabled"}
* @end{tag}{name="resolution"}
* @end{tag}{name="scroll_label_definition"}
* @end{parent}{name="gui/"}
*/
scroll_label_definition::resolution::resolution(const config& cfg)
: resolution_definition(cfg), grid(nullptr)
{
// Note the order should be the same as the enum state_t is scroll_label.hpp.
state.emplace_back(cfg.child("state_enabled"));
state.emplace_back(cfg.child("state_disabled"));
const config& child = cfg.child("grid");
VALIDATE(child, _("No grid defined."));
grid = std::make_shared<builder_grid>(child);
}
// }---------- BUILDER -----------{
/*WIKI_MACRO
* @begin{macro}{scroll_label_description}
*
* A scroll label is a label that wraps its text and also has a
* vertical scrollbar. This way a text can't be too long to be shown
* for this widget.
* @end{macro}
*/
/*WIKI
* @page = GUIWidgetInstanceWML
* @order = 2_scroll_label
* @begin{parent}{name="gui/window/resolution/grid/row/column/"}
* @begin{tag}{name="scroll_label"}{min="0"}{max="-1"}{super="generic/widget_instance"}
* == Scroll label ==
*
* @macro = scroll_label_description
*
* List with the scroll label specific variables:
* @begin{table}{config}
* vertical_scrollbar_mode & scrollbar_mode & initial_auto &
* Determines whether or not to show the
* scrollbar. $
* horizontal_scrollbar_mode & scrollbar_mode & initial_auto &
* Determines whether or not to show the
* scrollbar. $
* wrap & boolean & true & Determines whether the text of the
* label is allowed to wrap. $
* @end{table}
* @end{tag}{name="scroll_label"}
* @end{parent}{name="gui/window/resolution/grid/row/column/"}
*/
namespace implementation
{
builder_scroll_label::builder_scroll_label(const config& cfg)
: implementation::builder_styled_widget(cfg)
, vertical_scrollbar_mode(get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
, horizontal_scrollbar_mode(get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
, wrap_on(cfg["wrap"].to_bool(true))
, text_alignment(decode_text_alignment(cfg["text_alignment"]))
{
}
widget* builder_scroll_label::build() const
{
scroll_label* widget = new scroll_label(wrap_on, text_alignment);
init_control(widget);
widget->set_vertical_scrollbar_mode(vertical_scrollbar_mode);
widget->set_horizontal_scrollbar_mode(horizontal_scrollbar_mode);
const auto conf = widget->cast_config_to<scroll_label_definition>();
assert(conf);
widget->init_grid(conf->grid);
widget->finalize_setup();
DBG_GUI_G << "Window builder: placed scroll label '" << id
<< "' with definition '" << definition << "'.\n";
return widget;
}
} // namespace implementation
// }------------ END --------------
} // namespace gui2