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