The Battle for Wesnoth  1.19.7+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  , grid_(nullptr)
83 {
84  auto grid_definition = cfg.optional_child("definition");
85 
86  VALIDATE(grid_definition, _("No list defined."));
87  grid_ = std::make_shared<builder_grid>(*grid_definition);
88  assert(grid_);
89 }
90 
91 std::unique_ptr<widget> builder_scrollbar_panel::build() const
92 {
93  auto panel = std::make_unique<scrollbar_panel>(*this);
94 
95  DBG_GUI_G << "Window builder: placed scrollbar_panel '" << id
96  << "' with definition '" << definition << "'.";
97 
98  const auto conf = panel->cast_config_to<scrollbar_panel_definition>();
99  assert(conf);
100 
101  panel->init_grid(*conf->grid);
102  panel->finalize_setup();
103 
104  /*** Fill the content grid. ***/
105  grid* content_grid = panel->content_grid();
106  assert(content_grid);
107 
108  const unsigned rows = grid_->rows;
109  const unsigned cols = grid_->cols;
110 
111  content_grid->set_rows_cols(rows, cols);
112 
113  for(unsigned x = 0; x < rows; ++x) {
114  content_grid->set_row_grow_factor(x, grid_->row_grow_factor[x]);
115  for(unsigned y = 0; y < cols; ++y) {
116 
117  if(x == 0) {
118  content_grid->set_column_grow_factor(y,
119  grid_->col_grow_factor[y]);
120  }
121 
122  auto widget = grid_->widgets[x * cols + y]->build();
123  content_grid->set_child(std::move(widget),
124  x,
125  y,
126  grid_->flags[x * cols + y],
127  grid_->border_size[x * cols + y]);
128  }
129  }
130 
131  return panel;
132 }
133 
134 } // namespace implementation
135 
136 // }------------ END --------------
137 
138 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition: config.cpp:384
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:711
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:55
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
Generic file dialog.
Contains the implementation details for lexical_cast and shouldn't be used directly.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
virtual std::unique_ptr< widget > build() const override
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.