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