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