The Battle for Wesnoth  1.15.12+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 <functional>
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 
66  : resolution_definition(cfg), grid()
67 {
68  // The panel needs to know the order.
69  state.emplace_back(cfg.child("background"));
70  state.emplace_back(cfg.child("foreground"));
71 
72  const config& child = cfg.child("grid");
73  VALIDATE(child, _("No grid defined."));
74 
75  grid = std::make_shared<builder_grid>(child);
76 }
77 
78 // }---------- BUILDER -----------{
79 
80 namespace implementation
81 {
82 
83 builder_scrollbar_panel::builder_scrollbar_panel(const config& cfg)
85  , vertical_scrollbar_mode(
86  get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
87  , horizontal_scrollbar_mode(
88  get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
89  , grid_(nullptr)
90 {
91  const config& grid_definition = cfg.child("definition");
92 
93  VALIDATE(grid_definition, _("No list defined."));
94  grid_ = std::make_shared<builder_grid>(grid_definition);
95  assert(grid_);
96 }
97 
99 {
100  scrollbar_panel* panel = new scrollbar_panel(*this);
101 
104 
105  DBG_GUI_G << "Window builder: placed scrollbar_panel '" << id
106  << "' with definition '" << definition << "'.\n";
107 
108  const auto conf = panel->cast_config_to<scrollbar_panel_definition>();
109  assert(conf);
110 
111  panel->init_grid(*conf->grid);
112  panel->finalize_setup();
113 
114  /*** Fill the content grid. ***/
115  grid* content_grid = panel->content_grid();
116  assert(content_grid);
117 
118  const unsigned rows = grid_->rows;
119  const unsigned cols = grid_->cols;
120 
121  content_grid->set_rows_cols(rows, cols);
122 
123  for(unsigned x = 0; x < rows; ++x) {
124  content_grid->set_row_grow_factor(x, grid_->row_grow_factor[x]);
125  for(unsigned y = 0; y < cols; ++y) {
126 
127  if(x == 0) {
128  content_grid->set_column_grow_factor(y,
129  grid_->col_grow_factor[y]);
130  }
131 
132  widget* widget = grid_->widgets[x * cols + y]->build();
133  content_grid->set_child(widget,
134  x,
135  y,
136  grid_->flags[x * cols + y],
137  grid_->border_size[x * cols + y]);
138  }
139  }
140 
141  return panel;
142 }
143 
144 } // namespace implementation
145 
146 // }------------ END --------------
147 
148 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:65
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:414
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
A panel is a visible container to hold multiple widgets.
Definition: panel.hpp:57
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:92
virtual widget * build() const override
Generic file dialog.
Definition: field-fwd.hpp:22
Visible container to hold multiple widgets.
Base container class.
Definition: grid.hpp:30
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: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:59
#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