Don't depend on user32-internal implementation of accelerator tables.
[wine] / dlls / commdlg / filedlg16.c
1 /*
2  * COMMDLG - File Dialogs
3  *
4  * Copyright 1994 Martin Ayotte
5  * Copyright 1996 Albrecht Kleine
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "wine/winbase16.h"
32 #include "wine/winuser16.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
35 #include "cderr.h"
36 #include "winreg.h"
37 #include "winternl.h"
38 #include "winuser.h"
39 #include "commdlg.h"
40 #include "cderr.h"
41 #include "winreg.h"
42 #include "winternl.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
45
46 #include "cdlg.h"
47 #include "cdlg16.h"
48
49 #define BUFFILE 512
50 #define BUFFILEALLOC 512 * sizeof(WCHAR)
51
52 struct FSPRIVATE
53 {
54     HWND hwnd; /* file dialog window handle */
55     BOOL hook; /* TRUE if the dialog is hooked */
56     UINT lbselchstring; /* registered message id */
57     UINT fileokstring; /* registered message id */
58     LPARAM lParam; /* save original lparam */
59     HANDLE16 hDlgTmpl16; /* handle for resource 16 */
60     HANDLE16 hResource16; /* handle for allocated resource 16 */
61     HANDLE16 hGlobal16; /* 16 bits mem block (resources) */
62     LPCVOID template; /* template for 32 bits resource */
63     BOOL open; /* TRUE if open dialog, FALSE if save dialog */
64     OPENFILENAMEW *ofnW; /* original structure or work struct */
65     OPENFILENAMEA *ofnA; /* original structure if 32bits ansi dialog */
66     OPENFILENAME16 *ofn16; /* original structure if 16 bits dialog */
67 };
68
69 #define LFSPRIVATE struct FSPRIVATE *
70 #define LFS16 1
71 #define LFS32A 2
72 #define LFS32W 3
73 #define OFN_PROP "FILEDLG_OFN"
74
75 static const WCHAR FILE_star[] = {'*','.','*', 0};
76 static const WCHAR FILE_bslash[] = {'\\', 0};
77 static const WCHAR FILE_specc[] = {'%','c',':', 0};
78 static const int fldrHeight = 16;
79 static const int fldrWidth = 20;
80
81 static HICON hFolder = 0;
82 static HICON hFolder2 = 0;
83 static HICON hFloppy = 0;
84 static HICON hHDisk = 0;
85 static HICON hCDRom = 0;
86 static HICON hNet = 0;
87 static char defaultopen[]="Open File";
88 static char defaultsave[]="Save as";
89
90 /***********************************************************************
91  *                              FileDlg_Init                    [internal]
92  */
93 static BOOL FileDlg_Init(void)
94 {
95     static BOOL initialized = 0;
96
97     if (!initialized) {
98         HINSTANCE inst = GetModuleHandleA( "comdlg32.dll" );
99         if (!inst)
100         {
101             ERR( "cannot get comdlg32.dll instance\n" );
102             return FALSE;
103         }
104         hFolder  = LoadImageA( inst, "FOLDER", IMAGE_ICON, 16, 16, LR_SHARED );
105         hFolder2 = LoadImageA( inst, "FOLDER2", IMAGE_ICON, 16, 16, LR_SHARED );
106         hFloppy  = LoadImageA( inst, "FLOPPY", IMAGE_ICON, 16, 16, LR_SHARED );
107         hHDisk   = LoadImageA( inst, "HDISK", IMAGE_ICON, 16, 16, LR_SHARED );
108         hCDRom   = LoadImageA( inst, "CDROM", IMAGE_ICON, 16, 16, LR_SHARED );
109         hNet     = LoadImageA( inst, "NETWORK", IMAGE_ICON, 16, 16, LR_SHARED );
110         if (hFolder == 0 || hFolder2 == 0 || hFloppy == 0 ||
111             hHDisk == 0 || hCDRom == 0 || hNet == 0)
112         {
113             ERR("Error loading icons !\n");
114             return FALSE;
115         }
116         initialized = TRUE;
117     }
118     return TRUE;
119 }
120
121 /***********************************************************************
122  *           Get32BitsTemplate                                  [internal]
123  *
124  * Get a template (or FALSE if failure) when 16 bits dialogs are used
125  * by a 32 bits application
126  *
127  */
128 BOOL Get32BitsTemplate(LFSPRIVATE lfs)
129 {
130     LPOPENFILENAMEW ofnW = lfs->ofnW;
131     HANDLE hDlgTmpl;
132
133     if (ofnW->Flags & OFN_ENABLETEMPLATEHANDLE)
134     {
135         if (!(lfs->template = LockResource( ofnW->hInstance )))
136         {
137             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
138             return FALSE;
139         }
140     }
141     else if (ofnW->Flags & OFN_ENABLETEMPLATE)
142     {
143         HRSRC hResInfo;
144         if (lfs->ofnA)
145             hResInfo = FindResourceA(lfs->ofnA->hInstance,
146                                  lfs->ofnA->lpTemplateName,
147                                  (LPSTR)RT_DIALOG);
148         else
149             hResInfo = FindResourceW(ofnW->hInstance,
150                                  ofnW->lpTemplateName,
151                                  (LPWSTR)RT_DIALOG);
152         if (!hResInfo)
153         {
154             COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
155             return FALSE;
156         }
157         if (!(hDlgTmpl = LoadResource(ofnW->hInstance,
158                                 hResInfo)) ||
159                     !(lfs->template = LockResource(hDlgTmpl)))
160         {
161             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
162             return FALSE;
163         }
164     } else { /* get it from internal Wine resource */
165         HRSRC hResInfo;
166         if (!(hResInfo = FindResourceA(COMDLG32_hInstance,
167              lfs->open? "OPEN_FILE":"SAVE_FILE", (LPSTR)RT_DIALOG)))
168         {
169             COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
170             return FALSE;
171         }
172         if (!(hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo )) ||
173                 !(lfs->template = LockResource( hDlgTmpl )))
174         {
175             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
176             return FALSE;
177         }
178     }
179     return TRUE;
180 }
181
182 /***********************************************************************
183  *           Get16BitsTemplate                                [internal]
184  *
185  * Get a template (FALSE if failure) when 16 bits dialogs are used
186  * by a 16 bits application
187  *
188  */
189 BOOL Get16BitsTemplate(LFSPRIVATE lfs)
190 {
191     LPOPENFILENAME16 ofn16 = lfs->ofn16;
192     LPCVOID template;
193     HGLOBAL16 hGlobal16 = 0;
194
195     if (ofn16->Flags & OFN_ENABLETEMPLATEHANDLE)
196         lfs->hDlgTmpl16 = ofn16->hInstance;
197     else if (ofn16->Flags & OFN_ENABLETEMPLATE)
198     {
199         HANDLE16 hResInfo;
200         if (!(hResInfo = FindResource16(ofn16->hInstance,
201                                         MapSL(ofn16->lpTemplateName),
202                                         (LPSTR)RT_DIALOG)))
203         {
204             COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
205             return FALSE;
206         }
207         if (!(lfs->hDlgTmpl16 = LoadResource16( ofn16->hInstance, hResInfo )))
208         {
209             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
210             return FALSE;
211         }
212         lfs->hResource16 = lfs->hDlgTmpl16;
213     }
214     else
215     { /* get resource from (32 bits) own Wine resource; convert it to 16 */
216         HRSRC hResInfo;
217         HGLOBAL hDlgTmpl32;
218         LPCVOID template32;
219         DWORD size;
220
221         if (!(hResInfo = FindResourceA(COMDLG32_hInstance,
222                lfs->open ? "OPEN_FILE":"SAVE_FILE", (LPSTR)RT_DIALOG)))
223         {
224             COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
225             return FALSE;
226         }
227         if (!(hDlgTmpl32 = LoadResource(COMDLG32_hInstance, hResInfo )) ||
228             !(template32 = LockResource( hDlgTmpl32 )))
229         {
230             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
231             return FALSE;
232         }
233         size = SizeofResource(GetModuleHandleA("COMDLG32"), hResInfo);
234         hGlobal16 = GlobalAlloc16(0, size);
235         if (!hGlobal16)
236         {
237             COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
238             ERR("alloc failure for %ld bytes\n", size);
239             return FALSE;
240         }
241         template = GlobalLock16(hGlobal16);
242         if (!template)
243         {
244             COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE);
245             ERR("global lock failure for %x handle\n", hGlobal16);
246             GlobalFree16(hGlobal16);
247             return FALSE;
248         }
249         ConvertDialog32To16((LPVOID)template32, size, (LPVOID)template);
250         lfs->hDlgTmpl16 = hGlobal16;
251         lfs->hGlobal16 = hGlobal16;
252     }
253     return TRUE;
254 }
255
256 /***********************************************************************
257  *                              FILEDLG_StripEditControl        [internal]
258  * Strip pathnames off the contents of the edit control.
259  */
260 static void FILEDLG_StripEditControl(HWND hwnd)
261 {
262     WCHAR temp[BUFFILE], *cp;
263
264     GetDlgItemTextW( hwnd, edt1, temp, sizeof(temp)/sizeof(WCHAR));
265     cp = strrchrW(temp, '\\');
266     if (cp != NULL) {
267         strcpyW(temp, cp+1);
268     }
269     cp = strrchrW(temp, ':');
270     if (cp != NULL) {
271         strcpyW(temp, cp+1);
272     }
273     /* FIXME: shouldn't we do something with the result here? ;-) */
274 }
275
276 /***********************************************************************
277  *                              FILEDLG_CallWindowProc          [internal]
278  *
279  *      Call the appropriate hook
280  */
281 static BOOL FILEDLG_CallWindowProc(LFSPRIVATE lfs, UINT wMsg, WPARAM wParam,
282                                    LPARAM lParam)
283 {
284     if (lfs->ofnA)
285     {
286         return (BOOL) CallWindowProcA(
287           (WNDPROC)lfs->ofnA->lpfnHook, lfs->hwnd,
288           wMsg, wParam, lParam);
289     }
290
291     if (lfs->ofnW)
292     {
293         return (BOOL) CallWindowProcW(
294           (WNDPROC)lfs->ofnW->lpfnHook, lfs->hwnd,
295           wMsg, wParam, lParam);
296     }
297     return FALSE;
298 }
299
300 /***********************************************************************
301  *                              FILEDLG_ScanDir                 [internal]
302  */
303 static BOOL FILEDLG_ScanDir(HWND hWnd, LPWSTR newPath)
304 {
305     WCHAR               buffer[BUFFILE];
306     HWND                hdlg, hdlgDir;
307     LRESULT             lRet = TRUE;
308     HCURSOR             hCursorWait, oldCursor;
309
310     TRACE("Trying to change to %s\n", debugstr_w(newPath));
311     if  ( !SetCurrentDirectoryW( newPath ))
312         return FALSE;
313     lstrcpynW(buffer, newPath, sizeof(buffer)/sizeof(WCHAR));
314
315     /* get the list of spec files */
316     GetDlgItemTextW(hWnd, edt1, buffer, sizeof(buffer)/sizeof(WCHAR));
317
318     hCursorWait = LoadCursorA(0, (LPSTR)IDC_WAIT);
319     oldCursor = SetCursor(hCursorWait);
320
321     /* list of files */
322     if ((hdlg = GetDlgItem(hWnd, lst1)) != 0) {
323         WCHAR*  scptr; /* ptr on semi-colon */
324         WCHAR*  filter = buffer;
325
326         TRACE("Using filter %s\n", debugstr_w(filter));
327         SendMessageW(hdlg, LB_RESETCONTENT, 0, 0);
328         while (filter) {
329             scptr = strchrW(filter, ';');
330             if (scptr)  *scptr = 0;
331             while (*filter == ' ') filter++;
332             TRACE("Using file spec %s\n", debugstr_w(filter));
333             if (SendMessageW(hdlg, LB_DIR, 0, (LPARAM)filter) == LB_ERR)
334                 return FALSE;
335             if (scptr) *scptr = ';';
336                 filter = (scptr) ? (scptr + 1) : 0;
337          }
338     }
339
340     /* list of directories */
341     strcpyW(buffer, FILE_star);
342
343     if ((hdlgDir = GetDlgItem(hWnd, lst2)) != 0) {
344         lRet = DlgDirListW(hWnd, buffer, lst2, stc1, DDL_EXCLUSIVE | DDL_DIRECTORY);
345     }
346     SetCursor(oldCursor);
347     return lRet;
348 }
349
350 /***********************************************************************
351  *                              FILEDLG_GetFileType             [internal]
352  */
353
354 static LPWSTR FILEDLG_GetFileType(LPWSTR cfptr, LPWSTR fptr, WORD index)
355 {
356   int n, i;
357   i = 0;
358   if (cfptr)
359     for ( ;(n = lstrlenW(cfptr)) != 0; i++)
360       {
361         cfptr += n + 1;
362         if (i == index)
363           return cfptr;
364         cfptr += lstrlenW(cfptr) + 1;
365       }
366   if (fptr)
367     for ( ;(n = lstrlenW(fptr)) != 0; i++)
368       {
369         fptr += n + 1;
370         if (i == index)
371           return fptr;
372         fptr += lstrlenW(fptr) + 1;
373     }
374   return (LPWSTR) FILE_star; /* FIXME */
375 }
376
377 /***********************************************************************
378  *                              FILEDLG_WMDrawItem              [internal]
379  */
380 static LONG FILEDLG_WMDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam,
381        int savedlg, LPDRAWITEMSTRUCT lpdis)
382 {
383     WCHAR *str;
384     HICON hIcon;
385     COLORREF oldText = 0, oldBk = 0;
386
387     if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst1)
388     {
389         if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC))) return FALSE;
390         SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
391                       (LPARAM)str);
392
393         if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
394         {
395             oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
396             oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
397         }
398         if (savedlg)
399             SetTextColor(lpdis->hDC,GetSysColor(COLOR_GRAYTEXT) );
400
401         ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + 1,
402                   lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
403                   &(lpdis->rcItem), str, lstrlenW(str), NULL);
404
405         if (lpdis->itemState & ODS_SELECTED)
406             DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
407
408         if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
409         {
410             SetBkColor( lpdis->hDC, oldBk );
411             SetTextColor( lpdis->hDC, oldText );
412         }
413         HeapFree(GetProcessHeap(), 0, str);
414         return TRUE;
415     }
416
417     if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst2)
418     {
419         if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
420             return FALSE;
421         SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
422                       (LPARAM)str);
423
424         if (lpdis->itemState & ODS_SELECTED)
425         {
426             oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
427             oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
428         }
429         ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
430                   lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
431                   &(lpdis->rcItem), str, lstrlenW(str), NULL);
432
433         if (lpdis->itemState & ODS_SELECTED)
434             DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
435
436         if (lpdis->itemState & ODS_SELECTED)
437         {
438             SetBkColor( lpdis->hDC, oldBk );
439             SetTextColor( lpdis->hDC, oldText );
440         }
441         DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hFolder);
442         HeapFree(GetProcessHeap(), 0, str);
443         return TRUE;
444     }
445     if (lpdis->CtlType == ODT_COMBOBOX && lpdis->CtlID == cmb2)
446     {
447         char root[] = "a:";
448         if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
449             return FALSE;
450         SendMessageW(lpdis->hwndItem, CB_GETLBTEXT, lpdis->itemID,
451                       (LPARAM)str);
452         root[0] += str[2] - 'a';
453         switch(GetDriveTypeA(root))
454         {
455         case DRIVE_REMOVABLE: hIcon = hFloppy; break;
456         case DRIVE_CDROM:     hIcon = hCDRom; break;
457         case DRIVE_REMOTE:    hIcon = hNet; break;
458         case DRIVE_FIXED:
459         default:           hIcon = hHDisk; break;
460         }
461         if (lpdis->itemState & ODS_SELECTED)
462         {
463             oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
464             oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
465         }
466         ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
467                   lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
468                   &(lpdis->rcItem), str, lstrlenW(str), NULL);
469
470         if (lpdis->itemState & ODS_SELECTED)
471         {
472             SetBkColor( lpdis->hDC, oldBk );
473             SetTextColor( lpdis->hDC, oldText );
474         }
475         DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hIcon);
476         HeapFree(GetProcessHeap(), 0, str);
477         return TRUE;
478     }
479     return FALSE;
480 }
481
482 /***********************************************************************
483  *                              FILEDLG_UpdateResult            [internal]
484  *      update the displayed file name (with path)
485  */
486 void FILEDLG_UpdateResult(LFSPRIVATE lfs, WCHAR *tmpstr)
487 {
488     int lenstr2;
489     LPOPENFILENAMEW ofnW = lfs->ofnW;
490     WCHAR tmpstr2[BUFFILE];
491     WCHAR *bs;
492
493     TRACE("%s\n", debugstr_w(tmpstr));
494     if(ofnW->Flags & OFN_NOVALIDATE)
495         tmpstr2[0] = '\0';
496     else
497         GetCurrentDirectoryW(BUFFILE, tmpstr2);
498     lenstr2 = strlenW(tmpstr2);
499     if (lenstr2 > 3)
500         tmpstr2[lenstr2++]='\\';
501     lstrcpynW(tmpstr2+lenstr2, tmpstr, BUFFILE-lenstr2);
502     if (ofnW->lpstrFile)
503         lstrcpynW(ofnW->lpstrFile, tmpstr2, ofnW->nMaxFile);
504     if((bs = strrchrW(tmpstr2, '\\')) != NULL)
505         ofnW->nFileOffset = bs - tmpstr2 +1;
506     else
507         ofnW->nFileOffset = 0;
508     ofnW->nFileExtension = 0;
509     while(tmpstr2[ofnW->nFileExtension] != '.' && tmpstr2[ofnW->nFileExtension] != '\0')
510         ofnW->nFileExtension++;
511     if (tmpstr2[ofnW->nFileExtension] == '\0')
512         ofnW->nFileExtension = 0;
513     else
514         ofnW->nFileExtension++;
515     /* update the real client structures if any */
516     if (lfs->ofn16)
517     { /* we have to convert to short (8.3) path */
518         char tmp[1024]; /* MAX_PATHNAME_LEN */
519         LPOPENFILENAME16 ofn16 = lfs->ofn16;
520         char *dest = MapSL(ofn16->lpstrFile);
521         char *bs16;
522         if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFile, -1,
523                                   tmp, sizeof(tmp), NULL, NULL ))
524             tmp[sizeof(tmp)-1] = 0;
525         GetShortPathNameA(tmp, dest, ofn16->nMaxFile);
526
527         /* the same procedure as every year... */
528         if((bs16 = strrchr(dest, '\\')) != NULL)
529             ofn16->nFileOffset = bs16 - dest +1;
530         else
531             ofn16->nFileOffset = 0;
532         ofn16->nFileExtension = 0;
533         while(dest[ofn16->nFileExtension] != '.' && dest[ofn16->nFileExtension] != '\0')
534             ofn16->nFileExtension++;
535         if (dest[ofn16->nFileExtension] == '\0')
536             ofn16->nFileExtension = 0;
537         else
538             ofn16->nFileExtension++;
539     }
540     if (lfs->ofnA)
541     {
542         if (ofnW->nMaxFile &&
543             !WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFile, -1,
544                                   lfs->ofnA->lpstrFile, ofnW->nMaxFile, NULL, NULL ))
545             lfs->ofnA->lpstrFile[ofnW->nMaxFile-1] = 0;
546         lfs->ofnA->nFileOffset = ofnW->nFileOffset;
547         lfs->ofnA->nFileExtension = ofnW->nFileExtension;
548     }
549 }
550
551 /***********************************************************************
552  *                              FILEDLG_UpdateFileTitle         [internal]
553  *      update the displayed file name (without path)
554  */
555 void FILEDLG_UpdateFileTitle(LFSPRIVATE lfs)
556 {
557   LONG lRet;
558   LPOPENFILENAMEW ofnW = lfs->ofnW;
559   if (ofnW->lpstrFileTitle != NULL)
560   {
561     lRet = SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
562     SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETTEXT, lRet,
563                              (LPARAM)ofnW->lpstrFileTitle );
564     if (lfs->ofn16)
565     {
566         char *dest = MapSL(lfs->ofn16->lpstrFileTitle);
567         if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFileTitle, -1,
568                                   dest, ofnW->nMaxFileTitle, NULL, NULL ))
569             dest[ofnW->nMaxFileTitle-1] = 0;
570     }
571     if (lfs->ofnA)
572     {
573         if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFileTitle, -1,
574                                   lfs->ofnA->lpstrFileTitle, ofnW->nMaxFileTitle, NULL, NULL ))
575             lfs->ofnA->lpstrFileTitle[ofnW->nMaxFileTitle-1] = 0;
576     }
577   }
578 }
579
580 /***********************************************************************
581  *                              FILEDLG_DirListDblClick         [internal]
582  */
583 static LRESULT FILEDLG_DirListDblClick( LFSPRIVATE lfs )
584 {
585   LONG lRet;
586   HWND hWnd = lfs->hwnd;
587   LPWSTR pstr;
588   WCHAR tmpstr[BUFFILE];
589
590   /* get the raw string (with brackets) */
591   lRet = SendDlgItemMessageW(hWnd, lst2, LB_GETCURSEL, 0, 0);
592   if (lRet == LB_ERR) return TRUE;
593   pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
594   SendDlgItemMessageW(hWnd, lst2, LB_GETTEXT, lRet,
595                      (LPARAM)pstr);
596   strcpyW( tmpstr, pstr );
597   HeapFree(GetProcessHeap(), 0, pstr);
598   /* get the selected directory in tmpstr */
599   if (tmpstr[0] == '[')
600     {
601       tmpstr[lstrlenW(tmpstr) - 1] = 0;
602       strcpyW(tmpstr,tmpstr+1);
603     }
604   strcatW(tmpstr, FILE_bslash);
605
606   FILEDLG_ScanDir(hWnd, tmpstr);
607   /* notify the app */
608   if (lfs->hook)
609     {
610       if (FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, lst2,
611               MAKELONG(lRet,CD_LBSELCHANGE)))
612         return TRUE;
613     }
614   return TRUE;
615 }
616
617 /***********************************************************************
618  *                              FILEDLG_FileListSelect         [internal]
619  *    called when a new item is picked in the file list
620  */
621 static LRESULT FILEDLG_FileListSelect( LFSPRIVATE lfs )
622 {
623     LONG lRet;
624     HWND hWnd = lfs->hwnd;
625     LPWSTR pstr;
626
627     lRet = SendDlgItemMessageW(hWnd, lst1, LB_GETCURSEL16, 0, 0);
628     if (lRet == LB_ERR)
629         return TRUE;
630
631     /* set the edit control to the choosen file */
632     if ((pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
633     {
634         SendDlgItemMessageW(hWnd, lst1, LB_GETTEXT, lRet,
635                        (LPARAM)pstr);
636         SetDlgItemTextW( hWnd, edt1, pstr );
637         HeapFree(GetProcessHeap(), 0, pstr);
638     }
639     if (lfs->hook)
640     {
641         FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, lst1,
642                            MAKELONG(lRet,CD_LBSELCHANGE));
643     }
644     /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD,
645            CD_LBSELNOITEMS */
646     return TRUE;
647 }
648
649 /***********************************************************************
650  *                              FILEDLG_TestPath      [internal]
651  *      before accepting the file name, test if it includes wild cards
652  *      tries to scan the directory and returns TRUE if no error.
653  */
654 static LRESULT FILEDLG_TestPath( LFSPRIVATE lfs, LPWSTR path )
655 {
656     HWND hWnd = lfs->hwnd;
657     LPWSTR pBeginFileName, pstr2;
658     WCHAR tmpstr2[BUFFILE];
659
660     pBeginFileName = strrchrW(path, '\\');
661     if (pBeginFileName == NULL)
662         pBeginFileName = strrchrW(path, ':');
663
664     if (strchrW(path,'*') != NULL || strchrW(path,'?') != NULL)
665     {
666         /* edit control contains wildcards */
667         if (pBeginFileName != NULL)
668         {
669             lstrcpynW(tmpstr2, pBeginFileName + 1, BUFFILE);
670             *(pBeginFileName + 1) = 0;
671         }
672         else
673         {
674             strcpyW(tmpstr2, path);
675             if(!(lfs->ofnW->Flags & OFN_NOVALIDATE))
676                 *path = 0;
677         }
678
679         TRACE("path=%s, tmpstr2=%s\n", debugstr_w(path), debugstr_w(tmpstr2));
680         SetDlgItemTextW( hWnd, edt1, tmpstr2 );
681         FILEDLG_ScanDir(hWnd, path);
682         return (lfs->ofnW->Flags & OFN_NOVALIDATE) ? TRUE : FALSE;
683     }
684
685     /* no wildcards, we might have a directory or a filename */
686     /* try appending a wildcard and reading the directory */
687
688     pstr2 = path + lstrlenW(path);
689     if (pBeginFileName == NULL || *(pBeginFileName + 1) != 0)
690         strcatW(path, FILE_bslash);
691
692     /* if ScanDir succeeds, we have changed the directory */
693     if (FILEDLG_ScanDir(hWnd, path))
694         return FALSE; /* and path is not a valid file name */
695
696     /* if not, this must be a filename */
697
698     *pstr2 = 0; /* remove the wildcard added before */
699
700     if (pBeginFileName != NULL)
701     {
702         /* strip off the pathname */
703         *pBeginFileName = 0;
704         SetDlgItemTextW( hWnd, edt1, pBeginFileName + 1 );
705
706         lstrcpynW(tmpstr2, pBeginFileName + 1, sizeof(tmpstr2)/sizeof(WCHAR) );
707         /* Should we MessageBox() if this fails? */
708         if (!FILEDLG_ScanDir(hWnd, path))
709         {
710             return FALSE;
711         }
712         strcpyW(path, tmpstr2);
713     }
714     else
715         SetDlgItemTextW( hWnd, edt1, path );
716     return TRUE;
717 }
718
719 /***********************************************************************
720  *                              FILEDLG_Validate               [internal]
721  *   called on: click Ok button, Enter in edit, DoubleClick in file list
722  */
723 static LRESULT FILEDLG_Validate( LFSPRIVATE lfs, LPWSTR path, UINT control, INT itemIndex,
724                                  BOOL internalUse )
725 {
726     LONG lRet;
727     HWND hWnd = lfs->hwnd;
728     OPENFILENAMEW ofnsav;
729     LPOPENFILENAMEW ofnW = lfs->ofnW;
730     WCHAR filename[BUFFILE];
731
732     ofnsav = *ofnW; /* for later restoring */
733
734     /* get current file name */
735     if (path)
736         lstrcpynW(filename, path, sizeof(filename)/sizeof(WCHAR));
737     else
738         GetDlgItemTextW( hWnd, edt1, filename, sizeof(filename)/sizeof(WCHAR));
739
740     TRACE("got filename = %s\n", debugstr_w(filename));
741     /* if we did not click in file list to get there */
742     if (control != lst1)
743     {
744         if (!FILEDLG_TestPath( lfs, filename) )
745            return FALSE;
746     }
747     FILEDLG_UpdateResult(lfs, filename);
748
749     if (internalUse)
750     { /* called internally after a change in a combo */
751         if (lfs->hook)
752         {
753              FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, control,
754                              MAKELONG(itemIndex,CD_LBSELCHANGE));
755         }
756         return TRUE;
757     }
758
759     FILEDLG_UpdateFileTitle(lfs);
760     if (lfs->hook)
761     {
762         lRet = (BOOL)FILEDLG_CallWindowProc(lfs, lfs->fileokstring,
763                   0, lfs->lParam );
764         if (lRet)
765         {
766             *ofnW = ofnsav; /* restore old state */
767             return FALSE;
768         }
769     }
770     if ((ofnW->Flags & OFN_ALLOWMULTISELECT) && (ofnW->Flags & OFN_EXPLORER))
771     {
772         if (ofnW->lpstrFile)
773         {
774             LPWSTR str = (LPWSTR)ofnW->lpstrFile;
775             LPWSTR ptr = strrchrW(str, '\\');
776             str[lstrlenW(str) + 1] = '\0';
777             *ptr = 0;
778         }
779     }
780     return TRUE;
781 }
782
783 /***********************************************************************
784  *                              FILEDLG_DiskChange             [internal]
785  *    called when a new item is picked in the disk selection combo
786  */
787 static LRESULT FILEDLG_DiskChange( LFSPRIVATE lfs )
788 {
789     LONG lRet;
790     HWND hWnd = lfs->hwnd;
791     LPWSTR pstr;
792     WCHAR diskname[BUFFILE];
793
794     FILEDLG_StripEditControl(hWnd);
795     lRet = SendDlgItemMessageW(hWnd, cmb2, CB_GETCURSEL, 0, 0L);
796     if (lRet == LB_ERR)
797         return 0;
798     pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
799     SendDlgItemMessageW(hWnd, cmb2, CB_GETLBTEXT, lRet,
800                          (LPARAM)pstr);
801     wsprintfW(diskname, FILE_specc, pstr[2]);
802     HeapFree(GetProcessHeap(), 0, pstr);
803
804     return FILEDLG_Validate( lfs, diskname, cmb2, lRet, TRUE );
805 }
806
807 /***********************************************************************
808  *                              FILEDLG_FileTypeChange         [internal]
809  *    called when a new item is picked in the file type combo
810  */
811 static LRESULT FILEDLG_FileTypeChange( LFSPRIVATE lfs )
812 {
813     LONG lRet;
814     WCHAR diskname[BUFFILE];
815     LPWSTR pstr;
816
817     diskname[0] = 0;
818
819     lRet = SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETCURSEL, 0, 0);
820     if (lRet == LB_ERR)
821         return TRUE;
822     pstr = (LPWSTR)SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETITEMDATA, lRet, 0);
823     TRACE("Selected filter : %s\n", debugstr_w(pstr));
824     SetDlgItemTextW( lfs->hwnd, edt1, pstr );
825
826     return FILEDLG_Validate( lfs, NULL, cmb1, lRet, TRUE );
827 }
828
829 /***********************************************************************
830  *                              FILEDLG_WMCommand               [internal]
831  */
832 static LRESULT FILEDLG_WMCommand(HWND hWnd, LPARAM lParam, UINT notification,
833        UINT control, LFSPRIVATE lfs )
834 {
835     switch (control)
836     {
837         case lst1: /* file list */
838         FILEDLG_StripEditControl(hWnd);
839         if (notification == LBN_DBLCLK)
840         {
841             if (FILEDLG_Validate( lfs, NULL, control, 0, FALSE ))
842                 EndDialog(hWnd, TRUE);
843             return TRUE;
844         }
845         else if (notification == LBN_SELCHANGE)
846             return FILEDLG_FileListSelect( lfs );
847         break;
848
849         case lst2: /* directory list */
850         FILEDLG_StripEditControl(hWnd);
851         if (notification == LBN_DBLCLK)
852             return FILEDLG_DirListDblClick( lfs );
853         break;
854
855         case cmb1: /* file type drop list */
856         if (notification == CBN_SELCHANGE)
857             return FILEDLG_FileTypeChange( lfs );
858         break;
859
860         case chx1:
861         break;
862
863         case pshHelp:
864         break;
865
866         case cmb2: /* disk dropdown combo */
867         if (notification == CBN_SELCHANGE)
868             return FILEDLG_DiskChange( lfs );
869         break;
870
871         case IDOK:
872         TRACE("OK pressed\n");
873         if (FILEDLG_Validate( lfs, NULL, control, 0, FALSE ))
874             EndDialog(hWnd, TRUE);
875         return TRUE;
876
877         case IDCANCEL:
878         EndDialog(hWnd, FALSE);
879         return TRUE;
880
881         case IDABORT: /* can be sent by the hook procedure */
882         EndDialog(hWnd, TRUE);
883         return TRUE;
884     }
885     return FALSE;
886 }
887
888 /***********************************************************************
889  *                              FILEDLG_MapDrawItemStruct       [internal]
890  *      map a 16 bits drawitem struct to 32
891  */
892 static void FILEDLG_MapDrawItemStruct(LPDRAWITEMSTRUCT16 lpdis16, LPDRAWITEMSTRUCT lpdis)
893 {
894     lpdis->CtlType = lpdis16->CtlType;
895     lpdis->CtlID = lpdis16->CtlID;
896     lpdis->itemID = lpdis16->itemID;
897     lpdis->itemAction = lpdis16->itemAction;
898     lpdis->itemState = lpdis16->itemState;
899     lpdis->hwndItem = HWND_32(lpdis16->hwndItem);
900     lpdis->hDC = HDC_32(lpdis16->hDC);
901     lpdis->rcItem.right = lpdis16->rcItem.right;
902     lpdis->rcItem.left = lpdis16->rcItem.left;
903     lpdis->rcItem.top = lpdis16->rcItem.top;
904     lpdis->rcItem.bottom = lpdis16->rcItem.bottom;
905     lpdis->itemData = lpdis16->itemData;
906 }
907
908 /************************************************************************
909  *                              FILEDLG_MapStringPairsToW       [internal]
910  *      map string pairs to Unicode
911  */
912 static LPWSTR FILEDLG_MapStringPairsToW(LPCSTR strA, UINT size)
913 {
914     LPCSTR s;
915     LPWSTR x;
916     int n, len;
917
918     s = strA;
919     while (*s)
920         s = s+strlen(s)+1;
921     s++;
922     n = s + 1 - strA; /* Don't forget the other \0 */
923     if (n < size) n = size;
924
925     len = MultiByteToWideChar( CP_ACP, 0, strA, n, NULL, 0 );
926     x = HeapAlloc(GetProcessHeap(),0, len * sizeof(WCHAR));
927     MultiByteToWideChar( CP_ACP, 0, strA, n, x, len );
928     return x;
929 }
930
931
932 /************************************************************************
933  *                              FILEDLG_DupToW                  [internal]
934  *      duplicates an Ansi string to unicode, with a buffer size
935  */
936 LPWSTR FILEDLG_DupToW(LPCSTR str, DWORD size)
937 {
938     LPWSTR strW = NULL;
939     if (str && (size > 0))
940     {
941         strW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
942         if (strW) MultiByteToWideChar( CP_ACP, 0, str, -1, strW, size );
943     }
944     return strW;
945 }
946
947
948 /************************************************************************
949  *                              FILEDLG_MapOfnStructA          [internal]
950  *      map a 32 bits Ansi structure to an Unicode one
951  */
952 void FILEDLG_MapOfnStructA(LPOPENFILENAMEA ofnA, LPOPENFILENAMEW ofnW, BOOL open)
953 {
954     LPCSTR str;
955     UNICODE_STRING usBuffer;
956
957     ofnW->lStructSize = sizeof(OPENFILENAMEW);
958     ofnW->hwndOwner = ofnA->hwndOwner;
959     ofnW->hInstance = ofnA->hInstance;
960     if (ofnA->lpstrFilter)
961         ofnW->lpstrFilter = FILEDLG_MapStringPairsToW(ofnA->lpstrFilter, 0);
962
963     if ((ofnA->lpstrCustomFilter) && (*(ofnA->lpstrCustomFilter)))
964         ofnW->lpstrCustomFilter = FILEDLG_MapStringPairsToW(ofnA->lpstrCustomFilter, ofnA->nMaxCustFilter);
965     ofnW->nMaxCustFilter = ofnA->nMaxCustFilter;
966     ofnW->nFilterIndex = ofnA->nFilterIndex;
967     ofnW->nMaxFile = ofnA->nMaxFile;
968     ofnW->lpstrFile = FILEDLG_DupToW(ofnA->lpstrFile, ofnW->nMaxFile);
969     ofnW->nMaxFileTitle = ofnA->nMaxFileTitle;
970     ofnW->lpstrFileTitle = FILEDLG_DupToW(ofnA->lpstrFileTitle, ofnW->nMaxFileTitle);
971     if (ofnA->lpstrInitialDir)
972     {
973         RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpstrInitialDir);
974         ofnW->lpstrInitialDir = usBuffer.Buffer;
975     }
976     if (ofnA->lpstrTitle)
977         str = ofnA->lpstrTitle;
978     else
979         /* Allocates default title (FIXME : get it from resource) */
980         str = open ? defaultopen:defaultsave;
981     RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpstrTitle);
982     ofnW->lpstrTitle = usBuffer.Buffer;
983     ofnW->Flags = ofnA->Flags;
984     ofnW->nFileOffset = ofnA->nFileOffset;
985     ofnW->nFileExtension = ofnA->nFileExtension;
986     ofnW->lpstrDefExt = FILEDLG_DupToW(ofnA->lpstrDefExt, 3);
987     if ((ofnA->Flags & OFN_ENABLETEMPLATE) && (ofnA->lpTemplateName))
988     {
989         if (HIWORD(ofnA->lpTemplateName))
990         {
991             RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpTemplateName);
992             ofnW->lpTemplateName = usBuffer.Buffer;
993         }
994         else /* numbered resource */
995             ofnW->lpTemplateName = (LPWSTR) ofnA->lpTemplateName;
996     }
997 }
998
999 /************************************************************************
1000  *                              FILEDLG_MapOfnStruct16          [internal]
1001  *      map a 16 bits structure to an Unicode one
1002  */
1003 void FILEDLG_MapOfnStruct16(LPOPENFILENAME16 ofn16, LPOPENFILENAMEW ofnW, BOOL open)
1004 {
1005     OPENFILENAMEA ofnA;
1006     /* first convert to linear pointers */
1007     memset(&ofnA, 0, sizeof(OPENFILENAMEA));
1008     ofnA.lStructSize = sizeof(OPENFILENAMEA);
1009     ofnA.hwndOwner = HWND_32(ofn16->hwndOwner);
1010     ofnA.hInstance = HINSTANCE_32(ofn16->hInstance);
1011     if (ofn16->lpstrFilter)
1012         ofnA.lpstrFilter = MapSL(ofn16->lpstrFilter);
1013     if (ofn16->lpstrCustomFilter)
1014         ofnA.lpstrCustomFilter = MapSL(ofn16->lpstrCustomFilter);
1015     ofnA.nMaxCustFilter = ofn16->nMaxCustFilter;
1016     ofnA.nFilterIndex = ofn16->nFilterIndex;
1017     ofnA.lpstrFile = MapSL(ofn16->lpstrFile);
1018     ofnA.nMaxFile = ofn16->nMaxFile;
1019     ofnA.lpstrFileTitle = MapSL(ofn16->lpstrFileTitle);
1020     ofnA.nMaxFileTitle = ofn16->nMaxFileTitle;
1021     ofnA.lpstrInitialDir = MapSL(ofn16->lpstrInitialDir);
1022     ofnA.lpstrTitle = MapSL(ofn16->lpstrTitle);
1023     ofnA.Flags = ofn16->Flags;
1024     ofnA.nFileOffset = ofn16->nFileOffset;
1025     ofnA.nFileExtension = ofn16->nFileExtension;
1026     ofnA.lpstrDefExt = MapSL(ofn16->lpstrDefExt);
1027     if (HIWORD(ofn16->lpTemplateName))
1028         ofnA.lpTemplateName = MapSL(ofn16->lpTemplateName);
1029     else
1030         ofnA.lpTemplateName = (LPSTR) ofn16->lpTemplateName; /* ressource number */
1031     /* now calls the 32 bits Ansi to Unicode version to complete the job */
1032     FILEDLG_MapOfnStructA(&ofnA, ofnW, open);
1033 }
1034
1035 /************************************************************************
1036  *                              FILEDLG_DestroyPrivate            [internal]
1037  *      destroys the private object
1038  */
1039 static void FILEDLG_DestroyPrivate(LFSPRIVATE lfs)
1040 {
1041     HWND hwnd;
1042     if (!lfs) return;
1043     hwnd = lfs->hwnd;
1044     /* free resources for a 16 bits dialog */
1045     if (lfs->hResource16) FreeResource16(lfs->hResource16);
1046     if (lfs->hGlobal16)
1047     {
1048         GlobalUnlock16(lfs->hGlobal16);
1049         GlobalFree16(lfs->hGlobal16);
1050     }
1051     /* if ofnW has been allocated, have to free everything in it */
1052     if (lfs->ofn16 || lfs->ofnA)
1053     {
1054        LPOPENFILENAMEW ofnW = lfs->ofnW;
1055        if (ofnW->lpstrFilter) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrFilter);
1056        if (ofnW->lpstrCustomFilter) HeapFree(GetProcessHeap(), 0, ofnW->lpstrCustomFilter);
1057        if (ofnW->lpstrFile) HeapFree(GetProcessHeap(), 0, ofnW->lpstrFile);
1058        if (ofnW->lpstrFileTitle) HeapFree(GetProcessHeap(), 0, ofnW->lpstrFileTitle);
1059        if (ofnW->lpstrInitialDir) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrInitialDir);
1060        if (ofnW->lpstrTitle) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrTitle);
1061        if ((ofnW->lpTemplateName) && (HIWORD(ofnW->lpTemplateName)))
1062            HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpTemplateName);
1063        HeapFree(GetProcessHeap(), 0, ofnW);
1064     }
1065     TRACE("destroying private allocation %p\n", lfs);
1066     HeapFree(GetProcessHeap(), 0, lfs);
1067     RemovePropA(hwnd, OFN_PROP);
1068 }
1069
1070 /************************************************************************
1071  *                              FILEDLG_AllocPrivate            [internal]
1072  *      allocate a private object to hold 32 bits Unicode
1073  *      structure that will be used throughtout the calls, while
1074  *      keeping available the original structures and a few variables
1075  *      On entry : type = dialog procedure type (16,32A,32W)
1076  *                 dlgType = dialog type (open or save)
1077  */
1078 static LFSPRIVATE FILEDLG_AllocPrivate(LPARAM lParam, int type, UINT dlgType)
1079 {
1080     LFSPRIVATE lfs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct FSPRIVATE));
1081     LFSPRIVATE ret;
1082     TRACE("alloc private buf %p\n", lfs);
1083     if (!lfs) return NULL;
1084     lfs->hook = FALSE;
1085     lfs->lParam = lParam;
1086     if (dlgType == OPEN_DIALOG)
1087         lfs->open = TRUE;
1088     else
1089         lfs->open = FALSE;
1090     lfs->lbselchstring = RegisterWindowMessageA(LBSELCHSTRINGA);
1091     lfs->fileokstring = RegisterWindowMessageA(FILEOKSTRINGA);
1092     switch(type)
1093     {
1094         case LFS16:
1095         lfs->ofn16 = MapSL(lParam);
1096         if (lfs->ofn16->Flags & OFN_ENABLEHOOK)
1097             if (lfs->ofn16->lpfnHook)
1098                 lfs->hook = TRUE;
1099
1100         break;
1101
1102         case LFS32A:
1103         lfs->ofnA = (LPOPENFILENAMEA) lParam;
1104         if (lfs->ofnA->Flags & OFN_ENABLEHOOK)
1105             if (lfs->ofnA->lpfnHook)
1106                 lfs->hook = TRUE;
1107         break;
1108
1109         case LFS32W:
1110         lfs->ofnW = (LPOPENFILENAMEW) lParam;
1111         if (lfs->ofnW->Flags & OFN_ENABLEHOOK)
1112             if (lfs->ofnW->lpfnHook)
1113                 lfs->hook = TRUE;
1114         break;
1115     }
1116     ret = lfs;
1117     if (!lfs->ofnW)
1118     { /* this structure is needed internally, so create it */
1119         lfs->ofnW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(OPENFILENAMEW));
1120         if (lfs->ofnW)
1121         {
1122             if (lfs->ofn16)
1123                 FILEDLG_MapOfnStruct16(lfs->ofn16, lfs->ofnW, lfs->open);
1124             if (lfs->ofnA)
1125                 FILEDLG_MapOfnStructA(lfs->ofnA, lfs->ofnW, lfs->open);
1126         }
1127         else
1128             ret = NULL;
1129     }
1130     if (lfs->ofn16)
1131     {
1132         if (!Get16BitsTemplate(lfs)) ret = NULL;
1133     }
1134     else
1135         if (!Get32BitsTemplate(lfs)) ret = NULL;
1136     if (!ret) FILEDLG_DestroyPrivate(lfs);
1137     return ret;
1138 }
1139
1140 /***********************************************************************
1141  *                              FILEDLG_CallWindowProc16          [internal]
1142  *
1143  *      Call the appropriate hook
1144  */
1145 static BOOL FILEDLG_CallWindowProc16(LFSPRIVATE lfs, UINT wMsg, WPARAM wParam,
1146                                    LPARAM lParam)
1147 {
1148     if (lfs->ofn16)
1149     {
1150         return (BOOL16) CallWindowProc16(
1151           (WNDPROC16)lfs->ofn16->lpfnHook, HWND_16(lfs->hwnd),
1152           (UINT16)wMsg, (WPARAM16)wParam, lParam);
1153     }
1154     return FALSE;
1155 }
1156
1157 /***********************************************************************
1158  *                              FILEDLG_WMInitDialog16            [internal]
1159  *      The is a duplicate of the 32bit FILEDLG_WMInitDialog function 
1160  *      The only differnce is that it calls FILEDLG_CallWindowProc16 
1161  *      for a 16 bit Window Proc.
1162  */
1163
1164 static LONG FILEDLG_WMInitDialog16(HWND hWnd, WPARAM wParam, LPARAM lParam)
1165 {
1166   int i, n;
1167   WCHAR tmpstr[BUFFILE];
1168   LPWSTR pstr, old_pstr;
1169   LPOPENFILENAMEW ofn;
1170   LFSPRIVATE lfs = (LFSPRIVATE) lParam;
1171
1172   if (!lfs) return FALSE;
1173   SetPropA(hWnd, OFN_PROP, (HANDLE)lfs);
1174   lfs->hwnd = hWnd;
1175   ofn = lfs->ofnW;
1176
1177   TRACE("flags=%lx initialdir=%s\n", ofn->Flags, debugstr_w(ofn->lpstrInitialDir));
1178
1179   SetWindowTextW( hWnd, ofn->lpstrTitle );
1180   /* read custom filter information */
1181   if (ofn->lpstrCustomFilter)
1182     {
1183       pstr = ofn->lpstrCustomFilter;
1184       n = 0;
1185       TRACE("lpstrCustomFilter = %p\n", pstr);
1186       while(*pstr)
1187         {
1188           old_pstr = pstr;
1189           i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
1190                                    (LPARAM)(ofn->lpstrCustomFilter) + n );
1191           n += lstrlenW(pstr) + 1;
1192           pstr += lstrlenW(pstr) + 1;
1193           TRACE("add str=%s associated to %s\n",
1194                 debugstr_w(old_pstr), debugstr_w(pstr));
1195           SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
1196           n += lstrlenW(pstr) + 1;
1197           pstr += lstrlenW(pstr) + 1;
1198         }
1199     }
1200   /* read filter information */
1201   if (ofn->lpstrFilter) {
1202         pstr = (LPWSTR) ofn->lpstrFilter;
1203         n = 0;
1204         while(*pstr) {
1205           old_pstr = pstr;
1206           i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
1207                                        (LPARAM)(ofn->lpstrFilter + n) );
1208           n += lstrlenW(pstr) + 1;
1209           pstr += lstrlenW(pstr) + 1;
1210           TRACE("add str=%s associated to %s\n",
1211                 debugstr_w(old_pstr), debugstr_w(pstr));
1212           SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
1213           n += lstrlenW(pstr) + 1;
1214           pstr += lstrlenW(pstr) + 1;
1215         }
1216   }
1217   /* set default filter */
1218   if (ofn->nFilterIndex == 0 && ofn->lpstrCustomFilter == NULL)
1219         ofn->nFilterIndex = 1;
1220   SendDlgItemMessageW(hWnd, cmb1, CB_SETCURSEL, ofn->nFilterIndex - 1, 0);
1221   lstrcpynW(tmpstr, FILEDLG_GetFileType(ofn->lpstrCustomFilter,
1222              (LPWSTR)ofn->lpstrFilter, ofn->nFilterIndex - 1),BUFFILE);
1223   TRACE("nFilterIndex = %ld, SetText of edt1 to %s\n",
1224                         ofn->nFilterIndex, debugstr_w(tmpstr));
1225   SetDlgItemTextW( hWnd, edt1, tmpstr );
1226   /* get drive list */
1227   *tmpstr = 0;
1228   DlgDirListComboBoxW(hWnd, tmpstr, cmb2, 0, DDL_DRIVES | DDL_EXCLUSIVE);
1229   /* read initial directory */
1230   /* FIXME: Note that this is now very version-specific (See MSDN description of
1231    * the OPENFILENAME structure).  For example under 2000/XP any path in the
1232    * lpstrFile overrides the lpstrInitialDir, but not under 95/98/ME
1233    */
1234   if (ofn->lpstrInitialDir != NULL)
1235     {
1236       int len;
1237       lstrcpynW(tmpstr, ofn->lpstrInitialDir, 511);
1238       len = lstrlenW(tmpstr);
1239       if (len > 0 && tmpstr[len-1] != '\\'  && tmpstr[len-1] != ':') {
1240         tmpstr[len]='\\';
1241         tmpstr[len+1]='\0';
1242       }
1243     }
1244   else
1245     *tmpstr = 0;
1246   if (!FILEDLG_ScanDir(hWnd, tmpstr)) {
1247     *tmpstr = 0;
1248     if (!FILEDLG_ScanDir(hWnd, tmpstr))
1249       WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr));
1250   }
1251   /* select current drive in combo 2, omit missing drives */
1252   {
1253       char dir[MAX_PATH];
1254       char str[4] = "a:\\";
1255       GetCurrentDirectoryA( sizeof(dir), dir );
1256       for(i = 0, n = -1; i < 26; i++)
1257       {
1258           str[0] = 'a' + i;
1259           if (GetDriveTypeA(str) > DRIVE_NO_ROOT_DIR) n++;
1260           if (toupper(str[0]) == toupper(dir[0])) break;
1261       }
1262   }
1263   SendDlgItemMessageW(hWnd, cmb2, CB_SETCURSEL, n, 0);
1264   if (!(ofn->Flags & OFN_SHOWHELP))
1265     ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
1266   if (ofn->Flags & OFN_HIDEREADONLY)
1267     ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
1268   if (lfs->hook)
1269       return (BOOL) FILEDLG_CallWindowProc16(lfs, WM_INITDIALOG, wParam, lfs->lParam);
1270   return TRUE;
1271 }
1272
1273 /***********************************************************************
1274  *                              FILEDLG_WMMeasureItem16         [internal]
1275  */
1276 static LONG FILEDLG_WMMeasureItem16(HWND16 hWnd, WPARAM16 wParam, LPARAM lParam)
1277 {
1278     LPMEASUREITEMSTRUCT16 lpmeasure;
1279
1280     lpmeasure = MapSL(lParam);
1281     lpmeasure->itemHeight = fldrHeight;
1282     return TRUE;
1283 }
1284
1285 /* ------------------ Dialog procedures ---------------------- */
1286
1287 /***********************************************************************
1288  *           FileOpenDlgProc   (COMMDLG.6)
1289  */
1290 BOOL16 CALLBACK FileOpenDlgProc16(HWND16 hWnd16, UINT16 wMsg, WPARAM16 wParam,
1291                                LPARAM lParam)
1292 {
1293     HWND hWnd = HWND_32(hWnd16);
1294     LFSPRIVATE lfs = (LFSPRIVATE)GetPropA(hWnd,OFN_PROP);
1295     DRAWITEMSTRUCT dis;
1296
1297     TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
1298     if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
1299         {
1300             LRESULT lRet = (BOOL16)FILEDLG_CallWindowProc16(lfs, wMsg, wParam, lParam);
1301             if (lRet)
1302                 return lRet;         /* else continue message processing */
1303         }
1304     switch (wMsg)
1305     {
1306     case WM_INITDIALOG:
1307         return FILEDLG_WMInitDialog16(hWnd, wParam, lParam);
1308
1309     case WM_MEASUREITEM:
1310         return FILEDLG_WMMeasureItem16(hWnd16, wParam, lParam);
1311
1312     case WM_DRAWITEM:
1313         FILEDLG_MapDrawItemStruct(MapSL(lParam), &dis);
1314         return FILEDLG_WMDrawItem(hWnd, wParam, lParam, FALSE, &dis);
1315
1316     case WM_COMMAND:
1317         return FILEDLG_WMCommand(hWnd, lParam, HIWORD(lParam),wParam, lfs);
1318 #if 0
1319     case WM_CTLCOLOR:
1320          SetBkColor((HDC16)wParam, 0x00C0C0C0);
1321          switch (HIWORD(lParam))
1322          {
1323          case CTLCOLOR_BTN:
1324              SetTextColor((HDC16)wParam, 0x00000000);
1325              return hGRAYBrush;
1326         case CTLCOLOR_STATIC:
1327              SetTextColor((HDC16)wParam, 0x00000000);
1328              return hGRAYBrush;
1329         }
1330       break;
1331 #endif
1332     }
1333     return FALSE;
1334 }
1335
1336 /***********************************************************************
1337  *           FileSaveDlgProc   (COMMDLG.7)
1338  */
1339 BOOL16 CALLBACK FileSaveDlgProc16(HWND16 hWnd16, UINT16 wMsg, WPARAM16 wParam,
1340                                LPARAM lParam)
1341 {
1342  HWND hWnd = HWND_32(hWnd16);
1343  LFSPRIVATE lfs = (LFSPRIVATE)GetPropA(hWnd,OFN_PROP);
1344  DRAWITEMSTRUCT dis;
1345
1346  TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
1347  if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
1348   {
1349    LRESULT  lRet;
1350    lRet = (BOOL16)FILEDLG_CallWindowProc16(lfs, wMsg, wParam, lParam);
1351    if (lRet)
1352     return lRet;         /* else continue message processing */
1353   }
1354   switch (wMsg) {
1355    case WM_INITDIALOG:
1356       return FILEDLG_WMInitDialog16(hWnd, wParam, lParam);
1357
1358    case WM_MEASUREITEM:
1359       return FILEDLG_WMMeasureItem16(hWnd16, wParam, lParam);
1360
1361    case WM_DRAWITEM:
1362       FILEDLG_MapDrawItemStruct(MapSL(lParam), &dis);
1363       return FILEDLG_WMDrawItem(hWnd, wParam, lParam, TRUE, &dis);
1364
1365    case WM_COMMAND:
1366       return FILEDLG_WMCommand(hWnd, lParam, HIWORD(lParam), wParam, lfs);
1367   }
1368
1369   /*
1370   case WM_CTLCOLOR:
1371    SetBkColor((HDC16)wParam, 0x00C0C0C0);
1372    switch (HIWORD(lParam))
1373    {
1374     case CTLCOLOR_BTN:
1375      SetTextColor((HDC16)wParam, 0x00000000);
1376      return hGRAYBrush;
1377     case CTLCOLOR_STATIC:
1378      SetTextColor((HDC16)wParam, 0x00000000);
1379      return hGRAYBrush;
1380    }
1381    return FALSE;
1382
1383    */
1384   return FALSE;
1385 }
1386
1387 /* ------------------ APIs ---------------------- */
1388
1389 /***********************************************************************
1390  *           GetOpenFileName   (COMMDLG.1)
1391  *
1392  * Creates a dialog box for the user to select a file to open.
1393  *
1394  * RETURNS
1395  *    TRUE on success: user selected a valid file
1396  *    FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1397  *
1398  * BUGS
1399  *    unknown, there are some FIXME's left.
1400  */
1401 BOOL16 WINAPI GetOpenFileName16(
1402                                 SEGPTR ofn /* [in/out] address of structure with data*/
1403                                 )
1404 {
1405     HINSTANCE16 hInst;
1406     BOOL bRet = FALSE;
1407     LPOPENFILENAME16 lpofn = MapSL(ofn);
1408     LFSPRIVATE lfs;
1409     FARPROC16 ptr;
1410
1411     if (!lpofn || !FileDlg_Init()) return FALSE;
1412
1413     lfs = FILEDLG_AllocPrivate((LPARAM) ofn, LFS16, OPEN_DIALOG);
1414     if (lfs)
1415     {
1416         hInst = GetWindowWord( HWND_32(lpofn->hwndOwner), GWL_HINSTANCE );
1417         ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (LPCSTR) 6);
1418         bRet = DialogBoxIndirectParam16( hInst, lfs->hDlgTmpl16, lpofn->hwndOwner,
1419                                          (DLGPROC16) ptr, (LPARAM) lfs);
1420         FILEDLG_DestroyPrivate(lfs);
1421     }
1422
1423     TRACE("return lpstrFile='%s' !\n", (char *)MapSL(lpofn->lpstrFile));
1424     return bRet;
1425 }
1426
1427 /***********************************************************************
1428  *           GetSaveFileName   (COMMDLG.2)
1429  *
1430  * Creates a dialog box for the user to select a file to save.
1431  *
1432  * RETURNS
1433  *    TRUE on success: user enters a valid file
1434  *    FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1435  *
1436  * BUGS
1437  *    unknown. There are some FIXME's left.
1438  */
1439 BOOL16 WINAPI GetSaveFileName16(
1440                                 SEGPTR ofn /* [in/out] addess of structure with data*/
1441                                 )
1442 {
1443     HINSTANCE16 hInst;
1444     BOOL bRet = FALSE;
1445     LPOPENFILENAME16 lpofn = MapSL(ofn);
1446     LFSPRIVATE lfs;
1447     FARPROC16 ptr;
1448
1449     if (!lpofn || !FileDlg_Init()) return FALSE;
1450
1451     lfs = FILEDLG_AllocPrivate((LPARAM) ofn, LFS16, SAVE_DIALOG);
1452     if (lfs)
1453     {
1454         hInst = GetWindowWord( HWND_32(lpofn->hwndOwner), GWL_HINSTANCE );
1455         ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (LPCSTR) 7);
1456         bRet = DialogBoxIndirectParam16( hInst, lfs->hDlgTmpl16, lpofn->hwndOwner,
1457                                          (DLGPROC16) ptr, (LPARAM) lfs);
1458         FILEDLG_DestroyPrivate(lfs);
1459     }
1460
1461     TRACE("return lpstrFile='%s' !\n", (char *)MapSL(lpofn->lpstrFile));
1462     return bRet;
1463 }