]> git.sesse.net Git - vlc/blob - modules/gui/wince/dialogs.cpp
* modules/gui/wince: added dialogs provider + another massive cleanup + updates.
[vlc] / modules / gui / wince / dialogs.cpp
1 /*****************************************************************************
2  * dialogs.cpp : WinCE plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2005 VideoLAN
5  * $Id: dialogs.cpp 10101 2005-03-02 16:47:31Z robux4 $
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
29 #include <vlc/vlc.h>
30 #include <vlc/aout.h>
31 #include <vlc/intf.h>
32
33 #include "wince.h"
34
35 #include <commctrl.h>
36 #include <commdlg.h>
37 #include <shlobj.h>
38
39 /* Dialogs Provider */
40 class DialogsProvider: public CBaseWindow
41 {
42 public:
43     /* Constructor */
44     DialogsProvider( intf_thread_t *, CBaseWindow *, HINSTANCE = 0 );
45     virtual ~DialogsProvider();
46
47 protected:
48     virtual LRESULT WndProc( HWND, UINT, WPARAM, LPARAM );
49
50 private:
51
52     void OnExit( void );
53     void OnIdle( void );
54     void OnPlaylist( void );
55     void OnMessages( void );
56     void OnFileInfo( void );
57     void OnPreferences( void );
58
59     void OnOpen( int, int );
60     void OnOpenFileSimple( int );
61     void OnOpenDirectory( int );
62     void OnOpenFileGeneric( intf_dialog_args_t * );
63
64     /* GetOpenFileName replacement */
65     BOOL (WINAPI *GetOpenFile)(void *);
66     HMODULE h_gsgetfile_dll;
67
68 public:
69     /* Secondary windows */
70     OpenDialog          *p_open_dialog;
71     Playlist            *p_playlist_dialog;
72     Messages            *p_messages_dialog;
73     PrefsDialog         *p_prefs_dialog;
74     FileInfo            *p_fileinfo_dialog;
75 };
76
77 CBaseWindow *CreateDialogsProvider( intf_thread_t *p_intf,
78                                     CBaseWindow *p_parent, HINSTANCE h_inst )
79 {
80     return new DialogsProvider( p_intf, p_parent, h_inst );
81 }
82
83 /*****************************************************************************
84  * Constructor.
85  *****************************************************************************/
86 DialogsProvider::DialogsProvider( intf_thread_t *p_intf,
87                                   CBaseWindow *p_parent, HINSTANCE h_inst )
88   :  CBaseWindow( p_intf, p_parent, h_inst )
89 {
90     /* Initializations */
91     p_open_dialog = NULL;
92     p_playlist_dialog = NULL;
93     p_messages_dialog = NULL;
94     p_fileinfo_dialog = NULL;
95     p_prefs_dialog = NULL;
96
97     /* Create dummy window */
98     hWnd = CreateWindow( _T("VLC WinCE"), _T("DialogsProvider"), 0,
99                          0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
100                          p_parent->GetHandle(), NULL, h_inst, (void *)this );
101
102     GetOpenFile = 0;
103     h_gsgetfile_dll = LoadLibrary( _T("gsgetfile") );
104     if( h_gsgetfile_dll )
105     {
106         GetOpenFile = (BOOL (WINAPI *)(void *))
107             GetProcAddress( h_gsgetfile_dll, _T("gsGetOpenFileName") );
108     }
109
110     if( !GetOpenFile )
111         GetOpenFile = (BOOL (WINAPI *)(void *))::GetOpenFileName;
112 }
113
114 DialogsProvider::~DialogsProvider()
115 {
116     /* Clean up */
117     if( p_open_dialog )     delete p_open_dialog;
118     if( p_playlist_dialog ) delete p_playlist_dialog;
119     if( p_messages_dialog ) delete p_messages_dialog;
120     if( p_fileinfo_dialog ) delete p_fileinfo_dialog;
121     if( p_prefs_dialog )    delete p_prefs_dialog;
122
123     if( h_gsgetfile_dll ) FreeLibrary( h_gsgetfile_dll );
124 }
125
126 LRESULT DialogsProvider::WndProc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
127 {
128     switch( msg )
129     {
130     case WM_APP + INTF_DIALOG_FILE: OnOpen( FILE_ACCESS, wp ); return TRUE;
131     case WM_APP + INTF_DIALOG_NET: OnOpen( NET_ACCESS, wp ); return TRUE;
132     case WM_APP + INTF_DIALOG_FILE_SIMPLE: OnOpenFileSimple( wp ); return TRUE;
133     case WM_APP + INTF_DIALOG_DIRECTORY: OnOpenDirectory( wp ); return TRUE;
134     case WM_APP + INTF_DIALOG_FILE_GENERIC:
135         OnOpenFileGeneric( (intf_dialog_args_t*)lp ); return TRUE;
136     case WM_APP + INTF_DIALOG_PLAYLIST: OnPlaylist(); return TRUE;
137     case WM_APP + INTF_DIALOG_MESSAGES: OnMessages(); return TRUE;
138     case WM_APP + INTF_DIALOG_FILEINFO: OnFileInfo(); return TRUE;
139     case WM_APP + INTF_DIALOG_PREFS: OnPreferences(); return TRUE;
140     }
141
142     return DefWindowProc( hwnd, msg, wp, lp );
143 }
144
145 void DialogsProvider::OnIdle( void )
146 {
147     /* Update the log window */
148     if( p_messages_dialog ) p_messages_dialog->UpdateLog();
149
150     /* Update the playlist */
151     if( p_playlist_dialog ) p_playlist_dialog->UpdatePlaylist();
152
153     /* Update the fileinfo windows */
154     if( p_fileinfo_dialog ) p_fileinfo_dialog->UpdateFileInfo();
155 }
156
157 void DialogsProvider::OnPlaylist( void )
158 {
159 #if 1
160     Playlist *playlist = new Playlist( p_intf, this, hInst );
161     CreateDialogBox( hWnd, playlist );
162     delete playlist;
163 #else
164     /* Show/hide the playlist window */
165     if( !p_playlist_dialog )
166         p_playlist_dialog = new Playlist( p_intf, this, hInst );
167
168     if( p_playlist_dialog )
169     {
170         p_playlist_dialog->ShowPlaylist( !p_playlist_dialog->IsShown() );
171     }
172 #endif
173 }
174
175 void DialogsProvider::OnMessages( void )
176 {
177     /* Show/hide the log window */
178     if( !p_messages_dialog )
179         p_messages_dialog = new Messages( p_intf, this, hInst );
180
181     if( p_messages_dialog )
182     {
183         p_messages_dialog->Show( !p_messages_dialog->IsShown() );
184     }
185 }
186
187 void DialogsProvider::OnFileInfo( void )
188 {
189 #if 1
190     FileInfo *fileinfo = new FileInfo( p_intf, this, hInst );
191     CreateDialogBox( hWnd, fileinfo );
192     delete fileinfo;
193 #else
194     /* Show/hide the file info window */
195     if( !p_fileinfo_dialog )
196         p_fileinfo_dialog = new FileInfo( p_intf, this, hInst );
197
198     if( p_fileinfo_dialog )
199     {
200         p_fileinfo_dialog->Show( !p_fileinfo_dialog->IsShown() );
201     }
202 #endif
203 }
204
205 void DialogsProvider::OnPreferences( void )
206 {
207 #if 1
208     PrefsDialog *preferences = new PrefsDialog( p_intf, this, hInst );
209     CreateDialogBox( hWnd, preferences );
210     delete preferences;
211 #else
212     /* Show/hide the open dialog */
213     if( !p_prefs_dialog )
214         p_prefs_dialog = new PrefsDialog( p_intf, this, hInst );
215
216     if( p_prefs_dialog )
217     {
218         p_prefs_dialog->Show( !p_prefs_dialog->IsShown() );
219     }
220 #endif
221 }
222
223 void DialogsProvider::OnOpen( int i_access, int i_arg )
224 {
225     /* Show/hide the open dialog */
226     if( !p_open_dialog )
227         p_open_dialog = new OpenDialog( p_intf, this, hInst, i_access, i_arg );
228
229     if( p_open_dialog )
230     {
231         p_open_dialog->Show( !p_open_dialog->IsShown() );
232     }
233 }
234
235 void DialogsProvider::OnOpenFileGeneric( intf_dialog_args_t *p_arg )
236 {
237     if( p_arg == NULL )
238     {
239         msg_Dbg( p_intf, "OnOpenFileGeneric() called with NULL arg" );
240         return;
241     }
242
243     /* Convert the filter string */
244     TCHAR *psz_filters = (TCHAR *)
245         malloc( (strlen(p_arg->psz_extensions) + 2) * sizeof(TCHAR) );
246     _tcscpy( psz_filters, _FROMMB(p_arg->psz_extensions) );
247
248     int i;
249     for( i = 0; psz_filters[i]; i++ )
250     {
251         if( psz_filters[i] == '|' ) psz_filters[i] = 0;
252     }
253     psz_filters[++i] = 0;
254
255     OPENFILENAME ofn;
256     TCHAR szFile[MAX_PATH] = _T("\0");
257
258     memset( &ofn, 0, sizeof(OPENFILENAME) );
259     ofn.lStructSize = sizeof(OPENFILENAME);
260     ofn.hwndOwner = hWnd;
261     ofn.hInstance = hInst;
262     ofn.lpstrFilter = psz_filters;
263     ofn.lpstrCustomFilter = NULL;
264     ofn.nMaxCustFilter = 0;
265     ofn.nFilterIndex = 1;
266     ofn.lpstrFile = (LPTSTR)szFile; 
267     ofn.nMaxFile = MAX_PATH;
268     ofn.lpstrFileTitle = NULL; 
269     ofn.nMaxFileTitle = 40;
270     ofn.lpstrInitialDir = NULL;
271     ofn.lpstrTitle = _FROMMB(p_arg->psz_title);
272     ofn.Flags = 0; 
273     ofn.nFileOffset = 0;
274     ofn.nFileExtension = 0;
275     ofn.lpstrDefExt = NULL;
276     ofn.lCustData = 0L;
277     ofn.lpfnHook = NULL;
278     ofn.lpTemplateName = NULL;
279
280     SHFullScreen( GetForegroundWindow(), SHFS_HIDESIPBUTTON );
281
282     if( p_arg->b_save && GetSaveFileName( &ofn ) )
283     {
284         p_arg->i_results = 1;
285         p_arg->psz_results = (char **)malloc( p_arg->i_results *
286                                               sizeof(char *) );
287         p_arg->psz_results[0] = strdup( _TOMB(ofn.lpstrFile) );
288     }
289
290     if( !p_arg->b_save && GetOpenFile( &ofn ) )
291     {
292         p_arg->i_results = 1;
293         p_arg->psz_results = (char **)malloc( p_arg->i_results *
294                                               sizeof(char *) );
295         p_arg->psz_results[0] = strdup( _TOMB(ofn.lpstrFile) );
296     }
297
298     /* Callback */
299     if( p_arg->pf_callback )
300     {
301         p_arg->pf_callback( p_arg );
302     }
303
304     if( p_arg->psz_results )
305     {
306         for( int i = 0; i < p_arg->i_results; i++ )
307         {
308             free( p_arg->psz_results[i] );
309         }
310         free( p_arg->psz_results );
311     }
312     if( p_arg->psz_title ) free( p_arg->psz_title );
313     if( p_arg->psz_extensions ) free( p_arg->psz_extensions );
314
315     free( p_arg );
316 }
317
318 void DialogsProvider::OnOpenFileSimple( int i_arg )
319 {
320     OPENFILENAME ofn;
321     TCHAR szFile[MAX_PATH] = _T("\0");
322     static TCHAR szFilter[] = _T("All (*.*)\0*.*\0");
323
324     playlist_t *p_playlist = (playlist_t *)
325         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
326     if( p_playlist == NULL ) return;
327
328     memset( &ofn, 0, sizeof(OPENFILENAME) );
329     ofn.lStructSize = sizeof(OPENFILENAME);
330     ofn.hwndOwner = hWnd;
331     ofn.hInstance = hInst;
332     ofn.lpstrFilter = szFilter;
333     ofn.lpstrCustomFilter = NULL;
334     ofn.nMaxCustFilter = 0;
335     ofn.nFilterIndex = 1;     
336     ofn.lpstrFile = (LPTSTR)szFile; 
337     ofn.nMaxFile = MAX_PATH;
338     ofn.lpstrFileTitle = NULL; 
339     ofn.nMaxFileTitle = 40;
340     ofn.lpstrInitialDir = NULL;
341     ofn.lpstrTitle = _T("Quick Open File");
342     ofn.Flags = 0; 
343     ofn.nFileOffset = 0;
344     ofn.nFileExtension = 0;
345     ofn.lpstrDefExt = NULL;
346     ofn.lCustData = 0L;
347     ofn.lpfnHook = NULL;
348     ofn.lpTemplateName = NULL;
349
350     SHFullScreen( GetForegroundWindow(), SHFS_HIDESIPBUTTON );
351
352     if( GetOpenFile( &ofn ) )
353     {
354         char *psz_filename = _TOMB(ofn.lpstrFile);
355         playlist_Add( p_playlist, psz_filename, psz_filename,
356                       PLAYLIST_APPEND | (i_arg?PLAYLIST_GO:0), PLAYLIST_END );
357     }
358
359     vlc_object_release( p_playlist );
360 }
361
362 void DialogsProvider::OnOpenDirectory( int i_arg )
363 {
364     TCHAR psz_result[MAX_PATH];
365     LPMALLOC p_malloc = 0;
366     LPITEMIDLIST pidl;
367     BROWSEINFO bi;
368     playlist_t *p_playlist = 0;
369
370 #ifdef UNDER_CE
371 #   define SHGetMalloc MySHGetMalloc
372 #   define SHBrowseForFolder MySHBrowseForFolder
373 #   define SHGetPathFromIDList MySHGetPathFromIDList
374
375     HMODULE ceshell_dll = LoadLibrary( _T("ceshell") );
376     if( !ceshell_dll ) return;
377
378     HRESULT WINAPI (*SHGetMalloc)(LPMALLOC *) =
379         (HRESULT WINAPI (*)(LPMALLOC *))
380         GetProcAddress( ceshell_dll, _T("SHGetMalloc") );
381     LPITEMIDLIST WINAPI (*SHBrowseForFolder)(LPBROWSEINFO) =
382         (LPITEMIDLIST WINAPI (*)(LPBROWSEINFO))
383         GetProcAddress( ceshell_dll, _T("SHBrowseForFolder") );
384     BOOL WINAPI (*SHGetPathFromIDList)(LPCITEMIDLIST, LPTSTR) =
385         (BOOL WINAPI (*)(LPCITEMIDLIST, LPTSTR))
386         GetProcAddress( ceshell_dll, _T("SHGetPathFromIDList") );
387
388     if( !SHGetMalloc || !SHBrowseForFolder || !SHGetPathFromIDList )
389     {
390         msg_Err( p_intf, "couldn't load SHBrowseForFolder API" );
391         FreeLibrary( ceshell_dll );
392         return;
393     }
394 #endif
395
396     if( !SUCCEEDED( SHGetMalloc(&p_malloc) ) ) goto error;
397
398     p_playlist = (playlist_t *)
399         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
400     if( !p_playlist ) goto error;
401
402     memset( &bi, 0, sizeof(BROWSEINFO) );
403     bi.hwndOwner = hWnd;
404     bi.pszDisplayName = psz_result;
405     bi.ulFlags = BIF_EDITBOX;
406 #ifndef UNDER_CE
407     bi.ulFlags |= BIF_USENEWUI;
408 #endif
409
410     if( (pidl = SHBrowseForFolder( &bi ) ) )
411     {
412         if( SHGetPathFromIDList( pidl, psz_result ) )
413         {
414             char *psz_filename = _TOMB(psz_result);
415             playlist_Add( p_playlist, psz_filename, psz_filename,
416                           PLAYLIST_APPEND | (i_arg ? PLAYLIST_GO : 0),
417                           PLAYLIST_END );
418         }
419         p_malloc->Free( pidl );
420     }
421
422  error:
423
424     if( p_malloc) p_malloc->Release();
425     if( p_playlist ) vlc_object_release( p_playlist );
426
427 #ifdef UNDER_CE
428     FreeLibrary( ceshell_dll );
429 #endif
430 }