The Battle for Wesnoth  1.15.13+dev
scroll_label.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "gui/widgets/label.hpp"
21 #include "gui/core/log.hpp"
24 #include "gui/widgets/settings.hpp"
26 #include "gui/widgets/spacer.hpp"
27 #include "gui/widgets/window.hpp"
28 #include "gettext.hpp"
29 
30 #include <functional>
31 
32 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
33 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
34 
35 namespace gui2
36 {
37 
38 // ------------ WIDGET -----------{
39 
40 REGISTER_WIDGET(scroll_label)
41 
42 scroll_label::scroll_label(const implementation::builder_scroll_label& builder)
43  : scrollbar_container(builder, type())
44  , state_(ENABLED)
45  , wrap_on_(builder.wrap_on)
46  , text_alignment_(builder.text_alignment)
47  , link_aware_(builder.link_aware)
48 {
49  connect_signal<event::LEFT_BUTTON_DOWN>(
50  std::bind(&scroll_label::signal_handler_left_button_down, this, std::placeholders::_2),
52 }
53 
55 {
56  if(content_grid()) {
57  return dynamic_cast<label*>(content_grid()->find("_label", false));
58  }
59 
60  return nullptr;
61 }
62 
64 {
65  // Inherit.
67 
69  widget->set_label(lbl);
70 
71  bool resize_needed = !content_resize_request();
72  if(resize_needed && get_size() != point()) {
74  }
75  }
76 }
77 
78 void scroll_label::set_text_alignment(const PangoAlignment text_alignment)
79 {
80  // Inherit.
81  styled_widget::set_text_alignment(text_alignment);
82 
83  text_alignment_ = text_alignment;
84 
86  widget->set_text_alignment(text_alignment_);
87  }
88 }
89 
90 void scroll_label::set_use_markup(bool use_markup)
91 {
92  // Inherit.
94 
96  widget->set_use_markup(use_markup);
97  }
98 }
99 
100 void scroll_label::set_text_alpha(unsigned short alpha)
101 {
102  if(label* widget = get_internal_label()) {
103  widget->set_text_alpha(alpha);
104  }
105 }
106 
108 {
109  link_aware_ = l;
110 
111  if(label* widget = get_internal_label()) {
112  widget->set_link_aware(l);
113  }
114 }
115 
116 void scroll_label::set_self_active(const bool active)
117 {
118  state_ = active ? ENABLED : DISABLED;
119 }
120 
122 {
123  return state_ != DISABLED;
124 }
125 
126 unsigned scroll_label::get_state() const
127 {
128  return state_;
129 }
130 
132 {
133  label* lbl = get_internal_label();
134  assert(lbl);
135 
136  lbl->set_label(get_label());
137  lbl->set_can_wrap(wrap_on_);
141 }
142 
144 {
145  label* lbl = get_internal_label();
146  assert(lbl);
147 
148  wrap_on_ = can_wrap;
149  lbl->set_can_wrap(wrap_on_);
150 }
151 
153 {
154  return wrap_on_;
155 }
156 
158 {
159  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
160 
161  get_window()->keyboard_capture(this);
162 }
163 
164 // }---------- DEFINITION ---------{
165 
168 {
169  DBG_GUI_P << "Parsing scroll label " << id << '\n';
170 
171  load_resolutions<resolution>(cfg);
172 }
173 
175  : resolution_definition(cfg), grid(nullptr)
176 {
177  // Note the order should be the same as the enum state_t is scroll_label.hpp.
178  state.emplace_back(cfg.child("state_enabled"));
179  state.emplace_back(cfg.child("state_disabled"));
180 
181  const config& child = cfg.child("grid");
182  VALIDATE(child, _("No grid defined."));
183 
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 
203 {
204  scroll_label* widget = new scroll_label(*this);
205 
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 << "'.\n";
217 
218  return widget;
219 }
220 
221 } // namespace implementation
222 
223 // }------------ END --------------
224 
225 } // 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:1273
#define DBG_GUI_P
Definition: log.hpp:65
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:414
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:119
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:56
PangoAlignment decode_text_alignment(const std::string &alignment)
Converts a text alignment string to a text alignment.
Definition: helper.cpp:63
static std::string _(const char *str)
Definition: gettext.hpp:92
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:22
state_t state_
Current state of the widget.
virtual void set_label(const t_string &label)
Base container class.
Definition: grid.hpp:30
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:655
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:34
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:116
Holds a 2D point.
Definition: point.hpp:23
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:120
point get_size() const
Returns the size of the widget.
Definition: widget.cpp:304
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:59
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
#define DBG_GUI_G
Definition: log.hpp:40
scroll_label_definition(const config &cfg)
point get_origin() const
Returns the screen origin of the widget.
Definition: widget.cpp:299
void set_link_aware(bool l)
Definition: label.cpp:90
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:47