]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/dialogs.cpp
aee8b3282b141541e980dd1cd4674094d7daac56
[vlc] / modules / gui / wxwindows / dialogs.cpp
1 /*****************************************************************************
2  * dialogs.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/aout.h>
34 #include <vlc/intf.h>
35
36 #include "wxwindows.h"
37
38 /* include the icon graphic */
39 #include "../../../share/vlc32x32.xpm"
40
41 /* Dialogs Provider */
42 class DialogsProvider: public wxFrame
43 {
44 public:
45     /* Constructor */
46     DialogsProvider( intf_thread_t *p_intf, wxWindow *p_parent );
47     virtual ~DialogsProvider();
48
49 private:
50     void Open( int i_access_method, int i_arg );
51
52     /* Event handlers (these functions should _not_ be virtual) */
53     void OnExit( wxCommandEvent& event );
54     void OnPlaylist( wxCommandEvent& event );
55     void OnMessages( wxCommandEvent& event );
56     void OnFileInfo( wxCommandEvent& event );
57     void OnPreferences( wxCommandEvent& event );
58     void OnWizardDialog( wxCommandEvent& event );
59     void OnBookmarks( wxCommandEvent& event );
60
61     void OnOpenFileGeneric( wxCommandEvent& event );
62     void OnOpenFileSimple( wxCommandEvent& event );
63     void OnOpenDirectory( wxCommandEvent& event );
64     void OnOpenFile( wxCommandEvent& event );
65     void OnOpenDisc( wxCommandEvent& event );
66     void OnOpenNet( wxCommandEvent& event );
67     void OnOpenCapture( wxCommandEvent& event );
68     void OnOpenSat( wxCommandEvent& event );
69
70     void OnPopupMenu( wxCommandEvent& event );
71
72     void OnIdle( wxIdleEvent& event );
73
74     void OnExitThread( wxCommandEvent& event );
75
76     DECLARE_EVENT_TABLE();
77
78     intf_thread_t *p_intf;
79
80 public:
81     /* Secondary windows */
82     OpenDialog          *p_open_dialog;
83     wxFileDialog        *p_file_dialog;
84     wxDirDialog         *p_dir_dialog;
85     Playlist            *p_playlist_dialog;
86     Messages            *p_messages_dialog;
87     FileInfo            *p_fileinfo_dialog;
88     WizardDialog        *p_wizard_dialog;
89     wxFrame             *p_prefs_dialog;
90     wxWindow            *p_bookmarks_dialog;
91     wxFileDialog        *p_file_generic_dialog;
92 };
93
94 DEFINE_LOCAL_EVENT_TYPE( wxEVT_DIALOG );
95
96 BEGIN_EVENT_TABLE(DialogsProvider, wxFrame)
97     /* Idle loop used to update some of the dialogs */
98     EVT_IDLE(DialogsProvider::OnIdle)
99
100     /* Custom wxDialog events */
101     EVT_COMMAND(INTF_DIALOG_FILE, wxEVT_DIALOG, DialogsProvider::OnOpenFile)
102     EVT_COMMAND(INTF_DIALOG_DISC, wxEVT_DIALOG, DialogsProvider::OnOpenDisc)
103     EVT_COMMAND(INTF_DIALOG_NET, wxEVT_DIALOG, DialogsProvider::OnOpenNet)
104     EVT_COMMAND(INTF_DIALOG_CAPTURE, wxEVT_DIALOG,
105                 DialogsProvider::OnOpenCapture)
106     EVT_COMMAND(INTF_DIALOG_FILE_SIMPLE, wxEVT_DIALOG,
107                 DialogsProvider::OnOpenFileSimple)
108     EVT_COMMAND(INTF_DIALOG_FILE_GENERIC, wxEVT_DIALOG,
109                 DialogsProvider::OnOpenFileGeneric)
110     EVT_COMMAND(INTF_DIALOG_DIRECTORY, wxEVT_DIALOG,
111                 DialogsProvider::OnOpenDirectory)
112
113     EVT_COMMAND(INTF_DIALOG_PLAYLIST, wxEVT_DIALOG,
114                 DialogsProvider::OnPlaylist)
115     EVT_COMMAND(INTF_DIALOG_MESSAGES, wxEVT_DIALOG,
116                 DialogsProvider::OnMessages)
117     EVT_COMMAND(INTF_DIALOG_PREFS, wxEVT_DIALOG,
118                 DialogsProvider::OnPreferences)
119     EVT_COMMAND(INTF_DIALOG_WIZARD, wxEVT_DIALOG,
120                 DialogsProvider::OnWizardDialog)
121     EVT_COMMAND(INTF_DIALOG_FILEINFO, wxEVT_DIALOG,
122                 DialogsProvider::OnFileInfo)
123     EVT_COMMAND(INTF_DIALOG_BOOKMARKS, wxEVT_DIALOG,
124                 DialogsProvider::OnBookmarks)
125     EVT_COMMAND(INTF_DIALOG_POPUPMENU, wxEVT_DIALOG,
126                 DialogsProvider::OnPopupMenu)
127     EVT_COMMAND(INTF_DIALOG_EXIT, wxEVT_DIALOG,
128                 DialogsProvider::OnExitThread)
129 END_EVENT_TABLE()
130
131 wxWindow *CreateDialogsProvider( intf_thread_t *p_intf, wxWindow *p_parent )
132 {
133     return new DialogsProvider( p_intf, p_parent );
134 }
135
136 /*****************************************************************************
137  * Constructor.
138  *****************************************************************************/
139 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf, wxWindow *p_parent )
140   :  wxFrame( p_parent, -1, wxT("") )
141 {
142     /* Initializations */
143     p_intf = _p_intf;
144     p_open_dialog = NULL;
145     p_file_dialog = NULL;
146     p_playlist_dialog = NULL;
147     p_messages_dialog = NULL;
148     p_fileinfo_dialog = NULL;
149     p_prefs_dialog = NULL;
150     p_file_generic_dialog = NULL;
151     p_wizard_dialog = NULL;
152     p_bookmarks_dialog = NULL;
153     p_dir_dialog = NULL;
154
155     /* Give our interface a nice little icon */
156     p_intf->p_sys->p_icon = new wxIcon( vlc_xpm );
157
158     /* Create the messages dialog so it can begin storing logs */
159     p_messages_dialog = new Messages( p_intf, p_parent ? p_parent : this );
160
161     /* Check if user wants to show the bookmarks dialog by default */
162     wxCommandEvent dummy_event;
163     if( config_GetInt( p_intf, "wxwin-bookmarks" ) )
164         OnBookmarks( dummy_event );
165
166     /* Intercept all menu events in our custom event handler */
167     PushEventHandler( new MenuEvtHandler( p_intf, NULL ) );
168
169
170     WindowSettings *ws = p_intf->p_sys->p_window_settings;
171     wxPoint p;
172     wxSize  s;
173     bool    b_shown;
174
175 #define INIT( id, w, N, S ) \
176     if( ws->GetSettings( WindowSettings::id, b_shown, p, s ) && b_shown ) \
177     {                           \
178         if( !w )                \
179             w = N;              \
180         w->SetSize( s );        \
181         w->Move( p );           \
182         w->S( true );           \
183     }
184
185     INIT( ID_PLAYLIST, p_playlist_dialog, new Playlist(p_intf,this), ShowPlaylist );
186     INIT( ID_MESSAGES, p_messages_dialog, new Messages(p_intf,this), Show );
187     INIT( ID_FILE_INFO, p_fileinfo_dialog, new FileInfo(p_intf,this), Show );
188     INIT( ID_BOOKMARKS, p_bookmarks_dialog, BookmarksDialog(p_intf,this), Show);
189 #undef INIT
190 }
191
192 DialogsProvider::~DialogsProvider()
193 {
194     WindowSettings *ws = p_intf->p_sys->p_window_settings;
195
196 #define UPDATE(id,w)                                        \
197   {                                                         \
198     if( w && w->IsShown() )                                 \
199         ws->SetSettings(  WindowSettings::id, true,         \
200                           w->GetPosition(), w->GetSize() ); \
201     else                                                    \
202         ws->SetSettings(  WindowSettings::id, false );      \
203   }
204
205     UPDATE( ID_PLAYLIST,  p_playlist_dialog );
206     UPDATE( ID_MESSAGES,  p_messages_dialog );
207     UPDATE( ID_FILE_INFO, p_fileinfo_dialog );
208     UPDATE( ID_BOOKMARKS, p_bookmarks_dialog );
209
210 #undef UPDATE
211
212     /* Clean up */
213     if( p_open_dialog )     delete p_open_dialog;
214     if( p_prefs_dialog )    p_prefs_dialog->Destroy();
215     if( p_file_dialog )     delete p_file_dialog;
216     if( p_playlist_dialog ) delete p_playlist_dialog;
217     if( p_messages_dialog ) delete p_messages_dialog;
218     if( p_fileinfo_dialog ) delete p_fileinfo_dialog;
219     if( p_file_generic_dialog ) delete p_file_generic_dialog;
220     if( p_wizard_dialog ) delete p_wizard_dialog;
221     if( p_bookmarks_dialog ) delete p_bookmarks_dialog;
222
223
224     if( p_intf->p_sys->p_icon ) delete p_intf->p_sys->p_icon;
225
226     /* We must set this here because on win32 this destructor will be
227      * automatically called so we must not call it again on wxApp->OnExit().
228      * There shouldn't be any race conditions as all this should be done
229      * from the same thread. */
230     p_intf->p_sys->p_wxwindow = NULL;
231 }
232
233 void DialogsProvider::OnIdle( wxIdleEvent& WXUNUSED(event) )
234 {
235     /* Update the log window */
236     if( p_messages_dialog )
237         p_messages_dialog->UpdateLog();
238
239     /* Update the playlist */
240     if( p_playlist_dialog )
241         p_playlist_dialog->UpdatePlaylist();
242
243     /* Update the fileinfo windows */
244     if( p_fileinfo_dialog )
245         p_fileinfo_dialog->UpdateFileInfo();
246 }
247
248 void DialogsProvider::OnPlaylist( wxCommandEvent& WXUNUSED(event) )
249 {
250     /* Show/hide the playlist window */
251     if( !p_playlist_dialog )
252         p_playlist_dialog = new Playlist( p_intf, this );
253
254     if( p_playlist_dialog )
255     {
256         p_playlist_dialog->ShowPlaylist( !p_playlist_dialog->IsShown() );
257     }
258 }
259
260 void DialogsProvider::OnMessages( wxCommandEvent& WXUNUSED(event) )
261 {
262     /* Show/hide the log window */
263     if( !p_messages_dialog )
264         p_messages_dialog = new Messages( p_intf, this );
265
266     if( p_messages_dialog )
267     {
268         p_messages_dialog->Show( !p_messages_dialog->IsShown() );
269     }
270 }
271
272 void DialogsProvider::OnFileInfo( wxCommandEvent& WXUNUSED(event) )
273 {
274     /* Show/hide the file info window */
275     if( !p_fileinfo_dialog )
276         p_fileinfo_dialog = new FileInfo( p_intf, this );
277
278     if( p_fileinfo_dialog )
279     {
280         p_fileinfo_dialog->Show( !p_fileinfo_dialog->IsShown() );
281     }
282 }
283
284 void DialogsProvider::OnPreferences( wxCommandEvent& WXUNUSED(event) )
285 {
286     /* Show/hide the open dialog */
287     if( !p_prefs_dialog )
288         p_prefs_dialog = new PrefsDialog( p_intf, this );
289
290     if( p_prefs_dialog )
291     {
292         p_prefs_dialog->Show( !p_prefs_dialog->IsShown() );
293     }
294 }
295
296 void DialogsProvider::OnWizardDialog( wxCommandEvent& WXUNUSED(event) )
297 {
298     p_wizard_dialog = new WizardDialog( p_intf, this, NULL, 0, 0 );
299
300     if( p_wizard_dialog )
301     {
302         p_wizard_dialog->Run();
303         delete p_wizard_dialog;
304     }
305
306     p_wizard_dialog = NULL;
307 }
308
309 void DialogsProvider::OnBookmarks( wxCommandEvent& WXUNUSED(event) )
310 {
311     /* Show/hide the open dialog */
312     if( !p_bookmarks_dialog )
313         p_bookmarks_dialog = BookmarksDialog( p_intf, this );
314
315     if( p_bookmarks_dialog )
316     {
317         p_bookmarks_dialog->Show( !p_bookmarks_dialog->IsShown() );
318     }
319 }
320
321 void DialogsProvider::OnOpenFileGeneric( wxCommandEvent& event )
322 {
323     intf_dialog_args_t *p_arg = (intf_dialog_args_t *)event.GetClientData();
324
325     if( p_arg == NULL )
326     {
327         msg_Dbg( p_intf, "OnOpenFileGeneric() called with NULL arg" );
328         return;
329     }
330
331     if( p_file_generic_dialog == NULL )
332         p_file_generic_dialog = new wxFileDialog( NULL );
333
334     if( p_file_generic_dialog )
335     {
336         p_file_generic_dialog->SetMessage( wxU(p_arg->psz_title) );
337         p_file_generic_dialog->SetWildcard( wxU(p_arg->psz_extensions) );
338         p_file_generic_dialog->SetStyle( (p_arg->b_save ? wxSAVE : wxOPEN) |
339                                          (p_arg->b_multiple ? wxMULTIPLE:0) );
340     }
341
342     if( p_file_generic_dialog &&
343         p_file_generic_dialog->ShowModal() == wxID_OK )
344     {
345         wxArrayString paths;
346
347         p_file_generic_dialog->GetPaths( paths );
348
349         p_arg->i_results = paths.GetCount();
350         p_arg->psz_results = (char **)malloc( p_arg->i_results *
351                                               sizeof(char *) );
352         for( size_t i = 0; i < paths.GetCount(); i++ )
353         {
354             p_arg->psz_results[i] = strdup( paths[i].mb_str() );
355         }
356     }
357
358     /* Callback */
359     if( p_arg->pf_callback )
360     {
361         p_arg->pf_callback( p_arg );
362     }
363
364     if( p_arg->psz_results )
365     {
366         for( int i = 0; i < p_arg->i_results; i++ )
367         {
368             free( p_arg->psz_results[i] );
369         }
370         free( p_arg->psz_results );
371     }
372     if( p_arg->psz_title ) free( p_arg->psz_title );
373     if( p_arg->psz_extensions ) free( p_arg->psz_extensions );
374
375     free( p_arg );
376 }
377
378 void DialogsProvider::OnOpenFileSimple( wxCommandEvent& event )
379 {
380     playlist_t *p_playlist =
381         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
382                                        FIND_ANYWHERE );
383     if( p_playlist == NULL )
384     {
385         return;
386     }
387
388     if( p_file_dialog == NULL )
389         p_file_dialog = new wxFileDialog( NULL, wxU(_("Open File")),
390             wxT(""), wxT(""), wxT("*"), wxOPEN | wxMULTIPLE );
391
392     if( p_file_dialog && p_file_dialog->ShowModal() == wxID_OK )
393     {
394         wxArrayString paths;
395
396         p_file_dialog->GetPaths( paths );
397
398         for( size_t i = 0; i < paths.GetCount(); i++ )
399             if( event.GetInt() )
400                 playlist_Add( p_playlist, (const char *)paths[i].mb_str(),
401                               (const char *)paths[i].mb_str(),
402                               PLAYLIST_APPEND | (i ? 0 : PLAYLIST_GO),
403                               PLAYLIST_END );
404             else
405                 playlist_Add( p_playlist, (const char *)paths[i].mb_str(),
406                               (const char *)paths[i].mb_str(),
407                               PLAYLIST_APPEND, PLAYLIST_END );
408     }
409
410     vlc_object_release( p_playlist );
411 }
412
413 void DialogsProvider::OnOpenDirectory( wxCommandEvent& event )
414 {
415     playlist_t *p_playlist =
416         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
417                                        FIND_ANYWHERE );
418     if( p_playlist == NULL )
419     {
420         return;
421     }
422
423     if( p_dir_dialog == NULL )
424         p_dir_dialog = new wxDirDialog( NULL );
425
426     if( p_dir_dialog && p_dir_dialog->ShowModal() == wxID_OK )
427     {
428         wxString path = p_dir_dialog->GetPath();
429
430         playlist_Add( p_playlist, (const char *)path.mb_str(),
431                       (const char *)path.mb_str(),
432                       PLAYLIST_APPEND | (event.GetInt() ? PLAYLIST_GO : 0),
433                       PLAYLIST_END );
434     }
435
436     vlc_object_release( p_playlist );
437 }
438
439 void DialogsProvider::OnOpenFile( wxCommandEvent& event )
440 {
441     Open( FILE_ACCESS, event.GetInt() );
442 }
443
444 void DialogsProvider::OnOpenDisc( wxCommandEvent& event )
445 {
446     Open( DISC_ACCESS, event.GetInt() );
447 }
448
449 void DialogsProvider::OnOpenNet( wxCommandEvent& event )
450 {
451     Open( NET_ACCESS, event.GetInt() );
452 }
453
454 void DialogsProvider::OnOpenCapture( wxCommandEvent& event )
455 {
456     Open( CAPTURE_ACCESS, event.GetInt() );
457 }
458
459 void DialogsProvider::Open( int i_access_method, int i_arg )
460 {
461     /* Show/hide the open dialog */
462     if( !p_open_dialog )
463         p_open_dialog = new OpenDialog( p_intf, this, i_access_method, i_arg,
464                                         OPEN_NORMAL );
465
466     if( p_open_dialog )
467     {
468         p_open_dialog->Show( i_access_method, i_arg );
469     }
470 }
471
472 void DialogsProvider::OnPopupMenu( wxCommandEvent& event )
473 {
474     wxPoint mousepos = ScreenToClient( wxGetMousePosition() );
475     ::PopupMenu( p_intf, this, mousepos );
476 }
477
478 void DialogsProvider::OnExitThread( wxCommandEvent& WXUNUSED(event) )
479 {
480     wxTheApp->ExitMainLoop();
481 }