xref: /reactos/dll/win32/comdlg32/filedlg31.c (revision 5981ef3e)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * Win 3.1 Style File Dialogs
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * Copyright 1994 Martin Ayotte
5c2c66affSColin Finck  * Copyright 1996 Albrecht Kleine
6c2c66affSColin Finck  *
7c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
8c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
9c2c66affSColin Finck  * License as published by the Free Software Foundation; either
10c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
11c2c66affSColin Finck  *
12c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
13c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15c2c66affSColin Finck  * Lesser General Public License for more details.
16c2c66affSColin Finck  *
17c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
18c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
19c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20c2c66affSColin Finck  */
2145ed51c1SAmine Khaldi #include <ctype.h>
2245ed51c1SAmine Khaldi #include <stdlib.h>
2345ed51c1SAmine Khaldi #include <stdarg.h>
2445ed51c1SAmine Khaldi #include <stdio.h>
2545ed51c1SAmine Khaldi #include <string.h>
2645ed51c1SAmine Khaldi #include "windef.h"
2745ed51c1SAmine Khaldi #include "winbase.h"
2845ed51c1SAmine Khaldi #include "winnls.h"
2945ed51c1SAmine Khaldi #include "wingdi.h"
3045ed51c1SAmine Khaldi #include "winuser.h"
3145ed51c1SAmine Khaldi #include "wine/debug.h"
32f68b0c51SAmine Khaldi #include "wine/heap.h"
3345ed51c1SAmine Khaldi #include "winreg.h"
3445ed51c1SAmine Khaldi #include "winternl.h"
3545ed51c1SAmine Khaldi #include "commdlg.h"
3645ed51c1SAmine Khaldi #include "shlwapi.h"
3745ed51c1SAmine Khaldi #include "cderr.h"
3845ed51c1SAmine Khaldi 
3945ed51c1SAmine Khaldi WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
40c2c66affSColin Finck 
41c2c66affSColin Finck #include "cdlg.h"
42c2c66affSColin Finck 
43c2c66affSColin Finck #define BUFFILE 512
44c2c66affSColin Finck #define BUFFILEALLOC 512 * sizeof(WCHAR)
45c2c66affSColin Finck 
46c2c66affSColin Finck static const WCHAR FILE_star[] = {'*','.','*', 0};
47c2c66affSColin Finck static const WCHAR FILE_bslash[] = {'\\', 0};
48c2c66affSColin Finck static const WCHAR FILE_specc[] = {'%','c',':', 0};
49c2c66affSColin Finck static const int fldrHeight = 16;
50c2c66affSColin Finck static const int fldrWidth = 20;
51c2c66affSColin Finck 
52c2c66affSColin Finck static HICON hFolder = 0;
53c2c66affSColin Finck static HICON hFolder2 = 0;
54c2c66affSColin Finck static HICON hFloppy = 0;
55c2c66affSColin Finck static HICON hHDisk = 0;
56c2c66affSColin Finck static HICON hCDRom = 0;
57c2c66affSColin Finck static HICON hNet = 0;
58c2c66affSColin Finck 
59c2c66affSColin Finck #define FD31_OFN_PROP "FILEDLG_OFN"
60c2c66affSColin Finck 
61c2c66affSColin Finck typedef struct tagFD31_DATA
62c2c66affSColin Finck {
63c2c66affSColin Finck     HWND hwnd; /* file dialog window handle */
64c2c66affSColin Finck     BOOL hook; /* TRUE if the dialog is hooked */
65c2c66affSColin Finck     UINT lbselchstring; /* registered message id */
66c2c66affSColin Finck     UINT fileokstring; /* registered message id */
67c2c66affSColin Finck     LPARAM lParam; /* save original lparam */
68c2c66affSColin Finck     LPCVOID template; /* template for 32 bits resource */
69c2c66affSColin Finck     BOOL open; /* TRUE if open dialog, FALSE if save dialog */
70c2c66affSColin Finck     LPOPENFILENAMEW ofnW; /* pointer either to the original structure or
71c2c66affSColin Finck                              a W copy for A/16 API */
72c2c66affSColin Finck     LPOPENFILENAMEA ofnA; /* original structure if 32bits ansi dialog */
73c2c66affSColin Finck } FD31_DATA, *PFD31_DATA;
74c2c66affSColin Finck 
75c2c66affSColin Finck /***********************************************************************
76c2c66affSColin Finck  * 				FD31_Init			[internal]
77c2c66affSColin Finck  */
FD31_Init(void)78c2c66affSColin Finck static BOOL FD31_Init(void)
79c2c66affSColin Finck {
80c2c66affSColin Finck     static BOOL initialized = FALSE;
81c2c66affSColin Finck 
82c2c66affSColin Finck     if (!initialized) {
83c2c66affSColin Finck         hFolder  = LoadImageA( COMDLG32_hInstance, "FOLDER", IMAGE_ICON, 16, 16, LR_SHARED );
84c2c66affSColin Finck         hFolder2 = LoadImageA( COMDLG32_hInstance, "FOLDER2", IMAGE_ICON, 16, 16, LR_SHARED );
85c2c66affSColin Finck         hFloppy  = LoadImageA( COMDLG32_hInstance, "FLOPPY", IMAGE_ICON, 16, 16, LR_SHARED );
86c2c66affSColin Finck         hHDisk   = LoadImageA( COMDLG32_hInstance, "HDISK", IMAGE_ICON, 16, 16, LR_SHARED );
87c2c66affSColin Finck         hCDRom   = LoadImageA( COMDLG32_hInstance, "CDROM", IMAGE_ICON, 16, 16, LR_SHARED );
88c2c66affSColin Finck         hNet     = LoadImageA( COMDLG32_hInstance, "NETWORK", IMAGE_ICON, 16, 16, LR_SHARED );
89c2c66affSColin Finck 	if (hFolder == 0 || hFolder2 == 0 || hFloppy == 0 ||
90c2c66affSColin Finck 	    hHDisk == 0 || hCDRom == 0 || hNet == 0)
91c2c66affSColin Finck 	{
92c2c66affSColin Finck 	    ERR("Error loading icons!\n");
93c2c66affSColin Finck 	    return FALSE;
94c2c66affSColin Finck 	}
95c2c66affSColin Finck 	initialized = TRUE;
96c2c66affSColin Finck     }
97c2c66affSColin Finck     return TRUE;
98c2c66affSColin Finck }
99c2c66affSColin Finck 
100c2c66affSColin Finck /***********************************************************************
101c2c66affSColin Finck  *                              FD31_StripEditControl        [internal]
102c2c66affSColin Finck  * Strip pathnames off the contents of the edit control.
103c2c66affSColin Finck  */
FD31_StripEditControl(HWND hwnd)104c2c66affSColin Finck static void FD31_StripEditControl(HWND hwnd)
105c2c66affSColin Finck {
106c2c66affSColin Finck     WCHAR temp[BUFFILE], *cp;
107c2c66affSColin Finck 
108f68b0c51SAmine Khaldi     GetDlgItemTextW( hwnd, edt1, temp, ARRAY_SIZE(temp));
109*5981ef3eSAmine Khaldi     cp = wcsrchr(temp, '\\');
110c2c66affSColin Finck     if (cp != NULL) {
111*5981ef3eSAmine Khaldi 	lstrcpyW(temp, cp+1);
112c2c66affSColin Finck     }
113*5981ef3eSAmine Khaldi     cp = wcsrchr(temp, ':');
114c2c66affSColin Finck     if (cp != NULL) {
115*5981ef3eSAmine Khaldi 	lstrcpyW(temp, cp+1);
116c2c66affSColin Finck     }
117c2c66affSColin Finck     /* FIXME: shouldn't we do something with the result here? ;-) */
118c2c66affSColin Finck }
119c2c66affSColin Finck 
120c2c66affSColin Finck /***********************************************************************
121c2c66affSColin Finck  *                              FD31_CallWindowProc          [internal]
122c2c66affSColin Finck  *
123c2c66affSColin Finck  *      Call the appropriate hook
124c2c66affSColin Finck  */
FD31_CallWindowProc(const FD31_DATA * lfs,UINT wMsg,WPARAM wParam,LPARAM lParam)125c2c66affSColin Finck static BOOL FD31_CallWindowProc(const FD31_DATA *lfs, UINT wMsg, WPARAM wParam, LPARAM lParam)
126c2c66affSColin Finck {
127c2c66affSColin Finck     BOOL ret;
128c2c66affSColin Finck 
129c2c66affSColin Finck     if (lfs->ofnA)
130c2c66affSColin Finck     {
131c2c66affSColin Finck         TRACE("Call hookA %p (%p, %04x, %08lx, %08lx)\n",
132c2c66affSColin Finck                lfs->ofnA->lpfnHook, lfs->hwnd, wMsg, wParam, lParam);
133c2c66affSColin Finck         ret = lfs->ofnA->lpfnHook(lfs->hwnd, wMsg, wParam, lParam);
134c2c66affSColin Finck         TRACE("ret hookA %p (%p, %04x, %08lx, %08lx)\n",
135c2c66affSColin Finck                lfs->ofnA->lpfnHook, lfs->hwnd, wMsg, wParam, lParam);
136c2c66affSColin Finck         return ret;
137c2c66affSColin Finck     }
138c2c66affSColin Finck 
139c2c66affSColin Finck     TRACE("Call hookW %p (%p, %04x, %08lx, %08lx)\n",
140c2c66affSColin Finck            lfs->ofnW->lpfnHook, lfs->hwnd, wMsg, wParam, lParam);
141c2c66affSColin Finck     ret = lfs->ofnW->lpfnHook(lfs->hwnd, wMsg, wParam, lParam);
142c2c66affSColin Finck     TRACE("Ret hookW %p (%p, %04x, %08lx, %08lx)\n",
143c2c66affSColin Finck            lfs->ofnW->lpfnHook, lfs->hwnd, wMsg, wParam, lParam);
144c2c66affSColin Finck     return ret;
145c2c66affSColin Finck }
146c2c66affSColin Finck 
147c2c66affSColin Finck /***********************************************************************
148c2c66affSColin Finck  * 				FD31_GetFileType		[internal]
149c2c66affSColin Finck  */
FD31_GetFileType(LPCWSTR cfptr,LPCWSTR fptr,const WORD index)150c2c66affSColin Finck static LPCWSTR FD31_GetFileType(LPCWSTR cfptr, LPCWSTR fptr, const WORD index)
151c2c66affSColin Finck {
152c2c66affSColin Finck   int n, i;
153c2c66affSColin Finck   i = 0;
154c2c66affSColin Finck   if (cfptr)
155c2c66affSColin Finck     for ( ;(n = lstrlenW(cfptr)) != 0; i++)
156c2c66affSColin Finck       {
157c2c66affSColin Finck 	cfptr += n + 1;
158c2c66affSColin Finck 	if (i == index)
159c2c66affSColin Finck 	  return cfptr;
160c2c66affSColin Finck 	cfptr += lstrlenW(cfptr) + 1;
161c2c66affSColin Finck       }
162c2c66affSColin Finck   if (fptr)
163c2c66affSColin Finck     for ( ;(n = lstrlenW(fptr)) != 0; i++)
164c2c66affSColin Finck       {
165c2c66affSColin Finck 	fptr += n + 1;
166c2c66affSColin Finck 	if (i == index)
167c2c66affSColin Finck 	  return fptr;
168c2c66affSColin Finck 	fptr += lstrlenW(fptr) + 1;
169c2c66affSColin Finck     }
170c2c66affSColin Finck   return FILE_star; /* FIXME */
171c2c66affSColin Finck }
172c2c66affSColin Finck 
173c2c66affSColin Finck /***********************************************************************
174c2c66affSColin Finck  * 				FD31_ScanDir                 [internal]
175c2c66affSColin Finck  */
FD31_ScanDir(const OPENFILENAMEW * ofn,HWND hWnd,LPCWSTR newPath)176c2c66affSColin Finck static BOOL FD31_ScanDir(const OPENFILENAMEW *ofn, HWND hWnd, LPCWSTR newPath)
177c2c66affSColin Finck {
178c2c66affSColin Finck     WCHAR   buffer[BUFFILE];
179c2c66affSColin Finck     HWND    hdlg;
180c2c66affSColin Finck     LRESULT lRet = TRUE;
181c2c66affSColin Finck     HCURSOR hCursorWait, oldCursor;
182c2c66affSColin Finck 
183c2c66affSColin Finck     TRACE("Trying to change to %s\n", debugstr_w(newPath));
184c2c66affSColin Finck     if  ( newPath[0] && !SetCurrentDirectoryW( newPath ))
185c2c66affSColin Finck         return FALSE;
186c2c66affSColin Finck 
187c2c66affSColin Finck     /* get the list of spec files */
188c2c66affSColin Finck     lstrcpynW(buffer, FD31_GetFileType(ofn->lpstrCustomFilter,
189c2c66affSColin Finck               ofn->lpstrFilter, ofn->nFilterIndex - 1), BUFFILE);
190c2c66affSColin Finck 
191c2c66affSColin Finck     hCursorWait = LoadCursorA(0, (LPSTR)IDC_WAIT);
192c2c66affSColin Finck     oldCursor = SetCursor(hCursorWait);
193c2c66affSColin Finck 
194c2c66affSColin Finck     /* list of files */
195c2c66affSColin Finck     if ((hdlg = GetDlgItem(hWnd, lst1)) != 0) {
196c2c66affSColin Finck         WCHAR*	scptr; /* ptr on semi-colon */
197c2c66affSColin Finck 	WCHAR*	filter = buffer;
198c2c66affSColin Finck 
199c2c66affSColin Finck 	TRACE("Using filter %s\n", debugstr_w(filter));
200c2c66affSColin Finck 	SendMessageW(hdlg, LB_RESETCONTENT, 0, 0);
201c2c66affSColin Finck 	while (filter) {
202*5981ef3eSAmine Khaldi 	    scptr = wcschr(filter, ';');
203c2c66affSColin Finck 	    if (scptr)	*scptr = 0;
204c2c66affSColin Finck 	    while (*filter == ' ') filter++;
205c2c66affSColin Finck 	    TRACE("Using file spec %s\n", debugstr_w(filter));
206c2c66affSColin Finck 	    SendMessageW(hdlg, LB_DIR, 0, (LPARAM)filter);
207c2c66affSColin Finck 	    if (scptr) *scptr = ';';
208c2c66affSColin Finck 	    filter = (scptr) ? (scptr + 1) : 0;
209c2c66affSColin Finck 	 }
210c2c66affSColin Finck     }
211c2c66affSColin Finck 
212c2c66affSColin Finck     /* list of directories */
213*5981ef3eSAmine Khaldi     lstrcpyW(buffer, FILE_star);
214c2c66affSColin Finck 
215c2c66affSColin Finck     if (GetDlgItem(hWnd, lst2) != 0) {
216c2c66affSColin Finck         lRet = DlgDirListW(hWnd, buffer, lst2, stc1, DDL_EXCLUSIVE | DDL_DIRECTORY);
217c2c66affSColin Finck     }
218c2c66affSColin Finck     SetCursor(oldCursor);
219c2c66affSColin Finck     return lRet;
220c2c66affSColin Finck }
221c2c66affSColin Finck 
222c2c66affSColin Finck /***********************************************************************
223c2c66affSColin Finck  *                              FD31_WMDrawItem              [internal]
224c2c66affSColin Finck  */
FD31_WMDrawItem(HWND hWnd,WPARAM wParam,LPARAM lParam,int savedlg,const DRAWITEMSTRUCT * lpdis)225c2c66affSColin Finck static LONG FD31_WMDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam,
226c2c66affSColin Finck 			    int savedlg, const DRAWITEMSTRUCT *lpdis)
227c2c66affSColin Finck {
228c2c66affSColin Finck     WCHAR *str;
229c2c66affSColin Finck     HICON hIcon;
230c2c66affSColin Finck     COLORREF oldText = 0, oldBk = 0;
231c2c66affSColin Finck 
232c2c66affSColin Finck     if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst1)
233c2c66affSColin Finck     {
234f68b0c51SAmine Khaldi         if (!(str = heap_alloc(BUFFILEALLOC))) return FALSE;
235c2c66affSColin Finck 	SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
236c2c66affSColin Finck                       (LPARAM)str);
237c2c66affSColin Finck 
238c2c66affSColin Finck 	if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
239c2c66affSColin Finck 	{
240c2c66affSColin Finck 	    oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
241c2c66affSColin Finck 	    oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
242c2c66affSColin Finck 	}
243c2c66affSColin Finck 	if (savedlg)
244c2c66affSColin Finck 	    SetTextColor(lpdis->hDC,GetSysColor(COLOR_GRAYTEXT) );
245c2c66affSColin Finck 
246c2c66affSColin Finck 	ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + 1,
247c2c66affSColin Finck                   lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
248c2c66affSColin Finck                   &(lpdis->rcItem), str, lstrlenW(str), NULL);
249c2c66affSColin Finck 
250c2c66affSColin Finck 	if (lpdis->itemState & ODS_SELECTED)
251c2c66affSColin Finck 	    DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
252c2c66affSColin Finck 
253c2c66affSColin Finck 	if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
254c2c66affSColin Finck 	{
255c2c66affSColin Finck 	    SetBkColor( lpdis->hDC, oldBk );
256c2c66affSColin Finck 	    SetTextColor( lpdis->hDC, oldText );
257c2c66affSColin Finck 	}
258f68b0c51SAmine Khaldi         heap_free(str);
259c2c66affSColin Finck 	return TRUE;
260c2c66affSColin Finck     }
261c2c66affSColin Finck 
262c2c66affSColin Finck     if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst2)
263c2c66affSColin Finck     {
264f68b0c51SAmine Khaldi         if (!(str = heap_alloc(BUFFILEALLOC)))
265c2c66affSColin Finck             return FALSE;
266c2c66affSColin Finck 	SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
267c2c66affSColin Finck                       (LPARAM)str);
268c2c66affSColin Finck 
269c2c66affSColin Finck 	if (lpdis->itemState & ODS_SELECTED)
270c2c66affSColin Finck 	{
271c2c66affSColin Finck 	    oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
272c2c66affSColin Finck 	    oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
273c2c66affSColin Finck 	}
274c2c66affSColin Finck 	ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
275c2c66affSColin Finck                   lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
276c2c66affSColin Finck                   &(lpdis->rcItem), str, lstrlenW(str), NULL);
277c2c66affSColin Finck 
278c2c66affSColin Finck 	if (lpdis->itemState & ODS_SELECTED)
279c2c66affSColin Finck 	    DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
280c2c66affSColin Finck 
281c2c66affSColin Finck 	if (lpdis->itemState & ODS_SELECTED)
282c2c66affSColin Finck 	{
283c2c66affSColin Finck 	    SetBkColor( lpdis->hDC, oldBk );
284c2c66affSColin Finck 	    SetTextColor( lpdis->hDC, oldText );
285c2c66affSColin Finck 	}
286c2c66affSColin Finck 	DrawIconEx( lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hFolder, 16, 16, 0, 0, DI_NORMAL );
287f68b0c51SAmine Khaldi         heap_free(str);
288c2c66affSColin Finck 	return TRUE;
289c2c66affSColin Finck     }
290c2c66affSColin Finck     if (lpdis->CtlType == ODT_COMBOBOX && lpdis->CtlID == cmb2)
291c2c66affSColin Finck     {
292c2c66affSColin Finck         char root[] = "a:";
293f68b0c51SAmine Khaldi         if (!(str = heap_alloc(BUFFILEALLOC)))
294c2c66affSColin Finck             return FALSE;
295c2c66affSColin Finck 	SendMessageW(lpdis->hwndItem, CB_GETLBTEXT, lpdis->itemID,
296c2c66affSColin Finck                       (LPARAM)str);
297c2c66affSColin Finck         root[0] += str[2] - 'a';
298c2c66affSColin Finck         switch(GetDriveTypeA(root))
299c2c66affSColin Finck         {
300c2c66affSColin Finck         case DRIVE_REMOVABLE: hIcon = hFloppy; break;
301c2c66affSColin Finck         case DRIVE_CDROM:     hIcon = hCDRom; break;
302c2c66affSColin Finck         case DRIVE_REMOTE:    hIcon = hNet; break;
303c2c66affSColin Finck         case DRIVE_FIXED:
304c2c66affSColin Finck         default:           hIcon = hHDisk; break;
305c2c66affSColin Finck         }
306c2c66affSColin Finck 	if (lpdis->itemState & ODS_SELECTED)
307c2c66affSColin Finck 	{
308c2c66affSColin Finck 	    oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
309c2c66affSColin Finck 	    oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
310c2c66affSColin Finck 	}
311c2c66affSColin Finck 	ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
312c2c66affSColin Finck                   lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
313c2c66affSColin Finck                   &(lpdis->rcItem), str, lstrlenW(str), NULL);
314c2c66affSColin Finck 
315c2c66affSColin Finck 	if (lpdis->itemState & ODS_SELECTED)
316c2c66affSColin Finck 	{
317c2c66affSColin Finck 	    SetBkColor( lpdis->hDC, oldBk );
318c2c66affSColin Finck 	    SetTextColor( lpdis->hDC, oldText );
319c2c66affSColin Finck 	}
320c2c66affSColin Finck 	DrawIconEx( lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hIcon, 16, 16, 0, 0, DI_NORMAL );
321f68b0c51SAmine Khaldi         heap_free(str);
322c2c66affSColin Finck 	return TRUE;
323c2c66affSColin Finck     }
324c2c66affSColin Finck     return FALSE;
325c2c66affSColin Finck }
326c2c66affSColin Finck 
327c2c66affSColin Finck /***********************************************************************
328c2c66affSColin Finck  *                              FD31_UpdateResult            [internal]
329c2c66affSColin Finck  *      update the displayed file name (with path)
330c2c66affSColin Finck  */
FD31_UpdateResult(const FD31_DATA * lfs,const WCHAR * tmpstr)331c2c66affSColin Finck static void FD31_UpdateResult(const FD31_DATA *lfs, const WCHAR *tmpstr)
332c2c66affSColin Finck {
333c2c66affSColin Finck     int lenstr2;
334c2c66affSColin Finck     LPOPENFILENAMEW ofnW = lfs->ofnW;
335c2c66affSColin Finck     LPOPENFILENAMEA ofnA = lfs->ofnA;
336c2c66affSColin Finck     WCHAR tmpstr2[BUFFILE];
337c2c66affSColin Finck     WCHAR *p;
338c2c66affSColin Finck 
339c2c66affSColin Finck     TRACE("%s\n", debugstr_w(tmpstr));
340c2c66affSColin Finck     if(ofnW->Flags & OFN_NOVALIDATE)
341c2c66affSColin Finck         tmpstr2[0] = '\0';
342c2c66affSColin Finck     else
343c2c66affSColin Finck         GetCurrentDirectoryW(BUFFILE, tmpstr2);
344*5981ef3eSAmine Khaldi     lenstr2 = lstrlenW(tmpstr2);
345c2c66affSColin Finck     if (lenstr2 > 3)
346c2c66affSColin Finck         tmpstr2[lenstr2++]='\\';
347c2c66affSColin Finck     lstrcpynW(tmpstr2+lenstr2, tmpstr, BUFFILE-lenstr2);
348c2c66affSColin Finck     if (!ofnW->lpstrFile)
349c2c66affSColin Finck         return;
350c2c66affSColin Finck 
351c2c66affSColin Finck     lstrcpynW(ofnW->lpstrFile, tmpstr2, ofnW->nMaxFile);
352c2c66affSColin Finck 
353c2c66affSColin Finck     /* set filename offset */
354c2c66affSColin Finck     p = PathFindFileNameW(ofnW->lpstrFile);
355c2c66affSColin Finck     ofnW->nFileOffset = (p - ofnW->lpstrFile);
356c2c66affSColin Finck 
357c2c66affSColin Finck     /* set extension offset */
358c2c66affSColin Finck     p = PathFindExtensionW(ofnW->lpstrFile);
359c2c66affSColin Finck     ofnW->nFileExtension = (*p) ? (p - ofnW->lpstrFile) + 1 : 0;
360c2c66affSColin Finck 
361c2c66affSColin Finck     TRACE("file %s, file offset %d, ext offset %d\n",
362c2c66affSColin Finck           debugstr_w(ofnW->lpstrFile), ofnW->nFileOffset, ofnW->nFileExtension);
363c2c66affSColin Finck 
364c2c66affSColin Finck     /* update the real client structures if any */
365c2c66affSColin Finck     if (ofnA)
366c2c66affSColin Finck     {
367c2c66affSColin Finck         LPSTR lpszTemp;
368c2c66affSColin Finck         if (ofnW->nMaxFile &&
369c2c66affSColin Finck             !WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFile, -1,
370c2c66affSColin Finck                                   ofnA->lpstrFile, ofnA->nMaxFile, NULL, NULL ))
371c2c66affSColin Finck             ofnA->lpstrFile[ofnA->nMaxFile-1] = 0;
372c2c66affSColin Finck 
373c2c66affSColin Finck         /* offsets are not guaranteed to be the same in WCHAR to MULTIBYTE conversion */
374c2c66affSColin Finck         /* set filename offset */
375c2c66affSColin Finck         lpszTemp = PathFindFileNameA(ofnA->lpstrFile);
376c2c66affSColin Finck         ofnA->nFileOffset = (lpszTemp - ofnA->lpstrFile);
377c2c66affSColin Finck 
378c2c66affSColin Finck         /* set extension offset */
379c2c66affSColin Finck         lpszTemp = PathFindExtensionA(ofnA->lpstrFile);
380c2c66affSColin Finck         ofnA->nFileExtension = (*lpszTemp) ? (lpszTemp - ofnA->lpstrFile) + 1 : 0;
381c2c66affSColin Finck     }
382c2c66affSColin Finck }
383c2c66affSColin Finck 
384c2c66affSColin Finck /***********************************************************************
385c2c66affSColin Finck  *                              FD31_UpdateFileTitle         [internal]
386c2c66affSColin Finck  *      update the displayed file name (without path)
387c2c66affSColin Finck  */
FD31_UpdateFileTitle(const FD31_DATA * lfs)388c2c66affSColin Finck static void FD31_UpdateFileTitle(const FD31_DATA *lfs)
389c2c66affSColin Finck {
390c2c66affSColin Finck   LONG lRet;
391c2c66affSColin Finck   LPOPENFILENAMEW ofnW = lfs->ofnW;
392c2c66affSColin Finck   LPOPENFILENAMEA ofnA = lfs->ofnA;
393c2c66affSColin Finck 
394c2c66affSColin Finck   if (ofnW->lpstrFileTitle != NULL)
395c2c66affSColin Finck   {
396c2c66affSColin Finck     lRet = SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
397c2c66affSColin Finck     SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETTEXT, lRet,
398c2c66affSColin Finck                              (LPARAM)ofnW->lpstrFileTitle );
399c2c66affSColin Finck     if (ofnA)
400c2c66affSColin Finck     {
401c2c66affSColin Finck         if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFileTitle, -1,
402c2c66affSColin Finck                                   ofnA->lpstrFileTitle, ofnA->nMaxFileTitle, NULL, NULL ))
403c2c66affSColin Finck             ofnA->lpstrFileTitle[ofnA->nMaxFileTitle-1] = 0;
404c2c66affSColin Finck     }
405c2c66affSColin Finck   }
406c2c66affSColin Finck }
407c2c66affSColin Finck 
408c2c66affSColin Finck /***********************************************************************
409c2c66affSColin Finck  *                              FD31_DirListDblClick         [internal]
410c2c66affSColin Finck  */
FD31_DirListDblClick(const FD31_DATA * lfs)411c2c66affSColin Finck static LRESULT FD31_DirListDblClick( const FD31_DATA *lfs )
412c2c66affSColin Finck {
413c2c66affSColin Finck   LONG lRet;
414c2c66affSColin Finck   HWND hWnd = lfs->hwnd;
415c2c66affSColin Finck   LPWSTR pstr;
416c2c66affSColin Finck   WCHAR tmpstr[BUFFILE];
417c2c66affSColin Finck 
418c2c66affSColin Finck   /* get the raw string (with brackets) */
419c2c66affSColin Finck   lRet = SendDlgItemMessageW(hWnd, lst2, LB_GETCURSEL, 0, 0);
420c2c66affSColin Finck   if (lRet == LB_ERR) return TRUE;
421f68b0c51SAmine Khaldi   pstr = heap_alloc(BUFFILEALLOC);
422c2c66affSColin Finck   SendDlgItemMessageW(hWnd, lst2, LB_GETTEXT, lRet,
423c2c66affSColin Finck 		     (LPARAM)pstr);
424*5981ef3eSAmine Khaldi   lstrcpyW( tmpstr, pstr );
425f68b0c51SAmine Khaldi   heap_free(pstr);
426c2c66affSColin Finck   /* get the selected directory in tmpstr */
427c2c66affSColin Finck   if (tmpstr[0] == '[')
428c2c66affSColin Finck     {
429c2c66affSColin Finck       tmpstr[lstrlenW(tmpstr) - 1] = 0;
430*5981ef3eSAmine Khaldi       lstrcpyW(tmpstr,tmpstr+1);
431c2c66affSColin Finck     }
432*5981ef3eSAmine Khaldi   lstrcatW(tmpstr, FILE_bslash);
433c2c66affSColin Finck 
434c2c66affSColin Finck   FD31_ScanDir(lfs->ofnW, hWnd, tmpstr);
435c2c66affSColin Finck   /* notify the app */
436c2c66affSColin Finck   if (lfs->hook)
437c2c66affSColin Finck     {
438c2c66affSColin Finck       if (FD31_CallWindowProc(lfs, lfs->lbselchstring, lst2,
439c2c66affSColin Finck               MAKELONG(lRet,CD_LBSELCHANGE)))
440c2c66affSColin Finck         return TRUE;
441c2c66affSColin Finck     }
442c2c66affSColin Finck   return TRUE;
443c2c66affSColin Finck }
444c2c66affSColin Finck 
445c2c66affSColin Finck /***********************************************************************
446c2c66affSColin Finck  *                              FD31_FileListSelect         [internal]
447c2c66affSColin Finck  *    called when a new item is picked in the file list
448c2c66affSColin Finck  */
FD31_FileListSelect(const FD31_DATA * lfs)449c2c66affSColin Finck static LRESULT FD31_FileListSelect( const FD31_DATA *lfs )
450c2c66affSColin Finck {
451c2c66affSColin Finck     LONG lRet;
452c2c66affSColin Finck     HWND hWnd = lfs->hwnd;
453c2c66affSColin Finck     LPWSTR pstr;
454c2c66affSColin Finck 
455c2c66affSColin Finck     lRet =  SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
456c2c66affSColin Finck     if (lRet == LB_ERR)
457c2c66affSColin Finck         return TRUE;
458c2c66affSColin Finck 
459c2c66affSColin Finck     /* set the edit control to the chosen file */
460f68b0c51SAmine Khaldi     if ((pstr = heap_alloc(BUFFILEALLOC)))
461c2c66affSColin Finck     {
462c2c66affSColin Finck         SendDlgItemMessageW(hWnd, lst1, LB_GETTEXT, lRet,
463c2c66affSColin Finck                        (LPARAM)pstr);
464c2c66affSColin Finck         SetDlgItemTextW( hWnd, edt1, pstr );
465f68b0c51SAmine Khaldi         heap_free(pstr);
466c2c66affSColin Finck     }
467c2c66affSColin Finck     if (lfs->hook)
468c2c66affSColin Finck     {
469c2c66affSColin Finck         FD31_CallWindowProc(lfs, lfs->lbselchstring, lst1,
470c2c66affSColin Finck                            MAKELONG(lRet,CD_LBSELCHANGE));
471c2c66affSColin Finck     }
472c2c66affSColin Finck     /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD,
473c2c66affSColin Finck            CD_LBSELNOITEMS */
474c2c66affSColin Finck     return TRUE;
475c2c66affSColin Finck }
476c2c66affSColin Finck 
477c2c66affSColin Finck /***********************************************************************
478c2c66affSColin Finck  *                              FD31_TestPath      [internal]
479c2c66affSColin Finck  *      before accepting the file name, test if it includes wild cards
480c2c66affSColin Finck  *      tries to scan the directory and returns TRUE if no error.
481c2c66affSColin Finck  */
FD31_TestPath(const FD31_DATA * lfs,LPWSTR path)482c2c66affSColin Finck static LRESULT FD31_TestPath( const FD31_DATA *lfs, LPWSTR path )
483c2c66affSColin Finck {
484c2c66affSColin Finck     HWND hWnd = lfs->hwnd;
485c2c66affSColin Finck     LPWSTR pBeginFileName, pstr2;
486c2c66affSColin Finck     WCHAR tmpstr2[BUFFILE];
487c2c66affSColin Finck 
488*5981ef3eSAmine Khaldi     pBeginFileName = wcsrchr(path, '\\');
489c2c66affSColin Finck     if (pBeginFileName == NULL)
490*5981ef3eSAmine Khaldi 	pBeginFileName = wcsrchr(path, ':');
491c2c66affSColin Finck 
492*5981ef3eSAmine Khaldi     if (wcschr(path,'*') != NULL || wcschr(path,'?') != NULL)
493c2c66affSColin Finck     {
494c2c66affSColin Finck         /* edit control contains wildcards */
495c2c66affSColin Finck         if (pBeginFileName != NULL)
496c2c66affSColin Finck         {
497c2c66affSColin Finck 	    lstrcpynW(tmpstr2, pBeginFileName + 1, BUFFILE);
498c2c66affSColin Finck 	    *(pBeginFileName + 1) = 0;
499c2c66affSColin Finck 	}
500c2c66affSColin Finck 	else
501c2c66affSColin Finck 	{
502*5981ef3eSAmine Khaldi 	    lstrcpyW(tmpstr2, path);
503c2c66affSColin Finck             if(!(lfs->ofnW->Flags & OFN_NOVALIDATE))
504c2c66affSColin Finck                 *path = 0;
505c2c66affSColin Finck         }
506c2c66affSColin Finck 
507c2c66affSColin Finck         TRACE("path=%s, tmpstr2=%s\n", debugstr_w(path), debugstr_w(tmpstr2));
508c2c66affSColin Finck         SetDlgItemTextW( hWnd, edt1, tmpstr2 );
509c2c66affSColin Finck         FD31_ScanDir(lfs->ofnW, hWnd, path);
510c2c66affSColin Finck         return (lfs->ofnW->Flags & OFN_NOVALIDATE) != 0;
511c2c66affSColin Finck     }
512c2c66affSColin Finck 
513c2c66affSColin Finck     /* no wildcards, we might have a directory or a filename */
514c2c66affSColin Finck     /* try appending a wildcard and reading the directory */
515c2c66affSColin Finck 
516c2c66affSColin Finck     pstr2 = path + lstrlenW(path);
517c2c66affSColin Finck     if (pBeginFileName == NULL || *(pBeginFileName + 1) != 0)
518*5981ef3eSAmine Khaldi         lstrcatW(path, FILE_bslash);
519c2c66affSColin Finck 
520c2c66affSColin Finck     /* if ScanDir succeeds, we have changed the directory */
521c2c66affSColin Finck     if (FD31_ScanDir(lfs->ofnW, hWnd, path))
522c2c66affSColin Finck         return FALSE; /* and path is not a valid file name */
523c2c66affSColin Finck 
524c2c66affSColin Finck     /* if not, this must be a filename */
525c2c66affSColin Finck 
526c2c66affSColin Finck     *pstr2 = 0; /* remove the wildcard added before */
527c2c66affSColin Finck 
528c2c66affSColin Finck     if (pBeginFileName != NULL)
529c2c66affSColin Finck     {
530c2c66affSColin Finck         /* strip off the pathname */
531c2c66affSColin Finck         *pBeginFileName = 0;
532c2c66affSColin Finck         SetDlgItemTextW( hWnd, edt1, pBeginFileName + 1 );
533c2c66affSColin Finck 
534f68b0c51SAmine Khaldi         lstrcpynW(tmpstr2, pBeginFileName + 1, ARRAY_SIZE(tmpstr2));
535c2c66affSColin Finck         /* Should we MessageBox() if this fails? */
536c2c66affSColin Finck         if (!FD31_ScanDir(lfs->ofnW, hWnd, path))
537c2c66affSColin Finck         {
538c2c66affSColin Finck             return FALSE;
539c2c66affSColin Finck         }
540*5981ef3eSAmine Khaldi         lstrcpyW(path, tmpstr2);
541c2c66affSColin Finck     }
542c2c66affSColin Finck     else
543c2c66affSColin Finck         SetDlgItemTextW( hWnd, edt1, path );
544c2c66affSColin Finck     return TRUE;
545c2c66affSColin Finck }
546c2c66affSColin Finck 
547c2c66affSColin Finck /***********************************************************************
548c2c66affSColin Finck  *                              FD31_Validate               [internal]
549c2c66affSColin Finck  *   called on: click Ok button, Enter in edit, DoubleClick in file list
550c2c66affSColin Finck  */
FD31_Validate(const FD31_DATA * lfs,LPCWSTR path,UINT control,INT itemIndex,BOOL internalUse)551c2c66affSColin Finck static LRESULT FD31_Validate( const FD31_DATA *lfs, LPCWSTR path, UINT control, INT itemIndex,
552c2c66affSColin Finck                                  BOOL internalUse )
553c2c66affSColin Finck {
554c2c66affSColin Finck     LONG lRet;
555c2c66affSColin Finck     HWND hWnd = lfs->hwnd;
556c2c66affSColin Finck     OPENFILENAMEW ofnsav;
557c2c66affSColin Finck     LPOPENFILENAMEW ofnW = lfs->ofnW;
558c2c66affSColin Finck     WCHAR filename[BUFFILE];
559c2c66affSColin Finck     int copied_size = min( ofnW->lStructSize, sizeof(ofnsav) );
560c2c66affSColin Finck 
561c2c66affSColin Finck     memcpy( &ofnsav, ofnW, copied_size ); /* for later restoring */
562c2c66affSColin Finck 
563c2c66affSColin Finck     /* get current file name */
564c2c66affSColin Finck     if (path)
565f68b0c51SAmine Khaldi         lstrcpynW(filename, path, ARRAY_SIZE(filename));
566c2c66affSColin Finck     else
567f68b0c51SAmine Khaldi         GetDlgItemTextW( hWnd, edt1, filename, ARRAY_SIZE(filename));
568c2c66affSColin Finck 
569c2c66affSColin Finck     TRACE("got filename = %s\n", debugstr_w(filename));
570c2c66affSColin Finck     /* if we did not click in file list to get there */
571c2c66affSColin Finck     if (control != lst1)
572c2c66affSColin Finck     {
573c2c66affSColin Finck         if (!FD31_TestPath( lfs, filename) )
574c2c66affSColin Finck            return FALSE;
575c2c66affSColin Finck     }
576c2c66affSColin Finck     FD31_UpdateResult(lfs, filename);
577c2c66affSColin Finck 
578c2c66affSColin Finck     if (internalUse)
579c2c66affSColin Finck     { /* called internally after a change in a combo */
580c2c66affSColin Finck         if (lfs->hook)
581c2c66affSColin Finck         {
582c2c66affSColin Finck              FD31_CallWindowProc(lfs, lfs->lbselchstring, control,
583c2c66affSColin Finck                              MAKELONG(itemIndex,CD_LBSELCHANGE));
584c2c66affSColin Finck         }
585c2c66affSColin Finck         return TRUE;
586c2c66affSColin Finck     }
587c2c66affSColin Finck 
588c2c66affSColin Finck     FD31_UpdateFileTitle(lfs);
589c2c66affSColin Finck     if (lfs->hook)
590c2c66affSColin Finck     {
591c2c66affSColin Finck         lRet = FD31_CallWindowProc(lfs, lfs->fileokstring,
592c2c66affSColin Finck                   0, lfs->lParam );
593c2c66affSColin Finck         if (lRet)
594c2c66affSColin Finck         {
595c2c66affSColin Finck             memcpy( ofnW, &ofnsav, copied_size ); /* restore old state */
596c2c66affSColin Finck             return FALSE;
597c2c66affSColin Finck         }
598c2c66affSColin Finck     }
599c2c66affSColin Finck     if ((ofnW->Flags & OFN_ALLOWMULTISELECT) && (ofnW->Flags & OFN_EXPLORER))
600c2c66affSColin Finck     {
601c2c66affSColin Finck         if (ofnW->lpstrFile)
602c2c66affSColin Finck         {
603c2c66affSColin Finck             LPWSTR str = ofnW->lpstrFile;
604*5981ef3eSAmine Khaldi             LPWSTR ptr = wcsrchr(str, '\\');
605c2c66affSColin Finck 	    str[lstrlenW(str) + 1] = '\0';
606c2c66affSColin Finck 	    *ptr = 0;
607c2c66affSColin Finck         }
608c2c66affSColin Finck     }
609c2c66affSColin Finck     return TRUE;
610c2c66affSColin Finck }
611c2c66affSColin Finck 
612c2c66affSColin Finck /***********************************************************************
613c2c66affSColin Finck  *                              FD31_DiskChange             [internal]
614c2c66affSColin Finck  *    called when a new item is picked in the disk selection combo
615c2c66affSColin Finck  */
FD31_DiskChange(const FD31_DATA * lfs)616c2c66affSColin Finck static LRESULT FD31_DiskChange( const FD31_DATA *lfs )
617c2c66affSColin Finck {
618c2c66affSColin Finck     LONG lRet;
619c2c66affSColin Finck     HWND hWnd = lfs->hwnd;
620c2c66affSColin Finck     LPWSTR pstr;
621c2c66affSColin Finck     WCHAR diskname[BUFFILE];
622c2c66affSColin Finck 
623c2c66affSColin Finck     FD31_StripEditControl(hWnd);
624c2c66affSColin Finck     lRet = SendDlgItemMessageW(hWnd, cmb2, CB_GETCURSEL, 0, 0L);
625c2c66affSColin Finck     if (lRet == LB_ERR)
626c2c66affSColin Finck         return 0;
627f68b0c51SAmine Khaldi     pstr = heap_alloc(BUFFILEALLOC);
628c2c66affSColin Finck     SendDlgItemMessageW(hWnd, cmb2, CB_GETLBTEXT, lRet,
629c2c66affSColin Finck                          (LPARAM)pstr);
630c2c66affSColin Finck     wsprintfW(diskname, FILE_specc, pstr[2]);
631f68b0c51SAmine Khaldi     heap_free(pstr);
632c2c66affSColin Finck 
633c2c66affSColin Finck     return FD31_Validate( lfs, diskname, cmb2, lRet, TRUE );
634c2c66affSColin Finck }
635c2c66affSColin Finck 
636c2c66affSColin Finck /***********************************************************************
637c2c66affSColin Finck  *                              FD31_FileTypeChange         [internal]
638c2c66affSColin Finck  *    called when a new item is picked in the file type combo
639c2c66affSColin Finck  */
FD31_FileTypeChange(const FD31_DATA * lfs)640c2c66affSColin Finck static LRESULT FD31_FileTypeChange( const FD31_DATA *lfs )
641c2c66affSColin Finck {
642c2c66affSColin Finck     LONG lRet;
643c2c66affSColin Finck     LPWSTR pstr;
644c2c66affSColin Finck 
645c2c66affSColin Finck     lRet = SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETCURSEL, 0, 0);
646c2c66affSColin Finck     if (lRet == LB_ERR)
647c2c66affSColin Finck         return TRUE;
648c2c66affSColin Finck     lfs->ofnW->nFilterIndex = lRet + 1;
649c2c66affSColin Finck     if (lfs->ofnA)
650c2c66affSColin Finck         lfs->ofnA->nFilterIndex = lRet + 1;
651c2c66affSColin Finck     pstr = (LPWSTR)SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETITEMDATA, lRet, 0);
652c2c66affSColin Finck     TRACE("Selected filter : %s\n", debugstr_w(pstr));
653c2c66affSColin Finck 
654c2c66affSColin Finck     return FD31_Validate( lfs, pstr, cmb1, lRet, TRUE );
655c2c66affSColin Finck }
656c2c66affSColin Finck 
657c2c66affSColin Finck /***********************************************************************
658c2c66affSColin Finck  *                              FD31_WMCommand               [internal]
659c2c66affSColin Finck  */
FD31_WMCommand(HWND hWnd,LPARAM lParam,UINT notification,UINT control,const FD31_DATA * lfs)660c2c66affSColin Finck static LRESULT FD31_WMCommand( HWND hWnd, LPARAM lParam, UINT notification,
661c2c66affSColin Finck 			       UINT control, const FD31_DATA *lfs )
662c2c66affSColin Finck {
663c2c66affSColin Finck     switch (control)
664c2c66affSColin Finck     {
665c2c66affSColin Finck         case lst1: /* file list */
666c2c66affSColin Finck         FD31_StripEditControl(hWnd);
667c2c66affSColin Finck         if (notification == LBN_DBLCLK)
668c2c66affSColin Finck         {
669c2c66affSColin Finck             return SendMessageW(hWnd, WM_COMMAND, IDOK, 0);
670c2c66affSColin Finck         }
671c2c66affSColin Finck         else if (notification == LBN_SELCHANGE)
672c2c66affSColin Finck             return FD31_FileListSelect( lfs );
673c2c66affSColin Finck         break;
674c2c66affSColin Finck 
675c2c66affSColin Finck         case lst2: /* directory list */
676c2c66affSColin Finck         FD31_StripEditControl(hWnd);
677c2c66affSColin Finck         if (notification == LBN_DBLCLK)
678c2c66affSColin Finck             return FD31_DirListDblClick( lfs );
679c2c66affSColin Finck         break;
680c2c66affSColin Finck 
681c2c66affSColin Finck         case cmb1: /* file type drop list */
682c2c66affSColin Finck         if (notification == CBN_SELCHANGE)
683c2c66affSColin Finck             return FD31_FileTypeChange( lfs );
684c2c66affSColin Finck         break;
685c2c66affSColin Finck 
686c2c66affSColin Finck         case chx1:
687c2c66affSColin Finck         break;
688c2c66affSColin Finck 
689c2c66affSColin Finck         case pshHelp:
690c2c66affSColin Finck         break;
691c2c66affSColin Finck 
692c2c66affSColin Finck         case cmb2: /* disk dropdown combo */
693c2c66affSColin Finck         if (notification == CBN_SELCHANGE)
694c2c66affSColin Finck             return FD31_DiskChange( lfs );
695c2c66affSColin Finck         break;
696c2c66affSColin Finck 
697c2c66affSColin Finck         case IDOK:
698c2c66affSColin Finck         TRACE("OK pressed\n");
699c2c66affSColin Finck         if (FD31_Validate( lfs, NULL, control, 0, FALSE ))
700c2c66affSColin Finck             EndDialog(hWnd, TRUE);
701c2c66affSColin Finck         return TRUE;
702c2c66affSColin Finck 
703c2c66affSColin Finck         case IDCANCEL:
704c2c66affSColin Finck         EndDialog(hWnd, FALSE);
705c2c66affSColin Finck         return TRUE;
706c2c66affSColin Finck 
707c2c66affSColin Finck         case IDABORT: /* can be sent by the hook procedure */
708c2c66affSColin Finck         EndDialog(hWnd, TRUE);
709c2c66affSColin Finck         return TRUE;
710c2c66affSColin Finck     }
711c2c66affSColin Finck     return FALSE;
712c2c66affSColin Finck }
713c2c66affSColin Finck 
714c2c66affSColin Finck /************************************************************************
715c2c66affSColin Finck  *                              FD31_MapStringPairsToW       [internal]
716c2c66affSColin Finck  *      map string pairs to Unicode
717c2c66affSColin Finck  */
FD31_MapStringPairsToW(LPCSTR strA,UINT size)718c2c66affSColin Finck static LPWSTR FD31_MapStringPairsToW(LPCSTR strA, UINT size)
719c2c66affSColin Finck {
720c2c66affSColin Finck     LPCSTR s;
721c2c66affSColin Finck     LPWSTR x;
722c2c66affSColin Finck     unsigned int n, len;
723c2c66affSColin Finck 
724c2c66affSColin Finck     s = strA;
725c2c66affSColin Finck     while (*s)
726c2c66affSColin Finck         s = s+strlen(s)+1;
727c2c66affSColin Finck     s++;
728c2c66affSColin Finck     n = s + 1 - strA; /* Don't forget the other \0 */
729c2c66affSColin Finck     if (n < size) n = size;
730c2c66affSColin Finck 
731c2c66affSColin Finck     len = MultiByteToWideChar( CP_ACP, 0, strA, n, NULL, 0 );
732f68b0c51SAmine Khaldi     x = heap_alloc(len * sizeof(WCHAR));
733c2c66affSColin Finck     MultiByteToWideChar( CP_ACP, 0, strA, n, x, len );
734c2c66affSColin Finck     return x;
735c2c66affSColin Finck }
736c2c66affSColin Finck 
737c2c66affSColin Finck 
738c2c66affSColin Finck /************************************************************************
739c2c66affSColin Finck  *                              FD31_DupToW                  [internal]
740c2c66affSColin Finck  *      duplicates an Ansi string to unicode, with a buffer size
741c2c66affSColin Finck  */
FD31_DupToW(LPCSTR str,DWORD size)742c2c66affSColin Finck static LPWSTR FD31_DupToW(LPCSTR str, DWORD size)
743c2c66affSColin Finck {
744c2c66affSColin Finck     LPWSTR strW = NULL;
745c2c66affSColin Finck     if (str && (size > 0))
746c2c66affSColin Finck     {
747f68b0c51SAmine Khaldi         strW = heap_alloc(size * sizeof(WCHAR));
748c2c66affSColin Finck         if (strW) MultiByteToWideChar( CP_ACP, 0, str, -1, strW, size );
749c2c66affSColin Finck     }
750c2c66affSColin Finck     return strW;
751c2c66affSColin Finck }
752c2c66affSColin Finck 
753c2c66affSColin Finck /************************************************************************
754c2c66affSColin Finck  *                              FD31_MapOfnStructA          [internal]
755c2c66affSColin Finck  *      map a 32 bits Ansi structure to a Unicode one
756c2c66affSColin Finck  */
FD31_MapOfnStructA(const OPENFILENAMEA * ofnA,LPOPENFILENAMEW ofnW,BOOL open)757c2c66affSColin Finck static void FD31_MapOfnStructA(const OPENFILENAMEA *ofnA, LPOPENFILENAMEW ofnW, BOOL open)
758c2c66affSColin Finck {
759c2c66affSColin Finck     UNICODE_STRING usBuffer;
760c2c66affSColin Finck 
761c2c66affSColin Finck     ofnW->hwndOwner = ofnA->hwndOwner;
762c2c66affSColin Finck     ofnW->hInstance = ofnA->hInstance;
763c2c66affSColin Finck     if (ofnA->lpstrFilter)
764c2c66affSColin Finck         ofnW->lpstrFilter = FD31_MapStringPairsToW(ofnA->lpstrFilter, 0);
765c2c66affSColin Finck 
766c2c66affSColin Finck     if ((ofnA->lpstrCustomFilter) && (*(ofnA->lpstrCustomFilter)))
767c2c66affSColin Finck         ofnW->lpstrCustomFilter = FD31_MapStringPairsToW(ofnA->lpstrCustomFilter, ofnA->nMaxCustFilter);
768c2c66affSColin Finck     ofnW->nMaxCustFilter = ofnA->nMaxCustFilter;
769c2c66affSColin Finck     ofnW->nFilterIndex = ofnA->nFilterIndex;
770c2c66affSColin Finck     ofnW->nMaxFile = ofnA->nMaxFile;
771c2c66affSColin Finck     ofnW->lpstrFile = FD31_DupToW(ofnA->lpstrFile, ofnW->nMaxFile);
772c2c66affSColin Finck     ofnW->nMaxFileTitle = ofnA->nMaxFileTitle;
773c2c66affSColin Finck     ofnW->lpstrFileTitle = FD31_DupToW(ofnA->lpstrFileTitle, ofnW->nMaxFileTitle);
774c2c66affSColin Finck     if (ofnA->lpstrInitialDir)
775c2c66affSColin Finck     {
776c2c66affSColin Finck         RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpstrInitialDir);
777c2c66affSColin Finck         ofnW->lpstrInitialDir = usBuffer.Buffer;
778c2c66affSColin Finck     }
779c2c66affSColin Finck     if (ofnA->lpstrTitle) {
780c2c66affSColin Finck         RtlCreateUnicodeStringFromAsciiz (&usBuffer, ofnA->lpstrTitle);
781c2c66affSColin Finck         ofnW->lpstrTitle = usBuffer.Buffer;
782c2c66affSColin Finck     } else {
783c2c66affSColin Finck         WCHAR buf[16];
784c2c66affSColin Finck         LPWSTR title_tmp;
785c2c66affSColin Finck         int len;
786f68b0c51SAmine Khaldi         LoadStringW(COMDLG32_hInstance, open ? IDS_OPEN_FILE : IDS_SAVE_AS, buf, ARRAY_SIZE(buf));
787c2c66affSColin Finck         len = lstrlenW(buf)+1;
788f68b0c51SAmine Khaldi         title_tmp = heap_alloc(len * sizeof(WCHAR));
789c2c66affSColin Finck         memcpy(title_tmp, buf, len * sizeof(WCHAR));
790c2c66affSColin Finck         ofnW->lpstrTitle = title_tmp;
791c2c66affSColin Finck     }
792c2c66affSColin Finck     ofnW->Flags = ofnA->Flags;
793c2c66affSColin Finck     ofnW->nFileOffset = ofnA->nFileOffset;
794c2c66affSColin Finck     ofnW->nFileExtension = ofnA->nFileExtension;
795c2c66affSColin Finck     ofnW->lpstrDefExt = FD31_DupToW(ofnA->lpstrDefExt, 3);
796c2c66affSColin Finck     if ((ofnA->Flags & OFN_ENABLETEMPLATE) && (ofnA->lpTemplateName))
797c2c66affSColin Finck     {
798c2c66affSColin Finck         if (!IS_INTRESOURCE(ofnA->lpTemplateName))
799c2c66affSColin Finck         {
800c2c66affSColin Finck             RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpTemplateName);
801c2c66affSColin Finck             ofnW->lpTemplateName = usBuffer.Buffer;
802c2c66affSColin Finck         }
803c2c66affSColin Finck         else /* numbered resource */
804c2c66affSColin Finck             ofnW->lpTemplateName = (LPCWSTR) ofnA->lpTemplateName;
805c2c66affSColin Finck     }
806c2c66affSColin Finck     if (ofnW->lStructSize > OPENFILENAME_SIZE_VERSION_400W)
807c2c66affSColin Finck     {
808c2c66affSColin Finck         ofnW->pvReserved = ofnA->pvReserved;
809c2c66affSColin Finck         ofnW->dwReserved = ofnA->dwReserved;
810c2c66affSColin Finck         ofnW->FlagsEx    = ofnA->FlagsEx;
811c2c66affSColin Finck     }
812c2c66affSColin Finck }
813c2c66affSColin Finck 
814c2c66affSColin Finck 
815c2c66affSColin Finck /************************************************************************
816c2c66affSColin Finck  *                              FD31_FreeOfnW          [internal]
817c2c66affSColin Finck  *      Undo all allocations done by FD31_MapOfnStructA
818c2c66affSColin Finck  */
FD31_FreeOfnW(OPENFILENAMEW * ofnW)819c2c66affSColin Finck static void FD31_FreeOfnW(OPENFILENAMEW *ofnW)
820c2c66affSColin Finck {
821f68b0c51SAmine Khaldi     heap_free((void *)ofnW->lpstrFilter);
822f68b0c51SAmine Khaldi     heap_free(ofnW->lpstrCustomFilter);
823f68b0c51SAmine Khaldi     heap_free(ofnW->lpstrFile);
824f68b0c51SAmine Khaldi     heap_free(ofnW->lpstrFileTitle);
825f68b0c51SAmine Khaldi     heap_free((void *)ofnW->lpstrInitialDir);
826f68b0c51SAmine Khaldi     heap_free((void *)ofnW->lpstrTitle);
827c2c66affSColin Finck     if (!IS_INTRESOURCE(ofnW->lpTemplateName))
828f68b0c51SAmine Khaldi         heap_free((void *)ofnW->lpTemplateName);
829c2c66affSColin Finck }
830c2c66affSColin Finck 
831c2c66affSColin Finck /************************************************************************
832c2c66affSColin Finck  *                              FD31_DestroyPrivate            [internal]
833c2c66affSColin Finck  *      destroys the private object
834c2c66affSColin Finck  */
FD31_DestroyPrivate(PFD31_DATA lfs)835c2c66affSColin Finck static void FD31_DestroyPrivate(PFD31_DATA lfs)
836c2c66affSColin Finck {
837c2c66affSColin Finck     HWND hwnd;
838c2c66affSColin Finck     if (!lfs) return;
839c2c66affSColin Finck     hwnd = lfs->hwnd;
840c2c66affSColin Finck     TRACE("destroying private allocation %p\n", lfs);
841c2c66affSColin Finck 
842c2c66affSColin Finck     /* if ofnW has been allocated, have to free everything in it */
843c2c66affSColin Finck     if (lfs->ofnA)
844c2c66affSColin Finck     {
845c2c66affSColin Finck         FD31_FreeOfnW(lfs->ofnW);
846f68b0c51SAmine Khaldi         heap_free(lfs->ofnW);
847c2c66affSColin Finck     }
848f68b0c51SAmine Khaldi     heap_free(lfs);
849c2c66affSColin Finck     RemovePropA(hwnd, FD31_OFN_PROP);
850c2c66affSColin Finck }
851c2c66affSColin Finck 
852c2c66affSColin Finck /***********************************************************************
853c2c66affSColin Finck  *           FD31_GetTemplate                                  [internal]
854c2c66affSColin Finck  *
855c2c66affSColin Finck  * Get a template (or FALSE if failure) when 16 bits dialogs are used
856c2c66affSColin Finck  * by a 32 bits application
857c2c66affSColin Finck  *
858c2c66affSColin Finck  */
FD31_GetTemplate(PFD31_DATA lfs)859c2c66affSColin Finck static BOOL FD31_GetTemplate(PFD31_DATA lfs)
860c2c66affSColin Finck {
861c2c66affSColin Finck     LPOPENFILENAMEW ofnW = lfs->ofnW;
862c2c66affSColin Finck     LPOPENFILENAMEA ofnA = lfs->ofnA;
863c2c66affSColin Finck     HANDLE hDlgTmpl;
864c2c66affSColin Finck 
865c2c66affSColin Finck     if (ofnW->Flags & OFN_ENABLETEMPLATEHANDLE)
866c2c66affSColin Finck     {
867c2c66affSColin Finck         if (!(lfs->template = LockResource( ofnW->hInstance )))
868c2c66affSColin Finck         {
869c2c66affSColin Finck             COMDLG32_SetCommDlgExtendedError( CDERR_LOADRESFAILURE );
870c2c66affSColin Finck             return FALSE;
871c2c66affSColin Finck         }
872c2c66affSColin Finck     }
873c2c66affSColin Finck     else if (ofnW->Flags & OFN_ENABLETEMPLATE)
874c2c66affSColin Finck     {
875c2c66affSColin Finck         HRSRC hResInfo;
876c2c66affSColin Finck         if (ofnA)
877c2c66affSColin Finck             hResInfo = FindResourceA( ofnA->hInstance, ofnA->lpTemplateName, (LPSTR)RT_DIALOG );
878c2c66affSColin Finck         else
879c2c66affSColin Finck             hResInfo = FindResourceW( ofnW->hInstance, ofnW->lpTemplateName, (LPWSTR)RT_DIALOG );
880c2c66affSColin Finck         if (!hResInfo)
881c2c66affSColin Finck         {
882c2c66affSColin Finck             COMDLG32_SetCommDlgExtendedError( CDERR_FINDRESFAILURE );
883c2c66affSColin Finck             return FALSE;
884c2c66affSColin Finck         }
885c2c66affSColin Finck         if (!(hDlgTmpl = LoadResource( ofnW->hInstance, hResInfo )) ||
886c2c66affSColin Finck             !(lfs->template = LockResource( hDlgTmpl )))
887c2c66affSColin Finck         {
888c2c66affSColin Finck             COMDLG32_SetCommDlgExtendedError( CDERR_LOADRESFAILURE );
889c2c66affSColin Finck             return FALSE;
890c2c66affSColin Finck         }
891c2c66affSColin Finck     }
892c2c66affSColin Finck     else /* get it from internal Wine resource */
893c2c66affSColin Finck     {
894c2c66affSColin Finck         HRSRC hResInfo;
895c2c66affSColin Finck         if (!(hResInfo = FindResourceA( COMDLG32_hInstance, lfs->open ? "OPEN_FILE" : "SAVE_FILE", (LPSTR)RT_DIALOG )))
896c2c66affSColin Finck         {
897c2c66affSColin Finck             COMDLG32_SetCommDlgExtendedError( CDERR_FINDRESFAILURE );
898c2c66affSColin Finck             return FALSE;
899c2c66affSColin Finck         }
900c2c66affSColin Finck         if (!(hDlgTmpl = LoadResource( COMDLG32_hInstance, hResInfo )) ||
901c2c66affSColin Finck             !(lfs->template = LockResource( hDlgTmpl )))
902c2c66affSColin Finck         {
903c2c66affSColin Finck             COMDLG32_SetCommDlgExtendedError( CDERR_LOADRESFAILURE );
904c2c66affSColin Finck             return FALSE;
905c2c66affSColin Finck         }
906c2c66affSColin Finck     }
907c2c66affSColin Finck     return TRUE;
908c2c66affSColin Finck }
909c2c66affSColin Finck 
910c2c66affSColin Finck /************************************************************************
911c2c66affSColin Finck  *                              FD31_AllocPrivate            [internal]
912c2c66affSColin Finck  *      allocate a private object to hold 32 bits Unicode
913c2c66affSColin Finck  *      structure that will be used throughout the calls, while
914c2c66affSColin Finck  *      keeping available the original structures and a few variables
915c2c66affSColin Finck  *      On entry : type = dialog procedure type (16,32A,32W)
916c2c66affSColin Finck  *                 dlgType = dialog type (open or save)
917c2c66affSColin Finck  */
FD31_AllocPrivate(LPARAM lParam,UINT dlgType,BOOL IsUnicode)918c2c66affSColin Finck static PFD31_DATA FD31_AllocPrivate(LPARAM lParam, UINT dlgType, BOOL IsUnicode)
919c2c66affSColin Finck {
920f68b0c51SAmine Khaldi     FD31_DATA *lfs = heap_alloc_zero(sizeof(*lfs));
921c2c66affSColin Finck 
922c2c66affSColin Finck     TRACE("alloc private buf %p\n", lfs);
923c2c66affSColin Finck     if (!lfs) return NULL;
924c2c66affSColin Finck     lfs->hook = FALSE;
925c2c66affSColin Finck     lfs->lParam = lParam;
926c2c66affSColin Finck     lfs->open = (dlgType == OPEN_DIALOG);
927c2c66affSColin Finck 
928c2c66affSColin Finck     if (IsUnicode)
929c2c66affSColin Finck     {
930c2c66affSColin Finck         lfs->ofnA = NULL;
931c2c66affSColin Finck         lfs->ofnW = (LPOPENFILENAMEW) lParam;
932c2c66affSColin Finck         if (lfs->ofnW->Flags & OFN_ENABLEHOOK)
933c2c66affSColin Finck             if (lfs->ofnW->lpfnHook)
934c2c66affSColin Finck                 lfs->hook = TRUE;
935c2c66affSColin Finck     }
936c2c66affSColin Finck     else
937c2c66affSColin Finck     {
938c2c66affSColin Finck         lfs->ofnA = (LPOPENFILENAMEA) lParam;
939c2c66affSColin Finck         if (lfs->ofnA->Flags & OFN_ENABLEHOOK)
940c2c66affSColin Finck             if (lfs->ofnA->lpfnHook)
941c2c66affSColin Finck                 lfs->hook = TRUE;
942f68b0c51SAmine Khaldi         lfs->ofnW = heap_alloc_zero(lfs->ofnA->lStructSize);
943c2c66affSColin Finck         lfs->ofnW->lStructSize = lfs->ofnA->lStructSize;
944c2c66affSColin Finck         FD31_MapOfnStructA(lfs->ofnA, lfs->ofnW, lfs->open);
945c2c66affSColin Finck     }
946c2c66affSColin Finck 
947c2c66affSColin Finck     if (! FD31_GetTemplate(lfs))
948c2c66affSColin Finck     {
949c2c66affSColin Finck         FD31_DestroyPrivate(lfs);
950c2c66affSColin Finck         return NULL;
951c2c66affSColin Finck     }
952c2c66affSColin Finck     lfs->lbselchstring = RegisterWindowMessageA(LBSELCHSTRINGA);
953c2c66affSColin Finck     lfs->fileokstring = RegisterWindowMessageA(FILEOKSTRINGA);
954c2c66affSColin Finck 
955c2c66affSColin Finck     return lfs;
956c2c66affSColin Finck }
957c2c66affSColin Finck 
958c2c66affSColin Finck /***********************************************************************
959c2c66affSColin Finck  *                              FD31_WMInitDialog            [internal]
960c2c66affSColin Finck  */
FD31_WMInitDialog(HWND hWnd,WPARAM wParam,LPARAM lParam)961c2c66affSColin Finck static LONG FD31_WMInitDialog(HWND hWnd, WPARAM wParam, LPARAM lParam)
962c2c66affSColin Finck {
963c2c66affSColin Finck   int i, n;
964c2c66affSColin Finck   WCHAR tmpstr[BUFFILE];
965c2c66affSColin Finck   LPWSTR pstr, old_pstr;
966c2c66affSColin Finck   LPOPENFILENAMEW ofn;
967c2c66affSColin Finck   PFD31_DATA lfs = (PFD31_DATA) lParam;
968c2c66affSColin Finck 
969c2c66affSColin Finck   if (!lfs) return FALSE;
970c2c66affSColin Finck   SetPropA(hWnd, FD31_OFN_PROP, lfs);
971c2c66affSColin Finck   lfs->hwnd = hWnd;
972c2c66affSColin Finck   ofn = lfs->ofnW;
973c2c66affSColin Finck 
974c2c66affSColin Finck   TRACE("flags=%x initialdir=%s\n", ofn->Flags, debugstr_w(ofn->lpstrInitialDir));
975c2c66affSColin Finck 
976c2c66affSColin Finck   SetWindowTextW( hWnd, ofn->lpstrTitle );
977c2c66affSColin Finck   /* read custom filter information */
978c2c66affSColin Finck   if (ofn->lpstrCustomFilter)
979c2c66affSColin Finck     {
980c2c66affSColin Finck       pstr = ofn->lpstrCustomFilter;
981c2c66affSColin Finck       n = 0;
982c2c66affSColin Finck       TRACE("lpstrCustomFilter = %p\n", pstr);
983c2c66affSColin Finck       while(*pstr)
984c2c66affSColin Finck 	{
985c2c66affSColin Finck 	  old_pstr = pstr;
986c2c66affSColin Finck           i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
987c2c66affSColin Finck                                    (LPARAM)(ofn->lpstrCustomFilter) + n );
988c2c66affSColin Finck           n += lstrlenW(pstr) + 1;
989c2c66affSColin Finck 	  pstr += lstrlenW(pstr) + 1;
990c2c66affSColin Finck 	  TRACE("add str=%s associated to %s\n",
991c2c66affSColin Finck                 debugstr_w(old_pstr), debugstr_w(pstr));
992c2c66affSColin Finck           SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
993c2c66affSColin Finck           n += lstrlenW(pstr) + 1;
994c2c66affSColin Finck 	  pstr += lstrlenW(pstr) + 1;
995c2c66affSColin Finck 	}
996c2c66affSColin Finck     }
997c2c66affSColin Finck   /* read filter information */
998c2c66affSColin Finck   if (ofn->lpstrFilter) {
999c2c66affSColin Finck 	pstr = (LPWSTR) ofn->lpstrFilter;
1000c2c66affSColin Finck 	n = 0;
1001c2c66affSColin Finck 	while(*pstr) {
1002c2c66affSColin Finck 	  old_pstr = pstr;
1003c2c66affSColin Finck 	  i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
1004c2c66affSColin Finck 				       (LPARAM)(ofn->lpstrFilter + n) );
1005c2c66affSColin Finck 	  n += lstrlenW(pstr) + 1;
1006c2c66affSColin Finck 	  pstr += lstrlenW(pstr) + 1;
1007c2c66affSColin Finck 	  TRACE("add str=%s associated to %s\n",
1008c2c66affSColin Finck                 debugstr_w(old_pstr), debugstr_w(pstr));
1009c2c66affSColin Finck 	  SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
1010c2c66affSColin Finck 	  n += lstrlenW(pstr) + 1;
1011c2c66affSColin Finck 	  pstr += lstrlenW(pstr) + 1;
1012c2c66affSColin Finck 	}
1013c2c66affSColin Finck   }
1014c2c66affSColin Finck   /* set default filter */
1015c2c66affSColin Finck   if (ofn->nFilterIndex == 0 && ofn->lpstrCustomFilter == NULL)
1016c2c66affSColin Finck   	ofn->nFilterIndex = 1;
1017c2c66affSColin Finck   SendDlgItemMessageW(hWnd, cmb1, CB_SETCURSEL, ofn->nFilterIndex - 1, 0);
1018c2c66affSColin Finck   if (ofn->lpstrFile && ofn->lpstrFile[0])
1019c2c66affSColin Finck   {
1020c2c66affSColin Finck     TRACE( "SetText of edt1 to %s\n", debugstr_w(ofn->lpstrFile) );
1021c2c66affSColin Finck     SetDlgItemTextW( hWnd, edt1, ofn->lpstrFile );
1022c2c66affSColin Finck   }
1023c2c66affSColin Finck   else
1024c2c66affSColin Finck   {
1025c2c66affSColin Finck     lstrcpynW(tmpstr, FD31_GetFileType(ofn->lpstrCustomFilter,
1026c2c66affSColin Finck 	     ofn->lpstrFilter, ofn->nFilterIndex - 1),BUFFILE);
1027c2c66affSColin Finck     TRACE("nFilterIndex = %d, SetText of edt1 to %s\n",
1028c2c66affSColin Finck   			ofn->nFilterIndex, debugstr_w(tmpstr));
1029c2c66affSColin Finck     SetDlgItemTextW( hWnd, edt1, tmpstr );
1030c2c66affSColin Finck   }
1031c2c66affSColin Finck   /* get drive list */
1032c2c66affSColin Finck   *tmpstr = 0;
1033c2c66affSColin Finck   DlgDirListComboBoxW(hWnd, tmpstr, cmb2, 0, DDL_DRIVES | DDL_EXCLUSIVE);
1034c2c66affSColin Finck   /* read initial directory */
1035c2c66affSColin Finck   /* FIXME: Note that this is now very version-specific (See MSDN description of
1036c2c66affSColin Finck    * the OPENFILENAME structure).  For example under 2000/XP any path in the
1037c2c66affSColin Finck    * lpstrFile overrides the lpstrInitialDir, but not under 95/98/ME
1038c2c66affSColin Finck    */
1039c2c66affSColin Finck   if (ofn->lpstrInitialDir != NULL)
1040c2c66affSColin Finck     {
1041c2c66affSColin Finck       int len;
1042c2c66affSColin Finck       lstrcpynW(tmpstr, ofn->lpstrInitialDir, 511);
1043c2c66affSColin Finck       len = lstrlenW(tmpstr);
1044c2c66affSColin Finck       if (len > 0 && tmpstr[len-1] != '\\'  && tmpstr[len-1] != ':') {
1045c2c66affSColin Finck         tmpstr[len]='\\';
1046c2c66affSColin Finck         tmpstr[len+1]='\0';
1047c2c66affSColin Finck       }
1048c2c66affSColin Finck     }
1049c2c66affSColin Finck   else
1050c2c66affSColin Finck     *tmpstr = 0;
1051c2c66affSColin Finck   if (!FD31_ScanDir(ofn, hWnd, tmpstr)) {
1052c2c66affSColin Finck     *tmpstr = 0;
1053c2c66affSColin Finck     if (!FD31_ScanDir(ofn, hWnd, tmpstr))
1054c2c66affSColin Finck       WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr));
1055c2c66affSColin Finck   }
1056c2c66affSColin Finck   /* select current drive in combo 2, omit missing drives */
1057c2c66affSColin Finck   {
1058c2c66affSColin Finck       char dir[MAX_PATH];
1059c2c66affSColin Finck       char str[4] = "a:\\";
1060c2c66affSColin Finck       GetCurrentDirectoryA( sizeof(dir), dir );
1061c2c66affSColin Finck       for(i = 0, n = -1; i < 26; i++)
1062c2c66affSColin Finck       {
1063c2c66affSColin Finck           str[0] = 'a' + i;
1064c2c66affSColin Finck           if (GetDriveTypeA(str) > DRIVE_NO_ROOT_DIR) n++;
1065c2c66affSColin Finck           if (toupper(str[0]) == toupper(dir[0])) break;
1066c2c66affSColin Finck       }
1067c2c66affSColin Finck   }
1068c2c66affSColin Finck   SendDlgItemMessageW(hWnd, cmb2, CB_SETCURSEL, n, 0);
1069c2c66affSColin Finck   if (!(ofn->Flags & OFN_SHOWHELP))
1070c2c66affSColin Finck     ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
1071c2c66affSColin Finck   if (ofn->Flags & OFN_HIDEREADONLY)
1072c2c66affSColin Finck     ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
1073c2c66affSColin Finck   if (lfs->hook)
1074c2c66affSColin Finck       return FD31_CallWindowProc(lfs, WM_INITDIALOG, wParam, lfs->lParam);
1075c2c66affSColin Finck   return TRUE;
1076c2c66affSColin Finck }
1077c2c66affSColin Finck 
FD31_GetFldrHeight(void)1078c2c66affSColin Finck static int FD31_GetFldrHeight(void)
1079c2c66affSColin Finck {
1080c2c66affSColin Finck   return fldrHeight;
1081c2c66affSColin Finck }
1082c2c66affSColin Finck 
1083c2c66affSColin Finck /***********************************************************************
1084c2c66affSColin Finck  *                              FD31_WMMeasureItem           [internal]
1085c2c66affSColin Finck  */
FD31_WMMeasureItem(LPARAM lParam)1086c2c66affSColin Finck static LONG FD31_WMMeasureItem(LPARAM lParam)
1087c2c66affSColin Finck {
1088c2c66affSColin Finck     LPMEASUREITEMSTRUCT lpmeasure;
1089c2c66affSColin Finck 
1090c2c66affSColin Finck     lpmeasure = (LPMEASUREITEMSTRUCT)lParam;
1091c2c66affSColin Finck     lpmeasure->itemHeight = FD31_GetFldrHeight();
1092c2c66affSColin Finck     return TRUE;
1093c2c66affSColin Finck }
1094c2c66affSColin Finck 
1095c2c66affSColin Finck 
1096c2c66affSColin Finck /***********************************************************************
1097c2c66affSColin Finck  *           FileOpenDlgProc                                    [internal]
1098c2c66affSColin Finck  *      Used for open and save, in fact.
1099c2c66affSColin Finck  */
FD31_FileOpenDlgProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)1100c2c66affSColin Finck static INT_PTR CALLBACK FD31_FileOpenDlgProc(HWND hWnd, UINT wMsg,
1101c2c66affSColin Finck                                              WPARAM wParam, LPARAM lParam)
1102c2c66affSColin Finck {
1103c2c66affSColin Finck     PFD31_DATA lfs = (PFD31_DATA)GetPropA( hWnd, FD31_OFN_PROP );
1104c2c66affSColin Finck 
1105c2c66affSColin Finck     TRACE("msg=%x wparam=%lx lParam=%lx\n", wMsg, wParam, lParam);
1106c2c66affSColin Finck     if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
1107c2c66affSColin Finck     {
1108c2c66affSColin Finck         INT_PTR lRet;
1109c2c66affSColin Finck         lRet  = (INT_PTR)FD31_CallWindowProc( lfs, wMsg, wParam, lParam );
1110c2c66affSColin Finck         if (lRet) return lRet;   /* else continue message processing */
1111c2c66affSColin Finck     }
1112c2c66affSColin Finck     switch (wMsg)
1113c2c66affSColin Finck     {
1114c2c66affSColin Finck     case WM_INITDIALOG:
1115c2c66affSColin Finck         return FD31_WMInitDialog( hWnd, wParam, lParam );
1116c2c66affSColin Finck 
1117c2c66affSColin Finck     case WM_MEASUREITEM:
1118c2c66affSColin Finck         return FD31_WMMeasureItem( lParam );
1119c2c66affSColin Finck 
1120c2c66affSColin Finck     case WM_DRAWITEM:
1121c2c66affSColin Finck         return FD31_WMDrawItem( hWnd, wParam, lParam, !lfs->open, (DRAWITEMSTRUCT *)lParam );
1122c2c66affSColin Finck 
1123c2c66affSColin Finck     case WM_COMMAND:
1124c2c66affSColin Finck         return FD31_WMCommand( hWnd, lParam, HIWORD(wParam), LOWORD(wParam), lfs );
1125c2c66affSColin Finck #if 0
1126c2c66affSColin Finck     case WM_CTLCOLOR:
1127c2c66affSColin Finck         SetBkColor( (HDC16)wParam, 0x00C0C0C0 );
1128c2c66affSColin Finck         switch (HIWORD(lParam))
1129c2c66affSColin Finck         {
1130c2c66affSColin Finck         case CTLCOLOR_BTN:
1131c2c66affSColin Finck             SetTextColor( (HDC16)wParam, 0x00000000 );
1132c2c66affSColin Finck             return hGRAYBrush;
1133c2c66affSColin Finck         case CTLCOLOR_STATIC:
1134c2c66affSColin Finck             SetTextColor( (HDC16)wParam, 0x00000000 );
1135c2c66affSColin Finck             return hGRAYBrush;
1136c2c66affSColin Finck         }
1137c2c66affSColin Finck         break;
1138c2c66affSColin Finck #endif
1139c2c66affSColin Finck     }
1140c2c66affSColin Finck     return FALSE;
1141c2c66affSColin Finck }
1142c2c66affSColin Finck 
1143c2c66affSColin Finck /***********************************************************************
1144c2c66affSColin Finck  *           GetFileName31A                                 [internal]
1145c2c66affSColin Finck  *
1146c2c66affSColin Finck  * Creates a win31 style dialog box for the user to select a file to open/save.
1147c2c66affSColin Finck  */
GetFileName31A(OPENFILENAMEA * lpofn,UINT dlgType)1148c2c66affSColin Finck BOOL GetFileName31A( OPENFILENAMEA *lpofn, UINT dlgType )
1149c2c66affSColin Finck {
1150c2c66affSColin Finck     BOOL bRet = FALSE;
1151c2c66affSColin Finck     PFD31_DATA lfs;
1152c2c66affSColin Finck 
1153c2c66affSColin Finck     if (!lpofn || !FD31_Init()) return FALSE;
1154c2c66affSColin Finck 
1155c2c66affSColin Finck     TRACE("ofn flags %08x\n", lpofn->Flags);
1156c2c66affSColin Finck     lfs = FD31_AllocPrivate((LPARAM) lpofn, dlgType, FALSE);
1157c2c66affSColin Finck     if (lfs)
1158c2c66affSColin Finck     {
1159c2c66affSColin Finck         bRet = DialogBoxIndirectParamA( COMDLG32_hInstance, lfs->template, lpofn->hwndOwner,
1160c2c66affSColin Finck                                         FD31_FileOpenDlgProc, (LPARAM)lfs);
1161c2c66affSColin Finck         FD31_DestroyPrivate(lfs);
1162c2c66affSColin Finck     }
1163c2c66affSColin Finck 
1164c2c66affSColin Finck     TRACE("return lpstrFile='%s' !\n", lpofn->lpstrFile);
1165c2c66affSColin Finck     return bRet;
1166c2c66affSColin Finck }
1167c2c66affSColin Finck 
1168c2c66affSColin Finck /***********************************************************************
1169c2c66affSColin Finck  *           GetFileName31W                                 [internal]
1170c2c66affSColin Finck  *
1171c2c66affSColin Finck  * Creates a win31 style dialog box for the user to select a file to open/save
1172c2c66affSColin Finck  */
GetFileName31W(OPENFILENAMEW * lpofn,UINT dlgType)1173c2c66affSColin Finck BOOL GetFileName31W( OPENFILENAMEW *lpofn, UINT dlgType )
1174c2c66affSColin Finck {
1175c2c66affSColin Finck     BOOL bRet = FALSE;
1176c2c66affSColin Finck     PFD31_DATA lfs;
1177c2c66affSColin Finck 
1178c2c66affSColin Finck     if (!lpofn || !FD31_Init()) return FALSE;
1179c2c66affSColin Finck 
1180c2c66affSColin Finck     lfs = FD31_AllocPrivate((LPARAM) lpofn, dlgType, TRUE);
1181c2c66affSColin Finck     if (lfs)
1182c2c66affSColin Finck     {
1183c2c66affSColin Finck         bRet = DialogBoxIndirectParamW( COMDLG32_hInstance, lfs->template, lpofn->hwndOwner,
1184c2c66affSColin Finck                                         FD31_FileOpenDlgProc, (LPARAM)lfs);
1185c2c66affSColin Finck         FD31_DestroyPrivate(lfs);
1186c2c66affSColin Finck     }
1187c2c66affSColin Finck 
1188c2c66affSColin Finck     TRACE("file %s, file offset %d, ext offset %d\n",
1189c2c66affSColin Finck           debugstr_w(lpofn->lpstrFile), lpofn->nFileOffset, lpofn->nFileExtension);
1190c2c66affSColin Finck     return bRet;
1191c2c66affSColin Finck }
1192