1 // Copyright (C) 2007 Ole Laursen
2 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
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 Library General Public License for more details.
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
21 #include <sigc++/slot.h>
24 #include "splash-window.h"
27 #include "game-preferences-dialog.h"
28 #include "load-scenario-dialog.h"
29 #include "glade-helpers.h"
30 #include "Configuration.h"
34 #include "GameScenario.h"
35 #include "playerlist.h"
36 #include "network-game-selector-dialog.h"
37 #include "main-preferences-dialog.h"
38 #include "timed-message-dialog.h"
39 #include "new-random-map-dialog.h"
43 //void surface_attached_helper(GtkSDL *gtksdl, gpointer data)
45 //static_cast<SplashWindow*>(data)->on_sdl_surface_changed();
48 SplashWindow::SplashWindow()
50 network_game_nickname = "";
52 Glib::RefPtr<Gtk::Builder> xml
53 = Gtk::Builder::create_from_file(get_glade_path() + "/splash-window.ui");
55 xml->get_widget("window", window);
56 window->set_icon_from_file(File::getMiscFile("various/castle_icon.png"));
57 decorate(window, File::getMiscFile("various/back.bmp"));
58 window_closed.connect(sigc::mem_fun(this, &SplashWindow::on_window_closed));
60 window->signal_delete_event().connect
61 (sigc::mem_fun(*this, &SplashWindow::on_delete_event));
64 Gtk::Image *splash_image
65 = manage(new Gtk::Image(File::getMiscFile("/various/splash_screen.jpg")));
67 // the table is a hack to get the image shown behind the buttons
68 Gtk::Table *table = 0;
69 xml->get_widget("table", table);
70 table->attach(*splash_image, 0, 2, 0, 2, Gtk::EXPAND | Gtk::FILL);
72 xml->get_widget("load_game_button", load_game_button);
73 load_game_button->signal_clicked().connect
74 (sigc::mem_fun(*this, &SplashWindow::on_load_game_clicked));
75 xml->get_widget("load_scenario_button", load_scenario_button);
76 load_scenario_button->signal_clicked().connect
77 (sigc::mem_fun(*this, &SplashWindow::on_load_scenario_clicked));
78 xml->get_widget("quit_button", quit_button);
79 quit_button->signal_clicked().connect
80 (sigc::mem_fun(*this, &SplashWindow::on_quit_clicked));
81 xml->get_widget("new_network_game_button", new_network_game_button);
82 new_network_game_button->signal_clicked().connect
83 (sigc::mem_fun(*this, &SplashWindow::on_new_network_game_clicked));
84 xml->get_widget("new_pbm_game_button", new_pbm_game_button);
85 new_pbm_game_button->signal_clicked().connect
86 (sigc::mem_fun(*this, &SplashWindow::on_new_pbm_game_clicked));
87 xml->get_widget("preferences_button", preferences_button);
88 preferences_button->signal_clicked().connect
89 (sigc::mem_fun(*this, &SplashWindow::on_preferences_clicked));
90 Sound::getInstance()->playMusic("intro");
92 xml->get_widget("button_box", button_box);
93 if (Configuration::s_autosave_policy == 1)
96 std::string filename = File::getSavePath() + "autosave.sav";
97 FILE *fileptr = fopen (filename.c_str(), "r");
102 GameScenario::PlayMode mode =
103 GameScenario::loadPlayMode(File::getSavePath() + "autosave.sav",
105 if (mode == GameScenario::HOTSEAT && broken == false)
107 crash_button = Gtk::manage(new Gtk::Button());
108 crash_button->set_label(_("Rescue Crashed Game"));
109 button_box->pack_start(*crash_button, true, true, 0);
110 crash_button->signal_clicked().connect(sigc::mem_fun(*this, &SplashWindow::on_rescue_crashed_game_clicked));
111 button_box->reorder_child(*crash_button, 0);
118 SplashWindow::~SplashWindow()
120 Sound::deleteInstance();
125 void SplashWindow::show()
130 void SplashWindow::hide()
136 void SplashWindow::on_window_closed()
139 quit_requested.emit();
142 bool SplashWindow::on_delete_event(GdkEventAny *e)
144 quit_requested.emit();
149 void SplashWindow::on_quit_clicked()
151 quit_requested.emit();
154 void SplashWindow::on_rescue_crashed_game_clicked()
157 std::string filename = File::getSavePath() + "autosave.sav";
158 load_requested.emit(filename);
161 void SplashWindow::on_load_game_clicked()
163 Gtk::FileChooserDialog chooser(*window, _("Choose Game to Load"));
164 Gtk::FileFilter sav_filter;
165 sav_filter.add_pattern("*.sav");
166 chooser.set_filter(sav_filter);
167 chooser.set_current_folder(Configuration::s_savePath);
169 chooser.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
170 chooser.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
171 chooser.set_default_response(Gtk::RESPONSE_ACCEPT);
174 int res = chooser.run();
176 if (res == Gtk::RESPONSE_ACCEPT)
178 std::string filename = chooser.get_filename();
180 load_requested.emit(filename);
184 void SplashWindow::on_new_network_game_clicked()
186 Glib::RefPtr<Gtk::Builder> xml
187 = Gtk::Builder::create_from_file(get_glade_path() +
188 "/new-network-game-dialog.ui");
190 Gtk::RadioButton *client_radiobutton;
191 xml->get_widget("dialog", dialog);
192 xml->get_widget("client_radiobutton", client_radiobutton);
193 dialog->set_transient_for(*window);
194 Gtk::Entry *nick_entry;
195 xml->get_widget("nick_entry", nick_entry);
198 nick = getenv("USER");
199 else if (network_game_nickname != "")
200 nick = network_game_nickname;
203 nick_entry->set_text(nick);
204 nick_entry->set_activates_default(true);
205 int response = dialog->run();
208 if (response == Gtk::RESPONSE_ACCEPT) //we hit connect
210 network_game_nickname = nick_entry->get_text();
211 if (client_radiobutton->get_active() == true)
213 NetworkGameSelectorDialog ngsd;
214 ngsd.game_selected.connect(sigc::mem_fun(*this, &SplashWindow::on_network_game_selected));
219 //okay, we're a server.
220 LoadScenarioDialog d;
221 d.set_parent_window(*window);
223 std::string filename = d.get_scenario_filename();
224 if (filename.empty())
227 if (filename == "random.map")
229 NewRandomMapDialog nrmd;
230 nrmd.set_parent_window(*window);
231 int res = nrmd.run();
232 if (res == Gtk::RESPONSE_ACCEPT)
233 filename = nrmd.getRandomMapFilename();
238 GamePreferencesDialog gpd(filename, GameScenario::NETWORKED);
240 gpd.set_parent_window(*window);
241 gpd.set_title(_("New Networked Game"));
242 gpd.game_started.connect(sigc::mem_fun(*this, &SplashWindow::on_network_game_created));
243 gpd.run(network_game_nickname);
251 void SplashWindow::on_new_pbm_game_clicked()
253 LoadScenarioDialog d;
254 d.set_parent_window(*window);
257 std::string filename = d.get_scenario_filename();
258 if (filename.empty())
261 if (filename == "random.map")
263 NewRandomMapDialog nrmd;
264 nrmd.set_parent_window(*window);
265 int res = nrmd.run();
266 if (res == Gtk::RESPONSE_ACCEPT)
267 filename = nrmd.getRandomMapFilename();
271 GamePreferencesDialog gpd(filename, GameScenario::PLAY_BY_MAIL);
273 gpd.set_parent_window(*window);
274 gpd.set_title(_("New Play By Mail game"));
275 gpd.game_started.connect(sigc::mem_fun(*this, &SplashWindow::on_pbm_game_created));
280 void SplashWindow::on_load_scenario_clicked()
282 LoadScenarioDialog d;
284 d.set_parent_window(*window);
288 std::string filename = d.get_scenario_filename();
289 if (!filename.empty())
292 if (filename == "random.map")
294 NewRandomMapDialog nrmd;
295 nrmd.set_parent_window(*window);
296 int res = nrmd.run();
297 if (res == Gtk::RESPONSE_ACCEPT)
298 filename = nrmd.getRandomMapFilename();
302 GamePreferencesDialog gp(filename, GameScenario::HOTSEAT);
303 gp.set_parent_window(*window);
304 gp.game_started.connect(sigc::mem_fun(*this, &SplashWindow::on_game_started));
308 //load_requested.emit(filename);
311 void SplashWindow::on_network_game_selected(std::string ip, unsigned short port)
313 new_remote_network_game_requested.emit(ip, port, network_game_nickname);
316 void SplashWindow::on_game_started(GameParameters g)
318 new_game_requested.emit(g);
321 void SplashWindow::on_network_game_created(GameParameters g)
323 new_hosted_network_game_requested.emit(g, LORDSAWAR_PORT,
324 network_game_nickname);
327 void SplashWindow::on_pbm_game_created(GameParameters g)
329 new_pbm_game_requested.emit(g);
332 void SplashWindow::on_preferences_clicked()
334 bool saved = Configuration::s_decorated;
335 MainPreferencesDialog d;
336 d.set_parent_window(*window);
339 if (saved != Configuration::s_decorated)
341 TimedMessageDialog dialog(*window, _("Please exit the program and restart it for the changes to take effect."), 30);