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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
41 #include "filedlg31.h"
44 #define BUFFILEALLOC 512 * sizeof(WCHAR)
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;
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;
59 /***********************************************************************
60 * FD31_Init [internal]
64 static BOOL initialized = 0;
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)
76 ERR("Error loading icons !\n");
84 /***********************************************************************
85 * FD31_StripEditControl [internal]
86 * Strip pathnames off the contents of the edit control.
88 static void FD31_StripEditControl(HWND hwnd)
90 WCHAR temp[BUFFILE], *cp;
92 GetDlgItemTextW( hwnd, edt1, temp, sizeof(temp)/sizeof(WCHAR));
93 cp = strrchrW(temp, '\\');
97 cp = strrchrW(temp, ':');
101 /* FIXME: shouldn't we do something with the result here? ;-) */
104 /***********************************************************************
105 * FD31_CallWindowProc [internal]
107 * Call the appropriate hook
109 BOOL FD31_CallWindowProc(const FD31_DATA *lfs, UINT wMsg, WPARAM wParam,
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);
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);
132 /***********************************************************************
133 * FD31_GetFileType [internal]
135 static LPCWSTR FD31_GetFileType(LPCWSTR cfptr, LPCWSTR fptr, const WORD index)
140 for ( ;(n = lstrlenW(cfptr)) != 0; i++)
145 cfptr += lstrlenW(cfptr) + 1;
148 for ( ;(n = lstrlenW(fptr)) != 0; i++)
153 fptr += lstrlenW(fptr) + 1;
155 return FILE_star; /* FIXME */
158 /***********************************************************************
159 * FD31_ScanDir [internal]
161 static BOOL FD31_ScanDir(const OPENFILENAMEW *ofn, HWND hWnd, LPCWSTR newPath)
163 WCHAR buffer[BUFFILE];
166 HCURSOR hCursorWait, oldCursor;
168 TRACE("Trying to change to %s\n", debugstr_w(newPath));
169 if ( newPath[0] && !SetCurrentDirectoryW( newPath ))
172 /* get the list of spec files */
173 lstrcpynW(buffer, FD31_GetFileType(ofn->lpstrCustomFilter,
174 ofn->lpstrFilter, ofn->nFilterIndex - 1), BUFFILE);
176 hCursorWait = LoadCursorA(0, (LPSTR)IDC_WAIT);
177 oldCursor = SetCursor(hCursorWait);
180 if ((hdlg = GetDlgItem(hWnd, lst1)) != 0) {
181 WCHAR* scptr; /* ptr on semi-colon */
182 WCHAR* filter = buffer;
184 TRACE("Using filter %s\n", debugstr_w(filter));
185 SendMessageW(hdlg, LB_RESETCONTENT, 0, 0);
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;
197 /* list of directories */
198 strcpyW(buffer, FILE_star);
200 if (GetDlgItem(hWnd, lst2) != 0) {
201 lRet = DlgDirListW(hWnd, buffer, lst2, stc1, DDL_EXCLUSIVE | DDL_DIRECTORY);
203 SetCursor(oldCursor);
207 /***********************************************************************
208 * FD31_WMDrawItem [internal]
210 LONG FD31_WMDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam,
211 int savedlg, const DRAWITEMSTRUCT *lpdis)
215 COLORREF oldText = 0, oldBk = 0;
217 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst1)
219 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC))) return FALSE;
220 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
223 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
225 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
226 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
229 SetTextColor(lpdis->hDC,GetSysColor(COLOR_GRAYTEXT) );
231 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + 1,
232 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
233 &(lpdis->rcItem), str, lstrlenW(str), NULL);
235 if (lpdis->itemState & ODS_SELECTED)
236 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
238 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
240 SetBkColor( lpdis->hDC, oldBk );
241 SetTextColor( lpdis->hDC, oldText );
243 HeapFree(GetProcessHeap(), 0, str);
247 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst2)
249 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
251 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
254 if (lpdis->itemState & ODS_SELECTED)
256 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
257 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
259 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
260 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
261 &(lpdis->rcItem), str, lstrlenW(str), NULL);
263 if (lpdis->itemState & ODS_SELECTED)
264 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
266 if (lpdis->itemState & ODS_SELECTED)
268 SetBkColor( lpdis->hDC, oldBk );
269 SetTextColor( lpdis->hDC, oldText );
271 DrawIconEx( lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hFolder, 16, 16, 0, 0, DI_NORMAL );
272 HeapFree(GetProcessHeap(), 0, str);
275 if (lpdis->CtlType == ODT_COMBOBOX && lpdis->CtlID == cmb2)
278 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
280 SendMessageW(lpdis->hwndItem, CB_GETLBTEXT, lpdis->itemID,
282 root[0] += str[2] - 'a';
283 switch(GetDriveTypeA(root))
285 case DRIVE_REMOVABLE: hIcon = hFloppy; break;
286 case DRIVE_CDROM: hIcon = hCDRom; break;
287 case DRIVE_REMOTE: hIcon = hNet; break;
289 default: hIcon = hHDisk; break;
291 if (lpdis->itemState & ODS_SELECTED)
293 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
294 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
296 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
297 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
298 &(lpdis->rcItem), str, lstrlenW(str), NULL);
300 if (lpdis->itemState & ODS_SELECTED)
302 SetBkColor( lpdis->hDC, oldBk );
303 SetTextColor( lpdis->hDC, oldText );
305 DrawIconEx( lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hIcon, 16, 16, 0, 0, DI_NORMAL );
306 HeapFree(GetProcessHeap(), 0, str);
312 /***********************************************************************
313 * FD31_UpdateResult [internal]
314 * update the displayed file name (with path)
316 static void FD31_UpdateResult(const FD31_DATA *lfs, const WCHAR *tmpstr)
319 LPOPENFILENAMEW ofnW = lfs->ofnW;
320 LPOPENFILENAMEA ofnA = lfs->ofnA;
321 WCHAR tmpstr2[BUFFILE];
324 TRACE("%s\n", debugstr_w(tmpstr));
325 if(ofnW->Flags & OFN_NOVALIDATE)
328 GetCurrentDirectoryW(BUFFILE, tmpstr2);
329 lenstr2 = strlenW(tmpstr2);
331 tmpstr2[lenstr2++]='\\';
332 lstrcpynW(tmpstr2+lenstr2, tmpstr, BUFFILE-lenstr2);
333 if (!ofnW->lpstrFile)
336 lstrcpynW(ofnW->lpstrFile, tmpstr2, ofnW->nMaxFile);
338 /* set filename offset */
339 p = PathFindFileNameW(ofnW->lpstrFile);
340 ofnW->nFileOffset = (p - ofnW->lpstrFile);
342 /* set extension offset */
343 p = PathFindExtensionW(ofnW->lpstrFile);
344 ofnW->nFileExtension = (*p) ? (p - ofnW->lpstrFile) + 1 : 0;
346 TRACE("file %s, file offset %d, ext offset %d\n",
347 debugstr_w(ofnW->lpstrFile), ofnW->nFileOffset, ofnW->nFileExtension);
349 /* update the real client structures if any */
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;
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);
363 /* set extension offset */
364 lpszTemp = PathFindExtensionA(ofnA->lpstrFile);
365 ofnA->nFileExtension = (*lpszTemp) ? (lpszTemp - ofnA->lpstrFile) + 1 : 0;
369 /***********************************************************************
370 * FD31_UpdateFileTitle [internal]
371 * update the displayed file name (without path)
373 static void FD31_UpdateFileTitle(const FD31_DATA *lfs)
376 LPOPENFILENAMEW ofnW = lfs->ofnW;
377 LPOPENFILENAMEA ofnA = lfs->ofnA;
379 if (ofnW->lpstrFileTitle != NULL)
381 lRet = SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
382 SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETTEXT, lRet,
383 (LPARAM)ofnW->lpstrFileTitle );
386 if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFileTitle, -1,
387 ofnA->lpstrFileTitle, ofnA->nMaxFileTitle, NULL, NULL ))
388 ofnA->lpstrFileTitle[ofnA->nMaxFileTitle-1] = 0;
393 /***********************************************************************
394 * FD31_DirListDblClick [internal]
396 static LRESULT FD31_DirListDblClick( const FD31_DATA *lfs )
399 HWND hWnd = lfs->hwnd;
401 WCHAR tmpstr[BUFFILE];
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,
409 strcpyW( tmpstr, pstr );
410 HeapFree(GetProcessHeap(), 0, pstr);
411 /* get the selected directory in tmpstr */
412 if (tmpstr[0] == '[')
414 tmpstr[lstrlenW(tmpstr) - 1] = 0;
415 strcpyW(tmpstr,tmpstr+1);
417 strcatW(tmpstr, FILE_bslash);
419 FD31_ScanDir(lfs->ofnW, hWnd, tmpstr);
423 if (FD31_CallWindowProc(lfs, lfs->lbselchstring, lst2,
424 MAKELONG(lRet,CD_LBSELCHANGE)))
430 /***********************************************************************
431 * FD31_FileListSelect [internal]
432 * called when a new item is picked in the file list
434 static LRESULT FD31_FileListSelect( const FD31_DATA *lfs )
437 HWND hWnd = lfs->hwnd;
440 lRet = SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
444 /* set the edit control to the choosen file */
445 if ((pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
447 SendDlgItemMessageW(hWnd, lst1, LB_GETTEXT, lRet,
449 SetDlgItemTextW( hWnd, edt1, pstr );
450 HeapFree(GetProcessHeap(), 0, pstr);
454 FD31_CallWindowProc(lfs, lfs->lbselchstring, lst1,
455 MAKELONG(lRet,CD_LBSELCHANGE));
457 /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD,
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.
467 static LRESULT FD31_TestPath( const FD31_DATA *lfs, LPWSTR path )
469 HWND hWnd = lfs->hwnd;
470 LPWSTR pBeginFileName, pstr2;
471 WCHAR tmpstr2[BUFFILE];
473 pBeginFileName = strrchrW(path, '\\');
474 if (pBeginFileName == NULL)
475 pBeginFileName = strrchrW(path, ':');
477 if (strchrW(path,'*') != NULL || strchrW(path,'?') != NULL)
479 /* edit control contains wildcards */
480 if (pBeginFileName != NULL)
482 lstrcpynW(tmpstr2, pBeginFileName + 1, BUFFILE);
483 *(pBeginFileName + 1) = 0;
487 strcpyW(tmpstr2, path);
488 if(!(lfs->ofnW->Flags & OFN_NOVALIDATE))
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;
498 /* no wildcards, we might have a directory or a filename */
499 /* try appending a wildcard and reading the directory */
501 pstr2 = path + lstrlenW(path);
502 if (pBeginFileName == NULL || *(pBeginFileName + 1) != 0)
503 strcatW(path, FILE_bslash);
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 */
509 /* if not, this must be a filename */
511 *pstr2 = 0; /* remove the wildcard added before */
513 if (pBeginFileName != NULL)
515 /* strip off the pathname */
517 SetDlgItemTextW( hWnd, edt1, pBeginFileName + 1 );
519 lstrcpynW(tmpstr2, pBeginFileName + 1, sizeof(tmpstr2)/sizeof(WCHAR) );
520 /* Should we MessageBox() if this fails? */
521 if (!FD31_ScanDir(lfs->ofnW, hWnd, path))
525 strcpyW(path, tmpstr2);
528 SetDlgItemTextW( hWnd, edt1, path );
532 /***********************************************************************
533 * FD31_Validate [internal]
534 * called on: click Ok button, Enter in edit, DoubleClick in file list
536 static LRESULT FD31_Validate( const FD31_DATA *lfs, LPCWSTR path, UINT control, INT itemIndex,
540 HWND hWnd = lfs->hwnd;
541 OPENFILENAMEW ofnsav;
542 LPOPENFILENAMEW ofnW = lfs->ofnW;
543 WCHAR filename[BUFFILE];
545 ofnsav = *ofnW; /* for later restoring */
547 /* get current file name */
549 lstrcpynW(filename, path, sizeof(filename)/sizeof(WCHAR));
551 GetDlgItemTextW( hWnd, edt1, filename, sizeof(filename)/sizeof(WCHAR));
553 TRACE("got filename = %s\n", debugstr_w(filename));
554 /* if we did not click in file list to get there */
557 if (!FD31_TestPath( lfs, filename) )
560 FD31_UpdateResult(lfs, filename);
563 { /* called internally after a change in a combo */
566 FD31_CallWindowProc(lfs, lfs->lbselchstring, control,
567 MAKELONG(itemIndex,CD_LBSELCHANGE));
572 FD31_UpdateFileTitle(lfs);
575 lRet = FD31_CallWindowProc(lfs, lfs->fileokstring,
579 *ofnW = ofnsav; /* restore old state */
583 if ((ofnW->Flags & OFN_ALLOWMULTISELECT) && (ofnW->Flags & OFN_EXPLORER))
587 LPWSTR str = ofnW->lpstrFile;
588 LPWSTR ptr = strrchrW(str, '\\');
589 str[lstrlenW(str) + 1] = '\0';
596 /***********************************************************************
597 * FD31_DiskChange [internal]
598 * called when a new item is picked in the disk selection combo
600 static LRESULT FD31_DiskChange( const FD31_DATA *lfs )
603 HWND hWnd = lfs->hwnd;
605 WCHAR diskname[BUFFILE];
607 FD31_StripEditControl(hWnd);
608 lRet = SendDlgItemMessageW(hWnd, cmb2, CB_GETCURSEL, 0, 0L);
611 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
612 SendDlgItemMessageW(hWnd, cmb2, CB_GETLBTEXT, lRet,
614 wsprintfW(diskname, FILE_specc, pstr[2]);
615 HeapFree(GetProcessHeap(), 0, pstr);
617 return FD31_Validate( lfs, diskname, cmb2, lRet, TRUE );
620 /***********************************************************************
621 * FD31_FileTypeChange [internal]
622 * called when a new item is picked in the file type combo
624 static LRESULT FD31_FileTypeChange( const FD31_DATA *lfs )
629 lRet = SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETCURSEL, 0, 0);
632 lfs->ofnW->nFilterIndex = lRet + 1;
634 lfs->ofnA->nFilterIndex = lRet + 1;
635 pstr = (LPWSTR)SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETITEMDATA, lRet, 0);
636 TRACE("Selected filter : %s\n", debugstr_w(pstr));
638 return FD31_Validate( lfs, pstr, cmb1, lRet, TRUE );
641 /***********************************************************************
642 * FD31_WMCommand [internal]
644 LRESULT FD31_WMCommand(HWND hWnd, LPARAM lParam, UINT notification,
645 UINT control, const FD31_DATA *lfs )
649 case lst1: /* file list */
650 FD31_StripEditControl(hWnd);
651 if (notification == LBN_DBLCLK)
653 return SendMessageW(hWnd, WM_COMMAND, IDOK, 0);
655 else if (notification == LBN_SELCHANGE)
656 return FD31_FileListSelect( lfs );
659 case lst2: /* directory list */
660 FD31_StripEditControl(hWnd);
661 if (notification == LBN_DBLCLK)
662 return FD31_DirListDblClick( lfs );
665 case cmb1: /* file type drop list */
666 if (notification == CBN_SELCHANGE)
667 return FD31_FileTypeChange( lfs );
676 case cmb2: /* disk dropdown combo */
677 if (notification == CBN_SELCHANGE)
678 return FD31_DiskChange( lfs );
682 TRACE("OK pressed\n");
683 if (FD31_Validate( lfs, NULL, control, 0, FALSE ))
684 EndDialog(hWnd, TRUE);
688 EndDialog(hWnd, FALSE);
691 case IDABORT: /* can be sent by the hook procedure */
692 EndDialog(hWnd, TRUE);
698 /************************************************************************
699 * FD31_MapStringPairsToW [internal]
700 * map string pairs to Unicode
702 static LPWSTR FD31_MapStringPairsToW(LPCSTR strA, UINT size)
712 n = s + 1 - strA; /* Don't forget the other \0 */
713 if (n < size) n = size;
715 len = MultiByteToWideChar( CP_ACP, 0, strA, n, NULL, 0 );
716 x = HeapAlloc(GetProcessHeap(),0, len * sizeof(WCHAR));
717 MultiByteToWideChar( CP_ACP, 0, strA, n, x, len );
722 /************************************************************************
723 * FD31_DupToW [internal]
724 * duplicates an Ansi string to unicode, with a buffer size
726 static LPWSTR FD31_DupToW(LPCSTR str, DWORD size)
729 if (str && (size > 0))
731 strW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
732 if (strW) MultiByteToWideChar( CP_ACP, 0, str, -1, strW, size );
737 /************************************************************************
738 * FD31_MapOfnStructA [internal]
739 * map a 32 bits Ansi structure to a Unicode one
741 static void FD31_MapOfnStructA(const OPENFILENAMEA *ofnA, LPOPENFILENAMEW ofnW, BOOL open)
743 UNICODE_STRING usBuffer;
745 ofnW->lStructSize = sizeof(OPENFILENAMEW);
746 ofnW->hwndOwner = ofnA->hwndOwner;
747 ofnW->hInstance = ofnA->hInstance;
748 if (ofnA->lpstrFilter)
749 ofnW->lpstrFilter = FD31_MapStringPairsToW(ofnA->lpstrFilter, 0);
751 if ((ofnA->lpstrCustomFilter) && (*(ofnA->lpstrCustomFilter)))
752 ofnW->lpstrCustomFilter = FD31_MapStringPairsToW(ofnA->lpstrCustomFilter, ofnA->nMaxCustFilter);
753 ofnW->nMaxCustFilter = ofnA->nMaxCustFilter;
754 ofnW->nFilterIndex = ofnA->nFilterIndex;
755 ofnW->nMaxFile = ofnA->nMaxFile;
756 ofnW->lpstrFile = FD31_DupToW(ofnA->lpstrFile, ofnW->nMaxFile);
757 ofnW->nMaxFileTitle = ofnA->nMaxFileTitle;
758 ofnW->lpstrFileTitle = FD31_DupToW(ofnA->lpstrFileTitle, ofnW->nMaxFileTitle);
759 if (ofnA->lpstrInitialDir)
761 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpstrInitialDir);
762 ofnW->lpstrInitialDir = usBuffer.Buffer;
764 if (ofnA->lpstrTitle) {
765 RtlCreateUnicodeStringFromAsciiz (&usBuffer, ofnA->lpstrTitle);
766 ofnW->lpstrTitle = usBuffer.Buffer;
771 LoadStringW(COMDLG32_hInstance, open ? IDS_OPEN_FILE : IDS_SAVE_AS,
772 buf, sizeof(buf)/sizeof(WCHAR));
773 len = lstrlenW(buf)+1;
774 title_tmp = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
775 memcpy(title_tmp, buf, len * sizeof(WCHAR));
776 ofnW->lpstrTitle = title_tmp;
778 ofnW->Flags = ofnA->Flags;
779 ofnW->nFileOffset = ofnA->nFileOffset;
780 ofnW->nFileExtension = ofnA->nFileExtension;
781 ofnW->lpstrDefExt = FD31_DupToW(ofnA->lpstrDefExt, 3);
782 if ((ofnA->Flags & OFN_ENABLETEMPLATE) && (ofnA->lpTemplateName))
784 if (!IS_INTRESOURCE(ofnA->lpTemplateName))
786 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpTemplateName);
787 ofnW->lpTemplateName = usBuffer.Buffer;
789 else /* numbered resource */
790 ofnW->lpTemplateName = (LPCWSTR) ofnA->lpTemplateName;
795 /************************************************************************
796 * FD31_FreeOfnW [internal]
797 * Undo all allocations done by FD31_MapOfnStructA
799 static void FD31_FreeOfnW(OPENFILENAMEW *ofnW)
801 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrFilter);
802 HeapFree(GetProcessHeap(), 0, ofnW->lpstrCustomFilter);
803 HeapFree(GetProcessHeap(), 0, ofnW->lpstrFile);
804 HeapFree(GetProcessHeap(), 0, ofnW->lpstrFileTitle);
805 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrInitialDir);
806 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrTitle);
807 if (!IS_INTRESOURCE(ofnW->lpTemplateName))
808 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpTemplateName);
811 /************************************************************************
812 * FD31_DestroyPrivate [internal]
813 * destroys the private object
815 void FD31_DestroyPrivate(PFD31_DATA lfs)
820 TRACE("destroying private allocation %p\n", lfs);
822 /* if ofnW has been allocated, have to free everything in it */
825 FD31_FreeOfnW(lfs->ofnW);
826 HeapFree(GetProcessHeap(), 0, lfs->ofnW);
828 HeapFree(GetProcessHeap(), 0, lfs);
829 RemovePropA(hwnd, FD31_OFN_PROP);
832 /************************************************************************
833 * FD31_AllocPrivate [internal]
834 * allocate a private object to hold 32 bits Unicode
835 * structure that will be used throughout the calls, while
836 * keeping available the original structures and a few variables
837 * On entry : type = dialog procedure type (16,32A,32W)
838 * dlgType = dialog type (open or save)
840 PFD31_DATA FD31_AllocPrivate(LPARAM lParam, UINT dlgType, BOOL IsUnicode)
842 PFD31_DATA lfs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FD31_DATA));
844 TRACE("alloc private buf %p\n", lfs);
845 if (!lfs) return NULL;
847 lfs->lParam = lParam;
848 lfs->open = (dlgType == OPEN_DIALOG);
853 lfs->ofnW = (LPOPENFILENAMEW) lParam;
854 if (lfs->ofnW->Flags & OFN_ENABLEHOOK)
855 if (lfs->ofnW->lpfnHook)
860 lfs->ofnA = (LPOPENFILENAMEA) lParam;
861 if (lfs->ofnA->Flags & OFN_ENABLEHOOK)
862 if (lfs->ofnA->lpfnHook)
864 lfs->ofnW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*lfs->ofnW));
865 FD31_MapOfnStructA(lfs->ofnA, lfs->ofnW, lfs->open);
868 if (! FD32_GetTemplate(lfs))
870 FD31_DestroyPrivate(lfs);
873 lfs->lbselchstring = RegisterWindowMessageA(LBSELCHSTRINGA);
874 lfs->fileokstring = RegisterWindowMessageA(FILEOKSTRINGA);
879 /***********************************************************************
880 * FD31_WMInitDialog [internal]
883 LONG FD31_WMInitDialog(HWND hWnd, WPARAM wParam, LPARAM lParam)
886 WCHAR tmpstr[BUFFILE];
887 LPWSTR pstr, old_pstr;
889 PFD31_DATA lfs = (PFD31_DATA) lParam;
891 if (!lfs) return FALSE;
892 SetPropA(hWnd, FD31_OFN_PROP, lfs);
896 TRACE("flags=%x initialdir=%s\n", ofn->Flags, debugstr_w(ofn->lpstrInitialDir));
898 SetWindowTextW( hWnd, ofn->lpstrTitle );
899 /* read custom filter information */
900 if (ofn->lpstrCustomFilter)
902 pstr = ofn->lpstrCustomFilter;
904 TRACE("lpstrCustomFilter = %p\n", pstr);
908 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
909 (LPARAM)(ofn->lpstrCustomFilter) + n );
910 n += lstrlenW(pstr) + 1;
911 pstr += lstrlenW(pstr) + 1;
912 TRACE("add str=%s associated to %s\n",
913 debugstr_w(old_pstr), debugstr_w(pstr));
914 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
915 n += lstrlenW(pstr) + 1;
916 pstr += lstrlenW(pstr) + 1;
919 /* read filter information */
920 if (ofn->lpstrFilter) {
921 pstr = (LPWSTR) ofn->lpstrFilter;
925 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
926 (LPARAM)(ofn->lpstrFilter + n) );
927 n += lstrlenW(pstr) + 1;
928 pstr += lstrlenW(pstr) + 1;
929 TRACE("add str=%s associated to %s\n",
930 debugstr_w(old_pstr), debugstr_w(pstr));
931 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
932 n += lstrlenW(pstr) + 1;
933 pstr += lstrlenW(pstr) + 1;
936 /* set default filter */
937 if (ofn->nFilterIndex == 0 && ofn->lpstrCustomFilter == NULL)
938 ofn->nFilterIndex = 1;
939 SendDlgItemMessageW(hWnd, cmb1, CB_SETCURSEL, ofn->nFilterIndex - 1, 0);
940 if (ofn->lpstrFile && ofn->lpstrFile[0])
942 TRACE( "SetText of edt1 to %s\n", debugstr_w(ofn->lpstrFile) );
943 SetDlgItemTextW( hWnd, edt1, ofn->lpstrFile );
947 lstrcpynW(tmpstr, FD31_GetFileType(ofn->lpstrCustomFilter,
948 ofn->lpstrFilter, ofn->nFilterIndex - 1),BUFFILE);
949 TRACE("nFilterIndex = %d, SetText of edt1 to %s\n",
950 ofn->nFilterIndex, debugstr_w(tmpstr));
951 SetDlgItemTextW( hWnd, edt1, tmpstr );
955 DlgDirListComboBoxW(hWnd, tmpstr, cmb2, 0, DDL_DRIVES | DDL_EXCLUSIVE);
956 /* read initial directory */
957 /* FIXME: Note that this is now very version-specific (See MSDN description of
958 * the OPENFILENAME structure). For example under 2000/XP any path in the
959 * lpstrFile overrides the lpstrInitialDir, but not under 95/98/ME
961 if (ofn->lpstrInitialDir != NULL)
964 lstrcpynW(tmpstr, ofn->lpstrInitialDir, 511);
965 len = lstrlenW(tmpstr);
966 if (len > 0 && tmpstr[len-1] != '\\' && tmpstr[len-1] != ':') {
973 if (!FD31_ScanDir(ofn, hWnd, tmpstr)) {
975 if (!FD31_ScanDir(ofn, hWnd, tmpstr))
976 WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr));
978 /* select current drive in combo 2, omit missing drives */
981 char str[4] = "a:\\";
982 GetCurrentDirectoryA( sizeof(dir), dir );
983 for(i = 0, n = -1; i < 26; i++)
986 if (GetDriveTypeA(str) > DRIVE_NO_ROOT_DIR) n++;
987 if (toupper(str[0]) == toupper(dir[0])) break;
990 SendDlgItemMessageW(hWnd, cmb2, CB_SETCURSEL, n, 0);
991 if (!(ofn->Flags & OFN_SHOWHELP))
992 ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
993 if (ofn->Flags & OFN_HIDEREADONLY)
994 ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
996 return FD31_CallWindowProc(lfs, WM_INITDIALOG, wParam, lfs->lParam);
1000 int FD31_GetFldrHeight(void)