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