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