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
21 #include <commoncontrols.h>
22 #include <shlwapi_undoc.h>
25 #include "CMenuBand.h"
26 #include "CMenuToolbars.h"
28 #define IDS_MENU_EMPTY 34561
30 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
31 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
33 WINE_DEFAULT_DEBUG_CHANNEL(CMenuToolbars
);
36 HRESULT WINAPI
SHGetImageList(
42 // FIXME: Enable if/when wine comctl supports this flag properly
43 #define USE_TBSTYLE_EX_VERTICAL 0
45 // User-defined timer ID used while hot-tracking around the menu
46 #define TIMERID_HOTTRACK 1
48 LRESULT
CMenuToolbarBase::OnWinEventWrap(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
51 bHandled
= OnWinEvent(m_hWnd
, uMsg
, wParam
, lParam
, &lr
) != S_FALSE
;
55 HRESULT
CMenuToolbarBase::OnWinEvent(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*theResult
)
63 //return OnCommand(wParam, lParam, theResult);
67 hdr
= reinterpret_cast<LPNMHDR
>(lParam
);
70 case TBN_DELETINGBUTTON
:
71 return OnDeletingButton(reinterpret_cast<LPNMTOOLBAR
>(hdr
));
74 return OnPagerCalcSize(reinterpret_cast<LPNMPGCALCSIZE
>(hdr
));
77 return ProcessClick(reinterpret_cast<LPNMTOOLBAR
>(hdr
)->iItem
);
79 case TBN_HOTITEMCHANGE
:
80 //return OnHotItemChange(reinterpret_cast<LPNMTBHOTITEM>(hdr), theResult);
84 return OnContextMenu(reinterpret_cast<LPNMMOUSE
>(hdr
));
87 return OnCustomDraw(reinterpret_cast<LPNMTBCUSTOMDRAW
>(hdr
), theResult
);
90 return OnGetInfoTip(reinterpret_cast<LPNMTBGETINFOTIP
>(hdr
));
92 // Silence unhandled items so that they don't print as unknown
99 case NM_RELEASEDCAPTURE
:
107 case TBN_GETDISPINFO
:
114 case NM_TOOLTIPSCREATED
:
118 case -714: return S_FALSE
;
121 TRACE("WM_NOTIFY unknown code %d, %d\n", hdr
->code
, hdr
->idFrom
);
130 HRESULT
CMenuToolbarBase::DisableMouseTrack(BOOL bDisable
)
132 if (m_disableMouseTrack
!= bDisable
)
134 m_disableMouseTrack
= bDisable
;
135 TRACE("DisableMouseTrack %d\n", bDisable
);
140 HRESULT
CMenuToolbarBase::OnPagerCalcSize(LPNMPGCALCSIZE csize
)
143 GetSizes(NULL
, &tbs
, NULL
);
144 if (csize
->dwFlag
== PGF_CALCHEIGHT
)
146 csize
->iHeight
= tbs
.cy
;
148 else if (csize
->dwFlag
== PGF_CALCWIDTH
)
150 csize
->iWidth
= tbs
.cx
;
155 HRESULT
CMenuToolbarBase::OnCustomDraw(LPNMTBCUSTOMDRAW cdraw
, LRESULT
* theResult
)
160 switch (cdraw
->nmcd
.dwDrawStage
)
163 *theResult
= CDRF_NOTIFYITEMDRAW
;
166 case CDDS_ITEMPREPAINT
:
168 // The item with an active submenu gets the CHECKED flag.
169 isHot
= m_hotBar
== this && (int) cdraw
->nmcd
.dwItemSpec
== m_hotItem
;
170 isPopup
= m_popupBar
== this && (int) cdraw
->nmcd
.dwItemSpec
== m_popupItem
;
172 if (m_initFlags
& SMINIT_VERTICAL
|| IsAppThemed())
176 RECT rc
= cdraw
->nmcd
.rc
;
177 HDC hdc
= cdraw
->nmcd
.hdc
;
179 // Remove HOT and CHECKED flags (will restore HOT if necessary)
180 cdraw
->nmcd
.uItemState
&= ~(CDIS_HOT
| CDIS_CHECKED
);
182 // Decide on the colors
183 if (isHot
|| (m_hotItem
< 0 && isPopup
))
185 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
187 clrText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
188 bgBrush
= GetSysColorBrush(m_useFlatMenus
? COLOR_MENUHILIGHT
: COLOR_HIGHLIGHT
);
192 clrText
= GetSysColor(COLOR_MENUTEXT
);
193 bgBrush
= GetSysColorBrush(COLOR_MENU
);
196 // Paint the background color with the selected color
197 FillRect(hdc
, &rc
, bgBrush
);
199 // Set the text color in advance, this color will be assigned when the ITEMPOSTPAINT triggers
200 SetTextColor(hdc
, clrText
);
202 // Set the text color, will be used by the internal drawing code
203 cdraw
->clrText
= clrText
;
204 cdraw
->iListGap
+= 4;
206 // Tell the default drawing code we don't want any fanciness, not even a background.
207 *theResult
= CDRF_NOTIFYPOSTPAINT
| TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| 0x00800000; // FIXME: the last bit is Vista+, useful for debugging only
211 // Remove HOT and CHECKED flags (will restore HOT if necessary)
212 cdraw
->nmcd
.uItemState
&= ~CDIS_HOT
;
214 // Decide on the colors
215 if (isHot
|| (m_hotItem
< 0 && isPopup
))
217 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
225 case CDDS_ITEMPOSTPAINT
:
227 // Fetch the button style
228 btni
.cbSize
= sizeof(btni
);
229 btni
.dwMask
= TBIF_STYLE
;
230 GetButtonInfo(cdraw
->nmcd
.dwItemSpec
, &btni
);
232 // Check if we need to draw a submenu arrow
233 if (btni
.fsStyle
& BTNS_DROPDOWN
)
235 // TODO: Support RTL text modes by drawing a leftwards arrow aligned to the left of the control
237 // "8" is the rightwards dropdown arrow in the Marlett font
238 WCHAR text
[] = L
"8";
240 // Configure the font to draw with Marlett, keeping the current background color as-is
241 SelectObject(cdraw
->nmcd
.hdc
, m_marlett
);
242 SetBkMode(cdraw
->nmcd
.hdc
, TRANSPARENT
);
244 // Tweak the alignment by 1 pixel so the menu draws like the Windows start menu.
245 RECT rc
= cdraw
->nmcd
.rc
;
248 // The arrow is drawn at the right of the item's rect, aligned vertically.
249 DrawTextEx(cdraw
->nmcd
.hdc
, text
, 1, &rc
, DT_NOCLIP
| DT_VCENTER
| DT_RIGHT
| DT_SINGLELINE
, NULL
);
257 CMenuToolbarBase::CMenuToolbarBase(CMenuBand
*menuBand
, BOOL usePager
) :
259 m_useFlatMenus(FALSE
),
261 m_disableMouseTrack(FALSE
),
262 m_timerEnabled(FALSE
),
263 m_menuBand(menuBand
),
266 m_usePager(usePager
),
271 m_isTrackingPopup(FALSE
),
272 m_cancelingPopup(FALSE
)
278 m_marlett
= CreateFont(
279 0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET
,
280 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
281 DEFAULT_QUALITY
, FF_DONTCARE
, L
"Marlett");
284 CMenuToolbarBase::~CMenuToolbarBase()
290 m_pager
.DestroyWindow();
292 DeleteObject(m_marlett
);
295 void CMenuToolbarBase::InvalidateDraw()
297 InvalidateRect(NULL
, FALSE
);
300 HRESULT
CMenuToolbarBase::ShowDW(BOOL fShow
)
302 ShowWindow(fShow
? SW_SHOW
: SW_HIDE
);
304 // Ensure that the right image list is assigned to the toolbar
307 // For custom-drawing
308 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
313 HRESULT
CMenuToolbarBase::UpdateImageLists()
315 if ((m_initFlags
& (SMINIT_TOPLEVEL
| SMINIT_VERTICAL
)) == SMINIT_TOPLEVEL
) // not vertical.
317 // No image list, prevents the buttons from having a margin at the left side
322 // Assign the correct imagelist and padding based on the current icon size
325 if (m_menuBand
->UseBigIcons())
337 HRESULT hr
= SHGetImageList(shiml
, IID_PPV_ARG(IImageList
, &piml
));
338 if (FAILED_UNEXPECTEDLY(hr
))
344 SetImageList((HIMAGELIST
)piml
);
349 HRESULT
CMenuToolbarBase::Close()
355 m_pager
.DestroyWindow();
360 HRESULT
CMenuToolbarBase::CreateToolbar(HWND hwndParent
, DWORD dwFlags
)
362 LONG tbStyles
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
|
363 TBSTYLE_TOOLTIPS
| TBSTYLE_TRANSPARENT
| TBSTYLE_REGISTERDROP
| TBSTYLE_LIST
| TBSTYLE_FLAT
| TBSTYLE_CUSTOMERASE
|
364 CCS_NODIVIDER
| CCS_NOPARENTALIGN
| CCS_NORESIZE
| CCS_TOP
;
365 LONG tbExStyles
= TBSTYLE_EX_DOUBLEBUFFER
| WS_EX_TOOLWINDOW
;
367 if (dwFlags
& SMINIT_VERTICAL
)
369 // Activate vertical semantics
370 tbStyles
|= CCS_VERT
;
372 #if USE_TBSTYLE_EX_VERTICAL
373 tbExStyles
|= TBSTYLE_EX_VERTICAL
;
377 m_initFlags
= dwFlags
;
379 // Get a temporary rect to use while creating the toolbar window.
380 // Ensure that it is not a null rect.
382 if (!::GetClientRect(hwndParent
, &rc
) ||
383 (rc
.left
== rc
.right
) ||
384 (rc
.top
== rc
.bottom
))
392 SubclassWindow(Create(hwndParent
, tbStyles
, tbExStyles
));
394 // If needed, create the pager.
397 LONG pgStyles
= PGS_VERT
| WS_CHILD
| WS_VISIBLE
;
400 HWND hwndPager
= CreateWindowEx(
401 pgExStyles
, WC_PAGESCROLLER
, NULL
,
402 pgStyles
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
403 hwndParent
, NULL
, _AtlBaseModule
.GetModuleInstance(), 0);
405 m_pager
.SubclassWindow(hwndPager
);
407 ::SetParent(m_hWnd
, hwndPager
);
409 m_pager
.SendMessageW(PGM_SETCHILD
, 0, reinterpret_cast<LPARAM
>(m_hWnd
));
412 // Configure the image lists
418 HRESULT
CMenuToolbarBase::GetSizes(SIZE
* pMinSize
, SIZE
* pMaxSize
, SIZE
* pIntegralSize
)
421 *pMinSize
= m_idealSize
;
423 *pMaxSize
= m_idealSize
;
425 *pIntegralSize
= m_itemSize
;
430 TRACE("Sizes out of date, recalculating.\n");
437 // Obtain the ideal size, to be used as min and max
438 GetMaxSize(&m_idealSize
);
439 GetIdealSize((m_initFlags
& SMINIT_VERTICAL
) != 0, &m_idealSize
);
441 TRACE("Ideal Size: (%d, %d) for %d buttons\n", m_idealSize
, GetButtonCount());
443 // Obtain the button size, to be used as the integral size
444 DWORD size
= GetButtonSize();
445 m_itemSize
.cx
= GET_X_LPARAM(size
);
446 m_itemSize
.cy
= GET_Y_LPARAM(size
);
450 *pMinSize
= m_idealSize
;
452 *pMaxSize
= m_idealSize
;
454 *pIntegralSize
= m_itemSize
;
459 HRESULT
CMenuToolbarBase::SetPosSize(int x
, int y
, int cx
, int cy
)
461 // Update the toolbar or pager to fit the requested rect
462 // If we have a pager, set the toolbar height to the ideal height of the toolbar
465 SetWindowPos(NULL
, x
, y
, cx
, m_idealSize
.cy
, 0);
466 m_pager
.SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
470 SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
473 // In a vertical menu, resize the buttons to fit the width
474 if (m_initFlags
& SMINIT_VERTICAL
)
476 DWORD btnSize
= GetButtonSize();
477 SetButtonSize(cx
, GET_Y_LPARAM(btnSize
));
483 HRESULT
CMenuToolbarBase::IsWindowOwner(HWND hwnd
)
485 if (m_hWnd
&& m_hWnd
== hwnd
) return S_OK
;
486 if (m_pager
.m_hWnd
&& m_pager
.m_hWnd
== hwnd
) return S_OK
;
490 HRESULT
CMenuToolbarBase::GetWindow(HWND
*phwnd
)
496 *phwnd
= m_pager
.m_hWnd
;
503 HRESULT
CMenuToolbarBase::OnGetInfoTip(NMTBGETINFOTIP
* tip
)
508 INT iItem
= tip
->iItem
;
510 GetDataFromId(iItem
, &index
, &dwData
);
512 return InternalGetTooltip(iItem
, index
, dwData
, tip
->pszText
, tip
->cchTextMax
);
515 HRESULT
CMenuToolbarBase::OnPopupTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
517 if (wParam
!= TIMERID_HOTTRACK
)
523 KillTimer(TIMERID_HOTTRACK
);
528 m_timerEnabled
= FALSE
;
533 // Returns S_FALSE if the current item did not show a submenu
534 HRESULT hr
= PopupItem(m_hotItem
, FALSE
);
538 // If we didn't switch submenus, cancel the current popup regardless
541 HRESULT hr
= CancelCurrentPopup();
542 if (FAILED_UNEXPECTEDLY(hr
))
549 HRESULT
CMenuToolbarBase::KillPopupTimer()
553 m_timerEnabled
= FALSE
;
554 KillTimer(TIMERID_HOTTRACK
);
560 HRESULT
CMenuToolbarBase::ChangeHotItem(CMenuToolbarBase
* toolbar
, INT item
, DWORD dwFlags
)
562 // Ignore the change if it already matches the stored info
563 if (m_hotBar
== toolbar
&& m_hotItem
== item
)
566 // Prevent a change of hot item if the change was triggered by the mouse,
567 // and mouse tracking is disabled.
568 if (m_disableMouseTrack
&& dwFlags
& HICF_MOUSE
)
570 TRACE("Hot item change prevented by DisableMouseTrack\n");
574 // Notify the toolbar if the hot-tracking left this toolbar
575 if (m_hotBar
== this && toolbar
!= this)
580 TRACE("Hot item changed from %p %p, to %p %p\n", m_hotBar
, m_hotItem
, toolbar
, item
);
584 if (m_hotBar
== this)
586 if (m_isTrackingPopup
&& !(m_initFlags
& SMINIT_VERTICAL
))
588 // If the menubar has an open submenu, switch to the new item's submenu immediately
589 PopupItem(m_hotItem
, FALSE
);
591 else if (dwFlags
& HICF_MOUSE
)
593 // Vertical menus show/hide the submenu after a delay,
594 // but only with the mouse.
595 if (m_initFlags
& SMINIT_VERTICAL
)
598 SystemParametersInfo(SPI_GETMENUSHOWDELAY
, 0, &elapsed
, 0);
599 SetTimer(TIMERID_HOTTRACK
, elapsed
);
600 m_timerEnabled
= TRUE
;
601 TRACE("SetTimer called with m_hotItem=%d\n", m_hotItem
);
607 info
.cbSize
= sizeof(info
);
610 int index
= GetButtonInfo(item
, &info
);
620 HRESULT
CMenuToolbarBase::ChangePopupItem(CMenuToolbarBase
* toolbar
, INT item
)
622 // Ignore the change if it already matches the stored info
623 if (m_popupBar
== toolbar
&& m_popupItem
== item
)
626 // Notify the toolbar if the popup-tracking this toolbar
627 if (m_popupBar
== this && toolbar
!= this)
629 CheckButton(m_popupItem
, FALSE
);
630 m_isTrackingPopup
= FALSE
;
633 m_popupBar
= toolbar
;
636 if (m_popupBar
== this)
638 CheckButton(m_popupItem
, TRUE
);
645 LRESULT
CMenuToolbarBase::IsTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
649 if (m_hotBar
!= this)
655 if (!GetButton(wParam
, &btn
))
658 if (m_hotItem
== btn
.idCommand
)
661 if (m_popupItem
== btn
.idCommand
)
667 LRESULT
CMenuToolbarBase::ChangeTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
669 BOOL wasTracking
= LOWORD(lParam
);
670 BOOL mouse
= HIWORD(lParam
);
676 m_isTrackingPopup
= FALSE
;
677 return m_menuBand
->_ChangeHotItem(NULL
, -1, HICF_MOUSE
);
680 if (!GetButton(wParam
, &btn
))
683 TRACE("ChangeTrackedItem %d, %d\n", wParam
, wasTracking
);
684 m_isTrackingPopup
= wasTracking
;
685 return m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, mouse
? HICF_MOUSE
: 0);
688 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, IShellMenu
* childShellMenu
, BOOL keyInitiated
)
690 // Calculate the submenu position and exclude area
694 if (!GetItemRect(index
, &rc
))
699 GetWindowRect(topWnd
, &rcx
);
701 POINT a
= { rc
.left
, rc
.top
};
702 POINT b
= { rc
.right
, rc
.bottom
};
703 POINT c
= { rcx
.left
, rcx
.top
};
704 POINT d
= { rcx
.right
, rcx
.bottom
};
706 ClientToScreen(m_hWnd
, &a
);
707 ClientToScreen(m_hWnd
, &b
);
708 ClientToScreen(topWnd
, &c
);
709 ClientToScreen(topWnd
, &d
);
711 POINTL pt
= { a
.x
, b
.y
};
712 RECTL rcl
= { c
.x
, c
.y
, d
.x
, d
.y
};
714 if (m_initFlags
& SMINIT_VERTICAL
)
720 // Display the submenu
721 m_isTrackingPopup
= TRUE
;
723 m_menuBand
->_ChangePopupItem(this, iItem
);
724 m_menuBand
->_OnPopupSubMenu(childShellMenu
, &pt
, &rcl
, keyInitiated
);
729 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, HMENU menu
)
731 // Calculate the submenu position and exclude area
734 if (!GetItemRect(index
, &rc
))
737 POINT a
= { rc
.left
, rc
.top
};
738 POINT b
= { rc
.right
, rc
.bottom
};
740 ClientToScreen(m_hWnd
, &a
);
741 ClientToScreen(m_hWnd
, &b
);
743 POINT pt
= { a
.x
, b
.y
};
744 RECT rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
746 if (m_initFlags
& SMINIT_VERTICAL
)
752 HMENU popup
= GetSubMenu(menu
, index
);
754 // Display the submenu
755 m_isTrackingPopup
= TRUE
;
756 m_menuBand
->_ChangePopupItem(this, iItem
);
757 m_menuBand
->_TrackSubMenu(popup
, pt
.x
, pt
.y
, rcl
);
758 m_menuBand
->_ChangePopupItem(NULL
, -1);
759 m_isTrackingPopup
= FALSE
;
764 HRESULT
CMenuToolbarBase::TrackContextMenu(IContextMenu
* contextMenu
, POINT pt
)
767 m_menuBand
->_KillPopupTimers();
769 m_menuBand
->_CancelCurrentPopup();
771 // Display the context menu
772 return m_menuBand
->_TrackContextMenu(contextMenu
, pt
.x
, pt
.y
);
775 HRESULT
CMenuToolbarBase::BeforeCancelPopup()
777 m_cancelingPopup
= TRUE
;
778 DbgPrint("BeforeCancelPopup\n");
782 HRESULT
CMenuToolbarBase::ProcessClick(INT iItem
)
784 if (m_disableMouseTrack
)
786 TRACE("Item click prevented by DisableMouseTrack\n");
790 // If a button is clicked while a submenu was open, cancel the submenu.
791 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
793 TRACE("OnCommand cancelled because it was tracking submenu.\n");
797 if (PopupItem(iItem
, FALSE
) == S_OK
)
799 TRACE("PopupItem returned S_OK\n");
803 TRACE("Executing...\n");
805 return m_menuBand
->_MenuItemHotTrack(MPOS_EXECUTE
);
808 HRESULT
CMenuToolbarBase::MenuBarMouseDown(INT iIndex
)
812 if ((m_initFlags
& SMINIT_VERTICAL
)
816 m_cancelingPopup
= FALSE
;
820 GetButton(iIndex
, &btn
);
821 return ProcessClick(btn
.idCommand
);
824 HRESULT
CMenuToolbarBase::MenuBarMouseUp(INT iIndex
)
828 m_cancelingPopup
= FALSE
;
830 if (!(m_initFlags
& SMINIT_VERTICAL
))
833 GetButton(iIndex
, &btn
);
834 return ProcessClick(btn
.idCommand
);
837 HRESULT
CMenuToolbarBase::PrepareExecuteItem(INT iItem
)
839 this->m_menuBand
->_KillPopupTimers();
841 m_executeItem
= iItem
;
842 return GetDataFromId(iItem
, &m_executeIndex
, &m_executeData
);
845 HRESULT
CMenuToolbarBase::ExecuteItem()
847 return InternalExecuteItem(m_executeItem
, m_executeItem
, m_executeData
);
850 HRESULT
CMenuToolbarBase::OnContextMenu(NMMOUSE
* rclick
)
852 INT iItem
= rclick
->dwItemSpec
;
853 INT index
= rclick
->dwHitInfo
;
854 DWORD_PTR data
= rclick
->dwItemData
;
856 GetDataFromId(iItem
, &index
, &data
);
858 return InternalContextMenu(iItem
, index
, data
, rclick
->pt
);
861 HRESULT
CMenuToolbarBase::KeyboardItemChange(DWORD dwSelectType
)
863 int prev
= m_hotItem
;
866 if (dwSelectType
!= 0xFFFFFFFF)
868 int count
= GetButtonCount();
870 if (dwSelectType
== VK_HOME
)
873 dwSelectType
= VK_DOWN
;
875 else if (dwSelectType
== VK_END
)
878 dwSelectType
= VK_UP
;
884 TBBUTTONINFO info
= { 0 };
885 info
.cbSize
= sizeof(TBBUTTONINFO
);
887 index
= GetButtonInfo(m_hotItem
, &info
);
892 if (dwSelectType
== VK_UP
)
896 else if (dwSelectType
== VK_DOWN
)
903 if (dwSelectType
== VK_UP
)
907 else if (dwSelectType
== VK_DOWN
)
914 TBBUTTON btn
= { 0 };
915 while (index
>= 0 && index
< count
)
917 DWORD res
= GetButton(index
, &btn
);
923 if (prev
!= btn
.idCommand
)
925 TRACE("Setting Hot item to %d\n", index
);
926 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
929 m_menuBand
->_GetTopLevelWindow(&tlw
);
930 SendMessageW(tlw
, WM_CANCELMODE
, 0, 0);
931 PostMessageW(m_hWnd
, WM_USER_CHANGETRACKEDITEM
, index
, MAKELPARAM(m_isTrackingPopup
, FALSE
));
934 m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, 0);
939 if (dwSelectType
== VK_UP
)
943 else if (dwSelectType
== VK_DOWN
)
954 TRACE("Setting Hot item to null\n");
955 m_menuBand
->_ChangeHotItem(NULL
, -1, 0);
961 HRESULT
CMenuToolbarBase::AddButton(DWORD commandId
, LPCWSTR caption
, BOOL hasSubMenu
, INT iconId
, DWORD_PTR buttonData
, BOOL last
)
963 TBBUTTON tbb
= { 0 };
965 tbb
.fsState
= TBSTATE_ENABLED
;
966 #if !USE_TBSTYLE_EX_VERTICAL
967 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
968 tbb
.fsState
|= TBSTATE_WRAP
;
970 tbb
.fsStyle
= BTNS_CHECKGROUP
;
972 if (hasSubMenu
&& (m_initFlags
& SMINIT_VERTICAL
))
973 tbb
.fsStyle
|= BTNS_DROPDOWN
;
975 if (!(m_initFlags
& SMINIT_VERTICAL
))
976 tbb
.fsStyle
|= BTNS_AUTOSIZE
;
978 tbb
.iString
= (INT_PTR
) caption
;
979 tbb
.idCommand
= commandId
;
981 tbb
.iBitmap
= iconId
;
982 tbb
.dwData
= buttonData
;
986 if (!AddButtons(1, &tbb
))
987 return HRESULT_FROM_WIN32(GetLastError());
991 HRESULT
CMenuToolbarBase::AddSeparator(BOOL last
)
993 TBBUTTON tbb
= { 0 };
995 tbb
.fsState
= TBSTATE_ENABLED
;
996 #if !USE_TBSTYLE_EX_VERTICAL
997 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
998 tbb
.fsState
|= TBSTATE_WRAP
;
1000 tbb
.fsStyle
= BTNS_SEP
;
1005 if (!AddButtons(1, &tbb
))
1006 return HRESULT_FROM_WIN32(GetLastError());
1011 HRESULT
CMenuToolbarBase::AddPlaceholder()
1013 TBBUTTON tbb
= { 0 };
1014 WCHAR MenuString
[128];
1016 LoadStringW(GetModuleHandle(L
"shell32.dll"), IDS_MENU_EMPTY
, MenuString
, _countof(MenuString
));
1020 tbb
.iString
= (INT_PTR
) MenuString
;
1025 if (!AddButtons(1, &tbb
))
1026 return HRESULT_FROM_WIN32(GetLastError());
1031 HRESULT
CMenuToolbarBase::ClearToolbar()
1033 while (DeleteButton(0))
1041 HRESULT
CMenuToolbarBase::GetDataFromId(INT iItem
, INT
* pIndex
, DWORD_PTR
* pData
)
1052 TBBUTTONINFO info
= { 0 };
1054 info
.cbSize
= sizeof(TBBUTTONINFO
);
1055 info
.dwMask
= TBIF_COMMAND
| TBIF_LPARAM
;
1057 int index
= GetButtonInfo(iItem
, &info
);
1065 *pData
= info
.lParam
;
1070 HRESULT
CMenuToolbarBase::CancelCurrentPopup()
1072 return m_menuBand
->_CancelCurrentPopup();
1075 HRESULT
CMenuToolbarBase::PopupItem(INT iItem
, BOOL keyInitiated
)
1083 if (m_popupBar
== this && m_popupItem
== iItem
)
1086 GetDataFromId(iItem
, &index
, &dwData
);
1088 HRESULT hr
= InternalHasSubMenu(iItem
, index
, dwData
);
1094 HRESULT hr
= CancelCurrentPopup();
1095 if (FAILED_UNEXPECTEDLY(hr
))
1099 if (!(m_initFlags
& SMINIT_VERTICAL
))
1101 TRACE("PopupItem non-vertical %d %d\n", index
, iItem
);
1102 m_menuBand
->_ChangeHotItem(this, iItem
, 0);
1105 return InternalPopupItem(iItem
, index
, dwData
, keyInitiated
);
1108 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand
*menuBand
) :
1109 CMenuToolbarBase(menuBand
, FALSE
),
1115 CMenuStaticToolbar::~CMenuStaticToolbar()
1119 HRESULT
CMenuStaticToolbar::GetMenu(
1120 _Out_opt_ HMENU
*phmenu
,
1121 _Out_opt_ HWND
*phwnd
,
1122 _Out_opt_ DWORD
*pdwFlags
)
1127 *phwnd
= m_hwndMenu
;
1129 *pdwFlags
= m_dwMenuFlags
;
1134 HRESULT
CMenuStaticToolbar::SetMenu(
1141 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
;
1360 HRESULT
CMenuSFToolbar::GetShellFolder(DWORD
*pdwFlags
, LPITEMIDLIST
*ppidl
, REFIID riid
, void **ppv
)
1364 hr
= m_shellFolder
->QueryInterface(riid
, ppv
);
1365 if (FAILED_UNEXPECTEDLY(hr
))
1369 *pdwFlags
= m_dwMenuFlags
;
1373 LPITEMIDLIST pidl
= NULL
;
1377 pidl
= ILClone(m_idList
);
1380 ERR("ILClone failed!\n");
1381 (*reinterpret_cast<IUnknown
**>(ppv
))->Release();
1392 HRESULT
CMenuSFToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1395 CComPtr
<IContextMenu
> contextMenu
= NULL
;
1396 LPCITEMIDLIST pidl
= reinterpret_cast<LPCITEMIDLIST
>(dwData
);
1398 hr
= m_shellFolder
->GetUIObjectOf(GetToolbar(), 1, &pidl
, IID_NULL_PPV_ARG(IContextMenu
, &contextMenu
));
1399 if (FAILED_UNEXPECTEDLY(hr
))
1404 hr
= TrackContextMenu(contextMenu
, pt
);
1409 HRESULT
CMenuSFToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1411 return m_menuBand
->_CallCBWithItemPidl(reinterpret_cast<LPITEMIDLIST
>(data
), SMC_SFEXEC
, 0, 0);
1414 HRESULT
CMenuSFToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1420 CComPtr
<IShellMenuCallback
> psmc
;
1421 CComPtr
<IShellMenu
> shellMenu
;
1423 LPITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1428 hr
= CMenuBand_Constructor(IID_PPV_ARG(IShellMenu
, &shellMenu
));
1429 if (FAILED_UNEXPECTEDLY(hr
))
1432 m_menuBand
->GetMenuInfo(&psmc
, &uId
, &uIdAncestor
, &flags
);
1434 // FIXME: not sure what to use as uId/uIdAncestor here
1435 hr
= shellMenu
->Initialize(psmc
, 0, uId
, SMINIT_VERTICAL
);
1436 if (FAILED_UNEXPECTEDLY(hr
))
1439 CComPtr
<IShellFolder
> childFolder
;
1440 hr
= m_shellFolder
->BindToObject(pidl
, NULL
, IID_PPV_ARG(IShellFolder
, &childFolder
));
1441 if (FAILED_UNEXPECTEDLY(hr
))
1444 hr
= shellMenu
->SetShellFolder(childFolder
, NULL
, NULL
, 0);
1445 if (FAILED_UNEXPECTEDLY(hr
))
1448 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1451 HRESULT
CMenuSFToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1454 LPCITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1456 SFGAOF attrs
= SFGAO_FOLDER
;
1457 hr
= m_shellFolder
->GetAttributesOf(1, &pidl
, &attrs
);
1458 if (FAILED_UNEXPECTEDLY(hr
))
1461 return (attrs
& SFGAO_FOLDER
) ? S_OK
: S_FALSE
;