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