The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
scroll_label.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 "utils/functional.hpp"
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 tscroll_label::tscroll_label(bool wrap) : tscrollbar_container(COUNT), state_(ENABLED), wrap_on(wrap)
43 {
44  connect_signal<event::LEFT_BUTTON_DOWN>(
45  std::bind(
48 }
49 
51 {
52  // Inherit.
53  tcontrol::set_label(label);
54 
55  if(content_grid()) {
56  tlabel* widget
57  = find_widget<tlabel>(content_grid(), "_label", false, true);
58  widget->set_label(label);
59 
61  }
62 }
63 
64 void tscroll_label::set_use_markup(bool use_markup)
65 {
66  // Inherit.
67  tcontrol::set_use_markup(use_markup);
68 
69  if(content_grid()) {
70  tlabel* widget
71  = find_widget<tlabel>(content_grid(), "_label", false, true);
72  widget->set_use_markup(use_markup);
73  }
74 }
75 
76 void tscroll_label::set_self_active(const bool active)
77 {
78  state_ = active ? ENABLED : DISABLED;
79 }
80 
82 {
83  return state_ != DISABLED;
84 }
85 
86 unsigned tscroll_label::get_state() const
87 {
88  return state_;
89 }
90 
92 {
93  assert(content_grid());
94  tlabel* lbl = dynamic_cast<tlabel*>(content_grid()->find("_label", false));
95 
96  assert(lbl);
97  lbl->set_label(label());
98  lbl->set_can_wrap(wrap_on);
99 }
100 
101 void tscroll_label::set_can_wrap(bool can_wrap)
102 {
103  assert(content_grid());
104  tlabel* lbl = dynamic_cast<tlabel*>(content_grid()->find("_label", false));
105 
106  assert(lbl);
107  wrap_on = can_wrap;
108  lbl->set_can_wrap(wrap_on);
109 }
110 
112 {
113  return wrap_on;
114 }
115 
117 {
118  static const std::string type = "scroll_label";
119  return type;
120 }
121 
123 {
124  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
125 
126  get_window()->keyboard_capture(this);
127 }
128 
129 // }---------- DEFINITION ---------{
130 
132  : tcontrol_definition(cfg)
133 {
134  DBG_GUI_P << "Parsing scroll label " << id << '\n';
135 
136  load_resolutions<tresolution>(cfg);
137 }
138 
139 /*WIKI
140  * @page = GUIWidgetDefinitionWML
141  * @order = 1_scroll_label
142  *
143  * == Scroll label ==
144  *
145  * @macro = scroll_label_description
146  *
147  * @begin{parent}{name="gui/"}
148  * This widget is slower as a normal label widget so only use this widget
149  * when the scrollbar is required (or expected to become required).
150  * @begin{tag}{name="scroll_label_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
151  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
152  * @begin{table}{config}
153  * grid & grid & & A grid containing the widgets for main
154  * widget. $
155  * @end{table}
156  * @allow{link}{name="gui/window/resolution/grid"}
157  * TODO we need one definition for a vertical scrollbar since this is the second
158  * time we use it.
159  *
160  * @begin{table}{dialog_widgets}
161  * _content_grid & & grid & m & A grid which should only contain one
162  * label widget. $
163  * _scrollbar_grid & & grid & m & A grid for the scrollbar
164  * (Merge with listbox info.) $
165  * @end{table}
166  * @begin{tag}{name="content_grid"}{min=0}{max=1}{super="gui/window/resolution/grid"}
167  * @end{tag}{name="content_grid"}
168  * @begin{tag}{name="scrollbar_grid"}{min=0}{max=1}{super="gui/window/resolution/grid"}
169  * @end{tag}{name="scrollbar_grid"}
170  * The following states exist:
171  * * state_enabled, the scroll label is enabled.
172  * * state_disabled, the scroll label is disabled.
173  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
174  * @end{tag}{name="state_enabled"}
175  * @begin{tag}{name="state_disabled"}{min=0}{max=1}{super="generic/state"}
176  * @end{tag}{name="state_disabled"}
177  * @end{tag}{name="resolution"}
178  * @end{tag}{name="scroll_label_definition"}
179  * @end{parent}{name="gui/"}
180  */
182  : tresolution_definition_(cfg), grid(nullptr)
183 {
184  // Note the order should be the same as the enum tstate is scroll_label.hpp.
185  state.push_back(tstate_definition(cfg.child("state_enabled")));
186  state.push_back(tstate_definition(cfg.child("state_disabled")));
187 
188  const config& child = cfg.child("grid");
189  VALIDATE(child, _("No grid defined."));
190 
191  grid = new tbuilder_grid(child);
192 }
193 
194 // }---------- BUILDER -----------{
195 
196 /*WIKI_MACRO
197  * @begin{macro}{scroll_label_description}
198  *
199  * A scroll label is a label that wraps its text and also has a
200  * vertical scrollbar. This way a text can't be too long to be shown
201  * for this widget.
202  * @end{macro}
203  */
204 
205 /*WIKI
206  * @page = GUIWidgetInstanceWML
207  * @order = 2_scroll_label
208  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
209  * @begin{tag}{name="scroll_label"}{min="0"}{max="-1"}{super="generic/widget_instance"}
210  * == Scroll label ==
211  *
212  * @macro = scroll_label_description
213  *
214  * List with the scroll label specific variables:
215  * @begin{table}{config}
216  * vertical_scrollbar_mode & scrollbar_mode & initial_auto &
217  * Determines whether or not to show the
218  * scrollbar. $
219  * horizontal_scrollbar_mode & scrollbar_mode & initial_auto &
220  * Determines whether or not to show the
221  * scrollbar. $
222  * wrap & boolean & true & Determines whether the text of the
223  * label is allowed to wrap. $
224  * @end{table}
225  * @end{tag}{name="scroll_label"}
226  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
227  */
228 
229 namespace implementation
230 {
231 
232 tbuilder_scroll_label::tbuilder_scroll_label(const config& cfg)
234  , vertical_scrollbar_mode(
235  get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
236  , horizontal_scrollbar_mode(
237  get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
238  , wrap_on(cfg["wrap"].to_bool(true))
239 {
240 }
241 
243 {
244  tscroll_label* widget = new tscroll_label(wrap_on);
245 
246  init_control(widget);
247 
250 
252  conf = boost::
253  dynamic_pointer_cast<const tscroll_label_definition::tresolution>(
254  widget->config());
255  assert(conf);
256 
257  widget->init_grid(conf->grid);
258  widget->finalize_setup();
259 
260  DBG_GUI_G << "Window builder: placed scroll label '" << id
261  << "' with definition '" << definition << "'.\n";
262 
263  return widget;
264 }
265 
266 } // namespace implementation
267 
268 // }------------ END --------------
269 
270 } // namespace gui2
Define the common log macros for the gui toolkit.
#define DBG_GUI_P
Definition: log.hpp:69
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
virtual bool get_active() const override
See tcontrol::get_active.
void set_can_wrap(const bool wrap)
Definition: label.hpp:60
void signal_handler_left_button_down(const event::tevent event)
tresolution_definition_ptr config()
Definition: control.hpp:299
This file contains the window object, this object is a top level container which has the event manage...
virtual void set_label(const t_string &label)
Definition: control.cpp:330
tscrollbar_container::tscrollbar_mode vertical_scrollbar_mode
void set_can_wrap(bool can_wrap)
tscrollbar_container::tscrollbar_mode horizontal_scrollbar_mode
tscroll_label_definition(const config &cfg)
tstate state_
Current state of the widget.
virtual void set_use_markup(bool use_markup) override
See tcontrol::set_use_markup.
Label showing a text.
Definition: label.hpp:29
tscrollbar_container::tscrollbar_mode get_scrollbar_mode(const std::string &scrollbar_mode)
Returns the scrollbar mode flags.
Definition: helper.cpp:114
Base class of a resolution, contains the common keys for a resolution.
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
void init_control(tcontrol *control) const
Definition: control.cpp:675
std::string definition
Parameters for the control.
Definition: control.hpp:530
virtual void set_use_markup(bool use_markup)
Definition: control.cpp:342
bool content_resize_request(const bool force_sizing=false)
Notification if the content of a child needs a resize.
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
virtual void set_label(const t_string &label) override
See tcontrol::set_label.
void set_vertical_scrollbar_mode(const tscrollbar_mode scrollbar_mode)
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
This file contains the settings handling of the widget library.
void init_grid(const boost::intrusive_ptr< tbuilder_grid > &grid_builder)
Initializes and builds the grid.
Definition: container.cpp:209
const t_string & label() const
Definition: control.hpp:248
tevent
The event send to the dispatcher.
Definition: handler.hpp:54
Label showing a text.
Contains the state info for a resolution.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
#define DBG_GUI_E
Definition: log.hpp:35
virtual const std::string & get_control_type() const override
See tcontrol::get_control_type.
void set_horizontal_scrollbar_mode(const tscrollbar_mode scrollbar_mode)
std::vector< tstate_definition > state
cl_event event
Definition: glew.h:3070
void finalize_subclass()
Function for the subclasses to do their setup.
#define LOG_HEADER
config & child(const std::string &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:658
Base class for all widgets.
Definition: widget.hpp:49
twidget * find(const std::string &id, const bool must_be_active) override
See twidget::find.
Definition: grid.cpp:612
virtual unsigned get_state() const override
See tcontrol::get_state.
void keyboard_capture(twidget *widget)
Definition: window.cpp:1394
twindow * get_window()
Get the parent window.
Definition: widget.cpp:116
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
bool can_wrap() const
See twidget::can_wrap.
virtual void set_self_active(const bool active) override
See tcontainer_::set_self_active.
#define DBG_GUI_G
Definition: log.hpp:41
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Contains the implementation details for lexical_cast and shouldn't be used directly.
Base class for creating containers with one or two scrollbar(s).