Fixed returned length of files.
[wine.git] / dlls / commdlg / filedlg.c
blobdfc371956a30b5877db7a7876634a3a0b78ecc7e
1 /*
2 * COMMDLG - File Dialogs
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1996 Albrecht Kleine
6 */
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include "windef.h"
12 #include "winnls.h"
13 #include "winbase.h"
14 #include "wingdi.h"
15 #include "wine/winbase16.h"
16 #include "wine/winuser16.h"
17 #include "wine/unicode.h"
18 #include "ldt.h"
19 #include "heap.h"
20 #include "commdlg.h"
21 #include "module.h"
22 #include "debugtools.h"
23 #include "cderr.h"
24 #include "tweak.h"
26 DEFAULT_DEBUG_CHANNEL(commdlg);
28 #include "cdlg.h"
30 #define BUFFILE 512
31 #define BUFFILEALLOC 512 * sizeof(WCHAR)
33 struct FSPRIVATE
35 HWND hwnd; /* file dialog window handle */
36 BOOL hook; /* TRUE if the dialog is hooked */
37 UINT lbselchstring; /* registered message id */
38 UINT fileokstring; /* registered message id */
39 LPARAM lParam; /* save original lparam */
40 HANDLE16 hDlgTmpl16; /* handle for resource 16 */
41 HANDLE16 hResource16; /* handle for allocated resource 16 */
42 HANDLE16 hGlobal16; /* 16 bits mem block (resources) */
43 LPCVOID template; /* template for 32 bits resource */
44 BOOL open; /* TRUE if open dialog, FALSE if save dialog */
45 OPENFILENAMEW *ofnW; /* original structure or work struct */
46 OPENFILENAMEA *ofnA; /* original structure if 32bits ansi dialog */
47 OPENFILENAME16 *ofn16; /* original structure if 16 bits dialog */
51 #define LFSPRIVATE struct FSPRIVATE *
53 #define LFS16 1
54 #define LFS32A 2
55 #define LFS32W 3
57 static const WCHAR FILE_star[] = {'*','.','*', 0};
58 static const WCHAR FILE_bslash[] = {'\\', 0};
59 static const WCHAR FILE_specc[] = {'%','c',':', 0};
61 static HICON16 hFolder = 0;
62 static HICON16 hFolder2 = 0;
63 static HICON16 hFloppy = 0;
64 static HICON16 hHDisk = 0;
65 static HICON16 hCDRom = 0;
66 static HICON16 hNet = 0;
67 static int fldrHeight = 0;
68 static int fldrWidth = 0;
70 #define OFN_PROP "FILEDLG_OFN"
72 static const char defaultfilter[]=" \0\0";
73 static char defaultopen[]="Open File";
74 static char defaultsave[]="Save as";
76 /***********************************************************************
78 * Windows 3.1 style OpenFileName/SaveFileName dialog
82 LRESULT WINAPI FileOpenDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
83 LPARAM lParam);
84 LRESULT WINAPI FileSaveDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
85 LPARAM lParam);
87 static LRESULT WINAPI FileOpenDlgProc(HWND hDlg, UINT msg,
88 WPARAM wParam, LPARAM lParam);
90 /***********************************************************************
91 * FileDlg_Init [internal]
93 static BOOL FileDlg_Init(void)
95 static BOOL initialized = 0;
96 CURSORICONINFO *fldrInfo;
98 if (!initialized) {
99 if (!hFolder) hFolder = LoadIconA(0, MAKEINTRESOURCEA(OIC_FOLDER));
100 if (!hFolder2) hFolder2 = LoadIconA(0, MAKEINTRESOURCEA(OIC_FOLDER2));
101 if (!hFloppy) hFloppy = LoadIconA(0, MAKEINTRESOURCEA(OIC_FLOPPY));
102 if (!hHDisk) hHDisk = LoadIconA(0, MAKEINTRESOURCEA(OIC_HDISK));
103 if (!hCDRom) hCDRom = LoadIconA(0, MAKEINTRESOURCEA(OIC_CDROM));
104 if (!hNet) hNet = LoadIconA(0, MAKEINTRESOURCEA(OIC_NETWORK));
105 if (hFolder == 0 || hFolder2 == 0 || hFloppy == 0 ||
106 hHDisk == 0 || hCDRom == 0 || hNet == 0)
108 ERR("Error loading icons !\n");
109 return FALSE;
111 fldrInfo = (CURSORICONINFO *) GlobalLock16( hFolder2 );
112 if (!fldrInfo)
114 ERR("Error measuring icons !\n");
115 return FALSE;
117 fldrHeight = fldrInfo -> nHeight;
118 fldrWidth = fldrInfo -> nWidth;
119 GlobalUnlock16( hFolder2 );
120 initialized = TRUE;
122 return TRUE;
126 /***********************************************************************
127 * Get32BitsTemplate [internal]
129 * Get a template (or FALSE if failure) when 16 bits dialogs are used
130 * by a 32 bits application
133 BOOL Get32BitsTemplate(LFSPRIVATE lfs)
135 LPOPENFILENAMEW ofnW = lfs->ofnW;
136 HANDLE hDlgTmpl;
138 if (ofnW->Flags & OFN_ENABLETEMPLATEHANDLE)
140 if (!(lfs->template = LockResource( ofnW->hInstance )))
142 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
143 return FALSE;
146 else if (ofnW->Flags & OFN_ENABLETEMPLATE)
148 HANDLE hResInfo;
149 if (lfs->ofnA)
150 hResInfo = FindResourceA(lfs->ofnA->hInstance,
151 lfs->ofnA->lpTemplateName,
152 RT_DIALOGA);
153 else
154 hResInfo = FindResourceW(ofnW->hInstance,
155 ofnW->lpTemplateName,
156 RT_DIALOGW);
157 if (!hResInfo)
159 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
160 return FALSE;
162 if (!(hDlgTmpl = LoadResource(ofnW->hInstance,
163 hResInfo)) ||
164 !(lfs->template = LockResource(hDlgTmpl)))
166 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
167 return FALSE;
169 } else { /* get it from internal Wine resource */
170 HANDLE hResInfo;
171 if (!(hResInfo = FindResourceA(COMMDLG_hInstance32,
172 lfs->open? "OPEN_FILE":"SAVE_FILE", RT_DIALOGA)))
174 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
175 return FALSE;
177 if (!(hDlgTmpl = LoadResource(COMMDLG_hInstance32, hResInfo )) ||
178 !(lfs->template = LockResource( hDlgTmpl )))
180 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
181 return FALSE;
184 return TRUE;
188 /***********************************************************************
189 * Get16BitsTemplate [internal]
191 * Get a template (FALSE if failure) when 16 bits dialogs are used
192 * by a 16 bits application
195 BOOL Get16BitsTemplate(LFSPRIVATE lfs)
197 LPOPENFILENAME16 ofn16 = lfs->ofn16;
198 LPCVOID template;
199 HGLOBAL16 hGlobal16 = 0;
201 if (ofn16->Flags & OFN_ENABLETEMPLATEHANDLE)
202 lfs->hDlgTmpl16 = ofn16->hInstance;
203 else if (ofn16->Flags & OFN_ENABLETEMPLATE)
205 HANDLE16 hResInfo;
206 if (!(hResInfo = FindResource16(ofn16->hInstance,
207 PTR_SEG_TO_LIN(ofn16->lpTemplateName),
208 RT_DIALOGA)))
210 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
211 return FALSE;
213 if (!(lfs->hDlgTmpl16 = LoadResource16( ofn16->hInstance, hResInfo )))
215 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
216 return FALSE;
218 lfs->hResource16 = lfs->hDlgTmpl16;
220 else
221 { /* get resource from (32 bits) own Wine resource; convert it to 16 */
222 HANDLE hResInfo, hDlgTmpl32;
223 LPCVOID template32;
224 DWORD size;
226 if (!(hResInfo = FindResourceA(COMMDLG_hInstance32,
227 lfs->open ? "OPEN_FILE":"SAVE_FILE", RT_DIALOGA)))
229 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
230 return FALSE;
232 if (!(hDlgTmpl32 = LoadResource(COMMDLG_hInstance32, hResInfo )) ||
233 !(template32 = LockResource( hDlgTmpl32 )))
235 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
236 return FALSE;
238 size = SizeofResource(GetModuleHandleA("COMDLG32"), hResInfo);
239 hGlobal16 = GlobalAlloc16(0, size);
240 if (!hGlobal16)
242 COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
243 ERR("alloc failure for %ld bytes\n", size);
244 return FALSE;
246 template = GlobalLock16(hGlobal16);
247 if (!template)
249 COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE);
250 ERR("global lock failure for %x handle\n", hGlobal16);
251 GlobalFree16(hGlobal16);
252 return FALSE;
254 ConvertDialog32To16((LPVOID)template32, size, (LPVOID)template);
255 lfs->hDlgTmpl16 = hGlobal16;
256 lfs->hGlobal16 = hGlobal16;
258 return TRUE;
261 /***********************************************************************
262 * FILEDLG_StripEditControl [internal]
263 * Strip pathnames off the contents of the edit control.
265 static void FILEDLG_StripEditControl(HWND hwnd)
267 WCHAR temp[BUFFILE], *cp;
269 GetDlgItemTextW( hwnd, edt1, temp, sizeof(temp) );
270 cp = strrchrW(temp, '\\');
271 if (cp != NULL) {
272 strcpyW(temp, cp+1);
274 cp = strrchrW(temp, ':');
275 if (cp != NULL) {
276 strcpyW(temp, cp+1);
278 /* FIXME: shouldn't we do something with the result here? ;-) */
283 /***********************************************************************
284 * FILEDLG_CallWindowProc [internal]
286 * Call the appropriate hook
288 static BOOL FILEDLG_CallWindowProc(LFSPRIVATE lfs, UINT wMsg, WPARAM wParam,
289 LPARAM lParam)
291 if (lfs->ofn16)
293 return (BOOL16) CallWindowProc16(
294 (WNDPROC16)lfs->ofn16->lpfnHook, lfs->hwnd,
295 (UINT16)wMsg, (WPARAM16)wParam, lParam);
297 if (lfs->ofnA)
299 return (BOOL) CallWindowProcA(
300 (WNDPROC)lfs->ofnA->lpfnHook, lfs->hwnd,
301 wMsg, wParam, lParam);
304 if (lfs->ofnW)
306 return (BOOL) CallWindowProcW(
307 (WNDPROC)lfs->ofnW->lpfnHook, lfs->hwnd,
308 wMsg, wParam, lParam);
310 return FALSE;
314 /***********************************************************************
315 * FILEDLG_ScanDir [internal]
317 static BOOL FILEDLG_ScanDir(HWND hWnd, LPWSTR newPath)
319 WCHAR buffer[BUFFILE];
320 HWND hdlg, hdlgDir;
321 LRESULT lRet = TRUE;
322 HCURSOR hCursorWait, oldCursor;
324 if ( !SetCurrentDirectoryW( newPath ))
325 return FALSE;
326 lstrcpynW(buffer, newPath, sizeof(buffer));
328 /* get the list of spec files */
329 GetDlgItemTextW(hWnd, edt1, buffer, sizeof(buffer));
331 hCursorWait = LoadCursorA(0, IDC_WAITA);
332 oldCursor = SetCursor(hCursorWait);
334 /* list of files */
335 if ((hdlg = GetDlgItem(hWnd, lst1)) != 0) {
336 WCHAR* scptr; /* ptr on semi-colon */
337 WCHAR* filter = buffer;
339 TRACE("Using filter %s\n", debugstr_w(filter));
340 SendMessageW(hdlg, LB_RESETCONTENT, 0, 0);
341 while (filter) {
342 scptr = strchrW(filter, ';');
343 if (scptr) *scptr = 0;
344 while (*filter == ' ') filter++;
345 TRACE("Using file spec %s\n", debugstr_w(filter));
346 if (SendMessageW(hdlg, LB_DIR, 0, (LPARAM)filter) == LB_ERR)
347 return FALSE;
348 if (scptr) *scptr = ';';
349 filter = (scptr) ? (scptr + 1) : 0;
353 /* list of directories */
354 strcpyW(buffer, FILE_star);
356 if ((hdlgDir = GetDlgItem(hWnd, lst2)) != 0) {
357 lRet = DlgDirListW(hWnd, buffer, lst2, stc1, DDL_EXCLUSIVE | DDL_DIRECTORY);
359 SetCursor(oldCursor);
360 return lRet;
364 /***********************************************************************
365 * FILEDLG_GetFileType [internal]
368 static LPWSTR FILEDLG_GetFileType(LPWSTR cfptr, LPWSTR fptr, WORD index)
370 int n, i;
371 i = 0;
372 if (cfptr)
373 for ( ;(n = lstrlenW(cfptr)) != 0; i++)
375 cfptr += n + 1;
376 if (i == index)
377 return cfptr;
378 cfptr += lstrlenW(cfptr) + 1;
380 if (fptr)
381 for ( ;(n = lstrlenW(fptr)) != 0; i++)
383 fptr += n + 1;
384 if (i == index)
385 return fptr;
386 fptr += lstrlenW(fptr) + 1;
388 return (LPWSTR) FILE_star; /* FIXME */
391 /***********************************************************************
392 * FILEDLG_WMDrawItem [internal]
394 static LONG FILEDLG_WMDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam,
395 int savedlg, LPDRAWITEMSTRUCT lpdis)
397 WCHAR *str;
398 HICON hIcon;
399 COLORREF oldText = 0, oldBk = 0;
401 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst1)
403 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC))) return FALSE;
404 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
405 (LPARAM)str);
407 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
409 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
410 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
412 if (savedlg)
413 SetTextColor(lpdis->hDC,GetSysColor(COLOR_GRAYTEXT) );
415 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + 1,
416 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
417 &(lpdis->rcItem), str, lstrlenW(str), NULL);
419 if (lpdis->itemState & ODS_SELECTED)
420 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
422 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
424 SetBkColor( lpdis->hDC, oldBk );
425 SetTextColor( lpdis->hDC, oldText );
427 HeapFree(GetProcessHeap(), 0, str);
428 return TRUE;
431 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst2)
433 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
434 return FALSE;
435 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
436 (LPARAM)str);
438 if (lpdis->itemState & ODS_SELECTED)
440 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
441 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
443 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
444 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
445 &(lpdis->rcItem), str, lstrlenW(str), NULL);
447 if (lpdis->itemState & ODS_SELECTED)
448 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
450 if (lpdis->itemState & ODS_SELECTED)
452 SetBkColor( lpdis->hDC, oldBk );
453 SetTextColor( lpdis->hDC, oldText );
455 DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hFolder);
456 HeapFree(GetProcessHeap(), 0, str);
457 return TRUE;
459 if (lpdis->CtlType == ODT_COMBOBOX && lpdis->CtlID == cmb2)
461 char root[] = "a:";
462 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
463 return FALSE;
464 SendMessageW(lpdis->hwndItem, CB_GETLBTEXT, lpdis->itemID,
465 (LPARAM)str);
466 root[0] += str[2] - 'a';
467 switch(GetDriveTypeA(root))
469 case DRIVE_REMOVABLE: hIcon = hFloppy; break;
470 case DRIVE_CDROM: hIcon = hCDRom; break;
471 case DRIVE_REMOTE: hIcon = hNet; break;
472 case DRIVE_FIXED:
473 default: hIcon = hHDisk; break;
475 if (lpdis->itemState & ODS_SELECTED)
477 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
478 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
480 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
481 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
482 &(lpdis->rcItem), str, lstrlenW(str), NULL);
484 if (lpdis->itemState & ODS_SELECTED)
486 SetBkColor( lpdis->hDC, oldBk );
487 SetTextColor( lpdis->hDC, oldText );
489 DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hIcon);
490 HeapFree(GetProcessHeap(), 0, str);
491 return TRUE;
493 return FALSE;
496 /***********************************************************************
497 * FILEDLG_WMMeasureItem [internal]
499 static LONG FILEDLG_WMMeasureItem(HWND hWnd, WPARAM wParam, LPARAM lParam)
501 LPMEASUREITEMSTRUCT lpmeasure;
503 lpmeasure = (LPMEASUREITEMSTRUCT)lParam;
504 lpmeasure->itemHeight = fldrHeight;
505 return TRUE;
508 /***********************************************************************
509 * FILEDLG_WMMeasureItem16 [internal]
511 static LONG FILEDLG_WMMeasureItem16(HWND16 hWnd, WPARAM16 wParam, LPARAM lParam)
513 LPMEASUREITEMSTRUCT16 lpmeasure;
515 lpmeasure = (LPMEASUREITEMSTRUCT16)PTR_SEG_TO_LIN(lParam);
516 lpmeasure->itemHeight = fldrHeight;
517 return TRUE;
520 /***********************************************************************
521 * FILEDLG_WMInitDialog [internal]
524 static LONG FILEDLG_WMInitDialog(HWND hWnd, WPARAM wParam, LPARAM lParam)
526 int i, n;
527 WCHAR tmpstr[BUFFILE];
528 LPWSTR pstr, old_pstr;
529 LPOPENFILENAMEW ofn;
530 LFSPRIVATE lfs = (LFSPRIVATE) lParam;
532 if (!lfs) return FALSE;
533 SetPropA(hWnd, OFN_PROP, (HANDLE)lfs);
534 lfs->hwnd = hWnd;
535 ofn = lfs->ofnW;
537 TRACE("flags=%lx initialdir=%s\n", ofn->Flags, debugstr_w(ofn->lpstrInitialDir));
539 SetWindowTextW( hWnd, ofn->lpstrTitle );
540 /* read custom filter information */
541 if (ofn->lpstrCustomFilter)
543 pstr = ofn->lpstrCustomFilter;
544 n = 0;
545 TRACE("lpstrCustomFilter = %p\n", pstr);
546 while(*pstr)
548 old_pstr = pstr;
549 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
550 (LPARAM)(ofn->lpstrCustomFilter) + n );
551 n += lstrlenW(pstr) + 1;
552 pstr += lstrlenW(pstr) + 1;
553 TRACE("add str='%s' "
554 "associated to '%s'\n", debugstr_w(old_pstr), debugstr_w(pstr));
555 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
556 n += lstrlenW(pstr) + 1;
557 pstr += lstrlenW(pstr) + 1;
560 /* read filter information */
561 if (ofn->lpstrFilter) {
562 pstr = (LPWSTR) ofn->lpstrFilter;
563 n = 0;
564 while(*pstr) {
565 old_pstr = pstr;
566 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
567 (LPARAM)(ofn->lpstrFilter + n) );
568 n += lstrlenW(pstr) + 1;
569 pstr += lstrlenW(pstr) + 1;
570 TRACE("add str='%s' "
571 "associated to '%s'\n", debugstr_w(old_pstr), debugstr_w(pstr));
572 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
573 n += lstrlenW(pstr) + 1;
574 pstr += lstrlenW(pstr) + 1;
577 /* set default filter */
578 if (ofn->nFilterIndex == 0 && ofn->lpstrCustomFilter == NULL)
579 ofn->nFilterIndex = 1;
580 SendDlgItemMessageW(hWnd, cmb1, CB_SETCURSEL, ofn->nFilterIndex - 1, 0);
581 lstrcpynW(tmpstr, FILEDLG_GetFileType(ofn->lpstrCustomFilter,
582 (LPWSTR)ofn->lpstrFilter, ofn->nFilterIndex - 1),BUFFILE);
583 TRACE("nFilterIndex = %ld, SetText of edt1 to '%s'\n",
584 ofn->nFilterIndex, debugstr_w(tmpstr));
585 SetDlgItemTextW( hWnd, edt1, tmpstr );
586 /* get drive list */
587 *tmpstr = 0;
588 DlgDirListComboBoxW(hWnd, tmpstr, cmb2, 0, DDL_DRIVES | DDL_EXCLUSIVE);
589 /* read initial directory */
590 if (ofn->lpstrInitialDir != NULL)
592 int len;
593 lstrcpynW(tmpstr, ofn->lpstrInitialDir, 511);
594 len = lstrlenW(tmpstr);
595 if (len > 0 && tmpstr[len-1] != '\\' && tmpstr[len-1] != ':') {
596 tmpstr[len]='\\';
597 tmpstr[len+1]='\0';
600 else
601 *tmpstr = 0;
602 if (!FILEDLG_ScanDir(hWnd, tmpstr)) {
603 *tmpstr = 0;
604 if (!FILEDLG_ScanDir(hWnd, tmpstr))
605 WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr));
607 /* select current drive in combo 2, omit missing drives */
609 char dir[MAX_PATH];
610 char str[4] = "a:\\";
611 GetCurrentDirectoryA( sizeof(dir), dir );
612 for(i = 0, n = -1; i < 26; i++)
614 str[0] = 'a' + i;
615 if (GetDriveTypeA(str) != DRIVE_DOESNOTEXIST) n++;
616 if (toupper(str[0]) == toupper(dir[0])) break;
619 SendDlgItemMessageW(hWnd, cmb2, CB_SETCURSEL, n, 0);
620 if (!(ofn->Flags & OFN_SHOWHELP))
621 ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
622 if (ofn->Flags & OFN_HIDEREADONLY)
623 ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
624 if (lfs->hook)
625 return (BOOL) FILEDLG_CallWindowProc(lfs, WM_INITDIALOG, wParam, lfs->lParam);
626 return TRUE;
629 /***********************************************************************
630 * FILEDLG_UpdateResult [internal]
631 * update the displayed file name (with path)
633 void FILEDLG_UpdateResult(LFSPRIVATE lfs, WCHAR *tmpstr)
635 int lenstr2;
636 LPOPENFILENAMEW ofnW = lfs->ofnW;
637 WCHAR tmpstr2[BUFFILE];
639 GetCurrentDirectoryW(BUFFILE, tmpstr2);
640 lenstr2 = strlenW(tmpstr2);
641 if (lenstr2 > 3)
642 tmpstr2[lenstr2++]='\\';
643 lstrcpynW(tmpstr2+lenstr2, tmpstr, BUFFILE-lenstr2);
644 if (ofnW->lpstrFile)
645 lstrcpynW(ofnW->lpstrFile, tmpstr2, ofnW->nMaxFile);
646 ofnW->nFileOffset = strrchrW(tmpstr2,'\\') - tmpstr2 +1;
647 ofnW->nFileExtension = 0;
648 while(tmpstr2[ofnW->nFileExtension] != '.' && tmpstr2[ofnW->nFileExtension] != '\0')
649 ofnW->nFileExtension++;
650 if (tmpstr2[ofnW->nFileExtension] == '\0')
651 ofnW->nFileExtension = 0;
652 else
653 ofnW->nFileExtension++;
654 /* update the real client structures if any */
655 if (lfs->ofn16)
657 char *dest = PTR_SEG_TO_LIN(lfs->ofn16->lpstrFile);
658 if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFile, -1,
659 dest, ofnW->nMaxFile, NULL, NULL ))
660 dest[ofnW->nMaxFile-1] = 0;
661 lfs->ofn16->nFileOffset = ofnW->nFileOffset;
662 lfs->ofn16->nFileExtension = ofnW->nFileExtension;
664 if (lfs->ofnA)
666 if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFile, -1,
667 lfs->ofnA->lpstrFile, ofnW->nMaxFile, NULL, NULL ))
668 lfs->ofnA->lpstrFile[ofnW->nMaxFile-1] = 0;
669 lfs->ofnA->nFileOffset = ofnW->nFileOffset;
670 lfs->ofnA->nFileExtension = ofnW->nFileExtension;
675 /***********************************************************************
676 * FILEDLG_UpdateFileTitle [internal]
677 * update the displayed file name (without path)
679 void FILEDLG_UpdateFileTitle(LFSPRIVATE lfs)
681 LONG lRet;
682 LPOPENFILENAMEW ofnW = lfs->ofnW;
683 if (ofnW->lpstrFileTitle != NULL)
685 lRet = SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
686 SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETTEXT, lRet,
687 (LPARAM)ofnW->lpstrFileTitle );
688 if (lfs->ofn16)
690 char *dest = PTR_SEG_TO_LIN(lfs->ofn16->lpstrFileTitle);
691 if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFileTitle, -1,
692 dest, ofnW->nMaxFileTitle, NULL, NULL ))
693 dest[ofnW->nMaxFileTitle-1] = 0;
695 if (lfs->ofnA)
697 if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFileTitle, -1,
698 lfs->ofnA->lpstrFileTitle, ofnW->nMaxFileTitle, NULL, NULL ))
699 lfs->ofnA->lpstrFileTitle[ofnW->nMaxFileTitle-1] = 0;
706 /***********************************************************************
707 * FILEDLG_DirListDblClick [internal]
709 static LRESULT FILEDLG_DirListDblClick( LFSPRIVATE lfs )
711 LONG lRet;
712 HWND hWnd = lfs->hwnd;
713 LPWSTR pstr;
714 WCHAR tmpstr[BUFFILE];
716 /* get the raw string (with brackets) */
717 lRet = SendDlgItemMessageW(hWnd, lst2, LB_GETCURSEL, 0, 0);
718 if (lRet == LB_ERR) return TRUE;
719 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
720 SendDlgItemMessageW(hWnd, lst2, LB_GETTEXT, lRet,
721 (LPARAM)pstr);
722 strcpyW( tmpstr, pstr );
723 HeapFree(GetProcessHeap(), 0, pstr);
724 /* get the selected directory in tmpstr */
725 if (tmpstr[0] == '[')
727 tmpstr[lstrlenW(tmpstr) - 1] = 0;
728 strcpyW(tmpstr,tmpstr+1);
730 strcatW(tmpstr, FILE_bslash);
732 FILEDLG_ScanDir(hWnd, tmpstr);
733 /* notify the app */
734 if (lfs->hook)
736 if (FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, lst2,
737 MAKELONG(lRet,CD_LBSELCHANGE)))
738 return TRUE;
740 return TRUE;
744 /***********************************************************************
745 * FILEDLG_FileListSelect [internal]
746 * called when a new item is picked in the file list
748 static LRESULT FILEDLG_FileListSelect( LFSPRIVATE lfs )
750 LONG lRet;
751 HWND hWnd = lfs->hwnd;
752 LPWSTR pstr;
754 lRet = SendDlgItemMessageW(hWnd, lst1, LB_GETCURSEL16, 0, 0);
755 if (lRet == LB_ERR)
756 return TRUE;
758 /* set the edit control to the choosen file */
759 if ((pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
761 SendDlgItemMessageW(hWnd, lst1, LB_GETTEXT, lRet,
762 (LPARAM)pstr);
763 SetDlgItemTextW( hWnd, edt1, pstr );
764 HeapFree(GetProcessHeap(), 0, pstr);
766 if (lfs->hook)
768 FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, lst1,
769 MAKELONG(lRet,CD_LBSELCHANGE));
771 /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD,
772 CD_LBSELNOITEMS */
773 return TRUE;
776 /***********************************************************************
777 * FILEDLG_TestPath [internal]
778 * before accepting the file name, test if it includes wild cards
779 * tries to scan the directory and returns TRUE if no error.
781 static LRESULT FILEDLG_TestPath( LFSPRIVATE lfs, LPWSTR path )
783 HWND hWnd = lfs->hwnd;
784 LPWSTR pBeginFileName, pstr2;
785 WCHAR tmpstr2[BUFFILE];
787 pBeginFileName = strrchrW(path, '\\');
788 if (pBeginFileName == NULL)
789 pBeginFileName = strrchrW(path, ':');
791 if (strchrW(path,'*') != NULL || strchrW(path,'?') != NULL)
793 /* edit control contains wildcards */
794 if (pBeginFileName != NULL)
796 lstrcpynW(tmpstr2, pBeginFileName + 1, BUFFILE);
797 *(pBeginFileName + 1) = 0;
799 else
801 strcpyW(tmpstr2, path);
802 *path = 0;
805 TRACE("path=%s, tmpstr2=%s\n", debugstr_w(path), debugstr_w(tmpstr2));
806 SetDlgItemTextW( hWnd, edt1, tmpstr2 );
807 FILEDLG_ScanDir(hWnd, path);
808 return FALSE;
811 /* no wildcards, we might have a directory or a filename */
812 /* try appending a wildcard and reading the directory */
814 pstr2 = path + lstrlenW(path);
815 if (pBeginFileName == NULL || *(pBeginFileName + 1) != 0)
816 strcatW(path, FILE_bslash);
818 /* if ScanDir succeeds, we have changed the directory */
819 if (FILEDLG_ScanDir(hWnd, path))
820 return TRUE;
822 /* if not, this must be a filename */
824 *pstr2 = 0; /* remove the wildcard added before */
826 if (pBeginFileName != NULL)
828 /* strip off the pathname */
829 *pBeginFileName = 0;
830 SetDlgItemTextW( hWnd, edt1, pBeginFileName + 1 );
832 lstrcpynW(tmpstr2, pBeginFileName + 1, sizeof(tmpstr2) );
833 /* Should we MessageBox() if this fails? */
834 if (!FILEDLG_ScanDir(hWnd, path))
836 return FALSE;
838 strcpyW(path, tmpstr2);
840 else
841 SetDlgItemTextW( hWnd, edt1, path );
842 return TRUE;
845 /***********************************************************************
846 * FILEDLG_Validate [internal]
847 * called on: click Ok button, Enter in edit, DoubleClick in file list
849 static LRESULT FILEDLG_Validate( LFSPRIVATE lfs, LPWSTR path, UINT control, INT itemIndex,
850 BOOL internalUse )
852 LONG lRet;
853 HWND hWnd = lfs->hwnd;
854 OPENFILENAMEW ofnsav;
855 LPOPENFILENAMEW ofnW = lfs->ofnW;
856 WCHAR filename[BUFFILE];
858 ofnsav = *ofnW; /* for later restoring */
860 /* get current file name */
861 if (path)
862 lstrcpynW(filename, path, sizeof(filename));
863 else
864 GetDlgItemTextW( hWnd, edt1, filename, sizeof(filename) );
866 /* if we did not click in file list to get there */
867 if (control != lst1)
869 if (!FILEDLG_TestPath( lfs, filename) )
870 return FALSE;
872 FILEDLG_UpdateResult(lfs, filename);
874 if (internalUse)
875 { /* called internally after a change in a combo */
876 if (lfs->hook)
878 FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, control,
879 MAKELONG(itemIndex,CD_LBSELCHANGE));
881 return TRUE;
884 FILEDLG_UpdateFileTitle(lfs);
885 if (lfs->hook)
887 lRet = (BOOL)FILEDLG_CallWindowProc(lfs, lfs->fileokstring,
888 0, lfs->lParam );
889 if (lRet)
891 *ofnW = ofnsav; /* restore old state */
892 return FALSE;
895 if ((ofnW->Flags & OFN_ALLOWMULTISELECT) && (ofnW->Flags & OFN_EXPLORER))
897 if (ofnW->lpstrFile)
899 LPWSTR str = (LPWSTR)ofnW->lpstrFile;
900 LPWSTR ptr = strrchrW(str, '\\');
901 str[lstrlenW(str) + 1] = '\0';
902 *ptr = 0;
905 return TRUE;
908 /***********************************************************************
909 * FILEDLG_DiskChange [internal]
910 * called when a new item is picked in the disk selection combo
912 static LRESULT FILEDLG_DiskChange( LFSPRIVATE lfs )
914 LONG lRet;
915 HWND hWnd = lfs->hwnd;
916 LPWSTR pstr;
917 WCHAR diskname[BUFFILE];
919 FILEDLG_StripEditControl(hWnd);
920 lRet = SendDlgItemMessageW(hWnd, cmb2, CB_GETCURSEL, 0, 0L);
921 if (lRet == LB_ERR)
922 return 0;
923 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
924 SendDlgItemMessageW(hWnd, cmb2, CB_GETLBTEXT, lRet,
925 (LPARAM)pstr);
926 wsprintfW(diskname, FILE_specc, pstr[2]);
927 HeapFree(GetProcessHeap(), 0, pstr);
929 return FILEDLG_Validate( lfs, diskname, cmb2, lRet, TRUE );
933 /***********************************************************************
934 * FILEDLG_FileTypeChange [internal]
935 * called when a new item is picked in the file type combo
937 static LRESULT FILEDLG_FileTypeChange( LFSPRIVATE lfs )
939 LONG lRet;
940 WCHAR diskname[BUFFILE];
941 LPWSTR pstr;
943 diskname[0] = 0;
945 lRet = SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETCURSEL, 0, 0);
946 if (lRet == LB_ERR)
947 return TRUE;
948 pstr = (LPWSTR)SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETITEMDATA, lRet, 0);
949 TRACE("Selected filter : %s\n", debugstr_w(pstr));
950 SetDlgItemTextW( lfs->hwnd, edt1, pstr );
952 return FILEDLG_Validate( lfs, NULL, cmb1, lRet, TRUE );
955 /***********************************************************************
956 * FILEDLG_WMCommand [internal]
958 static LRESULT FILEDLG_WMCommand(HWND hWnd, LPARAM lParam, UINT notification,
959 UINT control, LFSPRIVATE lfs )
961 switch (control)
963 case lst1: /* file list */
964 FILEDLG_StripEditControl(hWnd);
965 if (notification == LBN_DBLCLK)
967 if (FILEDLG_Validate( lfs, NULL, control, 0, FALSE ))
968 EndDialog(hWnd, TRUE);
969 return TRUE;
971 else if (notification == LBN_SELCHANGE)
972 return FILEDLG_FileListSelect( lfs );
973 break;
975 case lst2: /* directory list */
976 FILEDLG_StripEditControl(hWnd);
977 if (notification == LBN_DBLCLK)
978 return FILEDLG_DirListDblClick( lfs );
979 break;
981 case cmb1: /* file type drop list */
982 if (notification == CBN_SELCHANGE)
983 return FILEDLG_FileTypeChange( lfs );
984 break;
986 case chx1:
987 break;
989 case pshHelp:
990 break;
992 case cmb2: /* disk dropdown combo */
993 if (notification == CBN_SELCHANGE)
994 return FILEDLG_DiskChange( lfs );
995 break;
997 case IDOK:
998 if (FILEDLG_Validate( lfs, NULL, control, 0, FALSE ))
999 EndDialog(hWnd, TRUE);
1000 return TRUE;
1002 case IDCANCEL:
1003 EndDialog(hWnd, FALSE);
1004 return TRUE;
1006 case IDABORT: /* can be sent by the hook procedure */
1007 EndDialog(hWnd, TRUE);
1008 return TRUE;
1010 return FALSE;
1013 /***********************************************************************
1014 * FILEDLG_MapDrawItemStruct [internal]
1015 * map a 16 bits drawitem struct to 32
1017 void FILEDLG_MapDrawItemStruct(LPDRAWITEMSTRUCT16 lpdis16, LPDRAWITEMSTRUCT lpdis)
1019 lpdis->CtlType = lpdis16->CtlType;
1020 lpdis->CtlID = lpdis16->CtlID;
1021 lpdis->itemID = lpdis16->itemID;
1022 lpdis->itemAction = lpdis16->itemAction;
1023 lpdis->itemState = lpdis16->itemState;
1024 lpdis->hwndItem = lpdis16->hwndItem;
1025 lpdis->hDC = lpdis16->hDC;
1026 lpdis->rcItem.right = lpdis16->rcItem.right;
1027 lpdis->rcItem.left = lpdis16->rcItem.left;
1028 lpdis->rcItem.top = lpdis16->rcItem.top;
1029 lpdis->rcItem.bottom = lpdis16->rcItem.bottom;
1030 lpdis->itemData = lpdis16->itemData;
1033 /************************************************************************
1034 * FILEDLG_MapStringPairsToW [internal]
1035 * map string pairs to Unicode
1037 LPWSTR FILEDLG_MapStringPairsToW(LPCSTR strA, UINT size)
1039 LPCSTR s;
1040 LPWSTR x;
1041 int n, len;
1043 s = strA;
1044 while (*s)
1045 s = s+strlen(s)+1;
1046 s++;
1047 n = s - strA;
1048 if (n < size) n = size;
1050 len = MultiByteToWideChar( CP_ACP, 0, strA, n, NULL, 0 );
1051 x = HeapAlloc(GetProcessHeap(),0, len * sizeof(WCHAR));
1052 MultiByteToWideChar( CP_ACP, 0, strA, n, x, len );
1053 return x;
1057 /************************************************************************
1058 * FILEDLG_DupToW [internal]
1059 * duplicates an Ansi string to unicode, with a buffer size
1061 LPWSTR FILEDLG_DupToW(LPCSTR str, DWORD size)
1063 LPWSTR strW = NULL;
1064 if (str && (size > 0))
1066 strW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1067 if (strW) MultiByteToWideChar( CP_ACP, 0, str, -1, strW, size );
1069 return strW;
1073 /************************************************************************
1074 * FILEDLG_MapOfnStructA [internal]
1075 * map a 32 bits Ansi structure to an Unicode one
1077 void FILEDLG_MapOfnStructA(LPOPENFILENAMEA ofnA, LPOPENFILENAMEW ofnW, BOOL open)
1079 LPCSTR str;
1081 ofnW->lStructSize = sizeof(OPENFILENAMEW);
1082 ofnW->hwndOwner = ofnA->hwndOwner;
1083 ofnW->hInstance = ofnA->hInstance;
1084 if (ofnA->lpstrFilter)
1085 ofnW->lpstrFilter = FILEDLG_MapStringPairsToW(ofnA->lpstrFilter, 0);
1086 else
1087 ofnW->lpstrFilter = FILEDLG_MapStringPairsToW(defaultfilter, 0);
1089 if ((ofnA->lpstrCustomFilter) && (*(ofnA->lpstrCustomFilter)))
1090 ofnW->lpstrCustomFilter = FILEDLG_MapStringPairsToW(ofnA->lpstrCustomFilter, ofnA->nMaxCustFilter);
1091 ofnW->nMaxCustFilter = ofnA->nMaxCustFilter;
1092 ofnW->nFilterIndex = ofnA->nFilterIndex;
1093 ofnW->nMaxFile = ofnA->nMaxFile;
1094 ofnW->lpstrFile = FILEDLG_DupToW(ofnA->lpstrFile, ofnW->nMaxFile);
1095 ofnW->nMaxFileTitle = ofnA->nMaxFileTitle;
1096 ofnW->lpstrFileTitle = FILEDLG_DupToW(ofnA->lpstrFileTitle, ofnW->nMaxFileTitle);
1097 if (ofnA->lpstrInitialDir)
1098 ofnW->lpstrInitialDir = HEAP_strdupAtoW(GetProcessHeap(),0,ofnA->lpstrInitialDir);
1099 if (ofnA->lpstrTitle)
1100 str = ofnA->lpstrTitle;
1101 else
1102 /* Allocates default title (FIXME : get it from resource) */
1103 str = open ? defaultopen:defaultsave;
1104 ofnW->lpstrTitle = HEAP_strdupAtoW(GetProcessHeap(),0, str);
1105 ofnW->Flags = ofnA->Flags;
1106 ofnW->nFileOffset = ofnA->nFileOffset;
1107 ofnW->nFileExtension = ofnA->nFileExtension;
1108 ofnW->lpstrDefExt = FILEDLG_DupToW(ofnA->lpstrDefExt, 3);
1109 if ((ofnA->Flags & OFN_ENABLETEMPLATE) && (ofnA->lpTemplateName))
1111 if (HIWORD(ofnA->lpTemplateName))
1112 ofnW->lpTemplateName = HEAP_strdupAtoW(GetProcessHeap(), 0, ofnA->lpTemplateName);
1113 else /* numbered resource */
1114 ofnW->lpTemplateName = (LPWSTR) ofnA->lpTemplateName;
1119 /************************************************************************
1120 * FILEDLG_MapOfnStruct16 [internal]
1121 * map a 16 bits structure to an Unicode one
1123 void FILEDLG_MapOfnStruct16(LPOPENFILENAME16 ofn16, LPOPENFILENAMEW ofnW, BOOL open)
1125 OPENFILENAMEA ofnA;
1126 /* first convert to linear pointers */
1127 memset(&ofnA, 0, sizeof(OPENFILENAMEA));
1128 ofnA.lStructSize = sizeof(OPENFILENAMEA);
1129 ofnA.hwndOwner = ofn16->hwndOwner;
1130 ofnA.hInstance = ofn16->hInstance;
1131 if (ofn16->lpstrFilter)
1132 ofnA.lpstrFilter = PTR_SEG_TO_LIN(ofn16->lpstrFilter);
1133 if (ofn16->lpstrCustomFilter)
1134 ofnA.lpstrCustomFilter = PTR_SEG_TO_LIN(ofn16->lpstrCustomFilter);
1135 ofnA.nMaxCustFilter = ofn16->nMaxCustFilter;
1136 ofnA.nFilterIndex = ofn16->nFilterIndex;
1137 ofnA.lpstrFile = PTR_SEG_TO_LIN(ofn16->lpstrFile);
1138 ofnA.nMaxFile = ofn16->nMaxFile;
1139 ofnA.lpstrFileTitle = PTR_SEG_TO_LIN(ofn16->lpstrFileTitle);
1140 ofnA.nMaxFileTitle = ofn16->nMaxFileTitle;
1141 ofnA.lpstrInitialDir = PTR_SEG_TO_LIN(ofn16->lpstrInitialDir);
1142 ofnA.lpstrTitle = PTR_SEG_TO_LIN(ofn16->lpstrTitle);
1143 ofnA.Flags = ofn16->Flags;
1144 ofnA.nFileOffset = ofn16->nFileOffset;
1145 ofnA.nFileExtension = ofn16->nFileExtension;
1146 ofnA.lpstrDefExt = PTR_SEG_TO_LIN(ofn16->lpstrDefExt);
1147 if (HIWORD(ofn16->lpTemplateName))
1148 ofnA.lpTemplateName = PTR_SEG_TO_LIN(ofn16->lpTemplateName);
1149 else
1150 ofnA.lpTemplateName = (LPSTR) ofn16->lpTemplateName; /* ressource number */
1151 /* now calls the 32 bits Ansi to Unicode version to complete the job */
1152 FILEDLG_MapOfnStructA(&ofnA, ofnW, open);
1156 /************************************************************************
1157 * FILEDLG_DestroyPrivate [internal]
1158 * destroys the private object
1160 void FILEDLG_DestroyPrivate(LFSPRIVATE lfs)
1162 HWND hwnd;
1163 if (!lfs) return;
1164 hwnd = lfs->hwnd;
1165 /* free resources for a 16 bits dialog */
1166 if (lfs->hResource16) FreeResource16(lfs->hResource16);
1167 if (lfs->hGlobal16)
1169 GlobalUnlock16(lfs->hGlobal16);
1170 GlobalFree16(lfs->hGlobal16);
1172 /* if ofnW has been allocated, have to free everything in it */
1173 if (lfs->ofn16 || lfs->ofnA)
1175 LPOPENFILENAMEW ofnW = lfs->ofnW;
1176 if (ofnW->lpstrFilter) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrFilter);
1177 if (ofnW->lpstrCustomFilter) HeapFree(GetProcessHeap(), 0, ofnW->lpstrCustomFilter);
1178 if (ofnW->lpstrFile) HeapFree(GetProcessHeap(), 0, ofnW->lpstrFile);
1179 if (ofnW->lpstrFileTitle) HeapFree(GetProcessHeap(), 0, ofnW->lpstrFileTitle);
1180 if (ofnW->lpstrInitialDir) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrInitialDir);
1181 if (ofnW->lpstrTitle) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrTitle);
1182 if ((ofnW->lpTemplateName) && (HIWORD(ofnW->lpTemplateName)))
1183 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpTemplateName);
1184 HeapFree(GetProcessHeap(), 0, ofnW);
1186 TRACE("destroying private allocation %p\n", lfs);
1187 HeapFree(GetProcessHeap(), 0, lfs);
1188 RemovePropA(hwnd, OFN_PROP);
1191 /************************************************************************
1192 * FILEDLG_AllocPrivate [internal]
1193 * allocate a private object to hold 32 bits Unicode
1194 * structure that will be used throughtout the calls, while
1195 * keeping available the original structures and a few variables
1196 * On entry : type = dialog procedure type (16,32A,32W)
1197 * dlgType = dialog type (open or save)
1199 LFSPRIVATE FILEDLG_AllocPrivate(LPARAM lParam, int type, UINT dlgType)
1201 LFSPRIVATE lfs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct FSPRIVATE));
1202 LFSPRIVATE ret;
1203 TRACE("alloc private buf %p\n", lfs);
1204 if (!lfs) return NULL;
1205 lfs->hook = FALSE;
1206 lfs->lParam = lParam;
1207 if (dlgType == OPEN_DIALOG)
1208 lfs->open = TRUE;
1209 else
1210 lfs->open = FALSE;
1211 lfs->lbselchstring = RegisterWindowMessageA(LBSELCHSTRING);
1212 lfs->fileokstring = RegisterWindowMessageA(FILEOKSTRING);
1213 switch(type)
1215 case LFS16:
1216 lfs->ofn16 = (LPOPENFILENAME16) PTR_SEG_TO_LIN(lParam);
1217 if (lfs->ofn16->Flags & OFN_ENABLEHOOK)
1218 if (lfs->ofn16->lpfnHook)
1219 lfs->hook = TRUE;
1221 break;
1223 case LFS32A:
1224 lfs->ofnA = (LPOPENFILENAMEA) lParam;
1225 if (lfs->ofnA->Flags & OFN_ENABLEHOOK)
1226 if (lfs->ofnA->lpfnHook)
1227 lfs->hook = TRUE;
1228 break;
1230 case LFS32W:
1231 lfs->ofnW = (LPOPENFILENAMEW) lParam;
1232 if (lfs->ofnW->Flags & OFN_ENABLEHOOK)
1233 if (lfs->ofnW->lpfnHook)
1234 lfs->hook = TRUE;
1235 break;
1237 ret = lfs;
1238 if (!lfs->ofnW)
1239 { /* this structure is needed internally, so create it */
1240 lfs->ofnW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(OPENFILENAMEW));
1241 if (lfs->ofnW)
1243 if (lfs->ofn16)
1244 FILEDLG_MapOfnStruct16(lfs->ofn16, lfs->ofnW, lfs->open);
1245 if (lfs->ofnA)
1246 FILEDLG_MapOfnStructA(lfs->ofnA, lfs->ofnW, lfs->open);
1248 else
1249 ret = NULL;
1251 if (lfs->ofn16)
1253 if (!Get16BitsTemplate(lfs)) ret = NULL;
1255 else
1256 if (!Get32BitsTemplate(lfs)) ret = NULL;
1257 if (!ret) FILEDLG_DestroyPrivate(lfs);
1258 return ret;
1262 /***********************************************************************
1263 * GetFileName31A [internal]
1265 * Creates a win31 style dialog box for the user to select a file to open/save.
1267 BOOL WINAPI GetFileName31A(
1268 LPOPENFILENAMEA lpofn, /* addess of structure with data*/
1269 UINT dlgType /* type dialogue : open/save */
1272 HINSTANCE hInst;
1273 BOOL bRet = FALSE;
1274 LFSPRIVATE lfs;
1276 if (!lpofn || !FileDlg_Init()) return FALSE;
1278 lfs = FILEDLG_AllocPrivate((LPARAM) lpofn, LFS32A, dlgType);
1279 if (lfs)
1281 hInst = GetWindowLongA( lpofn->hwndOwner, GWL_HINSTANCE );
1282 bRet = DialogBoxIndirectParamA( hInst, lfs->template, lpofn->hwndOwner,
1283 (DLGPROC) FileOpenDlgProc, (DWORD) lfs);
1284 FILEDLG_DestroyPrivate(lfs);
1287 TRACE("return lpstrFile='%s' !\n", lpofn->lpstrFile);
1288 return bRet;
1292 /***********************************************************************
1293 * GetFileName31W [internal]
1295 * Creates a win31 style dialog box for the user to select a file to open/save
1297 BOOL WINAPI GetFileName31W(
1298 LPOPENFILENAMEW lpofn, /* addess of structure with data*/
1299 UINT dlgType /* type dialogue : open/save */
1302 HINSTANCE hInst;
1303 BOOL bRet = FALSE;
1304 LFSPRIVATE lfs;
1306 if (!lpofn || !FileDlg_Init()) return FALSE;
1308 lfs = FILEDLG_AllocPrivate((LPARAM) lpofn, LFS32W, dlgType);
1309 if (lfs)
1311 hInst = GetWindowLongA( lpofn->hwndOwner, GWL_HINSTANCE );
1312 bRet = DialogBoxIndirectParamW( hInst, lfs->template, lpofn->hwndOwner,
1313 (DLGPROC) FileOpenDlgProc, (DWORD) lfs);
1314 FILEDLG_DestroyPrivate(lfs);
1317 TRACE("return lpstrFile='%s' !\n", debugstr_w(lpofn->lpstrFile));
1318 return bRet;
1322 /* ------------------ Dialog procedures ---------------------- */
1324 /***********************************************************************
1325 * FileOpenDlgProc16 (COMMDLG.6)
1327 LRESULT WINAPI FileOpenDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
1328 LPARAM lParam)
1330 LFSPRIVATE lfs = (LFSPRIVATE)GetPropA(hWnd,OFN_PROP);
1331 DRAWITEMSTRUCT dis;
1333 TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
1334 if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
1336 LRESULT lRet = (BOOL16)FILEDLG_CallWindowProc(lfs, wMsg, wParam, lParam);
1337 if (lRet)
1338 return lRet; /* else continue message processing */
1340 switch (wMsg)
1342 case WM_INITDIALOG:
1343 return FILEDLG_WMInitDialog(hWnd, wParam, lParam);
1345 case WM_MEASUREITEM:
1346 return FILEDLG_WMMeasureItem16(hWnd, wParam, lParam);
1348 case WM_DRAWITEM:
1349 FILEDLG_MapDrawItemStruct((LPDRAWITEMSTRUCT16)PTR_SEG_TO_LIN(lParam), &dis);
1350 return FILEDLG_WMDrawItem(hWnd, wParam, lParam, FALSE, &dis);
1352 case WM_COMMAND:
1353 return FILEDLG_WMCommand(hWnd, lParam, HIWORD(lParam),wParam, lfs);
1354 #if 0
1355 case WM_CTLCOLOR:
1356 SetBkColor((HDC16)wParam, 0x00C0C0C0);
1357 switch (HIWORD(lParam))
1359 case CTLCOLOR_BTN:
1360 SetTextColor((HDC16)wParam, 0x00000000);
1361 return hGRAYBrush;
1362 case CTLCOLOR_STATIC:
1363 SetTextColor((HDC16)wParam, 0x00000000);
1364 return hGRAYBrush;
1366 break;
1367 #endif
1369 return FALSE;
1372 /***********************************************************************
1373 * FileSaveDlgProc16 (COMMDLG.7)
1375 LRESULT WINAPI FileSaveDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
1376 LPARAM lParam)
1378 LFSPRIVATE lfs = (LFSPRIVATE)GetPropA(hWnd,OFN_PROP);
1379 DRAWITEMSTRUCT dis;
1381 TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
1382 if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
1384 LRESULT lRet;
1385 lRet = (BOOL16)FILEDLG_CallWindowProc(lfs, wMsg, wParam, lParam);
1386 if (lRet)
1387 return lRet; /* else continue message processing */
1389 switch (wMsg) {
1390 case WM_INITDIALOG:
1391 return FILEDLG_WMInitDialog(hWnd, wParam, lParam);
1393 case WM_MEASUREITEM:
1394 return FILEDLG_WMMeasureItem16(hWnd, wParam, lParam);
1396 case WM_DRAWITEM:
1397 FILEDLG_MapDrawItemStruct((LPDRAWITEMSTRUCT16)PTR_SEG_TO_LIN(lParam), &dis);
1398 return FILEDLG_WMDrawItem(hWnd, wParam, lParam, TRUE, &dis);
1400 case WM_COMMAND:
1401 return FILEDLG_WMCommand(hWnd, lParam, HIWORD(lParam), wParam, lfs);
1405 case WM_CTLCOLOR:
1406 SetBkColor((HDC16)wParam, 0x00C0C0C0);
1407 switch (HIWORD(lParam))
1409 case CTLCOLOR_BTN:
1410 SetTextColor((HDC16)wParam, 0x00000000);
1411 return hGRAYBrush;
1412 case CTLCOLOR_STATIC:
1413 SetTextColor((HDC16)wParam, 0x00000000);
1414 return hGRAYBrush;
1416 return FALSE;
1419 return FALSE;
1422 /***********************************************************************
1423 * FileOpenDlgProc [internal]
1424 * Used for open and save, in fact.
1426 static LRESULT WINAPI FileOpenDlgProc(HWND hWnd, UINT wMsg,
1427 WPARAM wParam, LPARAM lParam)
1429 LFSPRIVATE lfs = (LFSPRIVATE)GetPropA(hWnd,OFN_PROP);
1431 TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
1432 if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
1434 LRESULT lRet;
1435 lRet = (BOOL)FILEDLG_CallWindowProc(lfs, wMsg, wParam, lParam);
1436 if (lRet)
1437 return lRet; /* else continue message processing */
1439 switch (wMsg)
1441 case WM_INITDIALOG:
1442 return FILEDLG_WMInitDialog(hWnd, wParam, lParam);
1444 case WM_MEASUREITEM:
1445 return FILEDLG_WMMeasureItem(hWnd, wParam, lParam);
1447 case WM_DRAWITEM:
1448 return FILEDLG_WMDrawItem(hWnd, wParam, lParam, !lfs->open, (DRAWITEMSTRUCT *)lParam);
1450 case WM_COMMAND:
1451 return FILEDLG_WMCommand(hWnd, lParam, HIWORD(wParam), LOWORD(wParam), lfs);
1452 #if 0
1453 case WM_CTLCOLOR:
1454 SetBkColor((HDC16)wParam, 0x00C0C0C0);
1455 switch (HIWORD(lParam))
1457 case CTLCOLOR_BTN:
1458 SetTextColor((HDC16)wParam, 0x00000000);
1459 return hGRAYBrush;
1460 case CTLCOLOR_STATIC:
1461 SetTextColor((HDC16)wParam, 0x00000000);
1462 return hGRAYBrush;
1464 break;
1465 #endif
1467 return FALSE;
1470 /* ------------------ APIs ---------------------- */
1472 /***********************************************************************
1473 * GetOpenFileName16 (COMMDLG.1)
1475 * Creates a dialog box for the user to select a file to open.
1477 * RETURNS
1478 * TRUE on success: user selected a valid file
1479 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1481 * BUGS
1482 * unknown, there are some FIXME's left.
1484 BOOL16 WINAPI GetOpenFileName16(
1485 SEGPTR ofn /* addess of structure with data*/
1488 HINSTANCE hInst;
1489 BOOL bRet = FALSE;
1490 LPOPENFILENAME16 lpofn = (LPOPENFILENAME16)PTR_SEG_TO_LIN(ofn);
1491 LFSPRIVATE lfs;
1492 FARPROC16 ptr;
1494 if (!lpofn || !FileDlg_Init()) return FALSE;
1496 lfs = FILEDLG_AllocPrivate((LPARAM) ofn, LFS16, OPEN_DIALOG);
1497 if (lfs)
1499 hInst = GetWindowLongA( lpofn->hwndOwner, GWL_HINSTANCE );
1500 ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (LPCSTR) 6);
1501 bRet = DialogBoxIndirectParam16( hInst, lfs->hDlgTmpl16, lpofn->hwndOwner,
1502 (DLGPROC16) ptr, (DWORD) lfs);
1503 FILEDLG_DestroyPrivate(lfs);
1506 TRACE("return lpstrFile='%s' !\n",
1507 (LPSTR)PTR_SEG_TO_LIN(lpofn->lpstrFile));
1508 return bRet;
1511 /***********************************************************************
1512 * GetSaveFileName16 (COMMDLG.2)
1514 * Creates a dialog box for the user to select a file to save.
1516 * RETURNS
1517 * TRUE on success: user enters a valid file
1518 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1520 * BUGS
1521 * unknown. There are some FIXME's left.
1523 BOOL16 WINAPI GetSaveFileName16(
1524 SEGPTR ofn /* addess of structure with data*/
1527 HINSTANCE hInst;
1528 BOOL bRet = FALSE;
1529 LPOPENFILENAME16 lpofn = (LPOPENFILENAME16)PTR_SEG_TO_LIN(ofn);
1530 LFSPRIVATE lfs;
1531 FARPROC16 ptr;
1533 if (!lpofn || !FileDlg_Init()) return FALSE;
1535 lfs = FILEDLG_AllocPrivate((LPARAM) ofn, LFS16, SAVE_DIALOG);
1536 if (lfs)
1538 hInst = GetWindowLongA( lpofn->hwndOwner, GWL_HINSTANCE );
1539 ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (LPCSTR) 7);
1540 bRet = DialogBoxIndirectParam16( hInst, lfs->hDlgTmpl16, lpofn->hwndOwner,
1541 (DLGPROC16) ptr, (DWORD) lfs);
1542 FILEDLG_DestroyPrivate(lfs);
1545 TRACE("return lpstrFile='%s' !\n",
1546 (LPSTR)PTR_SEG_TO_LIN(lpofn->lpstrFile));
1547 return bRet;
1550 /***********************************************************************
1551 * GetOpenFileNameA (COMDLG32.10)
1553 * Creates a dialog box for the user to select a file to open.
1555 * RETURNS
1556 * TRUE on success: user enters a valid file
1557 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1560 BOOL WINAPI GetOpenFileNameA(
1561 LPOPENFILENAMEA ofn) /* address of init structure */
1563 BOOL newlook;
1565 /* some flags don't allow to match the TWEAK_WineLook */
1566 if (ofn->Flags & (OFN_ALLOWMULTISELECT|OFN_ENABLEHOOK|OFN_ENABLETEMPLATE))
1568 newlook = (ofn->Flags & OFN_EXPLORER) ? TRUE : FALSE;
1570 else
1572 /* no special flags set, we can match the TWEAK_WineLook */
1573 newlook = (TWEAK_WineLook>WIN31_LOOK) ? TRUE : FALSE;
1576 if (newlook)
1578 return GetFileDialog95A(ofn, OPEN_DIALOG);
1580 else
1582 return GetFileName31A(ofn, OPEN_DIALOG);
1586 /***********************************************************************
1587 * GetOpenFileNameW (COMDLG32.11)
1589 * Creates a dialog box for the user to select a file to open.
1591 * RETURNS
1592 * TRUE on success: user enters a valid file
1593 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1596 BOOL WINAPI GetOpenFileNameW(
1597 LPOPENFILENAMEW ofn) /* address of init structure */
1599 BOOL newlook;
1601 /* some flags don't allow to match the TWEAK_WineLook */
1602 if (ofn->Flags & (OFN_ALLOWMULTISELECT|OFN_ENABLEHOOK|OFN_ENABLETEMPLATE))
1604 newlook = (ofn->Flags & OFN_EXPLORER) ? TRUE : FALSE;
1606 else
1608 /* no special flags set, we can match the TWEAK_WineLook */
1609 newlook = (TWEAK_WineLook>WIN31_LOOK) ? TRUE : FALSE;
1612 if (newlook)
1614 return GetFileDialog95W(ofn, OPEN_DIALOG);
1616 else
1618 return GetFileName31W(ofn, OPEN_DIALOG);
1622 /***********************************************************************
1623 * GetSaveFileNameA (COMDLG32.12)
1625 * Creates a dialog box for the user to select a file to save.
1627 * RETURNS
1628 * TRUE on success: user enters a valid file
1629 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1632 BOOL WINAPI GetSaveFileNameA(
1633 LPOPENFILENAMEA ofn) /* address of init structure */
1635 BOOL newlook;
1637 /* some flags don't allow to match the TWEAK_WineLook */
1638 if (ofn->Flags & (OFN_ALLOWMULTISELECT|OFN_ENABLEHOOK|OFN_ENABLETEMPLATE))
1640 newlook = (ofn->Flags & OFN_EXPLORER) ? TRUE : FALSE;
1642 else
1644 /* no special flags set, we can match the TWEAK_WineLook */
1645 newlook = (TWEAK_WineLook>WIN31_LOOK) ? TRUE : FALSE;
1648 if (newlook)
1650 return GetFileDialog95A(ofn, SAVE_DIALOG);
1652 else
1654 return GetFileName31A(ofn, SAVE_DIALOG);
1658 /***********************************************************************
1659 * GetSaveFileNameW (COMDLG32.13)
1661 * Creates a dialog box for the user to select a file to save.
1663 * RETURNS
1664 * TRUE on success: user enters a valid file
1665 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1668 BOOL WINAPI GetSaveFileNameW(
1669 LPOPENFILENAMEW ofn) /* address of init structure */
1671 BOOL newlook;
1673 /* some flags don't allow to match the TWEAK_WineLook */
1674 if (ofn->Flags & (OFN_ALLOWMULTISELECT|OFN_ENABLEHOOK|OFN_ENABLETEMPLATE))
1676 newlook = (ofn->Flags & OFN_EXPLORER) ? TRUE : FALSE;
1678 else
1680 /* no special flags set, we can match the TWEAK_WineLook */
1681 newlook = (TWEAK_WineLook>WIN31_LOOK) ? TRUE : FALSE;
1684 if (newlook)
1686 return GetFileDialog95W(ofn, SAVE_DIALOG);
1688 else
1690 return GetFileName31W(ofn, SAVE_DIALOG);