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
);
116 case WM_WININICHANGE
:
117 if (wParam
== SPI_SETFLATMENU
)
119 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
126 HRESULT
CMenuToolbarBase::DisableMouseTrack(BOOL bDisable
)
128 if (m_disableMouseTrack
!= bDisable
)
130 m_disableMouseTrack
= bDisable
;
131 TRACE("DisableMouseTrack %d\n", bDisable
);
136 HRESULT
CMenuToolbarBase::OnPagerCalcSize(LPNMPGCALCSIZE csize
)
139 GetSizes(NULL
, &tbs
, NULL
);
140 if (csize
->dwFlag
== PGF_CALCHEIGHT
)
142 csize
->iHeight
= tbs
.cy
;
144 else if (csize
->dwFlag
== PGF_CALCWIDTH
)
146 csize
->iWidth
= tbs
.cx
;
151 HRESULT
CMenuToolbarBase::OnCustomDraw(LPNMTBCUSTOMDRAW cdraw
, LRESULT
* theResult
)
156 switch (cdraw
->nmcd
.dwDrawStage
)
159 *theResult
= CDRF_NOTIFYITEMDRAW
;
162 case CDDS_ITEMPREPAINT
:
164 // The item with an active submenu gets the CHECKED flag.
165 isHot
= m_hotBar
== this && (int) cdraw
->nmcd
.dwItemSpec
== m_hotItem
;
166 isPopup
= m_popupBar
== this && (int) cdraw
->nmcd
.dwItemSpec
== m_popupItem
;
168 if (m_hotItem
< 0 && isPopup
)
171 if ((m_useFlatMenus
&& isHot
) || (m_initFlags
& SMINIT_VERTICAL
))
175 RECT rc
= cdraw
->nmcd
.rc
;
176 HDC hdc
= cdraw
->nmcd
.hdc
;
178 // Remove HOT and CHECKED flags (will restore HOT if necessary)
179 cdraw
->nmcd
.uItemState
&= ~(CDIS_HOT
| CDIS_CHECKED
);
181 // Decide on the colors
184 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
186 clrText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
187 bgBrush
= GetSysColorBrush(m_useFlatMenus
? COLOR_MENUHILIGHT
: COLOR_HIGHLIGHT
);
191 clrText
= GetSysColor(COLOR_MENUTEXT
);
192 bgBrush
= GetSysColorBrush(COLOR_MENU
);
195 // Paint the background color with the selected color
196 FillRect(hdc
, &rc
, bgBrush
);
198 // Set the text color in advance, this color will be assigned when the ITEMPOSTPAINT triggers
199 SetTextColor(hdc
, clrText
);
201 // Set the text color, will be used by the internal drawing code
202 cdraw
->clrText
= clrText
;
203 cdraw
->iListGap
+= 4;
205 // Tell the default drawing code we don't want any fanciness, not even a background.
206 *theResult
= CDRF_NOTIFYPOSTPAINT
| TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| 0x00800000; // FIXME: the last bit is Vista+, useful for debugging only
210 // Remove HOT and CHECKED flags (will restore HOT if necessary)
211 cdraw
->nmcd
.uItemState
&= ~CDIS_HOT
;
213 // Decide on the colors
216 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
224 case CDDS_ITEMPOSTPAINT
:
226 // Fetch the button style
227 btni
.cbSize
= sizeof(btni
);
228 btni
.dwMask
= TBIF_STYLE
;
229 GetButtonInfo(cdraw
->nmcd
.dwItemSpec
, &btni
);
231 // Check if we need to draw a submenu arrow
232 if (btni
.fsStyle
& BTNS_DROPDOWN
)
234 // TODO: Support RTL text modes by drawing a leftwards arrow aligned to the left of the control
236 // "8" is the rightwards dropdown arrow in the Marlett font
237 WCHAR text
[] = L
"8";
239 // Configure the font to draw with Marlett, keeping the current background color as-is
240 SelectObject(cdraw
->nmcd
.hdc
, m_marlett
);
241 SetBkMode(cdraw
->nmcd
.hdc
, TRANSPARENT
);
243 // Tweak the alignment by 1 pixel so the menu draws like the Windows start menu.
244 RECT rc
= cdraw
->nmcd
.rc
;
247 // The arrow is drawn at the right of the item's rect, aligned vertically.
248 DrawTextEx(cdraw
->nmcd
.hdc
, text
, 1, &rc
, DT_NOCLIP
| DT_VCENTER
| DT_RIGHT
| DT_SINGLELINE
, NULL
);
256 CMenuToolbarBase::CMenuToolbarBase(CMenuBand
*menuBand
, BOOL usePager
) :
258 m_useFlatMenus(FALSE
),
260 m_disableMouseTrack(FALSE
),
261 m_timerEnabled(FALSE
),
262 m_menuBand(menuBand
),
265 m_usePager(usePager
),
270 m_isTrackingPopup(FALSE
),
271 m_cancelingPopup(FALSE
)
277 m_marlett
= CreateFont(
278 0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET
,
279 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
280 DEFAULT_QUALITY
, FF_DONTCARE
, L
"Marlett");
283 CMenuToolbarBase::~CMenuToolbarBase()
289 m_pager
.DestroyWindow();
291 DeleteObject(m_marlett
);
294 void CMenuToolbarBase::InvalidateDraw()
296 InvalidateRect(NULL
, FALSE
);
299 HRESULT
CMenuToolbarBase::ShowDW(BOOL fShow
)
301 ShowWindow(fShow
? SW_SHOW
: SW_HIDE
);
303 // Ensure that the right image list is assigned to the toolbar
306 // For custom-drawing
307 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
312 HRESULT
CMenuToolbarBase::UpdateImageLists()
314 if ((m_initFlags
& (SMINIT_TOPLEVEL
| SMINIT_VERTICAL
)) == SMINIT_TOPLEVEL
) // not vertical.
316 // No image list, prevents the buttons from having a margin at the left side
321 // Assign the correct imagelist and padding based on the current icon size
324 if (m_menuBand
->UseBigIcons())
336 HRESULT hr
= SHGetImageList(shiml
, IID_PPV_ARG(IImageList
, &piml
));
337 if (FAILED_UNEXPECTEDLY(hr
))
343 SetImageList((HIMAGELIST
)piml
);
348 HRESULT
CMenuToolbarBase::Close()
354 m_pager
.DestroyWindow();
359 HRESULT
CMenuToolbarBase::CreateToolbar(HWND hwndParent
, DWORD dwFlags
)
361 LONG tbStyles
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
|
362 TBSTYLE_TOOLTIPS
| TBSTYLE_TRANSPARENT
| TBSTYLE_REGISTERDROP
| TBSTYLE_LIST
| TBSTYLE_FLAT
| TBSTYLE_CUSTOMERASE
|
363 CCS_NODIVIDER
| CCS_NOPARENTALIGN
| CCS_NORESIZE
| CCS_TOP
;
364 LONG tbExStyles
= TBSTYLE_EX_DOUBLEBUFFER
| WS_EX_TOOLWINDOW
;
366 if (dwFlags
& SMINIT_VERTICAL
)
368 // Activate vertical semantics
369 tbStyles
|= CCS_VERT
;
371 #if USE_TBSTYLE_EX_VERTICAL
372 tbExStyles
|= TBSTYLE_EX_VERTICAL
;
376 m_initFlags
= dwFlags
;
378 // Get a temporary rect to use while creating the toolbar window.
379 // Ensure that it is not a null rect.
381 if (!::GetClientRect(hwndParent
, &rc
) ||
382 (rc
.left
== rc
.right
) ||
383 (rc
.top
== rc
.bottom
))
391 SubclassWindow(CToolbar::Create(hwndParent
, tbStyles
, tbExStyles
));
393 SetWindowTheme(m_hWnd
, L
"", L
"");
395 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
397 m_menuBand
->AdjustForTheme(m_useFlatMenus
);
399 // If needed, create the pager.
402 LONG pgStyles
= PGS_VERT
| WS_CHILD
| WS_VISIBLE
;
405 HWND hwndPager
= CreateWindowEx(
406 pgExStyles
, WC_PAGESCROLLER
, NULL
,
407 pgStyles
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
408 hwndParent
, NULL
, _AtlBaseModule
.GetModuleInstance(), 0);
410 m_pager
.SubclassWindow(hwndPager
);
412 ::SetParent(m_hWnd
, hwndPager
);
414 m_pager
.SendMessageW(PGM_SETCHILD
, 0, reinterpret_cast<LPARAM
>(m_hWnd
));
417 // Configure the image lists
423 HRESULT
CMenuToolbarBase::GetSizes(SIZE
* pMinSize
, SIZE
* pMaxSize
, SIZE
* pIntegralSize
)
426 *pMinSize
= m_idealSize
;
428 *pMaxSize
= m_idealSize
;
430 *pIntegralSize
= m_itemSize
;
435 TRACE("Sizes out of date, recalculating.\n");
442 // Obtain the ideal size, to be used as min and max
443 GetMaxSize(&m_idealSize
);
444 GetIdealSize((m_initFlags
& SMINIT_VERTICAL
) != 0, &m_idealSize
);
446 TRACE("Ideal Size: (%d, %d) for %d buttons\n", m_idealSize
, GetButtonCount());
448 // Obtain the button size, to be used as the integral size
449 DWORD size
= GetButtonSize();
450 m_itemSize
.cx
= GET_X_LPARAM(size
);
451 m_itemSize
.cy
= GET_Y_LPARAM(size
);
455 *pMinSize
= m_idealSize
;
457 *pMaxSize
= m_idealSize
;
459 *pIntegralSize
= m_itemSize
;
464 HRESULT
CMenuToolbarBase::SetPosSize(int x
, int y
, int cx
, int cy
)
466 // Update the toolbar or pager to fit the requested rect
467 // If we have a pager, set the toolbar height to the ideal height of the toolbar
470 SetWindowPos(NULL
, x
, y
, cx
, m_idealSize
.cy
, 0);
471 m_pager
.SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
475 SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
478 // In a vertical menu, resize the buttons to fit the width
479 if (m_initFlags
& SMINIT_VERTICAL
)
481 DWORD btnSize
= GetButtonSize();
482 SetButtonSize(cx
, GET_Y_LPARAM(btnSize
));
488 HRESULT
CMenuToolbarBase::IsWindowOwner(HWND hwnd
)
490 if (m_hWnd
&& m_hWnd
== hwnd
) return S_OK
;
491 if (m_pager
.m_hWnd
&& m_pager
.m_hWnd
== hwnd
) return S_OK
;
495 HRESULT
CMenuToolbarBase::GetWindow(HWND
*phwnd
)
501 *phwnd
= m_pager
.m_hWnd
;
508 HRESULT
CMenuToolbarBase::OnGetInfoTip(NMTBGETINFOTIP
* tip
)
513 INT iItem
= tip
->iItem
;
515 GetDataFromId(iItem
, &index
, &dwData
);
517 return InternalGetTooltip(iItem
, index
, dwData
, tip
->pszText
, tip
->cchTextMax
);
520 HRESULT
CMenuToolbarBase::OnPopupTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
522 if (wParam
!= TIMERID_HOTTRACK
)
528 KillTimer(TIMERID_HOTTRACK
);
533 m_timerEnabled
= FALSE
;
538 // Returns S_FALSE if the current item did not show a submenu
539 HRESULT hr
= PopupItem(m_hotItem
, FALSE
);
543 // If we didn't switch submenus, cancel the current popup regardless
546 HRESULT hr
= CancelCurrentPopup();
547 if (FAILED_UNEXPECTEDLY(hr
))
554 HRESULT
CMenuToolbarBase::KillPopupTimer()
558 m_timerEnabled
= FALSE
;
559 KillTimer(TIMERID_HOTTRACK
);
565 HRESULT
CMenuToolbarBase::ChangeHotItem(CMenuToolbarBase
* toolbar
, INT item
, DWORD dwFlags
)
567 // Ignore the change if it already matches the stored info
568 if (m_hotBar
== toolbar
&& m_hotItem
== item
)
571 // Prevent a change of hot item if the change was triggered by the mouse,
572 // and mouse tracking is disabled.
573 if (m_disableMouseTrack
&& dwFlags
& HICF_MOUSE
)
575 TRACE("Hot item change prevented by DisableMouseTrack\n");
579 // Notify the toolbar if the hot-tracking left this toolbar
580 if (m_hotBar
== this && toolbar
!= this)
585 TRACE("Hot item changed from %p %p, to %p %p\n", m_hotBar
, m_hotItem
, toolbar
, item
);
589 if (m_hotBar
== this)
591 if (m_isTrackingPopup
&& !(m_initFlags
& SMINIT_VERTICAL
))
593 // If the menubar has an open submenu, switch to the new item's submenu immediately
594 PopupItem(m_hotItem
, FALSE
);
596 else if (dwFlags
& HICF_MOUSE
)
598 // Vertical menus show/hide the submenu after a delay,
599 // but only with the mouse.
600 if (m_initFlags
& SMINIT_VERTICAL
)
603 SystemParametersInfo(SPI_GETMENUSHOWDELAY
, 0, &elapsed
, 0);
604 SetTimer(TIMERID_HOTTRACK
, elapsed
);
605 m_timerEnabled
= TRUE
;
606 TRACE("SetTimer called with m_hotItem=%d\n", m_hotItem
);
612 info
.cbSize
= sizeof(info
);
615 int index
= GetButtonInfo(item
, &info
);
625 HRESULT
CMenuToolbarBase::ChangePopupItem(CMenuToolbarBase
* toolbar
, INT item
)
627 // Ignore the change if it already matches the stored info
628 if (m_popupBar
== toolbar
&& m_popupItem
== item
)
631 // Notify the toolbar if the popup-tracking this toolbar
632 if (m_popupBar
== this && toolbar
!= this)
634 CheckButton(m_popupItem
, FALSE
);
635 m_isTrackingPopup
= FALSE
;
638 m_popupBar
= toolbar
;
641 if (m_popupBar
== this)
643 CheckButton(m_popupItem
, TRUE
);
650 LRESULT
CMenuToolbarBase::IsTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
653 INT idx
= (INT
)wParam
;
655 if (m_hotBar
!= this)
661 if (!GetButton(idx
, &btn
))
664 if (m_hotItem
== btn
.idCommand
)
667 if (m_popupItem
== btn
.idCommand
)
673 LRESULT
CMenuToolbarBase::ChangeTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
676 BOOL wasTracking
= LOWORD(lParam
);
677 BOOL mouse
= HIWORD(lParam
);
678 INT idx
= (INT
)wParam
;
682 m_isTrackingPopup
= FALSE
;
683 return m_menuBand
->_ChangeHotItem(NULL
, -1, HICF_MOUSE
);
686 if (!GetButton(idx
, &btn
))
689 TRACE("ChangeTrackedItem %d, %d\n", idx
, wasTracking
);
690 m_isTrackingPopup
= wasTracking
;
691 return m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, mouse
? HICF_MOUSE
: 0);
694 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, IShellMenu
* childShellMenu
, BOOL keyInitiated
)
696 // Calculate the submenu position and exclude area
699 if (!GetItemRect(index
, &rc
))
702 POINT a
= { rc
.left
, rc
.top
};
703 POINT b
= { rc
.right
, rc
.bottom
};
708 POINTL pt
= { a
.x
, b
.y
};
709 RECTL rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
711 if (m_initFlags
& SMINIT_VERTICAL
)
713 // FIXME: Hardcoding this here feels hacky.
726 // Display the submenu
727 m_isTrackingPopup
= TRUE
;
729 m_menuBand
->_ChangePopupItem(this, iItem
);
730 m_menuBand
->_OnPopupSubMenu(childShellMenu
, &pt
, &rcl
, keyInitiated
);
735 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, HMENU menu
)
737 // Calculate the submenu position and exclude area
740 if (!GetItemRect(index
, &rc
))
743 POINT a
= { rc
.left
, rc
.top
};
744 POINT b
= { rc
.right
, rc
.bottom
};
749 POINT pt
= { a
.x
, b
.y
};
750 RECT rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
752 if (m_initFlags
& SMINIT_VERTICAL
)
758 HMENU popup
= GetSubMenu(menu
, index
);
760 // Display the submenu
761 m_isTrackingPopup
= TRUE
;
762 m_menuBand
->_ChangePopupItem(this, iItem
);
763 m_menuBand
->_TrackSubMenu(popup
, pt
.x
, pt
.y
, rcl
);
764 m_menuBand
->_ChangePopupItem(NULL
, -1);
765 m_isTrackingPopup
= FALSE
;
770 HRESULT
CMenuToolbarBase::TrackContextMenu(IContextMenu
* contextMenu
, POINT pt
)
773 m_menuBand
->_KillPopupTimers();
775 m_menuBand
->_CancelCurrentPopup();
777 // Display the context menu
778 return m_menuBand
->_TrackContextMenu(contextMenu
, pt
.x
, pt
.y
);
781 HRESULT
CMenuToolbarBase::BeforeCancelPopup()
783 m_cancelingPopup
= TRUE
;
784 TRACE("BeforeCancelPopup\n");
788 HRESULT
CMenuToolbarBase::ProcessClick(INT iItem
)
790 if (m_disableMouseTrack
)
792 TRACE("Item click prevented by DisableMouseTrack\n");
796 // If a button is clicked while a submenu was open, cancel the submenu.
797 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
799 TRACE("OnCommand cancelled because it was tracking submenu.\n");
803 if (PopupItem(iItem
, FALSE
) == S_OK
)
805 TRACE("PopupItem returned S_OK\n");
809 TRACE("Executing...\n");
811 return m_menuBand
->_MenuItemSelect(MPOS_EXECUTE
);
814 HRESULT
CMenuToolbarBase::ProcessContextMenu(INT iItem
)
819 GetDataFromId(iItem
, &index
, &data
);
821 DWORD pos
= GetMessagePos();
822 POINT pt
= { GET_X_LPARAM(pos
), GET_Y_LPARAM(pos
) };
824 return InternalContextMenu(iItem
, index
, data
, pt
);
827 HRESULT
CMenuToolbarBase::MenuBarMouseDown(INT iIndex
, BOOL isLButton
)
831 GetButton(iIndex
, &btn
);
833 if ((m_initFlags
& SMINIT_VERTICAL
)
837 m_cancelingPopup
= FALSE
;
841 return ProcessClick(btn
.idCommand
);
844 HRESULT
CMenuToolbarBase::MenuBarMouseUp(INT iIndex
, BOOL isLButton
)
848 m_cancelingPopup
= FALSE
;
850 if (!(m_initFlags
& SMINIT_VERTICAL
))
853 GetButton(iIndex
, &btn
);
856 return ProcessClick(btn
.idCommand
);
858 return ProcessContextMenu(btn
.idCommand
);
861 HRESULT
CMenuToolbarBase::PrepareExecuteItem(INT iItem
)
863 this->m_menuBand
->_KillPopupTimers();
865 m_executeItem
= iItem
;
866 return GetDataFromId(iItem
, &m_executeIndex
, &m_executeData
);
869 HRESULT
CMenuToolbarBase::ExecuteItem()
871 return InternalExecuteItem(m_executeItem
, m_executeItem
, m_executeData
);
874 HRESULT
CMenuToolbarBase::KeyboardItemChange(DWORD dwSelectType
)
876 int prev
= m_hotItem
;
879 if (dwSelectType
!= 0xFFFFFFFF)
881 int count
= GetButtonCount();
883 if (dwSelectType
== VK_HOME
)
886 dwSelectType
= VK_DOWN
;
888 else if (dwSelectType
== VK_END
)
891 dwSelectType
= VK_UP
;
897 TBBUTTONINFO info
= { 0 };
898 info
.cbSize
= sizeof(TBBUTTONINFO
);
900 index
= GetButtonInfo(m_hotItem
, &info
);
905 if (dwSelectType
== VK_UP
)
909 else if (dwSelectType
== VK_DOWN
)
916 if (dwSelectType
== VK_UP
)
920 else if (dwSelectType
== VK_DOWN
)
927 TBBUTTON btn
= { 0 };
928 while (index
>= 0 && index
< count
)
930 DWORD res
= GetButton(index
, &btn
);
936 if (prev
!= btn
.idCommand
)
938 TRACE("Setting Hot item to %d\n", index
);
939 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
942 m_menuBand
->_GetTopLevelWindow(&tlw
);
943 SendMessageW(tlw
, WM_CANCELMODE
, 0, 0);
944 PostMessageW(WM_USER_CHANGETRACKEDITEM
, index
, MAKELPARAM(m_isTrackingPopup
, FALSE
));
947 m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, 0);
952 if (dwSelectType
== VK_UP
)
956 else if (dwSelectType
== VK_DOWN
)
967 TRACE("Setting Hot item to null\n");
968 m_menuBand
->_ChangeHotItem(NULL
, -1, 0);
974 HRESULT
CMenuToolbarBase::AddButton(DWORD commandId
, LPCWSTR caption
, BOOL hasSubMenu
, INT iconId
, DWORD_PTR buttonData
, BOOL last
)
976 TBBUTTON tbb
= { 0 };
978 tbb
.fsState
= TBSTATE_ENABLED
;
979 #if !USE_TBSTYLE_EX_VERTICAL
980 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
981 tbb
.fsState
|= TBSTATE_WRAP
;
983 tbb
.fsStyle
= BTNS_CHECKGROUP
;
985 if (hasSubMenu
&& (m_initFlags
& SMINIT_VERTICAL
))
986 tbb
.fsStyle
|= BTNS_DROPDOWN
;
988 if (!(m_initFlags
& SMINIT_VERTICAL
))
989 tbb
.fsStyle
|= BTNS_AUTOSIZE
;
991 tbb
.iString
= (INT_PTR
) caption
;
992 tbb
.idCommand
= commandId
;
994 tbb
.iBitmap
= iconId
;
995 tbb
.dwData
= buttonData
;
999 if (!AddButtons(1, &tbb
))
1000 return HRESULT_FROM_WIN32(GetLastError());
1004 HRESULT
CMenuToolbarBase::AddSeparator(BOOL last
)
1006 TBBUTTON tbb
= { 0 };
1008 tbb
.fsState
= TBSTATE_ENABLED
;
1009 #if !USE_TBSTYLE_EX_VERTICAL
1010 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
1011 tbb
.fsState
|= TBSTATE_WRAP
;
1013 tbb
.fsStyle
= BTNS_SEP
;
1018 if (!AddButtons(1, &tbb
))
1019 return HRESULT_FROM_WIN32(GetLastError());
1024 HRESULT
CMenuToolbarBase::AddPlaceholder()
1026 TBBUTTON tbb
= { 0 };
1027 WCHAR MenuString
[128];
1029 LoadStringW(GetModuleHandle(L
"shell32.dll"), IDS_MENU_EMPTY
, MenuString
, _countof(MenuString
));
1033 tbb
.iString
= (INT_PTR
) MenuString
;
1038 if (!AddButtons(1, &tbb
))
1039 return HRESULT_FROM_WIN32(GetLastError());
1044 HRESULT
CMenuToolbarBase::ClearToolbar()
1046 while (DeleteButton(0))
1054 HRESULT
CMenuToolbarBase::GetDataFromId(INT iItem
, INT
* pIndex
, DWORD_PTR
* pData
)
1065 TBBUTTONINFO info
= { 0 };
1067 info
.cbSize
= sizeof(TBBUTTONINFO
);
1068 info
.dwMask
= TBIF_COMMAND
| TBIF_LPARAM
;
1070 int index
= GetButtonInfo(iItem
, &info
);
1078 *pData
= info
.lParam
;
1083 HRESULT
CMenuToolbarBase::CancelCurrentPopup()
1085 return m_menuBand
->_CancelCurrentPopup();
1088 HRESULT
CMenuToolbarBase::PopupItem(INT iItem
, BOOL keyInitiated
)
1096 if (m_popupBar
== this && m_popupItem
== iItem
)
1099 GetDataFromId(iItem
, &index
, &dwData
);
1101 HRESULT hr
= InternalHasSubMenu(iItem
, index
, dwData
);
1107 HRESULT hr
= CancelCurrentPopup();
1108 if (FAILED_UNEXPECTEDLY(hr
))
1112 if (!(m_initFlags
& SMINIT_VERTICAL
))
1114 TRACE("PopupItem non-vertical %d %d\n", index
, iItem
);
1115 m_menuBand
->_ChangeHotItem(this, iItem
, 0);
1118 return InternalPopupItem(iItem
, index
, dwData
, keyInitiated
);
1121 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand
*menuBand
) :
1122 CMenuToolbarBase(menuBand
, FALSE
),
1128 CMenuStaticToolbar::~CMenuStaticToolbar()
1132 HRESULT
CMenuStaticToolbar::GetMenu(
1133 _Out_opt_ HMENU
*phmenu
,
1134 _Out_opt_ HWND
*phwnd
,
1135 _Out_opt_ DWORD
*pdwFlags
)
1140 *phwnd
= m_hwndMenu
;
1142 *pdwFlags
= m_dwMenuFlags
;
1147 HRESULT
CMenuStaticToolbar::SetMenu(
1154 m_dwMenuFlags
= dwFlags
;
1161 HRESULT
CMenuStaticToolbar::FillToolbar(BOOL clearFirst
)
1164 int ic
= GetMenuItemCount(m_hmenu
);
1172 for (i
= 0; i
< ic
; i
++)
1174 BOOL last
= i
+ 1 == ic
;
1178 info
.cbSize
= sizeof(info
);
1179 info
.dwTypeData
= NULL
;
1180 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_ID
;
1182 if (!GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
))
1184 TRACE("Error obtaining info for menu item at pos=%d\n", i
);
1190 if (info
.fType
& MFT_SEPARATOR
)
1194 else if (!(info
.fType
& MFT_BITMAP
))
1197 info
.dwTypeData
= (PWSTR
) HeapAlloc(GetProcessHeap(), 0, (info
.cch
+ 1) * sizeof(WCHAR
));
1199 info
.fMask
= MIIM_STRING
| MIIM_SUBMENU
| MIIM_ID
;
1200 GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
);
1202 SMINFO
* sminfo
= new SMINFO();
1203 sminfo
->dwMask
= SMIM_ICON
| SMIM_FLAGS
;
1205 HRESULT hr
= m_menuBand
->_CallCBWithItemId(info
.wID
, SMC_GETINFO
, 0, reinterpret_cast<LPARAM
>(sminfo
));
1206 if (FAILED_UNEXPECTEDLY(hr
))
1212 AddButton(info
.wID
, info
.dwTypeData
, info
.hSubMenu
!= NULL
, sminfo
->iIcon
, reinterpret_cast<DWORD_PTR
>(sminfo
), last
);
1214 HeapFree(GetProcessHeap(), 0, info
.dwTypeData
);
1221 HRESULT
CMenuStaticToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1223 //SMINFO * info = reinterpret_cast<SMINFO*>(dwData);
1228 HRESULT
CMenuStaticToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1230 delete reinterpret_cast<SMINFO
*>(tb
->tbButton
.dwData
);
1234 HRESULT
CMenuStaticToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1236 CComPtr
<IContextMenu
> contextMenu
;
1237 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
,
1238 reinterpret_cast<WPARAM
>(&IID_IContextMenu
), reinterpret_cast<LPARAM
>(&contextMenu
));
1242 return TrackContextMenu(contextMenu
, pt
);
1245 HRESULT
CMenuStaticToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1247 return m_menuBand
->_CallCBWithItemId(iItem
, SMC_EXEC
, 0, 0);
1250 HRESULT
CMenuStaticToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1252 SMINFO
* nfo
= reinterpret_cast<SMINFO
*>(dwData
);
1256 if (nfo
->dwFlags
&SMIF_TRACKPOPUP
)
1258 return PopupSubMenu(iItem
, index
, m_hmenu
);
1262 CComPtr
<IShellMenu
> shellMenu
;
1263 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
, reinterpret_cast<WPARAM
>(&IID_IShellMenu
), reinterpret_cast<LPARAM
>(&shellMenu
));
1264 if (FAILED_UNEXPECTEDLY(hr
))
1267 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1271 HRESULT
CMenuStaticToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1273 return ::GetSubMenu(m_hmenu
, index
) ? S_OK
: S_FALSE
;
1276 CMenuSFToolbar::CMenuSFToolbar(CMenuBand
* menuBand
) :
1277 CMenuToolbarBase(menuBand
, TRUE
),
1278 m_shellFolder(NULL
),
1284 CMenuSFToolbar::~CMenuSFToolbar()
1288 int CALLBACK
PidlListSort(void* item1
, void* item2
, LPARAM lParam
)
1290 IShellFolder
* psf
= (IShellFolder
*) lParam
;
1291 PCUIDLIST_RELATIVE pidl1
= (PCUIDLIST_RELATIVE
) item1
;
1292 PCUIDLIST_RELATIVE pidl2
= (PCUIDLIST_RELATIVE
) item2
;
1293 HRESULT hr
= psf
->CompareIDs(0, pidl1
, pidl2
);
1296 // No way to cancel, so sort to equal.
1299 return (int)(short)LOWORD(hr
);
1302 HRESULT
CMenuSFToolbar::FillToolbar(BOOL clearFirst
)
1306 CComPtr
<IEnumIDList
> eidl
;
1307 hr
= m_shellFolder
->EnumObjects(GetToolbar(), SHCONTF_FOLDERS
| SHCONTF_NONFOLDERS
, &eidl
);
1308 if (FAILED_UNEXPECTEDLY(hr
))
1311 HDPA dpaSort
= DPA_Create(10);
1313 LPITEMIDLIST item
= NULL
;
1314 hr
= eidl
->Next(1, &item
, NULL
);
1317 if (m_menuBand
->_CallCBWithItemPidl(item
, 0x10000000, 0, 0) == S_FALSE
)
1319 DPA_AppendPtr(dpaSort
, ILClone(item
));
1322 hr
= eidl
->Next(1, &item
, NULL
);
1325 // If no items were added, show the "empty" placeholder
1326 if (DPA_GetPtrCount(dpaSort
) == 0)
1328 DPA_Destroy(dpaSort
);
1329 return AddPlaceholder();
1332 TRACE("FillToolbar added %d items to the DPA\n", DPA_GetPtrCount(dpaSort
));
1334 DPA_Sort(dpaSort
, PidlListSort
, (LPARAM
) m_shellFolder
.p
);
1336 for (int i
= 0; i
<DPA_GetPtrCount(dpaSort
);)
1343 STRRET sr
= { STRRET_CSTR
, { 0 } };
1345 item
= (LPITEMIDLIST
)DPA_GetPtr(dpaSort
, i
);
1347 hr
= m_shellFolder
->GetDisplayNameOf(item
, SIGDN_NORMALDISPLAY
, &sr
);
1348 if (FAILED_UNEXPECTEDLY(hr
))
1350 DPA_Destroy(dpaSort
);
1354 StrRetToStr(&sr
, NULL
, &MenuString
);
1356 index
= SHMapPIDLToSystemImageListIndex(m_shellFolder
, item
, &indexOpen
);
1358 LPCITEMIDLIST itemc
= item
;
1360 SFGAOF attrs
= SFGAO_FOLDER
;
1361 hr
= m_shellFolder
->GetAttributesOf(1, &itemc
, &attrs
);
1363 DWORD_PTR dwData
= reinterpret_cast<DWORD_PTR
>(item
);
1365 // Fetch next item already, so we know if the current one is the last
1368 AddButton(i
, MenuString
, attrs
& SFGAO_FOLDER
, index
, dwData
, i
>= DPA_GetPtrCount(dpaSort
));
1370 CoTaskMemFree(MenuString
);
1373 DPA_Destroy(dpaSort
);
1377 HRESULT
CMenuSFToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1379 //ITEMIDLIST * pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1384 HRESULT
CMenuSFToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1386 ILFree(reinterpret_cast<LPITEMIDLIST
>(tb
->tbButton
.dwData
));
1390 HRESULT
CMenuSFToolbar::SetShellFolder(IShellFolder
*psf
, LPCITEMIDLIST pidlFolder
, HKEY hKey
, DWORD dwFlags
)
1392 m_shellFolder
= psf
;
1393 m_idList
= ILClone(pidlFolder
);
1395 m_dwMenuFlags
= dwFlags
;
1402 HRESULT
CMenuSFToolbar::GetShellFolder(DWORD
*pdwFlags
, LPITEMIDLIST
*ppidl
, REFIID riid
, void **ppv
)
1406 hr
= m_shellFolder
->QueryInterface(riid
, ppv
);
1407 if (FAILED_UNEXPECTEDLY(hr
))
1411 *pdwFlags
= m_dwMenuFlags
;
1415 LPITEMIDLIST pidl
= NULL
;
1419 pidl
= ILClone(m_idList
);
1422 ERR("ILClone failed!\n");
1423 (*reinterpret_cast<IUnknown
**>(ppv
))->Release();
1434 HRESULT
CMenuSFToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1437 CComPtr
<IContextMenu
> contextMenu
= NULL
;
1438 LPCITEMIDLIST pidl
= reinterpret_cast<LPCITEMIDLIST
>(dwData
);
1440 hr
= m_shellFolder
->GetUIObjectOf(GetToolbar(), 1, &pidl
, IID_NULL_PPV_ARG(IContextMenu
, &contextMenu
));
1441 if (FAILED_UNEXPECTEDLY(hr
))
1446 hr
= TrackContextMenu(contextMenu
, pt
);
1451 HRESULT
CMenuSFToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1453 return m_menuBand
->_CallCBWithItemPidl(reinterpret_cast<LPITEMIDLIST
>(data
), SMC_SFEXEC
, 0, 0);
1456 HRESULT
CMenuSFToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1462 CComPtr
<IShellMenuCallback
> psmc
;
1463 CComPtr
<IShellMenu
> shellMenu
;
1465 LPITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1470 hr
= CMenuBand_CreateInstance(IID_PPV_ARG(IShellMenu
, &shellMenu
));
1471 if (FAILED_UNEXPECTEDLY(hr
))
1474 m_menuBand
->GetMenuInfo(&psmc
, &uId
, &uIdAncestor
, &flags
);
1476 // FIXME: not sure what to use as uId/uIdAncestor here
1477 hr
= shellMenu
->Initialize(psmc
, 0, uId
, SMINIT_VERTICAL
);
1478 if (FAILED_UNEXPECTEDLY(hr
))
1481 CComPtr
<IShellFolder
> childFolder
;
1482 hr
= m_shellFolder
->BindToObject(pidl
, NULL
, IID_PPV_ARG(IShellFolder
, &childFolder
));
1483 if (FAILED_UNEXPECTEDLY(hr
))
1486 hr
= shellMenu
->SetShellFolder(childFolder
, NULL
, NULL
, 0);
1487 if (FAILED_UNEXPECTEDLY(hr
))
1490 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1493 HRESULT
CMenuSFToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1496 LPCITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1498 SFGAOF attrs
= SFGAO_FOLDER
;
1499 hr
= m_shellFolder
->GetAttributesOf(1, &pidl
, &attrs
);
1500 if (FAILED_UNEXPECTEDLY(hr
))
1503 return (attrs
& SFGAO_FOLDER
) ? S_OK
: S_FALSE
;