The Battle for Wesnoth  1.19.0-dev
scrollbar_panel.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
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 
22 
23 #include "gettext.hpp"
24 #include "wml_exception.hpp"
25 
26 
27 namespace gui2
28 {
29 
30 // ------------ WIDGET -----------{
31 
32 REGISTER_WIDGET(scrollbar_panel)
33 
34 scrollbar_panel::scrollbar_panel(const implementation::builder_scrollbar_panel& builder)
35  : scrollbar_container(builder, type())
36 {
37 }
38 
40 {
41  return true;
42 }
43 
45 {
46  return 0;
47 }
48 
49 void scrollbar_panel::set_self_active(const bool /*active*/)
50 {
51  /* DO NOTHING */
52 }
53 
54 // }---------- DEFINITION ---------{
55 
58 {
59  DBG_GUI_P << "Parsing scrollbar panel " << id;
60 
61  load_resolutions<resolution>(cfg);
62 }
63 
65  : resolution_definition(cfg), grid()
66 {
67  // The panel needs to know the order.
68  state.emplace_back(VALIDATE_WML_CHILD(cfg, "background", missing_mandatory_wml_tag("scrollbar_panel_definition][resolution", "background")));
69  state.emplace_back(VALIDATE_WML_CHILD(cfg, "foreground", missing_mandatory_wml_tag("scrollbar_panel_definition][resolution", "foreground")));
70 
71  auto child = VALIDATE_WML_CHILD(cfg, "grid", missing_mandatory_wml_tag("scrollbar_panel][definition", "grid"));
72  grid = std::make_shared<builder_grid>(child);
73 }
74 
75 // }---------- BUILDER -----------{
76 
77 namespace implementation
78 {
79 
80 builder_scrollbar_panel::builder_scrollbar_panel(const config& cfg)
82  , vertical_scrollbar_mode(
83  get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
84  , horizontal_scrollbar_mode(
85  get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
86  , grid_(nullptr)
87 {
88  auto grid_definition = cfg.optional_child("definition");
89 
90  VALIDATE(grid_definition, _("No list defined."));
91  grid_ = std::make_shared<builder_grid>(*grid_definition);
92  assert(grid_);
93 }
94 
95 std::unique_ptr<widget> builder_scrollbar_panel::build() const
96 {
97  auto panel = std::make_unique<scrollbar_panel>(*this);
98 
99  panel->set_vertical_scrollbar_mode(vertical_scrollbar_mode);
100  panel->set_horizontal_scrollbar_mode(horizontal_scrollbar_mode);
101 
102  DBG_GUI_G << "Window builder: placed scrollbar_panel '" << id
103  << "' with definition '" << definition << "'.";
104 
105  const auto conf = panel->cast_config_to<scrollbar_panel_definition>();
106  assert(conf);
107 
108  panel->init_grid(*conf->grid);
109  panel->finalize_setup();
110 
111  /*** Fill the content grid. ***/
112  grid* content_grid = panel->content_grid();
113  assert(content_grid);
114 
115  const unsigned rows = grid_->rows;
116  const unsigned cols = grid_->cols;
117 
118  content_grid->set_rows_cols(rows, cols);
119 
120  for(unsigned x = 0; x < rows; ++x) {
121  content_grid->set_row_grow_factor(x, grid_->row_grow_factor[x]);
122  for(unsigned y = 0; y < cols; ++y) {
123 
124  if(x == 0) {
125  content_grid->set_column_grow_factor(y,
126  grid_->col_grow_factor[y]);
127  }
128 
129  auto widget = grid_->widgets[x * cols + y]->build();
130  content_grid->set_child(std::move(widget),
131  x,
132  y,
133  grid_->flags[x * cols + y],
134  grid_->border_size[x * cols + y]);
135  }
136  }
137 
138  return panel;
139 }
140 
141 } // namespace implementation
142 
143 // }------------ END --------------
144 
145 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
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:385
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
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.
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:53
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.
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.
std::vector< state_definition > state
scrollbar_panel_definition(const config &cfg)
std::string missing_mandatory_wml_tag(const std::string &section, const std::string &tag)
Returns a standard message for a missing wml child (tag).
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.