Added some defines concerning SChannel CSPs.
[wine/multimedia.git] / dlls / commdlg / filedlg31.c
blobaf45ed627b779ef86fd3b9b6d2e46bbee4bb1e68
1 /*
2 * COMMDLG - File Dialogs
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1996 Albrecht Kleine
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.
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
33 #include "cderr.h"
34 #include "winreg.h"
35 #include "winternl.h"
36 #include "winuser.h"
37 #include "commdlg.h"
38 #include "cderr.h"
39 #include "winreg.h"
40 #include "winternl.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
44 #include "cdlg.h"
45 #include "filedlg31.h"
47 #define BUFFILE 512
48 #define BUFFILEALLOC 512 * sizeof(WCHAR)
50 static const WCHAR FILE_star[] = {'*','.','*', 0};
51 static const WCHAR FILE_bslash[] = {'\\', 0};
52 static const WCHAR FILE_specc[] = {'%','c',':', 0};
53 static const int fldrHeight = 16;
54 static const int fldrWidth = 20;
56 static HICON hFolder = 0;
57 static HICON hFolder2 = 0;
58 static HICON hFloppy = 0;
59 static HICON hHDisk = 0;
60 static HICON hCDRom = 0;
61 static HICON hNet = 0;
63 /***********************************************************************
64 * FD31_Init [internal]
66 BOOL FD31_Init(void)
68 static BOOL initialized = 0;
70 if (!initialized) {
71 hFolder = LoadImageA( COMDLG32_hInstance, "FOLDER", IMAGE_ICON, 16, 16, LR_SHARED );
72 hFolder2 = LoadImageA( COMDLG32_hInstance, "FOLDER2", IMAGE_ICON, 16, 16, LR_SHARED );
73 hFloppy = LoadImageA( COMDLG32_hInstance, "FLOPPY", IMAGE_ICON, 16, 16, LR_SHARED );
74 hHDisk = LoadImageA( COMDLG32_hInstance, "HDISK", IMAGE_ICON, 16, 16, LR_SHARED );
75 hCDRom = LoadImageA( COMDLG32_hInstance, "CDROM", IMAGE_ICON, 16, 16, LR_SHARED );
76 hNet = LoadImageA( COMDLG32_hInstance, "NETWORK", IMAGE_ICON, 16, 16, LR_SHARED );
77 if (hFolder == 0 || hFolder2 == 0 || hFloppy == 0 ||
78 hHDisk == 0 || hCDRom == 0 || hNet == 0)
80 ERR("Error loading icons !\n");
81 return FALSE;
83 initialized = TRUE;
85 return TRUE;
88 /***********************************************************************
89 * FD31_StripEditControl [internal]
90 * Strip pathnames off the contents of the edit control.
92 static void FD31_StripEditControl(HWND hwnd)
94 WCHAR temp[BUFFILE], *cp;
96 GetDlgItemTextW( hwnd, edt1, temp, sizeof(temp)/sizeof(WCHAR));
97 cp = strrchrW(temp, '\\');
98 if (cp != NULL) {
99 strcpyW(temp, cp+1);
101 cp = strrchrW(temp, ':');
102 if (cp != NULL) {
103 strcpyW(temp, cp+1);
105 /* FIXME: shouldn't we do something with the result here? ;-) */
108 /***********************************************************************
109 * FD31_CallWindowProc [internal]
111 * Call the appropriate hook
113 BOOL FD31_CallWindowProc(PFD31_DATA lfs, UINT wMsg, WPARAM wParam,
114 LPARAM lParam)
116 return lfs->callbacks->CWP(lfs, wMsg, wParam, lParam);
119 /***********************************************************************
120 * FD31_ScanDir [internal]
122 static BOOL FD31_ScanDir(HWND hWnd, LPWSTR newPath)
124 WCHAR buffer[BUFFILE];
125 HWND hdlg, hdlgDir;
126 LRESULT lRet = TRUE;
127 HCURSOR hCursorWait, oldCursor;
129 TRACE("Trying to change to %s\n", debugstr_w(newPath));
130 if ( newPath[0] && !SetCurrentDirectoryW( newPath ))
131 return FALSE;
132 lstrcpynW(buffer, newPath, sizeof(buffer)/sizeof(WCHAR));
134 /* get the list of spec files */
135 GetDlgItemTextW(hWnd, edt1, buffer, sizeof(buffer)/sizeof(WCHAR));
137 hCursorWait = LoadCursorA(0, (LPSTR)IDC_WAIT);
138 oldCursor = SetCursor(hCursorWait);
140 /* list of files */
141 if ((hdlg = GetDlgItem(hWnd, lst1)) != 0) {
142 WCHAR* scptr; /* ptr on semi-colon */
143 WCHAR* filter = buffer;
145 TRACE("Using filter %s\n", debugstr_w(filter));
146 SendMessageW(hdlg, LB_RESETCONTENT, 0, 0);
147 while (filter) {
148 scptr = strchrW(filter, ';');
149 if (scptr) *scptr = 0;
150 while (*filter == ' ') filter++;
151 TRACE("Using file spec %s\n", debugstr_w(filter));
152 if (SendMessageW(hdlg, LB_DIR, 0, (LPARAM)filter) == LB_ERR)
153 return FALSE;
154 if (scptr) *scptr = ';';
155 filter = (scptr) ? (scptr + 1) : 0;
159 /* list of directories */
160 strcpyW(buffer, FILE_star);
162 if ((hdlgDir = GetDlgItem(hWnd, lst2)) != 0) {
163 lRet = DlgDirListW(hWnd, buffer, lst2, stc1, DDL_EXCLUSIVE | DDL_DIRECTORY);
165 SetCursor(oldCursor);
166 return lRet;
169 /***********************************************************************
170 * FD31_GetFileType [internal]
173 static LPWSTR FD31_GetFileType(LPWSTR cfptr, LPWSTR fptr, WORD index)
175 int n, i;
176 i = 0;
177 if (cfptr)
178 for ( ;(n = lstrlenW(cfptr)) != 0; i++)
180 cfptr += n + 1;
181 if (i == index)
182 return cfptr;
183 cfptr += lstrlenW(cfptr) + 1;
185 if (fptr)
186 for ( ;(n = lstrlenW(fptr)) != 0; i++)
188 fptr += n + 1;
189 if (i == index)
190 return fptr;
191 fptr += lstrlenW(fptr) + 1;
193 return (LPWSTR) FILE_star; /* FIXME */
196 /***********************************************************************
197 * FD31_WMDrawItem [internal]
199 LONG FD31_WMDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam,
200 int savedlg, LPDRAWITEMSTRUCT lpdis)
202 WCHAR *str;
203 HICON hIcon;
204 COLORREF oldText = 0, oldBk = 0;
206 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst1)
208 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC))) return FALSE;
209 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
210 (LPARAM)str);
212 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
214 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
215 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
217 if (savedlg)
218 SetTextColor(lpdis->hDC,GetSysColor(COLOR_GRAYTEXT) );
220 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + 1,
221 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
222 &(lpdis->rcItem), str, lstrlenW(str), NULL);
224 if (lpdis->itemState & ODS_SELECTED)
225 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
227 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
229 SetBkColor( lpdis->hDC, oldBk );
230 SetTextColor( lpdis->hDC, oldText );
232 HeapFree(GetProcessHeap(), 0, str);
233 return TRUE;
236 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst2)
238 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
239 return FALSE;
240 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
241 (LPARAM)str);
243 if (lpdis->itemState & ODS_SELECTED)
245 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
246 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
248 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
249 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
250 &(lpdis->rcItem), str, lstrlenW(str), NULL);
252 if (lpdis->itemState & ODS_SELECTED)
253 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
255 if (lpdis->itemState & ODS_SELECTED)
257 SetBkColor( lpdis->hDC, oldBk );
258 SetTextColor( lpdis->hDC, oldText );
260 DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hFolder);
261 HeapFree(GetProcessHeap(), 0, str);
262 return TRUE;
264 if (lpdis->CtlType == ODT_COMBOBOX && lpdis->CtlID == cmb2)
266 char root[] = "a:";
267 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
268 return FALSE;
269 SendMessageW(lpdis->hwndItem, CB_GETLBTEXT, lpdis->itemID,
270 (LPARAM)str);
271 root[0] += str[2] - 'a';
272 switch(GetDriveTypeA(root))
274 case DRIVE_REMOVABLE: hIcon = hFloppy; break;
275 case DRIVE_CDROM: hIcon = hCDRom; break;
276 case DRIVE_REMOTE: hIcon = hNet; break;
277 case DRIVE_FIXED:
278 default: hIcon = hHDisk; break;
280 if (lpdis->itemState & ODS_SELECTED)
282 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
283 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
285 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
286 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
287 &(lpdis->rcItem), str, lstrlenW(str), NULL);
289 if (lpdis->itemState & ODS_SELECTED)
291 SetBkColor( lpdis->hDC, oldBk );
292 SetTextColor( lpdis->hDC, oldText );
294 DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hIcon);
295 HeapFree(GetProcessHeap(), 0, str);
296 return TRUE;
298 return FALSE;
301 /***********************************************************************
302 * FD31_UpdateResult [internal]
303 * update the displayed file name (with path)
305 void FD31_UpdateResult(PFD31_DATA lfs, WCHAR *tmpstr)
307 int lenstr2;
308 LPOPENFILENAMEW ofnW = &lfs->ofnW;
309 WCHAR tmpstr2[BUFFILE];
310 WCHAR *bs;
312 TRACE("%s\n", debugstr_w(tmpstr));
313 if(ofnW->Flags & OFN_NOVALIDATE)
314 tmpstr2[0] = '\0';
315 else
316 GetCurrentDirectoryW(BUFFILE, tmpstr2);
317 lenstr2 = strlenW(tmpstr2);
318 if (lenstr2 > 3)
319 tmpstr2[lenstr2++]='\\';
320 lstrcpynW(tmpstr2+lenstr2, tmpstr, BUFFILE-lenstr2);
321 if (ofnW->lpstrFile)
322 lstrcpynW(ofnW->lpstrFile, tmpstr2, ofnW->nMaxFile);
323 if((bs = strrchrW(tmpstr2, '\\')) != NULL)
324 ofnW->nFileOffset = bs - tmpstr2 +1;
325 else
326 ofnW->nFileOffset = 0;
327 ofnW->nFileExtension = 0;
328 while(tmpstr2[ofnW->nFileExtension] != '.' && tmpstr2[ofnW->nFileExtension] != '\0')
329 ofnW->nFileExtension++;
330 if (tmpstr2[ofnW->nFileExtension] == '\0')
331 ofnW->nFileExtension = 0;
332 else
333 ofnW->nFileExtension++;
334 /* update the real client structures if any */
335 lfs->callbacks->UpdateResult(lfs);
338 /***********************************************************************
339 * FD31_UpdateFileTitle [internal]
340 * update the displayed file name (without path)
342 void FD31_UpdateFileTitle(PFD31_DATA lfs)
344 LONG lRet;
345 LPOPENFILENAMEW ofnW = &lfs->ofnW;
346 if (ofnW->lpstrFileTitle != NULL)
348 lRet = SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
349 SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETTEXT, lRet,
350 (LPARAM)ofnW->lpstrFileTitle );
351 lfs->callbacks->UpdateFileTitle(lfs);
355 /***********************************************************************
356 * FD31_DirListDblClick [internal]
358 static LRESULT FD31_DirListDblClick( PFD31_DATA lfs )
360 LONG lRet;
361 HWND hWnd = lfs->hwnd;
362 LPWSTR pstr;
363 WCHAR tmpstr[BUFFILE];
365 /* get the raw string (with brackets) */
366 lRet = SendDlgItemMessageW(hWnd, lst2, LB_GETCURSEL, 0, 0);
367 if (lRet == LB_ERR) return TRUE;
368 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
369 SendDlgItemMessageW(hWnd, lst2, LB_GETTEXT, lRet,
370 (LPARAM)pstr);
371 strcpyW( tmpstr, pstr );
372 HeapFree(GetProcessHeap(), 0, pstr);
373 /* get the selected directory in tmpstr */
374 if (tmpstr[0] == '[')
376 tmpstr[lstrlenW(tmpstr) - 1] = 0;
377 strcpyW(tmpstr,tmpstr+1);
379 strcatW(tmpstr, FILE_bslash);
381 FD31_ScanDir(hWnd, tmpstr);
382 /* notify the app */
383 if (lfs->hook)
385 if (FD31_CallWindowProc(lfs, lfs->lbselchstring, lst2,
386 MAKELONG(lRet,CD_LBSELCHANGE)))
387 return TRUE;
389 return TRUE;
392 /***********************************************************************
393 * FD31_FileListSelect [internal]
394 * called when a new item is picked in the file list
396 static LRESULT FD31_FileListSelect( PFD31_DATA lfs )
398 LONG lRet;
399 HWND hWnd = lfs->hwnd;
400 LPWSTR pstr;
402 lRet = lfs->callbacks->SendLbGetCurSel(lfs);
403 if (lRet == LB_ERR)
404 return TRUE;
406 /* set the edit control to the choosen file */
407 if ((pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
409 SendDlgItemMessageW(hWnd, lst1, LB_GETTEXT, lRet,
410 (LPARAM)pstr);
411 SetDlgItemTextW( hWnd, edt1, pstr );
412 HeapFree(GetProcessHeap(), 0, pstr);
414 if (lfs->hook)
416 FD31_CallWindowProc(lfs, lfs->lbselchstring, lst1,
417 MAKELONG(lRet,CD_LBSELCHANGE));
419 /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD,
420 CD_LBSELNOITEMS */
421 return TRUE;
424 /***********************************************************************
425 * FD31_TestPath [internal]
426 * before accepting the file name, test if it includes wild cards
427 * tries to scan the directory and returns TRUE if no error.
429 static LRESULT FD31_TestPath( PFD31_DATA lfs, LPWSTR path )
431 HWND hWnd = lfs->hwnd;
432 LPWSTR pBeginFileName, pstr2;
433 WCHAR tmpstr2[BUFFILE];
435 pBeginFileName = strrchrW(path, '\\');
436 if (pBeginFileName == NULL)
437 pBeginFileName = strrchrW(path, ':');
439 if (strchrW(path,'*') != NULL || strchrW(path,'?') != NULL)
441 /* edit control contains wildcards */
442 if (pBeginFileName != NULL)
444 lstrcpynW(tmpstr2, pBeginFileName + 1, BUFFILE);
445 *(pBeginFileName + 1) = 0;
447 else
449 strcpyW(tmpstr2, path);
450 if(!(lfs->ofnW.Flags & OFN_NOVALIDATE))
451 *path = 0;
454 TRACE("path=%s, tmpstr2=%s\n", debugstr_w(path), debugstr_w(tmpstr2));
455 SetDlgItemTextW( hWnd, edt1, tmpstr2 );
456 FD31_ScanDir(hWnd, path);
457 return (lfs->ofnW.Flags & OFN_NOVALIDATE) ? TRUE : FALSE;
460 /* no wildcards, we might have a directory or a filename */
461 /* try appending a wildcard and reading the directory */
463 pstr2 = path + lstrlenW(path);
464 if (pBeginFileName == NULL || *(pBeginFileName + 1) != 0)
465 strcatW(path, FILE_bslash);
467 /* if ScanDir succeeds, we have changed the directory */
468 if (FD31_ScanDir(hWnd, path))
469 return FALSE; /* and path is not a valid file name */
471 /* if not, this must be a filename */
473 *pstr2 = 0; /* remove the wildcard added before */
475 if (pBeginFileName != NULL)
477 /* strip off the pathname */
478 *pBeginFileName = 0;
479 SetDlgItemTextW( hWnd, edt1, pBeginFileName + 1 );
481 lstrcpynW(tmpstr2, pBeginFileName + 1, sizeof(tmpstr2)/sizeof(WCHAR) );
482 /* Should we MessageBox() if this fails? */
483 if (!FD31_ScanDir(hWnd, path))
485 return FALSE;
487 strcpyW(path, tmpstr2);
489 else
490 SetDlgItemTextW( hWnd, edt1, path );
491 return TRUE;
494 /***********************************************************************
495 * FD31_Validate [internal]
496 * called on: click Ok button, Enter in edit, DoubleClick in file list
498 static LRESULT FD31_Validate( PFD31_DATA lfs, LPWSTR path, UINT control, INT itemIndex,
499 BOOL internalUse )
501 LONG lRet;
502 HWND hWnd = lfs->hwnd;
503 OPENFILENAMEW ofnsav;
504 LPOPENFILENAMEW ofnW = &lfs->ofnW;
505 WCHAR filename[BUFFILE];
507 ofnsav = *ofnW; /* for later restoring */
509 /* get current file name */
510 if (path)
511 lstrcpynW(filename, path, sizeof(filename)/sizeof(WCHAR));
512 else
513 GetDlgItemTextW( hWnd, edt1, filename, sizeof(filename)/sizeof(WCHAR));
515 TRACE("got filename = %s\n", debugstr_w(filename));
516 /* if we did not click in file list to get there */
517 if (control != lst1)
519 if (!FD31_TestPath( lfs, filename) )
520 return FALSE;
522 FD31_UpdateResult(lfs, filename);
524 if (internalUse)
525 { /* called internally after a change in a combo */
526 if (lfs->hook)
528 FD31_CallWindowProc(lfs, lfs->lbselchstring, control,
529 MAKELONG(itemIndex,CD_LBSELCHANGE));
531 return TRUE;
534 FD31_UpdateFileTitle(lfs);
535 if (lfs->hook)
537 lRet = (BOOL)FD31_CallWindowProc(lfs, lfs->fileokstring,
538 0, lfs->lParam );
539 if (lRet)
541 *ofnW = ofnsav; /* restore old state */
542 return FALSE;
545 if ((ofnW->Flags & OFN_ALLOWMULTISELECT) && (ofnW->Flags & OFN_EXPLORER))
547 if (ofnW->lpstrFile)
549 LPWSTR str = (LPWSTR)ofnW->lpstrFile;
550 LPWSTR ptr = strrchrW(str, '\\');
551 str[lstrlenW(str) + 1] = '\0';
552 *ptr = 0;
555 return TRUE;
558 /***********************************************************************
559 * FD31_DiskChange [internal]
560 * called when a new item is picked in the disk selection combo
562 static LRESULT FD31_DiskChange( PFD31_DATA lfs )
564 LONG lRet;
565 HWND hWnd = lfs->hwnd;
566 LPWSTR pstr;
567 WCHAR diskname[BUFFILE];
569 FD31_StripEditControl(hWnd);
570 lRet = SendDlgItemMessageW(hWnd, cmb2, CB_GETCURSEL, 0, 0L);
571 if (lRet == LB_ERR)
572 return 0;
573 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
574 SendDlgItemMessageW(hWnd, cmb2, CB_GETLBTEXT, lRet,
575 (LPARAM)pstr);
576 wsprintfW(diskname, FILE_specc, pstr[2]);
577 HeapFree(GetProcessHeap(), 0, pstr);
579 return FD31_Validate( lfs, diskname, cmb2, lRet, TRUE );
582 /***********************************************************************
583 * FD31_FileTypeChange [internal]
584 * called when a new item is picked in the file type combo
586 static LRESULT FD31_FileTypeChange( PFD31_DATA lfs )
588 LONG lRet;
589 WCHAR diskname[BUFFILE];
590 LPWSTR pstr;
592 diskname[0] = 0;
594 lRet = SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETCURSEL, 0, 0);
595 if (lRet == LB_ERR)
596 return TRUE;
597 pstr = (LPWSTR)SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETITEMDATA, lRet, 0);
598 TRACE("Selected filter : %s\n", debugstr_w(pstr));
599 SetDlgItemTextW( lfs->hwnd, edt1, pstr );
601 return FD31_Validate( lfs, NULL, cmb1, lRet, TRUE );
604 /***********************************************************************
605 * FD31_WMCommand [internal]
607 LRESULT FD31_WMCommand(HWND hWnd, LPARAM lParam, UINT notification,
608 UINT control, PFD31_DATA lfs )
610 switch (control)
612 case lst1: /* file list */
613 FD31_StripEditControl(hWnd);
614 if (notification == LBN_DBLCLK)
616 if (FD31_Validate( lfs, NULL, control, 0, FALSE ))
617 EndDialog(hWnd, TRUE);
618 return TRUE;
620 else if (notification == LBN_SELCHANGE)
621 return FD31_FileListSelect( lfs );
622 break;
624 case lst2: /* directory list */
625 FD31_StripEditControl(hWnd);
626 if (notification == LBN_DBLCLK)
627 return FD31_DirListDblClick( lfs );
628 break;
630 case cmb1: /* file type drop list */
631 if (notification == CBN_SELCHANGE)
632 return FD31_FileTypeChange( lfs );
633 break;
635 case chx1:
636 break;
638 case pshHelp:
639 break;
641 case cmb2: /* disk dropdown combo */
642 if (notification == CBN_SELCHANGE)
643 return FD31_DiskChange( lfs );
644 break;
646 case IDOK:
647 TRACE("OK pressed\n");
648 if (FD31_Validate( lfs, NULL, control, 0, FALSE ))
649 EndDialog(hWnd, TRUE);
650 return TRUE;
652 case IDCANCEL:
653 EndDialog(hWnd, FALSE);
654 return TRUE;
656 case IDABORT: /* can be sent by the hook procedure */
657 EndDialog(hWnd, TRUE);
658 return TRUE;
660 return FALSE;
663 /************************************************************************
664 * FD31_MapStringPairsToW [internal]
665 * map string pairs to Unicode
667 static LPWSTR FD31_MapStringPairsToW(LPCSTR strA, UINT size)
669 LPCSTR s;
670 LPWSTR x;
671 unsigned int n, len;
673 s = strA;
674 while (*s)
675 s = s+strlen(s)+1;
676 s++;
677 n = s + 1 - strA; /* Don't forget the other \0 */
678 if (n < size) n = size;
680 len = MultiByteToWideChar( CP_ACP, 0, strA, n, NULL, 0 );
681 x = HeapAlloc(GetProcessHeap(),0, len * sizeof(WCHAR));
682 MultiByteToWideChar( CP_ACP, 0, strA, n, x, len );
683 return x;
687 /************************************************************************
688 * FD31_DupToW [internal]
689 * duplicates an Ansi string to unicode, with a buffer size
691 static LPWSTR FD31_DupToW(LPCSTR str, DWORD size)
693 LPWSTR strW = NULL;
694 if (str && (size > 0))
696 strW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
697 if (strW) MultiByteToWideChar( CP_ACP, 0, str, -1, strW, size );
699 return strW;
702 /************************************************************************
703 * FD31_MapOfnStructA [internal]
704 * map a 32 bits Ansi structure to an Unicode one
706 void FD31_MapOfnStructA(LPOPENFILENAMEA ofnA, LPOPENFILENAMEW ofnW, BOOL open)
708 LPCSTR str;
709 UNICODE_STRING usBuffer;
711 ofnW->lStructSize = sizeof(OPENFILENAMEW);
712 ofnW->hwndOwner = ofnA->hwndOwner;
713 ofnW->hInstance = ofnA->hInstance;
714 if (ofnA->lpstrFilter)
715 ofnW->lpstrFilter = FD31_MapStringPairsToW(ofnA->lpstrFilter, 0);
717 if ((ofnA->lpstrCustomFilter) && (*(ofnA->lpstrCustomFilter)))
718 ofnW->lpstrCustomFilter = FD31_MapStringPairsToW(ofnA->lpstrCustomFilter, ofnA->nMaxCustFilter);
719 ofnW->nMaxCustFilter = ofnA->nMaxCustFilter;
720 ofnW->nFilterIndex = ofnA->nFilterIndex;
721 ofnW->nMaxFile = ofnA->nMaxFile;
722 ofnW->lpstrFile = FD31_DupToW(ofnA->lpstrFile, ofnW->nMaxFile);
723 ofnW->nMaxFileTitle = ofnA->nMaxFileTitle;
724 ofnW->lpstrFileTitle = FD31_DupToW(ofnA->lpstrFileTitle, ofnW->nMaxFileTitle);
725 if (ofnA->lpstrInitialDir)
727 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpstrInitialDir);
728 ofnW->lpstrInitialDir = usBuffer.Buffer;
730 if (ofnA->lpstrTitle)
731 str = ofnA->lpstrTitle;
732 else
733 /* Allocates default title (FIXME : get it from resource) */
734 str = open ? "Open File" : "Save as";
735 RtlCreateUnicodeStringFromAsciiz (&usBuffer,str);
736 ofnW->lpstrTitle = usBuffer.Buffer;
737 ofnW->Flags = ofnA->Flags;
738 ofnW->nFileOffset = ofnA->nFileOffset;
739 ofnW->nFileExtension = ofnA->nFileExtension;
740 ofnW->lpstrDefExt = FD31_DupToW(ofnA->lpstrDefExt, 3);
741 if ((ofnA->Flags & OFN_ENABLETEMPLATE) && (ofnA->lpTemplateName))
743 if (HIWORD(ofnA->lpTemplateName))
745 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpTemplateName);
746 ofnW->lpTemplateName = usBuffer.Buffer;
748 else /* numbered resource */
749 ofnW->lpTemplateName = (LPWSTR) ofnA->lpTemplateName;
754 /************************************************************************
755 * FD31_FreeOfnW [internal]
756 * Undo all allocations done by FD31_MapOfnStructA
758 void FD31_FreeOfnW(LPOPENFILENAMEW ofnW)
760 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrFilter);
761 HeapFree(GetProcessHeap(), 0, ofnW->lpstrCustomFilter);
762 HeapFree(GetProcessHeap(), 0, ofnW->lpstrFile);
763 HeapFree(GetProcessHeap(), 0, ofnW->lpstrFileTitle);
764 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrInitialDir);
765 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrTitle);
766 if ((ofnW->lpTemplateName) && (HIWORD(ofnW->lpTemplateName)))
767 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpTemplateName);
770 /************************************************************************
771 * FD31_DestroyPrivate [internal]
772 * destroys the private object
774 void FD31_DestroyPrivate(PFD31_DATA lfs)
776 HWND hwnd;
777 if (!lfs) return;
778 hwnd = lfs->hwnd;
779 TRACE("destroying private allocation %p\n", lfs);
780 lfs->callbacks->Destroy(lfs);
781 HeapFree(GetProcessHeap(), 0, lfs);
782 RemovePropA(hwnd, FD31_OFN_PROP);
785 /************************************************************************
786 * FD31_AllocPrivate [internal]
787 * allocate a private object to hold 32 bits Unicode
788 * structure that will be used throughtout the calls, while
789 * keeping available the original structures and a few variables
790 * On entry : type = dialog procedure type (16,32A,32W)
791 * dlgType = dialog type (open or save)
793 PFD31_DATA FD31_AllocPrivate(LPARAM lParam, UINT dlgType,
794 PFD31_CALLBACKS callbacks, DWORD data)
796 PFD31_DATA lfs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FD31_DATA));
798 TRACE("alloc private buf %p\n", lfs);
799 if (!lfs) return NULL;
800 lfs->hook = FALSE;
801 lfs->lParam = lParam;
802 lfs->open = (dlgType == OPEN_DIALOG);
803 lfs->callbacks = callbacks;
804 if (! lfs->callbacks->Init(lParam, lfs, data))
806 FD31_DestroyPrivate(lfs);
807 return NULL;
809 lfs->lbselchstring = RegisterWindowMessageA(LBSELCHSTRINGA);
810 lfs->fileokstring = RegisterWindowMessageA(FILEOKSTRINGA);
812 return lfs;
815 /***********************************************************************
816 * FD31_WMInitDialog [internal]
819 LONG FD31_WMInitDialog(HWND hWnd, WPARAM wParam, LPARAM lParam)
821 int i, n;
822 WCHAR tmpstr[BUFFILE];
823 LPWSTR pstr, old_pstr;
824 LPOPENFILENAMEW ofn;
825 PFD31_DATA lfs = (PFD31_DATA) lParam;
827 if (!lfs) return FALSE;
828 SetPropA(hWnd, FD31_OFN_PROP, (HANDLE)lfs);
829 lfs->hwnd = hWnd;
830 ofn = &lfs->ofnW;
832 TRACE("flags=%lx initialdir=%s\n", ofn->Flags, debugstr_w(ofn->lpstrInitialDir));
834 SetWindowTextW( hWnd, ofn->lpstrTitle );
835 /* read custom filter information */
836 if (ofn->lpstrCustomFilter)
838 pstr = ofn->lpstrCustomFilter;
839 n = 0;
840 TRACE("lpstrCustomFilter = %p\n", pstr);
841 while(*pstr)
843 old_pstr = pstr;
844 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
845 (LPARAM)(ofn->lpstrCustomFilter) + n );
846 n += lstrlenW(pstr) + 1;
847 pstr += lstrlenW(pstr) + 1;
848 TRACE("add str=%s associated to %s\n",
849 debugstr_w(old_pstr), debugstr_w(pstr));
850 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
851 n += lstrlenW(pstr) + 1;
852 pstr += lstrlenW(pstr) + 1;
855 /* read filter information */
856 if (ofn->lpstrFilter) {
857 pstr = (LPWSTR) ofn->lpstrFilter;
858 n = 0;
859 while(*pstr) {
860 old_pstr = pstr;
861 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
862 (LPARAM)(ofn->lpstrFilter + n) );
863 n += lstrlenW(pstr) + 1;
864 pstr += lstrlenW(pstr) + 1;
865 TRACE("add str=%s associated to %s\n",
866 debugstr_w(old_pstr), debugstr_w(pstr));
867 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
868 n += lstrlenW(pstr) + 1;
869 pstr += lstrlenW(pstr) + 1;
872 /* set default filter */
873 if (ofn->nFilterIndex == 0 && ofn->lpstrCustomFilter == NULL)
874 ofn->nFilterIndex = 1;
875 SendDlgItemMessageW(hWnd, cmb1, CB_SETCURSEL, ofn->nFilterIndex - 1, 0);
876 lstrcpynW(tmpstr, FD31_GetFileType(ofn->lpstrCustomFilter,
877 (LPWSTR)ofn->lpstrFilter, ofn->nFilterIndex - 1),BUFFILE);
878 TRACE("nFilterIndex = %ld, SetText of edt1 to %s\n",
879 ofn->nFilterIndex, debugstr_w(tmpstr));
880 SetDlgItemTextW( hWnd, edt1, tmpstr );
881 /* get drive list */
882 *tmpstr = 0;
883 DlgDirListComboBoxW(hWnd, tmpstr, cmb2, 0, DDL_DRIVES | DDL_EXCLUSIVE);
884 /* read initial directory */
885 /* FIXME: Note that this is now very version-specific (See MSDN description of
886 * the OPENFILENAME structure). For example under 2000/XP any path in the
887 * lpstrFile overrides the lpstrInitialDir, but not under 95/98/ME
889 if (ofn->lpstrInitialDir != NULL)
891 int len;
892 lstrcpynW(tmpstr, ofn->lpstrInitialDir, 511);
893 len = lstrlenW(tmpstr);
894 if (len > 0 && tmpstr[len-1] != '\\' && tmpstr[len-1] != ':') {
895 tmpstr[len]='\\';
896 tmpstr[len+1]='\0';
899 else
900 *tmpstr = 0;
901 if (!FD31_ScanDir(hWnd, tmpstr)) {
902 *tmpstr = 0;
903 if (!FD31_ScanDir(hWnd, tmpstr))
904 WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr));
906 /* select current drive in combo 2, omit missing drives */
908 char dir[MAX_PATH];
909 char str[4] = "a:\\";
910 GetCurrentDirectoryA( sizeof(dir), dir );
911 for(i = 0, n = -1; i < 26; i++)
913 str[0] = 'a' + i;
914 if (GetDriveTypeA(str) > DRIVE_NO_ROOT_DIR) n++;
915 if (toupper(str[0]) == toupper(dir[0])) break;
918 SendDlgItemMessageW(hWnd, cmb2, CB_SETCURSEL, n, 0);
919 if (!(ofn->Flags & OFN_SHOWHELP))
920 ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
921 if (ofn->Flags & OFN_HIDEREADONLY)
922 ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
923 if (lfs->hook)
924 return (BOOL) FD31_CallWindowProc(lfs, WM_INITDIALOG, wParam, lfs->lParam);
925 return TRUE;
928 int FD31_GetFldrHeight(void)
930 return fldrHeight;