The Battle for Wesnoth  1.17.12+dev
container_base.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2022
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() override;
112 
113 protected:
114  /** See @ref widget::layout_children. */
115  virtual void layout_children() override;
116 
117 public:
118  /** See @ref widget::find_at. */
119  virtual widget* find_at(const point& coordinate,
120  const bool must_be_active) override;
121 
122  /** See @ref widget::find_at. */
123  virtual const widget* find_at(const point& coordinate,
124  const bool must_be_active) const override;
125 
126  /** See @ref widget::find. */
127  widget* find(const std::string& id, const bool must_be_active) override;
128 
129  /** See @ref widget::find. */
130  const widget* find(const std::string& id,
131  const bool must_be_active) const override;
132 
133  /** See @ref styled_widget::set_active. */
134  virtual void set_active(const bool active) override;
135 
136  /** See @ref widget::disable_click_dismiss. */
137  bool disable_click_dismiss() const override;
138 
139  /**
140  * See @ref widget::create_walker.
141  */
142  virtual iteration::walker_ptr create_walker() override;
143 
144  /**
145  * Initializes and builds the grid.
146  *
147  * This function should only be called upon an empty grid.
148  *
149  * @param grid_builder The builder for the grid.
150  */
151  void init_grid(const builder_grid& grid_builder);
152 
153  /***** **** ***** ***** wrappers to the grid **** ********* *****/
154 
156  {
157  return grid_.begin();
158  }
160  {
161  return grid_.end();
162  }
163 
164  unsigned add_row(const unsigned count = 1)
165  {
166  return grid_.add_row(count);
167  }
168 
169  void set_rows(const unsigned rows)
170  {
171  grid_.set_rows(rows);
172  }
173  unsigned int get_rows() const
174  {
175  return grid_.get_rows();
176  }
177 
178  void set_cols(const unsigned cols)
179  {
180  grid_.set_cols(cols);
181  }
182  unsigned int get_cols() const
183  {
184  return grid_.get_cols();
185  }
186 
187  void set_rows_cols(const unsigned rows, const unsigned cols)
188  {
189  grid_.set_rows_cols(rows, cols);
190  }
191 
192  void set_child(std::unique_ptr<widget> widget,
193  const unsigned row,
194  const unsigned col,
195  const unsigned flags,
196  const unsigned border_size)
197  {
198  grid_.set_child(std::move(widget), row, col, flags, border_size);
199  }
200 
201  void set_row_grow_factor(const unsigned row, const unsigned factor)
202  {
203  grid_.set_row_grow_factor(row, factor);
204  }
205 
206  void set_column_grow_factor(const unsigned column, const unsigned factor)
207  {
208  grid_.set_column_grow_factor(column, factor);
209  }
210 
211 public:
212  /***** ***** ***** setters / getters for members ***** ****** *****/
213 
214  // Public due to the fact that window needs to be able to swap the
215  // children, might be protected again later.
216  const grid& get_grid() const
217  {
218  return grid_;
219  }
221  {
222  return grid_;
223  }
224 
225 private:
226  /** The grid which holds the child objects. */
228 
229  /** Returns the space used by the border. */
230  virtual point border_space() const;
231 
232  /**
233  * Helper for set_active.
234  *
235  * This function should set the styled_widget itself active. It's called by
236  * set_active if the state needs to change. The widget is set to dirty() by
237  * set_active so we only need to change the state.
238  */
239  virtual void set_self_active(const bool active) = 0;
240 
241  void inject_linked_groups();
242 };
243 
244 } // namespace gui2
std::unique_ptr< class walker_base > walker_ptr
Definition: widget.hpp:43
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:53
void set_column_grow_factor(const unsigned column, const unsigned factor)
Generic file dialog.
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
Iterator for the child items.
Definition: grid.hpp:437
void set_child(std::unique_ptr< widget > widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
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.
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.
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
void set_rows(const unsigned rows)