1 /*
2  * Copyright (C) 2002-2020 by the Widelands Development Team
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19 
20 #include "editor/ui_menus/main_menu_load_map.h"
21 
22 #include "base/i18n.h"
23 #include "editor/editorinteractive.h"
24 #include "io/filesystem/layered_filesystem.h"
25 #include "map_io/widelands_map_loader.h"
26 #include "wui/mapdetails.h"
27 #include "wui/maptable.h"
28 
29 /**
30  * Create all the buttons etc...
31  */
MainMenuLoadMap(EditorInteractive & parent,UI::UniqueWindow::Registry & registry)32 MainMenuLoadMap::MainMenuLoadMap(EditorInteractive& parent, UI::UniqueWindow::Registry& registry)
33    : MainMenuLoadOrSaveMap(parent, registry, "load_map_menu", _("Load Map")) {
34 	set_current_directory(curdir_);
35 
36 	table_.selected.connect([this](unsigned) { entry_selected(); });
37 	table_.double_clicked.connect([this](unsigned) { clicked_ok(); });
38 
39 	ok_.sigclicked.connect([this]() { clicked_ok(); });
40 	cancel_.sigclicked.connect([this]() { die(); });
41 }
42 
clicked_ok()43 void MainMenuLoadMap::clicked_ok() {
44 	assert(ok_.enabled());
45 	assert(table_.has_selection());
46 	const MapData& mapdata = maps_data_[table_.get_selected()];
47 	if (g_fs->is_directory(mapdata.filename) &&
48 	    !Widelands::WidelandsMapLoader::is_widelands_map(mapdata.filename)) {
49 		set_current_directory(mapdata.filename);
50 		fill_table();
51 	} else {
52 		EditorInteractive& eia = dynamic_cast<EditorInteractive&>(*get_parent());
53 		eia.egbase().create_loader_ui({"editor"}, true, "images/loadscreens/editor.jpg");
54 		eia.load(mapdata.filename);
55 		// load() will delete us.
56 		eia.egbase().remove_loader_ui();
57 	}
58 }
59 
set_current_directory(const std::string & filename)60 void MainMenuLoadMap::set_current_directory(const std::string& filename) {
61 	curdir_ = filename;
62 
63 	std::string display_dir = curdir_.substr(basedir_.size());
64 	if (boost::starts_with(display_dir, "/")) {
65 		display_dir = display_dir.substr(1);
66 	}
67 	if (boost::starts_with(display_dir, "My_Maps")) {
68 		boost::replace_first(display_dir, "My_Maps", _("My Maps"));
69 	} else if (boost::starts_with(display_dir, "MP_Scenarios")) {
70 		boost::replace_first(display_dir, "MP_Scenarios", _("Multiplayer Scenarios"));
71 	}
72 	/** TRANSLATORS: The folder that a file will be saved to. */
73 	directory_info_.set_text((boost::format(_("Current directory: %s")) % display_dir).str());
74 }
75 
76 /**
77  * Called when a entry is selected
78  */
entry_selected()79 void MainMenuLoadMap::entry_selected() {
80 	bool has_selection = table_.has_selection();
81 	ok_.set_enabled(has_selection);
82 	if (!has_selection) {
83 		map_details_.clear();
84 	} else {
85 		map_details_.update(
86 		   maps_data_[table_.get_selected()], !cb_dont_localize_mapnames_->get_state());
87 	}
88 }
89