The Battle for Wesnoth  1.19.7+dev
grid_private.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 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 /**
19  * @file
20  * Helper for header for the grid.
21  *
22  * @note This file should only be included by grid.cpp.
23  *
24  * This file is being used for a small experiment in which some private
25  * functions of grid are no longer in grid but moved in a friend class with
26  * static functions. The goal is to have less header recompilations, when
27  * there's a need to add or remove a private function.
28  * Also non-trivial functions like 'const foo& bar() const' and 'foo& bar()'
29  * are wrapped in a template to avoid code duplication (for typing not for the
30  * binary) to make maintenance easier.
31  */
32 
33 #include "gui/widgets/grid.hpp"
34 
35 #include "utils/const_clone.hpp"
36 
37 namespace gui2
38 {
39 
40 /**
41  * Helper to implement private functions without modifying the header.
42  *
43  * The class is a helper to avoid recompilation and only has static
44  * functions.
45  */
47 {
48  /**
49  * Implementation for the wrappers for
50  * [const] widget* grid::find_at(const point&, const bool) [const].
51  *
52  * @tparam W widget or const widget.
53  */
54  template <class W>
56  const point& coordinate,
57  const bool must_be_active)
58  {
59  for(auto& child : grid.children_)
60  {
61  W* widget = child.get_widget();
62  if(!widget) {
63  continue;
64  }
65 
66  widget = widget->find_at(coordinate, must_be_active);
67  if(widget) {
68  return widget;
69  }
70  }
71 
72  return nullptr;
73  }
74 
75  /**
76  * Implementation for the wrappers for
77  * [const] widget* grid::find(const std::string_view,
78  * const bool) [const].
79  *
80  * @tparam W widget or const widget.
81  */
82  template <class W>
84  const std::string_view id,
85  const bool must_be_active)
86  {
87  // Inherited.
88  W* widget = grid.widget::find(id, must_be_active);
89  if(widget) {
90  return widget;
91  }
92 
93  for(auto& child : grid.children_)
94  {
95  widget = child.get_widget();
96  if(!widget) {
97  continue;
98  }
99 
100  widget = widget->find(id, must_be_active);
101  if(widget) {
102  return widget;
103  }
104  }
105 
106  return nullptr;
107  }
108 
109  /**
110  * Helper function to do the resizing of a row.
111  *
112  * @param grid The grid to operate upon.
113  * @param row The row to resize.
114  * @param maximum_height The wanted maximum height.
115  *
116  * @returns The required row height after resizing.
117  */
118  static unsigned row_request_reduce_height(grid& grid,
119  const unsigned row,
120  const unsigned maximum_height);
121 
122  /**
123  * Helper function to do the resizing of a column.
124  *
125  * @param grid The grid to operate upon.
126  * @param column The column to resize.
127  * @param maximum_width The wanted maximum width.
128  *
129  * @returns The required column width after resizing.
130  */
131  static unsigned column_request_reduce_width(grid& grid,
132  const unsigned column,
133  const unsigned maximum_width);
134 
135 private:
136  /**
137  * Helper function to do the resizing of a widget.
138  *
139  * @param child The cell whose widget needs to be resized.
140  * @param maximum_height The wanted maximum height.
141  */
142  static void cell_request_reduce_height(grid::child& child,
143  const unsigned maximum_height);
144 
145  /**
146  * Helper function to do the resizing of a widget.
147  *
148  * @param child The cell whose widget needs to be resized.
149  * @param maximum_width The wanted maximum width.
150  */
151  static void cell_request_reduce_width(grid::child& child,
152  const unsigned maximum_width);
153 };
154 
155 } // namespace gui2
Child item of the grid.
Definition: grid.hpp:324
Base container class.
Definition: grid.hpp:32
std::vector< child > children_
The child items.
Definition: grid.hpp:514
Base class for all widgets.
Definition: widget.hpp:55
virtual widget * find(const std::string_view id, const bool must_be_active)
Returns a widget with the wanted id.
Definition: widget.cpp:560
virtual widget * find_at(const point &coordinate, const bool must_be_active)
Returns the widget at the wanted coordinates.
Definition: widget.cpp:549
Generic file dialog.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
typename const_clone< D, S >::reference const_clone_ref
Definition: const_clone.hpp:63
Helper to implement private functions without modifying the header.
static unsigned column_request_reduce_width(grid &grid, const unsigned column, const unsigned maximum_width)
Helper function to do the resizing of a column.
Definition: grid.cpp:1050
static void cell_request_reduce_width(grid::child &child, const unsigned maximum_width)
Helper function to do the resizing of a widget.
Definition: grid.cpp:1090
static W * find_at(utils::const_clone_ref< grid, W > grid, const point &coordinate, const bool must_be_active)
Implementation for the wrappers for [const] widget* grid::find_at(const point&, const bool) [const].
static void cell_request_reduce_height(grid::child &child, const unsigned maximum_height)
Helper function to do the resizing of a widget.
Definition: grid.cpp:1076
static unsigned row_request_reduce_height(grid &grid, const unsigned row, const unsigned maximum_height)
Helper function to do the resizing of a row.
Definition: grid.cpp:1025
static W * find(utils::const_clone_ref< grid, W > grid, const std::string_view id, const bool must_be_active)
Implementation for the wrappers for [const] widget* grid::find(const std::string_view,...
Holds a 2D point.
Definition: point.hpp:25