]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/dialogs.cpp
c5bce255f3ec8bc1f1924b1e6140a979dd72b6ec
[vlc] / modules / gui / wxwindows / dialogs.cpp
1 /*****************************************************************************
2  * dialogs.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
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 OnOpenFile( wxCommandEvent& event );
64     void OnOpenDisc( wxCommandEvent& event );
65     void OnOpenNet( wxCommandEvent& event );
66     void OnOpenCapture( wxCommandEvent& event );
67     void OnOpenSat( wxCommandEvent& event );
68
69     void OnPopupMenu( wxCommandEvent& event );
70
71     void OnIdle( wxIdleEvent& event );
72
73     void OnExitThread( wxCommandEvent& event );
74
75     DECLARE_EVENT_TABLE();
76
77     intf_thread_t *p_intf;
78
79 public:
80     /* Secondary windows */
81     OpenDialog          *p_open_dialog;
82     wxFileDialog        *p_file_dialog;
83     Playlist            *p_playlist_dialog;
84     Messages            *p_messages_dialog;
85     FileInfo            *p_fileinfo_dialog;
86     WizardDialog        *p_wizard_dialog;
87     wxFrame             *p_prefs_dialog;
88     wxWindow            *p_bookmarks_dialog;
89     wxFileDialog        *p_file_generic_dialog;
90 };
91
92 DEFINE_LOCAL_EVENT_TYPE( wxEVT_DIALOG );
93
94 BEGIN_EVENT_TABLE(DialogsProvider, wxFrame)
95     /* Idle loop used to update some of the dialogs */
96     EVT_IDLE(DialogsProvider::OnIdle)
97
98     /* Custom wxDialog events */
99     EVT_COMMAND(INTF_DIALOG_FILE, wxEVT_DIALOG, DialogsProvider::OnOpenFile)
100     EVT_COMMAND(INTF_DIALOG_DISC, wxEVT_DIALOG, DialogsProvider::OnOpenDisc)
101     EVT_COMMAND(INTF_DIALOG_NET, wxEVT_DIALOG, DialogsProvider::OnOpenNet)
102     EVT_COMMAND(INTF_DIALOG_CAPTURE, wxEVT_DIALOG,
103                 DialogsProvider::OnOpenCapture)
104     EVT_COMMAND(INTF_DIALOG_FILE_SIMPLE, wxEVT_DIALOG,
105                 DialogsProvider::OnOpenFileSimple)
106     EVT_COMMAND(INTF_DIALOG_FILE_GENERIC, wxEVT_DIALOG,
107                 DialogsProvider::OnOpenFileGeneric)
108
109     EVT_COMMAND(INTF_DIALOG_PLAYLIST, wxEVT_DIALOG,
110                 DialogsProvider::OnPlaylist)
111     EVT_COMMAND(INTF_DIALOG_MESSAGES, wxEVT_DIALOG,
112                 DialogsProvider::OnMessages)
113     EVT_COMMAND(INTF_DIALOG_PREFS, wxEVT_DIALOG,
114                 DialogsProvider::OnPreferences)
115     EVT_COMMAND(INTF_DIALOG_WIZARD, wxEVT_DIALOG,
116                 DialogsProvider::OnWizardDialog)
117     EVT_COMMAND(INTF_DIALOG_FILEINFO, wxEVT_DIALOG,
118                 DialogsProvider::OnFileInfo)
119     EVT_COMMAND(INTF_DIALOG_BOOKMARKS, wxEVT_DIALOG,
120                 DialogsProvider::OnBookmarks)
121     EVT_COMMAND(INTF_DIALOG_POPUPMENU, wxEVT_DIALOG,
122                 DialogsProvider::OnPopupMenu)
123     EVT_COMMAND(INTF_DIALOG_EXIT, wxEVT_DIALOG,
124                 DialogsProvider::OnExitThread)
125 END_EVENT_TABLE()
126
127 wxWindow *CreateDialogsProvider( intf_thread_t *p_intf, wxWindow *p_parent )
128 {
129     return new DialogsProvider( p_intf, p_parent );
130 }
131
132 /*****************************************************************************
133  * Constructor.
134  *****************************************************************************/
135 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf, wxWindow *p_parent )
136   :  wxFrame( p_parent, -1, wxT("") )
137 {
138     /* Initializations */
139     p_intf = _p_intf;
140     p_open_dialog = NULL;
141     p_file_dialog = NULL;
142     p_playlist_dialog = NULL;
143     p_messages_dialog = NULL;
144     p_fileinfo_dialog = NULL;
145     p_prefs_dialog = NULL;
146     p_file_generic_dialog = NULL;
147     p_wizard_dialog = NULL;
148     p_bookmarks_dialog = NULL;
149
150     /* Give our interface a nice little icon */
151     p_intf->p_sys->p_icon = new wxIcon( vlc_xpm );
152
153     /* Create the messages dialog so it can begin storing logs */
154     p_messages_dialog = new Messages( p_intf, p_parent ? p_parent : this );
155
156     /* Check if user wants to show the bookmarks dialog by default */
157     wxCommandEvent dummy_event;
158     if( config_GetInt( p_intf, "wxwin-bookmarks" ) )
159         OnBookmarks( dummy_event );
160
161     /* Intercept all menu events in our custom event handler */
162     PushEventHandler( new MenuEvtHandler( p_intf, NULL ) );
163 }
164
165 DialogsProvider::~DialogsProvider()
166 {
167     /* Clean up */
168     if( p_open_dialog )     delete p_open_dialog;
169     if( p_prefs_dialog )    p_prefs_dialog->Destroy();
170     if( p_file_dialog )     delete p_file_dialog;
171     if( p_playlist_dialog ) delete p_playlist_dialog;
172     if( p_messages_dialog ) delete p_messages_dialog;
173     if( p_fileinfo_dialog ) delete p_fileinfo_dialog;
174     if( p_file_generic_dialog ) delete p_file_generic_dialog;
175     if( p_wizard_dialog ) delete p_wizard_dialog;
176     if( p_bookmarks_dialog ) delete p_bookmarks_dialog;
177
178
179     if( p_intf->p_sys->p_icon ) delete p_intf->p_sys->p_icon;
180
181     /* We must set this here because on win32 this destructor will be
182      * automatically called so we must not call it again on wxApp->OnExit().
183      * There shouldn't be any race conditions as all this should be done
184      * from the same thread. */
185     p_intf->p_sys->p_wxwindow = NULL;
186 }
187
188 void DialogsProvider::OnIdle( wxIdleEvent& WXUNUSED(event) )
189 {
190   /* Update the log window */
191   if( p_messages_dialog )
192     p_messages_dialog->UpdateLog();
193
194   /* Update the playlist */
195   if( p_playlist_dialog )
196     p_playlist_dialog->UpdatePlaylist();
197
198   /* Update the fileinfo windows */
199   if( p_fileinfo_dialog )
200     p_fileinfo_dialog->UpdateFileInfo();
201 }
202
203 void DialogsProvider::OnPlaylist( wxCommandEvent& WXUNUSED(event) )
204 {
205     /* Show/hide the playlist window */
206     if( !p_playlist_dialog )
207         p_playlist_dialog = new Playlist( p_intf, this );
208
209     if( p_playlist_dialog )
210     {
211         p_playlist_dialog->ShowPlaylist( !p_playlist_dialog->IsShown() );
212     }
213 }
214
215 void DialogsProvider::OnMessages( wxCommandEvent& WXUNUSED(event) )
216 {
217     /* Show/hide the log window */
218     if( !p_messages_dialog )
219         p_messages_dialog = new Messages( p_intf, this );
220
221     if( p_messages_dialog )
222     {
223         p_messages_dialog->Show( !p_messages_dialog->IsShown() );
224     }
225 }
226
227 void DialogsProvider::OnFileInfo( wxCommandEvent& WXUNUSED(event) )
228 {
229     /* Show/hide the file info window */
230     if( !p_fileinfo_dialog )
231         p_fileinfo_dialog = new FileInfo( p_intf, this );
232
233     if( p_fileinfo_dialog )
234     {
235         p_fileinfo_dialog->Show( !p_fileinfo_dialog->IsShown() );
236     }
237 }
238
239 void DialogsProvider::OnPreferences( wxCommandEvent& WXUNUSED(event) )
240 {
241     /* Show/hide the open dialog */
242     if( !p_prefs_dialog )
243         p_prefs_dialog = new PrefsDialog( p_intf, this );
244
245     if( p_prefs_dialog )
246     {
247         p_prefs_dialog->Show( !p_prefs_dialog->IsShown() );
248     }
249 }
250
251 void DialogsProvider::OnWizardDialog( wxCommandEvent& WXUNUSED(event) )
252 {
253     p_wizard_dialog = new WizardDialog( p_intf, this, NULL, 0, 0 );
254
255     if( p_wizard_dialog )
256     {
257         p_wizard_dialog->Run();
258         delete p_wizard_dialog;
259     }
260
261     p_wizard_dialog = NULL;
262 }
263
264 void DialogsProvider::OnBookmarks( wxCommandEvent& WXUNUSED(event) )
265 {
266     /* Show/hide the open dialog */
267     if( !p_bookmarks_dialog )
268         p_bookmarks_dialog = BookmarksDialog( p_intf, this );
269
270     if( p_bookmarks_dialog )
271     {
272         p_bookmarks_dialog->Show( !p_bookmarks_dialog->IsShown() );
273     }
274 }
275
276 void DialogsProvider::OnOpenFileGeneric( wxCommandEvent& event )
277 {
278     intf_dialog_args_t *p_arg = (intf_dialog_args_t *)event.GetClientData();
279
280     if( p_arg == NULL )
281     {
282         msg_Dbg( p_intf, "OnOpenFileGeneric() called with NULL arg" );
283         return;
284     }
285
286     if( p_file_generic_dialog == NULL )
287         p_file_generic_dialog = new wxFileDialog( NULL );
288
289     if( p_file_generic_dialog )
290     {
291         p_file_generic_dialog->SetMessage( wxU(p_arg->psz_title) );
292         p_file_generic_dialog->SetWildcard( wxU(p_arg->psz_extensions) );
293         p_file_generic_dialog->SetStyle( (p_arg->b_save ? wxSAVE : wxOPEN) |
294                                          (p_arg->b_multiple ? wxMULTIPLE:0) );
295     }
296
297     if( p_file_generic_dialog &&
298         p_file_generic_dialog->ShowModal() == wxID_OK )
299     {
300         wxArrayString paths;
301
302         p_file_generic_dialog->GetPaths( paths );
303
304         p_arg->i_results = paths.GetCount();
305         p_arg->psz_results = (char **)malloc( p_arg->i_results *
306                                               sizeof(char *) );
307         for( size_t i = 0; i < paths.GetCount(); i++ )
308         {
309             p_arg->psz_results[i] = strdup( paths[i].mb_str() );
310         }
311     }
312
313     /* Callback */
314     if( p_arg->pf_callback )
315     {
316         p_arg->pf_callback( p_arg );
317     }
318
319     /* Blocking or not ? */
320     if( p_arg->b_blocking )
321     {
322         vlc_mutex_lock( &p_arg->lock );
323         p_arg->b_ready = 1;
324         vlc_cond_signal( &p_arg->wait );
325     }
326
327     if( p_arg->psz_results )
328     {
329         for( int i = 0; i < p_arg->i_results; i++ )
330         {
331             free( p_arg->psz_results[i] );
332         }
333         free( p_arg->psz_results );
334     }
335     if( p_arg->psz_title ) free( p_arg->psz_title );
336     if( p_arg->psz_extensions ) free( p_arg->psz_extensions );
337
338     if( p_arg->b_blocking )
339     {
340         vlc_mutex_unlock( &p_arg->lock );
341     }
342     else
343     {
344         free( p_arg );
345     }
346 }
347
348 void DialogsProvider::OnOpenFileSimple( wxCommandEvent& event )
349 {
350     playlist_t *p_playlist =
351         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
352                                        FIND_ANYWHERE );
353     if( p_playlist == NULL )
354     {
355         return;
356     }
357
358     if( p_file_dialog == NULL )
359         p_file_dialog = new wxFileDialog( NULL, wxU(_("Open File")),
360             wxT(""), wxT(""), wxT("*"), wxOPEN | wxMULTIPLE );
361
362     if( p_file_dialog && p_file_dialog->ShowModal() == wxID_OK )
363     {
364         wxArrayString paths;
365
366         p_file_dialog->GetPaths( paths );
367
368         for( size_t i = 0; i < paths.GetCount(); i++ )
369             if( event.GetInt() )
370                 playlist_Add( p_playlist, (const char *)paths[i].mb_str(),
371                               (const char *)paths[i].mb_str(),
372                               PLAYLIST_APPEND | (i ? 0 : PLAYLIST_GO),
373                               PLAYLIST_END );
374             else
375                 playlist_Add( p_playlist, (const char *)paths[i].mb_str(),
376                               (const char *)paths[i].mb_str(),
377                               PLAYLIST_APPEND, PLAYLIST_END );
378     }
379
380     vlc_object_release( p_playlist );
381 }
382
383 void DialogsProvider::OnOpenFile( wxCommandEvent& event )
384 {
385     Open( FILE_ACCESS, event.GetInt() );
386 }
387
388 void DialogsProvider::OnOpenDisc( wxCommandEvent& event )
389 {
390     Open( DISC_ACCESS, event.GetInt() );
391 }
392
393 void DialogsProvider::OnOpenNet( wxCommandEvent& event )
394 {
395     Open( NET_ACCESS, event.GetInt() );
396 }
397
398 void DialogsProvider::OnOpenCapture( wxCommandEvent& event )
399 {
400     Open( CAPTURE_ACCESS, event.GetInt() );
401 }
402
403 void DialogsProvider::Open( int i_access_method, int i_arg )
404 {
405     /* Show/hide the open dialog */
406     if( !p_open_dialog )
407         p_open_dialog = new OpenDialog( p_intf, this, i_access_method, i_arg,
408                                         OPEN_NORMAL );
409
410     if( p_open_dialog )
411     {
412         p_open_dialog->Show( i_access_method, i_arg );
413     }
414 }
415
416 void DialogsProvider::OnPopupMenu( wxCommandEvent& event )
417 {
418     wxPoint mousepos = ScreenToClient( wxGetMousePosition() );
419     ::PopupMenu( p_intf, this, mousepos );
420 }
421
422 void DialogsProvider::OnExitThread( wxCommandEvent& WXUNUSED(event) )
423 {
424     wxTheApp->ExitMainLoop();
425 }