The Battle for Wesnoth  1.19.0-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"
20 #include "gui/widgets/label.hpp"
21 #include "gui/widgets/listbox.hpp"
23 #include "preferences/general.hpp"
24 
25 namespace gui2::dialogs
26 {
27 
28 REGISTER_DIALOG(editor_choose_addon)
29 
31  : modal_dialog(window_id())
32  , addon_id_(addon_id)
33 {
34  connect_signal_mouse_left_click(find_widget<toggle_button>(get_window(), "show_all", false),
35  std::bind(&editor_choose_addon::toggle_installed, this));
36 
37  populate_list(false);
38 }
39 
41 {
42 }
43 
45 {
46  listbox& existing_addons = find_widget<listbox>(&win, "existing_addons", false);
47  int selected_row = existing_addons.get_selected_row();
48 
49  if(selected_row == 0) {
50  addon_id_ = "///newaddon///";
52  } else if(selected_row == 1 && find_widget<toggle_button>(get_window(), "show_all", false).get_value_bool()) {
53  addon_id_ = "mainline";
55  } else {
56  grid* row = existing_addons.get_row_grid(selected_row);
57  addon_id_ = dynamic_cast<label*>(row->find("existing_addon_id", false))->get_label();
59  }
60 }
61 
63 {
64  toggle_button& show_all = find_widget<toggle_button>(get_window(), "show_all", false);
65  populate_list(show_all.get_value_bool());
66 }
67 
69 {
70  listbox& existing_addons = find_widget<listbox>(get_window(), "existing_addons", false);
71  existing_addons.clear();
72 
73  std::vector<std::string> dirs;
75 
76  const widget_data& new_addon{
77  {"existing_addon_id", widget_item{{"label", _("New Add-on")}, {"tooltip", _("Create a new add-on")}}},
78  };
79  existing_addons.add_row(new_addon);
80 
81  if(show_all) {
82  const widget_data& mainline{
83  {"existing_addon_id",
84  widget_item{{"label", _("Mainline")}, {"tooltip", _("Mainline multiplayer scenarios")}}},
85  };
86  existing_addons.add_row(mainline);
87  }
88 
89  int selected_row = 0;
90  for(const std::string& dir : dirs) {
91  if((show_all || filesystem::file_exists(filesystem::get_addons_dir() + "/" + dir + "/_server.pbl"))
92  && filesystem::file_exists(filesystem::get_addons_dir() + "/" + dir + "/_main.cfg")) {
93  const widget_data& entry{
94  {"existing_addon_id", widget_item{{"label", dir}}},
95  };
96  existing_addons.add_row(entry);
98  selected_row = existing_addons.get_item_count()-1;
99  }
100  }
101  }
102 
103  existing_addons.select_row(selected_row);
104 }
105 
106 } // namespace gui2::dialogs
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Abstract base class for all modal dialogs.
window * get_window()
Returns a pointer to the dialog's window.
Base container class.
Definition: grid.hpp:32
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:645
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
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:230
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:243
void clear()
Removes all the rows in the listbox, clearing it.
Definition: listbox.cpp:118
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:268
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:124
const t_string & get_label() const
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:61
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:405
static bool file_exists(const bfs::path &fpath)
Definition: filesystem.cpp:319
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:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
std::string editor_chosen_addon()
Definition: general.cpp:1210
void set_editor_chosen_addon(const std::string &addon_id)
Definition: general.cpp:1205
SDL_Window * get_window()
Definition: video.cpp:655