The Battle for Wesnoth  1.19.7+dev
choose_addon.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
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"
20 #include "gui/widgets/listbox.hpp"
23 
24 namespace gui2::dialogs
25 {
26 
27 REGISTER_DIALOG(editor_choose_addon)
28 
30  : modal_dialog(window_id())
31  , addon_id_(addon_id)
32 {
34  find_widget<toggle_button>("show_all"),
35  std::bind(&editor_choose_addon::toggle_installed, this));
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  const widget_data& new_addon{
73  {"existing_addon_id", widget_item{{"label", _("New Add-on")}, {"tooltip", _("Create a new add-on")}}},
74  };
75  existing_addons.add_row(new_addon);
76 
77  if(show_all) {
78  const widget_data& mainline{
79  {"existing_addon_id",
80  widget_item{{"label", _("Mainline")}, {"tooltip", _("Mainline multiplayer scenarios")}}},
81  };
82  existing_addons.add_row(mainline);
83  }
84 
85  int selected_row = 0;
86  for(const std::string& dir : dirs) {
87  if((show_all || filesystem::file_exists(filesystem::get_addons_dir() + "/" + dir + "/_server.pbl"))
88  && filesystem::file_exists(filesystem::get_addons_dir() + "/" + dir + "/_main.cfg")) {
89  const widget_data& entry{
90  {"existing_addon_id", widget_item{{"label", dir}}},
91  };
92  existing_addons.add_row(entry);
93  if(dir == prefs::get().editor_chosen_addon()) {
94  selected_row = existing_addons.get_item_count()-1;
95  }
96  }
97  }
98 
99  existing_addons.select_row(selected_row);
100 }
101 
102 } // 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:267
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:280
void clear()
Removes all the rows in the listbox, clearing it.
Definition: listbox.cpp:153
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:305
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:159
const t_string & get_label() const
static prefs & get()
Declarations for File-IO.
static std::string _(const char *str)
Definition: gettext.hpp:93
void get_files_in_dir(const std::string &dir, std::vector< std::string > *files, std::vector< std::string > *dirs, name_mode mode, filter_mode filter, reorder_mode reorder, file_tree_checksum *checksum)
Get a list of all files and/or directories in a given directory.
Definition: filesystem.cpp:445
static bool file_exists(const bfs::path &fpath)
Definition: filesystem.cpp:326
std::string get_addons_dir()
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:36
std::map< std::string, t_string > widget_item
Definition: widget.hpp:33