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