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
)
152 bool isHot
, isPopup
, isActive
;
155 switch (cdraw
->nmcd
.dwDrawStage
)
158 *theResult
= CDRF_NOTIFYITEMDRAW
;
161 case CDDS_ITEMPREPAINT
:
164 m_menuBand
->_GetTopLevelWindow(&tlw
);
166 // The item with an active submenu gets the CHECKED flag.
167 isHot
= m_hotBar
== this && (int) cdraw
->nmcd
.dwItemSpec
== m_hotItem
;
168 isPopup
= m_popupBar
== this && (int) cdraw
->nmcd
.dwItemSpec
== m_popupItem
;
169 isActive
= (GetForegroundWindow() == tlw
) || (m_popupBar
== this);
171 if (m_hotItem
< 0 && isPopup
)
174 if ((m_useFlatMenus
&& isHot
) || (m_initFlags
& SMINIT_VERTICAL
))
178 RECT rc
= cdraw
->nmcd
.rc
;
179 HDC hdc
= cdraw
->nmcd
.hdc
;
181 // Remove HOT and CHECKED flags (will restore HOT if necessary)
182 cdraw
->nmcd
.uItemState
&= ~(CDIS_HOT
| CDIS_CHECKED
);
184 // Decide on the colors
187 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
189 clrText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
190 bgBrush
= GetSysColorBrush(m_useFlatMenus
? COLOR_MENUHILIGHT
: COLOR_HIGHLIGHT
);
194 clrText
= GetSysColor(COLOR_MENUTEXT
);
195 bgBrush
= GetSysColorBrush(COLOR_MENU
);
198 // Paint the background color with the selected color
199 FillRect(hdc
, &rc
, bgBrush
);
201 // Set the text color in advance, this color will be assigned when the ITEMPOSTPAINT triggers
202 SetTextColor(hdc
, clrText
);
204 // Set the text color, will be used by the internal drawing code
205 cdraw
->clrText
= clrText
;
206 cdraw
->iListGap
+= 4;
208 // Tell the default drawing code we don't want any fanciness, not even a background.
209 *theResult
= CDRF_NOTIFYPOSTPAINT
| TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| 0x00800000; // FIXME: the last bit is Vista+, useful for debugging only
213 // Set the text color, will be used by the internal drawing code
214 cdraw
->clrText
= GetSysColor(isActive
? COLOR_MENUTEXT
: COLOR_GRAYTEXT
);
216 // Remove HOT and CHECKED flags (will restore HOT if necessary)
217 cdraw
->nmcd
.uItemState
&= ~CDIS_HOT
;
219 // Decide on the colors
222 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
230 case CDDS_ITEMPOSTPAINT
:
232 // Fetch the button style
233 btni
.cbSize
= sizeof(btni
);
234 btni
.dwMask
= TBIF_STYLE
;
235 GetButtonInfo(cdraw
->nmcd
.dwItemSpec
, &btni
);
237 // Check if we need to draw a submenu arrow
238 if (btni
.fsStyle
& BTNS_DROPDOWN
)
240 // TODO: Support RTL text modes by drawing a leftwards arrow aligned to the left of the control
242 // "8" is the rightwards dropdown arrow in the Marlett font
243 WCHAR text
[] = L
"8";
245 // Configure the font to draw with Marlett, keeping the current background color as-is
246 SelectObject(cdraw
->nmcd
.hdc
, m_marlett
);
247 SetBkMode(cdraw
->nmcd
.hdc
, TRANSPARENT
);
249 // Tweak the alignment by 1 pixel so the menu draws like the Windows start menu.
250 RECT rc
= cdraw
->nmcd
.rc
;
253 // The arrow is drawn at the right of the item's rect, aligned vertically.
254 DrawTextEx(cdraw
->nmcd
.hdc
, text
, 1, &rc
, DT_NOCLIP
| DT_VCENTER
| DT_RIGHT
| DT_SINGLELINE
, NULL
);
262 CMenuToolbarBase::CMenuToolbarBase(CMenuBand
*menuBand
, BOOL usePager
) :
264 m_useFlatMenus(FALSE
),
265 m_disableMouseTrack(FALSE
),
266 m_timerEnabled(FALSE
),
267 m_menuBand(menuBand
),
270 m_usePager(usePager
),
275 m_isTrackingPopup(FALSE
),
276 m_cancelingPopup(FALSE
)
282 m_marlett
= CreateFont(
283 0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET
,
284 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
285 DEFAULT_QUALITY
, FF_DONTCARE
, L
"Marlett");
288 CMenuToolbarBase::~CMenuToolbarBase()
294 m_pager
.DestroyWindow();
296 DeleteObject(m_marlett
);
299 void CMenuToolbarBase::InvalidateDraw()
301 InvalidateRect(NULL
, FALSE
);
304 HRESULT
CMenuToolbarBase::ShowDW(BOOL fShow
)
306 ShowWindow(fShow
? SW_SHOW
: SW_HIDE
);
308 // Ensure that the right image list is assigned to the toolbar
311 // For custom-drawing
312 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
317 HRESULT
CMenuToolbarBase::UpdateImageLists()
319 if ((m_initFlags
& (SMINIT_TOPLEVEL
| SMINIT_VERTICAL
)) == SMINIT_TOPLEVEL
) // not vertical.
321 // No image list, prevents the buttons from having a margin at the left side
326 // Assign the correct imagelist and padding based on the current icon size
329 if (m_menuBand
->UseBigIcons())
341 HRESULT hr
= SHGetImageList(shiml
, IID_PPV_ARG(IImageList
, &piml
));
342 if (FAILED_UNEXPECTEDLY(hr
))
348 SetImageList((HIMAGELIST
)piml
);
353 HRESULT
CMenuToolbarBase::Close()
359 m_pager
.DestroyWindow();
364 HRESULT
CMenuToolbarBase::CreateToolbar(HWND hwndParent
, DWORD dwFlags
)
366 LONG tbStyles
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
|
367 TBSTYLE_TOOLTIPS
| TBSTYLE_TRANSPARENT
| TBSTYLE_REGISTERDROP
| TBSTYLE_LIST
| TBSTYLE_FLAT
| TBSTYLE_CUSTOMERASE
|
368 CCS_NODIVIDER
| CCS_NOPARENTALIGN
| CCS_NORESIZE
| CCS_TOP
;
369 LONG tbExStyles
= TBSTYLE_EX_DOUBLEBUFFER
| WS_EX_TOOLWINDOW
;
371 if (dwFlags
& SMINIT_VERTICAL
)
373 // Activate vertical semantics
374 tbStyles
|= CCS_VERT
;
376 #if USE_TBSTYLE_EX_VERTICAL
377 tbExStyles
|= TBSTYLE_EX_VERTICAL
;
381 m_initFlags
= dwFlags
;
383 // Get a temporary rect to use while creating the toolbar window.
384 // Ensure that it is not a null rect.
386 if (!::GetClientRect(hwndParent
, &rc
) ||
387 (rc
.left
== rc
.right
) ||
388 (rc
.top
== rc
.bottom
))
396 SubclassWindow(CToolbar::Create(hwndParent
, tbStyles
, tbExStyles
));
398 SetWindowTheme(m_hWnd
, L
"", L
"");
400 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
402 m_menuBand
->AdjustForTheme(m_useFlatMenus
);
404 // If needed, create the pager.
407 LONG pgStyles
= PGS_VERT
| WS_CHILD
| WS_VISIBLE
;
410 HWND hwndPager
= CreateWindowEx(
411 pgExStyles
, WC_PAGESCROLLER
, NULL
,
412 pgStyles
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
413 hwndParent
, NULL
, _AtlBaseModule
.GetModuleInstance(), 0);
415 m_pager
.SubclassWindow(hwndPager
);
417 ::SetParent(m_hWnd
, hwndPager
);
419 m_pager
.SendMessageW(PGM_SETCHILD
, 0, reinterpret_cast<LPARAM
>(m_hWnd
));
422 // Configure the image lists
428 HRESULT
CMenuToolbarBase::GetSizes(SIZE
* pMinSize
, SIZE
* pMaxSize
, SIZE
* pIntegralSize
)
431 *pMinSize
= m_idealSize
;
433 *pMaxSize
= m_idealSize
;
435 *pIntegralSize
= m_itemSize
;
440 TRACE("Sizes out of date, recalculating.\n");
447 // Obtain the ideal size, to be used as min and max
448 GetMaxSize(&m_idealSize
);
449 GetIdealSize((m_initFlags
& SMINIT_VERTICAL
) != 0, &m_idealSize
);
451 TRACE("Ideal Size: (%d, %d) for %d buttons\n", m_idealSize
, GetButtonCount());
453 // Obtain the button size, to be used as the integral size
454 DWORD size
= GetButtonSize();
455 m_itemSize
.cx
= GET_X_LPARAM(size
);
456 m_itemSize
.cy
= GET_Y_LPARAM(size
);
460 *pMinSize
= m_idealSize
;
462 *pMaxSize
= m_idealSize
;
464 *pIntegralSize
= m_itemSize
;
469 HRESULT
CMenuToolbarBase::SetPosSize(int x
, int y
, int cx
, int cy
)
471 // Update the toolbar or pager to fit the requested rect
472 // If we have a pager, set the toolbar height to the ideal height of the toolbar
475 SetWindowPos(NULL
, x
, y
, cx
, m_idealSize
.cy
, 0);
476 m_pager
.SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
480 SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
483 // In a vertical menu, resize the buttons to fit the width
484 if (m_initFlags
& SMINIT_VERTICAL
)
486 DWORD btnSize
= GetButtonSize();
487 SetButtonSize(cx
, GET_Y_LPARAM(btnSize
));
493 HRESULT
CMenuToolbarBase::IsWindowOwner(HWND hwnd
)
495 if (m_hWnd
&& m_hWnd
== hwnd
) return S_OK
;
496 if (m_pager
.m_hWnd
&& m_pager
.m_hWnd
== hwnd
) return S_OK
;
500 HRESULT
CMenuToolbarBase::GetWindow(HWND
*phwnd
)
506 *phwnd
= m_pager
.m_hWnd
;
513 HRESULT
CMenuToolbarBase::OnGetInfoTip(NMTBGETINFOTIP
* tip
)
518 INT iItem
= tip
->iItem
;
520 GetDataFromId(iItem
, &index
, &dwData
);
522 return InternalGetTooltip(iItem
, index
, dwData
, tip
->pszText
, tip
->cchTextMax
);
525 HRESULT
CMenuToolbarBase::OnPopupTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
527 if (wParam
!= TIMERID_HOTTRACK
)
533 KillTimer(TIMERID_HOTTRACK
);
538 m_timerEnabled
= FALSE
;
543 // Returns S_FALSE if the current item did not show a submenu
544 HRESULT hr
= PopupItem(m_hotItem
, FALSE
);
548 // If we didn't switch submenus, cancel the current popup regardless
551 HRESULT hr
= CancelCurrentPopup();
552 if (FAILED_UNEXPECTEDLY(hr
))
559 HRESULT
CMenuToolbarBase::KillPopupTimer()
563 m_timerEnabled
= FALSE
;
564 KillTimer(TIMERID_HOTTRACK
);
570 HRESULT
CMenuToolbarBase::ChangeHotItem(CMenuToolbarBase
* toolbar
, INT item
, DWORD dwFlags
)
572 // Ignore the change if it already matches the stored info
573 if (m_hotBar
== toolbar
&& m_hotItem
== item
)
576 // Prevent a change of hot item if the change was triggered by the mouse,
577 // and mouse tracking is disabled.
578 if (m_disableMouseTrack
&& dwFlags
& HICF_MOUSE
)
580 TRACE("Hot item change prevented by DisableMouseTrack\n");
584 // Notify the toolbar if the hot-tracking left this toolbar
585 if (m_hotBar
== this && toolbar
!= this)
590 TRACE("Hot item changed from %p %p, to %p %p\n", m_hotBar
, m_hotItem
, toolbar
, item
);
594 if (m_hotBar
== this)
596 if (m_isTrackingPopup
&& !(m_initFlags
& SMINIT_VERTICAL
))
598 // If the menubar has an open submenu, switch to the new item's submenu immediately
599 PopupItem(m_hotItem
, FALSE
);
601 else if (dwFlags
& HICF_MOUSE
)
603 // Vertical menus show/hide the submenu after a delay,
604 // but only with the mouse.
605 if (m_initFlags
& SMINIT_VERTICAL
)
608 SystemParametersInfo(SPI_GETMENUSHOWDELAY
, 0, &elapsed
, 0);
609 SetTimer(TIMERID_HOTTRACK
, elapsed
);
610 m_timerEnabled
= TRUE
;
611 TRACE("SetTimer called with m_hotItem=%d\n", m_hotItem
);
617 info
.cbSize
= sizeof(info
);
620 int index
= GetButtonInfo(item
, &info
);
630 HRESULT
CMenuToolbarBase::ChangePopupItem(CMenuToolbarBase
* toolbar
, INT item
)
632 // Ignore the change if it already matches the stored info
633 if (m_popupBar
== toolbar
&& m_popupItem
== item
)
636 // Notify the toolbar if the popup-tracking this toolbar
637 if (m_popupBar
== this && toolbar
!= this)
639 CheckButton(m_popupItem
, FALSE
);
640 m_isTrackingPopup
= FALSE
;
643 m_popupBar
= toolbar
;
646 if (m_popupBar
== this)
648 CheckButton(m_popupItem
, TRUE
);
655 LRESULT
CMenuToolbarBase::IsTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
658 INT idx
= (INT
)wParam
;
660 if (m_hotBar
!= this)
666 if (!GetButton(idx
, &btn
))
669 if (m_hotItem
== btn
.idCommand
)
672 if (m_popupItem
== btn
.idCommand
)
678 LRESULT
CMenuToolbarBase::ChangeTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
681 BOOL wasTracking
= LOWORD(lParam
);
682 BOOL mouse
= HIWORD(lParam
);
683 INT idx
= (INT
)wParam
;
687 m_isTrackingPopup
= FALSE
;
688 return m_menuBand
->_ChangeHotItem(NULL
, -1, HICF_MOUSE
);
691 if (!GetButton(idx
, &btn
))
694 TRACE("ChangeTrackedItem %d, %d\n", idx
, wasTracking
);
695 m_isTrackingPopup
= wasTracking
;
696 return m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, mouse
? HICF_MOUSE
: 0);
699 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, IShellMenu
* childShellMenu
, BOOL keyInitiated
)
701 // Calculate the submenu position and exclude area
704 if (!GetItemRect(index
, &rc
))
707 POINT a
= { rc
.left
, rc
.top
};
708 POINT b
= { rc
.right
, rc
.bottom
};
713 POINTL pt
= { a
.x
, b
.y
};
714 RECTL rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
716 if (m_initFlags
& SMINIT_VERTICAL
)
718 // FIXME: Hardcoding this here feels hacky.
731 // Display the submenu
732 m_isTrackingPopup
= TRUE
;
734 m_menuBand
->_ChangePopupItem(this, iItem
);
735 m_menuBand
->_OnPopupSubMenu(childShellMenu
, &pt
, &rcl
, keyInitiated
);
740 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, HMENU menu
)
742 // Calculate the submenu position and exclude area
745 if (!GetItemRect(index
, &rc
))
748 POINT a
= { rc
.left
, rc
.top
};
749 POINT b
= { rc
.right
, rc
.bottom
};
754 POINT pt
= { a
.x
, b
.y
};
755 RECT rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
757 if (m_initFlags
& SMINIT_VERTICAL
)
763 HMENU popup
= GetSubMenu(menu
, index
);
765 // Display the submenu
766 m_isTrackingPopup
= TRUE
;
767 m_menuBand
->_ChangePopupItem(this, iItem
);
768 m_menuBand
->_TrackSubMenu(popup
, pt
.x
, pt
.y
, rcl
);
769 m_menuBand
->_ChangePopupItem(NULL
, -1);
770 m_isTrackingPopup
= FALSE
;
775 HRESULT
CMenuToolbarBase::TrackContextMenu(IContextMenu
* contextMenu
, POINT pt
)
778 m_menuBand
->_KillPopupTimers();
780 m_menuBand
->_CancelCurrentPopup();
782 // Display the context menu
783 return m_menuBand
->_TrackContextMenu(contextMenu
, pt
.x
, pt
.y
);
786 HRESULT
CMenuToolbarBase::BeforeCancelPopup()
788 m_cancelingPopup
= TRUE
;
789 TRACE("BeforeCancelPopup\n");
793 HRESULT
CMenuToolbarBase::ProcessClick(INT iItem
)
795 if (m_disableMouseTrack
)
797 TRACE("Item click prevented by DisableMouseTrack\n");
801 // If a button is clicked while a submenu was open, cancel the submenu.
802 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
804 TRACE("OnCommand cancelled because it was tracking submenu.\n");
808 if (PopupItem(iItem
, FALSE
) == S_OK
)
810 TRACE("PopupItem returned S_OK\n");
814 TRACE("Executing...\n");
816 return m_menuBand
->_MenuItemSelect(MPOS_EXECUTE
);
819 HRESULT
CMenuToolbarBase::ProcessContextMenu(INT iItem
)
824 GetDataFromId(iItem
, &index
, &data
);
826 DWORD pos
= GetMessagePos();
827 POINT pt
= { GET_X_LPARAM(pos
), GET_Y_LPARAM(pos
) };
829 return InternalContextMenu(iItem
, index
, data
, pt
);
832 HRESULT
CMenuToolbarBase::MenuBarMouseDown(INT iIndex
, BOOL isLButton
)
836 GetButton(iIndex
, &btn
);
838 if ((m_initFlags
& SMINIT_VERTICAL
)
842 m_cancelingPopup
= FALSE
;
846 return ProcessClick(btn
.idCommand
);
849 HRESULT
CMenuToolbarBase::MenuBarMouseUp(INT iIndex
, BOOL isLButton
)
853 m_cancelingPopup
= FALSE
;
855 if (!(m_initFlags
& SMINIT_VERTICAL
))
858 GetButton(iIndex
, &btn
);
861 return ProcessClick(btn
.idCommand
);
863 return ProcessContextMenu(btn
.idCommand
);
866 HRESULT
CMenuToolbarBase::PrepareExecuteItem(INT iItem
)
868 this->m_menuBand
->_KillPopupTimers();
870 m_executeItem
= iItem
;
871 return GetDataFromId(iItem
, &m_executeIndex
, &m_executeData
);
874 HRESULT
CMenuToolbarBase::ExecuteItem()
876 return InternalExecuteItem(m_executeItem
, m_executeItem
, m_executeData
);
879 HRESULT
CMenuToolbarBase::KeyboardItemChange(DWORD dwSelectType
)
881 int prev
= m_hotItem
;
884 if (dwSelectType
!= 0xFFFFFFFF)
886 int count
= GetButtonCount();
888 if (dwSelectType
== VK_HOME
)
891 dwSelectType
= VK_DOWN
;
893 else if (dwSelectType
== VK_END
)
896 dwSelectType
= VK_UP
;
902 TBBUTTONINFO info
= { 0 };
903 info
.cbSize
= sizeof(TBBUTTONINFO
);
905 index
= GetButtonInfo(m_hotItem
, &info
);
910 if (dwSelectType
== VK_UP
)
914 else if (dwSelectType
== VK_DOWN
)
921 if (dwSelectType
== VK_UP
)
925 else if (dwSelectType
== VK_DOWN
)
932 TBBUTTON btn
= { 0 };
933 while (index
>= 0 && index
< count
)
935 DWORD res
= GetButton(index
, &btn
);
941 if (prev
!= btn
.idCommand
)
943 TRACE("Setting Hot item to %d\n", index
);
944 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
947 m_menuBand
->_GetTopLevelWindow(&tlw
);
948 SendMessageW(tlw
, WM_CANCELMODE
, 0, 0);
949 PostMessageW(WM_USER_CHANGETRACKEDITEM
, index
, MAKELPARAM(m_isTrackingPopup
, FALSE
));
952 m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, 0);
957 if (dwSelectType
== VK_UP
)
961 else if (dwSelectType
== VK_DOWN
)
972 TRACE("Setting Hot item to null\n");
973 m_menuBand
->_ChangeHotItem(NULL
, -1, 0);
979 HRESULT
CMenuToolbarBase::AddButton(DWORD commandId
, LPCWSTR caption
, BOOL hasSubMenu
, INT iconId
, DWORD_PTR buttonData
, BOOL last
)
981 TBBUTTON tbb
= { 0 };
983 tbb
.fsState
= TBSTATE_ENABLED
;
984 #if !USE_TBSTYLE_EX_VERTICAL
985 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
986 tbb
.fsState
|= TBSTATE_WRAP
;
988 tbb
.fsStyle
= BTNS_CHECKGROUP
;
990 if (hasSubMenu
&& (m_initFlags
& SMINIT_VERTICAL
))
991 tbb
.fsStyle
|= BTNS_DROPDOWN
;
993 if (!(m_initFlags
& SMINIT_VERTICAL
))
994 tbb
.fsStyle
|= BTNS_AUTOSIZE
;
996 tbb
.iString
= (INT_PTR
) caption
;
997 tbb
.idCommand
= commandId
;
999 tbb
.iBitmap
= iconId
;
1000 tbb
.dwData
= buttonData
;
1004 if (!AddButtons(1, &tbb
))
1005 return HRESULT_FROM_WIN32(GetLastError());
1009 HRESULT
CMenuToolbarBase::AddSeparator(BOOL last
)
1011 TBBUTTON tbb
= { 0 };
1013 tbb
.fsState
= TBSTATE_ENABLED
;
1014 #if !USE_TBSTYLE_EX_VERTICAL
1015 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
1016 tbb
.fsState
|= TBSTATE_WRAP
;
1018 tbb
.fsStyle
= BTNS_SEP
;
1023 if (!AddButtons(1, &tbb
))
1024 return HRESULT_FROM_WIN32(GetLastError());
1029 HRESULT
CMenuToolbarBase::AddPlaceholder()
1031 TBBUTTON tbb
= { 0 };
1032 WCHAR MenuString
[128];
1034 LoadStringW(GetModuleHandle(L
"shell32.dll"), IDS_MENU_EMPTY
, MenuString
, _countof(MenuString
));
1038 tbb
.iString
= (INT_PTR
) MenuString
;
1043 if (!AddButtons(1, &tbb
))
1044 return HRESULT_FROM_WIN32(GetLastError());
1049 HRESULT
CMenuToolbarBase::ClearToolbar()
1051 while (DeleteButton(0))
1059 HRESULT
CMenuToolbarBase::GetDataFromId(INT iItem
, INT
* pIndex
, DWORD_PTR
* pData
)
1070 TBBUTTONINFO info
= { 0 };
1072 info
.cbSize
= sizeof(TBBUTTONINFO
);
1073 info
.dwMask
= TBIF_COMMAND
| TBIF_LPARAM
;
1075 int index
= GetButtonInfo(iItem
, &info
);
1083 *pData
= info
.lParam
;
1088 HRESULT
CMenuToolbarBase::CancelCurrentPopup()
1090 return m_menuBand
->_CancelCurrentPopup();
1093 HRESULT
CMenuToolbarBase::PopupItem(INT iItem
, BOOL keyInitiated
)
1101 if (m_popupBar
== this && m_popupItem
== iItem
)
1104 GetDataFromId(iItem
, &index
, &dwData
);
1106 HRESULT hr
= InternalHasSubMenu(iItem
, index
, dwData
);
1112 HRESULT hr
= CancelCurrentPopup();
1113 if (FAILED_UNEXPECTEDLY(hr
))
1117 if (!(m_initFlags
& SMINIT_VERTICAL
))
1119 TRACE("PopupItem non-vertical %d %d\n", index
, iItem
);
1120 m_menuBand
->_ChangeHotItem(this, iItem
, 0);
1123 return InternalPopupItem(iItem
, index
, dwData
, keyInitiated
);
1126 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand
*menuBand
) :
1127 CMenuToolbarBase(menuBand
, FALSE
),
1133 CMenuStaticToolbar::~CMenuStaticToolbar()
1137 HRESULT
CMenuStaticToolbar::GetMenu(
1138 _Out_opt_ HMENU
*phmenu
,
1139 _Out_opt_ HWND
*phwnd
,
1140 _Out_opt_ DWORD
*pdwFlags
)
1145 *phwnd
= m_hwndMenu
;
1147 *pdwFlags
= m_dwMenuFlags
;
1152 HRESULT
CMenuStaticToolbar::SetMenu(
1159 m_dwMenuFlags
= dwFlags
;
1166 HRESULT
CMenuStaticToolbar::FillToolbar(BOOL clearFirst
)
1169 int ic
= GetMenuItemCount(m_hmenu
);
1177 for (i
= 0; i
< ic
; i
++)
1179 BOOL last
= i
+ 1 == ic
;
1183 info
.cbSize
= sizeof(info
);
1184 info
.dwTypeData
= NULL
;
1185 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_ID
;
1187 if (!GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
))
1189 TRACE("Error obtaining info for menu item at pos=%d\n", i
);
1195 if (info
.fType
& MFT_SEPARATOR
)
1199 else if (!(info
.fType
& MFT_BITMAP
))
1202 info
.dwTypeData
= (PWSTR
) HeapAlloc(GetProcessHeap(), 0, (info
.cch
+ 1) * sizeof(WCHAR
));
1204 info
.fMask
= MIIM_STRING
| MIIM_SUBMENU
| MIIM_ID
;
1205 GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
);
1207 SMINFO
* sminfo
= new SMINFO();
1208 sminfo
->dwMask
= SMIM_ICON
| SMIM_FLAGS
;
1210 HRESULT hr
= m_menuBand
->_CallCBWithItemId(info
.wID
, SMC_GETINFO
, 0, reinterpret_cast<LPARAM
>(sminfo
));
1211 if (FAILED_UNEXPECTEDLY(hr
))
1217 AddButton(info
.wID
, info
.dwTypeData
, info
.hSubMenu
!= NULL
, sminfo
->iIcon
, reinterpret_cast<DWORD_PTR
>(sminfo
), last
);
1219 HeapFree(GetProcessHeap(), 0, info
.dwTypeData
);
1226 HRESULT
CMenuStaticToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1228 //SMINFO * info = reinterpret_cast<SMINFO*>(dwData);
1233 HRESULT
CMenuStaticToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1235 delete reinterpret_cast<SMINFO
*>(tb
->tbButton
.dwData
);
1239 HRESULT
CMenuStaticToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1241 CComPtr
<IContextMenu
> contextMenu
;
1242 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
,
1243 reinterpret_cast<WPARAM
>(&IID_IContextMenu
), reinterpret_cast<LPARAM
>(&contextMenu
));
1247 return TrackContextMenu(contextMenu
, pt
);
1250 HRESULT
CMenuStaticToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1252 return m_menuBand
->_CallCBWithItemId(iItem
, SMC_EXEC
, 0, 0);
1255 HRESULT
CMenuStaticToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1257 SMINFO
* nfo
= reinterpret_cast<SMINFO
*>(dwData
);
1261 if (nfo
->dwFlags
&SMIF_TRACKPOPUP
)
1263 return PopupSubMenu(iItem
, index
, m_hmenu
);
1267 CComPtr
<IShellMenu
> shellMenu
;
1268 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
, reinterpret_cast<WPARAM
>(&IID_IShellMenu
), reinterpret_cast<LPARAM
>(&shellMenu
));
1269 if (FAILED_UNEXPECTEDLY(hr
))
1272 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1276 HRESULT
CMenuStaticToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1278 return ::GetSubMenu(m_hmenu
, index
) ? S_OK
: S_FALSE
;
1281 CMenuSFToolbar::CMenuSFToolbar(CMenuBand
* menuBand
) :
1282 CMenuToolbarBase(menuBand
, TRUE
),
1283 m_shellFolder(NULL
),
1289 CMenuSFToolbar::~CMenuSFToolbar()
1293 int CALLBACK
PidlListSort(void* item1
, void* item2
, LPARAM lParam
)
1295 IShellFolder
* psf
= (IShellFolder
*) lParam
;
1296 PCUIDLIST_RELATIVE pidl1
= (PCUIDLIST_RELATIVE
) item1
;
1297 PCUIDLIST_RELATIVE pidl2
= (PCUIDLIST_RELATIVE
) item2
;
1298 HRESULT hr
= psf
->CompareIDs(0, pidl1
, pidl2
);
1301 // No way to cancel, so sort to equal.
1304 return (int)(short)LOWORD(hr
);
1307 HRESULT
CMenuSFToolbar::FillToolbar(BOOL clearFirst
)
1311 CComPtr
<IEnumIDList
> eidl
;
1312 hr
= m_shellFolder
->EnumObjects(GetToolbar(), SHCONTF_FOLDERS
| SHCONTF_NONFOLDERS
, &eidl
);
1313 if (FAILED_UNEXPECTEDLY(hr
))
1316 HDPA dpaSort
= DPA_Create(10);
1318 LPITEMIDLIST item
= NULL
;
1319 hr
= eidl
->Next(1, &item
, NULL
);
1322 if (m_menuBand
->_CallCBWithItemPidl(item
, 0x10000000, 0, 0) == S_FALSE
)
1324 DPA_AppendPtr(dpaSort
, ILClone(item
));
1327 hr
= eidl
->Next(1, &item
, NULL
);
1330 // If no items were added, show the "empty" placeholder
1331 if (DPA_GetPtrCount(dpaSort
) == 0)
1333 DPA_Destroy(dpaSort
);
1334 return AddPlaceholder();
1337 TRACE("FillToolbar added %d items to the DPA\n", DPA_GetPtrCount(dpaSort
));
1339 DPA_Sort(dpaSort
, PidlListSort
, (LPARAM
) m_shellFolder
.p
);
1341 for (int i
= 0; i
<DPA_GetPtrCount(dpaSort
);)
1348 STRRET sr
= { STRRET_CSTR
, { 0 } };
1350 item
= (LPITEMIDLIST
)DPA_GetPtr(dpaSort
, i
);
1352 hr
= m_shellFolder
->GetDisplayNameOf(item
, SIGDN_NORMALDISPLAY
, &sr
);
1353 if (FAILED_UNEXPECTEDLY(hr
))
1355 DPA_Destroy(dpaSort
);
1359 StrRetToStr(&sr
, NULL
, &MenuString
);
1361 index
= SHMapPIDLToSystemImageListIndex(m_shellFolder
, item
, &indexOpen
);
1363 LPCITEMIDLIST itemc
= item
;
1365 SFGAOF attrs
= SFGAO_FOLDER
;
1366 hr
= m_shellFolder
->GetAttributesOf(1, &itemc
, &attrs
);
1368 DWORD_PTR dwData
= reinterpret_cast<DWORD_PTR
>(item
);
1370 // Fetch next item already, so we know if the current one is the last
1373 AddButton(i
, MenuString
, attrs
& SFGAO_FOLDER
, index
, dwData
, i
>= DPA_GetPtrCount(dpaSort
));
1375 CoTaskMemFree(MenuString
);
1378 DPA_Destroy(dpaSort
);
1382 HRESULT
CMenuSFToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1384 //ITEMIDLIST * pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1389 HRESULT
CMenuSFToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1391 ILFree(reinterpret_cast<LPITEMIDLIST
>(tb
->tbButton
.dwData
));
1395 HRESULT
CMenuSFToolbar::SetShellFolder(IShellFolder
*psf
, LPCITEMIDLIST pidlFolder
, HKEY hKey
, DWORD dwFlags
)
1397 m_shellFolder
= psf
;
1398 m_idList
= ILClone(pidlFolder
);
1400 m_dwMenuFlags
= dwFlags
;
1407 HRESULT
CMenuSFToolbar::GetShellFolder(DWORD
*pdwFlags
, LPITEMIDLIST
*ppidl
, REFIID riid
, void **ppv
)
1411 hr
= m_shellFolder
->QueryInterface(riid
, ppv
);
1412 if (FAILED_UNEXPECTEDLY(hr
))
1416 *pdwFlags
= m_dwMenuFlags
;
1420 LPITEMIDLIST pidl
= NULL
;
1424 pidl
= ILClone(m_idList
);
1427 ERR("ILClone failed!\n");
1428 (*reinterpret_cast<IUnknown
**>(ppv
))->Release();
1439 HRESULT
CMenuSFToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1442 CComPtr
<IContextMenu
> contextMenu
= NULL
;
1443 LPCITEMIDLIST pidl
= reinterpret_cast<LPCITEMIDLIST
>(dwData
);
1445 hr
= m_shellFolder
->GetUIObjectOf(GetToolbar(), 1, &pidl
, IID_NULL_PPV_ARG(IContextMenu
, &contextMenu
));
1446 if (FAILED_UNEXPECTEDLY(hr
))
1451 hr
= TrackContextMenu(contextMenu
, pt
);
1456 HRESULT
CMenuSFToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1458 return m_menuBand
->_CallCBWithItemPidl(reinterpret_cast<LPITEMIDLIST
>(data
), SMC_SFEXEC
, 0, 0);
1461 HRESULT
CMenuSFToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1467 CComPtr
<IShellMenuCallback
> psmc
;
1468 CComPtr
<IShellMenu
> shellMenu
;
1470 LPITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1475 hr
= CMenuBand_CreateInstance(IID_PPV_ARG(IShellMenu
, &shellMenu
));
1476 if (FAILED_UNEXPECTEDLY(hr
))
1479 m_menuBand
->GetMenuInfo(&psmc
, &uId
, &uIdAncestor
, &flags
);
1481 // FIXME: not sure what to use as uId/uIdAncestor here
1482 hr
= shellMenu
->Initialize(psmc
, 0, uId
, SMINIT_VERTICAL
);
1483 if (FAILED_UNEXPECTEDLY(hr
))
1486 CComPtr
<IShellFolder
> childFolder
;
1487 hr
= m_shellFolder
->BindToObject(pidl
, NULL
, IID_PPV_ARG(IShellFolder
, &childFolder
));
1488 if (FAILED_UNEXPECTEDLY(hr
))
1491 hr
= shellMenu
->SetShellFolder(childFolder
, NULL
, NULL
, 0);
1492 if (FAILED_UNEXPECTEDLY(hr
))
1495 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1498 HRESULT
CMenuSFToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1501 LPCITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1503 SFGAOF attrs
= SFGAO_FOLDER
;
1504 hr
= m_shellFolder
->GetAttributesOf(1, &pidl
, &attrs
);
1505 if (FAILED_UNEXPECTEDLY(hr
))
1508 return (attrs
& SFGAO_FOLDER
) ? S_OK
: S_FALSE
;