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