[RSHELL]
[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, 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);
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 m_hotBar = toolbar;
608 m_hotItem = item;
609
610 if (m_hotBar == this)
611 {
612 if (dwFlags & HICF_MOUSE)
613 {
614 // Vertical menus show/hide the submenu after a delay,
615 // but horizontal menubars switch between items instantly,
616 // if they were open.
617 if (m_initFlags & SMINIT_VERTICAL)
618 {
619 DWORD elapsed = 0;
620 SystemParametersInfo(SPI_GETMENUSHOWDELAY, 0, &elapsed, 0);
621 SetTimer(m_hwndToolbar, TIMERID_HOTTRACK, elapsed, NULL);
622 m_timerEnabled = TRUE;
623 TRACE("SetTimer called with m_hotItem=%d\n", m_hotItem);
624 }
625 else if (m_isTrackingPopup)
626 {
627 // If the menubar has an open submenu, switch to the new item's submenu immediately
628 PopupItem(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)
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 m_isTrackingPopup = wasTracking;
708 return m_menuBand->_ChangeHotItem(this, btn.idCommand, HICF_MOUSE);
709 }
710
711 HRESULT CMenuToolbarBase::PopupSubMenu(UINT iItem, UINT index, IShellMenu* childShellMenu)
712 {
713 // Calculate the submenu position and exclude area
714 RECT rc = { 0 };
715 RECT rcx = { 0 };
716
717 if (!SendMessage(m_hwndToolbar, TB_GETITEMRECT, index, reinterpret_cast<LPARAM>(&rc)))
718 return E_FAIL;
719
720 GetWindowRect(m_hwnd, &rcx);
721
722 POINT a = { rc.left, rc.top };
723 POINT b = { rc.right, rc.bottom };
724 POINT c = { rcx.left, rcx.top };
725 POINT d = { rcx.right, rcx.bottom };
726
727 ClientToScreen(m_hwndToolbar, &a);
728 ClientToScreen(m_hwndToolbar, &b);
729 ClientToScreen(m_hwnd, &c);
730 ClientToScreen(m_hwnd, &d);
731
732 POINTL pt = { a.x, b.y };
733 RECTL rcl = { c.x, c.y, d.x, d.y };
734
735 if (m_initFlags & SMINIT_VERTICAL)
736 {
737 pt.x = b.x - 3;
738 pt.y = a.y - 3;
739 }
740
741 // Display the submenu
742 m_isTrackingPopup = TRUE;
743
744 m_menuBand->_ChangePopupItem(this, iItem);
745 m_menuBand->_OnPopupSubMenu(childShellMenu, &pt, &rcl);
746
747 return S_OK;
748 }
749
750 HRESULT CMenuToolbarBase::PopupSubMenu(UINT iItem, UINT index, HMENU menu)
751 {
752 // Calculate the submenu position and exclude area
753 RECT rc = { 0 };
754
755 if (!SendMessage(m_hwndToolbar, TB_GETITEMRECT, index, reinterpret_cast<LPARAM>(&rc)))
756 return E_FAIL;
757
758 POINT a = { rc.left, rc.top };
759 POINT b = { rc.right, rc.bottom };
760
761 ClientToScreen(m_hwndToolbar, &a);
762 ClientToScreen(m_hwndToolbar, &b);
763
764 POINT pt = { a.x, b.y };
765 RECT rcl = { a.x, a.y, b.x, b.y };
766
767 if (m_initFlags & SMINIT_VERTICAL)
768 {
769 pt.x = b.x;
770 pt.y = a.y;
771 }
772
773 HMENU popup = GetSubMenu(menu, index);
774
775 // Display the submenu
776 m_isTrackingPopup = TRUE;
777 m_menuBand->_ChangePopupItem(this, iItem);
778 m_menuBand->_TrackSubMenu(popup, pt.x, pt.y, rcl);
779 m_menuBand->_ChangePopupItem(NULL, -1);
780 m_isTrackingPopup = FALSE;
781
782 m_menuBand->_ChangeHotItem(NULL, -1, 0);
783
784 return S_OK;
785 }
786
787 HRESULT CMenuToolbarBase::TrackContextMenu(IContextMenu* contextMenu, POINT pt)
788 {
789 // Cancel submenus
790 m_menuBand->_KillPopupTimers();
791 if (m_popupBar)
792 m_menuBand->_CancelCurrentPopup();
793
794 // Display the context menu
795 return m_menuBand->_TrackContextMenu(contextMenu, pt.x, pt.y);
796 }
797
798 HRESULT CMenuToolbarBase::OnCommand(WPARAM wParam, LPARAM lParam, LRESULT *theResult)
799 {
800 if (m_disableMouseTrack)
801 {
802 *theResult = 1;
803 TRACE("Item click prevented by DisableMouseTrack\n");
804 return S_OK;
805 }
806
807 // If a button is clicked while a submenu was open, cancel the submenu.
808 if (!(m_initFlags & SMINIT_VERTICAL) && m_isTrackingPopup)
809 {
810 TRACE("OnCommand cancelled because it was tracking submenu.\n");
811 return S_FALSE;
812 }
813
814 *theResult = 0;
815
816 m_menuBand->_KillPopupTimers();
817
818 if (PopupItem(wParam) == S_OK)
819 {
820 TRACE("PopupItem returned S_OK\n");
821 return S_FALSE;
822 }
823
824 TRACE("Executing...\n");
825
826 HRESULT hr = m_menuBand->_MenuItemHotTrack(MPOS_EXECUTE);
827
828 if (FAILED_UNEXPECTEDLY(hr))
829 return hr;
830
831 INT iItem = wParam;
832 INT index;
833 DWORD_PTR data;
834
835 GetDataFromId(iItem, &index, &data);
836
837 return InternalExecuteItem(iItem, index, data);
838 }
839
840 HRESULT CMenuToolbarBase::OnContextMenu(NMMOUSE * rclick)
841 {
842 INT iItem = rclick->dwItemSpec;
843 INT index = rclick->dwHitInfo;
844 DWORD_PTR data = rclick->dwItemData;
845
846 GetDataFromId(iItem, &index, &data);
847
848 return InternalContextMenu(iItem, index, data, rclick->pt);
849 }
850
851 HRESULT CMenuToolbarBase::KeyboardItemChange(DWORD dwSelectType)
852 {
853 int prev = m_hotItem;
854 int index = -1;
855
856 if (dwSelectType != 0xFFFFFFFF)
857 {
858 int count = SendMessage(m_hwndToolbar, TB_BUTTONCOUNT, 0, 0);
859
860 if (dwSelectType == VK_HOME)
861 {
862 index = 0;
863 dwSelectType = VK_DOWN;
864 }
865 else if (dwSelectType == VK_END)
866 {
867 index = count - 1;
868 dwSelectType = VK_UP;
869 }
870 else
871 {
872 if (m_hotItem >= 0)
873 {
874 TBBUTTONINFO info = { 0 };
875 info.cbSize = sizeof(TBBUTTONINFO);
876 info.dwMask = 0;
877 index = SendMessage(m_hwndToolbar, TB_GETBUTTONINFO, m_hotItem, reinterpret_cast<LPARAM>(&info));
878 }
879
880 if (index < 0)
881 {
882 if (dwSelectType == VK_UP)
883 {
884 index = count - 1;
885 }
886 else if (dwSelectType == VK_DOWN)
887 {
888 index = 0;
889 }
890 }
891 else
892 {
893 if (dwSelectType == VK_UP)
894 {
895 index--;
896 }
897 else if (dwSelectType == VK_DOWN)
898 {
899 index++;
900 }
901 }
902 }
903
904 TBBUTTON btn = { 0 };
905 while (index >= 0 && index < count)
906 {
907 DWORD res = SendMessage(m_hwndToolbar, TB_GETBUTTON, index, reinterpret_cast<LPARAM>(&btn));
908 if (!res)
909 return E_FAIL;
910
911 if (btn.dwData)
912 {
913 if (prev != btn.idCommand)
914 {
915 TRACE("Setting Hot item to %d\n", index);
916 m_menuBand->_ChangeHotItem(this, btn.idCommand, 0);
917 }
918 return S_OK;
919 }
920
921 if (dwSelectType == VK_UP)
922 {
923 index--;
924 }
925 else if (dwSelectType == VK_DOWN)
926 {
927 index++;
928 }
929 }
930
931 return S_FALSE;
932 }
933
934 if (prev != -1)
935 {
936 TRACE("Setting Hot item to null\n");
937 m_menuBand->_ChangeHotItem(NULL, -1, 0);
938 }
939
940 return S_FALSE;
941 }
942
943 HRESULT CMenuToolbarBase::AddButton(DWORD commandId, LPCWSTR caption, BOOL hasSubMenu, INT iconId, DWORD_PTR buttonData, BOOL last)
944 {
945 TBBUTTON tbb = { 0 };
946
947 tbb.fsState = TBSTATE_ENABLED;
948 #if !USE_TBSTYLE_EX_VERTICAL
949 if (!last && (m_initFlags & SMINIT_VERTICAL))
950 tbb.fsState |= TBSTATE_WRAP;
951 #endif
952 tbb.fsStyle = BTNS_CHECKGROUP;
953
954 if (hasSubMenu && (m_initFlags & SMINIT_VERTICAL))
955 tbb.fsStyle |= BTNS_DROPDOWN;
956
957 if (!(m_initFlags & SMINIT_VERTICAL))
958 tbb.fsStyle |= BTNS_AUTOSIZE;
959
960 tbb.iString = (INT_PTR) caption;
961 tbb.idCommand = commandId;
962
963 tbb.iBitmap = iconId;
964 tbb.dwData = buttonData;
965
966 if (!SendMessageW(m_hwndToolbar, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&tbb)))
967 return HRESULT_FROM_WIN32(GetLastError());
968
969 return S_OK;
970 }
971
972 HRESULT CMenuToolbarBase::AddSeparator(BOOL last)
973 {
974 TBBUTTON tbb = { 0 };
975
976 tbb.fsState = TBSTATE_ENABLED;
977 #if !USE_TBSTYLE_EX_VERTICAL
978 if (!last && (m_initFlags & SMINIT_VERTICAL))
979 tbb.fsState |= TBSTATE_WRAP;
980 #endif
981 tbb.fsStyle = BTNS_SEP;
982 tbb.iBitmap = 0;
983
984 if (!SendMessageW(m_hwndToolbar, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&tbb)))
985 return HRESULT_FROM_WIN32(GetLastError());
986
987 return S_OK;
988 }
989
990 HRESULT CMenuToolbarBase::AddPlaceholder()
991 {
992 TBBUTTON tbb = { 0 };
993 PCWSTR MenuString = L"(Empty)";
994
995 tbb.fsState = 0;
996 tbb.fsStyle = 0;
997 tbb.iString = (INT_PTR) MenuString;
998 tbb.iBitmap = -1;
999
1000 if (!SendMessageW(m_hwndToolbar, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&tbb)))
1001 return HRESULT_FROM_WIN32(GetLastError());
1002
1003 return S_OK;
1004 }
1005
1006 HRESULT CMenuToolbarBase::ClearToolbar()
1007 {
1008 while (SendMessage(m_hwndToolbar, TB_DELETEBUTTON, 0, 0))
1009 {
1010 // empty;
1011 }
1012 return S_OK;
1013 }
1014
1015 HRESULT CMenuToolbarBase::GetDataFromId(INT iItem, INT* pIndex, DWORD_PTR* pData)
1016 {
1017 if (pData)
1018 *pData = NULL;
1019
1020 if (pIndex)
1021 *pIndex = -1;
1022
1023 if (iItem < 0)
1024 return S_OK;
1025
1026 TBBUTTONINFO info = { 0 };
1027
1028 info.cbSize = sizeof(TBBUTTONINFO);
1029 info.dwMask = TBIF_COMMAND | TBIF_LPARAM;
1030
1031 int index = SendMessage(m_hwndToolbar, TB_GETBUTTONINFO, iItem, reinterpret_cast<LPARAM>(&info));
1032 if (index < 0)
1033 return E_FAIL;
1034
1035 if (pIndex)
1036 *pIndex = index;
1037
1038 if (pData)
1039 *pData = info.lParam;
1040
1041 return S_OK;
1042 }
1043
1044 HRESULT CMenuToolbarBase::CancelCurrentPopup()
1045 {
1046 return m_menuBand->_CancelCurrentPopup();
1047 }
1048
1049 HRESULT CMenuToolbarBase::PopupItem(INT iItem)
1050 {
1051 INT index;
1052 DWORD_PTR dwData;
1053
1054 if (iItem < 0)
1055 return S_OK;
1056
1057 if (m_popupBar == this && m_popupItem == iItem)
1058 return S_OK;
1059
1060 GetDataFromId(iItem, &index, &dwData);
1061
1062 HRESULT hr = InternalHasSubMenu(iItem, index, dwData);
1063 if (hr != S_OK)
1064 return hr;
1065
1066 if (m_popupBar)
1067 {
1068 HRESULT hr = CancelCurrentPopup();
1069 if (FAILED_UNEXPECTEDLY(hr))
1070 return hr;
1071 }
1072
1073 if (!(m_initFlags & SMINIT_VERTICAL))
1074 {
1075 TRACE("PopupItem non-vertical %d %d\n", index, iItem);
1076 m_menuBand->_ChangeHotItem(this, iItem, 0);
1077 }
1078
1079 return InternalPopupItem(iItem, index, dwData);
1080 }
1081
1082 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand *menuBand) :
1083 CMenuToolbarBase(menuBand, FALSE),
1084 m_hmenu(NULL)
1085 {
1086 }
1087
1088 HRESULT CMenuStaticToolbar::GetMenu(
1089 _Out_opt_ HMENU *phmenu,
1090 _Out_opt_ HWND *phwnd,
1091 _Out_opt_ DWORD *pdwFlags)
1092 {
1093 if (phmenu)
1094 *phmenu = m_hmenu;
1095 if (phwnd)
1096 *phwnd = NULL;
1097 if (pdwFlags)
1098 *pdwFlags = m_dwMenuFlags;
1099
1100 return S_OK;
1101 }
1102
1103 HRESULT CMenuStaticToolbar::SetMenu(
1104 HMENU hmenu,
1105 HWND hwnd,
1106 DWORD dwFlags)
1107 {
1108 m_hmenu = hmenu;
1109 m_dwMenuFlags = dwFlags;
1110
1111 return S_OK;
1112 }
1113
1114 HRESULT CMenuStaticToolbar::FillToolbar(BOOL clearFirst)
1115 {
1116 int i;
1117 int ic = GetMenuItemCount(m_hmenu);
1118
1119 if (clearFirst)
1120 {
1121 ClearToolbar();
1122 }
1123
1124 int count = 0;
1125 for (i = 0; i < ic; i++)
1126 {
1127 BOOL last = i + 1 == ic;
1128
1129 MENUITEMINFOW info;
1130
1131 info.cbSize = sizeof(info);
1132 info.dwTypeData = NULL;
1133 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_ID;
1134
1135 if (!GetMenuItemInfoW(m_hmenu, i, TRUE, &info))
1136 {
1137 TRACE("Error obtaining info for menu item at pos=%d\n", i);
1138 continue;
1139 }
1140
1141 count++;
1142
1143 if (info.fType & MFT_SEPARATOR)
1144 {
1145 AddSeparator(last);
1146 }
1147 else if (!(info.fType & MFT_BITMAP))
1148 {
1149
1150 info.cch++;
1151 info.dwTypeData = (PWSTR) HeapAlloc(GetProcessHeap(), 0, (info.cch + 1) * sizeof(WCHAR));
1152
1153 info.fMask = MIIM_STRING | MIIM_SUBMENU | MIIM_ID;
1154 GetMenuItemInfoW(m_hmenu, i, TRUE, &info);
1155
1156 SMINFO * sminfo = new SMINFO();
1157 sminfo->dwMask = SMIM_ICON | SMIM_FLAGS;
1158 // FIXME: remove before deleting the toolbar or it will leak
1159
1160 HRESULT hr = m_menuBand->_CallCBWithItemId(info.wID, SMC_GETINFO, 0, reinterpret_cast<LPARAM>(sminfo));
1161 if (FAILED_UNEXPECTEDLY(hr))
1162 return hr;
1163
1164 AddButton(info.wID, info.dwTypeData, info.hSubMenu != NULL, sminfo->iIcon, reinterpret_cast<DWORD_PTR>(sminfo), last);
1165
1166 HeapFree(GetProcessHeap(), 0, info.dwTypeData);
1167 }
1168 }
1169
1170 return S_OK;
1171 }
1172
1173 HRESULT CMenuStaticToolbar::InternalGetTooltip(INT iItem, INT index, DWORD_PTR dwData, LPWSTR pszText, INT cchTextMax)
1174 {
1175 //SMINFO * info = reinterpret_cast<SMINFO*>(dwData);
1176 UNIMPLEMENTED;
1177 return E_NOTIMPL;
1178 }
1179
1180 HRESULT CMenuStaticToolbar::OnDeletingButton(const NMTOOLBAR * tb)
1181 {
1182 delete reinterpret_cast<SMINFO*>(tb->tbButton.dwData);
1183 return S_OK;
1184 }
1185
1186 HRESULT CMenuStaticToolbar::InternalContextMenu(INT iItem, INT index, DWORD_PTR dwData, POINT pt)
1187 {
1188 CComPtr<IContextMenu> contextMenu;
1189 HRESULT hr = m_menuBand->_CallCBWithItemId(iItem, SMC_GETOBJECT,
1190 reinterpret_cast<WPARAM>(&IID_IContextMenu), reinterpret_cast<LPARAM>(&contextMenu));
1191 if (hr != S_OK)
1192 return hr;
1193
1194 return TrackContextMenu(contextMenu, pt);
1195 }
1196
1197 HRESULT CMenuStaticToolbar::InternalExecuteItem(INT iItem, INT index, DWORD_PTR data)
1198 {
1199 return m_menuBand->_CallCBWithItemId(iItem, SMC_EXEC, 0, 0);
1200 }
1201
1202 HRESULT CMenuStaticToolbar::InternalPopupItem(INT iItem, INT index, DWORD_PTR dwData)
1203 {
1204 SMINFO * nfo = reinterpret_cast<SMINFO*>(dwData);
1205 if (!nfo)
1206 return E_FAIL;
1207
1208 if (nfo->dwFlags&SMIF_TRACKPOPUP)
1209 {
1210 return PopupSubMenu(iItem, index, m_hmenu);
1211 }
1212 else
1213 {
1214 CComPtr<IShellMenu> shellMenu;
1215 HRESULT hr = m_menuBand->_CallCBWithItemId(iItem, SMC_GETOBJECT, reinterpret_cast<WPARAM>(&IID_IShellMenu), reinterpret_cast<LPARAM>(&shellMenu));
1216 if (FAILED_UNEXPECTEDLY(hr))
1217 return hr;
1218
1219 return PopupSubMenu(iItem, index, shellMenu);
1220 }
1221 }
1222
1223 HRESULT CMenuStaticToolbar::InternalHasSubMenu(INT iItem, INT index, DWORD_PTR dwData)
1224 {
1225 return ::GetSubMenu(m_hmenu, index) ? S_OK : S_FALSE;
1226 }
1227
1228 CMenuSFToolbar::CMenuSFToolbar(CMenuBand * menuBand) :
1229 CMenuToolbarBase(menuBand, TRUE),
1230 m_shellFolder(NULL),
1231 m_idList(NULL),
1232 m_hKey(NULL)
1233 {
1234 }
1235
1236 CMenuSFToolbar::~CMenuSFToolbar()
1237 {
1238 }
1239
1240 HRESULT CMenuSFToolbar::FillToolbar(BOOL clearFirst)
1241 {
1242 HRESULT hr;
1243 int i = 0;
1244 PWSTR MenuString;
1245
1246 IEnumIDList * eidl;
1247 m_shellFolder->EnumObjects(GetToolbar(), SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &eidl);
1248
1249 LPITEMIDLIST item = static_cast<LPITEMIDLIST>(CoTaskMemAlloc(sizeof(ITEMIDLIST)));
1250 ULONG fetched;
1251 hr = eidl->Next(1, &item, &fetched);
1252 while (SUCCEEDED(hr) && fetched > 0)
1253 {
1254 INT index = 0;
1255 INT indexOpen = 0;
1256
1257 STRRET sr = { STRRET_CSTR, { 0 } };
1258
1259 hr = m_shellFolder->GetDisplayNameOf(item, SIGDN_NORMALDISPLAY, &sr);
1260 if (FAILED_UNEXPECTEDLY(hr))
1261 return hr;
1262
1263 StrRetToStr(&sr, NULL, &MenuString);
1264
1265 index = SHMapPIDLToSystemImageListIndex(m_shellFolder, item, &indexOpen);
1266
1267 LPCITEMIDLIST itemc = item;
1268
1269 SFGAOF attrs = SFGAO_FOLDER;
1270 hr = m_shellFolder->GetAttributesOf(1, &itemc, &attrs);
1271
1272 DWORD_PTR dwData = reinterpret_cast<DWORD_PTR>(ILClone(item));
1273
1274 // Fetch next item already, so we know if the current one is the last
1275 hr = eidl->Next(1, &item, &fetched);
1276
1277 AddButton(++i, MenuString, attrs & SFGAO_FOLDER, index, dwData, FAILED(hr) || fetched == 0);
1278
1279 CoTaskMemFree(MenuString);
1280 }
1281 CoTaskMemFree(item);
1282
1283 // If no items were added, show the "empty" placeholder
1284 if (i == 0)
1285 {
1286 return AddPlaceholder();
1287 }
1288
1289 return hr;
1290 }
1291
1292 HRESULT CMenuSFToolbar::InternalGetTooltip(INT iItem, INT index, DWORD_PTR dwData, LPWSTR pszText, INT cchTextMax)
1293 {
1294 //ITEMIDLIST * pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1295 UNIMPLEMENTED;
1296 return E_NOTIMPL;
1297 }
1298
1299 HRESULT CMenuSFToolbar::OnDeletingButton(const NMTOOLBAR * tb)
1300 {
1301 ILFree(reinterpret_cast<LPITEMIDLIST>(tb->tbButton.dwData));
1302 return S_OK;
1303 }
1304
1305 HRESULT CMenuSFToolbar::SetShellFolder(IShellFolder *psf, LPCITEMIDLIST pidlFolder, HKEY hKey, DWORD dwFlags)
1306 {
1307 m_shellFolder = psf;
1308 m_idList = ILClone(pidlFolder);
1309 m_hKey = hKey;
1310 m_dwMenuFlags = dwFlags;
1311 return S_OK;
1312 }
1313
1314 HRESULT CMenuSFToolbar::GetShellFolder(DWORD *pdwFlags, LPITEMIDLIST *ppidl, REFIID riid, void **ppv)
1315 {
1316 HRESULT hr;
1317
1318 hr = m_shellFolder->QueryInterface(riid, ppv);
1319 if (FAILED_UNEXPECTEDLY(hr))
1320 return hr;
1321
1322 if (pdwFlags)
1323 *pdwFlags = m_dwMenuFlags;
1324
1325 if (ppidl)
1326 {
1327 LPITEMIDLIST pidl = NULL;
1328
1329 if (m_idList)
1330 {
1331 pidl = ILClone(m_idList);
1332 if (!pidl)
1333 {
1334 (*(IUnknown**) ppv)->Release();
1335 return E_FAIL;
1336 }
1337 }
1338
1339 *ppidl = pidl;
1340 }
1341
1342 return hr;
1343 }
1344
1345 HRESULT CMenuSFToolbar::InternalContextMenu(INT iItem, INT index, DWORD_PTR dwData, POINT pt)
1346 {
1347 HRESULT hr;
1348 CComPtr<IContextMenu> contextMenu = NULL;
1349 LPCITEMIDLIST pidl = reinterpret_cast<LPCITEMIDLIST>(dwData);
1350
1351 #define IID_NULL_PPV_ARG(Itype, ppType) IID_##Itype, NULL, reinterpret_cast<void**>((static_cast<Itype**>(ppType)))
1352
1353 hr = m_shellFolder->GetUIObjectOf(GetToolbar(), 1, &pidl, IID_NULL_PPV_ARG(IContextMenu, &contextMenu));
1354 if (FAILED_UNEXPECTEDLY(hr))
1355 {
1356 return hr;
1357 }
1358
1359 hr = TrackContextMenu(contextMenu, pt);
1360
1361 return hr;
1362 }
1363
1364 HRESULT CMenuSFToolbar::InternalExecuteItem(INT iItem, INT index, DWORD_PTR data)
1365 {
1366 return m_menuBand->_CallCBWithItemPidl(reinterpret_cast<LPITEMIDLIST>(data), SMC_SFEXEC, 0, 0);
1367 }
1368
1369 HRESULT CMenuSFToolbar::InternalPopupItem(INT iItem, INT index, DWORD_PTR dwData)
1370 {
1371 HRESULT hr;
1372 UINT uId;
1373 UINT uIdAncestor;
1374 DWORD flags;
1375 CComPtr<IShellMenuCallback> psmc;
1376 CComPtr<IShellMenu> shellMenu;
1377
1378 LPITEMIDLIST pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1379
1380 if (!pidl)
1381 return E_FAIL;
1382
1383 hr = CMenuBand_Constructor(IID_PPV_ARG(IShellMenu, &shellMenu));
1384 if (FAILED_UNEXPECTEDLY(hr))
1385 return hr;
1386
1387 m_menuBand->GetMenuInfo(&psmc, &uId, &uIdAncestor, &flags);
1388
1389 // FIXME: not sure what to use as uId/uIdAncestor here
1390 hr = shellMenu->Initialize(psmc, 0, uId, SMINIT_VERTICAL);
1391 if (FAILED_UNEXPECTEDLY(hr))
1392 return hr;
1393
1394 CComPtr<IShellFolder> childFolder;
1395 hr = m_shellFolder->BindToObject(pidl, NULL, IID_PPV_ARG(IShellFolder, &childFolder));
1396 if (FAILED_UNEXPECTEDLY(hr))
1397 return hr;
1398
1399 hr = shellMenu->SetShellFolder(childFolder, NULL, NULL, 0);
1400 if (FAILED_UNEXPECTEDLY(hr))
1401 return hr;
1402
1403 return PopupSubMenu(iItem, index, shellMenu);
1404 }
1405
1406 HRESULT CMenuSFToolbar::InternalHasSubMenu(INT iItem, INT index, DWORD_PTR dwData)
1407 {
1408 HRESULT hr;
1409 LPCITEMIDLIST pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1410
1411 SFGAOF attrs = SFGAO_FOLDER;
1412 hr = m_shellFolder->GetAttributesOf(1, &pidl, &attrs);
1413 if (FAILED_UNEXPECTEDLY(hr))
1414 return hr;
1415
1416 return (attrs & SFGAO_FOLDER) ? S_OK : S_FALSE;
1417 }