The Battle for Wesnoth  1.15.9+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 
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 
81  const builder_widget::replacements_map& replacements)
82  : widget(builder)
83  , widget_(*builder.widget_->build(replacements))
84  , owns_widget_(true)
85 {
86  widget_.set_parent(this);
87 }
88 
90 {
91  if(owns_widget_) {
92  delete &widget_;
93  }
94 }
95 
97  const builder_widget::replacements_map& replacements)
98 {
99  return new viewport(builder, replacements);
100 }
101 
102 void viewport::place(const point& origin, const point& size)
103 {
104  widget::place(origin, size);
105 
107 }
108 
109 void viewport::layout_initialize(const bool full_initialization)
110 {
111  widget::layout_initialize(full_initialization);
112 
114  widget_.layout_initialize(full_initialization);
115  }
116 }
117 
118 void
119 viewport::impl_draw_children(surface& frame_buffer, int x_offset, int y_offset)
120 {
121  x_offset += get_x();
122  y_offset += get_y();
123 
125  widget_.draw_background(frame_buffer, x_offset, y_offset);
126  widget_.draw_children(frame_buffer, x_offset, y_offset);
127  widget_.draw_foreground(frame_buffer, x_offset, y_offset);
128  widget_.set_is_dirty(false);
129  }
130 }
131 
132 void
134  const std::vector<widget*>& call_stack)
135 {
136  std::vector<widget*> child_call_stack = call_stack;
137  widget_.populate_dirty_list(caller, child_call_stack);
138 }
139 
140 void viewport::request_reduce_width(const unsigned /*maximum_width*/)
141 {
142 }
143 
144 widget* viewport::find_at(const point& coordinate, const bool must_be_active)
145 {
146  return viewport_implementation::find_at(this, coordinate, must_be_active);
147 }
148 
150  const bool must_be_active) const
151 {
152  return viewport_implementation::find_at(this, coordinate, must_be_active);
153 }
154 
155 widget* viewport::find(const std::string& id, const bool must_be_active)
156 {
157  return viewport_implementation::find(this, id, must_be_active);
158 }
159 
160 const widget* viewport::find(const std::string& id, const bool must_be_active)
161  const
162 {
163  return viewport_implementation::find(this, id, must_be_active);
164 }
165 
167 {
168  return widget_.get_best_size();
169 }
170 
172 {
173  return false;
174 }
175 
177 {
178  /**
179  * @todo Implement properly.
180  */
181  return nullptr;
182 }
183 
184 // }---------- BUILDER -----------{
185 
186 namespace implementation
187 {
188 
189 builder_viewport::builder_viewport(const config& cfg)
190  : builder_widget(cfg)
191  , widget_(create_widget_builder(cfg.child("widget", "[viewport]")))
192 {
193 }
194 
196 {
197  return build(replacements_map());
198 }
199 
201 {
202  return viewport::build(*this, replacements);
203 }
204 
205 } // namespace implementation
206 
207 // }------------ END --------------
208 
209 } // 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:314
void set_parent(widget *parent)
Definition: widget.cpp:154
virtual void layout_initialize(const bool full_initialization)
How the layout engine works.
Definition: widget.cpp:166
visibility get_visible() const
Definition: widget.cpp:502
Helper to implement private functions without modifying the header.
Definition: viewport.cpp:38
Base class for all widgets.
Definition: widget.hpp:49
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:140
int x
x coordinate.
Definition: point.hpp:44
Generic file dialog.
Definition: field-fwd.hpp:22
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:416
widget & widget_
Definition: viewport.hpp:104
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:192
virtual void place(const point &origin, const point &size)
Places the widget.
Definition: widget.cpp:237
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: viewport.cpp:155
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:465
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: viewport.cpp:102
static viewport * build(const implementation::builder_viewport &builder, const builder_widget::replacements_map &replacements)
Definition: viewport.cpp:96
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:144
void draw_background(surface &frame_buffer, int x_offset, int y_offset)
Draws the background of a widget.
Definition: widget.cpp:369
int get_y() const
Definition: widget.cpp:319
typename const_clone< D, S >::pointer const_clone_ptr
Definition: const_clone.hpp:65
virtual void impl_draw_children(surface &frame_buffer, int x_offset, int y_offset) override
See widget::impl_draw_children.
Definition: viewport.cpp:119
A viewport is an special widget used to view only a part of the widget it &#39;holds&#39;.
Definition: viewport.hpp:44
virtual iteration::walker_base * create_walker() override
See widget::create_walker.
Definition: viewport.cpp:176
The user set the widget invisible, that means:
viewport(const implementation::builder_viewport &builder, const builder_widget::replacements_map &replacements)
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:109
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: viewport.cpp:171
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
virtual void child_populate_dirty_list(window &caller, const std::vector< widget *> &call_stack) override
See widget::child_populate_dirty_list.
Definition: viewport.cpp:133
void draw_foreground(surface &frame_buffer, int x_offset, int y_offset)
Draws the foreground of the widget.
Definition: widget.cpp:401
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
void draw_children(surface &frame_buffer, int x_offset, int y_offset)
Draws the children of a widget.
Definition: widget.cpp:386
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:60
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:64
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:166
virtual widget * build() const override
Definition: viewport.cpp:195
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.