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