The Battle for Wesnoth  1.17.0-dev
container_base.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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 #pragma once
17 
18 #include "gui/widgets/grid.hpp"
21 
22 namespace gui2
23 {
24 namespace implementation
25 {
26 struct builder_styled_widget;
27 }
28 
29 /**
30  * A generic container base class.
31  *
32  * A container is a class build with multiple items either acting as one
33  * widget.
34  *
35  */
37 {
38  friend class debug_layout_graph;
39 
40 public:
41  explicit container_base(const implementation::builder_styled_widget& builder, const std::string& control_type);
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 widget::layout_initialize. */
56  virtual void layout_initialize(const bool full_initialization) 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 widget::request_reduce_width. */
68  virtual void request_reduce_width(const unsigned maximum_width) override;
69 
70  /** See @ref widget::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 widget::request_reduce_height. */
83  virtual void request_reduce_height(const unsigned maximum_height) override;
84 
85  /** See @ref widget::demand_reduce_height. */
86  virtual void demand_reduce_height(const unsigned maximum_height) override;
87 
88 protected:
89  /** See @ref widget::calculate_best_size. */
90  virtual point calculate_best_size() const override;
91 
92 public:
93  /** See @ref widget::can_wrap. */
94  virtual bool can_wrap() const override;
95 
96  /** See @ref widget::place. */
97  virtual void place(const point& origin, const point& size) override;
98 
99  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
100 
101  /** See @ref widget::has_widget. */
102  virtual bool has_widget(const widget& widget) const override;
103 
104  /** See @ref widget::set_origin. */
105  virtual void set_origin(const point& origin) override;
106 
107  /** See @ref widget::set_visible_rectangle. */
108  virtual void set_visible_rectangle(const SDL_Rect& rectangle) override;
109 
110  /** See @ref widget::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 widget::layout_children. */
117  virtual void layout_children() override;
118 
119  /** See @ref widget::child_populate_dirty_list. */
120  virtual void
121  child_populate_dirty_list(window& caller,
122  const std::vector<widget*>& call_stack) override;
123 
124 public:
125  /** See @ref widget::find_at. */
126  virtual widget* find_at(const point& coordinate,
127  const bool must_be_active) override;
128 
129  /** See @ref widget::find_at. */
130  virtual const widget* find_at(const point& coordinate,
131  const bool must_be_active) const override;
132 
133  /** See @ref widget::find. */
134  widget* find(const std::string& id, const bool must_be_active) override;
135 
136  /** See @ref widget::find. */
137  const widget* find(const std::string& id,
138  const bool must_be_active) const override;
139 
140  /** See @ref styled_widget::set_active. */
141  virtual void set_active(const bool active) override;
142 
143  /** See @ref widget::disable_click_dismiss. */
144  bool disable_click_dismiss() const override;
145 
146  /**
147  * See @ref widget::create_walker.
148  *
149  * @todo Implement properly.
150  */
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.
160  *
161  * @param grid_builder The builder for the grid.
162  */
163  void init_grid(const builder_grid& grid_builder);
164 
165  /***** **** ***** ***** wrappers to the grid **** ********* *****/
166 
168  {
169  return grid_.begin();
170  }
172  {
173  return grid_.end();
174  }
175 
176  unsigned add_row(const unsigned count = 1)
177  {
178  return grid_.add_row(count);
179  }
180 
181  void set_rows(const unsigned rows)
182  {
183  grid_.set_rows(rows);
184  }
185  unsigned int get_rows() const
186  {
187  return grid_.get_rows();
188  }
189 
190  void set_cols(const unsigned cols)
191  {
192  grid_.set_cols(cols);
193  }
194  unsigned int get_cols() const
195  {
196  return grid_.get_cols();
197  }
198 
199  void set_rows_cols(const unsigned rows, const unsigned cols)
200  {
201  grid_.set_rows_cols(rows, cols);
202  }
203 
205  const unsigned row,
206  const unsigned col,
207  const unsigned flags,
208  const unsigned border_size)
209  {
210  grid_.set_child(widget, row, col, flags, border_size);
211  }
212 
213  void set_row_grow_factor(const unsigned row, const unsigned factor)
214  {
215  grid_.set_row_grow_factor(row, factor);
216  }
217 
218  void set_column_grow_factor(const unsigned column, const unsigned factor)
219  {
220  grid_.set_column_grow_factor(column, factor);
221  }
222 
223 public:
224  /***** ***** ***** setters / getters for members ***** ****** *****/
225 
226  // Public due to the fact that window needs to be able to swap the
227  // children, might be protected again later.
228  const grid& get_grid() const
229  {
230  return grid_;
231  }
233  {
234  return grid_;
235  }
236 
237 private:
238  /** The grid which holds the child objects. */
240 
241  /** Returns the space used by the border. */
242  virtual point border_space() const;
243 
244  /**
245  * Helper for set_active.
246  *
247  * This function should set the styled_widget itself active. It's called by
248  * set_active if the state needs to change. The widget is set to dirty() by
249  * set_active so we only need to change the state.
250  */
251  virtual void set_self_active(const bool active) = 0;
252 
253  void inject_linked_groups();
254 };
255 
256 } // namespace gui2
grid::iterator end()
const grid & get_grid() const
void set_row_grow_factor(const unsigned row, const unsigned factor)
Base class for all widgets.
Definition: widget.hpp:49
void set_column_grow_factor(const unsigned column, const unsigned factor)
Generic file dialog.
Definition: field-fwd.hpp:23
void set_child(widget *widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
Base container class.
Definition: grid.hpp:31
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
grid grid_
The grid which holds the child objects.
unsigned int get_rows() const
The walker abstract base class.
Definition: walker.hpp:27
Iterator for the child items.
Definition: grid.hpp:441
static thread_local std::deque< std::string > call_stack
For printing error messages when WFL parsing or evaluation fails, this contains the names of the WFL ...
Definition: function.cpp:47
unsigned add_row(const unsigned count=1)
grid::iterator begin()
unsigned int get_cols() const
void set_cols(const unsigned cols)
Holds a 2D point.
Definition: point.hpp:24
Base class for all visible items.
A generic container base class.
virtual iteration::walker_base * create_walker() override
See widget::create_walker.
void set_rows_cols(const unsigned rows, const unsigned cols)
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
void set_rows(const unsigned rows)