Add missing '\n's to debug traces.
[wine/hacks.git] / dlls / commdlg / filedlg16.c
blob063a9c0281b5121150f57ec607b3e026b9051a3f
1 /*
2 * COMMDLG - File Dialogs
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1996 Albrecht Kleine
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "wine/winbase16.h"
32 #include "wine/winuser16.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
35 #include "cderr.h"
36 #include "winreg.h"
37 #include "winternl.h"
38 #include "winuser.h"
39 #include "commdlg.h"
40 #include "cderr.h"
41 #include "winreg.h"
42 #include "winternl.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
46 #include "cdlg.h"
47 #include "cdlg16.h"
49 #define BUFFILE 512
50 #define BUFFILEALLOC 512 * sizeof(WCHAR)
52 struct FSPRIVATE
54 HWND hwnd; /* file dialog window handle */
55 BOOL hook; /* TRUE if the dialog is hooked */
56 UINT lbselchstring; /* registered message id */
57 UINT fileokstring; /* registered message id */
58 LPARAM lParam; /* save original lparam */
59 HANDLE16 hDlgTmpl16; /* handle for resource 16 */
60 HANDLE16 hResource16; /* handle for allocated resource 16 */
61 HANDLE16 hGlobal16; /* 16 bits mem block (resources) */
62 LPCVOID template; /* template for 32 bits resource */
63 BOOL open; /* TRUE if open dialog, FALSE if save dialog */
64 OPENFILENAMEW *ofnW; /* original structure or work struct */
65 OPENFILENAMEA *ofnA; /* original structure if 32bits ansi dialog */
66 OPENFILENAME16 *ofn16; /* original structure if 16 bits dialog */
69 #define LFSPRIVATE struct FSPRIVATE *
70 #define LFS16 1
71 #define LFS32A 2
72 #define LFS32W 3
73 #define OFN_PROP "FILEDLG_OFN"
75 static const WCHAR FILE_star[] = {'*','.','*', 0};
76 static const WCHAR FILE_bslash[] = {'\\', 0};
77 static const WCHAR FILE_specc[] = {'%','c',':', 0};
78 static const int fldrHeight = 16;
79 static const int fldrWidth = 20;
81 static HICON hFolder = 0;
82 static HICON hFolder2 = 0;
83 static HICON hFloppy = 0;
84 static HICON hHDisk = 0;
85 static HICON hCDRom = 0;
86 static HICON hNet = 0;
87 static char defaultopen[]="Open File";
88 static char defaultsave[]="Save as";
90 /***********************************************************************
91 * FileDlg_Init [internal]
93 static BOOL FileDlg_Init(void)
95 static BOOL initialized = 0;
97 if (!initialized) {
98 HINSTANCE inst = GetModuleHandleA( "comdlg32.dll" );
99 if (!inst)
101 ERR( "cannot get comdlg32.dll instance\n" );
102 return FALSE;
104 hFolder = LoadImageA( inst, "FOLDER", IMAGE_ICON, 16, 16, LR_SHARED );
105 hFolder2 = LoadImageA( inst, "FOLDER2", IMAGE_ICON, 16, 16, LR_SHARED );
106 hFloppy = LoadImageA( inst, "FLOPPY", IMAGE_ICON, 16, 16, LR_SHARED );
107 hHDisk = LoadImageA( inst, "HDISK", IMAGE_ICON, 16, 16, LR_SHARED );
108 hCDRom = LoadImageA( inst, "CDROM", IMAGE_ICON, 16, 16, LR_SHARED );
109 hNet = LoadImageA( inst, "NETWORK", IMAGE_ICON, 16, 16, LR_SHARED );
110 if (hFolder == 0 || hFolder2 == 0 || hFloppy == 0 ||
111 hHDisk == 0 || hCDRom == 0 || hNet == 0)
113 ERR("Error loading icons !\n");
114 return FALSE;
116 initialized = TRUE;
118 return TRUE;
121 /***********************************************************************
122 * Get32BitsTemplate [internal]
124 * Get a template (or FALSE if failure) when 16 bits dialogs are used
125 * by a 32 bits application
128 BOOL Get32BitsTemplate(LFSPRIVATE lfs)
130 LPOPENFILENAMEW ofnW = lfs->ofnW;
131 HANDLE hDlgTmpl;
133 if (ofnW->Flags & OFN_ENABLETEMPLATEHANDLE)
135 if (!(lfs->template = LockResource( ofnW->hInstance )))
137 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
138 return FALSE;
141 else if (ofnW->Flags & OFN_ENABLETEMPLATE)
143 HRSRC hResInfo;
144 if (lfs->ofnA)
145 hResInfo = FindResourceA(lfs->ofnA->hInstance,
146 lfs->ofnA->lpTemplateName,
147 (LPSTR)RT_DIALOG);
148 else
149 hResInfo = FindResourceW(ofnW->hInstance,
150 ofnW->lpTemplateName,
151 (LPWSTR)RT_DIALOG);
152 if (!hResInfo)
154 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
155 return FALSE;
157 if (!(hDlgTmpl = LoadResource(ofnW->hInstance,
158 hResInfo)) ||
159 !(lfs->template = LockResource(hDlgTmpl)))
161 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
162 return FALSE;
164 } else { /* get it from internal Wine resource */
165 HRSRC hResInfo;
166 if (!(hResInfo = FindResourceA(COMDLG32_hInstance,
167 lfs->open? "OPEN_FILE":"SAVE_FILE", (LPSTR)RT_DIALOG)))
169 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
170 return FALSE;
172 if (!(hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo )) ||
173 !(lfs->template = LockResource( hDlgTmpl )))
175 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
176 return FALSE;
179 return TRUE;
182 /***********************************************************************
183 * Get16BitsTemplate [internal]
185 * Get a template (FALSE if failure) when 16 bits dialogs are used
186 * by a 16 bits application
189 BOOL Get16BitsTemplate(LFSPRIVATE lfs)
191 LPOPENFILENAME16 ofn16 = lfs->ofn16;
192 LPCVOID template;
193 HGLOBAL16 hGlobal16 = 0;
195 if (ofn16->Flags & OFN_ENABLETEMPLATEHANDLE)
196 lfs->hDlgTmpl16 = ofn16->hInstance;
197 else if (ofn16->Flags & OFN_ENABLETEMPLATE)
199 HANDLE16 hResInfo;
200 if (!(hResInfo = FindResource16(ofn16->hInstance,
201 MapSL(ofn16->lpTemplateName),
202 (LPSTR)RT_DIALOG)))
204 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
205 return FALSE;
207 if (!(lfs->hDlgTmpl16 = LoadResource16( ofn16->hInstance, hResInfo )))
209 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
210 return FALSE;
212 lfs->hResource16 = lfs->hDlgTmpl16;
214 else
215 { /* get resource from (32 bits) own Wine resource; convert it to 16 */
216 HRSRC hResInfo;
217 HGLOBAL hDlgTmpl32;
218 LPCVOID template32;
219 DWORD size;
221 if (!(hResInfo = FindResourceA(COMDLG32_hInstance,
222 lfs->open ? "OPEN_FILE":"SAVE_FILE", (LPSTR)RT_DIALOG)))
224 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
225 return FALSE;
227 if (!(hDlgTmpl32 = LoadResource(COMDLG32_hInstance, hResInfo )) ||
228 !(template32 = LockResource( hDlgTmpl32 )))
230 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
231 return FALSE;
233 size = SizeofResource(GetModuleHandleA("COMDLG32"), hResInfo);
234 hGlobal16 = GlobalAlloc16(0, size);
235 if (!hGlobal16)
237 COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
238 ERR("alloc failure for %ld bytes\n", size);
239 return FALSE;
241 template = GlobalLock16(hGlobal16);
242 if (!template)
244 COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE);
245 ERR("global lock failure for %x handle\n", hGlobal16);
246 GlobalFree16(hGlobal16);
247 return FALSE;
249 ConvertDialog32To16((LPVOID)template32, size, (LPVOID)template);
250 lfs->hDlgTmpl16 = hGlobal16;
251 lfs->hGlobal16 = hGlobal16;
253 return TRUE;
256 /***********************************************************************
257 * FILEDLG_StripEditControl [internal]
258 * Strip pathnames off the contents of the edit control.
260 static void FILEDLG_StripEditControl(HWND hwnd)
262 WCHAR temp[BUFFILE], *cp;
264 GetDlgItemTextW( hwnd, edt1, temp, sizeof(temp)/sizeof(WCHAR));
265 cp = strrchrW(temp, '\\');
266 if (cp != NULL) {
267 strcpyW(temp, cp+1);
269 cp = strrchrW(temp, ':');
270 if (cp != NULL) {
271 strcpyW(temp, cp+1);
273 /* FIXME: shouldn't we do something with the result here? ;-) */
276 /***********************************************************************
277 * FILEDLG_CallWindowProc [internal]
279 * Call the appropriate hook
281 static BOOL FILEDLG_CallWindowProc(LFSPRIVATE lfs, UINT wMsg, WPARAM wParam,
282 LPARAM lParam)
284 if (lfs->ofnA)
286 return (BOOL) CallWindowProcA(
287 (WNDPROC)lfs->ofnA->lpfnHook, lfs->hwnd,
288 wMsg, wParam, lParam);
291 if (lfs->ofnW)
293 return (BOOL) CallWindowProcW(
294 (WNDPROC)lfs->ofnW->lpfnHook, lfs->hwnd,
295 wMsg, wParam, lParam);
297 return FALSE;
300 /***********************************************************************
301 * FILEDLG_ScanDir [internal]
303 static BOOL FILEDLG_ScanDir(HWND hWnd, LPWSTR newPath)
305 WCHAR buffer[BUFFILE];
306 HWND hdlg, hdlgDir;
307 LRESULT lRet = TRUE;
308 HCURSOR hCursorWait, oldCursor;
310 TRACE("Trying to change to %s\n", debugstr_w(newPath));
311 if ( !SetCurrentDirectoryW( newPath ))
312 return FALSE;
313 lstrcpynW(buffer, newPath, sizeof(buffer)/sizeof(WCHAR));
315 /* get the list of spec files */
316 GetDlgItemTextW(hWnd, edt1, buffer, sizeof(buffer)/sizeof(WCHAR));
318 hCursorWait = LoadCursorA(0, (LPSTR)IDC_WAIT);
319 oldCursor = SetCursor(hCursorWait);
321 /* list of files */
322 if ((hdlg = GetDlgItem(hWnd, lst1)) != 0) {
323 WCHAR* scptr; /* ptr on semi-colon */
324 WCHAR* filter = buffer;
326 TRACE("Using filter %s\n", debugstr_w(filter));
327 SendMessageW(hdlg, LB_RESETCONTENT, 0, 0);
328 while (filter) {
329 scptr = strchrW(filter, ';');
330 if (scptr) *scptr = 0;
331 while (*filter == ' ') filter++;
332 TRACE("Using file spec %s\n", debugstr_w(filter));
333 if (SendMessageW(hdlg, LB_DIR, 0, (LPARAM)filter) == LB_ERR)
334 return FALSE;
335 if (scptr) *scptr = ';';
336 filter = (scptr) ? (scptr + 1) : 0;
340 /* list of directories */
341 strcpyW(buffer, FILE_star);
343 if ((hdlgDir = GetDlgItem(hWnd, lst2)) != 0) {
344 lRet = DlgDirListW(hWnd, buffer, lst2, stc1, DDL_EXCLUSIVE | DDL_DIRECTORY);
346 SetCursor(oldCursor);
347 return lRet;
350 /***********************************************************************
351 * FILEDLG_GetFileType [internal]
354 static LPWSTR FILEDLG_GetFileType(LPWSTR cfptr, LPWSTR fptr, WORD index)
356 int n, i;
357 i = 0;
358 if (cfptr)
359 for ( ;(n = lstrlenW(cfptr)) != 0; i++)
361 cfptr += n + 1;
362 if (i == index)
363 return cfptr;
364 cfptr += lstrlenW(cfptr) + 1;
366 if (fptr)
367 for ( ;(n = lstrlenW(fptr)) != 0; i++)
369 fptr += n + 1;
370 if (i == index)
371 return fptr;
372 fptr += lstrlenW(fptr) + 1;
374 return (LPWSTR) FILE_star; /* FIXME */
377 /***********************************************************************
378 * FILEDLG_WMDrawItem [internal]
380 static LONG FILEDLG_WMDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam,
381 int savedlg, LPDRAWITEMSTRUCT lpdis)
383 WCHAR *str;
384 HICON hIcon;
385 COLORREF oldText = 0, oldBk = 0;
387 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst1)
389 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC))) return FALSE;
390 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
391 (LPARAM)str);
393 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
395 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
396 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
398 if (savedlg)
399 SetTextColor(lpdis->hDC,GetSysColor(COLOR_GRAYTEXT) );
401 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + 1,
402 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
403 &(lpdis->rcItem), str, lstrlenW(str), NULL);
405 if (lpdis->itemState & ODS_SELECTED)
406 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
408 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
410 SetBkColor( lpdis->hDC, oldBk );
411 SetTextColor( lpdis->hDC, oldText );
413 HeapFree(GetProcessHeap(), 0, str);
414 return TRUE;
417 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst2)
419 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
420 return FALSE;
421 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
422 (LPARAM)str);
424 if (lpdis->itemState & ODS_SELECTED)
426 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
427 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
429 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
430 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
431 &(lpdis->rcItem), str, lstrlenW(str), NULL);
433 if (lpdis->itemState & ODS_SELECTED)
434 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
436 if (lpdis->itemState & ODS_SELECTED)
438 SetBkColor( lpdis->hDC, oldBk );
439 SetTextColor( lpdis->hDC, oldText );
441 DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hFolder);
442 HeapFree(GetProcessHeap(), 0, str);
443 return TRUE;
445 if (lpdis->CtlType == ODT_COMBOBOX && lpdis->CtlID == cmb2)
447 char root[] = "a:";
448 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
449 return FALSE;
450 SendMessageW(lpdis->hwndItem, CB_GETLBTEXT, lpdis->itemID,
451 (LPARAM)str);
452 root[0] += str[2] - 'a';
453 switch(GetDriveTypeA(root))
455 case DRIVE_REMOVABLE: hIcon = hFloppy; break;
456 case DRIVE_CDROM: hIcon = hCDRom; break;
457 case DRIVE_REMOTE: hIcon = hNet; break;
458 case DRIVE_FIXED:
459 default: hIcon = hHDisk; break;
461 if (lpdis->itemState & ODS_SELECTED)
463 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
464 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
466 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
467 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
468 &(lpdis->rcItem), str, lstrlenW(str), NULL);
470 if (lpdis->itemState & ODS_SELECTED)
472 SetBkColor( lpdis->hDC, oldBk );
473 SetTextColor( lpdis->hDC, oldText );
475 DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hIcon);
476 HeapFree(GetProcessHeap(), 0, str);
477 return TRUE;
479 return FALSE;
482 /***********************************************************************
483 * FILEDLG_UpdateResult [internal]
484 * update the displayed file name (with path)
486 void FILEDLG_UpdateResult(LFSPRIVATE lfs, WCHAR *tmpstr)
488 int lenstr2;
489 LPOPENFILENAMEW ofnW = lfs->ofnW;
490 WCHAR tmpstr2[BUFFILE];
491 WCHAR *bs;
493 TRACE("%s\n", debugstr_w(tmpstr));
494 if(ofnW->Flags & OFN_NOVALIDATE)
495 tmpstr2[0] = '\0';
496 else
497 GetCurrentDirectoryW(BUFFILE, tmpstr2);
498 lenstr2 = strlenW(tmpstr2);
499 if (lenstr2 > 3)
500 tmpstr2[lenstr2++]='\\';
501 lstrcpynW(tmpstr2+lenstr2, tmpstr, BUFFILE-lenstr2);
502 if (ofnW->lpstrFile)
503 lstrcpynW(ofnW->lpstrFile, tmpstr2, ofnW->nMaxFile);
504 if((bs = strrchrW(tmpstr2, '\\')) != NULL)
505 ofnW->nFileOffset = bs - tmpstr2 +1;
506 else
507 ofnW->nFileOffset = 0;
508 ofnW->nFileExtension = 0;
509 while(tmpstr2[ofnW->nFileExtension] != '.' && tmpstr2[ofnW->nFileExtension] != '\0')
510 ofnW->nFileExtension++;
511 if (tmpstr2[ofnW->nFileExtension] == '\0')
512 ofnW->nFileExtension = 0;
513 else
514 ofnW->nFileExtension++;
515 /* update the real client structures if any */
516 if (lfs->ofn16)
517 { /* we have to convert to short (8.3) path */
518 char tmp[1024]; /* MAX_PATHNAME_LEN */
519 LPOPENFILENAME16 ofn16 = lfs->ofn16;
520 char *dest = MapSL(ofn16->lpstrFile);
521 char *bs16;
522 if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFile, -1,
523 tmp, sizeof(tmp), NULL, NULL ))
524 tmp[sizeof(tmp)-1] = 0;
525 GetShortPathNameA(tmp, dest, ofn16->nMaxFile);
527 /* the same procedure as every year... */
528 if((bs16 = strrchr(dest, '\\')) != NULL)
529 ofn16->nFileOffset = bs16 - dest +1;
530 else
531 ofn16->nFileOffset = 0;
532 ofn16->nFileExtension = 0;
533 while(dest[ofn16->nFileExtension] != '.' && dest[ofn16->nFileExtension] != '\0')
534 ofn16->nFileExtension++;
535 if (dest[ofn16->nFileExtension] == '\0')
536 ofn16->nFileExtension = 0;
537 else
538 ofn16->nFileExtension++;
540 if (lfs->ofnA)
542 if (ofnW->nMaxFile &&
543 !WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFile, -1,
544 lfs->ofnA->lpstrFile, ofnW->nMaxFile, NULL, NULL ))
545 lfs->ofnA->lpstrFile[ofnW->nMaxFile-1] = 0;
546 lfs->ofnA->nFileOffset = ofnW->nFileOffset;
547 lfs->ofnA->nFileExtension = ofnW->nFileExtension;
551 /***********************************************************************
552 * FILEDLG_UpdateFileTitle [internal]
553 * update the displayed file name (without path)
555 void FILEDLG_UpdateFileTitle(LFSPRIVATE lfs)
557 LONG lRet;
558 LPOPENFILENAMEW ofnW = lfs->ofnW;
559 if (ofnW->lpstrFileTitle != NULL)
561 lRet = SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
562 SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETTEXT, lRet,
563 (LPARAM)ofnW->lpstrFileTitle );
564 if (lfs->ofn16)
566 char *dest = MapSL(lfs->ofn16->lpstrFileTitle);
567 if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFileTitle, -1,
568 dest, ofnW->nMaxFileTitle, NULL, NULL ))
569 dest[ofnW->nMaxFileTitle-1] = 0;
571 if (lfs->ofnA)
573 if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFileTitle, -1,
574 lfs->ofnA->lpstrFileTitle, ofnW->nMaxFileTitle, NULL, NULL ))
575 lfs->ofnA->lpstrFileTitle[ofnW->nMaxFileTitle-1] = 0;
580 /***********************************************************************
581 * FILEDLG_DirListDblClick [internal]
583 static LRESULT FILEDLG_DirListDblClick( LFSPRIVATE lfs )
585 LONG lRet;
586 HWND hWnd = lfs->hwnd;
587 LPWSTR pstr;
588 WCHAR tmpstr[BUFFILE];
590 /* get the raw string (with brackets) */
591 lRet = SendDlgItemMessageW(hWnd, lst2, LB_GETCURSEL, 0, 0);
592 if (lRet == LB_ERR) return TRUE;
593 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
594 SendDlgItemMessageW(hWnd, lst2, LB_GETTEXT, lRet,
595 (LPARAM)pstr);
596 strcpyW( tmpstr, pstr );
597 HeapFree(GetProcessHeap(), 0, pstr);
598 /* get the selected directory in tmpstr */
599 if (tmpstr[0] == '[')
601 tmpstr[lstrlenW(tmpstr) - 1] = 0;
602 strcpyW(tmpstr,tmpstr+1);
604 strcatW(tmpstr, FILE_bslash);
606 FILEDLG_ScanDir(hWnd, tmpstr);
607 /* notify the app */
608 if (lfs->hook)
610 if (FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, lst2,
611 MAKELONG(lRet,CD_LBSELCHANGE)))
612 return TRUE;
614 return TRUE;
617 /***********************************************************************
618 * FILEDLG_FileListSelect [internal]
619 * called when a new item is picked in the file list
621 static LRESULT FILEDLG_FileListSelect( LFSPRIVATE lfs )
623 LONG lRet;
624 HWND hWnd = lfs->hwnd;
625 LPWSTR pstr;
627 lRet = SendDlgItemMessageW(hWnd, lst1, LB_GETCURSEL16, 0, 0);
628 if (lRet == LB_ERR)
629 return TRUE;
631 /* set the edit control to the choosen file */
632 if ((pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
634 SendDlgItemMessageW(hWnd, lst1, LB_GETTEXT, lRet,
635 (LPARAM)pstr);
636 SetDlgItemTextW( hWnd, edt1, pstr );
637 HeapFree(GetProcessHeap(), 0, pstr);
639 if (lfs->hook)
641 FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, lst1,
642 MAKELONG(lRet,CD_LBSELCHANGE));
644 /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD,
645 CD_LBSELNOITEMS */
646 return TRUE;
649 /***********************************************************************
650 * FILEDLG_TestPath [internal]
651 * before accepting the file name, test if it includes wild cards
652 * tries to scan the directory and returns TRUE if no error.
654 static LRESULT FILEDLG_TestPath( LFSPRIVATE lfs, LPWSTR path )
656 HWND hWnd = lfs->hwnd;
657 LPWSTR pBeginFileName, pstr2;
658 WCHAR tmpstr2[BUFFILE];
660 pBeginFileName = strrchrW(path, '\\');
661 if (pBeginFileName == NULL)
662 pBeginFileName = strrchrW(path, ':');
664 if (strchrW(path,'*') != NULL || strchrW(path,'?') != NULL)
666 /* edit control contains wildcards */
667 if (pBeginFileName != NULL)
669 lstrcpynW(tmpstr2, pBeginFileName + 1, BUFFILE);
670 *(pBeginFileName + 1) = 0;
672 else
674 strcpyW(tmpstr2, path);
675 if(!(lfs->ofnW->Flags & OFN_NOVALIDATE))
676 *path = 0;
679 TRACE("path=%s, tmpstr2=%s\n", debugstr_w(path), debugstr_w(tmpstr2));
680 SetDlgItemTextW( hWnd, edt1, tmpstr2 );
681 FILEDLG_ScanDir(hWnd, path);
682 return (lfs->ofnW->Flags & OFN_NOVALIDATE) ? TRUE : FALSE;
685 /* no wildcards, we might have a directory or a filename */
686 /* try appending a wildcard and reading the directory */
688 pstr2 = path + lstrlenW(path);
689 if (pBeginFileName == NULL || *(pBeginFileName + 1) != 0)
690 strcatW(path, FILE_bslash);
692 /* if ScanDir succeeds, we have changed the directory */
693 if (FILEDLG_ScanDir(hWnd, path))
694 return FALSE; /* and path is not a valid file name */
696 /* if not, this must be a filename */
698 *pstr2 = 0; /* remove the wildcard added before */
700 if (pBeginFileName != NULL)
702 /* strip off the pathname */
703 *pBeginFileName = 0;
704 SetDlgItemTextW( hWnd, edt1, pBeginFileName + 1 );
706 lstrcpynW(tmpstr2, pBeginFileName + 1, sizeof(tmpstr2)/sizeof(WCHAR) );
707 /* Should we MessageBox() if this fails? */
708 if (!FILEDLG_ScanDir(hWnd, path))
710 return FALSE;
712 strcpyW(path, tmpstr2);
714 else
715 SetDlgItemTextW( hWnd, edt1, path );
716 return TRUE;
719 /***********************************************************************
720 * FILEDLG_Validate [internal]
721 * called on: click Ok button, Enter in edit, DoubleClick in file list
723 static LRESULT FILEDLG_Validate( LFSPRIVATE lfs, LPWSTR path, UINT control, INT itemIndex,
724 BOOL internalUse )
726 LONG lRet;
727 HWND hWnd = lfs->hwnd;
728 OPENFILENAMEW ofnsav;
729 LPOPENFILENAMEW ofnW = lfs->ofnW;
730 WCHAR filename[BUFFILE];
732 ofnsav = *ofnW; /* for later restoring */
734 /* get current file name */
735 if (path)
736 lstrcpynW(filename, path, sizeof(filename)/sizeof(WCHAR));
737 else
738 GetDlgItemTextW( hWnd, edt1, filename, sizeof(filename)/sizeof(WCHAR));
740 TRACE("got filename = %s\n", debugstr_w(filename));
741 /* if we did not click in file list to get there */
742 if (control != lst1)
744 if (!FILEDLG_TestPath( lfs, filename) )
745 return FALSE;
747 FILEDLG_UpdateResult(lfs, filename);
749 if (internalUse)
750 { /* called internally after a change in a combo */
751 if (lfs->hook)
753 FILEDLG_CallWindowProc(lfs, lfs->lbselchstring, control,
754 MAKELONG(itemIndex,CD_LBSELCHANGE));
756 return TRUE;
759 FILEDLG_UpdateFileTitle(lfs);
760 if (lfs->hook)
762 lRet = (BOOL)FILEDLG_CallWindowProc(lfs, lfs->fileokstring,
763 0, lfs->lParam );
764 if (lRet)
766 *ofnW = ofnsav; /* restore old state */
767 return FALSE;
770 if ((ofnW->Flags & OFN_ALLOWMULTISELECT) && (ofnW->Flags & OFN_EXPLORER))
772 if (ofnW->lpstrFile)
774 LPWSTR str = (LPWSTR)ofnW->lpstrFile;
775 LPWSTR ptr = strrchrW(str, '\\');
776 str[lstrlenW(str) + 1] = '\0';
777 *ptr = 0;
780 return TRUE;
783 /***********************************************************************
784 * FILEDLG_DiskChange [internal]
785 * called when a new item is picked in the disk selection combo
787 static LRESULT FILEDLG_DiskChange( LFSPRIVATE lfs )
789 LONG lRet;
790 HWND hWnd = lfs->hwnd;
791 LPWSTR pstr;
792 WCHAR diskname[BUFFILE];
794 FILEDLG_StripEditControl(hWnd);
795 lRet = SendDlgItemMessageW(hWnd, cmb2, CB_GETCURSEL, 0, 0L);
796 if (lRet == LB_ERR)
797 return 0;
798 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
799 SendDlgItemMessageW(hWnd, cmb2, CB_GETLBTEXT, lRet,
800 (LPARAM)pstr);
801 wsprintfW(diskname, FILE_specc, pstr[2]);
802 HeapFree(GetProcessHeap(), 0, pstr);
804 return FILEDLG_Validate( lfs, diskname, cmb2, lRet, TRUE );
807 /***********************************************************************
808 * FILEDLG_FileTypeChange [internal]
809 * called when a new item is picked in the file type combo
811 static LRESULT FILEDLG_FileTypeChange( LFSPRIVATE lfs )
813 LONG lRet;
814 WCHAR diskname[BUFFILE];
815 LPWSTR pstr;
817 diskname[0] = 0;
819 lRet = SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETCURSEL, 0, 0);
820 if (lRet == LB_ERR)
821 return TRUE;
822 pstr = (LPWSTR)SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETITEMDATA, lRet, 0);
823 TRACE("Selected filter : %s\n", debugstr_w(pstr));
824 SetDlgItemTextW( lfs->hwnd, edt1, pstr );
826 return FILEDLG_Validate( lfs, NULL, cmb1, lRet, TRUE );
829 /***********************************************************************
830 * FILEDLG_WMCommand [internal]
832 static LRESULT FILEDLG_WMCommand(HWND hWnd, LPARAM lParam, UINT notification,
833 UINT control, LFSPRIVATE lfs )
835 switch (control)
837 case lst1: /* file list */
838 FILEDLG_StripEditControl(hWnd);
839 if (notification == LBN_DBLCLK)
841 if (FILEDLG_Validate( lfs, NULL, control, 0, FALSE ))
842 EndDialog(hWnd, TRUE);
843 return TRUE;
845 else if (notification == LBN_SELCHANGE)
846 return FILEDLG_FileListSelect( lfs );
847 break;
849 case lst2: /* directory list */
850 FILEDLG_StripEditControl(hWnd);
851 if (notification == LBN_DBLCLK)
852 return FILEDLG_DirListDblClick( lfs );
853 break;
855 case cmb1: /* file type drop list */
856 if (notification == CBN_SELCHANGE)
857 return FILEDLG_FileTypeChange( lfs );
858 break;
860 case chx1:
861 break;
863 case pshHelp:
864 break;
866 case cmb2: /* disk dropdown combo */
867 if (notification == CBN_SELCHANGE)
868 return FILEDLG_DiskChange( lfs );
869 break;
871 case IDOK:
872 TRACE("OK pressed\n");
873 if (FILEDLG_Validate( lfs, NULL, control, 0, FALSE ))
874 EndDialog(hWnd, TRUE);
875 return TRUE;
877 case IDCANCEL:
878 EndDialog(hWnd, FALSE);
879 return TRUE;
881 case IDABORT: /* can be sent by the hook procedure */
882 EndDialog(hWnd, TRUE);
883 return TRUE;
885 return FALSE;
888 /***********************************************************************
889 * FILEDLG_MapDrawItemStruct [internal]
890 * map a 16 bits drawitem struct to 32
892 static void FILEDLG_MapDrawItemStruct(LPDRAWITEMSTRUCT16 lpdis16, LPDRAWITEMSTRUCT lpdis)
894 lpdis->CtlType = lpdis16->CtlType;
895 lpdis->CtlID = lpdis16->CtlID;
896 lpdis->itemID = lpdis16->itemID;
897 lpdis->itemAction = lpdis16->itemAction;
898 lpdis->itemState = lpdis16->itemState;
899 lpdis->hwndItem = HWND_32(lpdis16->hwndItem);
900 lpdis->hDC = HDC_32(lpdis16->hDC);
901 lpdis->rcItem.right = lpdis16->rcItem.right;
902 lpdis->rcItem.left = lpdis16->rcItem.left;
903 lpdis->rcItem.top = lpdis16->rcItem.top;
904 lpdis->rcItem.bottom = lpdis16->rcItem.bottom;
905 lpdis->itemData = lpdis16->itemData;
908 /************************************************************************
909 * FILEDLG_MapStringPairsToW [internal]
910 * map string pairs to Unicode
912 static LPWSTR FILEDLG_MapStringPairsToW(LPCSTR strA, UINT size)
914 LPCSTR s;
915 LPWSTR x;
916 int n, len;
918 s = strA;
919 while (*s)
920 s = s+strlen(s)+1;
921 s++;
922 n = s + 1 - strA; /* Don't forget the other \0 */
923 if (n < size) n = size;
925 len = MultiByteToWideChar( CP_ACP, 0, strA, n, NULL, 0 );
926 x = HeapAlloc(GetProcessHeap(),0, len * sizeof(WCHAR));
927 MultiByteToWideChar( CP_ACP, 0, strA, n, x, len );
928 return x;
932 /************************************************************************
933 * FILEDLG_DupToW [internal]
934 * duplicates an Ansi string to unicode, with a buffer size
936 LPWSTR FILEDLG_DupToW(LPCSTR str, DWORD size)
938 LPWSTR strW = NULL;
939 if (str && (size > 0))
941 strW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
942 if (strW) MultiByteToWideChar( CP_ACP, 0, str, -1, strW, size );
944 return strW;
948 /************************************************************************
949 * FILEDLG_MapOfnStructA [internal]
950 * map a 32 bits Ansi structure to an Unicode one
952 void FILEDLG_MapOfnStructA(LPOPENFILENAMEA ofnA, LPOPENFILENAMEW ofnW, BOOL open)
954 LPCSTR str;
955 UNICODE_STRING usBuffer;
957 ofnW->lStructSize = sizeof(OPENFILENAMEW);
958 ofnW->hwndOwner = ofnA->hwndOwner;
959 ofnW->hInstance = ofnA->hInstance;
960 if (ofnA->lpstrFilter)
961 ofnW->lpstrFilter = FILEDLG_MapStringPairsToW(ofnA->lpstrFilter, 0);
963 if ((ofnA->lpstrCustomFilter) && (*(ofnA->lpstrCustomFilter)))
964 ofnW->lpstrCustomFilter = FILEDLG_MapStringPairsToW(ofnA->lpstrCustomFilter, ofnA->nMaxCustFilter);
965 ofnW->nMaxCustFilter = ofnA->nMaxCustFilter;
966 ofnW->nFilterIndex = ofnA->nFilterIndex;
967 ofnW->nMaxFile = ofnA->nMaxFile;
968 ofnW->lpstrFile = FILEDLG_DupToW(ofnA->lpstrFile, ofnW->nMaxFile);
969 ofnW->nMaxFileTitle = ofnA->nMaxFileTitle;
970 ofnW->lpstrFileTitle = FILEDLG_DupToW(ofnA->lpstrFileTitle, ofnW->nMaxFileTitle);
971 if (ofnA->lpstrInitialDir)
973 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpstrInitialDir);
974 ofnW->lpstrInitialDir = usBuffer.Buffer;
976 if (ofnA->lpstrTitle)
977 str = ofnA->lpstrTitle;
978 else
979 /* Allocates default title (FIXME : get it from resource) */
980 str = open ? defaultopen:defaultsave;
981 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpstrTitle);
982 ofnW->lpstrTitle = usBuffer.Buffer;
983 ofnW->Flags = ofnA->Flags;
984 ofnW->nFileOffset = ofnA->nFileOffset;
985 ofnW->nFileExtension = ofnA->nFileExtension;
986 ofnW->lpstrDefExt = FILEDLG_DupToW(ofnA->lpstrDefExt, 3);
987 if ((ofnA->Flags & OFN_ENABLETEMPLATE) && (ofnA->lpTemplateName))
989 if (HIWORD(ofnA->lpTemplateName))
991 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpTemplateName);
992 ofnW->lpTemplateName = usBuffer.Buffer;
994 else /* numbered resource */
995 ofnW->lpTemplateName = (LPWSTR) ofnA->lpTemplateName;
999 /************************************************************************
1000 * FILEDLG_MapOfnStruct16 [internal]
1001 * map a 16 bits structure to an Unicode one
1003 void FILEDLG_MapOfnStruct16(LPOPENFILENAME16 ofn16, LPOPENFILENAMEW ofnW, BOOL open)
1005 OPENFILENAMEA ofnA;
1006 /* first convert to linear pointers */
1007 memset(&ofnA, 0, sizeof(OPENFILENAMEA));
1008 ofnA.lStructSize = sizeof(OPENFILENAMEA);
1009 ofnA.hwndOwner = HWND_32(ofn16->hwndOwner);
1010 ofnA.hInstance = HINSTANCE_32(ofn16->hInstance);
1011 if (ofn16->lpstrFilter)
1012 ofnA.lpstrFilter = MapSL(ofn16->lpstrFilter);
1013 if (ofn16->lpstrCustomFilter)
1014 ofnA.lpstrCustomFilter = MapSL(ofn16->lpstrCustomFilter);
1015 ofnA.nMaxCustFilter = ofn16->nMaxCustFilter;
1016 ofnA.nFilterIndex = ofn16->nFilterIndex;
1017 ofnA.lpstrFile = MapSL(ofn16->lpstrFile);
1018 ofnA.nMaxFile = ofn16->nMaxFile;
1019 ofnA.lpstrFileTitle = MapSL(ofn16->lpstrFileTitle);
1020 ofnA.nMaxFileTitle = ofn16->nMaxFileTitle;
1021 ofnA.lpstrInitialDir = MapSL(ofn16->lpstrInitialDir);
1022 ofnA.lpstrTitle = MapSL(ofn16->lpstrTitle);
1023 ofnA.Flags = ofn16->Flags;
1024 ofnA.nFileOffset = ofn16->nFileOffset;
1025 ofnA.nFileExtension = ofn16->nFileExtension;
1026 ofnA.lpstrDefExt = MapSL(ofn16->lpstrDefExt);
1027 if (HIWORD(ofn16->lpTemplateName))
1028 ofnA.lpTemplateName = MapSL(ofn16->lpTemplateName);
1029 else
1030 ofnA.lpTemplateName = (LPSTR) ofn16->lpTemplateName; /* ressource number */
1031 /* now calls the 32 bits Ansi to Unicode version to complete the job */
1032 FILEDLG_MapOfnStructA(&ofnA, ofnW, open);
1035 /************************************************************************
1036 * FILEDLG_DestroyPrivate [internal]
1037 * destroys the private object
1039 static void FILEDLG_DestroyPrivate(LFSPRIVATE lfs)
1041 HWND hwnd;
1042 if (!lfs) return;
1043 hwnd = lfs->hwnd;
1044 /* free resources for a 16 bits dialog */
1045 if (lfs->hResource16) FreeResource16(lfs->hResource16);
1046 if (lfs->hGlobal16)
1048 GlobalUnlock16(lfs->hGlobal16);
1049 GlobalFree16(lfs->hGlobal16);
1051 /* if ofnW has been allocated, have to free everything in it */
1052 if (lfs->ofn16 || lfs->ofnA)
1054 LPOPENFILENAMEW ofnW = lfs->ofnW;
1055 if (ofnW->lpstrFilter) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrFilter);
1056 if (ofnW->lpstrCustomFilter) HeapFree(GetProcessHeap(), 0, ofnW->lpstrCustomFilter);
1057 if (ofnW->lpstrFile) HeapFree(GetProcessHeap(), 0, ofnW->lpstrFile);
1058 if (ofnW->lpstrFileTitle) HeapFree(GetProcessHeap(), 0, ofnW->lpstrFileTitle);
1059 if (ofnW->lpstrInitialDir) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrInitialDir);
1060 if (ofnW->lpstrTitle) HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrTitle);
1061 if ((ofnW->lpTemplateName) && (HIWORD(ofnW->lpTemplateName)))
1062 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpTemplateName);
1063 HeapFree(GetProcessHeap(), 0, ofnW);
1065 TRACE("destroying private allocation %p\n", lfs);
1066 HeapFree(GetProcessHeap(), 0, lfs);
1067 RemovePropA(hwnd, OFN_PROP);
1070 /************************************************************************
1071 * FILEDLG_AllocPrivate [internal]
1072 * allocate a private object to hold 32 bits Unicode
1073 * structure that will be used throughtout the calls, while
1074 * keeping available the original structures and a few variables
1075 * On entry : type = dialog procedure type (16,32A,32W)
1076 * dlgType = dialog type (open or save)
1078 static LFSPRIVATE FILEDLG_AllocPrivate(LPARAM lParam, int type, UINT dlgType)
1080 LFSPRIVATE lfs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct FSPRIVATE));
1081 LFSPRIVATE ret;
1082 TRACE("alloc private buf %p\n", lfs);
1083 if (!lfs) return NULL;
1084 lfs->hook = FALSE;
1085 lfs->lParam = lParam;
1086 if (dlgType == OPEN_DIALOG)
1087 lfs->open = TRUE;
1088 else
1089 lfs->open = FALSE;
1090 lfs->lbselchstring = RegisterWindowMessageA(LBSELCHSTRINGA);
1091 lfs->fileokstring = RegisterWindowMessageA(FILEOKSTRINGA);
1092 switch(type)
1094 case LFS16:
1095 lfs->ofn16 = MapSL(lParam);
1096 if (lfs->ofn16->Flags & OFN_ENABLEHOOK)
1097 if (lfs->ofn16->lpfnHook)
1098 lfs->hook = TRUE;
1100 break;
1102 case LFS32A:
1103 lfs->ofnA = (LPOPENFILENAMEA) lParam;
1104 if (lfs->ofnA->Flags & OFN_ENABLEHOOK)
1105 if (lfs->ofnA->lpfnHook)
1106 lfs->hook = TRUE;
1107 break;
1109 case LFS32W:
1110 lfs->ofnW = (LPOPENFILENAMEW) lParam;
1111 if (lfs->ofnW->Flags & OFN_ENABLEHOOK)
1112 if (lfs->ofnW->lpfnHook)
1113 lfs->hook = TRUE;
1114 break;
1116 ret = lfs;
1117 if (!lfs->ofnW)
1118 { /* this structure is needed internally, so create it */
1119 lfs->ofnW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(OPENFILENAMEW));
1120 if (lfs->ofnW)
1122 if (lfs->ofn16)
1123 FILEDLG_MapOfnStruct16(lfs->ofn16, lfs->ofnW, lfs->open);
1124 if (lfs->ofnA)
1125 FILEDLG_MapOfnStructA(lfs->ofnA, lfs->ofnW, lfs->open);
1127 else
1128 ret = NULL;
1130 if (lfs->ofn16)
1132 if (!Get16BitsTemplate(lfs)) ret = NULL;
1134 else
1135 if (!Get32BitsTemplate(lfs)) ret = NULL;
1136 if (!ret) FILEDLG_DestroyPrivate(lfs);
1137 return ret;
1140 /***********************************************************************
1141 * FILEDLG_CallWindowProc16 [internal]
1143 * Call the appropriate hook
1145 static BOOL FILEDLG_CallWindowProc16(LFSPRIVATE lfs, UINT wMsg, WPARAM wParam,
1146 LPARAM lParam)
1148 if (lfs->ofn16)
1150 return (BOOL16) CallWindowProc16(
1151 (WNDPROC16)lfs->ofn16->lpfnHook, HWND_16(lfs->hwnd),
1152 (UINT16)wMsg, (WPARAM16)wParam, lParam);
1154 return FALSE;
1157 /***********************************************************************
1158 * FILEDLG_WMInitDialog16 [internal]
1159 * The is a duplicate of the 32bit FILEDLG_WMInitDialog function
1160 * The only differnce is that it calls FILEDLG_CallWindowProc16
1161 * for a 16 bit Window Proc.
1164 static LONG FILEDLG_WMInitDialog16(HWND hWnd, WPARAM wParam, LPARAM lParam)
1166 int i, n;
1167 WCHAR tmpstr[BUFFILE];
1168 LPWSTR pstr, old_pstr;
1169 LPOPENFILENAMEW ofn;
1170 LFSPRIVATE lfs = (LFSPRIVATE) lParam;
1172 if (!lfs) return FALSE;
1173 SetPropA(hWnd, OFN_PROP, (HANDLE)lfs);
1174 lfs->hwnd = hWnd;
1175 ofn = lfs->ofnW;
1177 TRACE("flags=%lx initialdir=%s\n", ofn->Flags, debugstr_w(ofn->lpstrInitialDir));
1179 SetWindowTextW( hWnd, ofn->lpstrTitle );
1180 /* read custom filter information */
1181 if (ofn->lpstrCustomFilter)
1183 pstr = ofn->lpstrCustomFilter;
1184 n = 0;
1185 TRACE("lpstrCustomFilter = %p\n", pstr);
1186 while(*pstr)
1188 old_pstr = pstr;
1189 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
1190 (LPARAM)(ofn->lpstrCustomFilter) + n );
1191 n += lstrlenW(pstr) + 1;
1192 pstr += lstrlenW(pstr) + 1;
1193 TRACE("add str=%s associated to %s\n",
1194 debugstr_w(old_pstr), debugstr_w(pstr));
1195 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
1196 n += lstrlenW(pstr) + 1;
1197 pstr += lstrlenW(pstr) + 1;
1200 /* read filter information */
1201 if (ofn->lpstrFilter) {
1202 pstr = (LPWSTR) ofn->lpstrFilter;
1203 n = 0;
1204 while(*pstr) {
1205 old_pstr = pstr;
1206 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
1207 (LPARAM)(ofn->lpstrFilter + n) );
1208 n += lstrlenW(pstr) + 1;
1209 pstr += lstrlenW(pstr) + 1;
1210 TRACE("add str=%s associated to %s\n",
1211 debugstr_w(old_pstr), debugstr_w(pstr));
1212 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
1213 n += lstrlenW(pstr) + 1;
1214 pstr += lstrlenW(pstr) + 1;
1217 /* set default filter */
1218 if (ofn->nFilterIndex == 0 && ofn->lpstrCustomFilter == NULL)
1219 ofn->nFilterIndex = 1;
1220 SendDlgItemMessageW(hWnd, cmb1, CB_SETCURSEL, ofn->nFilterIndex - 1, 0);
1221 lstrcpynW(tmpstr, FILEDLG_GetFileType(ofn->lpstrCustomFilter,
1222 (LPWSTR)ofn->lpstrFilter, ofn->nFilterIndex - 1),BUFFILE);
1223 TRACE("nFilterIndex = %ld, SetText of edt1 to %s\n",
1224 ofn->nFilterIndex, debugstr_w(tmpstr));
1225 SetDlgItemTextW( hWnd, edt1, tmpstr );
1226 /* get drive list */
1227 *tmpstr = 0;
1228 DlgDirListComboBoxW(hWnd, tmpstr, cmb2, 0, DDL_DRIVES | DDL_EXCLUSIVE);
1229 /* read initial directory */
1230 /* FIXME: Note that this is now very version-specific (See MSDN description of
1231 * the OPENFILENAME structure). For example under 2000/XP any path in the
1232 * lpstrFile overrides the lpstrInitialDir, but not under 95/98/ME
1234 if (ofn->lpstrInitialDir != NULL)
1236 int len;
1237 lstrcpynW(tmpstr, ofn->lpstrInitialDir, 511);
1238 len = lstrlenW(tmpstr);
1239 if (len > 0 && tmpstr[len-1] != '\\' && tmpstr[len-1] != ':') {
1240 tmpstr[len]='\\';
1241 tmpstr[len+1]='\0';
1244 else
1245 *tmpstr = 0;
1246 if (!FILEDLG_ScanDir(hWnd, tmpstr)) {
1247 *tmpstr = 0;
1248 if (!FILEDLG_ScanDir(hWnd, tmpstr))
1249 WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr));
1251 /* select current drive in combo 2, omit missing drives */
1253 char dir[MAX_PATH];
1254 char str[4] = "a:\\";
1255 GetCurrentDirectoryA( sizeof(dir), dir );
1256 for(i = 0, n = -1; i < 26; i++)
1258 str[0] = 'a' + i;
1259 if (GetDriveTypeA(str) > DRIVE_NO_ROOT_DIR) n++;
1260 if (toupper(str[0]) == toupper(dir[0])) break;
1263 SendDlgItemMessageW(hWnd, cmb2, CB_SETCURSEL, n, 0);
1264 if (!(ofn->Flags & OFN_SHOWHELP))
1265 ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
1266 if (ofn->Flags & OFN_HIDEREADONLY)
1267 ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
1268 if (lfs->hook)
1269 return (BOOL) FILEDLG_CallWindowProc16(lfs, WM_INITDIALOG, wParam, lfs->lParam);
1270 return TRUE;
1273 /***********************************************************************
1274 * FILEDLG_WMMeasureItem16 [internal]
1276 static LONG FILEDLG_WMMeasureItem16(HWND16 hWnd, WPARAM16 wParam, LPARAM lParam)
1278 LPMEASUREITEMSTRUCT16 lpmeasure;
1280 lpmeasure = MapSL(lParam);
1281 lpmeasure->itemHeight = fldrHeight;
1282 return TRUE;
1285 /* ------------------ Dialog procedures ---------------------- */
1287 /***********************************************************************
1288 * FileOpenDlgProc (COMMDLG.6)
1290 BOOL16 CALLBACK FileOpenDlgProc16(HWND16 hWnd16, UINT16 wMsg, WPARAM16 wParam,
1291 LPARAM lParam)
1293 HWND hWnd = HWND_32(hWnd16);
1294 LFSPRIVATE lfs = (LFSPRIVATE)GetPropA(hWnd,OFN_PROP);
1295 DRAWITEMSTRUCT dis;
1297 TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
1298 if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
1300 LRESULT lRet = (BOOL16)FILEDLG_CallWindowProc16(lfs, wMsg, wParam, lParam);
1301 if (lRet)
1302 return lRet; /* else continue message processing */
1304 switch (wMsg)
1306 case WM_INITDIALOG:
1307 return FILEDLG_WMInitDialog16(hWnd, wParam, lParam);
1309 case WM_MEASUREITEM:
1310 return FILEDLG_WMMeasureItem16(hWnd16, wParam, lParam);
1312 case WM_DRAWITEM:
1313 FILEDLG_MapDrawItemStruct(MapSL(lParam), &dis);
1314 return FILEDLG_WMDrawItem(hWnd, wParam, lParam, FALSE, &dis);
1316 case WM_COMMAND:
1317 return FILEDLG_WMCommand(hWnd, lParam, HIWORD(lParam),wParam, lfs);
1318 #if 0
1319 case WM_CTLCOLOR:
1320 SetBkColor((HDC16)wParam, 0x00C0C0C0);
1321 switch (HIWORD(lParam))
1323 case CTLCOLOR_BTN:
1324 SetTextColor((HDC16)wParam, 0x00000000);
1325 return hGRAYBrush;
1326 case CTLCOLOR_STATIC:
1327 SetTextColor((HDC16)wParam, 0x00000000);
1328 return hGRAYBrush;
1330 break;
1331 #endif
1333 return FALSE;
1336 /***********************************************************************
1337 * FileSaveDlgProc (COMMDLG.7)
1339 BOOL16 CALLBACK FileSaveDlgProc16(HWND16 hWnd16, UINT16 wMsg, WPARAM16 wParam,
1340 LPARAM lParam)
1342 HWND hWnd = HWND_32(hWnd16);
1343 LFSPRIVATE lfs = (LFSPRIVATE)GetPropA(hWnd,OFN_PROP);
1344 DRAWITEMSTRUCT dis;
1346 TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
1347 if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
1349 LRESULT lRet;
1350 lRet = (BOOL16)FILEDLG_CallWindowProc16(lfs, wMsg, wParam, lParam);
1351 if (lRet)
1352 return lRet; /* else continue message processing */
1354 switch (wMsg) {
1355 case WM_INITDIALOG:
1356 return FILEDLG_WMInitDialog16(hWnd, wParam, lParam);
1358 case WM_MEASUREITEM:
1359 return FILEDLG_WMMeasureItem16(hWnd16, wParam, lParam);
1361 case WM_DRAWITEM:
1362 FILEDLG_MapDrawItemStruct(MapSL(lParam), &dis);
1363 return FILEDLG_WMDrawItem(hWnd, wParam, lParam, TRUE, &dis);
1365 case WM_COMMAND:
1366 return FILEDLG_WMCommand(hWnd, lParam, HIWORD(lParam), wParam, lfs);
1370 case WM_CTLCOLOR:
1371 SetBkColor((HDC16)wParam, 0x00C0C0C0);
1372 switch (HIWORD(lParam))
1374 case CTLCOLOR_BTN:
1375 SetTextColor((HDC16)wParam, 0x00000000);
1376 return hGRAYBrush;
1377 case CTLCOLOR_STATIC:
1378 SetTextColor((HDC16)wParam, 0x00000000);
1379 return hGRAYBrush;
1381 return FALSE;
1384 return FALSE;
1387 /* ------------------ APIs ---------------------- */
1389 /***********************************************************************
1390 * GetOpenFileName (COMMDLG.1)
1392 * Creates a dialog box for the user to select a file to open.
1394 * RETURNS
1395 * TRUE on success: user selected a valid file
1396 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1398 * BUGS
1399 * unknown, there are some FIXME's left.
1401 BOOL16 WINAPI GetOpenFileName16(
1402 SEGPTR ofn /* [in/out] address of structure with data*/
1405 HINSTANCE16 hInst;
1406 BOOL bRet = FALSE;
1407 LPOPENFILENAME16 lpofn = MapSL(ofn);
1408 LFSPRIVATE lfs;
1409 FARPROC16 ptr;
1411 if (!lpofn || !FileDlg_Init()) return FALSE;
1413 lfs = FILEDLG_AllocPrivate((LPARAM) ofn, LFS16, OPEN_DIALOG);
1414 if (lfs)
1416 hInst = GetWindowWord( HWND_32(lpofn->hwndOwner), GWL_HINSTANCE );
1417 ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (LPCSTR) 6);
1418 bRet = DialogBoxIndirectParam16( hInst, lfs->hDlgTmpl16, lpofn->hwndOwner,
1419 (DLGPROC16) ptr, (LPARAM) lfs);
1420 FILEDLG_DestroyPrivate(lfs);
1423 TRACE("return lpstrFile='%s' !\n", (char *)MapSL(lpofn->lpstrFile));
1424 return bRet;
1427 /***********************************************************************
1428 * GetSaveFileName (COMMDLG.2)
1430 * Creates a dialog box for the user to select a file to save.
1432 * RETURNS
1433 * TRUE on success: user enters a valid file
1434 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
1436 * BUGS
1437 * unknown. There are some FIXME's left.
1439 BOOL16 WINAPI GetSaveFileName16(
1440 SEGPTR ofn /* [in/out] addess of structure with data*/
1443 HINSTANCE16 hInst;
1444 BOOL bRet = FALSE;
1445 LPOPENFILENAME16 lpofn = MapSL(ofn);
1446 LFSPRIVATE lfs;
1447 FARPROC16 ptr;
1449 if (!lpofn || !FileDlg_Init()) return FALSE;
1451 lfs = FILEDLG_AllocPrivate((LPARAM) ofn, LFS16, SAVE_DIALOG);
1452 if (lfs)
1454 hInst = GetWindowWord( HWND_32(lpofn->hwndOwner), GWL_HINSTANCE );
1455 ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (LPCSTR) 7);
1456 bRet = DialogBoxIndirectParam16( hInst, lfs->hDlgTmpl16, lpofn->hwndOwner,
1457 (DLGPROC16) ptr, (LPARAM) lfs);
1458 FILEDLG_DestroyPrivate(lfs);
1461 TRACE("return lpstrFile='%s' !\n", (char *)MapSL(lpofn->lpstrFile));
1462 return bRet;