ntdll._iswctype should be ntdll.iswctype.
[wine] / dlls / commdlg / filedlgbrowser.c
1 /*
2  *  Implementation of IShellBrowser for the File Open common dialog
3  * 
4  *
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9
10 #include "windef.h"
11 #include "winbase.h"
12 #include "winnls.h"
13 #include "wingdi.h"
14 #include "winuser.h"
15 #include "heap.h"
16 #include "debugtools.h"
17
18 #include "shlwapi.h"
19 #include "filedlgbrowser.h"
20 #include "cdlg.h"
21 #include "shlguid.h"
22 #include "wine/obj_serviceprovider.h"
23
24 DEFAULT_DEBUG_CHANNEL(commdlg);
25
26 typedef struct
27 {
28
29     ICOM_VTABLE(IShellBrowser)   * lpVtbl;
30     ICOM_VTABLE(ICommDlgBrowser) * lpVtblCommDlgBrowser;
31     ICOM_VTABLE(IServiceProvider)* lpVtblServiceProvider;
32     DWORD ref;                                  /* Reference counter */
33     HWND hwndOwner;                             /* Owner dialog of the interface */
34
35 } IShellBrowserImpl;
36
37 /**************************************************************************
38 *   vtable
39 */
40 static ICOM_VTABLE(IShellBrowser) IShellBrowserImpl_Vtbl;
41 static ICOM_VTABLE(ICommDlgBrowser) IShellBrowserImpl_ICommDlgBrowser_Vtbl;
42 static ICOM_VTABLE(IServiceProvider) IShellBrowserImpl_IServiceProvider_Vtbl;
43
44 /**************************************************************************
45 *   Local Prototypes
46 */
47
48 HRESULT IShellBrowserImpl_ICommDlgBrowser_OnSelChange(ICommDlgBrowser *iface, IShellView *ppshv);
49 #if 0
50 LPITEMIDLIST GetSelectedPidl(IShellView *ppshv);
51 #endif
52
53 /**************************************************************************
54 *   External Prototypes
55 */
56 extern const char *FileOpenDlgInfosStr; 
57
58 extern HRESULT          GetName(LPSHELLFOLDER lpsf, LPITEMIDLIST pidl,DWORD dwFlags,LPSTR lpstrFileName);
59 extern HRESULT          GetFileName(HWND hwnd, LPITEMIDLIST pidl, LPSTR lpstrFileName);
60 extern IShellFolder*    GetShellFolderFromPidl(LPITEMIDLIST pidlAbs);
61 extern LPITEMIDLIST     GetParentPidl(LPITEMIDLIST pidl);
62 extern LPITEMIDLIST     GetPidlFromName(IShellFolder *psf,LPCSTR lpcstrFileName);
63
64 extern BOOL    FILEDLG95_SHELL_FillIncludedItemList(HWND hwnd,
65                                                         LPITEMIDLIST pidlCurrentFolder,
66                                                         LPSTR lpstrMask);
67
68 extern int     FILEDLG95_LOOKIN_SelectItem(HWND hwnd,LPITEMIDLIST pidl);
69 extern BOOL    FILEDLG95_OnOpen(HWND hwnd);
70 extern HRESULT SendCustomDlgNotificationMessage(HWND hwndParentDlg, UINT uCode);
71
72
73 /*
74  *   Helper functions
75  */
76
77 static void COMDLG32_UpdateCurrentDir(FileOpenDlgInfos *fodInfos)
78 {
79     char lpstrPath[MAX_PATH];
80     SHGetPathFromIDListA(fodInfos->ShellInfos.pidlAbsCurrent,lpstrPath);
81     SetCurrentDirectoryA(lpstrPath);
82     TRACE("new current folder %s\n", lpstrPath);
83 }
84
85 /* copied from shell32 to avoid linking to it */
86 static HRESULT COMDLG32_StrRetToStrNW (LPVOID dest, DWORD len, LPSTRRET src, LPITEMIDLIST pidl)
87 {
88         TRACE("dest=0x%p len=0x%lx strret=0x%p pidl=%p stub\n",dest,len,src,pidl);
89
90         switch (src->uType)
91         {
92           case STRRET_WSTR:
93             lstrcpynW((LPWSTR)dest, src->u.pOleStr, len);
94             COMDLG32_SHFree(src->u.pOleStr);
95             break;
96
97           case STRRET_CSTRA:
98             if (len && !MultiByteToWideChar( CP_ACP, 0, src->u.cStr, -1, (LPWSTR)dest, len ))
99                 ((LPWSTR)dest)[len-1] = 0;
100             break;
101
102           case STRRET_OFFSETA:
103             if (pidl)
104             {
105                 if (len && !MultiByteToWideChar( CP_ACP, 0, ((LPCSTR)&pidl->mkid)+src->u.uOffset,
106                                                  -1, (LPWSTR)dest, len ))
107                     ((LPWSTR)dest)[len-1] = 0;
108             }
109             break;
110
111           default:
112             FIXME("unknown type!\n");
113             if (len)
114             { *(LPWSTR)dest = '\0';
115             }
116             return(FALSE);
117         }
118         return S_OK;
119 }
120
121 /*
122  *      IShellBrowser
123  */
124  
125 /**************************************************************************
126 *  IShellBrowserImpl_Construct
127 */
128 IShellBrowser * IShellBrowserImpl_Construct(HWND hwndOwner)
129 {
130     IShellBrowserImpl *sb;
131     FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwndOwner,FileOpenDlgInfosStr);
132
133     sb=(IShellBrowserImpl*)COMDLG32_SHAlloc(sizeof(IShellBrowserImpl));
134
135     /* Initialisation of the member variables */
136     sb->ref=1;
137     sb->hwndOwner = hwndOwner;
138
139     /* Initialisation of the vTables */
140     sb->lpVtbl = &IShellBrowserImpl_Vtbl;
141     sb->lpVtblCommDlgBrowser = &IShellBrowserImpl_ICommDlgBrowser_Vtbl;
142     sb->lpVtblServiceProvider = &IShellBrowserImpl_IServiceProvider_Vtbl;
143     SHGetSpecialFolderLocation(hwndOwner, CSIDL_DESKTOP,
144                                &fodInfos->ShellInfos.pidlAbsCurrent);
145
146     TRACE("%p\n", sb);
147
148     return (IShellBrowser *) sb;
149 }
150
151 /***************************************************************************
152 *  IShellBrowserImpl_QueryInterface
153 */
154 HRESULT WINAPI IShellBrowserImpl_QueryInterface(IShellBrowser *iface,
155                                             REFIID riid, 
156                                             LPVOID *ppvObj)
157 {
158     ICOM_THIS(IShellBrowserImpl, iface);
159
160     TRACE("(%p)\n\t%s\n", This, debugstr_guid(riid));
161
162     *ppvObj = NULL;
163
164     if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
165     { *ppvObj = This; 
166     }
167     else if(IsEqualIID(riid, &IID_IOleWindow))  /*IOleWindow*/
168     { *ppvObj = (IOleWindow*)This;
169     }
170
171     else if(IsEqualIID(riid, &IID_IShellBrowser))  /*IShellBrowser*/
172     { *ppvObj = (IShellBrowser*)This;
173     }
174
175     else if(IsEqualIID(riid, &IID_ICommDlgBrowser))  /*ICommDlgBrowser*/
176     { *ppvObj = (ICommDlgBrowser*) &(This->lpVtblCommDlgBrowser);
177     }
178
179     else if(IsEqualIID(riid, &IID_IServiceProvider))  /* IServiceProvider */
180     { *ppvObj = (ICommDlgBrowser*) &(This->lpVtblServiceProvider);
181     }
182
183     if(*ppvObj)
184     { IUnknown_AddRef( (IShellBrowser*) *ppvObj);
185       return S_OK;
186     }
187     FIXME("Unknown interface requested\n");
188     return E_NOINTERFACE;
189 }
190
191 /**************************************************************************
192 *  IShellBrowser::AddRef
193 */
194 ULONG WINAPI IShellBrowserImpl_AddRef(IShellBrowser * iface)
195 {
196     ICOM_THIS(IShellBrowserImpl, iface);
197
198     TRACE("(%p)\n", This);
199
200     return ++(This->ref);
201 }
202
203 /**************************************************************************
204 *  IShellBrowserImpl_Release
205 */
206 ULONG WINAPI IShellBrowserImpl_Release(IShellBrowser * iface)
207 {
208     ICOM_THIS(IShellBrowserImpl, iface);
209
210     TRACE("(%p)\n", This);
211
212     if (!--(This->ref)) 
213     { 
214       HeapFree(GetProcessHeap(),0, This);
215       return 0;
216     }
217     return This->ref;
218 }
219
220 /*
221  * IOleWindow
222  */
223
224 /**************************************************************************
225 *  IShellBrowserImpl_GetWindow  (IOleWindow)
226 *
227 *  Inherited from IOleWindow::GetWindow
228 *
229 *  See Windows documentation for more details
230 *
231 *  Note : We will never be window less in the File Open dialog
232 *  
233 */
234 HRESULT WINAPI IShellBrowserImpl_GetWindow(IShellBrowser * iface,  
235                                            HWND * phwnd)
236 {
237     ICOM_THIS(IShellBrowserImpl, iface);
238
239     TRACE("(%p)\n", This);
240
241     if(!This->hwndOwner)
242         return E_FAIL;
243
244     *phwnd = This->hwndOwner;
245
246     return (*phwnd) ? S_OK : E_UNEXPECTED; 
247
248 }
249
250 /**************************************************************************
251 *  IShellBrowserImpl_ContextSensitiveHelp
252 */
253 HRESULT WINAPI IShellBrowserImpl_ContextSensitiveHelp(IShellBrowser * iface,
254                                                       BOOL fEnterMode)
255 {
256     ICOM_THIS(IShellBrowserImpl, iface);
257
258     TRACE("(%p)\n", This);
259
260     /* Feature not implemented */
261     return E_NOTIMPL;
262 }
263
264 /*
265  * IShellBrowser
266  */
267
268 /**************************************************************************
269 *  IShellBrowserImpl_BrowseObject
270 *
271 *  See Windows documentation on IShellBrowser::BrowseObject for more details
272 *
273 *  This function will override user specified flags and will always 
274 *  use SBSP_DEFBROWSER and SBSP_DEFMODE.  
275 */
276 HRESULT WINAPI IShellBrowserImpl_BrowseObject(IShellBrowser *iface, 
277                                               LPCITEMIDLIST pidl, 
278                                               UINT wFlags)
279 {
280     HRESULT hRes;
281     IShellFolder *psfTmp;
282     IShellView *psvTmp;
283     FileOpenDlgInfos *fodInfos;
284     LPITEMIDLIST pidlTmp;
285     HWND hwndView;
286     HWND hDlgWnd;
287     BOOL bViewHasFocus;
288
289     ICOM_THIS(IShellBrowserImpl, iface);
290
291     TRACE("(%p)(%p,0x%08x)\n", This, pidl, wFlags);
292
293     fodInfos = (FileOpenDlgInfos *) GetPropA(This->hwndOwner,FileOpenDlgInfosStr);
294
295     /* Format the pidl according to its parameter's category */
296     if(wFlags & SBSP_RELATIVE)
297     {
298         
299         /* SBSP_RELATIVE  A relative pidl (relative from the current folder) */
300         if(FAILED(hRes = IShellFolder_BindToObject(fodInfos->Shell.FOIShellFolder,
301              pidl, NULL, &IID_IShellFolder, (LPVOID *)&psfTmp)))
302         {
303             ERR("bind to object failed\n");
304             return hRes;
305         }
306         /* create an absolute pidl */
307         pidlTmp = COMDLG32_PIDL_ILCombine(fodInfos->ShellInfos.pidlAbsCurrent,
308                                                         (LPITEMIDLIST)pidl);
309     }
310     else if(wFlags & SBSP_PARENT)
311     {
312         /* Browse the parent folder (ignores the pidl) */
313         pidlTmp = GetParentPidl(fodInfos->ShellInfos.pidlAbsCurrent);
314         psfTmp = GetShellFolderFromPidl(pidlTmp);
315
316     }
317     else /* SBSP_ABSOLUTE is 0x0000 */
318     {
319         /* An absolute pidl (relative from the desktop) */
320         pidlTmp =  COMDLG32_PIDL_ILClone((LPITEMIDLIST)pidl);
321         psfTmp = GetShellFolderFromPidl(pidlTmp);
322     }
323     
324     if(!psfTmp)
325     {
326       ERR("could not browse to folder\n");
327       return E_FAIL;
328     }
329
330     /* If the pidl to browse to is equal to the actual pidl ... 
331        do nothing and pretend you did it*/
332     if(COMDLG32_PIDL_ILIsEqual(pidlTmp,fodInfos->ShellInfos.pidlAbsCurrent))
333     {
334         IShellFolder_Release(psfTmp);
335         COMDLG32_SHFree(pidlTmp);
336         TRACE("keep current folder\n");
337         return NOERROR;
338     }
339
340     /* Release the current DataObject */
341     if (fodInfos->Shell.FOIDataObject)
342     {
343       IDataObject_Release(fodInfos->Shell.FOIDataObject);
344       fodInfos->Shell.FOIDataObject = NULL;
345     }
346
347     /* Create the associated view */
348     TRACE("create view object\n");
349     if(FAILED(hRes = IShellFolder_CreateViewObject(psfTmp, fodInfos->ShellInfos.hwndOwner,
350            &IID_IShellView, (LPVOID *)&psvTmp))) goto error;
351
352     /* Check if listview has focus */
353     bViewHasFocus = IsChild(fodInfos->ShellInfos.hwndView,GetFocus());
354
355     /* Get the foldersettings from the old view */
356     if(fodInfos->Shell.FOIShellView)
357       IShellView_GetCurrentInfo(fodInfos->Shell.FOIShellView, &fodInfos->ShellInfos.folderSettings);
358
359     /* Release the old fodInfos->Shell.FOIShellView and update its value.
360     We have to update this early since ShellView_CreateViewWindow of native
361     shell32 calls OnStateChange and needs the correct view here.*/
362     if(fodInfos->Shell.FOIShellView)
363     {
364       IShellView_DestroyViewWindow(fodInfos->Shell.FOIShellView);
365       IShellView_Release(fodInfos->Shell.FOIShellView);
366     }
367     fodInfos->Shell.FOIShellView = psvTmp;
368
369     /* Release old FOIShellFolder and update its value */
370     if (fodInfos->Shell.FOIShellFolder)
371       IShellFolder_Release(fodInfos->Shell.FOIShellFolder);
372     fodInfos->Shell.FOIShellFolder = psfTmp;
373
374     /* Release old pidlAbsCurrent and update its value */
375     COMDLG32_SHFree((LPVOID)fodInfos->ShellInfos.pidlAbsCurrent);
376     fodInfos->ShellInfos.pidlAbsCurrent = pidlTmp;
377
378     COMDLG32_UpdateCurrentDir(fodInfos);
379
380     /* Create the window */
381     TRACE("create view window\n");
382     if(FAILED(hRes = IShellView_CreateViewWindow(psvTmp, NULL,
383          &fodInfos->ShellInfos.folderSettings, fodInfos->Shell.FOIShellBrowser,
384          &fodInfos->ShellInfos.rectView, &hwndView))) goto error;
385
386     fodInfos->ShellInfos.hwndView = hwndView;
387
388     /* Select the new folder in the Look In combo box of the Open file dialog */
389     FILEDLG95_LOOKIN_SelectItem(fodInfos->DlgInfos.hwndLookInCB,fodInfos->ShellInfos.pidlAbsCurrent);
390
391     /* changes the tab order of the ListView to reflect the window's File Dialog */
392     hDlgWnd = GetDlgItem(GetParent(hwndView), IDC_LOOKIN); 
393     SetWindowPos(hwndView, hDlgWnd, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE);
394
395     /* Since we destroyed the old view if it had focus set focus to the newly created view */
396     if (bViewHasFocus)
397       SetFocus(fodInfos->ShellInfos.hwndView);
398
399     return hRes; 
400 error:
401     ERR("Failed with error 0x%08lx\n", hRes);
402     return hRes;
403 }
404
405 /**************************************************************************
406 *  IShellBrowserImpl_EnableModelessSB
407 */
408 HRESULT WINAPI IShellBrowserImpl_EnableModelessSB(IShellBrowser *iface,    
409                                               BOOL fEnable)
410                                               
411 {
412     ICOM_THIS(IShellBrowserImpl, iface);
413
414     TRACE("(%p)\n", This);
415
416     /* Feature not implemented */
417     return E_NOTIMPL;
418 }
419
420 /**************************************************************************
421 *  IShellBrowserImpl_GetControlWindow
422 */
423 HRESULT WINAPI IShellBrowserImpl_GetControlWindow(IShellBrowser *iface,    
424                                               UINT id,    
425                                               HWND *lphwnd)
426                                               
427 {
428     ICOM_THIS(IShellBrowserImpl, iface);
429
430     TRACE("(%p)\n", This);
431
432     /* Feature not implemented */
433     return E_NOTIMPL;
434 }
435 /**************************************************************************
436 *  IShellBrowserImpl_GetViewStateStream
437 */
438 HRESULT WINAPI IShellBrowserImpl_GetViewStateStream(IShellBrowser *iface,
439                                                 DWORD grfMode,    
440                                                 LPSTREAM *ppStrm)
441                                               
442 {
443     ICOM_THIS(IShellBrowserImpl, iface);
444
445     FIXME("(%p 0x%08lx %p)\n", This, grfMode, ppStrm);
446
447     /* Feature not implemented */
448     return E_NOTIMPL;
449 }       
450 /**************************************************************************
451 *  IShellBrowserImpl_InsertMenusSB
452 */
453 HRESULT WINAPI IShellBrowserImpl_InsertMenusSB(IShellBrowser *iface,
454                                            HMENU hmenuShared,
455                                            LPOLEMENUGROUPWIDTHS lpMenuWidths)
456                                               
457 {
458     ICOM_THIS(IShellBrowserImpl, iface);
459
460     TRACE("(%p)\n", This);
461
462     /* Feature not implemented */
463     return E_NOTIMPL;
464 }
465 /**************************************************************************
466 *  IShellBrowserImpl_OnViewWindowActive
467 */
468 HRESULT WINAPI IShellBrowserImpl_OnViewWindowActive(IShellBrowser *iface,
469                                                 IShellView *ppshv)
470                                               
471 {
472     ICOM_THIS(IShellBrowserImpl, iface);
473
474     TRACE("(%p)\n", This);
475
476     /* Feature not implemented */
477     return E_NOTIMPL;
478 }   
479 /**************************************************************************
480 *  IShellBrowserImpl_QueryActiveShellView
481 */
482 HRESULT WINAPI IShellBrowserImpl_QueryActiveShellView(IShellBrowser *iface,
483                                                   IShellView **ppshv)
484                                               
485 {
486     ICOM_THIS(IShellBrowserImpl, iface);
487
488     FileOpenDlgInfos *fodInfos;
489
490     TRACE("(%p)\n", This);
491
492     fodInfos = (FileOpenDlgInfos *) GetPropA(This->hwndOwner,FileOpenDlgInfosStr);
493
494     if(!(*ppshv = fodInfos->Shell.FOIShellView))
495     {
496         return E_FAIL;
497     }
498     IShellView_AddRef(fodInfos->Shell.FOIShellView);
499     return NOERROR;
500 }   
501 /**************************************************************************
502 *  IShellBrowserImpl_RemoveMenusSB
503 */
504 HRESULT WINAPI IShellBrowserImpl_RemoveMenusSB(IShellBrowser *iface,
505                                            HMENU hmenuShared)
506                                               
507 {
508     ICOM_THIS(IShellBrowserImpl, iface);
509
510     TRACE("(%p)\n", This);
511
512     /* Feature not implemented */
513     return E_NOTIMPL;
514 }   
515 /**************************************************************************
516 *  IShellBrowserImpl_SendControlMsg
517 */
518 HRESULT WINAPI IShellBrowserImpl_SendControlMsg(IShellBrowser *iface,    
519                                             UINT id,    
520                                             UINT uMsg,    
521                                             WPARAM wParam,    
522                                             LPARAM lParam,
523                                             LRESULT *pret)
524                                               
525 {
526     ICOM_THIS(IShellBrowserImpl, iface);
527     LRESULT lres;
528     
529     TRACE("(%p)->(0x%08x 0x%08x 0x%08x 0x%08lx %p)\n", This, id, uMsg, wParam, lParam, pret);
530
531     switch (id)
532     {
533       case FCW_TOOLBAR:
534         lres = SendDlgItemMessageA( This->hwndOwner, IDC_TOOLBAR, uMsg, wParam, lParam);
535         break;
536       default:
537         FIXME("ctrl id: %x\n", id);
538         return E_NOTIMPL;
539     }
540     if (pret) *pret = lres;
541     return S_OK;
542 }
543 /**************************************************************************
544 *  IShellBrowserImpl_SetMenuSB
545 */
546 HRESULT WINAPI IShellBrowserImpl_SetMenuSB(IShellBrowser *iface,
547                                        HMENU hmenuShared,    
548                                        HOLEMENU holemenuReserved,
549                                        HWND hwndActiveObject)
550                                               
551 {
552     ICOM_THIS(IShellBrowserImpl, iface);
553
554     TRACE("(%p)\n", This);
555
556     /* Feature not implemented */
557     return E_NOTIMPL;
558 }   
559 /**************************************************************************
560 *  IShellBrowserImpl_SetStatusTextSB
561 */
562 HRESULT WINAPI IShellBrowserImpl_SetStatusTextSB(IShellBrowser *iface,
563                                              LPCOLESTR lpszStatusText)
564                                               
565 {
566     ICOM_THIS(IShellBrowserImpl, iface);
567
568     TRACE("(%p)\n", This);
569
570     /* Feature not implemented */
571     return E_NOTIMPL;
572 }   
573 /**************************************************************************
574 *  IShellBrowserImpl_SetToolbarItems
575 */
576 HRESULT WINAPI IShellBrowserImpl_SetToolbarItems(IShellBrowser *iface,
577                                              LPTBBUTTON lpButtons,    
578                                              UINT nButtons,    
579                                              UINT uFlags)
580                                               
581 {
582     ICOM_THIS(IShellBrowserImpl, iface);
583
584     TRACE("(%p)\n", This);
585
586     /* Feature not implemented */
587     return E_NOTIMPL;
588 }   
589 /**************************************************************************
590 *  IShellBrowserImpl_TranslateAcceleratorSB
591 */
592 HRESULT WINAPI IShellBrowserImpl_TranslateAcceleratorSB(IShellBrowser *iface,
593                                                     LPMSG lpmsg,    
594                                                     WORD wID)
595                                               
596 {
597     ICOM_THIS(IShellBrowserImpl, iface);
598
599     TRACE("(%p)\n", This);
600
601     /* Feature not implemented */
602     return E_NOTIMPL;
603 }
604
605 static ICOM_VTABLE(IShellBrowser) IShellBrowserImpl_Vtbl =
606 {
607         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
608         /* IUnknown */
609         IShellBrowserImpl_QueryInterface,
610         IShellBrowserImpl_AddRef,
611         IShellBrowserImpl_Release,
612         /* IOleWindow */
613         IShellBrowserImpl_GetWindow,
614         IShellBrowserImpl_ContextSensitiveHelp,
615         /*  IShellBrowser */
616         IShellBrowserImpl_InsertMenusSB,
617         IShellBrowserImpl_SetMenuSB,
618         IShellBrowserImpl_RemoveMenusSB,
619         IShellBrowserImpl_SetStatusTextSB,
620         IShellBrowserImpl_EnableModelessSB,
621         IShellBrowserImpl_TranslateAcceleratorSB,
622         IShellBrowserImpl_BrowseObject,
623         IShellBrowserImpl_GetViewStateStream,
624         IShellBrowserImpl_GetControlWindow,
625         IShellBrowserImpl_SendControlMsg,
626         IShellBrowserImpl_QueryActiveShellView,
627         IShellBrowserImpl_OnViewWindowActive,
628         IShellBrowserImpl_SetToolbarItems
629 };
630
631
632
633 /*
634  * ICommDlgBrowser
635  */
636
637 /***************************************************************************
638 *  IShellBrowserImpl_ICommDlgBrowser_QueryInterface
639 */
640 HRESULT WINAPI IShellBrowserImpl_ICommDlgBrowser_QueryInterface(
641         ICommDlgBrowser *iface,
642         REFIID riid, 
643         LPVOID *ppvObj)
644 {
645     _ICOM_THIS_FromICommDlgBrowser(IShellBrowser,iface);
646
647     TRACE("(%p)\n", This);
648
649     return IShellBrowserImpl_QueryInterface(This,riid,ppvObj);
650 }
651
652 /**************************************************************************
653 *  IShellBrowserImpl_ICommDlgBrowser_AddRef
654 */
655 ULONG WINAPI IShellBrowserImpl_ICommDlgBrowser_AddRef(ICommDlgBrowser * iface)
656 {
657     _ICOM_THIS_FromICommDlgBrowser(IShellBrowser,iface);
658
659     TRACE("(%p)\n", This);
660
661     return IShellBrowserImpl_AddRef(This);
662 }
663
664 /**************************************************************************
665 *  IShellBrowserImpl_ICommDlgBrowser_Release
666 */
667 ULONG WINAPI IShellBrowserImpl_ICommDlgBrowser_Release(ICommDlgBrowser * iface)
668 {
669     _ICOM_THIS_FromICommDlgBrowser(IShellBrowser,iface);
670
671     TRACE("(%p)\n", This);
672
673     return IShellBrowserImpl_Release(This);
674 }
675 /**************************************************************************
676 *  IShellBrowserImpl_ICommDlgBrowser_OnDefaultCommand
677 *
678 *   Called when a user double-clicks in the view or presses the ENTER key
679 */
680 HRESULT WINAPI IShellBrowserImpl_ICommDlgBrowser_OnDefaultCommand(ICommDlgBrowser *iface,
681                                                                   IShellView *ppshv)
682 {
683     LPITEMIDLIST pidl;
684     FileOpenDlgInfos *fodInfos;
685
686     _ICOM_THIS_FromICommDlgBrowser(IShellBrowserImpl,iface);
687
688     TRACE("(%p)\n", This);
689
690     fodInfos = (FileOpenDlgInfos *) GetPropA(This->hwndOwner,FileOpenDlgInfosStr);  
691     
692     /* If the selected object is not a folder, send a IDOK command to parent window */
693     if((pidl = GetPidlFromDataObject(fodInfos->Shell.FOIDataObject, 1)))
694     {
695         HRESULT hRes;
696
697         ULONG  ulAttr = SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
698         IShellFolder_GetAttributesOf(fodInfos->Shell.FOIShellFolder, 1, &pidl, &ulAttr);
699         if (ulAttr & (SFGAO_FOLDER | SFGAO_HASSUBFOLDER) )
700         {
701           hRes = IShellBrowser_BrowseObject((IShellBrowser *)This,pidl,SBSP_RELATIVE);
702         }
703         else
704         {
705           /* Tell the dialog that the user selected a file */
706           hRes = PostMessageA(This->hwndOwner, WM_COMMAND, IDOK, 0L);
707         }
708
709         /* Free memory used by pidl */
710         COMDLG32_SHFree((LPVOID)pidl);
711
712         return hRes;
713     }
714
715     return E_FAIL;
716 }
717
718 /**************************************************************************
719 *  IShellBrowserImpl_ICommDlgBrowser_OnStateChange
720 */
721 HRESULT WINAPI IShellBrowserImpl_ICommDlgBrowser_OnStateChange(ICommDlgBrowser *iface,
722                                                                IShellView *ppshv,
723                                                                ULONG uChange)
724 {
725
726     _ICOM_THIS_FromICommDlgBrowser(IShellBrowserImpl,iface);
727
728     TRACE("(%p shv=%p)\n", This, ppshv);
729
730     switch (uChange)
731     {
732         case CDBOSC_SETFOCUS:
733              /* FIXME: Reset the default button.
734                 This should be taken care of by defdlg. If control
735                 other than button receives focus the default button
736                 should be restored. */
737              SendMessageA(This->hwndOwner, DM_SETDEFID, IDOK, 0);
738
739             break;
740         case CDBOSC_KILLFOCUS: 
741             {
742                 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(This->hwndOwner,FileOpenDlgInfosStr);
743                 if(fodInfos->DlgInfos.dwDlgProp & FODPROP_SAVEDLG)
744                     SetDlgItemTextA(fodInfos->ShellInfos.hwndOwner,IDOK,"&Save");
745             }
746             break;
747         case CDBOSC_SELCHANGE:
748             return IShellBrowserImpl_ICommDlgBrowser_OnSelChange(iface,ppshv);
749         case CDBOSC_RENAME:
750             /* nothing to do */
751             break;
752     }
753
754     return NOERROR;     
755 }
756
757 /**************************************************************************
758 *  IShellBrowserImpl_ICommDlgBrowser_IncludeObject
759 */
760 HRESULT WINAPI IShellBrowserImpl_ICommDlgBrowser_IncludeObject(ICommDlgBrowser *iface, 
761                                                                IShellView * ppshv,
762                                                                LPCITEMIDLIST pidl)
763 {
764     FileOpenDlgInfos *fodInfos;
765     ULONG ulAttr;
766     STRRET str;
767     WCHAR szPathW[MAX_PATH];
768
769     _ICOM_THIS_FromICommDlgBrowser(IShellBrowserImpl,iface);
770
771     TRACE("(%p)\n", This);
772
773     fodInfos = (FileOpenDlgInfos *) GetPropA(This->hwndOwner,FileOpenDlgInfosStr);
774
775     ulAttr = SFGAO_HIDDEN | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_FILESYSANCESTOR | SFGAO_LINK;
776     IShellFolder_GetAttributesOf(fodInfos->Shell.FOIShellFolder, 1, &pidl, &ulAttr);
777     
778     if( (ulAttr & SFGAO_HIDDEN)                                         /* hidden */
779       | !(ulAttr & (SFGAO_FILESYSTEM | SFGAO_FILESYSANCESTOR))) /* special folder */
780         return S_FALSE;
781
782     /* always include directorys and links */
783     if(ulAttr & (SFGAO_FOLDER | SFGAO_LINK)) 
784         return S_OK;
785
786     /* Check if there is a mask to apply if not */
787     if(!fodInfos->ShellInfos.lpstrCurrentFilter || !lstrlenW(fodInfos->ShellInfos.lpstrCurrentFilter))
788         return S_OK;
789
790     if (SUCCEEDED(IShellFolder_GetDisplayNameOf(fodInfos->Shell.FOIShellFolder, pidl, SHGDN_INFOLDER | SHGDN_FORPARSING, &str)))
791     {
792       if (SUCCEEDED(COMDLG32_StrRetToStrNW(szPathW, MAX_PATH, &str, pidl)))
793       {
794           if (PathMatchSpecW(szPathW, fodInfos->ShellInfos.lpstrCurrentFilter))
795           return S_OK;
796       }
797     }
798     return S_FALSE;
799
800 }
801
802 /**************************************************************************
803 *  IShellBrowserImpl_ICommDlgBrowser_OnSelChange
804 */  
805 HRESULT IShellBrowserImpl_ICommDlgBrowser_OnSelChange(ICommDlgBrowser *iface, IShellView *ppshv)
806 {
807     FileOpenDlgInfos *fodInfos;
808
809     _ICOM_THIS_FromICommDlgBrowser(IShellBrowserImpl,iface);
810
811     fodInfos = (FileOpenDlgInfos *) GetPropA(This->hwndOwner,FileOpenDlgInfosStr);
812     TRACE("(%p do=%p view=%p)\n", This, fodInfos->Shell.FOIDataObject, fodInfos->Shell.FOIShellView);
813     
814     /* release old selections */
815     if (fodInfos->Shell.FOIDataObject)
816       IDataObject_Release(fodInfos->Shell.FOIDataObject);
817     
818     /* get a new DataObject from the ShellView */
819     if(FAILED(IShellView_GetItemObject(fodInfos->Shell.FOIShellView, SVGIO_SELECTION,
820                               &IID_IDataObject, (LPVOID*)&fodInfos->Shell.FOIDataObject)))
821       return E_FAIL;
822                                           
823     FILEDLG95_FILENAME_FillFromSelection(This->hwndOwner);
824
825     SendCustomDlgNotificationMessage(This->hwndOwner, CDN_SELCHANGE);
826     return S_OK;
827 }
828
829 static ICOM_VTABLE(ICommDlgBrowser) IShellBrowserImpl_ICommDlgBrowser_Vtbl =
830 {
831         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
832         /* IUnknown */
833         IShellBrowserImpl_ICommDlgBrowser_QueryInterface,
834         IShellBrowserImpl_ICommDlgBrowser_AddRef,
835         IShellBrowserImpl_ICommDlgBrowser_Release,
836         /* ICommDlgBrowser */
837         IShellBrowserImpl_ICommDlgBrowser_OnDefaultCommand,
838         IShellBrowserImpl_ICommDlgBrowser_OnStateChange,
839         IShellBrowserImpl_ICommDlgBrowser_IncludeObject
840 };
841
842
843
844
845 /*
846  * IServiceProvider
847  */
848
849 /***************************************************************************
850 *  IShellBrowserImpl_IServiceProvider_QueryInterface
851 */
852 HRESULT WINAPI IShellBrowserImpl_IServiceProvider_QueryInterface(
853         IServiceProvider *iface,
854         REFIID riid, 
855         LPVOID *ppvObj)
856 {
857     _ICOM_THIS_FromIServiceProvider(IShellBrowser,iface);
858
859     FIXME("(%p)\n", This);
860
861     return IShellBrowserImpl_QueryInterface(This,riid,ppvObj);
862 }
863
864 /**************************************************************************
865 *  IShellBrowserImpl_IServiceProvider_AddRef
866 */
867 ULONG WINAPI IShellBrowserImpl_IServiceProvider_AddRef(IServiceProvider * iface)
868 {
869     _ICOM_THIS_FromIServiceProvider(IShellBrowser,iface);
870
871     FIXME("(%p)\n", This);
872
873     return IShellBrowserImpl_AddRef(This);
874 }
875
876 /**************************************************************************
877 *  IShellBrowserImpl_IServiceProvider_Release
878 */
879 ULONG WINAPI IShellBrowserImpl_IServiceProvider_Release(IServiceProvider * iface)
880 {
881     _ICOM_THIS_FromIServiceProvider(IShellBrowser,iface);
882
883     FIXME("(%p)\n", This);
884
885     return IShellBrowserImpl_Release(This);
886 }
887
888 /**************************************************************************
889 *  IShellBrowserImpl_IServiceProvider_Release
890 *
891 * NOTES
892 *  the w2k shellview asks for 
893 *   guidService = SID_STopLevelBrowser
894 *   riid = IShellBrowser
895 *
896 * FIXME
897 *  this is a hack!
898 */
899
900 HRESULT WINAPI IShellBrowserImpl_IServiceProvider_QueryService(
901         IServiceProvider * iface,
902         REFGUID guidService,
903         REFIID riid,
904         void** ppv)
905 {
906     _ICOM_THIS_FromIServiceProvider(IShellBrowser,iface);
907
908     FIXME("(%p)\n\t%s\n\t%s\n", This,debugstr_guid(guidService), debugstr_guid(riid) );
909
910     *ppv = NULL;
911     if(guidService && IsEqualIID(guidService, &SID_STopLevelBrowser))
912     {
913       return IShellBrowserImpl_QueryInterface(This,riid,ppv);
914     }
915     FIXME("(%p) unknown interface requested\n", This);
916     return E_NOINTERFACE;
917
918 }
919
920 static ICOM_VTABLE(IServiceProvider) IShellBrowserImpl_IServiceProvider_Vtbl =
921 {
922         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
923         /* IUnknown */
924         IShellBrowserImpl_IServiceProvider_QueryInterface,
925         IShellBrowserImpl_IServiceProvider_AddRef,
926         IShellBrowserImpl_IServiceProvider_Release,
927         /* IServiceProvider */
928         IShellBrowserImpl_IServiceProvider_QueryService
929 };