The Battle for Wesnoth  1.19.2+dev
tab_container.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2024
3  by Subhraman Sarkar (babaissarkar) <suvrax@gmail.com>
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 
20 #include "gui/core/log.hpp"
21 #include "gettext.hpp"
25 #include "gui/widgets/listbox.hpp"
26 #include "gui/widgets/settings.hpp"
27 #include "gui/widgets/window.hpp"
28 #include "wml_exception.hpp"
29 
30 #include <functional>
31 
32 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
33 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
34 
35 namespace gui2
36 {
37 
38 // ------------ WIDGET -----------{
39 
40 REGISTER_WIDGET(tab_container)
41 
42 tab_container::tab_container(const implementation::builder_tab_container& builder)
43  : container_base(builder, type())
44  , state_(ENABLED)
45 {
46 }
47 
48 void tab_container::set_self_active(const bool active)
49 {
50  state_ = active ? ENABLED : DISABLED;
51 }
52 
54 {
55  return state_ != DISABLED;
56 }
57 
58 unsigned tab_container::get_state() const
59 {
60  return state_;
61 }
62 
64 {
65  return true;
66 }
67 
69 {
70  return find_widget<listbox>(&get_grid(), "_tab_list", false);
71 }
72 
73 void tab_container::finalize(std::unique_ptr<generator_base> generator)
74 {
75  generator_ = generator.get();
76  assert(generator_);
77 
78  widget_item empty_data;
79  for(const auto& builder_entry : builders_) {
80  generator->create_item(-1, *builder_entry, empty_data, nullptr);
81  }
82 
83  grid* parent_grid = find_widget<grid>(this, "_content_grid", false, true);
84  if (parent_grid) {
85  parent_grid->swap_child("_page", std::move(generator), false);
86  }
87 
89 
90  select_tab(0);
91 }
92 
94  for (const widget_data& row : list_items_) {
95  add_tab_entry(row);
96  }
98 };
99 
101 {
102  listbox& list = get_internal_list();
103  list.add_row(row);
104 }
105 
107 {
108  if (index < get_tab_count()) {
110  generator_->select_item(index, true);
111  }
112 }
113 
116  place(get_origin(), get_size());
117  queue_redraw();
118 
119  fire(event::NOTIFY_MODIFIED, *this, nullptr);
120 }
121 
122 // }---------- DEFINITION ---------{
123 
126 {
127  DBG_GUI_P << "Parsing tab_container " << id;
128 
129  load_resolutions<resolution>(cfg);
130 }
131 
133  : resolution_definition(cfg), grid(nullptr)
134 {
135  // Note the order should be the same as the enum state_t is tab_container.hpp.
136  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", missing_mandatory_wml_tag("tab_container_definition][resolution", "state_enabled")));
137  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_disabled", missing_mandatory_wml_tag("tab_container_definition][resolution", "state_disabled")));
138 
139  auto child = VALIDATE_WML_CHILD(cfg, "grid", _("No grid defined for tab container control"));
140  grid = std::make_shared<builder_grid>(child);
141 }
142 
143 // }---------- BUILDER -----------{
144 
145 namespace implementation
146 {
147 
148 builder_tab_container::builder_tab_container(const config& cfg)
150 {
151  if (cfg.has_child("tab")) {
152  for(const auto & tab : cfg.child_range("tab"))
153  {
154  widget_data list_row;
156 
157  item["label"] = tab["image"];
158  list_row.emplace("image", item);
159  item["label"] = tab["name"];
160  list_row.emplace("name", item);
161 
162  list_items.emplace_back(list_row);
163 
164  if (tab.has_child("data")) {
165  auto builder = std::make_shared<builder_grid>(tab.mandatory_child("data"));
166  builders.push_back(builder);
167  }
168  }
169  }
170 }
171 
172 std::unique_ptr<widget> builder_tab_container::build() const
173 {
174  auto widget = std::make_unique<tab_container>(*this);
175 
176  const auto conf = widget->cast_config_to<tab_container_definition>();
177  assert(conf);
178 
179  widget->init_grid(*conf->grid);
180 
181  widget->set_items(list_items);
182  widget->set_builders(builders);
183 
185  widget->finalize(std::move(generator));
186 
187  DBG_GUI_G << "Window builder: placed tab_container '" << id
188  << "' with definition '" << definition << "'.";
189 
190  return widget;
191 }
192 
193 } // namespace implementation
194 
195 // }------------ END --------------
196 
197 } //
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Definition: config.cpp:317
child_itors child_range(config_key_type key)
Definition: config.cpp:273
A generic container base class.
const grid & get_grid() const
virtual void place(const point &origin, const point &size) override
See widget::place.
void connect_signal(const F &func, const queue_position position=back_child)
Adds a callback to the appropriate queue based on event type.
Definition: dispatcher.hpp:351
bool fire(const ui_event event, widget &target)
Fires an event which has no extra parameters.
Definition: dispatcher.cpp:74
virtual void select_item(const unsigned index, const bool select)=0
(De)selects an item.
static std::unique_ptr< generator_base > build(const bool has_minimum, const bool has_maximum, const placement placement, const bool select)
Create a new generator.
Definition: generator.cpp:1160
Basic template class to generate new items.
grid & create_item(const int index, const builder_grid &list_builder, const widget_item &item_data, const std::function< void(widget &)> &callback) override
Inherited from generator_base.
Base container class.
Definition: grid.hpp:32
std::unique_ptr< widget > swap_child(const std::string &id, std::unique_ptr< widget > w, const bool recurse, widget *new_parent=nullptr)
Exchanges a child in the grid.
Definition: grid.cpp:101
The listbox class.
Definition: listbox.hpp:43
grid & add_row(const widget_item &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:59
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:243
A container widget that shows one of its pages of widgets depending on which tab the user clicked.
state_t state_
Current state of the widget.
virtual unsigned get_state() const override
Returns the id of the state.
void finalize(std::unique_ptr< generator_base > generator)
Finishes the building initialization of the widget.
generator_base * generator_
Contains a pointer to the generator.
virtual bool get_active() const override
Gets the active state of the styled_widget.
unsigned get_tab_count() const
virtual void set_self_active(const bool active) override
Helper for set_active.
bool can_wrap() const override
See widget::can_wrap.
std::vector< std::shared_ptr< builder_grid > > builders_
void add_tab_entry(const widget_data row)
void select_tab(unsigned index)
std::vector< widget_data > list_items_
listbox & get_internal_list()
Get the listbox inside which the tabs are shown.
unsigned get_active_tab_index()
Base class for all widgets.
Definition: widget.hpp:53
void queue_redraw()
Indicates that this widget should be redrawn.
Definition: widget.cpp:454
point get_origin() const
Returns the screen origin of the widget.
Definition: widget.cpp:301
point get_size() const
Returns the size of the widget.
Definition: widget.cpp:306
static std::string _(const char *str)
Definition: gettext.hpp:93
Define the common log macros for the gui toolkit.
#define DBG_GUI_G
Definition: log.hpp:41
#define DBG_GUI_P
Definition: log.hpp:66
This file contains the window object, this object is a top level container which has the event manage...
@ NOTIFY_MODIFIED
Definition: handler.hpp:158
Generic file dialog.
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:411
Contains the implementation details for lexical_cast and shouldn't be used directly.
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
This file contains the settings handling of the widget library.
std::string definition
Parameters for the styled_widget.
virtual std::unique_ptr< widget > build() const override
std::vector< std::shared_ptr< builder_grid > > builders
std::vector< state_definition > state
tab_container_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)