The Battle for Wesnoth  1.19.12+dev
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"
20 #include "gui/widgets/listbox.hpp"
21 #include "gui/widgets/widget.hpp"
22 
23 #include <functional>
24 #include <string>
25 #include <vector>
26 
27 namespace gui2
28 {
29 
30 namespace implementation
31 {
32  struct builder_addon_list;
33 }
34 
35 class addon_list : public container_base
36 {
38 
39 public:
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(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. */
116  {
118  }
119 
120  /** Determines if install/uninstall buttons are shown for each widget. */
122  {
124  }
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 
152 private:
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. */
168  listbox& get_listbox();
169 
170  void finalize_setup();
171 
172 public:
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 
179 private:
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 
202 namespace 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:158
addon_op_func_t uninstall_function_
Definition: addon_list.hpp:159
void set_uninstall_function(addon_op_func_t function)
Sets the function to call when the player clicks the uninstall button.
Definition: addon_list.hpp:77
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
virtual bool get_active() const override
See styled_widget::get_active.
Definition: addon_list.hpp:141
std::function< bool(const addon_info &, const addon_info &)> addon_sort_func
Definition: addon_list.hpp:40
void set_publish_function(addon_op_func_t function)
Sets the function to upload an addon to the addons server.
Definition: addon_list.hpp:89
void set_addon_shown(boost::dynamic_bitset<> &shown)
Filters which add-ons are visible.
Definition: addon_list.hpp:101
void add_list_to_keyboard_chain()
Adds the internal listbox to the keyboard event chain.
Definition: addon_list.cpp:367
void set_install_status_visibility(visibility visibility)
Determines if install status of each widget is shown.
Definition: addon_list.hpp:115
void set_install_function(addon_op_func_t function)
Sets the function to call when the player clicks the install button.
Definition: addon_list.hpp:71
void set_delete_function(addon_op_func_t function)
Sets the function to install an addon from the addons server.
Definition: addon_list.hpp:95
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.
Definition: addon_list.cpp:135
void set_install_buttons_visibility(visibility visibility)
Determines if install/uninstall buttons are shown for each widget.
Definition: addon_list.hpp:121
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
const addon_info * get_selected_addon() const
Returns the selected add-on.
Definition: addon_list.cpp:310
virtual void set_active(const bool) override
See styled_widget::set_active.
Definition: addon_list.hpp:135
void set_update_function(addon_op_func_t function)
Sets the function to call when the player clicks the update button.
Definition: addon_list.hpp:83
void set_self_active(const bool) override
See container_base::set_self_active.
Definition: addon_list.hpp:184
visibility install_buttons_visibility_
Definition: addon_list.hpp:156
listbox & get_listbox()
Returns the underlying list box.
Definition: addon_list.cpp:362
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.
Definition: addon_list.hpp:130
std::string get_remote_addon_id()
Returns the selected add-on id, for use with remote publish/delete ops.
Definition: addon_list.cpp:321
const std::string display_title_full_shift(const addon_info &addon) const
Definition: addon_list.cpp:146
static const int DEFAULT_ACTION_RETVAL
Special retval for the toggle panels in the addons list.
Definition: addon_list.hpp:45
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...
Definition: addon_list.cpp:63
static std::string describe_status(const addon_tracking_info &info)
Definition: addon_list.cpp:96
std::vector< const addon_info * > addon_vector_
Definition: addon_list.hpp:153
addon_op_func_t update_function_
Definition: addon_list.hpp:160
void select_first_addon()
Choose the item at the top of the list (taking account of sort order).
Definition: addon_list.cpp:405
addon_op_func_t delete_function_
Definition: addon_list.hpp:163
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: addon_list.hpp:147
addon_list(const implementation::builder_addon_list &builder)
Definition: addon_list.cpp:51
addon_op_func_t publish_function_
Definition: addon_list.hpp:162
visibility install_status_visibility_
Definition: addon_list.hpp:155
void set_addon_order(const addon_sort_func &func)
Definition: addon_list.cpp:392
void select_addon(const std::string &id)
Selects the add-on with the given ID.
Definition: addon_list.cpp:331
std::function< void(const addon_info &)> addon_op_func_t
Definition: addon_list.hpp:61
void set_addons(const addons_list &addons)
Sets the add-ons to show.
Definition: addon_list.cpp:156
addon_op_func_t install_function_
Definition: addon_list.hpp:158
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:171
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
std::map< std::string, addon_info > addons_list
Definition: info.hpp:27
Generic file dialog.
std::shared_ptr< builder_grid > builder_grid_ptr
Contains the implementation details for lexical_cast and shouldn't be used directly.
logger & info()
Definition: log.cpp:318
ADDON_STATUS
Defines various add-on installation statuses.
Definition: state.hpp:22
Stores additional status information about add-ons.
Definition: state.hpp:47
addon_list_definition(const config &cfg)
Definition: addon_list.cpp:414
virtual std::unique_ptr< widget > build() const override
Definition: addon_list.cpp:465
virtual std::unique_ptr< widget > build() const=0