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
21 #include <commoncontrols.h>
22 #include <shlwapi_undoc.h>
25 #include "CMenuBand.h"
26 #include "CMenuToolbars.h"
28 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
29 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
31 WINE_DEFAULT_DEBUG_CHANNEL(CMenuToolbars
);
34 HRESULT WINAPI
SHGetImageList(
40 // FIXME: Enable if/when wine comctl supports this flag properly
41 #define USE_TBSTYLE_EX_VERTICAL 0
43 // User-defined timer ID used while hot-tracking around the menu
44 #define TIMERID_HOTTRACK 1
46 LRESULT
CMenuToolbarBase::OnWinEventWrap(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
49 bHandled
= OnWinEvent(m_hWnd
, uMsg
, wParam
, lParam
, &lr
) != S_FALSE
;
53 HRESULT
CMenuToolbarBase::OnWinEvent(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*theResult
)
61 //return OnCommand(wParam, lParam, theResult);
65 hdr
= reinterpret_cast<LPNMHDR
>(lParam
);
68 case TBN_DELETINGBUTTON
:
69 return OnDeletingButton(reinterpret_cast<LPNMTOOLBAR
>(hdr
));
72 return OnPagerCalcSize(reinterpret_cast<LPNMPGCALCSIZE
>(hdr
));
75 return ProcessClick(reinterpret_cast<LPNMTOOLBAR
>(hdr
)->iItem
);
77 case TBN_HOTITEMCHANGE
:
78 //return OnHotItemChange(reinterpret_cast<LPNMTBHOTITEM>(hdr), theResult);
82 return OnContextMenu(reinterpret_cast<LPNMMOUSE
>(hdr
));
85 return OnCustomDraw(reinterpret_cast<LPNMTBCUSTOMDRAW
>(hdr
), theResult
);
88 return OnGetInfoTip(reinterpret_cast<LPNMTBGETINFOTIP
>(hdr
));
90 // Silence unhandled items so that they don't print as unknown
97 case NM_RELEASEDCAPTURE
:
105 case TBN_GETDISPINFO
:
112 case NM_TOOLTIPSCREATED
:
116 case -714: return S_FALSE
;
119 TRACE("WM_NOTIFY unknown code %d, %d\n", hdr
->code
, hdr
->idFrom
);
128 HRESULT
CMenuToolbarBase::DisableMouseTrack(BOOL bDisable
)
130 if (m_disableMouseTrack
!= bDisable
)
132 m_disableMouseTrack
= bDisable
;
133 TRACE("DisableMouseTrack %d\n", bDisable
);
138 HRESULT
CMenuToolbarBase::OnPagerCalcSize(LPNMPGCALCSIZE csize
)
141 GetSizes(NULL
, &tbs
, NULL
);
142 if (csize
->dwFlag
== PGF_CALCHEIGHT
)
144 csize
->iHeight
= tbs
.cy
;
146 else if (csize
->dwFlag
== PGF_CALCWIDTH
)
148 csize
->iWidth
= tbs
.cx
;
153 HRESULT
CMenuToolbarBase::OnCustomDraw(LPNMTBCUSTOMDRAW cdraw
, LRESULT
* theResult
)
158 switch (cdraw
->nmcd
.dwDrawStage
)
161 *theResult
= CDRF_NOTIFYITEMDRAW
;
164 case CDDS_ITEMPREPAINT
:
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
;
170 if (m_initFlags
& SMINIT_VERTICAL
|| IsAppThemed())
174 RECT rc
= cdraw
->nmcd
.rc
;
175 HDC hdc
= cdraw
->nmcd
.hdc
;
177 // Remove HOT and CHECKED flags (will restore HOT if necessary)
178 cdraw
->nmcd
.uItemState
&= ~(CDIS_HOT
| CDIS_CHECKED
);
180 // Decide on the colors
181 if (isHot
|| (m_hotItem
< 0 && isPopup
))
183 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
185 clrText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
186 bgBrush
= GetSysColorBrush(m_useFlatMenus
? COLOR_MENUHILIGHT
: COLOR_HIGHLIGHT
);
190 clrText
= GetSysColor(COLOR_MENUTEXT
);
191 bgBrush
= GetSysColorBrush(COLOR_MENU
);
194 // Paint the background color with the selected color
195 FillRect(hdc
, &rc
, bgBrush
);
197 // Set the text color in advance, this color will be assigned when the ITEMPOSTPAINT triggers
198 SetTextColor(hdc
, clrText
);
200 // Set the text color, will be used by the internal drawing code
201 cdraw
->clrText
= clrText
;
202 cdraw
->iListGap
+= 4;
204 // Tell the default drawing code we don't want any fanciness, not even a background.
205 *theResult
= CDRF_NOTIFYPOSTPAINT
| TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| 0x00800000; // FIXME: the last bit is Vista+, useful for debugging only
209 // Remove HOT and CHECKED flags (will restore HOT if necessary)
210 cdraw
->nmcd
.uItemState
&= ~CDIS_HOT
;
212 // Decide on the colors
213 if (isHot
|| (m_hotItem
< 0 && isPopup
))
215 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
223 case CDDS_ITEMPOSTPAINT
:
225 // Fetch the button style
226 btni
.cbSize
= sizeof(btni
);
227 btni
.dwMask
= TBIF_STYLE
;
228 GetButtonInfo(cdraw
->nmcd
.dwItemSpec
, &btni
);
230 // Check if we need to draw a submenu arrow
231 if (btni
.fsStyle
& BTNS_DROPDOWN
)
233 // TODO: Support RTL text modes by drawing a leftwards arrow aligned to the left of the control
235 // "8" is the rightwards dropdown arrow in the Marlett font
236 WCHAR text
[] = L
"8";
238 // Configure the font to draw with Marlett, keeping the current background color as-is
239 SelectObject(cdraw
->nmcd
.hdc
, m_marlett
);
240 SetBkMode(cdraw
->nmcd
.hdc
, TRANSPARENT
);
242 // Tweak the alignment by 1 pixel so the menu draws like the Windows start menu.
243 RECT rc
= cdraw
->nmcd
.rc
;
246 // The arrow is drawn at the right of the item's rect, aligned vertically.
247 DrawTextEx(cdraw
->nmcd
.hdc
, text
, 1, &rc
, DT_NOCLIP
| DT_VCENTER
| DT_RIGHT
| DT_SINGLELINE
, NULL
);
255 CMenuToolbarBase::CMenuToolbarBase(CMenuBand
*menuBand
, BOOL usePager
) :
257 m_useFlatMenus(FALSE
),
259 m_disableMouseTrack(FALSE
),
260 m_timerEnabled(FALSE
),
261 m_menuBand(menuBand
),
264 m_usePager(usePager
),
269 m_isTrackingPopup(FALSE
),
270 m_cancelingPopup(FALSE
)
276 m_marlett
= CreateFont(
277 0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET
,
278 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
279 DEFAULT_QUALITY
, FF_DONTCARE
, L
"Marlett");
282 CMenuToolbarBase::~CMenuToolbarBase()
288 m_pager
.DestroyWindow();
290 DeleteObject(m_marlett
);
293 void CMenuToolbarBase::InvalidateDraw()
295 InvalidateRect(NULL
, FALSE
);
298 HRESULT
CMenuToolbarBase::ShowDW(BOOL fShow
)
300 ShowWindow(fShow
? SW_SHOW
: SW_HIDE
);
302 // Ensure that the right image list is assigned to the toolbar
305 // For custom-drawing
306 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
311 HRESULT
CMenuToolbarBase::UpdateImageLists()
313 if ((m_initFlags
& (SMINIT_TOPLEVEL
| SMINIT_VERTICAL
)) == SMINIT_TOPLEVEL
) // not vertical.
315 // No image list, prevents the buttons from having a margin at the left side
320 // Assign the correct imagelist and padding based on the current icon size
323 if (m_menuBand
->UseBigIcons())
335 HRESULT hr
= SHGetImageList(shiml
, IID_PPV_ARG(IImageList
, &piml
));
336 if (FAILED_UNEXPECTEDLY(hr
))
342 SetImageList((HIMAGELIST
)piml
);
347 HRESULT
CMenuToolbarBase::Close()
353 m_pager
.DestroyWindow();
358 HRESULT
CMenuToolbarBase::CreateToolbar(HWND hwndParent
, DWORD dwFlags
)
360 LONG tbStyles
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
|
361 TBSTYLE_TOOLTIPS
| TBSTYLE_TRANSPARENT
| TBSTYLE_REGISTERDROP
| TBSTYLE_LIST
| TBSTYLE_FLAT
| TBSTYLE_CUSTOMERASE
|
362 CCS_NODIVIDER
| CCS_NOPARENTALIGN
| CCS_NORESIZE
| CCS_TOP
;
363 LONG tbExStyles
= TBSTYLE_EX_DOUBLEBUFFER
| WS_EX_TOOLWINDOW
;
365 if (dwFlags
& SMINIT_VERTICAL
)
367 // Activate vertical semantics
368 tbStyles
|= CCS_VERT
;
370 #if USE_TBSTYLE_EX_VERTICAL
371 tbExStyles
|= TBSTYLE_EX_VERTICAL
;
375 m_initFlags
= dwFlags
;
377 // Get a temporary rect to use while creating the toolbar window.
378 // Ensure that it is not a null rect.
380 if (!::GetClientRect(hwndParent
, &rc
) ||
381 (rc
.left
== rc
.right
) ||
382 (rc
.top
== rc
.bottom
))
390 SubclassWindow(Create(hwndParent
, tbStyles
, tbExStyles
));
392 // If needed, create the pager.
395 LONG pgStyles
= PGS_VERT
| WS_CHILD
| WS_VISIBLE
;
398 HWND hwndPager
= CreateWindowEx(
399 pgExStyles
, WC_PAGESCROLLER
, NULL
,
400 pgStyles
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
401 hwndParent
, NULL
, _AtlBaseModule
.GetModuleInstance(), 0);
403 m_pager
.SubclassWindow(hwndPager
);
405 ::SetParent(m_hWnd
, hwndPager
);
407 m_pager
.SendMessageW(PGM_SETCHILD
, 0, reinterpret_cast<LPARAM
>(m_hWnd
));
410 // Configure the image lists
416 HRESULT
CMenuToolbarBase::GetSizes(SIZE
* pMinSize
, SIZE
* pMaxSize
, SIZE
* pIntegralSize
)
419 *pMinSize
= m_idealSize
;
421 *pMaxSize
= m_idealSize
;
423 *pIntegralSize
= m_itemSize
;
431 // Obtain the ideal size, to be used as min and max
433 GetMaxSize(&m_idealSize
);
434 GetIdealSize((m_initFlags
& SMINIT_VERTICAL
) != 0, &m_idealSize
);
436 // Obtain the button size, to be used as the integral size
437 DWORD size
= GetButtonSize();
438 m_itemSize
.cx
= GET_X_LPARAM(size
);
439 m_itemSize
.cy
= GET_Y_LPARAM(size
);
443 *pMinSize
= m_idealSize
;
445 *pMaxSize
= m_idealSize
;
447 *pIntegralSize
= m_itemSize
;
452 HRESULT
CMenuToolbarBase::SetPosSize(int x
, int y
, int cx
, int cy
)
454 // Update the toolbar or pager to fit the requested rect
455 // If we have a pager, set the toolbar height to the ideal height of the toolbar
458 SetWindowPos(NULL
, x
, y
, cx
, m_idealSize
.cy
, 0);
459 m_pager
.SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
463 SetWindowPos(NULL
, x
, y
, cx
, cy
, 0);
466 // In a vertical menu, resize the buttons to fit the width
467 if (m_initFlags
& SMINIT_VERTICAL
)
469 DWORD btnSize
= GetButtonSize();
470 SetButtonSize(cx
, GET_Y_LPARAM(btnSize
));
476 HRESULT
CMenuToolbarBase::IsWindowOwner(HWND hwnd
)
478 if (m_hWnd
&& m_hWnd
== hwnd
) return S_OK
;
479 if (m_pager
.m_hWnd
&& m_pager
.m_hWnd
== hwnd
) return S_OK
;
483 HRESULT
CMenuToolbarBase::GetWindow(HWND
*phwnd
)
489 *phwnd
= m_pager
.m_hWnd
;
496 HRESULT
CMenuToolbarBase::OnGetInfoTip(NMTBGETINFOTIP
* tip
)
501 INT iItem
= tip
->iItem
;
503 GetDataFromId(iItem
, &index
, &dwData
);
505 return InternalGetTooltip(iItem
, index
, dwData
, tip
->pszText
, tip
->cchTextMax
);
508 HRESULT
CMenuToolbarBase::OnPopupTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
510 if (wParam
!= TIMERID_HOTTRACK
)
516 KillTimer(TIMERID_HOTTRACK
);
521 m_timerEnabled
= FALSE
;
526 // Returns S_FALSE if the current item did not show a submenu
527 HRESULT hr
= PopupItem(m_hotItem
, FALSE
);
531 // If we didn't switch submenus, cancel the current popup regardless
534 HRESULT hr
= CancelCurrentPopup();
535 if (FAILED_UNEXPECTEDLY(hr
))
542 HRESULT
CMenuToolbarBase::KillPopupTimer()
546 m_timerEnabled
= FALSE
;
547 KillTimer(TIMERID_HOTTRACK
);
553 HRESULT
CMenuToolbarBase::ChangeHotItem(CMenuToolbarBase
* toolbar
, INT item
, DWORD dwFlags
)
555 // Ignore the change if it already matches the stored info
556 if (m_hotBar
== toolbar
&& m_hotItem
== item
)
559 // Prevent a change of hot item if the change was triggered by the mouse,
560 // and mouse tracking is disabled.
561 if (m_disableMouseTrack
&& dwFlags
& HICF_MOUSE
)
563 TRACE("Hot item change prevented by DisableMouseTrack\n");
567 // Notify the toolbar if the hot-tracking left this toolbar
568 if (m_hotBar
== this && toolbar
!= this)
573 TRACE("Hot item changed from %p %p, to %p %p\n", m_hotBar
, m_hotItem
, toolbar
, item
);
577 if (m_hotBar
== this)
579 if (m_isTrackingPopup
&& !(m_initFlags
& SMINIT_VERTICAL
))
581 // If the menubar has an open submenu, switch to the new item's submenu immediately
582 PopupItem(m_hotItem
, FALSE
);
584 else if (dwFlags
& HICF_MOUSE
)
586 // Vertical menus show/hide the submenu after a delay,
587 // but only with the mouse.
588 if (m_initFlags
& SMINIT_VERTICAL
)
591 SystemParametersInfo(SPI_GETMENUSHOWDELAY
, 0, &elapsed
, 0);
592 SetTimer(TIMERID_HOTTRACK
, elapsed
);
593 m_timerEnabled
= TRUE
;
594 TRACE("SetTimer called with m_hotItem=%d\n", m_hotItem
);
600 info
.cbSize
= sizeof(info
);
603 int index
= GetButtonInfo(item
, &info
);
613 HRESULT
CMenuToolbarBase::ChangePopupItem(CMenuToolbarBase
* toolbar
, INT item
)
615 // Ignore the change if it already matches the stored info
616 if (m_popupBar
== toolbar
&& m_popupItem
== item
)
619 // Notify the toolbar if the popup-tracking this toolbar
620 if (m_popupBar
== this && toolbar
!= this)
622 CheckButton(m_popupItem
, FALSE
);
623 m_isTrackingPopup
= FALSE
;
626 m_popupBar
= toolbar
;
629 if (m_popupBar
== this)
631 CheckButton(m_popupItem
, TRUE
);
638 LRESULT
CMenuToolbarBase::IsTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
642 if (m_hotBar
!= this)
648 if (!GetButton(wParam
, &btn
))
651 if (m_hotItem
== btn
.idCommand
)
654 if (m_popupItem
== btn
.idCommand
)
660 LRESULT
CMenuToolbarBase::ChangeTrackedItem(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
662 BOOL wasTracking
= LOWORD(lParam
);
663 BOOL mouse
= HIWORD(lParam
);
669 m_isTrackingPopup
= FALSE
;
670 return m_menuBand
->_ChangeHotItem(NULL
, -1, HICF_MOUSE
);
673 if (!GetButton(wParam
, &btn
))
676 TRACE("ChangeTrackedItem %d, %d\n", wParam
, wasTracking
);
677 m_isTrackingPopup
= wasTracking
;
678 return m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, mouse
? HICF_MOUSE
: 0);
681 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, IShellMenu
* childShellMenu
, BOOL keyInitiated
)
683 // Calculate the submenu position and exclude area
687 if (!GetItemRect(index
, &rc
))
692 GetWindowRect(topWnd
, &rcx
);
694 POINT a
= { rc
.left
, rc
.top
};
695 POINT b
= { rc
.right
, rc
.bottom
};
696 POINT c
= { rcx
.left
, rcx
.top
};
697 POINT d
= { rcx
.right
, rcx
.bottom
};
699 ClientToScreen(m_hWnd
, &a
);
700 ClientToScreen(m_hWnd
, &b
);
701 ClientToScreen(topWnd
, &c
);
702 ClientToScreen(topWnd
, &d
);
704 POINTL pt
= { a
.x
, b
.y
};
705 RECTL rcl
= { c
.x
, c
.y
, d
.x
, d
.y
};
707 if (m_initFlags
& SMINIT_VERTICAL
)
713 // Display the submenu
714 m_isTrackingPopup
= TRUE
;
716 m_menuBand
->_ChangePopupItem(this, iItem
);
717 m_menuBand
->_OnPopupSubMenu(childShellMenu
, &pt
, &rcl
, keyInitiated
);
722 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, HMENU menu
)
724 // Calculate the submenu position and exclude area
727 if (!GetItemRect(index
, &rc
))
730 POINT a
= { rc
.left
, rc
.top
};
731 POINT b
= { rc
.right
, rc
.bottom
};
733 ClientToScreen(m_hWnd
, &a
);
734 ClientToScreen(m_hWnd
, &b
);
736 POINT pt
= { a
.x
, b
.y
};
737 RECT rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
739 if (m_initFlags
& SMINIT_VERTICAL
)
745 HMENU popup
= GetSubMenu(menu
, index
);
747 // Display the submenu
748 m_isTrackingPopup
= TRUE
;
749 m_menuBand
->_ChangePopupItem(this, iItem
);
750 m_menuBand
->_TrackSubMenu(popup
, pt
.x
, pt
.y
, rcl
);
751 m_menuBand
->_ChangePopupItem(NULL
, -1);
752 m_isTrackingPopup
= FALSE
;
757 HRESULT
CMenuToolbarBase::TrackContextMenu(IContextMenu
* contextMenu
, POINT pt
)
760 m_menuBand
->_KillPopupTimers();
762 m_menuBand
->_CancelCurrentPopup();
764 // Display the context menu
765 return m_menuBand
->_TrackContextMenu(contextMenu
, pt
.x
, pt
.y
);
768 HRESULT
CMenuToolbarBase::BeforeCancelPopup()
770 m_cancelingPopup
= TRUE
;
771 DbgPrint("BeforeCancelPopup\n");
775 HRESULT
CMenuToolbarBase::ProcessClick(INT iItem
)
777 if (m_disableMouseTrack
)
779 TRACE("Item click prevented by DisableMouseTrack\n");
783 // If a button is clicked while a submenu was open, cancel the submenu.
784 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
786 TRACE("OnCommand cancelled because it was tracking submenu.\n");
790 if (PopupItem(iItem
, FALSE
) == S_OK
)
792 TRACE("PopupItem returned S_OK\n");
796 TRACE("Executing...\n");
798 return m_menuBand
->_MenuItemHotTrack(MPOS_EXECUTE
);
801 HRESULT
CMenuToolbarBase::MenuBarMouseDown(INT iIndex
)
805 if ((m_initFlags
& SMINIT_VERTICAL
)
809 m_cancelingPopup
= FALSE
;
813 GetButton(iIndex
, &btn
);
814 return ProcessClick(btn
.idCommand
);
817 HRESULT
CMenuToolbarBase::MenuBarMouseUp(INT iIndex
)
821 m_cancelingPopup
= FALSE
;
823 if (!(m_initFlags
& SMINIT_VERTICAL
))
826 GetButton(iIndex
, &btn
);
827 return ProcessClick(btn
.idCommand
);
830 HRESULT
CMenuToolbarBase::PrepareExecuteItem(INT iItem
)
832 this->m_menuBand
->_KillPopupTimers();
834 m_executeItem
= iItem
;
835 return GetDataFromId(iItem
, &m_executeIndex
, &m_executeData
);
838 HRESULT
CMenuToolbarBase::ExecuteItem()
840 return InternalExecuteItem(m_executeItem
, m_executeItem
, m_executeData
);
843 HRESULT
CMenuToolbarBase::OnContextMenu(NMMOUSE
* rclick
)
845 INT iItem
= rclick
->dwItemSpec
;
846 INT index
= rclick
->dwHitInfo
;
847 DWORD_PTR data
= rclick
->dwItemData
;
849 GetDataFromId(iItem
, &index
, &data
);
851 return InternalContextMenu(iItem
, index
, data
, rclick
->pt
);
854 HRESULT
CMenuToolbarBase::KeyboardItemChange(DWORD dwSelectType
)
856 int prev
= m_hotItem
;
859 if (dwSelectType
!= 0xFFFFFFFF)
861 int count
= GetButtonCount();
863 if (dwSelectType
== VK_HOME
)
866 dwSelectType
= VK_DOWN
;
868 else if (dwSelectType
== VK_END
)
871 dwSelectType
= VK_UP
;
877 TBBUTTONINFO info
= { 0 };
878 info
.cbSize
= sizeof(TBBUTTONINFO
);
880 index
= GetButtonInfo(m_hotItem
, &info
);
885 if (dwSelectType
== VK_UP
)
889 else if (dwSelectType
== VK_DOWN
)
896 if (dwSelectType
== VK_UP
)
900 else if (dwSelectType
== VK_DOWN
)
907 TBBUTTON btn
= { 0 };
908 while (index
>= 0 && index
< count
)
910 DWORD res
= GetButton(index
, &btn
);
916 if (prev
!= btn
.idCommand
)
918 TRACE("Setting Hot item to %d\n", index
);
919 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
922 m_menuBand
->_GetTopLevelWindow(&tlw
);
923 SendMessageW(tlw
, WM_CANCELMODE
, 0, 0);
924 PostMessageW(m_hWnd
, WM_USER_CHANGETRACKEDITEM
, index
, MAKELPARAM(m_isTrackingPopup
, FALSE
));
927 m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, 0);
932 if (dwSelectType
== VK_UP
)
936 else if (dwSelectType
== VK_DOWN
)
947 TRACE("Setting Hot item to null\n");
948 m_menuBand
->_ChangeHotItem(NULL
, -1, 0);
954 HRESULT
CMenuToolbarBase::AddButton(DWORD commandId
, LPCWSTR caption
, BOOL hasSubMenu
, INT iconId
, DWORD_PTR buttonData
, BOOL last
)
956 TBBUTTON tbb
= { 0 };
958 tbb
.fsState
= TBSTATE_ENABLED
;
959 #if !USE_TBSTYLE_EX_VERTICAL
960 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
961 tbb
.fsState
|= TBSTATE_WRAP
;
963 tbb
.fsStyle
= BTNS_CHECKGROUP
;
965 if (hasSubMenu
&& (m_initFlags
& SMINIT_VERTICAL
))
966 tbb
.fsStyle
|= BTNS_DROPDOWN
;
968 if (!(m_initFlags
& SMINIT_VERTICAL
))
969 tbb
.fsStyle
|= BTNS_AUTOSIZE
;
971 tbb
.iString
= (INT_PTR
) caption
;
972 tbb
.idCommand
= commandId
;
974 tbb
.iBitmap
= iconId
;
975 tbb
.dwData
= buttonData
;
979 if (!AddButtons(1, &tbb
))
980 return HRESULT_FROM_WIN32(GetLastError());
984 HRESULT
CMenuToolbarBase::AddSeparator(BOOL last
)
986 TBBUTTON tbb
= { 0 };
988 tbb
.fsState
= TBSTATE_ENABLED
;
989 #if !USE_TBSTYLE_EX_VERTICAL
990 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
991 tbb
.fsState
|= TBSTATE_WRAP
;
993 tbb
.fsStyle
= BTNS_SEP
;
998 if (!AddButtons(1, &tbb
))
999 return HRESULT_FROM_WIN32(GetLastError());
1004 HRESULT
CMenuToolbarBase::AddPlaceholder()
1006 TBBUTTON tbb
= { 0 };
1007 PCWSTR MenuString
= L
"(Empty)"; // FIXME: Make localizable
1011 tbb
.iString
= (INT_PTR
) MenuString
;
1016 if (!AddButtons(1, &tbb
))
1017 return HRESULT_FROM_WIN32(GetLastError());
1022 HRESULT
CMenuToolbarBase::ClearToolbar()
1024 while (DeleteButton(0))
1032 HRESULT
CMenuToolbarBase::GetDataFromId(INT iItem
, INT
* pIndex
, DWORD_PTR
* pData
)
1043 TBBUTTONINFO info
= { 0 };
1045 info
.cbSize
= sizeof(TBBUTTONINFO
);
1046 info
.dwMask
= TBIF_COMMAND
| TBIF_LPARAM
;
1048 int index
= GetButtonInfo(iItem
, &info
);
1056 *pData
= info
.lParam
;
1061 HRESULT
CMenuToolbarBase::CancelCurrentPopup()
1063 return m_menuBand
->_CancelCurrentPopup();
1066 HRESULT
CMenuToolbarBase::PopupItem(INT iItem
, BOOL keyInitiated
)
1074 if (m_popupBar
== this && m_popupItem
== iItem
)
1077 GetDataFromId(iItem
, &index
, &dwData
);
1079 HRESULT hr
= InternalHasSubMenu(iItem
, index
, dwData
);
1085 HRESULT hr
= CancelCurrentPopup();
1086 if (FAILED_UNEXPECTEDLY(hr
))
1090 if (!(m_initFlags
& SMINIT_VERTICAL
))
1092 TRACE("PopupItem non-vertical %d %d\n", index
, iItem
);
1093 m_menuBand
->_ChangeHotItem(this, iItem
, 0);
1096 return InternalPopupItem(iItem
, index
, dwData
, keyInitiated
);
1099 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand
*menuBand
) :
1100 CMenuToolbarBase(menuBand
, FALSE
),
1106 CMenuStaticToolbar::~CMenuStaticToolbar()
1110 HRESULT
CMenuStaticToolbar::GetMenu(
1111 _Out_opt_ HMENU
*phmenu
,
1112 _Out_opt_ HWND
*phwnd
,
1113 _Out_opt_ DWORD
*pdwFlags
)
1118 *phwnd
= m_hwndMenu
;
1120 *pdwFlags
= m_dwMenuFlags
;
1125 HRESULT
CMenuStaticToolbar::SetMenu(
1132 m_dwMenuFlags
= dwFlags
;
1137 HRESULT
CMenuStaticToolbar::FillToolbar(BOOL clearFirst
)
1140 int ic
= GetMenuItemCount(m_hmenu
);
1148 for (i
= 0; i
< ic
; i
++)
1150 BOOL last
= i
+ 1 == ic
;
1154 info
.cbSize
= sizeof(info
);
1155 info
.dwTypeData
= NULL
;
1156 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_ID
;
1158 if (!GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
))
1160 TRACE("Error obtaining info for menu item at pos=%d\n", i
);
1166 if (info
.fType
& MFT_SEPARATOR
)
1170 else if (!(info
.fType
& MFT_BITMAP
))
1173 info
.dwTypeData
= (PWSTR
) HeapAlloc(GetProcessHeap(), 0, (info
.cch
+ 1) * sizeof(WCHAR
));
1175 info
.fMask
= MIIM_STRING
| MIIM_SUBMENU
| MIIM_ID
;
1176 GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
);
1178 SMINFO
* sminfo
= new SMINFO();
1179 sminfo
->dwMask
= SMIM_ICON
| SMIM_FLAGS
;
1180 // FIXME: remove before deleting the toolbar or it will leak
1182 HRESULT hr
= m_menuBand
->_CallCBWithItemId(info
.wID
, SMC_GETINFO
, 0, reinterpret_cast<LPARAM
>(sminfo
));
1183 if (FAILED_UNEXPECTEDLY(hr
))
1189 AddButton(info
.wID
, info
.dwTypeData
, info
.hSubMenu
!= NULL
, sminfo
->iIcon
, reinterpret_cast<DWORD_PTR
>(sminfo
), last
);
1191 HeapFree(GetProcessHeap(), 0, info
.dwTypeData
);
1198 HRESULT
CMenuStaticToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1200 //SMINFO * info = reinterpret_cast<SMINFO*>(dwData);
1205 HRESULT
CMenuStaticToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1207 delete reinterpret_cast<SMINFO
*>(tb
->tbButton
.dwData
);
1211 HRESULT
CMenuStaticToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1213 CComPtr
<IContextMenu
> contextMenu
;
1214 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
,
1215 reinterpret_cast<WPARAM
>(&IID_IContextMenu
), reinterpret_cast<LPARAM
>(&contextMenu
));
1219 return TrackContextMenu(contextMenu
, pt
);
1222 HRESULT
CMenuStaticToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1224 return m_menuBand
->_CallCBWithItemId(iItem
, SMC_EXEC
, 0, 0);
1227 HRESULT
CMenuStaticToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1229 SMINFO
* nfo
= reinterpret_cast<SMINFO
*>(dwData
);
1233 if (nfo
->dwFlags
&SMIF_TRACKPOPUP
)
1235 return PopupSubMenu(iItem
, index
, m_hmenu
);
1239 CComPtr
<IShellMenu
> shellMenu
;
1240 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
, reinterpret_cast<WPARAM
>(&IID_IShellMenu
), reinterpret_cast<LPARAM
>(&shellMenu
));
1241 if (FAILED_UNEXPECTEDLY(hr
))
1244 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1248 HRESULT
CMenuStaticToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1250 return ::GetSubMenu(m_hmenu
, index
) ? S_OK
: S_FALSE
;
1253 CMenuSFToolbar::CMenuSFToolbar(CMenuBand
* menuBand
) :
1254 CMenuToolbarBase(menuBand
, TRUE
),
1255 m_shellFolder(NULL
),
1261 CMenuSFToolbar::~CMenuSFToolbar()
1265 HRESULT
CMenuSFToolbar::FillToolbar(BOOL clearFirst
)
1272 m_shellFolder
->EnumObjects(GetToolbar(), SHCONTF_FOLDERS
| SHCONTF_NONFOLDERS
, &eidl
);
1274 LPITEMIDLIST item
= NULL
;
1275 hr
= eidl
->Next(1, &item
, NULL
);
1281 if (m_menuBand
->_CallCBWithItemPidl(item
, 0x10000000, 0, 0) == S_OK
)
1283 STRRET sr
= { STRRET_CSTR
, { 0 } };
1285 hr
= m_shellFolder
->GetDisplayNameOf(item
, SIGDN_NORMALDISPLAY
, &sr
);
1286 if (FAILED_UNEXPECTEDLY(hr
))
1289 StrRetToStr(&sr
, NULL
, &MenuString
);
1291 index
= SHMapPIDLToSystemImageListIndex(m_shellFolder
, item
, &indexOpen
);
1293 LPCITEMIDLIST itemc
= item
;
1295 SFGAOF attrs
= SFGAO_FOLDER
;
1296 hr
= m_shellFolder
->GetAttributesOf(1, &itemc
, &attrs
);
1298 DWORD_PTR dwData
= reinterpret_cast<DWORD_PTR
>(ILClone(item
));
1300 // Fetch next item already, so we know if the current one is the last
1301 hr
= eidl
->Next(1, &item
, NULL
);
1303 AddButton(++i
, MenuString
, attrs
& SFGAO_FOLDER
, index
, dwData
, hr
!= S_OK
);
1305 CoTaskMemFree(MenuString
);
1309 // Fetch next item here also
1310 hr
= eidl
->Next(1, &item
, NULL
);
1315 // If no items were added, show the "empty" placeholder
1318 return AddPlaceholder();
1324 HRESULT
CMenuSFToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1326 //ITEMIDLIST * pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1331 HRESULT
CMenuSFToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1333 ILFree(reinterpret_cast<LPITEMIDLIST
>(tb
->tbButton
.dwData
));
1337 HRESULT
CMenuSFToolbar::SetShellFolder(IShellFolder
*psf
, LPCITEMIDLIST pidlFolder
, HKEY hKey
, DWORD dwFlags
)
1339 m_shellFolder
= psf
;
1340 m_idList
= ILClone(pidlFolder
);
1342 m_dwMenuFlags
= dwFlags
;
1346 HRESULT
CMenuSFToolbar::GetShellFolder(DWORD
*pdwFlags
, LPITEMIDLIST
*ppidl
, REFIID riid
, void **ppv
)
1350 hr
= m_shellFolder
->QueryInterface(riid
, ppv
);
1351 if (FAILED_UNEXPECTEDLY(hr
))
1355 *pdwFlags
= m_dwMenuFlags
;
1359 LPITEMIDLIST pidl
= NULL
;
1363 pidl
= ILClone(m_idList
);
1366 ERR("ILClone failed!\n");
1367 (*reinterpret_cast<IUnknown
**>(ppv
))->Release();
1378 HRESULT
CMenuSFToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1381 CComPtr
<IContextMenu
> contextMenu
= NULL
;
1382 LPCITEMIDLIST pidl
= reinterpret_cast<LPCITEMIDLIST
>(dwData
);
1384 hr
= m_shellFolder
->GetUIObjectOf(GetToolbar(), 1, &pidl
, IID_NULL_PPV_ARG(IContextMenu
, &contextMenu
));
1385 if (FAILED_UNEXPECTEDLY(hr
))
1390 hr
= TrackContextMenu(contextMenu
, pt
);
1395 HRESULT
CMenuSFToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1397 return m_menuBand
->_CallCBWithItemPidl(reinterpret_cast<LPITEMIDLIST
>(data
), SMC_SFEXEC
, 0, 0);
1400 HRESULT
CMenuSFToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
, BOOL keyInitiated
)
1406 CComPtr
<IShellMenuCallback
> psmc
;
1407 CComPtr
<IShellMenu
> shellMenu
;
1409 LPITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1414 hr
= CMenuBand_Constructor(IID_PPV_ARG(IShellMenu
, &shellMenu
));
1415 if (FAILED_UNEXPECTEDLY(hr
))
1418 m_menuBand
->GetMenuInfo(&psmc
, &uId
, &uIdAncestor
, &flags
);
1420 // FIXME: not sure what to use as uId/uIdAncestor here
1421 hr
= shellMenu
->Initialize(psmc
, 0, uId
, SMINIT_VERTICAL
);
1422 if (FAILED_UNEXPECTEDLY(hr
))
1425 CComPtr
<IShellFolder
> childFolder
;
1426 hr
= m_shellFolder
->BindToObject(pidl
, NULL
, IID_PPV_ARG(IShellFolder
, &childFolder
));
1427 if (FAILED_UNEXPECTEDLY(hr
))
1430 hr
= shellMenu
->SetShellFolder(childFolder
, NULL
, NULL
, 0);
1431 if (FAILED_UNEXPECTEDLY(hr
))
1434 return PopupSubMenu(iItem
, index
, shellMenu
, keyInitiated
);
1437 HRESULT
CMenuSFToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1440 LPCITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1442 SFGAOF attrs
= SFGAO_FOLDER
;
1443 hr
= m_shellFolder
->GetAttributesOf(1, &pidl
, &attrs
);
1444 if (FAILED_UNEXPECTEDLY(hr
))
1447 return (attrs
& SFGAO_FOLDER
) ? S_OK
: S_FALSE
;