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
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(commdlg
);
46 #include "filedlg31.h"
49 #define BUFFILEALLOC 512 * sizeof(WCHAR)
51 static const WCHAR FILE_star
[] = {'*','.','*', 0};
52 static const WCHAR FILE_bslash
[] = {'\\', 0};
53 static const WCHAR FILE_specc
[] = {'%','c',':', 0};
54 static const int fldrHeight
= 16;
55 static const int fldrWidth
= 20;
57 static HICON hFolder
= 0;
58 static HICON hFolder2
= 0;
59 static HICON hFloppy
= 0;
60 static HICON hHDisk
= 0;
61 static HICON hCDRom
= 0;
62 static HICON hNet
= 0;
64 /***********************************************************************
65 * FD31_Init [internal]
69 static BOOL initialized
= 0;
72 hFolder
= LoadImageA( COMDLG32_hInstance
, "FOLDER", IMAGE_ICON
, 16, 16, LR_SHARED
);
73 hFolder2
= LoadImageA( COMDLG32_hInstance
, "FOLDER2", IMAGE_ICON
, 16, 16, LR_SHARED
);
74 hFloppy
= LoadImageA( COMDLG32_hInstance
, "FLOPPY", IMAGE_ICON
, 16, 16, LR_SHARED
);
75 hHDisk
= LoadImageA( COMDLG32_hInstance
, "HDISK", IMAGE_ICON
, 16, 16, LR_SHARED
);
76 hCDRom
= LoadImageA( COMDLG32_hInstance
, "CDROM", IMAGE_ICON
, 16, 16, LR_SHARED
);
77 hNet
= LoadImageA( COMDLG32_hInstance
, "NETWORK", IMAGE_ICON
, 16, 16, LR_SHARED
);
78 if (hFolder
== 0 || hFolder2
== 0 || hFloppy
== 0 ||
79 hHDisk
== 0 || hCDRom
== 0 || hNet
== 0)
81 ERR("Error loading icons !\n");
89 /***********************************************************************
90 * FD31_StripEditControl [internal]
91 * Strip pathnames off the contents of the edit control.
93 static void FD31_StripEditControl(HWND hwnd
)
95 WCHAR temp
[BUFFILE
], *cp
;
97 GetDlgItemTextW( hwnd
, edt1
, temp
, sizeof(temp
)/sizeof(WCHAR
));
98 cp
= strrchrW(temp
, '\\');
102 cp
= strrchrW(temp
, ':');
106 /* FIXME: shouldn't we do something with the result here? ;-) */
109 /***********************************************************************
110 * FD31_CallWindowProc [internal]
112 * Call the appropriate hook
114 BOOL
FD31_CallWindowProc(PFD31_DATA lfs
, UINT wMsg
, WPARAM wParam
,
117 return lfs
->callbacks
->CWP(lfs
, wMsg
, wParam
, lParam
);
120 /***********************************************************************
121 * FD31_ScanDir [internal]
123 static BOOL
FD31_ScanDir(HWND hWnd
, LPWSTR newPath
)
125 WCHAR buffer
[BUFFILE
];
128 HCURSOR hCursorWait
, oldCursor
;
130 TRACE("Trying to change to %s\n", debugstr_w(newPath
));
131 if ( newPath
[0] && !SetCurrentDirectoryW( newPath
))
133 lstrcpynW(buffer
, newPath
, sizeof(buffer
)/sizeof(WCHAR
));
135 /* get the list of spec files */
136 GetDlgItemTextW(hWnd
, edt1
, buffer
, sizeof(buffer
)/sizeof(WCHAR
));
138 hCursorWait
= LoadCursorA(0, (LPSTR
)IDC_WAIT
);
139 oldCursor
= SetCursor(hCursorWait
);
142 if ((hdlg
= GetDlgItem(hWnd
, lst1
)) != 0) {
143 WCHAR
* scptr
; /* ptr on semi-colon */
144 WCHAR
* filter
= buffer
;
146 TRACE("Using filter %s\n", debugstr_w(filter
));
147 SendMessageW(hdlg
, LB_RESETCONTENT
, 0, 0);
149 scptr
= strchrW(filter
, ';');
150 if (scptr
) *scptr
= 0;
151 while (*filter
== ' ') filter
++;
152 TRACE("Using file spec %s\n", debugstr_w(filter
));
153 if (SendMessageW(hdlg
, LB_DIR
, 0, (LPARAM
)filter
) == LB_ERR
)
155 if (scptr
) *scptr
= ';';
156 filter
= (scptr
) ? (scptr
+ 1) : 0;
160 /* list of directories */
161 strcpyW(buffer
, FILE_star
);
163 if ((hdlgDir
= GetDlgItem(hWnd
, lst2
)) != 0) {
164 lRet
= DlgDirListW(hWnd
, buffer
, lst2
, stc1
, DDL_EXCLUSIVE
| DDL_DIRECTORY
);
166 SetCursor(oldCursor
);
170 /***********************************************************************
171 * FD31_GetFileType [internal]
174 static LPWSTR
FD31_GetFileType(LPWSTR cfptr
, LPWSTR fptr
, WORD index
)
179 for ( ;(n
= lstrlenW(cfptr
)) != 0; i
++)
184 cfptr
+= lstrlenW(cfptr
) + 1;
187 for ( ;(n
= lstrlenW(fptr
)) != 0; i
++)
192 fptr
+= lstrlenW(fptr
) + 1;
194 return (LPWSTR
) FILE_star
; /* FIXME */
197 /***********************************************************************
198 * FD31_WMDrawItem [internal]
200 LONG
FD31_WMDrawItem(HWND hWnd
, WPARAM wParam
, LPARAM lParam
,
201 int savedlg
, LPDRAWITEMSTRUCT lpdis
)
205 COLORREF oldText
= 0, oldBk
= 0;
207 if (lpdis
->CtlType
== ODT_LISTBOX
&& lpdis
->CtlID
== lst1
)
209 if (!(str
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
))) return FALSE
;
210 SendMessageW(lpdis
->hwndItem
, LB_GETTEXT
, lpdis
->itemID
,
213 if ((lpdis
->itemState
& ODS_SELECTED
) && !savedlg
)
215 oldBk
= SetBkColor( lpdis
->hDC
, GetSysColor( COLOR_HIGHLIGHT
) );
216 oldText
= SetTextColor( lpdis
->hDC
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
219 SetTextColor(lpdis
->hDC
,GetSysColor(COLOR_GRAYTEXT
) );
221 ExtTextOutW(lpdis
->hDC
, lpdis
->rcItem
.left
+ 1,
222 lpdis
->rcItem
.top
+ 1, ETO_OPAQUE
| ETO_CLIPPED
,
223 &(lpdis
->rcItem
), str
, lstrlenW(str
), NULL
);
225 if (lpdis
->itemState
& ODS_SELECTED
)
226 DrawFocusRect( lpdis
->hDC
, &(lpdis
->rcItem
) );
228 if ((lpdis
->itemState
& ODS_SELECTED
) && !savedlg
)
230 SetBkColor( lpdis
->hDC
, oldBk
);
231 SetTextColor( lpdis
->hDC
, oldText
);
233 HeapFree(GetProcessHeap(), 0, str
);
237 if (lpdis
->CtlType
== ODT_LISTBOX
&& lpdis
->CtlID
== lst2
)
239 if (!(str
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
)))
241 SendMessageW(lpdis
->hwndItem
, LB_GETTEXT
, lpdis
->itemID
,
244 if (lpdis
->itemState
& ODS_SELECTED
)
246 oldBk
= SetBkColor( lpdis
->hDC
, GetSysColor( COLOR_HIGHLIGHT
) );
247 oldText
= SetTextColor( lpdis
->hDC
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
249 ExtTextOutW(lpdis
->hDC
, lpdis
->rcItem
.left
+ fldrWidth
,
250 lpdis
->rcItem
.top
+ 1, ETO_OPAQUE
| ETO_CLIPPED
,
251 &(lpdis
->rcItem
), str
, lstrlenW(str
), NULL
);
253 if (lpdis
->itemState
& ODS_SELECTED
)
254 DrawFocusRect( lpdis
->hDC
, &(lpdis
->rcItem
) );
256 if (lpdis
->itemState
& ODS_SELECTED
)
258 SetBkColor( lpdis
->hDC
, oldBk
);
259 SetTextColor( lpdis
->hDC
, oldText
);
261 DrawIcon(lpdis
->hDC
, lpdis
->rcItem
.left
, lpdis
->rcItem
.top
, hFolder
);
262 HeapFree(GetProcessHeap(), 0, str
);
265 if (lpdis
->CtlType
== ODT_COMBOBOX
&& lpdis
->CtlID
== cmb2
)
268 if (!(str
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
)))
270 SendMessageW(lpdis
->hwndItem
, CB_GETLBTEXT
, lpdis
->itemID
,
272 root
[0] += str
[2] - 'a';
273 switch(GetDriveTypeA(root
))
275 case DRIVE_REMOVABLE
: hIcon
= hFloppy
; break;
276 case DRIVE_CDROM
: hIcon
= hCDRom
; break;
277 case DRIVE_REMOTE
: hIcon
= hNet
; break;
279 default: hIcon
= hHDisk
; break;
281 if (lpdis
->itemState
& ODS_SELECTED
)
283 oldBk
= SetBkColor( lpdis
->hDC
, GetSysColor( COLOR_HIGHLIGHT
) );
284 oldText
= SetTextColor( lpdis
->hDC
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
286 ExtTextOutW(lpdis
->hDC
, lpdis
->rcItem
.left
+ fldrWidth
,
287 lpdis
->rcItem
.top
+ 1, ETO_OPAQUE
| ETO_CLIPPED
,
288 &(lpdis
->rcItem
), str
, lstrlenW(str
), NULL
);
290 if (lpdis
->itemState
& ODS_SELECTED
)
292 SetBkColor( lpdis
->hDC
, oldBk
);
293 SetTextColor( lpdis
->hDC
, oldText
);
295 DrawIcon(lpdis
->hDC
, lpdis
->rcItem
.left
, lpdis
->rcItem
.top
, hIcon
);
296 HeapFree(GetProcessHeap(), 0, str
);
302 /***********************************************************************
303 * FD31_UpdateResult [internal]
304 * update the displayed file name (with path)
306 void FD31_UpdateResult(PFD31_DATA lfs
, WCHAR
*tmpstr
)
309 LPOPENFILENAMEW ofnW
= lfs
->ofnW
;
310 WCHAR tmpstr2
[BUFFILE
];
313 TRACE("%s\n", debugstr_w(tmpstr
));
314 if(ofnW
->Flags
& OFN_NOVALIDATE
)
317 GetCurrentDirectoryW(BUFFILE
, tmpstr2
);
318 lenstr2
= strlenW(tmpstr2
);
320 tmpstr2
[lenstr2
++]='\\';
321 lstrcpynW(tmpstr2
+lenstr2
, tmpstr
, BUFFILE
-lenstr2
);
323 lstrcpynW(ofnW
->lpstrFile
, tmpstr2
, ofnW
->nMaxFile
);
325 /* set filename offset */
326 p
= PathFindFileNameW(ofnW
->lpstrFile
);
327 ofnW
->nFileOffset
= (p
- ofnW
->lpstrFile
);
329 /* set extension offset */
330 p
= PathFindExtensionW(ofnW
->lpstrFile
);
331 ofnW
->nFileExtension
= (*p
) ? (p
- ofnW
->lpstrFile
) + 1 : 0;
333 TRACE("file %s, file offset %d, ext offset %d\n",
334 debugstr_w(ofnW
->lpstrFile
), ofnW
->nFileOffset
, ofnW
->nFileExtension
);
336 /* update the real client structures if any */
337 lfs
->callbacks
->UpdateResult(lfs
);
340 /***********************************************************************
341 * FD31_UpdateFileTitle [internal]
342 * update the displayed file name (without path)
344 void FD31_UpdateFileTitle(PFD31_DATA lfs
)
347 LPOPENFILENAMEW ofnW
= lfs
->ofnW
;
348 if (ofnW
->lpstrFileTitle
!= NULL
)
350 lRet
= SendDlgItemMessageW(lfs
->hwnd
, lst1
, LB_GETCURSEL
, 0, 0);
351 SendDlgItemMessageW(lfs
->hwnd
, lst1
, LB_GETTEXT
, lRet
,
352 (LPARAM
)ofnW
->lpstrFileTitle
);
353 lfs
->callbacks
->UpdateFileTitle(lfs
);
357 /***********************************************************************
358 * FD31_DirListDblClick [internal]
360 static LRESULT
FD31_DirListDblClick( PFD31_DATA lfs
)
363 HWND hWnd
= lfs
->hwnd
;
365 WCHAR tmpstr
[BUFFILE
];
367 /* get the raw string (with brackets) */
368 lRet
= SendDlgItemMessageW(hWnd
, lst2
, LB_GETCURSEL
, 0, 0);
369 if (lRet
== LB_ERR
) return TRUE
;
370 pstr
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
);
371 SendDlgItemMessageW(hWnd
, lst2
, LB_GETTEXT
, lRet
,
373 strcpyW( tmpstr
, pstr
);
374 HeapFree(GetProcessHeap(), 0, pstr
);
375 /* get the selected directory in tmpstr */
376 if (tmpstr
[0] == '[')
378 tmpstr
[lstrlenW(tmpstr
) - 1] = 0;
379 strcpyW(tmpstr
,tmpstr
+1);
381 strcatW(tmpstr
, FILE_bslash
);
383 FD31_ScanDir(hWnd
, tmpstr
);
387 if (FD31_CallWindowProc(lfs
, lfs
->lbselchstring
, lst2
,
388 MAKELONG(lRet
,CD_LBSELCHANGE
)))
394 /***********************************************************************
395 * FD31_FileListSelect [internal]
396 * called when a new item is picked in the file list
398 static LRESULT
FD31_FileListSelect( PFD31_DATA lfs
)
401 HWND hWnd
= lfs
->hwnd
;
404 lRet
= lfs
->callbacks
->SendLbGetCurSel(lfs
);
408 /* set the edit control to the choosen file */
409 if ((pstr
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
)))
411 SendDlgItemMessageW(hWnd
, lst1
, LB_GETTEXT
, lRet
,
413 SetDlgItemTextW( hWnd
, edt1
, pstr
);
414 HeapFree(GetProcessHeap(), 0, pstr
);
418 FD31_CallWindowProc(lfs
, lfs
->lbselchstring
, lst1
,
419 MAKELONG(lRet
,CD_LBSELCHANGE
));
421 /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD,
426 /***********************************************************************
427 * FD31_TestPath [internal]
428 * before accepting the file name, test if it includes wild cards
429 * tries to scan the directory and returns TRUE if no error.
431 static LRESULT
FD31_TestPath( PFD31_DATA lfs
, LPWSTR path
)
433 HWND hWnd
= lfs
->hwnd
;
434 LPWSTR pBeginFileName
, pstr2
;
435 WCHAR tmpstr2
[BUFFILE
];
437 pBeginFileName
= strrchrW(path
, '\\');
438 if (pBeginFileName
== NULL
)
439 pBeginFileName
= strrchrW(path
, ':');
441 if (strchrW(path
,'*') != NULL
|| strchrW(path
,'?') != NULL
)
443 /* edit control contains wildcards */
444 if (pBeginFileName
!= NULL
)
446 lstrcpynW(tmpstr2
, pBeginFileName
+ 1, BUFFILE
);
447 *(pBeginFileName
+ 1) = 0;
451 strcpyW(tmpstr2
, path
);
452 if(!(lfs
->ofnW
->Flags
& OFN_NOVALIDATE
))
456 TRACE("path=%s, tmpstr2=%s\n", debugstr_w(path
), debugstr_w(tmpstr2
));
457 SetDlgItemTextW( hWnd
, edt1
, tmpstr2
);
458 FD31_ScanDir(hWnd
, path
);
459 return (lfs
->ofnW
->Flags
& OFN_NOVALIDATE
) ? TRUE
: FALSE
;
462 /* no wildcards, we might have a directory or a filename */
463 /* try appending a wildcard and reading the directory */
465 pstr2
= path
+ lstrlenW(path
);
466 if (pBeginFileName
== NULL
|| *(pBeginFileName
+ 1) != 0)
467 strcatW(path
, FILE_bslash
);
469 /* if ScanDir succeeds, we have changed the directory */
470 if (FD31_ScanDir(hWnd
, path
))
471 return FALSE
; /* and path is not a valid file name */
473 /* if not, this must be a filename */
475 *pstr2
= 0; /* remove the wildcard added before */
477 if (pBeginFileName
!= NULL
)
479 /* strip off the pathname */
481 SetDlgItemTextW( hWnd
, edt1
, pBeginFileName
+ 1 );
483 lstrcpynW(tmpstr2
, pBeginFileName
+ 1, sizeof(tmpstr2
)/sizeof(WCHAR
) );
484 /* Should we MessageBox() if this fails? */
485 if (!FD31_ScanDir(hWnd
, path
))
489 strcpyW(path
, tmpstr2
);
492 SetDlgItemTextW( hWnd
, edt1
, path
);
496 /***********************************************************************
497 * FD31_Validate [internal]
498 * called on: click Ok button, Enter in edit, DoubleClick in file list
500 static LRESULT
FD31_Validate( PFD31_DATA lfs
, LPWSTR path
, UINT control
, INT itemIndex
,
504 HWND hWnd
= lfs
->hwnd
;
505 OPENFILENAMEW ofnsav
;
506 LPOPENFILENAMEW ofnW
= lfs
->ofnW
;
507 WCHAR filename
[BUFFILE
];
509 ofnsav
= *ofnW
; /* for later restoring */
511 /* get current file name */
513 lstrcpynW(filename
, path
, sizeof(filename
)/sizeof(WCHAR
));
515 GetDlgItemTextW( hWnd
, edt1
, filename
, sizeof(filename
)/sizeof(WCHAR
));
517 TRACE("got filename = %s\n", debugstr_w(filename
));
518 /* if we did not click in file list to get there */
521 if (!FD31_TestPath( lfs
, filename
) )
524 FD31_UpdateResult(lfs
, filename
);
527 { /* called internally after a change in a combo */
530 FD31_CallWindowProc(lfs
, lfs
->lbselchstring
, control
,
531 MAKELONG(itemIndex
,CD_LBSELCHANGE
));
536 FD31_UpdateFileTitle(lfs
);
539 lRet
= (BOOL
)FD31_CallWindowProc(lfs
, lfs
->fileokstring
,
543 *ofnW
= ofnsav
; /* restore old state */
547 if ((ofnW
->Flags
& OFN_ALLOWMULTISELECT
) && (ofnW
->Flags
& OFN_EXPLORER
))
551 LPWSTR str
= (LPWSTR
)ofnW
->lpstrFile
;
552 LPWSTR ptr
= strrchrW(str
, '\\');
553 str
[lstrlenW(str
) + 1] = '\0';
560 /***********************************************************************
561 * FD31_DiskChange [internal]
562 * called when a new item is picked in the disk selection combo
564 static LRESULT
FD31_DiskChange( PFD31_DATA lfs
)
567 HWND hWnd
= lfs
->hwnd
;
569 WCHAR diskname
[BUFFILE
];
571 FD31_StripEditControl(hWnd
);
572 lRet
= SendDlgItemMessageW(hWnd
, cmb2
, CB_GETCURSEL
, 0, 0L);
575 pstr
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
);
576 SendDlgItemMessageW(hWnd
, cmb2
, CB_GETLBTEXT
, lRet
,
578 wsprintfW(diskname
, FILE_specc
, pstr
[2]);
579 HeapFree(GetProcessHeap(), 0, pstr
);
581 return FD31_Validate( lfs
, diskname
, cmb2
, lRet
, TRUE
);
584 /***********************************************************************
585 * FD31_FileTypeChange [internal]
586 * called when a new item is picked in the file type combo
588 static LRESULT
FD31_FileTypeChange( PFD31_DATA lfs
)
593 lRet
= SendDlgItemMessageW(lfs
->hwnd
, cmb1
, CB_GETCURSEL
, 0, 0);
596 pstr
= (LPWSTR
)SendDlgItemMessageW(lfs
->hwnd
, cmb1
, CB_GETITEMDATA
, lRet
, 0);
597 TRACE("Selected filter : %s\n", debugstr_w(pstr
));
598 SetDlgItemTextW( lfs
->hwnd
, edt1
, pstr
);
600 return FD31_Validate( lfs
, NULL
, cmb1
, lRet
, TRUE
);
603 /***********************************************************************
604 * FD31_WMCommand [internal]
606 LRESULT
FD31_WMCommand(HWND hWnd
, LPARAM lParam
, UINT notification
,
607 UINT control
, PFD31_DATA lfs
)
611 case lst1
: /* file list */
612 FD31_StripEditControl(hWnd
);
613 if (notification
== LBN_DBLCLK
)
615 return SendMessageW(hWnd
, WM_COMMAND
, IDOK
, 0);
617 else if (notification
== LBN_SELCHANGE
)
618 return FD31_FileListSelect( lfs
);
621 case lst2
: /* directory list */
622 FD31_StripEditControl(hWnd
);
623 if (notification
== LBN_DBLCLK
)
624 return FD31_DirListDblClick( lfs
);
627 case cmb1
: /* file type drop list */
628 if (notification
== CBN_SELCHANGE
)
629 return FD31_FileTypeChange( lfs
);
638 case cmb2
: /* disk dropdown combo */
639 if (notification
== CBN_SELCHANGE
)
640 return FD31_DiskChange( lfs
);
644 TRACE("OK pressed\n");
645 if (FD31_Validate( lfs
, NULL
, control
, 0, FALSE
))
646 EndDialog(hWnd
, TRUE
);
650 EndDialog(hWnd
, FALSE
);
653 case IDABORT
: /* can be sent by the hook procedure */
654 EndDialog(hWnd
, TRUE
);
660 /************************************************************************
661 * FD31_MapStringPairsToW [internal]
662 * map string pairs to Unicode
664 static LPWSTR
FD31_MapStringPairsToW(LPCSTR strA
, UINT size
)
674 n
= s
+ 1 - strA
; /* Don't forget the other \0 */
675 if (n
< size
) n
= size
;
677 len
= MultiByteToWideChar( CP_ACP
, 0, strA
, n
, NULL
, 0 );
678 x
= HeapAlloc(GetProcessHeap(),0, len
* sizeof(WCHAR
));
679 MultiByteToWideChar( CP_ACP
, 0, strA
, n
, x
, len
);
684 /************************************************************************
685 * FD31_DupToW [internal]
686 * duplicates an Ansi string to unicode, with a buffer size
688 static LPWSTR
FD31_DupToW(LPCSTR str
, DWORD size
)
691 if (str
&& (size
> 0))
693 strW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
694 if (strW
) MultiByteToWideChar( CP_ACP
, 0, str
, -1, strW
, size
);
699 /************************************************************************
700 * FD31_MapOfnStructA [internal]
701 * map a 32 bits Ansi structure to an Unicode one
703 void FD31_MapOfnStructA(const LPOPENFILENAMEA ofnA
, LPOPENFILENAMEW ofnW
, BOOL open
)
705 UNICODE_STRING usBuffer
;
707 ofnW
->lStructSize
= sizeof(OPENFILENAMEW
);
708 ofnW
->hwndOwner
= ofnA
->hwndOwner
;
709 ofnW
->hInstance
= ofnA
->hInstance
;
710 if (ofnA
->lpstrFilter
)
711 ofnW
->lpstrFilter
= FD31_MapStringPairsToW(ofnA
->lpstrFilter
, 0);
713 if ((ofnA
->lpstrCustomFilter
) && (*(ofnA
->lpstrCustomFilter
)))
714 ofnW
->lpstrCustomFilter
= FD31_MapStringPairsToW(ofnA
->lpstrCustomFilter
, ofnA
->nMaxCustFilter
);
715 ofnW
->nMaxCustFilter
= ofnA
->nMaxCustFilter
;
716 ofnW
->nFilterIndex
= ofnA
->nFilterIndex
;
717 ofnW
->nMaxFile
= ofnA
->nMaxFile
;
718 ofnW
->lpstrFile
= FD31_DupToW(ofnA
->lpstrFile
, ofnW
->nMaxFile
);
719 ofnW
->nMaxFileTitle
= ofnA
->nMaxFileTitle
;
720 ofnW
->lpstrFileTitle
= FD31_DupToW(ofnA
->lpstrFileTitle
, ofnW
->nMaxFileTitle
);
721 if (ofnA
->lpstrInitialDir
)
723 RtlCreateUnicodeStringFromAsciiz (&usBuffer
,ofnA
->lpstrInitialDir
);
724 ofnW
->lpstrInitialDir
= usBuffer
.Buffer
;
726 if (ofnA
->lpstrTitle
) {
727 RtlCreateUnicodeStringFromAsciiz (&usBuffer
, ofnA
->lpstrTitle
);
728 ofnW
->lpstrTitle
= usBuffer
.Buffer
;
732 LoadStringW(COMDLG32_hInstance
, open
? IDS_OPEN_FILE
: IDS_SAVE_AS
,
733 buf
, sizeof(buf
)/sizeof(WCHAR
));
734 len
= lstrlenW(buf
)+1;
735 ofnW
->lpstrTitle
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
736 memcpy((void*)ofnW
->lpstrTitle
, buf
, len
*sizeof(WCHAR
));
738 ofnW
->Flags
= ofnA
->Flags
;
739 ofnW
->nFileOffset
= ofnA
->nFileOffset
;
740 ofnW
->nFileExtension
= ofnA
->nFileExtension
;
741 ofnW
->lpstrDefExt
= FD31_DupToW(ofnA
->lpstrDefExt
, 3);
742 if ((ofnA
->Flags
& OFN_ENABLETEMPLATE
) && (ofnA
->lpTemplateName
))
744 if (HIWORD(ofnA
->lpTemplateName
))
746 RtlCreateUnicodeStringFromAsciiz (&usBuffer
,ofnA
->lpTemplateName
);
747 ofnW
->lpTemplateName
= usBuffer
.Buffer
;
749 else /* numbered resource */
750 ofnW
->lpTemplateName
= (LPWSTR
) ofnA
->lpTemplateName
;
755 /************************************************************************
756 * FD31_FreeOfnW [internal]
757 * Undo all allocations done by FD31_MapOfnStructA
759 void FD31_FreeOfnW(LPOPENFILENAMEW ofnW
)
761 HeapFree(GetProcessHeap(), 0, (LPWSTR
) ofnW
->lpstrFilter
);
762 HeapFree(GetProcessHeap(), 0, ofnW
->lpstrCustomFilter
);
763 HeapFree(GetProcessHeap(), 0, ofnW
->lpstrFile
);
764 HeapFree(GetProcessHeap(), 0, ofnW
->lpstrFileTitle
);
765 HeapFree(GetProcessHeap(), 0, (LPWSTR
) ofnW
->lpstrInitialDir
);
766 HeapFree(GetProcessHeap(), 0, (LPWSTR
) ofnW
->lpstrTitle
);
767 if ((ofnW
->lpTemplateName
) && (HIWORD(ofnW
->lpTemplateName
)))
768 HeapFree(GetProcessHeap(), 0, (LPWSTR
) ofnW
->lpTemplateName
);
771 /************************************************************************
772 * FD31_DestroyPrivate [internal]
773 * destroys the private object
775 void FD31_DestroyPrivate(PFD31_DATA lfs
)
780 TRACE("destroying private allocation %p\n", lfs
);
781 lfs
->callbacks
->Destroy(lfs
);
782 HeapFree(GetProcessHeap(), 0, lfs
);
783 RemovePropA(hwnd
, FD31_OFN_PROP
);
786 /************************************************************************
787 * FD31_AllocPrivate [internal]
788 * allocate a private object to hold 32 bits Unicode
789 * structure that will be used throughtout the calls, while
790 * keeping available the original structures and a few variables
791 * On entry : type = dialog procedure type (16,32A,32W)
792 * dlgType = dialog type (open or save)
794 PFD31_DATA
FD31_AllocPrivate(LPARAM lParam
, UINT dlgType
,
795 PFD31_CALLBACKS callbacks
, DWORD data
)
797 PFD31_DATA lfs
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(FD31_DATA
));
799 TRACE("alloc private buf %p\n", lfs
);
800 if (!lfs
) return NULL
;
802 lfs
->lParam
= lParam
;
803 lfs
->open
= (dlgType
== OPEN_DIALOG
);
804 lfs
->callbacks
= callbacks
;
805 if (! lfs
->callbacks
->Init(lParam
, lfs
, data
))
807 FD31_DestroyPrivate(lfs
);
810 lfs
->lbselchstring
= RegisterWindowMessageA(LBSELCHSTRINGA
);
811 lfs
->fileokstring
= RegisterWindowMessageA(FILEOKSTRINGA
);
816 /***********************************************************************
817 * FD31_WMInitDialog [internal]
820 LONG
FD31_WMInitDialog(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
823 WCHAR tmpstr
[BUFFILE
];
824 LPWSTR pstr
, old_pstr
;
826 PFD31_DATA lfs
= (PFD31_DATA
) lParam
;
828 if (!lfs
) return FALSE
;
829 SetPropA(hWnd
, FD31_OFN_PROP
, (HANDLE
)lfs
);
833 TRACE("flags=%lx initialdir=%s\n", ofn
->Flags
, debugstr_w(ofn
->lpstrInitialDir
));
835 SetWindowTextW( hWnd
, ofn
->lpstrTitle
);
836 /* read custom filter information */
837 if (ofn
->lpstrCustomFilter
)
839 pstr
= ofn
->lpstrCustomFilter
;
841 TRACE("lpstrCustomFilter = %p\n", pstr
);
845 i
= SendDlgItemMessageW(hWnd
, cmb1
, CB_ADDSTRING
, 0,
846 (LPARAM
)(ofn
->lpstrCustomFilter
) + n
);
847 n
+= lstrlenW(pstr
) + 1;
848 pstr
+= lstrlenW(pstr
) + 1;
849 TRACE("add str=%s associated to %s\n",
850 debugstr_w(old_pstr
), debugstr_w(pstr
));
851 SendDlgItemMessageW(hWnd
, cmb1
, CB_SETITEMDATA
, i
, (LPARAM
)pstr
);
852 n
+= lstrlenW(pstr
) + 1;
853 pstr
+= lstrlenW(pstr
) + 1;
856 /* read filter information */
857 if (ofn
->lpstrFilter
) {
858 pstr
= (LPWSTR
) ofn
->lpstrFilter
;
862 i
= SendDlgItemMessageW(hWnd
, cmb1
, CB_ADDSTRING
, 0,
863 (LPARAM
)(ofn
->lpstrFilter
+ n
) );
864 n
+= lstrlenW(pstr
) + 1;
865 pstr
+= lstrlenW(pstr
) + 1;
866 TRACE("add str=%s associated to %s\n",
867 debugstr_w(old_pstr
), debugstr_w(pstr
));
868 SendDlgItemMessageW(hWnd
, cmb1
, CB_SETITEMDATA
, i
, (LPARAM
)pstr
);
869 n
+= lstrlenW(pstr
) + 1;
870 pstr
+= lstrlenW(pstr
) + 1;
873 /* set default filter */
874 if (ofn
->nFilterIndex
== 0 && ofn
->lpstrCustomFilter
== NULL
)
875 ofn
->nFilterIndex
= 1;
876 SendDlgItemMessageW(hWnd
, cmb1
, CB_SETCURSEL
, ofn
->nFilterIndex
- 1, 0);
877 lstrcpynW(tmpstr
, FD31_GetFileType(ofn
->lpstrCustomFilter
,
878 (LPWSTR
)ofn
->lpstrFilter
, ofn
->nFilterIndex
- 1),BUFFILE
);
879 TRACE("nFilterIndex = %ld, SetText of edt1 to %s\n",
880 ofn
->nFilterIndex
, debugstr_w(tmpstr
));
881 SetDlgItemTextW( hWnd
, edt1
, tmpstr
);
884 DlgDirListComboBoxW(hWnd
, tmpstr
, cmb2
, 0, DDL_DRIVES
| DDL_EXCLUSIVE
);
885 /* read initial directory */
886 /* FIXME: Note that this is now very version-specific (See MSDN description of
887 * the OPENFILENAME structure). For example under 2000/XP any path in the
888 * lpstrFile overrides the lpstrInitialDir, but not under 95/98/ME
890 if (ofn
->lpstrInitialDir
!= NULL
)
893 lstrcpynW(tmpstr
, ofn
->lpstrInitialDir
, 511);
894 len
= lstrlenW(tmpstr
);
895 if (len
> 0 && tmpstr
[len
-1] != '\\' && tmpstr
[len
-1] != ':') {
902 if (!FD31_ScanDir(hWnd
, tmpstr
)) {
904 if (!FD31_ScanDir(hWnd
, tmpstr
))
905 WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr
));
907 /* select current drive in combo 2, omit missing drives */
910 char str
[4] = "a:\\";
911 GetCurrentDirectoryA( sizeof(dir
), dir
);
912 for(i
= 0, n
= -1; i
< 26; i
++)
915 if (GetDriveTypeA(str
) > DRIVE_NO_ROOT_DIR
) n
++;
916 if (toupper(str
[0]) == toupper(dir
[0])) break;
919 SendDlgItemMessageW(hWnd
, cmb2
, CB_SETCURSEL
, n
, 0);
920 if (!(ofn
->Flags
& OFN_SHOWHELP
))
921 ShowWindow(GetDlgItem(hWnd
, pshHelp
), SW_HIDE
);
922 if (ofn
->Flags
& OFN_HIDEREADONLY
)
923 ShowWindow(GetDlgItem(hWnd
, chx1
), SW_HIDE
);
925 return (BOOL
) FD31_CallWindowProc(lfs
, WM_INITDIALOG
, wParam
, lfs
->lParam
);
929 int FD31_GetFldrHeight(void)