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,
112 return lfs->callbacks->CWP(lfs, wMsg, wParam, lParam);
115 /***********************************************************************
116 * FD31_ScanDir [internal]
118 static BOOL FD31_ScanDir(HWND hWnd, LPCWSTR newPath)
120 WCHAR buffer[BUFFILE];
123 HCURSOR hCursorWait, oldCursor;
125 TRACE("Trying to change to %s\n", debugstr_w(newPath));
126 if ( newPath[0] && !SetCurrentDirectoryW( newPath ))
128 lstrcpynW(buffer, newPath, sizeof(buffer)/sizeof(WCHAR));
130 /* get the list of spec files */
131 GetDlgItemTextW(hWnd, edt1, buffer, sizeof(buffer)/sizeof(WCHAR));
133 hCursorWait = LoadCursorA(0, (LPSTR)IDC_WAIT);
134 oldCursor = SetCursor(hCursorWait);
137 if ((hdlg = GetDlgItem(hWnd, lst1)) != 0) {
138 WCHAR* scptr; /* ptr on semi-colon */
139 WCHAR* filter = buffer;
141 TRACE("Using filter %s\n", debugstr_w(filter));
142 SendMessageW(hdlg, LB_RESETCONTENT, 0, 0);
144 scptr = strchrW(filter, ';');
145 if (scptr) *scptr = 0;
146 while (*filter == ' ') filter++;
147 TRACE("Using file spec %s\n", debugstr_w(filter));
148 if (SendMessageW(hdlg, LB_DIR, 0, (LPARAM)filter) == LB_ERR)
150 if (scptr) *scptr = ';';
151 filter = (scptr) ? (scptr + 1) : 0;
155 /* list of directories */
156 strcpyW(buffer, FILE_star);
158 if ((hdlgDir = GetDlgItem(hWnd, lst2)) != 0) {
159 lRet = DlgDirListW(hWnd, buffer, lst2, stc1, DDL_EXCLUSIVE | DDL_DIRECTORY);
161 SetCursor(oldCursor);
165 /***********************************************************************
166 * FD31_GetFileType [internal]
169 static LPCWSTR FD31_GetFileType(LPCWSTR cfptr, LPCWSTR fptr, const WORD index)
174 for ( ;(n = lstrlenW(cfptr)) != 0; i++)
179 cfptr += lstrlenW(cfptr) + 1;
182 for ( ;(n = lstrlenW(fptr)) != 0; i++)
187 fptr += lstrlenW(fptr) + 1;
189 return FILE_star; /* FIXME */
192 /***********************************************************************
193 * FD31_WMDrawItem [internal]
195 LONG FD31_WMDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam,
196 int savedlg, const DRAWITEMSTRUCT *lpdis)
200 COLORREF oldText = 0, oldBk = 0;
202 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst1)
204 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC))) return FALSE;
205 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
208 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
210 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
211 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
214 SetTextColor(lpdis->hDC,GetSysColor(COLOR_GRAYTEXT) );
216 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + 1,
217 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
218 &(lpdis->rcItem), str, lstrlenW(str), NULL);
220 if (lpdis->itemState & ODS_SELECTED)
221 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
223 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
225 SetBkColor( lpdis->hDC, oldBk );
226 SetTextColor( lpdis->hDC, oldText );
228 HeapFree(GetProcessHeap(), 0, str);
232 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst2)
234 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
236 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
239 if (lpdis->itemState & ODS_SELECTED)
241 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
242 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
244 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
245 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
246 &(lpdis->rcItem), str, lstrlenW(str), NULL);
248 if (lpdis->itemState & ODS_SELECTED)
249 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
251 if (lpdis->itemState & ODS_SELECTED)
253 SetBkColor( lpdis->hDC, oldBk );
254 SetTextColor( lpdis->hDC, oldText );
256 DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hFolder);
257 HeapFree(GetProcessHeap(), 0, str);
260 if (lpdis->CtlType == ODT_COMBOBOX && lpdis->CtlID == cmb2)
263 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
265 SendMessageW(lpdis->hwndItem, CB_GETLBTEXT, lpdis->itemID,
267 root[0] += str[2] - 'a';
268 switch(GetDriveTypeA(root))
270 case DRIVE_REMOVABLE: hIcon = hFloppy; break;
271 case DRIVE_CDROM: hIcon = hCDRom; break;
272 case DRIVE_REMOTE: hIcon = hNet; break;
274 default: hIcon = hHDisk; break;
276 if (lpdis->itemState & ODS_SELECTED)
278 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
279 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
281 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
282 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
283 &(lpdis->rcItem), str, lstrlenW(str), NULL);
285 if (lpdis->itemState & ODS_SELECTED)
287 SetBkColor( lpdis->hDC, oldBk );
288 SetTextColor( lpdis->hDC, oldText );
290 DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hIcon);
291 HeapFree(GetProcessHeap(), 0, str);
297 /***********************************************************************
298 * FD31_UpdateResult [internal]
299 * update the displayed file name (with path)
301 static void FD31_UpdateResult(const FD31_DATA *lfs, const WCHAR *tmpstr)
304 LPOPENFILENAMEW ofnW = lfs->ofnW;
305 WCHAR tmpstr2[BUFFILE];
308 TRACE("%s\n", debugstr_w(tmpstr));
309 if(ofnW->Flags & OFN_NOVALIDATE)
312 GetCurrentDirectoryW(BUFFILE, tmpstr2);
313 lenstr2 = strlenW(tmpstr2);
315 tmpstr2[lenstr2++]='\\';
316 lstrcpynW(tmpstr2+lenstr2, tmpstr, BUFFILE-lenstr2);
318 lstrcpynW(ofnW->lpstrFile, tmpstr2, ofnW->nMaxFile);
320 /* set filename offset */
321 p = PathFindFileNameW(ofnW->lpstrFile);
322 ofnW->nFileOffset = (p - ofnW->lpstrFile);
324 /* set extension offset */
325 p = PathFindExtensionW(ofnW->lpstrFile);
326 ofnW->nFileExtension = (*p) ? (p - ofnW->lpstrFile) + 1 : 0;
328 TRACE("file %s, file offset %d, ext offset %d\n",
329 debugstr_w(ofnW->lpstrFile), ofnW->nFileOffset, ofnW->nFileExtension);
331 /* update the real client structures if any */
332 lfs->callbacks->UpdateResult(lfs);
335 /***********************************************************************
336 * FD31_UpdateFileTitle [internal]
337 * update the displayed file name (without path)
339 static void FD31_UpdateFileTitle(const FD31_DATA *lfs)
342 LPOPENFILENAMEW ofnW = lfs->ofnW;
343 if (ofnW->lpstrFileTitle != NULL)
345 lRet = SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
346 SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETTEXT, lRet,
347 (LPARAM)ofnW->lpstrFileTitle );
348 lfs->callbacks->UpdateFileTitle(lfs);
352 /***********************************************************************
353 * FD31_DirListDblClick [internal]
355 static LRESULT FD31_DirListDblClick( const FD31_DATA *lfs )
358 HWND hWnd = lfs->hwnd;
360 WCHAR tmpstr[BUFFILE];
362 /* get the raw string (with brackets) */
363 lRet = SendDlgItemMessageW(hWnd, lst2, LB_GETCURSEL, 0, 0);
364 if (lRet == LB_ERR) return TRUE;
365 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
366 SendDlgItemMessageW(hWnd, lst2, LB_GETTEXT, lRet,
368 strcpyW( tmpstr, pstr );
369 HeapFree(GetProcessHeap(), 0, pstr);
370 /* get the selected directory in tmpstr */
371 if (tmpstr[0] == '[')
373 tmpstr[lstrlenW(tmpstr) - 1] = 0;
374 strcpyW(tmpstr,tmpstr+1);
376 strcatW(tmpstr, FILE_bslash);
378 FD31_ScanDir(hWnd, tmpstr);
382 if (FD31_CallWindowProc(lfs, lfs->lbselchstring, lst2,
383 MAKELONG(lRet,CD_LBSELCHANGE)))
389 /***********************************************************************
390 * FD31_FileListSelect [internal]
391 * called when a new item is picked in the file list
393 static LRESULT FD31_FileListSelect( const FD31_DATA *lfs )
396 HWND hWnd = lfs->hwnd;
399 lRet = lfs->callbacks->SendLbGetCurSel(lfs);
403 /* set the edit control to the choosen file */
404 if ((pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
406 SendDlgItemMessageW(hWnd, lst1, LB_GETTEXT, lRet,
408 SetDlgItemTextW( hWnd, edt1, pstr );
409 HeapFree(GetProcessHeap(), 0, pstr);
413 FD31_CallWindowProc(lfs, lfs->lbselchstring, lst1,
414 MAKELONG(lRet,CD_LBSELCHANGE));
416 /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD,
421 /***********************************************************************
422 * FD31_TestPath [internal]
423 * before accepting the file name, test if it includes wild cards
424 * tries to scan the directory and returns TRUE if no error.
426 static LRESULT FD31_TestPath( const FD31_DATA *lfs, LPWSTR path )
428 HWND hWnd = lfs->hwnd;
429 LPWSTR pBeginFileName, pstr2;
430 WCHAR tmpstr2[BUFFILE];
432 pBeginFileName = strrchrW(path, '\\');
433 if (pBeginFileName == NULL)
434 pBeginFileName = strrchrW(path, ':');
436 if (strchrW(path,'*') != NULL || strchrW(path,'?') != NULL)
438 /* edit control contains wildcards */
439 if (pBeginFileName != NULL)
441 lstrcpynW(tmpstr2, pBeginFileName + 1, BUFFILE);
442 *(pBeginFileName + 1) = 0;
446 strcpyW(tmpstr2, path);
447 if(!(lfs->ofnW->Flags & OFN_NOVALIDATE))
451 TRACE("path=%s, tmpstr2=%s\n", debugstr_w(path), debugstr_w(tmpstr2));
452 SetDlgItemTextW( hWnd, edt1, tmpstr2 );
453 FD31_ScanDir(hWnd, path);
454 return (lfs->ofnW->Flags & OFN_NOVALIDATE) ? TRUE : FALSE;
457 /* no wildcards, we might have a directory or a filename */
458 /* try appending a wildcard and reading the directory */
460 pstr2 = path + lstrlenW(path);
461 if (pBeginFileName == NULL || *(pBeginFileName + 1) != 0)
462 strcatW(path, FILE_bslash);
464 /* if ScanDir succeeds, we have changed the directory */
465 if (FD31_ScanDir(hWnd, path))
466 return FALSE; /* and path is not a valid file name */
468 /* if not, this must be a filename */
470 *pstr2 = 0; /* remove the wildcard added before */
472 if (pBeginFileName != NULL)
474 /* strip off the pathname */
476 SetDlgItemTextW( hWnd, edt1, pBeginFileName + 1 );
478 lstrcpynW(tmpstr2, pBeginFileName + 1, sizeof(tmpstr2)/sizeof(WCHAR) );
479 /* Should we MessageBox() if this fails? */
480 if (!FD31_ScanDir(hWnd, path))
484 strcpyW(path, tmpstr2);
487 SetDlgItemTextW( hWnd, edt1, path );
491 /***********************************************************************
492 * FD31_Validate [internal]
493 * called on: click Ok button, Enter in edit, DoubleClick in file list
495 static LRESULT FD31_Validate( const FD31_DATA *lfs, LPCWSTR path, UINT control, INT itemIndex,
499 HWND hWnd = lfs->hwnd;
500 OPENFILENAMEW ofnsav;
501 LPOPENFILENAMEW ofnW = lfs->ofnW;
502 WCHAR filename[BUFFILE];
504 ofnsav = *ofnW; /* for later restoring */
506 /* get current file name */
508 lstrcpynW(filename, path, sizeof(filename)/sizeof(WCHAR));
510 GetDlgItemTextW( hWnd, edt1, filename, sizeof(filename)/sizeof(WCHAR));
512 TRACE("got filename = %s\n", debugstr_w(filename));
513 /* if we did not click in file list to get there */
516 if (!FD31_TestPath( lfs, filename) )
519 FD31_UpdateResult(lfs, filename);
522 { /* called internally after a change in a combo */
525 FD31_CallWindowProc(lfs, lfs->lbselchstring, control,
526 MAKELONG(itemIndex,CD_LBSELCHANGE));
531 FD31_UpdateFileTitle(lfs);
534 lRet = FD31_CallWindowProc(lfs, lfs->fileokstring,
538 *ofnW = ofnsav; /* restore old state */
542 if ((ofnW->Flags & OFN_ALLOWMULTISELECT) && (ofnW->Flags & OFN_EXPLORER))
546 LPWSTR str = ofnW->lpstrFile;
547 LPWSTR ptr = strrchrW(str, '\\');
548 str[lstrlenW(str) + 1] = '\0';
555 /***********************************************************************
556 * FD31_DiskChange [internal]
557 * called when a new item is picked in the disk selection combo
559 static LRESULT FD31_DiskChange( const FD31_DATA *lfs )
562 HWND hWnd = lfs->hwnd;
564 WCHAR diskname[BUFFILE];
566 FD31_StripEditControl(hWnd);
567 lRet = SendDlgItemMessageW(hWnd, cmb2, CB_GETCURSEL, 0, 0L);
570 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
571 SendDlgItemMessageW(hWnd, cmb2, CB_GETLBTEXT, lRet,
573 wsprintfW(diskname, FILE_specc, pstr[2]);
574 HeapFree(GetProcessHeap(), 0, pstr);
576 return FD31_Validate( lfs, diskname, cmb2, lRet, TRUE );
579 /***********************************************************************
580 * FD31_FileTypeChange [internal]
581 * called when a new item is picked in the file type combo
583 static LRESULT FD31_FileTypeChange( const FD31_DATA *lfs )
588 lRet = SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETCURSEL, 0, 0);
591 pstr = (LPWSTR)SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETITEMDATA, lRet, 0);
592 TRACE("Selected filter : %s\n", debugstr_w(pstr));
593 SetDlgItemTextW( lfs->hwnd, edt1, pstr );
595 return FD31_Validate( lfs, NULL, cmb1, lRet, TRUE );
598 /***********************************************************************
599 * FD31_WMCommand [internal]
601 LRESULT FD31_WMCommand(HWND hWnd, LPARAM lParam, UINT notification,
602 UINT control, const FD31_DATA *lfs )
606 case lst1: /* file list */
607 FD31_StripEditControl(hWnd);
608 if (notification == LBN_DBLCLK)
610 return SendMessageW(hWnd, WM_COMMAND, IDOK, 0);
612 else if (notification == LBN_SELCHANGE)
613 return FD31_FileListSelect( lfs );
616 case lst2: /* directory list */
617 FD31_StripEditControl(hWnd);
618 if (notification == LBN_DBLCLK)
619 return FD31_DirListDblClick( lfs );
622 case cmb1: /* file type drop list */
623 if (notification == CBN_SELCHANGE)
624 return FD31_FileTypeChange( lfs );
633 case cmb2: /* disk dropdown combo */
634 if (notification == CBN_SELCHANGE)
635 return FD31_DiskChange( lfs );
639 TRACE("OK pressed\n");
640 if (FD31_Validate( lfs, NULL, control, 0, FALSE ))
641 EndDialog(hWnd, TRUE);
645 EndDialog(hWnd, FALSE);
648 case IDABORT: /* can be sent by the hook procedure */
649 EndDialog(hWnd, TRUE);
655 /************************************************************************
656 * FD31_MapStringPairsToW [internal]
657 * map string pairs to Unicode
659 static LPWSTR FD31_MapStringPairsToW(LPCSTR strA, UINT size)
669 n = s + 1 - strA; /* Don't forget the other \0 */
670 if (n < size) n = size;
672 len = MultiByteToWideChar( CP_ACP, 0, strA, n, NULL, 0 );
673 x = HeapAlloc(GetProcessHeap(),0, len * sizeof(WCHAR));
674 MultiByteToWideChar( CP_ACP, 0, strA, n, x, len );
679 /************************************************************************
680 * FD31_DupToW [internal]
681 * duplicates an Ansi string to unicode, with a buffer size
683 static LPWSTR FD31_DupToW(LPCSTR str, DWORD size)
686 if (str && (size > 0))
688 strW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
689 if (strW) MultiByteToWideChar( CP_ACP, 0, str, -1, strW, size );
694 /************************************************************************
695 * FD31_MapOfnStructA [internal]
696 * map a 32 bits Ansi structure to an Unicode one
698 void FD31_MapOfnStructA(const OPENFILENAMEA *ofnA, LPOPENFILENAMEW ofnW, BOOL open)
700 UNICODE_STRING usBuffer;
702 ofnW->lStructSize = sizeof(OPENFILENAMEW);
703 ofnW->hwndOwner = ofnA->hwndOwner;
704 ofnW->hInstance = ofnA->hInstance;
705 if (ofnA->lpstrFilter)
706 ofnW->lpstrFilter = FD31_MapStringPairsToW(ofnA->lpstrFilter, 0);
708 if ((ofnA->lpstrCustomFilter) && (*(ofnA->lpstrCustomFilter)))
709 ofnW->lpstrCustomFilter = FD31_MapStringPairsToW(ofnA->lpstrCustomFilter, ofnA->nMaxCustFilter);
710 ofnW->nMaxCustFilter = ofnA->nMaxCustFilter;
711 ofnW->nFilterIndex = ofnA->nFilterIndex;
712 ofnW->nMaxFile = ofnA->nMaxFile;
713 ofnW->lpstrFile = FD31_DupToW(ofnA->lpstrFile, ofnW->nMaxFile);
714 ofnW->nMaxFileTitle = ofnA->nMaxFileTitle;
715 ofnW->lpstrFileTitle = FD31_DupToW(ofnA->lpstrFileTitle, ofnW->nMaxFileTitle);
716 if (ofnA->lpstrInitialDir)
718 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpstrInitialDir);
719 ofnW->lpstrInitialDir = usBuffer.Buffer;
721 if (ofnA->lpstrTitle) {
722 RtlCreateUnicodeStringFromAsciiz (&usBuffer, ofnA->lpstrTitle);
723 ofnW->lpstrTitle = usBuffer.Buffer;
728 LoadStringW(COMDLG32_hInstance, open ? IDS_OPEN_FILE : IDS_SAVE_AS,
729 buf, sizeof(buf)/sizeof(WCHAR));
730 len = lstrlenW(buf)+1;
731 title_tmp = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
732 memcpy(title_tmp, buf, len * sizeof(WCHAR));
733 ofnW->lpstrTitle = title_tmp;
735 ofnW->Flags = ofnA->Flags;
736 ofnW->nFileOffset = ofnA->nFileOffset;
737 ofnW->nFileExtension = ofnA->nFileExtension;
738 ofnW->lpstrDefExt = FD31_DupToW(ofnA->lpstrDefExt, 3);
739 if ((ofnA->Flags & OFN_ENABLETEMPLATE) && (ofnA->lpTemplateName))
741 if (HIWORD(ofnA->lpTemplateName))
743 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpTemplateName);
744 ofnW->lpTemplateName = usBuffer.Buffer;
746 else /* numbered resource */
747 ofnW->lpTemplateName = (LPCWSTR) ofnA->lpTemplateName;
752 /************************************************************************
753 * FD31_FreeOfnW [internal]
754 * Undo all allocations done by FD31_MapOfnStructA
756 void FD31_FreeOfnW(OPENFILENAMEW *ofnW)
758 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrFilter);
759 HeapFree(GetProcessHeap(), 0, ofnW->lpstrCustomFilter);
760 HeapFree(GetProcessHeap(), 0, ofnW->lpstrFile);
761 HeapFree(GetProcessHeap(), 0, ofnW->lpstrFileTitle);
762 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrInitialDir);
763 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrTitle);
764 if (HIWORD(ofnW->lpTemplateName))
765 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpTemplateName);
768 /************************************************************************
769 * FD31_DestroyPrivate [internal]
770 * destroys the private object
772 void FD31_DestroyPrivate(PFD31_DATA lfs)
777 TRACE("destroying private allocation %p\n", lfs);
778 lfs->callbacks->Destroy(lfs);
779 HeapFree(GetProcessHeap(), 0, lfs);
780 RemovePropA(hwnd, FD31_OFN_PROP);
783 /************************************************************************
784 * FD31_AllocPrivate [internal]
785 * allocate a private object to hold 32 bits Unicode
786 * structure that will be used throughout the calls, while
787 * keeping available the original structures and a few variables
788 * On entry : type = dialog procedure type (16,32A,32W)
789 * dlgType = dialog type (open or save)
791 PFD31_DATA FD31_AllocPrivate(LPARAM lParam, UINT dlgType,
792 PFD31_CALLBACKS callbacks, DWORD data)
794 PFD31_DATA lfs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FD31_DATA));
796 TRACE("alloc private buf %p\n", lfs);
797 if (!lfs) return NULL;
799 lfs->lParam = lParam;
800 lfs->open = (dlgType == OPEN_DIALOG);
801 lfs->callbacks = callbacks;
802 if (! lfs->callbacks->Init(lParam, lfs, data))
804 FD31_DestroyPrivate(lfs);
807 lfs->lbselchstring = RegisterWindowMessageA(LBSELCHSTRINGA);
808 lfs->fileokstring = RegisterWindowMessageA(FILEOKSTRINGA);
813 /***********************************************************************
814 * FD31_WMInitDialog [internal]
817 LONG FD31_WMInitDialog(HWND hWnd, WPARAM wParam, LPARAM lParam)
820 WCHAR tmpstr[BUFFILE];
821 LPWSTR pstr, old_pstr;
823 PFD31_DATA lfs = (PFD31_DATA) lParam;
825 if (!lfs) return FALSE;
826 SetPropA(hWnd, FD31_OFN_PROP, (HANDLE)lfs);
830 TRACE("flags=%x initialdir=%s\n", ofn->Flags, debugstr_w(ofn->lpstrInitialDir));
832 SetWindowTextW( hWnd, ofn->lpstrTitle );
833 /* read custom filter information */
834 if (ofn->lpstrCustomFilter)
836 pstr = ofn->lpstrCustomFilter;
838 TRACE("lpstrCustomFilter = %p\n", pstr);
842 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
843 (LPARAM)(ofn->lpstrCustomFilter) + n );
844 n += lstrlenW(pstr) + 1;
845 pstr += lstrlenW(pstr) + 1;
846 TRACE("add str=%s associated to %s\n",
847 debugstr_w(old_pstr), debugstr_w(pstr));
848 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
849 n += lstrlenW(pstr) + 1;
850 pstr += lstrlenW(pstr) + 1;
853 /* read filter information */
854 if (ofn->lpstrFilter) {
855 pstr = (LPWSTR) ofn->lpstrFilter;
859 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
860 (LPARAM)(ofn->lpstrFilter + n) );
861 n += lstrlenW(pstr) + 1;
862 pstr += lstrlenW(pstr) + 1;
863 TRACE("add str=%s associated to %s\n",
864 debugstr_w(old_pstr), debugstr_w(pstr));
865 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
866 n += lstrlenW(pstr) + 1;
867 pstr += lstrlenW(pstr) + 1;
870 /* set default filter */
871 if (ofn->nFilterIndex == 0 && ofn->lpstrCustomFilter == NULL)
872 ofn->nFilterIndex = 1;
873 SendDlgItemMessageW(hWnd, cmb1, CB_SETCURSEL, ofn->nFilterIndex - 1, 0);
874 lstrcpynW(tmpstr, FD31_GetFileType(ofn->lpstrCustomFilter,
875 ofn->lpstrFilter, ofn->nFilterIndex - 1),BUFFILE);
876 TRACE("nFilterIndex = %d, SetText of edt1 to %s\n",
877 ofn->nFilterIndex, debugstr_w(tmpstr));
878 SetDlgItemTextW( hWnd, edt1, tmpstr );
881 DlgDirListComboBoxW(hWnd, tmpstr, cmb2, 0, DDL_DRIVES | DDL_EXCLUSIVE);
882 /* read initial directory */
883 /* FIXME: Note that this is now very version-specific (See MSDN description of
884 * the OPENFILENAME structure). For example under 2000/XP any path in the
885 * lpstrFile overrides the lpstrInitialDir, but not under 95/98/ME
887 if (ofn->lpstrInitialDir != NULL)
890 lstrcpynW(tmpstr, ofn->lpstrInitialDir, 511);
891 len = lstrlenW(tmpstr);
892 if (len > 0 && tmpstr[len-1] != '\\' && tmpstr[len-1] != ':') {
899 if (!FD31_ScanDir(hWnd, tmpstr)) {
901 if (!FD31_ScanDir(hWnd, tmpstr))
902 WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr));
904 /* select current drive in combo 2, omit missing drives */
907 char str[4] = "a:\\";
908 GetCurrentDirectoryA( sizeof(dir), dir );
909 for(i = 0, n = -1; i < 26; i++)
912 if (GetDriveTypeA(str) > DRIVE_NO_ROOT_DIR) n++;
913 if (toupper(str[0]) == toupper(dir[0])) break;
916 SendDlgItemMessageW(hWnd, cmb2, CB_SETCURSEL, n, 0);
917 if (!(ofn->Flags & OFN_SHOWHELP))
918 ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
919 if (ofn->Flags & OFN_HIDEREADONLY)
920 ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
922 return FD31_CallWindowProc(lfs, WM_INITDIALOG, wParam, lfs->lParam);
926 int FD31_GetFldrHeight(void)