4 * Copyright 2014 David Quintana
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.
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.
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
22 #include <commoncontrols.h>
23 #include <shlwapi_undoc.h>
26 #include "CMenuBand.h"
27 #include "CMenuToolbars.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(CMenuToolbars
);
32 HRESULT WINAPI
SHGetImageList(
38 // FIXME: Enable if/when wine comctl supports this flag properly
39 #define USE_TBSTYLE_EX_VERTICAL 0
41 // User-defined timer ID used while hot-tracking around the menu
42 #define TIMERID_HOTTRACK 1
44 HRESULT
CMenuToolbarBase::OnWinEvent(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*theResult
)
52 //return OnCommand(wParam, lParam, theResult);
56 hdr
= reinterpret_cast<LPNMHDR
>(lParam
);
59 case TBN_DELETINGBUTTON
:
60 return OnDeletingButton(reinterpret_cast<LPNMTOOLBAR
>(hdr
));
63 return OnPagerCalcSize(reinterpret_cast<LPNMPGCALCSIZE
>(hdr
));
66 return ProcessClick(reinterpret_cast<LPNMTOOLBAR
>(hdr
)->iItem
);
68 case TBN_HOTITEMCHANGE
:
69 //return OnHotItemChange(reinterpret_cast<LPNMTBHOTITEM>(hdr), theResult);
73 return OnContextMenu(reinterpret_cast<LPNMMOUSE
>(hdr
));
76 return OnCustomDraw(reinterpret_cast<LPNMTBCUSTOMDRAW
>(hdr
), theResult
);
79 return OnGetInfoTip(reinterpret_cast<LPNMTBGETINFOTIP
>(hdr
));
81 // Silence unhandled items so that they don't print as unknown
88 case NM_RELEASEDCAPTURE
:
103 case NM_TOOLTIPSCREATED
:
107 case -714: return S_FALSE
;
110 TRACE("WM_NOTIFY unknown code %d, %d\n", hdr
->code
, hdr
->idFrom
);
119 LRESULT CALLBACK
CMenuToolbarBase::s_SubclassProc(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
121 CMenuToolbarBase
* pthis
= reinterpret_cast<CMenuToolbarBase
*>(GetWindowLongPtr(hWnd
, GWLP_USERDATA
));
122 return pthis
->SubclassProc(hWnd
, uMsg
, wParam
, lParam
);
125 LRESULT
CMenuToolbarBase::SubclassProc(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
131 case WM_USER_ISTRACKEDITEM
:
132 m_SubclassOld(hWnd
, uMsg
, wParam
, lParam
);
133 return IsTrackedItem(wParam
);
134 case WM_USER_CHANGETRACKEDITEM
:
135 m_SubclassOld(hWnd
, uMsg
, wParam
, lParam
);
136 return ChangeTrackedItem(wParam
, LOWORD(lParam
), HIWORD(lParam
));
139 OnWinEvent(hWnd
, uMsg
, wParam
, lParam
, &lr
);
142 OnWinEvent(hWnd
, uMsg
, wParam
, lParam
, &lr
);
145 OnPopupTimer(wParam
);
148 return m_SubclassOld(hWnd
, uMsg
, wParam
, lParam
);
151 HRESULT
CMenuToolbarBase::DisableMouseTrack(BOOL bDisable
)
153 if (m_disableMouseTrack
!= bDisable
)
155 m_disableMouseTrack
= bDisable
;
156 TRACE("DisableMouseTrack %d\n", bDisable
);
161 HRESULT
CMenuToolbarBase::OnPagerCalcSize(LPNMPGCALCSIZE csize
)
164 GetSizes(NULL
, &tbs
, NULL
);
165 if (csize
->dwFlag
== PGF_CALCHEIGHT
)
167 csize
->iHeight
= tbs
.cy
;
169 else if (csize
->dwFlag
== PGF_CALCWIDTH
)
171 csize
->iWidth
= tbs
.cx
;
176 HRESULT
CMenuToolbarBase::OnCustomDraw(LPNMTBCUSTOMDRAW cdraw
, LRESULT
* theResult
)
185 switch (cdraw
->nmcd
.dwDrawStage
)
188 *theResult
= CDRF_NOTIFYITEMDRAW
;
191 case CDDS_ITEMPREPAINT
:
194 hdc
= cdraw
->nmcd
.hdc
;
196 // The item with an active submenu gets the CHECKED flag.
197 isHot
= m_hotBar
== this && (int) cdraw
->nmcd
.dwItemSpec
== m_hotItem
;
198 isPopup
= m_popupBar
== this && (int) cdraw
->nmcd
.dwItemSpec
== m_popupItem
;
200 if (m_initFlags
& SMINIT_VERTICAL
|| IsAppThemed())
202 // Remove HOT and CHECKED flags (will restore HOT if necessary)
203 cdraw
->nmcd
.uItemState
&= ~(CDIS_HOT
| CDIS_CHECKED
);
205 // Decide on the colors
206 if (isHot
|| (m_hotItem
< 0 && isPopup
))
208 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
210 clrText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
211 bgBrush
= GetSysColorBrush(m_useFlatMenus
? COLOR_MENUHILIGHT
: COLOR_HIGHLIGHT
);
215 clrText
= GetSysColor(COLOR_MENUTEXT
);
216 bgBrush
= GetSysColorBrush(COLOR_MENU
);
219 // Paint the background color with the selected color
220 FillRect(hdc
, &rc
, bgBrush
);
222 // Set the text color in advance, this color will be assigned when the ITEMPOSTPAINT triggers
223 SetTextColor(hdc
, clrText
);
225 // Set the text color, will be used by the internal drawing code
226 cdraw
->clrText
= clrText
;
227 cdraw
->iListGap
+= 4;
229 // Tell the default drawing code we don't want any fanciness, not even a background.
230 *theResult
= CDRF_NOTIFYPOSTPAINT
| TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| 0x00800000; // FIXME: the last bit is Vista+, useful for debugging only
234 // Remove HOT and CHECKED flags (will restore HOT if necessary)
235 cdraw
->nmcd
.uItemState
&= ~CDIS_HOT
;
237 // Decide on the colors
238 if (isHot
|| (m_hotItem
< 0 && isPopup
))
240 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
248 case CDDS_ITEMPOSTPAINT
:
250 // Fetch the button style
251 btni
.cbSize
= sizeof(btni
);
252 btni
.dwMask
= TBIF_STYLE
;
253 SendMessage(m_hwndToolbar
, TB_GETBUTTONINFO
, cdraw
->nmcd
.dwItemSpec
, reinterpret_cast<LPARAM
>(&btni
));
255 // Check if we need to draw a submenu arrow
256 if (btni
.fsStyle
& BTNS_DROPDOWN
)
258 // TODO: Support RTL text modes by drawing a leftwards arrow aligned to the left of the control
260 // "8" is the rightwards dropdown arrow in the Marlett font
261 WCHAR text
[] = L
"8";
263 // Configure the font to draw with Marlett, keeping the current background color as-is
264 SelectObject(cdraw
->nmcd
.hdc
, m_marlett
);
265 SetBkMode(cdraw
->nmcd
.hdc
, TRANSPARENT
);
267 // Tweak the alignment by 1 pixel so the menu draws like the Windows start menu.
268 RECT rc
= cdraw
->nmcd
.rc
;
271 // The arrow is drawn at the right of the item's rect, aligned vertically.
272 DrawTextEx(cdraw
->nmcd
.hdc
, text
, 1, &rc
, DT_NOCLIP
| DT_VCENTER
| DT_RIGHT
| DT_SINGLELINE
, NULL
);
280 CMenuToolbarBase::CMenuToolbarBase(CMenuBand
*menuBand
, BOOL usePager
) :
283 m_useFlatMenus(FALSE
),
285 m_disableMouseTrack(FALSE
),
286 m_timerEnabled(FALSE
),
287 m_menuBand(menuBand
),
290 m_usePager(usePager
),
293 m_isTrackingPopup(FALSE
)
299 m_marlett
= CreateFont(
300 0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET
,
301 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
302 DEFAULT_QUALITY
, FF_DONTCARE
, L
"Marlett");
305 CMenuToolbarBase::~CMenuToolbarBase()
307 if (m_hwndToolbar
&& m_hwndToolbar
!= m_hwnd
)
308 DestroyWindow(m_hwndToolbar
);
311 DestroyWindow(m_hwnd
);
313 DeleteObject(m_marlett
);
316 void CMenuToolbarBase::InvalidateDraw()
318 InvalidateRect(m_hwnd
, NULL
, FALSE
);
321 HRESULT
CMenuToolbarBase::ShowWindow(BOOL fShow
)
323 ::ShowWindow(m_hwnd
, fShow
? SW_SHOW
: SW_HIDE
);
325 // Ensure that the right image list is assigned to the toolbar
328 // For custom-drawing
329 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
334 HRESULT
CMenuToolbarBase::UpdateImageLists()
336 if ((m_initFlags
& (SMINIT_TOPLEVEL
| SMINIT_VERTICAL
)) == SMINIT_TOPLEVEL
) // not vertical.
338 // No image list, prevents the buttons from having a margin at the left side
339 SendMessageW(m_hwnd
, TB_SETIMAGELIST
, 0, 0);
343 // Assign the correct imagelist and padding based on the current icon size
346 if (m_menuBand
->UseBigIcons())
349 SendMessageW(m_hwndToolbar
, TB_SETPADDING
, 0, MAKELPARAM(4, 0));
354 SendMessageW(m_hwndToolbar
, TB_SETPADDING
, 0, MAKELPARAM(4, 4));
358 HRESULT hr
= SHGetImageList(shiml
, IID_PPV_ARG(IImageList
, &piml
));
359 if (FAILED_UNEXPECTEDLY(hr
))
361 SendMessageW(m_hwndToolbar
, TB_SETIMAGELIST
, 0, 0);
365 SendMessageW(m_hwndToolbar
, TB_SETIMAGELIST
, 0, reinterpret_cast<LPARAM
>(piml
));
370 HRESULT
CMenuToolbarBase::Close()
372 if (m_hwndToolbar
!= m_hwnd
)
373 DestroyWindow(m_hwndToolbar
);
375 DestroyWindow(m_hwnd
);
377 m_hwndToolbar
= NULL
;
383 HRESULT
CMenuToolbarBase::CreateToolbar(HWND hwndParent
, DWORD dwFlags
)
385 LONG tbStyles
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
|
386 TBSTYLE_TOOLTIPS
| TBSTYLE_TRANSPARENT
| TBSTYLE_REGISTERDROP
| TBSTYLE_LIST
| TBSTYLE_FLAT
| TBSTYLE_CUSTOMERASE
|
387 CCS_NODIVIDER
| CCS_NOPARENTALIGN
| CCS_NORESIZE
| CCS_TOP
;
388 LONG tbExStyles
= TBSTYLE_EX_DOUBLEBUFFER
| WS_EX_TOOLWINDOW
;
390 if (dwFlags
& SMINIT_VERTICAL
)
392 // Activate vertical semantics
393 tbStyles
|= CCS_VERT
;
395 #if USE_TBSTYLE_EX_VERTICAL
396 tbExStyles
|= TBSTYLE_EX_VERTICAL
;
400 m_initFlags
= dwFlags
;
402 // Get a temporary rect to use while creating the toolbar window.
403 // Ensure that it is not a null rect.
405 if (!::GetClientRect(hwndParent
, &rc
) ||
406 (rc
.left
== rc
.right
) ||
407 (rc
.top
== rc
.bottom
))
415 HWND hwndToolbar
= CreateWindowEx(
416 tbExStyles
, TOOLBARCLASSNAMEW
, NULL
,
417 tbStyles
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
418 hwndParent
, NULL
, _AtlBaseModule
.GetModuleInstance(), 0);
420 if (hwndToolbar
== NULL
)
423 // If needed, create the pager.
426 LONG pgStyles
= PGS_VERT
| WS_CHILD
| WS_VISIBLE
;
429 HWND hwndPager
= CreateWindowEx(
430 pgExStyles
, WC_PAGESCROLLER
, NULL
,
431 pgStyles
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
432 hwndParent
, NULL
, _AtlBaseModule
.GetModuleInstance(), 0);
434 ::SetParent(hwndToolbar
, hwndPager
);
435 ::SetParent(hwndPager
, hwndParent
);
437 SendMessage(hwndPager
, PGM_SETCHILD
, 0, reinterpret_cast<LPARAM
>(hwndToolbar
));
438 m_hwndToolbar
= hwndToolbar
;
443 ::SetParent(hwndToolbar
, hwndParent
);
444 m_hwndToolbar
= hwndToolbar
;
445 m_hwnd
= hwndToolbar
;
448 // Identify the version of the used Common Controls DLL by sending the size of the TBBUTTON structure.
449 SendMessageW(hwndToolbar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
452 SetWindowLongPtr(hwndToolbar
, GWLP_USERDATA
, reinterpret_cast<LONG_PTR
>(this));
453 m_SubclassOld
= (WNDPROC
) SetWindowLongPtr(hwndToolbar
, GWLP_WNDPROC
, reinterpret_cast<LONG_PTR
>(CMenuToolbarBase::s_SubclassProc
));
455 // Configure the image lists
461 HRESULT
CMenuToolbarBase::GetSizes(SIZE
* pMinSize
, SIZE
* pMaxSize
, SIZE
* pIntegralSize
)
464 *pMinSize
= m_idealSize
;
466 *pMaxSize
= m_idealSize
;
468 *pIntegralSize
= m_itemSize
;
476 // Obtain the ideal size, to be used as min and max
477 SendMessageW(m_hwndToolbar
, TB_AUTOSIZE
, 0, 0);
478 SendMessageW(m_hwndToolbar
, TB_GETMAXSIZE
, 0, reinterpret_cast<LPARAM
>(&m_idealSize
));
479 SendMessageW(m_hwndToolbar
, TB_GETIDEALSIZE
, (m_initFlags
& SMINIT_VERTICAL
) != 0, reinterpret_cast<LPARAM
>(&m_idealSize
));
481 // Obtain the button size, to be used as the integral size
482 DWORD size
= SendMessageW(m_hwndToolbar
, TB_GETBUTTONSIZE
, 0, 0);
483 m_itemSize
.cx
= GET_X_LPARAM(size
);
484 m_itemSize
.cy
= GET_Y_LPARAM(size
);
488 *pMinSize
= m_idealSize
;
490 *pMaxSize
= m_idealSize
;
492 *pIntegralSize
= m_itemSize
;
497 HRESULT
CMenuToolbarBase::SetPosSize(int x
, int y
, int cx
, int cy
)
499 // If we have a pager, set the toolbar height to the ideal height of the toolbar
500 if (m_hwnd
!= m_hwndToolbar
)
502 SetWindowPos(m_hwndToolbar
, NULL
, x
, y
, cx
, m_idealSize
.cy
, 0);
505 // Update the toolbar or pager to fit the requested rect
506 SetWindowPos(m_hwnd
, NULL
, x
, y
, cx
, cy
, 0);
508 // In a vertical menu, resize the buttons to fit the width
509 if (m_initFlags
& SMINIT_VERTICAL
)
511 DWORD btnSize
= SendMessage(m_hwndToolbar
, TB_GETBUTTONSIZE
, 0, 0);
512 SendMessage(m_hwndToolbar
, TB_SETBUTTONSIZE
, 0, MAKELPARAM(cx
, HIWORD(btnSize
)));
518 HRESULT
CMenuToolbarBase::IsWindowOwner(HWND hwnd
)
520 if (m_hwnd
&& m_hwnd
== hwnd
) return S_OK
;
521 if (m_hwndToolbar
&& m_hwndToolbar
== hwnd
) return S_OK
;
525 HRESULT
CMenuToolbarBase::GetWindow(HWND
*phwnd
)
535 HRESULT
CMenuToolbarBase::OnGetInfoTip(NMTBGETINFOTIP
* tip
)
540 INT iItem
= tip
->iItem
;
542 GetDataFromId(iItem
, &index
, &dwData
);
544 return InternalGetTooltip(iItem
, index
, dwData
, tip
->pszText
, tip
->cchTextMax
);
547 HRESULT
CMenuToolbarBase::OnPopupTimer(DWORD timerId
)
549 if (timerId
!= TIMERID_HOTTRACK
)
552 KillTimer(m_hwndToolbar
, TIMERID_HOTTRACK
);
557 m_timerEnabled
= FALSE
;
562 // Returns S_FALSE if the current item did not show a submenu
563 HRESULT hr
= PopupItem(m_hotItem
, FALSE
);
567 // If we didn't switch submenus, cancel the current popup regardless
570 HRESULT hr
= CancelCurrentPopup();
571 if (FAILED_UNEXPECTEDLY(hr
))
578 HRESULT
CMenuToolbarBase::KillPopupTimer()
582 m_timerEnabled
= FALSE
;
583 KillTimer(m_hwndToolbar
, TIMERID_HOTTRACK
);
589 HRESULT
CMenuToolbarBase::ChangeHotItem(CMenuToolbarBase
* toolbar
, INT item
, DWORD dwFlags
)
591 // Ignore the change if it already matches the stored info
592 if (m_hotBar
== toolbar
&& m_hotItem
== item
)
595 // Prevent a change of hot item if the change was triggered by the mouse,
596 // and mouse tracking is disabled.
597 if (m_disableMouseTrack
&& dwFlags
& HICF_MOUSE
)
599 TRACE("Hot item change prevented by DisableMouseTrack\n");
603 // Notify the toolbar if the hot-tracking left this toolbar
604 if (m_hotBar
== this && toolbar
!= this)
606 SendMessage(m_hwndToolbar
, TB_SETHOTITEM
, (WPARAM
) -1, 0);
609 TRACE("Hot item changed from %p %p, to %p %p\n", m_hotBar
, m_hotItem
, toolbar
, item
);
613 if (m_hotBar
== this)
615 if (m_isTrackingPopup
&& !(m_initFlags
& SMINIT_VERTICAL
))
617 // If the menubar has an open submenu, switch to the new item's submenu immediately
618 PopupItem(m_hotItem
, FALSE
);
620 else if (dwFlags
& HICF_MOUSE
)
622 // Vertical menus show/hide the submenu after a delay,
623 // but only with the mouse.
624 if (m_initFlags
& SMINIT_VERTICAL
)
627 SystemParametersInfo(SPI_GETMENUSHOWDELAY
, 0, &elapsed
, 0);
628 SetTimer(m_hwndToolbar
, TIMERID_HOTTRACK
, elapsed
, NULL
);
629 m_timerEnabled
= TRUE
;
630 TRACE("SetTimer called with m_hotItem=%d\n", m_hotItem
);
636 info
.cbSize
= sizeof(info
);
639 int index
= SendMessage(m_hwndToolbar
, TB_GETBUTTONINFO
, item
, reinterpret_cast<LPARAM
>(&info
));
641 SendMessage(m_hwndToolbar
, TB_SETHOTITEM
, index
, 0);
649 HRESULT
CMenuToolbarBase::ChangePopupItem(CMenuToolbarBase
* toolbar
, INT item
)
651 // Ignore the change if it already matches the stored info
652 if (m_popupBar
== toolbar
&& m_popupItem
== item
)
655 // Notify the toolbar if the popup-tracking this toolbar
656 if (m_popupBar
== this && toolbar
!= this)
658 SendMessage(m_hwndToolbar
, TB_CHECKBUTTON
, m_popupItem
, FALSE
);
659 m_isTrackingPopup
= FALSE
;
662 m_popupBar
= toolbar
;
665 if (m_popupBar
== this)
667 SendMessage(m_hwndToolbar
, TB_CHECKBUTTON
, m_popupItem
, TRUE
);
674 HRESULT
CMenuToolbarBase::IsTrackedItem(INT index
)
678 if (m_hotBar
!= this)
684 if (!SendMessage(m_hwndToolbar
, TB_GETBUTTON
, index
, reinterpret_cast<LPARAM
>(&btn
)))
687 if (m_hotItem
== btn
.idCommand
)
690 if (m_popupItem
== btn
.idCommand
)
696 HRESULT
CMenuToolbarBase::ChangeTrackedItem(INT index
, BOOL wasTracking
, BOOL mouse
)
702 m_isTrackingPopup
= FALSE
;
703 return m_menuBand
->_ChangeHotItem(NULL
, -1, HICF_MOUSE
);
706 if (!SendMessage(m_hwndToolbar
, TB_GETBUTTON
, index
, reinterpret_cast<LPARAM
>(&btn
)))
709 TRACE("ChangeTrackedItem %d, %d\n", index
, wasTracking
);
710 m_isTrackingPopup
= wasTracking
;
711 return m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, mouse
? HICF_MOUSE
: 0);
714 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, IShellMenu
* childShellMenu
, BOOL keyInitiated
)
716 // Calculate the submenu position and exclude area
720 if (!SendMessage(m_hwndToolbar
, TB_GETITEMRECT
, index
, reinterpret_cast<LPARAM
>(&rc
)))
723 GetWindowRect(m_hwnd
, &rcx
);
725 POINT a
= { rc
.left
, rc
.top
};
726 POINT b
= { rc
.right
, rc
.bottom
};
727 POINT c
= { rcx
.left
, rcx
.top
};
728 POINT d
= { rcx
.right
, rcx
.bottom
};
730 ClientToScreen(m_hwndToolbar
, &a
);
731 ClientToScreen(m_hwndToolbar
, &b
);
732 ClientToScreen(m_hwnd
, &c
);
733 ClientToScreen(m_hwnd
, &d
);
735 POINTL pt
= { a
.x
, b
.y
};
736 RECTL rcl
= { c
.x
, c
.y
, d
.x
, d
.y
};
738 if (m_initFlags
& SMINIT_VERTICAL
)
744 // Display the submenu
745 m_isTrackingPopup
= TRUE
;
747 m_menuBand
->_ChangePopupItem(this, iItem
);
748 m_menuBand
->_OnPopupSubMenu(childShellMenu
, &pt
, &rcl
, keyInitiated
);
753 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, HMENU menu
)
755 // Calculate the submenu position and exclude area
758 if (!SendMessage(m_hwndToolbar
, TB_GETITEMRECT
, index
, reinterpret_cast<LPARAM
>(&rc
)))
761 POINT a
= { rc
.left
, rc
.top
};
762 POINT b
= { rc
.right
, rc
.bottom
};
764 ClientToScreen(m_hwndToolbar
, &a
);
765 ClientToScreen(m_hwndToolbar
, &b
);
767 POINT pt
= { a
.x
, b
.y
};
768 RECT rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
770 if (m_initFlags
& SMINIT_VERTICAL
)
776 HMENU popup
= GetSubMenu(menu
, index
);
778 // Display the submenu
779 m_isTrackingPopup
= TRUE
;
780 m_menuBand
->_ChangePopupItem(this, iItem
);
781 m_menuBand
->_TrackSubMenu(popup
, pt
.x
, pt
.y
, rcl
);
782 m_menuBand
->_ChangePopupItem(NULL
, -1);
783 m_isTrackingPopup
= FALSE
;
785 m_menuBand
->_ChangeHotItem(NULL
, -1, 0);
790 HRESULT
CMenuToolbarBase::TrackContextMenu(IContextMenu
* contextMenu
, POINT pt
)
793 m_menuBand
->_KillPopupTimers();
795 m_menuBand
->_CancelCurrentPopup();
797 // Display the context menu
798 return m_menuBand
->_TrackContextMenu(contextMenu
, pt
.x
, pt
.y
);
801 HRESULT
CMenuToolbarBase::ProcessClick(INT iItem
)
803 if (m_disableMouseTrack
)
805 TRACE("Item click prevented by DisableMouseTrack\n");
809 // If a button is clicked while a submenu was open, cancel the submenu.
810 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
812 TRACE("OnCommand cancelled because it was tracking submenu.\n");
816 if (PopupItem(iItem
, FALSE
) == S_OK
)
818 TRACE("PopupItem returned S_OK\n");
822 TRACE("Executing...\n");
824 return m_menuBand
->_MenuItemHotTrack(MPOS_EXECUTE
);
827 HRESULT
CMenuToolbarBase::MenuBarMouseDown(INT iIndex
)
831 if (m_initFlags
& SMINIT_VERTICAL
)
834 ::SendMessageW(m_hwndToolbar
, TB_GETBUTTON
, iIndex
, reinterpret_cast<LPARAM
>(&btn
));
835 return ProcessClick(btn
.idCommand
);
838 HRESULT
CMenuToolbarBase::MenuBarMouseUp(INT iIndex
)
842 if (!(m_initFlags
& SMINIT_VERTICAL
))
845 ::SendMessageW(m_hwndToolbar
, TB_GETBUTTON
, iIndex
, reinterpret_cast<LPARAM
>(&btn
));
846 return ProcessClick(btn
.idCommand
);
849 HRESULT
CMenuToolbarBase::PrepareExecuteItem(INT iItem
)
851 this->m_menuBand
->_KillPopupTimers();
853 m_executeItem
= iItem
;
854 return GetDataFromId(iItem
, &m_executeIndex
, &m_executeData
);
857 HRESULT
CMenuToolbarBase::ExecuteItem()
859 return InternalExecuteItem(m_executeItem
, m_executeItem
, m_executeData
);
862 HRESULT
CMenuToolbarBase::OnContextMenu(NMMOUSE
* rclick
)
864 INT iItem
= rclick
->dwItemSpec
;
865 INT index
= rclick
->dwHitInfo
;
866 DWORD_PTR data
= rclick
->dwItemData
;
868 GetDataFromId(iItem
, &index
, &data
);
870 return InternalContextMenu(iItem
, index
, data
, rclick
->pt
);
873 HRESULT
CMenuToolbarBase::KeyboardItemChange(DWORD dwSelectType
)
875 int prev
= m_hotItem
;
878 if (dwSelectType
!= 0xFFFFFFFF)
880 int count
= SendMessage(m_hwndToolbar
, TB_BUTTONCOUNT
, 0, 0);
882 if (dwSelectType
== VK_HOME
)
885 dwSelectType
= VK_DOWN
;
887 else if (dwSelectType
== VK_END
)
890 dwSelectType
= VK_UP
;
896 TBBUTTONINFO info
= { 0 };
897 info
.cbSize
= sizeof(TBBUTTONINFO
);
899 index
= SendMessage(m_hwndToolbar
, TB_GETBUTTONINFO
, m_hotItem
, reinterpret_cast<LPARAM
>(&info
));
904 if (dwSelectType
== VK_UP
)
908 else if (dwSelectType
== VK_DOWN
)
915 if (dwSelectType
== VK_UP
)
919 else if (dwSelectType
== VK_DOWN
)
926 TBBUTTON btn
= { 0 };
927 while (index
>= 0 && index
< count
)
929 DWORD res
= SendMessage(m_hwndToolbar
, TB_GETBUTTON
, index
, reinterpret_cast<LPARAM
>(&btn
));
935 if (prev
!= btn
.idCommand
)
937 TRACE("Setting Hot item to %d\n", index
);
938 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
941 m_menuBand
->_GetTopLevelWindow(&tlw
);
942 SendMessage(tlw
, WM_CANCELMODE
, 0, 0);
943 PostMessage(m_hwndToolbar
, WM_USER_CHANGETRACKEDITEM
, index
, MAKELPARAM(m_isTrackingPopup
, FALSE
));
946 m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, 0);
951 if (dwSelectType
== VK_UP
)
955 else if (dwSelectType
== VK_DOWN
)
966 TRACE("Setting Hot item to null\n");
967 m_menuBand
->_ChangeHotItem(NULL
, -1, 0);
973 HRESULT
CMenuToolbarBase::AddButton(DWORD commandId
, LPCWSTR caption
, BOOL hasSubMenu
, INT iconId
, DWORD_PTR buttonData
, BOOL last
)
975 TBBUTTON tbb
= { 0 };
977 tbb
.fsState
= TBSTATE_ENABLED
;
978 #if !USE_TBSTYLE_EX_VERTICAL
979 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
980 tbb
.fsState
|= TBSTATE_WRAP
;
982 tbb
.fsStyle
= BTNS_CHECKGROUP
;
984 if (hasSubMenu
&& (m_initFlags
& SMINIT_VERTICAL
))
985 tbb
.fsStyle
|= BTNS_DROPDOWN
;
987 if (!(m_initFlags
& SMINIT_VERTICAL
))
988 tbb
.fsStyle
|= BTNS_AUTOSIZE
;
990 tbb
.iString
= (INT_PTR
) caption
;
991 tbb
.idCommand
= commandId
;
993 tbb
.iBitmap
= iconId
;
994 tbb
.dwData
= buttonData
;
996 if (!SendMessageW(m_hwndToolbar
, TB_ADDBUTTONS
, 1, reinterpret_cast<LPARAM
>(&tbb
)))
997 return HRESULT_FROM_WIN32(GetLastError());
1002 HRESULT
CMenuToolbarBase::AddSeparator(BOOL last
)
1004 TBBUTTON tbb
= { 0 };
1006 tbb
.fsState
= TBSTATE_ENABLED
;
1007 #if !USE_TBSTYLE_EX_VERTICAL
1008 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
1009 tbb
.fsState
|= TBSTATE_WRAP
;
1011 tbb
.fsStyle
= BTNS_SEP
;
1014 if (!SendMessageW(m_hwndToolbar
, TB_ADDBUTTONS
, 1, reinterpret_cast<LPARAM
>(&tbb
)))
1015 return HRESULT_FROM_WIN32(GetLastError());
1020 HRESULT
CMenuToolbarBase::AddPlaceholder()
1022 TBBUTTON tbb
= { 0 };
1023 PCWSTR MenuString
= L
"(Empty)";
1027 tbb
.iString
= (INT_PTR
) MenuString
;
1030 if (!SendMessageW(m_hwndToolbar
, TB_ADDBUTTONS
, 1, reinterpret_cast<LPARAM
>(&tbb
)))
1031 return HRESULT_FROM_WIN32(GetLastError());
1036 HRESULT
CMenuToolbarBase::ClearToolbar()
1038 while (SendMessage(m_hwndToolbar
, TB_DELETEBUTTON
, 0, 0))
1045 HRESULT
CMenuToolbarBase::GetDataFromId(INT iItem
, INT
* pIndex
, DWORD_PTR
* pData
)
1056 TBBUTTONINFO info
= { 0 };
1058 info
.cbSize
= sizeof(TBBUTTONINFO
);
1059 info
.dwMask
= TBIF_COMMAND
| TBIF_LPARAM
;
1061 int index
= SendMessage(m_hwndToolbar
, TB_GETBUTTONINFO
, iItem
, reinterpret_cast<LPARAM
>(&info
));
1069 *pData
= info
.lParam
;
1074 HRESULT
CMenuToolbarBase::CancelCurrentPopup()
1076 return m_menuBand
->_CancelCurrentPopup();
1079 HRESULT
CMenuToolbarBase::PopupItem(INT iItem
, BOOL keyInitiated
)
1087 if (m_popupBar
== this && m_popupItem
== iItem
)
1090 GetDataFromId(iItem
, &index
, &dwData
);
1092 HRESULT hr
= InternalHasSubMenu(iItem
, index
, dwData
);
1098 HRESULT hr
= CancelCurrentPopup();
1099 if (FAILED_UNEXPECTEDLY(hr
))
1103 if (!(m_initFlags
& SMINIT_VERTICAL
))
1105 TRACE("PopupItem non-vertical %d %d\n", index
, iItem
);
1106 m_menuBand
->_ChangeHotItem(this, iItem
, 0);
1109 return InternalPopupItem(iItem
, index
, dwData
, keyInitiated
);
1112 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand
*menuBand
) :
1113 CMenuToolbarBase(menuBand
, FALSE
),
1118 CMenuStaticToolbar::~CMenuStaticToolbar()
1122 HRESULT
CMenuStaticToolbar::GetMenu(
1123 _Out_opt_ HMENU
*phmenu
,
1124 _Out_opt_ HWND
*phwnd
,
1125 _Out_opt_ DWORD
*pdwFlags
)
1132 *pdwFlags
= m_dwMenuFlags
;
1137 HRESULT
CMenuStaticToolbar::SetMenu(
1143 m_dwMenuFlags
= dwFlags
;
1148 HRESULT
CMenuStaticToolbar::FillToolbar(BOOL clearFirst
)
1151 int ic
= GetMenuItemCount(m_hmenu
);
1159 for (i
= 0; i
< ic
; i
++)
1161 BOOL last
= i
+ 1 == ic
;
1165 info
.cbSize
= sizeof(info
);
1166 info
.dwTypeData
= NULL
;
1167 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_ID
;
1169 if (!GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
))
1171 TRACE("Error obtaining info for menu item at pos=%d\n", i
);
1177 if (info
.fType
& MFT_SEPARATOR
)
1181 else if (!(info
.fType
& MFT_BITMAP
))
1184 info
.dwTypeData
= (PWSTR
) HeapAlloc(GetProcessHeap(), 0, (info
.cch
+ 1) * sizeof(WCHAR
));
1186 info
.fMask
= MIIM_STRING
| MIIM_SUBMENU
| MIIM_ID
;
1187 GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
);
1189 SMINFO
* sminfo
= new SMINFO();
1190 sminfo
->dwMask
= SMIM_ICON
| SMIM_FLAGS
;
1191 // FIXME: remove before deleting the toolbar or it will leak
1193 HRESULT hr
= m_menuBand
->_CallCBWithItemId(info
.wID
, SMC_GETINFO
, 0, reinterpret_cast<LPARAM
>(sminfo
));
1194 if (FAILED_UNEXPECTEDLY(hr
))
1200 AddButton(info
.wID
, info
.dwTypeData
, info
.hSubMenu
!= NULL
, sminfo
->iIcon
, reinterpret_cast<DWORD_PTR
>(sminfo
), last
);
1202 HeapFree(GetProcessHeap(), 0, info
.dwTypeData
);
1209 HRESULT
CMenuStaticToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1211 //SMINFO * info = reinterpret_cast<SMINFO*>(dwData);
1216 HRESULT
CMenuStaticToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1218 delete reinterpret_cast<SMINFO
*>(tb
->tbButton
.dwData
);
1222 HRESULT
CMenuStaticToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1224 CComPtr
<IContextMenu
> contextMenu
;
1225 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
,
1226 reinterpret_cast<WPARAM
>(&IID_IContextMenu
), reinterpret_cast<LPARAM
>(&contextMenu
));
1230 return TrackContextMenu(contextMenu
, pt
);
1233 HRESULT
CMenuStaticToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1235 return m_menuBand
->_CallCBWithItemId(iItem
, SMC_EXEC
, 0, 0);
1238 HRESULT
CMenuStaticToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1240 SMINFO
* nfo
= reinterpret_cast<SMINFO
*>(dwData
);
1244 if (nfo
->dwFlags
&SMIF_TRACKPOPUP
)
1246 return PopupSubMenu(iItem
, index
, m_hmenu
);
1250 CComPtr
<IShellMenu
> shellMenu
;
1251 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
, reinterpret_cast<WPARAM
>(&IID_IShellMenu
), reinterpret_cast<LPARAM
>(&shellMenu
));
1252 if (FAILED_UNEXPECTEDLY(hr
))
1255 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1259 HRESULT
CMenuStaticToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1261 return ::GetSubMenu(m_hmenu
, index
) ? S_OK
: S_FALSE
;
1264 CMenuSFToolbar::CMenuSFToolbar(CMenuBand
* menuBand
) :
1265 CMenuToolbarBase(menuBand
, TRUE
),
1266 m_shellFolder(NULL
),
1272 CMenuSFToolbar::~CMenuSFToolbar()
1276 HRESULT
CMenuSFToolbar::FillToolbar(BOOL clearFirst
)
1283 m_shellFolder
->EnumObjects(GetToolbar(), SHCONTF_FOLDERS
| SHCONTF_NONFOLDERS
, &eidl
);
1285 LPITEMIDLIST item
= NULL
;
1286 hr
= eidl
->Next(1, &item
, NULL
);
1292 if (m_menuBand
->_CallCBWithItemPidl(item
, 0x10000000, 0, 0) == S_OK
)
1294 STRRET sr
= { STRRET_CSTR
, { 0 } };
1296 hr
= m_shellFolder
->GetDisplayNameOf(item
, SIGDN_NORMALDISPLAY
, &sr
);
1297 if (FAILED_UNEXPECTEDLY(hr
))
1300 StrRetToStr(&sr
, NULL
, &MenuString
);
1302 index
= SHMapPIDLToSystemImageListIndex(m_shellFolder
, item
, &indexOpen
);
1304 LPCITEMIDLIST itemc
= item
;
1306 SFGAOF attrs
= SFGAO_FOLDER
;
1307 hr
= m_shellFolder
->GetAttributesOf(1, &itemc
, &attrs
);
1309 DWORD_PTR dwData
= reinterpret_cast<DWORD_PTR
>(ILClone(item
));
1311 // Fetch next item already, so we know if the current one is the last
1312 hr
= eidl
->Next(1, &item
, NULL
);
1314 AddButton(++i
, MenuString
, attrs
& SFGAO_FOLDER
, index
, dwData
, hr
!= S_OK
);
1316 CoTaskMemFree(MenuString
);
1320 // Fetch next item here also
1321 hr
= eidl
->Next(1, &item
, NULL
);
1326 // If no items were added, show the "empty" placeholder
1329 return AddPlaceholder();
1335 HRESULT
CMenuSFToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1337 //ITEMIDLIST * pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1342 HRESULT
CMenuSFToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1344 ILFree(reinterpret_cast<LPITEMIDLIST
>(tb
->tbButton
.dwData
));
1348 HRESULT
CMenuSFToolbar::SetShellFolder(IShellFolder
*psf
, LPCITEMIDLIST pidlFolder
, HKEY hKey
, DWORD dwFlags
)
1350 m_shellFolder
= psf
;
1351 m_idList
= ILClone(pidlFolder
);
1353 m_dwMenuFlags
= dwFlags
;
1357 HRESULT
CMenuSFToolbar::GetShellFolder(DWORD
*pdwFlags
, LPITEMIDLIST
*ppidl
, REFIID riid
, void **ppv
)
1361 hr
= m_shellFolder
->QueryInterface(riid
, ppv
);
1362 if (FAILED_UNEXPECTEDLY(hr
))
1366 *pdwFlags
= m_dwMenuFlags
;
1370 LPITEMIDLIST pidl
= NULL
;
1374 pidl
= ILClone(m_idList
);
1377 (*reinterpret_cast<IUnknown
**>(ppv
))->Release();
1388 HRESULT
CMenuSFToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1391 CComPtr
<IContextMenu
> contextMenu
= NULL
;
1392 LPCITEMIDLIST pidl
= reinterpret_cast<LPCITEMIDLIST
>(dwData
);
1394 hr
= m_shellFolder
->GetUIObjectOf(GetToolbar(), 1, &pidl
, IID_NULL_PPV_ARG(IContextMenu
, &contextMenu
));
1395 if (FAILED_UNEXPECTEDLY(hr
))
1400 hr
= TrackContextMenu(contextMenu
, pt
);
1405 HRESULT
CMenuSFToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1407 return m_menuBand
->_CallCBWithItemPidl(reinterpret_cast<LPITEMIDLIST
>(data
), SMC_SFEXEC
, 0, 0);
1410 HRESULT
CMenuSFToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1416 CComPtr
<IShellMenuCallback
> psmc
;
1417 CComPtr
<IShellMenu
> shellMenu
;
1419 LPITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1424 hr
= CMenuBand_Constructor(IID_PPV_ARG(IShellMenu
, &shellMenu
));
1425 if (FAILED_UNEXPECTEDLY(hr
))
1428 m_menuBand
->GetMenuInfo(&psmc
, &uId
, &uIdAncestor
, &flags
);
1430 // FIXME: not sure what to use as uId/uIdAncestor here
1431 hr
= shellMenu
->Initialize(psmc
, 0, uId
, SMINIT_VERTICAL
);
1432 if (FAILED_UNEXPECTEDLY(hr
))
1435 CComPtr
<IShellFolder
> childFolder
;
1436 hr
= m_shellFolder
->BindToObject(pidl
, NULL
, IID_PPV_ARG(IShellFolder
, &childFolder
));
1437 if (FAILED_UNEXPECTEDLY(hr
))
1440 hr
= shellMenu
->SetShellFolder(childFolder
, NULL
, NULL
, 0);
1441 if (FAILED_UNEXPECTEDLY(hr
))
1444 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1447 HRESULT
CMenuSFToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1450 LPCITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1452 SFGAOF attrs
= SFGAO_FOLDER
;
1453 hr
= m_shellFolder
->GetAttributesOf(1, &pidl
, &attrs
);
1454 if (FAILED_UNEXPECTEDLY(hr
))
1457 return (attrs
& SFGAO_FOLDER
) ? S_OK
: S_FALSE
;