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
;
306 GetWindowRect(GetDesktopWindow(), &rcWorkArea
);
307 int waHeight
= rcWorkArea
.bottom
- rcWorkArea
.top
;
309 switch (dwFlags
& 0xFF000000)
313 cx
= rc
.right
- rc
.left
;
314 y
= ppt
->y
- rc
.bottom
;
315 cy
= rc
.bottom
- rc
.top
;
318 x
= ppt
->x
+ rc
.left
;
319 cx
= rc
.right
- rc
.left
;
321 cy
= rc
.bottom
- rc
.top
;
323 case MPPF_TOP
| MPPF_ALIGN_LEFT
:
324 x
= ppt
->x
- rc
.right
;
325 cx
= rc
.right
- rc
.left
;
327 cy
= rc
.bottom
- rc
.top
;
329 case MPPF_TOP
| MPPF_ALIGN_RIGHT
:
331 cx
= rc
.right
- rc
.left
;
333 cy
= rc
.bottom
- rc
.top
;
337 if (x
+ cx
> rcWorkArea
.right
)
339 // FIXME: Works, but it's oversimplified.
340 x
= prcExclude
->left
- cx
;
341 dwFlags
= (dwFlags
& (~MPPF_TOP
)) | MPPF_LEFT
;
344 if (y
< rcWorkArea
.top
)
354 if (y
+ cy
> rcWorkArea
.bottom
)
356 y
= rcWorkArea
.bottom
- cy
;
359 this->SetWindowPos(HWND_TOPMOST
, x
, y
, cx
, cy
, SWP_SHOWWINDOW
| SWP_NOACTIVATE
);
361 m_ShowFlags
= dwFlags
;
364 // HACK: The bar needs to be notified of the size AFTER it is shown.
365 // Quick & dirty way of getting it done.
367 _OnSize(WM_SIZE
, 0, 0, bHandled
);
369 UIActivateIO(TRUE
, NULL
);
374 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetIconSize(THIS_ DWORD iIcon
)
379 // Unknown meaning (set flags? set icon size?)
381 const int CMD_EXEC_OPT
= iIcon
? 0 : 2; // seems to work
383 hr
= IUnknown_QueryServiceExec(m_Client
, SID_SMenuBandChild
, &CLSID_MenuBand
, CMD
, CMD_EXEC_OPT
, NULL
, NULL
);
384 if (FAILED_UNEXPECTEDLY(hr
))
388 _OnSize(WM_SIZE
, 0, 0, bHandled
);
393 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetIconSize(THIS_ DWORD
* piIcon
)
396 *piIcon
= m_IconSize
;
400 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetBitmap(THIS_ HBITMAP hBitmap
)
405 _OnSize(WM_SIZE
, 0, 0, bHandled
);
410 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetBitmap(THIS_ HBITMAP
* phBitmap
)
413 *phBitmap
= m_Banner
;
417 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetSubMenu(IMenuPopup
*pmp
, BOOL fSet
)
419 // Called by the MenuBand to assign itself as the logical child of the DeskBar
423 m_SubMenuChild
= pmp
;
429 if (pmp
== m_SubMenuChild
)
431 m_SubMenuChild
= NULL
;
438 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::OnSelect(DWORD dwSelectType
)
440 /* As far as I can tell, the submenu hierarchy looks like this:
442 * The DeskBar's Child is the Band it contains.
443 * The DeskBar's Parent is the SID_SMenuPopup of the Site.
445 * The Band's Child is the IMenuPopup of the child submenu.
446 * The Band's Parent is the SID_SMenuPopup of the Site (the DeskBar).
448 * When the DeskBar receives a selection event:
449 * If it requires closing the window, it will notify the Child (Band) using CancelLevel.
450 * If it has to spread upwards (everything but CancelLevel), it will notify the Parent.
452 * When the Band receives a selection event, this is where it gets fuzzy:
453 * In which cases does it call the Parent? Probably not CancelLevel.
454 * In which cases does it call the Child?
455 * How does it react to calls?
459 switch (dwSelectType
)
462 case MPOS_FULLCANCEL
:
463 case MPOS_CANCELLEVEL
:
467 if (dwSelectType
== MPOS_CANCELLEVEL
)
470 case MPOS_SELECTLEFT
:
471 case MPOS_SELECTRIGHT
:
472 case MPOS_CHILDTRACKING
:
474 return m_SubMenuParent
->OnSelect(dwSelectType
);
481 HRESULT
CMenuDeskBar::_CloseBar()
483 CComPtr
<IDeskBarClient
> dbc
;
490 hr
= m_SubMenuChild
->OnSelect(MPOS_CANCELLEVEL
);
491 if (FAILED_UNEXPECTEDLY(hr
))
495 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &dbc
));
496 if (FAILED_UNEXPECTEDLY(hr
))
499 hr
= dbc
->UIActivateDBC(FALSE
);
500 if (FAILED_UNEXPECTEDLY(hr
))
503 SetWindowPos(NULL
, 0, 0, 0, 0, SWP_HIDEWINDOW
| SWP_NOACTIVATE
| SWP_NOMOVE
);
505 return UIActivateIO(FALSE
, NULL
);
508 BOOL
CMenuDeskBar::_IsSubMenuParent(HWND hwnd
)
510 CComPtr
<IMenuPopup
> popup
= m_SubMenuParent
;
515 CComPtr
<IOleWindow
> window
;
517 hr
= popup
->QueryInterface(IID_PPV_ARG(IOleWindow
, &window
));
518 if (FAILED_UNEXPECTEDLY(hr
))
523 hr
= window
->GetWindow(&parent
);
524 if (SUCCEEDED(hr
) && hwnd
== parent
)
528 hr
= IUnknown_GetSite(window
, IID_PPV_ARG(IMenuPopup
, &popup
));
536 LRESULT
CMenuDeskBar::_OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
544 if (m_Banner
!= NULL
)
547 ::GetObject(m_Banner
, sizeof(bm
), &bm
);
548 rc
.left
+= bm
.bmWidth
;
551 ::SetWindowPos(m_ClientWindow
, NULL
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, 0);
557 LRESULT
CMenuDeskBar::_OnNotify(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
562 CComPtr
<IWinEventHandler
> winEventHandler
;
563 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IWinEventHandler
, &winEventHandler
));
564 if (FAILED_UNEXPECTEDLY(hr
))
570 hr
= winEventHandler
->OnWinEvent(NULL
, uMsg
, wParam
, lParam
, &result
);
571 if (FAILED_UNEXPECTEDLY(hr
))
579 LRESULT
CMenuDeskBar::_OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
583 if (m_Banner
&& !m_IconSize
)
587 HDC hdc
= BeginPaint(&ps
);
589 HDC hdcMem
= ::CreateCompatibleDC(hdc
);
590 HGDIOBJ hbmOld
= ::SelectObject(hdcMem
, m_Banner
);
592 ::GetObject(m_Banner
, sizeof(bm
), &bm
);
595 if (!GetClientRect(&rc
))
596 WARN("GetClientRect failed\n");
598 const int bx
= bm
.bmWidth
;
599 const int by
= bm
.bmHeight
;
600 const int cy
= rc
.bottom
;
602 TRACE("Painting banner: %d by %d\n", bm
.bmWidth
, bm
.bmHeight
);
604 if (!::StretchBlt(hdc
, 0, 0, bx
, cy
- by
, hdcMem
, 0, 0, bx
, 1, SRCCOPY
))
605 WARN("StretchBlt failed\n");
607 if (!::BitBlt(hdc
, 0, cy
- by
, bx
, by
, hdcMem
, 0, 0, SRCCOPY
))
608 WARN("BitBlt failed\n");
610 ::SelectObject(hdcMem
, hbmOld
);
619 LRESULT
CMenuDeskBar::_OnActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
621 // BUG in ReactOS: WM_ACTIVATE/WA_INACTIVE makes no sense with lParam==hWnd
622 if (LOWORD(wParam
) != 0 || reinterpret_cast<HWND
>(lParam
) == m_hWnd
)
627 // HACK! I just want it to work !!!
628 CComPtr
<IDeskBar
> db
;
629 HRESULT hr
= IUnknown_QueryService(m_Client
, SID_SMenuBandChild
, IID_PPV_ARG(IDeskBar
, &db
));
630 if (FAILED_UNEXPECTEDLY(hr
))
633 CComPtr
<IUnknown
> punk
;
635 hr
= db
->GetClient(&punk
);
636 if (FAILED_UNEXPECTEDLY(hr
))
639 if (!punk
&& m_Shown
)
641 if (!_IsSubMenuParent(reinterpret_cast<HWND
>(lParam
)))
643 OnSelect(MPOS_FULLCANCEL
);
650 LRESULT
CMenuDeskBar::_OnMouseActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
652 return MA_NOACTIVATE
;
655 LRESULT
CMenuDeskBar::_OnAppActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
657 if (wParam
== 0 && m_Shown
)
659 OnSelect(MPOS_FULLCANCEL
);