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