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
24 WINE_DEFAULT_DEBUG_CHANNEL(commdlg
);
27 #define BUFFILEALLOC 512 * sizeof(WCHAR)
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;
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;
42 /***********************************************************************
43 * FD31_Init [internal]
47 static BOOL initialized
= 0;
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)
59 ERR("Error loading icons !\n");
67 /***********************************************************************
68 * FD31_StripEditControl [internal]
69 * Strip pathnames off the contents of the edit control.
71 static void FD31_StripEditControl(HWND hwnd
)
73 WCHAR temp
[BUFFILE
], *cp
;
75 GetDlgItemTextW( hwnd
, edt1
, temp
, sizeof(temp
)/sizeof(WCHAR
));
76 cp
= strrchrW(temp
, '\\');
80 cp
= strrchrW(temp
, ':');
84 /* FIXME: shouldn't we do something with the result here? ;-) */
87 /***********************************************************************
88 * FD31_CallWindowProc [internal]
90 * Call the appropriate hook
92 BOOL
FD31_CallWindowProc(PFD31_DATA lfs
, UINT wMsg
, WPARAM wParam
,
95 return lfs
->callbacks
->CWP(lfs
, wMsg
, wParam
, lParam
);
98 /***********************************************************************
99 * FD31_ScanDir [internal]
101 static BOOL
FD31_ScanDir(HWND hWnd
, LPWSTR newPath
)
103 WCHAR buffer
[BUFFILE
];
106 HCURSOR hCursorWait
, oldCursor
;
108 TRACE("Trying to change to %s\n", debugstr_w(newPath
));
109 if ( newPath
[0] && !SetCurrentDirectoryW( newPath
))
111 lstrcpynW(buffer
, newPath
, sizeof(buffer
)/sizeof(WCHAR
));
113 /* get the list of spec files */
114 GetDlgItemTextW(hWnd
, edt1
, buffer
, sizeof(buffer
)/sizeof(WCHAR
));
116 hCursorWait
= LoadCursorA(0, (LPSTR
)IDC_WAIT
);
117 oldCursor
= SetCursor(hCursorWait
);
120 if ((hdlg
= GetDlgItem(hWnd
, lst1
)) != 0) {
121 WCHAR
* scptr
; /* ptr on semi-colon */
122 WCHAR
* filter
= buffer
;
124 TRACE("Using filter %s\n", debugstr_w(filter
));
125 SendMessageW(hdlg
, LB_RESETCONTENT
, 0, 0);
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
)
133 if (scptr
) *scptr
= ';';
134 filter
= (scptr
) ? (scptr
+ 1) : 0;
138 /* list of directories */
139 strcpyW(buffer
, FILE_star
);
141 if ((hdlgDir
= GetDlgItem(hWnd
, lst2
)) != 0) {
142 lRet
= DlgDirListW(hWnd
, buffer
, lst2
, stc1
, DDL_EXCLUSIVE
| DDL_DIRECTORY
);
144 SetCursor(oldCursor
);
148 /***********************************************************************
149 * FD31_GetFileType [internal]
152 static LPWSTR
FD31_GetFileType(LPWSTR cfptr
, LPWSTR fptr
, WORD index
)
157 for ( ;(n
= lstrlenW(cfptr
)) != 0; i
++)
162 cfptr
+= lstrlenW(cfptr
) + 1;
165 for ( ;(n
= lstrlenW(fptr
)) != 0; i
++)
170 fptr
+= lstrlenW(fptr
) + 1;
172 return (LPWSTR
) FILE_star
; /* FIXME */
175 /***********************************************************************
176 * FD31_WMDrawItem [internal]
178 LONG
FD31_WMDrawItem(HWND hWnd
, WPARAM wParam
, LPARAM lParam
,
179 int savedlg
, LPDRAWITEMSTRUCT lpdis
)
183 COLORREF oldText
= 0, oldBk
= 0;
185 if (lpdis
->CtlType
== ODT_LISTBOX
&& lpdis
->CtlID
== lst1
)
187 if (!(str
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
))) return FALSE
;
188 SendMessageW(lpdis
->hwndItem
, LB_GETTEXT
, lpdis
->itemID
,
191 if ((lpdis
->itemState
& ODS_SELECTED
) && !savedlg
)
193 oldBk
= SetBkColor( lpdis
->hDC
, GetSysColor( COLOR_HIGHLIGHT
) );
194 oldText
= SetTextColor( lpdis
->hDC
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
197 SetTextColor(lpdis
->hDC
,GetSysColor(COLOR_GRAYTEXT
) );
199 ExtTextOutW(lpdis
->hDC
, lpdis
->rcItem
.left
+ 1,
200 lpdis
->rcItem
.top
+ 1, ETO_OPAQUE
| ETO_CLIPPED
,
201 &(lpdis
->rcItem
), str
, lstrlenW(str
), NULL
);
203 if (lpdis
->itemState
& ODS_SELECTED
)
204 DrawFocusRect( lpdis
->hDC
, &(lpdis
->rcItem
) );
206 if ((lpdis
->itemState
& ODS_SELECTED
) && !savedlg
)
208 SetBkColor( lpdis
->hDC
, oldBk
);
209 SetTextColor( lpdis
->hDC
, oldText
);
211 HeapFree(GetProcessHeap(), 0, str
);
215 if (lpdis
->CtlType
== ODT_LISTBOX
&& lpdis
->CtlID
== lst2
)
217 if (!(str
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
)))
219 SendMessageW(lpdis
->hwndItem
, LB_GETTEXT
, lpdis
->itemID
,
222 if (lpdis
->itemState
& ODS_SELECTED
)
224 oldBk
= SetBkColor( lpdis
->hDC
, GetSysColor( COLOR_HIGHLIGHT
) );
225 oldText
= SetTextColor( lpdis
->hDC
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
227 ExtTextOutW(lpdis
->hDC
, lpdis
->rcItem
.left
+ fldrWidth
,
228 lpdis
->rcItem
.top
+ 1, ETO_OPAQUE
| ETO_CLIPPED
,
229 &(lpdis
->rcItem
), str
, lstrlenW(str
), NULL
);
231 if (lpdis
->itemState
& ODS_SELECTED
)
232 DrawFocusRect( lpdis
->hDC
, &(lpdis
->rcItem
) );
234 if (lpdis
->itemState
& ODS_SELECTED
)
236 SetBkColor( lpdis
->hDC
, oldBk
);
237 SetTextColor( lpdis
->hDC
, oldText
);
239 DrawIcon(lpdis
->hDC
, lpdis
->rcItem
.left
, lpdis
->rcItem
.top
, hFolder
);
240 HeapFree(GetProcessHeap(), 0, str
);
243 if (lpdis
->CtlType
== ODT_COMBOBOX
&& lpdis
->CtlID
== cmb2
)
246 if (!(str
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
)))
248 SendMessageW(lpdis
->hwndItem
, CB_GETLBTEXT
, lpdis
->itemID
,
250 root
[0] += str
[2] - 'a';
251 switch(GetDriveTypeA(root
))
253 case DRIVE_REMOVABLE
: hIcon
= hFloppy
; break;
254 case DRIVE_CDROM
: hIcon
= hCDRom
; break;
255 case DRIVE_REMOTE
: hIcon
= hNet
; break;
257 default: hIcon
= hHDisk
; break;
259 if (lpdis
->itemState
& ODS_SELECTED
)
261 oldBk
= SetBkColor( lpdis
->hDC
, GetSysColor( COLOR_HIGHLIGHT
) );
262 oldText
= SetTextColor( lpdis
->hDC
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
264 ExtTextOutW(lpdis
->hDC
, lpdis
->rcItem
.left
+ fldrWidth
,
265 lpdis
->rcItem
.top
+ 1, ETO_OPAQUE
| ETO_CLIPPED
,
266 &(lpdis
->rcItem
), str
, lstrlenW(str
), NULL
);
268 if (lpdis
->itemState
& ODS_SELECTED
)
270 SetBkColor( lpdis
->hDC
, oldBk
);
271 SetTextColor( lpdis
->hDC
, oldText
);
273 DrawIcon(lpdis
->hDC
, lpdis
->rcItem
.left
, lpdis
->rcItem
.top
, hIcon
);
274 HeapFree(GetProcessHeap(), 0, str
);
280 /***********************************************************************
281 * FD31_UpdateResult [internal]
282 * update the displayed file name (with path)
284 static void FD31_UpdateResult(PFD31_DATA lfs
, WCHAR
*tmpstr
)
287 LPOPENFILENAMEW ofnW
= lfs
->ofnW
;
288 WCHAR tmpstr2
[BUFFILE
];
291 TRACE("%s\n", debugstr_w(tmpstr
));
292 if(ofnW
->Flags
& OFN_NOVALIDATE
)
295 GetCurrentDirectoryW(BUFFILE
, tmpstr2
);
296 lenstr2
= strlenW(tmpstr2
);
298 tmpstr2
[lenstr2
++]='\\';
299 lstrcpynW(tmpstr2
+lenstr2
, tmpstr
, BUFFILE
-lenstr2
);
301 lstrcpynW(ofnW
->lpstrFile
, tmpstr2
, ofnW
->nMaxFile
);
303 /* set filename offset */
304 p
= PathFindFileNameW(ofnW
->lpstrFile
);
305 ofnW
->nFileOffset
= (p
- ofnW
->lpstrFile
);
307 /* set extension offset */
308 p
= PathFindExtensionW(ofnW
->lpstrFile
);
309 ofnW
->nFileExtension
= (*p
) ? (p
- ofnW
->lpstrFile
) + 1 : 0;
311 TRACE("file %s, file offset %d, ext offset %d\n",
312 debugstr_w(ofnW
->lpstrFile
), ofnW
->nFileOffset
, ofnW
->nFileExtension
);
314 /* update the real client structures if any */
315 lfs
->callbacks
->UpdateResult(lfs
);
318 /***********************************************************************
319 * FD31_UpdateFileTitle [internal]
320 * update the displayed file name (without path)
322 static void FD31_UpdateFileTitle(PFD31_DATA lfs
)
325 LPOPENFILENAMEW ofnW
= lfs
->ofnW
;
326 if (ofnW
->lpstrFileTitle
!= NULL
)
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
);
335 /***********************************************************************
336 * FD31_DirListDblClick [internal]
338 static LRESULT
FD31_DirListDblClick( PFD31_DATA lfs
)
341 HWND hWnd
= lfs
->hwnd
;
343 WCHAR tmpstr
[BUFFILE
];
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
,
351 strcpyW( tmpstr
, pstr
);
352 HeapFree(GetProcessHeap(), 0, pstr
);
353 /* get the selected directory in tmpstr */
354 if (tmpstr
[0] == '[')
356 tmpstr
[lstrlenW(tmpstr
) - 1] = 0;
357 strcpyW(tmpstr
,tmpstr
+1);
359 strcatW(tmpstr
, FILE_bslash
);
361 FD31_ScanDir(hWnd
, tmpstr
);
365 if (FD31_CallWindowProc(lfs
, lfs
->lbselchstring
, lst2
,
366 MAKELONG(lRet
,CD_LBSELCHANGE
)))
372 /***********************************************************************
373 * FD31_FileListSelect [internal]
374 * called when a new item is picked in the file list
376 static LRESULT
FD31_FileListSelect( PFD31_DATA lfs
)
379 HWND hWnd
= lfs
->hwnd
;
382 lRet
= lfs
->callbacks
->SendLbGetCurSel(lfs
);
386 /* set the edit control to the choosen file */
387 if ((pstr
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
)))
389 SendDlgItemMessageW(hWnd
, lst1
, LB_GETTEXT
, lRet
,
391 SetDlgItemTextW( hWnd
, edt1
, pstr
);
392 HeapFree(GetProcessHeap(), 0, pstr
);
396 FD31_CallWindowProc(lfs
, lfs
->lbselchstring
, lst1
,
397 MAKELONG(lRet
,CD_LBSELCHANGE
));
399 /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD,
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.
409 static LRESULT
FD31_TestPath( PFD31_DATA lfs
, LPWSTR path
)
411 HWND hWnd
= lfs
->hwnd
;
412 LPWSTR pBeginFileName
, pstr2
;
413 WCHAR tmpstr2
[BUFFILE
];
415 pBeginFileName
= strrchrW(path
, '\\');
416 if (pBeginFileName
== NULL
)
417 pBeginFileName
= strrchrW(path
, ':');
419 if (strchrW(path
,'*') != NULL
|| strchrW(path
,'?') != NULL
)
421 /* edit control contains wildcards */
422 if (pBeginFileName
!= NULL
)
424 lstrcpynW(tmpstr2
, pBeginFileName
+ 1, BUFFILE
);
425 *(pBeginFileName
+ 1) = 0;
429 strcpyW(tmpstr2
, path
);
430 if(!(lfs
->ofnW
->Flags
& OFN_NOVALIDATE
))
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
;
440 /* no wildcards, we might have a directory or a filename */
441 /* try appending a wildcard and reading the directory */
443 pstr2
= path
+ lstrlenW(path
);
444 if (pBeginFileName
== NULL
|| *(pBeginFileName
+ 1) != 0)
445 strcatW(path
, FILE_bslash
);
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 */
451 /* if not, this must be a filename */
453 *pstr2
= 0; /* remove the wildcard added before */
455 if (pBeginFileName
!= NULL
)
457 /* strip off the pathname */
459 SetDlgItemTextW( hWnd
, edt1
, pBeginFileName
+ 1 );
461 lstrcpynW(tmpstr2
, pBeginFileName
+ 1, sizeof(tmpstr2
)/sizeof(WCHAR
) );
462 /* Should we MessageBox() if this fails? */
463 if (!FD31_ScanDir(hWnd
, path
))
467 strcpyW(path
, tmpstr2
);
470 SetDlgItemTextW( hWnd
, edt1
, path
);
474 /***********************************************************************
475 * FD31_Validate [internal]
476 * called on: click Ok button, Enter in edit, DoubleClick in file list
478 static LRESULT
FD31_Validate( PFD31_DATA lfs
, LPWSTR path
, UINT control
, INT itemIndex
,
482 HWND hWnd
= lfs
->hwnd
;
483 OPENFILENAMEW ofnsav
;
484 LPOPENFILENAMEW ofnW
= lfs
->ofnW
;
485 WCHAR filename
[BUFFILE
];
487 ofnsav
= *ofnW
; /* for later restoring */
489 /* get current file name */
491 lstrcpynW(filename
, path
, sizeof(filename
)/sizeof(WCHAR
));
493 GetDlgItemTextW( hWnd
, edt1
, filename
, sizeof(filename
)/sizeof(WCHAR
));
495 TRACE("got filename = %s\n", debugstr_w(filename
));
496 /* if we did not click in file list to get there */
499 if (!FD31_TestPath( lfs
, filename
) )
502 FD31_UpdateResult(lfs
, filename
);
505 { /* called internally after a change in a combo */
508 FD31_CallWindowProc(lfs
, lfs
->lbselchstring
, control
,
509 MAKELONG(itemIndex
,CD_LBSELCHANGE
));
514 FD31_UpdateFileTitle(lfs
);
517 lRet
= (BOOL
)FD31_CallWindowProc(lfs
, lfs
->fileokstring
,
521 *ofnW
= ofnsav
; /* restore old state */
525 if ((ofnW
->Flags
& OFN_ALLOWMULTISELECT
) && (ofnW
->Flags
& OFN_EXPLORER
))
529 LPWSTR str
= (LPWSTR
)ofnW
->lpstrFile
;
530 LPWSTR ptr
= strrchrW(str
, '\\');
531 str
[lstrlenW(str
) + 1] = '\0';
538 /***********************************************************************
539 * FD31_DiskChange [internal]
540 * called when a new item is picked in the disk selection combo
542 static LRESULT
FD31_DiskChange( PFD31_DATA lfs
)
545 HWND hWnd
= lfs
->hwnd
;
547 WCHAR diskname
[BUFFILE
];
549 FD31_StripEditControl(hWnd
);
550 lRet
= SendDlgItemMessageW(hWnd
, cmb2
, CB_GETCURSEL
, 0, 0L);
553 pstr
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
);
554 SendDlgItemMessageW(hWnd
, cmb2
, CB_GETLBTEXT
, lRet
,
556 wsprintfW(diskname
, FILE_specc
, pstr
[2]);
557 HeapFree(GetProcessHeap(), 0, pstr
);
559 return FD31_Validate( lfs
, diskname
, cmb2
, lRet
, TRUE
);
562 /***********************************************************************
563 * FD31_FileTypeChange [internal]
564 * called when a new item is picked in the file type combo
566 static LRESULT
FD31_FileTypeChange( PFD31_DATA lfs
)
571 lRet
= SendDlgItemMessageW(lfs
->hwnd
, cmb1
, CB_GETCURSEL
, 0, 0);
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
);
578 return FD31_Validate( lfs
, NULL
, cmb1
, lRet
, TRUE
);
581 /***********************************************************************
582 * FD31_WMCommand [internal]
584 LRESULT
FD31_WMCommand(HWND hWnd
, LPARAM lParam
, UINT notification
,
585 UINT control
, PFD31_DATA lfs
)
589 case lst1
: /* file list */
590 FD31_StripEditControl(hWnd
);
591 if (notification
== LBN_DBLCLK
)
593 return SendMessageW(hWnd
, WM_COMMAND
, IDOK
, 0);
595 else if (notification
== LBN_SELCHANGE
)
596 return FD31_FileListSelect( lfs
);
599 case lst2
: /* directory list */
600 FD31_StripEditControl(hWnd
);
601 if (notification
== LBN_DBLCLK
)
602 return FD31_DirListDblClick( lfs
);
605 case cmb1
: /* file type drop list */
606 if (notification
== CBN_SELCHANGE
)
607 return FD31_FileTypeChange( lfs
);
616 case cmb2
: /* disk dropdown combo */
617 if (notification
== CBN_SELCHANGE
)
618 return FD31_DiskChange( lfs
);
622 TRACE("OK pressed\n");
623 if (FD31_Validate( lfs
, NULL
, control
, 0, FALSE
))
624 EndDialog(hWnd
, TRUE
);
628 EndDialog(hWnd
, FALSE
);
631 case IDABORT
: /* can be sent by the hook procedure */
632 EndDialog(hWnd
, TRUE
);
638 /************************************************************************
639 * FD31_MapStringPairsToW [internal]
640 * map string pairs to Unicode
642 static LPWSTR
FD31_MapStringPairsToW(LPCSTR strA
, UINT size
)
652 n
= s
+ 1 - strA
; /* Don't forget the other \0 */
653 if (n
< size
) n
= size
;
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
);
662 /************************************************************************
663 * FD31_DupToW [internal]
664 * duplicates an Ansi string to unicode, with a buffer size
666 static LPWSTR
FD31_DupToW(LPCSTR str
, DWORD size
)
669 if (str
&& (size
> 0))
671 strW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
672 if (strW
) MultiByteToWideChar( CP_ACP
, 0, str
, -1, strW
, size
);
677 /************************************************************************
678 * FD31_MapOfnStructA [internal]
679 * map a 32 bits Ansi structure to an Unicode one
681 void FD31_MapOfnStructA(const LPOPENFILENAMEA ofnA
, LPOPENFILENAMEW ofnW
, BOOL open
)
683 UNICODE_STRING usBuffer
;
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);
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
)
701 RtlCreateUnicodeStringFromAsciiz (&usBuffer
,ofnA
->lpstrInitialDir
);
702 ofnW
->lpstrInitialDir
= usBuffer
.Buffer
;
704 if (ofnA
->lpstrTitle
) {
705 RtlCreateUnicodeStringFromAsciiz (&usBuffer
, ofnA
->lpstrTitle
);
706 ofnW
->lpstrTitle
= usBuffer
.Buffer
;
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
));
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
))
722 if (HIWORD(ofnA
->lpTemplateName
))
724 RtlCreateUnicodeStringFromAsciiz (&usBuffer
,ofnA
->lpTemplateName
);
725 ofnW
->lpTemplateName
= usBuffer
.Buffer
;
727 else /* numbered resource */
728 ofnW
->lpTemplateName
= (LPWSTR
) ofnA
->lpTemplateName
;
733 /************************************************************************
734 * FD31_FreeOfnW [internal]
735 * Undo all allocations done by FD31_MapOfnStructA
737 void FD31_FreeOfnW(LPOPENFILENAMEW ofnW
)
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
);
749 /************************************************************************
750 * FD31_DestroyPrivate [internal]
751 * destroys the private object
753 void FD31_DestroyPrivate(PFD31_DATA lfs
)
758 TRACE("destroying private allocation %p\n", lfs
);
759 lfs
->callbacks
->Destroy(lfs
);
760 HeapFree(GetProcessHeap(), 0, lfs
);
761 RemovePropA(hwnd
, FD31_OFN_PROP
);
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)
772 PFD31_DATA
FD31_AllocPrivate(LPARAM lParam
, UINT dlgType
,
773 PFD31_CALLBACKS callbacks
, DWORD data
)
775 PFD31_DATA lfs
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(FD31_DATA
));
777 TRACE("alloc private buf %p\n", lfs
);
778 if (!lfs
) return NULL
;
780 lfs
->lParam
= lParam
;
781 lfs
->open
= (dlgType
== OPEN_DIALOG
);
782 lfs
->callbacks
= callbacks
;
783 if (! lfs
->callbacks
->Init(lParam
, lfs
, data
))
785 FD31_DestroyPrivate(lfs
);
788 lfs
->lbselchstring
= RegisterWindowMessageA(LBSELCHSTRINGA
);
789 lfs
->fileokstring
= RegisterWindowMessageA(FILEOKSTRINGA
);
794 /***********************************************************************
795 * FD31_WMInitDialog [internal]
798 LONG
FD31_WMInitDialog(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
801 WCHAR tmpstr
[BUFFILE
];
802 LPWSTR pstr
, old_pstr
;
804 PFD31_DATA lfs
= (PFD31_DATA
) lParam
;
806 if (!lfs
) return FALSE
;
807 SetPropA(hWnd
, FD31_OFN_PROP
, (HANDLE
)lfs
);
811 TRACE("flags=%lx initialdir=%s\n", ofn
->Flags
, debugstr_w(ofn
->lpstrInitialDir
));
813 SetWindowTextW( hWnd
, ofn
->lpstrTitle
);
814 /* read custom filter information */
815 if (ofn
->lpstrCustomFilter
)
817 pstr
= ofn
->lpstrCustomFilter
;
819 TRACE("lpstrCustomFilter = %p\n", 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;
834 /* read filter information */
835 if (ofn
->lpstrFilter
) {
836 pstr
= (LPWSTR
) ofn
->lpstrFilter
;
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;
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
);
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
868 if (ofn
->lpstrInitialDir
!= NULL
)
871 lstrcpynW(tmpstr
, ofn
->lpstrInitialDir
, 511);
872 len
= lstrlenW(tmpstr
);
873 if (len
> 0 && tmpstr
[len
-1] != '\\' && tmpstr
[len
-1] != ':') {
880 if (!FD31_ScanDir(hWnd
, tmpstr
)) {
882 if (!FD31_ScanDir(hWnd
, tmpstr
))
883 WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr
));
885 /* select current drive in combo 2, omit missing drives */
888 char str
[4] = "a:\\";
889 GetCurrentDirectoryA( sizeof(dir
), dir
);
890 for(i
= 0, n
= -1; i
< 26; i
++)
893 if (GetDriveTypeA(str
) > DRIVE_NO_ROOT_DIR
) n
++;
894 if (toupper(str
[0]) == toupper(dir
[0])) break;
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
);
903 return (BOOL
) FD31_CallWindowProc(lfs
, WM_INITDIALOG
, wParam
, lfs
->lParam
);
907 int FD31_GetFldrHeight(void)