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