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 // Set the text color, will be used by the internal drawing code
210 cdraw
->clrText
= GetSysColor(COLOR_MENUTEXT
);
212 // Remove HOT and CHECKED flags (will restore HOT if necessary)
213 cdraw
->nmcd
.uItemState
&= ~CDIS_HOT
;
215 // Decide on the colors
218 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
226 case CDDS_ITEMPOSTPAINT
:
228 // Fetch the button style
229 btni
.cbSize
= sizeof(btni
);
230 btni
.dwMask
= TBIF_STYLE
;
231 GetButtonInfo(cdraw
->nmcd
.dwItemSpec
, &btni
);
233 // Check if we need to draw a submenu arrow
234 if (btni
.fsStyle
& BTNS_DROPDOWN
)
236 // TODO: Support RTL text modes by drawing a leftwards arrow aligned to the left of the control
238 // "8" is the rightwards dropdown arrow in the Marlett font
239 WCHAR text
[] = L
"8";
241 // Configure the font to draw with Marlett, keeping the current background color as-is
242 SelectObject(cdraw
->nmcd
.hdc
, m_marlett
);
243 SetBkMode(cdraw
->nmcd
.hdc
, TRANSPARENT
);
245 // Tweak the alignment by 1 pixel so the menu draws like the Windows start menu.
246 RECT rc
= cdraw
->nmcd
.rc
;
249 // The arrow is drawn at the right of the item's rect, aligned vertically.
250 DrawTextEx(cdraw
->nmcd
.hdc
, text
, 1, &rc
, DT_NOCLIP
| DT_VCENTER
| DT_RIGHT
| DT_SINGLELINE
, NULL
);
258 CMenuToolbarBase::CMenuToolbarBase(CMenuBand
*menuBand
, BOOL usePager
) :
260 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(CToolbar::Create(hwndParent
, tbStyles
, tbExStyles
));
394 SetWindowTheme(m_hWnd
, L
"", L
"");
396 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
398 m_menuBand
->AdjustForTheme(m_useFlatMenus
);
400 // If needed, create the pager.
403 LONG pgStyles
= PGS_VERT
| WS_CHILD
| WS_VISIBLE
;
406 HWND hwndPager
= CreateWindowEx(
407 pgExStyles
, WC_PAGESCROLLER
, NULL
,
408 pgStyles
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
409 hwndParent
, NULL
, _AtlBaseModule
.GetModuleInstance(), 0);
411 m_pager
.SubclassWindow(hwndPager
);
413 ::SetParent(m_hWnd
, hwndPager
);
415 m_pager
.SendMessageW(PGM_SETCHILD
, 0, reinterpret_cast<LPARAM
>(m_hWnd
));
418 // Configure the image lists
424 HRESULT
CMenuToolbarBase::GetSizes(SIZE
* pMinSize
, SIZE
* pMaxSize
, SIZE
* pIntegralSize
)
427 *pMinSize
= m_idealSize
;
429 *pMaxSize
= m_idealSize
;
431 *pIntegralSize
= m_itemSize
;
436 TRACE("Sizes out of date, recalculating.\n");
443 // Obtain the ideal size, to be used as min and max
444 GetMaxSize(&m_idealSize
);
445 GetIdealSize((m_initFlags
& SMINIT_VERTICAL
) != 0, &m_idealSize
);
447 TRACE("Ideal Size: (%d, %d) for %d buttons\n", m_idealSize
, GetButtonCount());
449 // Obtain the button size, to be used as the integral size
450 DWORD size
= GetButtonSize();
451 m_itemSize
.cx
= GET_X_LPARAM(size
);
452 m_itemSize
.cy
= GET_Y_LPARAM(size
);
456 *pMinSize
= m_idealSize
;
458 *pMaxSize
= m_idealSize
;
460 *pIntegralSize
= m_itemSize
;
465 HRESULT
CMenuToolbarBase::SetPosSize(int x
, int y
, int cx
, int cy
)
467 // Update the toolbar or pager to fit the requested rect
468 // If we have a pager, set the toolbar height to the ideal height of the toolbar
471 SetWindowPos(NULL
, x
, y
, cx
, m_idealSize
.cy
, 0);
472 m_pager
.SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
476 SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
479 // In a vertical menu, resize the buttons to fit the width
480 if (m_initFlags
& SMINIT_VERTICAL
)
482 DWORD btnSize
= GetButtonSize();
483 SetButtonSize(cx
, GET_Y_LPARAM(btnSize
));
489 HRESULT
CMenuToolbarBase::IsWindowOwner(HWND hwnd
)
491 if (m_hWnd
&& m_hWnd
== hwnd
) return S_OK
;
492 if (m_pager
.m_hWnd
&& m_pager
.m_hWnd
== hwnd
) return S_OK
;
496 HRESULT
CMenuToolbarBase::GetWindow(HWND
*phwnd
)
502 *phwnd
= m_pager
.m_hWnd
;
509 HRESULT
CMenuToolbarBase::OnGetInfoTip(NMTBGETINFOTIP
* tip
)
514 INT iItem
= tip
->iItem
;
516 GetDataFromId(iItem
, &index
, &dwData
);
518 return InternalGetTooltip(iItem
, index
, dwData
, tip
->pszText
, tip
->cchTextMax
);
521 HRESULT
CMenuToolbarBase::OnPopupTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
523 if (wParam
!= TIMERID_HOTTRACK
)
529 KillTimer(TIMERID_HOTTRACK
);
534 m_timerEnabled
= FALSE
;
539 // Returns S_FALSE if the current item did not show a submenu
540 HRESULT hr
= PopupItem(m_hotItem
, FALSE
);
544 // If we didn't switch submenus, cancel the current popup regardless
547 HRESULT hr
= CancelCurrentPopup();
548 if (FAILED_UNEXPECTEDLY(hr
))
555 HRESULT
CMenuToolbarBase::KillPopupTimer()
559 m_timerEnabled
= FALSE
;
560 KillTimer(TIMERID_HOTTRACK
);
566 HRESULT
CMenuToolbarBase::ChangeHotItem(CMenuToolbarBase
* toolbar
, INT item
, DWORD dwFlags
)
568 // Ignore the change if it already matches the stored info
569 if (m_hotBar
== toolbar
&& m_hotItem
== item
)
572 // Prevent a change of hot item if the change was triggered by the mouse,
573 // and mouse tracking is disabled.
574 if (m_disableMouseTrack
&& dwFlags
& HICF_MOUSE
)
576 TRACE("Hot item change prevented by DisableMouseTrack\n");
580 // Notify the toolbar if the hot-tracking left this toolbar
581 if (m_hotBar
== this && toolbar
!= this)
586 TRACE("Hot item changed from %p %p, to %p %p\n", m_hotBar
, m_hotItem
, toolbar
, item
);
590 if (m_hotBar
== this)
592 if (m_isTrackingPopup
&& !(m_initFlags
& SMINIT_VERTICAL
))
594 // If the menubar has an open submenu, switch to the new item's submenu immediately
595 PopupItem(m_hotItem
, FALSE
);
597 else if (dwFlags
& HICF_MOUSE
)
599 // Vertical menus show/hide the submenu after a delay,
600 // but only with the mouse.
601 if (m_initFlags
& SMINIT_VERTICAL
)
604 SystemParametersInfo(SPI_GETMENUSHOWDELAY
, 0, &elapsed
, 0);
605 SetTimer(TIMERID_HOTTRACK
, elapsed
);
606 m_timerEnabled
= TRUE
;
607 TRACE("SetTimer called with m_hotItem=%d\n", m_hotItem
);
613 info
.cbSize
= sizeof(info
);
616 int index
= GetButtonInfo(item
, &info
);
626 HRESULT
CMenuToolbarBase::ChangePopupItem(CMenuToolbarBase
* toolbar
, INT item
)
628 // Ignore the change if it already matches the stored info
629 if (m_popupBar
== toolbar
&& m_popupItem
== item
)
632 // Notify the toolbar if the popup-tracking this toolbar
633 if (m_popupBar
== this && toolbar
!= this)
635 CheckButton(m_popupItem
, FALSE
);
636 m_isTrackingPopup
= FALSE
;
639 m_popupBar
= toolbar
;
642 if (m_popupBar
== this)
644 CheckButton(m_popupItem
, TRUE
);
651 LRESULT
CMenuToolbarBase::IsTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
654 INT idx
= (INT
)wParam
;
656 if (m_hotBar
!= this)
662 if (!GetButton(idx
, &btn
))
665 if (m_hotItem
== btn
.idCommand
)
668 if (m_popupItem
== btn
.idCommand
)
674 LRESULT
CMenuToolbarBase::ChangeTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
677 BOOL wasTracking
= LOWORD(lParam
);
678 BOOL mouse
= HIWORD(lParam
);
679 INT idx
= (INT
)wParam
;
683 m_isTrackingPopup
= FALSE
;
684 return m_menuBand
->_ChangeHotItem(NULL
, -1, HICF_MOUSE
);
687 if (!GetButton(idx
, &btn
))
690 TRACE("ChangeTrackedItem %d, %d\n", idx
, wasTracking
);
691 m_isTrackingPopup
= wasTracking
;
692 return m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, mouse
? HICF_MOUSE
: 0);
695 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, IShellMenu
* childShellMenu
, BOOL keyInitiated
)
697 // Calculate the submenu position and exclude area
700 if (!GetItemRect(index
, &rc
))
703 POINT a
= { rc
.left
, rc
.top
};
704 POINT b
= { rc
.right
, rc
.bottom
};
709 POINTL pt
= { a
.x
, b
.y
};
710 RECTL rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
712 if (m_initFlags
& SMINIT_VERTICAL
)
714 // FIXME: Hardcoding this here feels hacky.
727 // Display the submenu
728 m_isTrackingPopup
= TRUE
;
730 m_menuBand
->_ChangePopupItem(this, iItem
);
731 m_menuBand
->_OnPopupSubMenu(childShellMenu
, &pt
, &rcl
, keyInitiated
);
736 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, HMENU menu
)
738 // Calculate the submenu position and exclude area
741 if (!GetItemRect(index
, &rc
))
744 POINT a
= { rc
.left
, rc
.top
};
745 POINT b
= { rc
.right
, rc
.bottom
};
750 POINT pt
= { a
.x
, b
.y
};
751 RECT rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
753 if (m_initFlags
& SMINIT_VERTICAL
)
759 HMENU popup
= GetSubMenu(menu
, index
);
761 // Display the submenu
762 m_isTrackingPopup
= TRUE
;
763 m_menuBand
->_ChangePopupItem(this, iItem
);
764 m_menuBand
->_TrackSubMenu(popup
, pt
.x
, pt
.y
, rcl
);
765 m_menuBand
->_ChangePopupItem(NULL
, -1);
766 m_isTrackingPopup
= FALSE
;
771 HRESULT
CMenuToolbarBase::TrackContextMenu(IContextMenu
* contextMenu
, POINT pt
)
774 m_menuBand
->_KillPopupTimers();
776 m_menuBand
->_CancelCurrentPopup();
778 // Display the context menu
779 return m_menuBand
->_TrackContextMenu(contextMenu
, pt
.x
, pt
.y
);
782 HRESULT
CMenuToolbarBase::BeforeCancelPopup()
784 m_cancelingPopup
= TRUE
;
785 TRACE("BeforeCancelPopup\n");
789 HRESULT
CMenuToolbarBase::ProcessClick(INT iItem
)
791 if (m_disableMouseTrack
)
793 TRACE("Item click prevented by DisableMouseTrack\n");
797 // If a button is clicked while a submenu was open, cancel the submenu.
798 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
800 TRACE("OnCommand cancelled because it was tracking submenu.\n");
804 if (PopupItem(iItem
, FALSE
) == S_OK
)
806 TRACE("PopupItem returned S_OK\n");
810 TRACE("Executing...\n");
812 return m_menuBand
->_MenuItemSelect(MPOS_EXECUTE
);
815 HRESULT
CMenuToolbarBase::ProcessContextMenu(INT iItem
)
820 GetDataFromId(iItem
, &index
, &data
);
822 DWORD pos
= GetMessagePos();
823 POINT pt
= { GET_X_LPARAM(pos
), GET_Y_LPARAM(pos
) };
825 return InternalContextMenu(iItem
, index
, data
, pt
);
828 HRESULT
CMenuToolbarBase::MenuBarMouseDown(INT iIndex
, BOOL isLButton
)
832 GetButton(iIndex
, &btn
);
834 if ((m_initFlags
& SMINIT_VERTICAL
)
838 m_cancelingPopup
= FALSE
;
842 return ProcessClick(btn
.idCommand
);
845 HRESULT
CMenuToolbarBase::MenuBarMouseUp(INT iIndex
, BOOL isLButton
)
849 m_cancelingPopup
= FALSE
;
851 if (!(m_initFlags
& SMINIT_VERTICAL
))
854 GetButton(iIndex
, &btn
);
857 return ProcessClick(btn
.idCommand
);
859 return ProcessContextMenu(btn
.idCommand
);
862 HRESULT
CMenuToolbarBase::PrepareExecuteItem(INT iItem
)
864 this->m_menuBand
->_KillPopupTimers();
866 m_executeItem
= iItem
;
867 return GetDataFromId(iItem
, &m_executeIndex
, &m_executeData
);
870 HRESULT
CMenuToolbarBase::ExecuteItem()
872 return InternalExecuteItem(m_executeItem
, m_executeItem
, m_executeData
);
875 HRESULT
CMenuToolbarBase::KeyboardItemChange(DWORD dwSelectType
)
877 int prev
= m_hotItem
;
880 if (dwSelectType
!= 0xFFFFFFFF)
882 int count
= GetButtonCount();
884 if (dwSelectType
== VK_HOME
)
887 dwSelectType
= VK_DOWN
;
889 else if (dwSelectType
== VK_END
)
892 dwSelectType
= VK_UP
;
898 TBBUTTONINFO info
= { 0 };
899 info
.cbSize
= sizeof(TBBUTTONINFO
);
901 index
= GetButtonInfo(m_hotItem
, &info
);
906 if (dwSelectType
== VK_UP
)
910 else if (dwSelectType
== VK_DOWN
)
917 if (dwSelectType
== VK_UP
)
921 else if (dwSelectType
== VK_DOWN
)
928 TBBUTTON btn
= { 0 };
929 while (index
>= 0 && index
< count
)
931 DWORD res
= GetButton(index
, &btn
);
937 if (prev
!= btn
.idCommand
)
939 TRACE("Setting Hot item to %d\n", index
);
940 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
943 m_menuBand
->_GetTopLevelWindow(&tlw
);
944 SendMessageW(tlw
, WM_CANCELMODE
, 0, 0);
945 PostMessageW(WM_USER_CHANGETRACKEDITEM
, index
, MAKELPARAM(m_isTrackingPopup
, FALSE
));
948 m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, 0);
953 if (dwSelectType
== VK_UP
)
957 else if (dwSelectType
== VK_DOWN
)
968 TRACE("Setting Hot item to null\n");
969 m_menuBand
->_ChangeHotItem(NULL
, -1, 0);
975 HRESULT
CMenuToolbarBase::AddButton(DWORD commandId
, LPCWSTR caption
, BOOL hasSubMenu
, INT iconId
, DWORD_PTR buttonData
, BOOL last
)
977 TBBUTTON tbb
= { 0 };
979 tbb
.fsState
= TBSTATE_ENABLED
;
980 #if !USE_TBSTYLE_EX_VERTICAL
981 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
982 tbb
.fsState
|= TBSTATE_WRAP
;
984 tbb
.fsStyle
= BTNS_CHECKGROUP
;
986 if (hasSubMenu
&& (m_initFlags
& SMINIT_VERTICAL
))
987 tbb
.fsStyle
|= BTNS_DROPDOWN
;
989 if (!(m_initFlags
& SMINIT_VERTICAL
))
990 tbb
.fsStyle
|= BTNS_AUTOSIZE
;
992 tbb
.iString
= (INT_PTR
) caption
;
993 tbb
.idCommand
= commandId
;
995 tbb
.iBitmap
= iconId
;
996 tbb
.dwData
= buttonData
;
1000 if (!AddButtons(1, &tbb
))
1001 return HRESULT_FROM_WIN32(GetLastError());
1005 HRESULT
CMenuToolbarBase::AddSeparator(BOOL last
)
1007 TBBUTTON tbb
= { 0 };
1009 tbb
.fsState
= TBSTATE_ENABLED
;
1010 #if !USE_TBSTYLE_EX_VERTICAL
1011 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
1012 tbb
.fsState
|= TBSTATE_WRAP
;
1014 tbb
.fsStyle
= BTNS_SEP
;
1019 if (!AddButtons(1, &tbb
))
1020 return HRESULT_FROM_WIN32(GetLastError());
1025 HRESULT
CMenuToolbarBase::AddPlaceholder()
1027 TBBUTTON tbb
= { 0 };
1028 WCHAR MenuString
[128];
1030 LoadStringW(GetModuleHandle(L
"shell32.dll"), IDS_MENU_EMPTY
, MenuString
, _countof(MenuString
));
1034 tbb
.iString
= (INT_PTR
) MenuString
;
1039 if (!AddButtons(1, &tbb
))
1040 return HRESULT_FROM_WIN32(GetLastError());
1045 HRESULT
CMenuToolbarBase::ClearToolbar()
1047 while (DeleteButton(0))
1055 HRESULT
CMenuToolbarBase::GetDataFromId(INT iItem
, INT
* pIndex
, DWORD_PTR
* pData
)
1066 TBBUTTONINFO info
= { 0 };
1068 info
.cbSize
= sizeof(TBBUTTONINFO
);
1069 info
.dwMask
= TBIF_COMMAND
| TBIF_LPARAM
;
1071 int index
= GetButtonInfo(iItem
, &info
);
1079 *pData
= info
.lParam
;
1084 HRESULT
CMenuToolbarBase::CancelCurrentPopup()
1086 return m_menuBand
->_CancelCurrentPopup();
1089 HRESULT
CMenuToolbarBase::PopupItem(INT iItem
, BOOL keyInitiated
)
1097 if (m_popupBar
== this && m_popupItem
== iItem
)
1100 GetDataFromId(iItem
, &index
, &dwData
);
1102 HRESULT hr
= InternalHasSubMenu(iItem
, index
, dwData
);
1108 HRESULT hr
= CancelCurrentPopup();
1109 if (FAILED_UNEXPECTEDLY(hr
))
1113 if (!(m_initFlags
& SMINIT_VERTICAL
))
1115 TRACE("PopupItem non-vertical %d %d\n", index
, iItem
);
1116 m_menuBand
->_ChangeHotItem(this, iItem
, 0);
1119 return InternalPopupItem(iItem
, index
, dwData
, keyInitiated
);
1122 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand
*menuBand
) :
1123 CMenuToolbarBase(menuBand
, FALSE
),
1129 CMenuStaticToolbar::~CMenuStaticToolbar()
1133 HRESULT
CMenuStaticToolbar::GetMenu(
1134 _Out_opt_ HMENU
*phmenu
,
1135 _Out_opt_ HWND
*phwnd
,
1136 _Out_opt_ DWORD
*pdwFlags
)
1141 *phwnd
= m_hwndMenu
;
1143 *pdwFlags
= m_dwMenuFlags
;
1148 HRESULT
CMenuStaticToolbar::SetMenu(
1155 m_dwMenuFlags
= dwFlags
;
1162 HRESULT
CMenuStaticToolbar::FillToolbar(BOOL clearFirst
)
1165 int ic
= GetMenuItemCount(m_hmenu
);
1173 for (i
= 0; i
< ic
; i
++)
1175 BOOL last
= i
+ 1 == ic
;
1179 info
.cbSize
= sizeof(info
);
1180 info
.dwTypeData
= NULL
;
1181 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_ID
;
1183 if (!GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
))
1185 TRACE("Error obtaining info for menu item at pos=%d\n", i
);
1191 if (info
.fType
& MFT_SEPARATOR
)
1195 else if (!(info
.fType
& MFT_BITMAP
))
1198 info
.dwTypeData
= (PWSTR
) HeapAlloc(GetProcessHeap(), 0, (info
.cch
+ 1) * sizeof(WCHAR
));
1200 info
.fMask
= MIIM_STRING
| MIIM_SUBMENU
| MIIM_ID
;
1201 GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
);
1203 SMINFO
* sminfo
= new SMINFO();
1204 sminfo
->dwMask
= SMIM_ICON
| SMIM_FLAGS
;
1206 HRESULT hr
= m_menuBand
->_CallCBWithItemId(info
.wID
, SMC_GETINFO
, 0, reinterpret_cast<LPARAM
>(sminfo
));
1207 if (FAILED_UNEXPECTEDLY(hr
))
1213 AddButton(info
.wID
, info
.dwTypeData
, info
.hSubMenu
!= NULL
, sminfo
->iIcon
, reinterpret_cast<DWORD_PTR
>(sminfo
), last
);
1215 HeapFree(GetProcessHeap(), 0, info
.dwTypeData
);
1222 HRESULT
CMenuStaticToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1224 //SMINFO * info = reinterpret_cast<SMINFO*>(dwData);
1229 HRESULT
CMenuStaticToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1231 delete reinterpret_cast<SMINFO
*>(tb
->tbButton
.dwData
);
1235 HRESULT
CMenuStaticToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1237 CComPtr
<IContextMenu
> contextMenu
;
1238 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
,
1239 reinterpret_cast<WPARAM
>(&IID_IContextMenu
), reinterpret_cast<LPARAM
>(&contextMenu
));
1243 return TrackContextMenu(contextMenu
, pt
);
1246 HRESULT
CMenuStaticToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1248 return m_menuBand
->_CallCBWithItemId(iItem
, SMC_EXEC
, 0, 0);
1251 HRESULT
CMenuStaticToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1253 SMINFO
* nfo
= reinterpret_cast<SMINFO
*>(dwData
);
1257 if (nfo
->dwFlags
&SMIF_TRACKPOPUP
)
1259 return PopupSubMenu(iItem
, index
, m_hmenu
);
1263 CComPtr
<IShellMenu
> shellMenu
;
1264 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
, reinterpret_cast<WPARAM
>(&IID_IShellMenu
), reinterpret_cast<LPARAM
>(&shellMenu
));
1265 if (FAILED_UNEXPECTEDLY(hr
))
1268 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1272 HRESULT
CMenuStaticToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1274 return ::GetSubMenu(m_hmenu
, index
) ? S_OK
: S_FALSE
;
1277 CMenuSFToolbar::CMenuSFToolbar(CMenuBand
* menuBand
) :
1278 CMenuToolbarBase(menuBand
, TRUE
),
1279 m_shellFolder(NULL
),
1285 CMenuSFToolbar::~CMenuSFToolbar()
1289 int CALLBACK
PidlListSort(void* item1
, void* item2
, LPARAM lParam
)
1291 IShellFolder
* psf
= (IShellFolder
*) lParam
;
1292 PCUIDLIST_RELATIVE pidl1
= (PCUIDLIST_RELATIVE
) item1
;
1293 PCUIDLIST_RELATIVE pidl2
= (PCUIDLIST_RELATIVE
) item2
;
1294 HRESULT hr
= psf
->CompareIDs(0, pidl1
, pidl2
);
1297 // No way to cancel, so sort to equal.
1300 return (int)(short)LOWORD(hr
);
1303 HRESULT
CMenuSFToolbar::FillToolbar(BOOL clearFirst
)
1307 CComPtr
<IEnumIDList
> eidl
;
1308 hr
= m_shellFolder
->EnumObjects(GetToolbar(), SHCONTF_FOLDERS
| SHCONTF_NONFOLDERS
, &eidl
);
1309 if (FAILED_UNEXPECTEDLY(hr
))
1312 HDPA dpaSort
= DPA_Create(10);
1314 LPITEMIDLIST item
= NULL
;
1315 hr
= eidl
->Next(1, &item
, NULL
);
1318 if (m_menuBand
->_CallCBWithItemPidl(item
, 0x10000000, 0, 0) == S_FALSE
)
1320 DPA_AppendPtr(dpaSort
, ILClone(item
));
1323 hr
= eidl
->Next(1, &item
, NULL
);
1326 // If no items were added, show the "empty" placeholder
1327 if (DPA_GetPtrCount(dpaSort
) == 0)
1329 DPA_Destroy(dpaSort
);
1330 return AddPlaceholder();
1333 TRACE("FillToolbar added %d items to the DPA\n", DPA_GetPtrCount(dpaSort
));
1335 DPA_Sort(dpaSort
, PidlListSort
, (LPARAM
) m_shellFolder
.p
);
1337 for (int i
= 0; i
<DPA_GetPtrCount(dpaSort
);)
1344 STRRET sr
= { STRRET_CSTR
, { 0 } };
1346 item
= (LPITEMIDLIST
)DPA_GetPtr(dpaSort
, i
);
1348 hr
= m_shellFolder
->GetDisplayNameOf(item
, SIGDN_NORMALDISPLAY
, &sr
);
1349 if (FAILED_UNEXPECTEDLY(hr
))
1351 DPA_Destroy(dpaSort
);
1355 StrRetToStr(&sr
, NULL
, &MenuString
);
1357 index
= SHMapPIDLToSystemImageListIndex(m_shellFolder
, item
, &indexOpen
);
1359 LPCITEMIDLIST itemc
= item
;
1361 SFGAOF attrs
= SFGAO_FOLDER
;
1362 hr
= m_shellFolder
->GetAttributesOf(1, &itemc
, &attrs
);
1364 DWORD_PTR dwData
= reinterpret_cast<DWORD_PTR
>(item
);
1366 // Fetch next item already, so we know if the current one is the last
1369 AddButton(i
, MenuString
, attrs
& SFGAO_FOLDER
, index
, dwData
, i
>= DPA_GetPtrCount(dpaSort
));
1371 CoTaskMemFree(MenuString
);
1374 DPA_Destroy(dpaSort
);
1378 HRESULT
CMenuSFToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1380 //ITEMIDLIST * pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1385 HRESULT
CMenuSFToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1387 ILFree(reinterpret_cast<LPITEMIDLIST
>(tb
->tbButton
.dwData
));
1391 HRESULT
CMenuSFToolbar::SetShellFolder(IShellFolder
*psf
, LPCITEMIDLIST pidlFolder
, HKEY hKey
, DWORD dwFlags
)
1393 m_shellFolder
= psf
;
1394 m_idList
= ILClone(pidlFolder
);
1396 m_dwMenuFlags
= dwFlags
;
1403 HRESULT
CMenuSFToolbar::GetShellFolder(DWORD
*pdwFlags
, LPITEMIDLIST
*ppidl
, REFIID riid
, void **ppv
)
1407 hr
= m_shellFolder
->QueryInterface(riid
, ppv
);
1408 if (FAILED_UNEXPECTEDLY(hr
))
1412 *pdwFlags
= m_dwMenuFlags
;
1416 LPITEMIDLIST pidl
= NULL
;
1420 pidl
= ILClone(m_idList
);
1423 ERR("ILClone failed!\n");
1424 (*reinterpret_cast<IUnknown
**>(ppv
))->Release();
1435 HRESULT
CMenuSFToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1438 CComPtr
<IContextMenu
> contextMenu
= NULL
;
1439 LPCITEMIDLIST pidl
= reinterpret_cast<LPCITEMIDLIST
>(dwData
);
1441 hr
= m_shellFolder
->GetUIObjectOf(GetToolbar(), 1, &pidl
, IID_NULL_PPV_ARG(IContextMenu
, &contextMenu
));
1442 if (FAILED_UNEXPECTEDLY(hr
))
1447 hr
= TrackContextMenu(contextMenu
, pt
);
1452 HRESULT
CMenuSFToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1454 return m_menuBand
->_CallCBWithItemPidl(reinterpret_cast<LPITEMIDLIST
>(data
), SMC_SFEXEC
, 0, 0);
1457 HRESULT
CMenuSFToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1463 CComPtr
<IShellMenuCallback
> psmc
;
1464 CComPtr
<IShellMenu
> shellMenu
;
1466 LPITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1471 hr
= CMenuBand_CreateInstance(IID_PPV_ARG(IShellMenu
, &shellMenu
));
1472 if (FAILED_UNEXPECTEDLY(hr
))
1475 m_menuBand
->GetMenuInfo(&psmc
, &uId
, &uIdAncestor
, &flags
);
1477 // FIXME: not sure what to use as uId/uIdAncestor here
1478 hr
= shellMenu
->Initialize(psmc
, 0, uId
, SMINIT_VERTICAL
);
1479 if (FAILED_UNEXPECTEDLY(hr
))
1482 CComPtr
<IShellFolder
> childFolder
;
1483 hr
= m_shellFolder
->BindToObject(pidl
, NULL
, IID_PPV_ARG(IShellFolder
, &childFolder
));
1484 if (FAILED_UNEXPECTEDLY(hr
))
1487 hr
= shellMenu
->SetShellFolder(childFolder
, NULL
, NULL
, 0);
1488 if (FAILED_UNEXPECTEDLY(hr
))
1491 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1494 HRESULT
CMenuSFToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1497 LPCITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1499 SFGAOF attrs
= SFGAO_FOLDER
;
1500 hr
= m_shellFolder
->GetAttributesOf(1, &pidl
, &attrs
);
1501 if (FAILED_UNEXPECTEDLY(hr
))
1504 return (attrs
& SFGAO_FOLDER
) ? S_OK
: S_FALSE
;