The Battle for Wesnoth  1.13.11+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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(surface& frame_buffer, int x_offset, int y_offset)
125 {
126  x_offset += get_x();
127  y_offset += get_y();
128 
130  widget_.draw_background(frame_buffer, x_offset, y_offset);
131  widget_.draw_children(frame_buffer, x_offset, y_offset);
132  widget_.draw_foreground(frame_buffer, x_offset, y_offset);
133  widget_.set_is_dirty(false);
134  }
135 }
136 
137 void
139  const std::vector<widget*>& call_stack)
140 {
141  std::vector<widget*> child_call_stack = call_stack;
142  widget_.populate_dirty_list(caller, child_call_stack);
143 }
144 
145 void viewport::request_reduce_width(const unsigned /*maximum_width*/)
146 {
147 }
148 
149 widget* viewport::find_at(const point& coordinate, const bool must_be_active)
150 {
151  return viewport_implementation::find_at(this, coordinate, must_be_active);
152 }
153 
155  const bool must_be_active) const
156 {
157  return viewport_implementation::find_at(this, coordinate, must_be_active);
158 }
159 
160 widget* viewport::find(const std::string& id, const bool must_be_active)
161 {
162  return viewport_implementation::find(this, id, must_be_active);
163 }
164 
165 const widget* viewport::find(const std::string& id, const bool must_be_active)
166  const
167 {
168  return viewport_implementation::find(this, id, must_be_active);
169 }
170 
172 {
173  return widget_.get_best_size();
174 }
175 
177 {
178  return false;
179 }
180 
182 {
183  /**
184  * @todo Implement properly.
185  */
186  return nullptr;
187 }
188 
189 // }---------- BUILDER -----------{
190 
191 /*WIKI_MACRO
192  * @begin{macro}{viewport_description}
193  *
194  * A viewport is an special widget used to view only a part of the
195  * widget it `holds'.
196  * @end{macro}
197  */
198 
199 /*WIKI
200  * @page = GUIWidgetInstanceWML
201  * @order = 2_viewport
202  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
203  * @begin{tag}{name="viewport"}{min=0}{max=-1}{super="generic/widget_instance"}
204  * @begin{tag}{name="widget"}{min="1"}{max="1"}{super="gui/window/resolution/grid/row/column"}
205  * == Label ==
206  *
207  * @macro = viewport_description
208  *
209  * List with the label specific variables:
210  * @begin{table}{config}
211  * widget & section & & Holds a single widget like a grid cell.$
212  * @end{table}
213  * @end{tag}{name="widget"}
214  * @end{tag}{name="viewport"}
215  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
216  */
217 
218 namespace implementation
219 {
220 
222  : builder_widget(cfg)
223  , widget_(create_widget_builder(cfg.child("widget", "[viewport]")))
224 {
225 }
226 
228 {
229  return build(replacements_map());
230 }
231 
233 {
234  return viewport::build(*this, replacements);
235 }
236 
237 } // namespace implementation
238 
239 // }------------ END --------------
240 
241 } // namespace gui2
Define the common log macros for the gui toolkit.
Contains the info needed to instantiate a widget.
void set_parent(widget *parent)
Definition: widget.cpp:158
std::vector< char_t > string
virtual void layout_initialize(const bool full_initialization)
How the layout engine works.
Definition: widget.cpp:170
int get_y() const
Definition: widget.cpp:323
Helper to implement private functions without modifying the header.
Definition: viewport.cpp:38
Base class for all widgets.
Definition: widget.hpp:48
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:145
int x
x coordinate.
Definition: point.hpp:44
Generic file dialog.
Definition: field-fwd.hpp:22
widget & widget_
Definition: viewport.hpp:96
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
virtual void place(const point &origin, const point &size)
Places the widget.
Definition: widget.cpp:241
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: viewport.cpp:160
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:469
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:149
void draw_background(surface &frame_buffer, int x_offset, int y_offset)
Draws the background of a widget.
Definition: widget.cpp:373
int get_x() const
Definition: widget.cpp:318
typename const_clone< D, S >::pointer const_clone_ptr
Definition: const_clone.hpp:67
virtual void impl_draw_children(surface &frame_buffer, int x_offset, int y_offset) override
See widget::impl_draw_children.
Definition: viewport.cpp:124
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
virtual void child_populate_dirty_list(window &caller, const std::vector< widget * > &call_stack) override
See widget::child_populate_dirty_list.
Definition: viewport.cpp:138
point get_best_size() const
Gets the best size for the widget.
Definition: widget.cpp:196
virtual iteration::walker_base * create_walker() override
See widget::create_walker.
Definition: viewport.cpp:181
The user set the widget invisible, that means:
window * build(const builder_window::window_resolution *definition)
Builds a window.
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:176
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
void draw_foreground(surface &frame_buffer, int x_offset, int y_offset)
Draws the foreground of the widget.
Definition: widget.cpp:405
visibility get_visible() const
Definition: widget.cpp:506
void draw_children(surface &frame_buffer, int x_offset, int y_offset)
Draws the children of a widget.
Definition: widget.cpp:390
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:93
int y
y coordinate.
Definition: point.hpp:47
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
void populate_dirty_list(window &caller, std::vector< widget * > &call_stack)
Adds a widget to the dirty list if it is dirty.
Definition: widget.cpp:420
static std::deque< std::string > call_stack
Definition: function.cpp:39
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: viewport.cpp:171
Contains the implementation details for lexical_cast and shouldn't be used directly.
bool owns_widget_
Definition: viewport.hpp:98
builder_widget_ptr create_widget_builder(const config &cfg)
Create a widget builder.