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
> m_Tray
;
41 CComPtr
<IUnknown
> m_Inner
;
42 CComPtr
<IBandSite
> m_BandSite
;
43 CComPtr
<IDeskBand
> m_TaskBand
;
44 CComPtr
<IWinEventHandler
> m_WindowEventHandler
;
45 CComPtr
<IContextMenu
> m_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 (m_Inner
!= NULL
)
97 return m_Inner
->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
, sizeof(liPosZero
));
141 hRet
= pStm
->Seek(liPosZero
, STREAM_SEEK_CUR
, &liCurrent
);
145 /* Now let's read the CLSID from the stream and see if it's our task band */
146 hRet
= pStm
->Read(&clsid
, (ULONG
)sizeof(clsid
), &ulRead
);
148 if (SUCCEEDED(hRet
) && ulRead
== sizeof(clsid
))
150 if (IsEqualGUID(clsid
, CLSID_ITaskBand
))
152 ASSERT(m_TaskBand
!= NULL
);
153 /* We're trying to load the task band! Let's create it... */
155 hRet
= m_TaskBand
->QueryInterface(
160 /* Load the stream */
161 TRACE("IBandSiteStreamCallback::OnLoad intercepted the task band CLSID!\n");
169 /* Reset the position and let the shell do all the work for us */
171 *(LARGE_INTEGER
*) &liCurrent
,
176 /* Let the shell handle everything else for us :) */
177 hRet
= OleLoadFromStream(pStm
,
182 if (!SUCCEEDED(hRet
))
184 TRACE("IBandSiteStreamCallback::OnLoad(0x%p, 0x%p, 0x%p) returns 0x%x\n", pStm
, riid
, pvObj
, hRet
);
190 virtual HRESULT STDMETHODCALLTYPE
OnSave(
191 IN OUT IUnknown
*pUnk
,
192 IN OUT IStream
*pStm
)
194 /* NOTE: Callback routine called by the shell while saving the task band
195 stream. We use it to intercept the default behavior when the task
196 band is saved to the stream */
197 /* FIXME: Implement */
198 TRACE("IBandSiteStreamCallback::OnSave(0x%p, 0x%p) returns E_NOTIMPL\n", pUnk
, pStm
);
202 virtual HRESULT STDMETHODCALLTYPE
IsTaskBand(IN IUnknown
*punk
)
204 return IsSameObject(m_BandSite
, punk
);
207 virtual HRESULT STDMETHODCALLTYPE
ProcessMessage(
212 OUT LRESULT
*plResult
)
216 ASSERT(m_Rebar
!= NULL
);
218 /* Custom task band behavior */
223 const NMHDR
*nmh
= (const NMHDR
*) lParam
;
225 if (nmh
->hwndFrom
== m_Rebar
)
231 LPNMMOUSE nmm
= (LPNMMOUSE
) lParam
;
233 if (nmm
->dwHitInfo
== RBHT_CLIENT
|| nmm
->dwHitInfo
== RBHT_NOWHERE
||
234 nmm
->dwItemSpec
== (DWORD_PTR
) -1)
236 /* Make the rebar control appear transparent so the user
237 can drag the tray window */
238 *plResult
= HTTRANSPARENT
;
244 /* Deny if an Administrator disabled this "feature" */
245 *plResult
= (SHRestricted(REST_NOMOVINGBAND
) != 0);
250 //TRACE("ITrayBandSite::ProcessMessage: WM_NOTIFY for 0x%p, From: 0x%p, Code: NM_FIRST-%u...\n", hWnd, nmh->hwndFrom, NM_FIRST - nmh->code);
255 /* Forward to the shell's IWinEventHandler interface to get the default shell behavior! */
256 if (!m_WindowEventHandler
)
259 /*TRACE("Calling IWinEventHandler::ProcessMessage(0x%p, 0x%x, 0x%p, 0x%p, 0x%p) hWndRebar=0x%p\n", hWnd, uMsg, wParam, lParam, plResult, hWndRebar);*/
260 hRet
= m_WindowEventHandler
->OnWinEvent(hWnd
, uMsg
, wParam
, lParam
, plResult
);
265 if (uMsg
== WM_NOTIFY
)
267 const NMHDR
*nmh
= (const NMHDR
*) lParam
;
268 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
);
272 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
);
280 virtual HRESULT STDMETHODCALLTYPE
AddContextMenus(
286 OUT IContextMenu
**ppcm
)
290 if (m_ContextMenu
== NULL
)
292 /* Cache the context menu so we don't need to CoCreateInstance all the time... */
293 hRet
= _CBandSiteMenu_CreateInstance(IID_PPV_ARG(IContextMenu
, &m_ContextMenu
));
294 if (FAILED_UNEXPECTEDLY(hRet
))
297 hRet
= IUnknown_SetOwner(m_ContextMenu
, (IBandSite
*)this);
298 if (FAILED_UNEXPECTEDLY(hRet
))
304 m_ContextMenu
->AddRef();
305 *ppcm
= m_ContextMenu
;
308 /* Add the menu items */
309 hRet
= m_ContextMenu
->QueryContextMenu(hmenu
, indexMenu
, idCmdFirst
, idCmdLast
, uFlags
);
310 if (FAILED_UNEXPECTEDLY(hRet
))
316 virtual HRESULT STDMETHODCALLTYPE
Lock(IN BOOL bLock
)
318 BOOL bPrevLocked
= Locked
;
322 ASSERT(m_BandSite
!= NULL
);
324 if (bPrevLocked
!= bLock
)
328 bsi
.dwMask
= BSIM_STYLE
;
329 bsi
.dwStyle
= (Locked
? BSIS_LOCKED
| BSIS_NOGRIPPER
: BSIS_AUTOGRIPPER
);
331 hRet
= m_BandSite
->SetBandSiteInfo(&bsi
);
333 /* HACK for CORE-9809 ! */
334 if (hRet
== E_NOTIMPL
)
337 ERR("HACK for CORE-9809 no longer needed!\n");
350 /*******************************************************************/
352 virtual HRESULT STDMETHODCALLTYPE
AddBand(IN IUnknown
*punk
)
354 /* Send the DBID_DELAYINIT command to initialize the band to be added */
355 /* FIXME: Should be delayed */
356 IUnknown_Exec(punk
, IID_IDeskBand
, DBID_DELAYINIT
, 0, NULL
, NULL
);
358 HRESULT hr
= m_BandSite
->AddBand(punk
);
359 if (FAILED_UNEXPECTEDLY(hr
))
363 V_VT(&vThemeName
) = VT_BSTR
;
364 V_BSTR(&vThemeName
) = SysAllocString(L
"TaskBar");
372 SysFreeString(V_BSTR(&vThemeName
));
377 virtual HRESULT STDMETHODCALLTYPE
EnumBands(
379 OUT DWORD
*pdwBandID
)
381 return m_BandSite
->EnumBands(uBand
, pdwBandID
);
384 virtual HRESULT STDMETHODCALLTYPE
QueryBand(
386 OUT IDeskBand
**ppstb
,
392 IDeskBand
*pstb
= NULL
;
394 hRet
= m_BandSite
->QueryBand(
403 hRet
= IsSameObject(pstb
, m_TaskBand
);
406 /* Add the BSSF_UNDELETEABLE flag to pdwState because the task bar band shouldn't be deletable */
407 if (pdwState
!= NULL
)
408 *pdwState
|= BSSF_UNDELETEABLE
;
410 else if (!SUCCEEDED(hRet
))
419 else if (ppstb
!= NULL
)
425 virtual HRESULT STDMETHODCALLTYPE
SetBandState(
430 return m_BandSite
->SetBandState(dwBandID
, dwMask
, dwState
);
433 virtual HRESULT STDMETHODCALLTYPE
RemoveBand(
436 return m_BandSite
->RemoveBand(dwBandID
);
439 virtual HRESULT STDMETHODCALLTYPE
GetBandObject(
444 return m_BandSite
->GetBandObject(dwBandID
, riid
, ppv
);
447 virtual HRESULT STDMETHODCALLTYPE
SetBandSiteInfo(
448 IN
const BANDSITEINFO
*pbsinfo
)
450 return m_BandSite
->SetBandSiteInfo(pbsinfo
);
453 virtual HRESULT STDMETHODCALLTYPE
GetBandSiteInfo(
454 IN OUT BANDSITEINFO
*pbsinfo
)
456 return m_BandSite
->GetBandSiteInfo(pbsinfo
);
459 virtual BOOL
HasTaskBand()
461 CComPtr
<IPersist
> pBand
;
466 /* Enumerate all bands */
467 while (SUCCEEDED(m_BandSite
->EnumBands(uBand
, &dwBandID
)))
469 if (SUCCEEDED(m_BandSite
->GetBandObject(dwBandID
, IID_PPV_ARG(IPersist
, &pBand
))))
471 if (SUCCEEDED(pBand
->GetClassID(&BandCLSID
)))
473 if (IsEqualGUID(BandCLSID
, CLSID_ITaskBand
))
485 virtual HRESULT
Update()
487 return IUnknown_Exec(m_Inner
,
489 DBID_BANDINFOCHANGED
,
495 virtual VOID
BroadcastOleCommandExec(REFGUID pguidCmdGroup
,
501 IOleCommandTarget
*pOct
;
505 /* Enumerate all bands */
506 while (SUCCEEDED(m_BandSite
->EnumBands(uBand
, &dwBandID
)))
508 if (SUCCEEDED(m_BandSite
->GetBandObject(dwBandID
, IID_PPV_ARG(IOleCommandTarget
, &pOct
))))
510 /* Execute the command */
525 virtual HRESULT
FinishInit()
527 /* Broadcast the DBID_FINISHINIT command */
528 BroadcastOleCommandExec(IID_IDeskBand
, DBID_FINISHINIT
, 0, NULL
, NULL
);
533 virtual HRESULT
Show(IN BOOL bShow
)
535 CComPtr
<IDeskBarClient
> pDbc
;
538 hRet
= m_BandSite
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &pDbc
));
541 hRet
= pDbc
->UIActivateDBC(bShow
? DBC_SHOW
: DBC_HIDE
);
547 virtual HRESULT
LoadFromStream(IN OUT IStream
*pStm
)
549 CComPtr
<IPersistStream
> pPStm
;
552 ASSERT(m_BandSite
!= NULL
);
554 /* We implement the undocumented COM interface IBandSiteStreamCallback
555 that the shell will query so that we can intercept and custom-load
556 the task band when it finds the task band's CLSID (which is internal).
557 This way we can prevent the shell from attempting to CoCreateInstance
558 the (internal) task band, resulting in a failure... */
559 hRet
= m_BandSite
->QueryInterface(IID_PPV_ARG(IPersistStream
, &pPStm
));
562 hRet
= pPStm
->Load(pStm
);
563 TRACE("->Load() returned 0x%x\n", hRet
);
569 virtual IStream
* GetUserBandsStream(IN DWORD grfMode
)
572 IStream
*Stream
= NULL
;
574 if (RegCreateKeyW(hkExplorer
,
576 &hkStreams
) == ERROR_SUCCESS
)
578 Stream
= SHOpenRegStreamW(hkStreams
,
583 RegCloseKey(hkStreams
);
589 virtual IStream
* GetDefaultBandsStream(IN DWORD grfMode
)
592 IStream
*Stream
= NULL
;
594 if (RegCreateKeyW(HKEY_LOCAL_MACHINE
,
595 L
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Streams",
596 &hkStreams
) == ERROR_SUCCESS
)
598 Stream
= SHOpenRegStreamW(hkStreams
,
603 RegCloseKey(hkStreams
);
609 virtual HRESULT
Load()
614 /* Try to load the user's settings */
615 pStm
= GetUserBandsStream(STGM_READ
);
618 hRet
= LoadFromStream(pStm
);
620 TRACE("Loaded user bands settings: 0x%x\n", hRet
);
626 /* If the user's settings couldn't be loaded, try with
627 default settings (ie. when the user logs in for the
629 if (!SUCCEEDED(hRet
))
631 pStm
= GetDefaultBandsStream(STGM_READ
);
634 hRet
= LoadFromStream(pStm
);
636 TRACE("Loaded default user bands settings: 0x%x\n", hRet
);
646 HRESULT
_Init(IN ITrayWindow
*tray
, IN IDeskBand
* pTaskBand
)
648 CComPtr
<IDeskBarClient
> pDbc
;
649 CComPtr
<IDeskBand
> pDb
;
650 CComPtr
<IOleWindow
> pOw
;
654 m_TaskBand
= pTaskBand
;
656 /* Create the RebarBandSite */
657 hRet
= _CBandSite_CreateInstance(static_cast<IBandSite
*>(this), IID_PPV_ARG(IUnknown
, &m_Inner
));
658 if (FAILED_UNEXPECTEDLY(hRet
))
661 hRet
= m_Inner
->QueryInterface(IID_PPV_ARG(IBandSite
, &m_BandSite
));
662 if (FAILED_UNEXPECTEDLY(hRet
))
665 hRet
= m_Inner
->QueryInterface(IID_PPV_ARG(IWinEventHandler
, &m_WindowEventHandler
));
666 if (FAILED_UNEXPECTEDLY(hRet
))
669 hRet
= m_Inner
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &pDbc
));
670 if (FAILED_UNEXPECTEDLY(hRet
))
676 /* Crete the rebar in the tray */
677 hRet
= pDbc
->SetDeskBarSite(tray
);
678 if (FAILED_UNEXPECTEDLY(hRet
))
681 hRet
= pDbc
->GetWindow(&m_Rebar
);
682 if (FAILED_UNEXPECTEDLY(hRet
))
685 SetWindowStyle(m_Rebar
, RBS_BANDBORDERS
, 0);
687 /* Set the Desk Bar mode to the current one */
689 /* FIXME: We need to set the mode (and update) whenever the user docks
690 the tray window to another monitor edge! */
691 if (!m_Tray
->IsHorizontal())
692 dwMode
= DBIF_VIEWMODE_VERTICAL
;
694 hRet
= pDbc
->SetModeDBC(dwMode
);
696 /* Load the saved state of the task band site */
697 /* FIXME: We should delay loading shell extensions, also see DBID_DELAYINIT */
700 /* Add the task bar band if it hasn't been added while loading */
703 hRet
= m_BandSite
->AddBand(m_TaskBand
);
704 if (FAILED_UNEXPECTEDLY(hRet
))
708 /* Should we send this after showing it? */
711 /* FIXME: When should we send this? Does anyone care anyway? */
714 /* Activate the band site */
720 /*******************************************************************/
722 HRESULT
CTrayBandSite_CreateInstance(IN ITrayWindow
*tray
, IN IDeskBand
* pTaskBand
, OUT ITrayBandSite
** pBandSite
)
726 CTrayBandSite
* tb
= new CTrayBandSite();
732 hr
= tb
->_Init(tray
, pTaskBand
);
733 if (FAILED_UNEXPECTEDLY(hr
))