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