mmdevapi: Fix small capture bugs.
[wine/multimedia.git] / dlls / comdlg32 / filedlg31.c
blob29c63106975ee4223cc79fee2b5aac461398695d
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 BOOL ret;
114 if (lfs->ofnA)
116 TRACE("Call hookA %p (%p, %04x, %08lx, %08lx)\n",
117 lfs->ofnA->lpfnHook, lfs->hwnd, wMsg, wParam, lParam);
118 ret = lfs->ofnA->lpfnHook(lfs->hwnd, wMsg, wParam, lParam);
119 TRACE("ret hookA %p (%p, %04x, %08lx, %08lx)\n",
120 lfs->ofnA->lpfnHook, lfs->hwnd, wMsg, wParam, lParam);
121 return ret;
124 TRACE("Call hookW %p (%p, %04x, %08lx, %08lx)\n",
125 lfs->ofnW->lpfnHook, lfs->hwnd, wMsg, wParam, lParam);
126 ret = lfs->ofnW->lpfnHook(lfs->hwnd, wMsg, wParam, lParam);
127 TRACE("Ret hookW %p (%p, %04x, %08lx, %08lx)\n",
128 lfs->ofnW->lpfnHook, lfs->hwnd, wMsg, wParam, lParam);
129 return ret;
132 /***********************************************************************
133 * FD31_GetFileType [internal]
135 static LPCWSTR FD31_GetFileType(LPCWSTR cfptr, LPCWSTR fptr, const WORD index)
137 int n, i;
138 i = 0;
139 if (cfptr)
140 for ( ;(n = lstrlenW(cfptr)) != 0; i++)
142 cfptr += n + 1;
143 if (i == index)
144 return cfptr;
145 cfptr += lstrlenW(cfptr) + 1;
147 if (fptr)
148 for ( ;(n = lstrlenW(fptr)) != 0; i++)
150 fptr += n + 1;
151 if (i == index)
152 return fptr;
153 fptr += lstrlenW(fptr) + 1;
155 return FILE_star; /* FIXME */
158 /***********************************************************************
159 * FD31_ScanDir [internal]
161 static BOOL FD31_ScanDir(const OPENFILENAMEW *ofn, HWND hWnd, LPCWSTR newPath)
163 WCHAR buffer[BUFFILE];
164 HWND hdlg;
165 LRESULT lRet = TRUE;
166 HCURSOR hCursorWait, oldCursor;
168 TRACE("Trying to change to %s\n", debugstr_w(newPath));
169 if ( newPath[0] && !SetCurrentDirectoryW( newPath ))
170 return FALSE;
172 /* get the list of spec files */
173 lstrcpynW(buffer, FD31_GetFileType(ofn->lpstrCustomFilter,
174 ofn->lpstrFilter, ofn->nFilterIndex - 1), BUFFILE);
176 hCursorWait = LoadCursorA(0, (LPSTR)IDC_WAIT);
177 oldCursor = SetCursor(hCursorWait);
179 /* list of files */
180 if ((hdlg = GetDlgItem(hWnd, lst1)) != 0) {
181 WCHAR* scptr; /* ptr on semi-colon */
182 WCHAR* filter = buffer;
184 TRACE("Using filter %s\n", debugstr_w(filter));
185 SendMessageW(hdlg, LB_RESETCONTENT, 0, 0);
186 while (filter) {
187 scptr = strchrW(filter, ';');
188 if (scptr) *scptr = 0;
189 while (*filter == ' ') filter++;
190 TRACE("Using file spec %s\n", debugstr_w(filter));
191 SendMessageW(hdlg, LB_DIR, 0, (LPARAM)filter);
192 if (scptr) *scptr = ';';
193 filter = (scptr) ? (scptr + 1) : 0;
197 /* list of directories */
198 strcpyW(buffer, FILE_star);
200 if (GetDlgItem(hWnd, lst2) != 0) {
201 lRet = DlgDirListW(hWnd, buffer, lst2, stc1, DDL_EXCLUSIVE | DDL_DIRECTORY);
203 SetCursor(oldCursor);
204 return lRet;
207 /***********************************************************************
208 * FD31_WMDrawItem [internal]
210 LONG FD31_WMDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam,
211 int savedlg, const DRAWITEMSTRUCT *lpdis)
213 WCHAR *str;
214 HICON hIcon;
215 COLORREF oldText = 0, oldBk = 0;
217 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst1)
219 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC))) return FALSE;
220 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
221 (LPARAM)str);
223 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
225 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
226 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
228 if (savedlg)
229 SetTextColor(lpdis->hDC,GetSysColor(COLOR_GRAYTEXT) );
231 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + 1,
232 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
233 &(lpdis->rcItem), str, lstrlenW(str), NULL);
235 if (lpdis->itemState & ODS_SELECTED)
236 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
238 if ((lpdis->itemState & ODS_SELECTED) && !savedlg)
240 SetBkColor( lpdis->hDC, oldBk );
241 SetTextColor( lpdis->hDC, oldText );
243 HeapFree(GetProcessHeap(), 0, str);
244 return TRUE;
247 if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst2)
249 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
250 return FALSE;
251 SendMessageW(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID,
252 (LPARAM)str);
254 if (lpdis->itemState & ODS_SELECTED)
256 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
257 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
259 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
260 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
261 &(lpdis->rcItem), str, lstrlenW(str), NULL);
263 if (lpdis->itemState & ODS_SELECTED)
264 DrawFocusRect( lpdis->hDC, &(lpdis->rcItem) );
266 if (lpdis->itemState & ODS_SELECTED)
268 SetBkColor( lpdis->hDC, oldBk );
269 SetTextColor( lpdis->hDC, oldText );
271 DrawIconEx( lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hFolder, 16, 16, 0, 0, DI_NORMAL );
272 HeapFree(GetProcessHeap(), 0, str);
273 return TRUE;
275 if (lpdis->CtlType == ODT_COMBOBOX && lpdis->CtlID == cmb2)
277 char root[] = "a:";
278 if (!(str = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
279 return FALSE;
280 SendMessageW(lpdis->hwndItem, CB_GETLBTEXT, lpdis->itemID,
281 (LPARAM)str);
282 root[0] += str[2] - 'a';
283 switch(GetDriveTypeA(root))
285 case DRIVE_REMOVABLE: hIcon = hFloppy; break;
286 case DRIVE_CDROM: hIcon = hCDRom; break;
287 case DRIVE_REMOTE: hIcon = hNet; break;
288 case DRIVE_FIXED:
289 default: hIcon = hHDisk; break;
291 if (lpdis->itemState & ODS_SELECTED)
293 oldBk = SetBkColor( lpdis->hDC, GetSysColor( COLOR_HIGHLIGHT ) );
294 oldText = SetTextColor( lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
296 ExtTextOutW(lpdis->hDC, lpdis->rcItem.left + fldrWidth,
297 lpdis->rcItem.top + 1, ETO_OPAQUE | ETO_CLIPPED,
298 &(lpdis->rcItem), str, lstrlenW(str), NULL);
300 if (lpdis->itemState & ODS_SELECTED)
302 SetBkColor( lpdis->hDC, oldBk );
303 SetTextColor( lpdis->hDC, oldText );
305 DrawIconEx( lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hIcon, 16, 16, 0, 0, DI_NORMAL );
306 HeapFree(GetProcessHeap(), 0, str);
307 return TRUE;
309 return FALSE;
312 /***********************************************************************
313 * FD31_UpdateResult [internal]
314 * update the displayed file name (with path)
316 static void FD31_UpdateResult(const FD31_DATA *lfs, const WCHAR *tmpstr)
318 int lenstr2;
319 LPOPENFILENAMEW ofnW = lfs->ofnW;
320 LPOPENFILENAMEA ofnA = lfs->ofnA;
321 WCHAR tmpstr2[BUFFILE];
322 WCHAR *p;
324 TRACE("%s\n", debugstr_w(tmpstr));
325 if(ofnW->Flags & OFN_NOVALIDATE)
326 tmpstr2[0] = '\0';
327 else
328 GetCurrentDirectoryW(BUFFILE, tmpstr2);
329 lenstr2 = strlenW(tmpstr2);
330 if (lenstr2 > 3)
331 tmpstr2[lenstr2++]='\\';
332 lstrcpynW(tmpstr2+lenstr2, tmpstr, BUFFILE-lenstr2);
333 if (!ofnW->lpstrFile)
334 return;
336 lstrcpynW(ofnW->lpstrFile, tmpstr2, ofnW->nMaxFile);
338 /* set filename offset */
339 p = PathFindFileNameW(ofnW->lpstrFile);
340 ofnW->nFileOffset = (p - ofnW->lpstrFile);
342 /* set extension offset */
343 p = PathFindExtensionW(ofnW->lpstrFile);
344 ofnW->nFileExtension = (*p) ? (p - ofnW->lpstrFile) + 1 : 0;
346 TRACE("file %s, file offset %d, ext offset %d\n",
347 debugstr_w(ofnW->lpstrFile), ofnW->nFileOffset, ofnW->nFileExtension);
349 /* update the real client structures if any */
350 if (ofnA)
352 LPSTR lpszTemp;
353 if (ofnW->nMaxFile &&
354 !WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFile, -1,
355 ofnA->lpstrFile, ofnA->nMaxFile, NULL, NULL ))
356 ofnA->lpstrFile[ofnA->nMaxFile-1] = 0;
358 /* offsets are not guaranteed to be the same in WCHAR to MULTIBYTE conversion */
359 /* set filename offset */
360 lpszTemp = PathFindFileNameA(ofnA->lpstrFile);
361 ofnA->nFileOffset = (lpszTemp - ofnA->lpstrFile);
363 /* set extension offset */
364 lpszTemp = PathFindExtensionA(ofnA->lpstrFile);
365 ofnA->nFileExtension = (*lpszTemp) ? (lpszTemp - ofnA->lpstrFile) + 1 : 0;
369 /***********************************************************************
370 * FD31_UpdateFileTitle [internal]
371 * update the displayed file name (without path)
373 static void FD31_UpdateFileTitle(const FD31_DATA *lfs)
375 LONG lRet;
376 LPOPENFILENAMEW ofnW = lfs->ofnW;
377 LPOPENFILENAMEA ofnA = lfs->ofnA;
379 if (ofnW->lpstrFileTitle != NULL)
381 lRet = SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
382 SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETTEXT, lRet,
383 (LPARAM)ofnW->lpstrFileTitle );
384 if (ofnA)
386 if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFileTitle, -1,
387 ofnA->lpstrFileTitle, ofnA->nMaxFileTitle, NULL, NULL ))
388 ofnA->lpstrFileTitle[ofnA->nMaxFileTitle-1] = 0;
393 /***********************************************************************
394 * FD31_DirListDblClick [internal]
396 static LRESULT FD31_DirListDblClick( const FD31_DATA *lfs )
398 LONG lRet;
399 HWND hWnd = lfs->hwnd;
400 LPWSTR pstr;
401 WCHAR tmpstr[BUFFILE];
403 /* get the raw string (with brackets) */
404 lRet = SendDlgItemMessageW(hWnd, lst2, LB_GETCURSEL, 0, 0);
405 if (lRet == LB_ERR) return TRUE;
406 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
407 SendDlgItemMessageW(hWnd, lst2, LB_GETTEXT, lRet,
408 (LPARAM)pstr);
409 strcpyW( tmpstr, pstr );
410 HeapFree(GetProcessHeap(), 0, pstr);
411 /* get the selected directory in tmpstr */
412 if (tmpstr[0] == '[')
414 tmpstr[lstrlenW(tmpstr) - 1] = 0;
415 strcpyW(tmpstr,tmpstr+1);
417 strcatW(tmpstr, FILE_bslash);
419 FD31_ScanDir(lfs->ofnW, hWnd, tmpstr);
420 /* notify the app */
421 if (lfs->hook)
423 if (FD31_CallWindowProc(lfs, lfs->lbselchstring, lst2,
424 MAKELONG(lRet,CD_LBSELCHANGE)))
425 return TRUE;
427 return TRUE;
430 /***********************************************************************
431 * FD31_FileListSelect [internal]
432 * called when a new item is picked in the file list
434 static LRESULT FD31_FileListSelect( const FD31_DATA *lfs )
436 LONG lRet;
437 HWND hWnd = lfs->hwnd;
438 LPWSTR pstr;
440 lRet = SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL, 0, 0);
441 if (lRet == LB_ERR)
442 return TRUE;
444 /* set the edit control to the choosen file */
445 if ((pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC)))
447 SendDlgItemMessageW(hWnd, lst1, LB_GETTEXT, lRet,
448 (LPARAM)pstr);
449 SetDlgItemTextW( hWnd, edt1, pstr );
450 HeapFree(GetProcessHeap(), 0, pstr);
452 if (lfs->hook)
454 FD31_CallWindowProc(lfs, lfs->lbselchstring, lst1,
455 MAKELONG(lRet,CD_LBSELCHANGE));
457 /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD,
458 CD_LBSELNOITEMS */
459 return TRUE;
462 /***********************************************************************
463 * FD31_TestPath [internal]
464 * before accepting the file name, test if it includes wild cards
465 * tries to scan the directory and returns TRUE if no error.
467 static LRESULT FD31_TestPath( const FD31_DATA *lfs, LPWSTR path )
469 HWND hWnd = lfs->hwnd;
470 LPWSTR pBeginFileName, pstr2;
471 WCHAR tmpstr2[BUFFILE];
473 pBeginFileName = strrchrW(path, '\\');
474 if (pBeginFileName == NULL)
475 pBeginFileName = strrchrW(path, ':');
477 if (strchrW(path,'*') != NULL || strchrW(path,'?') != NULL)
479 /* edit control contains wildcards */
480 if (pBeginFileName != NULL)
482 lstrcpynW(tmpstr2, pBeginFileName + 1, BUFFILE);
483 *(pBeginFileName + 1) = 0;
485 else
487 strcpyW(tmpstr2, path);
488 if(!(lfs->ofnW->Flags & OFN_NOVALIDATE))
489 *path = 0;
492 TRACE("path=%s, tmpstr2=%s\n", debugstr_w(path), debugstr_w(tmpstr2));
493 SetDlgItemTextW( hWnd, edt1, tmpstr2 );
494 FD31_ScanDir(lfs->ofnW, hWnd, path);
495 return (lfs->ofnW->Flags & OFN_NOVALIDATE) ? TRUE : FALSE;
498 /* no wildcards, we might have a directory or a filename */
499 /* try appending a wildcard and reading the directory */
501 pstr2 = path + lstrlenW(path);
502 if (pBeginFileName == NULL || *(pBeginFileName + 1) != 0)
503 strcatW(path, FILE_bslash);
505 /* if ScanDir succeeds, we have changed the directory */
506 if (FD31_ScanDir(lfs->ofnW, hWnd, path))
507 return FALSE; /* and path is not a valid file name */
509 /* if not, this must be a filename */
511 *pstr2 = 0; /* remove the wildcard added before */
513 if (pBeginFileName != NULL)
515 /* strip off the pathname */
516 *pBeginFileName = 0;
517 SetDlgItemTextW( hWnd, edt1, pBeginFileName + 1 );
519 lstrcpynW(tmpstr2, pBeginFileName + 1, sizeof(tmpstr2)/sizeof(WCHAR) );
520 /* Should we MessageBox() if this fails? */
521 if (!FD31_ScanDir(lfs->ofnW, hWnd, path))
523 return FALSE;
525 strcpyW(path, tmpstr2);
527 else
528 SetDlgItemTextW( hWnd, edt1, path );
529 return TRUE;
532 /***********************************************************************
533 * FD31_Validate [internal]
534 * called on: click Ok button, Enter in edit, DoubleClick in file list
536 static LRESULT FD31_Validate( const FD31_DATA *lfs, LPCWSTR path, UINT control, INT itemIndex,
537 BOOL internalUse )
539 LONG lRet;
540 HWND hWnd = lfs->hwnd;
541 OPENFILENAMEW ofnsav;
542 LPOPENFILENAMEW ofnW = lfs->ofnW;
543 WCHAR filename[BUFFILE];
545 ofnsav = *ofnW; /* for later restoring */
547 /* get current file name */
548 if (path)
549 lstrcpynW(filename, path, sizeof(filename)/sizeof(WCHAR));
550 else
551 GetDlgItemTextW( hWnd, edt1, filename, sizeof(filename)/sizeof(WCHAR));
553 TRACE("got filename = %s\n", debugstr_w(filename));
554 /* if we did not click in file list to get there */
555 if (control != lst1)
557 if (!FD31_TestPath( lfs, filename) )
558 return FALSE;
560 FD31_UpdateResult(lfs, filename);
562 if (internalUse)
563 { /* called internally after a change in a combo */
564 if (lfs->hook)
566 FD31_CallWindowProc(lfs, lfs->lbselchstring, control,
567 MAKELONG(itemIndex,CD_LBSELCHANGE));
569 return TRUE;
572 FD31_UpdateFileTitle(lfs);
573 if (lfs->hook)
575 lRet = FD31_CallWindowProc(lfs, lfs->fileokstring,
576 0, lfs->lParam );
577 if (lRet)
579 *ofnW = ofnsav; /* restore old state */
580 return FALSE;
583 if ((ofnW->Flags & OFN_ALLOWMULTISELECT) && (ofnW->Flags & OFN_EXPLORER))
585 if (ofnW->lpstrFile)
587 LPWSTR str = ofnW->lpstrFile;
588 LPWSTR ptr = strrchrW(str, '\\');
589 str[lstrlenW(str) + 1] = '\0';
590 *ptr = 0;
593 return TRUE;
596 /***********************************************************************
597 * FD31_DiskChange [internal]
598 * called when a new item is picked in the disk selection combo
600 static LRESULT FD31_DiskChange( const FD31_DATA *lfs )
602 LONG lRet;
603 HWND hWnd = lfs->hwnd;
604 LPWSTR pstr;
605 WCHAR diskname[BUFFILE];
607 FD31_StripEditControl(hWnd);
608 lRet = SendDlgItemMessageW(hWnd, cmb2, CB_GETCURSEL, 0, 0L);
609 if (lRet == LB_ERR)
610 return 0;
611 pstr = HeapAlloc(GetProcessHeap(), 0, BUFFILEALLOC);
612 SendDlgItemMessageW(hWnd, cmb2, CB_GETLBTEXT, lRet,
613 (LPARAM)pstr);
614 wsprintfW(diskname, FILE_specc, pstr[2]);
615 HeapFree(GetProcessHeap(), 0, pstr);
617 return FD31_Validate( lfs, diskname, cmb2, lRet, TRUE );
620 /***********************************************************************
621 * FD31_FileTypeChange [internal]
622 * called when a new item is picked in the file type combo
624 static LRESULT FD31_FileTypeChange( const FD31_DATA *lfs )
626 LONG lRet;
627 LPWSTR pstr;
629 lRet = SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETCURSEL, 0, 0);
630 if (lRet == LB_ERR)
631 return TRUE;
632 pstr = (LPWSTR)SendDlgItemMessageW(lfs->hwnd, cmb1, CB_GETITEMDATA, lRet, 0);
633 TRACE("Selected filter : %s\n", debugstr_w(pstr));
635 return FD31_Validate( lfs, NULL, cmb1, lRet, TRUE );
638 /***********************************************************************
639 * FD31_WMCommand [internal]
641 LRESULT FD31_WMCommand(HWND hWnd, LPARAM lParam, UINT notification,
642 UINT control, const FD31_DATA *lfs )
644 switch (control)
646 case lst1: /* file list */
647 FD31_StripEditControl(hWnd);
648 if (notification == LBN_DBLCLK)
650 return SendMessageW(hWnd, WM_COMMAND, IDOK, 0);
652 else if (notification == LBN_SELCHANGE)
653 return FD31_FileListSelect( lfs );
654 break;
656 case lst2: /* directory list */
657 FD31_StripEditControl(hWnd);
658 if (notification == LBN_DBLCLK)
659 return FD31_DirListDblClick( lfs );
660 break;
662 case cmb1: /* file type drop list */
663 if (notification == CBN_SELCHANGE)
664 return FD31_FileTypeChange( lfs );
665 break;
667 case chx1:
668 break;
670 case pshHelp:
671 break;
673 case cmb2: /* disk dropdown combo */
674 if (notification == CBN_SELCHANGE)
675 return FD31_DiskChange( lfs );
676 break;
678 case IDOK:
679 TRACE("OK pressed\n");
680 if (FD31_Validate( lfs, NULL, control, 0, FALSE ))
681 EndDialog(hWnd, TRUE);
682 return TRUE;
684 case IDCANCEL:
685 EndDialog(hWnd, FALSE);
686 return TRUE;
688 case IDABORT: /* can be sent by the hook procedure */
689 EndDialog(hWnd, TRUE);
690 return TRUE;
692 return FALSE;
695 /************************************************************************
696 * FD31_MapStringPairsToW [internal]
697 * map string pairs to Unicode
699 static LPWSTR FD31_MapStringPairsToW(LPCSTR strA, UINT size)
701 LPCSTR s;
702 LPWSTR x;
703 unsigned int n, len;
705 s = strA;
706 while (*s)
707 s = s+strlen(s)+1;
708 s++;
709 n = s + 1 - strA; /* Don't forget the other \0 */
710 if (n < size) n = size;
712 len = MultiByteToWideChar( CP_ACP, 0, strA, n, NULL, 0 );
713 x = HeapAlloc(GetProcessHeap(),0, len * sizeof(WCHAR));
714 MultiByteToWideChar( CP_ACP, 0, strA, n, x, len );
715 return x;
719 /************************************************************************
720 * FD31_DupToW [internal]
721 * duplicates an Ansi string to unicode, with a buffer size
723 static LPWSTR FD31_DupToW(LPCSTR str, DWORD size)
725 LPWSTR strW = NULL;
726 if (str && (size > 0))
728 strW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
729 if (strW) MultiByteToWideChar( CP_ACP, 0, str, -1, strW, size );
731 return strW;
734 /************************************************************************
735 * FD31_MapOfnStructA [internal]
736 * map a 32 bits Ansi structure to a Unicode one
738 void FD31_MapOfnStructA(const OPENFILENAMEA *ofnA, LPOPENFILENAMEW ofnW, BOOL open)
740 UNICODE_STRING usBuffer;
742 ofnW->lStructSize = sizeof(OPENFILENAMEW);
743 ofnW->hwndOwner = ofnA->hwndOwner;
744 ofnW->hInstance = ofnA->hInstance;
745 if (ofnA->lpstrFilter)
746 ofnW->lpstrFilter = FD31_MapStringPairsToW(ofnA->lpstrFilter, 0);
748 if ((ofnA->lpstrCustomFilter) && (*(ofnA->lpstrCustomFilter)))
749 ofnW->lpstrCustomFilter = FD31_MapStringPairsToW(ofnA->lpstrCustomFilter, ofnA->nMaxCustFilter);
750 ofnW->nMaxCustFilter = ofnA->nMaxCustFilter;
751 ofnW->nFilterIndex = ofnA->nFilterIndex;
752 ofnW->nMaxFile = ofnA->nMaxFile;
753 ofnW->lpstrFile = FD31_DupToW(ofnA->lpstrFile, ofnW->nMaxFile);
754 ofnW->nMaxFileTitle = ofnA->nMaxFileTitle;
755 ofnW->lpstrFileTitle = FD31_DupToW(ofnA->lpstrFileTitle, ofnW->nMaxFileTitle);
756 if (ofnA->lpstrInitialDir)
758 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpstrInitialDir);
759 ofnW->lpstrInitialDir = usBuffer.Buffer;
761 if (ofnA->lpstrTitle) {
762 RtlCreateUnicodeStringFromAsciiz (&usBuffer, ofnA->lpstrTitle);
763 ofnW->lpstrTitle = usBuffer.Buffer;
764 } else {
765 WCHAR buf[16];
766 LPWSTR title_tmp;
767 int len;
768 LoadStringW(COMDLG32_hInstance, open ? IDS_OPEN_FILE : IDS_SAVE_AS,
769 buf, sizeof(buf)/sizeof(WCHAR));
770 len = lstrlenW(buf)+1;
771 title_tmp = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
772 memcpy(title_tmp, buf, len * sizeof(WCHAR));
773 ofnW->lpstrTitle = title_tmp;
775 ofnW->Flags = ofnA->Flags;
776 ofnW->nFileOffset = ofnA->nFileOffset;
777 ofnW->nFileExtension = ofnA->nFileExtension;
778 ofnW->lpstrDefExt = FD31_DupToW(ofnA->lpstrDefExt, 3);
779 if ((ofnA->Flags & OFN_ENABLETEMPLATE) && (ofnA->lpTemplateName))
781 if (!IS_INTRESOURCE(ofnA->lpTemplateName))
783 RtlCreateUnicodeStringFromAsciiz (&usBuffer,ofnA->lpTemplateName);
784 ofnW->lpTemplateName = usBuffer.Buffer;
786 else /* numbered resource */
787 ofnW->lpTemplateName = (LPCWSTR) ofnA->lpTemplateName;
792 /************************************************************************
793 * FD31_FreeOfnW [internal]
794 * Undo all allocations done by FD31_MapOfnStructA
796 void FD31_FreeOfnW(OPENFILENAMEW *ofnW)
798 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrFilter);
799 HeapFree(GetProcessHeap(), 0, ofnW->lpstrCustomFilter);
800 HeapFree(GetProcessHeap(), 0, ofnW->lpstrFile);
801 HeapFree(GetProcessHeap(), 0, ofnW->lpstrFileTitle);
802 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrInitialDir);
803 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpstrTitle);
804 if (!IS_INTRESOURCE(ofnW->lpTemplateName))
805 HeapFree(GetProcessHeap(), 0, (LPWSTR) ofnW->lpTemplateName);
808 /************************************************************************
809 * FD31_DestroyPrivate [internal]
810 * destroys the private object
812 void FD31_DestroyPrivate(PFD31_DATA lfs)
814 HWND hwnd;
815 if (!lfs) return;
816 hwnd = lfs->hwnd;
817 TRACE("destroying private allocation %p\n", lfs);
819 /* if ofnW has been allocated, have to free everything in it */
820 if (lfs->ofnA)
822 FD31_FreeOfnW(lfs->ofnW);
823 HeapFree(GetProcessHeap(), 0, lfs->ofnW);
825 HeapFree(GetProcessHeap(), 0, lfs);
826 RemovePropA(hwnd, FD31_OFN_PROP);
829 /************************************************************************
830 * FD31_AllocPrivate [internal]
831 * allocate a private object to hold 32 bits Unicode
832 * structure that will be used throughout the calls, while
833 * keeping available the original structures and a few variables
834 * On entry : type = dialog procedure type (16,32A,32W)
835 * dlgType = dialog type (open or save)
837 PFD31_DATA FD31_AllocPrivate(LPARAM lParam, UINT dlgType, BOOL IsUnicode)
839 PFD31_DATA lfs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FD31_DATA));
841 TRACE("alloc private buf %p\n", lfs);
842 if (!lfs) return NULL;
843 lfs->hook = FALSE;
844 lfs->lParam = lParam;
845 lfs->open = (dlgType == OPEN_DIALOG);
847 if (IsUnicode)
849 lfs->ofnA = NULL;
850 lfs->ofnW = (LPOPENFILENAMEW) lParam;
851 if (lfs->ofnW->Flags & OFN_ENABLEHOOK)
852 if (lfs->ofnW->lpfnHook)
853 lfs->hook = TRUE;
855 else
857 lfs->ofnA = (LPOPENFILENAMEA) lParam;
858 if (lfs->ofnA->Flags & OFN_ENABLEHOOK)
859 if (lfs->ofnA->lpfnHook)
860 lfs->hook = TRUE;
861 lfs->ofnW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*lfs->ofnW));
862 FD31_MapOfnStructA(lfs->ofnA, lfs->ofnW, lfs->open);
865 if (! FD32_GetTemplate(lfs))
867 FD31_DestroyPrivate(lfs);
868 return NULL;
870 lfs->lbselchstring = RegisterWindowMessageA(LBSELCHSTRINGA);
871 lfs->fileokstring = RegisterWindowMessageA(FILEOKSTRINGA);
873 return lfs;
876 /***********************************************************************
877 * FD31_WMInitDialog [internal]
880 LONG FD31_WMInitDialog(HWND hWnd, WPARAM wParam, LPARAM lParam)
882 int i, n;
883 WCHAR tmpstr[BUFFILE];
884 LPWSTR pstr, old_pstr;
885 LPOPENFILENAMEW ofn;
886 PFD31_DATA lfs = (PFD31_DATA) lParam;
888 if (!lfs) return FALSE;
889 SetPropA(hWnd, FD31_OFN_PROP, lfs);
890 lfs->hwnd = hWnd;
891 ofn = lfs->ofnW;
893 TRACE("flags=%x initialdir=%s\n", ofn->Flags, debugstr_w(ofn->lpstrInitialDir));
895 SetWindowTextW( hWnd, ofn->lpstrTitle );
896 /* read custom filter information */
897 if (ofn->lpstrCustomFilter)
899 pstr = ofn->lpstrCustomFilter;
900 n = 0;
901 TRACE("lpstrCustomFilter = %p\n", pstr);
902 while(*pstr)
904 old_pstr = pstr;
905 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
906 (LPARAM)(ofn->lpstrCustomFilter) + n );
907 n += lstrlenW(pstr) + 1;
908 pstr += lstrlenW(pstr) + 1;
909 TRACE("add str=%s associated to %s\n",
910 debugstr_w(old_pstr), debugstr_w(pstr));
911 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
912 n += lstrlenW(pstr) + 1;
913 pstr += lstrlenW(pstr) + 1;
916 /* read filter information */
917 if (ofn->lpstrFilter) {
918 pstr = (LPWSTR) ofn->lpstrFilter;
919 n = 0;
920 while(*pstr) {
921 old_pstr = pstr;
922 i = SendDlgItemMessageW(hWnd, cmb1, CB_ADDSTRING, 0,
923 (LPARAM)(ofn->lpstrFilter + n) );
924 n += lstrlenW(pstr) + 1;
925 pstr += lstrlenW(pstr) + 1;
926 TRACE("add str=%s associated to %s\n",
927 debugstr_w(old_pstr), debugstr_w(pstr));
928 SendDlgItemMessageW(hWnd, cmb1, CB_SETITEMDATA, i, (LPARAM)pstr);
929 n += lstrlenW(pstr) + 1;
930 pstr += lstrlenW(pstr) + 1;
933 /* set default filter */
934 if (ofn->nFilterIndex == 0 && ofn->lpstrCustomFilter == NULL)
935 ofn->nFilterIndex = 1;
936 SendDlgItemMessageW(hWnd, cmb1, CB_SETCURSEL, ofn->nFilterIndex - 1, 0);
937 if (ofn->lpstrFile && ofn->lpstrFile[0])
939 TRACE( "SetText of edt1 to %s\n", debugstr_w(ofn->lpstrFile) );
940 SetDlgItemTextW( hWnd, edt1, ofn->lpstrFile );
942 else
944 lstrcpynW(tmpstr, FD31_GetFileType(ofn->lpstrCustomFilter,
945 ofn->lpstrFilter, ofn->nFilterIndex - 1),BUFFILE);
946 TRACE("nFilterIndex = %d, SetText of edt1 to %s\n",
947 ofn->nFilterIndex, debugstr_w(tmpstr));
948 SetDlgItemTextW( hWnd, edt1, tmpstr );
950 /* get drive list */
951 *tmpstr = 0;
952 DlgDirListComboBoxW(hWnd, tmpstr, cmb2, 0, DDL_DRIVES | DDL_EXCLUSIVE);
953 /* read initial directory */
954 /* FIXME: Note that this is now very version-specific (See MSDN description of
955 * the OPENFILENAME structure). For example under 2000/XP any path in the
956 * lpstrFile overrides the lpstrInitialDir, but not under 95/98/ME
958 if (ofn->lpstrInitialDir != NULL)
960 int len;
961 lstrcpynW(tmpstr, ofn->lpstrInitialDir, 511);
962 len = lstrlenW(tmpstr);
963 if (len > 0 && tmpstr[len-1] != '\\' && tmpstr[len-1] != ':') {
964 tmpstr[len]='\\';
965 tmpstr[len+1]='\0';
968 else
969 *tmpstr = 0;
970 if (!FD31_ScanDir(ofn, hWnd, tmpstr)) {
971 *tmpstr = 0;
972 if (!FD31_ScanDir(ofn, hWnd, tmpstr))
973 WARN("Couldn't read initial directory %s!\n", debugstr_w(tmpstr));
975 /* select current drive in combo 2, omit missing drives */
977 char dir[MAX_PATH];
978 char str[4] = "a:\\";
979 GetCurrentDirectoryA( sizeof(dir), dir );
980 for(i = 0, n = -1; i < 26; i++)
982 str[0] = 'a' + i;
983 if (GetDriveTypeA(str) > DRIVE_NO_ROOT_DIR) n++;
984 if (toupper(str[0]) == toupper(dir[0])) break;
987 SendDlgItemMessageW(hWnd, cmb2, CB_SETCURSEL, n, 0);
988 if (!(ofn->Flags & OFN_SHOWHELP))
989 ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
990 if (ofn->Flags & OFN_HIDEREADONLY)
991 ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
992 if (lfs->hook)
993 return FD31_CallWindowProc(lfs, WM_INITDIALOG, wParam, lfs->lParam);
994 return TRUE;
997 int FD31_GetFldrHeight(void)
999 return fldrHeight;