The Battle for Wesnoth  1.19.7+dev
scroll_label.cpp
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 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
20 #include "gui/widgets/label.hpp"
21 #include "gui/core/log.hpp"
24 #include "gui/widgets/window.hpp"
25 #include "gettext.hpp"
26 #include "wml_exception.hpp"
27 
28 #include <functional>
29 
30 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
31 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
32 
33 namespace gui2
34 {
35 
36 // ------------ WIDGET -----------{
37 
38 REGISTER_WIDGET(scroll_label)
39 
40 scroll_label::scroll_label(const implementation::builder_scroll_label& builder)
41  : scrollbar_container(builder, type())
42  , state_(ENABLED)
43  , wrap_on_(builder.wrap_on)
44  , text_alignment_(builder.text_alignment)
45  , link_aware_(builder.link_aware)
46 {
47  connect_signal<event::LEFT_BUTTON_DOWN>(
48  std::bind(&scroll_label::signal_handler_left_button_down, this, std::placeholders::_2),
50 }
51 
53 {
54  if(content_grid()) {
55  return dynamic_cast<label*>(content_grid()->find("_label", false));
56  }
57 
58  return nullptr;
59 }
60 
62 {
63  // Inherit.
65 
67  widget->set_label(lbl);
68 
69  bool resize_needed = !content_resize_request();
70  if(resize_needed && get_size() != point()) {
72  }
73  }
74 }
75 
76 void scroll_label::set_text_alignment(const PangoAlignment text_alignment)
77 {
78  // Inherit.
79  styled_widget::set_text_alignment(text_alignment);
80 
81  text_alignment_ = text_alignment;
82 
84  widget->set_text_alignment(text_alignment_);
85  }
86 }
87 
88 void scroll_label::set_use_markup(bool use_markup)
89 {
90  // Inherit.
92 
94  widget->set_use_markup(use_markup);
95  }
96 }
97 
98 void scroll_label::set_text_alpha(unsigned short alpha)
99 {
100  if(label* widget = get_internal_label()) {
101  widget->set_text_alpha(alpha);
102  }
103 }
104 
106 {
107  link_aware_ = l;
108 
109  if(label* widget = get_internal_label()) {
110  widget->set_link_aware(l);
111  }
112 }
113 
114 void scroll_label::set_self_active(const bool active)
115 {
116  state_ = active ? ENABLED : DISABLED;
117 }
118 
120 {
121  return state_ != DISABLED;
122 }
123 
124 unsigned scroll_label::get_state() const
125 {
126  return state_;
127 }
128 
130 {
131  label* lbl = get_internal_label();
132  assert(lbl);
133 
134  lbl->set_label(get_label());
135  lbl->set_can_wrap(wrap_on_);
139 }
140 
141 void scroll_label::set_can_wrap(bool can_wrap)
142 {
143  label* lbl = get_internal_label();
144  assert(lbl);
145 
146  wrap_on_ = can_wrap;
147  lbl->set_can_wrap(wrap_on_);
148 }
149 
151 {
152  return wrap_on_;
153 }
154 
156 {
157  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
158 
159  get_window()->keyboard_capture(this);
160 }
161 
162 // }---------- DEFINITION ---------{
163 
166 {
167  DBG_GUI_P << "Parsing scroll label " << id;
168 
169  load_resolutions<resolution>(cfg);
170 }
171 
173  : resolution_definition(cfg), grid(nullptr)
174 {
175  // Note the order should be the same as the enum state_t is scroll_label.hpp.
176  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", missing_mandatory_wml_tag("scroll_label_definition][resolution", "state_enabled")));
177  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_disabled", missing_mandatory_wml_tag("scroll_label_definition][resolution", "state_disabled")));
178 
179  auto child = VALIDATE_WML_CHILD(cfg, "grid", missing_mandatory_wml_tag("scroll_label_definition][resolution", "grid"));
180  grid = std::make_shared<builder_grid>(child);
181 }
182 
183 // }---------- BUILDER -----------{
184 
185 namespace implementation
186 {
187 
188 builder_scroll_label::builder_scroll_label(const config& cfg)
190  , wrap_on(cfg["wrap"].to_bool(true))
191  , text_alignment(decode_text_alignment(cfg["text_alignment"]))
192  , link_aware(cfg["link_aware"].to_bool(false))
193 {
194 }
195 
196 std::unique_ptr<widget> builder_scroll_label::build() const
197 {
198  auto widget = std::make_unique<scroll_label>(*this);
199 
200  const auto conf = widget->cast_config_to<scroll_label_definition>();
201  assert(conf);
202 
203  widget->init_grid(*conf->grid);
204  widget->finalize_setup();
205 
206  DBG_GUI_G << "Window builder: placed scroll label '" << id
207  << "' with definition '" << definition << "'.";
208 
209  return widget;
210 }
211 
212 } // namespace implementation
213 
214 // }------------ END --------------
215 
216 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Base container class.
Definition: grid.hpp:32
widget * find(const std::string_view id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:645
void set_can_wrap(const bool wrap)
Definition: label.hpp:94
void set_link_aware(bool l)
Definition: label.cpp:88
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_text_alignment(const PangoAlignment text_alignment)
virtual void set_label(const t_string &text)
virtual void set_use_markup(bool use_markup)
bool get_use_markup() const
Base class for all widgets.
Definition: widget.hpp:55
point get_origin() const
Returns the screen origin of the widget.
Definition: widget.cpp:311
point get_size() const
Returns the size of the widget.
Definition: widget.cpp:316
window * get_window()
Get the parent window.
Definition: widget.cpp:117
void keyboard_capture(widget *widget)
Definition: window.cpp:1207
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:202
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
Generic file dialog.
PangoAlignment decode_text_alignment(const std::string &alignment)
Converts a text alignment string to a text alignment.
Definition: helper.cpp:89
Contains the implementation details for lexical_cast and shouldn't be used directly.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
#define LOG_HEADER
virtual std::unique_ptr< widget > build() const override
std::string definition
Parameters for the styled_widget.
std::vector< state_definition > state
scroll_label_definition(const config &cfg)
std::string missing_mandatory_wml_tag(const std::string &section, const std::string &tag)
Returns a standard message for a missing wml child (tag).
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)