[SHELL32] - CMenuBand: Don't accept a NULL shell folder in SetShellFolder. Should...
[reactos.git] / reactos / dll / win32 / shell32 / shellmenu / CMenuBand.cpp
1 /*
2 * Shell Menu Band
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 "shellmenu.h"
21 #include <windowsx.h>
22 #include <commoncontrols.h>
23 #include <shlwapi_undoc.h>
24
25 #include "CMenuBand.h"
26 #include "CMenuToolbars.h"
27 #include "CMenuFocusManager.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(CMenuBand);
30
31 #undef UNIMPLEMENTED
32
33 #define UNIMPLEMENTED TRACE("%s is UNIMPLEMENTED!\n", __FUNCTION__)
34
35 extern "C"
36 HRESULT WINAPI CMenuBand_Constructor(REFIID riid, LPVOID *ppv)
37 {
38 return ShellObjectCreator<CMenuBand>(riid, ppv);
39 }
40
41 CMenuBand::CMenuBand() :
42 m_staticToolbar(NULL),
43 m_SFToolbar(NULL),
44 m_site(NULL),
45 m_psmc(NULL),
46 m_subMenuChild(NULL),
47 m_subMenuParent(NULL),
48 m_childBand(NULL),
49 m_parentBand(NULL),
50 m_hmenu(NULL),
51 m_menuOwner(NULL),
52 m_useBigIcons(FALSE),
53 m_topLevelWindow(NULL),
54 m_hotBar(NULL),
55 m_hotItem(-1),
56 m_popupBar(NULL),
57 m_popupItem(-1),
58 m_Show(FALSE),
59 m_shellBottom(FALSE),
60 m_trackedPopup(NULL),
61 m_trackedHwnd(NULL)
62 {
63 m_focusManager = CMenuFocusManager::AcquireManager();
64 }
65
66 CMenuBand::~CMenuBand()
67 {
68 CMenuFocusManager::ReleaseManager(m_focusManager);
69
70 delete m_staticToolbar;
71 delete m_SFToolbar;
72
73 if (m_hmenu)
74 DestroyMenu(m_hmenu);
75 }
76
77 HRESULT STDMETHODCALLTYPE CMenuBand::Initialize(
78 IShellMenuCallback *psmc,
79 UINT uId,
80 UINT uIdAncestor,
81 DWORD dwFlags)
82 {
83 if (m_psmc != psmc)
84 m_psmc = psmc;
85 m_uId = uId;
86 m_uIdAncestor = uIdAncestor;
87 m_dwFlags = dwFlags;
88
89 if (m_psmc)
90 {
91 _CallCB(SMC_CREATE, 0, reinterpret_cast<LPARAM>(&m_UserData));
92 }
93
94 return S_OK;
95 }
96
97 HRESULT STDMETHODCALLTYPE CMenuBand::GetMenuInfo(
98 IShellMenuCallback **ppsmc,
99 UINT *puId,
100 UINT *puIdAncestor,
101 DWORD *pdwFlags)
102 {
103 if (!pdwFlags) // maybe?
104 return E_INVALIDARG;
105
106 if (ppsmc)
107 {
108 if (m_psmc)
109 m_psmc->AddRef();
110 *ppsmc = m_psmc;
111 }
112
113 if (puId)
114 *puId = m_uId;
115
116 if (puIdAncestor)
117 *puIdAncestor = m_uIdAncestor;
118
119 *pdwFlags = m_dwFlags;
120
121 return S_OK;
122 }
123
124 HRESULT STDMETHODCALLTYPE CMenuBand::SetMenu(
125 HMENU hmenu,
126 HWND hwnd,
127 DWORD dwFlags)
128 {
129 HRESULT hr;
130
131 TRACE("CMenuBand::SetMenu called, hmenu=%p; hwnd=%p, flags=%x\n", hmenu, hwnd, dwFlags);
132
133 BOOL created = FALSE;
134
135 if (m_hmenu && m_hmenu != hmenu)
136 {
137 DestroyMenu(m_hmenu);
138 m_hmenu = NULL;
139 }
140
141 m_hmenu = hmenu;
142 m_menuOwner = hwnd;
143
144 if (m_hmenu && m_staticToolbar == NULL)
145 {
146 m_staticToolbar = new CMenuStaticToolbar(this);
147 created = true;
148 }
149
150 if (m_staticToolbar)
151 {
152 hr = m_staticToolbar->SetMenu(hmenu, hwnd, dwFlags);
153 if (FAILED_UNEXPECTEDLY(hr))
154 return hr;
155 }
156
157 if (m_site)
158 {
159 HWND hwndParent;
160
161 hr = m_site->GetWindow(&hwndParent);
162 if (FAILED_UNEXPECTEDLY(hr))
163 return hr;
164
165 if (created)
166 {
167 hr = m_staticToolbar->CreateToolbar(hwndParent, m_dwFlags);
168 if (FAILED_UNEXPECTEDLY(hr))
169 return hr;
170
171 hr = m_staticToolbar->FillToolbar();
172 }
173 else
174 {
175 hr = m_staticToolbar->FillToolbar(TRUE);
176 }
177 }
178
179 return hr;
180 }
181
182 HRESULT STDMETHODCALLTYPE CMenuBand::GetMenu(
183 HMENU *phmenu,
184 HWND *phwnd,
185 DWORD *pdwFlags)
186 {
187 if (m_staticToolbar == NULL)
188 return E_FAIL;
189
190 return m_staticToolbar->GetMenu(phmenu, phwnd, pdwFlags);
191 }
192
193 HRESULT STDMETHODCALLTYPE CMenuBand::SetSite(IUnknown *pUnkSite)
194 {
195 HWND hwndParent;
196 HRESULT hr;
197
198 m_site = NULL;
199
200 if (pUnkSite == NULL)
201 return S_OK;
202
203 hwndParent = NULL;
204 hr = pUnkSite->QueryInterface(IID_PPV_ARG(IOleWindow, &m_site));
205 if (FAILED_UNEXPECTEDLY(hr))
206 return hr;
207
208 hr = m_site->GetWindow(&hwndParent);
209 if (FAILED_UNEXPECTEDLY(hr))
210 return hr;
211
212 if (!::IsWindow(hwndParent))
213 return E_FAIL;
214
215 if (m_staticToolbar != NULL)
216 {
217 hr = m_staticToolbar->CreateToolbar(hwndParent, m_dwFlags);
218 if (FAILED_UNEXPECTEDLY(hr))
219 return hr;
220
221 hr = m_staticToolbar->FillToolbar();
222 if (FAILED_UNEXPECTEDLY(hr))
223 return hr;
224 }
225
226 if (m_SFToolbar != NULL)
227 {
228 hr = m_SFToolbar->CreateToolbar(hwndParent, m_dwFlags);
229 if (FAILED_UNEXPECTEDLY(hr))
230 return hr;
231
232 hr = m_SFToolbar->FillToolbar();
233 if (FAILED_UNEXPECTEDLY(hr))
234 return hr;
235 }
236
237 hr = IUnknown_QueryService(m_site, SID_SMenuPopup, IID_PPV_ARG(IMenuPopup, &m_subMenuParent));
238 if (hr != E_NOINTERFACE && FAILED_UNEXPECTEDLY(hr))
239 return hr;
240
241 CComPtr<IOleWindow> pTopLevelWindow;
242 hr = IUnknown_QueryService(m_site, SID_STopLevelBrowser, IID_PPV_ARG(IOleWindow, &pTopLevelWindow));
243 if (FAILED_UNEXPECTEDLY(hr))
244 return hr;
245
246 return pTopLevelWindow->GetWindow(&m_topLevelWindow);
247 }
248
249 HRESULT STDMETHODCALLTYPE CMenuBand::GetSite(REFIID riid, PVOID *ppvSite)
250 {
251 if (m_site == NULL)
252 return E_FAIL;
253
254 return m_site->QueryInterface(riid, ppvSite);
255 }
256
257 HRESULT STDMETHODCALLTYPE CMenuBand::GetWindow(HWND *phwnd)
258 {
259 if (m_SFToolbar != NULL)
260 return m_SFToolbar->GetWindow(phwnd);
261
262 if (m_staticToolbar != NULL)
263 return m_staticToolbar->GetWindow(phwnd);
264
265 if (phwnd) *phwnd = NULL;
266
267 return E_FAIL;
268 }
269
270 HRESULT STDMETHODCALLTYPE CMenuBand::OnPosRectChangeDB(RECT *prc)
271 {
272 SIZE maxStatic = { 0 };
273 SIZE maxShlFld = { 0 };
274 HRESULT hr = S_OK;
275
276 if (m_staticToolbar != NULL)
277 hr = m_staticToolbar->GetSizes(NULL, &maxStatic, NULL);
278 if (FAILED_UNEXPECTEDLY(hr))
279 return hr;
280
281 if (m_SFToolbar != NULL)
282 hr = m_SFToolbar->GetSizes(NULL, &maxShlFld, NULL);
283 if (FAILED_UNEXPECTEDLY(hr))
284 return hr;
285
286 if (m_staticToolbar == NULL && m_SFToolbar == NULL)
287 return E_FAIL;
288
289 int sy = min(prc->bottom - prc->top, maxStatic.cy + maxShlFld.cy);
290
291 int syStatic = maxStatic.cy;
292 int syShlFld = sy - syStatic;
293
294 // TODO: Windows has a more complex system to decide ordering.
295 // Because we only support two toolbars at once, this is enough for us.
296 if (m_shellBottom)
297 {
298 // Static menu on top
299 if (m_SFToolbar)
300 {
301 m_SFToolbar->SetPosSize(
302 prc->left,
303 prc->top + syStatic,
304 prc->right - prc->left,
305 syShlFld);
306 }
307 if (m_staticToolbar)
308 {
309 m_staticToolbar->SetPosSize(
310 prc->left,
311 prc->top,
312 prc->right - prc->left,
313 syStatic);
314 }
315 }
316 else
317 {
318 // Folder menu on top
319 if (m_SFToolbar)
320 {
321 m_SFToolbar->SetPosSize(
322 prc->left,
323 prc->top,
324 prc->right - prc->left,
325 syShlFld);
326 }
327 if (m_staticToolbar)
328 {
329 m_staticToolbar->SetPosSize(
330 prc->left,
331 prc->top + syShlFld,
332 prc->right - prc->left,
333 syStatic);
334 }
335 }
336
337 return S_OK;
338 }
339
340 HRESULT STDMETHODCALLTYPE CMenuBand::GetBandInfo(
341 DWORD dwBandID,
342 DWORD dwViewMode,
343 DESKBANDINFO *pdbi)
344 {
345 SIZE minStatic = { 0 };
346 SIZE minShlFld = { 0 };
347 SIZE maxStatic = { 0 };
348 SIZE maxShlFld = { 0 };
349 SIZE intStatic = { 0 };
350 SIZE intShlFld = { 0 };
351
352 HRESULT hr = S_OK;
353
354 if (m_staticToolbar != NULL)
355 hr = m_staticToolbar->GetSizes(&minStatic, &maxStatic, &intStatic);
356 if (FAILED_UNEXPECTEDLY(hr))
357 return hr;
358
359 if (m_SFToolbar != NULL)
360 hr = m_SFToolbar->GetSizes(&minShlFld, &maxShlFld, &intShlFld);
361 if (FAILED_UNEXPECTEDLY(hr))
362 return hr;
363
364 if (m_staticToolbar == NULL && m_SFToolbar == NULL)
365 return E_FAIL;
366
367 if (m_dwFlags & SMINIT_VERTICAL)
368 {
369 pdbi->ptMinSize.x = max(minStatic.cx, minShlFld.cx) + 20;
370 pdbi->ptMinSize.y = minStatic.cy + minShlFld.cy;
371 pdbi->ptMaxSize.x = max(maxStatic.cx, maxShlFld.cx) + 20;
372 pdbi->ptMaxSize.y = maxStatic.cy + maxShlFld.cy;
373 pdbi->dwModeFlags = DBIMF_VARIABLEHEIGHT;
374 }
375 else
376 {
377 pdbi->ptMinSize.x = minStatic.cx + minShlFld.cx;
378 pdbi->ptMinSize.y = max(minStatic.cy, minShlFld.cy);
379 pdbi->ptMaxSize.x = maxStatic.cx + maxShlFld.cx;
380 pdbi->ptMaxSize.y = max(maxStatic.cy, maxShlFld.cy);
381 }
382 pdbi->ptIntegral.x = max(intStatic.cx, intShlFld.cx);
383 pdbi->ptIntegral.y = max(intStatic.cy, intShlFld.cy);
384 pdbi->ptActual = pdbi->ptMinSize;
385
386 return S_OK;
387 }
388
389 HRESULT STDMETHODCALLTYPE CMenuBand::ShowDW(BOOL fShow)
390 {
391 HRESULT hr = S_OK;
392
393 if (m_Show == fShow)
394 return S_OK;
395
396 m_Show = fShow;
397
398 if (m_staticToolbar != NULL)
399 {
400 hr = m_staticToolbar->ShowDW(fShow);
401 if (FAILED_UNEXPECTEDLY(hr))
402 return hr;
403 }
404
405 if (m_SFToolbar != NULL)
406 {
407 hr = m_SFToolbar->ShowDW(fShow);
408 if (FAILED_UNEXPECTEDLY(hr))
409 return hr;
410 }
411
412 if (fShow)
413 {
414 hr = _CallCB(SMC_INITMENU, 0, 0);
415 if (FAILED_UNEXPECTEDLY(hr))
416 return hr;
417 }
418 else if (m_parentBand)
419 {
420 m_parentBand->SetClient(NULL);
421 }
422
423 if (_IsPopup() == S_OK)
424 {
425 if (fShow)
426 hr = m_focusManager->PushMenuPopup(this);
427 else
428 hr = m_focusManager->PopMenuPopup(this);
429 }
430 else
431 {
432 if (fShow)
433 hr = m_focusManager->PushMenuBar(this);
434 else
435 hr = m_focusManager->PopMenuBar(this);
436 }
437
438 return S_OK;
439 }
440
441 HRESULT STDMETHODCALLTYPE CMenuBand::CloseDW(DWORD dwReserved)
442 {
443 if (m_subMenuChild)
444 {
445 m_subMenuChild->OnSelect(MPOS_CANCELLEVEL);
446 }
447
448 if (m_subMenuChild)
449 {
450 TRACE("Child object should have removed itself.\n");
451 }
452
453 ShowDW(FALSE);
454
455 if (m_staticToolbar != NULL)
456 {
457 m_staticToolbar->Close();
458 }
459
460 if (m_SFToolbar != NULL)
461 {
462 m_SFToolbar->Close();
463 }
464
465 if (m_site) m_site.Release();
466 if (m_subMenuChild) m_subMenuChild.Release();
467 if (m_subMenuParent) m_subMenuParent.Release();
468 if (m_childBand) m_childBand.Release();
469 if (m_parentBand) m_parentBand.Release();
470
471 return S_OK;
472 }
473
474 HRESULT STDMETHODCALLTYPE CMenuBand::UIActivateIO(BOOL fActivate, LPMSG lpMsg)
475 {
476 HRESULT hr;
477
478 if (m_subMenuParent)
479 {
480 hr = m_subMenuParent->SetSubMenu(this, fActivate);
481 if (FAILED_UNEXPECTEDLY(hr))
482 return hr;
483 }
484
485 if (fActivate)
486 {
487 CComPtr<IOleWindow> pTopLevelWindow;
488 hr = IUnknown_QueryService(m_site, SID_SMenuPopup, IID_PPV_ARG(IOleWindow, &pTopLevelWindow));
489 if (FAILED_UNEXPECTEDLY(hr))
490 return hr;
491
492 hr = pTopLevelWindow->GetWindow(&m_topLevelWindow);
493 if (FAILED_UNEXPECTEDLY(hr))
494 return hr;
495 }
496 else
497 {
498 m_topLevelWindow = NULL;
499 }
500
501 return S_FALSE;
502 }
503
504 HRESULT STDMETHODCALLTYPE CMenuBand::Exec(const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut)
505 {
506 if (!pguidCmdGroup)
507 return E_FAIL;
508
509 if (IsEqualGUID(*pguidCmdGroup, CLSID_MenuBand))
510 {
511 if (nCmdID == 16) // set (big) icon size
512 {
513 this->m_useBigIcons = nCmdexecopt == 2;
514 return S_OK;
515 }
516 else if (nCmdID == 19) // popup-related
517 {
518 return S_FALSE;
519 }
520 else if (nCmdID == 5) // select an item
521 {
522 if (nCmdexecopt == 0) // first
523 {
524 _KeyboardItemChange(VK_HOME);
525 }
526 else // last
527 {
528 _KeyboardItemChange(VK_END);
529 }
530 return S_FALSE;
531 }
532
533 return S_FALSE;
534 }
535
536 UNIMPLEMENTED;
537 return S_OK;
538 }
539
540 HRESULT STDMETHODCALLTYPE CMenuBand::QueryService(REFGUID guidService, REFIID riid, void **ppvObject)
541 {
542 if (IsEqualIID(guidService, SID_SMenuBandChild) ||
543 IsEqualIID(guidService, SID_SMenuBandBottom) ||
544 IsEqualIID(guidService, SID_SMenuBandBottomSelected))
545 return this->QueryInterface(riid, ppvObject);
546 WARN("Unknown service requested %s\n", wine_dbgstr_guid(&guidService));
547 return E_NOINTERFACE;
548 }
549
550 HRESULT STDMETHODCALLTYPE CMenuBand::Popup(POINTL *ppt, RECTL *prcExclude, MP_POPUPFLAGS dwFlags)
551 {
552 UNIMPLEMENTED;
553 return S_OK;
554 }
555
556 HRESULT STDMETHODCALLTYPE CMenuBand::OnSelect(DWORD dwSelectType)
557 {
558 // When called from outside, this is straightforward:
559 // Things that a submenu needs to know, are spread down, and
560 // things that the parent needs to know, are spread up. No drama.
561 // The fun is in _MenuItemSelect (internal method).
562 switch (dwSelectType)
563 {
564 case MPOS_CHILDTRACKING:
565 if (!m_subMenuParent)
566 break;
567 // TODO: Cancel timers?
568 return m_subMenuParent->OnSelect(dwSelectType);
569 case MPOS_SELECTLEFT:
570 if (m_subMenuChild)
571 m_subMenuChild->OnSelect(MPOS_CANCELLEVEL);
572 if (!m_subMenuParent)
573 break;
574 return m_subMenuParent->OnSelect(dwSelectType);
575 case MPOS_SELECTRIGHT:
576 if (!m_subMenuParent)
577 break;
578 return m_subMenuParent->OnSelect(dwSelectType);
579 case MPOS_EXECUTE:
580 case MPOS_FULLCANCEL:
581 if (m_subMenuChild)
582 m_subMenuChild->OnSelect(dwSelectType);
583 if (!m_subMenuParent)
584 break;
585 return m_subMenuParent->OnSelect(dwSelectType);
586 case MPOS_CANCELLEVEL:
587 if (m_subMenuChild)
588 m_subMenuChild->OnSelect(dwSelectType);
589 break;
590 }
591 return S_FALSE;
592 }
593
594 HRESULT STDMETHODCALLTYPE CMenuBand::SetSubMenu(IMenuPopup *pmp, BOOL fSet)
595 {
596 UNIMPLEMENTED;
597 return S_OK;
598 }
599
600 // Used by the focus manager to update the child band pointer
601 HRESULT CMenuBand::_SetChildBand(CMenuBand * child)
602 {
603 m_childBand = child;
604 if (!child)
605 {
606 _ChangePopupItem(NULL, -1);
607 }
608 return S_OK;
609 }
610
611 // User by the focus manager to update the parent band pointer
612 HRESULT CMenuBand::_SetParentBand(CMenuBand * parent)
613 {
614 m_parentBand = parent;
615 return S_OK;
616 }
617
618 HRESULT CMenuBand::_IsPopup()
619 {
620 return !(m_dwFlags & SMINIT_VERTICAL);
621 }
622
623 HRESULT CMenuBand::_IsTracking()
624 {
625 return m_popupBar != NULL;
626 }
627
628 HRESULT STDMETHODCALLTYPE CMenuBand::SetClient(IUnknown *punkClient)
629 {
630 CComPtr<IMenuPopup> child = m_subMenuChild;
631
632 m_subMenuChild = NULL;
633
634 if (child)
635 {
636 IUnknown_SetSite(child, NULL);
637 child.Release();
638 }
639
640 if (!punkClient)
641 {
642 return S_OK;
643 }
644
645 return punkClient->QueryInterface(IID_PPV_ARG(IMenuPopup, &m_subMenuChild));
646 }
647
648 HRESULT STDMETHODCALLTYPE CMenuBand::GetClient(IUnknown **ppunkClient)
649 {
650 if (!ppunkClient)
651 return E_POINTER;
652 *ppunkClient = NULL;
653
654 if (m_subMenuChild)
655 {
656 m_subMenuChild->AddRef();
657 *ppunkClient = m_subMenuChild;
658 }
659
660 return S_OK;
661 }
662
663 HRESULT STDMETHODCALLTYPE CMenuBand::IsMenuMessage(MSG *pmsg)
664 {
665 return S_FALSE;
666 }
667
668 HRESULT STDMETHODCALLTYPE CMenuBand::TranslateMenuMessage(MSG *pmsg, LRESULT *plRet)
669 {
670 return S_FALSE;
671 }
672
673 HRESULT STDMETHODCALLTYPE CMenuBand::SetShellFolder(IShellFolder *psf, LPCITEMIDLIST pidlFolder, HKEY hKey, DWORD dwFlags)
674 {
675 if (!psf)
676 return E_INVALIDARG;
677
678 if (m_SFToolbar == NULL)
679 {
680 m_SFToolbar = new CMenuSFToolbar(this);
681 }
682
683 HRESULT hr = m_SFToolbar->SetShellFolder(psf, pidlFolder, hKey, dwFlags);
684 if (FAILED_UNEXPECTEDLY(hr))
685 return hr;
686
687 m_shellBottom = (dwFlags & SMSET_BOTTOM) != 0;
688
689 if (m_site)
690 {
691 HWND hwndParent;
692
693 hr = m_site->GetWindow(&hwndParent);
694 if (FAILED_UNEXPECTEDLY(hr))
695 return hr;
696
697 hr = m_SFToolbar->CreateToolbar(hwndParent, m_dwFlags);
698 if (FAILED_UNEXPECTEDLY(hr))
699 return hr;
700
701 hr = m_SFToolbar->FillToolbar();
702 }
703
704 return hr;
705 }
706
707 HRESULT STDMETHODCALLTYPE CMenuBand::GetShellFolder(DWORD *pdwFlags, LPITEMIDLIST *ppidl, REFIID riid, void **ppv)
708 {
709 if (m_SFToolbar)
710 return m_SFToolbar->GetShellFolder(pdwFlags, ppidl, riid, ppv);
711 return E_FAIL;
712 }
713
714 HRESULT STDMETHODCALLTYPE CMenuBand::OnWinEvent(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *theResult)
715 {
716 *theResult = 0;
717
718 if (uMsg == WM_WININICHANGE && wParam == SPI_SETFLATMENU)
719 {
720 BOOL bFlatMenus;
721 SystemParametersInfo(SPI_GETFLATMENU, 0, &bFlatMenus, 0);
722 AdjustForTheme(bFlatMenus);
723 return S_OK;
724 }
725
726 if (m_staticToolbar && m_staticToolbar->IsWindowOwner(hWnd) == S_OK)
727 {
728 return m_staticToolbar->OnWinEvent(hWnd, uMsg, wParam, lParam, theResult);
729 }
730
731 if (m_SFToolbar && m_SFToolbar->IsWindowOwner(hWnd) == S_OK)
732 {
733 return m_SFToolbar->OnWinEvent(hWnd, uMsg, wParam, lParam, theResult);
734 }
735
736 return S_FALSE;
737 }
738
739 HRESULT STDMETHODCALLTYPE CMenuBand::IsWindowOwner(HWND hWnd)
740 {
741 if (m_staticToolbar && m_staticToolbar->IsWindowOwner(hWnd) == S_OK)
742 return S_OK;
743
744 if (m_SFToolbar && m_SFToolbar->IsWindowOwner(hWnd) == S_OK)
745 return S_OK;
746
747 return S_FALSE;
748 }
749
750 HRESULT CMenuBand::_CallCBWithItemId(UINT id, UINT uMsg, WPARAM wParam, LPARAM lParam)
751 {
752 return _CallCB(uMsg, wParam, lParam, id);
753 }
754
755 HRESULT CMenuBand::_CallCBWithItemPidl(LPITEMIDLIST pidl, UINT uMsg, WPARAM wParam, LPARAM lParam)
756 {
757 return _CallCB(uMsg, wParam, lParam, 0, pidl);
758 }
759
760 HRESULT CMenuBand::_CallCB(UINT uMsg, WPARAM wParam, LPARAM lParam, UINT id, LPITEMIDLIST pidl)
761 {
762 if (!m_psmc)
763 return S_FALSE;
764
765 SMDATA smData = { 0 };
766 smData.punk = static_cast<IShellMenu2*>(this);
767 smData.uId = id;
768 smData.uIdParent = m_uId;
769 smData.uIdAncestor = m_uIdAncestor;
770 smData.pidlItem = pidl;
771 smData.hwnd = m_menuOwner ? m_menuOwner : m_topLevelWindow;
772 smData.hmenu = m_hmenu;
773 if (m_SFToolbar)
774 m_SFToolbar->GetShellFolder(NULL, &smData.pidlFolder, IID_PPV_ARG(IShellFolder, &smData.psf));
775 HRESULT hr = m_psmc->CallbackSM(&smData, uMsg, wParam, lParam);
776 ILFree(smData.pidlFolder);
777 if (smData.psf)
778 smData.psf->Release();
779 return hr;
780 }
781
782 HRESULT CMenuBand::_TrackSubMenu(HMENU popup, INT x, INT y, RECT& rcExclude)
783 {
784 TPMPARAMS params = { sizeof(TPMPARAMS), rcExclude };
785 UINT flags = TPM_VERPOSANIMATION | TPM_VERTICAL | TPM_LEFTALIGN;
786 HWND hwnd = m_menuOwner ? m_menuOwner : m_topLevelWindow;
787
788 m_trackedPopup = popup;
789 m_trackedHwnd = hwnd;
790
791 m_focusManager->PushTrackedPopup(popup);
792 ::TrackPopupMenuEx(popup, flags, x, y, hwnd, &params);
793 m_focusManager->PopTrackedPopup(popup);
794
795 m_trackedPopup = NULL;
796 m_trackedHwnd = NULL;
797
798 _DisableMouseTrack(FALSE);
799
800 return S_OK;
801 }
802
803 HRESULT CMenuBand::_TrackContextMenu(IContextMenu * contextMenu, INT x, INT y)
804 {
805 HRESULT hr;
806 UINT uCommand;
807
808 // Ensure that the menu doesn't disappear on us
809 CComPtr<IContextMenu> ctxMenu = contextMenu;
810
811 HMENU popup = CreatePopupMenu();
812
813 if (popup == NULL)
814 return E_FAIL;
815
816 TRACE("Before Query\n");
817 hr = contextMenu->QueryContextMenu(popup, 0, 0, UINT_MAX, CMF_NORMAL);
818 if (FAILED_UNEXPECTEDLY(hr))
819 {
820 TRACE("Query failed\n");
821 DestroyMenu(popup);
822 return hr;
823 }
824
825 HWND hwnd = m_menuOwner ? m_menuOwner : m_topLevelWindow;
826
827 m_focusManager->PushTrackedPopup(popup);
828
829 TRACE("Before Tracking\n");
830 uCommand = ::TrackPopupMenuEx(popup, TPM_RETURNCMD, x, y, hwnd, NULL);
831
832 m_focusManager->PopTrackedPopup(popup);
833
834 if (uCommand != 0)
835 {
836 _MenuItemSelect(MPOS_FULLCANCEL);
837
838 TRACE("Before InvokeCommand\n");
839 CMINVOKECOMMANDINFO cmi = { 0 };
840 cmi.cbSize = sizeof(cmi);
841 cmi.lpVerb = MAKEINTRESOURCEA(uCommand);
842 cmi.hwnd = hwnd;
843 hr = contextMenu->InvokeCommand(&cmi);
844 TRACE("InvokeCommand returned hr=%08x\n", hr);
845 }
846 else
847 {
848 TRACE("TrackPopupMenu failed. Code=%d, LastError=%d\n", uCommand, GetLastError());
849 hr = S_FALSE;
850 }
851
852 DestroyMenu(popup);
853 return hr;
854 }
855
856 HRESULT CMenuBand::_GetTopLevelWindow(HWND*topLevel)
857 {
858 *topLevel = m_topLevelWindow;
859 return S_OK;
860 }
861
862 HRESULT CMenuBand::_ChangeHotItem(CMenuToolbarBase * tb, INT id, DWORD dwFlags)
863 {
864 if (m_hotBar == tb && m_hotItem == id)
865 return S_FALSE;
866
867 TRACE("Hot item changed from %p %p, to %p %p\n", m_hotBar, m_hotItem, tb, id);
868
869 _KillPopupTimers();
870
871 m_hotBar = tb;
872 m_hotItem = id;
873 if (m_staticToolbar) m_staticToolbar->ChangeHotItem(tb, id, dwFlags);
874 if (m_SFToolbar) m_SFToolbar->ChangeHotItem(tb, id, dwFlags);
875
876 _MenuItemSelect(MPOS_CHILDTRACKING);
877
878 return S_OK;
879 }
880
881 HRESULT CMenuBand::_ChangePopupItem(CMenuToolbarBase * tb, INT id)
882 {
883 TRACE("Popup item changed from %p %p, to %p %p\n", m_popupBar, m_popupItem, tb, id);
884
885 m_popupBar = tb;
886 m_popupItem = id;
887 if (m_staticToolbar) m_staticToolbar->ChangePopupItem(tb, id);
888 if (m_SFToolbar) m_SFToolbar->ChangePopupItem(tb, id);
889
890 return S_OK;
891 }
892
893 HRESULT CMenuBand::_KeyboardItemChange(DWORD change)
894 {
895 HRESULT hr;
896 CMenuToolbarBase *tb = m_hotBar;
897
898 if (!tb)
899 {
900 // If no hot item was selected choose the appropriate toolbar
901 if (change == VK_UP || change == VK_END)
902 {
903 if (m_staticToolbar)
904 tb = m_staticToolbar;
905 else
906 tb = m_SFToolbar;
907 }
908 else if (change == VK_DOWN || change == VK_HOME)
909 {
910 if (m_SFToolbar)
911 tb = m_SFToolbar;
912 else
913 tb = m_staticToolbar;
914 }
915 }
916
917 // Ask the first toolbar to change
918 hr = tb->KeyboardItemChange(change);
919
920 if (hr != S_FALSE)
921 return hr;
922
923 // Select the second toolbar based on the first
924 if (tb == m_SFToolbar && m_staticToolbar)
925 tb = m_staticToolbar;
926 else if (m_SFToolbar)
927 tb = m_SFToolbar;
928
929 if (!tb)
930 return hr;
931
932 // Ask the second toolbar to change
933 return tb->KeyboardItemChange(change == VK_DOWN ? VK_HOME : VK_END);
934 }
935
936 HRESULT CMenuBand::_MenuItemSelect(DWORD changeType)
937 {
938 // Needed to prevent the this point from vanishing mid-function
939 CComPtr<CMenuBand> safeThis = this;
940 HRESULT hr;
941
942 if (m_dwFlags & SMINIT_VERTICAL)
943 {
944 switch (changeType)
945 {
946 case VK_UP:
947 case VK_DOWN:
948 return _KeyboardItemChange(changeType);
949
950 // TODO: Left/Right across multi-column menus, if they ever work.
951 case VK_LEFT:
952 changeType = MPOS_SELECTLEFT;
953 break;
954 case VK_RIGHT:
955 changeType = MPOS_SELECTRIGHT;
956 break;
957 }
958 }
959 else
960 {
961 // In horizontal menubars, left/right are equivalent to vertical's up/down
962 switch (changeType)
963 {
964 case VK_LEFT:
965 hr = _KeyboardItemChange(VK_UP);
966 if (hr != S_FALSE)
967 return hr;
968 case VK_RIGHT:
969 hr = _KeyboardItemChange(VK_DOWN);
970 if (hr != S_FALSE)
971 return hr;
972 }
973 }
974
975 // In this context, the parent is the CMenuDeskBar, so when it bubbles upward,
976 // it is notifying the deskbar, and not the the higher-level menu.
977 // Same for the child: since it points to a CMenuDeskBar, it's not just recursing.
978 switch (changeType)
979 {
980 case MPOS_EXECUTE:
981 {
982 CMenuToolbarBase * tb = m_hotBar;
983 int item = m_hotItem;
984 tb->PrepareExecuteItem(item);
985 if (m_subMenuParent)
986 {
987 m_subMenuParent->OnSelect(changeType);
988 }
989 TRACE("Menu closed, executing item...\n");
990 tb->ExecuteItem();
991 break;
992 }
993 case MPOS_SELECTLEFT:
994 if (m_parentBand && m_parentBand->_IsPopup()==S_FALSE)
995 return m_parentBand->_MenuItemSelect(VK_LEFT);
996 if (m_subMenuChild)
997 return m_subMenuChild->OnSelect(MPOS_CANCELLEVEL);
998 if (!m_subMenuParent)
999 return S_OK;
1000 return m_subMenuParent->OnSelect(MPOS_CANCELLEVEL);
1001
1002 case MPOS_SELECTRIGHT:
1003 if (m_hotBar && m_hotItem >= 0 && m_hotBar->PopupItem(m_hotItem, TRUE) == S_OK)
1004 return S_FALSE;
1005 if (m_parentBand)
1006 return m_parentBand->_MenuItemSelect(VK_RIGHT);
1007 if (!m_subMenuParent)
1008 return S_OK;
1009 return m_subMenuParent->OnSelect(MPOS_SELECTRIGHT);
1010
1011 default:
1012 if (!m_subMenuParent)
1013 return S_OK;
1014 return m_subMenuParent->OnSelect(changeType);
1015 }
1016
1017 return S_OK;
1018 }
1019
1020 HRESULT CMenuBand::_CancelCurrentPopup()
1021 {
1022 if (m_subMenuChild)
1023 {
1024 HRESULT hr = m_subMenuChild->OnSelect(MPOS_CANCELLEVEL);
1025 return hr;
1026 }
1027
1028 if (m_trackedPopup)
1029 {
1030 ::SendMessage(m_trackedHwnd, WM_CANCELMODE, 0, 0);
1031 return S_OK;
1032 }
1033
1034 return S_FALSE;
1035 }
1036
1037 HRESULT CMenuBand::_OnPopupSubMenu(IShellMenu * childShellMenu, POINTL * pAt, RECTL * pExclude, BOOL keyInitiated)
1038 {
1039 HRESULT hr = 0;
1040 CComPtr<IBandSite> pBandSite;
1041 CComPtr<IDeskBar> pDeskBar;
1042
1043 // Create the necessary objects
1044 #if USE_SYSTEM_MENUSITE
1045 hr = CoCreateInstance(CLSID_MenuBandSite,
1046 NULL,
1047 CLSCTX_INPROC_SERVER,
1048 IID_PPV_ARG(IBandSite, &pBandSite));
1049 #else
1050 hr = CMenuSite_Constructor(IID_PPV_ARG(IBandSite, &pBandSite));
1051 #endif
1052 if (FAILED_UNEXPECTEDLY(hr))
1053 return hr;
1054
1055 #if USE_SYSTEM_MENUDESKBAR
1056 hr = CoCreateInstance(CLSID_MenuDeskBar,
1057 NULL,
1058 CLSCTX_INPROC_SERVER,
1059 IID_PPV_ARG(IDeskBar, &pDeskBar));
1060 #else
1061 hr = CMenuDeskBar_Constructor(IID_PPV_ARG(IDeskBar, &pDeskBar));
1062 #endif
1063 if (FAILED_UNEXPECTEDLY(hr))
1064 return hr;
1065
1066 hr = pDeskBar->SetClient(pBandSite);
1067 if (FAILED_UNEXPECTEDLY(hr))
1068 return hr;
1069
1070 hr = pBandSite->AddBand(childShellMenu);
1071 if (FAILED_UNEXPECTEDLY(hr))
1072 return hr;
1073
1074 //
1075 CComPtr<IMenuPopup> popup;
1076 hr = pDeskBar->QueryInterface(IID_PPV_ARG(IMenuPopup, &popup));
1077 if (FAILED_UNEXPECTEDLY(hr))
1078 return hr;
1079
1080 m_subMenuChild = popup;
1081
1082 if (m_subMenuParent)
1083 IUnknown_SetSite(popup, m_subMenuParent);
1084 else
1085 IUnknown_SetSite(popup, m_site);
1086
1087 DWORD flags = MPPF_RIGHT;
1088
1089 if (keyInitiated && m_dwFlags & SMINIT_VERTICAL)
1090 flags |= MPPF_INITIALSELECT;
1091
1092 popup->Popup(pAt, pExclude, flags);
1093
1094 return S_OK;
1095 }
1096
1097 HRESULT CMenuBand::_BeforeCancelPopup()
1098 {
1099 if (m_staticToolbar)
1100 m_staticToolbar->BeforeCancelPopup();
1101 if (m_SFToolbar)
1102 m_SFToolbar->BeforeCancelPopup();
1103 return S_OK;
1104 }
1105
1106 HRESULT CMenuBand::_DisableMouseTrack(BOOL bDisable)
1107 {
1108 if (m_staticToolbar)
1109 m_staticToolbar->DisableMouseTrack(bDisable);
1110 if (m_SFToolbar)
1111 m_SFToolbar->DisableMouseTrack(bDisable);
1112 return S_OK;
1113 }
1114
1115 HRESULT CMenuBand::_KillPopupTimers()
1116 {
1117 HRESULT hr = S_OK;
1118 if (m_staticToolbar)
1119 hr = m_staticToolbar->KillPopupTimer();
1120 if (FAILED(hr))
1121 return hr;
1122
1123 if (m_SFToolbar)
1124 hr = m_SFToolbar->KillPopupTimer();
1125
1126 return hr;
1127 }
1128
1129 HRESULT CMenuBand::_MenuBarMouseDown(HWND hwnd, INT item, BOOL isLButton)
1130 {
1131 if (m_staticToolbar && m_staticToolbar->IsWindowOwner(hwnd) == S_OK)
1132 m_staticToolbar->MenuBarMouseDown(item, isLButton);
1133 if (m_SFToolbar && m_SFToolbar->IsWindowOwner(hwnd) == S_OK)
1134 m_SFToolbar->MenuBarMouseDown(item, isLButton);
1135 return S_OK;
1136 }
1137
1138 HRESULT CMenuBand::_MenuBarMouseUp(HWND hwnd, INT item, BOOL isLButton)
1139 {
1140 if (m_staticToolbar && m_staticToolbar->IsWindowOwner(hwnd) == S_OK)
1141 m_staticToolbar->MenuBarMouseUp(item, isLButton);
1142 if (m_SFToolbar && m_SFToolbar->IsWindowOwner(hwnd) == S_OK)
1143 m_SFToolbar->MenuBarMouseUp(item, isLButton);
1144 return S_OK;
1145 }
1146
1147 HRESULT CMenuBand::_HasSubMenu()
1148 {
1149 return m_popupBar ? S_OK : S_FALSE;
1150 }
1151
1152 HRESULT CMenuBand::AdjustForTheme(BOOL bFlatStyle)
1153 {
1154 return IUnknown_QueryServiceExec(m_site, SID_SMenuPopup, &CGID_MenuDeskBar, 4, bFlatStyle, NULL, NULL);
1155 }
1156
1157 HRESULT STDMETHODCALLTYPE CMenuBand::InvalidateItem(LPSMDATA psmd, DWORD dwFlags)
1158 {
1159 UNIMPLEMENTED;
1160 return S_OK;
1161 }
1162
1163 HRESULT STDMETHODCALLTYPE CMenuBand::GetState(LPSMDATA psmd)
1164 {
1165 UNIMPLEMENTED;
1166 return S_OK;
1167 }
1168
1169 HRESULT STDMETHODCALLTYPE CMenuBand::SetMenuToolbar(IUnknown *punk, DWORD dwFlags)
1170 {
1171 UNIMPLEMENTED;
1172 return S_OK;
1173 }
1174
1175 HRESULT STDMETHODCALLTYPE CMenuBand::ResizeBorderDW(LPCRECT prcBorder, IUnknown *punkToolbarSite, BOOL fReserved)
1176 {
1177 UNIMPLEMENTED;
1178 return S_OK;
1179 }
1180
1181 HRESULT STDMETHODCALLTYPE CMenuBand::ContextSensitiveHelp(BOOL fEnterMode)
1182 {
1183 UNIMPLEMENTED;
1184 return S_OK;
1185 }
1186
1187 HRESULT STDMETHODCALLTYPE CMenuBand::GetSubMenu(THIS)
1188 {
1189 UNIMPLEMENTED;
1190 return S_OK;
1191 }
1192
1193 HRESULT STDMETHODCALLTYPE CMenuBand::SetToolbar(THIS)
1194 {
1195 UNIMPLEMENTED;
1196 return S_OK;
1197 }
1198
1199 HRESULT STDMETHODCALLTYPE CMenuBand::SetMinWidth(THIS)
1200 {
1201 UNIMPLEMENTED;
1202 return S_OK;
1203 }
1204
1205 HRESULT STDMETHODCALLTYPE CMenuBand::SetNoBorder(THIS)
1206 {
1207 UNIMPLEMENTED;
1208 return S_OK;
1209 }
1210
1211 HRESULT STDMETHODCALLTYPE CMenuBand::SetTheme(THIS)
1212 {
1213 UNIMPLEMENTED;
1214 return S_OK;
1215 }
1216
1217 HRESULT STDMETHODCALLTYPE CMenuBand::GetTop(THIS)
1218 {
1219 UNIMPLEMENTED;
1220 return S_OK;
1221 }
1222
1223 HRESULT STDMETHODCALLTYPE CMenuBand::GetBottom(THIS)
1224 {
1225 UNIMPLEMENTED;
1226 return S_OK;
1227 }
1228
1229 HRESULT STDMETHODCALLTYPE CMenuBand::GetTracked(THIS)
1230 {
1231 UNIMPLEMENTED;
1232 return S_OK;
1233 }
1234
1235 HRESULT STDMETHODCALLTYPE CMenuBand::GetParentSite(THIS)
1236 {
1237 UNIMPLEMENTED;
1238 return S_OK;
1239 }
1240
1241 HRESULT STDMETHODCALLTYPE CMenuBand::GetState(THIS)
1242 {
1243 UNIMPLEMENTED;
1244 return S_OK;
1245 }
1246
1247 HRESULT STDMETHODCALLTYPE CMenuBand::DoDefaultAction(THIS)
1248 {
1249 UNIMPLEMENTED;
1250 return S_OK;
1251 }
1252
1253 HRESULT STDMETHODCALLTYPE CMenuBand::IsEmpty(THIS)
1254 {
1255 UNIMPLEMENTED;
1256 return S_OK;
1257 }
1258
1259 HRESULT STDMETHODCALLTYPE CMenuBand::HasFocusIO()
1260 {
1261 if (m_popupBar)
1262 return S_OK;
1263 return S_FALSE;
1264 }
1265
1266 HRESULT STDMETHODCALLTYPE CMenuBand::TranslateAcceleratorIO(LPMSG lpMsg)
1267 {
1268 // TODO: Alt down -> toggle menu focus
1269 return S_FALSE;
1270 }
1271
1272 HRESULT STDMETHODCALLTYPE CMenuBand::IsDirty()
1273 {
1274 UNIMPLEMENTED;
1275 return S_OK;
1276 }
1277
1278 HRESULT STDMETHODCALLTYPE CMenuBand::Load(IStream *pStm)
1279 {
1280 UNIMPLEMENTED;
1281 return S_OK;
1282 }
1283
1284 HRESULT STDMETHODCALLTYPE CMenuBand::Save(IStream *pStm, BOOL fClearDirty)
1285 {
1286 UNIMPLEMENTED;
1287 return S_OK;
1288 }
1289
1290 HRESULT STDMETHODCALLTYPE CMenuBand::GetSizeMax(ULARGE_INTEGER *pcbSize)
1291 {
1292 UNIMPLEMENTED;
1293 return S_OK;
1294 }
1295
1296 HRESULT STDMETHODCALLTYPE CMenuBand::GetClassID(CLSID *pClassID)
1297 {
1298 UNIMPLEMENTED;
1299 return S_OK;
1300 }
1301
1302 HRESULT STDMETHODCALLTYPE CMenuBand::QueryStatus(const GUID *pguidCmdGroup, ULONG cCmds, OLECMD prgCmds [], OLECMDTEXT *pCmdText)
1303 {
1304 UNIMPLEMENTED;
1305 return S_OK;
1306 }