The Battle for Wesnoth  1.13.4+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 - 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 
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 "tviewport [" + 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] twidget* tpane::find_at(const tpoint&, const bool) [const].
43  *
44  * @tparam W A pointer to the pane.
45  */
46  template <class W>
48  find_at(W viewport, tpoint coordinate, const bool must_be_active)
49  {
50 
51  /*
52  * First test whether the mouse is at the pane.
53  */
54  if(viewport->twidget::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->twidget::find(id, must_be_active)) {
73  return viewport;
74  } else {
75  return viewport->widget_.find(id, must_be_active);
76  }
77  }
78 };
79 
80 tviewport::tviewport(twidget& widget) : widget_(widget), owns_widget_(false)
81 {
82  widget_.set_parent(this);
83 }
84 
86  const tbuilder_widget::treplacements& replacements)
87  : twidget(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 tbuilder_widget::treplacements& replacements)
103 {
104  return new tviewport(builder, replacements);
105 }
106 
107 void tviewport::place(const tpoint& origin, const tpoint& size)
108 {
109  twidget::place(origin, size);
110 
112 }
113 
114 void tviewport::layout_initialise(const bool full_initialisation)
115 {
116  twidget::layout_initialise(full_initialisation);
117 
119  widget_.layout_initialise(full_initialisation);
120  }
121 }
122 
123 void
124 tviewport::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<twidget*>& call_stack)
140 {
141  std::vector<twidget*> child_call_stack = call_stack;
142  widget_.populate_dirty_list(caller, child_call_stack);
143 }
144 
145 void tviewport::request_reduce_width(const unsigned /*maximum_width*/)
146 {
147 }
148 
149 twidget* tviewport::find_at(const tpoint& coordinate, const bool must_be_active)
150 {
151  return tviewport_implementation::find_at(this, coordinate, must_be_active);
152 }
153 
154 const twidget* tviewport::find_at(const tpoint& coordinate,
155  const bool must_be_active) const
156 {
157  return tviewport_implementation::find_at(this, coordinate, must_be_active);
158 }
159 
160 twidget* tviewport::find(const std::string& id, const bool must_be_active)
161 {
162  return tviewport_implementation::find(this, id, must_be_active);
163 }
164 
165 const twidget* tviewport::find(const std::string& id, const bool must_be_active)
166  const
167 {
168  return tviewport_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  : tbuilder_widget(cfg)
223  , widget(create_builder_widget(cfg.child("widget", "[viewport]")))
224 {
225 }
226 
228 {
229  return build(treplacements());
230 }
231 
232 twidget* tbuilder_viewport::build(const treplacements& replacements) const
233 {
234  return tviewport::build(*this, replacements);
235 }
236 
237 } // namespace implementation
238 
239 // }------------ END --------------
240 
241 } // namespace gui2
virtual void child_populate_dirty_list(twindow &caller, const std::vector< twidget * > &call_stack) override
See twidget::child_populate_dirty_list.
Definition: viewport.cpp:138
Define the common log macros for the gui toolkit.
void draw_foreground(surface &frame_buffer, int x_offset, int y_offset)
Draws the foreground of the widget.
Definition: widget.cpp:371
virtual tpoint calculate_best_size() const override
See twidget::calculate_best_size.
Definition: viewport.cpp:171
virtual void request_reduce_width(const unsigned maximum_width) override
See twidget::request_reduce_width.
Definition: viewport.cpp:145
void set_parent(twidget *parent)
Definition: widget.cpp:150
twindow * build(CVideo &video, const twindow_builder::tresolution *definition)
Builds a window.
Contains the info needed to instantiate a widget.
virtual void impl_draw_children(surface &frame_buffer, int x_offset, int y_offset) override
See twidget::impl_draw_children.
Definition: viewport.cpp:124
tvisible::scoped_enum get_visible() const
Definition: widget.cpp:471
virtual iterator::twalker_ * create_walker() override
See twidget::create_walker.
Definition: viewport.cpp:181
int get_y() const
Definition: widget.cpp:289
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:435
tviewport(twidget &widget)
Definition: viewport.cpp:80
Definitions for the interface to Wesnoth Markup Language (WML).
tpoint get_best_size() const
Gets the best size for the widget.
Definition: widget.cpp:188
virtual void layout_initialise(const bool full_initialisation)
How the layout engine works.
Definition: widget.cpp:162
static utils::tconst_clone< twidget, W >::pointer find_at(W viewport, tpoint coordinate, const bool must_be_active)
Implementation for the wrappers for [const] twidget* tpane::find_at(const tpoint&, const bool) [const].
Definition: viewport.cpp:48
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
Helper to implement private functions without modifying the header.
Definition: viewport.cpp:38
static utils::tconst_clone< twidget, W >::pointer find(W viewport, const std::string &id, const bool must_be_active)
Definition: viewport.cpp:70
The user set the widget invisible, that means:
Definition: widget.hpp:103
twidget & widget_
Definition: viewport.hpp:97
bool disable_click_dismiss() const override
See twidget::disable_click_dismiss.
Definition: viewport.cpp:176
virtual void place(const tpoint &origin, const tpoint &size) override
See twidget::place.
Definition: viewport.cpp:107
int y
y coordinate.
Definition: point.hpp:34
void populate_dirty_list(twindow &caller, std::vector< twidget * > &call_stack)
Adds a widget to the dirty list if it is dirty.
Definition: widget.cpp:386
void draw_children(surface &frame_buffer, int x_offset, int y_offset)
Draws the children of a widget.
Definition: widget.cpp:356
int get_x() const
Definition: widget.cpp:284
virtual void place(const tpoint &origin, const tpoint &size)
Places the widget.
Definition: widget.cpp:233
void draw_background(surface &frame_buffer, int x_offset, int y_offset)
Draws the background of a widget.
Definition: widget.cpp:339
int x
x coordinate.
Definition: point.hpp:31
virtual void layout_initialise(const bool full_initialisation) override
See twidget::layout_initialise.
Definition: viewport.cpp:114
D * pointer
A pointer to the destination type, possibly const qualified.
Definition: const_clone.hpp:56
twidget * find(const std::string &id, const bool must_be_active) override
See twidget::find.
Definition: viewport.cpp:160
Holds a 2D point.
Definition: point.hpp:24
tbuilder_widget_ptr create_builder_widget(const config &cfg)
Create a widget builder.
GLsizeiptr size
Definition: glew.h:1649
static tviewport * build(const implementation::tbuilder_viewport &builder, const tbuilder_widget::treplacements &replacements)
Definition: viewport.cpp:101
Base class for all widgets.
Definition: widget.hpp:49
The walker abstract base class.
Definition: walker.hpp:27
std::map< std::string, boost::intrusive_ptr< tbuilder_widget > > treplacements
The replacements type is used to define replacement types.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
virtual twidget * find_at(const tpoint &coordinate, const bool must_be_active) override
See twidget::find_at.
Definition: viewport.cpp:149
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Contains the implementation details for lexical_cast and shouldn't be used directly.