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