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