The Battle for Wesnoth  1.17.17+dev
scroll_label.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2023
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 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
20 #include "gui/widgets/label.hpp"
22 #include "gui/core/log.hpp"
25 #include "gui/widgets/settings.hpp"
27 #include "gui/widgets/spacer.hpp"
28 #include "gui/widgets/window.hpp"
29 #include "gettext.hpp"
30 #include "wml_exception.hpp"
31 
32 #include <functional>
33 
34 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
35 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
36 
37 namespace gui2
38 {
39 
40 // ------------ WIDGET -----------{
41 
42 REGISTER_WIDGET(scroll_label)
43 
44 scroll_label::scroll_label(const implementation::builder_scroll_label& builder)
45  : scrollbar_container(builder, type())
46  , state_(ENABLED)
47  , wrap_on_(builder.wrap_on)
48  , text_alignment_(builder.text_alignment)
49  , link_aware_(builder.link_aware)
50 {
51  connect_signal<event::LEFT_BUTTON_DOWN>(
52  std::bind(&scroll_label::signal_handler_left_button_down, this, std::placeholders::_2),
54 }
55 
57 {
58  if(content_grid()) {
59  return dynamic_cast<label*>(content_grid()->find("_label", false));
60  }
61 
62  return nullptr;
63 }
64 
66 {
67  // Inherit.
69 
71  widget->set_label(lbl);
72 
73  bool resize_needed = !content_resize_request();
74  if(resize_needed && get_size() != point()) {
76  }
77  }
78 }
79 
80 void scroll_label::set_text_alignment(const PangoAlignment text_alignment)
81 {
82  // Inherit.
83  styled_widget::set_text_alignment(text_alignment);
84 
85  text_alignment_ = text_alignment;
86 
88  widget->set_text_alignment(text_alignment_);
89  }
90 }
91 
92 void scroll_label::set_use_markup(bool use_markup)
93 {
94  // Inherit.
96 
98  widget->set_use_markup(use_markup);
99  }
100 }
101 
102 void scroll_label::set_text_alpha(unsigned short alpha)
103 {
104  if(label* widget = get_internal_label()) {
105  widget->set_text_alpha(alpha);
106  }
107 }
108 
110 {
111  link_aware_ = l;
112 
113  if(label* widget = get_internal_label()) {
114  widget->set_link_aware(l);
115  }
116 }
117 
118 void scroll_label::set_self_active(const bool active)
119 {
120  state_ = active ? ENABLED : DISABLED;
121 }
122 
124 {
125  return state_ != DISABLED;
126 }
127 
128 unsigned scroll_label::get_state() const
129 {
130  return state_;
131 }
132 
134 {
135  label* lbl = get_internal_label();
136  assert(lbl);
137 
138  lbl->set_label(get_label());
139  lbl->set_can_wrap(wrap_on_);
143 }
144 
145 void scroll_label::set_can_wrap(bool can_wrap)
146 {
147  label* lbl = get_internal_label();
148  assert(lbl);
149 
150  wrap_on_ = can_wrap;
151  lbl->set_can_wrap(wrap_on_);
152 }
153 
155 {
156  return wrap_on_;
157 }
158 
160 {
161  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
162 
163  get_window()->keyboard_capture(this);
164 }
165 
166 // }---------- DEFINITION ---------{
167 
170 {
171  DBG_GUI_P << "Parsing scroll label " << id;
172 
173  load_resolutions<resolution>(cfg);
174 }
175 
177  : resolution_definition(cfg), grid(nullptr)
178 {
179  // Note the order should be the same as the enum state_t is scroll_label.hpp.
180  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", _("Missing required state for scroll label control")));
181  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_disabled", _("Missing required state for scroll label control")));
182 
183  auto child = VALIDATE_WML_CHILD(cfg, "grid", _("No grid defined for scroll label control"));
184  grid = std::make_shared<builder_grid>(child);
185 }
186 
187 // }---------- BUILDER -----------{
188 
189 namespace implementation
190 {
191 
192 builder_scroll_label::builder_scroll_label(const config& cfg)
194  , vertical_scrollbar_mode(get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
195  , horizontal_scrollbar_mode(get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
196  , wrap_on(cfg["wrap"].to_bool(true))
197  , text_alignment(decode_text_alignment(cfg["text_alignment"]))
198  , link_aware(cfg["link_aware"].to_bool(false))
199 {
200 }
201 
202 std::unique_ptr<widget> builder_scroll_label::build() const
203 {
204  auto widget = std::make_unique<scroll_label>(*this);
205 
206  widget->set_vertical_scrollbar_mode(vertical_scrollbar_mode);
207  widget->set_horizontal_scrollbar_mode(horizontal_scrollbar_mode);
208 
209  const auto conf = widget->cast_config_to<scroll_label_definition>();
210  assert(conf);
211 
212  widget->init_grid(*conf->grid);
213  widget->finalize_setup();
214 
215  DBG_GUI_G << "Window builder: placed scroll label '" << id
216  << "' with definition '" << definition << "'.";
217 
218  return widget;
219 }
220 
221 } // namespace implementation
222 
223 // }------------ END --------------
224 
225 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
Base container class.
Definition: grid.hpp:32
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:645
A label displays a text, the text can be wrapped but no scrollbars are provided.
Definition: label.hpp:58
void set_can_wrap(const bool wrap)
Definition: label.hpp:120
void set_link_aware(bool l)
Definition: label.cpp:92
Label showing a text.
virtual bool get_active() const override
See styled_widget::get_active.
void set_can_wrap(bool can_wrap)
void signal_handler_left_button_down(const event::ui_event event)
bool can_wrap() const override
See widget::can_wrap.
virtual void set_label(const t_string &label) override
See styled_widget::set_label.
virtual void set_text_alignment(const PangoAlignment text_alignment) override
See styled_widget::set_text_alignment.
void set_text_alpha(unsigned short alpha)
virtual void set_use_markup(bool use_markup) override
See styled_widget::set_use_markup.
void finalize_subclass() override
Function for the subclasses to do their setup.
label * get_internal_label()
void set_link_aware(bool l)
state_t state_
Current state of the widget.
virtual unsigned get_state() const override
See styled_widget::get_state.
PangoAlignment text_alignment_
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Base class for creating containers with one or two scrollbar(s).
virtual void place(const point &origin, const point &size) override
See widget::place.
bool content_resize_request(const bool force_sizing=false)
Notification if the content of a child needs a resize.
const t_string & get_label() const
virtual void set_label(const t_string &label)
virtual void set_text_alignment(const PangoAlignment text_alignment)
virtual void set_use_markup(bool use_markup)
bool get_use_markup() const
Base class for all widgets.
Definition: widget.hpp:54
point get_origin() const
Returns the screen origin of the widget.
Definition: widget.cpp:301
point get_size() const
Returns the size of the widget.
Definition: widget.cpp:306
window * get_window()
Get the parent window.
Definition: widget.cpp:118
void keyboard_capture(widget *widget)
Definition: window.cpp:1130
static std::string _(const char *str)
Definition: gettext.hpp:93
Define the common log macros for the gui toolkit.
#define DBG_GUI_G
Definition: log.hpp:41
#define DBG_GUI_P
Definition: log.hpp:66
#define DBG_GUI_E
Definition: log.hpp:35
This file contains the window object, this object is a top level container which has the event manage...
void point(int x, int y)
Draw a single point.
Definition: draw.cpp:193
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
scrollbar_mode get_scrollbar_mode(const std::string &scrollbar_mode)
Returns the scrollbar mode flags.
Definition: helper.cpp:121
Generic file dialog.
PangoAlignment decode_text_alignment(const std::string &alignment)
Converts a text alignment string to a text alignment.
Definition: helper.cpp:60
Contains the implementation details for lexical_cast and shouldn't be used directly.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
#define LOG_HEADER
This file contains the settings handling of the widget library.
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
virtual std::unique_ptr< widget > build() const override
std::string definition
Parameters for the styled_widget.
Base class of a resolution, contains the common keys for a resolution.
std::vector< state_definition > state
scroll_label_definition(const config &cfg)
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE_WML_CHILD(cfg, key, message)