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
299 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
304 HRESULT
CMenuToolbarBase::UpdateImageLists()
306 if ((m_initFlags
& (SMINIT_TOPLEVEL
| SMINIT_VERTICAL
)) == SMINIT_TOPLEVEL
) // not vertical.
308 // No image list, prevents the buttons from having a margin at the left side
313 // Assign the correct imagelist and padding based on the current icon size
316 if (m_menuBand
->UseBigIcons())
328 HRESULT hr
= SHGetImageList(shiml
, IID_PPV_ARG(IImageList
, &piml
));
329 if (FAILED_UNEXPECTEDLY(hr
))
335 SetImageList((HIMAGELIST
)piml
);
340 HRESULT
CMenuToolbarBase::Close()
346 m_pager
.DestroyWindow();
351 HRESULT
CMenuToolbarBase::CreateToolbar(HWND hwndParent
, DWORD dwFlags
)
353 LONG tbStyles
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
|
354 TBSTYLE_TOOLTIPS
| TBSTYLE_TRANSPARENT
| TBSTYLE_REGISTERDROP
| TBSTYLE_LIST
| TBSTYLE_FLAT
| TBSTYLE_CUSTOMERASE
|
355 CCS_NODIVIDER
| CCS_NOPARENTALIGN
| CCS_NORESIZE
| CCS_TOP
;
356 LONG tbExStyles
= TBSTYLE_EX_DOUBLEBUFFER
| WS_EX_TOOLWINDOW
;
358 if (dwFlags
& SMINIT_VERTICAL
)
360 // Activate vertical semantics
361 tbStyles
|= CCS_VERT
;
363 #if USE_TBSTYLE_EX_VERTICAL
364 tbExStyles
|= TBSTYLE_EX_VERTICAL
;
368 m_initFlags
= dwFlags
;
370 // Get a temporary rect to use while creating the toolbar window.
371 // Ensure that it is not a null rect.
373 if (!::GetClientRect(hwndParent
, &rc
) ||
374 (rc
.left
== rc
.right
) ||
375 (rc
.top
== rc
.bottom
))
383 SubclassWindow(CToolbar::Create(hwndParent
, tbStyles
, tbExStyles
));
385 SetWindowTheme(m_hWnd
, L
"", L
"");
387 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
389 m_menuBand
->AdjustForTheme(m_useFlatMenus
);
391 // If needed, create the pager.
394 LONG pgStyles
= PGS_VERT
| WS_CHILD
| WS_VISIBLE
;
397 HWND hwndPager
= CreateWindowEx(
398 pgExStyles
, WC_PAGESCROLLER
, NULL
,
399 pgStyles
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
400 hwndParent
, NULL
, _AtlBaseModule
.GetModuleInstance(), 0);
402 m_pager
.SubclassWindow(hwndPager
);
404 ::SetParent(m_hWnd
, hwndPager
);
406 m_pager
.SendMessageW(PGM_SETCHILD
, 0, reinterpret_cast<LPARAM
>(m_hWnd
));
409 // Configure the image lists
415 HRESULT
CMenuToolbarBase::GetSizes(SIZE
* pMinSize
, SIZE
* pMaxSize
, SIZE
* pIntegralSize
)
418 *pMinSize
= m_idealSize
;
420 *pMaxSize
= m_idealSize
;
422 *pIntegralSize
= m_itemSize
;
427 TRACE("Sizes out of date, recalculating.\n");
434 // Obtain the ideal size, to be used as min and max
435 GetMaxSize(&m_idealSize
);
436 GetIdealSize((m_initFlags
& SMINIT_VERTICAL
) != 0, &m_idealSize
);
438 TRACE("Ideal Size: (%d, %d) for %d buttons\n", m_idealSize
, GetButtonCount());
440 // Obtain the button size, to be used as the integral size
441 DWORD size
= GetButtonSize();
442 m_itemSize
.cx
= GET_X_LPARAM(size
);
443 m_itemSize
.cy
= GET_Y_LPARAM(size
);
447 *pMinSize
= m_idealSize
;
449 *pMaxSize
= m_idealSize
;
451 *pIntegralSize
= m_itemSize
;
456 HRESULT
CMenuToolbarBase::SetPosSize(int x
, int y
, int cx
, int cy
)
458 // Update the toolbar or pager to fit the requested rect
459 // If we have a pager, set the toolbar height to the ideal height of the toolbar
462 SetWindowPos(NULL
, x
, y
, cx
, m_idealSize
.cy
, 0);
463 m_pager
.SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
467 SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
470 // In a vertical menu, resize the buttons to fit the width
471 if (m_initFlags
& SMINIT_VERTICAL
)
473 DWORD btnSize
= GetButtonSize();
474 SetButtonSize(cx
, GET_Y_LPARAM(btnSize
));
480 HRESULT
CMenuToolbarBase::IsWindowOwner(HWND hwnd
)
482 if (m_hWnd
&& m_hWnd
== hwnd
) return S_OK
;
483 if (m_pager
.m_hWnd
&& m_pager
.m_hWnd
== hwnd
) return S_OK
;
487 HRESULT
CMenuToolbarBase::GetWindow(HWND
*phwnd
)
493 *phwnd
= m_pager
.m_hWnd
;
500 HRESULT
CMenuToolbarBase::OnGetInfoTip(NMTBGETINFOTIP
* tip
)
505 INT iItem
= tip
->iItem
;
507 GetDataFromId(iItem
, &index
, &dwData
);
509 return InternalGetTooltip(iItem
, index
, dwData
, tip
->pszText
, tip
->cchTextMax
);
512 HRESULT
CMenuToolbarBase::OnPopupTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
514 if (wParam
!= TIMERID_HOTTRACK
)
520 KillTimer(TIMERID_HOTTRACK
);
525 m_timerEnabled
= FALSE
;
530 // Returns S_FALSE if the current item did not show a submenu
531 HRESULT hr
= PopupItem(m_hotItem
, FALSE
);
535 // If we didn't switch submenus, cancel the current popup regardless
538 HRESULT hr
= CancelCurrentPopup();
539 if (FAILED_UNEXPECTEDLY(hr
))
546 HRESULT
CMenuToolbarBase::KillPopupTimer()
550 m_timerEnabled
= FALSE
;
551 KillTimer(TIMERID_HOTTRACK
);
557 HRESULT
CMenuToolbarBase::ChangeHotItem(CMenuToolbarBase
* toolbar
, INT item
, DWORD dwFlags
)
559 // Ignore the change if it already matches the stored info
560 if (m_hotBar
== toolbar
&& m_hotItem
== item
)
563 // Prevent a change of hot item if the change was triggered by the mouse,
564 // and mouse tracking is disabled.
565 if (m_disableMouseTrack
&& dwFlags
& HICF_MOUSE
)
567 TRACE("Hot item change prevented by DisableMouseTrack\n");
571 // Notify the toolbar if the hot-tracking left this toolbar
572 if (m_hotBar
== this && toolbar
!= this)
577 TRACE("Hot item changed from %p %p, to %p %p\n", m_hotBar
, m_hotItem
, toolbar
, item
);
581 if (m_hotBar
== this)
583 if (m_isTrackingPopup
&& !(m_initFlags
& SMINIT_VERTICAL
))
585 // If the menubar has an open submenu, switch to the new item's submenu immediately
586 PopupItem(m_hotItem
, FALSE
);
588 else if (dwFlags
& HICF_MOUSE
)
590 // Vertical menus show/hide the submenu after a delay,
591 // but only with the mouse.
592 if (m_initFlags
& SMINIT_VERTICAL
)
595 SystemParametersInfo(SPI_GETMENUSHOWDELAY
, 0, &elapsed
, 0);
596 SetTimer(TIMERID_HOTTRACK
, elapsed
);
597 m_timerEnabled
= TRUE
;
598 TRACE("SetTimer called with m_hotItem=%d\n", m_hotItem
);
604 info
.cbSize
= sizeof(info
);
607 int index
= GetButtonInfo(item
, &info
);
617 HRESULT
CMenuToolbarBase::ChangePopupItem(CMenuToolbarBase
* toolbar
, INT item
)
619 // Ignore the change if it already matches the stored info
620 if (m_popupBar
== toolbar
&& m_popupItem
== item
)
623 // Notify the toolbar if the popup-tracking this toolbar
624 if (m_popupBar
== this && toolbar
!= this)
626 CheckButton(m_popupItem
, FALSE
);
627 m_isTrackingPopup
= FALSE
;
630 m_popupBar
= toolbar
;
633 if (m_popupBar
== this)
635 CheckButton(m_popupItem
, TRUE
);
642 LRESULT
CMenuToolbarBase::IsTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
645 INT idx
= (INT
)wParam
;
647 if (m_hotBar
!= this)
653 if (!GetButton(idx
, &btn
))
656 if (m_hotItem
== btn
.idCommand
)
659 if (m_popupItem
== btn
.idCommand
)
665 LRESULT
CMenuToolbarBase::ChangeTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
668 BOOL wasTracking
= LOWORD(lParam
);
669 BOOL mouse
= HIWORD(lParam
);
670 INT idx
= (INT
)wParam
;
674 m_isTrackingPopup
= FALSE
;
675 return m_menuBand
->_ChangeHotItem(NULL
, -1, HICF_MOUSE
);
678 if (!GetButton(idx
, &btn
))
681 TRACE("ChangeTrackedItem %d, %d\n", idx
, wasTracking
);
682 m_isTrackingPopup
= wasTracking
;
683 return m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, mouse
? HICF_MOUSE
: 0);
686 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, IShellMenu
* childShellMenu
, BOOL keyInitiated
)
688 // Calculate the submenu position and exclude area
691 if (!GetItemRect(index
, &rc
))
694 POINT a
= { rc
.left
, rc
.top
};
695 POINT b
= { rc
.right
, rc
.bottom
};
700 POINTL pt
= { a
.x
, b
.y
};
701 RECTL rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
703 if (m_initFlags
& SMINIT_VERTICAL
)
705 // FIXME: Hardcoding this here feels hacky.
718 // Display the submenu
719 m_isTrackingPopup
= TRUE
;
721 m_menuBand
->_ChangePopupItem(this, iItem
);
722 m_menuBand
->_OnPopupSubMenu(childShellMenu
, &pt
, &rcl
, keyInitiated
);
727 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, HMENU menu
)
729 // Calculate the submenu position and exclude area
732 if (!GetItemRect(index
, &rc
))
735 POINT a
= { rc
.left
, rc
.top
};
736 POINT b
= { rc
.right
, rc
.bottom
};
741 POINT pt
= { a
.x
, b
.y
};
742 RECT rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
744 if (m_initFlags
& SMINIT_VERTICAL
)
750 HMENU popup
= GetSubMenu(menu
, index
);
752 // Display the submenu
753 m_isTrackingPopup
= TRUE
;
754 m_menuBand
->_ChangePopupItem(this, iItem
);
755 m_menuBand
->_TrackSubMenu(popup
, pt
.x
, pt
.y
, rcl
);
756 m_menuBand
->_ChangePopupItem(NULL
, -1);
757 m_isTrackingPopup
= FALSE
;
762 HRESULT
CMenuToolbarBase::TrackContextMenu(IContextMenu
* contextMenu
, POINT pt
)
765 m_menuBand
->_KillPopupTimers();
767 m_menuBand
->_CancelCurrentPopup();
769 // Display the context menu
770 return m_menuBand
->_TrackContextMenu(contextMenu
, pt
.x
, pt
.y
);
773 HRESULT
CMenuToolbarBase::BeforeCancelPopup()
775 m_cancelingPopup
= TRUE
;
776 TRACE("BeforeCancelPopup\n");
780 HRESULT
CMenuToolbarBase::ProcessClick(INT iItem
)
782 if (m_disableMouseTrack
)
784 TRACE("Item click prevented by DisableMouseTrack\n");
788 // If a button is clicked while a submenu was open, cancel the submenu.
789 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
791 TRACE("OnCommand cancelled because it was tracking submenu.\n");
795 if (PopupItem(iItem
, FALSE
) == S_OK
)
797 TRACE("PopupItem returned S_OK\n");
801 TRACE("Executing...\n");
803 return m_menuBand
->_MenuItemSelect(MPOS_EXECUTE
);
806 HRESULT
CMenuToolbarBase::ProcessContextMenu(INT iItem
)
811 GetDataFromId(iItem
, &index
, &data
);
813 DWORD pos
= GetMessagePos();
814 POINT pt
= { GET_X_LPARAM(pos
), GET_Y_LPARAM(pos
) };
816 return InternalContextMenu(iItem
, index
, data
, pt
);
819 HRESULT
CMenuToolbarBase::MenuBarMouseDown(INT iIndex
, BOOL isLButton
)
823 GetButton(iIndex
, &btn
);
825 if ((m_initFlags
& SMINIT_VERTICAL
)
829 m_cancelingPopup
= FALSE
;
833 return ProcessClick(btn
.idCommand
);
836 HRESULT
CMenuToolbarBase::MenuBarMouseUp(INT iIndex
, BOOL isLButton
)
840 m_cancelingPopup
= FALSE
;
842 if (!(m_initFlags
& SMINIT_VERTICAL
))
845 GetButton(iIndex
, &btn
);
848 return ProcessClick(btn
.idCommand
);
850 return ProcessContextMenu(btn
.idCommand
);
853 HRESULT
CMenuToolbarBase::PrepareExecuteItem(INT iItem
)
855 this->m_menuBand
->_KillPopupTimers();
857 m_executeItem
= iItem
;
858 return GetDataFromId(iItem
, &m_executeIndex
, &m_executeData
);
861 HRESULT
CMenuToolbarBase::ExecuteItem()
863 return InternalExecuteItem(m_executeItem
, m_executeItem
, m_executeData
);
866 HRESULT
CMenuToolbarBase::KeyboardItemChange(DWORD dwSelectType
)
868 int prev
= m_hotItem
;
871 if (dwSelectType
!= 0xFFFFFFFF)
873 int count
= GetButtonCount();
875 if (dwSelectType
== VK_HOME
)
878 dwSelectType
= VK_DOWN
;
880 else if (dwSelectType
== VK_END
)
883 dwSelectType
= VK_UP
;
889 TBBUTTONINFO info
= { 0 };
890 info
.cbSize
= sizeof(TBBUTTONINFO
);
892 index
= GetButtonInfo(m_hotItem
, &info
);
897 if (dwSelectType
== VK_UP
)
901 else if (dwSelectType
== VK_DOWN
)
908 if (dwSelectType
== VK_UP
)
912 else if (dwSelectType
== VK_DOWN
)
919 TBBUTTON btn
= { 0 };
920 while (index
>= 0 && index
< count
)
922 DWORD res
= GetButton(index
, &btn
);
928 if (prev
!= btn
.idCommand
)
930 TRACE("Setting Hot item to %d\n", index
);
931 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
934 m_menuBand
->_GetTopLevelWindow(&tlw
);
935 SendMessageW(tlw
, WM_CANCELMODE
, 0, 0);
936 PostMessageW(WM_USER_CHANGETRACKEDITEM
, index
, MAKELPARAM(m_isTrackingPopup
, FALSE
));
939 m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, 0);
944 if (dwSelectType
== VK_UP
)
948 else if (dwSelectType
== VK_DOWN
)
959 TRACE("Setting Hot item to null\n");
960 m_menuBand
->_ChangeHotItem(NULL
, -1, 0);
966 HRESULT
CMenuToolbarBase::AddButton(DWORD commandId
, LPCWSTR caption
, BOOL hasSubMenu
, INT iconId
, DWORD_PTR buttonData
, BOOL last
)
968 TBBUTTON tbb
= { 0 };
970 tbb
.fsState
= TBSTATE_ENABLED
;
971 #if !USE_TBSTYLE_EX_VERTICAL
972 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
973 tbb
.fsState
|= TBSTATE_WRAP
;
975 tbb
.fsStyle
= BTNS_CHECKGROUP
;
977 if (hasSubMenu
&& (m_initFlags
& SMINIT_VERTICAL
))
978 tbb
.fsStyle
|= BTNS_DROPDOWN
;
980 if (!(m_initFlags
& SMINIT_VERTICAL
))
981 tbb
.fsStyle
|= BTNS_AUTOSIZE
;
983 tbb
.iString
= (INT_PTR
) caption
;
984 tbb
.idCommand
= commandId
;
986 tbb
.iBitmap
= iconId
;
987 tbb
.dwData
= buttonData
;
991 if (!AddButtons(1, &tbb
))
992 return HRESULT_FROM_WIN32(GetLastError());
996 HRESULT
CMenuToolbarBase::AddSeparator(BOOL last
)
998 TBBUTTON tbb
= { 0 };
1000 tbb
.fsState
= TBSTATE_ENABLED
;
1001 #if !USE_TBSTYLE_EX_VERTICAL
1002 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
1003 tbb
.fsState
|= TBSTATE_WRAP
;
1005 tbb
.fsStyle
= BTNS_SEP
;
1010 if (!AddButtons(1, &tbb
))
1011 return HRESULT_FROM_WIN32(GetLastError());
1016 HRESULT
CMenuToolbarBase::AddPlaceholder()
1018 TBBUTTON tbb
= { 0 };
1019 WCHAR MenuString
[128];
1021 LoadStringW(GetModuleHandle(L
"shell32.dll"), IDS_MENU_EMPTY
, MenuString
, _countof(MenuString
));
1025 tbb
.iString
= (INT_PTR
) MenuString
;
1030 if (!AddButtons(1, &tbb
))
1031 return HRESULT_FROM_WIN32(GetLastError());
1036 HRESULT
CMenuToolbarBase::ClearToolbar()
1038 while (DeleteButton(0))
1046 HRESULT
CMenuToolbarBase::GetDataFromId(INT iItem
, INT
* pIndex
, DWORD_PTR
* pData
)
1057 TBBUTTONINFO info
= { 0 };
1059 info
.cbSize
= sizeof(TBBUTTONINFO
);
1060 info
.dwMask
= TBIF_COMMAND
| TBIF_LPARAM
;
1062 int index
= GetButtonInfo(iItem
, &info
);
1070 *pData
= info
.lParam
;
1075 HRESULT
CMenuToolbarBase::CancelCurrentPopup()
1077 return m_menuBand
->_CancelCurrentPopup();
1080 HRESULT
CMenuToolbarBase::PopupItem(INT iItem
, BOOL keyInitiated
)
1088 if (m_popupBar
== this && m_popupItem
== iItem
)
1091 GetDataFromId(iItem
, &index
, &dwData
);
1093 HRESULT hr
= InternalHasSubMenu(iItem
, index
, dwData
);
1099 HRESULT hr
= CancelCurrentPopup();
1100 if (FAILED_UNEXPECTEDLY(hr
))
1104 if (!(m_initFlags
& SMINIT_VERTICAL
))
1106 TRACE("PopupItem non-vertical %d %d\n", index
, iItem
);
1107 m_menuBand
->_ChangeHotItem(this, iItem
, 0);
1110 return InternalPopupItem(iItem
, index
, dwData
, keyInitiated
);
1113 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand
*menuBand
) :
1114 CMenuToolbarBase(menuBand
, FALSE
),
1120 CMenuStaticToolbar::~CMenuStaticToolbar()
1124 HRESULT
CMenuStaticToolbar::GetMenu(
1125 _Out_opt_ HMENU
*phmenu
,
1126 _Out_opt_ HWND
*phwnd
,
1127 _Out_opt_ DWORD
*pdwFlags
)
1132 *phwnd
= m_hwndMenu
;
1134 *pdwFlags
= m_dwMenuFlags
;
1139 HRESULT
CMenuStaticToolbar::SetMenu(
1146 m_dwMenuFlags
= dwFlags
;
1153 HRESULT
CMenuStaticToolbar::FillToolbar(BOOL clearFirst
)
1156 int ic
= GetMenuItemCount(m_hmenu
);
1164 for (i
= 0; i
< ic
; i
++)
1166 BOOL last
= i
+ 1 == ic
;
1170 info
.cbSize
= sizeof(info
);
1171 info
.dwTypeData
= NULL
;
1172 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_ID
;
1174 if (!GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
))
1176 TRACE("Error obtaining info for menu item at pos=%d\n", i
);
1182 if (info
.fType
& MFT_SEPARATOR
)
1186 else if (!(info
.fType
& MFT_BITMAP
))
1189 info
.dwTypeData
= (PWSTR
) HeapAlloc(GetProcessHeap(), 0, (info
.cch
+ 1) * sizeof(WCHAR
));
1191 info
.fMask
= MIIM_STRING
| MIIM_SUBMENU
| MIIM_ID
;
1192 GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
);
1194 SMINFO
* sminfo
= new SMINFO();
1195 sminfo
->dwMask
= SMIM_ICON
| SMIM_FLAGS
;
1197 HRESULT hr
= m_menuBand
->_CallCBWithItemId(info
.wID
, SMC_GETINFO
, 0, reinterpret_cast<LPARAM
>(sminfo
));
1198 if (FAILED_UNEXPECTEDLY(hr
))
1204 AddButton(info
.wID
, info
.dwTypeData
, info
.hSubMenu
!= NULL
, sminfo
->iIcon
, reinterpret_cast<DWORD_PTR
>(sminfo
), last
);
1206 HeapFree(GetProcessHeap(), 0, info
.dwTypeData
);
1213 HRESULT
CMenuStaticToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1215 //SMINFO * info = reinterpret_cast<SMINFO*>(dwData);
1220 HRESULT
CMenuStaticToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1222 delete reinterpret_cast<SMINFO
*>(tb
->tbButton
.dwData
);
1226 HRESULT
CMenuStaticToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1228 CComPtr
<IContextMenu
> contextMenu
;
1229 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
,
1230 reinterpret_cast<WPARAM
>(&IID_IContextMenu
), reinterpret_cast<LPARAM
>(&contextMenu
));
1234 return TrackContextMenu(contextMenu
, pt
);
1237 HRESULT
CMenuStaticToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1239 return m_menuBand
->_CallCBWithItemId(iItem
, SMC_EXEC
, 0, 0);
1242 HRESULT
CMenuStaticToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1244 SMINFO
* nfo
= reinterpret_cast<SMINFO
*>(dwData
);
1248 if (nfo
->dwFlags
&SMIF_TRACKPOPUP
)
1250 return PopupSubMenu(iItem
, index
, m_hmenu
);
1254 CComPtr
<IShellMenu
> shellMenu
;
1255 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
, reinterpret_cast<WPARAM
>(&IID_IShellMenu
), reinterpret_cast<LPARAM
>(&shellMenu
));
1256 if (FAILED_UNEXPECTEDLY(hr
))
1259 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1263 HRESULT
CMenuStaticToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1265 return ::GetSubMenu(m_hmenu
, index
) ? S_OK
: S_FALSE
;
1268 CMenuSFToolbar::CMenuSFToolbar(CMenuBand
* menuBand
) :
1269 CMenuToolbarBase(menuBand
, TRUE
),
1270 m_shellFolder(NULL
),
1276 CMenuSFToolbar::~CMenuSFToolbar()
1280 int CALLBACK
PidlListSort(void* item1
, void* item2
, LPARAM lParam
)
1282 IShellFolder
* psf
= (IShellFolder
*) lParam
;
1283 PCUIDLIST_RELATIVE pidl1
= (PCUIDLIST_RELATIVE
) item1
;
1284 PCUIDLIST_RELATIVE pidl2
= (PCUIDLIST_RELATIVE
) item2
;
1285 HRESULT hr
= psf
->CompareIDs(0, pidl1
, pidl2
);
1288 // No way to cancel, so sort to equal.
1291 return (int)(short)LOWORD(hr
);
1294 HRESULT
CMenuSFToolbar::FillToolbar(BOOL clearFirst
)
1298 CComPtr
<IEnumIDList
> eidl
;
1299 hr
= m_shellFolder
->EnumObjects(GetToolbar(), SHCONTF_FOLDERS
| SHCONTF_NONFOLDERS
, &eidl
);
1300 if (FAILED_UNEXPECTEDLY(hr
))
1303 HDPA dpaSort
= DPA_Create(10);
1305 LPITEMIDLIST item
= NULL
;
1306 hr
= eidl
->Next(1, &item
, NULL
);
1309 if (m_menuBand
->_CallCBWithItemPidl(item
, 0x10000000, 0, 0) == S_FALSE
)
1311 DPA_AppendPtr(dpaSort
, ILClone(item
));
1314 hr
= eidl
->Next(1, &item
, NULL
);
1317 // If no items were added, show the "empty" placeholder
1318 if (DPA_GetPtrCount(dpaSort
) == 0)
1320 DPA_Destroy(dpaSort
);
1321 return AddPlaceholder();
1324 TRACE("FillToolbar added %d items to the DPA\n", DPA_GetPtrCount(dpaSort
));
1326 DPA_Sort(dpaSort
, PidlListSort
, (LPARAM
) m_shellFolder
.p
);
1328 for (int i
= 0; i
<DPA_GetPtrCount(dpaSort
);)
1335 STRRET sr
= { STRRET_CSTR
, { 0 } };
1337 item
= (LPITEMIDLIST
)DPA_GetPtr(dpaSort
, i
);
1339 hr
= m_shellFolder
->GetDisplayNameOf(item
, SIGDN_NORMALDISPLAY
, &sr
);
1340 if (FAILED_UNEXPECTEDLY(hr
))
1342 DPA_Destroy(dpaSort
);
1346 StrRetToStr(&sr
, NULL
, &MenuString
);
1348 index
= SHMapPIDLToSystemImageListIndex(m_shellFolder
, item
, &indexOpen
);
1350 LPCITEMIDLIST itemc
= item
;
1352 SFGAOF attrs
= SFGAO_FOLDER
;
1353 hr
= m_shellFolder
->GetAttributesOf(1, &itemc
, &attrs
);
1355 DWORD_PTR dwData
= reinterpret_cast<DWORD_PTR
>(item
);
1357 // Fetch next item already, so we know if the current one is the last
1360 AddButton(i
, MenuString
, attrs
& SFGAO_FOLDER
, index
, dwData
, i
>= DPA_GetPtrCount(dpaSort
));
1362 CoTaskMemFree(MenuString
);
1365 DPA_Destroy(dpaSort
);
1369 HRESULT
CMenuSFToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1371 //ITEMIDLIST * pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1376 HRESULT
CMenuSFToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1378 ILFree(reinterpret_cast<LPITEMIDLIST
>(tb
->tbButton
.dwData
));
1382 HRESULT
CMenuSFToolbar::SetShellFolder(IShellFolder
*psf
, LPCITEMIDLIST pidlFolder
, HKEY hKey
, DWORD dwFlags
)
1384 m_shellFolder
= psf
;
1385 m_idList
= ILClone(pidlFolder
);
1387 m_dwMenuFlags
= dwFlags
;
1394 HRESULT
CMenuSFToolbar::GetShellFolder(DWORD
*pdwFlags
, LPITEMIDLIST
*ppidl
, REFIID riid
, void **ppv
)
1398 hr
= m_shellFolder
->QueryInterface(riid
, ppv
);
1399 if (FAILED_UNEXPECTEDLY(hr
))
1403 *pdwFlags
= m_dwMenuFlags
;
1407 LPITEMIDLIST pidl
= NULL
;
1411 pidl
= ILClone(m_idList
);
1414 ERR("ILClone failed!\n");
1415 (*reinterpret_cast<IUnknown
**>(ppv
))->Release();
1426 HRESULT
CMenuSFToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1429 CComPtr
<IContextMenu
> contextMenu
= NULL
;
1430 LPCITEMIDLIST pidl
= reinterpret_cast<LPCITEMIDLIST
>(dwData
);
1432 hr
= m_shellFolder
->GetUIObjectOf(GetToolbar(), 1, &pidl
, IID_NULL_PPV_ARG(IContextMenu
, &contextMenu
));
1433 if (FAILED_UNEXPECTEDLY(hr
))
1438 hr
= TrackContextMenu(contextMenu
, pt
);
1443 HRESULT
CMenuSFToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1445 return m_menuBand
->_CallCBWithItemPidl(reinterpret_cast<LPITEMIDLIST
>(data
), SMC_SFEXEC
, 0, 0);
1448 HRESULT
CMenuSFToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1454 CComPtr
<IShellMenuCallback
> psmc
;
1455 CComPtr
<IShellMenu
> shellMenu
;
1457 LPITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1462 hr
= CMenuBand_Constructor(IID_PPV_ARG(IShellMenu
, &shellMenu
));
1463 if (FAILED_UNEXPECTEDLY(hr
))
1466 m_menuBand
->GetMenuInfo(&psmc
, &uId
, &uIdAncestor
, &flags
);
1468 // FIXME: not sure what to use as uId/uIdAncestor here
1469 hr
= shellMenu
->Initialize(psmc
, 0, uId
, SMINIT_VERTICAL
);
1470 if (FAILED_UNEXPECTEDLY(hr
))
1473 CComPtr
<IShellFolder
> childFolder
;
1474 hr
= m_shellFolder
->BindToObject(pidl
, NULL
, IID_PPV_ARG(IShellFolder
, &childFolder
));
1475 if (FAILED_UNEXPECTEDLY(hr
))
1478 hr
= shellMenu
->SetShellFolder(childFolder
, NULL
, NULL
, 0);
1479 if (FAILED_UNEXPECTEDLY(hr
))
1482 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1485 HRESULT
CMenuSFToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1488 LPCITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1490 SFGAOF attrs
= SFGAO_FOLDER
;
1491 hr
= m_shellFolder
->GetAttributesOf(1, &pidl
, &attrs
);
1492 if (FAILED_UNEXPECTEDLY(hr
))
1495 return (attrs
& SFGAO_FOLDER
) ? S_OK
: S_FALSE
;