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