The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
container.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 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 
18 
19 #include "gui/core/log.hpp"
20 
21 #define LOG_SCOPE_HEADER \
22  "tcontainer(" + get_control_type() + ") [" + id() + "] " + __func__
23 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
24 
25 namespace gui2
26 {
27 
29 {
30  return get_rectangle();
31 }
32 
33 void tcontainer_::layout_initialise(const bool full_initialisation)
34 {
35  // Inherited.
36  tcontrol::layout_initialise(full_initialisation);
37 
38  grid_.layout_initialise(full_initialisation);
39 }
40 
41 void tcontainer_::reduce_width(const unsigned maximum_width)
42 {
43  grid_.reduce_width(maximum_width - border_space().x);
44 }
45 
46 void tcontainer_::request_reduce_width(const unsigned maximum_width)
47 {
48  grid_.request_reduce_width(maximum_width - border_space().x);
49 }
50 
51 void tcontainer_::demand_reduce_width(const unsigned maximum_width)
52 {
53  grid_.demand_reduce_width(maximum_width - border_space().x);
54 }
55 
56 void tcontainer_::reduce_height(const unsigned maximum_height)
57 {
58  grid_.reduce_height(maximum_height - border_space().y);
59 }
60 
61 void tcontainer_::request_reduce_height(const unsigned maximum_height)
62 {
63  grid_.request_reduce_height(maximum_height - border_space().y);
64 }
65 
66 void tcontainer_::demand_reduce_height(const unsigned maximum_height)
67 {
68  grid_.demand_reduce_height(maximum_height - border_space().y);
69 }
70 
72 {
73  return grid_.can_wrap() || twidget::can_wrap();
74 }
75 
76 void tcontainer_::place(const tpoint& origin, const tpoint& size)
77 {
78  tcontrol::place(origin, size);
79 
80  const SDL_Rect rect = get_client_rect();
81  const tpoint client_size(rect.w, rect.h);
82  const tpoint client_position(rect.x, rect.y);
83  grid_.place(client_position, client_size);
84 }
85 
86 bool tcontainer_::has_widget(const twidget& widget) const
87 {
88  return twidget::has_widget(widget) || grid_.has_widget(widget);
89 }
90 
92 {
94 
96  const tpoint border_size = border_space();
97  const tpoint minimum_size = get_config_minimum_size();
98 
99  // If the best size has a value of 0 it's means no limit so don't
100  // add the border_size might set a very small best size.
101  if(result.x) {
102  result.x += border_size.x;
103  }
104  if(minimum_size.x != 0 && result.x < minimum_size.x) {
105  result.x = minimum_size.x;
106  }
107 
108  if(result.y) {
109  result.y += border_size.y;
110  }
111  if(minimum_size.y != 0 && result.y < minimum_size.y) {
112  result.y = minimum_size.y;
113  }
114 
115 
116  DBG_GUI_L << LOG_HEADER << " border size " << border_size << " returning "
117  << result << ".\n";
118 
119  return result;
120 }
121 
122 void tcontainer_::set_origin(const tpoint& origin)
123 {
124  // Inherited.
125  twidget::set_origin(origin);
126 
127  const SDL_Rect rect = get_client_rect();
128  const tpoint client_position(rect.x, rect.y);
129  grid_.set_origin(client_position);
130 }
131 
132 void tcontainer_::set_visible_rectangle(const SDL_Rect& rectangle)
133 {
134  // Inherited.
136 
137  grid_.set_visible_rectangle(rectangle);
138 }
139 
141  int x_offset,
142  int y_offset)
143 {
146 
147  grid_.draw_children(frame_buffer, x_offset, y_offset);
148 }
149 
151 {
153 }
154 
155 void
157  const std::vector<twidget*>& call_stack)
158 {
159  std::vector<twidget*> child_call_stack = call_stack;
160  grid_.populate_dirty_list(caller, child_call_stack);
161 }
162 
164  const bool must_be_active)
165 {
166  return grid_.find_at(coordinate, must_be_active);
167 }
168 
169 const twidget* tcontainer_::find_at(const tpoint& coordinate,
170  const bool must_be_active) const
171 {
172  return grid_.find_at(coordinate, must_be_active);
173 }
174 
175 twidget* tcontainer_::find(const std::string& id, const bool must_be_active)
176 {
177  twidget* result = tcontrol::find(id, must_be_active);
178  return result ? result : grid_.find(id, must_be_active);
179 }
180 
182  const bool must_be_active) const
183 {
184  const twidget* result = tcontrol::find(id, must_be_active);
185  return result ? result : grid_.find(id, must_be_active);
186 }
187 
188 void tcontainer_::set_active(const bool active)
189 {
190  // Not all our children might have the proper state so let them run
191  // unconditionally.
192  grid_.set_active(active);
193 
194  if(active == get_active()) {
195  return;
196  }
197 
198  set_is_dirty(true);
199 
200  set_self_active(active);
201 }
202 
204 {
206 }
207 
208 void
210 {
212 
213  assert(initial_grid().get_rows() == 0 && initial_grid().get_cols() == 0);
214 
215  grid_builder->build(&initial_grid());
216 }
217 
219 {
220  return tpoint(0, 0);
221 }
222 
223 } // namespace gui2
virtual void request_reduce_height(const unsigned maximum_height) override
See twidget::request_reduce_height.
Definition: container.cpp:61
Define the common log macros for the gui toolkit.
virtual void layout_initialise(const bool full_initialisation) override
See twidget::layout_initialise.
Definition: container.cpp:33
void reduce_height(const unsigned maximum_height)
Tries to reduce the height of a container.
Definition: grid.cpp:280
virtual bool has_widget(const twidget &widget) const override
See twidget::has_widget.
Definition: container.cpp:86
#define DBG_GUI_L
Definition: log.hpp:58
virtual void demand_reduce_height(const unsigned maximum_height) override
See twidget::demand_reduce_height.
Definition: grid.cpp:366
virtual bool can_wrap() const
Can the widget wrap.
Definition: widget.cpp:211
virtual void set_visible_rectangle(const SDL_Rect &rectangle) override
See twidget::set_visible_rectangle.
Definition: container.cpp:132
virtual void layout_children() override
See twidget::layout_children.
Definition: grid.cpp:575
twidget * find(const std::string &id, const bool must_be_active) override
See twidget::find.
Definition: control.cpp:301
virtual void set_active(const bool active) override
See tcontrol::set_active.
Definition: container.cpp:188
virtual void impl_draw_children(surface &frame_buffer, int x_offset, int y_offset) override
See twidget::impl_draw_children.
Definition: container.cpp:140
tvisible::scoped_enum get_visible() const
Definition: widget.cpp:471
twidget * find(const std::string &id, const bool must_be_active) override
See twidget::find.
Definition: container.cpp:175
tgrid grid_
The grid which holds the child objects.
Definition: container.hpp:240
lg::log_domain log_gui_layout("gui/layout")
Definition: log.hpp:57
virtual void place(const tpoint &origin, const tpoint &size) override
See twidget::place.
Definition: grid.cpp:435
bool disable_click_dismiss() const override
See twidget::disable_click_dismiss.
Definition: grid.cpp:638
virtual void set_self_active(const bool active)=0
Helper for set_active.
virtual void request_reduce_width(const unsigned maximum_width) override
See twidget::request_reduce_width.
Definition: container.cpp:46
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
virtual void set_origin(const tpoint &origin) override
See twidget::set_origin.
Definition: grid.cpp:542
virtual void set_origin(const tpoint &origin) override
See twidget::set_origin.
Definition: container.cpp:122
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:435
tpoint get_best_size() const
Gets the best size for the widget.
Definition: widget.cpp:188
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
virtual bool can_wrap() const override
See twidget::can_wrap.
Definition: container.cpp:71
virtual void request_reduce_height(const unsigned maximum_height) override
See twidget::request_reduce_height.
Definition: grid.cpp:313
GLuint64EXT * result
Definition: glew.h:10727
bool disable_click_dismiss() const override
See twidget::disable_click_dismiss.
Definition: container.cpp:203
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
unsigned int get_cols() const
Definition: container.hpp:195
void draw_children(surface &frame_buffer, int x_offset, int y_offset)
Draws the children of a widget.
Definition: widget.cpp:356
void init_grid(const boost::intrusive_ptr< tbuilder_grid > &grid_builder)
Initializes and builds the grid.
Definition: container.cpp:209
virtual void place(const tpoint &origin, const tpoint &size) override
See twidget::place.
Definition: control.cpp:250
void set_active(const bool active)
Activates all children.
Definition: grid.cpp:168
virtual void request_reduce_width(const unsigned maximum_width) override
See twidget::request_reduce_width.
Definition: grid.cpp:237
virtual void place(const tpoint &origin, const tpoint &size) override
See twidget::place.
Definition: container.cpp:76
#define log_scope2(domain, description)
Definition: log.hpp:186
virtual bool has_widget(const twidget &widget) const override
See twidget::has_widget.
Definition: grid.cpp:623
void reduce_width(const unsigned maximum_width)
Tries to reduce the width of a container.
Definition: container.cpp:41
int x
x coordinate.
Definition: point.hpp:31
virtual void set_visible_rectangle(const SDL_Rect &rectangle) override
See twidget::set_visible_rectangle.
Definition: grid.cpp:560
#define LOG_HEADER
Definition: container.cpp:23
virtual void layout_children() override
See twidget::layout_children.
Definition: container.cpp:150
tpoint get_config_minimum_size() const
Gets the minimum size as defined in the config.
Definition: control.cpp:149
The user sets the widget visible, that means:
Definition: widget.hpp:79
virtual tpoint calculate_best_size() const override
See twidget::calculate_best_size.
Definition: container.cpp:91
virtual void child_populate_dirty_list(twindow &caller, const std::vector< twidget * > &call_stack) override
See twidget::child_populate_dirty_list.
Definition: container.cpp:156
virtual SDL_Rect get_client_rect() const
Returns the client rect.
Definition: container.cpp:28
virtual tgrid & initial_grid()
Returns the grid to initialize while building.
Definition: container.hpp:247
virtual bool has_widget(const twidget &widget) const
Does the widget contain the widget.
Definition: widget.cpp:569
virtual bool can_wrap() const override
See twidget::can_wrap.
Definition: grid.cpp:422
Holds a 2D point.
Definition: point.hpp:24
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
virtual void demand_reduce_width(const unsigned maximum_width) override
See twidget::demand_reduce_width.
Definition: grid.cpp:275
virtual twidget * find_at(const tpoint &coordinate, const bool must_be_active) override
See twidget::find_at.
Definition: container.cpp:163
virtual void set_visible_rectangle(const SDL_Rect &rectangle)
Sets the visible rectangle for a widget.
Definition: widget.cpp:422
lg::log_domain log_gui_general("gui/general")
Definition: log.hpp:40
bool disable_click_dismiss() const override
See twidget::disable_click_dismiss.
Definition: control.cpp:139
GLsizeiptr size
Definition: glew.h:1649
virtual void set_origin(const tpoint &origin)
Sets the origin of the widget.
Definition: widget.cpp:216
void reduce_height(const unsigned maximum_height)
Tries to reduce the height of a container.
Definition: container.cpp:56
unsigned int get_rows() const
Definition: container.hpp:186
virtual tpoint border_space() const
Returns the space used by the border.
Definition: container.cpp:218
Base class for all widgets.
Definition: widget.hpp:49
virtual void layout_initialise(const bool full_initialisation) override
See twidget::layout_initialise.
Definition: grid.cpp:191
twidget * find(const std::string &id, const bool must_be_active) override
See twidget::find.
Definition: grid.cpp:612
virtual void demand_reduce_width(const unsigned maximum_width) override
See twidget::demand_reduce_width.
Definition: container.cpp:51
void reduce_width(const unsigned maximum_width)
Tries to reduce the width of a container.
Definition: grid.cpp:204
SDL_Rect get_rectangle() const
Gets the bounding rectangle of the widget on the screen.
Definition: widget.cpp:279
virtual bool get_active() const =0
Gets the active state of the control.
virtual void layout_initialise(const bool full_initialisation) override
See twidget::layout_initialise.
Definition: control.cpp:194
GLsizei const GLcharARB ** string
Definition: glew.h:4503
virtual twidget * find_at(const tpoint &coordinate, const bool must_be_active) override
See twidget::find_at.
Definition: grid.cpp:599
virtual void demand_reduce_height(const unsigned maximum_height) override
See twidget::demand_reduce_height.
Definition: container.cpp:66
#define LOG_SCOPE_HEADER
Definition: container.cpp:21