save old text color during a call of DrawCaptionTempW
[wine/kumbayo.git] / dlls / comdlg32 / filedlg31.c
blob5b76d8e9646e404962200252f4f7904099e4326c
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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/unicode.h"
32 #include "wine/debug.h"
33 #include "winreg.h"
34 #include "winternl.h"
35 #include "commdlg.h"
36 #include "shlwapi.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
40 #include "cdlg.h"
41 #include "filedlg31.h"
43 #define BUFFILE 512
44 #define BUFFILEALLOC 512 * sizeof(WCHAR)
46 static const WCHAR FILE_star[] = {'*','.','*', 0};
47 static const WCHAR FILE_bslash[] = {'\\', 0};
48 static const WCHAR FILE_specc[] = {'%','c',':', 0};
49 static const int fldrHeight = 16;
50 static const int fldrWidth = 20;
52 static HICON hFolder = 0;
53 static HICON hFolder2 = 0;
54 static HICON hFloppy = 0;
55 static HICON hHDisk = 0;
56 static HICON hCDRom = 0;
57 static HICON hNet = 0;
59 /***********************************************************************
60 * FD31_Init [internal]
62 BOOL FD31_Init(void)
64 static BOOL initialized = 0;
66 if (!initialized) {
67 hFolder = LoadImageA( COMDLG32_hInstance, "FOLDER", IMAGE_ICON, 16, 16, LR_SHARED );
68 hFolder2 = LoadImageA( COMDLG32_hInstance, "FOLDER2", IMAGE_ICON, 16, 16, LR_SHARED );
69 hFloppy = LoadImageA( COMDLG32_hInstance, "FLOPPY", IMAGE_ICON, 16, 16, LR_SHARED );
70 hHDisk = LoadImageA( COMDLG32_hInstance, "HDISK", IMAGE_ICON, 16, 16, LR_SHARED );
71 hCDRom = LoadImageA( COMDLG32_hInstance, "CDROM", IMAGE_ICON, 16, 16, LR_SHARED );
72 hNet = LoadImageA( COMDLG32_hInstance, "NETWORK", IMAGE_ICON, 16, 16, LR_SHARED );
73 if (hFolder == 0 || hFolder2 == 0 || hFloppy == 0 ||
74 hHDisk == 0 || hCDRom == 0 || hNet == 0)
76 ERR("Error loading icons !\n");
77 return FALSE;
79 initialized = TRUE;
81 return TRUE;
84 /***********************************************************************
85 * FD31_StripEditControl [internal]
86 * Strip pathnames off the contents of the edit control.
88 static void FD31_StripEditControl(HWND hwnd)
90 WCHAR temp[BUFFILE], *cp;
92 GetDlgItemTextW( hwnd, edt1, temp, sizeof(temp)/sizeof(WCHAR));
93 cp = strrchrW(temp, '\\');
94 if (cp != NULL) {
95 strcpyW(temp, cp+1);
97 cp = strrchrW(temp, ':');
98 if (cp != NULL) {
99 strcpyW(temp, cp+1);
101 /* FIXME: shouldn't we do something with the result here? ;-) */
104 /***********************************************************************
105 * FD31_CallWindowProc [internal]
107 * Call the appropriate hook
109 BOOL FD31_CallWindowProc(const FD31_DATA *lfs, UINT wMsg, WPARAM wParam,
110 LPARAM lParam)
112 return lfs->callbacks->CWP(lfs, wMsg, wParam, lParam);
115 /***********************************************************************
116 * FD31_ScanDir [internal]
118 static BOOL FD31_ScanDir(HWND hWnd, LPCWSTR newPath)
120 WCHAR buffer[BUFFILE];
121 HWND hdlg, hdlgDir;
122 LRESULT lRet = TRUE;
123 HCURSOR hCursorWait, oldCursor;
125 TRACE("Trying to change to %s\n", debugstr_w(newPath));
126 if ( newPath[0] && !SetCurrentDirectoryW( newPath ))
127 return FALSE;
128 lstrcpynW(buffer, newPath, sizeof(buffer)/sizeof(WCHAR));
130 /* get the list of spec files */
131 GetDlgItemTextW(hWnd, edt1, buffer, sizeof(buffer)/sizeof(WCHAR));
133 hCursorWait = LoadCursorA(0, (LPSTR)IDC_WAIT);
134 oldCursor = SetCursor(hCursorWait);
136 /* list of files */
137 if ((hdlg = GetDlgItem(hWnd, lst1)) != 0) {
138 WCHAR* scptr; /* ptr on semi-colon */
139 WCHAR* filter = buffer;
141 TRACE("Using filter %s\n", debugstr_w(filter));
142 SendMessageW(hdlg, LB_RESETCONTENT, 0, 0);
143 while (filter) {
144 scptr = strchrW(filter, ';');
145 if (scptr) *scptr = 0;
146 while (*filter == ' ') filter++;
147 TRACE("Using file spec %s\n", debugstr_w(filter));
148 SendMessageW(hdlg, LB_DIR, 0, (LPARAM)filter);
149 if (scptr) *scptr = ';';
150 filter = (scptr) ? (scptr + 1) : 0;
154 /* list of directories */
155 strcpyW(buffer, FILE_star);
157 if ((hdlgDir = GetDlgItem(hWnd, lst2)) != 0) {
158 lRet = DlgDirListW(hWnd, buffer, lst2, stc1, DDL_EXCLUSIVE | DDL_DIRECTORY);
160 SetCursor(oldCursor);
161 return lRet;
164 /***********************************************************************
165 * FD31_GetFileType [internal]
168 static LPCWSTR FD31_GetFileType(LPCWSTR cfptr, LPCWSTR fptr, const WORD index)
170 int n, i;
171 i = 0;
172 if (cfptr)
173 for ( ;(n = lstrlenW(cfptr)) != 0; i++)
175 cfptr += n + 1;
176 if (i == index)
177 return cfptr;
178 cfptr += lstrlenW(cfptr) + 1;
180 if (fptr)
181 for ( ;(n = lstrlenW(fptr)) != 0; i++)
183 fptr += n + 1;
184 if (i == index)
185 return fptr;
186 fptr += lstrlenW(fptr) + 1;
188 return FILE_star; /* FIXME */
191 /***********************************************************************
192 * FD31_WMDrawItem [internal]
194 LONG FD31_WMDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam,
195 int savedlg, const DRAWITEMSTRUCT *lpdis)
197 WCHAR *str;
198 HICON hIcon;
199 COLORREF oldText = 0, oldBk = 0;
201 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst1)
203 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC))) return FALSE;
204 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
205 (LPARAM)str);
207 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
209 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
210 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
212 if (savedlg)
213 SetTextColor(lpdis->hDC,GetSysColor(COLOR_GRAYTEXT) );
215 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + 1,
216 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
217 &(lpdis->rcItem), str, lstrlenW(str), NULL);
219 if (lpdis->itemState & ODS_SELECTED)
220 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
222 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
224 SetBkColor( lpdis->hDC, oldBk );
225 SetTextColor( lpdis->hDC, oldText );
227 HeapFree(GetProcessHeap(), 0, str);
228 return TRUE;
231 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst2)
233 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
234 return FALSE;
235 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
236 (LPARAM)str);
238 if (lpdis->itemState & ODS_SELECTED)
240 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
241 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
243 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
244 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
245 &(lpdis->rcItem), str, lstrlenW(str), NULL);
247 if (lpdis->itemState & ODS_SELECTED)
248 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
250 if (lpdis->itemState & ODS_SELECTED)
252 SetBkColor( lpdis->hDC, oldBk );
253 SetTextColor( lpdis->hDC, oldText );
255 DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hFolder);
256 HeapFree(GetProcessHeap(), 0, str);
257 return TRUE;
259 if (lpdis->CtlType == ODT_COMBOBOX && lpdis->CtlID == cmb2)
261 char root[] = "a:";
262 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
263 return FALSE;
264 SendMessageW(lpdis->hwndItem, CB_GETLBTEXT, lpdis->itemID,
265 (LPARAM)str);
266 root[0] += str[2] - 'a';
267 switch(GetDriveTypeA(root))
269 case DRIVE_REMOVABLE: hIcon = hFloppy; break;
270 case DRIVE_CDROM: hIcon = hCDRom; break;
271 case DRIVE_REMOTE: hIcon = hNet; break;
272 case DRIVE_FIXED:
273 default: hIcon = hHDisk; break;
275 if (lpdis->itemState & ODS_SELECTED)
277 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
278 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
280 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
281 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
282 &(lpdis->rcItem), str, lstrlenW(str), NULL);
284 if (lpdis->itemState & ODS_SELECTED)
286 SetBkColor( lpdis->hDC, oldBk );
287 SetTextColor( lpdis->hDC, oldText );
289 DrawIcon(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hIcon);
290 HeapFree(GetProcessHeap(), 0, str);
291 return TRUE;
293 return FALSE;
296 /***********************************************************************
297 * FD31_UpdateResult [internal]
298 * update the displayed file name (with path)
300 static void FD31_UpdateResult(const FD31_DATA *lfs, const WCHAR *tmpstr)
302 int lenstr2;
303 LPOPENFILENAMEW ofnW = lfs->ofnW;
304 WCHAR tmpstr2[BUFFILE];
305 WCHAR *p;
307 TRACE("%s\n", debugstr_w(tmpstr));
308 if(ofnW->Flags & OFN_NOVALIDATE)
309 tmpstr2[0] = '\0';
310 else
311 GetCurrentDirectoryW(BUFFILE, tmpstr2);
312 lenstr2 = strlenW(tmpstr2);
313 if (lenstr2 > 3)
314 tmpstr2[lenstr2++]='\\';
315 lstrcpynW(tmpstr2+lenstr2, tmpstr, BUFFILE-lenstr2);
316 if (ofnW->lpstrFile)
317 lstrcpynW(ofnW->lpstrFile, tmpstr2, ofnW->nMaxFile);
319 /* set filename offset */
320 p = PathFindFileNameW(ofnW->lpstrFile);
321 ofnW->nFileOffset = (p - ofnW->lpstrFile);
323 /* set extension offset */
324 p = PathFindExtensionW(ofnW->lpstrFile);
325 ofnW->nFileExtension = (*p) ? (p - ofnW->lpstrFile) + 1 : 0;
327 TRACE("file %s, file offset %d, ext offset %d\n",
328 debugstr_w(ofnW->lpstrFile), ofnW->nFileOffset, ofnW->nFileExtension);
330 /* update the real client structures if any */
331 lfs->callbacks->UpdateResult(lfs);
334 /***********************************************************************
335 * FD31_UpdateFileTitle [internal]
336 * update the displayed file name (without path)
338 static void FD31_UpdateFileTitle(const FD31_DATA *lfs)
340 LONG lRet;
341 LPOPENFILENAMEW ofnW = lfs->ofnW;
342 if (ofnW->lpstrFileTitle != NULL)
344 lRet = SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
345 SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETTEXT, lRet,
346 (LPARAM)ofnW->lpstrFileTitle );
347 lfs->callbacks->UpdateFileTitle(lfs);
351 /***********************************************************************
352 * FD31_DirListDblClick [internal]
354 static LRESULT FD31_DirListDblClick( const FD31_DATA *lfs )
356 LONG lRet;
357 HWND hWnd = lfs->hwnd;
358 LPWSTR pstr;
359 WCHAR tmpstr[BUFFILE];
361 /* get the raw string (with brackets) */
362 lRet = SendDlgItemMessageW(hWnd, lst2, LB_GETCURSEL, 0, 0);
363 if (lRet == LB_ERR) return TRUE;
364 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
365 SendDlgItemMessageW(hWnd, lst2, LB_GETTEXT, lRet,
366 (LPARAM)pstr);
367 strcpyW( tmpstr, pstr );
368 HeapFree(GetProcessHeap(), 0, pstr);
369 /* get the selected directory in tmpstr */
370 if (tmpstr[0] == '[')
372 tmpstr[lstrlenW(tmpstr) - 1] = 0;
373 strcpyW(tmpstr,tmpstr+1);
375 strcatW(tmpstr, FILE_bslash);
377 FD31_ScanDir(hWnd, tmpstr);
378 /* notify the app */
379 if (lfs->hook)
381 if (FD31_CallWindowProc(lfs, lfs->lbselchstring, lst2,
382 MAKELONG(lRet,CD_LBSELCHANGE)))
383 return TRUE;
385 return TRUE;
388 /***********************************************************************
389 * FD31_FileListSelect [internal]
390 * called when a new item is picked in the file list
392 static LRESULT FD31_FileListSelect( const FD31_DATA *lfs )
394 LONG lRet;
395 HWND hWnd = lfs->hwnd;
396 LPWSTR pstr;
398 lRet = lfs->callbacks->SendLbGetCurSel(lfs);
399 if (lRet == LB_ERR)
400 return TRUE;
402 /* set the edit control to the choosen file */
403 if ((pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
405 SendDlgItemMessageW(hWnd, lst1, LB_GETTEXT, lRet,
406 (LPARAM)pstr);
407 SetDlgItemTextW( hWnd, edt1, pstr );
408 HeapFree(GetProcessHeap(), 0, pstr);
410 if (lfs->hook)
412 FD31_CallWindowProc(lfs, lfs->lbselchstring, lst1,
413 MAKELONG(lRet,CD_LBSELCHANGE));
415 /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD,
416 CD_LBSELNOITEMS */
417 return TRUE;
420 /***********************************************************************
421 * FD31_TestPath [internal]
422 * before accepting the file name, test if it includes wild cards
423 * tries to scan the directory and returns TRUE if no error.
425 static LRESULT FD31_TestPath( const FD31_DATA *lfs, LPWSTR path )
427 HWND hWnd = lfs->hwnd;
428 LPWSTR pBeginFileName, pstr2;
429 WCHAR tmpstr2[BUFFILE];
431 pBeginFileName = strrchrW(path, '\\');
432 if (pBeginFileName == NULL)
433 pBeginFileName = strrchrW(path, ':');
435 if (strchrW(path,'*') != NULL || strchrW(path,'?') != NULL)
437 /* edit control contains wildcards */
438 if (pBeginFileName != NULL)
440 lstrcpynW(tmpstr2, pBeginFileName + 1, BUFFILE);
441 *(pBeginFileName + 1) = 0;
443 else
445 strcpyW(tmpstr2, path);
446 if(!(lfs->ofnW->Flags & OFN_NOVALIDATE))
447 *path = 0;
450 TRACE("path=%s, tmpstr2=%s\n", debugstr_w(path), debugstr_w(tmpstr2));
451 SetDlgItemTextW( hWnd, edt1, tmpstr2 );
452 FD31_ScanDir(hWnd, path);
453 return (lfs->ofnW->Flags & OFN_NOVALIDATE) ? TRUE : FALSE;
456 /* no wildcards, we might have a directory or a filename */
457 /* try appending a wildcard and reading the directory */
459 pstr2 = path + lstrlenW(path);
460 if (pBeginFileName == NULL || *(pBeginFileName + 1) != 0)
461 strcatW(path, FILE_bslash);
463 /* if ScanDir succeeds, we have changed the directory */
464 if (FD31_ScanDir(hWnd, path))
465 return FALSE; /* and path is not a valid file name */
467 /* if not, this must be a filename */
469 *pstr2 = 0; /* remove the wildcard added before */
471 if (pBeginFileName != NULL)
473 /* strip off the pathname */
474 *pBeginFileName = 0;
475 SetDlgItemTextW( hWnd, edt1, pBeginFileName + 1 );
477 lstrcpynW(tmpstr2, pBeginFileName + 1, sizeof(tmpstr2)/sizeof(WCHAR) );
478 /* Should we MessageBox() if this fails? */
479 if (!FD31_ScanDir(hWnd, path))
481 return FALSE;
483 strcpyW(path, tmpstr2);
485 else
486 SetDlgItemTextW( hWnd, edt1, path );
487 return TRUE;
490 /***********************************************************************
491 * FD31_Validate [internal]
492 * called on: click Ok button, Enter in edit, DoubleClick in file list
494 static LRESULT FD31_Validate( const FD31_DATA *lfs, LPCWSTR path, UINT control, INT itemIndex,
495 BOOL internalUse )
497 LONG lRet;
498 HWND hWnd = lfs->hwnd;
499 OPENFILENAMEW ofnsav;
500 LPOPENFILENAMEW ofnW = lfs->ofnW;
501 WCHAR filename[BUFFILE];
503 ofnsav = *ofnW; /* for later restoring */
505 /* get current file name */
506 if (path)
507 lstrcpynW(filename, path, sizeof(filename)/sizeof(WCHAR));
508 else
509 GetDlgItemTextW( hWnd, edt1, filename, sizeof(filename)/sizeof(WCHAR));
511 TRACE("got filename = %s\n", debugstr_w(filename));
512 /* if we did not click in file list to get there */
513 if (control != lst1)
515 if (!FD31_TestPath( lfs, filename) )
516 return FALSE;
518 FD31_UpdateResult(lfs, filename);
520 if (internalUse)
521 { /* called internally after a change in a combo */
522 if (lfs->hook)
524 FD31_CallWindowProc(lfs, lfs->lbselchstring, control,
525 MAKELONG(itemIndex,CD_LBSELCHANGE));
527 return TRUE;
530 FD31_UpdateFileTitle(lfs);
531 if (lfs->hook)
533 lRet = FD31_CallWindowProc(lfs, lfs->fileokstring,
534 0, lfs->lParam );
535 if (lRet)
537 *ofnW = ofnsav; /* restore old state */
538 return FALSE;
541 if ((ofnW->Flags & OFN_ALLOWMULTISELECT) && (ofnW->Flags & OFN_EXPLORER))
543 if (ofnW->lpstrFile)
545 LPWSTR str = ofnW->lpstrFile;
546 LPWSTR ptr = strrchrW(str, '\\');
547 str[lstrlenW(str) + 1] = '\0';
548 *ptr = 0;
551 return TRUE;
554 /***********************************************************************
555 * FD31_DiskChange [internal]
556 * called when a new item is picked in the disk selection combo
558 static LRESULT FD31_DiskChange( const FD31_DATA *lfs )
560 LONG lRet;
561 HWND hWnd = lfs->hwnd;
562 LPWSTR pstr;
563 WCHAR diskname[BUFFILE];
565 FD31_StripEditControl(hWnd);
566 lRet = SendDlgItemMessageW(hWnd, cmb2, CB_GETCURSEL, 0, 0L);
567 if (lRet == LB_ERR)
568 return 0;
569 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
570 SendDlgItemMessageW(hWnd, cmb2, CB_GETLBTEXT, lRet,
571 (LPARAM)pstr);
572 wsprintfW(diskname, FILE_specc, pstr[2]);
573 HeapFree(GetProcessHeap(), 0, pstr);
575 return FD31_Validate( lfs, diskname, cmb2, lRet, TRUE );
578 /***********************************************************************
579 * FD31_FileTypeChange [internal]
580 * called when a new item is picked in the file type combo
582 static LRESULT FD31_FileTypeChange( const FD31_DATA *lfs )
584 LONG lRet;
585 LPWSTR pstr;
587 lRet = SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETCURSEL, 0, 0);
588 if (lRet == LB_ERR)
589 return TRUE;
590 pstr = (LPWSTR)SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETITEMDATA, lRet, 0);
591 TRACE("Selected filter : %s\n", debugstr_w(pstr));
592 SetDlgItemTextW( lfs->hwnd, edt1, pstr );
594 return FD31_Validate( lfs, NULL, cmb1, lRet, TRUE );
597 /***********************************************************************
598 * FD31_WMCommand [internal]
600 LRESULT FD31_WMCommand(HWND hWnd, LPARAM lParam, UINT notification,
601 UINT control, const FD31_DATA *lfs )
603 switch (control)
605 case lst1: /* file list */
606 FD31_StripEditControl(hWnd);
607 if (notification == LBN_DBLCLK)
609 return SendMessageW(hWnd, WM_COMMAND, IDOK, 0);
611 else if (notification == LBN_SELCHANGE)
612 return FD31_FileListSelect( lfs );
613 break;
615 case lst2: /* directory list */
616 FD31_StripEditControl(hWnd);
617 if (notification == LBN_DBLCLK)
618 return FD31_DirListDblClick( lfs );
619 break;
621 case cmb1: /* file type drop list */
622 if (notification == CBN_SELCHANGE)
623 return FD31_FileTypeChange( lfs );
624 break;
626 case chx1:
627 break;
629 case pshHelp:
630 break;
632 case cmb2: /* disk dropdown combo */
633 if (notification == CBN_SELCHANGE)
634 return FD31_DiskChange( lfs );
635 break;
637 case IDOK:
638 TRACE("OK pressed\n");
639 if (FD31_Validate( lfs, NULL, control, 0, FALSE ))
640 EndDialog(hWnd, TRUE);
641 return TRUE;
643 case IDCANCEL:
644 EndDialog(hWnd, FALSE);
645 return TRUE;
647 case IDABORT: /* can be sent by the hook procedure */
648 EndDialog(hWnd, TRUE);
649 return TRUE;
651 return FALSE;
654 /************************************************************************
655 * FD31_MapStringPairsToW [internal]
656 * map string pairs to Unicode
658 static LPWSTR FD31_MapStringPairsToW(LPCSTR strA, UINT size)
660 LPCSTR s;
661 LPWSTR x;
662 unsigned int n, len;
664 s = strA;
665 while (*s)
666 s = s+strlen(s)+1;
667 s++;
668 n = s + 1 - strA; /* Don't forget the other \0 */
669 if (n < size) n = size;
671 len = MultiByteToWideChar( CP_ACP, 0, strA, n, NULL, 0 );
672 x = HeapAlloc(GetProcessHeap(),0, len * sizeof(WCHAR));
673 MultiByteToWideChar( CP_ACP, 0, strA, n, x, len );
674 return x;
678 /************************************************************************
679 * FD31_DupToW [internal]
680 * duplicates an Ansi string to unicode, with a buffer size
682 static LPWSTR FD31_DupToW(LPCSTR str, DWORD size)
684 LPWSTR strW = NULL;
685 if (str && (size > 0))
687 strW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
688 if (strW) MultiByteToWideChar( CP_ACP, 0, str, -1, strW, size );
690 return strW;
693 /************************************************************************
694 * FD31_MapOfnStructA [internal]
695 * map a 32 bits Ansi structure to an Unicode one
697 void FD31_MapOfnStructA(const OPENFILENAMEA *ofnA, LPOPENFILENAMEW ofnW, BOOL open)
699 UNICODE_STRING usBuffer;
701 ofnW->lStructSize = sizeof(OPENFILENAMEW);
702 ofnW->hwndOwner = ofnA->hwndOwner;
703 ofnW->hInstance = ofnA->hInstance;
704 if (ofnA->lpstrFilter)
705 ofnW->lpstrFilter = FD31_MapStringPairsToW(ofnA->lpstrFilter, 0);
707 if ((ofnA->lpstrCustomFilter) && (*(ofnA->lpstrCustomFilter)))
708 ofnW->lpstrCustomFilter = FD31_MapStringPairsToW(ofnA->lpstrCustomFilter, ofnA->nMaxCustFilter);
709 ofnW->nMaxCustFilter = ofnA->nMaxCustFilter;
710 ofnW->nFilterIndex = ofnA->nFilterIndex;
711 ofnW->nMaxFile = ofnA->nMaxFile;
712 ofnW->lpstrFile = FD31_DupToW(ofnA->lpstrFile, ofnW->nMaxFile);
713 ofnW->nMaxFileTitle = ofnA->nMaxFileTitle;
714 ofnW->lpstrFileTitle = FD31_DupToW(ofnA->lpstrFileTitle, ofnW->nMaxFileTitle);
715 if (ofnA->lpstrInitialDir)
717 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpstrInitialDir);
718 ofnW->lpstrInitialDir = usBuffer.Buffer;
720 if (ofnA->lpstrTitle) {
721 RtlCreateUnicodeStringFromAsciiz (&usBuffer, ofnA->lpstrTitle);
722 ofnW->lpstrTitle = usBuffer.Buffer;
723 } else {
724 WCHAR buf[16];
725 LPWSTR title_tmp;
726 int len;
727 LoadStringW(COMDLG32_hInstance, open ? IDS_OPEN_FILE : IDS_SAVE_AS,
728 buf, sizeof(buf)/sizeof(WCHAR));
729 len = lstrlenW(buf)+1;
730 title_tmp = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
731 memcpy(title_tmp, buf, len * sizeof(WCHAR));
732 ofnW->lpstrTitle = title_tmp;
734 ofnW->Flags = ofnA->Flags;
735 ofnW->nFileOffset = ofnA->nFileOffset;
736 ofnW->nFileExtension = ofnA->nFileExtension;
737 ofnW->lpstrDefExt = FD31_DupToW(ofnA->lpstrDefExt, 3);
738 if ((ofnA->Flags & OFN_ENABLETEMPLATE) && (ofnA->lpTemplateName))
740 if (HIWORD(ofnA->lpTemplateName))
742 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpTemplateName);
743 ofnW->lpTemplateName = usBuffer.Buffer;
745 else /* numbered resource */
746 ofnW->lpTemplateName = (LPCWSTR) ofnA->lpTemplateName;
751 /************************************************************************
752 * FD31_FreeOfnW [internal]
753 * Undo all allocations done by FD31_MapOfnStructA
755 void FD31_FreeOfnW(OPENFILENAMEW *ofnW)
757 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrFilter);
758 HeapFree(GetProcessHeap(), 0, ofnW->lpstrCustomFilter);
759 HeapFree(GetProcessHeap(), 0, ofnW->lpstrFile);
760 HeapFree(GetProcessHeap(), 0, ofnW->lpstrFileTitle);
761 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrInitialDir);
762 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrTitle);
763 if (HIWORD(ofnW->lpTemplateName))
764 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpTemplateName);
767 /************************************************************************
768 * FD31_DestroyPrivate [internal]
769 * destroys the private object
771 void FD31_DestroyPrivate(PFD31_DATA lfs)
773 HWND hwnd;
774 if (!lfs) return;
775 hwnd = lfs->hwnd;
776 TRACE("destroying private allocation %p\n", lfs);
777 lfs->callbacks->Destroy(lfs);
778 HeapFree(GetProcessHeap(), 0, lfs);
779 RemovePropA(hwnd, FD31_OFN_PROP);
782 /************************************************************************
783 * FD31_AllocPrivate [internal]
784 * allocate a private object to hold 32 bits Unicode
785 * structure that will be used throughout the calls, while
786 * keeping available the original structures and a few variables
787 * On entry : type = dialog procedure type (16,32A,32W)
788 * dlgType = dialog type (open or save)
790 PFD31_DATA FD31_AllocPrivate(LPARAM lParam, UINT dlgType,
791 PFD31_CALLBACKS callbacks, DWORD data)
793 PFD31_DATA lfs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FD31_DATA));
795 TRACE("alloc private buf %p\n", lfs);
796 if (!lfs) return NULL;
797 lfs->hook = FALSE;
798 lfs->lParam = lParam;
799 lfs->open = (dlgType == OPEN_DIALOG);
800 lfs->callbacks = callbacks;
801 if (! lfs->callbacks->Init(lParam, lfs, data))
803 FD31_DestroyPrivate(lfs);
804 return NULL;
806 lfs->lbselchstring = RegisterWindowMessageA(LBSELCHSTRINGA);
807 lfs->fileokstring = RegisterWindowMessageA(FILEOKSTRINGA);
809 return lfs;
812 /***********************************************************************
813 * FD31_WMInitDialog [internal]
816 LONG FD31_WMInitDialog(HWND hWnd, WPARAM wParam, LPARAM lParam)
818 int i, n;
819 WCHAR tmpstr[BUFFILE];
820 LPWSTR pstr, old_pstr;
821 LPOPENFILENAMEW ofn;
822 PFD31_DATA lfs = (PFD31_DATA) lParam;
824 if (!lfs) return FALSE;
825 SetPropA(hWnd, FD31_OFN_PROP, (HANDLE)lfs);
826 lfs->hwnd = hWnd;
827 ofn = lfs->ofnW;
829 TRACE("flags=%x initialdir=%s\n", ofn->Flags, debugstr_w(ofn->lpstrInitialDir));
831 SetWindowTextW( hWnd, ofn->lpstrTitle );
832 /* read custom filter information */
833 if (ofn->lpstrCustomFilter)
835 pstr = ofn->lpstrCustomFilter;
836 n = 0;
837 TRACE("lpstrCustomFilter = %p\n", pstr);
838 while(*pstr)
840 old_pstr = pstr;
841 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
842 (LPARAM)(ofn->lpstrCustomFilter) + n );
843 n += lstrlenW(pstr) + 1;
844 pstr += lstrlenW(pstr) + 1;
845 TRACE("add str=%s associated to %s\n",
846 debugstr_w(old_pstr), debugstr_w(pstr));
847 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
848 n += lstrlenW(pstr) + 1;
849 pstr += lstrlenW(pstr) + 1;
852 /* read filter information */
853 if (ofn->lpstrFilter) {
854 pstr = (LPWSTR) ofn->lpstrFilter;
855 n = 0;
856 while(*pstr) {
857 old_pstr = pstr;
858 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
859 (LPARAM)(ofn->lpstrFilter + n) );
860 n += lstrlenW(pstr) + 1;
861 pstr += lstrlenW(pstr) + 1;
862 TRACE("add str=%s associated to %s\n",
863 debugstr_w(old_pstr), debugstr_w(pstr));
864 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
865 n += lstrlenW(pstr) + 1;
866 pstr += lstrlenW(pstr) + 1;
869 /* set default filter */
870 if (ofn->nFilterIndex == 0 && ofn->lpstrCustomFilter == NULL)
871 ofn->nFilterIndex = 1;
872 SendDlgItemMessageW(hWnd, cmb1, CB_SETCURSEL, ofn->nFilterIndex - 1, 0);
873 lstrcpynW(tmpstr, FD31_GetFileType(ofn->lpstrCustomFilter,
874 ofn->lpstrFilter, ofn->nFilterIndex - 1),BUFFILE);
875 TRACE("nFilterIndex = %d, SetText of edt1 to %s\n",
876 ofn->nFilterIndex, debugstr_w(tmpstr));
877 SetDlgItemTextW( hWnd, edt1, tmpstr );
878 /* get drive list */
879 *tmpstr = 0;
880 DlgDirListComboBoxW(hWnd, tmpstr, cmb2, 0, DDL_DRIVES | DDL_EXCLUSIVE);
881 /* read initial directory */
882 /* FIXME: Note that this is now very version-specific (See MSDN description of
883 * the OPENFILENAME structure). For example under 2000/XP any path in the
884 * lpstrFile overrides the lpstrInitialDir, but not under 95/98/ME
886 if (ofn->lpstrInitialDir != NULL)
888 int len;
889 lstrcpynW(tmpstr, ofn->lpstrInitialDir, 511);
890 len = lstrlenW(tmpstr);
891 if (len > 0 && tmpstr[len-1] != '\\' && tmpstr[len-1] != ':') {
892 tmpstr[len]='\\';
893 tmpstr[len+1]='\0';
896 else
897 *tmpstr = 0;
898 if (!FD31_ScanDir(hWnd, tmpstr)) {
899 *tmpstr = 0;
900 if (!FD31_ScanDir(hWnd, tmpstr))
901 WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr));
903 /* select current drive in combo 2, omit missing drives */
905 char dir[MAX_PATH];
906 char str[4] = "a:\\";
907 GetCurrentDirectoryA( sizeof(dir), dir );
908 for(i = 0, n = -1; i < 26; i++)
910 str[0] = 'a' + i;
911 if (GetDriveTypeA(str) > DRIVE_NO_ROOT_DIR) n++;
912 if (toupper(str[0]) == toupper(dir[0])) break;
915 SendDlgItemMessageW(hWnd, cmb2, CB_SETCURSEL, n, 0);
916 if (!(ofn->Flags & OFN_SHOWHELP))
917 ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
918 if (ofn->Flags & OFN_HIDEREADONLY)
919 ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
920 if (lfs->hook)
921 return FD31_CallWindowProc(lfs, WM_INITDIALOG, wParam, lfs->lParam);
922 return TRUE;
925 int FD31_GetFldrHeight(void)
927 return fldrHeight;