Link to ntdll.
[wine.git] / dlls / commdlg / filedlg16.c
blob4c3bbdd979a0dc200a08be809c44c4ca6b8c7758
1 /*
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
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "wine/winbase16.h"
32 #include "wine/winuser16.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
35 #include "cderr.h"
36 #include "winreg.h"
37 #include "winternl.h"
38 #include "winuser.h"
39 #include "commdlg.h"
40 #include "cderr.h"
41 #include "winreg.h"
42 #include "winternl.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
46 #include "cdlg.h"
47 #include "cdlg16.h"
49 #define BUFFILE 512
50 #define BUFFILEALLOC 512 * sizeof(WCHAR)
52 struct FSPRIVATE
54 HWND hwnd; /* file dialog window handle */
55 BOOL hook; /* TRUE if the dialog is hooked */
56 UINT lbselchstring; /* registered message id */
57 UINT fileokstring; /* registered message id */
58 LPARAM lParam; /* save original lparam */
59 HANDLE16 hDlgTmpl16; /* handle for resource 16 */
60 HANDLE16 hResource16; /* handle for allocated resource 16 */
61 HANDLE16 hGlobal16; /* 16 bits mem block (resources) */
62 LPCVOID template; /* template for 32 bits resource */
63 BOOL open; /* TRUE if open dialog, FALSE if save dialog */
64 OPENFILENAMEW *ofnW; /* original structure or work struct */
65 OPENFILENAMEA *ofnA; /* original structure if 32bits ansi dialog */
66 OPENFILENAME16 *ofn16; /* original structure if 16 bits dialog */
69 #define LFSPRIVATE struct FSPRIVATE *
70 #define LFS16 1
71 #define LFS32A 2
72 #define LFS32W 3
73 #define OFN_PROP "FILEDLG_OFN"
75 static const WCHAR FILE_star[] = {'*','.','*', 0};
76 static const WCHAR FILE_bslash[] = {'\\', 0};
77 static const WCHAR FILE_specc[] = {'%','c',':', 0};
78 static const int fldrHeight = 16;
79 static const int fldrWidth = 20;
81 static HICON hFolder = 0;
82 static HICON hFolder2 = 0;
83 static HICON hFloppy = 0;
84 static HICON hHDisk = 0;
85 static HICON hCDRom = 0;
86 static HICON hNet = 0;
88 /***********************************************************************
89 * FileDlg_Init [internal]
91 static BOOL FileDlg_Init(void)
93 static BOOL initialized = 0;
95 if (!initialized) {
96 hFolder = LoadImageA( COMDLG32_hInstance, "FOLDER", IMAGE_ICON, 16, 16, LR_SHARED );
97 hFolder2 = LoadImageA( COMDLG32_hInstance, "FOLDER2", IMAGE_ICON, 16, 16, LR_SHARED );
98 hFloppy = LoadImageA( COMDLG32_hInstance, "FLOPPY", IMAGE_ICON, 16, 16, LR_SHARED );
99 hHDisk = LoadImageA( COMDLG32_hInstance, "HDISK", IMAGE_ICON, 16, 16, LR_SHARED );
100 hCDRom = LoadImageA( COMDLG32_hInstance, "CDROM", IMAGE_ICON, 16, 16, LR_SHARED );
101 hNet = LoadImageA( COMDLG32_hInstance, "NETWORK", IMAGE_ICON, 16, 16, LR_SHARED );
102 if (hFolder == 0 || hFolder2 == 0 || hFloppy == 0 ||
103 hHDisk == 0 || hCDRom == 0 || hNet == 0)
105 ERR("Error loading icons !\n");
106 return FALSE;
108 initialized = TRUE;
110 return TRUE;
113 /***********************************************************************
114 * Get32BitsTemplate [internal]
116 * Get a template (or FALSE if failure) when 16 bits dialogs are used
117 * by a 32 bits application
120 BOOL Get32BitsTemplate(LFSPRIVATE lfs)
122 LPOPENFILENAMEW ofnW = lfs->ofnW;
123 HANDLE hDlgTmpl;
125 if (ofnW->Flags & OFN_ENABLETEMPLATEHANDLE)
127 if (!(lfs->template = LockResource( ofnW->hInstance )))
129 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
130 return FALSE;
133 else if (ofnW->Flags & OFN_ENABLETEMPLATE)
135 HRSRC hResInfo;
136 if (lfs->ofnA)
137 hResInfo = FindResourceA(lfs->ofnA->hInstance,
138 lfs->ofnA->lpTemplateName,
139 (LPSTR)RT_DIALOG);
140 else
141 hResInfo = FindResourceW(ofnW->hInstance,
142 ofnW->lpTemplateName,
143 (LPWSTR)RT_DIALOG);
144 if (!hResInfo)
146 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
147 return FALSE;
149 if (!(hDlgTmpl = LoadResource(ofnW->hInstance,
150 hResInfo)) ||
151 !(lfs->template = LockResource(hDlgTmpl)))
153 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
154 return FALSE;
156 } else { /* get it from internal Wine resource */
157 HRSRC hResInfo;
158 if (!(hResInfo = FindResourceA(COMDLG32_hInstance,
159 lfs->open? "OPEN_FILE":"SAVE_FILE", (LPSTR)RT_DIALOG)))
161 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
162 return FALSE;
164 if (!(hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo )) ||
165 !(lfs->template = LockResource( hDlgTmpl )))
167 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
168 return FALSE;
171 return TRUE;
174 /***********************************************************************
175 * Get16BitsTemplate [internal]
177 * Get a template (FALSE if failure) when 16 bits dialogs are used
178 * by a 16 bits application
181 BOOL Get16BitsTemplate(LFSPRIVATE lfs)
183 LPOPENFILENAME16 ofn16 = lfs->ofn16;
184 LPCVOID template;
185 HGLOBAL16 hGlobal16 = 0;
187 if (ofn16->Flags & OFN_ENABLETEMPLATEHANDLE)
188 lfs->hDlgTmpl16 = ofn16->hInstance;
189 else if (ofn16->Flags & OFN_ENABLETEMPLATE)
191 HANDLE16 hResInfo;
192 if (!(hResInfo = FindResource16(ofn16->hInstance,
193 MapSL(ofn16->lpTemplateName),
194 (LPSTR)RT_DIALOG)))
196 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
197 return FALSE;
199 if (!(lfs->hDlgTmpl16 = LoadResource16( ofn16->hInstance, hResInfo )))
201 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
202 return FALSE;
204 lfs->hResource16 = lfs->hDlgTmpl16;
206 else
207 { /* get resource from (32 bits) own Wine resource; convert it to 16 */
208 HRSRC hResInfo;
209 HGLOBAL hDlgTmpl32;
210 LPCVOID template32;
211 DWORD size;
213 if (!(hResInfo = FindResourceA(COMDLG32_hInstance,
214 lfs->open ? "OPEN_FILE":"SAVE_FILE", (LPSTR)RT_DIALOG)))
216 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
217 return FALSE;
219 if (!(hDlgTmpl32 = LoadResource(COMDLG32_hInstance, hResInfo )) ||
220 !(template32 = LockResource( hDlgTmpl32 )))
222 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
223 return FALSE;
225 size = SizeofResource(COMDLG32_hInstance, hResInfo);
226 hGlobal16 = GlobalAlloc16(0, size);
227 if (!hGlobal16)
229 COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
230 ERR("alloc failure for %ld bytes\n", size);
231 return FALSE;
233 template = GlobalLock16(hGlobal16);
234 if (!template)
236 COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE);
237 ERR("global lock failure for %x handle\n", hGlobal16);
238 GlobalFree16(hGlobal16);
239 return FALSE;
241 ConvertDialog32To16((LPVOID)template32, size, (LPVOID)template);
242 lfs->hDlgTmpl16 = hGlobal16;
243 lfs->hGlobal16 = hGlobal16;
245 return TRUE;
248 /***********************************************************************
249 * FILEDLG_StripEditControl [internal]
250 * Strip pathnames off the contents of the edit control.
252 static void FILEDLG_StripEditControl(HWND hwnd)
254 WCHAR temp[BUFFILE], *cp;
256 GetDlgItemTextW( hwnd, edt1, temp, sizeof(temp)/sizeof(WCHAR));
257 cp = strrchrW(temp, '\\');
258 if (cp != NULL) {
259 strcpyW(temp, cp+1);
261 cp = strrchrW(temp, ':');
262 if (cp != NULL) {
263 strcpyW(temp, cp+1);
265 /* FIXME: shouldn't we do something with the result here? ;-) */
268 /***********************************************************************
269 * FILEDLG_CallWindowProc [internal]
271 * Call the appropriate hook
273 static BOOL FILEDLG_CallWindowProc(LFSPRIVATE lfs, UINT wMsg, WPARAM wParam,
274 LPARAM lParam)
276 if (lfs->ofnA)
278 return (BOOL) CallWindowProcA(
279 (WNDPROC)lfs->ofnA->lpfnHook, lfs->hwnd,
280 wMsg, wParam, lParam);
283 if (lfs->ofnW)
285 return (BOOL) CallWindowProcW(
286 (WNDPROC)lfs->ofnW->lpfnHook, lfs->hwnd,
287 wMsg, wParam, lParam);
289 return FALSE;
292 /***********************************************************************
293 * FILEDLG_ScanDir [internal]
295 static BOOL FILEDLG_ScanDir(HWND hWnd, LPWSTR newPath)
297 WCHAR buffer[BUFFILE];
298 HWND hdlg, hdlgDir;
299 LRESULT lRet = TRUE;
300 HCURSOR hCursorWait, oldCursor;
302 TRACE("Trying to change to %s\n", debugstr_w(newPath));
303 if ( !SetCurrentDirectoryW( newPath ))
304 return FALSE;
305 lstrcpynW(buffer, newPath, sizeof(buffer)/sizeof(WCHAR));
307 /* get the list of spec files */
308 GetDlgItemTextW(hWnd, edt1, buffer, sizeof(buffer)/sizeof(WCHAR));
310 hCursorWait = LoadCursorA(0, (LPSTR)IDC_WAIT);
311 oldCursor = SetCursor(hCursorWait);
313 /* list of files */
314 if ((hdlg = GetDlgItem(hWnd, lst1)) != 0) {
315 WCHAR* scptr; /* ptr on semi-colon */
316 WCHAR* filter = buffer;
318 TRACE("Using filter %s\n", debugstr_w(filter));
319 SendMessageW(hdlg, LB_RESETCONTENT, 0, 0);
320 while (filter) {
321 scptr = strchrW(filter, ';');
322 if (scptr) *scptr = 0;
323 while (*filter == ' ') filter++;
324 TRACE("Using file spec %s\n", debugstr_w(filter));
325 if (SendMessageW(hdlg, LB_DIR, 0, (LPARAM)filter) == LB_ERR)
326 return FALSE;
327 if (scptr) *scptr = ';';
328 filter = (scptr) ? (scptr + 1) : 0;
332 /* list of directories */
333 strcpyW(buffer, FILE_star);
335 if ((hdlgDir = GetDlgItem(hWnd, lst2)) != 0) {
336 lRet = DlgDirListW(hWnd, buffer, lst2, stc1, DDL_EXCLUSIVE | DDL_DIRECTORY);
338 SetCursor(oldCursor);
339 return lRet;
342 /***********************************************************************
343 * FILEDLG_GetFileType [internal]
346 static LPWSTR FILEDLG_GetFileType(LPWSTR cfptr, LPWSTR fptr, WORD index)
348 int n, i;
349 i = 0;
350 if (cfptr)
351 for ( ;(n = lstrlenW(cfptr)) != 0; i++)
353 cfptr += n + 1;
354 if (i == index)
355 return cfptr;
356 cfptr += lstrlenW(cfptr) + 1;
358 if (fptr)
359 for ( ;(n = lstrlenW(fptr)) != 0; i++)
361 fptr += n + 1;
362 if (i == index)
363 return fptr;
364 fptr += lstrlenW(fptr) + 1;
366 return (LPWSTR) FILE_star; /* FIXME */
369 /***********************************************************************
370 * FILEDLG_WMDrawItem [internal]
372 static LONG FILEDLG_WMDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam,
373 int savedlg, LPDRAWITEMSTRUCT lpdis)
375 WCHAR *str;
376 HICON hIcon;
377 COLORREF oldText = 0, oldBk = 0;
379 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst1)
381 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC))) return FALSE;
382 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
383 (LPARAM)str);
385 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
387 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
388 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
390 if (savedlg)
391 SetTextColor(lpdis->hDC,GetSysColor(COLOR_GRAYTEXT) );
393 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + 1,
394 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
395 &(lpdis->rcItem), str, lstrlenW(str), NULL);
397 if (lpdis->itemState & ODS_SELECTED)
398 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
400 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
402 SetBkColor( lpdis->hDC, oldBk );
403 SetTextColor( lpdis->hDC, oldText );
405 HeapFree(GetProcessHeap(), 0, str);
406 return TRUE;
409 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst2)
411 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
412 return FALSE;
413 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
414 (LPARAM)str);
416 if (lpdis->itemState & ODS_SELECTED)
418 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
419 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
421 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
422 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
423 &(lpdis->rcItem), str, lstrlenW(str), NULL);
425 if (lpdis->itemState & ODS_SELECTED)
426 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
428 if (lpdis->itemState & ODS_SELECTED)
430 SetBkColor( lpdis->hDC, oldBk );
431 SetTextColor( lpdis->hDC, oldText );
433 DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hFolder);
434 HeapFree(GetProcessHeap(), 0, str);
435 return TRUE;
437 if (lpdis->CtlType == ODT_COMBOBOX && lpdis->CtlID == cmb2)
439 char root[] = "a:";
440 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
441 return FALSE;
442 SendMessageW(lpdis->hwndItem, CB_GETLBTEXT, lpdis->itemID,
443 (LPARAM)str);
444 root[0] += str[2] - 'a';
445 switch(GetDriveTypeA(root))
447 case DRIVE_REMOVABLE: hIcon = hFloppy; break;
448 case DRIVE_CDROM: hIcon = hCDRom; break;
449 case DRIVE_REMOTE: hIcon = hNet; break;
450 case DRIVE_FIXED:
451 default: hIcon = hHDisk; break;
453 if (lpdis->itemState & ODS_SELECTED)
455 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
456 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
458 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
459 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
460 &(lpdis->rcItem), str, lstrlenW(str), NULL);
462 if (lpdis->itemState & ODS_SELECTED)
464 SetBkColor( lpdis->hDC, oldBk );
465 SetTextColor( lpdis->hDC, oldText );
467 DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hIcon);
468 HeapFree(GetProcessHeap(), 0, str);
469 return TRUE;
471 return FALSE;
474 /***********************************************************************
475 * FILEDLG_UpdateResult [internal]
476 * update the displayed file name (with path)
478 void FILEDLG_UpdateResult(LFSPRIVATE lfs, WCHAR *tmpstr)
480 int lenstr2;
481 LPOPENFILENAMEW ofnW = lfs->ofnW;
482 WCHAR tmpstr2[BUFFILE];
483 WCHAR *bs;
485 TRACE("%s\n", debugstr_w(tmpstr));
486 if(ofnW->Flags & OFN_NOVALIDATE)
487 tmpstr2[0] = '\0';
488 else
489 GetCurrentDirectoryW(BUFFILE, tmpstr2);
490 lenstr2 = strlenW(tmpstr2);
491 if (lenstr2 > 3)
492 tmpstr2[lenstr2++]='\\';
493 lstrcpynW(tmpstr2+lenstr2, tmpstr, BUFFILE-lenstr2);
494 if (ofnW->lpstrFile)
495 lstrcpynW(ofnW->lpstrFile, tmpstr2, ofnW->nMaxFile);
496 if((bs = strrchrW(tmpstr2, '\\')) != NULL)
497 ofnW->nFileOffset = bs - tmpstr2 +1;
498 else
499 ofnW->nFileOffset = 0;
500 ofnW->nFileExtension = 0;
501 while(tmpstr2[ofnW->nFileExtension] != '.' && tmpstr2[ofnW->nFileExtension] != '\0')
502 ofnW->nFileExtension++;
503 if (tmpstr2[ofnW->nFileExtension] == '\0')
504 ofnW->nFileExtension = 0;
505 else
506 ofnW->nFileExtension++;
507 /* update the real client structures if any */
508 if (lfs->ofn16)
509 { /* we have to convert to short (8.3) path */
510 char tmp[1024]; /* MAX_PATHNAME_LEN */
511 LPOPENFILENAME16 ofn16 = lfs->ofn16;
512 char *dest = MapSL(ofn16->lpstrFile);
513 char *bs16;
514 if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFile, -1,
515 tmp, sizeof(tmp), NULL, NULL ))
516 tmp[sizeof(tmp)-1] = 0;
517 GetShortPathNameA(tmp, dest, ofn16->nMaxFile);
519 /* the same procedure as every year... */
520 if((bs16 = strrchr(dest, '\\')) != NULL)
521 ofn16->nFileOffset = bs16 - dest +1;
522 else
523 ofn16->nFileOffset = 0;
524 ofn16->nFileExtension = 0;
525 while(dest[ofn16->nFileExtension] != '.' && dest[ofn16->nFileExtension] != '\0')
526 ofn16->nFileExtension++;
527 if (dest[ofn16->nFileExtension] == '\0')
528 ofn16->nFileExtension = 0;
529 else
530 ofn16->nFileExtension++;
532 if (lfs->ofnA)
534 if (ofnW->nMaxFile &&
535 !WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFile, -1,
536 lfs->ofnA->lpstrFile, ofnW->nMaxFile, NULL, NULL ))
537 lfs->ofnA->lpstrFile[ofnW->nMaxFile-1] = 0;
538 lfs->ofnA->nFileOffset = ofnW->nFileOffset;
539 lfs->ofnA->nFileExtension = ofnW->nFileExtension;
543 /***********************************************************************
544 * FILEDLG_UpdateFileTitle [internal]
545 * update the displayed file name (without path)
547 void FILEDLG_UpdateFileTitle(LFSPRIVATE lfs)
549 LONG lRet;
550 LPOPENFILENAMEW ofnW = lfs->ofnW;
551 if (ofnW->lpstrFileTitle != NULL)
553 lRet = SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
554 SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETTEXT, lRet,
555 (LPARAM)ofnW->lpstrFileTitle );
556 if (lfs->ofn16)
558 char *dest = MapSL(lfs->ofn16->lpstrFileTitle);
559 if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFileTitle, -1,
560 dest, ofnW->nMaxFileTitle, NULL, NULL ))
561 dest[ofnW->nMaxFileTitle-1] = 0;
563 if (lfs->ofnA)
565 if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFileTitle, -1,
566 lfs->ofnA->lpstrFileTitle, ofnW->nMaxFileTitle, NULL, NULL ))
567 lfs->ofnA->lpstrFileTitle[ofnW->nMaxFileTitle-1] = 0;
572 /***********************************************************************
573 * FILEDLG_DirListDblClick [internal]
575 static LRESULT FILEDLG_DirListDblClick( LFSPRIVATE lfs )
577 LONG lRet;
578 HWND hWnd = lfs->hwnd;
579 LPWSTR pstr;
580 WCHAR tmpstr[BUFFILE];
582 /* get the raw string (with brackets) */
583 lRet = SendDlgItemMessageW(hWnd, lst2, LB_GETCURSEL, 0, 0);
584 if (lRet == LB_ERR) return TRUE;
585 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
586 SendDlgItemMessageW(hWnd, lst2, LB_GETTEXT, lRet,
587 (LPARAM)pstr);
588 strcpyW( tmpstr, pstr );
589 HeapFree(GetProcessHeap(), 0, pstr);
590 /* get the selected directory in tmpstr */
591 if (tmpstr[0] == '[')
593 tmpstr[lstrlenW(tmpstr) - 1] = 0;
594 strcpyW(tmpstr,tmpstr+1);
596 strcatW(tmpstr, FILE_bslash);
598 FILEDLG_ScanDir(hWnd, tmpstr);
599 /* notify the app */
600 if (lfs->hook)
602 if (FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, lst2,
603 MAKELONG(lRet,CD_LBSELCHANGE)))
604 return TRUE;
606 return TRUE;
609 /***********************************************************************
610 * FILEDLG_FileListSelect [internal]
611 * called when a new item is picked in the file list
613 static LRESULT FILEDLG_FileListSelect( LFSPRIVATE lfs )
615 LONG lRet;
616 HWND hWnd = lfs->hwnd;
617 LPWSTR pstr;
619 lRet = SendDlgItemMessageW(hWnd, lst1, LB_GETCURSEL16, 0, 0);
620 if (lRet == LB_ERR)
621 return TRUE;
623 /* set the edit control to the choosen file */
624 if ((pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
626 SendDlgItemMessageW(hWnd, lst1, LB_GETTEXT, lRet,
627 (LPARAM)pstr);
628 SetDlgItemTextW( hWnd, edt1, pstr );
629 HeapFree(GetProcessHeap(), 0, pstr);
631 if (lfs->hook)
633 FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, lst1,
634 MAKELONG(lRet,CD_LBSELCHANGE));
636 /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD,
637 CD_LBSELNOITEMS */
638 return TRUE;
641 /***********************************************************************
642 * FILEDLG_TestPath [internal]
643 * before accepting the file name, test if it includes wild cards
644 * tries to scan the directory and returns TRUE if no error.
646 static LRESULT FILEDLG_TestPath( LFSPRIVATE lfs, LPWSTR path )
648 HWND hWnd = lfs->hwnd;
649 LPWSTR pBeginFileName, pstr2;
650 WCHAR tmpstr2[BUFFILE];
652 pBeginFileName = strrchrW(path, '\\');
653 if (pBeginFileName == NULL)
654 pBeginFileName = strrchrW(path, ':');
656 if (strchrW(path,'*') != NULL || strchrW(path,'?') != NULL)
658 /* edit control contains wildcards */
659 if (pBeginFileName != NULL)
661 lstrcpynW(tmpstr2, pBeginFileName + 1, BUFFILE);
662 *(pBeginFileName + 1) = 0;
664 else
666 strcpyW(tmpstr2, path);
667 if(!(lfs->ofnW->Flags & OFN_NOVALIDATE))
668 *path = 0;
671 TRACE("path=%s, tmpstr2=%s\n", debugstr_w(path), debugstr_w(tmpstr2));
672 SetDlgItemTextW( hWnd, edt1, tmpstr2 );
673 FILEDLG_ScanDir(hWnd, path);
674 return (lfs->ofnW->Flags & OFN_NOVALIDATE) ? TRUE : FALSE;
677 /* no wildcards, we might have a directory or a filename */
678 /* try appending a wildcard and reading the directory */
680 pstr2 = path + lstrlenW(path);
681 if (pBeginFileName == NULL || *(pBeginFileName + 1) != 0)
682 strcatW(path, FILE_bslash);
684 /* if ScanDir succeeds, we have changed the directory */
685 if (FILEDLG_ScanDir(hWnd, path))
686 return FALSE; /* and path is not a valid file name */
688 /* if not, this must be a filename */
690 *pstr2 = 0; /* remove the wildcard added before */
692 if (pBeginFileName != NULL)
694 /* strip off the pathname */
695 *pBeginFileName = 0;
696 SetDlgItemTextW( hWnd, edt1, pBeginFileName + 1 );
698 lstrcpynW(tmpstr2, pBeginFileName + 1, sizeof(tmpstr2)/sizeof(WCHAR) );
699 /* Should we MessageBox() if this fails? */
700 if (!FILEDLG_ScanDir(hWnd, path))
702 return FALSE;
704 strcpyW(path, tmpstr2);
706 else
707 SetDlgItemTextW( hWnd, edt1, path );
708 return TRUE;
711 /***********************************************************************
712 * FILEDLG_Validate [internal]
713 * called on: click Ok button, Enter in edit, DoubleClick in file list
715 static LRESULT FILEDLG_Validate( LFSPRIVATE lfs, LPWSTR path, UINT control, INT itemIndex,
716 BOOL internalUse )
718 LONG lRet;
719 HWND hWnd = lfs->hwnd;
720 OPENFILENAMEW ofnsav;
721 LPOPENFILENAMEW ofnW = lfs->ofnW;
722 WCHAR filename[BUFFILE];
724 ofnsav = *ofnW; /* for later restoring */
726 /* get current file name */
727 if (path)
728 lstrcpynW(filename, path, sizeof(filename)/sizeof(WCHAR));
729 else
730 GetDlgItemTextW( hWnd, edt1, filename, sizeof(filename)/sizeof(WCHAR));
732 TRACE("got filename = %s\n", debugstr_w(filename));
733 /* if we did not click in file list to get there */
734 if (control != lst1)
736 if (!FILEDLG_TestPath( lfs, filename) )
737 return FALSE;
739 FILEDLG_UpdateResult(lfs, filename);
741 if (internalUse)
742 { /* called internally after a change in a combo */
743 if (lfs->hook)
745 FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, control,
746 MAKELONG(itemIndex,CD_LBSELCHANGE));
748 return TRUE;
751 FILEDLG_UpdateFileTitle(lfs);
752 if (lfs->hook)
754 lRet = (BOOL)FILEDLG_CallWindowProc(lfs, lfs->fileokstring,
755 0, lfs->lParam );
756 if (lRet)
758 *ofnW = ofnsav; /* restore old state */
759 return FALSE;
762 if ((ofnW->Flags & OFN_ALLOWMULTISELECT) && (ofnW->Flags & OFN_EXPLORER))
764 if (ofnW->lpstrFile)
766 LPWSTR str = (LPWSTR)ofnW->lpstrFile;
767 LPWSTR ptr = strrchrW(str, '\\');
768 str[lstrlenW(str) + 1] = '\0';
769 *ptr = 0;
772 return TRUE;
775 /***********************************************************************
776 * FILEDLG_DiskChange [internal]
777 * called when a new item is picked in the disk selection combo
779 static LRESULT FILEDLG_DiskChange( LFSPRIVATE lfs )
781 LONG lRet;
782 HWND hWnd = lfs->hwnd;
783 LPWSTR pstr;
784 WCHAR diskname[BUFFILE];
786 FILEDLG_StripEditControl(hWnd);
787 lRet = SendDlgItemMessageW(hWnd, cmb2, CB_GETCURSEL, 0, 0L);
788 if (lRet == LB_ERR)
789 return 0;
790 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
791 SendDlgItemMessageW(hWnd, cmb2, CB_GETLBTEXT, lRet,
792 (LPARAM)pstr);
793 wsprintfW(diskname, FILE_specc, pstr[2]);
794 HeapFree(GetProcessHeap(), 0, pstr);
796 return FILEDLG_Validate( lfs, diskname, cmb2, lRet, TRUE );
799 /***********************************************************************
800 * FILEDLG_FileTypeChange [internal]
801 * called when a new item is picked in the file type combo
803 static LRESULT FILEDLG_FileTypeChange( LFSPRIVATE lfs )
805 LONG lRet;
806 WCHAR diskname[BUFFILE];
807 LPWSTR pstr;
809 diskname[0] = 0;
811 lRet = SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETCURSEL, 0, 0);
812 if (lRet == LB_ERR)
813 return TRUE;
814 pstr = (LPWSTR)SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETITEMDATA, lRet, 0);
815 TRACE("Selected filter : %s\n", debugstr_w(pstr));
816 SetDlgItemTextW( lfs->hwnd, edt1, pstr );
818 return FILEDLG_Validate( lfs, NULL, cmb1, lRet, TRUE );
821 /***********************************************************************
822 * FILEDLG_WMCommand [internal]
824 static LRESULT FILEDLG_WMCommand(HWND hWnd, LPARAM lParam, UINT notification,
825 UINT control, LFSPRIVATE lfs )
827 switch (control)
829 case lst1: /* file list */
830 FILEDLG_StripEditControl(hWnd);
831 if (notification == LBN_DBLCLK)
833 if (FILEDLG_Validate( lfs, NULL, control, 0, FALSE ))
834 EndDialog(hWnd, TRUE);
835 return TRUE;
837 else if (notification == LBN_SELCHANGE)
838 return FILEDLG_FileListSelect( lfs );
839 break;
841 case lst2: /* directory list */
842 FILEDLG_StripEditControl(hWnd);
843 if (notification == LBN_DBLCLK)
844 return FILEDLG_DirListDblClick( lfs );
845 break;
847 case cmb1: /* file type drop list */
848 if (notification == CBN_SELCHANGE)
849 return FILEDLG_FileTypeChange( lfs );
850 break;
852 case chx1:
853 break;
855 case pshHelp:
856 break;
858 case cmb2: /* disk dropdown combo */
859 if (notification == CBN_SELCHANGE)
860 return FILEDLG_DiskChange( lfs );
861 break;
863 case IDOK:
864 TRACE("OK pressed\n");
865 if (FILEDLG_Validate( lfs, NULL, control, 0, FALSE ))
866 EndDialog(hWnd, TRUE);
867 return TRUE;
869 case IDCANCEL:
870 EndDialog(hWnd, FALSE);
871 return TRUE;
873 case IDABORT: /* can be sent by the hook procedure */
874 EndDialog(hWnd, TRUE);
875 return TRUE;
877 return FALSE;
880 /***********************************************************************
881 * FILEDLG_MapDrawItemStruct [internal]
882 * map a 16 bits drawitem struct to 32
884 static void FILEDLG_MapDrawItemStruct(LPDRAWITEMSTRUCT16 lpdis16, LPDRAWITEMSTRUCT lpdis)
886 lpdis->CtlType = lpdis16->CtlType;
887 lpdis->CtlID = lpdis16->CtlID;
888 lpdis->itemID = lpdis16->itemID;
889 lpdis->itemAction = lpdis16->itemAction;
890 lpdis->itemState = lpdis16->itemState;
891 lpdis->hwndItem = HWND_32(lpdis16->hwndItem);
892 lpdis->hDC = HDC_32(lpdis16->hDC);
893 lpdis->rcItem.right = lpdis16->rcItem.right;
894 lpdis->rcItem.left = lpdis16->rcItem.left;
895 lpdis->rcItem.top = lpdis16->rcItem.top;
896 lpdis->rcItem.bottom = lpdis16->rcItem.bottom;
897 lpdis->itemData = lpdis16->itemData;
900 /************************************************************************
901 * FILEDLG_MapStringPairsToW [internal]
902 * map string pairs to Unicode
904 static LPWSTR FILEDLG_MapStringPairsToW(LPCSTR strA, UINT size)
906 LPCSTR s;
907 LPWSTR x;
908 int n, len;
910 s = strA;
911 while (*s)
912 s = s+strlen(s)+1;
913 s++;
914 n = s + 1 - strA; /* Don't forget the other \0 */
915 if (n < size) n = size;
917 len = MultiByteToWideChar( CP_ACP, 0, strA, n, NULL, 0 );
918 x = HeapAlloc(GetProcessHeap(),0, len * sizeof(WCHAR));
919 MultiByteToWideChar( CP_ACP, 0, strA, n, x, len );
920 return x;
924 /************************************************************************
925 * FILEDLG_DupToW [internal]
926 * duplicates an Ansi string to unicode, with a buffer size
928 LPWSTR FILEDLG_DupToW(LPCSTR str, DWORD size)
930 LPWSTR strW = NULL;
931 if (str && (size > 0))
933 strW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
934 if (strW) MultiByteToWideChar( CP_ACP, 0, str, -1, strW, size );
936 return strW;
940 /************************************************************************
941 * FILEDLG_MapOfnStructA [internal]
942 * map a 32 bits Ansi structure to an Unicode one
944 void FILEDLG_MapOfnStructA(LPOPENFILENAMEA ofnA, LPOPENFILENAMEW ofnW, BOOL open)
946 LPCSTR str;
947 UNICODE_STRING usBuffer;
949 ofnW->lStructSize = sizeof(OPENFILENAMEW);
950 ofnW->hwndOwner = ofnA->hwndOwner;
951 ofnW->hInstance = ofnA->hInstance;
952 if (ofnA->lpstrFilter)
953 ofnW->lpstrFilter = FILEDLG_MapStringPairsToW(ofnA->lpstrFilter, 0);
955 if ((ofnA->lpstrCustomFilter) && (*(ofnA->lpstrCustomFilter)))
956 ofnW->lpstrCustomFilter = FILEDLG_MapStringPairsToW(ofnA->lpstrCustomFilter, ofnA->nMaxCustFilter);
957 ofnW->nMaxCustFilter = ofnA->nMaxCustFilter;
958 ofnW->nFilterIndex = ofnA->nFilterIndex;
959 ofnW->nMaxFile = ofnA->nMaxFile;
960 ofnW->lpstrFile = FILEDLG_DupToW(ofnA->lpstrFile, ofnW->nMaxFile);
961 ofnW->nMaxFileTitle = ofnA->nMaxFileTitle;
962 ofnW->lpstrFileTitle = FILEDLG_DupToW(ofnA->lpstrFileTitle, ofnW->nMaxFileTitle);
963 if (ofnA->lpstrInitialDir)
965 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpstrInitialDir);
966 ofnW->lpstrInitialDir = usBuffer.Buffer;
968 if (ofnA->lpstrTitle)
969 str = ofnA->lpstrTitle;
970 else
971 /* Allocates default title (FIXME : get it from resource) */
972 str = open ? "Open File" : "Save as";
973 RtlCreateUnicodeStringFromAsciiz (&usBuffer,str);
974 ofnW->lpstrTitle = usBuffer.Buffer;
975 ofnW->Flags = ofnA->Flags;
976 ofnW->nFileOffset = ofnA->nFileOffset;
977 ofnW->nFileExtension = ofnA->nFileExtension;
978 ofnW->lpstrDefExt = FILEDLG_DupToW(ofnA->lpstrDefExt, 3);
979 if ((ofnA->Flags & OFN_ENABLETEMPLATE) && (ofnA->lpTemplateName))
981 if (HIWORD(ofnA->lpTemplateName))
983 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpTemplateName);
984 ofnW->lpTemplateName = usBuffer.Buffer;
986 else /* numbered resource */
987 ofnW->lpTemplateName = (LPWSTR) ofnA->lpTemplateName;
991 /************************************************************************
992 * FILEDLG_MapOfnStruct16 [internal]
993 * map a 16 bits structure to an Unicode one
995 void FILEDLG_MapOfnStruct16(LPOPENFILENAME16 ofn16, LPOPENFILENAMEW ofnW, BOOL open)
997 OPENFILENAMEA ofnA;
998 /* first convert to linear pointers */
999 memset(&ofnA, 0, sizeof(OPENFILENAMEA));
1000 ofnA.lStructSize = sizeof(OPENFILENAMEA);
1001 ofnA.hwndOwner = HWND_32(ofn16->hwndOwner);
1002 ofnA.hInstance = HINSTANCE_32(ofn16->hInstance);
1003 if (ofn16->lpstrFilter)
1004 ofnA.lpstrFilter = MapSL(ofn16->lpstrFilter);
1005 if (ofn16->lpstrCustomFilter)
1006 ofnA.lpstrCustomFilter = MapSL(ofn16->lpstrCustomFilter);
1007 ofnA.nMaxCustFilter = ofn16->nMaxCustFilter;
1008 ofnA.nFilterIndex = ofn16->nFilterIndex;
1009 ofnA.lpstrFile = MapSL(ofn16->lpstrFile);
1010 ofnA.nMaxFile = ofn16->nMaxFile;
1011 ofnA.lpstrFileTitle = MapSL(ofn16->lpstrFileTitle);
1012 ofnA.nMaxFileTitle = ofn16->nMaxFileTitle;
1013 ofnA.lpstrInitialDir = MapSL(ofn16->lpstrInitialDir);
1014 ofnA.lpstrTitle = MapSL(ofn16->lpstrTitle);
1015 ofnA.Flags = ofn16->Flags;
1016 ofnA.nFileOffset = ofn16->nFileOffset;
1017 ofnA.nFileExtension = ofn16->nFileExtension;
1018 ofnA.lpstrDefExt = MapSL(ofn16->lpstrDefExt);
1019 if (HIWORD(ofn16->lpTemplateName))
1020 ofnA.lpTemplateName = MapSL(ofn16->lpTemplateName);
1021 else
1022 ofnA.lpTemplateName = (LPSTR) ofn16->lpTemplateName; /* ressource number */
1023 /* now calls the 32 bits Ansi to Unicode version to complete the job */
1024 FILEDLG_MapOfnStructA(&ofnA, ofnW, open);
1027 /************************************************************************
1028 * FILEDLG_DestroyPrivate [internal]
1029 * destroys the private object
1031 static void FILEDLG_DestroyPrivate(LFSPRIVATE lfs)
1033 HWND hwnd;
1034 if (!lfs) return;
1035 hwnd = lfs->hwnd;
1036 /* free resources for a 16 bits dialog */
1037 if (lfs->hResource16) FreeResource16(lfs->hResource16);
1038 if (lfs->hGlobal16)
1040 GlobalUnlock16(lfs->hGlobal16);
1041 GlobalFree16(lfs->hGlobal16);
1043 /* if ofnW has been allocated, have to free everything in it */
1044 if (lfs->ofn16 || lfs->ofnA)
1046 LPOPENFILENAMEW ofnW = lfs->ofnW;
1047 if (ofnW->lpstrFilter) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrFilter);
1048 if (ofnW->lpstrCustomFilter) HeapFree(GetProcessHeap(), 0, ofnW->lpstrCustomFilter);
1049 if (ofnW->lpstrFile) HeapFree(GetProcessHeap(), 0, ofnW->lpstrFile);
1050 if (ofnW->lpstrFileTitle) HeapFree(GetProcessHeap(), 0, ofnW->lpstrFileTitle);
1051 if (ofnW->lpstrInitialDir) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrInitialDir);
1052 if (ofnW->lpstrTitle) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrTitle);
1053 if ((ofnW->lpTemplateName) && (HIWORD(ofnW->lpTemplateName)))
1054 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpTemplateName);
1055 HeapFree(GetProcessHeap(), 0, ofnW);
1057 TRACE("destroying private allocation %p\n", lfs);
1058 HeapFree(GetProcessHeap(), 0, lfs);
1059 RemovePropA(hwnd, OFN_PROP);
1062 /************************************************************************
1063 * FILEDLG_AllocPrivate [internal]
1064 * allocate a private object to hold 32 bits Unicode
1065 * structure that will be used throughtout the calls, while
1066 * keeping available the original structures and a few variables
1067 * On entry : type = dialog procedure type (16,32A,32W)
1068 * dlgType = dialog type (open or save)
1070 static LFSPRIVATE FILEDLG_AllocPrivate(LPARAM lParam, int type, UINT dlgType)
1072 LFSPRIVATE lfs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct FSPRIVATE));
1073 LFSPRIVATE ret;
1074 TRACE("alloc private buf %p\n", lfs);
1075 if (!lfs) return NULL;
1076 lfs->hook = FALSE;
1077 lfs->lParam = lParam;
1078 if (dlgType == OPEN_DIALOG)
1079 lfs->open = TRUE;
1080 else
1081 lfs->open = FALSE;
1082 lfs->lbselchstring = RegisterWindowMessageA(LBSELCHSTRINGA);
1083 lfs->fileokstring = RegisterWindowMessageA(FILEOKSTRINGA);
1084 switch(type)
1086 case LFS16:
1087 lfs->ofn16 = MapSL(lParam);
1088 if (lfs->ofn16->Flags & OFN_ENABLEHOOK)
1089 if (lfs->ofn16->lpfnHook)
1090 lfs->hook = TRUE;
1092 break;
1094 case LFS32A:
1095 lfs->ofnA = (LPOPENFILENAMEA) lParam;
1096 if (lfs->ofnA->Flags & OFN_ENABLEHOOK)
1097 if (lfs->ofnA->lpfnHook)
1098 lfs->hook = TRUE;
1099 break;
1101 case LFS32W:
1102 lfs->ofnW = (LPOPENFILENAMEW) lParam;
1103 if (lfs->ofnW->Flags & OFN_ENABLEHOOK)
1104 if (lfs->ofnW->lpfnHook)
1105 lfs->hook = TRUE;
1106 break;
1108 ret = lfs;
1109 if (!lfs->ofnW)
1110 { /* this structure is needed internally, so create it */
1111 lfs->ofnW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(OPENFILENAMEW));
1112 if (lfs->ofnW)
1114 if (lfs->ofn16)
1115 FILEDLG_MapOfnStruct16(lfs->ofn16, lfs->ofnW, lfs->open);
1116 if (lfs->ofnA)
1117 FILEDLG_MapOfnStructA(lfs->ofnA, lfs->ofnW, lfs->open);
1119 else
1120 ret = NULL;
1122 if (lfs->ofn16)
1124 if (!Get16BitsTemplate(lfs)) ret = NULL;
1126 else
1127 if (!Get32BitsTemplate(lfs)) ret = NULL;
1128 if (!ret) FILEDLG_DestroyPrivate(lfs);
1129 return ret;
1132 /***********************************************************************
1133 * FILEDLG_CallWindowProc16 [internal]
1135 * Call the appropriate hook
1137 static BOOL FILEDLG_CallWindowProc16(LFSPRIVATE lfs, UINT wMsg, WPARAM wParam,
1138 LPARAM lParam)
1140 if (lfs->ofn16)
1142 return (BOOL16) CallWindowProc16(
1143 (WNDPROC16)lfs->ofn16->lpfnHook, HWND_16(lfs->hwnd),
1144 (UINT16)wMsg, (WPARAM16)wParam, lParam);
1146 return FALSE;
1149 /***********************************************************************
1150 * FILEDLG_WMInitDialog [internal]
1153 static LONG FILEDLG_WMInitDialog(HWND hWnd, WPARAM wParam, LPARAM lParam)
1155 int i, n;
1156 WCHAR tmpstr[BUFFILE];
1157 LPWSTR pstr, old_pstr;
1158 LPOPENFILENAMEW ofn;
1159 LFSPRIVATE lfs = (LFSPRIVATE) lParam;
1161 if (!lfs) return FALSE;
1162 SetPropA(hWnd, OFN_PROP, (HANDLE)lfs);
1163 lfs->hwnd = hWnd;
1164 ofn = lfs->ofnW;
1166 TRACE("flags=%lx initialdir=%s\n", ofn->Flags, debugstr_w(ofn->lpstrInitialDir));
1168 SetWindowTextW( hWnd, ofn->lpstrTitle );
1169 /* read custom filter information */
1170 if (ofn->lpstrCustomFilter)
1172 pstr = ofn->lpstrCustomFilter;
1173 n = 0;
1174 TRACE("lpstrCustomFilter = %p\n", pstr);
1175 while(*pstr)
1177 old_pstr = pstr;
1178 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
1179 (LPARAM)(ofn->lpstrCustomFilter) + n );
1180 n += lstrlenW(pstr) + 1;
1181 pstr += lstrlenW(pstr) + 1;
1182 TRACE("add str=%s associated to %s\n",
1183 debugstr_w(old_pstr), debugstr_w(pstr));
1184 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
1185 n += lstrlenW(pstr) + 1;
1186 pstr += lstrlenW(pstr) + 1;
1189 /* read filter information */
1190 if (ofn->lpstrFilter) {
1191 pstr = (LPWSTR) ofn->lpstrFilter;
1192 n = 0;
1193 while(*pstr) {
1194 old_pstr = pstr;
1195 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
1196 (LPARAM)(ofn->lpstrFilter + n) );
1197 n += lstrlenW(pstr) + 1;
1198 pstr += lstrlenW(pstr) + 1;
1199 TRACE("add str=%s associated to %s\n",
1200 debugstr_w(old_pstr), debugstr_w(pstr));
1201 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
1202 n += lstrlenW(pstr) + 1;
1203 pstr += lstrlenW(pstr) + 1;
1206 /* set default filter */
1207 if (ofn->nFilterIndex == 0 && ofn->lpstrCustomFilter == NULL)
1208 ofn->nFilterIndex = 1;
1209 SendDlgItemMessageW(hWnd, cmb1, CB_SETCURSEL, ofn->nFilterIndex - 1, 0);
1210 lstrcpynW(tmpstr, FILEDLG_GetFileType(ofn->lpstrCustomFilter,
1211 (LPWSTR)ofn->lpstrFilter, ofn->nFilterIndex - 1),BUFFILE);
1212 TRACE("nFilterIndex = %ld, SetText of edt1 to %s\n",
1213 ofn->nFilterIndex, debugstr_w(tmpstr));
1214 SetDlgItemTextW( hWnd, edt1, tmpstr );
1215 /* get drive list */
1216 *tmpstr = 0;
1217 DlgDirListComboBoxW(hWnd, tmpstr, cmb2, 0, DDL_DRIVES | DDL_EXCLUSIVE);
1218 /* read initial directory */
1219 /* FIXME: Note that this is now very version-specific (See MSDN description of
1220 * the OPENFILENAME structure). For example under 2000/XP any path in the
1221 * lpstrFile overrides the lpstrInitialDir, but not under 95/98/ME
1223 if (ofn->lpstrInitialDir != NULL)
1225 int len;
1226 lstrcpynW(tmpstr, ofn->lpstrInitialDir, 511);
1227 len = lstrlenW(tmpstr);
1228 if (len > 0 && tmpstr[len-1] != '\\' && tmpstr[len-1] != ':') {
1229 tmpstr[len]='\\';
1230 tmpstr[len+1]='\0';
1233 else
1234 *tmpstr = 0;
1235 if (!FILEDLG_ScanDir(hWnd, tmpstr)) {
1236 *tmpstr = 0;
1237 if (!FILEDLG_ScanDir(hWnd, tmpstr))
1238 WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr));
1240 /* select current drive in combo 2, omit missing drives */
1242 char dir[MAX_PATH];
1243 char str[4] = "a:\\";
1244 GetCurrentDirectoryA( sizeof(dir), dir );
1245 for(i = 0, n = -1; i < 26; i++)
1247 str[0] = 'a' + i;
1248 if (GetDriveTypeA(str) > DRIVE_NO_ROOT_DIR) n++;
1249 if (toupper(str[0]) == toupper(dir[0])) break;
1252 SendDlgItemMessageW(hWnd, cmb2, CB_SETCURSEL, n, 0);
1253 if (!(ofn->Flags & OFN_SHOWHELP))
1254 ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
1255 if (ofn->Flags & OFN_HIDEREADONLY)
1256 ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
1257 if (lfs->hook)
1258 return (BOOL) FILEDLG_CallWindowProc(lfs, WM_INITDIALOG, wParam, lfs->lParam);
1259 return TRUE;
1265 /***********************************************************************
1266 * FILEDLG_WMInitDialog16 [internal]
1267 * The is a duplicate of the 32bit FILEDLG_WMInitDialog function
1268 * The only differnce is that it calls FILEDLG_CallWindowProc16
1269 * for a 16 bit Window Proc.
1272 static LONG FILEDLG_WMInitDialog16(HWND hWnd, WPARAM wParam, LPARAM lParam)
1274 int i, n;
1275 WCHAR tmpstr[BUFFILE];
1276 LPWSTR pstr, old_pstr;
1277 LPOPENFILENAMEW ofn;
1278 LFSPRIVATE lfs = (LFSPRIVATE) lParam;
1280 if (!lfs) return FALSE;
1281 SetPropA(hWnd, OFN_PROP, (HANDLE)lfs);
1282 lfs->hwnd = hWnd;
1283 ofn = lfs->ofnW;
1285 TRACE("flags=%lx initialdir=%s\n", ofn->Flags, debugstr_w(ofn->lpstrInitialDir));
1287 SetWindowTextW( hWnd, ofn->lpstrTitle );
1288 /* read custom filter information */
1289 if (ofn->lpstrCustomFilter)
1291 pstr = ofn->lpstrCustomFilter;
1292 n = 0;
1293 TRACE("lpstrCustomFilter = %p\n", pstr);
1294 while(*pstr)
1296 old_pstr = pstr;
1297 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
1298 (LPARAM)(ofn->lpstrCustomFilter) + n );
1299 n += lstrlenW(pstr) + 1;
1300 pstr += lstrlenW(pstr) + 1;
1301 TRACE("add str=%s associated to %s\n",
1302 debugstr_w(old_pstr), debugstr_w(pstr));
1303 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
1304 n += lstrlenW(pstr) + 1;
1305 pstr += lstrlenW(pstr) + 1;
1308 /* read filter information */
1309 if (ofn->lpstrFilter) {
1310 pstr = (LPWSTR) ofn->lpstrFilter;
1311 n = 0;
1312 while(*pstr) {
1313 old_pstr = pstr;
1314 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
1315 (LPARAM)(ofn->lpstrFilter + n) );
1316 n += lstrlenW(pstr) + 1;
1317 pstr += lstrlenW(pstr) + 1;
1318 TRACE("add str=%s associated to %s\n",
1319 debugstr_w(old_pstr), debugstr_w(pstr));
1320 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
1321 n += lstrlenW(pstr) + 1;
1322 pstr += lstrlenW(pstr) + 1;
1325 /* set default filter */
1326 if (ofn->nFilterIndex == 0 && ofn->lpstrCustomFilter == NULL)
1327 ofn->nFilterIndex = 1;
1328 SendDlgItemMessageW(hWnd, cmb1, CB_SETCURSEL, ofn->nFilterIndex - 1, 0);
1329 lstrcpynW(tmpstr, FILEDLG_GetFileType(ofn->lpstrCustomFilter,
1330 (LPWSTR)ofn->lpstrFilter, ofn->nFilterIndex - 1),BUFFILE);
1331 TRACE("nFilterIndex = %ld, SetText of edt1 to %s\n",
1332 ofn->nFilterIndex, debugstr_w(tmpstr));
1333 SetDlgItemTextW( hWnd, edt1, tmpstr );
1334 /* get drive list */
1335 *tmpstr = 0;
1336 DlgDirListComboBoxW(hWnd, tmpstr, cmb2, 0, DDL_DRIVES | DDL_EXCLUSIVE);
1337 /* read initial directory */
1338 /* FIXME: Note that this is now very version-specific (See MSDN description of
1339 * the OPENFILENAME structure). For example under 2000/XP any path in the
1340 * lpstrFile overrides the lpstrInitialDir, but not under 95/98/ME
1342 if (ofn->lpstrInitialDir != NULL)
1344 int len;
1345 lstrcpynW(tmpstr, ofn->lpstrInitialDir, 511);
1346 len = lstrlenW(tmpstr);
1347 if (len > 0 && tmpstr[len-1] != '\\' && tmpstr[len-1] != ':') {
1348 tmpstr[len]='\\';
1349 tmpstr[len+1]='\0';
1352 else
1353 *tmpstr = 0;
1354 if (!FILEDLG_ScanDir(hWnd, tmpstr)) {
1355 *tmpstr = 0;
1356 if (!FILEDLG_ScanDir(hWnd, tmpstr))
1357 WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr));
1359 /* select current drive in combo 2, omit missing drives */
1361 char dir[MAX_PATH];
1362 char str[4] = "a:\\";
1363 GetCurrentDirectoryA( sizeof(dir), dir );
1364 for(i = 0, n = -1; i < 26; i++)
1366 str[0] = 'a' + i;
1367 if (GetDriveTypeA(str) > DRIVE_NO_ROOT_DIR) n++;
1368 if (toupper(str[0]) == toupper(dir[0])) break;
1371 SendDlgItemMessageW(hWnd, cmb2, CB_SETCURSEL, n, 0);
1372 if (!(ofn->Flags & OFN_SHOWHELP))
1373 ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
1374 if (ofn->Flags & OFN_HIDEREADONLY)
1375 ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
1376 if (lfs->hook)
1377 return (BOOL) FILEDLG_CallWindowProc16(lfs, WM_INITDIALOG, wParam, lfs->lParam);
1378 return TRUE;
1381 /***********************************************************************
1382 * FILEDLG_WMMeasureItem [internal]
1384 static LONG FILEDLG_WMMeasureItem(HWND hWnd, WPARAM wParam, LPARAM lParam)
1386 LPMEASUREITEMSTRUCT lpmeasure;
1388 lpmeasure = (LPMEASUREITEMSTRUCT)lParam;
1389 lpmeasure->itemHeight = fldrHeight;
1390 return TRUE;
1394 /***********************************************************************
1395 * FILEDLG_WMMeasureItem16 [internal]
1397 static LONG FILEDLG_WMMeasureItem16(HWND16 hWnd, WPARAM16 wParam, LPARAM lParam)
1399 LPMEASUREITEMSTRUCT16 lpmeasure;
1401 lpmeasure = MapSL(lParam);
1402 lpmeasure->itemHeight = fldrHeight;
1403 return TRUE;
1406 /* ------------------ Dialog procedures ---------------------- */
1408 /***********************************************************************
1409 * FileOpenDlgProc [internal]
1410 * Used for open and save, in fact.
1412 static INT_PTR CALLBACK FileOpenDlgProc(HWND hWnd, UINT wMsg,
1413 WPARAM wParam, LPARAM lParam)
1415 LFSPRIVATE lfs = (LFSPRIVATE)GetPropA(hWnd,OFN_PROP);
1417 TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
1418 if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
1420 INT_PTR lRet;
1421 lRet = (INT_PTR)FILEDLG_CallWindowProc(lfs, wMsg, wParam, lParam);
1422 if (lRet)
1423 return lRet; /* else continue message processing */
1425 switch (wMsg)
1427 case WM_INITDIALOG:
1428 return FILEDLG_WMInitDialog(hWnd, wParam, lParam);
1430 case WM_MEASUREITEM:
1431 return FILEDLG_WMMeasureItem(hWnd, wParam, lParam);
1433 case WM_DRAWITEM:
1434 return FILEDLG_WMDrawItem(hWnd, wParam, lParam, !lfs->open, (DRAWITEMSTRUCT *)lParam);
1436 case WM_COMMAND:
1437 return FILEDLG_WMCommand(hWnd, lParam, HIWORD(wParam), LOWORD(wParam), lfs);
1438 #if 0
1439 case WM_CTLCOLOR:
1440 SetBkColor((HDC16)wParam, 0x00C0C0C0);
1441 switch (HIWORD(lParam))
1443 case CTLCOLOR_BTN:
1444 SetTextColor((HDC16)wParam, 0x00000000);
1445 return hGRAYBrush;
1446 case CTLCOLOR_STATIC:
1447 SetTextColor((HDC16)wParam, 0x00000000);
1448 return hGRAYBrush;
1450 break;
1451 #endif
1453 return FALSE;
1456 /***********************************************************************
1457 * FileOpenDlgProc (COMMDLG.6)
1459 BOOL16 CALLBACK FileOpenDlgProc16(HWND16 hWnd16, UINT16 wMsg, WPARAM16 wParam,
1460 LPARAM lParam)
1462 HWND hWnd = HWND_32(hWnd16);
1463 LFSPRIVATE lfs = (LFSPRIVATE)GetPropA(hWnd,OFN_PROP);
1464 DRAWITEMSTRUCT dis;
1466 TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
1467 if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
1469 LRESULT lRet = (BOOL16)FILEDLG_CallWindowProc16(lfs, wMsg, wParam, lParam);
1470 if (lRet)
1471 return lRet; /* else continue message processing */
1473 switch (wMsg)
1475 case WM_INITDIALOG:
1476 return FILEDLG_WMInitDialog16(hWnd, wParam, lParam);
1478 case WM_MEASUREITEM:
1479 return FILEDLG_WMMeasureItem16(hWnd16, wParam, lParam);
1481 case WM_DRAWITEM:
1482 FILEDLG_MapDrawItemStruct(MapSL(lParam), &dis);
1483 return FILEDLG_WMDrawItem(hWnd, wParam, lParam, FALSE, &dis);
1485 case WM_COMMAND:
1486 return FILEDLG_WMCommand(hWnd, lParam, HIWORD(lParam),wParam, lfs);
1487 #if 0
1488 case WM_CTLCOLOR:
1489 SetBkColor((HDC16)wParam, 0x00C0C0C0);
1490 switch (HIWORD(lParam))
1492 case CTLCOLOR_BTN:
1493 SetTextColor((HDC16)wParam, 0x00000000);
1494 return hGRAYBrush;
1495 case CTLCOLOR_STATIC:
1496 SetTextColor((HDC16)wParam, 0x00000000);
1497 return hGRAYBrush;
1499 break;
1500 #endif
1502 return FALSE;
1505 /***********************************************************************
1506 * FileSaveDlgProc (COMMDLG.7)
1508 BOOL16 CALLBACK FileSaveDlgProc16(HWND16 hWnd16, UINT16 wMsg, WPARAM16 wParam,
1509 LPARAM lParam)
1511 HWND hWnd = HWND_32(hWnd16);
1512 LFSPRIVATE lfs = (LFSPRIVATE)GetPropA(hWnd,OFN_PROP);
1513 DRAWITEMSTRUCT dis;
1515 TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
1516 if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
1518 LRESULT lRet;
1519 lRet = (BOOL16)FILEDLG_CallWindowProc16(lfs, wMsg, wParam, lParam);
1520 if (lRet)
1521 return lRet; /* else continue message processing */
1523 switch (wMsg) {
1524 case WM_INITDIALOG:
1525 return FILEDLG_WMInitDialog16(hWnd, wParam, lParam);
1527 case WM_MEASUREITEM:
1528 return FILEDLG_WMMeasureItem16(hWnd16, wParam, lParam);
1530 case WM_DRAWITEM:
1531 FILEDLG_MapDrawItemStruct(MapSL(lParam), &dis);
1532 return FILEDLG_WMDrawItem(hWnd, wParam, lParam, TRUE, &dis);
1534 case WM_COMMAND:
1535 return FILEDLG_WMCommand(hWnd, lParam, HIWORD(lParam), wParam, lfs);
1539 case WM_CTLCOLOR:
1540 SetBkColor((HDC16)wParam, 0x00C0C0C0);
1541 switch (HIWORD(lParam))
1543 case CTLCOLOR_BTN:
1544 SetTextColor((HDC16)wParam, 0x00000000);
1545 return hGRAYBrush;
1546 case CTLCOLOR_STATIC:
1547 SetTextColor((HDC16)wParam, 0x00000000);
1548 return hGRAYBrush;
1550 return FALSE;
1553 return FALSE;
1558 /***********************************************************************
1559 * GetFileName31A [internal]
1561 * Creates a win31 style dialog box for the user to select a file to open/save.
1563 BOOL GetFileName31A(
1564 LPOPENFILENAMEA lpofn, /* addess of structure with data*/
1565 UINT dlgType /* type dialogue : open/save */
1568 HINSTANCE hInst;
1569 BOOL bRet = FALSE;
1570 LFSPRIVATE lfs;
1572 if (!lpofn || !FileDlg_Init()) return FALSE;
1574 TRACE("ofn flags %08lx\n", lpofn->Flags);
1575 lfs = FILEDLG_AllocPrivate((LPARAM) lpofn, LFS32A, dlgType);
1576 if (lfs)
1578 hInst = (HINSTANCE)GetWindowLongA( lpofn->hwndOwner, GWL_HINSTANCE );
1579 bRet = DialogBoxIndirectParamA( hInst, lfs->template, lpofn->hwndOwner,
1580 FileOpenDlgProc, (LPARAM)lfs);
1581 FILEDLG_DestroyPrivate(lfs);
1584 TRACE("return lpstrFile='%s' !\n", lpofn->lpstrFile);
1585 return bRet;
1588 /***********************************************************************
1589 * GetFileName31W [internal]
1591 * Creates a win31 style dialog box for the user to select a file to open/save
1593 BOOL GetFileName31W(
1594 LPOPENFILENAMEW lpofn, /* addess of structure with data*/
1595 UINT dlgType /* type dialogue : open/save */
1598 HINSTANCE hInst;
1599 BOOL bRet = FALSE;
1600 LFSPRIVATE lfs;
1602 if (!lpofn || !FileDlg_Init()) return FALSE;
1604 lfs = FILEDLG_AllocPrivate((LPARAM) lpofn, LFS32W, dlgType);
1605 if (lfs)
1607 hInst = (HINSTANCE)GetWindowLongA( lpofn->hwndOwner, GWL_HINSTANCE );
1608 bRet = DialogBoxIndirectParamW( hInst, lfs->template, lpofn->hwndOwner,
1609 FileOpenDlgProc, (LPARAM)lfs);
1610 FILEDLG_DestroyPrivate(lfs);
1613 TRACE("return lpstrFile=%s !\n", debugstr_w(lpofn->lpstrFile));
1614 return bRet;
1623 /* ------------------ APIs ---------------------- */
1625 /***********************************************************************
1626 * GetOpenFileName (COMMDLG.1)
1628 * Creates a dialog box for the user to select a file to open.
1630 * RETURNS
1631 * TRUE on success: user selected a valid file
1632 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1634 * BUGS
1635 * unknown, there are some FIXME's left.
1637 BOOL16 WINAPI GetOpenFileName16(
1638 SEGPTR ofn /* [in/out] address of structure with data*/
1641 HINSTANCE16 hInst;
1642 BOOL bRet = FALSE;
1643 LPOPENFILENAME16 lpofn = MapSL(ofn);
1644 LFSPRIVATE lfs;
1645 FARPROC16 ptr;
1647 if (!lpofn || !FileDlg_Init()) return FALSE;
1649 lfs = FILEDLG_AllocPrivate((LPARAM) ofn, LFS16, OPEN_DIALOG);
1650 if (lfs)
1652 hInst = GetWindowWord( HWND_32(lpofn->hwndOwner), GWL_HINSTANCE );
1653 ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (LPCSTR) 6);
1654 bRet = DialogBoxIndirectParam16( hInst, lfs->hDlgTmpl16, lpofn->hwndOwner,
1655 (DLGPROC16) ptr, (LPARAM) lfs);
1656 FILEDLG_DestroyPrivate(lfs);
1659 TRACE("return lpstrFile='%s' !\n", (char *)MapSL(lpofn->lpstrFile));
1660 return bRet;
1663 /***********************************************************************
1664 * GetSaveFileName (COMMDLG.2)
1666 * Creates a dialog box for the user to select a file to save.
1668 * RETURNS
1669 * TRUE on success: user enters a valid file
1670 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1672 * BUGS
1673 * unknown. There are some FIXME's left.
1675 BOOL16 WINAPI GetSaveFileName16(
1676 SEGPTR ofn /* [in/out] addess of structure with data*/
1679 HINSTANCE16 hInst;
1680 BOOL bRet = FALSE;
1681 LPOPENFILENAME16 lpofn = MapSL(ofn);
1682 LFSPRIVATE lfs;
1683 FARPROC16 ptr;
1685 if (!lpofn || !FileDlg_Init()) return FALSE;
1687 lfs = FILEDLG_AllocPrivate((LPARAM) ofn, LFS16, SAVE_DIALOG);
1688 if (lfs)
1690 hInst = GetWindowWord( HWND_32(lpofn->hwndOwner), GWL_HINSTANCE );
1691 ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (LPCSTR) 7);
1692 bRet = DialogBoxIndirectParam16( hInst, lfs->hDlgTmpl16, lpofn->hwndOwner,
1693 (DLGPROC16) ptr, (LPARAM) lfs);
1694 FILEDLG_DestroyPrivate(lfs);
1697 TRACE("return lpstrFile='%s' !\n", (char *)MapSL(lpofn->lpstrFile));
1698 return bRet;