The Battle for Wesnoth  1.19.7+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_view id, const bool must_be_active) override;
123 
124  /** See @ref widget::find. */
125  const widget* find(const std::string_view id, const bool must_be_active) const override;
126 
127  /** See @ref styled_widget::set_active. */
128  virtual void set_active(const bool active) override;
129 
130  /** See @ref widget::disable_click_dismiss. */
131  bool disable_click_dismiss() const override;
132 
133  /**
134  * See @ref widget::create_walker.
135  */
136  virtual iteration::walker_ptr create_walker() override;
137 
138  /**
139  * Initializes and builds the grid.
140  *
141  * This function should only be called upon an empty grid.
142  *
143  * @param grid_builder The builder for the grid.
144  */
145  void init_grid(const builder_grid& grid_builder);
146 
147  /***** **** ***** ***** wrappers to the grid **** ********* *****/
148 
150  {
151  return grid_.begin();
152  }
154  {
155  return grid_.end();
156  }
157 
158  unsigned add_row(const unsigned count = 1)
159  {
160  return grid_.add_row(count);
161  }
162 
163  void set_rows(const unsigned rows)
164  {
165  grid_.set_rows(rows);
166  }
167  unsigned int get_rows() const
168  {
169  return grid_.get_rows();
170  }
171 
172  void set_cols(const unsigned cols)
173  {
174  grid_.set_cols(cols);
175  }
176  unsigned int get_cols() const
177  {
178  return grid_.get_cols();
179  }
180 
181  void set_rows_cols(const unsigned rows, const unsigned cols)
182  {
183  grid_.set_rows_cols(rows, cols);
184  }
185 
186  void set_child(std::unique_ptr<widget> widget,
187  const unsigned row,
188  const unsigned col,
189  const unsigned flags,
190  const unsigned border_size)
191  {
192  grid_.set_child(std::move(widget), row, col, flags, border_size);
193  }
194 
195  void set_row_grow_factor(const unsigned row, const unsigned factor)
196  {
197  grid_.set_row_grow_factor(row, factor);
198  }
199 
200  void set_column_grow_factor(const unsigned column, const unsigned factor)
201  {
202  grid_.set_column_grow_factor(column, factor);
203  }
204 
205 public:
206  /***** ***** ***** setters / getters for members ***** ****** *****/
207 
208  // Public due to the fact that window needs to be able to swap the
209  // children, might be protected again later.
210  const grid& get_grid() const
211  {
212  return grid_;
213  }
215  {
216  return grid_;
217  }
218 
219 private:
220  /** The grid which holds the child objects. */
222 
223  /** Returns the space used by the border. */
224  virtual point border_space() const;
225 
226  /**
227  * Helper for set_active.
228  *
229  * This function should set the styled_widget itself active. It's called by
230  * set_active if the state needs to change. The widget is set to dirty() by
231  * set_active so we only need to change the state.
232  */
233  virtual void set_self_active(const bool active) = 0;
234 
235  void inject_linked_groups();
236 };
237 
238 } // 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_view 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:437
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:302
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:478
void set_cols(const unsigned cols)
Definition: grid.cpp:702
void set_rows(const unsigned rows)
Definition: grid.cpp:693
iterator end()
Definition: grid.hpp:482
void set_rows_cols(const unsigned rows, const unsigned cols)
Wrapper to set_rows and set_cols.
Definition: grid.cpp:711
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:308
Base class for all widgets.
Definition: widget.hpp:55
std::unique_ptr< class walker_base > walker_ptr
Definition: widget.hpp:44
Generic file dialog.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
Holds a 2D point.
Definition: point.hpp:25