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