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