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
20 #include "shellmenu.h"
21 #include <commoncontrols.h>
22 #include <shlwapi_undoc.h>
26 #include "CMenuBand.h"
27 #include "CMenuToolbars.h"
29 #define IDS_MENU_EMPTY 34561
31 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
32 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
34 WINE_DEFAULT_DEBUG_CHANNEL(CMenuToolbars
);
36 // FIXME: Enable if/when wine comctl supports this flag properly
37 #define USE_TBSTYLE_EX_VERTICAL 0
39 // User-defined timer ID used while hot-tracking around the menu
40 #define TIMERID_HOTTRACK 1
42 LRESULT
CMenuToolbarBase::OnWinEventWrap(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
45 bHandled
= OnWinEvent(m_hWnd
, uMsg
, wParam
, lParam
, &lr
) != S_FALSE
;
49 HRESULT
CMenuToolbarBase::OnWinEvent(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*theResult
)
57 //return OnCommand(wParam, lParam, theResult);
61 hdr
= reinterpret_cast<LPNMHDR
>(lParam
);
64 case TBN_DELETINGBUTTON
:
65 return OnDeletingButton(reinterpret_cast<LPNMTOOLBAR
>(hdr
));
68 return OnPagerCalcSize(reinterpret_cast<LPNMPGCALCSIZE
>(hdr
));
71 return ProcessClick(reinterpret_cast<LPNMTOOLBAR
>(hdr
)->iItem
);
73 case TBN_HOTITEMCHANGE
:
74 //return OnHotItemChange(reinterpret_cast<LPNMTBHOTITEM>(hdr), theResult);
78 return OnCustomDraw(reinterpret_cast<LPNMTBCUSTOMDRAW
>(hdr
), theResult
);
81 return OnGetInfoTip(reinterpret_cast<LPNMTBGETINFOTIP
>(hdr
));
83 // Silence unhandled items so that they don't print as unknown
90 case NM_RELEASEDCAPTURE
:
105 case NM_TOOLTIPSCREATED
:
108 case TBN_DRAGOUT
: return S_FALSE
;
111 TRACE("WM_NOTIFY unknown code %d, %d\n", hdr
->code
, hdr
->idFrom
);
115 case WM_WININICHANGE
:
116 if (wParam
== SPI_SETFLATMENU
)
118 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
125 HRESULT
CMenuToolbarBase::DisableMouseTrack(BOOL bDisable
)
127 if (m_disableMouseTrack
!= bDisable
)
129 m_disableMouseTrack
= bDisable
;
130 TRACE("DisableMouseTrack %d\n", bDisable
);
135 HRESULT
CMenuToolbarBase::OnPagerCalcSize(LPNMPGCALCSIZE csize
)
138 GetSizes(NULL
, &tbs
, NULL
);
139 if (csize
->dwFlag
== PGF_CALCHEIGHT
)
141 csize
->iHeight
= tbs
.cy
;
143 else if (csize
->dwFlag
== PGF_CALCWIDTH
)
145 csize
->iWidth
= tbs
.cx
;
150 HRESULT
CMenuToolbarBase::OnCustomDraw(LPNMTBCUSTOMDRAW cdraw
, LRESULT
* theResult
)
155 switch (cdraw
->nmcd
.dwDrawStage
)
158 *theResult
= CDRF_NOTIFYITEMDRAW
;
161 case CDDS_ITEMPREPAINT
:
163 // The item with an active submenu gets the CHECKED flag.
164 isHot
= m_hotBar
== this && (int) cdraw
->nmcd
.dwItemSpec
== m_hotItem
;
165 isPopup
= m_popupBar
== this && (int) cdraw
->nmcd
.dwItemSpec
== m_popupItem
;
167 if (m_hotItem
< 0 && isPopup
)
170 if ((m_useFlatMenus
&& isHot
) || (m_initFlags
& SMINIT_VERTICAL
))
174 RECT rc
= cdraw
->nmcd
.rc
;
175 HDC hdc
= cdraw
->nmcd
.hdc
;
177 // Remove HOT and CHECKED flags (will restore HOT if necessary)
178 cdraw
->nmcd
.uItemState
&= ~(CDIS_HOT
| CDIS_CHECKED
);
180 // Decide on the colors
183 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
185 clrText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
186 bgBrush
= GetSysColorBrush(m_useFlatMenus
? COLOR_MENUHILIGHT
: COLOR_HIGHLIGHT
);
190 clrText
= GetSysColor(COLOR_MENUTEXT
);
191 bgBrush
= GetSysColorBrush(COLOR_MENU
);
194 // Paint the background color with the selected color
195 FillRect(hdc
, &rc
, bgBrush
);
197 // Set the text color in advance, this color will be assigned when the ITEMPOSTPAINT triggers
198 SetTextColor(hdc
, clrText
);
200 // Set the text color, will be used by the internal drawing code
201 cdraw
->clrText
= clrText
;
202 cdraw
->iListGap
+= 4;
204 // Tell the default drawing code we don't want any fanciness, not even a background.
205 *theResult
= CDRF_NOTIFYPOSTPAINT
| TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| 0x00800000; // FIXME: the last bit is Vista+, useful for debugging only
209 // Remove HOT and CHECKED flags (will restore HOT if necessary)
210 cdraw
->nmcd
.uItemState
&= ~CDIS_HOT
;
212 // Decide on the colors
215 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
223 case CDDS_ITEMPOSTPAINT
:
225 // Fetch the button style
226 btni
.cbSize
= sizeof(btni
);
227 btni
.dwMask
= TBIF_STYLE
;
228 GetButtonInfo(cdraw
->nmcd
.dwItemSpec
, &btni
);
230 // Check if we need to draw a submenu arrow
231 if (btni
.fsStyle
& BTNS_DROPDOWN
)
233 // TODO: Support RTL text modes by drawing a leftwards arrow aligned to the left of the control
235 // "8" is the rightwards dropdown arrow in the Marlett font
236 WCHAR text
[] = L
"8";
238 // Configure the font to draw with Marlett, keeping the current background color as-is
239 SelectObject(cdraw
->nmcd
.hdc
, m_marlett
);
240 SetBkMode(cdraw
->nmcd
.hdc
, TRANSPARENT
);
242 // Tweak the alignment by 1 pixel so the menu draws like the Windows start menu.
243 RECT rc
= cdraw
->nmcd
.rc
;
246 // The arrow is drawn at the right of the item's rect, aligned vertically.
247 DrawTextEx(cdraw
->nmcd
.hdc
, text
, 1, &rc
, DT_NOCLIP
| DT_VCENTER
| DT_RIGHT
| DT_SINGLELINE
, NULL
);
255 CMenuToolbarBase::CMenuToolbarBase(CMenuBand
*menuBand
, BOOL usePager
) :
257 m_useFlatMenus(FALSE
),
258 m_disableMouseTrack(FALSE
),
259 m_timerEnabled(FALSE
),
260 m_menuBand(menuBand
),
263 m_usePager(usePager
),
268 m_isTrackingPopup(FALSE
),
269 m_cancelingPopup(FALSE
)
275 m_marlett
= CreateFont(
276 0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET
,
277 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
278 DEFAULT_QUALITY
, FF_DONTCARE
, L
"Marlett");
281 CMenuToolbarBase::~CMenuToolbarBase()
287 m_pager
.DestroyWindow();
289 DeleteObject(m_marlett
);
292 void CMenuToolbarBase::InvalidateDraw()
294 InvalidateRect(NULL
, FALSE
);
297 HRESULT
CMenuToolbarBase::ShowDW(BOOL fShow
)
299 ShowWindow(fShow
? SW_SHOW
: SW_HIDE
);
301 // Ensure that the right image list is assigned to the toolbar
304 // For custom-drawing
305 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
310 HRESULT
CMenuToolbarBase::UpdateImageLists()
312 if ((m_initFlags
& (SMINIT_TOPLEVEL
| SMINIT_VERTICAL
)) == SMINIT_TOPLEVEL
) // not vertical.
314 // No image list, prevents the buttons from having a margin at the left side
319 // Assign the correct imagelist and padding based on the current icon size
322 if (m_menuBand
->UseBigIcons())
334 HRESULT hr
= SHGetImageList(shiml
, IID_PPV_ARG(IImageList
, &piml
));
335 if (FAILED_UNEXPECTEDLY(hr
))
341 SetImageList((HIMAGELIST
)piml
);
346 HRESULT
CMenuToolbarBase::Close()
352 m_pager
.DestroyWindow();
357 HRESULT
CMenuToolbarBase::CreateToolbar(HWND hwndParent
, DWORD dwFlags
)
359 LONG tbStyles
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
|
360 TBSTYLE_TOOLTIPS
| TBSTYLE_TRANSPARENT
| TBSTYLE_REGISTERDROP
| TBSTYLE_LIST
| TBSTYLE_FLAT
| TBSTYLE_CUSTOMERASE
|
361 CCS_NODIVIDER
| CCS_NOPARENTALIGN
| CCS_NORESIZE
| CCS_TOP
;
362 LONG tbExStyles
= TBSTYLE_EX_DOUBLEBUFFER
| WS_EX_TOOLWINDOW
;
364 if (dwFlags
& SMINIT_VERTICAL
)
366 // Activate vertical semantics
367 tbStyles
|= CCS_VERT
;
369 #if USE_TBSTYLE_EX_VERTICAL
370 tbExStyles
|= TBSTYLE_EX_VERTICAL
;
374 m_initFlags
= dwFlags
;
376 // Get a temporary rect to use while creating the toolbar window.
377 // Ensure that it is not a null rect.
379 if (!::GetClientRect(hwndParent
, &rc
) ||
380 (rc
.left
== rc
.right
) ||
381 (rc
.top
== rc
.bottom
))
389 SubclassWindow(CToolbar::Create(hwndParent
, tbStyles
, tbExStyles
));
391 SetWindowTheme(m_hWnd
, L
"", L
"");
393 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
395 m_menuBand
->AdjustForTheme(m_useFlatMenus
);
397 // If needed, create the pager.
400 LONG pgStyles
= PGS_VERT
| WS_CHILD
| WS_VISIBLE
;
403 HWND hwndPager
= CreateWindowEx(
404 pgExStyles
, WC_PAGESCROLLER
, NULL
,
405 pgStyles
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
406 hwndParent
, NULL
, _AtlBaseModule
.GetModuleInstance(), 0);
408 m_pager
.SubclassWindow(hwndPager
);
410 ::SetParent(m_hWnd
, hwndPager
);
412 m_pager
.SendMessageW(PGM_SETCHILD
, 0, reinterpret_cast<LPARAM
>(m_hWnd
));
415 // Configure the image lists
421 HRESULT
CMenuToolbarBase::GetSizes(SIZE
* pMinSize
, SIZE
* pMaxSize
, SIZE
* pIntegralSize
)
424 *pMinSize
= m_idealSize
;
426 *pMaxSize
= m_idealSize
;
428 *pIntegralSize
= m_itemSize
;
433 TRACE("Sizes out of date, recalculating.\n");
440 // Obtain the ideal size, to be used as min and max
441 GetMaxSize(&m_idealSize
);
442 GetIdealSize((m_initFlags
& SMINIT_VERTICAL
) != 0, &m_idealSize
);
444 TRACE("Ideal Size: (%d, %d) for %d buttons\n", m_idealSize
, GetButtonCount());
446 // Obtain the button size, to be used as the integral size
447 DWORD size
= GetButtonSize();
448 m_itemSize
.cx
= GET_X_LPARAM(size
);
449 m_itemSize
.cy
= GET_Y_LPARAM(size
);
453 *pMinSize
= m_idealSize
;
455 *pMaxSize
= m_idealSize
;
457 *pIntegralSize
= m_itemSize
;
462 HRESULT
CMenuToolbarBase::SetPosSize(int x
, int y
, int cx
, int cy
)
464 // Update the toolbar or pager to fit the requested rect
465 // If we have a pager, set the toolbar height to the ideal height of the toolbar
468 SetWindowPos(NULL
, x
, y
, cx
, m_idealSize
.cy
, 0);
469 m_pager
.SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
473 SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
476 // In a vertical menu, resize the buttons to fit the width
477 if (m_initFlags
& SMINIT_VERTICAL
)
479 DWORD btnSize
= GetButtonSize();
480 SetButtonSize(cx
, GET_Y_LPARAM(btnSize
));
486 HRESULT
CMenuToolbarBase::IsWindowOwner(HWND hwnd
)
488 if (m_hWnd
&& m_hWnd
== hwnd
) return S_OK
;
489 if (m_pager
.m_hWnd
&& m_pager
.m_hWnd
== hwnd
) return S_OK
;
493 HRESULT
CMenuToolbarBase::GetWindow(HWND
*phwnd
)
499 *phwnd
= m_pager
.m_hWnd
;
506 HRESULT
CMenuToolbarBase::OnGetInfoTip(NMTBGETINFOTIP
* tip
)
511 INT iItem
= tip
->iItem
;
513 GetDataFromId(iItem
, &index
, &dwData
);
515 return InternalGetTooltip(iItem
, index
, dwData
, tip
->pszText
, tip
->cchTextMax
);
518 HRESULT
CMenuToolbarBase::OnPopupTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
520 if (wParam
!= TIMERID_HOTTRACK
)
526 KillTimer(TIMERID_HOTTRACK
);
531 m_timerEnabled
= FALSE
;
536 // Returns S_FALSE if the current item did not show a submenu
537 HRESULT hr
= PopupItem(m_hotItem
, FALSE
);
541 // If we didn't switch submenus, cancel the current popup regardless
544 HRESULT hr
= CancelCurrentPopup();
545 if (FAILED_UNEXPECTEDLY(hr
))
552 HRESULT
CMenuToolbarBase::KillPopupTimer()
556 m_timerEnabled
= FALSE
;
557 KillTimer(TIMERID_HOTTRACK
);
563 HRESULT
CMenuToolbarBase::ChangeHotItem(CMenuToolbarBase
* toolbar
, INT item
, DWORD dwFlags
)
565 // Ignore the change if it already matches the stored info
566 if (m_hotBar
== toolbar
&& m_hotItem
== item
)
569 // Prevent a change of hot item if the change was triggered by the mouse,
570 // and mouse tracking is disabled.
571 if (m_disableMouseTrack
&& dwFlags
& HICF_MOUSE
)
573 TRACE("Hot item change prevented by DisableMouseTrack\n");
577 // Notify the toolbar if the hot-tracking left this toolbar
578 if (m_hotBar
== this && toolbar
!= this)
583 TRACE("Hot item changed from %p %p, to %p %p\n", m_hotBar
, m_hotItem
, toolbar
, item
);
587 if (m_hotBar
== this)
589 if (m_isTrackingPopup
&& !(m_initFlags
& SMINIT_VERTICAL
))
591 // If the menubar has an open submenu, switch to the new item's submenu immediately
592 PopupItem(m_hotItem
, FALSE
);
594 else if (dwFlags
& HICF_MOUSE
)
596 // Vertical menus show/hide the submenu after a delay,
597 // but only with the mouse.
598 if (m_initFlags
& SMINIT_VERTICAL
)
601 SystemParametersInfo(SPI_GETMENUSHOWDELAY
, 0, &elapsed
, 0);
602 SetTimer(TIMERID_HOTTRACK
, elapsed
);
603 m_timerEnabled
= TRUE
;
604 TRACE("SetTimer called with m_hotItem=%d\n", m_hotItem
);
610 info
.cbSize
= sizeof(info
);
613 int index
= GetButtonInfo(item
, &info
);
623 HRESULT
CMenuToolbarBase::ChangePopupItem(CMenuToolbarBase
* toolbar
, INT item
)
625 // Ignore the change if it already matches the stored info
626 if (m_popupBar
== toolbar
&& m_popupItem
== item
)
629 // Notify the toolbar if the popup-tracking this toolbar
630 if (m_popupBar
== this && toolbar
!= this)
632 CheckButton(m_popupItem
, FALSE
);
633 m_isTrackingPopup
= FALSE
;
636 m_popupBar
= toolbar
;
639 if (m_popupBar
== this)
641 CheckButton(m_popupItem
, TRUE
);
648 LRESULT
CMenuToolbarBase::IsTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
651 INT idx
= (INT
)wParam
;
653 if (m_hotBar
!= this)
659 if (!GetButton(idx
, &btn
))
662 if (m_hotItem
== btn
.idCommand
)
665 if (m_popupItem
== btn
.idCommand
)
671 LRESULT
CMenuToolbarBase::ChangeTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
674 BOOL wasTracking
= LOWORD(lParam
);
675 BOOL mouse
= HIWORD(lParam
);
676 INT idx
= (INT
)wParam
;
680 m_isTrackingPopup
= FALSE
;
681 return m_menuBand
->_ChangeHotItem(NULL
, -1, HICF_MOUSE
);
684 if (!GetButton(idx
, &btn
))
687 TRACE("ChangeTrackedItem %d, %d\n", idx
, wasTracking
);
688 m_isTrackingPopup
= wasTracking
;
689 return m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, mouse
? HICF_MOUSE
: 0);
692 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, IShellMenu
* childShellMenu
, BOOL keyInitiated
)
694 // Calculate the submenu position and exclude area
697 if (!GetItemRect(index
, &rc
))
700 POINT a
= { rc
.left
, rc
.top
};
701 POINT b
= { rc
.right
, rc
.bottom
};
706 POINTL pt
= { a
.x
, b
.y
};
707 RECTL rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
709 if (m_initFlags
& SMINIT_VERTICAL
)
711 // FIXME: Hardcoding this here feels hacky.
724 // Display the submenu
725 m_isTrackingPopup
= TRUE
;
727 m_menuBand
->_ChangePopupItem(this, iItem
);
728 m_menuBand
->_OnPopupSubMenu(childShellMenu
, &pt
, &rcl
, keyInitiated
);
733 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, HMENU menu
)
735 // Calculate the submenu position and exclude area
738 if (!GetItemRect(index
, &rc
))
741 POINT a
= { rc
.left
, rc
.top
};
742 POINT b
= { rc
.right
, rc
.bottom
};
747 POINT pt
= { a
.x
, b
.y
};
748 RECT rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
750 if (m_initFlags
& SMINIT_VERTICAL
)
756 HMENU popup
= GetSubMenu(menu
, index
);
758 // Display the submenu
759 m_isTrackingPopup
= TRUE
;
760 m_menuBand
->_ChangePopupItem(this, iItem
);
761 m_menuBand
->_TrackSubMenu(popup
, pt
.x
, pt
.y
, rcl
);
762 m_menuBand
->_ChangePopupItem(NULL
, -1);
763 m_isTrackingPopup
= FALSE
;
768 HRESULT
CMenuToolbarBase::TrackContextMenu(IContextMenu
* contextMenu
, POINT pt
)
771 m_menuBand
->_KillPopupTimers();
773 m_menuBand
->_CancelCurrentPopup();
775 // Display the context menu
776 return m_menuBand
->_TrackContextMenu(contextMenu
, pt
.x
, pt
.y
);
779 HRESULT
CMenuToolbarBase::BeforeCancelPopup()
781 m_cancelingPopup
= TRUE
;
782 TRACE("BeforeCancelPopup\n");
786 HRESULT
CMenuToolbarBase::ProcessClick(INT iItem
)
788 if (m_disableMouseTrack
)
790 TRACE("Item click prevented by DisableMouseTrack\n");
794 // If a button is clicked while a submenu was open, cancel the submenu.
795 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
797 TRACE("OnCommand cancelled because it was tracking submenu.\n");
801 if (PopupItem(iItem
, FALSE
) == S_OK
)
803 TRACE("PopupItem returned S_OK\n");
807 TRACE("Executing...\n");
809 return m_menuBand
->_MenuItemSelect(MPOS_EXECUTE
);
812 HRESULT
CMenuToolbarBase::ProcessContextMenu(INT iItem
)
817 GetDataFromId(iItem
, &index
, &data
);
819 DWORD pos
= GetMessagePos();
820 POINT pt
= { GET_X_LPARAM(pos
), GET_Y_LPARAM(pos
) };
822 return InternalContextMenu(iItem
, index
, data
, pt
);
825 HRESULT
CMenuToolbarBase::MenuBarMouseDown(INT iIndex
, BOOL isLButton
)
829 GetButton(iIndex
, &btn
);
831 if ((m_initFlags
& SMINIT_VERTICAL
)
835 m_cancelingPopup
= FALSE
;
839 return ProcessClick(btn
.idCommand
);
842 HRESULT
CMenuToolbarBase::MenuBarMouseUp(INT iIndex
, BOOL isLButton
)
846 m_cancelingPopup
= FALSE
;
848 if (!(m_initFlags
& SMINIT_VERTICAL
))
851 GetButton(iIndex
, &btn
);
854 return ProcessClick(btn
.idCommand
);
856 return ProcessContextMenu(btn
.idCommand
);
859 HRESULT
CMenuToolbarBase::PrepareExecuteItem(INT iItem
)
861 this->m_menuBand
->_KillPopupTimers();
863 m_executeItem
= iItem
;
864 return GetDataFromId(iItem
, &m_executeIndex
, &m_executeData
);
867 HRESULT
CMenuToolbarBase::ExecuteItem()
869 return InternalExecuteItem(m_executeItem
, m_executeItem
, m_executeData
);
872 HRESULT
CMenuToolbarBase::KeyboardItemChange(DWORD dwSelectType
)
874 int prev
= m_hotItem
;
877 if (dwSelectType
!= 0xFFFFFFFF)
879 int count
= GetButtonCount();
881 if (dwSelectType
== VK_HOME
)
884 dwSelectType
= VK_DOWN
;
886 else if (dwSelectType
== VK_END
)
889 dwSelectType
= VK_UP
;
895 TBBUTTONINFO info
= { 0 };
896 info
.cbSize
= sizeof(TBBUTTONINFO
);
898 index
= GetButtonInfo(m_hotItem
, &info
);
903 if (dwSelectType
== VK_UP
)
907 else if (dwSelectType
== VK_DOWN
)
914 if (dwSelectType
== VK_UP
)
918 else if (dwSelectType
== VK_DOWN
)
925 TBBUTTON btn
= { 0 };
926 while (index
>= 0 && index
< count
)
928 DWORD res
= GetButton(index
, &btn
);
934 if (prev
!= btn
.idCommand
)
936 TRACE("Setting Hot item to %d\n", index
);
937 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
940 m_menuBand
->_GetTopLevelWindow(&tlw
);
941 SendMessageW(tlw
, WM_CANCELMODE
, 0, 0);
942 PostMessageW(WM_USER_CHANGETRACKEDITEM
, index
, MAKELPARAM(m_isTrackingPopup
, FALSE
));
945 m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, 0);
950 if (dwSelectType
== VK_UP
)
954 else if (dwSelectType
== VK_DOWN
)
965 TRACE("Setting Hot item to null\n");
966 m_menuBand
->_ChangeHotItem(NULL
, -1, 0);
972 HRESULT
CMenuToolbarBase::AddButton(DWORD commandId
, LPCWSTR caption
, BOOL hasSubMenu
, INT iconId
, DWORD_PTR buttonData
, BOOL last
)
974 TBBUTTON tbb
= { 0 };
976 tbb
.fsState
= TBSTATE_ENABLED
;
977 #if !USE_TBSTYLE_EX_VERTICAL
978 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
979 tbb
.fsState
|= TBSTATE_WRAP
;
981 tbb
.fsStyle
= BTNS_CHECKGROUP
;
983 if (hasSubMenu
&& (m_initFlags
& SMINIT_VERTICAL
))
984 tbb
.fsStyle
|= BTNS_DROPDOWN
;
986 if (!(m_initFlags
& SMINIT_VERTICAL
))
987 tbb
.fsStyle
|= BTNS_AUTOSIZE
;
989 tbb
.iString
= (INT_PTR
) caption
;
990 tbb
.idCommand
= commandId
;
992 tbb
.iBitmap
= iconId
;
993 tbb
.dwData
= buttonData
;
997 if (!AddButtons(1, &tbb
))
998 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
;
1016 if (!AddButtons(1, &tbb
))
1017 return HRESULT_FROM_WIN32(GetLastError());
1022 HRESULT
CMenuToolbarBase::AddPlaceholder()
1024 TBBUTTON tbb
= { 0 };
1025 WCHAR MenuString
[128];
1027 LoadStringW(GetModuleHandle(L
"shell32.dll"), IDS_MENU_EMPTY
, MenuString
, _countof(MenuString
));
1031 tbb
.iString
= (INT_PTR
) MenuString
;
1036 if (!AddButtons(1, &tbb
))
1037 return HRESULT_FROM_WIN32(GetLastError());
1042 HRESULT
CMenuToolbarBase::ClearToolbar()
1044 while (DeleteButton(0))
1052 HRESULT
CMenuToolbarBase::GetDataFromId(INT iItem
, INT
* pIndex
, DWORD_PTR
* pData
)
1063 TBBUTTONINFO info
= { 0 };
1065 info
.cbSize
= sizeof(TBBUTTONINFO
);
1066 info
.dwMask
= TBIF_COMMAND
| TBIF_LPARAM
;
1068 int index
= GetButtonInfo(iItem
, &info
);
1076 *pData
= info
.lParam
;
1081 HRESULT
CMenuToolbarBase::CancelCurrentPopup()
1083 return m_menuBand
->_CancelCurrentPopup();
1086 HRESULT
CMenuToolbarBase::PopupItem(INT iItem
, BOOL keyInitiated
)
1094 if (m_popupBar
== this && m_popupItem
== iItem
)
1097 GetDataFromId(iItem
, &index
, &dwData
);
1099 HRESULT hr
= InternalHasSubMenu(iItem
, index
, dwData
);
1105 HRESULT hr
= CancelCurrentPopup();
1106 if (FAILED_UNEXPECTEDLY(hr
))
1110 if (!(m_initFlags
& SMINIT_VERTICAL
))
1112 TRACE("PopupItem non-vertical %d %d\n", index
, iItem
);
1113 m_menuBand
->_ChangeHotItem(this, iItem
, 0);
1116 return InternalPopupItem(iItem
, index
, dwData
, keyInitiated
);
1119 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand
*menuBand
) :
1120 CMenuToolbarBase(menuBand
, FALSE
),
1126 CMenuStaticToolbar::~CMenuStaticToolbar()
1130 HRESULT
CMenuStaticToolbar::GetMenu(
1131 _Out_opt_ HMENU
*phmenu
,
1132 _Out_opt_ HWND
*phwnd
,
1133 _Out_opt_ DWORD
*pdwFlags
)
1138 *phwnd
= m_hwndMenu
;
1140 *pdwFlags
= m_dwMenuFlags
;
1145 HRESULT
CMenuStaticToolbar::SetMenu(
1152 m_dwMenuFlags
= dwFlags
;
1159 HRESULT
CMenuStaticToolbar::FillToolbar(BOOL clearFirst
)
1162 int ic
= GetMenuItemCount(m_hmenu
);
1170 for (i
= 0; i
< ic
; i
++)
1172 BOOL last
= i
+ 1 == ic
;
1176 info
.cbSize
= sizeof(info
);
1177 info
.dwTypeData
= NULL
;
1178 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_ID
;
1180 if (!GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
))
1182 TRACE("Error obtaining info for menu item at pos=%d\n", i
);
1188 if (info
.fType
& MFT_SEPARATOR
)
1192 else if (!(info
.fType
& MFT_BITMAP
))
1195 info
.dwTypeData
= (PWSTR
) HeapAlloc(GetProcessHeap(), 0, (info
.cch
+ 1) * sizeof(WCHAR
));
1197 info
.fMask
= MIIM_STRING
| MIIM_SUBMENU
| MIIM_ID
;
1198 GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
);
1200 SMINFO
* sminfo
= new SMINFO();
1201 sminfo
->dwMask
= SMIM_ICON
| SMIM_FLAGS
;
1203 HRESULT hr
= m_menuBand
->_CallCBWithItemId(info
.wID
, SMC_GETINFO
, 0, reinterpret_cast<LPARAM
>(sminfo
));
1204 if (FAILED_UNEXPECTEDLY(hr
))
1210 AddButton(info
.wID
, info
.dwTypeData
, info
.hSubMenu
!= NULL
, sminfo
->iIcon
, reinterpret_cast<DWORD_PTR
>(sminfo
), last
);
1212 HeapFree(GetProcessHeap(), 0, info
.dwTypeData
);
1219 HRESULT
CMenuStaticToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1221 //SMINFO * info = reinterpret_cast<SMINFO*>(dwData);
1226 HRESULT
CMenuStaticToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1228 delete reinterpret_cast<SMINFO
*>(tb
->tbButton
.dwData
);
1232 HRESULT
CMenuStaticToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1234 CComPtr
<IContextMenu
> contextMenu
;
1235 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
,
1236 reinterpret_cast<WPARAM
>(&IID_IContextMenu
), reinterpret_cast<LPARAM
>(&contextMenu
));
1240 return TrackContextMenu(contextMenu
, pt
);
1243 HRESULT
CMenuStaticToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1245 return m_menuBand
->_CallCBWithItemId(iItem
, SMC_EXEC
, 0, 0);
1248 HRESULT
CMenuStaticToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1250 SMINFO
* nfo
= reinterpret_cast<SMINFO
*>(dwData
);
1254 if (nfo
->dwFlags
&SMIF_TRACKPOPUP
)
1256 return PopupSubMenu(iItem
, index
, m_hmenu
);
1260 CComPtr
<IShellMenu
> shellMenu
;
1261 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
, reinterpret_cast<WPARAM
>(&IID_IShellMenu
), reinterpret_cast<LPARAM
>(&shellMenu
));
1262 if (FAILED_UNEXPECTEDLY(hr
))
1265 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1269 HRESULT
CMenuStaticToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1271 return ::GetSubMenu(m_hmenu
, index
) ? S_OK
: S_FALSE
;
1274 CMenuSFToolbar::CMenuSFToolbar(CMenuBand
* menuBand
) :
1275 CMenuToolbarBase(menuBand
, TRUE
),
1276 m_shellFolder(NULL
),
1282 CMenuSFToolbar::~CMenuSFToolbar()
1286 int CALLBACK
PidlListSort(void* item1
, void* item2
, LPARAM lParam
)
1288 IShellFolder
* psf
= (IShellFolder
*) lParam
;
1289 PCUIDLIST_RELATIVE pidl1
= (PCUIDLIST_RELATIVE
) item1
;
1290 PCUIDLIST_RELATIVE pidl2
= (PCUIDLIST_RELATIVE
) item2
;
1291 HRESULT hr
= psf
->CompareIDs(0, pidl1
, pidl2
);
1294 // No way to cancel, so sort to equal.
1297 return (int)(short)LOWORD(hr
);
1300 HRESULT
CMenuSFToolbar::FillToolbar(BOOL clearFirst
)
1304 CComPtr
<IEnumIDList
> eidl
;
1305 hr
= m_shellFolder
->EnumObjects(GetToolbar(), SHCONTF_FOLDERS
| SHCONTF_NONFOLDERS
, &eidl
);
1306 if (FAILED_UNEXPECTEDLY(hr
))
1309 HDPA dpaSort
= DPA_Create(10);
1311 LPITEMIDLIST item
= NULL
;
1312 hr
= eidl
->Next(1, &item
, NULL
);
1315 if (m_menuBand
->_CallCBWithItemPidl(item
, 0x10000000, 0, 0) == S_FALSE
)
1317 DPA_AppendPtr(dpaSort
, ILClone(item
));
1320 hr
= eidl
->Next(1, &item
, NULL
);
1323 // If no items were added, show the "empty" placeholder
1324 if (DPA_GetPtrCount(dpaSort
) == 0)
1326 DPA_Destroy(dpaSort
);
1327 return AddPlaceholder();
1330 TRACE("FillToolbar added %d items to the DPA\n", DPA_GetPtrCount(dpaSort
));
1332 DPA_Sort(dpaSort
, PidlListSort
, (LPARAM
) m_shellFolder
.p
);
1334 for (int i
= 0; i
<DPA_GetPtrCount(dpaSort
);)
1341 STRRET sr
= { STRRET_CSTR
, { 0 } };
1343 item
= (LPITEMIDLIST
)DPA_GetPtr(dpaSort
, i
);
1345 hr
= m_shellFolder
->GetDisplayNameOf(item
, SIGDN_NORMALDISPLAY
, &sr
);
1346 if (FAILED_UNEXPECTEDLY(hr
))
1348 DPA_Destroy(dpaSort
);
1352 StrRetToStr(&sr
, NULL
, &MenuString
);
1354 index
= SHMapPIDLToSystemImageListIndex(m_shellFolder
, item
, &indexOpen
);
1356 LPCITEMIDLIST itemc
= item
;
1358 SFGAOF attrs
= SFGAO_FOLDER
;
1359 hr
= m_shellFolder
->GetAttributesOf(1, &itemc
, &attrs
);
1361 DWORD_PTR dwData
= reinterpret_cast<DWORD_PTR
>(item
);
1363 // Fetch next item already, so we know if the current one is the last
1366 AddButton(i
, MenuString
, attrs
& SFGAO_FOLDER
, index
, dwData
, i
>= DPA_GetPtrCount(dpaSort
));
1368 CoTaskMemFree(MenuString
);
1371 DPA_Destroy(dpaSort
);
1375 HRESULT
CMenuSFToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1377 //ITEMIDLIST * pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1382 HRESULT
CMenuSFToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1384 ILFree(reinterpret_cast<LPITEMIDLIST
>(tb
->tbButton
.dwData
));
1388 HRESULT
CMenuSFToolbar::SetShellFolder(IShellFolder
*psf
, LPCITEMIDLIST pidlFolder
, HKEY hKey
, DWORD dwFlags
)
1390 m_shellFolder
= psf
;
1391 m_idList
= ILClone(pidlFolder
);
1393 m_dwMenuFlags
= dwFlags
;
1400 HRESULT
CMenuSFToolbar::GetShellFolder(DWORD
*pdwFlags
, LPITEMIDLIST
*ppidl
, REFIID riid
, void **ppv
)
1404 hr
= m_shellFolder
->QueryInterface(riid
, ppv
);
1405 if (FAILED_UNEXPECTEDLY(hr
))
1409 *pdwFlags
= m_dwMenuFlags
;
1413 LPITEMIDLIST pidl
= NULL
;
1417 pidl
= ILClone(m_idList
);
1420 ERR("ILClone failed!\n");
1421 (*reinterpret_cast<IUnknown
**>(ppv
))->Release();
1432 HRESULT
CMenuSFToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1435 CComPtr
<IContextMenu
> contextMenu
= NULL
;
1436 LPCITEMIDLIST pidl
= reinterpret_cast<LPCITEMIDLIST
>(dwData
);
1438 hr
= m_shellFolder
->GetUIObjectOf(GetToolbar(), 1, &pidl
, IID_NULL_PPV_ARG(IContextMenu
, &contextMenu
));
1439 if (FAILED_UNEXPECTEDLY(hr
))
1444 hr
= TrackContextMenu(contextMenu
, pt
);
1449 HRESULT
CMenuSFToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1451 return m_menuBand
->_CallCBWithItemPidl(reinterpret_cast<LPITEMIDLIST
>(data
), SMC_SFEXEC
, 0, 0);
1454 HRESULT
CMenuSFToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1460 CComPtr
<IShellMenuCallback
> psmc
;
1461 CComPtr
<IShellMenu
> shellMenu
;
1463 LPITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1468 hr
= CMenuBand_CreateInstance(IID_PPV_ARG(IShellMenu
, &shellMenu
));
1469 if (FAILED_UNEXPECTEDLY(hr
))
1472 m_menuBand
->GetMenuInfo(&psmc
, &uId
, &uIdAncestor
, &flags
);
1474 // FIXME: not sure what to use as uId/uIdAncestor here
1475 hr
= shellMenu
->Initialize(psmc
, 0, uId
, SMINIT_VERTICAL
);
1476 if (FAILED_UNEXPECTEDLY(hr
))
1479 CComPtr
<IShellFolder
> childFolder
;
1480 hr
= m_shellFolder
->BindToObject(pidl
, NULL
, IID_PPV_ARG(IShellFolder
, &childFolder
));
1481 if (FAILED_UNEXPECTEDLY(hr
))
1484 hr
= shellMenu
->SetShellFolder(childFolder
, NULL
, NULL
, 0);
1485 if (FAILED_UNEXPECTEDLY(hr
))
1488 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1491 HRESULT
CMenuSFToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1494 LPCITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1496 SFGAOF attrs
= SFGAO_FOLDER
;
1497 hr
= m_shellFolder
->GetAttributesOf(1, &pidl
, &attrs
);
1498 if (FAILED_UNEXPECTEDLY(hr
))
1501 return (attrs
& SFGAO_FOLDER
) ? S_OK
: S_FALSE
;