The Battle for Wesnoth  1.15.0-dev
viewport.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 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 
17 #include "gui/widgets/viewport.hpp"
18 
20 #include "gui/core/log.hpp"
21 #include "config.hpp"
22 #include "utils/const_clone.hpp"
23 
24 #define LOG_SCOPE_HEADER "viewport [" + id() + "] " + __func__
25 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
26 
27 namespace gui2
28 {
29 
30 // ------------ WIDGET -----------{
31 
32 /**
33  * Helper to implement private functions without modifying the header.
34  *
35  * The class is a helper to avoid recompilation and only has static
36  * functions. It also facilitates to create duplicates of functions for a const
37  * and a non-const member function.
38  */
40 {
41  /**
42  * Implementation for the wrappers for
43  * [const] widget* pane::find_at(const point&, const bool) [const].
44  *
45  * @tparam W A pointer to the pane.
46  */
47  template <class W>
49  find_at(W viewport, point coordinate, const bool must_be_active)
50  {
51 
52  /*
53  * First test whether the mouse is at the pane.
54  */
55  if(viewport->widget::find_at(coordinate, must_be_active) != viewport) {
56  return nullptr;
57  }
58 
59  /*
60  * The widgets are placed at coordinate 0,0 so adjust the offset to
61  * that coordinate system.
62  */
63  coordinate.x -= viewport->get_x();
64  coordinate.y -= viewport->get_y();
65 
66  return viewport->widget_->find_at(coordinate, must_be_active);
67  }
68 
69  template <class W>
71  find(W viewport, const std::string& id, const bool must_be_active)
72  {
73  if(viewport->widget::find(id, must_be_active)) {
74  return viewport;
75  } else {
76  return viewport->widget_->find(id, must_be_active);
77  }
78  }
79 };
80 
82  const builder_widget::replacements_map& replacements)
83  : widget(builder)
84  , widget_(builder.widget_->build(replacements))
85 {
86  widget_->set_parent(this);
87 }
88 
90 {
91 }
92 
93 void viewport::place(const point& origin, const point& size)
94 {
95  widget::place(origin, size);
96 
97  widget_->place(point(), widget_->get_best_size());
98 }
99 
100 void viewport::layout_initialize(const bool full_initialization)
101 {
102  widget::layout_initialize(full_initialization);
103 
104  if(widget_->get_visible() != widget::visibility::invisible) {
105  widget_->layout_initialize(full_initialization);
106  }
107 }
108 
110 {
111  if(widget_->get_visible() != widget::visibility::invisible) {
112  widget_->draw_background();
113  widget_->draw_children();
114  widget_->draw_foreground();
115  }
116 }
117 
118 void viewport::request_reduce_width(const unsigned /*maximum_width*/)
119 {
120 }
121 
122 widget* viewport::find_at(const point& coordinate, const bool must_be_active)
123 {
124  return viewport_implementation::find_at(this, coordinate, must_be_active);
125 }
126 
128  const bool must_be_active) const
129 {
130  return viewport_implementation::find_at(this, coordinate, must_be_active);
131 }
132 
133 widget* viewport::find(const std::string& id, const bool must_be_active)
134 {
135  return viewport_implementation::find(this, id, must_be_active);
136 }
137 
138 const widget* viewport::find(const std::string& id, const bool must_be_active)
139  const
140 {
141  return viewport_implementation::find(this, id, must_be_active);
142 }
143 
145 {
146  return widget_->get_best_size();
147 }
148 
150 {
151  return false;
152 }
153 
155 {
156  return nullptr;
157 }
158 
159 // }---------- BUILDER -----------{
160 
161 /*WIKI_MACRO
162  * @begin{macro}{viewport_description}
163  *
164  * A viewport is an special widget used to view only a part of the
165  * widget it `holds'.
166  * @end{macro}
167  */
168 
169 /*WIKI
170  * @page = GUIWidgetInstanceWML
171  * @order = 2_viewport
172  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
173  * @begin{tag}{name="viewport"}{min=0}{max=-1}{super="generic/widget_instance"}
174  * @begin{tag}{name="widget"}{min="1"}{max="1"}{super="gui/window/resolution/grid/row/column"}
175  * == Label ==
176  *
177  * @macro = viewport_description
178  *
179  * List with the label specific variables:
180  * @begin{table}{config}
181  * widget & section & & Holds a single widget like a grid cell.$
182  * @end{table}
183  * @end{tag}{name="widget"}
184  * @end{tag}{name="viewport"}
185  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
186  */
187 
188 namespace implementation
189 {
190 
191 builder_viewport::builder_viewport(const config& cfg)
192  : builder_widget(cfg)
193  , widget_(create_widget_builder(cfg.child("widget", "[viewport]")))
194 {
195 }
196 
198 {
199  return build(replacements_map());
200 }
201 
203 {
204  return std::make_shared<viewport>(*this, replacements);
205 }
206 
207 } // namespace implementation
208 
209 // }------------ END --------------
210 
211 } // namespace gui2
Define the common log macros for the gui toolkit.
Contains the info needed to instantiate a widget.
std::unique_ptr< class walker_base > walker_ptr
Definition: widget.hpp:38
virtual void layout_initialize(const bool full_initialization)
How the layout engine works.
Definition: widget.cpp:163
Helper to implement private functions without modifying the header.
Definition: viewport.cpp:39
Base class for all widgets.
Definition: widget.hpp:48
widget_ptr widget_
Definition: viewport.hpp:81
virtual widget_ptr build() const override
Definition: viewport.cpp:197
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
Definitions for the interface to Wesnoth Markup Language (WML).
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: viewport.cpp:118
int x
x coordinate.
Definition: point.hpp:44
Generic file dialog.
Definition: field-fwd.hpp:22
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
virtual void place(const point &origin, const point &size)
Places the widget.
Definition: widget.cpp:232
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: viewport.cpp:133
std::shared_ptr< widget > widget_ptr
Definition: widget.hpp:732
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: viewport.cpp:93
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: viewport.cpp:122
typename const_clone< D, S >::pointer const_clone_ptr
Definition: const_clone.hpp:65
The user set the widget invisible, that means:
viewport(const implementation::builder_viewport &builder, const builder_widget::replacements_map &replacements)
Definition: viewport.cpp:81
Holds a 2D point.
Definition: point.hpp:23
static utils::const_clone_ptr< widget, W > find(W viewport, const std::string &id, const bool must_be_active)
Definition: viewport.cpp:71
virtual void impl_draw_children() override
See widget::impl_draw_children.
Definition: viewport.cpp:109
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: viewport.cpp:100
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: viewport.cpp:149
virtual iteration::walker_ptr create_walker() override
See widget::create_walker.
Definition: viewport.cpp:154
static utils::const_clone_ptr< widget, W > find_at(W viewport, point coordinate, const bool must_be_active)
Implementation for the wrappers for [const] widget* pane::find_at(const point&, const bool) [const]...
Definition: viewport.cpp:49
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
int y
y coordinate.
Definition: point.hpp:47
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: viewport.cpp:144
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
builder_widget_ptr create_widget_builder(const config &cfg)
Create a widget builder.