The Battle for Wesnoth  1.15.2+dev
container_base.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 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 
18 
19 #include "gui/core/log.hpp"
20 #include "gui/widgets/window.hpp"
21 
22 #include <algorithm>
23 
24 #define LOG_SCOPE_HEADER \
25  "tcontainer(" + get_control_type() + ") [" + id() + "] " + __func__
26 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
27 
28 namespace gui2
29 {
30 
31 container_base::container_base(const implementation::builder_styled_widget& builder, const std::string& control_type)
32  : styled_widget(builder, control_type)
33  , grid_()
34 {
35  grid_.set_parent(this);
36  connect_signal<event::REQUEST_PLACEMENT>(
37  std::bind(&container_base::clear_layout_size, this),
39 }
40 
42 {
43  return get_rectangle();
44 }
45 
46 void container_base::layout_initialize(const bool full_initialization)
47 {
48  // Inherited.
49  styled_widget::layout_initialize(full_initialization);
50 
52 
53  grid_.layout_initialize(full_initialization);
54 }
55 
56 void container_base::reduce_width(const unsigned maximum_width)
57 {
59  point grid_size = grid_.get_best_size();
60  if(static_cast<int>(maximum_width) - border_space().x < grid_size.x) {
61  grid_.reduce_width(maximum_width - border_space().x);
62  grid_size = grid_.get_best_size();
63  size.x = grid_size.x + border_space().x;
64  size.y = std::max(size.y, grid_size.y + border_space().y);
65  } else {
66  size.x = maximum_width;
67  }
68 
69  set_layout_size(size);
70 }
71 
72 void container_base::request_reduce_width(const unsigned maximum_width)
73 {
75  point grid_size = grid_.get_best_size();
76  if(static_cast<int>(maximum_width) - border_space().x < grid_size.x) {
77  grid_.request_reduce_width(maximum_width - border_space().x);
78  grid_size = grid_.get_best_size();
79  size.x = grid_size.x + border_space().x;
80  size.y = std::max(size.y, grid_size.y + border_space().y);
81  } else {
82  size.x = maximum_width;
83  }
84 
85  set_layout_size(size);
86 }
87 
88 void container_base::demand_reduce_width(const unsigned maximum_width)
89 {
90  grid_.demand_reduce_width(maximum_width - border_space().x);
91 }
92 
93 void container_base::reduce_height(const unsigned maximum_height)
94 {
96  point grid_size = grid_.get_best_size();
97  if(static_cast<int>(maximum_height) - border_space().y < grid_size.y) {
98  grid_.reduce_height(maximum_height - border_space().y);
99  grid_size = grid_.get_best_size();
100  size.y = grid_size.y + border_space().y;
101  } else {
102  size.y = maximum_height;
103  }
104 
105  set_layout_size(size);
106 }
107 
108 void container_base::request_reduce_height(const unsigned maximum_height)
109 {
111  point grid_size = grid_.get_best_size();
112  if(static_cast<int>(maximum_height) - border_space().y < grid_size.y) {
113  grid_.request_reduce_height(maximum_height - border_space().y);
114  grid_size = grid_.get_best_size();
115  size.y = grid_size.y + border_space().y;
116  } else {
117  size.y = maximum_height;
118  }
119 
120  set_layout_size(size);
121 }
122 
123 void container_base::demand_reduce_height(const unsigned maximum_height)
124 {
125  grid_.demand_reduce_height(maximum_height - border_space().y);
126 }
127 
129 {
130  return grid_.can_wrap() || widget::can_wrap();
131 }
132 
133 void container_base::place(const point& origin, const point& size)
134 {
135  styled_widget::place(origin, size);
136 
137  const SDL_Rect rect = get_client_rect();
138  const point client_size(rect.w, rect.h);
139  const point client_position(rect.x, rect.y);
140  grid_.place(client_position, client_size);
141 }
142 
144 {
145  return widget::has_widget(widget) || grid_.has_widget(widget);
146 }
147 
149 {
151 
152  point result(grid_.get_best_size());
153  const point border_size = border_space();
154  const point default_size = get_config_default_size();
155 
156  // If the best size has a value of 0 it's means no limit so don't
157  // add the border_size might set a very small best size.
158  if(result.x) {
159  result.x += border_size.x;
160  }
161  if(default_size.x != 0 && result.x < default_size.x) {
162  result.x = default_size.x;
163  }
164 
165  if(result.y) {
166  result.y += border_size.y;
167  }
168  if(default_size.y != 0 && result.y < default_size.y) {
169  result.y = default_size.y;
170  }
171 
172 
173  DBG_GUI_L << LOG_HEADER << " border size " << border_size << " returning "
174  << result << ".\n";
175 
176  return result;
177 }
178 
180 {
181  // Inherited.
182  widget::set_origin(origin);
183 
184  const SDL_Rect rect = get_client_rect();
185  const point client_position(rect.x, rect.y);
186  grid_.set_origin(client_position);
187 }
188 
189 void container_base::set_visible_rectangle(const SDL_Rect& rectangle)
190 {
191  // Inherited.
193 
194  grid_.set_visible_rectangle(rectangle);
195 }
196 
198  int x_offset,
199  int y_offset)
200 {
203 
204  grid_.draw_children(frame_buffer, x_offset, y_offset);
205 }
206 
208 {
210 }
211 
212 void
214  const std::vector<widget*>& call_stack)
215 {
216  std::vector<widget*> child_call_stack = call_stack;
217  grid_.populate_dirty_list(caller, child_call_stack);
218 }
219 
221  const bool must_be_active)
222 {
223  return grid_.find_at(coordinate, must_be_active);
224 }
225 
227  const bool must_be_active) const
228 {
229  return grid_.find_at(coordinate, must_be_active);
230 }
231 
232 widget* container_base::find(const std::string& id, const bool must_be_active)
233 {
234  widget* result = styled_widget::find(id, must_be_active);
235  return result ? result : grid_.find(id, must_be_active);
236 }
237 
238 const widget* container_base::find(const std::string& id,
239  const bool must_be_active) const
240 {
241  const widget* result = styled_widget::find(id, must_be_active);
242  return result ? result : grid_.find(id, must_be_active);
243 }
244 
245 void container_base::set_active(const bool active)
246 {
247  // Not all our children might have the proper state so let them run
248  // unconditionally.
249  grid_.set_active(active);
250 
251  if(active == get_active()) {
252  return;
253  }
254 
255  set_is_dirty(true);
256 
257  set_self_active(active);
258 }
259 
261 {
263 }
264 
265 void
266 container_base::init_grid(const std::shared_ptr<builder_grid>& grid_builder)
267 {
269 
270  assert(grid_.get_rows() == 0 && grid_.get_cols() == 0);
271 
272  grid_builder->build(&grid_);
273 }
274 
276 {
277  return point();
278 }
279 
281 {
282  for(const auto& lg : config()->linked_groups) {
283  if(!get_window()->has_linked_size_group(lg.id)) {
284  get_window()->init_linked_size_group(lg.id, lg.fixed_width, lg.fixed_height);
285  }
286  }
287 }
288 
289 } // namespace gui2
Define the common log macros for the gui toolkit.
virtual void set_visible_rectangle(const SDL_Rect &rectangle) override
See widget::set_visible_rectangle.
Definition: grid.cpp:603
void reduce_width(const unsigned maximum_width)
Tries to reduce the width of a container.
virtual void child_populate_dirty_list(window &caller, const std::vector< widget *> &call_stack) override
See widget::child_populate_dirty_list.
void set_parent(widget *parent)
Definition: widget.cpp:152
#define DBG_GUI_L
Definition: log.hpp:57
void set_layout_size(const point &size)
Definition: widget.cpp:332
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
virtual void demand_reduce_width(const unsigned maximum_width) override
See widget::demand_reduce_width.
Definition: grid.cpp:271
virtual void place(const point &origin, const point &size) override
See widget::place.
virtual void place(const point &origin, const point &size) override
See widget::place.
virtual bool can_wrap() const override
See widget::can_wrap.
Definition: grid.cpp:466
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: grid.cpp:681
visibility get_visible() const
Definition: widget.cpp:500
virtual bool get_active() const =0
Gets the active state of the styled_widget.
#define LOG_SCOPE_HEADER
unsigned int get_rows() const
Definition: grid.hpp:307
virtual bool has_widget(const widget &widget) const override
See widget::has_widget.
Definition: grid.cpp:666
This file contains the window object, this object is a top level container which has the event manage...
SDL_Rect get_rectangle() const
Gets the bounding rectangle of the widget on the screen.
Definition: widget.cpp:307
Base class for all widgets.
Definition: widget.hpp:47
virtual void set_visible_rectangle(const SDL_Rect &rectangle)
Sets the visible rectangle for a widget.
Definition: widget.cpp:450
lg::log_domain log_gui_layout("gui/layout")
Definition: log.hpp:56
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
virtual void impl_draw_children(surface &frame_buffer, int x_offset, int y_offset) override
See widget::impl_draw_children.
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: grid.cpp:231
virtual point calculate_best_size() const override
See widget::calculate_best_size.
#define LOG_HEADER
int x
x coordinate.
Definition: point.hpp:44
Generic file dialog.
Definition: field-fwd.hpp:22
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: grid.cpp:185
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:414
void reduce_height(const unsigned maximum_height)
Tries to reduce the height of a container.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
point get_best_size() const
Gets the best size for the widget.
Definition: widget.cpp:190
grid grid_
The grid which holds the child objects.
virtual bool has_widget(const widget &widget) const override
See widget::has_widget.
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:463
virtual void set_origin(const point &origin)
Sets the origin of the widget.
Definition: widget.cpp:218
virtual void set_self_active(const bool active)=0
Helper for set_active.
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
virtual bool has_widget(const widget &widget) const
Does the widget contain the widget.
Definition: widget.cpp:592
virtual void set_visible_rectangle(const SDL_Rect &rectangle) override
See widget::set_visible_rectangle.
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:655
unsigned int get_cols() const
Definition: grid.hpp:313
#define log_scope2(domain, description)
Definition: log.hpp:187
void init_linked_size_group(const std::string &id, const bool fixed_width, const bool fixed_height)
Initializes a linked size group.
Definition: window.cpp:833
virtual bool can_wrap() const override
See widget::can_wrap.
void init_grid(const std::shared_ptr< builder_grid > &grid_builder)
Initializes and builds the grid.
container_base(const implementation::builder_styled_widget &builder, const std::string &control_type)
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: grid.cpp:479
Definition: pump.hpp:39
point get_config_default_size() const
Gets the default size as defined in the config.
resolution_definition_ptr config()
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
window * get_window()
Get the parent window.
Definition: widget.cpp:114
void reduce_width(const unsigned maximum_width)
Tries to reduce the width of a container.
Definition: grid.cpp:198
virtual void demand_reduce_width(const unsigned maximum_width) override
See widget::demand_reduce_width.
Holds a 2D point.
Definition: point.hpp:23
void set_active(const bool active)
Activates all children.
Definition: grid.cpp:162
void clear_layout_size()
Throws away layout_size_.
Definition: widget.hpp:452
Base class for all visible items.
virtual void set_origin(const point &origin) override
See widget::set_origin.
Definition: grid.cpp:586
virtual void request_reduce_height(const unsigned maximum_height) override
See widget::request_reduce_height.
Definition: grid.cpp:309
virtual void set_active(const bool active) override
See styled_widget::set_active.
lg::log_domain log_gui_general("gui/general")
Definition: log.hpp:39
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: grid.cpp:642
The user sets the widget visible, that means:
virtual void set_origin(const point &origin) override
See widget::set_origin.
virtual void demand_reduce_height(const unsigned maximum_height) override
See widget::demand_reduce_height.
Definition: grid.cpp:369
virtual void layout_children() override
See widget::layout_children.
Definition: grid.cpp:618
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
virtual void request_reduce_height(const unsigned maximum_height) override
See widget::request_reduce_height.
virtual void layout_children() override
See widget::layout_children.
virtual void demand_reduce_height(const unsigned maximum_height) override
See widget::demand_reduce_height.
void draw_children(surface &frame_buffer, int x_offset, int y_offset)
Draws the children of a widget.
Definition: widget.cpp:384
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
virtual bool can_wrap() const
Can the widget wrap.
Definition: widget.cpp:213
virtual SDL_Rect get_client_rect() const
Returns the client rect.
virtual point border_space() const
Returns the space used by the border.
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 reduce_height(const unsigned maximum_height)
Tries to reduce the height of a container.
Definition: grid.cpp:276
static std::deque< std::string > call_stack
Definition: function.cpp:39