initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / splash-window.cpp
1 //  Copyright (C) 2007 Ole Laursen
2 //  Copyright (C) 2007, 2008, 2009 Ben Asselstine
3 //
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.
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 Library 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 
17 //  02110-1301, USA.
18
19 #include <config.h>
20
21 #include <sigc++/slot.h>
22 #include <gtkmm.h>
23
24 #include "splash-window.h"
25 #include "driver.h"
26
27 #include "game-preferences-dialog.h"
28 #include "load-scenario-dialog.h"
29 #include "glade-helpers.h"
30 #include "Configuration.h"
31 #include "defs.h"
32 #include "sound.h"
33 #include "File.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"
40
41 //namespace
42 //{
43   //void surface_attached_helper(GtkSDL *gtksdl, gpointer data)
44     //{
45       //static_cast<SplashWindow*>(data)->on_sdl_surface_changed();
46     //}
47 //}
48 SplashWindow::SplashWindow()
49 {
50   network_game_nickname = "";
51
52     Glib::RefPtr<Gtk::Builder> xml
53         = Gtk::Builder::create_from_file(get_glade_path() + "/splash-window.ui");
54
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));
59
60     window->signal_delete_event().connect
61       (sigc::mem_fun(*this, &SplashWindow::on_delete_event));
62     
63     // load background
64     Gtk::Image *splash_image
65         = manage(new Gtk::Image(File::getMiscFile("/various/splash_screen.jpg")));
66
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);
71     
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");
91
92     xml->get_widget("button_box", button_box);
93     if (Configuration::s_autosave_policy == 1)
94       {
95   
96         std::string filename = File::getSavePath() + "autosave.sav";
97         FILE *fileptr = fopen (filename.c_str(), "r");
98         if (fileptr)
99           {
100             bool broken = false;
101             fclose (fileptr);
102             GameScenario::PlayMode mode = 
103               GameScenario::loadPlayMode(File::getSavePath() + "autosave.sav",
104                                          broken);
105             if (mode == GameScenario::HOTSEAT && broken == false)
106               {
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);
112               }
113           }
114       }
115
116 }
117
118 SplashWindow::~SplashWindow()
119 {
120     Sound::deleteInstance();
121     //clearData();
122     delete window;
123 }
124
125 void SplashWindow::show()
126 {
127     window->show_all();
128 }
129
130 void SplashWindow::hide()
131 {
132     window->hide();
133 }
134
135     
136 void SplashWindow::on_window_closed()
137 {
138   hide();
139   quit_requested.emit();
140 }
141
142 bool SplashWindow::on_delete_event(GdkEventAny *e)
143 {
144     quit_requested.emit();
145
146     return true;
147 }
148
149 void SplashWindow::on_quit_clicked()
150 {
151   quit_requested.emit();
152 }
153
154 void SplashWindow::on_rescue_crashed_game_clicked()
155 {
156   delete crash_button;
157   std::string filename = File::getSavePath() + "autosave.sav";
158   load_requested.emit(filename);
159 }
160
161 void SplashWindow::on_load_game_clicked()
162 {
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);
168
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);
172         
173     chooser.show_all();
174     int res = chooser.run();
175     
176     if (res == Gtk::RESPONSE_ACCEPT)
177     {
178         std::string filename = chooser.get_filename();
179         chooser.hide(); 
180         load_requested.emit(filename);
181     }
182 }
183
184 void SplashWindow::on_new_network_game_clicked()
185 {
186   Glib::RefPtr<Gtk::Builder> xml
187     = Gtk::Builder::create_from_file(get_glade_path() + 
188                                 "/new-network-game-dialog.ui");
189   Gtk::Dialog* dialog;
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);
196   std::string nick;
197   if (getenv("USER"))
198     nick = getenv("USER");
199   else if (network_game_nickname != "")
200     nick = network_game_nickname;
201   else
202     nick = "guest";
203   nick_entry->set_text(nick);
204   nick_entry->set_activates_default(true);
205   int response = dialog->run();
206   dialog->hide();
207   delete dialog;
208   if (response == Gtk::RESPONSE_ACCEPT) //we hit connect
209     {
210       network_game_nickname = nick_entry->get_text();
211       if (client_radiobutton->get_active() == true)
212         {
213           NetworkGameSelectorDialog ngsd;
214           ngsd.game_selected.connect(sigc::mem_fun(*this, &SplashWindow::on_network_game_selected));
215           ngsd.run();
216         }
217       else
218         {
219           //okay, we're a server.
220           LoadScenarioDialog d;
221           d.set_parent_window(*window);
222           d.run();
223           std::string filename = d.get_scenario_filename();
224           if (filename.empty())
225             return;
226           d.hide();
227           if (filename == "random.map")
228             {
229               NewRandomMapDialog nrmd;
230               nrmd.set_parent_window(*window);
231               int res = nrmd.run();
232               if (res == Gtk::RESPONSE_ACCEPT)
233                 filename = nrmd.getRandomMapFilename();
234               else
235                 return;
236             }
237
238           GamePreferencesDialog gpd(filename, GameScenario::NETWORKED);
239
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);
244           gpd.hide();
245           return;
246         }
247     }
248
249 }
250
251 void SplashWindow::on_new_pbm_game_clicked()
252 {
253   LoadScenarioDialog d;
254   d.set_parent_window(*window);
255   d.run();
256
257   std::string filename = d.get_scenario_filename();
258   if (filename.empty())
259     return;
260   d.hide();
261   if (filename == "random.map")
262     {
263       NewRandomMapDialog nrmd;
264       nrmd.set_parent_window(*window);
265       int res = nrmd.run();
266       if (res == Gtk::RESPONSE_ACCEPT)
267         filename = nrmd.getRandomMapFilename();
268       else
269         return;
270     }
271   GamePreferencesDialog gpd(filename, GameScenario::PLAY_BY_MAIL);
272
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));
276   gpd.run();
277   gpd.hide();
278 }
279
280 void SplashWindow::on_load_scenario_clicked()
281 {
282   LoadScenarioDialog d;
283
284   d.set_parent_window(*window);
285
286   d.run();
287
288   std::string filename = d.get_scenario_filename();
289   if (!filename.empty())
290     {
291       d.hide();
292       if (filename == "random.map")
293         {
294           NewRandomMapDialog nrmd;
295           nrmd.set_parent_window(*window);
296           int res = nrmd.run();
297           if (res == Gtk::RESPONSE_ACCEPT)
298             filename = nrmd.getRandomMapFilename();
299           else
300             return;
301         }
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));
305
306       gp.run();
307     } 
308   //load_requested.emit(filename);
309 }
310
311 void SplashWindow::on_network_game_selected(std::string ip, unsigned short port)
312 {
313   new_remote_network_game_requested.emit(ip, port, network_game_nickname);
314 }
315
316 void SplashWindow::on_game_started(GameParameters g)
317 {
318   new_game_requested.emit(g);
319 }
320
321 void SplashWindow::on_network_game_created(GameParameters g)
322 {
323   new_hosted_network_game_requested.emit(g, LORDSAWAR_PORT, 
324                                          network_game_nickname);
325 }
326
327 void SplashWindow::on_pbm_game_created(GameParameters g)
328 {
329   new_pbm_game_requested.emit(g);
330 }
331
332 void SplashWindow::on_preferences_clicked()
333 {
334   bool saved = Configuration::s_decorated;
335   MainPreferencesDialog d;
336   d.set_parent_window(*window);
337   d.run();
338   d.hide();
339   if (saved != Configuration::s_decorated)
340     {
341       TimedMessageDialog dialog(*window, _("Please exit the program and restart it for the changes to take effect."), 30);
342       dialog.run();
343     }
344 }