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
)
34 #if USE_SYSTEM_MENUDESKBAR
35 hr
= CoCreateInstance(CLSID_MenuDeskBar
,
42 CMenuDeskBar
* deskbar
= new CComObject
<CMenuDeskBar
>();
47 hr
= deskbar
->QueryInterface(riid
, ppv
);
49 if (FAILED_UNEXPECTEDLY(hr
))
56 CMenuDeskBar::CMenuDeskBar() :
63 CMenuDeskBar::~CMenuDeskBar()
67 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::Initialize(THIS
)
72 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetWindow(HWND
*lphwnd
)
80 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::ContextSensitiveHelp(BOOL fEnterMode
)
85 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::OnFocusChangeIS(IUnknown
*punkObj
, BOOL fSetFocus
)
87 CComPtr
<IInputObjectSite
> ios
;
89 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IInputObjectSite
, &ios
));
90 if (FAILED_UNEXPECTEDLY(hr
))
93 return ios
->OnFocusChangeIS(punkObj
, fSetFocus
);
96 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::QueryStatus(const GUID
*pguidCmdGroup
, ULONG cCmds
,
97 OLECMD prgCmds
[], OLECMDTEXT
*pCmdText
)
102 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::Exec(const GUID
*pguidCmdGroup
, DWORD nCmdID
,
103 DWORD nCmdexecopt
, VARIANT
*pvaIn
, VARIANT
*pvaOut
)
105 if (IsEqualIID(*pguidCmdGroup
, CGID_MenuDeskBar
))
111 case 3: // load complete
113 case 4: // set font metrics
117 if (IsEqualIID(*pguidCmdGroup
, CGID_Explorer
))
120 else if (IsEqualIID(*pguidCmdGroup
, IID_IDeskBarClient
))
136 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::QueryService(REFGUID guidService
, REFIID riid
, void **ppvObject
)
138 if (IsEqualGUID(guidService
, SID_SMenuPopup
) ||
139 IsEqualGUID(guidService
, SID_SMenuBandParent
) ||
140 IsEqualGUID(guidService
, SID_STopLevelBrowser
))
142 return this->QueryInterface(riid
, ppvObject
);
145 if (IsEqualGUID(guidService
, SID_SMenuBandBottom
) ||
146 IsEqualGUID(guidService
, SID_SMenuBandBottomSelected
) ||
147 IsEqualGUID(guidService
, SID_SMenuBandChild
))
149 if (m_Client
== NULL
)
150 return E_NOINTERFACE
;
152 return IUnknown_QueryService(m_Client
, guidService
, riid
, ppvObject
);
157 return E_NOINTERFACE
;
159 return IUnknown_QueryService(m_Site
, guidService
, riid
, ppvObject
);
162 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::UIActivateIO(BOOL fActivate
, LPMSG lpMsg
)
164 return IUnknown_UIActivateIO(m_Client
, fActivate
, lpMsg
);
167 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::HasFocusIO()
169 CComPtr
<IInputObject
> io
;
171 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IInputObject
, &io
));
172 if (FAILED_UNEXPECTEDLY(hr
))
175 return io
->HasFocusIO();
178 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::TranslateAcceleratorIO(LPMSG lpMsg
)
180 CComPtr
<IInputObject
> io
;
182 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IInputObject
, &io
));
183 if (FAILED_UNEXPECTEDLY(hr
))
186 return io
->TranslateAcceleratorIO(lpMsg
);
189 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetClient(IUnknown
*punkClient
)
191 CComPtr
<IDeskBarClient
> pDeskBandClient
;
196 if (punkClient
== NULL
)
204 hr
= punkClient
->QueryInterface(IID_PPV_ARG(IUnknown
, &m_Client
));
205 if (FAILED_UNEXPECTEDLY(hr
))
208 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &pDeskBandClient
));
209 if (FAILED_UNEXPECTEDLY(hr
))
212 hr
= pDeskBandClient
->SetDeskBarSite(static_cast<IDeskBar
*>(this));
213 if (FAILED_UNEXPECTEDLY(hr
))
216 return IUnknown_GetWindow(m_Client
, &m_ClientWindow
);
219 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetClient(IUnknown
**ppunkClient
)
221 if (ppunkClient
== NULL
)
227 return m_Client
->QueryInterface(IID_PPV_ARG(IUnknown
, ppunkClient
));
230 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::OnPosRectChangeDB(LPRECT prc
)
238 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetSite(IUnknown
*pUnkSite
)
240 // Windows closes the bar if this is called when the bar is shown
247 IUnknown_QueryService(m_Site
, SID_SMenuPopup
, IID_PPV_ARG(IMenuPopup
, &m_SubMenuParent
));
252 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetSite(REFIID riid
, void **ppvSite
)
257 return m_Site
->QueryInterface(riid
, ppvSite
);
260 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::Popup(POINTL
*ppt
, RECTL
*prcExclude
, MP_POPUPFLAGS dwFlags
)
263 CComPtr
<IOleCommandTarget
> oct
;
264 CComPtr
<IInputObject
> io
;
265 CComPtr
<IDeskBand
> band
;
266 CComPtr
<IDeskBarClient
> dbc
;
271 hr
= IUnknown_QueryService(m_Client
, SID_SMenuBandChild
, IID_PPV_ARG(IOleCommandTarget
, &oct
));
272 if (FAILED_UNEXPECTEDLY(hr
))
275 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &dbc
));
276 if (FAILED_UNEXPECTEDLY(hr
))
279 // Windows calls this, but it appears to be unimplemented?
280 hr
= dbc
->SetModeDBC(1);
281 // Allow it to fail with E_NOTIMPL.
283 // No clue about the arg, using anything != 0
284 hr
= dbc
->UIActivateDBC(TRUE
);
285 if (FAILED_UNEXPECTEDLY(hr
))
289 hr
= dbc
->GetSize(0, &rc
);
290 if (FAILED_UNEXPECTEDLY(hr
))
295 const int CMD_EXEC_OPT
= 0;
297 hr
= IUnknown_QueryServiceExec(m_Client
, SID_SMenuBandChild
, &CLSID_MenuBand
, CMD
, CMD_EXEC_OPT
, NULL
, NULL
);
298 if (FAILED_UNEXPECTEDLY(hr
))
301 ::AdjustWindowRect(&rc
, ::GetWindowLong(m_hWnd
, GWL_STYLE
), FALSE
);
302 ::OffsetRect(&rc
, -rc
.left
, -rc
.top
);
304 if (m_Banner
!= NULL
)
307 ::GetObject(m_Banner
, sizeof(bm
), &bm
);
308 rc
.right
+= bm
.bmWidth
;
312 GetWindowRect(GetDesktopWindow(), &rcWorkArea
);
313 int waHeight
= rcWorkArea
.bottom
- rcWorkArea
.top
;
317 int cx
= rc
.right
- rc
.left
;
318 int cy
= rc
.bottom
- rc
.top
;
320 switch (dwFlags
& 0xFF000000)
324 y
= ppt
->y
- rc
.bottom
;
327 x
= ppt
->x
+ rc
.left
;
330 case MPPF_TOP
| MPPF_ALIGN_LEFT
:
331 x
= ppt
->x
- rc
.right
;
334 case MPPF_TOP
| MPPF_ALIGN_RIGHT
:
340 if (x
+ cx
> rcWorkArea
.right
)
342 // FIXME: Works, but it's oversimplified.
343 x
= prcExclude
->left
- cx
;
344 dwFlags
= (dwFlags
& (~MPPF_TOP
)) | MPPF_LEFT
;
347 if (y
< rcWorkArea
.top
)
357 if (y
+ cy
> rcWorkArea
.bottom
)
359 y
= rcWorkArea
.bottom
- cy
;
362 this->SetWindowPos(HWND_TOPMOST
, x
, y
, cx
, cy
, SWP_SHOWWINDOW
| SWP_NOACTIVATE
);
364 m_ShowFlags
= dwFlags
;
367 // HACK: The bar needs to be notified of the size AFTER it is shown.
368 // Quick & dirty way of getting it done.
370 _OnSize(WM_SIZE
, 0, 0, bHandled
);
372 UIActivateIO(TRUE
, NULL
);
374 if (dwFlags
& (MPPF_INITIALSELECT
| MPPF_FINALSELECT
))
376 const int CMD_SELECT
= 5;
377 int CMD_SELECT_OPTS
= dwFlags
& MPPF_INITIALSELECT
? 0 : -2;
378 IUnknown_QueryServiceExec(m_Client
, SID_SMenuBandChild
, &CLSID_MenuBand
, CMD_SELECT
, CMD_SELECT_OPTS
, NULL
, NULL
);
384 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetIconSize(THIS_ DWORD iIcon
)
389 // Unknown meaning (set flags? set icon size?)
391 const int CMD_EXEC_OPT
= iIcon
? 0 : 2; // seems to work
393 hr
= IUnknown_QueryServiceExec(m_Client
, SID_SMenuBandChild
, &CLSID_MenuBand
, CMD
, CMD_EXEC_OPT
, NULL
, NULL
);
394 if (FAILED_UNEXPECTEDLY(hr
))
398 _OnSize(WM_SIZE
, 0, 0, bHandled
);
403 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetIconSize(THIS_ DWORD
* piIcon
)
406 *piIcon
= m_IconSize
;
410 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetBitmap(THIS_ HBITMAP hBitmap
)
415 _OnSize(WM_SIZE
, 0, 0, bHandled
);
420 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::GetBitmap(THIS_ HBITMAP
* phBitmap
)
423 *phBitmap
= m_Banner
;
427 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::SetSubMenu(IMenuPopup
*pmp
, BOOL fSet
)
429 // Called by the MenuBand to assign itself as the logical child of the DeskBar
433 m_SubMenuChild
= pmp
;
439 if (pmp
== m_SubMenuChild
)
441 m_SubMenuChild
= NULL
;
448 HRESULT STDMETHODCALLTYPE
CMenuDeskBar::OnSelect(DWORD dwSelectType
)
450 /* As far as I can tell, the submenu hierarchy looks like this:
452 * The DeskBar's Child is the Band it contains.
453 * The DeskBar's Parent is the SID_SMenuPopup of the Site.
455 * The Band's Child is the IMenuPopup of the child submenu.
456 * The Band's Parent is the SID_SMenuPopup of the Site (the DeskBar).
458 * When the DeskBar receives a selection event:
459 * If it requires closing the window, it will notify the Child (Band) using CancelLevel.
460 * If it has to spread upwards (everything but CancelLevel), it will notify the Parent.
462 * When the Band receives a selection event, this is where it gets fuzzy:
463 * In which cases does it call the Parent? Probably not CancelLevel.
464 * In which cases does it call the Child?
465 * How does it react to calls?
469 switch (dwSelectType
)
472 case MPOS_FULLCANCEL
:
473 case MPOS_CANCELLEVEL
:
477 if (dwSelectType
== MPOS_CANCELLEVEL
)
480 case MPOS_SELECTLEFT
:
481 case MPOS_SELECTRIGHT
:
482 case MPOS_CHILDTRACKING
:
484 return m_SubMenuParent
->OnSelect(dwSelectType
);
491 HRESULT
CMenuDeskBar::_CloseBar()
493 CComPtr
<IDeskBarClient
> dbc
;
500 hr
= m_SubMenuChild
->OnSelect(MPOS_CANCELLEVEL
);
501 if (FAILED_UNEXPECTEDLY(hr
))
505 hr
= m_Client
->QueryInterface(IID_PPV_ARG(IDeskBarClient
, &dbc
));
506 if (FAILED_UNEXPECTEDLY(hr
))
509 hr
= dbc
->UIActivateDBC(FALSE
);
510 if (FAILED_UNEXPECTEDLY(hr
))
513 SetWindowPos(NULL
, 0, 0, 0, 0, SWP_HIDEWINDOW
| SWP_NOACTIVATE
| SWP_NOMOVE
);
515 return UIActivateIO(FALSE
, NULL
);
518 BOOL
CMenuDeskBar::_IsSubMenuParent(HWND hwnd
)
520 CComPtr
<IMenuPopup
> popup
= m_SubMenuParent
;
525 CComPtr
<IOleWindow
> window
;
527 hr
= popup
->QueryInterface(IID_PPV_ARG(IOleWindow
, &window
));
528 if (FAILED_UNEXPECTEDLY(hr
))
533 hr
= window
->GetWindow(&parent
);
534 if (SUCCEEDED(hr
) && hwnd
== parent
)
538 hr
= IUnknown_GetSite(window
, IID_PPV_ARG(IMenuPopup
, &popup
));
546 LRESULT
CMenuDeskBar::_OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
554 if (m_Banner
!= NULL
)
557 ::GetObject(m_Banner
, sizeof(bm
), &bm
);
558 rc
.left
+= bm
.bmWidth
;
561 ::SetWindowPos(m_ClientWindow
, NULL
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, 0);
567 LRESULT
CMenuDeskBar::_OnNotify(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
572 CComPtr
<IWinEventHandler
> winEventHandler
;
573 HRESULT hr
= m_Client
->QueryInterface(IID_PPV_ARG(IWinEventHandler
, &winEventHandler
));
574 if (FAILED_UNEXPECTEDLY(hr
))
580 hr
= winEventHandler
->OnWinEvent(NULL
, uMsg
, wParam
, lParam
, &result
);
581 if (FAILED_UNEXPECTEDLY(hr
))
589 LRESULT
CMenuDeskBar::_OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
593 if (m_Banner
&& !m_IconSize
)
597 HDC hdc
= BeginPaint(&ps
);
599 HDC hdcMem
= ::CreateCompatibleDC(hdc
);
600 HGDIOBJ hbmOld
= ::SelectObject(hdcMem
, m_Banner
);
602 ::GetObject(m_Banner
, sizeof(bm
), &bm
);
605 if (!GetClientRect(&rc
))
606 WARN("GetClientRect failed\n");
608 const int bx
= bm
.bmWidth
;
609 const int by
= bm
.bmHeight
;
610 const int cy
= rc
.bottom
;
612 TRACE("Painting banner: %d by %d\n", bm
.bmWidth
, bm
.bmHeight
);
614 if (!::StretchBlt(hdc
, 0, 0, bx
, cy
- by
, hdcMem
, 0, 0, bx
, 1, SRCCOPY
))
615 WARN("StretchBlt failed\n");
617 if (!::BitBlt(hdc
, 0, cy
- by
, bx
, by
, hdcMem
, 0, 0, SRCCOPY
))
618 WARN("BitBlt failed\n");
620 ::SelectObject(hdcMem
, hbmOld
);
629 LRESULT
CMenuDeskBar::_OnActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
631 // BUG in ReactOS: WM_ACTIVATE/WA_INACTIVE makes no sense with lParam==hWnd
632 if (LOWORD(wParam
) != 0 || reinterpret_cast<HWND
>(lParam
) == m_hWnd
)
637 // HACK! I just want it to work !!!
638 CComPtr
<IDeskBar
> db
;
639 HRESULT hr
= IUnknown_QueryService(m_Client
, SID_SMenuBandChild
, IID_PPV_ARG(IDeskBar
, &db
));
640 if (FAILED_UNEXPECTEDLY(hr
))
643 CComPtr
<IUnknown
> punk
;
645 hr
= db
->GetClient(&punk
);
646 if (FAILED_UNEXPECTEDLY(hr
))
649 if (!punk
&& m_Shown
)
651 if (!_IsSubMenuParent(reinterpret_cast<HWND
>(lParam
)))
653 OnSelect(MPOS_FULLCANCEL
);
660 LRESULT
CMenuDeskBar::_OnMouseActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
662 return MA_NOACTIVATE
;
665 LRESULT
CMenuDeskBar::_OnAppActivate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
667 if (wParam
== 0 && m_Shown
)
669 OnSelect(MPOS_FULLCANCEL
);