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