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