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