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
22 #include <commoncontrols.h>
23 #include <shlwapi_undoc.h>
25 #include "CMenuBand.h"
26 #include "CMenuToolbars.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(CMenuToolbars
);
31 HRESULT WINAPI
SHGetImageList(
37 // FIXME: Enable if/when wine comctl supports this flag properly
38 #define USE_TBSTYLE_EX_VERTICAL 0
40 // User-defined timer ID used while hot-tracking around the menu
41 #define TIMERID_HOTTRACK 1
43 HRESULT
CMenuToolbarBase::OnWinEvent(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*theResult
)
51 return OnCommand(wParam
, lParam
, theResult
);
54 hdr
= reinterpret_cast<LPNMHDR
>(lParam
);
57 case TBN_DELETINGBUTTON
:
58 return OnDeletingButton(reinterpret_cast<LPNMTOOLBAR
>(hdr
));
61 return OnPagerCalcSize(reinterpret_cast<LPNMPGCALCSIZE
>(hdr
));
64 return OnCommand(reinterpret_cast<LPNMTOOLBAR
>(hdr
)->iItem
, 0, theResult
);
66 case TBN_HOTITEMCHANGE
:
67 //return OnHotItemChange(reinterpret_cast<LPNMTBHOTITEM>(hdr), theResult);
71 return OnContextMenu(reinterpret_cast<LPNMMOUSE
>(hdr
));
74 return OnCustomDraw(reinterpret_cast<LPNMTBCUSTOMDRAW
>(hdr
), theResult
);
77 return OnGetInfoTip(reinterpret_cast<LPNMTBGETINFOTIP
>(hdr
));
79 // Silence unhandled items so that they don't print as unknown
86 case NM_RELEASEDCAPTURE
:
101 case NM_TOOLTIPSCREATED
:
105 case -714: return S_FALSE
;
108 TRACE("WM_NOTIFY unknown code %d, %d\n", hdr
->code
, hdr
->idFrom
);
117 LRESULT CALLBACK
CMenuToolbarBase::s_SubclassProc(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
119 CMenuToolbarBase
* pthis
= reinterpret_cast<CMenuToolbarBase
*>(GetWindowLongPtr(hWnd
, GWLP_USERDATA
));
120 return pthis
->SubclassProc(hWnd
, uMsg
, wParam
, lParam
);
123 LRESULT
CMenuToolbarBase::SubclassProc(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
129 case WM_USER_ISTRACKEDITEM
:
130 m_SubclassOld(hWnd
, uMsg
, wParam
, lParam
);
131 return IsTrackedItem(wParam
);
132 case WM_USER_CHANGETRACKEDITEM
:
133 m_SubclassOld(hWnd
, uMsg
, wParam
, lParam
);
134 return ChangeTrackedItem(wParam
, lParam
);
137 OnWinEvent(hWnd
, uMsg
, wParam
, lParam
, &lr
);
140 OnWinEvent(hWnd
, uMsg
, wParam
, lParam
, &lr
);
143 OnPopupTimer(wParam
);
146 return m_SubclassOld(hWnd
, uMsg
, wParam
, lParam
);
149 HRESULT
CMenuToolbarBase::DisableMouseTrack(BOOL bDisable
)
151 if (m_disableMouseTrack
!= bDisable
)
153 m_disableMouseTrack
= bDisable
;
154 TRACE("DisableMouseTrack %d\n", bDisable
);
159 HRESULT
CMenuToolbarBase::OnPagerCalcSize(LPNMPGCALCSIZE csize
)
162 GetSizes(NULL
, &tbs
, NULL
);
163 if (csize
->dwFlag
== PGF_CALCHEIGHT
)
165 csize
->iHeight
= tbs
.cy
;
167 else if (csize
->dwFlag
== PGF_CALCWIDTH
)
169 csize
->iWidth
= tbs
.cx
;
174 HRESULT
CMenuToolbarBase::OnCustomDraw(LPNMTBCUSTOMDRAW cdraw
, LRESULT
* theResult
)
183 switch (cdraw
->nmcd
.dwDrawStage
)
186 *theResult
= CDRF_NOTIFYITEMDRAW
;
189 case CDDS_ITEMPREPAINT
:
192 hdc
= cdraw
->nmcd
.hdc
;
194 // The item with an active submenu gets the CHECKED flag.
195 isHot
= m_hotBar
== this && (int) cdraw
->nmcd
.dwItemSpec
== m_hotItem
;
196 isPopup
= m_popupBar
== this && (int) cdraw
->nmcd
.dwItemSpec
== m_popupItem
;
198 if (m_initFlags
& SMINIT_VERTICAL
)
200 // Remove HOT and CHECKED flags (will restore HOT if necessary)
201 cdraw
->nmcd
.uItemState
&= ~(CDIS_HOT
| CDIS_CHECKED
);
203 // Decide on the colors
204 if (isHot
|| (m_hotItem
< 0 && isPopup
))
206 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
208 clrText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
209 bgBrush
= GetSysColorBrush(m_useFlatMenus
? COLOR_MENUHILIGHT
: COLOR_HIGHLIGHT
);
213 clrText
= GetSysColor(COLOR_MENUTEXT
);
214 bgBrush
= GetSysColorBrush(COLOR_MENU
);
217 // Paint the background color with the selected color
218 FillRect(hdc
, &rc
, bgBrush
);
220 // Set the text color in advance, this color will be assigned when the ITEMPOSTPAINT triggers
221 SetTextColor(hdc
, clrText
);
223 // Set the text color, will be used by the internal drawing code
224 cdraw
->clrText
= clrText
;
225 cdraw
->iListGap
+= 4;
227 // Tell the default drawing code we don't want any fanciness, not even a background.
228 *theResult
= CDRF_NOTIFYPOSTPAINT
| TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| 0x00800000; // FIXME: the last bit is Vista+, useful for debugging only
232 // Remove HOT and CHECKED flags (will restore HOT if necessary)
233 cdraw
->nmcd
.uItemState
&= ~CDIS_HOT
;
235 // Decide on the colors
236 if (isHot
|| (m_hotItem
< 0 && isPopup
))
238 cdraw
->nmcd
.uItemState
|= CDIS_HOT
;
246 case CDDS_ITEMPOSTPAINT
:
248 // Fetch the button style
249 btni
.cbSize
= sizeof(btni
);
250 btni
.dwMask
= TBIF_STYLE
;
251 SendMessage(m_hwndToolbar
, TB_GETBUTTONINFO
, cdraw
->nmcd
.dwItemSpec
, reinterpret_cast<LPARAM
>(&btni
));
253 // Check if we need to draw a submenu arrow
254 if (btni
.fsStyle
& BTNS_DROPDOWN
)
256 // TODO: Support RTL text modes by drawing a leftwards arrow aligned to the left of the control
258 // "8" is the rightwards dropdown arrow in the Marlett font
259 WCHAR text
[] = L
"8";
261 // Configure the font to draw with Marlett, keeping the current background color as-is
262 SelectObject(cdraw
->nmcd
.hdc
, m_marlett
);
263 SetBkMode(cdraw
->nmcd
.hdc
, TRANSPARENT
);
265 // Tweak the alignment by 1 pixel so the menu draws like the Windows start menu.
266 RECT rc
= cdraw
->nmcd
.rc
;
269 // The arrow is drawn at the right of the item's rect, aligned vertically.
270 DrawTextEx(cdraw
->nmcd
.hdc
, text
, 1, &rc
, DT_NOCLIP
| DT_VCENTER
| DT_RIGHT
| DT_SINGLELINE
, NULL
);
278 CMenuToolbarBase::CMenuToolbarBase(CMenuBand
*menuBand
, BOOL usePager
) :
281 m_useFlatMenus(FALSE
),
283 m_disableMouseTrack(FALSE
),
284 m_timerEnabled(FALSE
),
285 m_menuBand(menuBand
),
288 m_usePager(usePager
),
291 m_isTrackingPopup(FALSE
)
297 m_marlett
= CreateFont(
298 0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET
,
299 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
300 DEFAULT_QUALITY
, FF_DONTCARE
, L
"Marlett");
303 CMenuToolbarBase::~CMenuToolbarBase()
305 if (m_hwndToolbar
&& m_hwndToolbar
!= m_hwnd
)
306 DestroyWindow(m_hwndToolbar
);
309 DestroyWindow(m_hwnd
);
311 DeleteObject(m_marlett
);
314 void CMenuToolbarBase::InvalidateDraw()
316 InvalidateRect(m_hwnd
, NULL
, FALSE
);
319 HRESULT
CMenuToolbarBase::ShowWindow(BOOL fShow
)
321 ::ShowWindow(m_hwnd
, fShow
? SW_SHOW
: SW_HIDE
);
323 // Ensure that the right image list is assigned to the toolbar
326 // For custom-drawing
327 SystemParametersInfo(SPI_GETFLATMENU
, 0, &m_useFlatMenus
, 0);
332 HRESULT
CMenuToolbarBase::UpdateImageLists()
334 if ((m_initFlags
& (SMINIT_TOPLEVEL
| SMINIT_VERTICAL
)) == SMINIT_TOPLEVEL
) // not vertical.
336 // No image list, prevents the buttons from having a margin at the left side
337 SendMessageW(m_hwnd
, TB_SETIMAGELIST
, 0, 0);
341 // Assign the correct imagelist and padding based on the current icon size
344 if (m_menuBand
->UseBigIcons())
347 SendMessageW(m_hwndToolbar
, TB_SETPADDING
, 0, MAKELPARAM(4, 0));
352 SendMessageW(m_hwndToolbar
, TB_SETPADDING
, 0, MAKELPARAM(4, 4));
356 HRESULT hr
= SHGetImageList(shiml
, IID_PPV_ARG(IImageList
, &piml
));
357 if (FAILED_UNEXPECTEDLY(hr
))
359 SendMessageW(m_hwndToolbar
, TB_SETIMAGELIST
, 0, 0);
363 SendMessageW(m_hwndToolbar
, TB_SETIMAGELIST
, 0, reinterpret_cast<LPARAM
>(piml
));
368 HRESULT
CMenuToolbarBase::Close()
370 if (m_hwndToolbar
!= m_hwnd
)
371 DestroyWindow(m_hwndToolbar
);
373 DestroyWindow(m_hwnd
);
375 m_hwndToolbar
= NULL
;
381 HRESULT
CMenuToolbarBase::CreateToolbar(HWND hwndParent
, DWORD dwFlags
)
383 LONG tbStyles
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
|
384 TBSTYLE_TOOLTIPS
| TBSTYLE_TRANSPARENT
| TBSTYLE_REGISTERDROP
| TBSTYLE_LIST
| TBSTYLE_FLAT
| TBSTYLE_CUSTOMERASE
|
385 CCS_NODIVIDER
| CCS_NOPARENTALIGN
| CCS_NORESIZE
| CCS_TOP
;
386 LONG tbExStyles
= TBSTYLE_EX_DOUBLEBUFFER
| WS_EX_TOOLWINDOW
;
388 if (dwFlags
& SMINIT_VERTICAL
)
390 // Activate vertical semantics
391 tbStyles
|= CCS_VERT
;
393 #if USE_TBSTYLE_EX_VERTICAL
394 tbExStyles
|= TBSTYLE_EX_VERTICAL
;
398 m_initFlags
= dwFlags
;
400 // Get a temporary rect to use while creating the toolbar window.
401 // Ensure that it is not a null rect.
403 if (!::GetClientRect(hwndParent
, &rc
) ||
404 (rc
.left
== rc
.right
) ||
405 (rc
.top
== rc
.bottom
))
413 HWND hwndToolbar
= CreateWindowEx(
414 tbExStyles
, TOOLBARCLASSNAMEW
, NULL
,
415 tbStyles
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
416 hwndParent
, NULL
, _AtlBaseModule
.GetModuleInstance(), 0);
418 if (hwndToolbar
== NULL
)
421 // If needed, create the pager.
424 LONG pgStyles
= PGS_VERT
| WS_CHILD
| WS_VISIBLE
;
427 HWND hwndPager
= CreateWindowEx(
428 pgExStyles
, WC_PAGESCROLLER
, NULL
,
429 pgStyles
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
430 hwndParent
, NULL
, _AtlBaseModule
.GetModuleInstance(), 0);
432 ::SetParent(hwndToolbar
, hwndPager
);
433 ::SetParent(hwndPager
, hwndParent
);
435 SendMessage(hwndPager
, PGM_SETCHILD
, 0, reinterpret_cast<LPARAM
>(hwndToolbar
));
436 m_hwndToolbar
= hwndToolbar
;
441 ::SetParent(hwndToolbar
, hwndParent
);
442 m_hwndToolbar
= hwndToolbar
;
443 m_hwnd
= hwndToolbar
;
446 // Identify the version of the used Common Controls DLL by sending the size of the TBBUTTON structure.
447 SendMessageW(hwndToolbar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
450 SetWindowLongPtr(hwndToolbar
, GWLP_USERDATA
, reinterpret_cast<LONG_PTR
>(this));
451 m_SubclassOld
= (WNDPROC
) SetWindowLongPtr(hwndToolbar
, GWLP_WNDPROC
, reinterpret_cast<LONG_PTR
>(CMenuToolbarBase::s_SubclassProc
));
453 // Configure the image lists
459 HRESULT
CMenuToolbarBase::GetSizes(SIZE
* pMinSize
, SIZE
* pMaxSize
, SIZE
* pIntegralSize
)
462 *pMinSize
= m_idealSize
;
464 *pMaxSize
= m_idealSize
;
466 *pIntegralSize
= m_itemSize
;
474 // Obtain the ideal size, to be used as min and max
475 SendMessageW(m_hwndToolbar
, TB_AUTOSIZE
, 0, 0);
476 SendMessageW(m_hwndToolbar
, TB_GETMAXSIZE
, 0, reinterpret_cast<LPARAM
>(&m_idealSize
));
477 SendMessageW(m_hwndToolbar
, TB_GETIDEALSIZE
, (m_initFlags
& SMINIT_VERTICAL
) != 0, reinterpret_cast<LPARAM
>(&m_idealSize
));
479 // Obtain the button size, to be used as the integral size
480 DWORD size
= SendMessageW(m_hwndToolbar
, TB_GETBUTTONSIZE
, 0, 0);
481 m_itemSize
.cx
= GET_X_LPARAM(size
);
482 m_itemSize
.cy
= GET_Y_LPARAM(size
);
486 *pMinSize
= m_idealSize
;
488 *pMaxSize
= m_idealSize
;
490 *pIntegralSize
= m_itemSize
;
495 HRESULT
CMenuToolbarBase::SetPosSize(int x
, int y
, int cx
, int cy
)
497 // If we have a pager, set the toolbar height to the ideal height of the toolbar
498 if (m_hwnd
!= m_hwndToolbar
)
500 SetWindowPos(m_hwndToolbar
, NULL
, x
, y
, cx
, m_idealSize
.cy
, 0);
503 // Update the toolbar or pager to fit the requested rect
504 SetWindowPos(m_hwnd
, NULL
, x
, y
, cx
, cy
, 0);
506 // In a vertical menu, resize the buttons to fit the width
507 if (m_initFlags
& SMINIT_VERTICAL
)
509 DWORD btnSize
= SendMessage(m_hwndToolbar
, TB_GETBUTTONSIZE
, 0, 0);
510 SendMessage(m_hwndToolbar
, TB_SETBUTTONSIZE
, 0, MAKELPARAM(cx
, HIWORD(btnSize
)));
516 HRESULT
CMenuToolbarBase::IsWindowOwner(HWND hwnd
)
518 if (m_hwnd
&& m_hwnd
== hwnd
) return S_OK
;
519 if (m_hwndToolbar
&& m_hwndToolbar
== hwnd
) return S_OK
;
523 HRESULT
CMenuToolbarBase::GetWindow(HWND
*phwnd
)
533 HRESULT
CMenuToolbarBase::OnGetInfoTip(NMTBGETINFOTIP
* tip
)
538 INT iItem
= tip
->iItem
;
540 GetDataFromId(iItem
, &index
, &dwData
);
542 return InternalGetTooltip(iItem
, index
, dwData
, tip
->pszText
, tip
->cchTextMax
);
545 HRESULT
CMenuToolbarBase::OnPopupTimer(DWORD timerId
)
547 if (timerId
!= TIMERID_HOTTRACK
)
550 KillTimer(m_hwndToolbar
, TIMERID_HOTTRACK
);
555 m_timerEnabled
= FALSE
;
560 // Returns S_FALSE if the current item did not show a submenu
561 HRESULT hr
= PopupItem(m_hotItem
);
565 // If we didn't switch submenus, cancel the current popup regardless
568 HRESULT hr
= CancelCurrentPopup();
569 if (FAILED_UNEXPECTEDLY(hr
))
576 HRESULT
CMenuToolbarBase::KillPopupTimer()
580 m_timerEnabled
= FALSE
;
581 KillTimer(m_hwndToolbar
, TIMERID_HOTTRACK
);
587 HRESULT
CMenuToolbarBase::ChangeHotItem(CMenuToolbarBase
* toolbar
, INT item
, DWORD dwFlags
)
589 // Ignore the change if it already matches the stored info
590 if (m_hotBar
== toolbar
&& m_hotItem
== item
)
593 // Prevent a change of hot item if the change was triggered by the mouse,
594 // and mouse tracking is disabled.
595 if (m_disableMouseTrack
&& dwFlags
& HICF_MOUSE
)
597 TRACE("Hot item change prevented by DisableMouseTrack\n");
601 // Notify the toolbar if the hot-tracking left this toolbar
602 if (m_hotBar
== this && toolbar
!= this)
604 SendMessage(m_hwndToolbar
, TB_SETHOTITEM
, (WPARAM
) -1, 0);
610 if (m_hotBar
== this)
612 if (dwFlags
& HICF_MOUSE
)
614 // Vertical menus show/hide the submenu after a delay,
615 // but horizontal menubars switch between items instantly,
616 // if they were open.
617 if (m_initFlags
& SMINIT_VERTICAL
)
620 SystemParametersInfo(SPI_GETMENUSHOWDELAY
, 0, &elapsed
, 0);
621 SetTimer(m_hwndToolbar
, TIMERID_HOTTRACK
, elapsed
, NULL
);
622 m_timerEnabled
= TRUE
;
623 TRACE("SetTimer called with m_hotItem=%d\n", m_hotItem
);
625 else if (m_isTrackingPopup
)
627 // If the menubar has an open submenu, switch to the new item's submenu immediately
628 PopupItem(m_hotItem
);
634 info
.cbSize
= sizeof(info
);
637 int index
= SendMessage(m_hwndToolbar
, TB_GETBUTTONINFO
, item
, reinterpret_cast<LPARAM
>(&info
));
639 SendMessage(m_hwndToolbar
, TB_SETHOTITEM
, index
, 0);
647 HRESULT
CMenuToolbarBase::ChangePopupItem(CMenuToolbarBase
* toolbar
, INT item
)
649 // Ignore the change if it already matches the stored info
650 if (m_popupBar
== toolbar
&& m_popupItem
== item
)
653 // Notify the toolbar if the popup-tracking this toolbar
654 if (m_popupBar
== this && toolbar
!= this)
656 SendMessage(m_hwndToolbar
, TB_CHECKBUTTON
, m_popupItem
, FALSE
);
657 m_isTrackingPopup
= FALSE
;
660 m_popupBar
= toolbar
;
663 if (m_popupBar
== this)
665 SendMessage(m_hwndToolbar
, TB_CHECKBUTTON
, m_popupItem
, TRUE
);
672 HRESULT
CMenuToolbarBase::IsTrackedItem(INT index
)
676 if (m_hotBar
!= this)
682 if (!SendMessage(m_hwndToolbar
, TB_GETBUTTON
, index
, reinterpret_cast<LPARAM
>(&btn
)))
685 if (m_hotItem
== btn
.idCommand
)
688 if (m_popupItem
== btn
.idCommand
)
694 HRESULT
CMenuToolbarBase::ChangeTrackedItem(INT index
, BOOL wasTracking
)
700 m_isTrackingPopup
= FALSE
;
701 return m_menuBand
->_ChangeHotItem(NULL
, -1, HICF_MOUSE
);
704 if (!SendMessage(m_hwndToolbar
, TB_GETBUTTON
, index
, reinterpret_cast<LPARAM
>(&btn
)))
707 m_isTrackingPopup
= wasTracking
;
708 return m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, HICF_MOUSE
);
711 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, IShellMenu
* childShellMenu
)
713 // Calculate the submenu position and exclude area
717 if (!SendMessage(m_hwndToolbar
, TB_GETITEMRECT
, index
, reinterpret_cast<LPARAM
>(&rc
)))
720 GetWindowRect(m_hwnd
, &rcx
);
722 POINT a
= { rc
.left
, rc
.top
};
723 POINT b
= { rc
.right
, rc
.bottom
};
724 POINT c
= { rcx
.left
, rcx
.top
};
725 POINT d
= { rcx
.right
, rcx
.bottom
};
727 ClientToScreen(m_hwndToolbar
, &a
);
728 ClientToScreen(m_hwndToolbar
, &b
);
729 ClientToScreen(m_hwnd
, &c
);
730 ClientToScreen(m_hwnd
, &d
);
732 POINTL pt
= { a
.x
, b
.y
};
733 RECTL rcl
= { c
.x
, c
.y
, d
.x
, d
.y
};
735 if (m_initFlags
& SMINIT_VERTICAL
)
741 // Display the submenu
742 m_isTrackingPopup
= TRUE
;
744 m_menuBand
->_ChangePopupItem(this, iItem
);
745 m_menuBand
->_OnPopupSubMenu(childShellMenu
, &pt
, &rcl
);
750 HRESULT
CMenuToolbarBase::PopupSubMenu(UINT iItem
, UINT index
, HMENU menu
)
752 // Calculate the submenu position and exclude area
755 if (!SendMessage(m_hwndToolbar
, TB_GETITEMRECT
, index
, reinterpret_cast<LPARAM
>(&rc
)))
758 POINT a
= { rc
.left
, rc
.top
};
759 POINT b
= { rc
.right
, rc
.bottom
};
761 ClientToScreen(m_hwndToolbar
, &a
);
762 ClientToScreen(m_hwndToolbar
, &b
);
764 POINT pt
= { a
.x
, b
.y
};
765 RECT rcl
= { a
.x
, a
.y
, b
.x
, b
.y
};
767 if (m_initFlags
& SMINIT_VERTICAL
)
773 HMENU popup
= GetSubMenu(menu
, index
);
775 // Display the submenu
776 m_isTrackingPopup
= TRUE
;
777 m_menuBand
->_ChangePopupItem(this, iItem
);
778 m_menuBand
->_TrackSubMenu(popup
, pt
.x
, pt
.y
, rcl
);
779 m_menuBand
->_ChangePopupItem(NULL
, -1);
780 m_isTrackingPopup
= FALSE
;
782 m_menuBand
->_ChangeHotItem(NULL
, -1, 0);
787 HRESULT
CMenuToolbarBase::TrackContextMenu(IContextMenu
* contextMenu
, POINT pt
)
790 m_menuBand
->_KillPopupTimers();
792 m_menuBand
->_CancelCurrentPopup();
794 // Display the context menu
795 return m_menuBand
->_TrackContextMenu(contextMenu
, pt
.x
, pt
.y
);
798 HRESULT
CMenuToolbarBase::OnCommand(WPARAM wParam
, LPARAM lParam
, LRESULT
*theResult
)
800 if (m_disableMouseTrack
)
803 TRACE("Item click prevented by DisableMouseTrack\n");
807 // If a button is clicked while a submenu was open, cancel the submenu.
808 if (!(m_initFlags
& SMINIT_VERTICAL
) && m_isTrackingPopup
)
810 TRACE("OnCommand cancelled because it was tracking submenu.\n");
816 m_menuBand
->_KillPopupTimers();
818 if (PopupItem(wParam
) == S_OK
)
820 TRACE("PopupItem returned S_OK\n");
824 TRACE("Executing...\n");
826 HRESULT hr
= m_menuBand
->_MenuItemHotTrack(MPOS_EXECUTE
);
828 if (FAILED_UNEXPECTEDLY(hr
))
835 GetDataFromId(iItem
, &index
, &data
);
837 return InternalExecuteItem(iItem
, index
, data
);
840 HRESULT
CMenuToolbarBase::OnContextMenu(NMMOUSE
* rclick
)
842 INT iItem
= rclick
->dwItemSpec
;
843 INT index
= rclick
->dwHitInfo
;
844 DWORD_PTR data
= rclick
->dwItemData
;
846 GetDataFromId(iItem
, &index
, &data
);
848 return InternalContextMenu(iItem
, index
, data
, rclick
->pt
);
851 HRESULT
CMenuToolbarBase::KeyboardItemChange(DWORD dwSelectType
)
853 int prev
= m_hotItem
;
856 if (dwSelectType
!= 0xFFFFFFFF)
858 int count
= SendMessage(m_hwndToolbar
, TB_BUTTONCOUNT
, 0, 0);
860 if (dwSelectType
== VK_HOME
)
863 dwSelectType
= VK_DOWN
;
865 else if (dwSelectType
== VK_END
)
868 dwSelectType
= VK_UP
;
874 TBBUTTONINFO info
= { 0 };
875 info
.cbSize
= sizeof(TBBUTTONINFO
);
877 index
= SendMessage(m_hwndToolbar
, TB_GETBUTTONINFO
, m_hotItem
, reinterpret_cast<LPARAM
>(&info
));
882 if (dwSelectType
== VK_UP
)
886 else if (dwSelectType
== VK_DOWN
)
893 if (dwSelectType
== VK_UP
)
897 else if (dwSelectType
== VK_DOWN
)
904 TBBUTTON btn
= { 0 };
905 while (index
>= 0 && index
< count
)
907 DWORD res
= SendMessage(m_hwndToolbar
, TB_GETBUTTON
, index
, reinterpret_cast<LPARAM
>(&btn
));
913 if (prev
!= btn
.idCommand
)
915 TRACE("Setting Hot item to %d\n", index
);
916 m_menuBand
->_ChangeHotItem(this, btn
.idCommand
, 0);
921 if (dwSelectType
== VK_UP
)
925 else if (dwSelectType
== VK_DOWN
)
936 TRACE("Setting Hot item to null\n");
937 m_menuBand
->_ChangeHotItem(NULL
, -1, 0);
943 HRESULT
CMenuToolbarBase::AddButton(DWORD commandId
, LPCWSTR caption
, BOOL hasSubMenu
, INT iconId
, DWORD_PTR buttonData
, BOOL last
)
945 TBBUTTON tbb
= { 0 };
947 tbb
.fsState
= TBSTATE_ENABLED
;
948 #if !USE_TBSTYLE_EX_VERTICAL
949 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
950 tbb
.fsState
|= TBSTATE_WRAP
;
952 tbb
.fsStyle
= BTNS_CHECKGROUP
;
954 if (hasSubMenu
&& (m_initFlags
& SMINIT_VERTICAL
))
955 tbb
.fsStyle
|= BTNS_DROPDOWN
;
957 if (!(m_initFlags
& SMINIT_VERTICAL
))
958 tbb
.fsStyle
|= BTNS_AUTOSIZE
;
960 tbb
.iString
= (INT_PTR
) caption
;
961 tbb
.idCommand
= commandId
;
963 tbb
.iBitmap
= iconId
;
964 tbb
.dwData
= buttonData
;
966 if (!SendMessageW(m_hwndToolbar
, TB_ADDBUTTONS
, 1, reinterpret_cast<LPARAM
>(&tbb
)))
967 return HRESULT_FROM_WIN32(GetLastError());
972 HRESULT
CMenuToolbarBase::AddSeparator(BOOL last
)
974 TBBUTTON tbb
= { 0 };
976 tbb
.fsState
= TBSTATE_ENABLED
;
977 #if !USE_TBSTYLE_EX_VERTICAL
978 if (!last
&& (m_initFlags
& SMINIT_VERTICAL
))
979 tbb
.fsState
|= TBSTATE_WRAP
;
981 tbb
.fsStyle
= BTNS_SEP
;
984 if (!SendMessageW(m_hwndToolbar
, TB_ADDBUTTONS
, 1, reinterpret_cast<LPARAM
>(&tbb
)))
985 return HRESULT_FROM_WIN32(GetLastError());
990 HRESULT
CMenuToolbarBase::AddPlaceholder()
992 TBBUTTON tbb
= { 0 };
993 PCWSTR MenuString
= L
"(Empty)";
997 tbb
.iString
= (INT_PTR
) MenuString
;
1000 if (!SendMessageW(m_hwndToolbar
, TB_ADDBUTTONS
, 1, reinterpret_cast<LPARAM
>(&tbb
)))
1001 return HRESULT_FROM_WIN32(GetLastError());
1006 HRESULT
CMenuToolbarBase::ClearToolbar()
1008 while (SendMessage(m_hwndToolbar
, TB_DELETEBUTTON
, 0, 0))
1015 HRESULT
CMenuToolbarBase::GetDataFromId(INT iItem
, INT
* pIndex
, DWORD_PTR
* pData
)
1026 TBBUTTONINFO info
= { 0 };
1028 info
.cbSize
= sizeof(TBBUTTONINFO
);
1029 info
.dwMask
= TBIF_COMMAND
| TBIF_LPARAM
;
1031 int index
= SendMessage(m_hwndToolbar
, TB_GETBUTTONINFO
, iItem
, reinterpret_cast<LPARAM
>(&info
));
1039 *pData
= info
.lParam
;
1044 HRESULT
CMenuToolbarBase::CancelCurrentPopup()
1046 return m_menuBand
->_CancelCurrentPopup();
1049 HRESULT
CMenuToolbarBase::PopupItem(INT iItem
)
1057 if (m_popupBar
== this && m_popupItem
== iItem
)
1060 GetDataFromId(iItem
, &index
, &dwData
);
1062 HRESULT hr
= InternalHasSubMenu(iItem
, index
, dwData
);
1068 HRESULT hr
= CancelCurrentPopup();
1069 if (FAILED_UNEXPECTEDLY(hr
))
1073 if (!(m_initFlags
& SMINIT_VERTICAL
))
1075 TRACE("PopupItem non-vertical %d %d\n", index
, iItem
);
1076 m_menuBand
->_ChangeHotItem(this, iItem
, 0);
1079 return InternalPopupItem(iItem
, index
, dwData
);
1082 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand
*menuBand
) :
1083 CMenuToolbarBase(menuBand
, FALSE
),
1088 HRESULT
CMenuStaticToolbar::GetMenu(
1089 _Out_opt_ HMENU
*phmenu
,
1090 _Out_opt_ HWND
*phwnd
,
1091 _Out_opt_ DWORD
*pdwFlags
)
1098 *pdwFlags
= m_dwMenuFlags
;
1103 HRESULT
CMenuStaticToolbar::SetMenu(
1109 m_dwMenuFlags
= dwFlags
;
1114 HRESULT
CMenuStaticToolbar::FillToolbar(BOOL clearFirst
)
1117 int ic
= GetMenuItemCount(m_hmenu
);
1125 for (i
= 0; i
< ic
; i
++)
1127 BOOL last
= i
+ 1 == ic
;
1131 info
.cbSize
= sizeof(info
);
1132 info
.dwTypeData
= NULL
;
1133 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_ID
;
1135 if (!GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
))
1137 TRACE("Error obtaining info for menu item at pos=%d\n", i
);
1143 if (info
.fType
& MFT_SEPARATOR
)
1147 else if (!(info
.fType
& MFT_BITMAP
))
1151 info
.dwTypeData
= (PWSTR
) HeapAlloc(GetProcessHeap(), 0, (info
.cch
+ 1) * sizeof(WCHAR
));
1153 info
.fMask
= MIIM_STRING
| MIIM_SUBMENU
| MIIM_ID
;
1154 GetMenuItemInfoW(m_hmenu
, i
, TRUE
, &info
);
1156 SMINFO
* sminfo
= new SMINFO();
1157 sminfo
->dwMask
= SMIM_ICON
| SMIM_FLAGS
;
1158 // FIXME: remove before deleting the toolbar or it will leak
1160 HRESULT hr
= m_menuBand
->_CallCBWithItemId(info
.wID
, SMC_GETINFO
, 0, reinterpret_cast<LPARAM
>(sminfo
));
1161 if (FAILED_UNEXPECTEDLY(hr
))
1164 AddButton(info
.wID
, info
.dwTypeData
, info
.hSubMenu
!= NULL
, sminfo
->iIcon
, reinterpret_cast<DWORD_PTR
>(sminfo
), last
);
1166 HeapFree(GetProcessHeap(), 0, info
.dwTypeData
);
1173 HRESULT
CMenuStaticToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1175 //SMINFO * info = reinterpret_cast<SMINFO*>(dwData);
1180 HRESULT
CMenuStaticToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1182 delete reinterpret_cast<SMINFO
*>(tb
->tbButton
.dwData
);
1186 HRESULT
CMenuStaticToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1188 CComPtr
<IContextMenu
> contextMenu
;
1189 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
,
1190 reinterpret_cast<WPARAM
>(&IID_IContextMenu
), reinterpret_cast<LPARAM
>(&contextMenu
));
1194 return TrackContextMenu(contextMenu
, pt
);
1197 HRESULT
CMenuStaticToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1199 return m_menuBand
->_CallCBWithItemId(iItem
, SMC_EXEC
, 0, 0);
1202 HRESULT
CMenuStaticToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
)
1204 SMINFO
* nfo
= reinterpret_cast<SMINFO
*>(dwData
);
1208 if (nfo
->dwFlags
&SMIF_TRACKPOPUP
)
1210 return PopupSubMenu(iItem
, index
, m_hmenu
);
1214 CComPtr
<IShellMenu
> shellMenu
;
1215 HRESULT hr
= m_menuBand
->_CallCBWithItemId(iItem
, SMC_GETOBJECT
, reinterpret_cast<WPARAM
>(&IID_IShellMenu
), reinterpret_cast<LPARAM
>(&shellMenu
));
1216 if (FAILED_UNEXPECTEDLY(hr
))
1219 return PopupSubMenu(iItem
, index
, shellMenu
);
1223 HRESULT
CMenuStaticToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1225 return ::GetSubMenu(m_hmenu
, index
) ? S_OK
: S_FALSE
;
1228 CMenuSFToolbar::CMenuSFToolbar(CMenuBand
* menuBand
) :
1229 CMenuToolbarBase(menuBand
, TRUE
),
1230 m_shellFolder(NULL
),
1236 CMenuSFToolbar::~CMenuSFToolbar()
1240 HRESULT
CMenuSFToolbar::FillToolbar(BOOL clearFirst
)
1247 m_shellFolder
->EnumObjects(GetToolbar(), SHCONTF_FOLDERS
| SHCONTF_NONFOLDERS
, &eidl
);
1249 LPITEMIDLIST item
= static_cast<LPITEMIDLIST
>(CoTaskMemAlloc(sizeof(ITEMIDLIST
)));
1251 hr
= eidl
->Next(1, &item
, &fetched
);
1252 while (SUCCEEDED(hr
) && fetched
> 0)
1257 STRRET sr
= { STRRET_CSTR
, { 0 } };
1259 hr
= m_shellFolder
->GetDisplayNameOf(item
, SIGDN_NORMALDISPLAY
, &sr
);
1260 if (FAILED_UNEXPECTEDLY(hr
))
1263 StrRetToStr(&sr
, NULL
, &MenuString
);
1265 index
= SHMapPIDLToSystemImageListIndex(m_shellFolder
, item
, &indexOpen
);
1267 LPCITEMIDLIST itemc
= item
;
1269 SFGAOF attrs
= SFGAO_FOLDER
;
1270 hr
= m_shellFolder
->GetAttributesOf(1, &itemc
, &attrs
);
1272 DWORD_PTR dwData
= reinterpret_cast<DWORD_PTR
>(ILClone(item
));
1274 // Fetch next item already, so we know if the current one is the last
1275 hr
= eidl
->Next(1, &item
, &fetched
);
1277 AddButton(++i
, MenuString
, attrs
& SFGAO_FOLDER
, index
, dwData
, FAILED(hr
) || fetched
== 0);
1279 CoTaskMemFree(MenuString
);
1281 CoTaskMemFree(item
);
1283 // If no items were added, show the "empty" placeholder
1286 return AddPlaceholder();
1292 HRESULT
CMenuSFToolbar::InternalGetTooltip(INT iItem
, INT index
, DWORD_PTR dwData
, LPWSTR pszText
, INT cchTextMax
)
1294 //ITEMIDLIST * pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
1299 HRESULT
CMenuSFToolbar::OnDeletingButton(const NMTOOLBAR
* tb
)
1301 ILFree(reinterpret_cast<LPITEMIDLIST
>(tb
->tbButton
.dwData
));
1305 HRESULT
CMenuSFToolbar::SetShellFolder(IShellFolder
*psf
, LPCITEMIDLIST pidlFolder
, HKEY hKey
, DWORD dwFlags
)
1307 m_shellFolder
= psf
;
1308 m_idList
= ILClone(pidlFolder
);
1310 m_dwMenuFlags
= dwFlags
;
1314 HRESULT
CMenuSFToolbar::GetShellFolder(DWORD
*pdwFlags
, LPITEMIDLIST
*ppidl
, REFIID riid
, void **ppv
)
1318 hr
= m_shellFolder
->QueryInterface(riid
, ppv
);
1319 if (FAILED_UNEXPECTEDLY(hr
))
1323 *pdwFlags
= m_dwMenuFlags
;
1327 LPITEMIDLIST pidl
= NULL
;
1331 pidl
= ILClone(m_idList
);
1334 (*(IUnknown
**) ppv
)->Release();
1345 HRESULT
CMenuSFToolbar::InternalContextMenu(INT iItem
, INT index
, DWORD_PTR dwData
, POINT pt
)
1348 CComPtr
<IContextMenu
> contextMenu
= NULL
;
1349 LPCITEMIDLIST pidl
= reinterpret_cast<LPCITEMIDLIST
>(dwData
);
1351 #define IID_NULL_PPV_ARG(Itype, ppType) IID_##Itype, NULL, reinterpret_cast<void**>((static_cast<Itype**>(ppType)))
1353 hr
= m_shellFolder
->GetUIObjectOf(GetToolbar(), 1, &pidl
, IID_NULL_PPV_ARG(IContextMenu
, &contextMenu
));
1354 if (FAILED_UNEXPECTEDLY(hr
))
1359 hr
= TrackContextMenu(contextMenu
, pt
);
1364 HRESULT
CMenuSFToolbar::InternalExecuteItem(INT iItem
, INT index
, DWORD_PTR data
)
1366 return m_menuBand
->_CallCBWithItemPidl(reinterpret_cast<LPITEMIDLIST
>(data
), SMC_SFEXEC
, 0, 0);
1369 HRESULT
CMenuSFToolbar::InternalPopupItem(INT iItem
, INT index
, DWORD_PTR dwData
)
1375 CComPtr
<IShellMenuCallback
> psmc
;
1376 CComPtr
<IShellMenu
> shellMenu
;
1378 LPITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1383 hr
= CMenuBand_Constructor(IID_PPV_ARG(IShellMenu
, &shellMenu
));
1384 if (FAILED_UNEXPECTEDLY(hr
))
1387 m_menuBand
->GetMenuInfo(&psmc
, &uId
, &uIdAncestor
, &flags
);
1389 // FIXME: not sure what to use as uId/uIdAncestor here
1390 hr
= shellMenu
->Initialize(psmc
, 0, uId
, SMINIT_VERTICAL
);
1391 if (FAILED_UNEXPECTEDLY(hr
))
1394 CComPtr
<IShellFolder
> childFolder
;
1395 hr
= m_shellFolder
->BindToObject(pidl
, NULL
, IID_PPV_ARG(IShellFolder
, &childFolder
));
1396 if (FAILED_UNEXPECTEDLY(hr
))
1399 hr
= shellMenu
->SetShellFolder(childFolder
, NULL
, NULL
, 0);
1400 if (FAILED_UNEXPECTEDLY(hr
))
1403 return PopupSubMenu(iItem
, index
, shellMenu
);
1406 HRESULT
CMenuSFToolbar::InternalHasSubMenu(INT iItem
, INT index
, DWORD_PTR dwData
)
1409 LPCITEMIDLIST pidl
= reinterpret_cast<LPITEMIDLIST
>(dwData
);
1411 SFGAOF attrs
= SFGAO_FOLDER
;
1412 hr
= m_shellFolder
->GetAttributesOf(1, &pidl
, &attrs
);
1413 if (FAILED_UNEXPECTEDLY(hr
))
1416 return (attrs
& SFGAO_FOLDER
) ? S_OK
: S_FALSE
;