4 * Copyright 2014 David Quintana
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 St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <shlwapi_undoc.h>
24 #include "CMenuDeskBar.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(CMenuDeskBar
);
28 const static GUID CGID_MenuDeskBar
= { 0x5C9F0A12, 0x959E, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x08, 0x26, 0x36 } };
31 HRESULT WINAPI
CMenuDeskBar_Constructor(REFIID riid
, LPVOID
*ppv
)
33 return ShellObjectCreator
<CMenuDeskBar
>(riid
, ppv
);
36 CMenuDeskBar::CMenuDeskBar() :
46 CMenuDeskBar::~CMenuDeskBar()
50 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::Initialize(THIS
)
55 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetWindow(HWND
*lphwnd
)
63 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::ContextSensitiveHelp(BOOL fEnterMode
)
68 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::OnFocusChangeIS(IUnknown
*punkObj
, BOOL fSetFocus
)
70 CComPtr
<IInputObjectSite
> ios
;
72 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IInputObjectSite
, &ios
));
73 if (FAILED_UNEXPECTEDLY(hr
))
76 return ios
->OnFocusChangeIS(punkObj
, fSetFocus
);
79 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::QueryStatus(const GUID
*pguidCmdGroup
, ULONG cCmds
,
80 OLECMD prgCmds
[], OLECMDTEXT
*pCmdText
)
85 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::Exec(const GUID
*pguidCmdGroup
, DWORD nCmdID
,
86 DWORD nCmdexecopt
, VARIANT
*pvaIn
, VARIANT
*pvaOut
)
88 if (IsEqualIID(*pguidCmdGroup
, CGID_MenuDeskBar
))
94 case 3: // load complete
96 case 4: // set font metrics
100 if (IsEqualIID(*pguidCmdGroup
, CGID_Explorer
))
103 else if (IsEqualIID(*pguidCmdGroup
, IID_IDeskBarClient
))
119 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::QueryService(REFGUID guidService
, REFIID riid
, void **ppvObject
)
121 if (IsEqualGUID(guidService
, SID_SMenuPopup
) ||
122 IsEqualGUID(guidService
, SID_SMenuBandParent
) ||
123 IsEqualGUID(guidService
, SID_STopLevelBrowser
))
125 return this->QueryInterface(riid
, ppvObject
);
128 if (IsEqualGUID(guidService
, SID_SMenuBandBottom
) ||
129 IsEqualGUID(guidService
, SID_SMenuBandBottomSelected
) ||
130 IsEqualGUID(guidService
, SID_SMenuBandChild
))
132 if (m_Client
== NULL
)
133 return E_NOINTERFACE
;
135 return IUnknown_QueryService(m_Client
, guidService
, riid
, ppvObject
);
140 return E_NOINTERFACE
;
142 return IUnknown_QueryService(m_Site
, guidService
, riid
, ppvObject
);
145 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::UIActivateIO(BOOL fActivate
, LPMSG lpMsg
)
147 return IUnknown_UIActivateIO(m_Client
, fActivate
, lpMsg
);
150 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::HasFocusIO()
152 CComPtr
<IInputObject
> io
;
154 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IInputObject
, &io
));
155 if (FAILED_UNEXPECTEDLY(hr
))
158 return io
->HasFocusIO();
161 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::TranslateAcceleratorIO(LPMSG lpMsg
)
163 CComPtr
<IInputObject
> io
;
165 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IInputObject
, &io
));
166 if (FAILED_UNEXPECTEDLY(hr
))
169 return io
->TranslateAcceleratorIO(lpMsg
);
172 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetClient(IUnknown
*punkClient
)
174 CComPtr
<IDeskBarClient
> pDeskBandClient
;
179 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &pDeskBandClient
));
180 if (FAILED_UNEXPECTEDLY(hr
))
183 pDeskBandClient
->SetDeskBarSite(NULL
);
185 pDeskBandClient
= NULL
;
189 if (punkClient
== NULL
)
197 hr
= punkClient
->QueryInterface(IID_PPV_ARG(IUnknown
, &m_Client
));
198 if (FAILED_UNEXPECTEDLY(hr
))
201 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &pDeskBandClient
));
202 if (FAILED_UNEXPECTEDLY(hr
))
205 hr
= pDeskBandClient
->SetDeskBarSite(static_cast<IDeskBar
*>(this));
206 if (FAILED_UNEXPECTEDLY(hr
))
209 return IUnknown_GetWindow(m_Client
, &m_ClientWindow
);
212 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetClient(IUnknown
**ppunkClient
)
214 if (ppunkClient
== NULL
)
220 return m_Client
->QueryInterface(IID_PPV_ARG(IUnknown
, ppunkClient
));
223 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::OnPosRectChangeDB(LPRECT prc
)
231 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetSite(IUnknown
*pUnkSite
)
233 // Windows closes the bar if this is called when the bar is shown
238 m_SubMenuParent
= NULL
;
244 IUnknown_QueryService(m_Site
, SID_SMenuPopup
, IID_PPV_ARG(IMenuPopup
, &m_SubMenuParent
));
255 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetSite(REFIID riid
, void **ppvSite
)
260 return m_Site
->QueryInterface(riid
, ppvSite
);
263 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::Popup(POINTL
*ppt
, RECTL
*prcExclude
, MP_POPUPFLAGS dwFlags
)
266 CComPtr
<IOleCommandTarget
> oct
;
267 CComPtr
<IInputObject
> io
;
268 CComPtr
<IDeskBand
> band
;
269 CComPtr
<IDeskBarClient
> dbc
;
274 hr
= IUnknown_QueryService(m_Client
, SID_SMenuBandChild
, IID_PPV_ARG(IOleCommandTarget
, &oct
));
275 if (FAILED_UNEXPECTEDLY(hr
))
278 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &dbc
));
279 if (FAILED_UNEXPECTEDLY(hr
))
282 // Windows calls this, but it appears to be unimplemented?
283 hr
= dbc
->SetModeDBC(1);
284 // Allow it to fail with E_NOTIMPL.
286 // No clue about the arg, using anything != 0
287 hr
= dbc
->UIActivateDBC(TRUE
);
288 if (FAILED_UNEXPECTEDLY(hr
))
292 hr
= dbc
->GetSize(0, &rc
);
293 if (FAILED_UNEXPECTEDLY(hr
))
298 const int CMD_EXEC_OPT
= 0;
300 hr
= IUnknown_QueryServiceExec(m_Client
, SID_SMenuBandChild
, &CLSID_MenuBand
, CMD
, CMD_EXEC_OPT
, NULL
, NULL
);
301 if (FAILED_UNEXPECTEDLY(hr
))
304 ::AdjustWindowRect(&rc
, ::GetWindowLong(m_hWnd
, GWL_STYLE
), FALSE
);
305 ::OffsetRect(&rc
, -rc
.left
, -rc
.top
);
307 if (m_Banner
!= NULL
)
310 ::GetObject(m_Banner
, sizeof(bm
), &bm
);
311 rc
.right
+= bm
.bmWidth
;
315 GetWindowRect(GetDesktopWindow(), &rcWorkArea
);
316 int waHeight
= rcWorkArea
.bottom
- rcWorkArea
.top
;
320 int cx
= rc
.right
- rc
.left
;
321 int cy
= rc
.bottom
- rc
.top
;
323 switch (dwFlags
& 0xFF000000)
327 y
= ppt
->y
- rc
.bottom
;
330 x
= ppt
->x
+ rc
.left
;
333 case MPPF_TOP
| MPPF_ALIGN_LEFT
:
334 x
= ppt
->x
- rc
.right
;
337 case MPPF_TOP
| MPPF_ALIGN_RIGHT
:
343 if (x
+ cx
> rcWorkArea
.right
)
345 // FIXME: Works, but it's oversimplified.
346 x
= prcExclude
->left
- cx
;
347 dwFlags
= (dwFlags
& (~MPPF_TOP
)) | MPPF_LEFT
;
350 if (y
< rcWorkArea
.top
)
360 if (y
+ cy
> rcWorkArea
.bottom
)
362 y
= rcWorkArea
.bottom
- cy
;
365 this->SetWindowPos(HWND_TOPMOST
, x
, y
, cx
, cy
, SWP_SHOWWINDOW
| SWP_NOACTIVATE
);
367 m_ShowFlags
= dwFlags
;
370 // HACK: The bar needs to be notified of the size AFTER it is shown.
371 // Quick & dirty way of getting it done.
373 _OnSize(WM_SIZE
, 0, 0, bHandled
);
375 UIActivateIO(TRUE
, NULL
);
377 if (dwFlags
& (MPPF_INITIALSELECT
| MPPF_FINALSELECT
))
379 const int CMD_SELECT
= 5;
380 int CMD_SELECT_OPTS
= dwFlags
& MPPF_INITIALSELECT
? 0 : -2;
381 IUnknown_QueryServiceExec(m_Client
, SID_SMenuBandChild
, &CLSID_MenuBand
, CMD_SELECT
, CMD_SELECT_OPTS
, NULL
, NULL
);
387 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetIconSize(THIS_ DWORD iIcon
)
392 // Unknown meaning (set flags? set icon size?)
394 const int CMD_EXEC_OPT
= iIcon
? 0 : 2; // seems to work
396 hr
= IUnknown_QueryServiceExec(m_Client
, SID_SMenuBandChild
, &CLSID_MenuBand
, CMD
, CMD_EXEC_OPT
, NULL
, NULL
);
397 if (FAILED_UNEXPECTEDLY(hr
))
401 _OnSize(WM_SIZE
, 0, 0, bHandled
);
406 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetIconSize(THIS_ DWORD
* piIcon
)
409 *piIcon
= m_IconSize
;
413 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetBitmap(THIS_ HBITMAP hBitmap
)
418 _OnSize(WM_SIZE
, 0, 0, bHandled
);
423 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetBitmap(THIS_ HBITMAP
* phBitmap
)
426 *phBitmap
= m_Banner
;
430 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetSubMenu(IMenuPopup
*pmp
, BOOL fSet
)
432 // Called by the MenuBand to assign itself as the logical child of the DeskBar
436 m_SubMenuChild
= pmp
;
442 if (pmp
== m_SubMenuChild
)
444 m_SubMenuChild
= NULL
;
451 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::OnSelect(DWORD dwSelectType
)
453 /* As far as I can tell, the submenu hierarchy looks like this:
455 * The DeskBar's Child is the Band it contains.
456 * The DeskBar's Parent is the SID_SMenuPopup of the Site.
458 * The Band's Child is the IMenuPopup of the child submenu.
459 * The Band's Parent is the SID_SMenuPopup of the Site (the DeskBar).
461 * When the DeskBar receives a selection event:
462 * If it requires closing the window, it will notify the Child (Band) using CancelLevel.
463 * If it has to spread upwards (everything but CancelLevel), it will notify the Parent.
465 * When the Band receives a selection event, this is where it gets fuzzy:
466 * In which cases does it call the Parent? Probably not CancelLevel.
467 * In which cases does it call the Child?
468 * How does it react to calls?
472 switch (dwSelectType
)
475 case MPOS_FULLCANCEL
:
476 case MPOS_CANCELLEVEL
:
480 if (dwSelectType
== MPOS_CANCELLEVEL
)
483 case MPOS_SELECTLEFT
:
484 case MPOS_SELECTRIGHT
:
485 case MPOS_CHILDTRACKING
:
487 return m_SubMenuParent
->OnSelect(dwSelectType
);
494 HRESULT
CMenuDeskBar::_CloseBar()
496 CComPtr
<IDeskBarClient
> dbc
;
503 m_SubMenuParent
->SetSubMenu(this, FALSE
);
508 hr
= m_SubMenuChild
->OnSelect(MPOS_CANCELLEVEL
);
509 if (FAILED_UNEXPECTEDLY(hr
))
513 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &dbc
));
514 if (FAILED_UNEXPECTEDLY(hr
))
517 hr
= dbc
->UIActivateDBC(FALSE
);
518 if (FAILED_UNEXPECTEDLY(hr
))
521 SetWindowPos(NULL
, 0, 0, 0, 0, SWP_HIDEWINDOW
| SWP_NOACTIVATE
| SWP_NOMOVE
);
523 return UIActivateIO(FALSE
, NULL
);
526 BOOL
CMenuDeskBar::_IsSubMenuParent(HWND hwnd
)
528 CComPtr
<IMenuPopup
> popup
= m_SubMenuParent
;
533 CComPtr
<IOleWindow
> window
;
535 hr
= popup
->QueryInterface(IID_PPV_ARG(IOleWindow
, &window
));
536 if (FAILED_UNEXPECTEDLY(hr
))
541 hr
= window
->GetWindow(&parent
);
542 if (SUCCEEDED(hr
) && hwnd
== parent
)
546 hr
= IUnknown_GetSite(window
, IID_PPV_ARG(IMenuPopup
, &popup
));
554 LRESULT
CMenuDeskBar::_OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
562 if (m_Banner
!= NULL
)
565 ::GetObject(m_Banner
, sizeof(bm
), &bm
);
566 rc
.left
+= bm
.bmWidth
;
569 ::SetWindowPos(m_ClientWindow
, NULL
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, 0);
575 LRESULT
CMenuDeskBar::_OnNotify(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
580 CComPtr
<IWinEventHandler
> winEventHandler
;
581 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IWinEventHandler
, &winEventHandler
));
582 if (FAILED_UNEXPECTEDLY(hr
))
588 hr
= winEventHandler
->OnWinEvent(NULL
, uMsg
, wParam
, lParam
, &result
);
589 if (FAILED_UNEXPECTEDLY(hr
))
597 LRESULT
CMenuDeskBar::_OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
601 if (m_Banner
&& !m_IconSize
)
605 HDC hdc
= BeginPaint(&ps
);
607 HDC hdcMem
= ::CreateCompatibleDC(hdc
);
608 HGDIOBJ hbmOld
= ::SelectObject(hdcMem
, m_Banner
);
610 ::GetObject(m_Banner
, sizeof(bm
), &bm
);
613 if (!GetClientRect(&rc
))
614 WARN("GetClientRect failed\n");
616 const int bx
= bm
.bmWidth
;
617 const int by
= bm
.bmHeight
;
618 const int cy
= rc
.bottom
;
620 TRACE("Painting banner: %d by %d\n", bm
.bmWidth
, bm
.bmHeight
);
622 if (!::StretchBlt(hdc
, 0, 0, bx
, cy
- by
, hdcMem
, 0, 0, bx
, 1, SRCCOPY
))
623 WARN("StretchBlt failed\n");
625 if (!::BitBlt(hdc
, 0, cy
- by
, bx
, by
, hdcMem
, 0, 0, SRCCOPY
))
626 WARN("BitBlt failed\n");
628 ::SelectObject(hdcMem
, hbmOld
);
637 LRESULT
CMenuDeskBar::_OnActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
639 // BUG in ReactOS: WM_ACTIVATE/WA_INACTIVE makes no sense with lParam==hWnd
640 if (LOWORD(wParam
) != 0 || reinterpret_cast<HWND
>(lParam
) == m_hWnd
)
645 // HACK! I just want it to work !!!
646 CComPtr
<IDeskBar
> db
;
647 HRESULT hr
= IUnknown_QueryService(m_Client
, SID_SMenuBandChild
, IID_PPV_ARG(IDeskBar
, &db
));
648 if (FAILED_UNEXPECTEDLY(hr
))
651 CComPtr
<IUnknown
> punk
;
653 hr
= db
->GetClient(&punk
);
654 if (FAILED_UNEXPECTEDLY(hr
))
657 if (!punk
&& m_Shown
)
659 if (!_IsSubMenuParent(reinterpret_cast<HWND
>(lParam
)))
661 OnSelect(MPOS_FULLCANCEL
);
668 LRESULT
CMenuDeskBar::_OnMouseActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
670 return MA_NOACTIVATE
;
673 LRESULT
CMenuDeskBar::_OnAppActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
675 if (wParam
== 0 && m_Shown
)
677 OnSelect(MPOS_FULLCANCEL
);