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