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
:
109 case -714: return S_FALSE
;
112 TRACE("WM_NOTIFY unknown code %d, %d\n", hdr
->code
, hdr
->idFrom
);
121 HRESULT
CMenuToolbarBase::DisableMouseTrack(BOOL bDisable
)
123 if (m_disableMouseTrack
!= bDisable
)
125 m_disableMouseTrack
= bDisable
;
126 TRACE("DisableMouseTrack %d\n", bDisable
);
131 HRESULT
CMenuToolbarBase::OnPagerCalcSize(LPNMPGCALCSIZE csize
)
134 GetSizes(NULL
, &tbs
, NULL
);
135 if (csize
->dwFlag
== PGF_CALCHEIGHT
)
137 csize
->iHeight
= tbs
.cy
;
139 else if (csize
->dwFlag
== PGF_CALCWIDTH
)
141 csize
->iWidth
= tbs
.cx
;
146 HRESULT
CMenuToolbarBase::OnCustomDraw(LPNMTBCUSTOMDRAW cdraw
, LRESULT
* theResult
)
151 switch (cdraw
->nmcd
.dwDrawStage
)
154 *theResult
= CDRF_NOTIFYITEMDRAW
;
157 case CDDS_ITEMPREPAINT
:
159 // The item with an active submenu gets the CHECKED flag.
160 isHot
= m_hotBar
== this && (int) cdraw
->nmcd
.dwItemSpec
== m_hotItem
;
161 isPopup
= m_popupBar
== this && (int) cdraw
->nmcd
.dwItemSpec
== m_popupItem
;
163 if ((m_initFlags
& SMINIT_VERTICAL
))
167 RECT rc
= cdraw
->nmcd
.rc
;
168 HDC hdc
= cdraw
->nmcd
.hdc
;
170 // Remove HOT and CHECKED flags (will restore HOT if necessary)
171 cdraw
->nmcd
.uItemState
&= ~(CDIS_HOT
| CDIS_CHECKED
);
173 // Decide on the colors
174 if (isHot
|| (m_hotItem
< 0 && isPopup
))
176 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
178 clrText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
179 bgBrush
= GetSysColorBrush(m_useFlatMenus
? COLOR_MENUHILIGHT
: COLOR_HIGHLIGHT
);
183 clrText
= GetSysColor(COLOR_MENUTEXT
);
184 bgBrush
= GetSysColorBrush(COLOR_MENU
);
187 // Paint the background color with the selected color
188 FillRect(hdc
, &rc
, bgBrush
);
190 // Set the text color in advance, this color will be assigned when the ITEMPOSTPAINT triggers
191 SetTextColor(hdc
, clrText
);
193 // Set the text color, will be used by the internal drawing code
194 cdraw
->clrText
= clrText
;
195 cdraw
->iListGap
+= 4;
197 // Tell the default drawing code we don't want any fanciness, not even a background.
198 *theResult
= CDRF_NOTIFYPOSTPAINT
| TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| 0x00800000; // FIXME: the last bit is Vista+, useful for debugging only
202 // Remove HOT and CHECKED flags (will restore HOT if necessary)
203 cdraw
->nmcd
.uItemState
&= ~CDIS_HOT
;
205 // Decide on the colors
206 if (isHot
|| (m_hotItem
< 0 && isPopup
))
208 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
216 case CDDS_ITEMPOSTPAINT
:
218 // Fetch the button style
219 btni
.cbSize
= sizeof(btni
);
220 btni
.dwMask
= TBIF_STYLE
;
221 GetButtonInfo(cdraw
->nmcd
.dwItemSpec
, &btni
);
223 // Check if we need to draw a submenu arrow
224 if (btni
.fsStyle
& BTNS_DROPDOWN
)
226 // TODO: Support RTL text modes by drawing a leftwards arrow aligned to the left of the control
228 // "8" is the rightwards dropdown arrow in the Marlett font
229 WCHAR text
[] = L
"8";
231 // Configure the font to draw with Marlett, keeping the current background color as-is
232 SelectObject(cdraw
->nmcd
.hdc
, m_marlett
);
233 SetBkMode(cdraw
->nmcd
.hdc
, TRANSPARENT
);
235 // Tweak the alignment by 1 pixel so the menu draws like the Windows start menu.
236 RECT rc
= cdraw
->nmcd
.rc
;
239 // The arrow is drawn at the right of the item's rect, aligned vertically.
240 DrawTextEx(cdraw
->nmcd
.hdc
, text
, 1, &rc
, DT_NOCLIP
| DT_VCENTER
| DT_RIGHT
| DT_SINGLELINE
, NULL
);
248 CMenuToolbarBase::CMenuToolbarBase(CMenuBand
*menuBand
, BOOL usePager
) :
250 m_useFlatMenus(FALSE
),
252 m_disableMouseTrack(FALSE
),
253 m_timerEnabled(FALSE
),
254 m_menuBand(menuBand
),
257 m_usePager(usePager
),
262 m_isTrackingPopup(FALSE
),
263 m_cancelingPopup(FALSE
)
269 m_marlett
= CreateFont(
270 0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET
,
271 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
272 DEFAULT_QUALITY
, FF_DONTCARE
, L
"Marlett");
275 CMenuToolbarBase::~CMenuToolbarBase()
281 m_pager
.DestroyWindow();
283 DeleteObject(m_marlett
);
286 void CMenuToolbarBase::InvalidateDraw()
288 InvalidateRect(NULL
, FALSE
);
291 HRESULT
CMenuToolbarBase::ShowDW(BOOL fShow
)
293 ShowWindow(fShow
? SW_SHOW
: SW_HIDE
);
295 // Ensure that the right image list is assigned to the toolbar
298 // For custom-drawing
300 GetThemeSysBool(GetWindowTheme(m_hWnd
), TMT_FLATMENUS
);
302 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
307 HRESULT
CMenuToolbarBase::UpdateImageLists()
309 if ((m_initFlags
& (SMINIT_TOPLEVEL
| SMINIT_VERTICAL
)) == SMINIT_TOPLEVEL
) // not vertical.
311 // No image list, prevents the buttons from having a margin at the left side
316 // Assign the correct imagelist and padding based on the current icon size
319 if (m_menuBand
->UseBigIcons())
331 HRESULT hr
= SHGetImageList(shiml
, IID_PPV_ARG(IImageList
, &piml
));
332 if (FAILED_UNEXPECTEDLY(hr
))
338 SetImageList((HIMAGELIST
)piml
);
343 HRESULT
CMenuToolbarBase::Close()
349 m_pager
.DestroyWindow();
354 HRESULT
CMenuToolbarBase::CreateToolbar(HWND hwndParent
, DWORD dwFlags
)
356 LONG tbStyles
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
|
357 TBSTYLE_TOOLTIPS
| TBSTYLE_TRANSPARENT
| TBSTYLE_REGISTERDROP
| TBSTYLE_LIST
| TBSTYLE_FLAT
| TBSTYLE_CUSTOMERASE
|
358 CCS_NODIVIDER
| CCS_NOPARENTALIGN
| CCS_NORESIZE
| CCS_TOP
;
359 LONG tbExStyles
= TBSTYLE_EX_DOUBLEBUFFER
| WS_EX_TOOLWINDOW
;
361 if (dwFlags
& SMINIT_VERTICAL
)
363 // Activate vertical semantics
364 tbStyles
|= CCS_VERT
;
366 #if USE_TBSTYLE_EX_VERTICAL
367 tbExStyles
|= TBSTYLE_EX_VERTICAL
;
371 m_initFlags
= dwFlags
;
373 // Get a temporary rect to use while creating the toolbar window.
374 // Ensure that it is not a null rect.
376 if (!::GetClientRect(hwndParent
, &rc
) ||
377 (rc
.left
== rc
.right
) ||
378 (rc
.top
== rc
.bottom
))
386 SubclassWindow(CToolbar::Create(hwndParent
, tbStyles
, tbExStyles
));
388 SetWindowTheme(m_hWnd
, L
"", L
"");
391 GetThemeSysBool(GetWindowTheme(m_hWnd
), TMT_FLATMENUS
);
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 return ProcessContextMenu(btn
.idCommand
);
833 if ((m_initFlags
& SMINIT_VERTICAL
)
837 m_cancelingPopup
= FALSE
;
841 return ProcessClick(btn
.idCommand
);
844 HRESULT
CMenuToolbarBase::MenuBarMouseUp(INT iIndex
)
848 m_cancelingPopup
= FALSE
;
850 if (!(m_initFlags
& SMINIT_VERTICAL
))
853 GetButton(iIndex
, &btn
);
854 return ProcessClick(btn
.idCommand
);
857 HRESULT
CMenuToolbarBase::PrepareExecuteItem(INT iItem
)
859 this->m_menuBand
->_KillPopupTimers();
861 m_executeItem
= iItem
;
862 return GetDataFromId(iItem
, &m_executeIndex
, &m_executeData
);
865 HRESULT
CMenuToolbarBase::ExecuteItem()
867 return InternalExecuteItem(m_executeItem
, m_executeItem
, m_executeData
);
870 HRESULT
CMenuToolbarBase::KeyboardItemChange(DWORD dwSelectType
)
872 int prev
= m_hotItem
;
875 if (dwSelectType
!= 0xFFFFFFFF)
877 int count
= GetButtonCount();
879 if (dwSelectType
== VK_HOME
)
882 dwSelectType
= VK_DOWN
;
884 else if (dwSelectType
== VK_END
)
887 dwSelectType
= VK_UP
;
893 TBBUTTONINFO info
= { 0 };
894 info
.cbSize
= sizeof(TBBUTTONINFO
);
896 index
= GetButtonInfo(m_hotItem
, &info
);
901 if (dwSelectType
== VK_UP
)
905 else if (dwSelectType
== VK_DOWN
)
912 if (dwSelectType
== VK_UP
)
916 else if (dwSelectType
== VK_DOWN
)
923 TBBUTTON btn
= { 0 };
924 while (index
>= 0 && index
< count
)
926 DWORD res
= GetButton(index
, &btn
);
932 if (prev
!= btn
.idCommand
)
934 TRACE("Setting Hot item to %d\n", index
);
935 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
938 m_menuBand
->_GetTopLevelWindow(&tlw
);
939 SendMessageW(tlw
, WM_CANCELMODE
, 0, 0);
940 PostMessageW(WM_USER_CHANGETRACKEDITEM
, index
, MAKELPARAM(m_isTrackingPopup
, FALSE
));
943 m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, 0);
948 if (dwSelectType
== VK_UP
)
952 else if (dwSelectType
== VK_DOWN
)
963 TRACE("Setting Hot item to null\n");
964 m_menuBand
->_ChangeHotItem(NULL
, -1, 0);
970 HRESULT
CMenuToolbarBase::AddButton(DWORD commandId
, LPCWSTR caption
, BOOL hasSubMenu
, INT iconId
, DWORD_PTR buttonData
, BOOL last
)
972 TBBUTTON tbb
= { 0 };
974 tbb
.fsState
= TBSTATE_ENABLED
;
975 #if !USE_TBSTYLE_EX_VERTICAL
976 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
977 tbb
.fsState
|= TBSTATE_WRAP
;
979 tbb
.fsStyle
= BTNS_CHECKGROUP
;
981 if (hasSubMenu
&& (m_initFlags
& SMINIT_VERTICAL
))
982 tbb
.fsStyle
|= BTNS_DROPDOWN
;
984 if (!(m_initFlags
& SMINIT_VERTICAL
))
985 tbb
.fsStyle
|= BTNS_AUTOSIZE
;
987 tbb
.iString
= (INT_PTR
) caption
;
988 tbb
.idCommand
= commandId
;
990 tbb
.iBitmap
= iconId
;
991 tbb
.dwData
= buttonData
;
995 if (!AddButtons(1, &tbb
))
996 return HRESULT_FROM_WIN32(GetLastError());
1000 HRESULT
CMenuToolbarBase::AddSeparator(BOOL last
)
1002 TBBUTTON tbb
= { 0 };
1004 tbb
.fsState
= TBSTATE_ENABLED
;
1005 #if !USE_TBSTYLE_EX_VERTICAL
1006 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
1007 tbb
.fsState
|= TBSTATE_WRAP
;
1009 tbb
.fsStyle
= BTNS_SEP
;
1014 if (!AddButtons(1, &tbb
))
1015 return HRESULT_FROM_WIN32(GetLastError());
1020 HRESULT
CMenuToolbarBase::AddPlaceholder()
1022 TBBUTTON tbb
= { 0 };
1023 WCHAR MenuString
[128];
1025 LoadStringW(GetModuleHandle(L
"shell32.dll"), IDS_MENU_EMPTY
, MenuString
, _countof(MenuString
));
1029 tbb
.iString
= (INT_PTR
) MenuString
;
1034 if (!AddButtons(1, &tbb
))
1035 return HRESULT_FROM_WIN32(GetLastError());
1040 HRESULT
CMenuToolbarBase::ClearToolbar()
1042 while (DeleteButton(0))
1050 HRESULT
CMenuToolbarBase::GetDataFromId(INT iItem
, INT
* pIndex
, DWORD_PTR
* pData
)
1061 TBBUTTONINFO info
= { 0 };
1063 info
.cbSize
= sizeof(TBBUTTONINFO
);
1064 info
.dwMask
= TBIF_COMMAND
| TBIF_LPARAM
;
1066 int index
= GetButtonInfo(iItem
, &info
);
1074 *pData
= info
.lParam
;
1079 HRESULT
CMenuToolbarBase::CancelCurrentPopup()
1081 return m_menuBand
->_CancelCurrentPopup();
1084 HRESULT
CMenuToolbarBase::PopupItem(INT iItem
, BOOL keyInitiated
)
1092 if (m_popupBar
== this && m_popupItem
== iItem
)
1095 GetDataFromId(iItem
, &index
, &dwData
);
1097 HRESULT hr
= InternalHasSubMenu(iItem
, index
, dwData
);
1103 HRESULT hr
= CancelCurrentPopup();
1104 if (FAILED_UNEXPECTEDLY(hr
))
1108 if (!(m_initFlags
& SMINIT_VERTICAL
))
1110 TRACE("PopupItem non-vertical %d %d\n", index
, iItem
);
1111 m_menuBand
->_ChangeHotItem(this, iItem
, 0);
1114 return InternalPopupItem(iItem
, index
, dwData
, keyInitiated
);
1117 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand
*menuBand
) :
1118 CMenuToolbarBase(menuBand
, FALSE
),
1124 CMenuStaticToolbar::~CMenuStaticToolbar()
1128 HRESULT
CMenuStaticToolbar::GetMenu(
1129 _Out_opt_ HMENU
*phmenu
,
1130 _Out_opt_ HWND
*phwnd
,
1131 _Out_opt_ DWORD
*pdwFlags
)
1136 *phwnd
= m_hwndMenu
;
1138 *pdwFlags
= m_dwMenuFlags
;
1143 HRESULT
CMenuStaticToolbar::SetMenu(
1150 m_dwMenuFlags
= dwFlags
;
1157 HRESULT
CMenuStaticToolbar::FillToolbar(BOOL clearFirst
)
1160 int ic
= GetMenuItemCount(m_hmenu
);
1168 for (i
= 0; i
< ic
; i
++)
1170 BOOL last
= i
+ 1 == ic
;
1174 info
.cbSize
= sizeof(info
);
1175 info
.dwTypeData
= NULL
;
1176 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_ID
;
1178 if (!GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
))
1180 TRACE("Error obtaining info for menu item at pos=%d\n", i
);
1186 if (info
.fType
& MFT_SEPARATOR
)
1190 else if (!(info
.fType
& MFT_BITMAP
))
1193 info
.dwTypeData
= (PWSTR
) HeapAlloc(GetProcessHeap(), 0, (info
.cch
+ 1) * sizeof(WCHAR
));
1195 info
.fMask
= MIIM_STRING
| MIIM_SUBMENU
| MIIM_ID
;
1196 GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
);
1198 SMINFO
* sminfo
= new SMINFO();
1199 sminfo
->dwMask
= SMIM_ICON
| SMIM_FLAGS
;
1201 HRESULT hr
= m_menuBand
->_CallCBWithItemId(info
.wID
, SMC_GETINFO
, 0, reinterpret_cast<LPARAM
>(sminfo
));
1202 if (FAILED_UNEXPECTEDLY(hr
))
1208 AddButton(info
.wID
, info
.dwTypeData
, info
.hSubMenu
!= NULL
, sminfo
->iIcon
, reinterpret_cast<DWORD_PTR
>(sminfo
), last
);
1210 HeapFree(GetProcessHeap(), 0, info
.dwTypeData
);
1217 HRESULT
CMenuStaticToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1219 //SMINFO * info = reinterpret_cast<SMINFO*>(dwData);
1224 HRESULT
CMenuStaticToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1226 delete reinterpret_cast<SMINFO
*>(tb
->tbButton
.dwData
);
1230 HRESULT
CMenuStaticToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1232 CComPtr
<IContextMenu
> contextMenu
;
1233 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
,
1234 reinterpret_cast<WPARAM
>(&IID_IContextMenu
), reinterpret_cast<LPARAM
>(&contextMenu
));
1238 return TrackContextMenu(contextMenu
, pt
);
1241 HRESULT
CMenuStaticToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1243 return m_menuBand
->_CallCBWithItemId(iItem
, SMC_EXEC
, 0, 0);
1246 HRESULT
CMenuStaticToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1248 SMINFO
* nfo
= reinterpret_cast<SMINFO
*>(dwData
);
1252 if (nfo
->dwFlags
&SMIF_TRACKPOPUP
)
1254 return PopupSubMenu(iItem
, index
, m_hmenu
);
1258 CComPtr
<IShellMenu
> shellMenu
;
1259 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
, reinterpret_cast<WPARAM
>(&IID_IShellMenu
), reinterpret_cast<LPARAM
>(&shellMenu
));
1260 if (FAILED_UNEXPECTEDLY(hr
))
1263 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1267 HRESULT
CMenuStaticToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1269 return ::GetSubMenu(m_hmenu
, index
) ? S_OK
: S_FALSE
;
1272 CMenuSFToolbar::CMenuSFToolbar(CMenuBand
* menuBand
) :
1273 CMenuToolbarBase(menuBand
, TRUE
),
1274 m_shellFolder(NULL
),
1280 CMenuSFToolbar::~CMenuSFToolbar()
1284 int CALLBACK
PidlListSort(void* item1
, void* item2
, LPARAM lParam
)
1286 IShellFolder
* psf
= (IShellFolder
*) lParam
;
1287 PCUIDLIST_RELATIVE pidl1
= (PCUIDLIST_RELATIVE
) item1
;
1288 PCUIDLIST_RELATIVE pidl2
= (PCUIDLIST_RELATIVE
) item2
;
1289 HRESULT hr
= psf
->CompareIDs(0, pidl1
, pidl2
);
1292 // No way to cancel, so sort to equal.
1295 return (int)(short)LOWORD(hr
);
1298 HRESULT
CMenuSFToolbar::FillToolbar(BOOL clearFirst
)
1302 CComPtr
<IEnumIDList
> eidl
;
1303 hr
= m_shellFolder
->EnumObjects(GetToolbar(), SHCONTF_FOLDERS
| SHCONTF_NONFOLDERS
, &eidl
);
1304 if (FAILED_UNEXPECTEDLY(hr
))
1307 HDPA dpaSort
= DPA_Create(10);
1309 LPITEMIDLIST item
= NULL
;
1310 hr
= eidl
->Next(1, &item
, NULL
);
1313 if (m_menuBand
->_CallCBWithItemPidl(item
, 0x10000000, 0, 0) == S_FALSE
)
1315 DPA_AppendPtr(dpaSort
, ILClone(item
));
1318 hr
= eidl
->Next(1, &item
, NULL
);
1321 // If no items were added, show the "empty" placeholder
1322 if (DPA_GetPtrCount(dpaSort
) == 0)
1324 DPA_Destroy(dpaSort
);
1325 return AddPlaceholder();
1328 TRACE("FillToolbar added %d items to the DPA\n", DPA_GetPtrCount(dpaSort
));
1330 DPA_Sort(dpaSort
, PidlListSort
, (LPARAM
) m_shellFolder
.p
);
1332 for (int i
= 0; i
<DPA_GetPtrCount(dpaSort
);)
1339 STRRET sr
= { STRRET_CSTR
, { 0 } };
1341 item
= (LPITEMIDLIST
)DPA_GetPtr(dpaSort
, i
);
1343 hr
= m_shellFolder
->GetDisplayNameOf(item
, SIGDN_NORMALDISPLAY
, &sr
);
1344 if (FAILED_UNEXPECTEDLY(hr
))
1346 DPA_Destroy(dpaSort
);
1350 StrRetToStr(&sr
, NULL
, &MenuString
);
1352 index
= SHMapPIDLToSystemImageListIndex(m_shellFolder
, item
, &indexOpen
);
1354 LPCITEMIDLIST itemc
= item
;
1356 SFGAOF attrs
= SFGAO_FOLDER
;
1357 hr
= m_shellFolder
->GetAttributesOf(1, &itemc
, &attrs
);
1359 DWORD_PTR dwData
= reinterpret_cast<DWORD_PTR
>(item
);
1361 // Fetch next item already, so we know if the current one is the last
1364 AddButton(i
, MenuString
, attrs
& SFGAO_FOLDER
, index
, dwData
, i
>= DPA_GetPtrCount(dpaSort
));
1366 CoTaskMemFree(MenuString
);
1369 DPA_Destroy(dpaSort
);
1373 HRESULT
CMenuSFToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1375 //ITEMIDLIST * pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1380 HRESULT
CMenuSFToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1382 ILFree(reinterpret_cast<LPITEMIDLIST
>(tb
->tbButton
.dwData
));
1386 HRESULT
CMenuSFToolbar::SetShellFolder(IShellFolder
*psf
, LPCITEMIDLIST pidlFolder
, HKEY hKey
, DWORD dwFlags
)
1388 m_shellFolder
= psf
;
1389 m_idList
= ILClone(pidlFolder
);
1391 m_dwMenuFlags
= dwFlags
;
1398 HRESULT
CMenuSFToolbar::GetShellFolder(DWORD
*pdwFlags
, LPITEMIDLIST
*ppidl
, REFIID riid
, void **ppv
)
1402 hr
= m_shellFolder
->QueryInterface(riid
, ppv
);
1403 if (FAILED_UNEXPECTEDLY(hr
))
1407 *pdwFlags
= m_dwMenuFlags
;
1411 LPITEMIDLIST pidl
= NULL
;
1415 pidl
= ILClone(m_idList
);
1418 ERR("ILClone failed!\n");
1419 (*reinterpret_cast<IUnknown
**>(ppv
))->Release();
1430 HRESULT
CMenuSFToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1433 CComPtr
<IContextMenu
> contextMenu
= NULL
;
1434 LPCITEMIDLIST pidl
= reinterpret_cast<LPCITEMIDLIST
>(dwData
);
1436 hr
= m_shellFolder
->GetUIObjectOf(GetToolbar(), 1, &pidl
, IID_NULL_PPV_ARG(IContextMenu
, &contextMenu
));
1437 if (FAILED_UNEXPECTEDLY(hr
))
1442 hr
= TrackContextMenu(contextMenu
, pt
);
1447 HRESULT
CMenuSFToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1449 return m_menuBand
->_CallCBWithItemPidl(reinterpret_cast<LPITEMIDLIST
>(data
), SMC_SFEXEC
, 0, 0);
1452 HRESULT
CMenuSFToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1458 CComPtr
<IShellMenuCallback
> psmc
;
1459 CComPtr
<IShellMenu
> shellMenu
;
1461 LPITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1466 hr
= CMenuBand_Constructor(IID_PPV_ARG(IShellMenu
, &shellMenu
));
1467 if (FAILED_UNEXPECTEDLY(hr
))
1470 m_menuBand
->GetMenuInfo(&psmc
, &uId
, &uIdAncestor
, &flags
);
1472 // FIXME: not sure what to use as uId/uIdAncestor here
1473 hr
= shellMenu
->Initialize(psmc
, 0, uId
, SMINIT_VERTICAL
);
1474 if (FAILED_UNEXPECTEDLY(hr
))
1477 CComPtr
<IShellFolder
> childFolder
;
1478 hr
= m_shellFolder
->BindToObject(pidl
, NULL
, IID_PPV_ARG(IShellFolder
, &childFolder
));
1479 if (FAILED_UNEXPECTEDLY(hr
))
1482 hr
= shellMenu
->SetShellFolder(childFolder
, NULL
, NULL
, 0);
1483 if (FAILED_UNEXPECTEDLY(hr
))
1486 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1489 HRESULT
CMenuSFToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1492 LPCITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1494 SFGAOF attrs
= SFGAO_FOLDER
;
1495 hr
= m_shellFolder
->GetAttributesOf(1, &pidl
, &attrs
);
1496 if (FAILED_UNEXPECTEDLY(hr
))
1499 return (attrs
& SFGAO_FOLDER
) ? S_OK
: S_FALSE
;