]> icculus.org git repositories - taylor/freespace2.git/blob - src/launcher/launcher.cpp
initial launchersetup merge
[taylor/freespace2.git] / src / launcher / launcher.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 #include "launcher.h"
10 #include "launchersetup.h"
11
12 #include "wx/filename.h"
13 #include "wx/stdpaths.h"
14
15 #include "pstypes.h"
16 #include "osregistry.h"
17 #include "cfile.h"
18
19 #ifndef MAKE_FS1
20 #include "res/fs2_background.xpm"
21 #include "res/fs2_btn_help.xpm"
22 #include "res/fs2_btn_help-hover.xpm"
23 #include "res/fs2_btn_help-click.xpm"
24 #include "res/fs2_btn_play.xpm"
25 #include "res/fs2_btn_play-hover.xpm"
26 #include "res/fs2_btn_play-click.xpm"
27 #include "res/fs2_btn_pxo.xpm"
28 #include "res/fs2_btn_pxo-hover.xpm"
29 #include "res/fs2_btn_pxo-click.xpm"
30 #include "res/fs2_btn_quit.xpm"
31 #include "res/fs2_btn_quit-hover.xpm"
32 #include "res/fs2_btn_quit-click.xpm"
33 #include "res/fs2_btn_readme.xpm"
34 #include "res/fs2_btn_readme-hover.xpm"
35 #include "res/fs2_btn_readme-click.xpm"
36 #include "res/fs2_btn_setup.xpm"
37 #include "res/fs2_btn_setup-hover.xpm"
38 #include "res/fs2_btn_setup-click.xpm"
39 #include "res/fs2_btn_uninstall.xpm"
40 #include "res/fs2_btn_uninstall-hover.xpm"
41 #include "res/fs2_btn_uninstall-click.xpm"
42 #include "res/fs2_btn_update.xpm"
43 #include "res/fs2_btn_update-hover.xpm"
44 #include "res/fs2_btn_update-click.xpm"
45 #include "res/fs2_btn_volition.xpm"
46 #include "res/fs2_btn_volition-hover.xpm"
47 #include "res/fs2_btn_volition-click.xpm"
48
49 #include "res/fs2_snd_hover_wav.inc"
50 #include "res/fs2_snd_click_wav.inc"
51 #else
52 #include "res/freespace_img.xpm"
53 #include "res/volition_img.xpm"
54 #endif
55
56
57
58 class LauncherApp: public wxApp
59 {
60         public:
61                 virtual bool OnInit();
62 };
63
64
65 IMPLEMENT_APP(LauncherApp)
66
67 bool LauncherApp::OnInit()
68 {
69         Launcher *frame = new Launcher(NULL);
70
71         frame->Show();
72         SetTopWindow(frame);
73
74         return true;
75 }
76
77 bool wxBackgroundBitmap::ProcessEvent(wxEvent &Event)
78 {
79         if (Event.GetEventType() == wxEVT_ERASE_BACKGROUND) {
80                 wxEraseEvent &EraseEvent = dynamic_cast<wxEraseEvent &>(Event);
81                 wxDC *DC = EraseEvent.GetDC();
82                 DC->DrawBitmap(Bitmap, 0, 0, false);
83
84                 return true;
85         } else {
86                 return Inherited::ProcessEvent(Event);
87         }
88 }
89
90
91 wxBEGIN_EVENT_TABLE(wxLauncherButton, wxStaticBitmap)
92         EVT_LEFT_DOWN(wxLauncherButton::onMouseDown)
93         EVT_LEFT_UP(wxLauncherButton::onMouseUp)
94         EVT_MOTION(wxLauncherButton::onMouseEnter)
95 wxEND_EVENT_TABLE()
96
97
98 wxLauncherButton::wxLauncherButton(wxWindow *parent, wxWindowID id, const wxBitmap& label, const wxPoint& pos, const wxSize& size)
99 {
100         this->Create(parent, id, label, pos, size);
101
102         parent->Connect(wxEVT_MOTION, wxMouseEventHandler(wxLauncherButton::onMouseLeave), NULL, this);
103
104         m_bitmap = label;
105
106         m_in_hover = false;
107 }
108
109 wxLauncherButton::~wxLauncherButton()
110 {
111 }
112
113 void wxLauncherButton::onMouseEnter(wxMouseEvent& event)
114 {
115         if ( !m_in_hover ) {
116                 this->SetBitmap(m_bitmap_hover);
117
118                 ((Launcher*)GetGrandParent())->SndPlayHover();
119
120                 m_in_hover = true;
121
122                 // hack to toggle off certain buttons that don't get the onMouseLeave
123                 // call due to placement/overlap
124                 {
125                         wxMouseEvent mv;
126
127                         const wxWindowList wl = GetParent()->GetChildren();
128
129                         wxWindowList::compatibility_iterator node = wl.GetFirst();
130
131                         while (node) {
132                                 wxLauncherButton *cur = (wxLauncherButton*)node->GetData();
133
134                                 if ( cur->GetId() != event.GetId() ) {
135                                         cur->onMouseLeave(mv);
136                                 }
137
138                                 node = node->GetNext();
139                         }
140                 }
141         }
142
143         event.Skip();
144 }
145
146 void wxLauncherButton::onMouseLeave(wxMouseEvent& event)
147 {
148         if (m_in_hover) {
149                 this->SetBitmap(m_bitmap);
150
151                 m_in_hover = false;
152         }
153
154         event.Skip();
155 }
156
157 void wxLauncherButton::onMouseDown(wxMouseEvent& event)
158 {
159         this->SetBitmap(m_bitmap_pressed);
160
161         ((Launcher*)GetGrandParent())->SndPlayPressed();
162
163         event.Skip();
164 }
165
166 void wxLauncherButton::onMouseUp(wxMouseEvent& event)
167 {
168         this->SetBitmap(m_bitmap_hover);
169
170         wxCommandEvent ev(wxEVT_COMMAND_BUTTON_CLICKED, event.GetId());
171         GetGrandParent()->GetEventHandler()->ProcessEvent(ev);
172
173         event.Skip();
174 }
175
176
177 wxBEGIN_EVENT_TABLE(Launcher, wxDialog)
178         EVT_CLOSE(Launcher::OnClose)
179         EVT_BUTTON(ID_B_PLAY, Launcher::OnPlay)
180         EVT_BUTTON(ID_B_SETUP, Launcher::OnSetup)
181         EVT_BUTTON(ID_B_README, Launcher::OnReadme)
182         EVT_BUTTON(ID_B_UPDATE, Launcher::OnUpdate)
183         EVT_BUTTON(ID_B_HELP, Launcher::OnHelp)
184         EVT_BUTTON(ID_B_UNINSTALL, Launcher::OnUninstall)
185         EVT_BUTTON(ID_B_VOLITION, Launcher::OnVolition)
186         EVT_BUTTON(ID_B_PXO, Launcher::OnPXO)
187         EVT_BUTTON(ID_B_QUIT, Launcher::OnQuit)
188 wxEND_EVENT_TABLE()
189
190
191 Launcher::Launcher( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style )
192         : wxDialog( parent, id, title, pos, size, style )
193 {
194         this->SetBackgroundColour( wxColour( 0, 0, 0 ) );
195
196         use_sound = false;
197
198 #ifndef MAKE_FS1
199         this->SetClientSize(375, 440);
200         this->SetTitle( wxT("FreeSpace 2 Launcher") );
201
202         init_sound();
203
204
205         p_background = new wxBackgroundBitmap(fs2_background_xpm);
206
207         m_panel = new wxPanel(this);
208         m_panel->PushEventHandler(p_background);
209
210
211         m_btn_Play = new wxLauncherButton( m_panel, ID_B_PLAY, wxBitmap(fs2_btn_play_xpm), wxPoint(45, 102), wxSize(131, 58) );
212         m_btn_Play->SetBitmapHover( wxBitmap(fs2_btn_play_hover_xpm) );
213         m_btn_Play->SetBitmapPressed( wxBitmap(fs2_btn_play_click_xpm) );
214
215         m_btn_Setup = new wxLauncherButton( m_panel, ID_B_SETUP, wxBitmap(fs2_btn_setup_xpm), wxPoint(199, 102), wxSize(131, 58) );
216         m_btn_Setup->SetBitmapHover( wxBitmap(fs2_btn_setup_hover_xpm) );
217         m_btn_Setup->SetBitmapPressed( wxBitmap(fs2_btn_setup_click_xpm) );
218
219         m_btn_Readme = new wxLauncherButton( m_panel, ID_B_README, wxBitmap(fs2_btn_readme_xpm), wxPoint(45, 175), wxSize(131, 58) );
220         m_btn_Readme->SetBitmapHover( wxBitmap(fs2_btn_readme_hover_xpm) );
221         m_btn_Readme->SetBitmapPressed( wxBitmap(fs2_btn_readme_click_xpm) );
222
223         m_btn_Update = new wxLauncherButton( m_panel, ID_B_UPDATE, wxBitmap(fs2_btn_update_xpm), wxPoint(199, 175), wxSize(131, 58) );
224         m_btn_Update->SetBitmapHover( wxBitmap(fs2_btn_update_hover_xpm) );
225         m_btn_Update->SetBitmapPressed( wxBitmap(fs2_btn_update_click_xpm) );
226
227         m_btn_Help = new wxLauncherButton( m_panel, ID_B_HELP, wxBitmap(fs2_btn_help_xpm), wxPoint(45, 247), wxSize(131, 58) );
228         m_btn_Help->SetBitmapHover( wxBitmap(fs2_btn_help_hover_xpm) );
229         m_btn_Help->SetBitmapPressed( wxBitmap(fs2_btn_help_click_xpm) );
230
231         m_btn_Uninstall = new wxLauncherButton( m_panel, ID_B_UNINSTALL, wxBitmap(fs2_btn_uninstall_xpm), wxPoint(199, 247), wxSize(131, 58) );
232         m_btn_Uninstall->SetBitmapHover( wxBitmap(fs2_btn_uninstall_hover_xpm) );
233         m_btn_Uninstall->SetBitmapPressed( wxBitmap(fs2_btn_uninstall_click_xpm) );
234
235         m_btn_Volition = new wxLauncherButton( m_panel, ID_B_VOLITION, wxBitmap(fs2_btn_volition_xpm), wxPoint(15, 304), wxSize(90, 108) );
236         m_btn_Volition->SetBitmapHover( wxBitmap(fs2_btn_volition_hover_xpm) );
237         m_btn_Volition->SetBitmapPressed( wxBitmap(fs2_btn_volition_click_xpm) );
238
239         m_btn_PXO = new wxLauncherButton( m_panel, ID_B_PXO, wxBitmap(fs2_btn_pxo_xpm), wxPoint(249, 305), wxSize(114, 113) );
240         m_btn_PXO->SetBitmapHover( wxBitmap(fs2_btn_pxo_hover_xpm) );
241         m_btn_PXO->SetBitmapPressed( wxBitmap(fs2_btn_pxo_click_xpm) );
242
243         m_btn_Quit = new wxLauncherButton( m_panel, ID_B_QUIT, wxBitmap(fs2_btn_quit_xpm), wxPoint(116, 339), wxSize(131, 58) );
244         m_btn_Quit->SetBitmapHover( wxBitmap(fs2_btn_quit_hover_xpm) );
245         m_btn_Quit->SetBitmapPressed( wxBitmap(fs2_btn_quit_click_xpm) );
246 #else
247         this->SetSizeHints( wxDefaultSize, wxDefaultSize );
248         this->SetTitle( wxT("FreeSpace Launcher") );
249
250         wxBoxSizer* bSizer3;
251         bSizer3 = new wxBoxSizer( wxVERTICAL );
252
253         wxStaticBitmap *m_bitmap1 = new wxStaticBitmap( this, wxID_ANY, wxBitmap( freespace_img_xpm ), wxDefaultPosition, wxDefaultSize, 0 );
254         bSizer3->Add( m_bitmap1, 0, wxALIGN_LEFT|wxALIGN_TOP|wxALL, 5 );
255
256         wxFlexGridSizer* fgSizer3;
257         fgSizer3 = new wxFlexGridSizer( 0, 2, 0, 0 );
258         fgSizer3->SetFlexibleDirection( wxBOTH );
259         fgSizer3->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
260
261         wxStaticBitmap *m_bitmap2 = new wxStaticBitmap( this, wxID_ANY, wxBitmap( volition_img_xpm ), wxDefaultPosition, wxDefaultSize, 0 );
262         fgSizer3->Add( m_bitmap2, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
263
264         wxBoxSizer* bSizer5;
265         bSizer5 = new wxBoxSizer( wxVERTICAL );
266
267         m_btn_Play = new wxButton( this, ID_B_PLAY, wxT("Play FreeSpace"), wxDefaultPosition, wxDefaultSize, 0 );
268         m_btn_Play->SetDefault();
269         bSizer5->Add( m_btn_Play, 0, wxALL|wxEXPAND, 5 );
270
271         m_btn_Setup = new wxButton( this, ID_B_SETUP, wxT("Setup"), wxDefaultPosition, wxDefaultSize, 0 );
272         bSizer5->Add( m_btn_Setup, 0, wxALL|wxEXPAND, 5 );
273
274         m_btn_Readme = new wxButton( this, ID_B_README, wxT("View README"), wxDefaultPosition, wxDefaultSize, 0 );
275         bSizer5->Add( m_btn_Readme, 0, wxALL|wxEXPAND, 5 );
276
277         m_btn_Update = new wxButton( this, ID_B_UPDATE, wxT("Update FreeSpace"), wxDefaultPosition, wxDefaultSize, 0 );
278         bSizer5->Add( m_btn_Update, 0, wxALL|wxEXPAND, 5 );
279
280         m_btn_Volition = new wxButton( this, ID_B_VOLITION, wxT("FreeSpace Webpage"), wxDefaultPosition, wxDefaultSize, 0 );
281         bSizer5->Add( m_btn_Volition, 0, wxALL|wxEXPAND, 5 );
282
283         m_btn_Uninstall = new wxButton( this, ID_B_UNINSTALL, wxT("Uninstall"), wxDefaultPosition, wxDefaultSize, 0 );
284         bSizer5->Add( m_btn_Uninstall, 0, wxALL|wxEXPAND, 5 );
285
286         m_btn_Quit = new wxButton( this, ID_B_QUIT, wxT("Quit"), wxDefaultPosition, wxDefaultSize, 0 );
287         bSizer5->Add( m_btn_Quit, 0, wxALL|wxEXPAND, 5 );
288
289         fgSizer3->Add( bSizer5, 1, wxALIGN_CENTER|wxALL, 10 );
290
291         bSizer3->Add( fgSizer3, 1, wxALIGN_BOTTOM|wxALIGN_RIGHT|wxEXPAND, 5 );
292
293         this->SetSizer( bSizer3 );
294         this->Layout();
295         bSizer3->Fit( this );
296 #endif
297
298         this->Centre( wxBOTH );
299 }
300
301 Launcher::~Launcher()
302 {
303         close_sound();
304 }
305
306 void Launcher::OnClose( wxCloseEvent& WXUNUSED(event) )
307 {
308 #ifndef MAKE_FS1
309         m_panel->RemoveEventHandler(p_background);
310         delete p_background;
311 #endif
312
313         Destroy();
314 }
315
316 void Launcher::OnPlay( wxCommandEvent& WXUNUSED(event) )
317 {
318         wxString epath = wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath(true);
319
320 #ifndef MAKE_FS1
321         epath.Append( wxT("freespace2") );
322 #else
323         epath.Append( wxT("freespace") );
324 #endif
325
326 #ifdef FS2_DEMO
327         epath.Append( wxT("_demo") );
328 #endif
329
330 #ifdef _WIN32
331         epath.Append( wxT(".exe") );
332 #endif
333
334         wxExecute(epath, wxEXEC_ASYNC | wxEXEC_MAKE_GROUP_LEADER | wxEXEC_HIDE_CONSOLE);
335
336         this->Close();
337 }
338
339 void Launcher::OnSetup( wxCommandEvent& WXUNUSED(event) )
340 {
341         LauncherSetup setup(this);
342
343         setup.ShowModal();
344 }
345
346 void Launcher::OnReadme( wxCommandEvent& WXUNUSED(event) )
347 {
348         wxLaunchDefaultApplication("README.txt");
349 }
350
351 void Launcher::OnUpdate( wxCommandEvent& WXUNUSED(event) )
352 {
353         wxMessageBox( wxT("Not implemented") );
354 }
355
356 void Launcher::OnHelp( wxCommandEvent& WXUNUSED(event) )
357 {
358
359 }
360
361 void Launcher::OnUninstall( wxCommandEvent& WXUNUSED(event) )
362 {
363         wxMessageBox( wxT("Not implemented") );
364 }
365
366 void Launcher::OnVolition( wxCommandEvent& WXUNUSED(event) )
367 {
368         wxLaunchDefaultBrowser( wxT("http://www.volition-inc.com") );
369 }
370
371 void Launcher::OnPXO( wxCommandEvent& WXUNUSED(event) )
372 {
373         wxLaunchDefaultBrowser( wxT("http://www.pxo.net") );
374 }
375
376 void Launcher::OnQuit( wxCommandEvent& WXUNUSED(event) )
377 {
378         this->Close();
379 }
380
381 void Launcher::SndPlayHover()
382 {
383         if (use_sound) {
384                 alSourcePlay(m_snd_hover_source_id);
385         }
386 }
387
388 void Launcher::SndPlayPressed()
389 {
390         if (use_sound) {
391                 alSourcePlay(m_snd_click_source_id);
392         }
393 }
394
395 void Launcher::init_sound()
396 {
397 #ifndef MAKE_FS1
398         if (use_sound) {
399                 return;
400         }
401
402         if ( os_config_read_uint("Audio", "LauncherSoundEnabled", 1) == 0 ) {
403                 return;
404         }
405
406         al_device = alcOpenDevice(NULL);
407
408         if (al_device == NULL) {
409                 return;
410         }
411
412         al_context = alcCreateContext(al_device, NULL);
413
414         if (al_context == NULL) {
415                 alcCloseDevice(al_device);
416                 return;
417         }
418
419         alcMakeContextCurrent(al_context);
420
421         // 'hover' sound
422         alGenBuffers(1, &m_snd_hover_buf_id);
423         alBufferData(m_snd_hover_buf_id, AL_FORMAT_MONO8, fs2_snd_hover_wav, sizeof(fs2_snd_hover_wav), 22050);
424
425         alGenSources(1, &m_snd_hover_source_id);
426         alSourcef(m_snd_hover_source_id, AL_GAIN, 1.0f);
427         alSourcei(m_snd_hover_source_id, AL_BUFFER, m_snd_hover_buf_id);
428
429         // 'click' sound
430         alGenBuffers(1, &m_snd_click_buf_id);
431         alBufferData(m_snd_click_buf_id, AL_FORMAT_MONO8, fs2_snd_click_wav, sizeof(fs2_snd_click_wav), 22050);
432
433         alGenSources(1, &m_snd_click_source_id);
434         alSourcef(m_snd_click_source_id, AL_GAIN, 1.0f);
435         alSourcei(m_snd_click_source_id, AL_BUFFER, m_snd_click_buf_id);
436
437         use_sound = true;
438 #endif
439 }
440
441 void Launcher::close_sound()
442 {
443         if ( !use_sound ) {
444                 return;
445         }
446
447         alSourceStop(m_snd_click_source_id);
448
449         alSourcei(m_snd_click_source_id, AL_BUFFER, 0);
450         alDeleteSources(1, &m_snd_click_source_id);
451         alDeleteBuffers(1, &m_snd_click_buf_id);
452
453         alSourceStop(m_snd_hover_source_id);
454
455         alSourcei(m_snd_hover_source_id, AL_BUFFER, 0);
456         alDeleteSources(1, &m_snd_hover_source_id);
457         alDeleteBuffers(1, &m_snd_hover_buf_id);
458
459         alcMakeContextCurrent(NULL);
460         alcDestroyContext(al_context);
461         alcCloseDevice(al_device);
462
463         use_sound = false;
464 }