Uniformize DllMain TRACEing across dlls.
[wine] / dlls / commdlg / filedlg31.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/unicode.h"
32 #include "wine/debug.h"
33 #include "cderr.h"
34 #include "winreg.h"
35 #include "winternl.h"
36 #include "winuser.h"
37 #include "commdlg.h"
38 #include "cderr.h"
39 #include "winreg.h"
40 #include "winternl.h"
41 #include "shlwapi.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
44
45 #include "cdlg.h"
46 #include "filedlg31.h"
47
48 #define BUFFILE 512
49 #define BUFFILEALLOC 512 * sizeof(WCHAR)
50
51 static const WCHAR FILE_star[] = {'*','.','*', 0};
52 static const WCHAR FILE_bslash[] = {'\\', 0};
53 static const WCHAR FILE_specc[] = {'%','c',':', 0};
54 static const int fldrHeight = 16;
55 static const int fldrWidth = 20;
56
57 static HICON hFolder = 0;
58 static HICON hFolder2 = 0;
59 static HICON hFloppy = 0;
60 static HICON hHDisk = 0;
61 static HICON hCDRom = 0;
62 static HICON hNet = 0;
63
64 /***********************************************************************
65  *                              FD31_Init                       [internal]
66  */
67 BOOL FD31_Init(void)
68 {
69     static BOOL initialized = 0;
70
71     if (!initialized) {
72         hFolder  = LoadImageA( COMDLG32_hInstance, "FOLDER", IMAGE_ICON, 16, 16, LR_SHARED );
73         hFolder2 = LoadImageA( COMDLG32_hInstance, "FOLDER2", IMAGE_ICON, 16, 16, LR_SHARED );
74         hFloppy  = LoadImageA( COMDLG32_hInstance, "FLOPPY", IMAGE_ICON, 16, 16, LR_SHARED );
75         hHDisk   = LoadImageA( COMDLG32_hInstance, "HDISK", IMAGE_ICON, 16, 16, LR_SHARED );
76         hCDRom   = LoadImageA( COMDLG32_hInstance, "CDROM", IMAGE_ICON, 16, 16, LR_SHARED );
77         hNet     = LoadImageA( COMDLG32_hInstance, "NETWORK", IMAGE_ICON, 16, 16, LR_SHARED );
78         if (hFolder == 0 || hFolder2 == 0 || hFloppy == 0 ||
79             hHDisk == 0 || hCDRom == 0 || hNet == 0)
80         {
81             ERR("Error loading icons !\n");
82             return FALSE;
83         }
84         initialized = TRUE;
85     }
86     return TRUE;
87 }
88
89 /***********************************************************************
90  *                              FD31_StripEditControl        [internal]
91  * Strip pathnames off the contents of the edit control.
92  */
93 static void FD31_StripEditControl(HWND hwnd)
94 {
95     WCHAR temp[BUFFILE], *cp;
96
97     GetDlgItemTextW( hwnd, edt1, temp, sizeof(temp)/sizeof(WCHAR));
98     cp = strrchrW(temp, '\\');
99     if (cp != NULL) {
100         strcpyW(temp, cp+1);
101     }
102     cp = strrchrW(temp, ':');
103     if (cp != NULL) {
104         strcpyW(temp, cp+1);
105     }
106     /* FIXME: shouldn't we do something with the result here? ;-) */
107 }
108
109 /***********************************************************************
110  *                              FD31_CallWindowProc          [internal]
111  *
112  *      Call the appropriate hook
113  */
114 BOOL FD31_CallWindowProc(PFD31_DATA lfs, UINT wMsg, WPARAM wParam,
115                          LPARAM lParam)
116 {
117     return lfs->callbacks->CWP(lfs, wMsg, wParam, lParam);
118 }
119
120 /***********************************************************************
121  *                              FD31_ScanDir                 [internal]
122  */
123 static BOOL FD31_ScanDir(HWND hWnd, LPWSTR newPath)
124 {
125     WCHAR               buffer[BUFFILE];
126     HWND                hdlg, hdlgDir;
127     LRESULT             lRet = TRUE;
128     HCURSOR             hCursorWait, oldCursor;
129
130     TRACE("Trying to change to %s\n", debugstr_w(newPath));
131     if  ( newPath[0] && !SetCurrentDirectoryW( newPath ))
132         return FALSE;
133     lstrcpynW(buffer, newPath, sizeof(buffer)/sizeof(WCHAR));
134
135     /* get the list of spec files */
136     GetDlgItemTextW(hWnd, edt1, buffer, sizeof(buffer)/sizeof(WCHAR));
137
138     hCursorWait = LoadCursorA(0, (LPSTR)IDC_WAIT);
139     oldCursor = SetCursor(hCursorWait);
140
141     /* list of files */
142     if ((hdlg = GetDlgItem(hWnd, lst1)) != 0) {
143         WCHAR*  scptr; /* ptr on semi-colon */
144         WCHAR*  filter = buffer;
145
146         TRACE("Using filter %s\n", debugstr_w(filter));
147         SendMessageW(hdlg, LB_RESETCONTENT, 0, 0);
148         while (filter) {
149             scptr = strchrW(filter, ';');
150             if (scptr)  *scptr = 0;
151             while (*filter == ' ') filter++;
152             TRACE("Using file spec %s\n", debugstr_w(filter));
153             if (SendMessageW(hdlg, LB_DIR, 0, (LPARAM)filter) == LB_ERR)
154                 return FALSE;
155             if (scptr) *scptr = ';';
156                 filter = (scptr) ? (scptr + 1) : 0;
157          }
158     }
159
160     /* list of directories */
161     strcpyW(buffer, FILE_star);
162
163     if ((hdlgDir = GetDlgItem(hWnd, lst2)) != 0) {
164         lRet = DlgDirListW(hWnd, buffer, lst2, stc1, DDL_EXCLUSIVE | DDL_DIRECTORY);
165     }
166     SetCursor(oldCursor);
167     return lRet;
168 }
169
170 /***********************************************************************
171  *                              FD31_GetFileType                [internal]
172  */
173
174 static LPWSTR FD31_GetFileType(LPWSTR cfptr, LPWSTR fptr, WORD index)
175 {
176   int n, i;
177   i = 0;
178   if (cfptr)
179     for ( ;(n = lstrlenW(cfptr)) != 0; i++)
180       {
181         cfptr += n + 1;
182         if (i == index)
183           return cfptr;
184         cfptr += lstrlenW(cfptr) + 1;
185       }
186   if (fptr)
187     for ( ;(n = lstrlenW(fptr)) != 0; i++)
188       {
189         fptr += n + 1;
190         if (i == index)
191           return fptr;
192         fptr += lstrlenW(fptr) + 1;
193     }
194   return (LPWSTR) FILE_star; /* FIXME */
195 }
196
197 /***********************************************************************
198  *                              FD31_WMDrawItem              [internal]
199  */
200 LONG FD31_WMDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam,
201        int savedlg, LPDRAWITEMSTRUCT lpdis)
202 {
203     WCHAR *str;
204     HICON hIcon;
205     COLORREF oldText = 0, oldBk = 0;
206
207     if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst1)
208     {
209         if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC))) return FALSE;
210         SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
211                       (LPARAM)str);
212
213         if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
214         {
215             oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
216             oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
217         }
218         if (savedlg)
219             SetTextColor(lpdis->hDC,GetSysColor(COLOR_GRAYTEXT) );
220
221         ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + 1,
222                   lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
223                   &(lpdis->rcItem), str, lstrlenW(str), NULL);
224
225         if (lpdis->itemState & ODS_SELECTED)
226             DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
227
228         if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
229         {
230             SetBkColor( lpdis->hDC, oldBk );
231             SetTextColor( lpdis->hDC, oldText );
232         }
233         HeapFree(GetProcessHeap(), 0, str);
234         return TRUE;
235     }
236
237     if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst2)
238     {
239         if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
240             return FALSE;
241         SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
242                       (LPARAM)str);
243
244         if (lpdis->itemState & ODS_SELECTED)
245         {
246             oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
247             oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
248         }
249         ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
250                   lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
251                   &(lpdis->rcItem), str, lstrlenW(str), NULL);
252
253         if (lpdis->itemState & ODS_SELECTED)
254             DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
255
256         if (lpdis->itemState & ODS_SELECTED)
257         {
258             SetBkColor( lpdis->hDC, oldBk );
259             SetTextColor( lpdis->hDC, oldText );
260         }
261         DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hFolder);
262         HeapFree(GetProcessHeap(), 0, str);
263         return TRUE;
264     }
265     if (lpdis->CtlType == ODT_COMBOBOX && lpdis->CtlID == cmb2)
266     {
267         char root[] = "a:";
268         if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
269             return FALSE;
270         SendMessageW(lpdis->hwndItem, CB_GETLBTEXT, lpdis->itemID,
271                       (LPARAM)str);
272         root[0] += str[2] - 'a';
273         switch(GetDriveTypeA(root))
274         {
275         case DRIVE_REMOVABLE: hIcon = hFloppy; break;
276         case DRIVE_CDROM:     hIcon = hCDRom; break;
277         case DRIVE_REMOTE:    hIcon = hNet; break;
278         case DRIVE_FIXED:
279         default:           hIcon = hHDisk; break;
280         }
281         if (lpdis->itemState & ODS_SELECTED)
282         {
283             oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
284             oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
285         }
286         ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
287                   lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
288                   &(lpdis->rcItem), str, lstrlenW(str), NULL);
289
290         if (lpdis->itemState & ODS_SELECTED)
291         {
292             SetBkColor( lpdis->hDC, oldBk );
293             SetTextColor( lpdis->hDC, oldText );
294         }
295         DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hIcon);
296         HeapFree(GetProcessHeap(), 0, str);
297         return TRUE;
298     }
299     return FALSE;
300 }
301
302 /***********************************************************************
303  *                              FD31_UpdateResult            [internal]
304  *      update the displayed file name (with path)
305  */
306 static void FD31_UpdateResult(PFD31_DATA lfs, WCHAR *tmpstr)
307 {
308     int lenstr2;
309     LPOPENFILENAMEW ofnW = lfs->ofnW;
310     WCHAR tmpstr2[BUFFILE];
311     WCHAR *p;
312
313     TRACE("%s\n", debugstr_w(tmpstr));
314     if(ofnW->Flags & OFN_NOVALIDATE)
315         tmpstr2[0] = '\0';
316     else
317         GetCurrentDirectoryW(BUFFILE, tmpstr2);
318     lenstr2 = strlenW(tmpstr2);
319     if (lenstr2 > 3)
320         tmpstr2[lenstr2++]='\\';
321     lstrcpynW(tmpstr2+lenstr2, tmpstr, BUFFILE-lenstr2);
322     if (ofnW->lpstrFile)
323         lstrcpynW(ofnW->lpstrFile, tmpstr2, ofnW->nMaxFile);
324
325     /* set filename offset */
326     p = PathFindFileNameW(ofnW->lpstrFile);
327     ofnW->nFileOffset = (p - ofnW->lpstrFile);
328
329     /* set extension offset */
330     p = PathFindExtensionW(ofnW->lpstrFile);
331     ofnW->nFileExtension = (*p) ? (p - ofnW->lpstrFile) + 1 : 0;
332
333     TRACE("file %s, file offset %d, ext offset %d\n",
334           debugstr_w(ofnW->lpstrFile), ofnW->nFileOffset, ofnW->nFileExtension);
335
336     /* update the real client structures if any */
337     lfs->callbacks->UpdateResult(lfs);
338 }
339
340 /***********************************************************************
341  *                              FD31_UpdateFileTitle         [internal]
342  *      update the displayed file name (without path)
343  */
344 static void FD31_UpdateFileTitle(PFD31_DATA lfs)
345 {
346   LONG lRet;
347   LPOPENFILENAMEW ofnW = lfs->ofnW;
348   if (ofnW->lpstrFileTitle != NULL)
349   {
350     lRet = SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
351     SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETTEXT, lRet,
352                              (LPARAM)ofnW->lpstrFileTitle );
353     lfs->callbacks->UpdateFileTitle(lfs);
354   }
355 }
356
357 /***********************************************************************
358  *                              FD31_DirListDblClick         [internal]
359  */
360 static LRESULT FD31_DirListDblClick( PFD31_DATA lfs )
361 {
362   LONG lRet;
363   HWND hWnd = lfs->hwnd;
364   LPWSTR pstr;
365   WCHAR tmpstr[BUFFILE];
366
367   /* get the raw string (with brackets) */
368   lRet = SendDlgItemMessageW(hWnd, lst2, LB_GETCURSEL, 0, 0);
369   if (lRet == LB_ERR) return TRUE;
370   pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
371   SendDlgItemMessageW(hWnd, lst2, LB_GETTEXT, lRet,
372                      (LPARAM)pstr);
373   strcpyW( tmpstr, pstr );
374   HeapFree(GetProcessHeap(), 0, pstr);
375   /* get the selected directory in tmpstr */
376   if (tmpstr[0] == '[')
377     {
378       tmpstr[lstrlenW(tmpstr) - 1] = 0;
379       strcpyW(tmpstr,tmpstr+1);
380     }
381   strcatW(tmpstr, FILE_bslash);
382
383   FD31_ScanDir(hWnd, tmpstr);
384   /* notify the app */
385   if (lfs->hook)
386     {
387       if (FD31_CallWindowProc(lfs, lfs->lbselchstring, lst2,
388               MAKELONG(lRet,CD_LBSELCHANGE)))
389         return TRUE;
390     }
391   return TRUE;
392 }
393
394 /***********************************************************************
395  *                              FD31_FileListSelect         [internal]
396  *    called when a new item is picked in the file list
397  */
398 static LRESULT FD31_FileListSelect( PFD31_DATA lfs )
399 {
400     LONG lRet;
401     HWND hWnd = lfs->hwnd;
402     LPWSTR pstr;
403
404     lRet = lfs->callbacks->SendLbGetCurSel(lfs);
405     if (lRet == LB_ERR)
406         return TRUE;
407
408     /* set the edit control to the choosen file */
409     if ((pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
410     {
411         SendDlgItemMessageW(hWnd, lst1, LB_GETTEXT, lRet,
412                        (LPARAM)pstr);
413         SetDlgItemTextW( hWnd, edt1, pstr );
414         HeapFree(GetProcessHeap(), 0, pstr);
415     }
416     if (lfs->hook)
417     {
418         FD31_CallWindowProc(lfs, lfs->lbselchstring, lst1,
419                            MAKELONG(lRet,CD_LBSELCHANGE));
420     }
421     /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD,
422            CD_LBSELNOITEMS */
423     return TRUE;
424 }
425
426 /***********************************************************************
427  *                              FD31_TestPath      [internal]
428  *      before accepting the file name, test if it includes wild cards
429  *      tries to scan the directory and returns TRUE if no error.
430  */
431 static LRESULT FD31_TestPath( PFD31_DATA lfs, LPWSTR path )
432 {
433     HWND hWnd = lfs->hwnd;
434     LPWSTR pBeginFileName, pstr2;
435     WCHAR tmpstr2[BUFFILE];
436
437     pBeginFileName = strrchrW(path, '\\');
438     if (pBeginFileName == NULL)
439         pBeginFileName = strrchrW(path, ':');
440
441     if (strchrW(path,'*') != NULL || strchrW(path,'?') != NULL)
442     {
443         /* edit control contains wildcards */
444         if (pBeginFileName != NULL)
445         {
446             lstrcpynW(tmpstr2, pBeginFileName + 1, BUFFILE);
447             *(pBeginFileName + 1) = 0;
448         }
449         else
450         {
451             strcpyW(tmpstr2, path);
452             if(!(lfs->ofnW->Flags & OFN_NOVALIDATE))
453                 *path = 0;
454         }
455
456         TRACE("path=%s, tmpstr2=%s\n", debugstr_w(path), debugstr_w(tmpstr2));
457         SetDlgItemTextW( hWnd, edt1, tmpstr2 );
458         FD31_ScanDir(hWnd, path);
459         return (lfs->ofnW->Flags & OFN_NOVALIDATE) ? TRUE : FALSE;
460     }
461
462     /* no wildcards, we might have a directory or a filename */
463     /* try appending a wildcard and reading the directory */
464
465     pstr2 = path + lstrlenW(path);
466     if (pBeginFileName == NULL || *(pBeginFileName + 1) != 0)
467         strcatW(path, FILE_bslash);
468
469     /* if ScanDir succeeds, we have changed the directory */
470     if (FD31_ScanDir(hWnd, path))
471         return FALSE; /* and path is not a valid file name */
472
473     /* if not, this must be a filename */
474
475     *pstr2 = 0; /* remove the wildcard added before */
476
477     if (pBeginFileName != NULL)
478     {
479         /* strip off the pathname */
480         *pBeginFileName = 0;
481         SetDlgItemTextW( hWnd, edt1, pBeginFileName + 1 );
482
483         lstrcpynW(tmpstr2, pBeginFileName + 1, sizeof(tmpstr2)/sizeof(WCHAR) );
484         /* Should we MessageBox() if this fails? */
485         if (!FD31_ScanDir(hWnd, path))
486         {
487             return FALSE;
488         }
489         strcpyW(path, tmpstr2);
490     }
491     else
492         SetDlgItemTextW( hWnd, edt1, path );
493     return TRUE;
494 }
495
496 /***********************************************************************
497  *                              FD31_Validate               [internal]
498  *   called on: click Ok button, Enter in edit, DoubleClick in file list
499  */
500 static LRESULT FD31_Validate( PFD31_DATA lfs, LPWSTR path, UINT control, INT itemIndex,
501                                  BOOL internalUse )
502 {
503     LONG lRet;
504     HWND hWnd = lfs->hwnd;
505     OPENFILENAMEW ofnsav;
506     LPOPENFILENAMEW ofnW = lfs->ofnW;
507     WCHAR filename[BUFFILE];
508
509     ofnsav = *ofnW; /* for later restoring */
510
511     /* get current file name */
512     if (path)
513         lstrcpynW(filename, path, sizeof(filename)/sizeof(WCHAR));
514     else
515         GetDlgItemTextW( hWnd, edt1, filename, sizeof(filename)/sizeof(WCHAR));
516
517     TRACE("got filename = %s\n", debugstr_w(filename));
518     /* if we did not click in file list to get there */
519     if (control != lst1)
520     {
521         if (!FD31_TestPath( lfs, filename) )
522            return FALSE;
523     }
524     FD31_UpdateResult(lfs, filename);
525
526     if (internalUse)
527     { /* called internally after a change in a combo */
528         if (lfs->hook)
529         {
530              FD31_CallWindowProc(lfs, lfs->lbselchstring, control,
531                              MAKELONG(itemIndex,CD_LBSELCHANGE));
532         }
533         return TRUE;
534     }
535
536     FD31_UpdateFileTitle(lfs);
537     if (lfs->hook)
538     {
539         lRet = (BOOL)FD31_CallWindowProc(lfs, lfs->fileokstring,
540                   0, lfs->lParam );
541         if (lRet)
542         {
543             *ofnW = ofnsav; /* restore old state */
544             return FALSE;
545         }
546     }
547     if ((ofnW->Flags & OFN_ALLOWMULTISELECT) && (ofnW->Flags & OFN_EXPLORER))
548     {
549         if (ofnW->lpstrFile)
550         {
551             LPWSTR str = (LPWSTR)ofnW->lpstrFile;
552             LPWSTR ptr = strrchrW(str, '\\');
553             str[lstrlenW(str) + 1] = '\0';
554             *ptr = 0;
555         }
556     }
557     return TRUE;
558 }
559
560 /***********************************************************************
561  *                              FD31_DiskChange             [internal]
562  *    called when a new item is picked in the disk selection combo
563  */
564 static LRESULT FD31_DiskChange( PFD31_DATA lfs )
565 {
566     LONG lRet;
567     HWND hWnd = lfs->hwnd;
568     LPWSTR pstr;
569     WCHAR diskname[BUFFILE];
570
571     FD31_StripEditControl(hWnd);
572     lRet = SendDlgItemMessageW(hWnd, cmb2, CB_GETCURSEL, 0, 0L);
573     if (lRet == LB_ERR)
574         return 0;
575     pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
576     SendDlgItemMessageW(hWnd, cmb2, CB_GETLBTEXT, lRet,
577                          (LPARAM)pstr);
578     wsprintfW(diskname, FILE_specc, pstr[2]);
579     HeapFree(GetProcessHeap(), 0, pstr);
580
581     return FD31_Validate( lfs, diskname, cmb2, lRet, TRUE );
582 }
583
584 /***********************************************************************
585  *                              FD31_FileTypeChange         [internal]
586  *    called when a new item is picked in the file type combo
587  */
588 static LRESULT FD31_FileTypeChange( PFD31_DATA lfs )
589 {
590     LONG lRet;
591     LPWSTR pstr;
592
593     lRet = SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETCURSEL, 0, 0);
594     if (lRet == LB_ERR)
595         return TRUE;
596     pstr = (LPWSTR)SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETITEMDATA, lRet, 0);
597     TRACE("Selected filter : %s\n", debugstr_w(pstr));
598     SetDlgItemTextW( lfs->hwnd, edt1, pstr );
599
600     return FD31_Validate( lfs, NULL, cmb1, lRet, TRUE );
601 }
602
603 /***********************************************************************
604  *                              FD31_WMCommand               [internal]
605  */
606 LRESULT FD31_WMCommand(HWND hWnd, LPARAM lParam, UINT notification,
607        UINT control, PFD31_DATA lfs )
608 {
609     switch (control)
610     {
611         case lst1: /* file list */
612         FD31_StripEditControl(hWnd);
613         if (notification == LBN_DBLCLK)
614         {
615             return SendMessageW(hWnd, WM_COMMAND, IDOK, 0);
616         }
617         else if (notification == LBN_SELCHANGE)
618             return FD31_FileListSelect( lfs );
619         break;
620
621         case lst2: /* directory list */
622         FD31_StripEditControl(hWnd);
623         if (notification == LBN_DBLCLK)
624             return FD31_DirListDblClick( lfs );
625         break;
626
627         case cmb1: /* file type drop list */
628         if (notification == CBN_SELCHANGE)
629             return FD31_FileTypeChange( lfs );
630         break;
631
632         case chx1:
633         break;
634
635         case pshHelp:
636         break;
637
638         case cmb2: /* disk dropdown combo */
639         if (notification == CBN_SELCHANGE)
640             return FD31_DiskChange( lfs );
641         break;
642
643         case IDOK:
644         TRACE("OK pressed\n");
645         if (FD31_Validate( lfs, NULL, control, 0, FALSE ))
646             EndDialog(hWnd, TRUE);
647         return TRUE;
648
649         case IDCANCEL:
650         EndDialog(hWnd, FALSE);
651         return TRUE;
652
653         case IDABORT: /* can be sent by the hook procedure */
654         EndDialog(hWnd, TRUE);
655         return TRUE;
656     }
657     return FALSE;
658 }
659
660 /************************************************************************
661  *                              FD31_MapStringPairsToW       [internal]
662  *      map string pairs to Unicode
663  */
664 static LPWSTR FD31_MapStringPairsToW(LPCSTR strA, UINT size)
665 {
666     LPCSTR s;
667     LPWSTR x;
668     unsigned int n, len;
669
670     s = strA;
671     while (*s)
672         s = s+strlen(s)+1;
673     s++;
674     n = s + 1 - strA; /* Don't forget the other \0 */
675     if (n < size) n = size;
676
677     len = MultiByteToWideChar( CP_ACP, 0, strA, n, NULL, 0 );
678     x = HeapAlloc(GetProcessHeap(),0, len * sizeof(WCHAR));
679     MultiByteToWideChar( CP_ACP, 0, strA, n, x, len );
680     return x;
681 }
682
683
684 /************************************************************************
685  *                              FD31_DupToW                  [internal]
686  *      duplicates an Ansi string to unicode, with a buffer size
687  */
688 static LPWSTR FD31_DupToW(LPCSTR str, DWORD size)
689 {
690     LPWSTR strW = NULL;
691     if (str && (size > 0))
692     {
693         strW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
694         if (strW) MultiByteToWideChar( CP_ACP, 0, str, -1, strW, size );
695     }
696     return strW;
697 }
698
699 /************************************************************************
700  *                              FD31_MapOfnStructA          [internal]
701  *      map a 32 bits Ansi structure to an Unicode one
702  */
703 void FD31_MapOfnStructA(const LPOPENFILENAMEA ofnA, LPOPENFILENAMEW ofnW, BOOL open)
704 {
705     UNICODE_STRING usBuffer;
706
707     ofnW->lStructSize = sizeof(OPENFILENAMEW);
708     ofnW->hwndOwner = ofnA->hwndOwner;
709     ofnW->hInstance = ofnA->hInstance;
710     if (ofnA->lpstrFilter)
711         ofnW->lpstrFilter = FD31_MapStringPairsToW(ofnA->lpstrFilter, 0);
712
713     if ((ofnA->lpstrCustomFilter) && (*(ofnA->lpstrCustomFilter)))
714         ofnW->lpstrCustomFilter = FD31_MapStringPairsToW(ofnA->lpstrCustomFilter, ofnA->nMaxCustFilter);
715     ofnW->nMaxCustFilter = ofnA->nMaxCustFilter;
716     ofnW->nFilterIndex = ofnA->nFilterIndex;
717     ofnW->nMaxFile = ofnA->nMaxFile;
718     ofnW->lpstrFile = FD31_DupToW(ofnA->lpstrFile, ofnW->nMaxFile);
719     ofnW->nMaxFileTitle = ofnA->nMaxFileTitle;
720     ofnW->lpstrFileTitle = FD31_DupToW(ofnA->lpstrFileTitle, ofnW->nMaxFileTitle);
721     if (ofnA->lpstrInitialDir)
722     {
723         RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpstrInitialDir);
724         ofnW->lpstrInitialDir = usBuffer.Buffer;
725     }
726     if (ofnA->lpstrTitle) {
727         RtlCreateUnicodeStringFromAsciiz (&usBuffer, ofnA->lpstrTitle);
728         ofnW->lpstrTitle = usBuffer.Buffer;
729     } else {
730         WCHAR buf[16];
731         int len;
732         LoadStringW(COMDLG32_hInstance, open ? IDS_OPEN_FILE : IDS_SAVE_AS,
733                     buf, sizeof(buf)/sizeof(WCHAR));
734         len = lstrlenW(buf)+1;
735         ofnW->lpstrTitle = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
736         memcpy((void*)ofnW->lpstrTitle, buf, len*sizeof(WCHAR));
737     }
738     ofnW->Flags = ofnA->Flags;
739     ofnW->nFileOffset = ofnA->nFileOffset;
740     ofnW->nFileExtension = ofnA->nFileExtension;
741     ofnW->lpstrDefExt = FD31_DupToW(ofnA->lpstrDefExt, 3);
742     if ((ofnA->Flags & OFN_ENABLETEMPLATE) && (ofnA->lpTemplateName))
743     {
744         if (HIWORD(ofnA->lpTemplateName))
745         {
746             RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpTemplateName);
747             ofnW->lpTemplateName = usBuffer.Buffer;
748         }
749         else /* numbered resource */
750             ofnW->lpTemplateName = (LPWSTR) ofnA->lpTemplateName;
751     }
752 }
753
754
755 /************************************************************************
756  *                              FD31_FreeOfnW          [internal]
757  *      Undo all allocations done by FD31_MapOfnStructA
758  */
759 void FD31_FreeOfnW(LPOPENFILENAMEW ofnW)
760 {
761    HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrFilter);
762    HeapFree(GetProcessHeap(), 0, ofnW->lpstrCustomFilter);
763    HeapFree(GetProcessHeap(), 0, ofnW->lpstrFile);
764    HeapFree(GetProcessHeap(), 0, ofnW->lpstrFileTitle);
765    HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrInitialDir);
766    HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrTitle);
767    if ((ofnW->lpTemplateName) && (HIWORD(ofnW->lpTemplateName)))
768        HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpTemplateName);
769 }
770
771 /************************************************************************
772  *                              FD31_DestroyPrivate            [internal]
773  *      destroys the private object
774  */
775 void FD31_DestroyPrivate(PFD31_DATA lfs)
776 {
777     HWND hwnd;
778     if (!lfs) return;
779     hwnd = lfs->hwnd;
780     TRACE("destroying private allocation %p\n", lfs);
781     lfs->callbacks->Destroy(lfs);
782     HeapFree(GetProcessHeap(), 0, lfs);
783     RemovePropA(hwnd, FD31_OFN_PROP);
784 }
785
786 /************************************************************************
787  *                              FD31_AllocPrivate            [internal]
788  *      allocate a private object to hold 32 bits Unicode
789  *      structure that will be used throughtout the calls, while
790  *      keeping available the original structures and a few variables
791  *      On entry : type = dialog procedure type (16,32A,32W)
792  *                 dlgType = dialog type (open or save)
793  */
794 PFD31_DATA FD31_AllocPrivate(LPARAM lParam, UINT dlgType,
795                              PFD31_CALLBACKS callbacks, DWORD data)
796 {
797     PFD31_DATA lfs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FD31_DATA));
798
799     TRACE("alloc private buf %p\n", lfs);
800     if (!lfs) return NULL;
801     lfs->hook = FALSE;
802     lfs->lParam = lParam;
803     lfs->open = (dlgType == OPEN_DIALOG);
804     lfs->callbacks = callbacks;
805     if (! lfs->callbacks->Init(lParam, lfs, data))
806     {
807         FD31_DestroyPrivate(lfs);
808         return NULL;
809     }
810     lfs->lbselchstring = RegisterWindowMessageA(LBSELCHSTRINGA);
811     lfs->fileokstring = RegisterWindowMessageA(FILEOKSTRINGA);
812
813     return lfs;
814 }
815
816 /***********************************************************************
817  *                              FD31_WMInitDialog            [internal]
818  */
819
820 LONG FD31_WMInitDialog(HWND hWnd, WPARAM wParam, LPARAM lParam)
821 {
822   int i, n;
823   WCHAR tmpstr[BUFFILE];
824   LPWSTR pstr, old_pstr;
825   LPOPENFILENAMEW ofn;
826   PFD31_DATA lfs = (PFD31_DATA) lParam;
827
828   if (!lfs) return FALSE;
829   SetPropA(hWnd, FD31_OFN_PROP, (HANDLE)lfs);
830   lfs->hwnd = hWnd;
831   ofn = lfs->ofnW;
832
833   TRACE("flags=%lx initialdir=%s\n", ofn->Flags, debugstr_w(ofn->lpstrInitialDir));
834
835   SetWindowTextW( hWnd, ofn->lpstrTitle );
836   /* read custom filter information */
837   if (ofn->lpstrCustomFilter)
838     {
839       pstr = ofn->lpstrCustomFilter;
840       n = 0;
841       TRACE("lpstrCustomFilter = %p\n", pstr);
842       while(*pstr)
843         {
844           old_pstr = pstr;
845           i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
846                                    (LPARAM)(ofn->lpstrCustomFilter) + n );
847           n += lstrlenW(pstr) + 1;
848           pstr += lstrlenW(pstr) + 1;
849           TRACE("add str=%s associated to %s\n",
850                 debugstr_w(old_pstr), debugstr_w(pstr));
851           SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
852           n += lstrlenW(pstr) + 1;
853           pstr += lstrlenW(pstr) + 1;
854         }
855     }
856   /* read filter information */
857   if (ofn->lpstrFilter) {
858         pstr = (LPWSTR) ofn->lpstrFilter;
859         n = 0;
860         while(*pstr) {
861           old_pstr = pstr;
862           i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
863                                        (LPARAM)(ofn->lpstrFilter + n) );
864           n += lstrlenW(pstr) + 1;
865           pstr += lstrlenW(pstr) + 1;
866           TRACE("add str=%s associated to %s\n",
867                 debugstr_w(old_pstr), debugstr_w(pstr));
868           SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
869           n += lstrlenW(pstr) + 1;
870           pstr += lstrlenW(pstr) + 1;
871         }
872   }
873   /* set default filter */
874   if (ofn->nFilterIndex == 0 && ofn->lpstrCustomFilter == NULL)
875         ofn->nFilterIndex = 1;
876   SendDlgItemMessageW(hWnd, cmb1, CB_SETCURSEL, ofn->nFilterIndex - 1, 0);
877   lstrcpynW(tmpstr, FD31_GetFileType(ofn->lpstrCustomFilter,
878              (LPWSTR)ofn->lpstrFilter, ofn->nFilterIndex - 1),BUFFILE);
879   TRACE("nFilterIndex = %ld, SetText of edt1 to %s\n",
880                         ofn->nFilterIndex, debugstr_w(tmpstr));
881   SetDlgItemTextW( hWnd, edt1, tmpstr );
882   /* get drive list */
883   *tmpstr = 0;
884   DlgDirListComboBoxW(hWnd, tmpstr, cmb2, 0, DDL_DRIVES | DDL_EXCLUSIVE);
885   /* read initial directory */
886   /* FIXME: Note that this is now very version-specific (See MSDN description of
887    * the OPENFILENAME structure).  For example under 2000/XP any path in the
888    * lpstrFile overrides the lpstrInitialDir, but not under 95/98/ME
889    */
890   if (ofn->lpstrInitialDir != NULL)
891     {
892       int len;
893       lstrcpynW(tmpstr, ofn->lpstrInitialDir, 511);
894       len = lstrlenW(tmpstr);
895       if (len > 0 && tmpstr[len-1] != '\\'  && tmpstr[len-1] != ':') {
896         tmpstr[len]='\\';
897         tmpstr[len+1]='\0';
898       }
899     }
900   else
901     *tmpstr = 0;
902   if (!FD31_ScanDir(hWnd, tmpstr)) {
903     *tmpstr = 0;
904     if (!FD31_ScanDir(hWnd, tmpstr))
905       WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr));
906   }
907   /* select current drive in combo 2, omit missing drives */
908   {
909       char dir[MAX_PATH];
910       char str[4] = "a:\\";
911       GetCurrentDirectoryA( sizeof(dir), dir );
912       for(i = 0, n = -1; i < 26; i++)
913       {
914           str[0] = 'a' + i;
915           if (GetDriveTypeA(str) > DRIVE_NO_ROOT_DIR) n++;
916           if (toupper(str[0]) == toupper(dir[0])) break;
917       }
918   }
919   SendDlgItemMessageW(hWnd, cmb2, CB_SETCURSEL, n, 0);
920   if (!(ofn->Flags & OFN_SHOWHELP))
921     ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
922   if (ofn->Flags & OFN_HIDEREADONLY)
923     ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
924   if (lfs->hook)
925       return (BOOL) FD31_CallWindowProc(lfs, WM_INITDIALOG, wParam, lfs->lParam);
926   return TRUE;
927 }
928
929 int FD31_GetFldrHeight(void)
930 {
931   return fldrHeight;
932 }