4 * Copyright 2006 - 2007 Thomas Weidenmueller <w3seek@reactos.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <shdeprecated.h>
25 /*****************************************************************************
26 ** ITrayBandSite ************************************************************
27 *****************************************************************************/
29 // WARNING: Can't use ATL for this class due to our ATL not fully supporting the AGGREGATION functions needed for this class to be an "outer" class
30 // it works just fine this way.
34 public IBandSiteStreamCallback
35 /* TODO: IWinEventHandler */
37 volatile LONG m_RefCount
;
39 CComPtr
<ITrayWindow
> Tray
;
41 CComPtr
<IUnknown
> punkInner
;
42 CComPtr
<IBandSite
> BandSite
;
43 CComPtr
<ITaskBand
> TaskBand
;
44 CComPtr
<IWinEventHandler
> WindowEventHandler
;
45 CComPtr
<IContextMenu
> ContextMenu
;
60 virtual ULONG STDMETHODCALLTYPE
AddRef()
62 return InterlockedIncrement(&m_RefCount
);
65 virtual ULONG STDMETHODCALLTYPE
Release()
67 ULONG Ret
= InterlockedDecrement(&m_RefCount
);
75 virtual HRESULT STDMETHODCALLTYPE
QueryInterface(IN REFIID riid
, OUT LPVOID
*ppvObj
)
80 if (IsEqualIID(riid
, IID_IUnknown
) || IsEqualIID(riid
, IID_IBandSiteHelper
))
82 // return IBandSiteStreamCallback's IUnknown
83 *ppvObj
= static_cast<IBandSiteStreamCallback
*>(this);
85 else if (IsEqualIID(riid
, IID_IBandSite
))
87 *ppvObj
= static_cast<IBandSite
*>(this);
89 else if (IsEqualIID(riid
, IID_IWinEventHandler
))
91 TRACE("ITaskBandSite: IWinEventHandler queried!\n");
95 else if (punkInner
!= NULL
)
97 return punkInner
->QueryInterface(riid
, ppvObj
);
102 return E_NOINTERFACE
;
116 virtual ~CTrayBandSite() { }
118 virtual HRESULT STDMETHODCALLTYPE
OnLoad(
119 IN OUT IStream
*pStm
,
123 LARGE_INTEGER liPosZero
;
124 ULARGE_INTEGER liCurrent
;
129 /* NOTE: Callback routine called by the shell while loading the task band
130 stream. We use it to intercept the default behavior when the task
131 band is loaded from the stream.
133 NOTE: riid always points to IID_IUnknown! This is because the shell hasn't
134 read anything from the stream and therefore doesn't know what CLSID
135 it's dealing with. We'll have to find it out ourselves by reading
136 the GUID from the stream. */
138 /* Read the current position of the stream, we'll have to reset it everytime
139 we read a CLSID that's not the task band... */
140 ZeroMemory(&liPosZero
,
142 hRet
= pStm
->Seek(liPosZero
, STREAM_SEEK_CUR
, &liCurrent
);
146 /* Now let's read the CLSID from the stream and see if it's our task band */
147 hRet
= pStm
->Read(&clsid
, (ULONG
)sizeof(clsid
), &ulRead
);
149 if (SUCCEEDED(hRet
) && ulRead
== sizeof(clsid
))
151 if (IsEqualGUID(clsid
, CLSID_ITaskBand
))
153 ASSERT(TaskBand
!= NULL
);
154 /* We're trying to load the task band! Let's create it... */
156 hRet
= TaskBand
->QueryInterface(
161 /* Load the stream */
162 TRACE("IBandSiteStreamCallback::OnLoad intercepted the task band CLSID!\n");
170 /* Reset the position and let the shell do all the work for us */
172 *(LARGE_INTEGER
*) &liCurrent
,
177 /* Let the shell handle everything else for us :) */
178 hRet
= OleLoadFromStream(pStm
,
183 if (!SUCCEEDED(hRet
))
185 TRACE("IBandSiteStreamCallback::OnLoad(0x%p, 0x%p, 0x%p) returns 0x%x\n", pStm
, riid
, pvObj
, hRet
);
191 virtual HRESULT STDMETHODCALLTYPE
OnSave(
192 IN OUT IUnknown
*pUnk
,
193 IN OUT IStream
*pStm
)
195 /* NOTE: Callback routine called by the shell while saving the task band
196 stream. We use it to intercept the default behavior when the task
197 band is saved to the stream */
198 /* FIXME: Implement */
199 TRACE("IBandSiteStreamCallback::OnSave(0x%p, 0x%p) returns E_NOTIMPL\n", pUnk
, pStm
);
203 virtual HRESULT STDMETHODCALLTYPE
IsTaskBand(
206 return IsSameObject((IUnknown
*) BandSite
,
210 virtual HRESULT STDMETHODCALLTYPE
ProcessMessage(
215 OUT LRESULT
*plResult
)
219 ASSERT(hWndRebar
!= NULL
);
221 /* Custom task band behavior */
226 const NMHDR
*nmh
= (const NMHDR
*) lParam
;
228 if (nmh
->hwndFrom
== hWndRebar
)
234 LPNMMOUSE nmm
= (LPNMMOUSE
) lParam
;
236 if (nmm
->dwHitInfo
== RBHT_CLIENT
|| nmm
->dwHitInfo
== RBHT_NOWHERE
||
237 nmm
->dwItemSpec
== (DWORD_PTR
) -1)
239 /* Make the rebar control appear transparent so the user
240 can drag the tray window */
241 *plResult
= HTTRANSPARENT
;
247 /* Deny if an Administrator disabled this "feature" */
248 *plResult
= (SHRestricted(REST_NOMOVINGBAND
) != 0);
253 //TRACE("ITrayBandSite::ProcessMessage: WM_NOTIFY for 0x%p, From: 0x%p, Code: NM_FIRST-%u...\n", hWnd, nmh->hwndFrom, NM_FIRST - nmh->code);
258 /* Forward to the shell's IWinEventHandler interface to get the default shell behavior! */
259 if (!WindowEventHandler
)
262 /*TRACE("Calling IWinEventHandler::ProcessMessage(0x%p, 0x%x, 0x%p, 0x%p, 0x%p) hWndRebar=0x%p\n", hWnd, uMsg, wParam, lParam, plResult, hWndRebar);*/
263 hRet
= WindowEventHandler
->OnWinEvent(hWnd
, uMsg
, wParam
, lParam
, plResult
);
268 if (uMsg
== WM_NOTIFY
)
270 const NMHDR
*nmh
= (const NMHDR
*) lParam
;
271 ERR("ITrayBandSite->IWinEventHandler::ProcessMessage: WM_NOTIFY for 0x%p, From: 0x%p, Code: NM_FIRST-%u returned 0x%x\n", hWnd
, nmh
->hwndFrom
, NM_FIRST
- nmh
->code
, hRet
);
275 ERR("ITrayBandSite->IWinEventHandler::ProcessMessage(0x%p,0x%x,0x%p,0x%p,0x%p->0x%p) returned: 0x%x\n", hWnd
, uMsg
, wParam
, lParam
, plResult
, *plResult
, hRet
);
283 virtual HRESULT STDMETHODCALLTYPE
AddContextMenus(
289 OUT IContextMenu
**ppcm
)
294 if (ContextMenu
== NULL
)
296 /* Cache the context menu so we don't need to CoCreateInstance all the time... */
297 hRet
= CoCreateInstance(CLSID_BandSiteMenu
, NULL
, CLSCTX_INPROC_SERVER
, IID_PPV_ARG(IShellService
, &pSs
));
298 TRACE("CoCreateInstance(CLSID_BandSiteMenu) for IShellService returned: 0x%x\n", hRet
);
299 if (!SUCCEEDED(hRet
))
302 hRet
= pSs
->SetOwner((IBandSite
*)this);
303 if (!SUCCEEDED(hRet
))
309 hRet
= pSs
->QueryInterface(IID_PPV_ARG(IContextMenu
, &ContextMenu
));
313 if (!SUCCEEDED(hRet
))
319 ContextMenu
->AddRef();
323 /* Add the menu items */
324 return ContextMenu
->QueryContextMenu(
332 virtual HRESULT STDMETHODCALLTYPE
Lock(
335 BOOL bPrevLocked
= Locked
;
339 ASSERT(BandSite
!= NULL
);
341 if (bPrevLocked
!= bLock
)
345 bsi
.dwMask
= BSIM_STYLE
;
346 bsi
.dwStyle
= (Locked
? BSIS_LOCKED
| BSIS_NOGRIPPER
: BSIS_AUTOGRIPPER
);
348 hRet
= BandSite
->SetBandSiteInfo(&bsi
);
360 /*******************************************************************/
362 virtual HRESULT STDMETHODCALLTYPE
AddBand(
365 IOleCommandTarget
*pOct
;
368 hRet
= punk
->QueryInterface(IID_PPV_ARG(IOleCommandTarget
, &pOct
));
371 /* Send the DBID_DELAYINIT command to initialize the band to be added */
372 /* FIXME: Should be delayed */
383 return BandSite
->AddBand(
387 virtual HRESULT STDMETHODCALLTYPE
EnumBands(
389 OUT DWORD
*pdwBandID
)
391 return BandSite
->EnumBands(
396 virtual HRESULT STDMETHODCALLTYPE
QueryBand(
398 OUT IDeskBand
**ppstb
,
404 IDeskBand
*pstb
= NULL
;
406 hRet
= BandSite
->QueryBand(
415 hRet
= IsSameObject(pstb
, TaskBand
);
418 /* Add the BSSF_UNDELETEABLE flag to pdwState because the task bar band shouldn't be deletable */
419 if (pdwState
!= NULL
)
420 *pdwState
|= BSSF_UNDELETEABLE
;
422 else if (!SUCCEEDED(hRet
))
431 else if (ppstb
!= NULL
)
437 virtual HRESULT STDMETHODCALLTYPE
SetBandState(
442 return BandSite
->SetBandState(dwBandID
, dwMask
, dwState
);
445 virtual HRESULT STDMETHODCALLTYPE
RemoveBand(
448 return BandSite
->RemoveBand(dwBandID
);
451 virtual HRESULT STDMETHODCALLTYPE
GetBandObject(
456 return BandSite
->GetBandObject(dwBandID
, riid
, ppv
);
459 virtual HRESULT STDMETHODCALLTYPE
SetBandSiteInfo(
460 IN
const BANDSITEINFO
*pbsinfo
)
462 return BandSite
->SetBandSiteInfo(pbsinfo
);
465 virtual HRESULT STDMETHODCALLTYPE
GetBandSiteInfo(
466 IN OUT BANDSITEINFO
*pbsinfo
)
468 return BandSite
->GetBandSiteInfo(pbsinfo
);
471 virtual BOOL
HasTaskBand()
473 ASSERT(TaskBand
!= NULL
);
475 return SUCCEEDED(TaskBand
->GetRebarBandID(
479 virtual HRESULT
AddTaskBand()
482 /* FIXME: This is the code for the simple taskbar */
483 IObjectWithSite
*pOws
;
486 hRet
= TaskBand
->QueryInterface(
487 &IID_IObjectWithSite
,
491 hRet
= pOws
->SetSite(
492 (IUnknown
*)TaskBand
);
501 return BandSite
->AddBand(TaskBand
);
508 virtual HRESULT
Update()
510 IOleCommandTarget
*pOct
;
513 hRet
= punkInner
->QueryInterface(IID_PPV_ARG(IOleCommandTarget
, &pOct
));
516 /* Send the DBID_BANDINFOCHANGED command to update the band site */
519 DBID_BANDINFOCHANGED
,
530 virtual VOID
BroadcastOleCommandExec(const GUID
*pguidCmdGroup
,
536 IOleCommandTarget
*pOct
;
540 /* Enumerate all bands */
541 while (SUCCEEDED(BandSite
->EnumBands(uBand
, &dwBandID
)))
543 if (SUCCEEDED(BandSite
->GetBandObject(dwBandID
, IID_PPV_ARG(IOleCommandTarget
, &pOct
))))
545 /* Execute the command */
560 virtual HRESULT
FinishInit()
562 /* Broadcast the DBID_FINISHINIT command */
563 BroadcastOleCommandExec(&IID_IDeskBand
, DBID_FINISHINIT
, 0, NULL
, NULL
);
568 virtual HRESULT
Show(
571 IDeskBarClient
*pDbc
;
574 hRet
= BandSite
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &pDbc
));
577 hRet
= pDbc
->UIActivateDBC(
578 bShow
? DBC_SHOW
: DBC_HIDE
);
585 virtual HRESULT
LoadFromStream(IN OUT IStream
*pStm
)
587 IPersistStream
*pPStm
;
590 ASSERT(BandSite
!= NULL
);
592 /* We implement the undocumented COM interface IBandSiteStreamCallback
593 that the shell will query so that we can intercept and custom-load
594 the task band when it finds the task band's CLSID (which is internal).
595 This way we can prevent the shell from attempting to CoCreateInstance
596 the (internal) task band, resulting in a failure... */
597 hRet
= BandSite
->QueryInterface(IID_PPV_ARG(IPersistStream
, &pPStm
));
602 TRACE("->Load() returned 0x%x\n", hRet
);
610 GetUserBandsStream(IN DWORD grfMode
)
613 IStream
*Stream
= NULL
;
615 if (RegCreateKey(hkExplorer
,
617 &hkStreams
) == ERROR_SUCCESS
)
619 Stream
= SHOpenRegStream(hkStreams
,
621 TEXT("TaskbarWinXP"),
624 RegCloseKey(hkStreams
);
631 GetDefaultBandsStream(IN DWORD grfMode
)
634 IStream
*Stream
= NULL
;
636 if (RegCreateKey(HKEY_LOCAL_MACHINE
,
637 TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Streams"),
638 &hkStreams
) == ERROR_SUCCESS
)
640 Stream
= SHOpenRegStream(hkStreams
,
642 TEXT("Default Taskbar"),
645 RegCloseKey(hkStreams
);
651 virtual HRESULT
Load()
656 /* Try to load the user's settings */
657 pStm
= GetUserBandsStream(STGM_READ
);
660 hRet
= LoadFromStream(pStm
);
662 TRACE("Loaded user bands settings: 0x%x\n", hRet
);
668 /* If the user's settings couldn't be loaded, try with
669 default settings (ie. when the user logs in for the
671 if (!SUCCEEDED(hRet
))
673 pStm
= GetDefaultBandsStream(STGM_READ
);
676 hRet
= LoadFromStream(pStm
);
678 TRACE("Loaded default user bands settings: 0x%x\n", hRet
);
688 HRESULT
_Init(IN OUT ITrayWindow
*tray
, OUT HWND
*phWndRebar
, OUT HWND
*phwndTaskSwitch
)
690 IDeskBarClient
*pDbc
;
696 *phwndTaskSwitch
= NULL
;
700 /* Create a RebarBandSite provided by the shell */
701 hRet
= CoCreateInstance(CLSID_RebarBandSite
,
702 static_cast<IBandSite
*>(this),
703 CLSCTX_INPROC_SERVER
,
704 IID_PPV_ARG(IUnknown
, &punkInner
));
705 if (!SUCCEEDED(hRet
))
710 hRet
= punkInner
->QueryInterface(IID_PPV_ARG(IBandSite
, &BandSite
));
711 if (!SUCCEEDED(hRet
))
716 hRet
= punkInner
->QueryInterface(IID_PPV_ARG(IWinEventHandler
, &WindowEventHandler
));
717 if (!SUCCEEDED(hRet
))
722 TaskBand
= CreateTaskBand(Tray
);
723 if (TaskBand
!= NULL
)
725 /* Add the task band to the site */
726 hRet
= BandSite
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &pDbc
));
729 hRet
= TaskBand
->QueryInterface(IID_PPV_ARG(IOleWindow
, &pOw
));
732 /* We cause IDeskBarClient to create the rebar control by passing the new
733 task band to it. The band reports the tray window handle as window handle
734 so that IDeskBarClient knows the parent window of the Rebar control that
735 it wants to create. */
736 hRet
= pDbc
->SetDeskBarSite(pOw
);
740 /* The Rebar control is now created, we can query the window handle */
741 hRet
= pDbc
->GetWindow(&hWndRebar
);
745 /* We need to manually remove the RBS_BANDBORDERS style! */
746 SetWindowStyle(hWndRebar
, RBS_BANDBORDERS
, 0);
757 /* Set the Desk Bar mode to the current one */
759 /* FIXME: We need to set the mode (and update) whenever the user docks
760 the tray window to another monitor edge! */
762 if (!Tray
->IsHorizontal())
763 dwMode
= DBIF_VIEWMODE_VERTICAL
;
765 hRet
= pDbc
->SetModeDBC(dwMode
);
771 /* Load the saved state of the task band site */
772 /* FIXME: We should delay loading shell extensions, also see DBID_DELAYINIT */
775 /* Add the task bar band if it hasn't been added already */
776 hRet
= AddTaskBand();
779 hRet
= TaskBand
->QueryInterface(IID_PPV_ARG(IDeskBand
, &pDb
));
782 hRet
= pDb
->GetWindow(phwndTaskSwitch
);
783 if (!SUCCEEDED(hRet
))
784 *phwndTaskSwitch
= NULL
;
790 /* Should we send this after showing it? */
793 /* FIXME: When should we send this? Does anyone care anyway? */
796 /* Activate the band site */
801 *phWndRebar
= hWndRebar
;
806 /*******************************************************************/
809 CreateTrayBandSite(IN OUT ITrayWindow
*Tray
,
810 OUT HWND
*phWndRebar
,
811 OUT HWND
*phWndTaskSwitch
)
815 CTrayBandSite
* tb
= new CTrayBandSite();
822 hr
= tb
->_Init(Tray
, phWndRebar
, phWndTaskSwitch
);
823 if (FAILED_UNEXPECTEDLY(hr
))