[SHELL32]
[reactos.git] / reactos / dll / win32 / shell32 / shellmenu / CMenuToolbars.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 <commoncontrols.h>
22 #include <shlwapi_undoc.h>
23 #include <uxtheme.h>
24 #include <vssym32.h>
25
26 #include "CMenuBand.h"
27 #include "CMenuToolbars.h"
28
29 #define IDS_MENU_EMPTY 34561
30
31 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
32 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
33
34 WINE_DEFAULT_DEBUG_CHANNEL(CMenuToolbars);
35
36 // FIXME: Enable if/when wine comctl supports this flag properly
37 #define USE_TBSTYLE_EX_VERTICAL 0
38
39 // User-defined timer ID used while hot-tracking around the menu
40 #define TIMERID_HOTTRACK 1
41
42 LRESULT CMenuToolbarBase::OnWinEventWrap(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
43 {
44 LRESULT lr;
45 bHandled = OnWinEvent(m_hWnd, uMsg, wParam, lParam, &lr) != S_FALSE;
46 return lr;
47 }
48
49 HRESULT CMenuToolbarBase::OnWinEvent(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *theResult)
50 {
51 NMHDR * hdr;
52
53 *theResult = 0;
54 switch (uMsg)
55 {
56 case WM_COMMAND:
57 //return OnCommand(wParam, lParam, theResult);
58 return S_OK;
59
60 case WM_NOTIFY:
61 hdr = reinterpret_cast<LPNMHDR>(lParam);
62 switch (hdr->code)
63 {
64 case TBN_DELETINGBUTTON:
65 return OnDeletingButton(reinterpret_cast<LPNMTOOLBAR>(hdr));
66
67 case PGN_CALCSIZE:
68 return OnPagerCalcSize(reinterpret_cast<LPNMPGCALCSIZE>(hdr));
69
70 case TBN_DROPDOWN:
71 return ProcessClick(reinterpret_cast<LPNMTOOLBAR>(hdr)->iItem);
72
73 case TBN_HOTITEMCHANGE:
74 //return OnHotItemChange(reinterpret_cast<LPNMTBHOTITEM>(hdr), theResult);
75 return S_OK;
76
77 case NM_CUSTOMDRAW:
78 return OnCustomDraw(reinterpret_cast<LPNMTBCUSTOMDRAW>(hdr), theResult);
79
80 case TBN_GETINFOTIP:
81 return OnGetInfoTip(reinterpret_cast<LPNMTBGETINFOTIP>(hdr));
82
83 // Silence unhandled items so that they don't print as unknown
84 case RBN_CHILDSIZE:
85 return S_OK;
86
87 case TTN_GETDISPINFO:
88 return S_OK;
89
90 case NM_RELEASEDCAPTURE:
91 break;
92
93 case NM_CLICK:
94 case NM_RDOWN:
95 case NM_LDOWN:
96 break;
97
98 case TBN_GETDISPINFO:
99 break;
100
101 case TBN_BEGINDRAG:
102 case TBN_ENDDRAG:
103 break;
104
105 case NM_TOOLTIPSCREATED:
106 break;
107
108 // Unknown
109 case -714: return S_FALSE;
110
111 default:
112 TRACE("WM_NOTIFY unknown code %d, %d\n", hdr->code, hdr->idFrom);
113 return S_OK;
114 }
115 return S_FALSE;
116 case WM_WININICHANGE:
117 if (wParam == SPI_SETFLATMENU)
118 {
119 SystemParametersInfo(SPI_GETFLATMENU, 0, &m_useFlatMenus, 0);
120 }
121 }
122
123 return S_FALSE;
124 }
125
126 HRESULT CMenuToolbarBase::DisableMouseTrack(BOOL bDisable)
127 {
128 if (m_disableMouseTrack != bDisable)
129 {
130 m_disableMouseTrack = bDisable;
131 TRACE("DisableMouseTrack %d\n", bDisable);
132 }
133 return S_OK;
134 }
135
136 HRESULT CMenuToolbarBase::OnPagerCalcSize(LPNMPGCALCSIZE csize)
137 {
138 SIZE tbs;
139 GetSizes(NULL, &tbs, NULL);
140 if (csize->dwFlag == PGF_CALCHEIGHT)
141 {
142 csize->iHeight = tbs.cy;
143 }
144 else if (csize->dwFlag == PGF_CALCWIDTH)
145 {
146 csize->iWidth = tbs.cx;
147 }
148 return S_OK;
149 }
150
151 HRESULT CMenuToolbarBase::OnCustomDraw(LPNMTBCUSTOMDRAW cdraw, LRESULT * theResult)
152 {
153 bool isHot, isPopup;
154 TBBUTTONINFO btni;
155
156 switch (cdraw->nmcd.dwDrawStage)
157 {
158 case CDDS_PREPAINT:
159 *theResult = CDRF_NOTIFYITEMDRAW;
160 return S_OK;
161
162 case CDDS_ITEMPREPAINT:
163
164 // The item with an active submenu gets the CHECKED flag.
165 isHot = m_hotBar == this && (int) cdraw->nmcd.dwItemSpec == m_hotItem;
166 isPopup = m_popupBar == this && (int) cdraw->nmcd.dwItemSpec == m_popupItem;
167
168 if (m_hotItem < 0 && isPopup)
169 isHot = TRUE;
170
171 if ((m_useFlatMenus && isHot) || (m_initFlags & SMINIT_VERTICAL))
172 {
173 COLORREF clrText;
174 HBRUSH bgBrush;
175 RECT rc = cdraw->nmcd.rc;
176 HDC hdc = cdraw->nmcd.hdc;
177
178 // Remove HOT and CHECKED flags (will restore HOT if necessary)
179 cdraw->nmcd.uItemState &= ~(CDIS_HOT | CDIS_CHECKED);
180
181 // Decide on the colors
182 if (isHot)
183 {
184 cdraw->nmcd.uItemState |= CDIS_HOT;
185
186 clrText = GetSysColor(COLOR_HIGHLIGHTTEXT);
187 bgBrush = GetSysColorBrush(m_useFlatMenus ? COLOR_MENUHILIGHT : COLOR_HIGHLIGHT);
188 }
189 else
190 {
191 clrText = GetSysColor(COLOR_MENUTEXT);
192 bgBrush = GetSysColorBrush(COLOR_MENU);
193 }
194
195 // Paint the background color with the selected color
196 FillRect(hdc, &rc, bgBrush);
197
198 // Set the text color in advance, this color will be assigned when the ITEMPOSTPAINT triggers
199 SetTextColor(hdc, clrText);
200
201 // Set the text color, will be used by the internal drawing code
202 cdraw->clrText = clrText;
203 cdraw->iListGap += 4;
204
205 // Tell the default drawing code we don't want any fanciness, not even a background.
206 *theResult = CDRF_NOTIFYPOSTPAINT | TBCDRF_NOBACKGROUND | TBCDRF_NOEDGES | TBCDRF_NOOFFSET | TBCDRF_NOMARK | 0x00800000; // FIXME: the last bit is Vista+, useful for debugging only
207 }
208 else
209 {
210 // Remove HOT and CHECKED flags (will restore HOT if necessary)
211 cdraw->nmcd.uItemState &= ~CDIS_HOT;
212
213 // Decide on the colors
214 if (isHot)
215 {
216 cdraw->nmcd.uItemState |= CDIS_HOT;
217 }
218
219 *theResult = 0;
220 }
221
222 return S_OK;
223
224 case CDDS_ITEMPOSTPAINT:
225
226 // Fetch the button style
227 btni.cbSize = sizeof(btni);
228 btni.dwMask = TBIF_STYLE;
229 GetButtonInfo(cdraw->nmcd.dwItemSpec, &btni);
230
231 // Check if we need to draw a submenu arrow
232 if (btni.fsStyle & BTNS_DROPDOWN)
233 {
234 // TODO: Support RTL text modes by drawing a leftwards arrow aligned to the left of the control
235
236 // "8" is the rightwards dropdown arrow in the Marlett font
237 WCHAR text [] = L"8";
238
239 // Configure the font to draw with Marlett, keeping the current background color as-is
240 SelectObject(cdraw->nmcd.hdc, m_marlett);
241 SetBkMode(cdraw->nmcd.hdc, TRANSPARENT);
242
243 // Tweak the alignment by 1 pixel so the menu draws like the Windows start menu.
244 RECT rc = cdraw->nmcd.rc;
245 rc.right += 1;
246
247 // The arrow is drawn at the right of the item's rect, aligned vertically.
248 DrawTextEx(cdraw->nmcd.hdc, text, 1, &rc, DT_NOCLIP | DT_VCENTER | DT_RIGHT | DT_SINGLELINE, NULL);
249 }
250 *theResult = TRUE;
251 return S_OK;
252 }
253 return S_OK;
254 }
255
256 CMenuToolbarBase::CMenuToolbarBase(CMenuBand *menuBand, BOOL usePager) :
257 m_pager(this, 1),
258 m_useFlatMenus(FALSE),
259 m_SubclassOld(NULL),
260 m_disableMouseTrack(FALSE),
261 m_timerEnabled(FALSE),
262 m_menuBand(menuBand),
263 m_dwMenuFlags(0),
264 m_hasSizes(FALSE),
265 m_usePager(usePager),
266 m_hotBar(NULL),
267 m_hotItem(-1),
268 m_popupBar(NULL),
269 m_popupItem(-1),
270 m_isTrackingPopup(FALSE),
271 m_cancelingPopup(FALSE)
272 {
273 m_idealSize.cx = 0;
274 m_idealSize.cy = 0;
275 m_itemSize.cx = 0;
276 m_itemSize.cy = 0;
277 m_marlett = CreateFont(
278 0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET,
279 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
280 DEFAULT_QUALITY, FF_DONTCARE, L"Marlett");
281 }
282
283 CMenuToolbarBase::~CMenuToolbarBase()
284 {
285 if (m_hWnd)
286 DestroyWindow();
287
288 if (m_pager.m_hWnd)
289 m_pager.DestroyWindow();
290
291 DeleteObject(m_marlett);
292 }
293
294 void CMenuToolbarBase::InvalidateDraw()
295 {
296 InvalidateRect(NULL, FALSE);
297 }
298
299 HRESULT CMenuToolbarBase::ShowDW(BOOL fShow)
300 {
301 ShowWindow(fShow ? SW_SHOW : SW_HIDE);
302
303 // Ensure that the right image list is assigned to the toolbar
304 UpdateImageLists();
305
306 // For custom-drawing
307 SystemParametersInfo(SPI_GETFLATMENU, 0, &m_useFlatMenus, 0);
308
309 return S_OK;
310 }
311
312 HRESULT CMenuToolbarBase::UpdateImageLists()
313 {
314 if ((m_initFlags & (SMINIT_TOPLEVEL | SMINIT_VERTICAL)) == SMINIT_TOPLEVEL) // not vertical.
315 {
316 // No image list, prevents the buttons from having a margin at the left side
317 SetImageList(NULL);
318 return S_OK;
319 }
320
321 // Assign the correct imagelist and padding based on the current icon size
322
323 int shiml;
324 if (m_menuBand->UseBigIcons())
325 {
326 shiml = SHIL_LARGE;
327 SetPadding(4, 0);
328 }
329 else
330 {
331 shiml = SHIL_SMALL;
332 SetPadding(4, 4);
333 }
334
335 IImageList * piml;
336 HRESULT hr = SHGetImageList(shiml, IID_PPV_ARG(IImageList, &piml));
337 if (FAILED_UNEXPECTEDLY(hr))
338 {
339 SetImageList(NULL);
340 }
341 else
342 {
343 SetImageList((HIMAGELIST)piml);
344 }
345 return S_OK;
346 }
347
348 HRESULT CMenuToolbarBase::Close()
349 {
350 if (m_hWnd)
351 DestroyWindow();
352
353 if (m_pager.m_hWnd)
354 m_pager.DestroyWindow();
355
356 return S_OK;
357 }
358
359 HRESULT CMenuToolbarBase::CreateToolbar(HWND hwndParent, DWORD dwFlags)
360 {
361 LONG tbStyles = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
362 TBSTYLE_TOOLTIPS | TBSTYLE_TRANSPARENT | TBSTYLE_REGISTERDROP | TBSTYLE_LIST | TBSTYLE_FLAT | TBSTYLE_CUSTOMERASE |
363 CCS_NODIVIDER | CCS_NOPARENTALIGN | CCS_NORESIZE | CCS_TOP;
364 LONG tbExStyles = TBSTYLE_EX_DOUBLEBUFFER | WS_EX_TOOLWINDOW;
365
366 if (dwFlags & SMINIT_VERTICAL)
367 {
368 // Activate vertical semantics
369 tbStyles |= CCS_VERT;
370
371 #if USE_TBSTYLE_EX_VERTICAL
372 tbExStyles |= TBSTYLE_EX_VERTICAL;
373 #endif
374 }
375
376 m_initFlags = dwFlags;
377
378 // Get a temporary rect to use while creating the toolbar window.
379 // Ensure that it is not a null rect.
380 RECT rc;
381 if (!::GetClientRect(hwndParent, &rc) ||
382 (rc.left == rc.right) ||
383 (rc.top == rc.bottom))
384 {
385 rc.left = 0;
386 rc.top = 0;
387 rc.right = 1;
388 rc.bottom = 1;
389 }
390
391 SubclassWindow(CToolbar::Create(hwndParent, tbStyles, tbExStyles));
392
393 SetWindowTheme(m_hWnd, L"", L"");
394
395 SystemParametersInfo(SPI_GETFLATMENU, 0, &m_useFlatMenus, 0);
396
397 m_menuBand->AdjustForTheme(m_useFlatMenus);
398
399 // If needed, create the pager.
400 if (m_usePager)
401 {
402 LONG pgStyles = PGS_VERT | WS_CHILD | WS_VISIBLE;
403 LONG pgExStyles = 0;
404
405 HWND hwndPager = CreateWindowEx(
406 pgExStyles, WC_PAGESCROLLER, NULL,
407 pgStyles, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
408 hwndParent, NULL, _AtlBaseModule.GetModuleInstance(), 0);
409
410 m_pager.SubclassWindow(hwndPager);
411
412 ::SetParent(m_hWnd, hwndPager);
413
414 m_pager.SendMessageW(PGM_SETCHILD, 0, reinterpret_cast<LPARAM>(m_hWnd));
415 }
416
417 // Configure the image lists
418 UpdateImageLists();
419
420 return S_OK;
421 }
422
423 HRESULT CMenuToolbarBase::GetSizes(SIZE* pMinSize, SIZE* pMaxSize, SIZE* pIntegralSize)
424 {
425 if (pMinSize)
426 *pMinSize = m_idealSize;
427 if (pMaxSize)
428 *pMaxSize = m_idealSize;
429 if (pIntegralSize)
430 *pIntegralSize = m_itemSize;
431
432 if (m_hasSizes)
433 return S_OK;
434
435 TRACE("Sizes out of date, recalculating.\n");
436
437 if (!m_hWnd)
438 {
439 return S_OK;
440 }
441
442 // Obtain the ideal size, to be used as min and max
443 GetMaxSize(&m_idealSize);
444 GetIdealSize((m_initFlags & SMINIT_VERTICAL) != 0, &m_idealSize);
445
446 TRACE("Ideal Size: (%d, %d) for %d buttons\n", m_idealSize, GetButtonCount());
447
448 // Obtain the button size, to be used as the integral size
449 DWORD size = GetButtonSize();
450 m_itemSize.cx = GET_X_LPARAM(size);
451 m_itemSize.cy = GET_Y_LPARAM(size);
452 m_hasSizes = TRUE;
453
454 if (pMinSize)
455 *pMinSize = m_idealSize;
456 if (pMaxSize)
457 *pMaxSize = m_idealSize;
458 if (pIntegralSize)
459 *pIntegralSize = m_itemSize;
460
461 return S_OK;
462 }
463
464 HRESULT CMenuToolbarBase::SetPosSize(int x, int y, int cx, int cy)
465 {
466 // Update the toolbar or pager to fit the requested rect
467 // If we have a pager, set the toolbar height to the ideal height of the toolbar
468 if (m_pager.m_hWnd)
469 {
470 SetWindowPos(NULL, x, y, cx, m_idealSize.cy, 0);
471 m_pager.SetWindowPos(NULL, x, y, cx, cy, 0);
472 }
473 else
474 {
475 SetWindowPos(NULL, x, y, cx, cy, 0);
476 }
477
478 // In a vertical menu, resize the buttons to fit the width
479 if (m_initFlags & SMINIT_VERTICAL)
480 {
481 DWORD btnSize = GetButtonSize();
482 SetButtonSize(cx, GET_Y_LPARAM(btnSize));
483 }
484
485 return S_OK;
486 }
487
488 HRESULT CMenuToolbarBase::IsWindowOwner(HWND hwnd)
489 {
490 if (m_hWnd && m_hWnd == hwnd) return S_OK;
491 if (m_pager.m_hWnd && m_pager.m_hWnd == hwnd) return S_OK;
492 return S_FALSE;
493 }
494
495 HRESULT CMenuToolbarBase::GetWindow(HWND *phwnd)
496 {
497 if (!phwnd)
498 return E_FAIL;
499
500 if (m_pager.m_hWnd)
501 *phwnd = m_pager.m_hWnd;
502 else
503 *phwnd = m_hWnd;
504
505 return S_OK;
506 }
507
508 HRESULT CMenuToolbarBase::OnGetInfoTip(NMTBGETINFOTIP * tip)
509 {
510 INT index;
511 DWORD_PTR dwData;
512
513 INT iItem = tip->iItem;
514
515 GetDataFromId(iItem, &index, &dwData);
516
517 return InternalGetTooltip(iItem, index, dwData, tip->pszText, tip->cchTextMax);
518 }
519
520 HRESULT CMenuToolbarBase::OnPopupTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
521 {
522 if (wParam != TIMERID_HOTTRACK)
523 {
524 bHandled = FALSE;
525 return 0;
526 }
527
528 KillTimer(TIMERID_HOTTRACK);
529
530 if (!m_timerEnabled)
531 return 0;
532
533 m_timerEnabled = FALSE;
534
535 if (m_hotItem < 0)
536 return 0;
537
538 // Returns S_FALSE if the current item did not show a submenu
539 HRESULT hr = PopupItem(m_hotItem, FALSE);
540 if (hr != S_FALSE)
541 return 0;
542
543 // If we didn't switch submenus, cancel the current popup regardless
544 if (m_popupBar)
545 {
546 HRESULT hr = CancelCurrentPopup();
547 if (FAILED_UNEXPECTEDLY(hr))
548 return 0;
549 }
550
551 return 0;
552 }
553
554 HRESULT CMenuToolbarBase::KillPopupTimer()
555 {
556 if (m_timerEnabled)
557 {
558 m_timerEnabled = FALSE;
559 KillTimer(TIMERID_HOTTRACK);
560 return S_OK;
561 }
562 return S_FALSE;
563 }
564
565 HRESULT CMenuToolbarBase::ChangeHotItem(CMenuToolbarBase * toolbar, INT item, DWORD dwFlags)
566 {
567 // Ignore the change if it already matches the stored info
568 if (m_hotBar == toolbar && m_hotItem == item)
569 return S_FALSE;
570
571 // Prevent a change of hot item if the change was triggered by the mouse,
572 // and mouse tracking is disabled.
573 if (m_disableMouseTrack && dwFlags & HICF_MOUSE)
574 {
575 TRACE("Hot item change prevented by DisableMouseTrack\n");
576 return S_OK;
577 }
578
579 // Notify the toolbar if the hot-tracking left this toolbar
580 if (m_hotBar == this && toolbar != this)
581 {
582 SetHotItem(-1);
583 }
584
585 TRACE("Hot item changed from %p %p, to %p %p\n", m_hotBar, m_hotItem, toolbar, item);
586 m_hotBar = toolbar;
587 m_hotItem = item;
588
589 if (m_hotBar == this)
590 {
591 if (m_isTrackingPopup && !(m_initFlags & SMINIT_VERTICAL))
592 {
593 // If the menubar has an open submenu, switch to the new item's submenu immediately
594 PopupItem(m_hotItem, FALSE);
595 }
596 else if (dwFlags & HICF_MOUSE)
597 {
598 // Vertical menus show/hide the submenu after a delay,
599 // but only with the mouse.
600 if (m_initFlags & SMINIT_VERTICAL)
601 {
602 DWORD elapsed = 0;
603 SystemParametersInfo(SPI_GETMENUSHOWDELAY, 0, &elapsed, 0);
604 SetTimer(TIMERID_HOTTRACK, elapsed);
605 m_timerEnabled = TRUE;
606 TRACE("SetTimer called with m_hotItem=%d\n", m_hotItem);
607 }
608 }
609 else
610 {
611 TBBUTTONINFO info;
612 info.cbSize = sizeof(info);
613 info.dwMask = 0;
614
615 int index = GetButtonInfo(item, &info);
616
617 SetHotItem(index);
618 }
619 }
620
621 InvalidateDraw();
622 return S_OK;
623 }
624
625 HRESULT CMenuToolbarBase::ChangePopupItem(CMenuToolbarBase * toolbar, INT item)
626 {
627 // Ignore the change if it already matches the stored info
628 if (m_popupBar == toolbar && m_popupItem == item)
629 return S_FALSE;
630
631 // Notify the toolbar if the popup-tracking this toolbar
632 if (m_popupBar == this && toolbar != this)
633 {
634 CheckButton(m_popupItem, FALSE);
635 m_isTrackingPopup = FALSE;
636 }
637
638 m_popupBar = toolbar;
639 m_popupItem = item;
640
641 if (m_popupBar == this)
642 {
643 CheckButton(m_popupItem, TRUE);
644 }
645
646 InvalidateDraw();
647 return S_OK;
648 }
649
650 LRESULT CMenuToolbarBase::IsTrackedItem(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
651 {
652 TBBUTTON btn;
653 INT idx = (INT)wParam;
654
655 if (m_hotBar != this)
656 return S_FALSE;
657
658 if (idx < 0)
659 return S_FALSE;
660
661 if (!GetButton(idx, &btn))
662 return E_FAIL;
663
664 if (m_hotItem == btn.idCommand)
665 return S_OK;
666
667 if (m_popupItem == btn.idCommand)
668 return S_OK;
669
670 return S_FALSE;
671 }
672
673 LRESULT CMenuToolbarBase::ChangeTrackedItem(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
674 {
675 TBBUTTON btn;
676 BOOL wasTracking = LOWORD(lParam);
677 BOOL mouse = HIWORD(lParam);
678 INT idx = (INT)wParam;
679
680 if (idx < 0)
681 {
682 m_isTrackingPopup = FALSE;
683 return m_menuBand->_ChangeHotItem(NULL, -1, HICF_MOUSE);
684 }
685
686 if (!GetButton(idx, &btn))
687 return E_FAIL;
688
689 TRACE("ChangeTrackedItem %d, %d\n", idx, wasTracking);
690 m_isTrackingPopup = wasTracking;
691 return m_menuBand->_ChangeHotItem(this, btn.idCommand, mouse ? HICF_MOUSE : 0);
692 }
693
694 HRESULT CMenuToolbarBase::PopupSubMenu(UINT iItem, UINT index, IShellMenu* childShellMenu, BOOL keyInitiated)
695 {
696 // Calculate the submenu position and exclude area
697 RECT rc = { 0 };
698
699 if (!GetItemRect(index, &rc))
700 return E_FAIL;
701
702 POINT a = { rc.left, rc.top };
703 POINT b = { rc.right, rc.bottom };
704
705 ClientToScreen(&a);
706 ClientToScreen(&b);
707
708 POINTL pt = { a.x, b.y };
709 RECTL rcl = { a.x, a.y, b.x, b.y };
710
711 if (m_initFlags & SMINIT_VERTICAL)
712 {
713 // FIXME: Hardcoding this here feels hacky.
714 if (IsAppThemed())
715 {
716 pt.x = b.x - 1;
717 pt.y = a.y - 1;
718 }
719 else
720 {
721 pt.x = b.x - 3;
722 pt.y = a.y - 3;
723 }
724 }
725
726 // Display the submenu
727 m_isTrackingPopup = TRUE;
728
729 m_menuBand->_ChangePopupItem(this, iItem);
730 m_menuBand->_OnPopupSubMenu(childShellMenu, &pt, &rcl, keyInitiated);
731
732 return S_OK;
733 }
734
735 HRESULT CMenuToolbarBase::PopupSubMenu(UINT iItem, UINT index, HMENU menu)
736 {
737 // Calculate the submenu position and exclude area
738 RECT rc = { 0 };
739
740 if (!GetItemRect(index, &rc))
741 return E_FAIL;
742
743 POINT a = { rc.left, rc.top };
744 POINT b = { rc.right, rc.bottom };
745
746 ClientToScreen(&a);
747 ClientToScreen(&b);
748
749 POINT pt = { a.x, b.y };
750 RECT rcl = { a.x, a.y, b.x, b.y };
751
752 if (m_initFlags & SMINIT_VERTICAL)
753 {
754 pt.x = b.x;
755 pt.y = a.y;
756 }
757
758 HMENU popup = GetSubMenu(menu, index);
759
760 // Display the submenu
761 m_isTrackingPopup = TRUE;
762 m_menuBand->_ChangePopupItem(this, iItem);
763 m_menuBand->_TrackSubMenu(popup, pt.x, pt.y, rcl);
764 m_menuBand->_ChangePopupItem(NULL, -1);
765 m_isTrackingPopup = FALSE;
766
767 return S_OK;
768 }
769
770 HRESULT CMenuToolbarBase::TrackContextMenu(IContextMenu* contextMenu, POINT pt)
771 {
772 // Cancel submenus
773 m_menuBand->_KillPopupTimers();
774 if (m_popupBar)
775 m_menuBand->_CancelCurrentPopup();
776
777 // Display the context menu
778 return m_menuBand->_TrackContextMenu(contextMenu, pt.x, pt.y);
779 }
780
781 HRESULT CMenuToolbarBase::BeforeCancelPopup()
782 {
783 m_cancelingPopup = TRUE;
784 TRACE("BeforeCancelPopup\n");
785 return S_OK;
786 }
787
788 HRESULT CMenuToolbarBase::ProcessClick(INT iItem)
789 {
790 if (m_disableMouseTrack)
791 {
792 TRACE("Item click prevented by DisableMouseTrack\n");
793 return S_OK;
794 }
795
796 // If a button is clicked while a submenu was open, cancel the submenu.
797 if (!(m_initFlags & SMINIT_VERTICAL) && m_isTrackingPopup)
798 {
799 TRACE("OnCommand cancelled because it was tracking submenu.\n");
800 return S_FALSE;
801 }
802
803 if (PopupItem(iItem, FALSE) == S_OK)
804 {
805 TRACE("PopupItem returned S_OK\n");
806 return S_FALSE;
807 }
808
809 TRACE("Executing...\n");
810
811 return m_menuBand->_MenuItemSelect(MPOS_EXECUTE);
812 }
813
814 HRESULT CMenuToolbarBase::ProcessContextMenu(INT iItem)
815 {
816 INT index;
817 DWORD_PTR data;
818
819 GetDataFromId(iItem, &index, &data);
820
821 DWORD pos = GetMessagePos();
822 POINT pt = { GET_X_LPARAM(pos), GET_Y_LPARAM(pos) };
823
824 return InternalContextMenu(iItem, index, data, pt);
825 }
826
827 HRESULT CMenuToolbarBase::MenuBarMouseDown(INT iIndex, BOOL isLButton)
828 {
829 TBBUTTON btn;
830
831 GetButton(iIndex, &btn);
832
833 if ((m_initFlags & SMINIT_VERTICAL)
834 || m_popupBar
835 || m_cancelingPopup)
836 {
837 m_cancelingPopup = FALSE;
838 return S_OK;
839 }
840
841 return ProcessClick(btn.idCommand);
842 }
843
844 HRESULT CMenuToolbarBase::MenuBarMouseUp(INT iIndex, BOOL isLButton)
845 {
846 TBBUTTON btn;
847
848 m_cancelingPopup = FALSE;
849
850 if (!(m_initFlags & SMINIT_VERTICAL))
851 return S_OK;
852
853 GetButton(iIndex, &btn);
854
855 if (isLButton)
856 return ProcessClick(btn.idCommand);
857 else
858 return ProcessContextMenu(btn.idCommand);
859 }
860
861 HRESULT CMenuToolbarBase::PrepareExecuteItem(INT iItem)
862 {
863 this->m_menuBand->_KillPopupTimers();
864
865 m_executeItem = iItem;
866 return GetDataFromId(iItem, &m_executeIndex, &m_executeData);
867 }
868
869 HRESULT CMenuToolbarBase::ExecuteItem()
870 {
871 return InternalExecuteItem(m_executeItem, m_executeItem, m_executeData);
872 }
873
874 HRESULT CMenuToolbarBase::KeyboardItemChange(DWORD dwSelectType)
875 {
876 int prev = m_hotItem;
877 int index = -1;
878
879 if (dwSelectType != 0xFFFFFFFF)
880 {
881 int count = GetButtonCount();
882
883 if (dwSelectType == VK_HOME)
884 {
885 index = 0;
886 dwSelectType = VK_DOWN;
887 }
888 else if (dwSelectType == VK_END)
889 {
890 index = count - 1;
891 dwSelectType = VK_UP;
892 }
893 else
894 {
895 if (m_hotItem >= 0)
896 {
897 TBBUTTONINFO info = { 0 };
898 info.cbSize = sizeof(TBBUTTONINFO);
899 info.dwMask = 0;
900 index = GetButtonInfo(m_hotItem, &info);
901 }
902
903 if (index < 0)
904 {
905 if (dwSelectType == VK_UP)
906 {
907 index = count - 1;
908 }
909 else if (dwSelectType == VK_DOWN)
910 {
911 index = 0;
912 }
913 }
914 else
915 {
916 if (dwSelectType == VK_UP)
917 {
918 index--;
919 }
920 else if (dwSelectType == VK_DOWN)
921 {
922 index++;
923 }
924 }
925 }
926
927 TBBUTTON btn = { 0 };
928 while (index >= 0 && index < count)
929 {
930 DWORD res = GetButton(index, &btn);
931 if (!res)
932 return E_FAIL;
933
934 if (btn.dwData)
935 {
936 if (prev != btn.idCommand)
937 {
938 TRACE("Setting Hot item to %d\n", index);
939 if (!(m_initFlags & SMINIT_VERTICAL) && m_isTrackingPopup)
940 {
941 HWND tlw;
942 m_menuBand->_GetTopLevelWindow(&tlw);
943 SendMessageW(tlw, WM_CANCELMODE, 0, 0);
944 PostMessageW(WM_USER_CHANGETRACKEDITEM, index, MAKELPARAM(m_isTrackingPopup, FALSE));
945 }
946 else
947 m_menuBand->_ChangeHotItem(this, btn.idCommand, 0);
948 }
949 return S_OK;
950 }
951
952 if (dwSelectType == VK_UP)
953 {
954 index--;
955 }
956 else if (dwSelectType == VK_DOWN)
957 {
958 index++;
959 }
960 }
961
962 return S_FALSE;
963 }
964
965 if (prev != -1)
966 {
967 TRACE("Setting Hot item to null\n");
968 m_menuBand->_ChangeHotItem(NULL, -1, 0);
969 }
970
971 return S_FALSE;
972 }
973
974 HRESULT CMenuToolbarBase::AddButton(DWORD commandId, LPCWSTR caption, BOOL hasSubMenu, INT iconId, DWORD_PTR buttonData, BOOL last)
975 {
976 TBBUTTON tbb = { 0 };
977
978 tbb.fsState = TBSTATE_ENABLED;
979 #if !USE_TBSTYLE_EX_VERTICAL
980 if (!last && (m_initFlags & SMINIT_VERTICAL))
981 tbb.fsState |= TBSTATE_WRAP;
982 #endif
983 tbb.fsStyle = BTNS_CHECKGROUP;
984
985 if (hasSubMenu && (m_initFlags & SMINIT_VERTICAL))
986 tbb.fsStyle |= BTNS_DROPDOWN;
987
988 if (!(m_initFlags & SMINIT_VERTICAL))
989 tbb.fsStyle |= BTNS_AUTOSIZE;
990
991 tbb.iString = (INT_PTR) caption;
992 tbb.idCommand = commandId;
993
994 tbb.iBitmap = iconId;
995 tbb.dwData = buttonData;
996
997 m_hasSizes = FALSE;
998
999 if (!AddButtons(1, &tbb))
1000 return HRESULT_FROM_WIN32(GetLastError());
1001 return S_OK;
1002 }
1003
1004 HRESULT CMenuToolbarBase::AddSeparator(BOOL last)
1005 {
1006 TBBUTTON tbb = { 0 };
1007
1008 tbb.fsState = TBSTATE_ENABLED;
1009 #if !USE_TBSTYLE_EX_VERTICAL
1010 if (!last && (m_initFlags & SMINIT_VERTICAL))
1011 tbb.fsState |= TBSTATE_WRAP;
1012 #endif
1013 tbb.fsStyle = BTNS_SEP;
1014 tbb.iBitmap = 0;
1015
1016 m_hasSizes = FALSE;
1017
1018 if (!AddButtons(1, &tbb))
1019 return HRESULT_FROM_WIN32(GetLastError());
1020
1021 return S_OK;
1022 }
1023
1024 HRESULT CMenuToolbarBase::AddPlaceholder()
1025 {
1026 TBBUTTON tbb = { 0 };
1027 WCHAR MenuString[128];
1028
1029 LoadStringW(GetModuleHandle(L"shell32.dll"), IDS_MENU_EMPTY, MenuString, _countof(MenuString));
1030
1031 tbb.fsState = 0;
1032 tbb.fsStyle = 0;
1033 tbb.iString = (INT_PTR) MenuString;
1034 tbb.iBitmap = -1;
1035
1036 m_hasSizes = FALSE;
1037
1038 if (!AddButtons(1, &tbb))
1039 return HRESULT_FROM_WIN32(GetLastError());
1040
1041 return S_OK;
1042 }
1043
1044 HRESULT CMenuToolbarBase::ClearToolbar()
1045 {
1046 while (DeleteButton(0))
1047 {
1048 // empty;
1049 }
1050 m_hasSizes = FALSE;
1051 return S_OK;
1052 }
1053
1054 HRESULT CMenuToolbarBase::GetDataFromId(INT iItem, INT* pIndex, DWORD_PTR* pData)
1055 {
1056 if (pData)
1057 *pData = NULL;
1058
1059 if (pIndex)
1060 *pIndex = -1;
1061
1062 if (iItem < 0)
1063 return S_OK;
1064
1065 TBBUTTONINFO info = { 0 };
1066
1067 info.cbSize = sizeof(TBBUTTONINFO);
1068 info.dwMask = TBIF_COMMAND | TBIF_LPARAM;
1069
1070 int index = GetButtonInfo(iItem, &info);
1071 if (index < 0)
1072 return E_FAIL;
1073
1074 if (pIndex)
1075 *pIndex = index;
1076
1077 if (pData)
1078 *pData = info.lParam;
1079
1080 return S_OK;
1081 }
1082
1083 HRESULT CMenuToolbarBase::CancelCurrentPopup()
1084 {
1085 return m_menuBand->_CancelCurrentPopup();
1086 }
1087
1088 HRESULT CMenuToolbarBase::PopupItem(INT iItem, BOOL keyInitiated)
1089 {
1090 INT index;
1091 DWORD_PTR dwData;
1092
1093 if (iItem < 0)
1094 return S_OK;
1095
1096 if (m_popupBar == this && m_popupItem == iItem)
1097 return S_OK;
1098
1099 GetDataFromId(iItem, &index, &dwData);
1100
1101 HRESULT hr = InternalHasSubMenu(iItem, index, dwData);
1102 if (hr != S_OK)
1103 return hr;
1104
1105 if (m_popupBar)
1106 {
1107 HRESULT hr = CancelCurrentPopup();
1108 if (FAILED_UNEXPECTEDLY(hr))
1109 return hr;
1110 }
1111
1112 if (!(m_initFlags & SMINIT_VERTICAL))
1113 {
1114 TRACE("PopupItem non-vertical %d %d\n", index, iItem);
1115 m_menuBand->_ChangeHotItem(this, iItem, 0);
1116 }
1117
1118 return InternalPopupItem(iItem, index, dwData, keyInitiated);
1119 }
1120
1121 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand *menuBand) :
1122 CMenuToolbarBase(menuBand, FALSE),
1123 m_hmenu(NULL),
1124 m_hwndMenu(NULL)
1125 {
1126 }
1127
1128 CMenuStaticToolbar::~CMenuStaticToolbar()
1129 {
1130 }
1131
1132 HRESULT CMenuStaticToolbar::GetMenu(
1133 _Out_opt_ HMENU *phmenu,
1134 _Out_opt_ HWND *phwnd,
1135 _Out_opt_ DWORD *pdwFlags)
1136 {
1137 if (phmenu)
1138 *phmenu = m_hmenu;
1139 if (phwnd)
1140 *phwnd = m_hwndMenu;
1141 if (pdwFlags)
1142 *pdwFlags = m_dwMenuFlags;
1143
1144 return S_OK;
1145 }
1146
1147 HRESULT CMenuStaticToolbar::SetMenu(
1148 HMENU hmenu,
1149 HWND hwnd,
1150 DWORD dwFlags)
1151 {
1152 m_hmenu = hmenu;
1153 m_hwndMenu = hwnd;
1154 m_dwMenuFlags = dwFlags;
1155
1156 ClearToolbar();
1157
1158 return S_OK;
1159 }
1160
1161 HRESULT CMenuStaticToolbar::FillToolbar(BOOL clearFirst)
1162 {
1163 int i;
1164 int ic = GetMenuItemCount(m_hmenu);
1165
1166 if (clearFirst)
1167 {
1168 ClearToolbar();
1169 }
1170
1171 int count = 0;
1172 for (i = 0; i < ic; i++)
1173 {
1174 BOOL last = i + 1 == ic;
1175
1176 MENUITEMINFOW info;
1177
1178 info.cbSize = sizeof(info);
1179 info.dwTypeData = NULL;
1180 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_ID;
1181
1182 if (!GetMenuItemInfoW(m_hmenu, i, TRUE, &info))
1183 {
1184 TRACE("Error obtaining info for menu item at pos=%d\n", i);
1185 continue;
1186 }
1187
1188 count++;
1189
1190 if (info.fType & MFT_SEPARATOR)
1191 {
1192 AddSeparator(last);
1193 }
1194 else if (!(info.fType & MFT_BITMAP))
1195 {
1196 info.cch++;
1197 info.dwTypeData = (PWSTR) HeapAlloc(GetProcessHeap(), 0, (info.cch + 1) * sizeof(WCHAR));
1198
1199 info.fMask = MIIM_STRING | MIIM_SUBMENU | MIIM_ID;
1200 GetMenuItemInfoW(m_hmenu, i, TRUE, &info);
1201
1202 SMINFO * sminfo = new SMINFO();
1203 sminfo->dwMask = SMIM_ICON | SMIM_FLAGS;
1204
1205 HRESULT hr = m_menuBand->_CallCBWithItemId(info.wID, SMC_GETINFO, 0, reinterpret_cast<LPARAM>(sminfo));
1206 if (FAILED_UNEXPECTEDLY(hr))
1207 {
1208 delete sminfo;
1209 return hr;
1210 }
1211
1212 AddButton(info.wID, info.dwTypeData, info.hSubMenu != NULL, sminfo->iIcon, reinterpret_cast<DWORD_PTR>(sminfo), last);
1213
1214 HeapFree(GetProcessHeap(), 0, info.dwTypeData);
1215 }
1216 }
1217
1218 return S_OK;
1219 }
1220
1221 HRESULT CMenuStaticToolbar::InternalGetTooltip(INT iItem, INT index, DWORD_PTR dwData, LPWSTR pszText, INT cchTextMax)
1222 {
1223 //SMINFO * info = reinterpret_cast<SMINFO*>(dwData);
1224 UNIMPLEMENTED;
1225 return E_NOTIMPL;
1226 }
1227
1228 HRESULT CMenuStaticToolbar::OnDeletingButton(const NMTOOLBAR * tb)
1229 {
1230 delete reinterpret_cast<SMINFO*>(tb->tbButton.dwData);
1231 return S_OK;
1232 }
1233
1234 HRESULT CMenuStaticToolbar::InternalContextMenu(INT iItem, INT index, DWORD_PTR dwData, POINT pt)
1235 {
1236 CComPtr<IContextMenu> contextMenu;
1237 HRESULT hr = m_menuBand->_CallCBWithItemId(iItem, SMC_GETOBJECT,
1238 reinterpret_cast<WPARAM>(&IID_IContextMenu), reinterpret_cast<LPARAM>(&contextMenu));
1239 if (hr != S_OK)
1240 return hr;
1241
1242 return TrackContextMenu(contextMenu, pt);
1243 }
1244
1245 HRESULT CMenuStaticToolbar::InternalExecuteItem(INT iItem, INT index, DWORD_PTR data)
1246 {
1247 return m_menuBand->_CallCBWithItemId(iItem, SMC_EXEC, 0, 0);
1248 }
1249
1250 HRESULT CMenuStaticToolbar::InternalPopupItem(INT iItem, INT index, DWORD_PTR dwData, BOOL keyInitiated)
1251 {
1252 SMINFO * nfo = reinterpret_cast<SMINFO*>(dwData);
1253 if (!nfo)
1254 return E_FAIL;
1255
1256 if (nfo->dwFlags&SMIF_TRACKPOPUP)
1257 {
1258 return PopupSubMenu(iItem, index, m_hmenu);
1259 }
1260 else
1261 {
1262 CComPtr<IShellMenu> shellMenu;
1263 HRESULT hr = m_menuBand->_CallCBWithItemId(iItem, SMC_GETOBJECT, reinterpret_cast<WPARAM>(&IID_IShellMenu), reinterpret_cast<LPARAM>(&shellMenu));
1264 if (FAILED_UNEXPECTEDLY(hr))
1265 return hr;
1266
1267 return PopupSubMenu(iItem, index, shellMenu, keyInitiated);
1268 }
1269 }
1270
1271 HRESULT CMenuStaticToolbar::InternalHasSubMenu(INT iItem, INT index, DWORD_PTR dwData)
1272 {
1273 return ::GetSubMenu(m_hmenu, index) ? S_OK : S_FALSE;
1274 }
1275
1276 CMenuSFToolbar::CMenuSFToolbar(CMenuBand * menuBand) :
1277 CMenuToolbarBase(menuBand, TRUE),
1278 m_shellFolder(NULL),
1279 m_idList(NULL),
1280 m_hKey(NULL)
1281 {
1282 }
1283
1284 CMenuSFToolbar::~CMenuSFToolbar()
1285 {
1286 }
1287
1288 int CALLBACK PidlListSort(void* item1, void* item2, LPARAM lParam)
1289 {
1290 IShellFolder * psf = (IShellFolder*) lParam;
1291 PCUIDLIST_RELATIVE pidl1 = (PCUIDLIST_RELATIVE) item1;
1292 PCUIDLIST_RELATIVE pidl2 = (PCUIDLIST_RELATIVE) item2;
1293 HRESULT hr = psf->CompareIDs(0, pidl1, pidl2);
1294 if (FAILED(hr))
1295 {
1296 // No way to cancel, so sort to equal.
1297 return 0;
1298 }
1299 return (int)(short)LOWORD(hr);
1300 }
1301
1302 HRESULT CMenuSFToolbar::FillToolbar(BOOL clearFirst)
1303 {
1304 HRESULT hr;
1305
1306 CComPtr<IEnumIDList> eidl;
1307 hr = m_shellFolder->EnumObjects(GetToolbar(), SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &eidl);
1308 if (FAILED_UNEXPECTEDLY(hr))
1309 return hr;
1310
1311 HDPA dpaSort = DPA_Create(10);
1312
1313 LPITEMIDLIST item = NULL;
1314 hr = eidl->Next(1, &item, NULL);
1315 while (hr == S_OK)
1316 {
1317 if (m_menuBand->_CallCBWithItemPidl(item, 0x10000000, 0, 0) == S_FALSE)
1318 {
1319 DPA_AppendPtr(dpaSort, ILClone(item));
1320 }
1321
1322 hr = eidl->Next(1, &item, NULL);
1323 }
1324
1325 // If no items were added, show the "empty" placeholder
1326 if (DPA_GetPtrCount(dpaSort) == 0)
1327 {
1328 DPA_Destroy(dpaSort);
1329 return AddPlaceholder();
1330 }
1331
1332 TRACE("FillToolbar added %d items to the DPA\n", DPA_GetPtrCount(dpaSort));
1333
1334 DPA_Sort(dpaSort, PidlListSort, (LPARAM) m_shellFolder.p);
1335
1336 for (int i = 0; i<DPA_GetPtrCount(dpaSort);)
1337 {
1338 PWSTR MenuString;
1339
1340 INT index = 0;
1341 INT indexOpen = 0;
1342
1343 STRRET sr = { STRRET_CSTR, { 0 } };
1344
1345 item = (LPITEMIDLIST)DPA_GetPtr(dpaSort, i);
1346
1347 hr = m_shellFolder->GetDisplayNameOf(item, SIGDN_NORMALDISPLAY, &sr);
1348 if (FAILED_UNEXPECTEDLY(hr))
1349 {
1350 DPA_Destroy(dpaSort);
1351 return hr;
1352 }
1353
1354 StrRetToStr(&sr, NULL, &MenuString);
1355
1356 index = SHMapPIDLToSystemImageListIndex(m_shellFolder, item, &indexOpen);
1357
1358 LPCITEMIDLIST itemc = item;
1359
1360 SFGAOF attrs = SFGAO_FOLDER;
1361 hr = m_shellFolder->GetAttributesOf(1, &itemc, &attrs);
1362
1363 DWORD_PTR dwData = reinterpret_cast<DWORD_PTR>(item);
1364
1365 // Fetch next item already, so we know if the current one is the last
1366 i++;
1367
1368 AddButton(i, MenuString, attrs & SFGAO_FOLDER, index, dwData, i >= DPA_GetPtrCount(dpaSort));
1369
1370 CoTaskMemFree(MenuString);
1371 }
1372
1373 DPA_Destroy(dpaSort);
1374 return hr;
1375 }
1376
1377 HRESULT CMenuSFToolbar::InternalGetTooltip(INT iItem, INT index, DWORD_PTR dwData, LPWSTR pszText, INT cchTextMax)
1378 {
1379 //ITEMIDLIST * pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1380 UNIMPLEMENTED;
1381 return E_NOTIMPL;
1382 }
1383
1384 HRESULT CMenuSFToolbar::OnDeletingButton(const NMTOOLBAR * tb)
1385 {
1386 ILFree(reinterpret_cast<LPITEMIDLIST>(tb->tbButton.dwData));
1387 return S_OK;
1388 }
1389
1390 HRESULT CMenuSFToolbar::SetShellFolder(IShellFolder *psf, LPCITEMIDLIST pidlFolder, HKEY hKey, DWORD dwFlags)
1391 {
1392 m_shellFolder = psf;
1393 m_idList = ILClone(pidlFolder);
1394 m_hKey = hKey;
1395 m_dwMenuFlags = dwFlags;
1396
1397 ClearToolbar();
1398
1399 return S_OK;
1400 }
1401
1402 HRESULT CMenuSFToolbar::GetShellFolder(DWORD *pdwFlags, LPITEMIDLIST *ppidl, REFIID riid, void **ppv)
1403 {
1404 HRESULT hr;
1405
1406 hr = m_shellFolder->QueryInterface(riid, ppv);
1407 if (FAILED_UNEXPECTEDLY(hr))
1408 return hr;
1409
1410 if (pdwFlags)
1411 *pdwFlags = m_dwMenuFlags;
1412
1413 if (ppidl)
1414 {
1415 LPITEMIDLIST pidl = NULL;
1416
1417 if (m_idList)
1418 {
1419 pidl = ILClone(m_idList);
1420 if (!pidl)
1421 {
1422 ERR("ILClone failed!\n");
1423 (*reinterpret_cast<IUnknown**>(ppv))->Release();
1424 return E_FAIL;
1425 }
1426 }
1427
1428 *ppidl = pidl;
1429 }
1430
1431 return hr;
1432 }
1433
1434 HRESULT CMenuSFToolbar::InternalContextMenu(INT iItem, INT index, DWORD_PTR dwData, POINT pt)
1435 {
1436 HRESULT hr;
1437 CComPtr<IContextMenu> contextMenu = NULL;
1438 LPCITEMIDLIST pidl = reinterpret_cast<LPCITEMIDLIST>(dwData);
1439
1440 hr = m_shellFolder->GetUIObjectOf(GetToolbar(), 1, &pidl, IID_NULL_PPV_ARG(IContextMenu, &contextMenu));
1441 if (FAILED_UNEXPECTEDLY(hr))
1442 {
1443 return hr;
1444 }
1445
1446 hr = TrackContextMenu(contextMenu, pt);
1447
1448 return hr;
1449 }
1450
1451 HRESULT CMenuSFToolbar::InternalExecuteItem(INT iItem, INT index, DWORD_PTR data)
1452 {
1453 return m_menuBand->_CallCBWithItemPidl(reinterpret_cast<LPITEMIDLIST>(data), SMC_SFEXEC, 0, 0);
1454 }
1455
1456 HRESULT CMenuSFToolbar::InternalPopupItem(INT iItem, INT index, DWORD_PTR dwData, BOOL keyInitiated)
1457 {
1458 HRESULT hr;
1459 UINT uId;
1460 UINT uIdAncestor;
1461 DWORD flags;
1462 CComPtr<IShellMenuCallback> psmc;
1463 CComPtr<IShellMenu> shellMenu;
1464
1465 LPITEMIDLIST pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1466
1467 if (!pidl)
1468 return E_FAIL;
1469
1470 hr = CMenuBand_CreateInstance(IID_PPV_ARG(IShellMenu, &shellMenu));
1471 if (FAILED_UNEXPECTEDLY(hr))
1472 return hr;
1473
1474 m_menuBand->GetMenuInfo(&psmc, &uId, &uIdAncestor, &flags);
1475
1476 // FIXME: not sure what to use as uId/uIdAncestor here
1477 hr = shellMenu->Initialize(psmc, 0, uId, SMINIT_VERTICAL);
1478 if (FAILED_UNEXPECTEDLY(hr))
1479 return hr;
1480
1481 CComPtr<IShellFolder> childFolder;
1482 hr = m_shellFolder->BindToObject(pidl, NULL, IID_PPV_ARG(IShellFolder, &childFolder));
1483 if (FAILED_UNEXPECTEDLY(hr))
1484 return hr;
1485
1486 hr = shellMenu->SetShellFolder(childFolder, NULL, NULL, 0);
1487 if (FAILED_UNEXPECTEDLY(hr))
1488 return hr;
1489
1490 return PopupSubMenu(iItem, index, shellMenu, keyInitiated);
1491 }
1492
1493 HRESULT CMenuSFToolbar::InternalHasSubMenu(INT iItem, INT index, DWORD_PTR dwData)
1494 {
1495 HRESULT hr;
1496 LPCITEMIDLIST pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1497
1498 SFGAOF attrs = SFGAO_FOLDER;
1499 hr = m_shellFolder->GetAttributesOf(1, &pidl, &attrs);
1500 if (FAILED_UNEXPECTEDLY(hr))
1501 return hr;
1502
1503 return (attrs & SFGAO_FOLDER) ? S_OK : S_FALSE;
1504 }