The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
addon_list.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 - 2025
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#pragma once
16
17#include "addon/info.hpp"
18#include "addon/state.hpp"
22
23#include <functional>
24#include <string>
25#include <vector>
26
27namespace gui2
28{
29
30namespace implementation
31{
32 struct builder_addon_list;
33}
34
36{
38
39public:
40 using addon_sort_func = std::function<bool(const addon_info&, const addon_info&)>;
41
42 explicit addon_list(const implementation::builder_addon_list& builder);
43
44 /** Special retval for the toggle panels in the addons list */
45 static const int DEFAULT_ACTION_RETVAL = 200;
46
47 const std::string display_title_full_shift_markup(const addon_info& addon) const;
48
49 /** Sets the add-ons to show. */
50 void set_addons(const addons_list& addons);
51
52 /** Returns the selected add-on. */
53 const addon_info* get_selected_addon() const;
54
55 /** Returns the selected add-on id, for use with remote publish/delete ops. */
56 std::string get_remote_addon_id();
57
58 /** Selects the add-on with the given ID. */
59 void select_addon(const std::string& id);
60
61 using addon_op_func_t = std::function<void(const addon_info&)>;
62
63 /**
64 * Helper to wrap the execution of any of the addon operation functions.
65 * It catches addons_client::user_exit exceptions and halts GUI2 event execution
66 * after calling the given function.
67 */
68 void addon_action_wrapper(addon_op_func_t& func, const addon_info& addon, bool& handled, bool& halt);
69
70 /** Sets the function to call when the player clicks the install button. */
72 {
73 install_function_ = function;
74 }
75
76 /** Sets the function to call when the player clicks the uninstall button. */
78 {
79 uninstall_function_ = function;
80 }
81
82 /** Sets the function to call when the player clicks the update button. */
84 {
85 update_function_ = function;
86 }
87
88 /** Sets the function to upload an addon to the addons server. */
90 {
91 publish_function_ = function;
92 }
93
94 /** Sets the function to install an addon from the addons server. */
96 {
97 delete_function_ = function;
98 }
99
100 /** Filters which add-ons are visible. 1 = visible, 0 = hidden. */
101 void set_addon_shown(boost::dynamic_bitset<>& shown)
102 {
103 get_listbox().set_row_shown(shown);
104 }
105
106 void set_addon_order(const addon_sort_func& func);
107
108 /**
109 * Changes the color of an add-on state string (installed, outdated, etc.) according to the state itself.
110 * This function is here because the add-on list widget itself needs it.
111 */
112 static std::string colorize_addon_state_string(const std::string& str, ADDON_STATUS state, bool verbose = false);
113
114 /** Determines if install status of each widget is shown. */
119
120 /** Determines if install/uninstall buttons are shown for each widget. */
125
126 /** Adds the internal listbox to the keyboard event chain. */
128
129 /** Sets up a callback that will be called when the player changes the sorting order. */
130 void set_callback_order_change(std::function<void(unsigned, sort_order::type)> callback) {
132 }
133
134 /** See @ref styled_widget::set_active. */
135 virtual void set_active(const bool) override
136 {
137 // DO NOTHING
138 }
139
140 /** See @ref styled_widget::get_active. */
141 virtual bool get_active() const override
142 {
143 return true;
144 }
145
146 /** See @ref styled_widget::get_state. */
147 virtual unsigned get_state() const override
148 {
149 return 0;
150 }
151
152private:
153 std::vector<const addon_info*> addon_vector_;
154
157
161
164
165 static std::string describe_status(const addon_tracking_info& info);
166
167 /** Returns the underlying list box. */
169
170 void finalize_setup();
171
172public:
173 /** Choose the item at the top of the list (taking account of sort order). */
174 void select_first_addon();
175
176 /** Static type getter that does not rely on the widget being constructed. */
177 static const std::string& type();
178
179private:
180 /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
181 virtual const std::string& get_control_type() const override;
182
183 /** See @ref container_base::set_self_active */
184 void set_self_active(const bool) override
185 {
186 // DO NOTHING
187 }
188};
189
191{
192 explicit addon_list_definition(const config& cfg);
193
195 {
196 explicit resolution(const config& cfg);
197
199 };
200};
201
202namespace implementation
203{
204
206{
207 explicit builder_addon_list(const config& cfg);
208
210
211 virtual std::unique_ptr<widget> build() const override;
212
215};
216}
217}
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
addon_op_func_t uninstall_function_
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
void set_uninstall_function(addon_op_func_t function)
Sets the function to call when the player clicks the uninstall button.
virtual bool get_active() const override
See styled_widget::get_active.
std::function< bool(const addon_info &, const addon_info &)> addon_sort_func
void set_publish_function(addon_op_func_t function)
Sets the function to upload an addon to the addons server.
void set_addon_shown(boost::dynamic_bitset<> &shown)
Filters which add-ons are visible.
void add_list_to_keyboard_chain()
Adds the internal listbox to the keyboard event chain.
void set_install_status_visibility(visibility visibility)
Determines if install status of each widget is shown.
void set_install_function(addon_op_func_t function)
Sets the function to call when the player clicks the install button.
void set_delete_function(addon_op_func_t function)
Sets the function to install an addon from the addons server.
void addon_action_wrapper(addon_op_func_t &func, const addon_info &addon, bool &handled, bool &halt)
Helper to wrap the execution of any of the addon operation functions.
const std::string display_title_full_shift_markup(const addon_info &addon) const
void set_install_buttons_visibility(visibility visibility)
Determines if install/uninstall buttons are shown for each widget.
const addon_info * get_selected_addon() const
Returns the selected add-on.
virtual void set_active(const bool) override
See styled_widget::set_active.
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
void set_update_function(addon_op_func_t function)
Sets the function to call when the player clicks the update button.
void set_self_active(const bool) override
See container_base::set_self_active.
visibility install_buttons_visibility_
listbox & get_listbox()
Returns the underlying list box.
void set_callback_order_change(std::function< void(unsigned, sort_order::type)> callback)
Sets up a callback that will be called when the player changes the sorting order.
std::string get_remote_addon_id()
Returns the selected add-on id, for use with remote publish/delete ops.
static const int DEFAULT_ACTION_RETVAL
Special retval for the toggle panels in the addons list.
static std::string colorize_addon_state_string(const std::string &str, ADDON_STATUS state, bool verbose=false)
Changes the color of an add-on state string (installed, outdated, etc.) according to the state itself...
static std::string describe_status(const addon_tracking_info &info)
std::vector< const addon_info * > addon_vector_
addon_op_func_t update_function_
void select_first_addon()
Choose the item at the top of the list (taking account of sort order).
addon_op_func_t delete_function_
virtual unsigned get_state() const override
See styled_widget::get_state.
addon_op_func_t publish_function_
visibility install_status_visibility_
void set_addon_order(const addon_sort_func &func)
void select_addon(const std::string &id)
Selects the add-on with the given ID.
std::function< void(const addon_info &)> addon_op_func_t
void set_addons(const addons_list &addons)
Sets the add-ons to show.
addon_op_func_t install_function_
A generic container base class.
The listbox class.
Definition listbox.hpp:41
void set_row_shown(const unsigned row, const bool shown)
Makes a row visible or invisible.
Definition listbox.cpp:163
void set_callback_order_change(std::function< void(unsigned, sort_order::type)> callback)
Registers a callback to be called when the active sorting option changes.
Definition listbox.hpp:331
visibility
Visibility settings done by the user.
Definition widget.hpp:65
const config * cfg
std::map< std::string, addon_info > addons_list
Definition info.hpp:28
Generic file dialog.
std::shared_ptr< builder_grid > builder_grid_ptr
Contains the implementation details for lexical_cast and shouldn't be used directly.
ADDON_STATUS
Defines various add-on installation statuses.
Definition state.hpp:22
Stores additional status information about add-ons.
Definition state.hpp:47
virtual std::unique_ptr< widget > build() const override
virtual std::unique_ptr< widget > build() const=0