2 * Win 3.1 Style 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
25 #define BUFFILEALLOC 512 * sizeof(WCHAR)
27 static const WCHAR FILE_star
[] = {'*','.','*', 0};
28 static const WCHAR FILE_bslash
[] = {'\\', 0};
29 static const WCHAR FILE_specc
[] = {'%','c',':', 0};
30 static const int fldrHeight
= 16;
31 static const int fldrWidth
= 20;
33 static HICON hFolder
= 0;
34 static HICON hFolder2
= 0;
35 static HICON hFloppy
= 0;
36 static HICON hHDisk
= 0;
37 static HICON hCDRom
= 0;
38 static HICON hNet
= 0;
40 #define FD31_OFN_PROP "FILEDLG_OFN"
42 typedef struct tagFD31_DATA
44 HWND hwnd
; /* file dialog window handle */
45 BOOL hook
; /* TRUE if the dialog is hooked */
46 UINT lbselchstring
; /* registered message id */
47 UINT fileokstring
; /* registered message id */
48 LPARAM lParam
; /* save original lparam */
49 LPCVOID
template; /* template for 32 bits resource */
50 BOOL open
; /* TRUE if open dialog, FALSE if save dialog */
51 LPOPENFILENAMEW ofnW
; /* pointer either to the original structure or
52 a W copy for A/16 API */
53 LPOPENFILENAMEA ofnA
; /* original structure if 32bits ansi dialog */
54 } FD31_DATA
, *PFD31_DATA
;
56 /***********************************************************************
57 * FD31_Init [internal]
59 static BOOL
FD31_Init(void)
61 static BOOL initialized
= FALSE
;
64 hFolder
= LoadImageA( COMDLG32_hInstance
, "FOLDER", IMAGE_ICON
, 16, 16, LR_SHARED
);
65 hFolder2
= LoadImageA( COMDLG32_hInstance
, "FOLDER2", IMAGE_ICON
, 16, 16, LR_SHARED
);
66 hFloppy
= LoadImageA( COMDLG32_hInstance
, "FLOPPY", IMAGE_ICON
, 16, 16, LR_SHARED
);
67 hHDisk
= LoadImageA( COMDLG32_hInstance
, "HDISK", IMAGE_ICON
, 16, 16, LR_SHARED
);
68 hCDRom
= LoadImageA( COMDLG32_hInstance
, "CDROM", IMAGE_ICON
, 16, 16, LR_SHARED
);
69 hNet
= LoadImageA( COMDLG32_hInstance
, "NETWORK", IMAGE_ICON
, 16, 16, LR_SHARED
);
70 if (hFolder
== 0 || hFolder2
== 0 || hFloppy
== 0 ||
71 hHDisk
== 0 || hCDRom
== 0 || hNet
== 0)
73 ERR("Error loading icons!\n");
81 /***********************************************************************
82 * FD31_StripEditControl [internal]
83 * Strip pathnames off the contents of the edit control.
85 static void FD31_StripEditControl(HWND hwnd
)
87 WCHAR temp
[BUFFILE
], *cp
;
89 GetDlgItemTextW( hwnd
, edt1
, temp
, sizeof(temp
)/sizeof(WCHAR
));
90 cp
= strrchrW(temp
, '\\');
94 cp
= strrchrW(temp
, ':');
98 /* FIXME: shouldn't we do something with the result here? ;-) */
101 /***********************************************************************
102 * FD31_CallWindowProc [internal]
104 * Call the appropriate hook
106 static BOOL
FD31_CallWindowProc(const FD31_DATA
*lfs
, UINT wMsg
, WPARAM wParam
, LPARAM lParam
)
112 TRACE("Call hookA %p (%p, %04x, %08lx, %08lx)\n",
113 lfs
->ofnA
->lpfnHook
, lfs
->hwnd
, wMsg
, wParam
, lParam
);
114 ret
= lfs
->ofnA
->lpfnHook(lfs
->hwnd
, wMsg
, wParam
, lParam
);
115 TRACE("ret hookA %p (%p, %04x, %08lx, %08lx)\n",
116 lfs
->ofnA
->lpfnHook
, lfs
->hwnd
, wMsg
, wParam
, lParam
);
120 TRACE("Call hookW %p (%p, %04x, %08lx, %08lx)\n",
121 lfs
->ofnW
->lpfnHook
, lfs
->hwnd
, wMsg
, wParam
, lParam
);
122 ret
= lfs
->ofnW
->lpfnHook(lfs
->hwnd
, wMsg
, wParam
, lParam
);
123 TRACE("Ret hookW %p (%p, %04x, %08lx, %08lx)\n",
124 lfs
->ofnW
->lpfnHook
, lfs
->hwnd
, wMsg
, wParam
, lParam
);
128 /***********************************************************************
129 * FD31_GetFileType [internal]
131 static LPCWSTR
FD31_GetFileType(LPCWSTR cfptr
, LPCWSTR fptr
, const WORD index
)
136 for ( ;(n
= lstrlenW(cfptr
)) != 0; i
++)
141 cfptr
+= lstrlenW(cfptr
) + 1;
144 for ( ;(n
= lstrlenW(fptr
)) != 0; i
++)
149 fptr
+= lstrlenW(fptr
) + 1;
151 return FILE_star
; /* FIXME */
154 /***********************************************************************
155 * FD31_ScanDir [internal]
157 static BOOL
FD31_ScanDir(const OPENFILENAMEW
*ofn
, HWND hWnd
, LPCWSTR newPath
)
159 WCHAR buffer
[BUFFILE
];
162 HCURSOR hCursorWait
, oldCursor
;
164 TRACE("Trying to change to %s\n", debugstr_w(newPath
));
165 if ( newPath
[0] && !SetCurrentDirectoryW( newPath
))
168 /* get the list of spec files */
169 lstrcpynW(buffer
, FD31_GetFileType(ofn
->lpstrCustomFilter
,
170 ofn
->lpstrFilter
, ofn
->nFilterIndex
- 1), BUFFILE
);
172 hCursorWait
= LoadCursorA(0, (LPSTR
)IDC_WAIT
);
173 oldCursor
= SetCursor(hCursorWait
);
176 if ((hdlg
= GetDlgItem(hWnd
, lst1
)) != 0) {
177 WCHAR
* scptr
; /* ptr on semi-colon */
178 WCHAR
* filter
= buffer
;
180 TRACE("Using filter %s\n", debugstr_w(filter
));
181 SendMessageW(hdlg
, LB_RESETCONTENT
, 0, 0);
183 scptr
= strchrW(filter
, ';');
184 if (scptr
) *scptr
= 0;
185 while (*filter
== ' ') filter
++;
186 TRACE("Using file spec %s\n", debugstr_w(filter
));
187 SendMessageW(hdlg
, LB_DIR
, 0, (LPARAM
)filter
);
188 if (scptr
) *scptr
= ';';
189 filter
= (scptr
) ? (scptr
+ 1) : 0;
193 /* list of directories */
194 strcpyW(buffer
, FILE_star
);
196 if (GetDlgItem(hWnd
, lst2
) != 0) {
197 lRet
= DlgDirListW(hWnd
, buffer
, lst2
, stc1
, DDL_EXCLUSIVE
| DDL_DIRECTORY
);
199 SetCursor(oldCursor
);
203 /***********************************************************************
204 * FD31_WMDrawItem [internal]
206 static LONG
FD31_WMDrawItem(HWND hWnd
, WPARAM wParam
, LPARAM lParam
,
207 int savedlg
, const DRAWITEMSTRUCT
*lpdis
)
211 COLORREF oldText
= 0, oldBk
= 0;
213 if (lpdis
->CtlType
== ODT_LISTBOX
&& lpdis
->CtlID
== lst1
)
215 if (!(str
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
))) return FALSE
;
216 SendMessageW(lpdis
->hwndItem
, LB_GETTEXT
, lpdis
->itemID
,
219 if ((lpdis
->itemState
& ODS_SELECTED
) && !savedlg
)
221 oldBk
= SetBkColor( lpdis
->hDC
, GetSysColor( COLOR_HIGHLIGHT
) );
222 oldText
= SetTextColor( lpdis
->hDC
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
225 SetTextColor(lpdis
->hDC
,GetSysColor(COLOR_GRAYTEXT
) );
227 ExtTextOutW(lpdis
->hDC
, lpdis
->rcItem
.left
+ 1,
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
) && !savedlg
)
236 SetBkColor( lpdis
->hDC
, oldBk
);
237 SetTextColor( lpdis
->hDC
, oldText
);
239 HeapFree(GetProcessHeap(), 0, str
);
243 if (lpdis
->CtlType
== ODT_LISTBOX
&& lpdis
->CtlID
== lst2
)
245 if (!(str
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
)))
247 SendMessageW(lpdis
->hwndItem
, LB_GETTEXT
, lpdis
->itemID
,
250 if (lpdis
->itemState
& ODS_SELECTED
)
252 oldBk
= SetBkColor( lpdis
->hDC
, GetSysColor( COLOR_HIGHLIGHT
) );
253 oldText
= SetTextColor( lpdis
->hDC
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
255 ExtTextOutW(lpdis
->hDC
, lpdis
->rcItem
.left
+ fldrWidth
,
256 lpdis
->rcItem
.top
+ 1, ETO_OPAQUE
| ETO_CLIPPED
,
257 &(lpdis
->rcItem
), str
, lstrlenW(str
), NULL
);
259 if (lpdis
->itemState
& ODS_SELECTED
)
260 DrawFocusRect( lpdis
->hDC
, &(lpdis
->rcItem
) );
262 if (lpdis
->itemState
& ODS_SELECTED
)
264 SetBkColor( lpdis
->hDC
, oldBk
);
265 SetTextColor( lpdis
->hDC
, oldText
);
267 DrawIconEx( lpdis
->hDC
, lpdis
->rcItem
.left
, lpdis
->rcItem
.top
, hFolder
, 16, 16, 0, 0, DI_NORMAL
);
268 HeapFree(GetProcessHeap(), 0, str
);
271 if (lpdis
->CtlType
== ODT_COMBOBOX
&& lpdis
->CtlID
== cmb2
)
274 if (!(str
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
)))
276 SendMessageW(lpdis
->hwndItem
, CB_GETLBTEXT
, lpdis
->itemID
,
278 root
[0] += str
[2] - 'a';
279 switch(GetDriveTypeA(root
))
281 case DRIVE_REMOVABLE
: hIcon
= hFloppy
; break;
282 case DRIVE_CDROM
: hIcon
= hCDRom
; break;
283 case DRIVE_REMOTE
: hIcon
= hNet
; break;
285 default: hIcon
= hHDisk
; break;
287 if (lpdis
->itemState
& ODS_SELECTED
)
289 oldBk
= SetBkColor( lpdis
->hDC
, GetSysColor( COLOR_HIGHLIGHT
) );
290 oldText
= SetTextColor( lpdis
->hDC
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
292 ExtTextOutW(lpdis
->hDC
, lpdis
->rcItem
.left
+ fldrWidth
,
293 lpdis
->rcItem
.top
+ 1, ETO_OPAQUE
| ETO_CLIPPED
,
294 &(lpdis
->rcItem
), str
, lstrlenW(str
), NULL
);
296 if (lpdis
->itemState
& ODS_SELECTED
)
298 SetBkColor( lpdis
->hDC
, oldBk
);
299 SetTextColor( lpdis
->hDC
, oldText
);
301 DrawIconEx( lpdis
->hDC
, lpdis
->rcItem
.left
, lpdis
->rcItem
.top
, hIcon
, 16, 16, 0, 0, DI_NORMAL
);
302 HeapFree(GetProcessHeap(), 0, str
);
308 /***********************************************************************
309 * FD31_UpdateResult [internal]
310 * update the displayed file name (with path)
312 static void FD31_UpdateResult(const FD31_DATA
*lfs
, const WCHAR
*tmpstr
)
315 LPOPENFILENAMEW ofnW
= lfs
->ofnW
;
316 LPOPENFILENAMEA ofnA
= lfs
->ofnA
;
317 WCHAR tmpstr2
[BUFFILE
];
320 TRACE("%s\n", debugstr_w(tmpstr
));
321 if(ofnW
->Flags
& OFN_NOVALIDATE
)
324 GetCurrentDirectoryW(BUFFILE
, tmpstr2
);
325 lenstr2
= strlenW(tmpstr2
);
327 tmpstr2
[lenstr2
++]='\\';
328 lstrcpynW(tmpstr2
+lenstr2
, tmpstr
, BUFFILE
-lenstr2
);
329 if (!ofnW
->lpstrFile
)
332 lstrcpynW(ofnW
->lpstrFile
, tmpstr2
, ofnW
->nMaxFile
);
334 /* set filename offset */
335 p
= PathFindFileNameW(ofnW
->lpstrFile
);
336 ofnW
->nFileOffset
= (p
- ofnW
->lpstrFile
);
338 /* set extension offset */
339 p
= PathFindExtensionW(ofnW
->lpstrFile
);
340 ofnW
->nFileExtension
= (*p
) ? (p
- ofnW
->lpstrFile
) + 1 : 0;
342 TRACE("file %s, file offset %d, ext offset %d\n",
343 debugstr_w(ofnW
->lpstrFile
), ofnW
->nFileOffset
, ofnW
->nFileExtension
);
345 /* update the real client structures if any */
349 if (ofnW
->nMaxFile
&&
350 !WideCharToMultiByte( CP_ACP
, 0, ofnW
->lpstrFile
, -1,
351 ofnA
->lpstrFile
, ofnA
->nMaxFile
, NULL
, NULL
))
352 ofnA
->lpstrFile
[ofnA
->nMaxFile
-1] = 0;
354 /* offsets are not guaranteed to be the same in WCHAR to MULTIBYTE conversion */
355 /* set filename offset */
356 lpszTemp
= PathFindFileNameA(ofnA
->lpstrFile
);
357 ofnA
->nFileOffset
= (lpszTemp
- ofnA
->lpstrFile
);
359 /* set extension offset */
360 lpszTemp
= PathFindExtensionA(ofnA
->lpstrFile
);
361 ofnA
->nFileExtension
= (*lpszTemp
) ? (lpszTemp
- ofnA
->lpstrFile
) + 1 : 0;
365 /***********************************************************************
366 * FD31_UpdateFileTitle [internal]
367 * update the displayed file name (without path)
369 static void FD31_UpdateFileTitle(const FD31_DATA
*lfs
)
372 LPOPENFILENAMEW ofnW
= lfs
->ofnW
;
373 LPOPENFILENAMEA ofnA
= lfs
->ofnA
;
375 if (ofnW
->lpstrFileTitle
!= NULL
)
377 lRet
= SendDlgItemMessageW(lfs
->hwnd
, lst1
, LB_GETCURSEL
, 0, 0);
378 SendDlgItemMessageW(lfs
->hwnd
, lst1
, LB_GETTEXT
, lRet
,
379 (LPARAM
)ofnW
->lpstrFileTitle
);
382 if (!WideCharToMultiByte( CP_ACP
, 0, ofnW
->lpstrFileTitle
, -1,
383 ofnA
->lpstrFileTitle
, ofnA
->nMaxFileTitle
, NULL
, NULL
))
384 ofnA
->lpstrFileTitle
[ofnA
->nMaxFileTitle
-1] = 0;
389 /***********************************************************************
390 * FD31_DirListDblClick [internal]
392 static LRESULT
FD31_DirListDblClick( const FD31_DATA
*lfs
)
395 HWND hWnd
= lfs
->hwnd
;
397 WCHAR tmpstr
[BUFFILE
];
399 /* get the raw string (with brackets) */
400 lRet
= SendDlgItemMessageW(hWnd
, lst2
, LB_GETCURSEL
, 0, 0);
401 if (lRet
== LB_ERR
) return TRUE
;
402 pstr
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
);
403 SendDlgItemMessageW(hWnd
, lst2
, LB_GETTEXT
, lRet
,
405 strcpyW( tmpstr
, pstr
);
406 HeapFree(GetProcessHeap(), 0, pstr
);
407 /* get the selected directory in tmpstr */
408 if (tmpstr
[0] == '[')
410 tmpstr
[lstrlenW(tmpstr
) - 1] = 0;
411 strcpyW(tmpstr
,tmpstr
+1);
413 strcatW(tmpstr
, FILE_bslash
);
415 FD31_ScanDir(lfs
->ofnW
, hWnd
, tmpstr
);
419 if (FD31_CallWindowProc(lfs
, lfs
->lbselchstring
, lst2
,
420 MAKELONG(lRet
,CD_LBSELCHANGE
)))
426 /***********************************************************************
427 * FD31_FileListSelect [internal]
428 * called when a new item is picked in the file list
430 static LRESULT
FD31_FileListSelect( const FD31_DATA
*lfs
)
433 HWND hWnd
= lfs
->hwnd
;
436 lRet
= SendDlgItemMessageW(lfs
->hwnd
, lst1
, LB_GETCURSEL
, 0, 0);
440 /* set the edit control to the chosen file */
441 if ((pstr
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
)))
443 SendDlgItemMessageW(hWnd
, lst1
, LB_GETTEXT
, lRet
,
445 SetDlgItemTextW( hWnd
, edt1
, pstr
);
446 HeapFree(GetProcessHeap(), 0, pstr
);
450 FD31_CallWindowProc(lfs
, lfs
->lbselchstring
, lst1
,
451 MAKELONG(lRet
,CD_LBSELCHANGE
));
453 /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD,
458 /***********************************************************************
459 * FD31_TestPath [internal]
460 * before accepting the file name, test if it includes wild cards
461 * tries to scan the directory and returns TRUE if no error.
463 static LRESULT
FD31_TestPath( const FD31_DATA
*lfs
, LPWSTR path
)
465 HWND hWnd
= lfs
->hwnd
;
466 LPWSTR pBeginFileName
, pstr2
;
467 WCHAR tmpstr2
[BUFFILE
];
469 pBeginFileName
= strrchrW(path
, '\\');
470 if (pBeginFileName
== NULL
)
471 pBeginFileName
= strrchrW(path
, ':');
473 if (strchrW(path
,'*') != NULL
|| strchrW(path
,'?') != NULL
)
475 /* edit control contains wildcards */
476 if (pBeginFileName
!= NULL
)
478 lstrcpynW(tmpstr2
, pBeginFileName
+ 1, BUFFILE
);
479 *(pBeginFileName
+ 1) = 0;
483 strcpyW(tmpstr2
, path
);
484 if(!(lfs
->ofnW
->Flags
& OFN_NOVALIDATE
))
488 TRACE("path=%s, tmpstr2=%s\n", debugstr_w(path
), debugstr_w(tmpstr2
));
489 SetDlgItemTextW( hWnd
, edt1
, tmpstr2
);
490 FD31_ScanDir(lfs
->ofnW
, hWnd
, path
);
491 return (lfs
->ofnW
->Flags
& OFN_NOVALIDATE
) != 0;
494 /* no wildcards, we might have a directory or a filename */
495 /* try appending a wildcard and reading the directory */
497 pstr2
= path
+ lstrlenW(path
);
498 if (pBeginFileName
== NULL
|| *(pBeginFileName
+ 1) != 0)
499 strcatW(path
, FILE_bslash
);
501 /* if ScanDir succeeds, we have changed the directory */
502 if (FD31_ScanDir(lfs
->ofnW
, hWnd
, path
))
503 return FALSE
; /* and path is not a valid file name */
505 /* if not, this must be a filename */
507 *pstr2
= 0; /* remove the wildcard added before */
509 if (pBeginFileName
!= NULL
)
511 /* strip off the pathname */
513 SetDlgItemTextW( hWnd
, edt1
, pBeginFileName
+ 1 );
515 lstrcpynW(tmpstr2
, pBeginFileName
+ 1, sizeof(tmpstr2
)/sizeof(WCHAR
) );
516 /* Should we MessageBox() if this fails? */
517 if (!FD31_ScanDir(lfs
->ofnW
, hWnd
, path
))
521 strcpyW(path
, tmpstr2
);
524 SetDlgItemTextW( hWnd
, edt1
, path
);
528 /***********************************************************************
529 * FD31_Validate [internal]
530 * called on: click Ok button, Enter in edit, DoubleClick in file list
532 static LRESULT
FD31_Validate( const FD31_DATA
*lfs
, LPCWSTR path
, UINT control
, INT itemIndex
,
536 HWND hWnd
= lfs
->hwnd
;
537 OPENFILENAMEW ofnsav
;
538 LPOPENFILENAMEW ofnW
= lfs
->ofnW
;
539 WCHAR filename
[BUFFILE
];
540 int copied_size
= min( ofnW
->lStructSize
, sizeof(ofnsav
) );
542 memcpy( &ofnsav
, ofnW
, copied_size
); /* for later restoring */
544 /* get current file name */
546 lstrcpynW(filename
, path
, sizeof(filename
)/sizeof(WCHAR
));
548 GetDlgItemTextW( hWnd
, edt1
, filename
, sizeof(filename
)/sizeof(WCHAR
));
550 TRACE("got filename = %s\n", debugstr_w(filename
));
551 /* if we did not click in file list to get there */
554 if (!FD31_TestPath( lfs
, filename
) )
557 FD31_UpdateResult(lfs
, filename
);
560 { /* called internally after a change in a combo */
563 FD31_CallWindowProc(lfs
, lfs
->lbselchstring
, control
,
564 MAKELONG(itemIndex
,CD_LBSELCHANGE
));
569 FD31_UpdateFileTitle(lfs
);
572 lRet
= FD31_CallWindowProc(lfs
, lfs
->fileokstring
,
576 memcpy( ofnW
, &ofnsav
, copied_size
); /* restore old state */
580 if ((ofnW
->Flags
& OFN_ALLOWMULTISELECT
) && (ofnW
->Flags
& OFN_EXPLORER
))
584 LPWSTR str
= ofnW
->lpstrFile
;
585 LPWSTR ptr
= strrchrW(str
, '\\');
586 str
[lstrlenW(str
) + 1] = '\0';
593 /***********************************************************************
594 * FD31_DiskChange [internal]
595 * called when a new item is picked in the disk selection combo
597 static LRESULT
FD31_DiskChange( const FD31_DATA
*lfs
)
600 HWND hWnd
= lfs
->hwnd
;
602 WCHAR diskname
[BUFFILE
];
604 FD31_StripEditControl(hWnd
);
605 lRet
= SendDlgItemMessageW(hWnd
, cmb2
, CB_GETCURSEL
, 0, 0L);
608 pstr
= HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC
);
609 SendDlgItemMessageW(hWnd
, cmb2
, CB_GETLBTEXT
, lRet
,
611 wsprintfW(diskname
, FILE_specc
, pstr
[2]);
612 HeapFree(GetProcessHeap(), 0, pstr
);
614 return FD31_Validate( lfs
, diskname
, cmb2
, lRet
, TRUE
);
617 /***********************************************************************
618 * FD31_FileTypeChange [internal]
619 * called when a new item is picked in the file type combo
621 static LRESULT
FD31_FileTypeChange( const FD31_DATA
*lfs
)
626 lRet
= SendDlgItemMessageW(lfs
->hwnd
, cmb1
, CB_GETCURSEL
, 0, 0);
629 lfs
->ofnW
->nFilterIndex
= lRet
+ 1;
631 lfs
->ofnA
->nFilterIndex
= lRet
+ 1;
632 pstr
= (LPWSTR
)SendDlgItemMessageW(lfs
->hwnd
, cmb1
, CB_GETITEMDATA
, lRet
, 0);
633 TRACE("Selected filter : %s\n", debugstr_w(pstr
));
635 return FD31_Validate( lfs
, pstr
, cmb1
, lRet
, TRUE
);
638 /***********************************************************************
639 * FD31_WMCommand [internal]
641 static LRESULT
FD31_WMCommand( HWND hWnd
, LPARAM lParam
, UINT notification
,
642 UINT control
, const FD31_DATA
*lfs
)
646 case lst1
: /* file list */
647 FD31_StripEditControl(hWnd
);
648 if (notification
== LBN_DBLCLK
)
650 return SendMessageW(hWnd
, WM_COMMAND
, IDOK
, 0);
652 else if (notification
== LBN_SELCHANGE
)
653 return FD31_FileListSelect( lfs
);
656 case lst2
: /* directory list */
657 FD31_StripEditControl(hWnd
);
658 if (notification
== LBN_DBLCLK
)
659 return FD31_DirListDblClick( lfs
);
662 case cmb1
: /* file type drop list */
663 if (notification
== CBN_SELCHANGE
)
664 return FD31_FileTypeChange( lfs
);
673 case cmb2
: /* disk dropdown combo */
674 if (notification
== CBN_SELCHANGE
)
675 return FD31_DiskChange( lfs
);
679 TRACE("OK pressed\n");
680 if (FD31_Validate( lfs
, NULL
, control
, 0, FALSE
))
681 EndDialog(hWnd
, TRUE
);
685 EndDialog(hWnd
, FALSE
);
688 case IDABORT
: /* can be sent by the hook procedure */
689 EndDialog(hWnd
, TRUE
);
695 /************************************************************************
696 * FD31_MapStringPairsToW [internal]
697 * map string pairs to Unicode
699 static LPWSTR
FD31_MapStringPairsToW(LPCSTR strA
, UINT size
)
709 n
= s
+ 1 - strA
; /* Don't forget the other \0 */
710 if (n
< size
) n
= size
;
712 len
= MultiByteToWideChar( CP_ACP
, 0, strA
, n
, NULL
, 0 );
713 x
= HeapAlloc(GetProcessHeap(),0, len
* sizeof(WCHAR
));
714 MultiByteToWideChar( CP_ACP
, 0, strA
, n
, x
, len
);
719 /************************************************************************
720 * FD31_DupToW [internal]
721 * duplicates an Ansi string to unicode, with a buffer size
723 static LPWSTR
FD31_DupToW(LPCSTR str
, DWORD size
)
726 if (str
&& (size
> 0))
728 strW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
729 if (strW
) MultiByteToWideChar( CP_ACP
, 0, str
, -1, strW
, size
);
734 /************************************************************************
735 * FD31_MapOfnStructA [internal]
736 * map a 32 bits Ansi structure to a Unicode one
738 static void FD31_MapOfnStructA(const OPENFILENAMEA
*ofnA
, LPOPENFILENAMEW ofnW
, BOOL open
)
740 UNICODE_STRING usBuffer
;
742 ofnW
->hwndOwner
= ofnA
->hwndOwner
;
743 ofnW
->hInstance
= ofnA
->hInstance
;
744 if (ofnA
->lpstrFilter
)
745 ofnW
->lpstrFilter
= FD31_MapStringPairsToW(ofnA
->lpstrFilter
, 0);
747 if ((ofnA
->lpstrCustomFilter
) && (*(ofnA
->lpstrCustomFilter
)))
748 ofnW
->lpstrCustomFilter
= FD31_MapStringPairsToW(ofnA
->lpstrCustomFilter
, ofnA
->nMaxCustFilter
);
749 ofnW
->nMaxCustFilter
= ofnA
->nMaxCustFilter
;
750 ofnW
->nFilterIndex
= ofnA
->nFilterIndex
;
751 ofnW
->nMaxFile
= ofnA
->nMaxFile
;
752 ofnW
->lpstrFile
= FD31_DupToW(ofnA
->lpstrFile
, ofnW
->nMaxFile
);
753 ofnW
->nMaxFileTitle
= ofnA
->nMaxFileTitle
;
754 ofnW
->lpstrFileTitle
= FD31_DupToW(ofnA
->lpstrFileTitle
, ofnW
->nMaxFileTitle
);
755 if (ofnA
->lpstrInitialDir
)
757 RtlCreateUnicodeStringFromAsciiz (&usBuffer
,ofnA
->lpstrInitialDir
);
758 ofnW
->lpstrInitialDir
= usBuffer
.Buffer
;
760 if (ofnA
->lpstrTitle
) {
761 RtlCreateUnicodeStringFromAsciiz (&usBuffer
, ofnA
->lpstrTitle
);
762 ofnW
->lpstrTitle
= usBuffer
.Buffer
;
767 LoadStringW(COMDLG32_hInstance
, open
? IDS_OPEN_FILE
: IDS_SAVE_AS
,
768 buf
, sizeof(buf
)/sizeof(WCHAR
));
769 len
= lstrlenW(buf
)+1;
770 title_tmp
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
771 memcpy(title_tmp
, buf
, len
* sizeof(WCHAR
));
772 ofnW
->lpstrTitle
= title_tmp
;
774 ofnW
->Flags
= ofnA
->Flags
;
775 ofnW
->nFileOffset
= ofnA
->nFileOffset
;
776 ofnW
->nFileExtension
= ofnA
->nFileExtension
;
777 ofnW
->lpstrDefExt
= FD31_DupToW(ofnA
->lpstrDefExt
, 3);
778 if ((ofnA
->Flags
& OFN_ENABLETEMPLATE
) && (ofnA
->lpTemplateName
))
780 if (!IS_INTRESOURCE(ofnA
->lpTemplateName
))
782 RtlCreateUnicodeStringFromAsciiz (&usBuffer
,ofnA
->lpTemplateName
);
783 ofnW
->lpTemplateName
= usBuffer
.Buffer
;
785 else /* numbered resource */
786 ofnW
->lpTemplateName
= (LPCWSTR
) ofnA
->lpTemplateName
;
788 if (ofnW
->lStructSize
> OPENFILENAME_SIZE_VERSION_400W
)
790 ofnW
->pvReserved
= ofnA
->pvReserved
;
791 ofnW
->dwReserved
= ofnA
->dwReserved
;
792 ofnW
->FlagsEx
= ofnA
->FlagsEx
;
797 /************************************************************************
798 * FD31_FreeOfnW [internal]
799 * Undo all allocations done by FD31_MapOfnStructA
801 static void FD31_FreeOfnW(OPENFILENAMEW
*ofnW
)
803 HeapFree(GetProcessHeap(), 0, (LPWSTR
) ofnW
->lpstrFilter
);
804 HeapFree(GetProcessHeap(), 0, ofnW
->lpstrCustomFilter
);
805 HeapFree(GetProcessHeap(), 0, ofnW
->lpstrFile
);
806 HeapFree(GetProcessHeap(), 0, ofnW
->lpstrFileTitle
);
807 HeapFree(GetProcessHeap(), 0, (LPWSTR
) ofnW
->lpstrInitialDir
);
808 HeapFree(GetProcessHeap(), 0, (LPWSTR
) ofnW
->lpstrTitle
);
809 if (!IS_INTRESOURCE(ofnW
->lpTemplateName
))
810 HeapFree(GetProcessHeap(), 0, (LPWSTR
) ofnW
->lpTemplateName
);
813 /************************************************************************
814 * FD31_DestroyPrivate [internal]
815 * destroys the private object
817 static void FD31_DestroyPrivate(PFD31_DATA lfs
)
822 TRACE("destroying private allocation %p\n", lfs
);
824 /* if ofnW has been allocated, have to free everything in it */
827 FD31_FreeOfnW(lfs
->ofnW
);
828 HeapFree(GetProcessHeap(), 0, lfs
->ofnW
);
830 HeapFree(GetProcessHeap(), 0, lfs
);
831 RemovePropA(hwnd
, FD31_OFN_PROP
);
834 /***********************************************************************
835 * FD31_GetTemplate [internal]
837 * Get a template (or FALSE if failure) when 16 bits dialogs are used
838 * by a 32 bits application
841 static BOOL
FD31_GetTemplate(PFD31_DATA lfs
)
843 LPOPENFILENAMEW ofnW
= lfs
->ofnW
;
844 LPOPENFILENAMEA ofnA
= lfs
->ofnA
;
847 if (ofnW
->Flags
& OFN_ENABLETEMPLATEHANDLE
)
849 if (!(lfs
->template = LockResource( ofnW
->hInstance
)))
851 COMDLG32_SetCommDlgExtendedError( CDERR_LOADRESFAILURE
);
855 else if (ofnW
->Flags
& OFN_ENABLETEMPLATE
)
859 hResInfo
= FindResourceA( ofnA
->hInstance
, ofnA
->lpTemplateName
, (LPSTR
)RT_DIALOG
);
861 hResInfo
= FindResourceW( ofnW
->hInstance
, ofnW
->lpTemplateName
, (LPWSTR
)RT_DIALOG
);
864 COMDLG32_SetCommDlgExtendedError( CDERR_FINDRESFAILURE
);
867 if (!(hDlgTmpl
= LoadResource( ofnW
->hInstance
, hResInfo
)) ||
868 !(lfs
->template = LockResource( hDlgTmpl
)))
870 COMDLG32_SetCommDlgExtendedError( CDERR_LOADRESFAILURE
);
874 else /* get it from internal Wine resource */
877 if (!(hResInfo
= FindResourceA( COMDLG32_hInstance
, lfs
->open
? "OPEN_FILE" : "SAVE_FILE", (LPSTR
)RT_DIALOG
)))
879 COMDLG32_SetCommDlgExtendedError( CDERR_FINDRESFAILURE
);
882 if (!(hDlgTmpl
= LoadResource( COMDLG32_hInstance
, hResInfo
)) ||
883 !(lfs
->template = LockResource( hDlgTmpl
)))
885 COMDLG32_SetCommDlgExtendedError( CDERR_LOADRESFAILURE
);
892 /************************************************************************
893 * FD31_AllocPrivate [internal]
894 * allocate a private object to hold 32 bits Unicode
895 * structure that will be used throughout the calls, while
896 * keeping available the original structures and a few variables
897 * On entry : type = dialog procedure type (16,32A,32W)
898 * dlgType = dialog type (open or save)
900 static PFD31_DATA
FD31_AllocPrivate(LPARAM lParam
, UINT dlgType
, BOOL IsUnicode
)
902 PFD31_DATA lfs
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(FD31_DATA
));
904 TRACE("alloc private buf %p\n", lfs
);
905 if (!lfs
) return NULL
;
907 lfs
->lParam
= lParam
;
908 lfs
->open
= (dlgType
== OPEN_DIALOG
);
913 lfs
->ofnW
= (LPOPENFILENAMEW
) lParam
;
914 if (lfs
->ofnW
->Flags
& OFN_ENABLEHOOK
)
915 if (lfs
->ofnW
->lpfnHook
)
920 lfs
->ofnA
= (LPOPENFILENAMEA
) lParam
;
921 if (lfs
->ofnA
->Flags
& OFN_ENABLEHOOK
)
922 if (lfs
->ofnA
->lpfnHook
)
924 lfs
->ofnW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lfs
->ofnA
->lStructSize
);
925 lfs
->ofnW
->lStructSize
= lfs
->ofnA
->lStructSize
;
926 FD31_MapOfnStructA(lfs
->ofnA
, lfs
->ofnW
, lfs
->open
);
929 if (! FD31_GetTemplate(lfs
))
931 FD31_DestroyPrivate(lfs
);
934 lfs
->lbselchstring
= RegisterWindowMessageA(LBSELCHSTRINGA
);
935 lfs
->fileokstring
= RegisterWindowMessageA(FILEOKSTRINGA
);
940 /***********************************************************************
941 * FD31_WMInitDialog [internal]
943 static LONG
FD31_WMInitDialog(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
946 WCHAR tmpstr
[BUFFILE
];
947 LPWSTR pstr
, old_pstr
;
949 PFD31_DATA lfs
= (PFD31_DATA
) lParam
;
951 if (!lfs
) return FALSE
;
952 SetPropA(hWnd
, FD31_OFN_PROP
, lfs
);
956 TRACE("flags=%x initialdir=%s\n", ofn
->Flags
, debugstr_w(ofn
->lpstrInitialDir
));
958 SetWindowTextW( hWnd
, ofn
->lpstrTitle
);
959 /* read custom filter information */
960 if (ofn
->lpstrCustomFilter
)
962 pstr
= ofn
->lpstrCustomFilter
;
964 TRACE("lpstrCustomFilter = %p\n", pstr
);
968 i
= SendDlgItemMessageW(hWnd
, cmb1
, CB_ADDSTRING
, 0,
969 (LPARAM
)(ofn
->lpstrCustomFilter
) + n
);
970 n
+= lstrlenW(pstr
) + 1;
971 pstr
+= lstrlenW(pstr
) + 1;
972 TRACE("add str=%s associated to %s\n",
973 debugstr_w(old_pstr
), debugstr_w(pstr
));
974 SendDlgItemMessageW(hWnd
, cmb1
, CB_SETITEMDATA
, i
, (LPARAM
)pstr
);
975 n
+= lstrlenW(pstr
) + 1;
976 pstr
+= lstrlenW(pstr
) + 1;
979 /* read filter information */
980 if (ofn
->lpstrFilter
) {
981 pstr
= (LPWSTR
) ofn
->lpstrFilter
;
985 i
= SendDlgItemMessageW(hWnd
, cmb1
, CB_ADDSTRING
, 0,
986 (LPARAM
)(ofn
->lpstrFilter
+ n
) );
987 n
+= lstrlenW(pstr
) + 1;
988 pstr
+= lstrlenW(pstr
) + 1;
989 TRACE("add str=%s associated to %s\n",
990 debugstr_w(old_pstr
), debugstr_w(pstr
));
991 SendDlgItemMessageW(hWnd
, cmb1
, CB_SETITEMDATA
, i
, (LPARAM
)pstr
);
992 n
+= lstrlenW(pstr
) + 1;
993 pstr
+= lstrlenW(pstr
) + 1;
996 /* set default filter */
997 if (ofn
->nFilterIndex
== 0 && ofn
->lpstrCustomFilter
== NULL
)
998 ofn
->nFilterIndex
= 1;
999 SendDlgItemMessageW(hWnd
, cmb1
, CB_SETCURSEL
, ofn
->nFilterIndex
- 1, 0);
1000 if (ofn
->lpstrFile
&& ofn
->lpstrFile
[0])
1002 TRACE( "SetText of edt1 to %s\n", debugstr_w(ofn
->lpstrFile
) );
1003 SetDlgItemTextW( hWnd
, edt1
, ofn
->lpstrFile
);
1007 lstrcpynW(tmpstr
, FD31_GetFileType(ofn
->lpstrCustomFilter
,
1008 ofn
->lpstrFilter
, ofn
->nFilterIndex
- 1),BUFFILE
);
1009 TRACE("nFilterIndex = %d, SetText of edt1 to %s\n",
1010 ofn
->nFilterIndex
, debugstr_w(tmpstr
));
1011 SetDlgItemTextW( hWnd
, edt1
, tmpstr
);
1013 /* get drive list */
1015 DlgDirListComboBoxW(hWnd
, tmpstr
, cmb2
, 0, DDL_DRIVES
| DDL_EXCLUSIVE
);
1016 /* read initial directory */
1017 /* FIXME: Note that this is now very version-specific (See MSDN description of
1018 * the OPENFILENAME structure). For example under 2000/XP any path in the
1019 * lpstrFile overrides the lpstrInitialDir, but not under 95/98/ME
1021 if (ofn
->lpstrInitialDir
!= NULL
)
1024 lstrcpynW(tmpstr
, ofn
->lpstrInitialDir
, 511);
1025 len
= lstrlenW(tmpstr
);
1026 if (len
> 0 && tmpstr
[len
-1] != '\\' && tmpstr
[len
-1] != ':') {
1033 if (!FD31_ScanDir(ofn
, hWnd
, tmpstr
)) {
1035 if (!FD31_ScanDir(ofn
, hWnd
, tmpstr
))
1036 WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr
));
1038 /* select current drive in combo 2, omit missing drives */
1041 char str
[4] = "a:\\";
1042 GetCurrentDirectoryA( sizeof(dir
), dir
);
1043 for(i
= 0, n
= -1; i
< 26; i
++)
1046 if (GetDriveTypeA(str
) > DRIVE_NO_ROOT_DIR
) n
++;
1047 if (toupper(str
[0]) == toupper(dir
[0])) break;
1050 SendDlgItemMessageW(hWnd
, cmb2
, CB_SETCURSEL
, n
, 0);
1051 if (!(ofn
->Flags
& OFN_SHOWHELP
))
1052 ShowWindow(GetDlgItem(hWnd
, pshHelp
), SW_HIDE
);
1053 if (ofn
->Flags
& OFN_HIDEREADONLY
)
1054 ShowWindow(GetDlgItem(hWnd
, chx1
), SW_HIDE
);
1056 return FD31_CallWindowProc(lfs
, WM_INITDIALOG
, wParam
, lfs
->lParam
);
1060 static int FD31_GetFldrHeight(void)
1065 /***********************************************************************
1066 * FD31_WMMeasureItem [internal]
1068 static LONG
FD31_WMMeasureItem(LPARAM lParam
)
1070 LPMEASUREITEMSTRUCT lpmeasure
;
1072 lpmeasure
= (LPMEASUREITEMSTRUCT
)lParam
;
1073 lpmeasure
->itemHeight
= FD31_GetFldrHeight();
1078 /***********************************************************************
1079 * FileOpenDlgProc [internal]
1080 * Used for open and save, in fact.
1082 static INT_PTR CALLBACK
FD31_FileOpenDlgProc(HWND hWnd
, UINT wMsg
,
1083 WPARAM wParam
, LPARAM lParam
)
1085 PFD31_DATA lfs
= (PFD31_DATA
)GetPropA( hWnd
, FD31_OFN_PROP
);
1087 TRACE("msg=%x wparam=%lx lParam=%lx\n", wMsg
, wParam
, lParam
);
1088 if ((wMsg
!= WM_INITDIALOG
) && lfs
&& lfs
->hook
)
1091 lRet
= (INT_PTR
)FD31_CallWindowProc( lfs
, wMsg
, wParam
, lParam
);
1092 if (lRet
) return lRet
; /* else continue message processing */
1097 return FD31_WMInitDialog( hWnd
, wParam
, lParam
);
1099 case WM_MEASUREITEM
:
1100 return FD31_WMMeasureItem( lParam
);
1103 return FD31_WMDrawItem( hWnd
, wParam
, lParam
, !lfs
->open
, (DRAWITEMSTRUCT
*)lParam
);
1106 return FD31_WMCommand( hWnd
, lParam
, HIWORD(wParam
), LOWORD(wParam
), lfs
);
1109 SetBkColor( (HDC16
)wParam
, 0x00C0C0C0 );
1110 switch (HIWORD(lParam
))
1113 SetTextColor( (HDC16
)wParam
, 0x00000000 );
1115 case CTLCOLOR_STATIC
:
1116 SetTextColor( (HDC16
)wParam
, 0x00000000 );
1125 /***********************************************************************
1126 * GetFileName31A [internal]
1128 * Creates a win31 style dialog box for the user to select a file to open/save.
1130 BOOL
GetFileName31A( OPENFILENAMEA
*lpofn
, UINT dlgType
)
1135 if (!lpofn
|| !FD31_Init()) return FALSE
;
1137 TRACE("ofn flags %08x\n", lpofn
->Flags
);
1138 lfs
= FD31_AllocPrivate((LPARAM
) lpofn
, dlgType
, FALSE
);
1141 bRet
= DialogBoxIndirectParamA( COMDLG32_hInstance
, lfs
->template, lpofn
->hwndOwner
,
1142 FD31_FileOpenDlgProc
, (LPARAM
)lfs
);
1143 FD31_DestroyPrivate(lfs
);
1146 TRACE("return lpstrFile='%s' !\n", lpofn
->lpstrFile
);
1150 /***********************************************************************
1151 * GetFileName31W [internal]
1153 * Creates a win31 style dialog box for the user to select a file to open/save
1155 BOOL
GetFileName31W( OPENFILENAMEW
*lpofn
, UINT dlgType
)
1160 if (!lpofn
|| !FD31_Init()) return FALSE
;
1162 lfs
= FD31_AllocPrivate((LPARAM
) lpofn
, dlgType
, TRUE
);
1165 bRet
= DialogBoxIndirectParamW( COMDLG32_hInstance
, lfs
->template, lpofn
->hwndOwner
,
1166 FD31_FileOpenDlgProc
, (LPARAM
)lfs
);
1167 FD31_DestroyPrivate(lfs
);
1170 TRACE("file %s, file offset %d, ext offset %d\n",
1171 debugstr_w(lpofn
->lpstrFile
), lpofn
->nFileOffset
, lpofn
->nFileExtension
);