The Battle for Wesnoth  1.17.0-dev
scroll_label.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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 
31 #include <functional>
32 
33 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
34 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
35 
36 namespace gui2
37 {
38 
39 // ------------ WIDGET -----------{
40 
41 REGISTER_WIDGET(scroll_label)
42 
43 scroll_label::scroll_label(const implementation::builder_scroll_label& builder)
44  : scrollbar_container(builder, type())
45  , state_(ENABLED)
46  , wrap_on_(builder.wrap_on)
47  , text_alignment_(builder.text_alignment)
48  , link_aware_(builder.link_aware)
49 {
50  connect_signal<event::LEFT_BUTTON_DOWN>(
51  std::bind(&scroll_label::signal_handler_left_button_down, this, std::placeholders::_2),
53 }
54 
56 {
57  if(content_grid()) {
58  return dynamic_cast<label*>(content_grid()->find("_label", false));
59  }
60 
61  return nullptr;
62 }
63 
65 {
66  // Inherit.
68 
70  widget->set_label(lbl);
71 
72  bool resize_needed = !content_resize_request();
73  if(resize_needed && get_size() != point()) {
75  }
76  }
77 }
78 
79 void scroll_label::set_text_alignment(const PangoAlignment text_alignment)
80 {
81  // Inherit.
82  styled_widget::set_text_alignment(text_alignment);
83 
84  text_alignment_ = text_alignment;
85 
87  widget->set_text_alignment(text_alignment_);
88  }
89 }
90 
91 void scroll_label::set_use_markup(bool use_markup)
92 {
93  // Inherit.
95 
97  widget->set_use_markup(use_markup);
98  }
99 }
100 
101 void scroll_label::set_text_alpha(unsigned short alpha)
102 {
103  if(label* widget = get_internal_label()) {
104  widget->set_text_alpha(alpha);
105  }
106 }
107 
109 {
110  link_aware_ = l;
111 
112  if(label* widget = get_internal_label()) {
113  widget->set_link_aware(l);
114  }
115 }
116 
117 void scroll_label::set_self_active(const bool active)
118 {
119  state_ = active ? ENABLED : DISABLED;
120 }
121 
123 {
124  return state_ != DISABLED;
125 }
126 
127 unsigned scroll_label::get_state() const
128 {
129  return state_;
130 }
131 
133 {
134  label* lbl = get_internal_label();
135  assert(lbl);
136 
137  lbl->set_label(get_label());
138  lbl->set_can_wrap(wrap_on_);
142 }
143 
145 {
146  label* lbl = get_internal_label();
147  assert(lbl);
148 
149  wrap_on_ = can_wrap;
150  lbl->set_can_wrap(wrap_on_);
151 }
152 
154 {
155  return wrap_on_;
156 }
157 
159 {
160  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
161 
162  get_window()->keyboard_capture(this);
163 }
164 
165 // }---------- DEFINITION ---------{
166 
169 {
170  DBG_GUI_P << "Parsing scroll label " << id << '\n';
171 
172  load_resolutions<resolution>(cfg);
173 }
174 
176  : resolution_definition(cfg), grid(nullptr)
177 {
178  // Note the order should be the same as the enum state_t is scroll_label.hpp.
179  state.emplace_back(cfg.child("state_enabled"));
180  state.emplace_back(cfg.child("state_disabled"));
181 
182  const config& child = cfg.child("grid");
183  VALIDATE(child, _("No grid defined."));
184 
185  grid = std::make_shared<builder_grid>(child);
186 }
187 
188 // }---------- BUILDER -----------{
189 
190 namespace implementation
191 {
192 
193 builder_scroll_label::builder_scroll_label(const config& cfg)
195  , vertical_scrollbar_mode(get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
196  , horizontal_scrollbar_mode(get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
197  , wrap_on(cfg["wrap"].to_bool(true))
198  , text_alignment(decode_text_alignment(cfg["text_alignment"]))
199  , link_aware(cfg["link_aware"].to_bool(false))
200 {
201 }
202 
204 {
205  scroll_label* widget = new scroll_label(*this);
206 
209 
210  const auto conf = widget->cast_config_to<scroll_label_definition>();
211  assert(conf);
212 
213  widget->init_grid(*conf->grid);
214  widget->finalize_setup();
215 
216  DBG_GUI_G << "Window builder: placed scroll label '" << id
217  << "' with definition '" << definition << "'.\n";
218 
219  return widget;
220 }
221 
222 } // namespace implementation
223 
224 // }------------ END --------------
225 
226 } // namespace gui2
Define the common log macros for the gui toolkit.
Base class of a resolution, contains the common keys for a resolution.
void keyboard_capture(widget *widget)
Definition: window.cpp:1276
#define DBG_GUI_P
Definition: log.hpp:66
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:402
virtual unsigned get_state() const override
See styled_widget::get_state.
std::vector< state_definition > state
void set_horizontal_scrollbar_mode(const scrollbar_mode scrollbar_mode)
virtual void set_use_markup(bool use_markup) override
See styled_widget::set_use_markup.
void set_can_wrap(const bool wrap)
Definition: label.hpp:120
bool get_use_markup() const
virtual void place(const point &origin, const point &size) override
See widget::place.
void finalize_subclass() override
Function for the subclasses to do their setup.
This file contains the window object, this object is a top level container which has the event manage...
void set_vertical_scrollbar_mode(const scrollbar_mode scrollbar_mode)
PangoAlignment text_alignment_
Base class for all widgets.
Definition: widget.hpp:49
virtual widget * build() const override
A label displays a text, the text can be wrapped but no scrollbars are provided.
Definition: label.hpp:57
PangoAlignment decode_text_alignment(const std::string &alignment)
Converts a text alignment string to a text alignment.
Definition: helper.cpp:64
static std::string _(const char *str)
Definition: gettext.hpp:93
void signal_handler_left_button_down(const event::ui_event event)
void set_text_alpha(unsigned short alpha)
virtual void set_label(const t_string &label) override
See styled_widget::set_label.
Generic file dialog.
Definition: field-fwd.hpp:23
state_t state_
Current state of the widget.
virtual void set_label(const t_string &label)
Base container class.
Definition: grid.hpp:31
std::string definition
Parameters for the styled_widget.
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
This file contains the settings handling of the widget library.
virtual bool get_active() const override
See styled_widget::get_active.
void init_grid(const builder_grid &grid_builder)
Initializes and builds the grid.
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:656
virtual void set_text_alignment(const PangoAlignment text_alignment)
bool content_resize_request(const bool force_sizing=false)
Notification if the content of a child needs a resize.
virtual void set_use_markup(bool use_markup)
void set_link_aware(bool l)
Label showing a text.
virtual void set_text_alignment(const PangoAlignment text_alignment) override
See styled_widget::set_text_alignment.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
label * get_internal_label()
Base class for creating containers with one or two scrollbar(s).
#define DBG_GUI_E
Definition: log.hpp:35
std::shared_ptr< const typename T::resolution > cast_config_to() const
Casts the current resolution definition config to the respective type of a derived widget...
window * get_window()
Get the parent window.
Definition: widget.cpp:117
Holds a 2D point.
Definition: point.hpp:24
void set_can_wrap(bool can_wrap)
scrollbar_mode get_scrollbar_mode(const std::string &scrollbar_mode)
Returns the scrollbar mode flags.
Definition: helper.cpp:121
point get_size() const
Returns the size of the widget.
Definition: widget.cpp:305
const t_string & get_label() const
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
#define LOG_HEADER
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
bool can_wrap() const override
See widget::can_wrap.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
#define DBG_GUI_G
Definition: log.hpp:41
scroll_label_definition(const config &cfg)
point get_origin() const
Returns the screen origin of the widget.
Definition: widget.cpp:300
void set_link_aware(bool l)
Definition: label.cpp:91
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
ui_event
The event send to the dispatcher.
Definition: handler.hpp:48