Fix return value to 16 bits for 16 bit hook; remove leading space for
[wine/multimedia.git] / dlls / commdlg / filedlg.c
blob3ccf30c2ff5f3d4ba265095c36ac04861c1d64e0
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 "winbase.h"
13 #include "wingdi.h"
14 #include "wine/winbase16.h"
15 #include "wine/winuser16.h"
16 #include "wine/unicode.h"
17 #include "ldt.h"
18 #include "heap.h"
19 #include "commdlg.h"
20 #include "dialog.h"
21 #include "dlgs.h"
22 #include "module.h"
23 #include "drive.h"
24 #include "debugtools.h"
25 #include "winproc.h"
26 #include "cderr.h"
27 #include "tweak.h"
29 DEFAULT_DEBUG_CHANNEL(commdlg);
31 #include "cdlg.h"
33 #define BUFFILE 512
34 #define BUFFILEALLOC 512 * sizeof(WCHAR)
36 struct FSPRIVATE
38 HWND hwnd; /* file dialog window handle */
39 BOOL hook; /* TRUE if the dialog is hooked */
40 UINT lbselchstring; /* registered message id */
41 UINT fileokstring; /* registered message id */
42 LPARAM lParam; /* save original lparam */
43 HANDLE16 hDlgTmpl16; /* handle for resource 16 */
44 HANDLE16 hResource16; /* handle for allocated resource 16 */
45 HANDLE16 hGlobal16; /* 16 bits mem block (resources) */
46 LPCVOID template; /* template for 32 bits resource */
47 BOOL open; /* TRUE if open dialog, FALSE if save dialog */
48 OPENFILENAMEW *ofnW; /* original structure or work struct */
49 OPENFILENAMEA *ofnA; /* original structure if 32bits ansi dialog */
50 OPENFILENAME16 *ofn16; /* original structure if 16 bits dialog */
54 #define LFSPRIVATE struct FSPRIVATE *
56 #define LFS16 1
57 #define LFS32A 2
58 #define LFS32W 3
60 static const WCHAR FILE_star[] = {'*','.','*', 0};
61 static const WCHAR FILE_bslash[] = {'\\', 0};
62 static const WCHAR FILE_specc[] = {'%','c',':', 0};
64 static HICON16 hFolder = 0;
65 static HICON16 hFolder2 = 0;
66 static HICON16 hFloppy = 0;
67 static HICON16 hHDisk = 0;
68 static HICON16 hCDRom = 0;
69 static HICON16 hNet = 0;
70 static int fldrHeight = 0;
71 static int fldrWidth = 0;
73 #define OFN_PROP "FILEDLG_OFN"
75 static const char defaultfilter[]=" \0\0";
76 static char defaultopen[]="Open File";
77 static char defaultsave[]="Save as";
79 /***********************************************************************
81 * Windows 3.1 style OpenFileName/SaveFileName dialog
85 LRESULT WINAPI FileOpenDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
86 LPARAM lParam);
87 LRESULT WINAPI FileSaveDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
88 LPARAM lParam);
90 static LRESULT WINAPI FileOpenDlgProc(HWND hDlg, UINT msg,
91 WPARAM wParam, LPARAM lParam);
93 /***********************************************************************
94 * FileDlg_Init [internal]
96 static BOOL FileDlg_Init(void)
98 static BOOL initialized = 0;
99 CURSORICONINFO *fldrInfo;
101 if (!initialized) {
102 if (!hFolder) hFolder = LoadIcon16(0, MAKEINTRESOURCE16(OIC_FOLDER));
103 if (!hFolder2) hFolder2 = LoadIcon16(0, MAKEINTRESOURCE16(OIC_FOLDER2));
104 if (!hFloppy) hFloppy = LoadIcon16(0, MAKEINTRESOURCE16(OIC_FLOPPY));
105 if (!hHDisk) hHDisk = LoadIcon16(0, MAKEINTRESOURCE16(OIC_HDISK));
106 if (!hCDRom) hCDRom = LoadIcon16(0, MAKEINTRESOURCE16(OIC_CDROM));
107 if (!hNet) hNet = LoadIcon16(0, MAKEINTRESOURCE16(OIC_NETWORK));
108 if (hFolder == 0 || hFolder2 == 0 || hFloppy == 0 ||
109 hHDisk == 0 || hCDRom == 0 || hNet == 0)
111 ERR("Error loading icons !\n");
112 return FALSE;
114 fldrInfo = (CURSORICONINFO *) GlobalLock16( hFolder2 );
115 if (!fldrInfo)
117 ERR("Error measuring icons !\n");
118 return FALSE;
120 fldrHeight = fldrInfo -> nHeight;
121 fldrWidth = fldrInfo -> nWidth;
122 GlobalUnlock16( hFolder2 );
123 initialized = TRUE;
125 return TRUE;
129 /***********************************************************************
130 * Get32BitsTemplate [internal]
132 * Get a template (or FALSE if failure) when 16 bits dialogs are used
133 * by a 32 bits application
136 BOOL Get32BitsTemplate(LFSPRIVATE lfs)
138 LPOPENFILENAMEW ofnW = lfs->ofnW;
139 HANDLE hDlgTmpl;
141 if (ofnW->Flags & OFN_ENABLETEMPLATEHANDLE)
143 if (!(lfs->template = LockResource( ofnW->hInstance )))
145 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
146 return FALSE;
149 else if (ofnW->Flags & OFN_ENABLETEMPLATE)
151 HANDLE hResInfo;
152 if (lfs->ofnA)
153 hResInfo = FindResourceA(lfs->ofnA->hInstance,
154 lfs->ofnA->lpTemplateName,
155 RT_DIALOGA);
156 else
157 hResInfo = FindResourceW(ofnW->hInstance,
158 ofnW->lpTemplateName,
159 RT_DIALOGW);
160 if (!hResInfo)
162 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
163 return FALSE;
165 if (!(hDlgTmpl = LoadResource(ofnW->hInstance,
166 hResInfo)) ||
167 !(lfs->template = LockResource(hDlgTmpl)))
169 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
170 return FALSE;
172 } else { /* get it from internal Wine resource */
173 HANDLE hResInfo;
174 if (!(hResInfo = FindResourceA(COMMDLG_hInstance32,
175 lfs->open? "OPEN_FILE":"SAVE_FILE", RT_DIALOGA)))
177 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
178 return FALSE;
180 if (!(hDlgTmpl = LoadResource(COMMDLG_hInstance32, hResInfo )) ||
181 !(lfs->template = LockResource( hDlgTmpl )))
183 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
184 return FALSE;
187 return TRUE;
191 /***********************************************************************
192 * Get16BitsTemplate [internal]
194 * Get a template (FALSE if failure) when 16 bits dialogs are used
195 * by a 16 bits application
198 BOOL Get16BitsTemplate(LFSPRIVATE lfs)
200 LPOPENFILENAME16 ofn16 = lfs->ofn16;
201 LPCVOID template;
202 HGLOBAL16 hGlobal16 = 0;
204 if (ofn16->Flags & OFN_ENABLETEMPLATEHANDLE)
205 lfs->hDlgTmpl16 = ofn16->hInstance;
206 else if (ofn16->Flags & OFN_ENABLETEMPLATE)
208 HANDLE16 hResInfo;
209 if (!(hResInfo = FindResource16(ofn16->hInstance,
210 ofn16->lpTemplateName,
211 RT_DIALOG16)))
213 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
214 return FALSE;
216 if (!(lfs->hDlgTmpl16 = LoadResource16( ofn16->hInstance, hResInfo )))
218 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
219 return FALSE;
221 lfs->hResource16 = lfs->hDlgTmpl16;
223 else
224 { /* get resource from (32 bits) own Wine resource; convert it to 16 */
225 HANDLE hResInfo, hDlgTmpl32;
226 LPCVOID template32;
227 DWORD size;
229 if (!(hResInfo = FindResourceA(COMMDLG_hInstance32,
230 lfs->open ? "OPEN_FILE":"SAVE_FILE", RT_DIALOGA)))
232 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
233 return FALSE;
235 if (!(hDlgTmpl32 = LoadResource(COMMDLG_hInstance32, hResInfo )) ||
236 !(template32 = LockResource( hDlgTmpl32 )))
238 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
239 return FALSE;
241 size = SizeofResource(GetModuleHandleA("COMDLG32"), hResInfo);
242 hGlobal16 = GlobalAlloc16(0, size);
243 if (!hGlobal16)
245 COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
246 ERR("alloc failure for %ld bytes\n", size);
247 return FALSE;
249 template = GlobalLock16(hGlobal16);
250 if (!template)
252 COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE);
253 ERR("global lock failure for %x handle\n", hGlobal16);
254 GlobalFree16(hGlobal16);
255 return FALSE;
257 ConvertDialog32To16((LPVOID)template32, size, (LPVOID)template);
258 lfs->hDlgTmpl16 = hGlobal16;
259 lfs->hGlobal16 = hGlobal16;
261 return TRUE;
264 /***********************************************************************
265 * FILEDLG_StripEditControl [internal]
266 * Strip pathnames off the contents of the edit control.
268 static void FILEDLG_StripEditControl(HWND hwnd)
270 WCHAR temp[BUFFILE], *cp;
272 GetDlgItemTextW( hwnd, edt1, temp, sizeof(temp) );
273 cp = strrchrW(temp, '\\');
274 if (cp != NULL) {
275 strcpyW(temp, cp+1);
277 cp = strrchrW(temp, ':');
278 if (cp != NULL) {
279 strcpyW(temp, cp+1);
281 /* FIXME: shouldn't we do something with the result here? ;-) */
286 /***********************************************************************
287 * FILEDLG_CallWindowProc [internal]
289 * Call the appropriate hook
291 static BOOL FILEDLG_CallWindowProc(LFSPRIVATE lfs, UINT wMsg, WPARAM wParam,
292 LPARAM lParam)
294 if (lfs->ofn16)
296 return (BOOL16) CallWindowProc16(
297 (WNDPROC16)lfs->ofn16->lpfnHook, lfs->hwnd,
298 (UINT16)wMsg, (WPARAM16)wParam, lParam);
300 if (lfs->ofnA)
302 return (BOOL) CallWindowProcA(
303 (WNDPROC)lfs->ofnA->lpfnHook, lfs->hwnd,
304 wMsg, wParam, lParam);
307 if (lfs->ofnW)
309 return (BOOL) CallWindowProcW(
310 (WNDPROC)lfs->ofnW->lpfnHook, lfs->hwnd,
311 wMsg, wParam, lParam);
313 return FALSE;
317 /***********************************************************************
318 * FILEDLG_ScanDir [internal]
320 static BOOL FILEDLG_ScanDir(HWND hWnd, LPWSTR newPath)
322 WCHAR buffer[BUFFILE];
323 HWND hdlg;
325 lstrcpynW(buffer, newPath, sizeof(buffer));
327 if ( !SetCurrentDirectoryW( newPath ))
328 return FALSE;
329 /* get the list of spec files */
330 GetDlgItemTextW(hWnd, edt1, buffer, sizeof(buffer));
331 /* list of files */
332 if ((hdlg = GetDlgItem(hWnd, lst1)) != 0) {
333 WCHAR* scptr; /* ptr on semi-colon */
334 WCHAR* filter = buffer;
336 TRACE("Using filter %s\n", debugstr_w(filter));
337 SendMessageW(hdlg, LB_RESETCONTENT, 0, 0);
338 while (filter) {
339 scptr = strchrW(filter, ';');
340 if (scptr) *scptr = 0;
341 while (*filter == ' ') filter++;
342 TRACE("Using file spec %s\n", debugstr_w(filter));
343 if (SendMessageW(hdlg, LB_DIR, 0, (LPARAM)filter) == LB_ERR)
344 return FALSE;
345 if (scptr) *scptr = ';';
346 filter = (scptr) ? (scptr + 1) : 0;
349 /* list of directories */
350 strcpyW(buffer, FILE_star);
351 return DlgDirListW(hWnd, buffer, lst2, stc1, DDL_EXCLUSIVE | DDL_DIRECTORY);
354 /***********************************************************************
355 * FILEDLG_GetFileType [internal]
358 static LPWSTR FILEDLG_GetFileType(LPWSTR cfptr, LPWSTR fptr, WORD index)
360 int n, i;
361 i = 0;
362 if (cfptr)
363 for ( ;(n = lstrlenW(cfptr)) != 0; i++)
365 cfptr += n + 1;
366 if (i == index)
367 return cfptr;
368 cfptr += lstrlenW(cfptr) + 1;
370 if (fptr)
371 for ( ;(n = lstrlenW(fptr)) != 0; i++)
373 fptr += n + 1;
374 if (i == index)
375 return fptr;
376 fptr += lstrlenW(fptr) + 1;
378 return (LPWSTR) FILE_star; /* FIXME */
381 /***********************************************************************
382 * FILEDLG_WMDrawItem [internal]
384 static LONG FILEDLG_WMDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam,
385 int savedlg, LPDRAWITEMSTRUCT lpdis)
387 WCHAR *str;
388 HICON hIcon;
389 COLORREF oldText = 0, oldBk = 0;
391 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst1)
393 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC))) return FALSE;
394 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
395 (LPARAM)str);
397 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
399 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
400 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
402 if (savedlg)
403 SetTextColor(lpdis->hDC,GetSysColor(COLOR_GRAYTEXT) );
405 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + 1,
406 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
407 &(lpdis->rcItem), str, lstrlenW(str), NULL);
409 if (lpdis->itemState & ODS_SELECTED)
410 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
412 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
414 SetBkColor( lpdis->hDC, oldBk );
415 SetTextColor( lpdis->hDC, oldText );
417 HeapFree(GetProcessHeap(), 0, str);
418 return TRUE;
421 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst2)
423 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
424 return FALSE;
425 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
426 (LPARAM)str);
428 if (lpdis->itemState & ODS_SELECTED)
430 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
431 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
433 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
434 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
435 &(lpdis->rcItem), str, lstrlenW(str), NULL);
437 if (lpdis->itemState & ODS_SELECTED)
438 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
440 if (lpdis->itemState & ODS_SELECTED)
442 SetBkColor( lpdis->hDC, oldBk );
443 SetTextColor( lpdis->hDC, oldText );
445 DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hFolder);
446 HeapFree(GetProcessHeap(), 0, str);
447 return TRUE;
449 if (lpdis->CtlType == ODT_COMBOBOX && lpdis->CtlID == cmb2)
451 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
452 return FALSE;
453 SendMessageW(lpdis->hwndItem, CB_GETLBTEXT, lpdis->itemID,
454 (LPARAM)str);
455 switch(DRIVE_GetType( str[2] - 'a' ))
457 case TYPE_FLOPPY: hIcon = hFloppy; break;
458 case TYPE_CDROM: hIcon = hCDRom; break;
459 case TYPE_NETWORK: hIcon = hNet; break;
460 case TYPE_HD:
461 default: hIcon = hHDisk; break;
463 if (lpdis->itemState & ODS_SELECTED)
465 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
466 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
468 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
469 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
470 &(lpdis->rcItem), str, lstrlenW(str), NULL);
472 if (lpdis->itemState & ODS_SELECTED)
474 SetBkColor( lpdis->hDC, oldBk );
475 SetTextColor( lpdis->hDC, oldText );
477 DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hIcon);
478 HeapFree(GetProcessHeap(), 0, str);
479 return TRUE;
481 return FALSE;
484 /***********************************************************************
485 * FILEDLG_WMMeasureItem [internal]
487 static LONG FILEDLG_WMMeasureItem(HWND hWnd, WPARAM wParam, LPARAM lParam)
489 LPMEASUREITEMSTRUCT lpmeasure;
491 lpmeasure = (LPMEASUREITEMSTRUCT)lParam;
492 lpmeasure->itemHeight = fldrHeight;
493 return TRUE;
496 /***********************************************************************
497 * FILEDLG_WMMeasureItem16 [internal]
499 static LONG FILEDLG_WMMeasureItem16(HWND16 hWnd, WPARAM16 wParam, LPARAM lParam)
501 LPMEASUREITEMSTRUCT16 lpmeasure;
503 lpmeasure = (LPMEASUREITEMSTRUCT16)PTR_SEG_TO_LIN(lParam);
504 lpmeasure->itemHeight = fldrHeight;
505 return TRUE;
508 /***********************************************************************
509 * FILEDLG_WMInitDialog [internal]
512 static LONG FILEDLG_WMInitDialog(HWND hWnd, WPARAM wParam, LPARAM lParam)
514 int i, n;
515 WCHAR tmpstr[BUFFILE];
516 LPWSTR pstr, old_pstr;
517 LPOPENFILENAMEW ofn;
518 LFSPRIVATE lfs = (LFSPRIVATE) lParam;
520 if (!lfs) return FALSE;
521 SetPropA(hWnd, OFN_PROP, (HANDLE)lfs);
522 lfs->hwnd = hWnd;
523 ofn = lfs->ofnW;
525 TRACE("flags=%lx initialdir=%s\n", ofn->Flags, debugstr_w(ofn->lpstrInitialDir));
527 SetWindowTextW( hWnd, ofn->lpstrTitle );
528 /* read custom filter information */
529 if (ofn->lpstrCustomFilter)
531 pstr = ofn->lpstrCustomFilter;
532 n = 0;
533 TRACE("lpstrCustomFilter = %p\n", pstr);
534 while(*pstr)
536 old_pstr = pstr;
537 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
538 (LPARAM)(ofn->lpstrCustomFilter) + n );
539 n += lstrlenW(pstr) + 1;
540 pstr += lstrlenW(pstr) + 1;
541 TRACE("add str='%s' "
542 "associated to '%s'\n", debugstr_w(old_pstr), debugstr_w(pstr));
543 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
544 n += lstrlenW(pstr) + 1;
545 pstr += lstrlenW(pstr) + 1;
548 /* read filter information */
549 if (ofn->lpstrFilter) {
550 pstr = (LPWSTR) ofn->lpstrFilter;
551 n = 0;
552 while(*pstr) {
553 old_pstr = pstr;
554 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
555 (LPARAM)(ofn->lpstrFilter + n) );
556 n += lstrlenW(pstr) + 1;
557 pstr += lstrlenW(pstr) + 1;
558 TRACE("add str='%s' "
559 "associated to '%s'\n", debugstr_w(old_pstr), debugstr_w(pstr));
560 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
561 n += lstrlenW(pstr) + 1;
562 pstr += lstrlenW(pstr) + 1;
565 /* set default filter */
566 if (ofn->nFilterIndex == 0 && ofn->lpstrCustomFilter == NULL)
567 ofn->nFilterIndex = 1;
568 SendDlgItemMessageW(hWnd, cmb1, CB_SETCURSEL, ofn->nFilterIndex - 1, 0);
569 lstrcpynW(tmpstr, FILEDLG_GetFileType(ofn->lpstrCustomFilter,
570 (LPWSTR)ofn->lpstrFilter, ofn->nFilterIndex - 1),BUFFILE);
571 TRACE("nFilterIndex = %ld, SetText of edt1 to '%s'\n",
572 ofn->nFilterIndex, debugstr_w(tmpstr));
573 SetDlgItemTextW( hWnd, edt1, tmpstr );
574 /* get drive list */
575 *tmpstr = 0;
576 DlgDirListComboBoxW(hWnd, tmpstr, cmb2, 0, DDL_DRIVES | DDL_EXCLUSIVE);
577 /* read initial directory */
578 if (ofn->lpstrInitialDir != NULL)
580 int len;
581 lstrcpynW(tmpstr, ofn->lpstrInitialDir, 511);
582 len = lstrlenW(tmpstr);
583 if (len > 0 && tmpstr[len-1] != '\\' && tmpstr[len-1] != ':') {
584 tmpstr[len]='\\';
585 tmpstr[len+1]='\0';
588 else
589 *tmpstr = 0;
590 if (!FILEDLG_ScanDir(hWnd, tmpstr)) {
591 *tmpstr = 0;
592 if (!FILEDLG_ScanDir(hWnd, tmpstr))
593 WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr));
595 /* select current drive in combo 2, omit missing drives */
596 for(i = 0, n = -1; i <= DRIVE_GetCurrentDrive(); i++)
597 if (DRIVE_IsValid(i)) n++;
598 SendDlgItemMessageW(hWnd, cmb2, CB_SETCURSEL, n, 0);
599 if (!(ofn->Flags & OFN_SHOWHELP))
600 ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
601 if (ofn->Flags & OFN_HIDEREADONLY)
602 ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
603 if (lfs->hook)
604 return (BOOL) FILEDLG_CallWindowProc(lfs, WM_INITDIALOG, wParam, lfs->lParam);
605 return TRUE;
608 /***********************************************************************
609 * FILEDLG_UpdateResult [internal]
610 * update the displayed file name (with path)
612 void FILEDLG_UpdateResult(LFSPRIVATE lfs, WCHAR *tmpstr, WCHAR *tmpstr2)
614 int lenstr2;
615 LPOPENFILENAMEW ofnW = lfs->ofnW;
617 GetCurrentDirectoryW(BUFFILE, tmpstr2);
618 lenstr2 = lstrlenW(tmpstr2);
619 if (lenstr2 > 3)
620 tmpstr2[lenstr2++]='\\';
621 lstrcpynW(tmpstr2+lenstr2, tmpstr, BUFFILE-lenstr2);
622 if (ofnW->lpstrFile)
623 lstrcpynW(ofnW->lpstrFile, tmpstr2, ofnW->nMaxFile);
624 ofnW->nFileOffset = strrchrW(tmpstr2,'\\') - tmpstr2 +1;
625 ofnW->nFileExtension = 0;
626 while(tmpstr2[ofnW->nFileExtension] != '.' && tmpstr2[ofnW->nFileExtension] != '\0')
627 ofnW->nFileExtension++;
628 if (tmpstr2[ofnW->nFileExtension] == '\0')
629 ofnW->nFileExtension = 0;
630 else
631 ofnW->nFileExtension++;
632 /* update the real client structures if any */
633 if (lfs->ofn16)
635 lstrcpynWtoA(PTR_SEG_TO_LIN(lfs->ofn16->lpstrFile), ofnW->lpstrFile, ofnW->nMaxFile);
636 lfs->ofn16->nFileOffset = ofnW->nFileOffset;
637 lfs->ofn16->nFileExtension = ofnW->nFileExtension;
639 if (lfs->ofnA)
641 lstrcpynWtoA(lfs->ofnA->lpstrFile, ofnW->lpstrFile, ofnW->nMaxFile);
642 lfs->ofnA->nFileOffset = ofnW->nFileOffset;
643 lfs->ofnA->nFileExtension = ofnW->nFileExtension;
648 /***********************************************************************
649 * FILEDLG_UpdateFileTitle [internal]
650 * update the displayed file name (without path)
652 void FILEDLG_UpdateFileTitle(LFSPRIVATE lfs)
654 LONG lRet;
655 LPOPENFILENAMEW ofnW = lfs->ofnW;
656 if (ofnW->lpstrFileTitle != NULL)
658 lRet = SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
659 SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETTEXT, lRet,
660 (LPARAM)ofnW->lpstrFileTitle );
661 if (lfs->ofn16)
662 lstrcpynWtoA(PTR_SEG_TO_LIN(lfs->ofn16->lpstrFileTitle),ofnW->lpstrFileTitle,
663 ofnW->nMaxFileTitle);
664 if (lfs->ofnA)
665 lstrcpynWtoA(lfs->ofnA->lpstrFileTitle,ofnW->lpstrFileTitle,
666 ofnW->nMaxFileTitle);
670 /***********************************************************************
671 * FILEDLG_WMCommand [internal]
673 BOOL in_update=FALSE;
675 static LRESULT FILEDLG_WMCommand(HWND hWnd, LPARAM lParam, UINT notification,
676 UINT control, LFSPRIVATE lfs )
678 LONG lRet;
679 OPENFILENAMEW ofn2;
680 LPOPENFILENAMEW ofnW = lfs->ofnW;
681 WCHAR tmpstr[BUFFILE], tmpstr2[BUFFILE];
682 LPWSTR pstr, pstr2;
684 switch (control)
686 case lst1: /* file list */
687 FILEDLG_StripEditControl(hWnd);
688 if (notification == LBN_DBLCLK)
689 goto almost_ok;
690 /* notify the app */
691 lRet = SendDlgItemMessageW(hWnd, lst1, LB_GETCURSEL16, 0, 0);
692 if (lRet == LB_ERR) return TRUE;
693 if ((pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
695 SendDlgItemMessageW(hWnd, lst1, LB_GETTEXT, lRet,
696 (LPARAM)pstr);
697 SetDlgItemTextW( hWnd, edt1, pstr );
698 HeapFree(GetProcessHeap(), 0, pstr);
700 if (lfs->hook)
701 FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, control,
702 MAKELONG(lRet,CD_LBSELCHANGE));
703 /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD, CD_LBSELNOITEMS */
704 return TRUE;
706 case lst2: /* directory list */
707 FILEDLG_StripEditControl(hWnd);
708 if (notification == LBN_DBLCLK)
710 /* get the raw string (with brackets) */
711 lRet = SendDlgItemMessageW(hWnd, lst2, LB_GETCURSEL, 0, 0);
712 if (lRet == LB_ERR) return TRUE;
713 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
714 SendDlgItemMessageW(hWnd, lst2, LB_GETTEXT, lRet,
715 (LPARAM)pstr);
716 strcpyW( tmpstr, pstr );
717 HeapFree(GetProcessHeap(), 0, pstr);
718 /* get the selected directory in tmpstr */
719 if (tmpstr[0] == '[')
721 tmpstr[lstrlenW(tmpstr) - 1] = 0;
722 strcpyW(tmpstr,tmpstr+1);
724 strcatW(tmpstr, FILE_bslash);
725 /* directory *has* to be changed before notifying the hook */
726 SetCurrentDirectoryW( tmpstr );
727 /* notify the app */
728 if (lfs->hook)
730 if (FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, control,
731 MAKELONG(lRet,CD_LBSELCHANGE)))
732 return TRUE;
734 goto reset_scan;
736 return TRUE;
738 case cmb1: /* file type drop list */
739 if (notification == CBN_SELCHANGE)
741 *tmpstr = 0;
742 goto reset_scan;
744 return TRUE;
746 case chx1:
747 return TRUE;
748 case pshHelp:
749 return TRUE;
751 case cmb2: /* disk drop list */
752 FILEDLG_StripEditControl(hWnd);
753 lRet = SendDlgItemMessageW(hWnd, cmb2, CB_GETCURSEL, 0, 0L);
754 if (lRet == LB_ERR) return 0;
755 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
756 SendDlgItemMessageW(hWnd, cmb2, CB_GETLBTEXT, lRet,
757 (LPARAM)pstr);
758 wsprintfW(tmpstr, FILE_specc, pstr[2]);
759 HeapFree(GetProcessHeap(), 0, pstr);
760 if (notification != CBN_SELCHANGE) return TRUE;
762 /* on entry tmpstr points to the new path to scan */
763 reset_scan:
764 lRet = SendDlgItemMessageW(hWnd, cmb1, CB_GETCURSEL, 0, 0);
765 if (lRet == LB_ERR)
766 return TRUE;
767 pstr = (LPWSTR)SendDlgItemMessageW(hWnd, cmb1, CB_GETITEMDATA, lRet, 0);
768 TRACE("Selected filter : %s\n", debugstr_w(pstr));
769 SetDlgItemTextW( hWnd, edt1, pstr );
770 FILEDLG_ScanDir(hWnd, tmpstr);
771 in_update=TRUE;
773 case IDOK:
774 almost_ok:
776 ofn2 = *ofnW; /* for later restoring */
777 /* get current file name */
778 GetDlgItemTextW( hWnd, edt1, tmpstr, sizeof(tmpstr) );
779 pstr = strrchrW(tmpstr, '\\');
780 if (pstr == NULL)
781 pstr = strrchrW(tmpstr, ':');
783 if (strchrW(tmpstr,'*') != NULL || strchrW(tmpstr,'?') != NULL)
785 /* edit control contains wildcards */
786 if (pstr != NULL)
788 lstrcpynW(tmpstr2, pstr+1, BUFFILE);
789 *(pstr+1) = 0;
791 else
793 strcpyW(tmpstr2, tmpstr);
794 *tmpstr=0;
797 TRACE("tmpstr=%s, tmpstr2=%s\n", debugstr_w(tmpstr), debugstr_w(tmpstr2));
798 SetDlgItemTextW( hWnd, edt1, tmpstr2 );
799 FILEDLG_ScanDir(hWnd, tmpstr);
800 return TRUE;
802 /* no wildcards, we might have a directory or a filename */
803 /* try appending a wildcard and reading the directory */
804 pstr2 = tmpstr + lstrlenW(tmpstr);
805 if (pstr == NULL || *(pstr+1) != 0)
806 strcatW(tmpstr, FILE_bslash);
807 lRet = SendDlgItemMessageW(hWnd, cmb1, CB_GETCURSEL, 0, 0);
808 if (lRet == LB_ERR) return TRUE;
809 ofnW->nFilterIndex = lRet + 1;
810 TRACE("ofn->nFilterIndex=%ld\n", ofnW->nFilterIndex);
811 lstrcpynW(tmpstr2,
812 FILEDLG_GetFileType(ofnW->lpstrCustomFilter,
813 (LPWSTR) ofnW->lpstrFilter,
814 lRet), sizeof(tmpstr2));
815 SetDlgItemTextW( hWnd, edt1, tmpstr2 );
817 if (!in_update)
819 /* if ScanDir succeeds, we have changed the directory */
820 if (FILEDLG_ScanDir(hWnd, tmpstr))
821 return TRUE;
823 /* if not, this must be a filename */
824 *pstr2 = 0; /* remove the wildcard added before */
826 if (pstr != NULL)
828 /* strip off the pathname */
829 *pstr = 0;
830 SetDlgItemTextW( hWnd, edt1, pstr + 1 );
831 lstrcpynW(tmpstr2, pstr+1, sizeof(tmpstr2) );
832 /* Should we MessageBox() if this fails? */
833 if (!FILEDLG_ScanDir(hWnd, tmpstr)) return TRUE;
834 strcpyW(tmpstr, tmpstr2);
836 else SetDlgItemTextW( hWnd, edt1, tmpstr );
837 FILEDLG_UpdateResult(lfs, tmpstr, tmpstr2);
838 if(in_update)
840 if (lfs->hook)
841 FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, control,
842 MAKELONG(lRet,CD_LBSELCHANGE));
843 in_update = FALSE;
844 return TRUE;
846 FILEDLG_UpdateFileTitle(lfs);
847 if (lfs->hook)
849 lRet = (BOOL)FILEDLG_CallWindowProc(lfs, lfs->fileokstring,
850 0, lfs->lParam );
851 if (lRet)
853 *ofnW = ofn2; /* restore old state */
854 break;
857 if ((ofnW->Flags & OFN_ALLOWMULTISELECT) && (ofnW->Flags & OFN_EXPLORER)) {
858 if (ofnW->lpstrFile) {
859 LPWSTR str = (LPWSTR)ofnW->lpstrFile;
860 LPWSTR ptr = strrchrW(str, '\\');
861 str[lstrlenW(str) + 1] = '\0';
862 *ptr = 0;
865 EndDialog(hWnd, TRUE);
866 return TRUE;
867 case IDCANCEL:
868 EndDialog(hWnd, FALSE);
869 return TRUE;
870 case IDABORT: /* can be sent by the hook procedure */
871 EndDialog(hWnd, TRUE);
872 return TRUE;
874 return FALSE;
878 /***********************************************************************
879 * FILEDLG_MapDrawItemStruct [internal]
880 * map a 16 bits drawitem struct to 32
882 void FILEDLG_MapDrawItemStruct(LPDRAWITEMSTRUCT16 lpdis16, LPDRAWITEMSTRUCT lpdis)
884 lpdis->CtlType = lpdis16->CtlType;
885 lpdis->CtlID = lpdis16->CtlID;
886 lpdis->itemID = lpdis16->itemID;
887 lpdis->itemAction = lpdis16->itemAction;
888 lpdis->itemState = lpdis16->itemState;
889 lpdis->hwndItem = lpdis16->hwndItem;
890 lpdis->hDC = lpdis16->hDC;
891 lpdis->rcItem.right = lpdis16->rcItem.right;
892 lpdis->rcItem.left = lpdis16->rcItem.left;
893 lpdis->rcItem.top = lpdis16->rcItem.top;
894 lpdis->rcItem.bottom = lpdis16->rcItem.bottom;
895 lpdis->itemData = lpdis16->itemData;
898 /************************************************************************
899 * FILEDLG_MapStringPairsToW [internal]
900 * map string pairs to Unicode
902 LPWSTR FILEDLG_MapStringPairsToW(LPCSTR strA, UINT size)
904 LPCSTR s;
905 LPWSTR x, y;
906 int n;
908 s = strA;
909 while (*s)
910 s = s+strlen(s)+1;
911 s++;
912 n = s - strA;
913 if (n < size) n = size;
915 x = y = HeapAlloc(GetProcessHeap(),0, n * sizeof(WCHAR));
916 s = strA;
917 while (*s) {
918 lstrcpyAtoW(x, s);
919 x += lstrlenW(x)+1;
920 s += strlen(s)+1;
922 *x = 0;
923 return y;
927 /************************************************************************
928 * FILEDLG_DupToW [internal]
929 * duplicates an Ansi string to unicode, with a buffer size
931 LPWSTR FILEDLG_DupToW(LPCSTR str, UINT size)
933 LPWSTR strW;
934 if (str && (size > 0))
936 strW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
937 lstrcpynAtoW(strW, str, size);
938 return strW;
940 else
941 return NULL;
945 /************************************************************************
946 * FILEDLG_MapOfnStructA [internal]
947 * map a 32 bits Ansi structure to an Unicode one
949 void FILEDLG_MapOfnStructA(LPOPENFILENAMEA ofnA, LPOPENFILENAMEW ofnW, BOOL open)
951 LPCSTR str;
953 ofnW->lStructSize = sizeof(OPENFILENAMEW);
954 ofnW->hwndOwner = ofnA->hwndOwner;
955 ofnW->hInstance = ofnA->hInstance;
956 if (ofnA->lpstrFilter)
957 ofnW->lpstrFilter = FILEDLG_MapStringPairsToW(ofnA->lpstrFilter, 0);
958 else
959 ofnW->lpstrFilter = FILEDLG_MapStringPairsToW(defaultfilter, 0);
961 if ((ofnA->lpstrCustomFilter) && (*(ofnA->lpstrCustomFilter)))
962 ofnW->lpstrCustomFilter = FILEDLG_MapStringPairsToW(ofnA->lpstrCustomFilter, ofnA->nMaxCustFilter);
963 ofnW->nMaxCustFilter = ofnA->nMaxCustFilter;
964 ofnW->nFilterIndex = ofnA->nFilterIndex;
965 ofnW->lpstrFile = FILEDLG_DupToW(ofnA->lpstrFile, ofnA->nMaxFile);
966 ofnW->nMaxFile = ofnA->nMaxFile;
967 ofnW->lpstrFileTitle = FILEDLG_DupToW(ofnA->lpstrFileTitle, ofnA->nMaxFileTitle);
968 ofnW->nMaxFileTitle = ofnA->nMaxFileTitle;
969 if (ofnA->lpstrInitialDir)
970 ofnW->lpstrInitialDir = HEAP_strdupAtoW(GetProcessHeap(),0,ofnA->lpstrInitialDir);
971 if (ofnA->lpstrTitle)
972 str = ofnA->lpstrTitle;
973 else
974 /* Allocates default title (FIXME : get it from resource) */
975 str = open ? defaultopen:defaultsave;
976 ofnW->lpstrTitle = HEAP_strdupAtoW(GetProcessHeap(),0, str);
977 ofnW->Flags = ofnA->Flags;
978 ofnW->nFileOffset = ofnA->nFileOffset;
979 ofnW->nFileExtension = ofnA->nFileExtension;
980 ofnW->lpstrDefExt = FILEDLG_DupToW(ofnA->lpstrDefExt, 3);
981 if ((ofnA->Flags & OFN_ENABLETEMPLATE) && (ofnA->lpTemplateName))
983 if (HIWORD(ofnA->lpTemplateName))
984 ofnW->lpTemplateName = HEAP_strdupAtoW(GetProcessHeap(), 0, ofnA->lpTemplateName);
985 else /* numbered resource */
986 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 = ofn16->hwndOwner;
1002 ofnA.hInstance = ofn16->hInstance;
1003 if (ofn16->lpstrFilter)
1004 ofnA.lpstrFilter = PTR_SEG_TO_LIN(ofn16->lpstrFilter);
1005 if (ofn16->lpstrCustomFilter)
1006 ofnA.lpstrCustomFilter = PTR_SEG_TO_LIN(ofn16->lpstrCustomFilter);
1007 ofnA.nMaxCustFilter = ofn16->nMaxCustFilter;
1008 ofnA.nFilterIndex = ofn16->nFilterIndex;
1009 ofnA.lpstrFile = PTR_SEG_TO_LIN(ofn16->lpstrFile);
1010 ofnA.nMaxFile = ofn16->nMaxFile;
1011 ofnA.lpstrFileTitle = PTR_SEG_TO_LIN(ofn16->lpstrFileTitle);
1012 ofnA.nMaxFileTitle = ofn16->nMaxFileTitle;
1013 ofnA.lpstrInitialDir = PTR_SEG_TO_LIN(ofn16->lpstrInitialDir);
1014 ofnA.lpstrTitle = PTR_SEG_TO_LIN(ofn16->lpstrTitle);
1015 ofnA.Flags = ofn16->Flags;
1016 ofnA.nFileOffset = ofn16->nFileOffset;
1017 ofnA.nFileExtension = ofn16->nFileExtension;
1018 ofnA.lpstrDefExt = PTR_SEG_TO_LIN(ofn16->lpstrDefExt);
1019 if (HIWORD(ofn16->lpTemplateName))
1020 ofnA.lpTemplateName = PTR_SEG_TO_LIN(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);
1028 /************************************************************************
1029 * FILEDLG_DestroyPrivate [internal]
1030 * destroys the private object
1032 void FILEDLG_DestroyPrivate(LFSPRIVATE lfs)
1034 HWND hwnd;
1035 if (!lfs) return;
1036 hwnd = lfs->hwnd;
1037 /* free resources for a 16 bits dialog */
1038 if (lfs->hResource16) FreeResource16(lfs->hResource16);
1039 if (lfs->hGlobal16)
1041 GlobalUnlock16(lfs->hGlobal16);
1042 GlobalFree16(lfs->hGlobal16);
1044 /* if ofnW has been allocated, have to free everything in it */
1045 if (lfs->ofn16 || lfs->ofnA)
1047 LPOPENFILENAMEW ofnW = lfs->ofnW;
1048 if (ofnW->lpstrFilter) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrFilter);
1049 if (ofnW->lpstrCustomFilter) HeapFree(GetProcessHeap(), 0, ofnW->lpstrCustomFilter);
1050 if (ofnW->lpstrFile) HeapFree(GetProcessHeap(), 0, ofnW->lpstrFile);
1051 if (ofnW->lpstrFileTitle) HeapFree(GetProcessHeap(), 0, ofnW->lpstrFileTitle);
1052 if (ofnW->lpstrInitialDir) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrInitialDir);
1053 if (ofnW->lpstrTitle) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrTitle);
1054 if ((ofnW->lpTemplateName) && (HIWORD(ofnW->lpTemplateName)))
1055 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpTemplateName);
1056 HeapFree(GetProcessHeap(), 0, ofnW);
1058 TRACE("destroying private allocation %p\n", lfs);
1059 HeapFree(GetProcessHeap(), 0, lfs);
1060 RemovePropA(hwnd, OFN_PROP);
1063 /************************************************************************
1064 * FILEDLG_AllocPrivate [internal]
1065 * allocate a private object to hold 32 bits Unicode
1066 * structure that will be used throughtout the calls, while
1067 * keeping available the original structures and a few variables
1068 * On entry : type = dialog procedure type (16,32A,32W)
1069 * dlgType = dialog type (open or save)
1071 LFSPRIVATE FILEDLG_AllocPrivate(LPARAM lParam, int type, UINT dlgType)
1073 LFSPRIVATE lfs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct FSPRIVATE));
1074 LFSPRIVATE ret;
1075 TRACE("alloc private buf %p\n", lfs);
1076 if (!lfs) return NULL;
1077 lfs->hook = FALSE;
1078 lfs->lParam = lParam;
1079 if (dlgType == OPEN_DIALOG)
1080 lfs->open = TRUE;
1081 else
1082 lfs->open = FALSE;
1083 lfs->lbselchstring = RegisterWindowMessageA(LBSELCHSTRING);
1084 lfs->fileokstring = RegisterWindowMessageA(FILEOKSTRING);
1085 switch(type)
1087 case LFS16:
1088 lfs->ofn16 = (LPOPENFILENAME16) PTR_SEG_TO_LIN(lParam);
1089 if (lfs->ofn16->Flags & OFN_ENABLEHOOK)
1090 if (lfs->ofn16->lpfnHook)
1091 lfs->hook = TRUE;
1093 break;
1095 case LFS32A:
1096 lfs->ofnA = (LPOPENFILENAMEA) lParam;
1097 if (lfs->ofnA->Flags & OFN_ENABLEHOOK)
1098 if (lfs->ofnA->lpfnHook)
1099 lfs->hook = TRUE;
1100 break;
1102 case LFS32W:
1103 lfs->ofnW = (LPOPENFILENAMEW) lParam;
1104 if (lfs->ofnW->Flags & OFN_ENABLEHOOK)
1105 if (lfs->ofnW->lpfnHook)
1106 lfs->hook = TRUE;
1107 break;
1109 ret = lfs;
1110 if (!lfs->ofnW)
1111 { /* this structure is needed internally, so create it */
1112 lfs->ofnW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(OPENFILENAMEW));
1113 if (lfs->ofnW)
1115 if (lfs->ofn16)
1116 FILEDLG_MapOfnStruct16(lfs->ofn16, lfs->ofnW, lfs->open);
1117 if (lfs->ofnA)
1118 FILEDLG_MapOfnStructA(lfs->ofnA, lfs->ofnW, lfs->open);
1120 else
1121 ret = NULL;
1123 if (lfs->ofn16)
1125 if (!Get16BitsTemplate(lfs)) ret = NULL;
1127 else
1128 if (!Get32BitsTemplate(lfs)) ret = NULL;
1129 if (!ret) FILEDLG_DestroyPrivate(lfs);
1130 return ret;
1134 /***********************************************************************
1135 * GetFileName31A [internal]
1137 * Creates a win31 style dialog box for the user to select a file to open/save.
1139 BOOL WINAPI GetFileName31A(
1140 LPOPENFILENAMEA lpofn, /* addess of structure with data*/
1141 UINT dlgType /* type dialogue : open/save */
1144 HINSTANCE hInst;
1145 BOOL bRet = FALSE;
1146 LFSPRIVATE lfs;
1148 if (!lpofn || !FileDlg_Init()) return FALSE;
1150 lfs = FILEDLG_AllocPrivate((LPARAM) lpofn, LFS32A, dlgType);
1151 if (lfs)
1153 hInst = GetWindowLongA( lpofn->hwndOwner, GWL_HINSTANCE );
1154 bRet = DialogBoxIndirectParamA( hInst, lfs->template, lpofn->hwndOwner,
1155 (DLGPROC) FileOpenDlgProc, (DWORD) lfs);
1156 FILEDLG_DestroyPrivate(lfs);
1159 TRACE("return lpstrFile='%s' !\n", lpofn->lpstrFile);
1160 return bRet;
1164 /***********************************************************************
1165 * GetFileName31W [internal]
1167 * Creates a win31 style dialog box for the user to select a file to open/save
1169 BOOL WINAPI GetFileName31W(
1170 LPOPENFILENAMEW lpofn, /* addess of structure with data*/
1171 UINT dlgType /* type dialogue : open/save */
1174 HINSTANCE hInst;
1175 BOOL bRet = FALSE;
1176 LFSPRIVATE lfs;
1178 if (!lpofn || !FileDlg_Init()) return FALSE;
1180 lfs = FILEDLG_AllocPrivate((LPARAM) lpofn, LFS32W, dlgType);
1181 if (lfs)
1183 hInst = GetWindowLongA( lpofn->hwndOwner, GWL_HINSTANCE );
1184 bRet = DialogBoxIndirectParamW( hInst, lfs->template, lpofn->hwndOwner,
1185 (DLGPROC) FileOpenDlgProc, (DWORD) lfs);
1186 FILEDLG_DestroyPrivate(lfs);
1189 TRACE("return lpstrFile='%s' !\n", debugstr_w(lpofn->lpstrFile));
1190 return bRet;
1194 /* ------------------ Dialog procedures ---------------------- */
1196 /***********************************************************************
1197 * FileOpenDlgProc16 (COMMDLG.6)
1199 LRESULT WINAPI FileOpenDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
1200 LPARAM lParam)
1202 LFSPRIVATE lfs = (LFSPRIVATE)GetPropA(hWnd,OFN_PROP);
1203 DRAWITEMSTRUCT dis;
1205 TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
1206 if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
1208 LRESULT lRet = (BOOL16)FILEDLG_CallWindowProc(lfs, wMsg, wParam, lParam);
1209 if (lRet)
1210 return lRet; /* else continue message processing */
1212 switch (wMsg)
1214 case WM_INITDIALOG:
1215 return FILEDLG_WMInitDialog(hWnd, wParam, lParam);
1217 case WM_MEASUREITEM:
1218 return FILEDLG_WMMeasureItem16(hWnd, wParam, lParam);
1220 case WM_DRAWITEM:
1221 FILEDLG_MapDrawItemStruct((LPDRAWITEMSTRUCT16)PTR_SEG_TO_LIN(lParam), &dis);
1222 return FILEDLG_WMDrawItem(hWnd, wParam, lParam, FALSE, &dis);
1224 case WM_COMMAND:
1225 return FILEDLG_WMCommand(hWnd, lParam, HIWORD(lParam),wParam, lfs);
1226 #if 0
1227 case WM_CTLCOLOR:
1228 SetBkColor((HDC16)wParam, 0x00C0C0C0);
1229 switch (HIWORD(lParam))
1231 case CTLCOLOR_BTN:
1232 SetTextColor((HDC16)wParam, 0x00000000);
1233 return hGRAYBrush;
1234 case CTLCOLOR_STATIC:
1235 SetTextColor((HDC16)wParam, 0x00000000);
1236 return hGRAYBrush;
1238 break;
1239 #endif
1241 return FALSE;
1244 /***********************************************************************
1245 * FileSaveDlgProc16 (COMMDLG.7)
1247 LRESULT WINAPI FileSaveDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
1248 LPARAM lParam)
1250 LFSPRIVATE lfs = (LFSPRIVATE)GetPropA(hWnd,OFN_PROP);
1251 DRAWITEMSTRUCT dis;
1253 TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
1254 if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
1256 LRESULT lRet;
1257 lRet = (BOOL16)FILEDLG_CallWindowProc(lfs, wMsg, wParam, lParam);
1258 if (lRet)
1259 return lRet; /* else continue message processing */
1261 switch (wMsg) {
1262 case WM_INITDIALOG:
1263 return FILEDLG_WMInitDialog(hWnd, wParam, lParam);
1265 case WM_MEASUREITEM:
1266 return FILEDLG_WMMeasureItem16(hWnd, wParam, lParam);
1268 case WM_DRAWITEM:
1269 FILEDLG_MapDrawItemStruct((LPDRAWITEMSTRUCT16)PTR_SEG_TO_LIN(lParam), &dis);
1270 return FILEDLG_WMDrawItem(hWnd, wParam, lParam, TRUE, &dis);
1272 case WM_COMMAND:
1273 return FILEDLG_WMCommand(hWnd, lParam, HIWORD(lParam), wParam, lfs);
1277 case WM_CTLCOLOR:
1278 SetBkColor((HDC16)wParam, 0x00C0C0C0);
1279 switch (HIWORD(lParam))
1281 case CTLCOLOR_BTN:
1282 SetTextColor((HDC16)wParam, 0x00000000);
1283 return hGRAYBrush;
1284 case CTLCOLOR_STATIC:
1285 SetTextColor((HDC16)wParam, 0x00000000);
1286 return hGRAYBrush;
1288 return FALSE;
1291 return FALSE;
1294 /***********************************************************************
1295 * FileOpenDlgProc [internal]
1296 * Used for open and save, in fact.
1298 static LRESULT WINAPI FileOpenDlgProc(HWND hWnd, UINT wMsg,
1299 WPARAM wParam, LPARAM lParam)
1301 LFSPRIVATE lfs = (LFSPRIVATE)GetPropA(hWnd,OFN_PROP);
1303 TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
1304 if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
1306 LRESULT lRet;
1307 lRet = (BOOL)FILEDLG_CallWindowProc(lfs, wMsg, wParam, lParam);
1308 if (lRet)
1309 return lRet; /* else continue message processing */
1311 switch (wMsg)
1313 case WM_INITDIALOG:
1314 return FILEDLG_WMInitDialog(hWnd, wParam, lParam);
1316 case WM_MEASUREITEM:
1317 return FILEDLG_WMMeasureItem(hWnd, wParam, lParam);
1319 case WM_DRAWITEM:
1320 return FILEDLG_WMDrawItem(hWnd, wParam, lParam, !lfs->open, (DRAWITEMSTRUCT *)lParam);
1322 case WM_COMMAND:
1323 return FILEDLG_WMCommand(hWnd, lParam, HIWORD(wParam), LOWORD(wParam), lfs);
1324 #if 0
1325 case WM_CTLCOLOR:
1326 SetBkColor((HDC16)wParam, 0x00C0C0C0);
1327 switch (HIWORD(lParam))
1329 case CTLCOLOR_BTN:
1330 SetTextColor((HDC16)wParam, 0x00000000);
1331 return hGRAYBrush;
1332 case CTLCOLOR_STATIC:
1333 SetTextColor((HDC16)wParam, 0x00000000);
1334 return hGRAYBrush;
1336 break;
1337 #endif
1339 return FALSE;
1342 /* ------------------ APIs ---------------------- */
1344 /***********************************************************************
1345 * GetOpenFileName16 (COMMDLG.1)
1347 * Creates a dialog box for the user to select a file to open.
1349 * RETURNS
1350 * TRUE on success: user selected a valid file
1351 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1353 * BUGS
1354 * unknown, there are some FIXME's left.
1356 BOOL16 WINAPI GetOpenFileName16(
1357 SEGPTR ofn /* addess of structure with data*/
1360 HINSTANCE hInst;
1361 BOOL bRet = FALSE;
1362 LPOPENFILENAME16 lpofn = (LPOPENFILENAME16)PTR_SEG_TO_LIN(ofn);
1363 LFSPRIVATE lfs;
1364 FARPROC16 ptr;
1366 if (!lpofn || !FileDlg_Init()) return FALSE;
1368 lfs = FILEDLG_AllocPrivate((LPARAM) ofn, LFS16, OPEN_DIALOG);
1369 if (lfs)
1371 hInst = GetWindowLongA( lpofn->hwndOwner, GWL_HINSTANCE );
1372 ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (SEGPTR) 6);
1373 bRet = DialogBoxIndirectParam16( hInst, lfs->hDlgTmpl16, lpofn->hwndOwner,
1374 (DLGPROC16) ptr, (DWORD) lfs);
1375 FILEDLG_DestroyPrivate(lfs);
1378 TRACE("return lpstrFile='%s' !\n",
1379 (LPSTR)PTR_SEG_TO_LIN(lpofn->lpstrFile));
1380 return bRet;
1383 /***********************************************************************
1384 * GetSaveFileName16 (COMMDLG.2)
1386 * Creates a dialog box for the user to select a file to save.
1388 * RETURNS
1389 * TRUE on success: user enters a valid file
1390 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1392 * BUGS
1393 * unknown. There are some FIXME's left.
1395 BOOL16 WINAPI GetSaveFileName16(
1396 SEGPTR ofn /* addess of structure with data*/
1399 HINSTANCE hInst;
1400 BOOL bRet = FALSE;
1401 LPOPENFILENAME16 lpofn = (LPOPENFILENAME16)PTR_SEG_TO_LIN(ofn);
1402 LFSPRIVATE lfs;
1403 FARPROC16 ptr;
1405 if (!lpofn || !FileDlg_Init()) return FALSE;
1407 lfs = FILEDLG_AllocPrivate((LPARAM) ofn, LFS16, SAVE_DIALOG);
1408 if (lfs)
1410 hInst = GetWindowLongA( lpofn->hwndOwner, GWL_HINSTANCE );
1411 ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (SEGPTR) 7);
1412 bRet = DialogBoxIndirectParam16( hInst, lfs->hDlgTmpl16, lpofn->hwndOwner,
1413 (DLGPROC16) ptr, (DWORD) lfs);
1414 FILEDLG_DestroyPrivate(lfs);
1417 TRACE("return lpstrFile='%s' !\n",
1418 (LPSTR)PTR_SEG_TO_LIN(lpofn->lpstrFile));
1419 return bRet;
1422 /***********************************************************************
1423 * GetOpenFileNameA (COMDLG32.10)
1425 * Creates a dialog box for the user to select a file to open.
1427 * RETURNS
1428 * TRUE on success: user enters a valid file
1429 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1432 BOOL WINAPI GetOpenFileNameA(
1433 LPOPENFILENAMEA ofn) /* address of init structure */
1435 BOOL newlook;
1437 /* some flags don't allow to match the TWEAK_WineLook */
1438 if (ofn->Flags & (OFN_ALLOWMULTISELECT|OFN_ENABLEHOOK|OFN_ENABLETEMPLATE))
1440 newlook = (ofn->Flags & OFN_EXPLORER) ? TRUE : FALSE;
1442 else
1444 /* no special flags set, we can match the TWEAK_WineLook */
1445 newlook = (TWEAK_WineLook>WIN31_LOOK) ? TRUE : FALSE;
1448 if (newlook)
1450 return GetFileDialog95A(ofn, OPEN_DIALOG);
1452 else
1454 return GetFileName31A(ofn, OPEN_DIALOG);
1458 /***********************************************************************
1459 * GetOpenFileNameW (COMDLG32.11)
1461 * Creates a dialog box for the user to select a file to open.
1463 * RETURNS
1464 * TRUE on success: user enters a valid file
1465 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1468 BOOL WINAPI GetOpenFileNameW(
1469 LPOPENFILENAMEW ofn) /* address of init structure */
1471 BOOL newlook;
1473 /* some flags don't allow to match the TWEAK_WineLook */
1474 if (ofn->Flags & (OFN_ALLOWMULTISELECT|OFN_ENABLEHOOK|OFN_ENABLETEMPLATE))
1476 newlook = (ofn->Flags & OFN_EXPLORER) ? TRUE : FALSE;
1478 else
1480 /* no special flags set, we can match the TWEAK_WineLook */
1481 newlook = (TWEAK_WineLook>WIN31_LOOK) ? TRUE : FALSE;
1484 if (newlook)
1486 return GetFileDialog95W(ofn, OPEN_DIALOG);
1488 else
1490 return GetFileName31W(ofn, OPEN_DIALOG);
1494 /***********************************************************************
1495 * GetSaveFileNameA (COMDLG32.12)
1497 * Creates a dialog box for the user to select a file to save.
1499 * RETURNS
1500 * TRUE on success: user enters a valid file
1501 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1504 BOOL WINAPI GetSaveFileNameA(
1505 LPOPENFILENAMEA ofn) /* address of init structure */
1507 BOOL newlook;
1509 /* some flags don't allow to match the TWEAK_WineLook */
1510 if (ofn->Flags & (OFN_ALLOWMULTISELECT|OFN_ENABLEHOOK|OFN_ENABLETEMPLATE))
1512 newlook = (ofn->Flags & OFN_EXPLORER) ? TRUE : FALSE;
1514 else
1516 /* no special flags set, we can match the TWEAK_WineLook */
1517 newlook = (TWEAK_WineLook>WIN31_LOOK) ? TRUE : FALSE;
1520 if (newlook)
1522 return GetFileDialog95A(ofn, SAVE_DIALOG);
1524 else
1526 return GetFileName31A(ofn, SAVE_DIALOG);
1530 /***********************************************************************
1531 * GetSaveFileNameW (COMDLG32.13)
1533 * Creates a dialog box for the user to select a file to save.
1535 * RETURNS
1536 * TRUE on success: user enters a valid file
1537 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1540 BOOL WINAPI GetSaveFileNameW(
1541 LPOPENFILENAMEW ofn) /* address of init structure */
1543 BOOL newlook;
1545 /* some flags don't allow to match the TWEAK_WineLook */
1546 if (ofn->Flags & (OFN_ALLOWMULTISELECT|OFN_ENABLEHOOK|OFN_ENABLETEMPLATE))
1548 newlook = (ofn->Flags & OFN_EXPLORER) ? TRUE : FALSE;
1550 else
1552 /* no special flags set, we can match the TWEAK_WineLook */
1553 newlook = (TWEAK_WineLook>WIN31_LOOK) ? TRUE : FALSE;
1556 if (newlook)
1558 return GetFileDialog95W(ofn, SAVE_DIALOG);
1560 else
1562 return GetFileName31W(ofn, SAVE_DIALOG);