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