The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
container.hpp
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 #ifndef GUI_WIDGETS_CONTAINER_HPP_INCLUDED
16 #define GUI_WIDGETS_CONTAINER_HPP_INCLUDED
17 
18 #include "gui/widgets/grid.hpp"
19 #include "gui/widgets/control.hpp"
21 
22 namespace gui2
23 {
24 
25 /**
26  * A generic container base class.
27  *
28  * A container is a class build with multiple items either acting as one
29  * widget.
30  *
31  */
32 class tcontainer_ : public tcontrol
33 {
34  friend class tdebug_layout_graph;
35 
36 public:
37  explicit tcontainer_(const unsigned canvas_count)
38  : tcontrol(canvas_count), grid_()
39  {
40  grid_.set_parent(this);
41  }
42 
43  /**
44  * Returns the client rect.
45  *
46  * The client rect is the area which is used for child items. The rest of
47  * the area of this widget is used for its own decoration.
48  *
49  * @returns The client rect.
50  */
51  virtual SDL_Rect get_client_rect() const;
52 
53  /***** ***** ***** ***** layout functions ***** ***** ***** *****/
54 
55  /** See @ref twidget::layout_initialise. */
56  virtual void layout_initialise(const bool full_initialisation) override;
57 
58  /**
59  * Tries to reduce the width of a container.
60  *
61  * See @ref layout_algorithm for more information.
62  *
63  * @param maximum_width The wanted maximum width.
64  */
65  void reduce_width(const unsigned maximum_width);
66 
67  /** See @ref twidget::request_reduce_width. */
68  virtual void request_reduce_width(const unsigned maximum_width) override;
69 
70  /** See @ref twidget::demand_reduce_width. */
71  virtual void demand_reduce_width(const unsigned maximum_width) override;
72 
73  /**
74  * Tries to reduce the height of a container.
75  *
76  * See @ref layout_algorithm for more information.
77  *
78  * @param maximum_height The wanted maximum height.
79  */
80  void reduce_height(const unsigned maximum_height);
81 
82  /** See @ref twidget::request_reduce_height. */
83  virtual void request_reduce_height(const unsigned maximum_height) override;
84 
85  /** See @ref twidget::demand_reduce_height. */
86  virtual void demand_reduce_height(const unsigned maximum_height) override;
87 
88 private:
89  /** See @ref twidget::calculate_best_size. */
90  virtual tpoint calculate_best_size() const override;
91 
92 public:
93  /** See @ref twidget::can_wrap. */
94  virtual bool can_wrap() const override;
95 
96  /** See @ref twidget::place. */
97  virtual void place(const tpoint& origin, const tpoint& size) override;
98 
99  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
100 
101  /** See @ref twidget::has_widget. */
102  virtual bool has_widget(const twidget& widget) const override;
103 
104  /** See @ref twidget::set_origin. */
105  virtual void set_origin(const tpoint& origin) override;
106 
107  /** See @ref twidget::set_visible_rectangle. */
108  virtual void set_visible_rectangle(const SDL_Rect& rectangle) override;
109 
110  /** See @ref twidget::impl_draw_children. */
111  virtual void impl_draw_children(surface& frame_buffer,
112  int x_offset,
113  int y_offset) override;
114 
115 protected:
116  /** See @ref twidget::layout_children. */
117  virtual void layout_children() override;
118 
119  /** See @ref twidget::child_populate_dirty_list. */
120  virtual void
122  const std::vector<twidget*>& call_stack) override;
123 
124 public:
125  /** See @ref twidget::find_at. */
126  virtual twidget* find_at(const tpoint& coordinate,
127  const bool must_be_active) override;
128 
129  /** See @ref twidget::find_at. */
130  virtual const twidget* find_at(const tpoint& coordinate,
131  const bool must_be_active) const override;
132 
133  /** See @ref twidget::find. */
134  twidget* find(const std::string& id, const bool must_be_active) override;
135 
136  /** See @ref twidget::find. */
137  const twidget* find(const std::string& id,
138  const bool must_be_active) const override;
139 
140  /** See @ref tcontrol::set_active. */
141  virtual void set_active(const bool active) override;
142 
143  /** See @ref twidget::disable_click_dismiss. */
144  bool disable_click_dismiss() const override;
145 
146  /**
147  * See @ref twidget::create_walker.
148  *
149  * @todo Implement properly.
150  */
151  virtual iterator::twalker_* create_walker() override
152  {
153  return nullptr;
154  }
155 
156  /**
157  * Initializes and builds the grid.
158  *
159  * This function should only be called upon an empty grid. This grid is
160  * returned by initial_grid();
161  *
162  * @param grid_builder The builder for the grid.
163  */
164  void init_grid(const boost::intrusive_ptr<tbuilder_grid>& grid_builder);
165 
166  /***** **** ***** ***** wrappers to the grid **** ********* *****/
167 
169  {
170  return grid_.begin();
171  }
173  {
174  return grid_.end();
175  }
176 
177  unsigned add_row(const unsigned count = 1)
178  {
179  return grid_.add_row(count);
180  }
181 
182  void set_rows(const unsigned rows)
183  {
184  grid_.set_rows(rows);
185  }
186  unsigned int get_rows() const
187  {
188  return grid_.get_rows();
189  }
190 
191  void set_cols(const unsigned cols)
192  {
193  grid_.set_cols(cols);
194  }
195  unsigned int get_cols() const
196  {
197  return grid_.get_cols();
198  }
199 
200  void set_rows_cols(const unsigned rows, const unsigned cols)
201  {
202  grid_.set_rows_cols(rows, cols);
203  }
204 
205  void set_child(twidget* widget,
206  const unsigned row,
207  const unsigned col,
208  const unsigned flags,
209  const unsigned border_size)
210  {
211  grid_.set_child(widget, row, col, flags, border_size);
212  }
213 
214  void set_row_grow_factor(const unsigned row, const unsigned factor)
215  {
216  grid_.set_row_grow_factor(row, factor);
217  }
218 
219  void set_column_grow_factor(const unsigned column, const unsigned factor)
220  {
221  grid_.set_column_grow_factor(column, factor);
222  }
223 
224 public:
225  /***** ***** ***** setters / getters for members ***** ****** *****/
226 
227  // Public due to the fact that window needs to be able to swap the
228  // children, might be protected again later.
229  const tgrid& grid() const
230  {
231  return grid_;
232  }
234  {
235  return grid_;
236  }
237 
238 private:
239  /** The grid which holds the child objects. */
241 
242  /**
243  * Returns the grid to initialize while building.
244  *
245  * @todo Evaluate whether this function is overridden if not remove.
246  */
247  virtual tgrid& initial_grid()
248  {
249  return grid_;
250  }
251 
252  /** Returns the space used by the border. */
253  virtual tpoint border_space() const;
254 
255  /**
256  * Helper for set_active.
257  *
258  * This function should set the control itself active. It's called by
259  * set_active if the state needs to change. The widget is set to dirty() by
260  * set_active so we only need to change the state.
261  */
262  virtual void set_self_active(const bool active) = 0;
263 };
264 
265 } // namespace gui2
266 
267 #endif
virtual void request_reduce_height(const unsigned maximum_height) override
See twidget::request_reduce_height.
Definition: container.cpp:61
virtual void layout_initialise(const bool full_initialisation) override
See twidget::layout_initialise.
Definition: container.cpp:33
void set_child(twidget *widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
Definition: container.hpp:205
virtual bool has_widget(const twidget &widget) const override
See twidget::has_widget.
Definition: container.cpp:86
const tgrid & grid() const
Definition: container.hpp:229
void set_row_grow_factor(const unsigned row, const unsigned factor)
Sets the grow factor for a row.
Definition: grid.hpp:81
void set_parent(twidget *parent)
Definition: widget.cpp:150
void set_cols(const unsigned cols)
Definition: container.hpp:191
virtual void set_visible_rectangle(const SDL_Rect &rectangle) override
See twidget::set_visible_rectangle.
Definition: container.cpp:132
virtual void set_active(const bool active) override
See tcontrol::set_active.
Definition: container.cpp:188
void set_rows_cols(const unsigned rows, const unsigned cols)
Definition: container.hpp:200
virtual void impl_draw_children(surface &frame_buffer, int x_offset, int y_offset) override
See twidget::impl_draw_children.
Definition: container.cpp:140
Base container class.
Definition: grid.hpp:29
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
unsigned int get_rows() const
Definition: grid.hpp:288
void set_row_grow_factor(const unsigned row, const unsigned factor)
Definition: container.hpp:214
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
virtual void set_origin(const tpoint &origin) override
See twidget::set_origin.
Definition: container.cpp:122
tcontainer_(const unsigned canvas_count)
Definition: container.hpp:37
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
bool disable_click_dismiss() const override
See twidget::disable_click_dismiss.
Definition: container.cpp:203
iterator begin()
Definition: grid.hpp:440
void set_column_grow_factor(const unsigned column, const unsigned factor)
Sets the grow factor for a column.
Definition: grid.hpp:96
void set_rows(const unsigned rows)
Definition: grid.cpp:661
unsigned int get_cols() const
Definition: container.hpp:195
void set_column_grow_factor(const unsigned column, const unsigned factor)
Definition: container.hpp:219
virtual iterator::twalker_ * create_walker() override
See twidget::create_walker.
Definition: container.hpp:151
void init_grid(const boost::intrusive_ptr< tbuilder_grid > &grid_builder)
Initializes and builds the grid.
Definition: container.cpp:209
Iterator for the tchild items.
Definition: grid.hpp:401
virtual void place(const tpoint &origin, const tpoint &size) override
See twidget::place.
Definition: container.cpp:76
void set_rows(const unsigned rows)
Definition: container.hpp:182
GLuint GLuint GLsizei count
Definition: glew.h:1221
cl_event GLbitfield flags
Definition: glew.h:3070
void reduce_width(const unsigned maximum_width)
Tries to reduce the width of a container.
Definition: container.cpp:41
GLenum GLenum GLvoid GLvoid * column
Definition: glew.h:3805
virtual void layout_children() override
See twidget::layout_children.
Definition: container.cpp:150
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
Holds a 2D point.
Definition: point.hpp:24
virtual twidget * find_at(const tpoint &coordinate, const bool must_be_active) override
See twidget::find_at.
Definition: container.cpp:163
unsigned add_row(const unsigned count=1)
Adds a row to end of the grid.
Definition: grid.cpp:58
Base class for all visible items.
Definition: control.hpp:34
A generic container base class.
Definition: container.hpp:32
GLsizeiptr size
Definition: glew.h:1649
void set_rows_cols(const unsigned rows, const unsigned cols)
Wrapper to set_rows and set_cols.
Definition: grid.cpp:679
tgrid::iterator end()
Definition: container.hpp:172
GLenum GLenum GLvoid * row
Definition: glew.h:3805
void reduce_height(const unsigned maximum_height)
Tries to reduce the height of a container.
Definition: container.cpp:56
void set_cols(const unsigned cols)
Definition: grid.cpp:670
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
tgrid::iterator begin()
Definition: container.hpp:168
void set_child(twidget *widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
Sets a child in the grid.
Definition: grid.cpp:69
The walker abstract base class.
Definition: walker.hpp:27
virtual void demand_reduce_width(const unsigned maximum_width) override
See twidget::demand_reduce_width.
Definition: container.cpp:51
unsigned int get_cols() const
Definition: grid.hpp:294
GLsizei const GLcharARB ** string
Definition: glew.h:4503
unsigned add_row(const unsigned count=1)
Definition: container.hpp:177
virtual void demand_reduce_height(const unsigned maximum_height) override
See twidget::demand_reduce_height.
Definition: container.cpp:66
iterator end()
Definition: grid.hpp:444
friend class tdebug_layout_graph
Definition: container.hpp:34