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
)
35 CMenuDeskBar
* deskbar
= new CComObject
<CMenuDeskBar
>();
40 HRESULT hr
= deskbar
->QueryInterface(riid
, ppv
);
42 if (FAILED_UNEXPECTEDLY(hr
))
48 CMenuDeskBar::CMenuDeskBar() :
55 CMenuDeskBar::~CMenuDeskBar()
59 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::Initialize(THIS
)
64 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetWindow(HWND
*lphwnd
)
72 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::ContextSensitiveHelp(BOOL fEnterMode
)
77 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::OnFocusChangeIS(IUnknown
*punkObj
, BOOL fSetFocus
)
79 CComPtr
<IInputObjectSite
> ios
;
81 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IInputObjectSite
, &ios
));
82 if (FAILED_UNEXPECTEDLY(hr
))
85 return ios
->OnFocusChangeIS(punkObj
, fSetFocus
);
88 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::QueryStatus(const GUID
*pguidCmdGroup
, ULONG cCmds
,
89 OLECMD prgCmds
[], OLECMDTEXT
*pCmdText
)
94 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::Exec(const GUID
*pguidCmdGroup
, DWORD nCmdID
,
95 DWORD nCmdexecopt
, VARIANT
*pvaIn
, VARIANT
*pvaOut
)
97 if (IsEqualIID(*pguidCmdGroup
, CGID_MenuDeskBar
))
103 case 3: // load complete
105 case 4: // set font metrics
109 if (IsEqualIID(*pguidCmdGroup
, CGID_Explorer
))
112 else if (IsEqualIID(*pguidCmdGroup
, IID_IDeskBarClient
))
128 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::QueryService(REFGUID guidService
, REFIID riid
, void **ppvObject
)
130 if (IsEqualGUID(guidService
, SID_SMenuPopup
) ||
131 IsEqualGUID(guidService
, SID_SMenuBandParent
) ||
132 IsEqualGUID(guidService
, SID_STopLevelBrowser
))
134 return this->QueryInterface(riid
, ppvObject
);
137 if (IsEqualGUID(guidService
, SID_SMenuBandBottom
) ||
138 IsEqualGUID(guidService
, SID_SMenuBandBottomSelected
) ||
139 IsEqualGUID(guidService
, SID_SMenuBandChild
))
141 if (m_Client
== NULL
)
142 return E_NOINTERFACE
;
144 return IUnknown_QueryService(m_Client
, guidService
, riid
, ppvObject
);
149 return E_NOINTERFACE
;
151 return IUnknown_QueryService(m_Site
, guidService
, riid
, ppvObject
);
154 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::UIActivateIO(BOOL fActivate
, LPMSG lpMsg
)
156 return IUnknown_UIActivateIO(m_Client
, fActivate
, lpMsg
);
159 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::HasFocusIO()
161 CComPtr
<IInputObject
> io
;
163 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IInputObject
, &io
));
164 if (FAILED_UNEXPECTEDLY(hr
))
167 return io
->HasFocusIO();
170 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::TranslateAcceleratorIO(LPMSG lpMsg
)
172 CComPtr
<IInputObject
> io
;
174 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IInputObject
, &io
));
175 if (FAILED_UNEXPECTEDLY(hr
))
178 return io
->TranslateAcceleratorIO(lpMsg
);
181 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetClient(IUnknown
*punkClient
)
183 CComPtr
<IDeskBarClient
> pDeskBandClient
;
188 if (punkClient
== NULL
)
196 hr
= punkClient
->QueryInterface(IID_PPV_ARG(IUnknown
, &m_Client
));
197 if (FAILED_UNEXPECTEDLY(hr
))
200 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &pDeskBandClient
));
201 if (FAILED_UNEXPECTEDLY(hr
))
204 hr
= pDeskBandClient
->SetDeskBarSite(static_cast<IDeskBar
*>(this));
205 if (FAILED_UNEXPECTEDLY(hr
))
208 return IUnknown_GetWindow(m_Client
, &m_ClientWindow
);
211 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetClient(IUnknown
**ppunkClient
)
213 if (ppunkClient
== NULL
)
219 return m_Client
->QueryInterface(IID_PPV_ARG(IUnknown
, ppunkClient
));
222 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::OnPosRectChangeDB(LPRECT prc
)
230 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetSite(IUnknown
*pUnkSite
)
232 // Windows closes the bar if this is called when the bar is shown
239 IUnknown_QueryService(m_Site
, SID_SMenuPopup
, IID_PPV_ARG(IMenuPopup
, &m_SubMenuParent
));
244 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetSite(REFIID riid
, void **ppvSite
)
249 return m_Site
->QueryInterface(riid
, ppvSite
);
252 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::Popup(POINTL
*ppt
, RECTL
*prcExclude
, MP_POPUPFLAGS dwFlags
)
255 CComPtr
<IOleCommandTarget
> oct
;
256 CComPtr
<IInputObject
> io
;
257 CComPtr
<IDeskBand
> band
;
258 CComPtr
<IDeskBarClient
> dbc
;
263 hr
= IUnknown_QueryService(m_Client
, SID_SMenuBandChild
, IID_PPV_ARG(IOleCommandTarget
, &oct
));
264 if (FAILED_UNEXPECTEDLY(hr
))
267 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &dbc
));
268 if (FAILED_UNEXPECTEDLY(hr
))
271 // Windows calls this, but it appears to be unimplemented?
272 hr
= dbc
->SetModeDBC(1);
273 // Allow it to fail with E_NOTIMPL.
275 // No clue about the arg, using anything != 0
276 hr
= dbc
->UIActivateDBC(TRUE
);
277 if (FAILED_UNEXPECTEDLY(hr
))
281 hr
= dbc
->GetSize(0, &rc
);
282 if (FAILED_UNEXPECTEDLY(hr
))
287 const int CMD_EXEC_OPT
= 0;
289 hr
= IUnknown_QueryServiceExec(m_Client
, SID_SMenuBandChild
, &CLSID_MenuBand
, CMD
, CMD_EXEC_OPT
, NULL
, NULL
);
290 if (FAILED_UNEXPECTEDLY(hr
))
293 ::AdjustWindowRect(&rc
, ::GetWindowLong(m_hWnd
, GWL_STYLE
), FALSE
);
294 ::OffsetRect(&rc
, -rc
.left
, -rc
.top
);
296 if (m_Banner
!= NULL
)
299 ::GetObject(m_Banner
, sizeof(bm
), &bm
);
300 rc
.right
+= bm
.bmWidth
;
304 GetWindowRect(GetDesktopWindow(), &rcWorkArea
);
305 int waHeight
= rcWorkArea
.bottom
- rcWorkArea
.top
;
309 int cx
= rc
.right
- rc
.left
;
310 int cy
= rc
.bottom
- rc
.top
;
312 switch (dwFlags
& 0xFF000000)
316 y
= ppt
->y
- rc
.bottom
;
319 x
= ppt
->x
+ rc
.left
;
322 case MPPF_TOP
| MPPF_ALIGN_LEFT
:
323 x
= ppt
->x
- rc
.right
;
326 case MPPF_TOP
| MPPF_ALIGN_RIGHT
:
332 if (x
+ cx
> rcWorkArea
.right
)
334 // FIXME: Works, but it's oversimplified.
335 x
= prcExclude
->left
- cx
;
336 dwFlags
= (dwFlags
& (~MPPF_TOP
)) | MPPF_LEFT
;
339 if (y
< rcWorkArea
.top
)
349 if (y
+ cy
> rcWorkArea
.bottom
)
351 y
= rcWorkArea
.bottom
- cy
;
354 this->SetWindowPos(HWND_TOPMOST
, x
, y
, cx
, cy
, SWP_SHOWWINDOW
| SWP_NOACTIVATE
);
356 m_ShowFlags
= dwFlags
;
359 // HACK: The bar needs to be notified of the size AFTER it is shown.
360 // Quick & dirty way of getting it done.
362 _OnSize(WM_SIZE
, 0, 0, bHandled
);
364 UIActivateIO(TRUE
, NULL
);
369 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetIconSize(THIS_ DWORD iIcon
)
374 // Unknown meaning (set flags? set icon size?)
376 const int CMD_EXEC_OPT
= iIcon
? 0 : 2; // seems to work
378 hr
= IUnknown_QueryServiceExec(m_Client
, SID_SMenuBandChild
, &CLSID_MenuBand
, CMD
, CMD_EXEC_OPT
, NULL
, NULL
);
379 if (FAILED_UNEXPECTEDLY(hr
))
383 _OnSize(WM_SIZE
, 0, 0, bHandled
);
388 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetIconSize(THIS_ DWORD
* piIcon
)
391 *piIcon
= m_IconSize
;
395 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetBitmap(THIS_ HBITMAP hBitmap
)
400 _OnSize(WM_SIZE
, 0, 0, bHandled
);
405 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetBitmap(THIS_ HBITMAP
* phBitmap
)
408 *phBitmap
= m_Banner
;
412 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetSubMenu(IMenuPopup
*pmp
, BOOL fSet
)
414 // Called by the MenuBand to assign itself as the logical child of the DeskBar
418 m_SubMenuChild
= pmp
;
424 if (pmp
== m_SubMenuChild
)
426 m_SubMenuChild
= NULL
;
433 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::OnSelect(DWORD dwSelectType
)
435 /* As far as I can tell, the submenu hierarchy looks like this:
437 * The DeskBar's Child is the Band it contains.
438 * The DeskBar's Parent is the SID_SMenuPopup of the Site.
440 * The Band's Child is the IMenuPopup of the child submenu.
441 * The Band's Parent is the SID_SMenuPopup of the Site (the DeskBar).
443 * When the DeskBar receives a selection event:
444 * If it requires closing the window, it will notify the Child (Band) using CancelLevel.
445 * If it has to spread upwards (everything but CancelLevel), it will notify the Parent.
447 * When the Band receives a selection event, this is where it gets fuzzy:
448 * In which cases does it call the Parent? Probably not CancelLevel.
449 * In which cases does it call the Child?
450 * How does it react to calls?
454 switch (dwSelectType
)
457 case MPOS_FULLCANCEL
:
458 case MPOS_CANCELLEVEL
:
462 if (dwSelectType
== MPOS_CANCELLEVEL
)
465 case MPOS_SELECTLEFT
:
466 case MPOS_SELECTRIGHT
:
467 case MPOS_CHILDTRACKING
:
469 return m_SubMenuParent
->OnSelect(dwSelectType
);
476 HRESULT
CMenuDeskBar::_CloseBar()
478 CComPtr
<IDeskBarClient
> dbc
;
485 hr
= m_SubMenuChild
->OnSelect(MPOS_CANCELLEVEL
);
486 if (FAILED_UNEXPECTEDLY(hr
))
490 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &dbc
));
491 if (FAILED_UNEXPECTEDLY(hr
))
494 hr
= dbc
->UIActivateDBC(FALSE
);
495 if (FAILED_UNEXPECTEDLY(hr
))
498 SetWindowPos(NULL
, 0, 0, 0, 0, SWP_HIDEWINDOW
| SWP_NOACTIVATE
| SWP_NOMOVE
);
500 return UIActivateIO(FALSE
, NULL
);
503 BOOL
CMenuDeskBar::_IsSubMenuParent(HWND hwnd
)
505 CComPtr
<IMenuPopup
> popup
= m_SubMenuParent
;
510 CComPtr
<IOleWindow
> window
;
512 hr
= popup
->QueryInterface(IID_PPV_ARG(IOleWindow
, &window
));
513 if (FAILED_UNEXPECTEDLY(hr
))
518 hr
= window
->GetWindow(&parent
);
519 if (SUCCEEDED(hr
) && hwnd
== parent
)
523 hr
= IUnknown_GetSite(window
, IID_PPV_ARG(IMenuPopup
, &popup
));
531 LRESULT
CMenuDeskBar::_OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
539 if (m_Banner
!= NULL
)
542 ::GetObject(m_Banner
, sizeof(bm
), &bm
);
543 rc
.left
+= bm
.bmWidth
;
546 ::SetWindowPos(m_ClientWindow
, NULL
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, 0);
552 LRESULT
CMenuDeskBar::_OnNotify(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
557 CComPtr
<IWinEventHandler
> winEventHandler
;
558 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IWinEventHandler
, &winEventHandler
));
559 if (FAILED_UNEXPECTEDLY(hr
))
565 hr
= winEventHandler
->OnWinEvent(NULL
, uMsg
, wParam
, lParam
, &result
);
566 if (FAILED_UNEXPECTEDLY(hr
))
574 LRESULT
CMenuDeskBar::_OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
578 if (m_Banner
&& !m_IconSize
)
582 HDC hdc
= BeginPaint(&ps
);
584 HDC hdcMem
= ::CreateCompatibleDC(hdc
);
585 HGDIOBJ hbmOld
= ::SelectObject(hdcMem
, m_Banner
);
587 ::GetObject(m_Banner
, sizeof(bm
), &bm
);
590 if (!GetClientRect(&rc
))
591 WARN("GetClientRect failed\n");
593 const int bx
= bm
.bmWidth
;
594 const int by
= bm
.bmHeight
;
595 const int cy
= rc
.bottom
;
597 TRACE("Painting banner: %d by %d\n", bm
.bmWidth
, bm
.bmHeight
);
599 if (!::StretchBlt(hdc
, 0, 0, bx
, cy
- by
, hdcMem
, 0, 0, bx
, 1, SRCCOPY
))
600 WARN("StretchBlt failed\n");
602 if (!::BitBlt(hdc
, 0, cy
- by
, bx
, by
, hdcMem
, 0, 0, SRCCOPY
))
603 WARN("BitBlt failed\n");
605 ::SelectObject(hdcMem
, hbmOld
);
614 LRESULT
CMenuDeskBar::_OnActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
616 // BUG in ReactOS: WM_ACTIVATE/WA_INACTIVE makes no sense with lParam==hWnd
617 if (LOWORD(wParam
) != 0 || reinterpret_cast<HWND
>(lParam
) == m_hWnd
)
622 // HACK! I just want it to work !!!
623 CComPtr
<IDeskBar
> db
;
624 HRESULT hr
= IUnknown_QueryService(m_Client
, SID_SMenuBandChild
, IID_PPV_ARG(IDeskBar
, &db
));
625 if (FAILED_UNEXPECTEDLY(hr
))
628 CComPtr
<IUnknown
> punk
;
630 hr
= db
->GetClient(&punk
);
631 if (FAILED_UNEXPECTEDLY(hr
))
634 if (!punk
&& m_Shown
)
636 if (!_IsSubMenuParent(reinterpret_cast<HWND
>(lParam
)))
638 OnSelect(MPOS_FULLCANCEL
);
645 LRESULT
CMenuDeskBar::_OnMouseActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
647 return MA_NOACTIVATE
;
650 LRESULT
CMenuDeskBar::_OnAppActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
652 if (wParam
== 0 && m_Shown
)
654 OnSelect(MPOS_FULLCANCEL
);