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