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