The Battle for Wesnoth  1.19.0-dev
container_base.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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"
20 
21 namespace gui2
22 {
23 
24 /**
25  * A generic container base class.
26  *
27  * A container is a class build with multiple items either acting as one
28  * widget.
29  *
30  */
32 {
33  friend class debug_layout_graph;
34 
35 public:
36  explicit container_base(const implementation::builder_styled_widget& builder, const std::string& control_type);
37 
38  /**
39  * Returns the client rect.
40  *
41  * The client rect is the area which is used for child items. The rest of
42  * the area of this widget is used for its own decoration.
43  *
44  * @returns The client rect.
45  */
46  virtual SDL_Rect get_client_rect() const;
47 
48  /***** ***** ***** ***** layout functions ***** ***** ***** *****/
49 
50  /** See @ref widget::layout_initialize. */
51  virtual void layout_initialize(const bool full_initialization) override;
52 
53  /**
54  * Tries to reduce the width of a container.
55  *
56  * See @ref layout_algorithm for more information.
57  *
58  * @param maximum_width The wanted maximum width.
59  */
60  void reduce_width(const unsigned maximum_width);
61 
62  /** See @ref widget::request_reduce_width. */
63  virtual void request_reduce_width(const unsigned maximum_width) override;
64 
65  /** See @ref widget::demand_reduce_width. */
66  virtual void demand_reduce_width(const unsigned maximum_width) override;
67 
68  /**
69  * Tries to reduce the height of a container.
70  *
71  * See @ref layout_algorithm for more information.
72  *
73  * @param maximum_height The wanted maximum height.
74  */
75  void reduce_height(const unsigned maximum_height);
76 
77  /** See @ref widget::request_reduce_height. */
78  virtual void request_reduce_height(const unsigned maximum_height) override;
79 
80  /** See @ref widget::demand_reduce_height. */
81  virtual void demand_reduce_height(const unsigned maximum_height) override;
82 
83 protected:
84  /** See @ref widget::calculate_best_size. */
85  virtual point calculate_best_size() const override;
86 
87 public:
88  /** See @ref widget::can_wrap. */
89  virtual bool can_wrap() const override;
90 
91  /** See @ref widget::place. */
92  virtual void place(const point& origin, const point& size) override;
93 
94  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
95 
96  /** See @ref widget::has_widget. */
97  virtual bool has_widget(const widget& widget) const override;
98 
99  /** See @ref widget::set_origin. */
100  virtual void set_origin(const point& origin) override;
101 
102  /** See @ref widget::set_visible_rectangle. */
103  virtual void set_visible_rectangle(const SDL_Rect& rectangle) override;
104 
105  /** See @ref widget::impl_draw_children. */
106  virtual void impl_draw_children() override;
107 
108 protected:
109  /** See @ref widget::layout_children. */
110  virtual void layout_children() override;
111 
112 public:
113  /** See @ref widget::find_at. */
114  virtual widget* find_at(const point& coordinate,
115  const bool must_be_active) override;
116 
117  /** See @ref widget::find_at. */
118  virtual const widget* find_at(const point& coordinate,
119  const bool must_be_active) const override;
120 
121  /** See @ref widget::find. */
122  widget* find(const std::string& id, const bool must_be_active) override;
123 
124  /** See @ref widget::find. */
125  const widget* find(const std::string& id,
126  const bool must_be_active) const override;
127 
128  /** See @ref styled_widget::set_active. */
129  virtual void set_active(const bool active) override;
130 
131  /** See @ref widget::disable_click_dismiss. */
132  bool disable_click_dismiss() const override;
133 
134  /**
135  * See @ref widget::create_walker.
136  */
137  virtual iteration::walker_ptr create_walker() override;
138 
139  /**
140  * Initializes and builds the grid.
141  *
142  * This function should only be called upon an empty grid.
143  *
144  * @param grid_builder The builder for the grid.
145  */
146  void init_grid(const builder_grid& grid_builder);
147 
148  /***** **** ***** ***** wrappers to the grid **** ********* *****/
149 
151  {
152  return grid_.begin();
153  }
155  {
156  return grid_.end();
157  }
158 
159  unsigned add_row(const unsigned count = 1)
160  {
161  return grid_.add_row(count);
162  }
163 
164  void set_rows(const unsigned rows)
165  {
166  grid_.set_rows(rows);
167  }
168  unsigned int get_rows() const
169  {
170  return grid_.get_rows();
171  }
172 
173  void set_cols(const unsigned cols)
174  {
175  grid_.set_cols(cols);
176  }
177  unsigned int get_cols() const
178  {
179  return grid_.get_cols();
180  }
181 
182  void set_rows_cols(const unsigned rows, const unsigned cols)
183  {
184  grid_.set_rows_cols(rows, cols);
185  }
186 
187  void set_child(std::unique_ptr<widget> widget,
188  const unsigned row,
189  const unsigned col,
190  const unsigned flags,
191  const unsigned border_size)
192  {
193  grid_.set_child(std::move(widget), row, col, flags, border_size);
194  }
195 
196  void set_row_grow_factor(const unsigned row, const unsigned factor)
197  {
198  grid_.set_row_grow_factor(row, factor);
199  }
200 
201  void set_column_grow_factor(const unsigned column, const unsigned factor)
202  {
203  grid_.set_column_grow_factor(column, factor);
204  }
205 
206 public:
207  /***** ***** ***** setters / getters for members ***** ****** *****/
208 
209  // Public due to the fact that window needs to be able to swap the
210  // children, might be protected again later.
211  const grid& get_grid() const
212  {
213  return grid_;
214  }
216  {
217  return grid_;
218  }
219 
220 private:
221  /** The grid which holds the child objects. */
223 
224  /** Returns the space used by the border. */
225  virtual point border_space() const;
226 
227  /**
228  * Helper for set_active.
229  *
230  * This function should set the styled_widget itself active. It's called by
231  * set_active if the state needs to change. The widget is set to dirty() by
232  * set_active so we only need to change the state.
233  */
234  virtual void set_self_active(const bool active) = 0;
235 
236  void inject_linked_groups();
237 };
238 
239 } // namespace gui2
A generic container base class.
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
void reduce_height(const unsigned maximum_height)
Tries to reduce the height of a container.
virtual iteration::walker_ptr create_walker() override
See widget::create_walker.
void set_rows(const unsigned rows)
unsigned int get_cols() const
container_base(const implementation::builder_styled_widget &builder, const std::string &control_type)
void set_rows_cols(const unsigned rows, const unsigned cols)
virtual void set_self_active(const bool active)=0
Helper for set_active.
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
unsigned int get_rows() const
virtual void request_reduce_height(const unsigned maximum_height) override
See widget::request_reduce_height.
const grid & get_grid() const
grid::iterator end()
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
virtual void set_active(const bool active) override
See styled_widget::set_active.
virtual bool can_wrap() const override
See widget::can_wrap.
void set_cols(const unsigned cols)
virtual SDL_Rect get_client_rect() const
Returns the client rect.
virtual point calculate_best_size() const override
See widget::calculate_best_size.
void set_row_grow_factor(const unsigned row, const unsigned factor)
virtual void demand_reduce_width(const unsigned maximum_width) override
See widget::demand_reduce_width.
virtual void layout_children() override
See widget::layout_children.
friend class debug_layout_graph
void reduce_width(const unsigned maximum_width)
Tries to reduce the width of a container.
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
grid grid_
The grid which holds the child objects.
virtual void set_origin(const point &origin) override
See widget::set_origin.
virtual void place(const point &origin, const point &size) override
See widget::place.
virtual void set_visible_rectangle(const SDL_Rect &rectangle) override
See widget::set_visible_rectangle.
void set_column_grow_factor(const unsigned column, const unsigned factor)
virtual void demand_reduce_height(const unsigned maximum_height) override
See widget::demand_reduce_height.
unsigned add_row(const unsigned count=1)
void set_child(std::unique_ptr< widget > widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
grid::iterator begin()
void init_grid(const builder_grid &grid_builder)
Initializes and builds the grid.
virtual bool has_widget(const widget &widget) const override
See widget::has_widget.
virtual void impl_draw_children() override
See widget::impl_draw_children.
virtual point border_space() const
Returns the space used by the border.
Iterator for the child items.
Definition: grid.hpp:438
Base container class.
Definition: grid.hpp:32
void set_row_grow_factor(const unsigned row, const unsigned factor)
Sets the grow factor for a row.
Definition: grid.hpp:87
unsigned add_row(const unsigned count=1)
Adds a row to end of the grid.
Definition: grid.cpp:60
unsigned int get_rows() const
Definition: grid.hpp:303
void set_column_grow_factor(const unsigned column, const unsigned factor)
Sets the grow factor for a column.
Definition: grid.hpp:102
iterator begin()
Definition: grid.hpp:479
void set_cols(const unsigned cols)
Definition: grid.cpp:703
void set_rows(const unsigned rows)
Definition: grid.cpp:694
iterator end()
Definition: grid.hpp:483
void set_rows_cols(const unsigned rows, const unsigned cols)
Wrapper to set_rows and set_cols.
Definition: grid.cpp:712
void set_child(std::unique_ptr< widget > widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
Sets a child in the grid.
Definition: grid.cpp:71
unsigned int get_cols() const
Definition: grid.hpp:309
Base class for all visible items.
Base class for all widgets.
Definition: widget.hpp:53
std::unique_ptr< class walker_base > walker_ptr
Definition: widget.hpp:42
Generic file dialog.
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:85
Holds a 2D point.
Definition: point.hpp:25