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