The Battle for Wesnoth  1.19.0-dev
viewport.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2024
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
18 #include "gui/widgets/viewport.hpp"
19 
21 #include "gettext.hpp"
22 #include "utils/const_clone.hpp"
23 #include "wml_exception.hpp"
24 
25 #define LOG_SCOPE_HEADER "viewport [" + id() + "] " + __func__
26 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
27 
28 namespace gui2
29 {
30 
31 // ------------ WIDGET -----------{
32 
33 /**
34  * Helper to implement private functions without modifying the header.
35  *
36  * The class is a helper to avoid recompilation and only has static
37  * functions. It also facilitates to create duplicates of functions for a const
38  * and a non-const member function.
39  */
41 {
42  /**
43  * Implementation for the wrappers for
44  * [const] widget* pane::find_at(const point&, const bool) [const].
45  *
46  * @tparam W A pointer to the pane.
47  */
48  template <class W>
50  find_at(W viewport, point coordinate, const bool must_be_active)
51  {
52 
53  /*
54  * First test whether the mouse is at the pane.
55  */
56  if(viewport->widget::find_at(coordinate, must_be_active) != viewport) {
57  return nullptr;
58  }
59 
60  /*
61  * The widgets are placed at coordinate 0,0 so adjust the offset to
62  * that coordinate system.
63  */
64  coordinate.x -= viewport->get_x();
65  coordinate.y -= viewport->get_y();
66 
67  return viewport->widget_->find_at(coordinate, must_be_active);
68  }
69 
70  template <class W>
72  find(W viewport, const std::string& id, const bool must_be_active)
73  {
74  if(viewport->widget::find(id, must_be_active)) {
75  return viewport;
76  } else {
77  return viewport->widget_->find(id, must_be_active);
78  }
79  }
80 };
81 
83  const builder_widget::replacements_map& replacements)
84  : widget(builder)
85  , widget_(builder.widget_->build(replacements))
86 {
87  widget_->set_parent(this);
88 }
89 
91 {
92 }
93 
94 void viewport::place(const point& origin, const point& size)
95 {
96  widget::place(origin, size);
97 
98  widget_->place(point(), widget_->get_best_size());
99 }
100 
101 void viewport::layout_initialize(const bool full_initialization)
102 {
103  widget::layout_initialize(full_initialization);
104 
105  if(widget_->get_visible() != widget::visibility::invisible) {
106  widget_->layout_initialize(full_initialization);
107  }
108 }
109 
111 {
112  if(widget_->get_visible() != widget::visibility::invisible) {
113  widget_->draw_background();
114  widget_->draw_children();
115  widget_->draw_foreground();
116  }
117 }
118 
119 void viewport::request_reduce_width(const unsigned /*maximum_width*/)
120 {
121 }
122 
123 widget* viewport::find_at(const point& coordinate, const bool must_be_active)
124 {
125  return viewport_implementation::find_at(this, coordinate, must_be_active);
126 }
127 
129  const bool must_be_active) const
130 {
131  return viewport_implementation::find_at(this, coordinate, must_be_active);
132 }
133 
134 widget* viewport::find(const std::string& id, const bool must_be_active)
135 {
136  return viewport_implementation::find(this, id, must_be_active);
137 }
138 
139 const widget* viewport::find(const std::string& id, const bool must_be_active)
140  const
141 {
142  return viewport_implementation::find(this, id, must_be_active);
143 }
144 
146 {
147  return widget_->get_best_size();
148 }
149 
151 {
152  return false;
153 }
154 
156 {
157  /**
158  * @todo Implement properly.
159  */
160  return nullptr;
161 }
162 
163 // }---------- BUILDER -----------{
164 
165 namespace implementation
166 {
167 
169  : builder_widget(cfg)
170  , widget_(create_widget_builder(VALIDATE_WML_CHILD(cfg, "widget", missing_mandatory_wml_tag("viewport", "widget"))))
171 {
172 }
173 
174 std::unique_ptr<widget> builder_viewport::build() const
175 {
176  return build(replacements_map());
177 }
178 
179 std::unique_ptr<widget> builder_viewport::build(const replacements_map& replacements) const
180 {
181  return std::make_unique<viewport>(*this, replacements);
182 }
183 
184 } // namespace implementation
185 
186 // }------------ END --------------
187 
188 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
A viewport is an special widget used to view only a part of the widget it 'holds'.
Definition: viewport.hpp:45
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: viewport.cpp:101
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: viewport.cpp:145
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: viewport.cpp:150
std::unique_ptr< widget > widget_
Definition: viewport.hpp:93
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: viewport.cpp:119
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: viewport.cpp:134
virtual iteration::walker_ptr create_walker() override
See widget::create_walker.
Definition: viewport.cpp:155
virtual void impl_draw_children() override
See widget::impl_draw_children.
Definition: viewport.cpp:110
viewport(const implementation::builder_viewport &builder, const builder_widget::replacements_map &replacements)
Definition: viewport.cpp:82
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: viewport.cpp:94
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: viewport.cpp:123
Base class for all widgets.
Definition: widget.hpp:53
virtual void place(const point &origin, const point &size)
Places the widget.
Definition: widget.cpp:239
virtual void layout_initialize(const bool full_initialization)
How the layout engine works.
Definition: widget.cpp:167
int get_x() const
Definition: widget.cpp:317
int get_y() const
Definition: widget.cpp:322
@ invisible
The user set the widget invisible, that means:
void point(int x, int y)
Draw a single point.
Definition: draw.cpp:202
std::unique_ptr< class walker_base > walker_ptr
Definition: widget.hpp:42
Generic file dialog.
builder_widget_ptr create_widget_builder(const config &cfg)
Create a widget builder.
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
Contains the implementation details for lexical_cast and shouldn't be used directly.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
typename const_clone< D, S >::pointer const_clone_ptr
Definition: const_clone.hpp:66
Contains the info needed to instantiate a widget.
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
virtual std::unique_ptr< widget > build() const override
Definition: viewport.cpp:174
Helper to implement private functions without modifying the header.
Definition: viewport.cpp:41
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:50
static utils::const_clone_ptr< widget, W > find(W viewport, const std::string &id, const bool must_be_active)
Definition: viewport.cpp:72
Holds a 2D point.
Definition: point.hpp:25
std::string missing_mandatory_wml_tag(const std::string &section, const std::string &tag)
Returns a standard message for a missing wml child (tag).
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE_WML_CHILD(cfg, key, message)