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