The Battle for Wesnoth  1.15.2+dev
scrollbar_panel.cpp
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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
20 #include "gui/widgets/settings.hpp"
22 
23 #include "gettext.hpp"
24 #include "wml_exception.hpp"
25 
26 #include "utils/functional.hpp"
27 
28 namespace gui2
29 {
30 
31 // ------------ WIDGET -----------{
32 
33 REGISTER_WIDGET(scrollbar_panel)
34 
35 scrollbar_panel::scrollbar_panel(const implementation::builder_scrollbar_panel& builder)
36  : scrollbar_container(builder, type())
37 {
38 }
39 
41 {
42  return true;
43 }
44 
46 {
47  return 0;
48 }
49 
50 void scrollbar_panel::set_self_active(const bool /*active*/)
51 {
52  /* DO NOTHING */
53 }
54 
55 // }---------- DEFINITION ---------{
56 
59 {
60  DBG_GUI_P << "Parsing scrollbar panel " << id << '\n';
61 
62  load_resolutions<resolution>(cfg);
63 }
64 
65 /*WIKI
66  * @page = GUIWidgetDefinitionWML
67  * @order = 1_scrollbar_panel
68  *
69  * == Scrollbar panel ==
70  *
71  * @begin{parent}{name="gui/"}
72  * @begin{tag}{name="scrollbar_panel_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
73  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="gui/window_definition/resolution"}
74  * The definition of a panel with scrollbars. A panel is a container holding
75  * other elements in its grid. A panel is always enabled and can't be
76  * disabled. Instead it uses the states as layers to draw on.
77  *
78  * @begin{table}{config}
79  * grid & grid & & A grid containing the widgets for main
80  * widget. $
81  * @end{table}
82  * The following layers exist:
83  * * background, the background of the panel.
84  * * foreground, the foreground of the panel.
85  *
86  * @end{tag}{name="resolution"}
87  * @end{tag}{name="scrollbar_panel_definition"}
88  * @end{parent}{name="gui/"}
89  */
91  : resolution_definition(cfg), grid()
92 {
93  // The panel needs to know the order.
94  state.emplace_back(cfg.child("background"));
95  state.emplace_back(cfg.child("foreground"));
96 
97  const config& child = cfg.child("grid");
98  VALIDATE(child, _("No grid defined."));
99 
100  grid = std::make_shared<builder_grid>(child);
101 }
102 
103 // }---------- BUILDER -----------{
104 
105 /*WIKI
106  * @page = GUIWidgetInstanceWML
107  * @order = 2_scrollbar_panel
108  *
109  * == Scrollbar panel ==
110  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
111  * @begin{tag}{name="scrollbar_panel"}{min="0"}{max="-1"}{super="generic/widget_instance"}
112  * Instance of a scrollbar_panel.
113  *
114  * List with the scrollbar_panel specific variables:
115  * @begin{table}{config}
116  * vertical_scrollbar_mode & scrollbar_mode & initial_auto &
117  * Determines whether or not to show the
118  * scrollbar. $
119  * horizontal_scrollbar_mode & scrollbar_mode & initial_auto &
120  * Determines whether or not to show the
121  * scrollbar. $
122  *
123  * definition & section & & This defines how a scrollbar_panel item
124  * looks. It must contain the grid
125  * definition for 1 row of the list. $
126  *
127  * @end{table}
128  * @begin{tag}{name="definition"}{min=0}{max=1}{super="gui/window/resolution/grid"}
129  * @end{tag}{name="definition"}
130  * @end{tag}{name="scrollbar_panel"}
131  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
132  */
133 
134 namespace implementation
135 {
136 
137 builder_scrollbar_panel::builder_scrollbar_panel(const config& cfg)
138  : builder_styled_widget(cfg)
139  , vertical_scrollbar_mode(
140  get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
141  , horizontal_scrollbar_mode(
142  get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
143  , grid_(nullptr)
144 {
145  const config& grid_definition = cfg.child("definition");
146 
147  VALIDATE(grid_definition, _("No list defined."));
148  grid_ = std::make_shared<builder_grid>(grid_definition);
149  assert(grid_);
150 }
151 
153 {
154  scrollbar_panel* panel = new scrollbar_panel(*this);
155 
158 
159  DBG_GUI_G << "Window builder: placed scrollbar_panel '" << id
160  << "' with definition '" << definition << "'.\n";
161 
162  const auto conf = panel->cast_config_to<scrollbar_panel_definition>();
163  assert(conf);
164 
165  panel->init_grid(conf->grid);
166  panel->finalize_setup();
167 
168  /*** Fill the content grid. ***/
169  grid* content_grid = panel->content_grid();
170  assert(content_grid);
171 
172  const unsigned rows = grid_->rows;
173  const unsigned cols = grid_->cols;
174 
175  content_grid->set_rows_cols(rows, cols);
176 
177  for(unsigned x = 0; x < rows; ++x) {
178  content_grid->set_row_grow_factor(x, grid_->row_grow_factor[x]);
179  for(unsigned y = 0; y < cols; ++y) {
180 
181  if(x == 0) {
182  content_grid->set_column_grow_factor(y,
183  grid_->col_grow_factor[y]);
184  }
185 
186  widget* widget = grid_->widgets[x * cols + y]->build();
187  content_grid->set_child(widget,
188  x,
189  y,
190  grid_->flags[x * cols + y],
191  grid_->border_size[x * cols + y]);
192  }
193  }
194 
195  return panel;
196 }
197 
198 } // namespace implementation
199 
200 // }------------ END --------------
201 
202 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:68
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:420
std::vector< state_definition > state
void set_horizontal_scrollbar_mode(const scrollbar_mode scrollbar_mode)
void set_row_grow_factor(const unsigned row, const unsigned factor)
Sets the grow factor for a row.
Definition: grid.hpp:86
Visible container to hold multiple widgets.
Definition: panel.hpp:37
Add a special kind of assert to validate whether the input from WML doesn&#39;t contain any problems that...
virtual unsigned get_state() const override
See styled_widget::get_state.
void set_vertical_scrollbar_mode(const scrollbar_mode scrollbar_mode)
Base class for all widgets.
Definition: widget.hpp:47
Generic file dialog.
Definition: field-fwd.hpp:22
Visible container to hold multiple widgets.
Base container class.
Definition: grid.hpp:30
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
std::string definition
Parameters for the styled_widget.
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
This file contains the settings handling of the widget library.
void init_grid(const std::shared_ptr< builder_grid > &grid_builder)
Initializes and builds the grid.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
void set_child(widget *widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
Sets a child in the grid.
Definition: grid.cpp:69
Base class for creating containers with one or two scrollbar(s).
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
virtual bool get_active() const override
See styled_widget::get_active.
std::shared_ptr< const typename T::resolution > cast_config_to() const
Casts the current resolution definition config to the respective type of a derived widget...
scrollbar_mode get_scrollbar_mode(const std::string &scrollbar_mode)
Returns the scrollbar mode flags.
Definition: helper.cpp:120
void set_rows_cols(const unsigned rows, const unsigned cols)
Wrapper to set_rows and set_cols.
Definition: grid.cpp:722
scrollbar_panel_definition(const config &cfg)
void set_column_grow_factor(const unsigned column, const unsigned factor)
Sets the grow factor for a column.
Definition: grid.hpp:101
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
#define DBG_GUI_G
Definition: log.hpp:40
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
scrollbar_container::scrollbar_mode vertical_scrollbar_mode