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_hotItem
< 0 && isPopup
)
166 if ((m_useFlatMenus
&& isHot
) || (m_initFlags
& SMINIT_VERTICAL
))
170 RECT rc
= cdraw
->nmcd
.rc
;
171 HDC hdc
= cdraw
->nmcd
.hdc
;
173 // Remove HOT and CHECKED flags (will restore HOT if necessary)
174 cdraw
->nmcd
.uItemState
&= ~(CDIS_HOT
| CDIS_CHECKED
);
176 // Decide on the colors
179 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
181 clrText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
182 bgBrush
= GetSysColorBrush(m_useFlatMenus
? COLOR_MENUHILIGHT
: COLOR_HIGHLIGHT
);
186 clrText
= GetSysColor(COLOR_MENUTEXT
);
187 bgBrush
= GetSysColorBrush(COLOR_MENU
);
190 // Paint the background color with the selected color
191 FillRect(hdc
, &rc
, bgBrush
);
193 // Set the text color in advance, this color will be assigned when the ITEMPOSTPAINT triggers
194 SetTextColor(hdc
, clrText
);
196 // Set the text color, will be used by the internal drawing code
197 cdraw
->clrText
= clrText
;
198 cdraw
->iListGap
+= 4;
200 // Tell the default drawing code we don't want any fanciness, not even a background.
201 *theResult
= CDRF_NOTIFYPOSTPAINT
| TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| 0x00800000; // FIXME: the last bit is Vista+, useful for debugging only
205 // Remove HOT and CHECKED flags (will restore HOT if necessary)
206 cdraw
->nmcd
.uItemState
&= ~CDIS_HOT
;
208 // Decide on the colors
211 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
219 case CDDS_ITEMPOSTPAINT
:
221 // Fetch the button style
222 btni
.cbSize
= sizeof(btni
);
223 btni
.dwMask
= TBIF_STYLE
;
224 GetButtonInfo(cdraw
->nmcd
.dwItemSpec
, &btni
);
226 // Check if we need to draw a submenu arrow
227 if (btni
.fsStyle
& BTNS_DROPDOWN
)
229 // TODO: Support RTL text modes by drawing a leftwards arrow aligned to the left of the control
231 // "8" is the rightwards dropdown arrow in the Marlett font
232 WCHAR text
[] = L
"8";
234 // Configure the font to draw with Marlett, keeping the current background color as-is
235 SelectObject(cdraw
->nmcd
.hdc
, m_marlett
);
236 SetBkMode(cdraw
->nmcd
.hdc
, TRANSPARENT
);
238 // Tweak the alignment by 1 pixel so the menu draws like the Windows start menu.
239 RECT rc
= cdraw
->nmcd
.rc
;
242 // The arrow is drawn at the right of the item's rect, aligned vertically.
243 DrawTextEx(cdraw
->nmcd
.hdc
, text
, 1, &rc
, DT_NOCLIP
| DT_VCENTER
| DT_RIGHT
| DT_SINGLELINE
, NULL
);
251 CMenuToolbarBase::CMenuToolbarBase(CMenuBand
*menuBand
, BOOL usePager
) :
253 m_useFlatMenus(FALSE
),
255 m_disableMouseTrack(FALSE
),
256 m_timerEnabled(FALSE
),
257 m_menuBand(menuBand
),
260 m_usePager(usePager
),
265 m_isTrackingPopup(FALSE
),
266 m_cancelingPopup(FALSE
)
272 m_marlett
= CreateFont(
273 0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET
,
274 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
275 DEFAULT_QUALITY
, FF_DONTCARE
, L
"Marlett");
278 CMenuToolbarBase::~CMenuToolbarBase()
284 m_pager
.DestroyWindow();
286 DeleteObject(m_marlett
);
289 void CMenuToolbarBase::InvalidateDraw()
291 InvalidateRect(NULL
, FALSE
);
294 HRESULT
CMenuToolbarBase::ShowDW(BOOL fShow
)
296 ShowWindow(fShow
? SW_SHOW
: SW_HIDE
);
298 // Ensure that the right image list is assigned to the toolbar
301 // For custom-drawing
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
"");
390 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
392 m_menuBand
->AdjustForTheme(m_useFlatMenus
);
394 // If needed, create the pager.
397 LONG pgStyles
= PGS_VERT
| WS_CHILD
| WS_VISIBLE
;
400 HWND hwndPager
= CreateWindowEx(
401 pgExStyles
, WC_PAGESCROLLER
, NULL
,
402 pgStyles
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
403 hwndParent
, NULL
, _AtlBaseModule
.GetModuleInstance(), 0);
405 m_pager
.SubclassWindow(hwndPager
);
407 ::SetParent(m_hWnd
, hwndPager
);
409 m_pager
.SendMessageW(PGM_SETCHILD
, 0, reinterpret_cast<LPARAM
>(m_hWnd
));
412 // Configure the image lists
418 HRESULT
CMenuToolbarBase::GetSizes(SIZE
* pMinSize
, SIZE
* pMaxSize
, SIZE
* pIntegralSize
)
421 *pMinSize
= m_idealSize
;
423 *pMaxSize
= m_idealSize
;
425 *pIntegralSize
= m_itemSize
;
430 TRACE("Sizes out of date, recalculating.\n");
437 // Obtain the ideal size, to be used as min and max
438 GetMaxSize(&m_idealSize
);
439 GetIdealSize((m_initFlags
& SMINIT_VERTICAL
) != 0, &m_idealSize
);
441 TRACE("Ideal Size: (%d, %d) for %d buttons\n", m_idealSize
, GetButtonCount());
443 // Obtain the button size, to be used as the integral size
444 DWORD size
= GetButtonSize();
445 m_itemSize
.cx
= GET_X_LPARAM(size
);
446 m_itemSize
.cy
= GET_Y_LPARAM(size
);
450 *pMinSize
= m_idealSize
;
452 *pMaxSize
= m_idealSize
;
454 *pIntegralSize
= m_itemSize
;
459 HRESULT
CMenuToolbarBase::SetPosSize(int x
, int y
, int cx
, int cy
)
461 // Update the toolbar or pager to fit the requested rect
462 // If we have a pager, set the toolbar height to the ideal height of the toolbar
465 SetWindowPos(NULL
, x
, y
, cx
, m_idealSize
.cy
, 0);
466 m_pager
.SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
470 SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
473 // In a vertical menu, resize the buttons to fit the width
474 if (m_initFlags
& SMINIT_VERTICAL
)
476 DWORD btnSize
= GetButtonSize();
477 SetButtonSize(cx
, GET_Y_LPARAM(btnSize
));
483 HRESULT
CMenuToolbarBase::IsWindowOwner(HWND hwnd
)
485 if (m_hWnd
&& m_hWnd
== hwnd
) return S_OK
;
486 if (m_pager
.m_hWnd
&& m_pager
.m_hWnd
== hwnd
) return S_OK
;
490 HRESULT
CMenuToolbarBase::GetWindow(HWND
*phwnd
)
496 *phwnd
= m_pager
.m_hWnd
;
503 HRESULT
CMenuToolbarBase::OnGetInfoTip(NMTBGETINFOTIP
* tip
)
508 INT iItem
= tip
->iItem
;
510 GetDataFromId(iItem
, &index
, &dwData
);
512 return InternalGetTooltip(iItem
, index
, dwData
, tip
->pszText
, tip
->cchTextMax
);
515 HRESULT
CMenuToolbarBase::OnPopupTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
517 if (wParam
!= TIMERID_HOTTRACK
)
523 KillTimer(TIMERID_HOTTRACK
);
528 m_timerEnabled
= FALSE
;
533 // Returns S_FALSE if the current item did not show a submenu
534 HRESULT hr
= PopupItem(m_hotItem
, FALSE
);
538 // If we didn't switch submenus, cancel the current popup regardless
541 HRESULT hr
= CancelCurrentPopup();
542 if (FAILED_UNEXPECTEDLY(hr
))
549 HRESULT
CMenuToolbarBase::KillPopupTimer()
553 m_timerEnabled
= FALSE
;
554 KillTimer(TIMERID_HOTTRACK
);
560 HRESULT
CMenuToolbarBase::ChangeHotItem(CMenuToolbarBase
* toolbar
, INT item
, DWORD dwFlags
)
562 // Ignore the change if it already matches the stored info
563 if (m_hotBar
== toolbar
&& m_hotItem
== item
)
566 // Prevent a change of hot item if the change was triggered by the mouse,
567 // and mouse tracking is disabled.
568 if (m_disableMouseTrack
&& dwFlags
& HICF_MOUSE
)
570 TRACE("Hot item change prevented by DisableMouseTrack\n");
574 // Notify the toolbar if the hot-tracking left this toolbar
575 if (m_hotBar
== this && toolbar
!= this)
580 TRACE("Hot item changed from %p %p, to %p %p\n", m_hotBar
, m_hotItem
, toolbar
, item
);
584 if (m_hotBar
== this)
586 if (m_isTrackingPopup
&& !(m_initFlags
& SMINIT_VERTICAL
))
588 // If the menubar has an open submenu, switch to the new item's submenu immediately
589 PopupItem(m_hotItem
, FALSE
);
591 else if (dwFlags
& HICF_MOUSE
)
593 // Vertical menus show/hide the submenu after a delay,
594 // but only with the mouse.
595 if (m_initFlags
& SMINIT_VERTICAL
)
598 SystemParametersInfo(SPI_GETMENUSHOWDELAY
, 0, &elapsed
, 0);
599 SetTimer(TIMERID_HOTTRACK
, elapsed
);
600 m_timerEnabled
= TRUE
;
601 TRACE("SetTimer called with m_hotItem=%d\n", m_hotItem
);
607 info
.cbSize
= sizeof(info
);
610 int index
= GetButtonInfo(item
, &info
);
620 HRESULT
CMenuToolbarBase::ChangePopupItem(CMenuToolbarBase
* toolbar
, INT item
)
622 // Ignore the change if it already matches the stored info
623 if (m_popupBar
== toolbar
&& m_popupItem
== item
)
626 // Notify the toolbar if the popup-tracking this toolbar
627 if (m_popupBar
== this && toolbar
!= this)
629 CheckButton(m_popupItem
, FALSE
);
630 m_isTrackingPopup
= FALSE
;
633 m_popupBar
= toolbar
;
636 if (m_popupBar
== this)
638 CheckButton(m_popupItem
, TRUE
);
645 LRESULT
CMenuToolbarBase::IsTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
648 INT idx
= (INT
)wParam
;
650 if (m_hotBar
!= this)
656 if (!GetButton(idx
, &btn
))
659 if (m_hotItem
== btn
.idCommand
)
662 if (m_popupItem
== btn
.idCommand
)
668 LRESULT
CMenuToolbarBase::ChangeTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
671 BOOL wasTracking
= LOWORD(lParam
);
672 BOOL mouse
= HIWORD(lParam
);
673 INT idx
= (INT
)wParam
;
677 m_isTrackingPopup
= FALSE
;
678 return m_menuBand
->_ChangeHotItem(NULL
, -1, HICF_MOUSE
);
681 if (!GetButton(idx
, &btn
))
684 TRACE("ChangeTrackedItem %d, %d\n", idx
, wasTracking
);
685 m_isTrackingPopup
= wasTracking
;
686 return m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, mouse
? HICF_MOUSE
: 0);
689 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, IShellMenu
* childShellMenu
, BOOL keyInitiated
)
691 // Calculate the submenu position and exclude area
694 if (!GetItemRect(index
, &rc
))
697 POINT a
= { rc
.left
, rc
.top
};
698 POINT b
= { rc
.right
, rc
.bottom
};
703 POINTL pt
= { a
.x
, b
.y
};
704 RECTL rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
706 if (m_initFlags
& SMINIT_VERTICAL
)
708 // FIXME: Hardcoding this here feels hacky.
721 // Display the submenu
722 m_isTrackingPopup
= TRUE
;
724 m_menuBand
->_ChangePopupItem(this, iItem
);
725 m_menuBand
->_OnPopupSubMenu(childShellMenu
, &pt
, &rcl
, keyInitiated
);
730 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, HMENU menu
)
732 // Calculate the submenu position and exclude area
735 if (!GetItemRect(index
, &rc
))
738 POINT a
= { rc
.left
, rc
.top
};
739 POINT b
= { rc
.right
, rc
.bottom
};
744 POINT pt
= { a
.x
, b
.y
};
745 RECT rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
747 if (m_initFlags
& SMINIT_VERTICAL
)
753 HMENU popup
= GetSubMenu(menu
, index
);
755 // Display the submenu
756 m_isTrackingPopup
= TRUE
;
757 m_menuBand
->_ChangePopupItem(this, iItem
);
758 m_menuBand
->_TrackSubMenu(popup
, pt
.x
, pt
.y
, rcl
);
759 m_menuBand
->_ChangePopupItem(NULL
, -1);
760 m_isTrackingPopup
= FALSE
;
765 HRESULT
CMenuToolbarBase::TrackContextMenu(IContextMenu
* contextMenu
, POINT pt
)
768 m_menuBand
->_KillPopupTimers();
770 m_menuBand
->_CancelCurrentPopup();
772 // Display the context menu
773 return m_menuBand
->_TrackContextMenu(contextMenu
, pt
.x
, pt
.y
);
776 HRESULT
CMenuToolbarBase::BeforeCancelPopup()
778 m_cancelingPopup
= TRUE
;
779 TRACE("BeforeCancelPopup\n");
783 HRESULT
CMenuToolbarBase::ProcessClick(INT iItem
)
785 if (m_disableMouseTrack
)
787 TRACE("Item click prevented by DisableMouseTrack\n");
791 // If a button is clicked while a submenu was open, cancel the submenu.
792 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
794 TRACE("OnCommand cancelled because it was tracking submenu.\n");
798 if (PopupItem(iItem
, FALSE
) == S_OK
)
800 TRACE("PopupItem returned S_OK\n");
804 TRACE("Executing...\n");
806 return m_menuBand
->_MenuItemSelect(MPOS_EXECUTE
);
809 HRESULT
CMenuToolbarBase::ProcessContextMenu(INT iItem
)
814 GetDataFromId(iItem
, &index
, &data
);
816 DWORD pos
= GetMessagePos();
817 POINT pt
= { GET_X_LPARAM(pos
), GET_Y_LPARAM(pos
) };
819 return InternalContextMenu(iItem
, index
, data
, pt
);
822 HRESULT
CMenuToolbarBase::MenuBarMouseDown(INT iIndex
, BOOL isLButton
)
826 GetButton(iIndex
, &btn
);
828 if ((m_initFlags
& SMINIT_VERTICAL
)
832 m_cancelingPopup
= FALSE
;
836 return ProcessClick(btn
.idCommand
);
839 HRESULT
CMenuToolbarBase::MenuBarMouseUp(INT iIndex
, BOOL isLButton
)
843 m_cancelingPopup
= FALSE
;
845 if (!(m_initFlags
& SMINIT_VERTICAL
))
848 GetButton(iIndex
, &btn
);
851 return ProcessClick(btn
.idCommand
);
853 return ProcessContextMenu(btn
.idCommand
);
856 HRESULT
CMenuToolbarBase::PrepareExecuteItem(INT iItem
)
858 this->m_menuBand
->_KillPopupTimers();
860 m_executeItem
= iItem
;
861 return GetDataFromId(iItem
, &m_executeIndex
, &m_executeData
);
864 HRESULT
CMenuToolbarBase::ExecuteItem()
866 return InternalExecuteItem(m_executeItem
, m_executeItem
, m_executeData
);
869 HRESULT
CMenuToolbarBase::KeyboardItemChange(DWORD dwSelectType
)
871 int prev
= m_hotItem
;
874 if (dwSelectType
!= 0xFFFFFFFF)
876 int count
= GetButtonCount();
878 if (dwSelectType
== VK_HOME
)
881 dwSelectType
= VK_DOWN
;
883 else if (dwSelectType
== VK_END
)
886 dwSelectType
= VK_UP
;
892 TBBUTTONINFO info
= { 0 };
893 info
.cbSize
= sizeof(TBBUTTONINFO
);
895 index
= GetButtonInfo(m_hotItem
, &info
);
900 if (dwSelectType
== VK_UP
)
904 else if (dwSelectType
== VK_DOWN
)
911 if (dwSelectType
== VK_UP
)
915 else if (dwSelectType
== VK_DOWN
)
922 TBBUTTON btn
= { 0 };
923 while (index
>= 0 && index
< count
)
925 DWORD res
= GetButton(index
, &btn
);
931 if (prev
!= btn
.idCommand
)
933 TRACE("Setting Hot item to %d\n", index
);
934 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
937 m_menuBand
->_GetTopLevelWindow(&tlw
);
938 SendMessageW(tlw
, WM_CANCELMODE
, 0, 0);
939 PostMessageW(WM_USER_CHANGETRACKEDITEM
, index
, MAKELPARAM(m_isTrackingPopup
, FALSE
));
942 m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, 0);
947 if (dwSelectType
== VK_UP
)
951 else if (dwSelectType
== VK_DOWN
)
962 TRACE("Setting Hot item to null\n");
963 m_menuBand
->_ChangeHotItem(NULL
, -1, 0);
969 HRESULT
CMenuToolbarBase::AddButton(DWORD commandId
, LPCWSTR caption
, BOOL hasSubMenu
, INT iconId
, DWORD_PTR buttonData
, BOOL last
)
971 TBBUTTON tbb
= { 0 };
973 tbb
.fsState
= TBSTATE_ENABLED
;
974 #if !USE_TBSTYLE_EX_VERTICAL
975 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
976 tbb
.fsState
|= TBSTATE_WRAP
;
978 tbb
.fsStyle
= BTNS_CHECKGROUP
;
980 if (hasSubMenu
&& (m_initFlags
& SMINIT_VERTICAL
))
981 tbb
.fsStyle
|= BTNS_DROPDOWN
;
983 if (!(m_initFlags
& SMINIT_VERTICAL
))
984 tbb
.fsStyle
|= BTNS_AUTOSIZE
;
986 tbb
.iString
= (INT_PTR
) caption
;
987 tbb
.idCommand
= commandId
;
989 tbb
.iBitmap
= iconId
;
990 tbb
.dwData
= buttonData
;
994 if (!AddButtons(1, &tbb
))
995 return HRESULT_FROM_WIN32(GetLastError());
999 HRESULT
CMenuToolbarBase::AddSeparator(BOOL last
)
1001 TBBUTTON tbb
= { 0 };
1003 tbb
.fsState
= TBSTATE_ENABLED
;
1004 #if !USE_TBSTYLE_EX_VERTICAL
1005 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
1006 tbb
.fsState
|= TBSTATE_WRAP
;
1008 tbb
.fsStyle
= BTNS_SEP
;
1013 if (!AddButtons(1, &tbb
))
1014 return HRESULT_FROM_WIN32(GetLastError());
1019 HRESULT
CMenuToolbarBase::AddPlaceholder()
1021 TBBUTTON tbb
= { 0 };
1022 WCHAR MenuString
[128];
1024 LoadStringW(GetModuleHandle(L
"shell32.dll"), IDS_MENU_EMPTY
, MenuString
, _countof(MenuString
));
1028 tbb
.iString
= (INT_PTR
) MenuString
;
1033 if (!AddButtons(1, &tbb
))
1034 return HRESULT_FROM_WIN32(GetLastError());
1039 HRESULT
CMenuToolbarBase::ClearToolbar()
1041 while (DeleteButton(0))
1049 HRESULT
CMenuToolbarBase::GetDataFromId(INT iItem
, INT
* pIndex
, DWORD_PTR
* pData
)
1060 TBBUTTONINFO info
= { 0 };
1062 info
.cbSize
= sizeof(TBBUTTONINFO
);
1063 info
.dwMask
= TBIF_COMMAND
| TBIF_LPARAM
;
1065 int index
= GetButtonInfo(iItem
, &info
);
1073 *pData
= info
.lParam
;
1078 HRESULT
CMenuToolbarBase::CancelCurrentPopup()
1080 return m_menuBand
->_CancelCurrentPopup();
1083 HRESULT
CMenuToolbarBase::PopupItem(INT iItem
, BOOL keyInitiated
)
1091 if (m_popupBar
== this && m_popupItem
== iItem
)
1094 GetDataFromId(iItem
, &index
, &dwData
);
1096 HRESULT hr
= InternalHasSubMenu(iItem
, index
, dwData
);
1102 HRESULT hr
= CancelCurrentPopup();
1103 if (FAILED_UNEXPECTEDLY(hr
))
1107 if (!(m_initFlags
& SMINIT_VERTICAL
))
1109 TRACE("PopupItem non-vertical %d %d\n", index
, iItem
);
1110 m_menuBand
->_ChangeHotItem(this, iItem
, 0);
1113 return InternalPopupItem(iItem
, index
, dwData
, keyInitiated
);
1116 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand
*menuBand
) :
1117 CMenuToolbarBase(menuBand
, FALSE
),
1123 CMenuStaticToolbar::~CMenuStaticToolbar()
1127 HRESULT
CMenuStaticToolbar::GetMenu(
1128 _Out_opt_ HMENU
*phmenu
,
1129 _Out_opt_ HWND
*phwnd
,
1130 _Out_opt_ DWORD
*pdwFlags
)
1135 *phwnd
= m_hwndMenu
;
1137 *pdwFlags
= m_dwMenuFlags
;
1142 HRESULT
CMenuStaticToolbar::SetMenu(
1149 m_dwMenuFlags
= dwFlags
;
1156 HRESULT
CMenuStaticToolbar::FillToolbar(BOOL clearFirst
)
1159 int ic
= GetMenuItemCount(m_hmenu
);
1167 for (i
= 0; i
< ic
; i
++)
1169 BOOL last
= i
+ 1 == ic
;
1173 info
.cbSize
= sizeof(info
);
1174 info
.dwTypeData
= NULL
;
1175 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_ID
;
1177 if (!GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
))
1179 TRACE("Error obtaining info for menu item at pos=%d\n", i
);
1185 if (info
.fType
& MFT_SEPARATOR
)
1189 else if (!(info
.fType
& MFT_BITMAP
))
1192 info
.dwTypeData
= (PWSTR
) HeapAlloc(GetProcessHeap(), 0, (info
.cch
+ 1) * sizeof(WCHAR
));
1194 info
.fMask
= MIIM_STRING
| MIIM_SUBMENU
| MIIM_ID
;
1195 GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
);
1197 SMINFO
* sminfo
= new SMINFO();
1198 sminfo
->dwMask
= SMIM_ICON
| SMIM_FLAGS
;
1200 HRESULT hr
= m_menuBand
->_CallCBWithItemId(info
.wID
, SMC_GETINFO
, 0, reinterpret_cast<LPARAM
>(sminfo
));
1201 if (FAILED_UNEXPECTEDLY(hr
))
1207 AddButton(info
.wID
, info
.dwTypeData
, info
.hSubMenu
!= NULL
, sminfo
->iIcon
, reinterpret_cast<DWORD_PTR
>(sminfo
), last
);
1209 HeapFree(GetProcessHeap(), 0, info
.dwTypeData
);
1216 HRESULT
CMenuStaticToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1218 //SMINFO * info = reinterpret_cast<SMINFO*>(dwData);
1223 HRESULT
CMenuStaticToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1225 delete reinterpret_cast<SMINFO
*>(tb
->tbButton
.dwData
);
1229 HRESULT
CMenuStaticToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1231 CComPtr
<IContextMenu
> contextMenu
;
1232 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
,
1233 reinterpret_cast<WPARAM
>(&IID_IContextMenu
), reinterpret_cast<LPARAM
>(&contextMenu
));
1237 return TrackContextMenu(contextMenu
, pt
);
1240 HRESULT
CMenuStaticToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1242 return m_menuBand
->_CallCBWithItemId(iItem
, SMC_EXEC
, 0, 0);
1245 HRESULT
CMenuStaticToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1247 SMINFO
* nfo
= reinterpret_cast<SMINFO
*>(dwData
);
1251 if (nfo
->dwFlags
&SMIF_TRACKPOPUP
)
1253 return PopupSubMenu(iItem
, index
, m_hmenu
);
1257 CComPtr
<IShellMenu
> shellMenu
;
1258 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
, reinterpret_cast<WPARAM
>(&IID_IShellMenu
), reinterpret_cast<LPARAM
>(&shellMenu
));
1259 if (FAILED_UNEXPECTEDLY(hr
))
1262 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1266 HRESULT
CMenuStaticToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1268 return ::GetSubMenu(m_hmenu
, index
) ? S_OK
: S_FALSE
;
1271 CMenuSFToolbar::CMenuSFToolbar(CMenuBand
* menuBand
) :
1272 CMenuToolbarBase(menuBand
, TRUE
),
1273 m_shellFolder(NULL
),
1279 CMenuSFToolbar::~CMenuSFToolbar()
1283 int CALLBACK
PidlListSort(void* item1
, void* item2
, LPARAM lParam
)
1285 IShellFolder
* psf
= (IShellFolder
*) lParam
;
1286 PCUIDLIST_RELATIVE pidl1
= (PCUIDLIST_RELATIVE
) item1
;
1287 PCUIDLIST_RELATIVE pidl2
= (PCUIDLIST_RELATIVE
) item2
;
1288 HRESULT hr
= psf
->CompareIDs(0, pidl1
, pidl2
);
1291 // No way to cancel, so sort to equal.
1294 return (int)(short)LOWORD(hr
);
1297 HRESULT
CMenuSFToolbar::FillToolbar(BOOL clearFirst
)
1301 CComPtr
<IEnumIDList
> eidl
;
1302 hr
= m_shellFolder
->EnumObjects(GetToolbar(), SHCONTF_FOLDERS
| SHCONTF_NONFOLDERS
, &eidl
);
1303 if (FAILED_UNEXPECTEDLY(hr
))
1306 HDPA dpaSort
= DPA_Create(10);
1308 LPITEMIDLIST item
= NULL
;
1309 hr
= eidl
->Next(1, &item
, NULL
);
1312 if (m_menuBand
->_CallCBWithItemPidl(item
, 0x10000000, 0, 0) == S_FALSE
)
1314 DPA_AppendPtr(dpaSort
, ILClone(item
));
1317 hr
= eidl
->Next(1, &item
, NULL
);
1320 // If no items were added, show the "empty" placeholder
1321 if (DPA_GetPtrCount(dpaSort
) == 0)
1323 DPA_Destroy(dpaSort
);
1324 return AddPlaceholder();
1327 TRACE("FillToolbar added %d items to the DPA\n", DPA_GetPtrCount(dpaSort
));
1329 DPA_Sort(dpaSort
, PidlListSort
, (LPARAM
) m_shellFolder
.p
);
1331 for (int i
= 0; i
<DPA_GetPtrCount(dpaSort
);)
1338 STRRET sr
= { STRRET_CSTR
, { 0 } };
1340 item
= (LPITEMIDLIST
)DPA_GetPtr(dpaSort
, i
);
1342 hr
= m_shellFolder
->GetDisplayNameOf(item
, SIGDN_NORMALDISPLAY
, &sr
);
1343 if (FAILED_UNEXPECTEDLY(hr
))
1345 DPA_Destroy(dpaSort
);
1349 StrRetToStr(&sr
, NULL
, &MenuString
);
1351 index
= SHMapPIDLToSystemImageListIndex(m_shellFolder
, item
, &indexOpen
);
1353 LPCITEMIDLIST itemc
= item
;
1355 SFGAOF attrs
= SFGAO_FOLDER
;
1356 hr
= m_shellFolder
->GetAttributesOf(1, &itemc
, &attrs
);
1358 DWORD_PTR dwData
= reinterpret_cast<DWORD_PTR
>(item
);
1360 // Fetch next item already, so we know if the current one is the last
1363 AddButton(i
, MenuString
, attrs
& SFGAO_FOLDER
, index
, dwData
, i
>= DPA_GetPtrCount(dpaSort
));
1365 CoTaskMemFree(MenuString
);
1368 DPA_Destroy(dpaSort
);
1372 HRESULT
CMenuSFToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1374 //ITEMIDLIST * pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1379 HRESULT
CMenuSFToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1381 ILFree(reinterpret_cast<LPITEMIDLIST
>(tb
->tbButton
.dwData
));
1385 HRESULT
CMenuSFToolbar::SetShellFolder(IShellFolder
*psf
, LPCITEMIDLIST pidlFolder
, HKEY hKey
, DWORD dwFlags
)
1387 m_shellFolder
= psf
;
1388 m_idList
= ILClone(pidlFolder
);
1390 m_dwMenuFlags
= dwFlags
;
1397 HRESULT
CMenuSFToolbar::GetShellFolder(DWORD
*pdwFlags
, LPITEMIDLIST
*ppidl
, REFIID riid
, void **ppv
)
1401 hr
= m_shellFolder
->QueryInterface(riid
, ppv
);
1402 if (FAILED_UNEXPECTEDLY(hr
))
1406 *pdwFlags
= m_dwMenuFlags
;
1410 LPITEMIDLIST pidl
= NULL
;
1414 pidl
= ILClone(m_idList
);
1417 ERR("ILClone failed!\n");
1418 (*reinterpret_cast<IUnknown
**>(ppv
))->Release();
1429 HRESULT
CMenuSFToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1432 CComPtr
<IContextMenu
> contextMenu
= NULL
;
1433 LPCITEMIDLIST pidl
= reinterpret_cast<LPCITEMIDLIST
>(dwData
);
1435 hr
= m_shellFolder
->GetUIObjectOf(GetToolbar(), 1, &pidl
, IID_NULL_PPV_ARG(IContextMenu
, &contextMenu
));
1436 if (FAILED_UNEXPECTEDLY(hr
))
1441 hr
= TrackContextMenu(contextMenu
, pt
);
1446 HRESULT
CMenuSFToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1448 return m_menuBand
->_CallCBWithItemPidl(reinterpret_cast<LPITEMIDLIST
>(data
), SMC_SFEXEC
, 0, 0);
1451 HRESULT
CMenuSFToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1457 CComPtr
<IShellMenuCallback
> psmc
;
1458 CComPtr
<IShellMenu
> shellMenu
;
1460 LPITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1465 hr
= CMenuBand_Constructor(IID_PPV_ARG(IShellMenu
, &shellMenu
));
1466 if (FAILED_UNEXPECTEDLY(hr
))
1469 m_menuBand
->GetMenuInfo(&psmc
, &uId
, &uIdAncestor
, &flags
);
1471 // FIXME: not sure what to use as uId/uIdAncestor here
1472 hr
= shellMenu
->Initialize(psmc
, 0, uId
, SMINIT_VERTICAL
);
1473 if (FAILED_UNEXPECTEDLY(hr
))
1476 CComPtr
<IShellFolder
> childFolder
;
1477 hr
= m_shellFolder
->BindToObject(pidl
, NULL
, IID_PPV_ARG(IShellFolder
, &childFolder
));
1478 if (FAILED_UNEXPECTEDLY(hr
))
1481 hr
= shellMenu
->SetShellFolder(childFolder
, NULL
, NULL
, 0);
1482 if (FAILED_UNEXPECTEDLY(hr
))
1485 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1488 HRESULT
CMenuSFToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1491 LPCITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1493 SFGAOF attrs
= SFGAO_FOLDER
;
1494 hr
= m_shellFolder
->GetAttributesOf(1, &pidl
, &attrs
);
1495 if (FAILED_UNEXPECTEDLY(hr
))
1498 return (attrs
& SFGAO_FOLDER
) ? S_OK
: S_FALSE
;