The Battle for Wesnoth  1.17.21+dev
viewport.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2023
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 "gui/core/log.hpp"
22 #include "config.hpp"
23 #include "gettext.hpp"
24 #include "utils/const_clone.hpp"
25 #include "wml_exception.hpp"
26 
27 #define LOG_SCOPE_HEADER "viewport [" + id() + "] " + __func__
28 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
29 
30 namespace gui2
31 {
32 
33 // ------------ WIDGET -----------{
34 
35 /**
36  * Helper to implement private functions without modifying the header.
37  *
38  * The class is a helper to avoid recompilation and only has static
39  * functions. It also facilitates to create duplicates of functions for a const
40  * and a non-const member function.
41  */
43 {
44  /**
45  * Implementation for the wrappers for
46  * [const] widget* pane::find_at(const point&, const bool) [const].
47  *
48  * @tparam W A pointer to the pane.
49  */
50  template <class W>
52  find_at(W viewport, point coordinate, const bool must_be_active)
53  {
54 
55  /*
56  * First test whether the mouse is at the pane.
57  */
58  if(viewport->widget::find_at(coordinate, must_be_active) != viewport) {
59  return nullptr;
60  }
61 
62  /*
63  * The widgets are placed at coordinate 0,0 so adjust the offset to
64  * that coordinate system.
65  */
66  coordinate.x -= viewport->get_x();
67  coordinate.y -= viewport->get_y();
68 
69  return viewport->widget_->find_at(coordinate, must_be_active);
70  }
71 
72  template <class W>
74  find(W viewport, const std::string& id, const bool must_be_active)
75  {
76  if(viewport->widget::find(id, must_be_active)) {
77  return viewport;
78  } else {
79  return viewport->widget_->find(id, must_be_active);
80  }
81  }
82 };
83 
85  const builder_widget::replacements_map& replacements)
86  : widget(builder)
87  , widget_(builder.widget_->build(replacements))
88 {
89  widget_->set_parent(this);
90 }
91 
93 {
94 }
95 
96 void viewport::place(const point& origin, const point& size)
97 {
98  widget::place(origin, size);
99 
100  widget_->place(point(), widget_->get_best_size());
101 }
102 
103 void viewport::layout_initialize(const bool full_initialization)
104 {
105  widget::layout_initialize(full_initialization);
106 
107  if(widget_->get_visible() != widget::visibility::invisible) {
108  widget_->layout_initialize(full_initialization);
109  }
110 }
111 
113 {
114  if(widget_->get_visible() != widget::visibility::invisible) {
115  widget_->draw_background();
116  widget_->draw_children();
117  widget_->draw_foreground();
118  }
119 }
120 
121 void viewport::request_reduce_width(const unsigned /*maximum_width*/)
122 {
123 }
124 
125 widget* viewport::find_at(const point& coordinate, const bool must_be_active)
126 {
127  return viewport_implementation::find_at(this, coordinate, must_be_active);
128 }
129 
131  const bool must_be_active) const
132 {
133  return viewport_implementation::find_at(this, coordinate, must_be_active);
134 }
135 
136 widget* viewport::find(const std::string& id, const bool must_be_active)
137 {
138  return viewport_implementation::find(this, id, must_be_active);
139 }
140 
141 const widget* viewport::find(const std::string& id, const bool must_be_active)
142  const
143 {
144  return viewport_implementation::find(this, id, must_be_active);
145 }
146 
148 {
149  return widget_->get_best_size();
150 }
151 
153 {
154  return false;
155 }
156 
158 {
159  /**
160  * @todo Implement properly.
161  */
162  return nullptr;
163 }
164 
165 // }---------- BUILDER -----------{
166 
167 namespace implementation
168 {
169 
171  : builder_widget(cfg)
172  , widget_(create_widget_builder(VALIDATE_WML_CHILD(cfg, "widget", _("Missing [widget] in [viewport]"))))
173 {
174 }
175 
176 std::unique_ptr<widget> builder_viewport::build() const
177 {
178  return build(replacements_map());
179 }
180 
181 std::unique_ptr<widget> builder_viewport::build(const replacements_map& replacements) const
182 {
183  return std::make_unique<viewport>(*this, replacements);
184 }
185 
186 } // namespace implementation
187 
188 // }------------ END --------------
189 
190 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
A viewport is an special widget used to view only a part of the widget it 'holds'.
Definition: viewport.hpp:46
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: viewport.cpp:103
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: viewport.cpp:147
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: viewport.cpp:152
std::unique_ptr< widget > widget_
Definition: viewport.hpp:94
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: viewport.cpp:121
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: viewport.cpp:136
virtual iteration::walker_ptr create_walker() override
See widget::create_walker.
Definition: viewport.cpp:157
virtual void impl_draw_children() override
See widget::impl_draw_children.
Definition: viewport.cpp:112
viewport(const implementation::builder_viewport &builder, const builder_widget::replacements_map &replacements)
Definition: viewport.cpp:84
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: viewport.cpp:96
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: viewport.cpp:125
Base class for all widgets.
Definition: widget.hpp:54
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:168
int get_x() const
Definition: widget.cpp:316
int get_y() const
Definition: widget.cpp:321
@ invisible
The user set the widget invisible, that means:
static std::string _(const char *str)
Definition: gettext.hpp:93
Define the common log macros for the gui toolkit.
void point(int x, int y)
Draw a single point.
Definition: draw.cpp:193
std::unique_ptr< class walker_base > walker_ptr
Definition: widget.hpp:43
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:87
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:176
Helper to implement private functions without modifying the header.
Definition: viewport.cpp:43
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:52
static utils::const_clone_ptr< widget, W > find(W viewport, const std::string &id, const bool must_be_active)
Definition: viewport.cpp:74
Holds a 2D point.
Definition: point.hpp:25
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)