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 LRESULT
CMenuDeskBar::_OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
58 void CMenuDeskBar::OnFinalMessage(HWND
/* hWnd */)
63 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::Initialize(THIS
)
68 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetWindow(HWND
*lphwnd
)
76 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::ContextSensitiveHelp(BOOL fEnterMode
)
81 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::OnFocusChangeIS(IUnknown
*punkObj
, BOOL fSetFocus
)
83 CComPtr
<IInputObjectSite
> ios
;
85 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IInputObjectSite
, &ios
));
86 if (FAILED_UNEXPECTEDLY(hr
))
89 return ios
->OnFocusChangeIS(punkObj
, fSetFocus
);
92 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::QueryStatus(const GUID
*pguidCmdGroup
, ULONG cCmds
,
93 OLECMD prgCmds
[], OLECMDTEXT
*pCmdText
)
98 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::Exec(const GUID
*pguidCmdGroup
, DWORD nCmdID
,
99 DWORD nCmdexecopt
, VARIANT
*pvaIn
, VARIANT
*pvaOut
)
101 if (IsEqualIID(*pguidCmdGroup
, CGID_MenuDeskBar
))
107 case 3: // load complete
109 case 4: // set font metrics
113 if (IsEqualIID(*pguidCmdGroup
, CGID_Explorer
))
116 else if (IsEqualIID(*pguidCmdGroup
, IID_IDeskBarClient
))
132 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::QueryService(REFGUID guidService
, REFIID riid
, void **ppvObject
)
134 if (IsEqualGUID(guidService
, SID_SMenuPopup
) ||
135 IsEqualGUID(guidService
, SID_SMenuBandParent
) ||
136 IsEqualGUID(guidService
, SID_STopLevelBrowser
))
138 return this->QueryInterface(riid
, ppvObject
);
141 if (IsEqualGUID(guidService
, SID_SMenuBandBottom
) ||
142 IsEqualGUID(guidService
, SID_SMenuBandBottomSelected
) ||
143 IsEqualGUID(guidService
, SID_SMenuBandChild
))
145 if (m_Client
== NULL
)
146 return E_NOINTERFACE
;
148 return IUnknown_QueryService(m_Client
, guidService
, riid
, ppvObject
);
153 return E_NOINTERFACE
;
155 return IUnknown_QueryService(m_Site
, guidService
, riid
, ppvObject
);
158 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::UIActivateIO(BOOL fActivate
, LPMSG lpMsg
)
160 return IUnknown_UIActivateIO(m_Client
, fActivate
, lpMsg
);
163 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::HasFocusIO()
165 CComPtr
<IInputObject
> io
;
167 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IInputObject
, &io
));
168 if (FAILED_UNEXPECTEDLY(hr
))
171 return io
->HasFocusIO();
174 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::TranslateAcceleratorIO(LPMSG lpMsg
)
176 CComPtr
<IInputObject
> io
;
178 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IInputObject
, &io
));
179 if (FAILED_UNEXPECTEDLY(hr
))
182 return io
->TranslateAcceleratorIO(lpMsg
);
185 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetClient(IUnknown
*punkClient
)
187 CComPtr
<IDeskBarClient
> pDeskBandClient
;
192 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &pDeskBandClient
));
193 if (FAILED_UNEXPECTEDLY(hr
))
196 pDeskBandClient
->SetDeskBarSite(NULL
);
198 pDeskBandClient
= NULL
;
202 if (punkClient
== NULL
)
210 hr
= punkClient
->QueryInterface(IID_PPV_ARG(IUnknown
, &m_Client
));
211 if (FAILED_UNEXPECTEDLY(hr
))
214 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &pDeskBandClient
));
215 if (FAILED_UNEXPECTEDLY(hr
))
218 hr
= pDeskBandClient
->SetDeskBarSite(static_cast<IDeskBar
*>(this));
219 if (FAILED_UNEXPECTEDLY(hr
))
222 return IUnknown_GetWindow(m_Client
, &m_ClientWindow
);
225 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetClient(IUnknown
**ppunkClient
)
227 if (ppunkClient
== NULL
)
233 return m_Client
->QueryInterface(IID_PPV_ARG(IUnknown
, ppunkClient
));
236 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::OnPosRectChangeDB(LPRECT prc
)
244 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetSite(IUnknown
*pUnkSite
)
246 // Windows closes the bar if this is called when the bar is shown
251 m_SubMenuParent
= NULL
;
257 IUnknown_QueryService(m_Site
, SID_SMenuPopup
, IID_PPV_ARG(IMenuPopup
, &m_SubMenuParent
));
268 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetSite(REFIID riid
, void **ppvSite
)
273 return m_Site
->QueryInterface(riid
, ppvSite
);
276 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::Popup(POINTL
*ppt
, RECTL
*prcExclude
, MP_POPUPFLAGS dwFlags
)
279 CComPtr
<IOleCommandTarget
> oct
;
280 CComPtr
<IInputObject
> io
;
281 CComPtr
<IDeskBand
> band
;
282 CComPtr
<IDeskBarClient
> dbc
;
287 hr
= IUnknown_QueryService(m_Client
, SID_SMenuBandChild
, IID_PPV_ARG(IOleCommandTarget
, &oct
));
288 if (FAILED_UNEXPECTEDLY(hr
))
291 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &dbc
));
292 if (FAILED_UNEXPECTEDLY(hr
))
295 // Windows calls this, but it appears to be unimplemented?
296 hr
= dbc
->SetModeDBC(1);
297 // Allow it to fail with E_NOTIMPL.
299 // No clue about the arg, using anything != 0
300 hr
= dbc
->UIActivateDBC(TRUE
);
301 if (FAILED_UNEXPECTEDLY(hr
))
305 hr
= dbc
->GetSize(0, &rc
);
306 if (FAILED_UNEXPECTEDLY(hr
))
311 const int CMD_EXEC_OPT
= 0;
313 hr
= IUnknown_QueryServiceExec(m_Client
, SID_SMenuBandChild
, &CLSID_MenuBand
, CMD
, CMD_EXEC_OPT
, NULL
, NULL
);
314 if (FAILED_UNEXPECTEDLY(hr
))
317 ::AdjustWindowRect(&rc
, ::GetWindowLong(m_hWnd
, GWL_STYLE
), FALSE
);
318 ::OffsetRect(&rc
, -rc
.left
, -rc
.top
);
320 if (m_Banner
!= NULL
)
323 ::GetObject(m_Banner
, sizeof(bm
), &bm
);
324 rc
.right
+= bm
.bmWidth
;
328 GetWindowRect(GetDesktopWindow(), &rcWorkArea
);
329 int waHeight
= rcWorkArea
.bottom
- rcWorkArea
.top
;
333 int cx
= rc
.right
- rc
.left
;
334 int cy
= rc
.bottom
- rc
.top
;
336 switch (dwFlags
& 0xFF000000)
340 y
= ppt
->y
- rc
.bottom
;
343 x
= ppt
->x
+ rc
.left
;
346 case MPPF_TOP
| MPPF_ALIGN_LEFT
:
347 x
= ppt
->x
- rc
.right
;
350 case MPPF_TOP
| MPPF_ALIGN_RIGHT
:
356 if (x
+ cx
> rcWorkArea
.right
)
358 // FIXME: Works, but it's oversimplified.
359 x
= prcExclude
->left
- cx
;
360 dwFlags
= (dwFlags
& (~MPPF_TOP
)) | MPPF_LEFT
;
363 if (y
< rcWorkArea
.top
)
373 if (y
+ cy
> rcWorkArea
.bottom
)
375 y
= rcWorkArea
.bottom
- cy
;
378 this->SetWindowPos(HWND_TOPMOST
, x
, y
, cx
, cy
, SWP_SHOWWINDOW
| SWP_NOACTIVATE
);
380 m_ShowFlags
= dwFlags
;
383 // HACK: The bar needs to be notified of the size AFTER it is shown.
384 // Quick & dirty way of getting it done.
386 _OnSize(WM_SIZE
, 0, 0, bHandled
);
388 UIActivateIO(TRUE
, NULL
);
390 if (dwFlags
& (MPPF_INITIALSELECT
| MPPF_FINALSELECT
))
392 const int CMD_SELECT
= 5;
393 int CMD_SELECT_OPTS
= dwFlags
& MPPF_INITIALSELECT
? 0 : -2;
394 IUnknown_QueryServiceExec(m_Client
, SID_SMenuBandChild
, &CLSID_MenuBand
, CMD_SELECT
, CMD_SELECT_OPTS
, NULL
, NULL
);
400 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetIconSize(THIS_ DWORD iIcon
)
405 // Unknown meaning (set flags? set icon size?)
407 const int CMD_EXEC_OPT
= iIcon
? 0 : 2; // seems to work
409 hr
= IUnknown_QueryServiceExec(m_Client
, SID_SMenuBandChild
, &CLSID_MenuBand
, CMD
, CMD_EXEC_OPT
, NULL
, NULL
);
410 if (FAILED_UNEXPECTEDLY(hr
))
414 _OnSize(WM_SIZE
, 0, 0, bHandled
);
419 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetIconSize(THIS_ DWORD
* piIcon
)
422 *piIcon
= m_IconSize
;
426 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetBitmap(THIS_ HBITMAP hBitmap
)
431 _OnSize(WM_SIZE
, 0, 0, bHandled
);
436 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetBitmap(THIS_ HBITMAP
* phBitmap
)
439 *phBitmap
= m_Banner
;
443 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetSubMenu(IMenuPopup
*pmp
, BOOL fSet
)
445 // Called by the MenuBand to assign itself as the logical child of the DeskBar
449 m_SubMenuChild
= pmp
;
455 if (pmp
== m_SubMenuChild
)
457 m_SubMenuChild
= NULL
;
464 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::OnSelect(DWORD dwSelectType
)
466 CComPtr
<IDeskBar
> safeThis
= this;
468 /* As far as I can tell, the submenu hierarchy looks like this:
470 * The DeskBar's Child is the Band it contains.
471 * The DeskBar's Parent is the SID_SMenuPopup of the Site.
473 * The Band's Child is the IMenuPopup of the child submenu.
474 * The Band's Parent is the SID_SMenuPopup of the Site (the DeskBar).
476 * When the DeskBar receives a selection event:
477 * If it requires closing the window, it will notify the Child (Band) using CancelLevel.
478 * If it has to spread upwards (everything but CancelLevel), it will notify the Parent.
480 * When the Band receives a selection event, this is where it gets fuzzy:
481 * In which cases does it call the Parent? Probably not CancelLevel.
482 * In which cases does it call the Child?
483 * How does it react to calls?
487 switch (dwSelectType
)
490 case MPOS_FULLCANCEL
:
491 case MPOS_CANCELLEVEL
:
495 if (dwSelectType
== MPOS_CANCELLEVEL
)
498 case MPOS_SELECTLEFT
:
499 case MPOS_SELECTRIGHT
:
500 case MPOS_CHILDTRACKING
:
502 return m_SubMenuParent
->OnSelect(dwSelectType
);
509 HRESULT
CMenuDeskBar::_CloseBar()
511 CComPtr
<IDeskBar
> safeThis
= this;
512 CComPtr
<IDeskBarClient
> dbc
;
519 m_SubMenuParent
->SetSubMenu(this, FALSE
);
524 hr
= m_SubMenuChild
->OnSelect(MPOS_CANCELLEVEL
);
525 if (FAILED_UNEXPECTEDLY(hr
))
529 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &dbc
));
530 if (FAILED_UNEXPECTEDLY(hr
))
533 hr
= dbc
->UIActivateDBC(FALSE
);
534 if (FAILED_UNEXPECTEDLY(hr
))
537 SetWindowPos(NULL
, 0, 0, 0, 0, SWP_HIDEWINDOW
| SWP_NOACTIVATE
| SWP_NOMOVE
);
539 return UIActivateIO(FALSE
, NULL
);
542 BOOL
CMenuDeskBar::_IsSubMenuParent(HWND hwnd
)
544 CComPtr
<IMenuPopup
> popup
= m_SubMenuParent
;
549 CComPtr
<IOleWindow
> window
;
551 hr
= popup
->QueryInterface(IID_PPV_ARG(IOleWindow
, &window
));
552 if (FAILED_UNEXPECTEDLY(hr
))
557 hr
= window
->GetWindow(&parent
);
558 if (SUCCEEDED(hr
) && hwnd
== parent
)
562 hr
= IUnknown_GetSite(window
, IID_PPV_ARG(IMenuPopup
, &popup
));
570 LRESULT
CMenuDeskBar::_OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
578 if (m_Banner
!= NULL
)
581 ::GetObject(m_Banner
, sizeof(bm
), &bm
);
582 rc
.left
+= bm
.bmWidth
;
585 ::SetWindowPos(m_ClientWindow
, NULL
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, 0);
591 LRESULT
CMenuDeskBar::_OnNotify(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
596 CComPtr
<IWinEventHandler
> winEventHandler
;
597 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IWinEventHandler
, &winEventHandler
));
598 if (FAILED_UNEXPECTEDLY(hr
))
604 hr
= winEventHandler
->OnWinEvent(NULL
, uMsg
, wParam
, lParam
, &result
);
605 if (FAILED_UNEXPECTEDLY(hr
))
613 LRESULT
CMenuDeskBar::_OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
617 if (m_Banner
&& !m_IconSize
)
621 HDC hdc
= BeginPaint(&ps
);
623 HDC hdcMem
= ::CreateCompatibleDC(hdc
);
624 HGDIOBJ hbmOld
= ::SelectObject(hdcMem
, m_Banner
);
626 ::GetObject(m_Banner
, sizeof(bm
), &bm
);
629 if (!GetClientRect(&rc
))
630 WARN("GetClientRect failed\n");
632 const int bx
= bm
.bmWidth
;
633 const int by
= bm
.bmHeight
;
634 const int cy
= rc
.bottom
;
636 TRACE("Painting banner: %d by %d\n", bm
.bmWidth
, bm
.bmHeight
);
638 if (!::StretchBlt(hdc
, 0, 0, bx
, cy
- by
, hdcMem
, 0, 0, bx
, 1, SRCCOPY
))
639 WARN("StretchBlt failed\n");
641 if (!::BitBlt(hdc
, 0, cy
- by
, bx
, by
, hdcMem
, 0, 0, SRCCOPY
))
642 WARN("BitBlt failed\n");
644 ::SelectObject(hdcMem
, hbmOld
);
653 LRESULT
CMenuDeskBar::_OnActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
655 // BUG in ReactOS: WM_ACTIVATE/WA_INACTIVE makes no sense with lParam==hWnd
656 if (LOWORD(wParam
) != 0 || reinterpret_cast<HWND
>(lParam
) == m_hWnd
)
661 // HACK! I just want it to work !!!
662 CComPtr
<IDeskBar
> db
;
663 HRESULT hr
= IUnknown_QueryService(m_Client
, SID_SMenuBandChild
, IID_PPV_ARG(IDeskBar
, &db
));
664 if (FAILED_UNEXPECTEDLY(hr
))
667 CComPtr
<IUnknown
> punk
;
669 hr
= db
->GetClient(&punk
);
670 if (FAILED_UNEXPECTEDLY(hr
))
673 if (!punk
&& m_Shown
)
675 if (!_IsSubMenuParent(reinterpret_cast<HWND
>(lParam
)))
677 OnSelect(MPOS_FULLCANCEL
);
684 LRESULT
CMenuDeskBar::_OnMouseActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
686 return MA_NOACTIVATE
;
689 LRESULT
CMenuDeskBar::_OnAppActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
691 if (wParam
== 0 && m_Shown
)
693 OnSelect(MPOS_FULLCANCEL
);