* Sync up to trunk head (r64716).
[reactos.git] / base / shell / rshell / CMenuDeskBar.cpp
1 /*
2 * Shell Menu Desk Bar
3 *
4 * Copyright 2014 David Quintana
5 *
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.
10 *
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.
15 *
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
19 */
20 #include "precomp.h"
21 #include <atlwin.h>
22 #include <shlwapi_undoc.h>
23
24 #include "CMenuDeskBar.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(CMenuDeskBar);
27
28 const static GUID CGID_MenuDeskBar = { 0x5C9F0A12, 0x959E, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x08, 0x26, 0x36 } };
29
30 extern "C"
31 HRESULT WINAPI CMenuDeskBar_Constructor(REFIID riid, LPVOID *ppv)
32 {
33 HRESULT hr;
34 #if USE_SYSTEM_MENUDESKBAR
35 hr = CoCreateInstance(CLSID_MenuDeskBar,
36 NULL,
37 CLSCTX_INPROC_SERVER,
38 riid, ppv);
39 #else
40 *ppv = NULL;
41
42 CMenuDeskBar * deskbar = new CComObject<CMenuDeskBar>();
43
44 if (!deskbar)
45 return E_OUTOFMEMORY;
46
47 hr = deskbar->QueryInterface(riid, ppv);
48
49 if (FAILED_UNEXPECTEDLY(hr))
50 delete deskbar;
51
52 #endif
53 return hr;
54 }
55
56 CMenuDeskBar::CMenuDeskBar() :
57 m_Client(NULL),
58 m_Banner(NULL),
59 m_Shown(FALSE)
60 {
61 }
62
63 CMenuDeskBar::~CMenuDeskBar()
64 {
65 }
66
67 HRESULT STDMETHODCALLTYPE CMenuDeskBar::Initialize(THIS)
68 {
69 return S_OK;
70 }
71
72 HRESULT STDMETHODCALLTYPE CMenuDeskBar::GetWindow(HWND *lphwnd)
73 {
74 if (lphwnd == NULL)
75 return E_POINTER;
76 *lphwnd = m_hWnd;
77 return S_OK;
78 }
79
80 HRESULT STDMETHODCALLTYPE CMenuDeskBar::ContextSensitiveHelp(BOOL fEnterMode)
81 {
82 return E_NOTIMPL;
83 }
84
85 HRESULT STDMETHODCALLTYPE CMenuDeskBar::OnFocusChangeIS(IUnknown *punkObj, BOOL fSetFocus)
86 {
87 CComPtr<IInputObjectSite> ios;
88
89 HRESULT hr = m_Client->QueryInterface(IID_PPV_ARG(IInputObjectSite, &ios));
90 if (FAILED_UNEXPECTEDLY(hr))
91 return hr;
92
93 return ios->OnFocusChangeIS(punkObj, fSetFocus);
94 }
95
96 HRESULT STDMETHODCALLTYPE CMenuDeskBar::QueryStatus(const GUID *pguidCmdGroup, ULONG cCmds,
97 OLECMD prgCmds [], OLECMDTEXT *pCmdText)
98 {
99 return E_NOTIMPL;
100 }
101
102 HRESULT STDMETHODCALLTYPE CMenuDeskBar::Exec(const GUID *pguidCmdGroup, DWORD nCmdID,
103 DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut)
104 {
105 if (IsEqualIID(*pguidCmdGroup, CGID_MenuDeskBar))
106 {
107 switch (nCmdID)
108 {
109 case 2: // refresh
110 return S_OK;
111 case 3: // load complete
112 return S_OK;
113 case 4: // set font metrics
114 return S_OK;
115 }
116 }
117 if (IsEqualIID(*pguidCmdGroup, CGID_Explorer))
118 {
119 }
120 else if (IsEqualIID(*pguidCmdGroup, IID_IDeskBarClient))
121 {
122 switch (nCmdID)
123 {
124 case 0:
125 // hide current band
126 break;
127 case 2:
128 break;
129 case 3:
130 break;
131 }
132 }
133 return E_NOTIMPL;
134 }
135
136 HRESULT STDMETHODCALLTYPE CMenuDeskBar::QueryService(REFGUID guidService, REFIID riid, void **ppvObject)
137 {
138 if (IsEqualGUID(guidService, SID_SMenuPopup) ||
139 IsEqualGUID(guidService, SID_SMenuBandParent) ||
140 IsEqualGUID(guidService, SID_STopLevelBrowser))
141 {
142 return this->QueryInterface(riid, ppvObject);
143 }
144
145 if (IsEqualGUID(guidService, SID_SMenuBandBottom) ||
146 IsEqualGUID(guidService, SID_SMenuBandBottomSelected) ||
147 IsEqualGUID(guidService, SID_SMenuBandChild))
148 {
149 if (m_Client == NULL)
150 return E_NOINTERFACE;
151
152 return IUnknown_QueryService(m_Client, guidService, riid, ppvObject);
153 }
154
155
156 if (m_Site == NULL)
157 return E_NOINTERFACE;
158
159 return IUnknown_QueryService(m_Site, guidService, riid, ppvObject);
160 }
161
162 HRESULT STDMETHODCALLTYPE CMenuDeskBar::UIActivateIO(BOOL fActivate, LPMSG lpMsg)
163 {
164 return IUnknown_UIActivateIO(m_Client, fActivate, lpMsg);
165 }
166
167 HRESULT STDMETHODCALLTYPE CMenuDeskBar::HasFocusIO()
168 {
169 CComPtr<IInputObject> io;
170
171 HRESULT hr = m_Client->QueryInterface(IID_PPV_ARG(IInputObject, &io));
172 if (FAILED_UNEXPECTEDLY(hr))
173 return hr;
174
175 return io->HasFocusIO();
176 }
177
178 HRESULT STDMETHODCALLTYPE CMenuDeskBar::TranslateAcceleratorIO(LPMSG lpMsg)
179 {
180 CComPtr<IInputObject> io;
181
182 HRESULT hr = m_Client->QueryInterface(IID_PPV_ARG(IInputObject, &io));
183 if (FAILED_UNEXPECTEDLY(hr))
184 return hr;
185
186 return io->TranslateAcceleratorIO(lpMsg);
187 }
188
189 HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetClient(IUnknown *punkClient)
190 {
191 CComPtr<IDeskBarClient> pDeskBandClient;
192 HRESULT hr;
193
194 m_Client.Release();
195
196 if (punkClient == NULL)
197 return S_OK;
198
199 if (m_hWnd == NULL)
200 {
201 Create(NULL);
202 }
203
204 hr = punkClient->QueryInterface(IID_PPV_ARG(IUnknown, &m_Client));
205 if (FAILED_UNEXPECTEDLY(hr))
206 return hr;
207
208 hr = m_Client->QueryInterface(IID_PPV_ARG(IDeskBarClient, &pDeskBandClient));
209 if (FAILED_UNEXPECTEDLY(hr))
210 return hr;
211
212 hr = pDeskBandClient->SetDeskBarSite(static_cast<IDeskBar*>(this));
213 if (FAILED_UNEXPECTEDLY(hr))
214 return hr;
215
216 return IUnknown_GetWindow(m_Client, &m_ClientWindow);
217 }
218
219 HRESULT STDMETHODCALLTYPE CMenuDeskBar::GetClient(IUnknown **ppunkClient)
220 {
221 if (ppunkClient == NULL)
222 return E_POINTER;
223
224 if (!m_Client)
225 return E_FAIL;
226
227 return m_Client->QueryInterface(IID_PPV_ARG(IUnknown, ppunkClient));
228 }
229
230 HRESULT STDMETHODCALLTYPE CMenuDeskBar::OnPosRectChangeDB(LPRECT prc)
231 {
232 if (prc == NULL)
233 return E_POINTER;
234
235 return S_OK;
236 }
237
238 HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetSite(IUnknown *pUnkSite)
239 {
240 // Windows closes the bar if this is called when the bar is shown
241
242 if (m_Shown)
243 _CloseBar();
244
245 m_Site = pUnkSite;
246
247 IUnknown_QueryService(m_Site, SID_SMenuPopup, IID_PPV_ARG(IMenuPopup, &m_SubMenuParent));
248
249 return S_OK;
250 }
251
252 HRESULT STDMETHODCALLTYPE CMenuDeskBar::GetSite(REFIID riid, void **ppvSite)
253 {
254 if (m_Site == NULL)
255 return E_FAIL;
256
257 return m_Site->QueryInterface(riid, ppvSite);
258 }
259
260 HRESULT STDMETHODCALLTYPE CMenuDeskBar::Popup(POINTL *ppt, RECTL *prcExclude, MP_POPUPFLAGS dwFlags)
261 {
262 HRESULT hr;
263 CComPtr<IOleCommandTarget> oct;
264 CComPtr<IInputObject> io;
265 CComPtr<IDeskBand> band;
266 CComPtr<IDeskBarClient> dbc;
267
268 if (m_hWnd == NULL)
269 return E_FAIL;
270
271 hr = IUnknown_QueryService(m_Client, SID_SMenuBandChild, IID_PPV_ARG(IOleCommandTarget, &oct));
272 if (FAILED_UNEXPECTEDLY(hr))
273 return hr;
274
275 hr = m_Client->QueryInterface(IID_PPV_ARG(IDeskBarClient, &dbc));
276 if (FAILED_UNEXPECTEDLY(hr))
277 return hr;
278
279 // Windows calls this, but it appears to be unimplemented?
280 hr = dbc->SetModeDBC(1);
281 // Allow it to fail with E_NOTIMPL.
282
283 // No clue about the arg, using anything != 0
284 hr = dbc->UIActivateDBC(TRUE);
285 if (FAILED_UNEXPECTEDLY(hr))
286 return hr;
287
288 RECT rc = { 0 };
289 hr = dbc->GetSize(0, &rc);
290 if (FAILED_UNEXPECTEDLY(hr))
291 return hr;
292
293 // Unknown meaning
294 const int CMD = 19;
295 const int CMD_EXEC_OPT = 0;
296
297 hr = IUnknown_QueryServiceExec(m_Client, SID_SMenuBandChild, &CLSID_MenuBand, CMD, CMD_EXEC_OPT, NULL, NULL);
298 if (FAILED_UNEXPECTEDLY(hr))
299 return hr;
300
301 ::AdjustWindowRect(&rc, ::GetWindowLong(m_hWnd, GWL_STYLE), FALSE);
302 ::OffsetRect(&rc, -rc.left, -rc.top);
303
304 if (m_Banner != NULL)
305 {
306 BITMAP bm;
307 ::GetObject(m_Banner, sizeof(bm), &bm);
308 rc.right += bm.bmWidth;
309 }
310
311 RECT rcWorkArea;
312 GetWindowRect(GetDesktopWindow(), &rcWorkArea);
313 int waHeight = rcWorkArea.bottom - rcWorkArea.top;
314
315 int x = ppt->x;
316 int y = ppt->y;
317 int cx = rc.right - rc.left;
318 int cy = rc.bottom - rc.top;
319
320 switch (dwFlags & 0xFF000000)
321 {
322 case MPPF_BOTTOM:
323 x = ppt->x;
324 y = ppt->y - rc.bottom;
325 break;
326 case MPPF_RIGHT:
327 x = ppt->x + rc.left;
328 y = ppt->y + rc.top;
329 break;
330 case MPPF_TOP | MPPF_ALIGN_LEFT:
331 x = ppt->x - rc.right;
332 y = ppt->y + rc.top;
333 break;
334 case MPPF_TOP | MPPF_ALIGN_RIGHT:
335 x = ppt->x;
336 y = ppt->y + rc.top;
337 break;
338 }
339
340 if (x + cx > rcWorkArea.right)
341 {
342 // FIXME: Works, but it's oversimplified.
343 x = prcExclude->left - cx;
344 dwFlags = (dwFlags & (~MPPF_TOP)) | MPPF_LEFT;
345 }
346
347 if (y < rcWorkArea.top)
348 {
349 y = rcWorkArea.top;
350 }
351
352 if (cy > waHeight)
353 {
354 cy = waHeight;
355 }
356
357 if (y + cy > rcWorkArea.bottom)
358 {
359 y = rcWorkArea.bottom - cy;
360 }
361
362 this->SetWindowPos(HWND_TOPMOST, x, y, cx, cy, SWP_SHOWWINDOW | SWP_NOACTIVATE);
363
364 m_ShowFlags = dwFlags;
365 m_Shown = true;
366
367 // HACK: The bar needs to be notified of the size AFTER it is shown.
368 // Quick & dirty way of getting it done.
369 BOOL bHandled;
370 _OnSize(WM_SIZE, 0, 0, bHandled);
371
372 UIActivateIO(TRUE, NULL);
373
374 if (dwFlags & (MPPF_INITIALSELECT | MPPF_FINALSELECT))
375 {
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);
379 }
380
381 return S_OK;
382 }
383
384 HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetIconSize(THIS_ DWORD iIcon)
385 {
386 HRESULT hr;
387 m_IconSize = iIcon;
388
389 // Unknown meaning (set flags? set icon size?)
390 const int CMD = 16;
391 const int CMD_EXEC_OPT = iIcon ? 0 : 2; // seems to work
392
393 hr = IUnknown_QueryServiceExec(m_Client, SID_SMenuBandChild, &CLSID_MenuBand, CMD, CMD_EXEC_OPT, NULL, NULL);
394 if (FAILED_UNEXPECTEDLY(hr))
395 return hr;
396
397 BOOL bHandled;
398 _OnSize(WM_SIZE, 0, 0, bHandled);
399
400 return hr;
401 }
402
403 HRESULT STDMETHODCALLTYPE CMenuDeskBar::GetIconSize(THIS_ DWORD* piIcon)
404 {
405 if (piIcon)
406 *piIcon = m_IconSize;
407 return S_OK;
408 }
409
410 HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetBitmap(THIS_ HBITMAP hBitmap)
411 {
412 m_Banner = hBitmap;
413
414 BOOL bHandled;
415 _OnSize(WM_SIZE, 0, 0, bHandled);
416
417 return S_OK;
418 }
419
420 HRESULT STDMETHODCALLTYPE CMenuDeskBar::GetBitmap(THIS_ HBITMAP* phBitmap)
421 {
422 if (phBitmap)
423 *phBitmap = m_Banner;
424 return S_OK;
425 }
426
427 HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetSubMenu(IMenuPopup *pmp, BOOL fSet)
428 {
429 // Called by the MenuBand to assign itself as the logical child of the DeskBar
430
431 if (fSet)
432 {
433 m_SubMenuChild = pmp;
434 }
435 else
436 {
437 if (m_SubMenuChild)
438 {
439 if (pmp == m_SubMenuChild)
440 {
441 m_SubMenuChild = NULL;
442 }
443 }
444 }
445 return S_OK;
446 }
447
448 HRESULT STDMETHODCALLTYPE CMenuDeskBar::OnSelect(DWORD dwSelectType)
449 {
450 /* As far as I can tell, the submenu hierarchy looks like this:
451 *
452 * The DeskBar's Child is the Band it contains.
453 * The DeskBar's Parent is the SID_SMenuPopup of the Site.
454 *
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).
457 *
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.
461 *
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?
466 *
467 */
468
469 switch (dwSelectType)
470 {
471 case MPOS_EXECUTE:
472 case MPOS_FULLCANCEL:
473 case MPOS_CANCELLEVEL:
474
475 _CloseBar();
476
477 if (dwSelectType == MPOS_CANCELLEVEL)
478 return S_OK;
479
480 case MPOS_SELECTLEFT:
481 case MPOS_SELECTRIGHT:
482 case MPOS_CHILDTRACKING:
483 if (m_SubMenuParent)
484 return m_SubMenuParent->OnSelect(dwSelectType);
485 break;
486 }
487
488 return S_OK;
489 }
490
491 HRESULT CMenuDeskBar::_CloseBar()
492 {
493 CComPtr<IDeskBarClient> dbc;
494 HRESULT hr;
495
496 m_Shown = false;
497
498 if (m_SubMenuParent)
499 {
500 m_SubMenuParent->SetSubMenu(this, FALSE);
501 }
502
503 if (m_SubMenuChild)
504 {
505 hr = m_SubMenuChild->OnSelect(MPOS_CANCELLEVEL);
506 if (FAILED_UNEXPECTEDLY(hr))
507 return hr;
508 }
509
510 hr = m_Client->QueryInterface(IID_PPV_ARG(IDeskBarClient, &dbc));
511 if (FAILED_UNEXPECTEDLY(hr))
512 return hr;
513
514 hr = dbc->UIActivateDBC(FALSE);
515 if (FAILED_UNEXPECTEDLY(hr))
516 return hr;
517
518 SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOACTIVATE | SWP_NOMOVE);
519
520 return UIActivateIO(FALSE, NULL);
521 }
522
523 BOOL CMenuDeskBar::_IsSubMenuParent(HWND hwnd)
524 {
525 CComPtr<IMenuPopup> popup = m_SubMenuParent;
526
527 while (popup)
528 {
529 HRESULT hr;
530 CComPtr<IOleWindow> window;
531
532 hr = popup->QueryInterface(IID_PPV_ARG(IOleWindow, &window));
533 if (FAILED_UNEXPECTEDLY(hr))
534 return FALSE;
535
536 HWND parent;
537
538 hr = window->GetWindow(&parent);
539 if (SUCCEEDED(hr) && hwnd == parent)
540 return TRUE;
541
542 popup = NULL;
543 hr = IUnknown_GetSite(window, IID_PPV_ARG(IMenuPopup, &popup));
544 if (FAILED(hr))
545 return FALSE;
546 }
547
548 return FALSE;
549 }
550
551 LRESULT CMenuDeskBar::_OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
552 {
553 if (m_Client)
554 {
555 RECT rc;
556
557 GetClientRect(&rc);
558
559 if (m_Banner != NULL)
560 {
561 BITMAP bm;
562 ::GetObject(m_Banner, sizeof(bm), &bm);
563 rc.left += bm.bmWidth;
564 }
565
566 ::SetWindowPos(m_ClientWindow, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, 0);
567 }
568
569 return 0;
570 }
571
572 LRESULT CMenuDeskBar::_OnNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
573 {
574 if (!m_Client)
575 return 0;
576
577 CComPtr<IWinEventHandler> winEventHandler;
578 HRESULT hr = m_Client->QueryInterface(IID_PPV_ARG(IWinEventHandler, &winEventHandler));
579 if (FAILED_UNEXPECTEDLY(hr))
580 return 0;
581
582 if (winEventHandler)
583 {
584 LRESULT result;
585 hr = winEventHandler->OnWinEvent(NULL, uMsg, wParam, lParam, &result);
586 if (FAILED_UNEXPECTEDLY(hr))
587 return 0;
588 return result;
589 }
590
591 return 0;
592 }
593
594 LRESULT CMenuDeskBar::_OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
595 {
596 bHandled = FALSE;
597
598 if (m_Banner && !m_IconSize)
599 {
600 BITMAP bm;
601 PAINTSTRUCT ps;
602 HDC hdc = BeginPaint(&ps);
603
604 HDC hdcMem = ::CreateCompatibleDC(hdc);
605 HGDIOBJ hbmOld = ::SelectObject(hdcMem, m_Banner);
606
607 ::GetObject(m_Banner, sizeof(bm), &bm);
608
609 RECT rc;
610 if (!GetClientRect(&rc))
611 WARN("GetClientRect failed\n");
612
613 const int bx = bm.bmWidth;
614 const int by = bm.bmHeight;
615 const int cy = rc.bottom;
616
617 TRACE("Painting banner: %d by %d\n", bm.bmWidth, bm.bmHeight);
618
619 if (!::StretchBlt(hdc, 0, 0, bx, cy - by, hdcMem, 0, 0, bx, 1, SRCCOPY))
620 WARN("StretchBlt failed\n");
621
622 if (!::BitBlt(hdc, 0, cy - by, bx, by, hdcMem, 0, 0, SRCCOPY))
623 WARN("BitBlt failed\n");
624
625 ::SelectObject(hdcMem, hbmOld);
626 ::DeleteDC(hdcMem);
627
628 EndPaint(&ps);
629 }
630
631 return TRUE;
632 }
633
634 LRESULT CMenuDeskBar::_OnActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
635 {
636 // BUG in ReactOS: WM_ACTIVATE/WA_INACTIVE makes no sense with lParam==hWnd
637 if (LOWORD(wParam) != 0 || reinterpret_cast<HWND>(lParam) == m_hWnd)
638 {
639 return 0;
640 }
641
642 // HACK! I just want it to work !!!
643 CComPtr<IDeskBar> db;
644 HRESULT hr = IUnknown_QueryService(m_Client, SID_SMenuBandChild, IID_PPV_ARG(IDeskBar, &db));
645 if (FAILED_UNEXPECTEDLY(hr))
646 return 0;
647
648 CComPtr<IUnknown> punk;
649
650 hr = db->GetClient(&punk);
651 if (FAILED_UNEXPECTEDLY(hr))
652 return 0;
653
654 if (!punk && m_Shown)
655 {
656 if (!_IsSubMenuParent(reinterpret_cast<HWND>(lParam)))
657 {
658 OnSelect(MPOS_FULLCANCEL);
659 }
660 }
661
662 return 0;
663 }
664
665 LRESULT CMenuDeskBar::_OnMouseActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
666 {
667 return MA_NOACTIVATE;
668 }
669
670 LRESULT CMenuDeskBar::_OnAppActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
671 {
672 if (wParam == 0 && m_Shown)
673 {
674 OnSelect(MPOS_FULLCANCEL);
675 }
676 return 0;
677 }