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