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