- Better implementation for GetTopindex/GetOrigin/SetIconSpacing.
[wine] / dlls / commdlg / filedlg95.c
1 /*
2  * COMMDLG - File Open Dialogs Win95 look and feel
3  *
4  * Copyright 1999 Francois Boisvert
5  * Copyright 1999, 2000 Juergen Schmied
6  *
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.
11  *
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.
16  *
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
20  *
21  * FIXME: The whole concept of handling unicode is badly broken.
22  *      many hook-messages expecting a pointer to a
23  *      OPENFILENAMEA or W structure. With the current architecture
24  *      we would have to convert the beast at every call to a hook.
25  *      we have to find a better solution but if would likely cause
26  *      a complete rewrite with after we shouldhandle the
27  *      OPENFILENAME structure without any converting (jsch).
28  *
29  * FIXME: any hook gets a OPENFILENAMEA structure
30  *
31  * FIXME: CDN_FILEOK is wrong implemented, other CDN_ messages likely too
32  *
33  * FIXME: old style hook messages are not implemented (except FILEOKSTRING)
34  *
35  * FIXME: lpstrCustomFilter not handled
36  *
37  * FIXME: if the size of lpstrFile (nMaxFile) is too small the first
38  * two bytes of lpstrFile should contain the needed size
39  *
40  * FIXME: algorithm for selecting the initial directory is too simple
41  *
42  * FIXME: add to recent docs
43  *
44  * FIXME: flags not implemented: OFN_CREATEPROMPT, OFN_DONTADDTORECENT,
45  * OFN_ENABLEINCLUDENOTIFY, OFN_ENABLESIZING, OFN_EXTENSIONDIFFERENT,
46  * OFN_NODEREFERENCELINKS, OFN_NOREADONLYRETURN,
47  * OFN_NOTESTFILECREATE, OFN_OVERWRITEPROMPT, OFN_USEMONIKERS
48  *
49  * FIXME: lCustData for lpfnHook (WM_INITDIALOG)
50  *
51  *
52  */
53
54 #include "config.h"
55 #include "wine/port.h"
56
57 #include <ctype.h>
58 #include <stdlib.h>
59 #include <stdio.h>
60 #include <string.h>
61
62 #include "winbase.h"
63 #include "winternl.h"
64 #include "winnls.h"
65 #include "wine/unicode.h"
66 #include "commdlg.h"
67 #include "dlgs.h"
68 #include "cdlg.h"
69 #include "wine/debug.h"
70 #include "cderr.h"
71 #include "shellapi.h"
72 #include "shlguid.h"
73 #include "filedlgbrowser.h"
74 #include "shlwapi.h"
75 #include "wine/obj_contextmenu.h"
76
77 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
78
79 #define UNIMPLEMENTED_FLAGS \
80 (OFN_CREATEPROMPT | OFN_DONTADDTORECENT |\
81 OFN_ENABLEINCLUDENOTIFY | OFN_ENABLESIZING | OFN_EXTENSIONDIFFERENT |\
82 OFN_NODEREFERENCELINKS | OFN_NOREADONLYRETURN |\
83 OFN_NOTESTFILECREATE | OFN_OVERWRITEPROMPT /*| OFN_USEMONIKERS*/)
84
85 #define IsHooked(fodInfos) \
86         ((fodInfos->ofnInfos->Flags & OFN_ENABLEHOOK) && fodInfos->ofnInfos->lpfnHook)
87 /***********************************************************************
88  * Data structure and global variables
89  */
90 typedef struct SFolder
91 {
92   int m_iImageIndex;    /* Index of picture in image list */
93   HIMAGELIST hImgList;
94   int m_iIndent;      /* Indentation index */
95   LPITEMIDLIST pidlItem;  /* absolute pidl of the item */
96
97 } SFOLDER,*LPSFOLDER;
98
99 typedef struct tagLookInInfo
100 {
101   int iMaxIndentation;
102   UINT uSelectedItem;
103 } LookInInfos;
104
105
106 /***********************************************************************
107  * Defines and global variables
108  */
109
110 /* Draw item constant */
111 #define ICONWIDTH 18
112 #define XTEXTOFFSET 3
113
114 /* AddItem flags*/
115 #define LISTEND -1
116
117 /* SearchItem methods */
118 #define SEARCH_PIDL 1
119 #define SEARCH_EXP  2
120 #define ITEM_NOTFOUND -1
121
122 /* Undefined windows message sent by CreateViewObject*/
123 #define WM_GETISHELLBROWSER  WM_USER+7
124
125 /* NOTE
126  * Those macros exist in windowsx.h. However, you can't really use them since
127  * they rely on the UNICODE defines and can't be used inside Wine itself.
128  */
129
130 /* Combo box macros */
131 #define CBAddString(hwnd,str) \
132   SendMessageA(hwnd,CB_ADDSTRING,0,(LPARAM)str);
133 #define CBAddStringW(hwnd,str) \
134   SendMessageW(hwnd,CB_ADDSTRING,0,(LPARAM)str);
135
136 #define CBInsertString(hwnd,str,pos) \
137   SendMessageA(hwnd,CB_INSERTSTRING,(WPARAM)pos,(LPARAM)str);
138
139 #define CBDeleteString(hwnd,pos) \
140   SendMessageA(hwnd,CB_DELETESTRING,(WPARAM)pos,0);
141
142 #define CBSetItemDataPtr(hwnd,iItemId,dataPtr) \
143   SendMessageA(hwnd,CB_SETITEMDATA,(WPARAM)iItemId,(LPARAM)dataPtr);
144
145 #define CBGetItemDataPtr(hwnd,iItemId) \
146   SendMessageA(hwnd,CB_GETITEMDATA,(WPARAM)iItemId,0)
147
148 #define CBGetLBText(hwnd,iItemId,str) \
149   SendMessageA(hwnd,CB_GETLBTEXT,(WPARAM)iItemId,(LPARAM)str);
150
151 #define CBGetCurSel(hwnd) \
152   SendMessageA(hwnd,CB_GETCURSEL,0,0);
153
154 #define CBSetCurSel(hwnd,pos) \
155   SendMessageA(hwnd,CB_SETCURSEL,(WPARAM)pos,0);
156
157 #define CBGetCount(hwnd) \
158     SendMessageA(hwnd,CB_GETCOUNT,0,0);
159 #define CBShowDropDown(hwnd,show) \
160   SendMessageA(hwnd,CB_SHOWDROPDOWN,(WPARAM)show,0);
161 #define CBSetItemHeight(hwnd,index,height) \
162   SendMessageA(hwnd,CB_SETITEMHEIGHT,(WPARAM)index,(LPARAM)height);
163
164
165 const char *FileOpenDlgInfosStr = "FileOpenDlgInfos"; /* windows property description string */
166 const char *LookInInfosStr = "LookInInfos"; /* LOOKIN combo box property */
167
168 /***********************************************************************
169  * Prototypes
170  */
171
172 /* Internal functions used by the dialog */
173 static LRESULT FILEDLG95_FillControls(HWND hwnd, WPARAM wParam, LPARAM lParam);
174 static LRESULT FILEDLG95_OnWMCommand(HWND hwnd, WPARAM wParam, LPARAM lParam);
175 static LRESULT FILEDLG95_OnWMGetIShellBrowser(HWND hwnd);
176        BOOL    FILEDLG95_OnOpen(HWND hwnd);
177 static LRESULT FILEDLG95_InitControls(HWND hwnd);
178 static void    FILEDLG95_Clean(HWND hwnd);
179
180 /* Functions used by the shell navigation */
181 static LRESULT FILEDLG95_SHELL_Init(HWND hwnd);
182 static BOOL    FILEDLG95_SHELL_UpFolder(HWND hwnd);
183 static BOOL    FILEDLG95_SHELL_ExecuteCommand(HWND hwnd, LPCSTR lpVerb);
184 static void    FILEDLG95_SHELL_Clean(HWND hwnd);
185 static BOOL    FILEDLG95_SHELL_BrowseToDesktop(HWND hwnd);
186
187 /* Functions used by the filetype combo box */
188 static HRESULT FILEDLG95_FILETYPE_Init(HWND hwnd);
189 static BOOL    FILEDLG95_FILETYPE_OnCommand(HWND hwnd, WORD wNotifyCode);
190 static int     FILEDLG95_FILETYPE_SearchExt(HWND hwnd,LPCWSTR lpstrExt);
191 static void    FILEDLG95_FILETYPE_Clean(HWND hwnd);
192
193 /* Functions used by the Look In combo box */
194 static HRESULT FILEDLG95_LOOKIN_Init(HWND hwndCombo);
195 static LRESULT FILEDLG95_LOOKIN_DrawItem(LPDRAWITEMSTRUCT pDIStruct);
196 static BOOL    FILEDLG95_LOOKIN_OnCommand(HWND hwnd, WORD wNotifyCode);
197 static int     FILEDLG95_LOOKIN_AddItem(HWND hwnd,LPITEMIDLIST pidl, int iInsertId);
198 static int     FILEDLG95_LOOKIN_SearchItem(HWND hwnd,WPARAM searchArg,int iSearchMethod);
199 static int     FILEDLG95_LOOKIN_InsertItemAfterParent(HWND hwnd,LPITEMIDLIST pidl);
200 static int     FILEDLG95_LOOKIN_RemoveMostExpandedItem(HWND hwnd);
201        int     FILEDLG95_LOOKIN_SelectItem(HWND hwnd,LPITEMIDLIST pidl);
202 static void    FILEDLG95_LOOKIN_Clean(HWND hwnd);
203
204 /* Miscellaneous tool functions */
205 HRESULT       GetName(LPSHELLFOLDER lpsf, LPITEMIDLIST pidl,DWORD dwFlags,LPSTR lpstrFileName);
206 HRESULT       GetFileName(HWND hwnd, LPITEMIDLIST pidl, LPSTR lpstrFileName);
207 IShellFolder* GetShellFolderFromPidl(LPITEMIDLIST pidlAbs);
208 LPITEMIDLIST  GetParentPidl(LPITEMIDLIST pidl);
209 LPITEMIDLIST  GetPidlFromName(IShellFolder *psf,LPWSTR lpcstrFileName);
210
211 /* Shell memory allocation */
212 static void *MemAlloc(UINT size);
213 static void MemFree(void *mem);
214
215 BOOL WINAPI GetFileName95(FileOpenDlgInfos *fodInfos);
216 HRESULT WINAPI FileOpenDlgProc95(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
217 HRESULT SendCustomDlgNotificationMessage(HWND hwndParentDlg, UINT uCode);
218 HRESULT FILEDLG95_HandleCustomDialogMessages(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
219 BOOL FILEDLG95_OnOpenMultipleFiles(HWND hwnd, LPWSTR lpstrFileList, UINT nFileCount, UINT sizeUsed);
220 static BOOL BrowseSelectedFolder(HWND hwnd);
221
222 LPWSTR strpbrkW(LPWSTR str, LPCWSTR clist)
223 {
224     LPCWSTR p;
225
226     while(str && *str)
227     {
228         for(p = clist; *p; p++ )
229             if(*p == *str)
230                 return str;
231         str++;
232     }
233     return NULL;
234 }
235
236 /***********************************************************************
237  *      GetFileName95
238  *
239  * Creates an Open common dialog box that lets the user select
240  * the drive, directory, and the name of a file or set of files to open.
241  *
242  * IN  : The FileOpenDlgInfos structure associated with the dialog
243  * OUT : TRUE on success
244  *       FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
245  */
246 BOOL WINAPI GetFileName95(FileOpenDlgInfos *fodInfos)
247 {
248
249     LRESULT lRes;
250     LPCVOID template;
251     HRSRC hRes;
252     HANDLE hDlgTmpl = 0;
253
254     /* test for missing functionality */
255     if (fodInfos->ofnInfos->Flags & UNIMPLEMENTED_FLAGS)
256     {
257       FIXME("Flags 0x%08lx not yet implemented\n",
258          fodInfos->ofnInfos->Flags & UNIMPLEMENTED_FLAGS);
259     }
260
261     /* Create the dialog from a template */
262
263     if(!(hRes = FindResourceA(COMMDLG_hInstance32,MAKEINTRESOURCEA(NEWFILEOPENORD),RT_DIALOGA)))
264     {
265         COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
266         return FALSE;
267     }
268     if (!(hDlgTmpl = LoadResource(COMMDLG_hInstance32, hRes )) ||
269         !(template = LockResource( hDlgTmpl )))
270     {
271         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
272         return FALSE;
273     }
274
275     /* old style hook messages */
276     if (IsHooked(fodInfos))
277     {
278       fodInfos->HookMsg.fileokstring = RegisterWindowMessageA(FILEOKSTRINGA);
279       fodInfos->HookMsg.lbselchstring = RegisterWindowMessageA(LBSELCHSTRINGA);
280       fodInfos->HookMsg.helpmsgstring = RegisterWindowMessageA(HELPMSGSTRINGA);
281       fodInfos->HookMsg.sharevistring = RegisterWindowMessageA(SHAREVISTRINGA);
282     }
283
284     lRes = DialogBoxIndirectParamA(COMMDLG_hInstance32,
285                                   (LPDLGTEMPLATEA) template,
286                                   fodInfos->ofnInfos->hwndOwner,
287                                   (DLGPROC) FileOpenDlgProc95,
288                                   (LPARAM) fodInfos);
289
290     /* Unable to create the dialog */
291     if( lRes == -1)
292         return FALSE;
293
294     return lRes;
295 }
296
297 /***********************************************************************
298  *      GetFileDialog95A
299  *
300  * Call GetFileName95 with this structure and clean the memory.
301  *
302  * IN  : The OPENFILENAMEA initialisation structure passed to
303  *       GetOpenFileNameA win api function (see filedlg.c)
304  */
305 BOOL  WINAPI GetFileDialog95A(LPOPENFILENAMEA ofn,UINT iDlgType)
306 {
307   BOOL ret;
308   FileOpenDlgInfos fodInfos;
309   LPSTR lpstrSavDir = NULL;
310   LPWSTR title = NULL;
311   LPWSTR defext = NULL;
312   LPWSTR filter = NULL;
313   LPWSTR customfilter = NULL;
314
315   /* Initialize FileOpenDlgInfos structure */
316   ZeroMemory(&fodInfos, sizeof(FileOpenDlgInfos));
317
318   /* Pass in the original ofn */
319   fodInfos.ofnInfos = ofn;
320
321   /* save current directory */
322   if (ofn->Flags & OFN_NOCHANGEDIR)
323   {
324      lpstrSavDir = MemAlloc(MAX_PATH);
325      GetCurrentDirectoryA(MAX_PATH, lpstrSavDir);
326   }
327
328   fodInfos.unicode = FALSE;
329
330   /* convert all the input strings to unicode */
331   if(ofn->lpstrInitialDir)
332   {
333     DWORD len = MultiByteToWideChar( CP_ACP, 0, ofn->lpstrInitialDir, -1, NULL, 0 );
334     fodInfos.initdir = MemAlloc((len+1)*sizeof(WCHAR));
335     MultiByteToWideChar( CP_ACP, 0, ofn->lpstrInitialDir, -1, fodInfos.initdir, len);
336   }
337   else
338     fodInfos.initdir = NULL;
339
340   if(ofn->lpstrFile)
341   {
342     fodInfos.filename = MemAlloc(ofn->nMaxFile*sizeof(WCHAR));
343     MultiByteToWideChar( CP_ACP, 0, ofn->lpstrFile, -1, fodInfos.filename, ofn->nMaxFile);
344   }
345   else
346     fodInfos.filename = NULL;
347
348   if(ofn->lpstrDefExt)
349   {
350     DWORD len = MultiByteToWideChar( CP_ACP, 0, ofn->lpstrDefExt, -1, NULL, 0 );
351     defext = MemAlloc((len+1)*sizeof(WCHAR));
352     MultiByteToWideChar( CP_ACP, 0, ofn->lpstrDefExt, -1, defext, len);
353   }
354   fodInfos.defext = defext;
355
356   if(ofn->lpstrTitle)
357   {
358     DWORD len = MultiByteToWideChar( CP_ACP, 0, ofn->lpstrTitle, -1, NULL, 0 );
359     title = MemAlloc((len+1)*sizeof(WCHAR));
360     MultiByteToWideChar( CP_ACP, 0, ofn->lpstrTitle, -1, title, len);
361   }
362   fodInfos.title = title;
363
364   if (ofn->lpstrFilter)
365   {
366     LPCSTR s;
367     int n, len;
368
369     /* filter is a list...  title\0ext\0......\0\0 */
370     s = ofn->lpstrFilter;
371     while (*s) s = s+strlen(s)+1;
372     s++;
373     n = s - ofn->lpstrFilter;
374     len = MultiByteToWideChar( CP_ACP, 0, ofn->lpstrFilter, n, NULL, 0 );
375     filter = MemAlloc(len*sizeof(WCHAR));
376     MultiByteToWideChar( CP_ACP, 0, ofn->lpstrFilter, n, filter, len );
377   }
378   fodInfos.filter = filter;
379
380   /* convert lpstrCustomFilter */
381   if (ofn->lpstrCustomFilter)
382   {
383     LPCSTR s;
384     int n, len;
385
386     /* filter is a list...  title\0ext\0......\0\0 */
387     s = ofn->lpstrCustomFilter;
388     while (*s) s = s+strlen(s)+1;
389     s++;
390     n = s - ofn->lpstrCustomFilter;
391     len = MultiByteToWideChar( CP_ACP, 0, ofn->lpstrCustomFilter, n, NULL, 0 );
392     customfilter = MemAlloc(len*sizeof(WCHAR));
393     MultiByteToWideChar( CP_ACP, 0, ofn->lpstrCustomFilter, n, customfilter, len );
394   }
395   fodInfos.customfilter = customfilter;
396
397   /* Initialize the dialog property */
398   fodInfos.DlgInfos.dwDlgProp = 0;
399   fodInfos.DlgInfos.hwndCustomDlg = (HWND)NULL;
400
401   switch(iDlgType)
402   {
403     case OPEN_DIALOG :
404       ret = GetFileName95(&fodInfos);
405       break;
406     case SAVE_DIALOG :
407       fodInfos.DlgInfos.dwDlgProp |= FODPROP_SAVEDLG;
408       ret = GetFileName95(&fodInfos);
409       break;
410     default :
411       ret = 0;
412   }
413
414   if (lpstrSavDir)
415   {
416       SetCurrentDirectoryA(lpstrSavDir);
417       MemFree(lpstrSavDir);
418   }
419
420   if(title)
421     MemFree(title);
422   if(defext)
423     MemFree(defext);
424   if(filter)
425     MemFree(filter);
426   if(customfilter)
427     MemFree(customfilter);
428   if(fodInfos.initdir)
429     MemFree(fodInfos.initdir);
430
431   if(fodInfos.filename)
432     MemFree(fodInfos.filename);
433
434   TRACE("selected file: %s\n",ofn->lpstrFile);
435
436   return ret;
437 }
438
439 /***********************************************************************
440  *      GetFileDialog95W
441  *
442  * Copy the OPENFILENAMEW structure in a FileOpenDlgInfos structure.
443  * Call GetFileName95 with this structure and clean the memory.
444  *
445  * FIXME: lpstrCustomFilter has to be converted back
446  *
447  */
448 BOOL  WINAPI GetFileDialog95W(LPOPENFILENAMEW ofn,UINT iDlgType)
449 {
450   BOOL ret;
451   FileOpenDlgInfos fodInfos;
452   LPSTR lpstrSavDir = NULL;
453
454   /* Initialize FileOpenDlgInfos structure */
455   ZeroMemory(&fodInfos, sizeof(FileOpenDlgInfos));
456
457   /*  Pass in the original ofn */
458   fodInfos.ofnInfos = (LPOPENFILENAMEA) ofn;
459
460   fodInfos.title = ofn->lpstrTitle;
461   fodInfos.defext = ofn->lpstrDefExt;
462   fodInfos.filter = ofn->lpstrFilter;
463   fodInfos.customfilter = ofn->lpstrCustomFilter;
464
465   /* convert string arguments, save others */
466   if(ofn->lpstrFile)
467   {
468     fodInfos.filename = MemAlloc(ofn->nMaxFile*sizeof(WCHAR));
469     strncpyW(fodInfos.filename,ofn->lpstrFile,ofn->nMaxFile);
470   }
471   else
472     fodInfos.filename = NULL;
473
474   if(ofn->lpstrInitialDir)
475   {
476     DWORD len = strlenW(ofn->lpstrInitialDir);
477     fodInfos.initdir = MemAlloc((len+1)*sizeof(WCHAR));
478     strcpyW(fodInfos.initdir,ofn->lpstrInitialDir);
479   }
480   else
481     fodInfos.initdir = NULL;
482
483   /* save current directory */
484   if (ofn->Flags & OFN_NOCHANGEDIR)
485   {
486      lpstrSavDir = MemAlloc(MAX_PATH);
487      GetCurrentDirectoryA(MAX_PATH, lpstrSavDir);
488   }
489
490   fodInfos.unicode = TRUE;
491
492   switch(iDlgType)
493   {
494   case OPEN_DIALOG :
495       ret = GetFileName95(&fodInfos);
496       break;
497   case SAVE_DIALOG :
498       fodInfos.DlgInfos.dwDlgProp |= FODPROP_SAVEDLG;
499       ret = GetFileName95(&fodInfos);
500       break;
501   default :
502       ret = 0;
503   }
504
505   if (lpstrSavDir)
506   {
507       SetCurrentDirectoryA(lpstrSavDir);
508       MemFree(lpstrSavDir);
509   }
510
511   /* restore saved IN arguments and convert OUT arguments back */
512   MemFree(fodInfos.filename);
513   MemFree(fodInfos.initdir);
514   return ret;
515 }
516
517 void ArrangeCtrlPositions( HWND hwndChildDlg, HWND hwndParentDlg)
518 {
519     HWND hwndChild,hwndStc32;
520     RECT rectParent, rectChild, rectCtrl, rectStc32, rectTemp;
521     POINT ptMoveCtl;
522     POINT ptParentClient;
523
524     TRACE("\n");
525
526     ptMoveCtl.x = ptMoveCtl.y = 0;
527     hwndStc32=GetDlgItem(hwndChildDlg,stc32);
528     GetClientRect(hwndParentDlg,&rectParent);
529     GetClientRect(hwndChildDlg,&rectChild);
530
531     if(hwndStc32)
532     {
533       GetWindowRect(hwndStc32,&rectStc32);
534       MapWindowPoints(0, hwndChildDlg,(LPPOINT)&rectStc32,2);
535       CopyRect(&rectTemp,&rectStc32);
536
537       SetRect(&rectStc32,rectStc32.left,rectStc32.top,rectStc32.left + (rectParent.right-rectParent.left),rectStc32.top+(rectParent.bottom-rectParent.top));
538       SetWindowPos(hwndStc32,0,rectStc32.left,rectStc32.top,rectStc32.right-rectStc32.left,rectStc32.bottom-rectStc32.top,SWP_NOMOVE|SWP_NOZORDER | SWP_NOACTIVATE);
539
540       ptParentClient.x = max((rectParent.right-rectParent.left),(rectChild.right-rectChild.left));
541       if(rectStc32.right < rectTemp.right)
542         ptMoveCtl.x = 0;
543       else
544         ptMoveCtl.x = (rectStc32.right - rectTemp.right);
545
546       ptParentClient.y = max((rectParent.bottom-rectParent.top),(rectChild.bottom-rectChild.top));
547       if(rectStc32.bottom < rectTemp.bottom)
548         ptMoveCtl.y = 0;
549       else
550         ptMoveCtl.y = (rectStc32.bottom - rectTemp.bottom);
551     }
552     else
553     {
554       if( (GetWindow(hwndChildDlg,GW_CHILD)) == (HWND) NULL) return;
555
556       SetRectEmpty(&rectTemp);
557       ptParentClient.x = max((rectParent.right-rectParent.left),(rectChild.right-rectChild.left));
558       ptParentClient.y = (rectParent.bottom-rectParent.top) + (rectChild.bottom-rectChild.top);
559       ptMoveCtl.y = rectParent.bottom-rectParent.top;
560       ptMoveCtl.x=0;
561     }
562     SetRect(&rectParent,rectParent.left,rectParent.top,rectParent.left+ptParentClient.x,rectParent.top+ptParentClient.y);
563     AdjustWindowRectEx( &rectParent,GetWindowLongA(hwndParentDlg,GWL_STYLE),FALSE,GetWindowLongA(hwndParentDlg,GWL_EXSTYLE));
564
565     SetWindowPos(hwndChildDlg, 0, 0,0, ptParentClient.x,ptParentClient.y, SWP_NOZORDER );
566     SetWindowPos(hwndParentDlg, 0, rectParent.left,rectParent.top, (rectParent.right- rectParent.left),
567         (rectParent.bottom-rectParent.top),SWP_NOMOVE | SWP_NOZORDER);
568
569     hwndChild = GetWindow(hwndChildDlg,GW_CHILD);
570     if(hwndStc32)
571     {
572       GetWindowRect(hwndStc32,&rectStc32);
573       MapWindowPoints( 0, hwndChildDlg,(LPPOINT)&rectStc32,2);
574     }
575     else
576       SetRect(&rectStc32,0,0,0,0);
577
578     if (hwndChild )
579     {
580       do
581       {
582         if(hwndChild != hwndStc32)
583         {
584           if (GetWindowLongA( hwndChild, GWL_STYLE ) & WS_MAXIMIZE)
585                                 continue;
586           GetWindowRect(hwndChild,&rectCtrl);
587           MapWindowPoints( 0, hwndParentDlg,(LPPOINT)&rectCtrl,2);
588
589           /*
590             Check the initial position of the controls relative to the initial
591             position and size of stc32 (before it is expanded).
592           */
593           if (rectCtrl.left >= rectTemp.right && rectCtrl.top >= rectTemp.bottom)
594           {
595             rectCtrl.left += ptMoveCtl.x;
596             rectCtrl.top  += ptMoveCtl.y;
597           }
598           else if (rectCtrl.left >= rectTemp.right)
599           {
600             rectCtrl.left += ptMoveCtl.x;
601           }
602           else if (rectCtrl.top >= rectTemp.bottom)
603           {
604             rectCtrl.top  += ptMoveCtl.y;
605           }
606
607           SetWindowPos( hwndChild, 0, rectCtrl.left, rectCtrl.top,
608                                 rectCtrl.right-rectCtrl.left,rectCtrl.bottom-rectCtrl.top,
609                                 SWP_NOSIZE | SWP_NOZORDER );
610         }
611       } while ((hwndChild=GetWindow( hwndChild, GW_HWNDNEXT )) != (HWND)NULL);
612     }
613     hwndChild = GetWindow(hwndParentDlg,GW_CHILD);
614
615     if(hwndStc32)
616     {
617       GetWindowRect(hwndStc32,&rectStc32);
618       MapWindowPoints( 0, hwndChildDlg,(LPPOINT)&rectStc32,2);
619       ptMoveCtl.x = rectStc32.left - 0;
620       ptMoveCtl.y = rectStc32.top - 0;
621       if (hwndChild )
622       {
623         do
624         {
625           if(hwndChild != hwndChildDlg)
626           {
627             if (GetWindowLongA( hwndChild, GWL_STYLE ) & WS_MAXIMIZE)
628               continue;
629             GetWindowRect(hwndChild,&rectCtrl);
630             MapWindowPoints( 0, hwndParentDlg,(LPPOINT)&rectCtrl,2);
631
632             rectCtrl.left += ptMoveCtl.x;
633             rectCtrl.top += ptMoveCtl.y;
634
635             SetWindowPos( hwndChild, 0, rectCtrl.left, rectCtrl.top,
636                 rectCtrl.right-rectCtrl.left,rectCtrl.bottom-rectCtrl.top,
637                 SWP_NOSIZE |SWP_NOZORDER );
638           }
639         } while ((hwndChild=GetWindow( hwndChild, GW_HWNDNEXT )) != (HWND)NULL);
640       }
641     }
642 }
643
644
645 HRESULT WINAPI FileOpenDlgProcUserTemplate(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
646 {
647     FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(GetParent(hwnd),FileOpenDlgInfosStr);
648
649 #if 0
650     TRACE("0x%04x\n", uMsg);
651 #endif
652
653     switch(uMsg)
654     {
655       case WM_INITDIALOG:
656       {
657         /* Hide caption since some program may leave it */
658         DWORD Style = GetWindowLongA(hwnd, GWL_STYLE);
659         if (Style & WS_CAPTION) SetWindowLongA(hwnd, GWL_STYLE, Style & (~WS_CAPTION));
660
661         fodInfos = (FileOpenDlgInfos *)lParam;
662         lParam = (LPARAM) fodInfos->ofnInfos;
663         ArrangeCtrlPositions(hwnd,GetParent(hwnd));
664
665         if(fodInfos && IsHooked(fodInfos))
666           return CallWindowProcA((WNDPROC)fodInfos->ofnInfos->lpfnHook,hwnd,uMsg,wParam,lParam);
667         return 0;
668       }
669     }
670
671     if(fodInfos && IsHooked(fodInfos))
672       return CallWindowProcA((WNDPROC)fodInfos->ofnInfos->lpfnHook,hwnd,uMsg,wParam,lParam);
673
674     return DefWindowProcA(hwnd,uMsg,wParam,lParam);
675 }
676
677 HWND CreateTemplateDialog(FileOpenDlgInfos *fodInfos, HWND hwnd)
678 {
679     LPCVOID template;
680     HRSRC hRes;
681     HANDLE hDlgTmpl = 0;
682     HWND hChildDlg = 0;
683
684     TRACE("\n");
685
686     /*
687      * If OFN_ENABLETEMPLATEHANDLE is specified, the OPENFILENAME
688      * structure's hInstance parameter is not a HINSTANCE, but
689      * instead a pointer to a template resource to use.
690      */
691     if (fodInfos->ofnInfos->Flags & OFN_ENABLETEMPLATE ||
692         fodInfos->ofnInfos->Flags & OFN_ENABLETEMPLATEHANDLE)
693     {
694       HINSTANCE hinst;
695
696       if (fodInfos->ofnInfos->Flags  & OFN_ENABLETEMPLATEHANDLE)
697       {
698         hinst = 0;
699         if( !(template = LockResource( fodInfos->ofnInfos->hInstance)))
700         {
701           COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
702           return (HWND)NULL;
703         }
704       }
705       else
706       {
707         hinst = fodInfos->ofnInfos->hInstance;
708         if(fodInfos->unicode)
709         {
710             LPOPENFILENAMEW ofn = (LPOPENFILENAMEW) fodInfos->ofnInfos;
711             hRes = FindResourceW( hinst, ofn->lpTemplateName, RT_DIALOGW);
712         }
713         else
714         {
715             LPOPENFILENAMEA ofn = fodInfos->ofnInfos;
716             hRes = FindResourceA( hinst, ofn->lpTemplateName, RT_DIALOGA);
717         }
718         if (!hRes)
719         {
720           COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
721           return (HWND)NULL;
722         }
723         if (!(hDlgTmpl = LoadResource( hinst, hRes )) ||
724             !(template = LockResource( hDlgTmpl )))
725         {
726           COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
727           return (HWND)NULL;
728         }
729       }
730
731       hChildDlg= CreateDialogIndirectParamA(hinst, template,
732            hwnd,(DLGPROC)FileOpenDlgProcUserTemplate,(LPARAM)fodInfos);
733       if(hChildDlg)
734       {
735         ShowWindow(hChildDlg,SW_SHOW);
736         return hChildDlg;
737       }
738     }
739     else if( IsHooked(fodInfos))
740     {
741       RECT rectHwnd;
742       struct  {
743          DLGTEMPLATE tmplate;
744          WORD menu,class,title;
745          } temp;
746       GetClientRect(hwnd,&rectHwnd);
747       temp.tmplate.style = WS_CHILD | WS_CLIPSIBLINGS;
748       temp.tmplate.dwExtendedStyle = 0;
749       temp.tmplate.cdit = 0;
750       temp.tmplate.x = 0;
751       temp.tmplate.y = 0;
752       temp.tmplate.cx = rectHwnd.right-rectHwnd.left;
753       temp.tmplate.cy = rectHwnd.bottom-rectHwnd.top;
754       temp.menu = temp.class = temp.title = 0;
755       hChildDlg = CreateDialogIndirectParamA(fodInfos->ofnInfos->hInstance,&temp,
756                   hwnd,(DLGPROC)FileOpenDlgProcUserTemplate,(LPARAM)fodInfos);
757       return hChildDlg;
758     }
759     return (HWND)NULL;
760 }
761
762 /***********************************************************************
763 *          SendCustomDlgNotificationMessage
764 *
765 * Send CustomDialogNotification (CDN_FIRST -- CDN_LAST) message to the custom template dialog
766 */
767
768 HRESULT SendCustomDlgNotificationMessage(HWND hwndParentDlg, UINT uCode)
769 {
770     FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwndParentDlg,FileOpenDlgInfosStr);
771
772     TRACE("0x%04x 0x%04x\n",hwndParentDlg, uCode);
773
774     if(!fodInfos) return 0;
775
776     if(fodInfos->unicode)
777       FIXME("sending OPENFILENAMEA structure. Hook is expecting OPENFILENAMEW!\n");
778
779     if(fodInfos->DlgInfos.hwndCustomDlg)
780     {
781         OFNOTIFYA ofnNotify;
782         HRESULT ret;
783         ofnNotify.hdr.hwndFrom=hwndParentDlg;
784         ofnNotify.hdr.idFrom=0;
785         ofnNotify.hdr.code = uCode;
786         ofnNotify.lpOFN = fodInfos->ofnInfos;
787         TRACE("CALL NOTIFY for %x\n", uCode);
788         ret = SendMessageA(fodInfos->DlgInfos.hwndCustomDlg,WM_NOTIFY,0,(LPARAM)&ofnNotify);
789         TRACE("RET NOTIFY\n");
790         return ret;
791     }
792     return TRUE;
793 }
794
795 /***********************************************************************
796 *         FILEDLG95_HandleCustomDialogMessages
797 *
798 * Handle Custom Dialog Messages (CDM_FIRST -- CDM_LAST) messages
799 */
800 HRESULT FILEDLG95_HandleCustomDialogMessages(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
801 {
802     LPSTR lpstrFileSpec;
803     int reqSize;
804     char lpstrPath[MAX_PATH];
805     FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
806     if(!fodInfos) return -1;
807
808     switch(uMsg)
809     {
810         case CDM_GETFILEPATH:
811             GetDlgItemTextA(hwnd,IDC_FILENAME,lpstrPath, sizeof(lpstrPath));
812             lpstrFileSpec = (LPSTR)PathFindFileNameA(lpstrPath);
813             if (lpstrFileSpec==lpstrPath)
814             {
815                 char lpstrCurrentDir[MAX_PATH];
816                 /* Prepend the current path */
817                 SHGetPathFromIDListA(fodInfos->ShellInfos.pidlAbsCurrent,lpstrCurrentDir);
818                 if ((LPSTR)lParam!=NULL)
819                     snprintf((LPSTR)lParam,(int)wParam,"%s\\%s",lpstrCurrentDir,lpstrPath);
820                 reqSize=strlen(lpstrCurrentDir)+1+strlen(lpstrPath)+1;
821             }
822             else
823             {
824                 lstrcpynA((LPSTR)lParam,(LPSTR)lpstrPath,(int)wParam);
825                 reqSize=strlen(lpstrPath);
826             }
827             /* return the required buffer size */
828             return reqSize;
829
830         case CDM_GETFOLDERPATH:
831             SHGetPathFromIDListA(fodInfos->ShellInfos.pidlAbsCurrent,lpstrPath);
832             if ((LPSTR)lParam!=NULL)
833                 lstrcpynA((LPSTR)lParam,lpstrPath,(int)wParam);
834             return strlen(lpstrPath);
835
836         case CDM_GETSPEC:
837             reqSize=GetDlgItemTextA(hwnd,IDC_FILENAME,lpstrPath, sizeof(lpstrPath));
838             lpstrFileSpec = (LPSTR)PathFindFileNameA(lpstrPath);
839             if ((LPSTR)lParam!=NULL)
840                 lstrcpynA((LPSTR)lParam, lpstrFileSpec, (int)wParam);
841             return strlen(lpstrFileSpec);
842
843         case CDM_SETCONTROLTEXT:
844             if ( 0 != lParam )
845                 SetDlgItemTextA( hwnd, (UINT) wParam, (LPSTR) lParam );
846             return TRUE;
847
848         case CDM_HIDECONTROL:
849         case CDM_SETDEFEXT:
850             FIXME("CDM_HIDECONTROL,CDM_SETCONTROLTEXT,CDM_SETDEFEXT not implemented\n");
851             return -1;
852     }
853     return TRUE;
854 }
855
856 /***********************************************************************
857  *          FileOpenDlgProc95
858  *
859  * File open dialog procedure
860  */
861 HRESULT WINAPI FileOpenDlgProc95(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
862 {
863 #if 0
864   TRACE("0x%04x 0x%04x\n", hwnd, uMsg);
865 #endif
866
867   switch(uMsg)
868   {
869     case WM_INITDIALOG:
870       {
871          FileOpenDlgInfos * fodInfos = (FileOpenDlgInfos *)lParam;
872
873          /* Adds the FileOpenDlgInfos in the property list of the dialog
874             so it will be easily accessible through a GetPropA(...) */
875          SetPropA(hwnd, FileOpenDlgInfosStr, (HANDLE) fodInfos);
876
877          fodInfos->DlgInfos.hwndCustomDlg =
878            CreateTemplateDialog((FileOpenDlgInfos *)lParam, hwnd);
879
880          FILEDLG95_InitControls(hwnd);
881          SendCustomDlgNotificationMessage(hwnd,CDN_INITDONE);
882          FILEDLG95_FillControls(hwnd, wParam, lParam);
883          SendCustomDlgNotificationMessage(hwnd,CDN_FOLDERCHANGE);
884          SendCustomDlgNotificationMessage(hwnd,CDN_SELCHANGE);
885          SetWindowPos(fodInfos->DlgInfos.hwndCustomDlg, HWND_BOTTOM,
886                       0,0,0,0, SWP_NOMOVE|SWP_NOSIZE);
887          return 0;
888        }
889     case WM_COMMAND:
890       return FILEDLG95_OnWMCommand(hwnd, wParam, lParam);
891     case WM_DRAWITEM:
892       {
893         switch(((LPDRAWITEMSTRUCT)lParam)->CtlID)
894         {
895         case IDC_LOOKIN:
896           FILEDLG95_LOOKIN_DrawItem((LPDRAWITEMSTRUCT) lParam);
897           return TRUE;
898         }
899       }
900       return FALSE;
901
902     case WM_GETISHELLBROWSER:
903       return FILEDLG95_OnWMGetIShellBrowser(hwnd);
904
905     case WM_DESTROY:
906       RemovePropA(hwnd, FileOpenDlgInfosStr);
907       return FALSE;
908
909     case WM_NOTIFY:
910     {
911         LPNMHDR lpnmh = (LPNMHDR)lParam;
912         UINT stringId = -1;
913
914         /* set up the button tooltips strings */
915         if(TTN_GETDISPINFOA == lpnmh->code )
916         {
917             LPNMTTDISPINFOA lpdi = (LPNMTTDISPINFOA)lParam;
918             switch(lpnmh->idFrom )
919             {
920                 /* Up folder button */
921                 case FCIDM_TB_UPFOLDER:
922                     stringId = IDS_UPFOLDER;
923                     break;
924                 /* New folder button */
925                 case FCIDM_TB_NEWFOLDER:
926                     stringId = IDS_NEWFOLDER;
927                     break;
928                 /* List option button */
929                 case FCIDM_TB_SMALLICON:
930                     stringId = IDS_LISTVIEW;
931                     break;
932                 /* Details option button */
933                 case FCIDM_TB_REPORTVIEW:
934                     stringId = IDS_REPORTVIEW;
935                     break;
936                 /* Desktop button */
937                 case FCIDM_TB_DESKTOP:
938                     stringId = IDS_TODESKTOP;
939                     break;
940                 default:
941                     stringId = 0;
942             }
943             lpdi->hinst = COMMDLG_hInstance32;
944             lpdi->lpszText =  (LPSTR) stringId;
945         }
946         return FALSE;
947     }
948     default :
949       if(uMsg >= CDM_FIRST && uMsg <= CDM_LAST)
950         return FILEDLG95_HandleCustomDialogMessages(hwnd, uMsg, wParam, lParam);
951       return FALSE;
952   }
953 }
954
955 /***********************************************************************
956  *      FILEDLG95_InitControls
957  *
958  * WM_INITDIALOG message handler (before hook notification)
959  */
960 static LRESULT FILEDLG95_InitControls(HWND hwnd)
961 {
962   int win2000plus = 0;
963   int win98plus   = 0;
964   int handledPath = FALSE;
965   OSVERSIONINFOA osVi;
966   const WCHAR szwSlash[] = { '\\', 0 };
967   const WCHAR szwStar[] = { '*',0 };
968
969   TBBUTTON tbb[] =
970   {
971    {0,                 0,                   TBSTATE_ENABLED, TBSTYLE_SEP, {0, 0}, 0, 0 },
972    {VIEW_PARENTFOLDER, FCIDM_TB_UPFOLDER,   TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
973    {0,                 0,                   TBSTATE_ENABLED, TBSTYLE_SEP, {0, 0}, 0, 0 },
974    {VIEW_NEWFOLDER+1,  FCIDM_TB_DESKTOP,    TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
975    {0,                 0,                   TBSTATE_ENABLED, TBSTYLE_SEP, {0, 0}, 0, 0 },
976    {VIEW_NEWFOLDER,    FCIDM_TB_NEWFOLDER,  TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
977    {0,                 0,                   TBSTATE_ENABLED, TBSTYLE_SEP, {0, 0}, 0, 0 },
978    {VIEW_LIST,         FCIDM_TB_SMALLICON,  TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
979    {VIEW_DETAILS,      FCIDM_TB_REPORTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
980   };
981   TBADDBITMAP tba[2];
982   RECT rectTB;
983   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
984
985   tba[0].hInst = HINST_COMMCTRL;
986   tba[0].nID   = IDB_VIEW_SMALL_COLOR;
987   tba[1].hInst = COMDLG32_hInstance;
988   tba[1].nID   = 800;
989
990   TRACE("%p\n", fodInfos);
991
992   /* Get windows version emulating */
993   osVi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
994   GetVersionExA(&osVi);
995   if (osVi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
996     win98plus   = ((osVi.dwMajorVersion > 4) || ((osVi.dwMajorVersion == 4) && (osVi.dwMinorVersion > 0)));
997   } else if (osVi.dwPlatformId == VER_PLATFORM_WIN32_NT) {
998     win2000plus = (osVi.dwMajorVersion > 4);
999     if (win2000plus) win98plus = TRUE;
1000   }
1001   TRACE("Running on 2000+ %d, 98+ %d\n", win2000plus, win98plus);
1002
1003   /* Get the hwnd of the controls */
1004   fodInfos->DlgInfos.hwndFileName = GetDlgItem(hwnd,IDC_FILENAME);
1005   fodInfos->DlgInfos.hwndFileTypeCB = GetDlgItem(hwnd,IDC_FILETYPE);
1006   fodInfos->DlgInfos.hwndLookInCB = GetDlgItem(hwnd,IDC_LOOKIN);
1007
1008   /* construct the toolbar */
1009   GetWindowRect(GetDlgItem(hwnd,IDC_TOOLBARSTATIC),&rectTB);
1010   MapWindowPoints( 0, hwnd,(LPPOINT)&rectTB,2);
1011
1012   fodInfos->DlgInfos.hwndTB = CreateWindowExA(0, TOOLBARCLASSNAMEA, (LPSTR) NULL,
1013         WS_CHILD | WS_GROUP | TBSTYLE_TOOLTIPS | CCS_NODIVIDER | CCS_NORESIZE,
1014         0, 0, 150, 26, hwnd, (HMENU) IDC_TOOLBAR, COMMDLG_hInstance32, NULL);
1015
1016   SetWindowPos(fodInfos->DlgInfos.hwndTB, 0,
1017         rectTB.left,rectTB.top, rectTB.right-rectTB.left, rectTB.bottom-rectTB.top,
1018         SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_NOZORDER );
1019
1020   SendMessageA(fodInfos->DlgInfos.hwndTB, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);
1021
1022 /* FIXME: use TB_LOADIMAGES when implemented */
1023 /*  SendMessageA(fodInfos->DlgInfos.hwndTB, TB_LOADIMAGES, (WPARAM) IDB_VIEW_SMALL_COLOR, HINST_COMMCTRL);*/
1024   SendMessageA(fodInfos->DlgInfos.hwndTB, TB_ADDBITMAP, (WPARAM) 12, (LPARAM) &tba[0]);
1025   SendMessageA(fodInfos->DlgInfos.hwndTB, TB_ADDBITMAP, (WPARAM) 1, (LPARAM) &tba[1]);
1026
1027   SendMessageA(fodInfos->DlgInfos.hwndTB, TB_ADDBUTTONSA, (WPARAM) 9,(LPARAM) &tbb);
1028   SendMessageA(fodInfos->DlgInfos.hwndTB, TB_AUTOSIZE, 0, 0);
1029
1030   /* Set the window text with the text specified in the OPENFILENAME structure */
1031   if(fodInfos->title)
1032   {
1033       SetWindowTextW(hwnd,fodInfos->title);
1034   }
1035   else if (fodInfos->DlgInfos.dwDlgProp & FODPROP_SAVEDLG)
1036   {
1037       SetWindowTextA(hwnd,"Save");
1038   }
1039
1040   /* Initialise the file name edit control */
1041   handledPath = FALSE;
1042   TRACE("Before manipilation, file = %s, dir = %s\n", debugstr_w(fodInfos->filename), debugstr_w(fodInfos->initdir));
1043
1044   if(fodInfos->filename)
1045   {
1046       /* 1. If win2000 or higher and filename contains a path, use it
1047          in preference over the lpstrInitialDir                       */
1048       if (win2000plus && *fodInfos->filename && strpbrkW(fodInfos->filename, szwSlash)) {
1049          WCHAR tmpBuf[MAX_PATH];
1050          WCHAR *nameBit;
1051          DWORD result;
1052
1053          result = GetFullPathNameW(fodInfos->filename, MAX_PATH, tmpBuf, &nameBit);
1054          if (result) {
1055
1056             /* nameBit is always shorter than the original filename */
1057             strcpyW(fodInfos->filename,nameBit);
1058
1059             *nameBit = 0x00;
1060             if (fodInfos->initdir == NULL)
1061                 MemFree(fodInfos->initdir);
1062             fodInfos->initdir = MemAlloc((strlenW(tmpBuf) + 1)*sizeof(WCHAR));
1063             strcpyW(fodInfos->initdir, tmpBuf);
1064             handledPath = TRUE;
1065             TRACE("Value in Filename includes path, overriding InitialDir: %s, %s\n",
1066                     debugstr_w(fodInfos->filename), debugstr_w(fodInfos->initdir));
1067          }
1068          SetDlgItemTextW(hwnd, IDC_FILENAME, fodInfos->filename);
1069
1070       } else {
1071          SetDlgItemTextW(hwnd, IDC_FILENAME, fodInfos->filename);
1072       }
1073   }
1074
1075   /* 2. (All platforms) If initdir is not null, then use it */
1076   if ((handledPath == FALSE) && (fodInfos->initdir!=NULL) &&
1077                                 (*fodInfos->initdir!=0x00))
1078   {
1079       /* Work out the proper path as supplied one might be relative          */
1080       /* (Here because supplying '.' as dir browses to My Computer)          */
1081       if (handledPath==FALSE) {
1082           WCHAR tmpBuf[MAX_PATH];
1083           WCHAR tmpBuf2[MAX_PATH];
1084           WCHAR *nameBit;
1085           DWORD result;
1086
1087           strcpyW(tmpBuf, fodInfos->initdir);
1088           if (tmpBuf[strlenW(tmpBuf)-1] != '\\') {
1089              strcatW(tmpBuf, szwSlash);
1090           }
1091           strcatW(tmpBuf, szwStar);
1092           result = GetFullPathNameW(tmpBuf, MAX_PATH, tmpBuf2, &nameBit);
1093           if (result) {
1094              *nameBit = 0x00;
1095              if (fodInfos->initdir)
1096                 MemFree(fodInfos->initdir);
1097              fodInfos->initdir = MemAlloc((strlenW(tmpBuf2) + 1)*sizeof(WCHAR));
1098              strcpyW(fodInfos->initdir, tmpBuf2);
1099              handledPath = TRUE;
1100              TRACE("Value in InitDir changed to %s\n", debugstr_w(fodInfos->initdir));
1101           }
1102       }
1103   }
1104
1105   if ((handledPath == FALSE) && ((fodInfos->initdir==NULL) ||
1106                                  (*fodInfos->initdir==0x00)))
1107   {
1108       /* 3. All except w2k+: if filename contains a path use it */
1109       if (!win2000plus && fodInfos->filename &&
1110           *fodInfos->filename &&
1111           strpbrkW(fodInfos->filename, szwSlash)) {
1112          WCHAR tmpBuf[MAX_PATH];
1113          WCHAR *nameBit;
1114          DWORD result;
1115
1116          result = GetFullPathNameW(fodInfos->filename, MAX_PATH,
1117                                   tmpBuf, &nameBit);
1118          if (result) {
1119             int len;
1120
1121             /* nameBit is always shorter than the original filename */
1122             strcpyW(fodInfos->filename, nameBit);
1123             *nameBit = 0x00;
1124
1125             len = strlenW(tmpBuf);
1126             if(fodInfos->initdir)
1127                MemFree(fodInfos->initdir);
1128             fodInfos->initdir = MemAlloc((len+1)*sizeof(WCHAR));
1129             strcpyW(fodInfos->initdir, tmpBuf);
1130
1131             handledPath = TRUE;
1132             TRACE("Value in Filename includes path, overriding initdir: %s, %s\n",
1133                  debugstr_w(fodInfos->filename), debugstr_w(fodInfos->initdir));
1134          }
1135          SetDlgItemTextW(hwnd, IDC_FILENAME, fodInfos->filename);
1136       }
1137
1138       /* 4. win98+ and win2000+ if any files of specified filter types in
1139             current directory, use it                                      */
1140       if ( win98plus && handledPath == FALSE &&
1141            fodInfos->filter && *fodInfos->filter) {
1142
1143          BOOL   searchMore = TRUE;
1144          LPCWSTR lpstrPos = fodInfos->filter;
1145          WIN32_FIND_DATAW FindFileData;
1146          HANDLE hFind;
1147
1148          while (searchMore)
1149          {
1150            /* filter is a list...  title\0ext\0......\0\0 */
1151
1152            /* Skip the title */
1153            if(! *lpstrPos) break;       /* end */
1154            lpstrPos += strlenW(lpstrPos) + 1;
1155
1156            /* See if any files exist in the current dir with this extension */
1157            if(! *lpstrPos) break;       /* end */
1158
1159            hFind = FindFirstFileW(lpstrPos, &FindFileData);
1160
1161            if (hFind == INVALID_HANDLE_VALUE) {
1162                /* None found - continue search */
1163                lpstrPos += strlenW(lpstrPos) + 1;
1164
1165            } else {
1166                searchMore = FALSE;
1167
1168                if(fodInfos->initdir)
1169                   MemFree(fodInfos->initdir);
1170                fodInfos->initdir = MemAlloc(MAX_PATH*sizeof(WCHAR));
1171                GetCurrentDirectoryW(MAX_PATH, fodInfos->initdir);
1172
1173                handledPath = TRUE;
1174                TRACE("No initial dir specified, but files of type %s found in current, so using it\n",
1175                  debugstr_w(lpstrPos));
1176                break;
1177            }
1178          }
1179       }
1180
1181       /* 5. Win2000+: FIXME: Next, Recently used? Not sure how windows does this */
1182
1183       /* 6. Win98+ and 2000+: Use personal files dir, others use current dir */
1184       if (handledPath == FALSE && (win2000plus || win98plus)) {
1185           fodInfos->initdir = MemAlloc(MAX_PATH*sizeof(WCHAR));
1186
1187           if(FAILED(COMDLG32_SHGetFolderPathW(hwnd, CSIDL_PERSONAL, 0, 0, fodInfos->initdir)))
1188           {
1189             if(FAILED(COMDLG32_SHGetFolderPathW(hwnd, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, 0, 0, fodInfos->initdir)))
1190             {
1191                 /* last fallback */
1192                 GetCurrentDirectoryW(MAX_PATH, fodInfos->initdir);
1193                 TRACE("No personal or desktop dir, using cwd as failsafe: %s\n", debugstr_w(fodInfos->initdir));
1194             } else {
1195                 TRACE("No personal dir, using desktop instead: %s\n", debugstr_w(fodInfos->initdir));
1196             }
1197           } else {
1198             TRACE("No initial dir specified, using personal files dir of %s\n", debugstr_w(fodInfos->initdir));
1199           }
1200           handledPath = TRUE;
1201       } else if (handledPath==FALSE) {
1202           fodInfos->initdir = MemAlloc(MAX_PATH*sizeof(WCHAR));
1203           GetCurrentDirectoryW(MAX_PATH, fodInfos->initdir);
1204           handledPath = TRUE;
1205           TRACE("No initial dir specified, using current dir of %s\n", debugstr_w(fodInfos->initdir));
1206       }
1207   }
1208   TRACE("After manipilation, file = %s, dir = %s\n", debugstr_w(fodInfos->filename), debugstr_w(fodInfos->initdir));
1209
1210   /* Must the open as read only check box be checked ?*/
1211   if(fodInfos->ofnInfos->Flags & OFN_READONLY)
1212   {
1213     SendDlgItemMessageA(hwnd,IDC_OPENREADONLY,BM_SETCHECK,(WPARAM)TRUE,0);
1214   }
1215
1216   /* Must the open as read only check box be hid ?*/
1217   if(fodInfos->ofnInfos->Flags & OFN_HIDEREADONLY)
1218   {
1219     ShowWindow(GetDlgItem(hwnd,IDC_OPENREADONLY),SW_HIDE);
1220   }
1221
1222   /* Must the help button be hid ?*/
1223   if (!(fodInfos->ofnInfos->Flags & OFN_SHOWHELP))
1224   {
1225     ShowWindow(GetDlgItem(hwnd, pshHelp), SW_HIDE);
1226   }
1227
1228   /* Resize the height, if open as read only checkbox ad help button
1229      are hidden and we are not using a custom template */
1230   if ( (fodInfos->ofnInfos->Flags & OFN_HIDEREADONLY) &&
1231        (!(fodInfos->ofnInfos->Flags &
1232          (OFN_SHOWHELP|OFN_ENABLETEMPLATE|OFN_ENABLETEMPLATEHANDLE))))
1233   {
1234     RECT rectDlg, rectHelp, rectCancel;
1235     GetWindowRect(hwnd, &rectDlg);
1236     GetWindowRect(GetDlgItem(hwnd, pshHelp), &rectHelp);
1237     GetWindowRect(GetDlgItem(hwnd, IDCANCEL), &rectCancel);
1238     /* subtract the height of the help button plus the space between
1239        the help button and the cancel button to the height of the dialog */
1240     SetWindowPos(hwnd, 0, 0, 0, rectDlg.right-rectDlg.left,
1241                  (rectDlg.bottom-rectDlg.top) - (rectHelp.bottom - rectCancel.bottom),
1242                  SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOZORDER);
1243   }
1244
1245   /* change Open to Save FIXME: use resources */
1246   if (fodInfos->DlgInfos.dwDlgProp & FODPROP_SAVEDLG)
1247   {
1248       SetDlgItemTextA(hwnd,IDOK,"&Save");
1249       SetDlgItemTextA(hwnd,IDC_LOOKINSTATIC,"Save &in");
1250   }
1251   return 0;
1252 }
1253
1254 /***********************************************************************
1255  *      FILEDLG95_FillControls
1256  *
1257  * WM_INITDIALOG message handler (after hook notification)
1258  */
1259 static LRESULT FILEDLG95_FillControls(HWND hwnd, WPARAM wParam, LPARAM lParam)
1260 {
1261   LPITEMIDLIST pidlItemId = NULL;
1262
1263   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) lParam;
1264
1265   TRACE("dir=%s file=%s\n",
1266   debugstr_w(fodInfos->initdir), debugstr_w(fodInfos->filename));
1267
1268   /* Get the initial directory pidl */
1269
1270   if(!(pidlItemId = GetPidlFromName(fodInfos->Shell.FOIShellFolder,fodInfos->initdir)))
1271   {
1272     WCHAR path[MAX_PATH];
1273
1274     GetCurrentDirectoryW(MAX_PATH,path);
1275     pidlItemId = GetPidlFromName(fodInfos->Shell.FOIShellFolder, path);
1276   }
1277
1278   /* Initialise shell objects */
1279   FILEDLG95_SHELL_Init(hwnd);
1280
1281   /* Initialize the Look In combo box */
1282   FILEDLG95_LOOKIN_Init(fodInfos->DlgInfos.hwndLookInCB);
1283
1284   /* Initialize the filter combo box */
1285   FILEDLG95_FILETYPE_Init(hwnd);
1286
1287   /* Browse to the initial directory */
1288   IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser,pidlItemId, SBSP_ABSOLUTE);
1289
1290   /* Free pidlItem memory */
1291   COMDLG32_SHFree(pidlItemId);
1292
1293   return TRUE;
1294 }
1295 /***********************************************************************
1296  *      FILEDLG95_Clean
1297  *
1298  * Regroups all the cleaning functions of the filedlg
1299  */
1300 void FILEDLG95_Clean(HWND hwnd)
1301 {
1302       FILEDLG95_FILETYPE_Clean(hwnd);
1303       FILEDLG95_LOOKIN_Clean(hwnd);
1304       FILEDLG95_SHELL_Clean(hwnd);
1305 }
1306 /***********************************************************************
1307  *      FILEDLG95_OnWMCommand
1308  *
1309  * WM_COMMAND message handler
1310  */
1311 static LRESULT FILEDLG95_OnWMCommand(HWND hwnd, WPARAM wParam, LPARAM lParam)
1312 {
1313   WORD wNotifyCode = HIWORD(wParam); /* notification code */
1314   WORD wID = LOWORD(wParam);         /* item, control, or accelerator identifier */
1315   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1316
1317   switch(wID)
1318   {
1319     /* OK button */
1320   case IDOK:
1321     FILEDLG95_OnOpen(hwnd);
1322     break;
1323     /* Cancel button */
1324   case IDCANCEL:
1325     FILEDLG95_Clean(hwnd);
1326     EndDialog(hwnd, FALSE);
1327     break;
1328     /* Filetype combo box */
1329   case IDC_FILETYPE:
1330     FILEDLG95_FILETYPE_OnCommand(hwnd,wNotifyCode);
1331     break;
1332     /* LookIn combo box */
1333   case IDC_LOOKIN:
1334     FILEDLG95_LOOKIN_OnCommand(hwnd,wNotifyCode);
1335     break;
1336
1337   /* --- toolbar --- */
1338     /* Up folder button */
1339   case FCIDM_TB_UPFOLDER:
1340     FILEDLG95_SHELL_UpFolder(hwnd);
1341     break;
1342     /* New folder button */
1343   case FCIDM_TB_NEWFOLDER:
1344     FILEDLG95_SHELL_ExecuteCommand(hwnd,CMDSTR_NEWFOLDER);
1345     break;
1346     /* List option button */
1347   case FCIDM_TB_SMALLICON:
1348     FILEDLG95_SHELL_ExecuteCommand(hwnd,CMDSTR_VIEWLIST);
1349     break;
1350     /* Details option button */
1351   case FCIDM_TB_REPORTVIEW:
1352     FILEDLG95_SHELL_ExecuteCommand(hwnd,CMDSTR_VIEWDETAILS);
1353     break;
1354     /* Details option button */
1355   case FCIDM_TB_DESKTOP:
1356     FILEDLG95_SHELL_BrowseToDesktop(hwnd);
1357     break;
1358
1359   case IDC_FILENAME:
1360     break;
1361
1362   }
1363   /* Do not use the listview selection anymore */
1364   fodInfos->DlgInfos.dwDlgProp &= ~FODPROP_USEVIEW;
1365   return 0;
1366 }
1367
1368 /***********************************************************************
1369  *      FILEDLG95_OnWMGetIShellBrowser
1370  *
1371  * WM_GETISHELLBROWSER message handler
1372  */
1373 static LRESULT FILEDLG95_OnWMGetIShellBrowser(HWND hwnd)
1374 {
1375
1376   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1377
1378   TRACE("\n");
1379
1380   SetWindowLongA(hwnd,DWL_MSGRESULT,(LONG)fodInfos->Shell.FOIShellBrowser);
1381
1382   return TRUE;
1383 }
1384
1385
1386 /***********************************************************************
1387  *      FILEDLG95_OnOpenMultipleFiles
1388  *
1389  * Handles the opening of multiple files.
1390  *
1391  * FIXME
1392  *  check destination buffer size
1393  */
1394 BOOL FILEDLG95_OnOpenMultipleFiles(HWND hwnd, LPWSTR lpstrFileList, UINT nFileCount, UINT sizeUsed)
1395 {
1396   WCHAR   lpstrPathSpec[MAX_PATH] = {0};
1397   UINT   nCount, nSizePath;
1398   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1399
1400   TRACE("\n");
1401
1402   if(fodInfos->unicode)
1403   {
1404      LPOPENFILENAMEW ofn = (LPOPENFILENAMEW) fodInfos->ofnInfos;
1405      ofn->lpstrFile[0] = '\0';
1406   }
1407   else
1408   {
1409      LPOPENFILENAMEA ofn = fodInfos->ofnInfos;
1410      ofn->lpstrFile[0] = '\0';
1411   }
1412
1413   SHGetPathFromIDListW( fodInfos->ShellInfos.pidlAbsCurrent, lpstrPathSpec );
1414
1415   if ( !(fodInfos->ofnInfos->Flags & OFN_NOVALIDATE) &&
1416       ( fodInfos->ofnInfos->Flags & OFN_FILEMUSTEXIST) &&
1417        ! ( fodInfos->DlgInfos.dwDlgProp & FODPROP_SAVEDLG ) )
1418   {
1419     LPWSTR lpstrTemp = lpstrFileList;
1420
1421     for ( nCount = 0; nCount < nFileCount; nCount++ )
1422     {
1423       LPITEMIDLIST pidl;
1424
1425       pidl = GetPidlFromName(fodInfos->Shell.FOIShellFolder, lpstrTemp);
1426       if (!pidl)
1427       {
1428         WCHAR lpstrNotFound[100];
1429         WCHAR lpstrMsg[100];
1430         WCHAR tmp[400];
1431         WCHAR nl[] = {'\n',0};
1432
1433         LoadStringW(COMMDLG_hInstance32, IDS_FILENOTFOUND, lpstrNotFound, 100);
1434         LoadStringW(COMMDLG_hInstance32, IDS_VERIFYFILE, lpstrMsg, 100);
1435
1436         strcpyW(tmp, lpstrTemp);
1437         strcatW(tmp, nl);
1438         strcatW(tmp, lpstrNotFound);
1439         strcatW(tmp, nl);
1440         strcatW(tmp, lpstrMsg);
1441
1442         MessageBoxW(hwnd, tmp, fodInfos->title, MB_OK | MB_ICONEXCLAMATION);
1443         return FALSE;
1444       }
1445
1446       /* move to the next file in the list of files */
1447       lpstrTemp += strlenW(lpstrTemp) + 1;
1448       COMDLG32_SHFree(pidl);
1449     }
1450   }
1451
1452   nSizePath = strlenW(lpstrPathSpec) + 1;
1453   if ( !(fodInfos->ofnInfos->Flags & OFN_EXPLORER) )
1454   {
1455     /* For "oldstyle" dialog the components have to
1456        be spearated by blanks (not '\0'!) and short
1457        filenames have to be used! */
1458     FIXME("Components have to be separated by blanks");
1459   }
1460   if(fodInfos->unicode)
1461   {
1462     LPOPENFILENAMEW ofn = (LPOPENFILENAMEW) fodInfos->ofnInfos;
1463     strcpyW( ofn->lpstrFile, lpstrPathSpec);
1464     memcpy( ofn->lpstrFile + nSizePath, lpstrFileList, sizeUsed*sizeof(WCHAR) );
1465   }
1466   else
1467   {
1468     LPOPENFILENAMEA ofn = fodInfos->ofnInfos;
1469
1470     if (ofn->lpstrFile != NULL)
1471     {
1472       WideCharToMultiByte(CP_ACP, 0, lpstrPathSpec, -1,
1473                           ofn->lpstrFile, ofn->nMaxFile, NULL, NULL);
1474       if (ofn->nMaxFile > nSizePath)
1475       {
1476         WideCharToMultiByte(CP_ACP, 0, lpstrFileList, sizeUsed,
1477                             ofn->lpstrFile + nSizePath,
1478                             ofn->nMaxFile - nSizePath, NULL, NULL);
1479       }
1480     }
1481   }
1482
1483   fodInfos->ofnInfos->nFileOffset = nSizePath + 1;
1484   fodInfos->ofnInfos->nFileExtension = 0;
1485
1486   /* clean and exit */
1487   FILEDLG95_Clean(hwnd);
1488   return EndDialog(hwnd,TRUE);
1489 }
1490
1491 /***********************************************************************
1492  *      FILEDLG95_OnOpen
1493  *
1494  * Ok button WM_COMMAND message handler
1495  *
1496  * If the function succeeds, the return value is nonzero.
1497  */
1498 #define ONOPEN_BROWSE 1
1499 #define ONOPEN_OPEN   2
1500 #define ONOPEN_SEARCH 3
1501 static void FILEDLG95_OnOpenMessage(HWND hwnd, int idCaption, int idText)
1502 {
1503   char strMsgTitle[MAX_PATH];
1504   char strMsgText [MAX_PATH];
1505   if (idCaption)
1506     LoadStringA(COMDLG32_hInstance, idCaption, strMsgTitle, sizeof(strMsgTitle));
1507   else
1508     strMsgTitle[0] = '\0';
1509   LoadStringA(COMDLG32_hInstance, idText, strMsgText, sizeof(strMsgText));
1510   MessageBoxA(hwnd,strMsgText, strMsgTitle, MB_OK | MB_ICONHAND);
1511 }
1512
1513 BOOL FILEDLG95_OnOpen(HWND hwnd)
1514 {
1515   LPWSTR lpstrFileList;
1516   UINT nFileCount = 0;
1517   UINT sizeUsed = 0;
1518   BOOL ret = TRUE;
1519   WCHAR lpstrPathAndFile[MAX_PATH];
1520   WCHAR lpstrTemp[MAX_PATH];
1521   LPSHELLFOLDER lpsf = NULL;
1522   int nOpenAction;
1523   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1524
1525   TRACE("hwnd=0x%04x\n", hwnd);
1526
1527   /* get the files from the edit control */
1528   nFileCount = FILEDLG95_FILENAME_GetFileNames(hwnd, &lpstrFileList, &sizeUsed);
1529
1530   /* try if the user selected a folder in the shellview */
1531   if(nFileCount == 0)
1532   {
1533       BrowseSelectedFolder(hwnd);
1534       return FALSE;
1535   }
1536
1537   if(nFileCount > 1)
1538   {
1539       ret = FILEDLG95_OnOpenMultipleFiles(hwnd, lpstrFileList, nFileCount, sizeUsed);
1540       goto ret;
1541   }
1542
1543   TRACE("count=%u len=%u file=%s\n", nFileCount, sizeUsed, debugstr_w(lpstrFileList));
1544
1545 /*
1546   Step 1:  Build a complete path name from the current folder and
1547   the filename or path in the edit box.
1548   Special cases:
1549   - the path in the edit box is a root path
1550     (with or without drive letter)
1551   - the edit box contains ".." (or a path with ".." in it)
1552 */
1553
1554   /* Get the current directory name */
1555   if (!SHGetPathFromIDListW(fodInfos->ShellInfos.pidlAbsCurrent, lpstrPathAndFile))
1556   {
1557     /* we are in a special folder, default to desktop */
1558     if(FAILED(COMDLG32_SHGetFolderPathW(hwnd, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, 0, 0, lpstrPathAndFile)))
1559     {
1560       /* last fallback */
1561       GetCurrentDirectoryW(MAX_PATH, lpstrPathAndFile);
1562     }
1563   }
1564   PathAddBackslashW(lpstrPathAndFile);
1565
1566   TRACE("current directory=%s\n", debugstr_w(lpstrPathAndFile));
1567
1568   /* if the user specifyed a fully qualified path use it */
1569   if(PathIsRelativeW(lpstrFileList))
1570   {
1571     strcatW(lpstrPathAndFile, lpstrFileList);
1572   }
1573   else
1574   {
1575     /* does the path have a drive letter? */
1576     if (PathGetDriveNumberW(lpstrFileList) == -1)
1577       strcpyW(lpstrPathAndFile+2, lpstrFileList);
1578     else
1579       strcpyW(lpstrPathAndFile, lpstrFileList);
1580   }
1581
1582   /* resolve "." and ".." */
1583   PathCanonicalizeW(lpstrTemp, lpstrPathAndFile );
1584   strcpyW(lpstrPathAndFile, lpstrTemp);
1585   TRACE("canon=%s\n", debugstr_w(lpstrPathAndFile));
1586
1587   MemFree(lpstrFileList);
1588
1589 /*
1590   Step 2: here we have a cleaned up path
1591
1592   We have to parse the path step by step to see if we have to browse
1593   to a folder if the path points to a directory or the last
1594   valid element is a directory.
1595
1596   valid variables:
1597     lpstrPathAndFile: cleaned up path
1598  */
1599
1600   nOpenAction = ONOPEN_BROWSE;
1601
1602   /* dont apply any checks with OFN_NOVALIDATE */
1603   {
1604     LPWSTR lpszTemp, lpszTemp1;
1605     LPITEMIDLIST pidl = NULL;
1606     WCHAR szwInvalid[] = { '/',':','<','>','|', 0};
1607
1608     /* check for invalid chars */
1609     if((strpbrkW(lpstrPathAndFile+3, szwInvalid) != NULL) && !(fodInfos->ofnInfos->Flags & OFN_NOVALIDATE))
1610     {
1611       FILEDLG95_OnOpenMessage(hwnd, IDS_INVALID_FILENAME_TITLE, IDS_INVALID_FILENAME);
1612       ret = FALSE;
1613       goto ret;
1614     }
1615
1616     if (FAILED (SHGetDesktopFolder(&lpsf))) return FALSE;
1617
1618     lpszTemp1 = lpszTemp = lpstrPathAndFile;
1619     while (lpszTemp1)
1620     {
1621       LPSHELLFOLDER lpsfChild;
1622       WCHAR lpwstrTemp[MAX_PATH];
1623       DWORD dwEaten, dwAttributes;
1624       LPWSTR p;
1625
1626       strcpyW(lpwstrTemp, lpszTemp);
1627       p = PathFindNextComponentW(lpwstrTemp);
1628
1629       if (!p) break; /* end of path */
1630
1631       *p = 0;
1632       lpszTemp = lpszTemp + strlenW(lpwstrTemp);
1633
1634       if(*lpszTemp==0)
1635       {
1636         WCHAR wszWild[] = { '*', '?', 0 };
1637         /* if the last element is a wildcard do a search */
1638         if(strpbrkW(lpszTemp1, wszWild) != NULL)
1639         {
1640           nOpenAction = ONOPEN_SEARCH;
1641           break;
1642         }
1643       }
1644       lpszTemp1 = lpszTemp;
1645
1646       TRACE("parse now=%s next=%s sf=%p\n",debugstr_w(lpwstrTemp), debugstr_w(lpszTemp), lpsf);
1647
1648       if(lstrlenW(lpwstrTemp)==2) PathAddBackslashW(lpwstrTemp);
1649
1650       dwAttributes = SFGAO_FOLDER;
1651       if(SUCCEEDED(IShellFolder_ParseDisplayName(lpsf, hwnd, NULL, lpwstrTemp, &dwEaten, &pidl, &dwAttributes)))
1652       {
1653         /* the path component is valid, we have a pidl of the next path component */
1654         TRACE("parse OK attr=0x%08lx pidl=%p\n", dwAttributes, pidl);
1655         if(dwAttributes & SFGAO_FOLDER)
1656         {
1657           if(FAILED(IShellFolder_BindToObject(lpsf, pidl, 0, &IID_IShellFolder, (LPVOID*)&lpsfChild)))
1658           {
1659             ERR("bind to failed\n"); /* should not fail */
1660             break;
1661           }
1662           IShellFolder_Release(lpsf);
1663           lpsf = lpsfChild;
1664           lpsfChild = NULL;
1665         }
1666         else
1667         {
1668           TRACE("value\n");
1669
1670           /* end dialog, return value */
1671           nOpenAction = ONOPEN_OPEN;
1672           break;
1673         }
1674         COMDLG32_SHFree(pidl);
1675         pidl = NULL;
1676       }
1677       else if (!(fodInfos->ofnInfos->Flags & OFN_NOVALIDATE))
1678       {
1679         if(*lpszTemp)   /* points to trailing null for last path element */
1680         {
1681           if(fodInfos->ofnInfos->Flags & OFN_PATHMUSTEXIST)
1682           {
1683             FILEDLG95_OnOpenMessage(hwnd, 0, IDS_PATHNOTEXISTING);
1684             break;
1685           }
1686         }
1687         else
1688         {
1689           if( (fodInfos->ofnInfos->Flags & OFN_FILEMUSTEXIST) &&
1690              !( fodInfos->DlgInfos.dwDlgProp & FODPROP_SAVEDLG ) )
1691           {
1692             FILEDLG95_OnOpenMessage(hwnd, 0, IDS_FILENOTEXISTING);
1693             break;
1694           }
1695         }
1696         /* change to the current folder */
1697         nOpenAction = ONOPEN_OPEN;
1698         break;
1699       }
1700       else
1701       {
1702         nOpenAction = ONOPEN_OPEN;
1703         break;
1704       }
1705     }
1706     if(pidl) COMDLG32_SHFree(pidl);
1707   }
1708
1709 /*
1710   Step 3: here we have a cleaned up and validated path
1711
1712   valid variables:
1713    lpsf:             ShellFolder bound to the rightmost valid path component
1714    lpstrPathAndFile: cleaned up path
1715    nOpenAction:      action to do
1716 */
1717   TRACE("end validate sf=%p\n", lpsf);
1718
1719   switch(nOpenAction)
1720   {
1721     case ONOPEN_SEARCH:   /* set the current filter to the file mask and refresh */
1722       TRACE("ONOPEN_SEARCH %s\n", debugstr_w(lpstrPathAndFile));
1723       {
1724         int iPos;
1725         LPWSTR lpszTemp = PathFindFileNameW(lpstrPathAndFile);
1726         DWORD len;
1727
1728         /* replace the current filter */
1729         if(fodInfos->ShellInfos.lpstrCurrentFilter)
1730           MemFree((LPVOID)fodInfos->ShellInfos.lpstrCurrentFilter);
1731         len = strlenW(lpszTemp)+1;
1732         fodInfos->ShellInfos.lpstrCurrentFilter = MemAlloc(len * sizeof(WCHAR));
1733         strcpyW( fodInfos->ShellInfos.lpstrCurrentFilter, lpszTemp);
1734
1735         /* set the filter cb to the extension when possible */
1736         if(-1 < (iPos = FILEDLG95_FILETYPE_SearchExt(fodInfos->DlgInfos.hwndFileTypeCB, lpszTemp)))
1737         CBSetCurSel(fodInfos->DlgInfos.hwndFileTypeCB, iPos);
1738       }
1739       /* fall through */
1740     case ONOPEN_BROWSE:   /* browse to the highest folder we could bind to */
1741       TRACE("ONOPEN_BROWSE\n");
1742       {
1743         IPersistFolder2 * ppf2;
1744         if(SUCCEEDED(IShellFolder_QueryInterface( lpsf, &IID_IPersistFolder2, (LPVOID*)&ppf2)))
1745         {
1746           LPITEMIDLIST pidlCurrent;
1747           IPersistFolder2_GetCurFolder(ppf2, &pidlCurrent);
1748           IPersistFolder2_Release(ppf2);
1749           if( ! COMDLG32_PIDL_ILIsEqual(pidlCurrent, fodInfos->ShellInfos.pidlAbsCurrent))
1750           {
1751             IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser, pidlCurrent, SBSP_ABSOLUTE);
1752           }
1753           else if( nOpenAction == ONOPEN_SEARCH )
1754           {
1755             IShellView_Refresh(fodInfos->Shell.FOIShellView);
1756           }
1757           COMDLG32_SHFree(pidlCurrent);
1758         }
1759       }
1760       ret = FALSE;
1761       break;
1762     case ONOPEN_OPEN:   /* fill in the return struct and close the dialog */
1763       TRACE("ONOPEN_OPEN %s\n", debugstr_w(lpstrPathAndFile));
1764       {
1765         /* add default extension */
1766         if (fodInfos->defext)
1767         {
1768           if (! *PathFindExtensionW(lpstrPathAndFile))
1769           {
1770             /* only add "." in case a default extension does exist */
1771             if (*fodInfos->defext != '\0')
1772             {
1773                 const WCHAR szwDot[] = {'.',0};
1774                 int PathLength = strlenW(lpstrPathAndFile);
1775
1776                 strcatW(lpstrPathAndFile, szwDot);
1777                 strcatW(lpstrPathAndFile, fodInfos->defext);
1778
1779                 /* if file does not exist try without extension */
1780                 if (!PathFileExistsW(lpstrPathAndFile))
1781                   lpstrPathAndFile[PathLength] = '\0';
1782             }
1783           }
1784         }
1785
1786         /* Check that the size of the file does not exceed buffer size.
1787              (Allow for extra \0 if OFN_MULTISELECT is set.) */
1788         if(strlenW(lpstrPathAndFile) < fodInfos->ofnInfos->nMaxFile -
1789             ((fodInfos->ofnInfos->Flags & OFN_ALLOWMULTISELECT) ? 1 : 0))
1790         {
1791           LPWSTR lpszTemp;
1792
1793           /* fill destination buffer */
1794           if (fodInfos->ofnInfos->lpstrFile)
1795           {
1796              if(fodInfos->unicode)
1797              {
1798                LPOPENFILENAMEW ofn = (LPOPENFILENAMEW) fodInfos->ofnInfos;
1799
1800                strncpyW(ofn->lpstrFile, lpstrPathAndFile, ofn->nMaxFile);
1801                if (ofn->Flags & OFN_ALLOWMULTISELECT)
1802                  ofn->lpstrFile[lstrlenW(ofn->lpstrFile) + 1] = '\0';
1803              }
1804              else
1805              {
1806                LPOPENFILENAMEA ofn = fodInfos->ofnInfos;
1807
1808                WideCharToMultiByte(CP_ACP, 0, lpstrPathAndFile, -1,
1809                                    ofn->lpstrFile, ofn->nMaxFile, NULL, NULL);
1810                if (ofn->Flags & OFN_ALLOWMULTISELECT)
1811                  ofn->lpstrFile[lstrlenA(ofn->lpstrFile) + 1] = '\0';
1812              }
1813           }
1814
1815           /* set filename offset */
1816           lpszTemp = PathFindFileNameW(lpstrPathAndFile);
1817           fodInfos->ofnInfos->nFileOffset = (lpszTemp - lpstrPathAndFile);
1818
1819           /* set extension offset */
1820           lpszTemp = PathFindExtensionW(lpstrPathAndFile);
1821           fodInfos->ofnInfos->nFileExtension = (*lpszTemp) ? (lpszTemp - lpstrPathAndFile) + 1 : 0;
1822
1823           /* set the lpstrFileTitle */
1824           if(fodInfos->ofnInfos->lpstrFileTitle)
1825           {
1826             LPWSTR lpstrFileTitle = PathFindFileNameW(lpstrPathAndFile);
1827             if(fodInfos->unicode)
1828             {
1829               LPOPENFILENAMEW ofn = (LPOPENFILENAMEW) fodInfos->ofnInfos;
1830               strncpyW(ofn->lpstrFileTitle, lpstrFileTitle, ofn->nMaxFileTitle);
1831             }
1832             else
1833             {
1834               LPOPENFILENAMEA ofn = fodInfos->ofnInfos;
1835               WideCharToMultiByte(CP_ACP, 0, lpstrFileTitle, -1,
1836                     ofn->lpstrFileTitle, ofn->nMaxFileTitle, NULL, NULL);
1837             }
1838           }
1839
1840           /* ask the hook if we can close */
1841           if(IsHooked(fodInfos))
1842           {
1843             TRACE("---\n");
1844             /* First send CDN_FILEOK as MSDN doc says */
1845             SendCustomDlgNotificationMessage(hwnd,CDN_FILEOK);
1846
1847             /* FIXME we are sending ASCII-structures. Does not work with NT */
1848             CallWindowProcA((WNDPROC)fodInfos->ofnInfos->lpfnHook,
1849                             fodInfos->DlgInfos.hwndCustomDlg,
1850                             fodInfos->HookMsg.fileokstring, 0, (LPARAM)fodInfos->ofnInfos);
1851             if (GetWindowLongA(fodInfos->DlgInfos.hwndCustomDlg, DWL_MSGRESULT))
1852             {
1853               TRACE("canceled\n");
1854               ret = FALSE;
1855               goto ret;
1856             }
1857           }
1858
1859           TRACE("close\n");
1860           FILEDLG95_Clean(hwnd);
1861           ret = EndDialog(hwnd, TRUE);
1862         }
1863         else
1864         {
1865           /* FIXME set error FNERR_BUFFERTOSMALL */
1866           FILEDLG95_Clean(hwnd);
1867           ret = EndDialog(hwnd, FALSE);
1868         }
1869         goto ret;
1870       }
1871       break;
1872   }
1873
1874 ret:
1875   if(lpsf) IShellFolder_Release(lpsf);
1876   return ret;
1877 }
1878
1879 /***********************************************************************
1880  *      FILEDLG95_SHELL_Init
1881  *
1882  * Initialisation of the shell objects
1883  */
1884 static HRESULT FILEDLG95_SHELL_Init(HWND hwnd)
1885 {
1886   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1887
1888   TRACE("\n");
1889
1890   /*
1891    * Initialisation of the FileOpenDialogInfos structure
1892    */
1893
1894   /* Shell */
1895
1896   /*ShellInfos */
1897   fodInfos->ShellInfos.hwndOwner = hwnd;
1898
1899   /* Disable multi-select if flag not set */
1900   if (!(fodInfos->ofnInfos->Flags & OFN_ALLOWMULTISELECT))
1901   {
1902      fodInfos->ShellInfos.folderSettings.fFlags |= FWF_SINGLESEL;
1903   }
1904   fodInfos->ShellInfos.folderSettings.fFlags |= FWF_AUTOARRANGE | FWF_ALIGNLEFT;
1905   fodInfos->ShellInfos.folderSettings.ViewMode = FVM_LIST;
1906
1907   GetWindowRect(GetDlgItem(hwnd,IDC_SHELLSTATIC),&fodInfos->ShellInfos.rectView);
1908   ScreenToClient(hwnd,(LPPOINT)&fodInfos->ShellInfos.rectView.left);
1909   ScreenToClient(hwnd,(LPPOINT)&fodInfos->ShellInfos.rectView.right);
1910
1911   /* Construct the IShellBrowser interface */
1912   fodInfos->Shell.FOIShellBrowser = IShellBrowserImpl_Construct(hwnd);
1913
1914   return NOERROR;
1915 }
1916
1917 /***********************************************************************
1918  *      FILEDLG95_SHELL_ExecuteCommand
1919  *
1920  * Change the folder option and refresh the view
1921  * If the function succeeds, the return value is nonzero.
1922  */
1923 static BOOL FILEDLG95_SHELL_ExecuteCommand(HWND hwnd, LPCSTR lpVerb)
1924 {
1925   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1926
1927   IContextMenu * pcm;
1928   TRACE("(0x%08x,%p)\n", hwnd, lpVerb);
1929
1930   if(SUCCEEDED(IShellView_GetItemObject(fodInfos->Shell.FOIShellView,
1931                                         SVGIO_BACKGROUND,
1932                                         &IID_IContextMenu,
1933                                         (LPVOID*)&pcm)))
1934   {
1935     CMINVOKECOMMANDINFO ci;
1936     ZeroMemory(&ci, sizeof(CMINVOKECOMMANDINFO));
1937     ci.cbSize = sizeof(CMINVOKECOMMANDINFO);
1938     ci.lpVerb = lpVerb;
1939     ci.hwnd = hwnd;
1940
1941     IContextMenu_InvokeCommand(pcm, &ci);
1942     IContextMenu_Release(pcm);
1943   }
1944
1945   return FALSE;
1946 }
1947
1948 /***********************************************************************
1949  *      FILEDLG95_SHELL_UpFolder
1950  *
1951  * Browse to the specified object
1952  * If the function succeeds, the return value is nonzero.
1953  */
1954 static BOOL FILEDLG95_SHELL_UpFolder(HWND hwnd)
1955 {
1956   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1957
1958   TRACE("\n");
1959
1960   if(SUCCEEDED(IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser,
1961                                           NULL,
1962                                           SBSP_PARENT)))
1963   {
1964     return TRUE;
1965   }
1966   return FALSE;
1967 }
1968
1969 /***********************************************************************
1970  *      FILEDLG95_SHELL_BrowseToDesktop
1971  *
1972  * Browse to the Desktop
1973  * If the function succeeds, the return value is nonzero.
1974  */
1975 static BOOL FILEDLG95_SHELL_BrowseToDesktop(HWND hwnd)
1976 {
1977   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1978   LPITEMIDLIST pidl;
1979   HRESULT hres;
1980
1981   TRACE("\n");
1982
1983   SHGetSpecialFolderLocation(0,CSIDL_DESKTOP,&pidl);
1984   hres = IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser, pidl, SBSP_ABSOLUTE);
1985   COMDLG32_SHFree(pidl);
1986   return SUCCEEDED(hres);
1987 }
1988 /***********************************************************************
1989  *      FILEDLG95_SHELL_Clean
1990  *
1991  * Cleans the memory used by shell objects
1992  */
1993 static void FILEDLG95_SHELL_Clean(HWND hwnd)
1994 {
1995     FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1996
1997     TRACE("\n");
1998
1999     COMDLG32_SHFree(fodInfos->ShellInfos.pidlAbsCurrent);
2000
2001     /* clean Shell interfaces */
2002     IShellView_DestroyViewWindow(fodInfos->Shell.FOIShellView);
2003     IShellView_Release(fodInfos->Shell.FOIShellView);
2004     IShellFolder_Release(fodInfos->Shell.FOIShellFolder);
2005     IShellBrowser_Release(fodInfos->Shell.FOIShellBrowser);
2006     if (fodInfos->Shell.FOIDataObject)
2007       IDataObject_Release(fodInfos->Shell.FOIDataObject);
2008 }
2009
2010 /***********************************************************************
2011  *      FILEDLG95_FILETYPE_Init
2012  *
2013  * Initialisation of the file type combo box
2014  */
2015 static HRESULT FILEDLG95_FILETYPE_Init(HWND hwnd)
2016 {
2017   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
2018
2019   TRACE("\n");
2020
2021   if(fodInfos->filter)
2022   {
2023     int nFilters = 0;   /* number of filters */
2024     LPWSTR lpstrFilter;
2025     LPCWSTR lpstrPos = fodInfos->filter;
2026
2027     for(;;)
2028     {
2029       /* filter is a list...  title\0ext\0......\0\0
2030        * Set the combo item text to the title and the item data
2031        *  to the ext
2032        */
2033       LPCWSTR lpstrDisplay;
2034       LPWSTR lpstrExt;
2035
2036       /* Get the title */
2037       if(! *lpstrPos) break;    /* end */
2038       lpstrDisplay = lpstrPos;
2039       lpstrPos += strlenW(lpstrPos) + 1;
2040
2041       /* Copy the extensions */
2042       if (! *lpstrPos) return E_FAIL;   /* malformed filter */
2043       if (!(lpstrExt = MemAlloc((strlenW(lpstrPos)+1)*sizeof(WCHAR)))) return E_FAIL;
2044       strcpyW(lpstrExt,lpstrPos);
2045       lpstrPos += strlenW(lpstrPos) + 1;
2046
2047       /* Add the item at the end of the combo */
2048       CBAddStringW(fodInfos->DlgInfos.hwndFileTypeCB, lpstrDisplay);
2049       CBSetItemDataPtr(fodInfos->DlgInfos.hwndFileTypeCB, nFilters, lpstrExt);
2050       nFilters++;
2051     }
2052     /*
2053      * Set the current filter to the one specified
2054      * in the initialisation structure
2055      * FIXME: lpstrCustomFilter not handled at all
2056      */
2057
2058     /* set default filter index */
2059     if(fodInfos->ofnInfos->nFilterIndex == 0 && fodInfos->customfilter == NULL)
2060       fodInfos->ofnInfos->nFilterIndex = 1;
2061
2062     /* First, check to make sure our index isn't out of bounds. */
2063     if ( fodInfos->ofnInfos->nFilterIndex > nFilters )
2064       fodInfos->ofnInfos->nFilterIndex = nFilters;
2065
2066     /* Set the current index selection. */
2067     CBSetCurSel(fodInfos->DlgInfos.hwndFileTypeCB, fodInfos->ofnInfos->nFilterIndex-1);
2068
2069     /* Get the corresponding text string from the combo box. */
2070     lpstrFilter = (LPWSTR) CBGetItemDataPtr(fodInfos->DlgInfos.hwndFileTypeCB,
2071                                              fodInfos->ofnInfos->nFilterIndex-1);
2072
2073     if ((INT)lpstrFilter == CB_ERR)  /* control is empty */
2074       lpstrFilter = NULL;
2075
2076     if(lpstrFilter)
2077     {
2078       DWORD len;
2079       CharLowerW(lpstrFilter); /* lowercase */
2080       len = strlenW(lpstrFilter)+1;
2081       fodInfos->ShellInfos.lpstrCurrentFilter = MemAlloc( len * sizeof(WCHAR) );
2082       strcpyW(fodInfos->ShellInfos.lpstrCurrentFilter,lpstrFilter);
2083     }
2084   }
2085   return NOERROR;
2086 }
2087
2088 /***********************************************************************
2089  *      FILEDLG95_FILETYPE_OnCommand
2090  *
2091  * WM_COMMAND of the file type combo box
2092  * If the function succeeds, the return value is nonzero.
2093  */
2094 static BOOL FILEDLG95_FILETYPE_OnCommand(HWND hwnd, WORD wNotifyCode)
2095 {
2096   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
2097
2098   switch(wNotifyCode)
2099   {
2100     case CBN_SELENDOK:
2101     {
2102       LPWSTR lpstrFilter;
2103
2104       /* Get the current item of the filetype combo box */
2105       int iItem = CBGetCurSel(fodInfos->DlgInfos.hwndFileTypeCB);
2106
2107       /* set the current filter index - indexed from 1 */
2108       fodInfos->ofnInfos->nFilterIndex = iItem + 1;
2109
2110       /* Set the current filter with the current selection */
2111       if(fodInfos->ShellInfos.lpstrCurrentFilter)
2112          MemFree((LPVOID)fodInfos->ShellInfos.lpstrCurrentFilter);
2113
2114       lpstrFilter = (LPWSTR) CBGetItemDataPtr(fodInfos->DlgInfos.hwndFileTypeCB,
2115                                              iItem);
2116       if((int)lpstrFilter != CB_ERR)
2117       {
2118           DWORD len;
2119           CharLowerW(lpstrFilter); /* lowercase */
2120           len = strlenW(lpstrFilter)+1;
2121           fodInfos->ShellInfos.lpstrCurrentFilter = MemAlloc( len * sizeof(WCHAR) );
2122           strcpyW(fodInfos->ShellInfos.lpstrCurrentFilter,lpstrFilter);
2123           SendCustomDlgNotificationMessage(hwnd,CDN_TYPECHANGE);
2124       }
2125
2126       /* Refresh the actual view to display the included items*/
2127       IShellView_Refresh(fodInfos->Shell.FOIShellView);
2128     }
2129   }
2130   return FALSE;
2131 }
2132 /***********************************************************************
2133  *      FILEDLG95_FILETYPE_SearchExt
2134  *
2135  * searches for a extension in the filetype box
2136  */
2137 static int FILEDLG95_FILETYPE_SearchExt(HWND hwnd,LPCWSTR lpstrExt)
2138 {
2139   int i, iCount = CBGetCount(hwnd);
2140
2141   TRACE("%s\n", debugstr_w(lpstrExt));
2142
2143   if(iCount != CB_ERR)
2144   {
2145     for(i=0;i<iCount;i++)
2146     {
2147       if(!lstrcmpiW(lpstrExt,(LPWSTR)CBGetItemDataPtr(hwnd,i)))
2148           return i;
2149     }
2150   }
2151   return -1;
2152 }
2153
2154 /***********************************************************************
2155  *      FILEDLG95_FILETYPE_Clean
2156  *
2157  * Clean the memory used by the filetype combo box
2158  */
2159 static void FILEDLG95_FILETYPE_Clean(HWND hwnd)
2160 {
2161   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
2162   int iPos;
2163   int iCount = CBGetCount(fodInfos->DlgInfos.hwndFileTypeCB);
2164
2165   TRACE("\n");
2166
2167   /* Delete each string of the combo and their associated data */
2168   if(iCount != CB_ERR)
2169   {
2170     for(iPos = iCount-1;iPos>=0;iPos--)
2171     {
2172       MemFree((LPSTR) CBGetItemDataPtr(fodInfos->DlgInfos.hwndFileTypeCB,iPos));
2173       CBDeleteString(fodInfos->DlgInfos.hwndFileTypeCB,iPos);
2174     }
2175   }
2176   /* Current filter */
2177   if(fodInfos->ShellInfos.lpstrCurrentFilter)
2178      MemFree(fodInfos->ShellInfos.lpstrCurrentFilter);
2179
2180 }
2181
2182 /***********************************************************************
2183  *      FILEDLG95_LOOKIN_Init
2184  *
2185  * Initialisation of the look in combo box
2186  */
2187 static HRESULT FILEDLG95_LOOKIN_Init(HWND hwndCombo)
2188 {
2189   IShellFolder  *psfRoot, *psfDrives;
2190   IEnumIDList   *lpeRoot, *lpeDrives;
2191   LPITEMIDLIST  pidlDrives, pidlTmp, pidlTmp1, pidlAbsTmp;
2192
2193   LookInInfos *liInfos = MemAlloc(sizeof(LookInInfos));
2194
2195   TRACE("\n");
2196
2197   liInfos->iMaxIndentation = 0;
2198
2199   SetPropA(hwndCombo, LookInInfosStr, (HANDLE) liInfos);
2200
2201   /* set item height for both text field and listbox */
2202   CBSetItemHeight(hwndCombo,-1,GetSystemMetrics(SM_CYSMICON));
2203   CBSetItemHeight(hwndCombo,0,GetSystemMetrics(SM_CYSMICON));
2204
2205   /* Initialise data of Desktop folder */
2206   SHGetSpecialFolderLocation(0,CSIDL_DESKTOP,&pidlTmp);
2207   FILEDLG95_LOOKIN_AddItem(hwndCombo, pidlTmp,LISTEND);
2208   COMDLG32_SHFree(pidlTmp);
2209
2210   SHGetSpecialFolderLocation(0,CSIDL_DRIVES,&pidlDrives);
2211
2212   SHGetDesktopFolder(&psfRoot);
2213
2214   if (psfRoot)
2215   {
2216     /* enumerate the contents of the desktop */
2217     if(SUCCEEDED(IShellFolder_EnumObjects(psfRoot, hwndCombo, SHCONTF_FOLDERS, &lpeRoot)))
2218     {
2219       while (S_OK == IEnumIDList_Next(lpeRoot, 1, &pidlTmp, NULL))
2220       {
2221         FILEDLG95_LOOKIN_AddItem(hwndCombo, pidlTmp,LISTEND);
2222
2223         /* special handling for CSIDL_DRIVES */
2224         if (COMDLG32_PIDL_ILIsEqual(pidlTmp, pidlDrives))
2225         {
2226           if(SUCCEEDED(IShellFolder_BindToObject(psfRoot, pidlTmp, NULL, &IID_IShellFolder, (LPVOID*)&psfDrives)))
2227           {
2228             /* enumerate the drives */
2229             if(SUCCEEDED(IShellFolder_EnumObjects(psfDrives, hwndCombo,SHCONTF_FOLDERS, &lpeDrives)))
2230             {
2231               while (S_OK == IEnumIDList_Next(lpeDrives, 1, &pidlTmp1, NULL))
2232               {
2233                 pidlAbsTmp = COMDLG32_PIDL_ILCombine(pidlTmp, pidlTmp1);
2234                 FILEDLG95_LOOKIN_AddItem(hwndCombo, pidlAbsTmp,LISTEND);
2235                 COMDLG32_SHFree(pidlAbsTmp);
2236                 COMDLG32_SHFree(pidlTmp1);
2237               }
2238               IEnumIDList_Release(lpeDrives);
2239             }
2240             IShellFolder_Release(psfDrives);
2241           }
2242         }
2243         COMDLG32_SHFree(pidlTmp);
2244       }
2245       IEnumIDList_Release(lpeRoot);
2246     }
2247   }
2248
2249   IShellFolder_Release(psfRoot);
2250   COMDLG32_SHFree(pidlDrives);
2251   return NOERROR;
2252 }
2253
2254 /***********************************************************************
2255  *      FILEDLG95_LOOKIN_DrawItem
2256  *
2257  * WM_DRAWITEM message handler
2258  */
2259 static LRESULT FILEDLG95_LOOKIN_DrawItem(LPDRAWITEMSTRUCT pDIStruct)
2260 {
2261   COLORREF crWin = GetSysColor(COLOR_WINDOW);
2262   COLORREF crHighLight = GetSysColor(COLOR_HIGHLIGHT);
2263   COLORREF crText = GetSysColor(COLOR_WINDOWTEXT);
2264   RECT rectText;
2265   RECT rectIcon;
2266   SHFILEINFOA sfi;
2267   HIMAGELIST ilItemImage;
2268   int iIndentation;
2269   TEXTMETRICA tm;
2270   LPSFOLDER tmpFolder;
2271
2272
2273   LookInInfos *liInfos = (LookInInfos *)GetPropA(pDIStruct->hwndItem,LookInInfosStr);
2274
2275   TRACE("\n");
2276
2277   if(pDIStruct->itemID == -1)
2278     return 0;
2279
2280   if(!(tmpFolder = (LPSFOLDER) CBGetItemDataPtr(pDIStruct->hwndItem,
2281                             pDIStruct->itemID)))
2282     return 0;
2283
2284
2285   if(pDIStruct->itemID == liInfos->uSelectedItem)
2286   {
2287     ilItemImage = (HIMAGELIST) SHGetFileInfoA ((LPCSTR) tmpFolder->pidlItem,
2288                                                0,
2289                                                &sfi,
2290                                                sizeof (SHFILEINFOA),
2291                                                SHGFI_PIDL | SHGFI_SMALLICON |
2292                                                SHGFI_OPENICON | SHGFI_SYSICONINDEX    |
2293                                                SHGFI_DISPLAYNAME );
2294   }
2295   else
2296   {
2297     ilItemImage = (HIMAGELIST) SHGetFileInfoA ((LPCSTR) tmpFolder->pidlItem,
2298                                                   0,
2299                                                   &sfi,
2300                                                   sizeof (SHFILEINFOA),
2301                                                   SHGFI_PIDL | SHGFI_SMALLICON |
2302                                                   SHGFI_SYSICONINDEX |
2303                                                   SHGFI_DISPLAYNAME);
2304   }
2305
2306   /* Is this item selected ? */
2307   if(pDIStruct->itemState & ODS_SELECTED)
2308   {
2309     SetTextColor(pDIStruct->hDC,(0x00FFFFFF & ~(crText)));
2310     SetBkColor(pDIStruct->hDC,crHighLight);
2311     FillRect(pDIStruct->hDC,&pDIStruct->rcItem,GetSysColorBrush(COLOR_HIGHLIGHT));
2312   }
2313   else
2314   {
2315     SetTextColor(pDIStruct->hDC,crText);
2316     SetBkColor(pDIStruct->hDC,crWin);
2317     FillRect(pDIStruct->hDC,&pDIStruct->rcItem,GetSysColorBrush(COLOR_WINDOW));
2318   }
2319
2320   /* Do not indent item if drawing in the edit of the combo */
2321   if(pDIStruct->itemState & ODS_COMBOBOXEDIT)
2322   {
2323     iIndentation = 0;
2324     ilItemImage = (HIMAGELIST) SHGetFileInfoA ((LPCSTR) tmpFolder->pidlItem,
2325                                                 0,
2326                                                 &sfi,
2327                                                 sizeof (SHFILEINFOA),
2328                                                 SHGFI_PIDL | SHGFI_SMALLICON | SHGFI_OPENICON
2329                                                 | SHGFI_SYSICONINDEX | SHGFI_DISPLAYNAME  );
2330
2331   }
2332   else
2333   {
2334     iIndentation = tmpFolder->m_iIndent;
2335   }
2336   /* Draw text and icon */
2337
2338   /* Initialise the icon display area */
2339   rectIcon.left = pDIStruct->rcItem.left + ICONWIDTH/2 * iIndentation;
2340   rectIcon.top = pDIStruct->rcItem.top;
2341   rectIcon.right = rectIcon.left + ICONWIDTH;
2342   rectIcon.bottom = pDIStruct->rcItem.bottom;
2343
2344   /* Initialise the text display area */
2345   GetTextMetricsA(pDIStruct->hDC, &tm);
2346   rectText.left = rectIcon.right;
2347   rectText.top =
2348           (pDIStruct->rcItem.top + pDIStruct->rcItem.bottom - tm.tmHeight) / 2;
2349   rectText.right = pDIStruct->rcItem.right + XTEXTOFFSET;
2350   rectText.bottom =
2351           (pDIStruct->rcItem.top + pDIStruct->rcItem.bottom + tm.tmHeight) / 2;
2352
2353   /* Draw the icon from the image list */
2354   ImageList_Draw(ilItemImage,
2355                  sfi.iIcon,
2356                  pDIStruct->hDC,
2357                  rectIcon.left,
2358                  rectIcon.top,
2359                  ILD_TRANSPARENT );
2360
2361   /* Draw the associated text */
2362   if(sfi.szDisplayName)
2363     TextOutA(pDIStruct->hDC,rectText.left,rectText.top,sfi.szDisplayName,strlen(sfi.szDisplayName));
2364
2365
2366   return NOERROR;
2367 }
2368
2369 /***********************************************************************
2370  *      FILEDLG95_LOOKIN_OnCommand
2371  *
2372  * LookIn combo box WM_COMMAND message handler
2373  * If the function succeeds, the return value is nonzero.
2374  */
2375 static BOOL FILEDLG95_LOOKIN_OnCommand(HWND hwnd, WORD wNotifyCode)
2376 {
2377   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
2378
2379   TRACE("%p\n", fodInfos);
2380
2381   switch(wNotifyCode)
2382   {
2383     case CBN_SELENDOK:
2384     {
2385       LPSFOLDER tmpFolder;
2386       int iItem;
2387
2388       iItem = CBGetCurSel(fodInfos->DlgInfos.hwndLookInCB);
2389
2390       if(!(tmpFolder = (LPSFOLDER) CBGetItemDataPtr(fodInfos->DlgInfos.hwndLookInCB,
2391                                                iItem)))
2392         return FALSE;
2393
2394
2395       if(SUCCEEDED(IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser,
2396                                               tmpFolder->pidlItem,
2397                                               SBSP_ABSOLUTE)))
2398       {
2399         return TRUE;
2400       }
2401       break;
2402     }
2403
2404   }
2405   return FALSE;
2406 }
2407
2408 /***********************************************************************
2409  *      FILEDLG95_LOOKIN_AddItem
2410  *
2411  * Adds an absolute pidl item to the lookin combo box
2412  * returns the index of the inserted item
2413  */
2414 static int FILEDLG95_LOOKIN_AddItem(HWND hwnd,LPITEMIDLIST pidl, int iInsertId)
2415 {
2416   LPITEMIDLIST pidlNext;
2417   SHFILEINFOA sfi;
2418   SFOLDER *tmpFolder;
2419   LookInInfos *liInfos;
2420
2421   TRACE("%08x\n", iInsertId);
2422
2423   if(!pidl)
2424     return -1;
2425
2426   if(!(liInfos = (LookInInfos *)GetPropA(hwnd,LookInInfosStr)))
2427     return -1;
2428
2429   tmpFolder = MemAlloc(sizeof(SFOLDER));
2430   tmpFolder->m_iIndent = 0;
2431
2432   /* Calculate the indentation of the item in the lookin*/
2433   pidlNext = pidl;
2434   while( (pidlNext=COMDLG32_PIDL_ILGetNext(pidlNext)) )
2435   {
2436     tmpFolder->m_iIndent++;
2437   }
2438
2439   tmpFolder->pidlItem = COMDLG32_PIDL_ILClone(pidl);
2440
2441   if(tmpFolder->m_iIndent > liInfos->iMaxIndentation)
2442     liInfos->iMaxIndentation = tmpFolder->m_iIndent;
2443
2444   sfi.dwAttributes = SFGAO_FILESYSANCESTOR | SFGAO_FILESYSTEM;
2445   SHGetFileInfoA((LPSTR)pidl,
2446                   0,
2447                   &sfi,
2448                   sizeof(sfi),
2449                   SHGFI_DISPLAYNAME | SHGFI_SYSICONINDEX
2450                   | SHGFI_PIDL | SHGFI_SMALLICON | SHGFI_ATTRIBUTES | SHGFI_ATTR_SPECIFIED);
2451
2452   TRACE("-- Add %s attr=%08lx\n", sfi.szDisplayName, sfi.dwAttributes);
2453
2454   if((sfi.dwAttributes & SFGAO_FILESYSANCESTOR) || (sfi.dwAttributes & SFGAO_FILESYSTEM))
2455   {
2456     int iItemID;
2457
2458     TRACE("-- Add %s at %u\n", sfi.szDisplayName, tmpFolder->m_iIndent);
2459
2460     /* Add the item at the end of the list */
2461     if(iInsertId < 0)
2462     {
2463       iItemID = CBAddString(hwnd,sfi.szDisplayName);
2464     }
2465     /* Insert the item at the iInsertId position*/
2466     else
2467     {
2468       iItemID = CBInsertString(hwnd,sfi.szDisplayName,iInsertId);
2469     }
2470
2471     CBSetItemDataPtr(hwnd,iItemID,tmpFolder);
2472     return iItemID;
2473   }
2474
2475   COMDLG32_SHFree( tmpFolder->pidlItem );
2476   MemFree( tmpFolder );
2477   return -1;
2478
2479 }
2480
2481 /***********************************************************************
2482  *      FILEDLG95_LOOKIN_InsertItemAfterParent
2483  *
2484  * Insert an item below its parent
2485  */
2486 static int FILEDLG95_LOOKIN_InsertItemAfterParent(HWND hwnd,LPITEMIDLIST pidl)
2487 {
2488
2489   LPITEMIDLIST pidlParent = GetParentPidl(pidl);
2490   int iParentPos;
2491
2492   TRACE("\n");
2493
2494   iParentPos = FILEDLG95_LOOKIN_SearchItem(hwnd,(WPARAM)pidlParent,SEARCH_PIDL);
2495
2496   if(iParentPos < 0)
2497   {
2498     iParentPos = FILEDLG95_LOOKIN_InsertItemAfterParent(hwnd,pidlParent);
2499   }
2500
2501   /* Free pidlParent memory */
2502   COMDLG32_SHFree((LPVOID)pidlParent);
2503
2504   return FILEDLG95_LOOKIN_AddItem(hwnd,pidl,iParentPos + 1);
2505 }
2506
2507 /***********************************************************************
2508  *      FILEDLG95_LOOKIN_SelectItem
2509  *
2510  * Adds an absolute pidl item to the lookin combo box
2511  * returns the index of the inserted item
2512  */
2513 int FILEDLG95_LOOKIN_SelectItem(HWND hwnd,LPITEMIDLIST pidl)
2514 {
2515   int iItemPos;
2516   LookInInfos *liInfos;
2517
2518   TRACE("\n");
2519
2520   iItemPos = FILEDLG95_LOOKIN_SearchItem(hwnd,(WPARAM)pidl,SEARCH_PIDL);
2521
2522   liInfos = (LookInInfos *)GetPropA(hwnd,LookInInfosStr);
2523
2524   if(iItemPos < 0)
2525   {
2526     while(FILEDLG95_LOOKIN_RemoveMostExpandedItem(hwnd) > -1);
2527     iItemPos = FILEDLG95_LOOKIN_InsertItemAfterParent(hwnd,pidl);
2528   }
2529
2530   else
2531   {
2532     SFOLDER *tmpFolder = (LPSFOLDER) CBGetItemDataPtr(hwnd,iItemPos);
2533     while(liInfos->iMaxIndentation > tmpFolder->m_iIndent)
2534     {
2535       int iRemovedItem;
2536
2537       if(-1 == (iRemovedItem = FILEDLG95_LOOKIN_RemoveMostExpandedItem(hwnd)))
2538         break;
2539       if(iRemovedItem < iItemPos)
2540         iItemPos--;
2541     }
2542   }
2543
2544   CBSetCurSel(hwnd,iItemPos);
2545   liInfos->uSelectedItem = iItemPos;
2546
2547   return 0;
2548
2549 }
2550
2551 /***********************************************************************
2552  *      FILEDLG95_LOOKIN_RemoveMostExpandedItem
2553  *
2554  * Remove the item with an expansion level over iExpansionLevel
2555  */
2556 static int FILEDLG95_LOOKIN_RemoveMostExpandedItem(HWND hwnd)
2557 {
2558   int iItemPos;
2559
2560   LookInInfos *liInfos = (LookInInfos *)GetPropA(hwnd,LookInInfosStr);
2561
2562   TRACE("\n");
2563
2564   if(liInfos->iMaxIndentation <= 2)
2565     return -1;
2566
2567   if((iItemPos = FILEDLG95_LOOKIN_SearchItem(hwnd,(WPARAM)liInfos->iMaxIndentation,SEARCH_EXP)) >=0)
2568   {
2569     SFOLDER *tmpFolder = (LPSFOLDER) CBGetItemDataPtr(hwnd,iItemPos);
2570     COMDLG32_SHFree(tmpFolder->pidlItem);
2571     MemFree(tmpFolder);
2572     CBDeleteString(hwnd,iItemPos);
2573     liInfos->iMaxIndentation--;
2574
2575     return iItemPos;
2576   }
2577
2578   return -1;
2579 }
2580
2581 /***********************************************************************
2582  *      FILEDLG95_LOOKIN_SearchItem
2583  *
2584  * Search for pidl in the lookin combo box
2585  * returns the index of the found item
2586  */
2587 static int FILEDLG95_LOOKIN_SearchItem(HWND hwnd,WPARAM searchArg,int iSearchMethod)
2588 {
2589   int i = 0;
2590   int iCount = CBGetCount(hwnd);
2591
2592   TRACE("0x%08x 0x%x\n",searchArg, iSearchMethod);
2593
2594   if (iCount != CB_ERR)
2595   {
2596     for(;i<iCount;i++)
2597     {
2598       LPSFOLDER tmpFolder = (LPSFOLDER) CBGetItemDataPtr(hwnd,i);
2599
2600       if(iSearchMethod == SEARCH_PIDL && COMDLG32_PIDL_ILIsEqual((LPITEMIDLIST)searchArg,tmpFolder->pidlItem))
2601         return i;
2602       if(iSearchMethod == SEARCH_EXP && tmpFolder->m_iIndent == (int)searchArg)
2603         return i;
2604     }
2605   }
2606
2607   return -1;
2608 }
2609
2610 /***********************************************************************
2611  *      FILEDLG95_LOOKIN_Clean
2612  *
2613  * Clean the memory used by the lookin combo box
2614  */
2615 static void FILEDLG95_LOOKIN_Clean(HWND hwnd)
2616 {
2617     FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
2618     int iPos;
2619     int iCount = CBGetCount(fodInfos->DlgInfos.hwndLookInCB);
2620
2621     TRACE("\n");
2622
2623     /* Delete each string of the combo and their associated data */
2624     if (iCount != CB_ERR)
2625     {
2626       for(iPos = iCount-1;iPos>=0;iPos--)
2627       {
2628         SFOLDER *tmpFolder = (LPSFOLDER) CBGetItemDataPtr(fodInfos->DlgInfos.hwndLookInCB,iPos);
2629         COMDLG32_SHFree(tmpFolder->pidlItem);
2630         MemFree(tmpFolder);
2631         CBDeleteString(fodInfos->DlgInfos.hwndLookInCB,iPos);
2632       }
2633     }
2634
2635     /* LookInInfos structure */
2636     RemovePropA(fodInfos->DlgInfos.hwndLookInCB,LookInInfosStr);
2637
2638 }
2639 /***********************************************************************
2640  * FILEDLG95_FILENAME_FillFromSelection
2641  *
2642  * fills the edit box from the cached DataObject
2643  */
2644 void FILEDLG95_FILENAME_FillFromSelection (HWND hwnd)
2645 {
2646     FileOpenDlgInfos *fodInfos;
2647     LPITEMIDLIST      pidl;
2648     UINT              nFiles = 0, nFileToOpen, nFileSelected, nLength = 0;
2649     char              lpstrTemp[MAX_PATH];
2650     LPSTR             lpstrAllFile = NULL, lpstrCurrFile = NULL;
2651
2652     TRACE("\n");
2653     fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
2654
2655     /* Count how many files we have */
2656     nFileSelected = GetNumSelected( fodInfos->Shell.FOIDataObject );
2657
2658     /* calculate the string length, count files */
2659     if (nFileSelected >= 1)
2660     {
2661       nLength += 3;     /* first and last quotes, trailing \0 */
2662       for ( nFileToOpen = 0; nFileToOpen < nFileSelected; nFileToOpen++ )
2663       {
2664         pidl = GetPidlFromDataObject( fodInfos->Shell.FOIDataObject, nFileToOpen+1 );
2665
2666         if (pidl)
2667         {
2668           /* get the total length of the selected file names*/
2669           lpstrTemp[0] = '\0';
2670           GetName( fodInfos->Shell.FOIShellFolder, pidl, SHGDN_INFOLDER|SHGDN_FORPARSING, lpstrTemp );
2671
2672           if ( ! IsPidlFolder(fodInfos->Shell.FOIShellFolder, pidl) ) /* Ignore folders */
2673           {
2674             nLength += strlen( lpstrTemp ) + 3;
2675             nFiles++;
2676           }
2677           COMDLG32_SHFree( pidl );
2678         }
2679       }
2680     }
2681
2682     /* allocate the buffer */
2683     if (nFiles <= 1) nLength = MAX_PATH;
2684     lpstrAllFile = (LPSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nLength);
2685     lpstrAllFile[0] = '\0';
2686
2687     /* Generate the string for the edit control */
2688     if(nFiles >= 1)
2689     {
2690       lpstrCurrFile = lpstrAllFile;
2691       for ( nFileToOpen = 0; nFileToOpen < nFileSelected; nFileToOpen++ )
2692       {
2693         pidl = GetPidlFromDataObject( fodInfos->Shell.FOIDataObject, nFileToOpen+1 );
2694
2695         if (pidl)
2696         {
2697           /* get the file name */
2698           lpstrTemp[0] = '\0';
2699           GetName( fodInfos->Shell.FOIShellFolder, pidl, SHGDN_INFOLDER|SHGDN_FORPARSING, lpstrTemp );
2700
2701           if (! IsPidlFolder(fodInfos->Shell.FOIShellFolder, pidl)) /* Ignore folders */
2702           {
2703             if ( nFiles > 1)
2704             {
2705               *lpstrCurrFile++ =  '\"';
2706               strcpy( lpstrCurrFile, lpstrTemp );
2707               lpstrCurrFile += strlen( lpstrTemp );
2708               strcpy( lpstrCurrFile, "\" " );
2709               lpstrCurrFile += 2;
2710             }
2711             else
2712             {
2713               strcpy( lpstrAllFile, lpstrTemp );
2714             }
2715           }
2716           COMDLG32_SHFree( (LPVOID) pidl );
2717         }
2718       }
2719       SetWindowTextA( fodInfos->DlgInfos.hwndFileName, lpstrAllFile );
2720     }
2721     HeapFree(GetProcessHeap(),0, lpstrAllFile );
2722 }
2723
2724
2725 /* copied from shell32 to avoid linking to it */
2726 static HRESULT COMDLG32_StrRetToStrNA (LPVOID dest, DWORD len, LPSTRRET src, LPITEMIDLIST pidl)
2727 {
2728         switch (src->uType)
2729         {
2730           case STRRET_WSTR:
2731             WideCharToMultiByte(CP_ACP, 0, src->u.pOleStr, -1, (LPSTR)dest, len, NULL, NULL);
2732             COMDLG32_SHFree(src->u.pOleStr);
2733             break;
2734
2735           case STRRET_CSTR:
2736             lstrcpynA((LPSTR)dest, src->u.cStr, len);
2737             break;
2738
2739           case STRRET_OFFSET:
2740             lstrcpynA((LPSTR)dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
2741             break;
2742
2743           default:
2744             FIXME("unknown type!\n");
2745             if (len)
2746             {
2747               *(LPSTR)dest = '\0';
2748             }
2749             return(FALSE);
2750         }
2751         return S_OK;
2752 }
2753
2754 /***********************************************************************
2755  * FILEDLG95_FILENAME_GetFileNames
2756  *
2757  * copies the filenames to a 0-delimited string list (A\0B\0C\0\0)
2758  */
2759 int FILEDLG95_FILENAME_GetFileNames (HWND hwnd, LPWSTR * lpstrFileList, UINT * sizeUsed)
2760 {
2761         FileOpenDlgInfos *fodInfos  = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
2762         UINT nStrCharCount = 0; /* index in src buffer */
2763         UINT nFileIndex = 0;    /* index in dest buffer */
2764         UINT nFileCount = 0;    /* number of files */
2765         UINT nStrLen = 0;       /* length of string in edit control */
2766         LPWSTR lpstrEdit;       /* buffer for string from edit control */
2767
2768         TRACE("\n");
2769
2770         /* get the filenames from the edit control */
2771         nStrLen = SendMessageW(fodInfos->DlgInfos.hwndFileName, WM_GETTEXTLENGTH, 0, 0);
2772         lpstrEdit = MemAlloc( (nStrLen+1)*sizeof(WCHAR) );
2773         GetDlgItemTextW(hwnd, IDC_FILENAME, lpstrEdit, nStrLen+1);
2774
2775         TRACE("nStrLen=%u str=%s\n", nStrLen, debugstr_w(lpstrEdit));
2776
2777         /* we might get single filename without any '"',
2778          * so we need nStrLen + terminating \0 + end-of-list \0 */
2779         *lpstrFileList = MemAlloc( (nStrLen+2)*sizeof(WCHAR) );
2780         *sizeUsed = 0;
2781
2782         /* build 0-delimited file list from filenames */
2783         while ( nStrCharCount <= nStrLen )
2784         {
2785           if ( lpstrEdit[nStrCharCount]=='"' )
2786           {
2787             nStrCharCount++;
2788             while ((lpstrEdit[nStrCharCount]!='"') && (nStrCharCount <= nStrLen))
2789             {
2790               (*lpstrFileList)[nFileIndex++] = lpstrEdit[nStrCharCount];
2791               (*sizeUsed)++;
2792               nStrCharCount++;
2793             }
2794             (*lpstrFileList)[nFileIndex++] = '\0';
2795             (*sizeUsed)++;
2796             nFileCount++;
2797           }
2798           nStrCharCount++;
2799         }
2800
2801         /* single, unquoted string */
2802         if ((nStrLen > 0) && (*sizeUsed == 0) )
2803         {
2804           strcpyW(*lpstrFileList, lpstrEdit);
2805           nFileIndex = strlenW(lpstrEdit) + 1;
2806           (*sizeUsed) = nFileIndex;
2807           nFileCount = 1;
2808         }
2809
2810         /* trailing \0 */
2811         (*lpstrFileList)[nFileIndex] = '\0';
2812         (*sizeUsed)++;
2813
2814         MemFree(lpstrEdit);
2815         return nFileCount;
2816 }
2817
2818 #define SETDefFormatEtc(fe,cf,med) \
2819 { \
2820     (fe).cfFormat = cf;\
2821     (fe).dwAspect = DVASPECT_CONTENT; \
2822     (fe).ptd =NULL;\
2823     (fe).tymed = med;\
2824     (fe).lindex = -1;\
2825 };
2826
2827 /*
2828  * DATAOBJECT Helper functions
2829  */
2830
2831 /***********************************************************************
2832  * COMCTL32_ReleaseStgMedium
2833  *
2834  * like ReleaseStgMedium from ole32
2835  */
2836 static void COMCTL32_ReleaseStgMedium (STGMEDIUM medium)
2837 {
2838       if(medium.pUnkForRelease)
2839       {
2840         IUnknown_Release(medium.pUnkForRelease);
2841       }
2842       else
2843       {
2844         GlobalUnlock(medium.u.hGlobal);
2845         GlobalFree(medium.u.hGlobal);
2846       }
2847 }
2848
2849 /***********************************************************************
2850  *          GetPidlFromDataObject
2851  *
2852  * Return pidl(s) by number from the cached DataObject
2853  *
2854  * nPidlIndex=0 gets the fully qualified root path
2855  */
2856 LPITEMIDLIST GetPidlFromDataObject ( IDataObject *doSelected, UINT nPidlIndex)
2857 {
2858
2859     STGMEDIUM medium;
2860     FORMATETC formatetc;
2861     LPITEMIDLIST pidl = NULL;
2862
2863     TRACE("sv=%p index=%u\n", doSelected, nPidlIndex);
2864
2865     /* Set the FORMATETC structure*/
2866     SETDefFormatEtc(formatetc, RegisterClipboardFormatA(CFSTR_SHELLIDLIST), TYMED_HGLOBAL);
2867
2868     /* Get the pidls from IDataObject */
2869     if(SUCCEEDED(IDataObject_GetData(doSelected,&formatetc,&medium)))
2870     {
2871       LPIDA cida = GlobalLock(medium.u.hGlobal);
2872       if(nPidlIndex <= cida->cidl)
2873       {
2874         pidl = COMDLG32_PIDL_ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[nPidlIndex]]));
2875       }
2876       COMCTL32_ReleaseStgMedium(medium);
2877     }
2878     return pidl;
2879 }
2880
2881 /***********************************************************************
2882  *          GetNumSelected
2883  *
2884  * Return the number of selected items in the DataObject.
2885  *
2886 */
2887 UINT GetNumSelected( IDataObject *doSelected )
2888 {
2889     UINT retVal = 0;
2890     STGMEDIUM medium;
2891     FORMATETC formatetc;
2892
2893     TRACE("sv=%p\n", doSelected);
2894
2895     if (!doSelected) return 0;
2896
2897     /* Set the FORMATETC structure*/
2898     SETDefFormatEtc(formatetc, RegisterClipboardFormatA(CFSTR_SHELLIDLIST), TYMED_HGLOBAL);
2899
2900     /* Get the pidls from IDataObject */
2901     if(SUCCEEDED(IDataObject_GetData(doSelected,&formatetc,&medium)))
2902     {
2903       LPIDA cida = GlobalLock(medium.u.hGlobal);
2904       retVal = cida->cidl;
2905       COMCTL32_ReleaseStgMedium(medium);
2906       return retVal;
2907     }
2908     return 0;
2909 }
2910
2911 /*
2912  * TOOLS
2913  */
2914
2915 /***********************************************************************
2916  *      GetName
2917  *
2918  * Get the pidl's display name (relative to folder) and
2919  * put it in lpstrFileName.
2920  *
2921  * Return NOERROR on success,
2922  * E_FAIL otherwise
2923  */
2924
2925 HRESULT GetName(LPSHELLFOLDER lpsf, LPITEMIDLIST pidl,DWORD dwFlags,LPSTR lpstrFileName)
2926 {
2927   STRRET str;
2928   HRESULT hRes;
2929
2930   TRACE("sf=%p pidl=%p\n", lpsf, pidl);
2931
2932   if(!lpsf)
2933   {
2934     HRESULT hRes;
2935     SHGetDesktopFolder(&lpsf);
2936     hRes = GetName(lpsf,pidl,dwFlags,lpstrFileName);
2937     IShellFolder_Release(lpsf);
2938     return hRes;
2939   }
2940
2941   /* Get the display name of the pidl relative to the folder */
2942   if (SUCCEEDED(hRes = IShellFolder_GetDisplayNameOf(lpsf, pidl, dwFlags, &str)))
2943   {
2944       return COMDLG32_StrRetToStrNA(lpstrFileName, MAX_PATH, &str, pidl);
2945   }
2946   return E_FAIL;
2947 }
2948
2949 /***********************************************************************
2950  *      GetShellFolderFromPidl
2951  *
2952  * pidlRel is the item pidl relative
2953  * Return the IShellFolder of the absolute pidl
2954  */
2955 IShellFolder *GetShellFolderFromPidl(LPITEMIDLIST pidlAbs)
2956 {
2957   IShellFolder *psf = NULL,*psfParent;
2958
2959   TRACE("%p\n", pidlAbs);
2960
2961   if(SUCCEEDED(SHGetDesktopFolder(&psfParent)))
2962   {
2963     psf = psfParent;
2964     if(pidlAbs && pidlAbs->mkid.cb)
2965     {
2966       if(SUCCEEDED(IShellFolder_BindToObject(psfParent, pidlAbs, NULL, &IID_IShellFolder, (LPVOID*)&psf)))
2967       {
2968         IShellFolder_Release(psfParent);
2969         return psf;
2970       }
2971     }
2972     /* return the desktop */
2973     return psfParent;
2974   }
2975   return NULL;
2976 }
2977
2978 /***********************************************************************
2979  *      GetParentPidl
2980  *
2981  * Return the LPITEMIDLIST to the parent of the pidl in the list
2982  */
2983 LPITEMIDLIST GetParentPidl(LPITEMIDLIST pidl)
2984 {
2985   LPITEMIDLIST pidlParent;
2986
2987   TRACE("%p\n", pidl);
2988
2989   pidlParent = COMDLG32_PIDL_ILClone(pidl);
2990   COMDLG32_PIDL_ILRemoveLastID(pidlParent);
2991
2992   return pidlParent;
2993 }
2994
2995 /***********************************************************************
2996  *      GetPidlFromName
2997  *
2998  * returns the pidl of the file name relative to folder
2999  * NULL if an error occurred
3000  */
3001 LPITEMIDLIST GetPidlFromName(IShellFolder *lpsf,LPWSTR lpcstrFileName)
3002 {
3003   LPITEMIDLIST pidl = NULL;
3004   ULONG ulEaten;
3005
3006   TRACE("sf=%p file=%s\n", lpsf, debugstr_w(lpcstrFileName));
3007
3008   if(!lpcstrFileName) return NULL;
3009   if(!*lpcstrFileName) return NULL;
3010
3011   if(!lpsf)
3012   {
3013     if (SUCCEEDED(SHGetDesktopFolder(&lpsf))) {
3014         IShellFolder_ParseDisplayName(lpsf, 0, NULL, lpcstrFileName, &ulEaten, &pidl, NULL);
3015         IShellFolder_Release(lpsf);
3016     }
3017   }
3018   else
3019   {
3020     IShellFolder_ParseDisplayName(lpsf, 0, NULL, lpcstrFileName, &ulEaten, &pidl, NULL);
3021   }
3022   return pidl;
3023 }
3024
3025 /*
3026 */
3027 BOOL IsPidlFolder (LPSHELLFOLDER psf, LPITEMIDLIST pidl)
3028 {
3029         ULONG uAttr  = SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
3030         HRESULT ret;
3031
3032         TRACE("%p, %p\n", psf, pidl);
3033
3034         ret = IShellFolder_GetAttributesOf( psf, 1, &pidl, &uAttr );
3035
3036         TRACE("-- 0x%08lx 0x%08lx\n", uAttr, ret);
3037         /* see documentation shell 4.1*/
3038         return uAttr & (SFGAO_FOLDER | SFGAO_HASSUBFOLDER);
3039 }
3040
3041 /***********************************************************************
3042  *      BrowseSelectedFolder
3043  */
3044 static BOOL BrowseSelectedFolder(HWND hwnd)
3045 {
3046   BOOL bBrowseSelFolder = FALSE;
3047   FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
3048
3049   TRACE("\n");
3050
3051   if (GetNumSelected(fodInfos->Shell.FOIDataObject) == 1)
3052   {
3053       LPITEMIDLIST pidlSelection;
3054
3055       /* get the file selected */
3056       pidlSelection  = GetPidlFromDataObject( fodInfos->Shell.FOIDataObject, 1);
3057       if (IsPidlFolder (fodInfos->Shell.FOIShellFolder, pidlSelection))
3058       {
3059           if ( FAILED( IShellBrowser_BrowseObject( fodInfos->Shell.FOIShellBrowser,
3060                          pidlSelection, SBSP_RELATIVE ) ) )
3061           {
3062                WCHAR notexist[] = {'P','a','t','h',' ','d','o','e','s',
3063                                    ' ','n','o','t',' ','e','x','i','s','t',0};
3064                MessageBoxW( hwnd, notexist, fodInfos->title, MB_OK | MB_ICONEXCLAMATION );
3065           }
3066
3067          bBrowseSelFolder = TRUE;
3068       }
3069       COMDLG32_SHFree( pidlSelection );
3070   }
3071
3072   return bBrowseSelFolder;
3073 }
3074
3075 /*
3076  * Memory allocation methods */
3077 static void *MemAlloc(UINT size)
3078 {
3079     return HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,size);
3080 }
3081
3082 static void MemFree(void *mem)
3083 {
3084     if(mem)
3085     {
3086         HeapFree(GetProcessHeap(),0,mem);
3087     }
3088 }