The Battle for Wesnoth  1.17.0-dev
scrollbar_panel.cpp
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 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
21 #include "gui/widgets/settings.hpp"
23 
24 #include "gettext.hpp"
25 #include "wml_exception.hpp"
26 
27 #include <functional>
28 
29 namespace gui2
30 {
31 
32 // ------------ WIDGET -----------{
33 
34 REGISTER_WIDGET(scrollbar_panel)
35 
36 scrollbar_panel::scrollbar_panel(const implementation::builder_scrollbar_panel& builder)
37  : scrollbar_container(builder, type())
38 {
39 }
40 
42 {
43  return true;
44 }
45 
47 {
48  return 0;
49 }
50 
51 void scrollbar_panel::set_self_active(const bool /*active*/)
52 {
53  /* DO NOTHING */
54 }
55 
56 // }---------- DEFINITION ---------{
57 
60 {
61  DBG_GUI_P << "Parsing scrollbar panel " << id << '\n';
62 
63  load_resolutions<resolution>(cfg);
64 }
65 
67  : resolution_definition(cfg), grid()
68 {
69  // The panel needs to know the order.
70  state.emplace_back(cfg.child("background"));
71  state.emplace_back(cfg.child("foreground"));
72 
73  const config& child = cfg.child("grid");
74  VALIDATE(child, _("No grid defined."));
75 
76  grid = std::make_shared<builder_grid>(child);
77 }
78 
79 // }---------- BUILDER -----------{
80 
81 namespace implementation
82 {
83 
84 builder_scrollbar_panel::builder_scrollbar_panel(const config& cfg)
86  , vertical_scrollbar_mode(
87  get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
88  , horizontal_scrollbar_mode(
89  get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
90  , grid_(nullptr)
91 {
92  const config& grid_definition = cfg.child("definition");
93 
94  VALIDATE(grid_definition, _("No list defined."));
95  grid_ = std::make_shared<builder_grid>(grid_definition);
96  assert(grid_);
97 }
98 
100 {
101  scrollbar_panel* panel = new scrollbar_panel(*this);
102 
105 
106  DBG_GUI_G << "Window builder: placed scrollbar_panel '" << id
107  << "' with definition '" << definition << "'.\n";
108 
109  const auto conf = panel->cast_config_to<scrollbar_panel_definition>();
110  assert(conf);
111 
112  panel->init_grid(*conf->grid);
113  panel->finalize_setup();
114 
115  /*** Fill the content grid. ***/
116  grid* content_grid = panel->content_grid();
117  assert(content_grid);
118 
119  const unsigned rows = grid_->rows;
120  const unsigned cols = grid_->cols;
121 
122  content_grid->set_rows_cols(rows, cols);
123 
124  for(unsigned x = 0; x < rows; ++x) {
125  content_grid->set_row_grow_factor(x, grid_->row_grow_factor[x]);
126  for(unsigned y = 0; y < cols; ++y) {
127 
128  if(x == 0) {
129  content_grid->set_column_grow_factor(y,
130  grid_->col_grow_factor[y]);
131  }
132 
133  widget* widget = grid_->widgets[x * cols + y]->build();
134  content_grid->set_child(widget,
135  x,
136  y,
137  grid_->flags[x * cols + y],
138  grid_->border_size[x * cols + y]);
139  }
140  }
141 
142  return panel;
143 }
144 
145 } // namespace implementation
146 
147 // }------------ END --------------
148 
149 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:66
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:419
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:87
A panel is a visible container to hold multiple widgets.
Definition: panel.hpp:58
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:49
static std::string _(const char *str)
Definition: gettext.hpp:93
virtual widget * build() const override
Generic file dialog.
Definition: field-fwd.hpp:23
Visible container to hold multiple widgets.
Base container class.
Definition: grid.hpp:31
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 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:70
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:121
void set_rows_cols(const unsigned rows, const unsigned cols)
Wrapper to set_rows and set_cols.
Definition: grid.cpp:723
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:102
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
#define DBG_GUI_G
Definition: log.hpp:41
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
scrollbar_container::scrollbar_mode vertical_scrollbar_mode