The Battle for Wesnoth  1.15.2+dev
grid_private.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 /**
18  * @file
19  * Helper for header for the grid.
20  *
21  * @note This file should only be included by grid.cpp.
22  *
23  * This file is being used for a small experiment in which some private
24  * functions of grid are no longer in grid but moved in a friend class with
25  * static functions. The goal is to have less header recompilations, when
26  * there's a need to add or remove a private function.
27  * Also non-trivial functions like 'const foo& bar() const' and 'foo& bar()'
28  * are wrapped in a template to avoid code duplication (for typing not for the
29  * binary) to make maintenance easier.
30  */
31 
32 #include "gui/widgets/grid.hpp"
33 
34 #include "utils/const_clone.hpp"
35 
36 namespace gui2
37 {
38 
39 /**
40  * Helper to implement private functions without modifying the header.
41  *
42  * The class is a helper to avoid recompilation and only has static
43  * functions.
44  */
46 {
47  /**
48  * Implementation for the wrappers for
49  * [const] widget* grid::find_at(const point&, const bool) [const].
50  *
51  * @tparam W widget or const widget.
52  */
53  template <class W>
55  const point& coordinate,
56  const bool must_be_active)
57  {
59  for(hack & child : grid.children_)
60  {
61 
62  W* widget = child.get_widget();
63  if(!widget) {
64  continue;
65  }
66 
67  widget = widget->find_at(coordinate, must_be_active);
68  if(widget) {
69  return widget;
70  }
71  }
72 
73  return 0;
74  }
75 
76  /**
77  * Implementation for the wrappers for
78  * [const] widget* grid::find(const std::string&,
79  * const bool) [const].
80  *
81  * @tparam W widget or const widget.
82  */
83  template <class W>
85  const std::string& id,
86  const bool must_be_active)
87  {
88  // Inherited.
89  W* widget = grid.widget::find(id, must_be_active);
90  if(widget) {
91  return widget;
92  }
93 
95  for(hack & child : grid.children_)
96  {
97 
98  widget = child.get_widget();
99  if(!widget) {
100  continue;
101  }
102 
103  widget = widget->find(id, must_be_active);
104  if(widget) {
105  return widget;
106  }
107  }
108 
109  return 0;
110  }
111 
112  /**
113  * Helper function to do the resizing of a row.
114  *
115  * @param grid The grid to operate upon.
116  * @param row The row to resize.
117  * @param maximum_height The wanted maximum height.
118  *
119  * @returns The required row height after resizing.
120  */
121  static unsigned row_request_reduce_height(grid& grid,
122  const unsigned row,
123  const unsigned maximum_height);
124 
125  /**
126  * Helper function to do the resizing of a column.
127  *
128  * @param grid The grid to operate upon.
129  * @param column The column to resize.
130  * @param maximum_width The wanted maximum width.
131  *
132  * @returns The required column width after resizing.
133  */
134  static unsigned column_request_reduce_width(grid& grid,
135  const unsigned column,
136  const unsigned maximum_width);
137 
138 private:
139  /**
140  * Helper function to do the resizing of a widget.
141  *
142  * @param child The cell whose widget needs to be resized.
143  * @param maximum_height The wanted maximum height.
144  */
145  static void cell_request_reduce_height(grid::child& child,
146  const unsigned maximum_height);
147 
148  /**
149  * Helper function to do the resizing of a widget.
150  *
151  * @param child The cell whose widget needs to be resized.
152  * @param maximum_width The wanted maximum width.
153  */
154  static void cell_request_reduce_width(grid::child& child,
155  const unsigned maximum_width);
156 };
157 
158 } // namespace gui2
virtual widget * find(const std::string &id, const bool must_be_active)
Returns a widget with the wanted id.
Definition: widget.cpp:581
Child item of the grid.
Definition: grid.hpp:328
Base class for all widgets.
Definition: widget.hpp:47
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:62
Generic file dialog.
Definition: field-fwd.hpp:22
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:1080
Base container class.
Definition: grid.hpp:30
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:1029
Holds a 2D point.
Definition: point.hpp:23
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:1054
typename const_clone< D, S >::type const_clone_t
Definition: const_clone.hpp:59
virtual widget * find_at(const point &coordinate, const bool must_be_active)
Returns the widget at the wanted coordinates.
Definition: widget.cpp:570
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:1094