The Battle for Wesnoth  1.17.0-dev
grid_private.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 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 /**
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  {
60  for(hack & child : grid.children_)
61  {
62 
63  W* widget = child.get_widget();
64  if(!widget) {
65  continue;
66  }
67 
68  widget = widget->find_at(coordinate, must_be_active);
69  if(widget) {
70  return widget;
71  }
72  }
73 
74  return 0;
75  }
76 
77  /**
78  * Implementation for the wrappers for
79  * [const] widget* grid::find(const std::string&,
80  * const bool) [const].
81  *
82  * @tparam W widget or const widget.
83  */
84  template <class W>
86  const std::string& id,
87  const bool must_be_active)
88  {
89  // Inherited.
90  W* widget = grid.widget::find(id, must_be_active);
91  if(widget) {
92  return widget;
93  }
94 
96  for(hack & child : grid.children_)
97  {
98 
99  widget = child.get_widget();
100  if(!widget) {
101  continue;
102  }
103 
104  widget = widget->find(id, must_be_active);
105  if(widget) {
106  return widget;
107  }
108  }
109 
110  return 0;
111  }
112 
113  /**
114  * Helper function to do the resizing of a row.
115  *
116  * @param grid The grid to operate upon.
117  * @param row The row to resize.
118  * @param maximum_height The wanted maximum height.
119  *
120  * @returns The required row height after resizing.
121  */
122  static unsigned row_request_reduce_height(grid& grid,
123  const unsigned row,
124  const unsigned maximum_height);
125 
126  /**
127  * Helper function to do the resizing of a column.
128  *
129  * @param grid The grid to operate upon.
130  * @param column The column to resize.
131  * @param maximum_width The wanted maximum width.
132  *
133  * @returns The required column width after resizing.
134  */
135  static unsigned column_request_reduce_width(grid& grid,
136  const unsigned column,
137  const unsigned maximum_width);
138 
139 private:
140  /**
141  * Helper function to do the resizing of a widget.
142  *
143  * @param child The cell whose widget needs to be resized.
144  * @param maximum_height The wanted maximum height.
145  */
146  static void cell_request_reduce_height(grid::child& child,
147  const unsigned maximum_height);
148 
149  /**
150  * Helper function to do the resizing of a widget.
151  *
152  * @param child The cell whose widget needs to be resized.
153  * @param maximum_width The wanted maximum width.
154  */
155  static void cell_request_reduce_width(grid::child& child,
156  const unsigned maximum_width);
157 };
158 
159 } // namespace gui2
virtual widget * find(const std::string &id, const bool must_be_active)
Returns a widget with the wanted id.
Definition: widget.cpp:584
Child item of the grid.
Definition: grid.hpp:329
Base class for all widgets.
Definition: widget.hpp:49
static W * find(utils::const_clone_ref< grid, W > grid, const std::string &id, const bool must_be_active)
Implementation for the wrappers for [const] widget* grid::find(const std::string&, const bool) [const].
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]...
typename const_clone< D, S >::reference const_clone_ref
Definition: const_clone.hpp:63
Generic file dialog.
Definition: field-fwd.hpp:23
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:1081
Base container class.
Definition: grid.hpp:31
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:1030
Holds a 2D point.
Definition: point.hpp:24
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:1055
typename const_clone< D, S >::type const_clone_t
Definition: const_clone.hpp:60
virtual widget * find_at(const point &coordinate, const bool must_be_active)
Returns the widget at the wanted coordinates.
Definition: widget.cpp:573
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
Helper to implement private functions without modifying the header.
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:1095