The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
choose_addon.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2009 - 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
16
17#include "filesystem.hpp"
18#include "gettext.hpp"
19#include "gui/widgets/label.hpp"
23
24namespace gui2::dialogs
25{
26
27REGISTER_DIALOG(editor_choose_addon)
28
30 : modal_dialog(window_id())
31 , addon_id_(addon_id)
32{
33 connect_signal_mouse_left_click(
34 find_widget<toggle_button>("show_all"),
36
37 populate_list(false);
38}
39
41{
42 listbox& existing_addons = find_widget<listbox>("existing_addons");
43 int selected_row = existing_addons.get_selected_row();
44
45 if(selected_row == 0) {
46 addon_id_ = "///newaddon///";
47 prefs::get().set_editor_chosen_addon("");
48 } else if(selected_row == 1 && find_widget<toggle_button>("show_all").get_value_bool()) {
49 addon_id_ = "mainline";
50 prefs::get().set_editor_chosen_addon("");
51 } else {
52 grid* row = existing_addons.get_row_grid(selected_row);
53 addon_id_ = dynamic_cast<label*>(row->find("existing_addon_id", false))->get_label();
54 prefs::get().set_editor_chosen_addon(addon_id_);
55 }
56}
57
59{
60 toggle_button& show_all = find_widget<toggle_button>("show_all");
61 populate_list(show_all.get_value_bool());
62}
63
65{
66 listbox& existing_addons = find_widget<listbox>("existing_addons");
67 existing_addons.clear();
68
69 std::vector<std::string> dirs;
71
72 existing_addons.add_row(widget_data{
73 { "existing_addon_id", {{ "label", _("New Add-on") }, { "tooltip", _("Create a new add-on") }}},
74 });
75
76 if(show_all) {
77 existing_addons.add_row(widget_data{
78 { "existing_addon_id", {{ "label", _("Mainline") }, { "tooltip", _("Mainline multiplayer scenarios") }}},
79 });
80 }
81
82 int selected_row = 0;
83 for(const std::string& dir : dirs) {
84 if((show_all || filesystem::file_exists(filesystem::get_addons_dir() + "/" + dir + "/_server.pbl"))
85 && filesystem::file_exists(filesystem::get_addons_dir() + "/" + dir + "/_main.cfg"))
86 {
87 existing_addons.add_row(widget_data{{ "existing_addon_id", {{ "label", dir }}}});
88 if(dir == prefs::get().editor_chosen_addon()) {
89 selected_row = existing_addons.get_item_count()-1;
90 }
91 }
92 }
93
94 existing_addons.select_row(selected_row);
95}
96
97} // namespace gui2::dialogs
virtual void post_show() override
Actions to be taken after the window has been shown.
Abstract base class for all modal dialogs.
Base container class.
Definition grid.hpp:32
widget * find(const std::string_view id, const bool must_be_active) override
See widget::find.
Definition grid.cpp:645
The listbox class.
Definition listbox.hpp:41
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:92
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition listbox.cpp:256
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition listbox.cpp:267
void clear()
Removes all the rows in the listbox, clearing it.
Definition listbox.cpp:147
int get_selected_row() const
Returns the first selected row.
Definition listbox.cpp:290
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition listbox.cpp:153
const t_string & get_label() const
static prefs & get()
Declarations for File-IO.
static std::string _(const char *str)
Definition gettext.hpp:100
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
bool file_exists(const std::string &name)
Returns true if a file or directory with such name already exists.
void get_files_in_dir(const std::string &dir, std::vector< std::string > *files, std::vector< std::string > *dirs=nullptr, name_mode mode=name_mode::FILE_NAME_ONLY, filter_mode filter=filter_mode::NO_FILTER, reorder_mode reorder=reorder_mode::DONT_REORDER, file_tree_checksum *checksum=nullptr)
Get a list of all files and/or directories in a given directory.
std::string get_addons_dir()
std::map< std::string, widget_item > widget_data
Definition widget.hpp:36
STL namespace.