The Battle for Wesnoth  1.17.23+dev
scrollbar_panel.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2023
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;
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(VALIDATE_WML_CHILD(cfg, "background", _("Missing required background for scrollbar panel")));
71  state.emplace_back(VALIDATE_WML_CHILD(cfg, "foreground", _("Missing required foreground for scrollbar panel")));
72 
73  auto child = VALIDATE_WML_CHILD(cfg, "grid", _("Missing required grid for scrollbar panel"));
74  grid = std::make_shared<builder_grid>(child);
75 }
76 
77 // }---------- BUILDER -----------{
78 
79 namespace implementation
80 {
81 
82 builder_scrollbar_panel::builder_scrollbar_panel(const config& cfg)
84  , vertical_scrollbar_mode(
85  get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
86  , horizontal_scrollbar_mode(
87  get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
88  , grid_(nullptr)
89 {
90  auto grid_definition = cfg.optional_child("definition");
91 
92  VALIDATE(grid_definition, _("No list defined."));
93  grid_ = std::make_shared<builder_grid>(*grid_definition);
94  assert(grid_);
95 }
96 
97 std::unique_ptr<widget> builder_scrollbar_panel::build() const
98 {
99  auto panel = std::make_unique<scrollbar_panel>(*this);
100 
101  panel->set_vertical_scrollbar_mode(vertical_scrollbar_mode);
102  panel->set_horizontal_scrollbar_mode(horizontal_scrollbar_mode);
103 
104  DBG_GUI_G << "Window builder: placed scrollbar_panel '" << id
105  << "' with definition '" << definition << "'.";
106 
107  const auto conf = panel->cast_config_to<scrollbar_panel_definition>();
108  assert(conf);
109 
110  panel->init_grid(*conf->grid);
111  panel->finalize_setup();
112 
113  /*** Fill the content grid. ***/
114  grid* content_grid = panel->content_grid();
115  assert(content_grid);
116 
117  const unsigned rows = grid_->rows;
118  const unsigned cols = grid_->cols;
119 
120  content_grid->set_rows_cols(rows, cols);
121 
122  for(unsigned x = 0; x < rows; ++x) {
123  content_grid->set_row_grow_factor(x, grid_->row_grow_factor[x]);
124  for(unsigned y = 0; y < cols; ++y) {
125 
126  if(x == 0) {
127  content_grid->set_column_grow_factor(y,
128  grid_->col_grow_factor[y]);
129  }
130 
131  auto widget = grid_->widgets[x * cols + y]->build();
132  content_grid->set_child(std::move(widget),
133  x,
134  y,
135  grid_->flags[x * cols + y],
136  grid_->border_size[x * cols + y]);
137  }
138  }
139 
140  return panel;
141 }
142 
143 } // namespace implementation
144 
145 // }------------ END --------------
146 
147 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Euivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition: config.cpp:389
void init_grid(const builder_grid &grid_builder)
Initializes and builds the grid.
Base container class.
Definition: grid.hpp:32
void set_row_grow_factor(const unsigned row, const unsigned factor)
Sets the grow factor for a row.
Definition: grid.hpp:87
void set_column_grow_factor(const unsigned column, const unsigned factor)
Sets the grow factor for a column.
Definition: grid.hpp:102
void set_rows_cols(const unsigned rows, const unsigned cols)
Wrapper to set_rows and set_cols.
Definition: grid.cpp:712
void set_child(std::unique_ptr< widget > widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
Sets a child in the grid.
Definition: grid.cpp:71
A panel is a visible container to hold multiple widgets.
Definition: panel.hpp:59
Base class for creating containers with one or two scrollbar(s).
Visible container to hold multiple widgets.
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.
virtual unsigned get_state() const override
See styled_widget::get_state.
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.
Base class for all widgets.
Definition: widget.hpp:54
static std::string _(const char *str)
Definition: gettext.hpp:93
#define DBG_GUI_G
Definition: log.hpp:41
#define DBG_GUI_P
Definition: log.hpp:66
scrollbar_mode get_scrollbar_mode(const std::string &scrollbar_mode)
Returns the scrollbar mode flags.
Definition: helper.cpp:121
Generic file dialog.
Contains the implementation details for lexical_cast and shouldn't be used directly.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
This file contains the settings handling of the widget library.
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
virtual std::unique_ptr< widget > build() const override
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
std::string definition
Parameters for the styled_widget.
Base class of a resolution, contains the common keys for a resolution.
std::vector< state_definition > state
scrollbar_panel_definition(const config &cfg)
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE_WML_CHILD(cfg, key, message)
#define VALIDATE(cond, message)
The macro to use for the validation of WML.