1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/menuitem.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/menuitem.h"
30 #include "wx/stockitem.h"
34 #include "wx/dcmemory.h"
36 #include "wx/bitmap.h"
37 #include "wx/settings.h"
38 #include "wx/window.h"
40 #include "wx/string.h"
49 #include "wx/msw/private.h"
50 #include "wx/msw/dc.h"
53 // Implemented in menu.cpp
54 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
) ;
58 #include "wx/msw/uxtheme.h"
61 // ---------------------------------------------------------------------------
63 // ---------------------------------------------------------------------------
66 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
68 // ----------------------------------------------------------------------------
69 // helper classes for temporarily changing HDC parameters
70 // ----------------------------------------------------------------------------
75 // This class just stores an HDC.
79 HDCHandler(HDC hdc
) : m_hdc(hdc
) { }
84 class HDCTextColChanger
: HDCHandler
87 HDCTextColChanger(HDC hdc
, COLORREF col
)
89 m_colOld(::SetTextColor(hdc
, col
))
95 ::SetTextColor(m_hdc
, m_colOld
);
102 class HDCBgColChanger
: HDCHandler
105 HDCBgColChanger(HDC hdc
, COLORREF col
)
107 m_colOld(::SetBkColor(hdc
, col
))
113 ::SetBkColor(m_hdc
, m_colOld
);
120 class HDCBgModeChanger
: HDCHandler
123 HDCBgModeChanger(HDC hdc
, int mode
)
125 m_modeOld(::SetBkMode(hdc
, mode
))
131 ::SetBkMode(m_hdc
, m_modeOld
);
138 } // anonymous namespace
140 // ============================================================================
142 // ============================================================================
144 #if wxUSE_OWNER_DRAWN
146 #include "wx/fontutil.h"
147 #include "wx/msw/private/metrics.h"
149 #ifndef SPI_GETKEYBOARDCUES
150 #define SPI_GETKEYBOARDCUES 0x100A
153 #ifndef DSS_HIDEPREFIX
154 #define DSS_HIDEPREFIX 0x0200
161 MENU_MENUITEM_TMSCHEMA
= 1,
162 MENU_SEPARATOR_TMSCHEMA
= 6,
163 MENU_POPUPBACKGROUND
= 9,
164 MENU_POPUPBORDERS
= 10,
165 MENU_POPUPCHECK
= 11,
166 MENU_POPUPCHECKBACKGROUND
= 12,
167 MENU_POPUPGUTTER
= 13,
169 MENU_POPUPSEPARATOR
= 15,
170 MENU_POPUPSUBMENU
= 16,
182 enum POPUPCHECKBACKGROUNDSTATES
189 enum POPUPCHECKSTATES
191 MC_CHECKMARKNORMAL
= 1,
192 MC_CHECKMARKDISABLED
= 2,
194 MC_BULLETDISABLED
= 4,
197 const int TMT_MENUFONT
= 803;
198 const int TMT_BORDERSIZE
= 2403;
199 const int TMT_CONTENTMARGINS
= 3602;
200 const int TMT_SIZINGMARGINS
= 3601;
202 #endif // wxUSE_UXTHEME
204 #endif // wxUSE_OWNER_DRAWN
206 // ----------------------------------------------------------------------------
207 // dynamic classes implementation
208 // ----------------------------------------------------------------------------
210 #if wxUSE_EXTENDED_RTTI
212 bool wxMenuItemStreamingCallback( const wxObject
*object
, wxWriter
* , wxPersister
* , wxxVariantArray
& )
214 const wxMenuItem
* mitem
= dynamic_cast<const wxMenuItem
*>(object
) ;
215 if ( mitem
->GetMenu() && !mitem
->GetMenu()->GetTitle().empty() )
217 // we don't stream out the first two items for menus with a title, they will be reconstructed
218 if ( mitem
->GetMenu()->FindItemByPosition(0) == mitem
|| mitem
->GetMenu()->FindItemByPosition(1) == mitem
)
224 wxBEGIN_ENUM( wxItemKind
)
225 wxENUM_MEMBER( wxITEM_SEPARATOR
)
226 wxENUM_MEMBER( wxITEM_NORMAL
)
227 wxENUM_MEMBER( wxITEM_CHECK
)
228 wxENUM_MEMBER( wxITEM_RADIO
)
229 wxEND_ENUM( wxItemKind
)
231 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem
, wxObject
,"wx/menuitem.h",wxMenuItemStreamingCallback
)
233 wxBEGIN_PROPERTIES_TABLE(wxMenuItem
)
234 wxPROPERTY( Parent
,wxMenu
*, SetMenu
, GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
235 wxPROPERTY( Id
,int, SetId
, GetId
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
236 wxPROPERTY( Text
, wxString
, SetText
, GetText
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
237 wxPROPERTY( Help
, wxString
, SetHelp
, GetHelp
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
238 wxREADONLY_PROPERTY( Kind
, wxItemKind
, GetKind
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
239 wxPROPERTY( SubMenu
,wxMenu
*, SetSubMenu
, GetSubMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
240 wxPROPERTY( Enabled
, bool , Enable
, IsEnabled
, wxxVariant((bool)true) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
241 wxPROPERTY( Checked
, bool , Check
, IsChecked
, wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
242 wxPROPERTY( Checkable
, bool , SetCheckable
, IsCheckable
, wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
243 wxEND_PROPERTIES_TABLE()
245 wxBEGIN_HANDLERS_TABLE(wxMenuItem
)
246 wxEND_HANDLERS_TABLE()
248 wxDIRECT_CONSTRUCTOR_6( wxMenuItem
, wxMenu
* , Parent
, int , Id
, wxString
, Text
, wxString
, Help
, wxItemKind
, Kind
, wxMenu
* , SubMenu
)
250 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
257 #if wxUSE_OWNER_DRAWN
262 // helper class to keep information about metrics and other stuff
263 // needed for measuring and drawing menu item
267 // Wrapper around standard MARGINS structure providing some helper
268 // functions and automatically initializing the margin fields to 0.
269 struct Margins
: MARGINS
279 int GetTotalX() const { return cxLeftWidth
+ cxRightWidth
; }
280 int GetTotalY() const { return cyTopHeight
+ cyBottomHeight
; }
282 void ApplyTo(RECT
& rect
) const
284 rect
.top
+= cyTopHeight
;
285 rect
.left
+= cxLeftWidth
;
286 rect
.right
-= cyTopHeight
;
287 rect
.bottom
-= cyBottomHeight
;
290 void UnapplyFrom(RECT
& rect
) const
292 rect
.top
-= cyTopHeight
;
293 rect
.left
-= cxLeftWidth
;
294 rect
.right
+= cyTopHeight
;
295 rect
.bottom
+= cyBottomHeight
;
299 Margins ItemMargin
; // popup item margins
301 Margins CheckMargin
; // popup check margins
302 Margins CheckBgMargin
; // popup check background margins
304 Margins ArrowMargin
; // popup submenu arrow margins
306 Margins SeparatorMargin
; // popup separator margins
308 SIZE CheckSize
; // popup check size metric
309 SIZE ArrowSize
; // popup submenu arrow size metric
310 SIZE SeparatorSize
; // popup separator size metric
312 int TextBorder
; // popup border space between
313 // item text and gutter
315 int AccelBorder
; // popup border space between
316 // item text and accelerator
318 int ArrowBorder
; // popup border space between
319 // item accelerator and submenu arrow
321 int Offset
; // system added space at the end of the menu,
322 // add this offset for remove the extra space
324 wxFont Font
; // default menu font
326 bool AlwaysShowCues
; // must keyboard cues always be shown?
328 bool Theme
; // is data initialized for FullTheme?
330 static const MenuDrawData
* Get()
332 // notice that s_menuData can't be created as a global variable because
333 // it needs a window to initialize and no windows exist at the time of
334 // globals initialization yet
337 static MenuDrawData s_menuData
;
338 ms_instance
= &s_menuData
;
342 bool theme
= MenuLayout() == FullTheme
;
343 if ( ms_instance
->Theme
!= theme
)
345 #endif // wxUSE_UXTHEME
355 // get the theme engine or NULL if themes
356 // are not available or not supported on menu
357 static wxUxThemeEngine
*GetUxThemeEngine()
360 if ( MenuLayout() == FullTheme
)
361 return wxUxThemeEngine::GetIfActive();
362 #endif // wxUSE_UXTHEME
369 FullTheme
, // full menu themes (Vista or new)
370 PseudoTheme
, // pseudo menu themes (on XP)
374 static MenuLayoutType
MenuLayout()
376 MenuLayoutType menu
= Classic
;
378 if ( wxUxThemeEngine::GetIfActive() != NULL
)
380 static wxWinVersion ver
= wxGetWinVersion();
381 if ( ver
>= wxWinVersion_Vista
)
383 else if ( ver
== wxWinVersion_XP
)
386 #endif // wxUSE_UXTHEME
393 static MenuDrawData
* ms_instance
;
396 MenuDrawData
* MenuDrawData::ms_instance
= NULL
;
398 void MenuDrawData::Init()
401 wxUxThemeEngine
* theme
= GetUxThemeEngine();
404 wxWindow
* window
= static_cast<wxApp
*>(wxApp::GetInstance())->GetTopWindow();
405 wxUxThemeHandle
hTheme(window
, L
"MENU");
407 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPITEM
, 0,
408 TMT_CONTENTMARGINS
, NULL
,
411 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPCHECK
, 0,
412 TMT_CONTENTMARGINS
, NULL
,
414 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPCHECKBACKGROUND
, 0,
415 TMT_CONTENTMARGINS
, NULL
,
418 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPSUBMENU
, 0,
419 TMT_CONTENTMARGINS
, NULL
,
422 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPSEPARATOR
, 0,
423 TMT_SIZINGMARGINS
, NULL
,
426 theme
->GetThemePartSize(hTheme
, NULL
, MENU_POPUPCHECK
, 0,
427 NULL
, TS_TRUE
, &CheckSize
);
429 theme
->GetThemePartSize(hTheme
, NULL
, MENU_POPUPSUBMENU
, 0,
430 NULL
, TS_TRUE
, &ArrowSize
);
432 theme
->GetThemePartSize(hTheme
, NULL
, MENU_POPUPSEPARATOR
, 0,
433 NULL
, TS_TRUE
, &SeparatorSize
);
435 theme
->GetThemeInt(hTheme
, MENU_POPUPBACKGROUND
, 0, TMT_BORDERSIZE
, &TextBorder
);
442 wxUxThemeFont themeFont
;
443 theme
->GetThemeSysFont(hTheme
, TMT_MENUFONT
, themeFont
.GetPtr());
444 Font
= wxFont(themeFont
.GetLOGFONT());
448 // native menu doesn't uses the vertical margins
449 ItemMargin
.cyTopHeight
=
450 ItemMargin
.cyBottomHeight
= 0;
452 // native menu uses small top margin for separator
453 if ( SeparatorMargin
.cyTopHeight
>= 2 )
454 SeparatorMargin
.cyTopHeight
-= 2;
457 #endif // wxUSE_UXTHEME
459 const NONCLIENTMETRICS
& metrics
= wxMSWImpl::GetNonClientMetrics();
461 CheckMargin
.cxLeftWidth
=
462 CheckMargin
.cxRightWidth
= ::GetSystemMetrics(SM_CXEDGE
);
463 CheckMargin
.cyTopHeight
=
464 CheckMargin
.cyBottomHeight
= ::GetSystemMetrics(SM_CYEDGE
);
466 CheckSize
.cx
= ::GetSystemMetrics(SM_CXMENUCHECK
);
467 CheckSize
.cy
= ::GetSystemMetrics(SM_CYMENUCHECK
);
469 ArrowSize
= CheckSize
;
471 // separator height with margins
472 int sepFullSize
= metrics
.iMenuHeight
/ 2;
474 SeparatorMargin
.cxLeftWidth
=
475 SeparatorMargin
.cxRightWidth
= 1;
476 SeparatorMargin
.cyTopHeight
=
477 SeparatorMargin
.cyBottomHeight
= sepFullSize
/ 2 - 1;
479 SeparatorSize
.cx
= 1;
480 SeparatorSize
.cy
= sepFullSize
- SeparatorMargin
.GetTotalY();
488 Font
= wxFont(wxNativeFontInfo(metrics
.lfMenuFont
));
494 if ( ::SystemParametersInfo(SPI_GETKEYBOARDCUES
, 0, &value
, 0) == 0 )
496 // if it's not supported, we must be on an old Windows version
497 // which always shows them
501 AlwaysShowCues
= value
== 1;
505 } // anonymous namespace
507 #endif // wxUSE_OWNER_DRAWN
513 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
515 const wxString
& text
,
516 const wxString
& strHelp
,
519 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
524 #if WXWIN_COMPATIBILITY_2_8
525 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
527 const wxString
& text
,
528 const wxString
& help
,
531 : wxMenuItemBase(parentMenu
, id
, text
, help
,
532 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
538 void wxMenuItem::Init()
540 m_radioGroup
.start
= -1;
541 m_isRadioGroupStart
= false;
543 #if wxUSE_OWNER_DRAWN
545 // when the color is not valid, wxOwnerDraw takes the default ones.
546 // If we set the colors here and they are changed by the user during
547 // the execution, then the colors are not updated until the application
548 // is restarted and our menus look bad
549 SetTextColour(wxNullColour
);
550 SetBackgroundColour(wxNullColour
);
552 // setting default colors switched ownerdraw on: switch it off again
553 SetOwnerDrawn(false);
555 // switch ownerdraw back on if using a non default margin
556 if ( !IsSeparator() )
557 SetMarginWidth(GetMarginWidth());
559 #endif // wxUSE_OWNER_DRAWN
562 wxMenuItem::~wxMenuItem()
569 // return the id for calling Win32 API functions
570 WXWPARAM
wxMenuItem::GetMSWId() const
572 // we must use ids in unsigned short range with Windows functions, if we
573 // pass ids > USHRT_MAX to them they get very confused (e.g. start
574 // generating WM_COMMAND messages with negative high word of wParam), so
575 // use the cast to ensure the id is in range
576 return m_subMenu
? wxPtrToUInt(m_subMenu
->GetHMenu())
577 : static_cast<unsigned short>(GetId());
583 bool wxMenuItem::IsChecked() const
585 // fix that RTTI is always getting the correct state (separators cannot be
586 // checked, but the Windows call below returns true
590 // the item might not be attached to a menu yet
592 // TODO: shouldn't we just always call the base class version? It seems
593 // like it ought to always be in sync
595 return wxMenuItemBase::IsChecked();
597 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
598 int flag
= ::GetMenuState(hmenu
, GetMSWId(), MF_BYCOMMAND
);
600 return (flag
& MF_CHECKED
) != 0;
606 void wxMenuItem::SetAsRadioGroupStart()
608 m_isRadioGroupStart
= true;
611 void wxMenuItem::SetRadioGroupStart(int start
)
613 wxASSERT_MSG( !m_isRadioGroupStart
,
614 wxT("should only be called for the next radio items") );
616 m_radioGroup
.start
= start
;
619 void wxMenuItem::SetRadioGroupEnd(int end
)
621 wxASSERT_MSG( m_isRadioGroupStart
,
622 wxT("should only be called for the first radio item") );
624 m_radioGroup
.end
= end
;
630 void wxMenuItem::Enable(bool enable
)
632 if ( m_isEnabled
== enable
)
637 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
640 (enable
? MF_ENABLED
: MF_GRAYED
));
644 wxLogLastError(wxT("EnableMenuItem"));
648 wxMenuItemBase::Enable(enable
);
651 void wxMenuItem::Check(bool check
)
653 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
655 if ( m_isChecked
== check
)
660 int flags
= check
? MF_CHECKED
: MF_UNCHECKED
;
661 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
663 if ( GetKind() == wxITEM_RADIO
)
665 // it doesn't make sense to uncheck a radio item -- what would this
670 // get the index of this item in the menu
671 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
672 int pos
= items
.IndexOf(this);
673 wxCHECK_RET( pos
!= wxNOT_FOUND
,
674 wxT("menuitem not found in the menu items list?") );
676 // get the radio group range
680 if ( m_isRadioGroupStart
)
682 // we already have all information we need
684 end
= m_radioGroup
.end
;
686 else // next radio group item
688 // get the radio group end from the start item
689 start
= m_radioGroup
.start
;
690 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
694 // calling CheckMenuRadioItem() with such parameters hangs my system
695 // (NT4 SP6) and I suspect this could happen to the others as well,
697 wxCHECK_RET( start
!= -1 && end
!= -1,
698 wxT("invalid ::CheckMenuRadioItem() parameter(s)") );
700 if ( !::CheckMenuRadioItem(hmenu
,
701 start
, // the first radio group item
703 pos
, // the one to check
706 wxLogLastError(wxT("CheckMenuRadioItem"));
710 // also uncheck all the other items in this radio group
711 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
712 for ( int n
= start
; n
<= end
&& node
; n
++ )
716 node
->GetData()->m_isChecked
= false;
719 node
= node
->GetNext();
724 if ( ::CheckMenuItem(hmenu
,
726 MF_BYCOMMAND
| flags
) == (DWORD
)-1 )
728 wxFAIL_MSG(wxT("CheckMenuItem() failed, item not in the menu?"));
733 wxMenuItemBase::Check(check
);
736 void wxMenuItem::SetItemLabel(const wxString
& txt
)
740 // don't do anything if label didn't change
744 // wxMenuItemBase will do stock ID checks
745 wxMenuItemBase::SetItemLabel(text
);
747 // the item can be not attached to any menu yet and SetItemLabel() is still
748 // valid to call in this case and should do nothing else
753 m_parentMenu
->UpdateAccel(this);
754 #endif // wxUSE_ACCEL
756 const UINT id
= GetMSWId();
757 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
758 if ( !hMenu
|| ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
) == (UINT
)-1 )
761 #if wxUSE_OWNER_DRAWN
762 if ( IsOwnerDrawn() )
764 // we don't need to do anything for owner drawn items, they will redraw
765 // themselves using the new text the next time they're displayed
768 #endif // owner drawn
770 // update the text of the native menu item
771 WinStruct
<MENUITEMINFO
> info
;
773 // surprisingly, calling SetMenuItemInfo() with just MIIM_STRING doesn't
774 // work as it resets the menu bitmap, so we need to first get the old item
775 // state and then modify it
776 const bool isLaterThanWin95
= wxGetWinVersion() > wxWinVersion_95
;
777 info
.fMask
= MIIM_STATE
|
782 if ( isLaterThanWin95
)
783 info
.fMask
|= MIIM_BITMAP
| MIIM_FTYPE
;
785 info
.fMask
|= MIIM_TYPE
;
786 if ( !::GetMenuItemInfo(hMenu
, id
, FALSE
, &info
) )
788 wxLogLastError(wxT("GetMenuItemInfo"));
792 if ( isLaterThanWin95
)
793 info
.fMask
|= MIIM_STRING
;
794 //else: MIIM_TYPE already specified
795 info
.dwTypeData
= (LPTSTR
)m_text
.wx_str();
796 info
.cch
= m_text
.length();
797 if ( !::SetMenuItemInfo(hMenu
, id
, FALSE
, &info
) )
799 wxLogLastError(wxT("SetMenuItemInfo"));
803 #if wxUSE_OWNER_DRAWN
805 int wxMenuItem::MeasureAccelWidth() const
807 wxString accel
= GetItemLabel().AfterFirst(wxT('\t'));
815 dc
.GetTextExtent(accel
, &w
, NULL
);
820 wxString
wxMenuItem::GetName() const
822 return GetItemLabelText();
825 bool wxMenuItem::OnMeasureItem(size_t *width
, size_t *height
)
827 const MenuDrawData
* data
= MenuDrawData::Get();
829 if ( IsOwnerDrawn() )
831 *width
= data
->ItemMargin
.GetTotalX();
832 *height
= data
->ItemMargin
.GetTotalY();
836 *width
+= data
->SeparatorSize
.cx
837 + data
->SeparatorMargin
.GetTotalX();
838 *height
+= data
->SeparatorSize
.cy
839 + data
->SeparatorMargin
.GetTotalY();
843 wxString str
= GetName();
851 dc
.GetTextExtent(str
, &w
, &h
);
853 *width
= data
->TextBorder
+ w
+ data
->AccelBorder
;
856 w
= m_parentMenu
->GetMaxAccelWidth();
858 *width
+= w
+ data
->ArrowBorder
;
860 *width
+= data
->Offset
;
861 *width
+= data
->ArrowMargin
.GetTotalX() + data
->ArrowSize
.cx
;
863 else // don't draw the text, just the bitmap (if any)
871 if ( IsOwnerDrawn() )
873 // width of menu icon with margins in ownerdrawn menu
874 // if any bitmap is not set, the width of space reserved for icon
875 // image is equal to the width of std check mark,
876 // if bitmap is set, then the width is set to the width of the widest
877 // bitmap in menu (GetMarginWidth()) unless std check mark is wider,
878 // then it's is set to std mark's width
879 int imgWidth
= wxMax(GetMarginWidth(), data
->CheckSize
.cx
)
880 + data
->CheckMargin
.GetTotalX();
882 *width
+= imgWidth
+ data
->CheckBgMargin
.GetTotalX();
885 if ( m_bmpChecked
.IsOk() || m_bmpChecked
.IsOk() )
887 // get size of bitmap always return valid value (0 for invalid bitmap),
888 // so we don't needed check if bitmap is valid ;)
889 size_t heightBmp
= wxMax(m_bmpChecked
.GetHeight(), m_bmpUnchecked
.GetHeight());
890 size_t widthtBmp
= wxMax(m_bmpChecked
.GetWidth(), m_bmpUnchecked
.GetWidth());
892 if ( IsOwnerDrawn() )
894 heightBmp
+= data
->CheckMargin
.GetTotalY();
898 // we must allocate enough space for the bitmap
902 // Is BMP height larger than text height?
903 if ( *height
< heightBmp
)
907 // make sure that this item is at least as tall as the system menu height
908 const size_t menuHeight
= data
->CheckMargin
.GetTotalY()
909 + data
->CheckSize
.cy
;
910 if (*height
< menuHeight
)
911 *height
= menuHeight
;
916 bool wxMenuItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
917 wxODAction
WXUNUSED(act
), wxODStatus stat
)
919 const MenuDrawData
* data
= MenuDrawData::Get();
921 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
922 HDC hdc
= GetHdcOf(*impl
);
925 wxCopyRectToRECT(rc
, rect
);
927 int imgWidth
= wxMax(GetMarginWidth(), data
->CheckSize
.cx
);
929 if ( IsOwnerDrawn() )
931 // font and colors to use
935 wxColour colText
, colBack
;
936 GetColourToUse(stat
, colText
, colBack
);
938 // calculate metrics of item parts
939 RECT rcSelection
= rect
;
940 data
->ItemMargin
.ApplyTo(rcSelection
);
942 RECT rcSeparator
= rcSelection
;
943 data
->SeparatorMargin
.ApplyTo(rcSeparator
);
945 RECT rcGutter
= rcSelection
;
946 rcGutter
.right
= data
->ItemMargin
.cxLeftWidth
947 + data
->CheckBgMargin
.cxLeftWidth
948 + data
->CheckMargin
.cxLeftWidth
950 + data
->CheckMargin
.cxRightWidth
951 + data
->CheckBgMargin
.cxRightWidth
;
953 RECT rcText
= rcSelection
;
954 rcText
.left
= rcGutter
.right
+ data
->TextBorder
;
956 // we draw the text label vertically centered, but this results in it
957 // being 1px too low compared to native menus for some reason, fix it
958 if ( data
->MenuLayout() != MenuDrawData::FullTheme
)
962 wxUxThemeEngine
* theme
= MenuDrawData::GetUxThemeEngine();
965 POPUPITEMSTATES state
;
966 if ( stat
& wxODDisabled
)
968 state
= (stat
& wxODSelected
) ? MPI_DISABLEDHOT
971 else if ( stat
& wxODSelected
)
980 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
982 if ( theme
->IsThemeBackgroundPartiallyTransparent(hTheme
,
983 MENU_POPUPITEM
, state
) )
985 theme
->DrawThemeBackground(hTheme
, hdc
,
986 MENU_POPUPBACKGROUND
,
990 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPGUTTER
,
995 rcSeparator
.left
= rcGutter
.right
;
996 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPSEPARATOR
,
997 0, &rcSeparator
, NULL
);
1001 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPITEM
,
1002 state
, &rcSelection
, NULL
);
1006 #endif // wxUSE_UXTHEME
1008 if ( IsSeparator() )
1010 DrawEdge(hdc
, &rcSeparator
, EDGE_ETCHED
, BF_TOP
);
1014 AutoHBRUSH
hbr(colBack
.GetPixel());
1015 SelectInHDC
selBrush(hdc
, hbr
);
1016 ::FillRect(hdc
, &rcSelection
, hbr
);
1021 // using native API because it recognizes '&'
1023 HDCTextColChanger
changeTextCol(hdc
, colText
.GetPixel());
1024 HDCBgColChanger
changeBgCol(hdc
, colBack
.GetPixel());
1025 HDCBgModeChanger
changeBgMode(hdc
, TRANSPARENT
);
1027 SelectInHDC
selFont(hdc
, GetHfontOf(font
));
1030 // item text name without mnemonic for calculating size
1031 wxString text
= GetName();
1034 ::GetTextExtentPoint32(hdc
, text
.c_str(), text
.length(), &textSize
);
1036 // item text name with mnemonic
1037 text
= GetItemLabel().BeforeFirst('\t');
1039 int flags
= DST_PREFIXTEXT
;
1040 // themes menu is using specified color for disabled labels
1041 if ( data
->MenuLayout() == MenuDrawData::Classic
&&
1042 (stat
& wxODDisabled
) && !(stat
& wxODSelected
) )
1043 flags
|= DSS_DISABLED
;
1045 if ( (stat
& wxODHidePrefix
) && !data
->AlwaysShowCues
)
1046 flags
|= DSS_HIDEPREFIX
;
1048 int x
= rcText
.left
;
1049 int y
= rcText
.top
+ (rcText
.bottom
- rcText
.top
- textSize
.cy
) / 2;
1051 ::DrawState(hdc
, NULL
, NULL
, (LPARAM
)text
.wx_str(),
1052 text
.length(), x
, y
, 0, 0, flags
);
1054 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
1055 // as the last parameter in DrawState() (at least with Windows98). So we have
1056 // to take care of right alignment ourselves.
1057 wxString accel
= GetItemLabel().AfterFirst(wxT('\t'));
1058 if ( !accel
.empty() )
1061 ::GetTextExtentPoint32(hdc
, accel
.c_str(), accel
.length(), &accelSize
);
1063 int flags
= DST_TEXT
;
1064 // themes menu is using specified color for disabled labels
1065 if ( data
->MenuLayout() == MenuDrawData::Classic
&&
1066 (stat
& wxODDisabled
) && !(stat
& wxODSelected
) )
1067 flags
|= DSS_DISABLED
;
1069 int x
= rcText
.right
- data
->ArrowMargin
.GetTotalX()
1070 - data
->ArrowSize
.cx
1071 - data
->ArrowBorder
;
1073 // right align accel on FullTheme menu, left otherwise
1074 if ( data
->MenuLayout() == MenuDrawData::FullTheme
)
1077 x
-= m_parentMenu
->GetMaxAccelWidth();
1079 int y
= rcText
.top
+ (rcText
.bottom
- rcText
.top
- accelSize
.cy
) / 2;
1081 ::DrawState(hdc
, NULL
, NULL
, (LPARAM
)accel
.wx_str(),
1082 accel
.length(), x
, y
, 0, 0, flags
);
1091 rect
.left
+ data
->ItemMargin
.cxLeftWidth
1092 + data
->CheckBgMargin
.cxLeftWidth
1093 + data
->CheckMargin
.cxLeftWidth
,
1094 rect
.top
+ data
->ItemMargin
.cyTopHeight
1095 + data
->CheckBgMargin
.cyTopHeight
1096 + data
->CheckMargin
.cyTopHeight
,
1097 rect
.left
+ data
->ItemMargin
.cxLeftWidth
1098 + data
->CheckBgMargin
.cxLeftWidth
1099 + data
->CheckMargin
.cxLeftWidth
1101 rect
.bottom
- data
->ItemMargin
.cyBottomHeight
1102 - data
->CheckBgMargin
.cyBottomHeight
1103 - data
->CheckMargin
.cyBottomHeight
);
1105 if ( IsCheckable() && !m_bmpChecked
.Ok() )
1107 if ( stat
& wxODChecked
)
1109 DrawStdCheckMark((WXHDC
)hdc
, &rcImg
, stat
);
1116 if ( stat
& wxODDisabled
)
1118 bmp
= GetDisabledBitmap();
1123 // for not checkable bitmaps we should always use unchecked one
1124 // because their checked bitmap is not set
1125 bmp
= GetBitmap(!IsCheckable() || (stat
& wxODChecked
));
1128 if ( bmp
.Ok() && stat
& wxODDisabled
)
1130 // we need to grey out the bitmap as we don't have any specific
1132 wxImage imgGrey
= bmp
.ConvertToImage().ConvertToGreyscale();
1134 bmp
= wxBitmap(imgGrey
);
1136 #endif // wxUSE_IMAGE
1141 wxMemoryDC
dcMem(&dc
);
1142 dcMem
.SelectObjectAsSource(bmp
);
1145 int nBmpWidth
= bmp
.GetWidth(),
1146 nBmpHeight
= bmp
.GetHeight();
1148 // there should be enough space!
1149 wxASSERT( nBmpWidth
<= imgWidth
&& nBmpHeight
<= (rcImg
.bottom
- rcImg
.top
) );
1151 int x
= rcImg
.left
+ (imgWidth
- nBmpWidth
) / 2;
1152 int y
= rcImg
.top
+ (rcImg
.bottom
- rcImg
.top
- nBmpHeight
) / 2;
1153 dc
.Blit(x
, y
, nBmpWidth
, nBmpHeight
, &dcMem
, 0, 0, wxCOPY
, true);
1164 // helper function for draw coloured check mark
1165 void DrawColorCheckMark(HDC hdc
, int x
, int y
, int cx
, int cy
, HDC hdcCheckMask
, int idxColor
)
1167 const COLORREF colBlack
= RGB(0, 0, 0);
1168 const COLORREF colWhite
= RGB(255, 255, 255);
1170 HDCTextColChanger
changeTextCol(hdc
, colBlack
);
1171 HDCBgColChanger
changeBgCol(hdc
, colWhite
);
1172 HDCBgModeChanger
changeBgMode(hdc
, TRANSPARENT
);
1174 // memory DC for color bitmap
1175 MemoryHDC
hdcMem(hdc
);
1176 CompatibleBitmap
hbmpMem(hdc
, cx
, cy
);
1177 SelectInHDC
selMem(hdcMem
, hbmpMem
);
1179 RECT rect
= { 0, 0, cx
, cy
};
1180 ::FillRect(hdcMem
, &rect
, ::GetSysColorBrush(idxColor
));
1182 const COLORREF colCheck
= ::GetSysColor(idxColor
);
1183 if ( colCheck
== colWhite
)
1185 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcCheckMask
, 0, 0, MERGEPAINT
);
1186 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcMem
, 0, 0, SRCAND
);
1190 if ( colCheck
!= colBlack
)
1192 const DWORD ROP_DSna
= 0x00220326; // dest = (NOT src) AND dest
1193 ::BitBlt(hdcMem
, 0, 0, cx
, cy
, hdcCheckMask
, 0, 0, ROP_DSna
);
1196 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcCheckMask
, 0, 0, SRCAND
);
1197 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcMem
, 0, 0, SRCPAINT
);
1201 } // anonymous namespace
1203 void wxMenuItem::DrawStdCheckMark(WXHDC hdc_
, const RECT
* rc
, wxODStatus stat
)
1205 HDC hdc
= (HDC
)hdc_
;
1208 wxUxThemeEngine
* theme
= MenuDrawData::GetUxThemeEngine();
1211 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
1213 const MenuDrawData
* data
= MenuDrawData::Get();
1215 // rect for background must be without check margins
1217 data
->CheckMargin
.UnapplyFrom(rcBg
);
1219 POPUPCHECKBACKGROUNDSTATES stateCheckBg
= (stat
& wxODDisabled
)
1223 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPCHECKBACKGROUND
,
1224 stateCheckBg
, &rcBg
, NULL
);
1226 POPUPCHECKSTATES stateCheck
;
1227 if ( GetKind() == wxITEM_CHECK
)
1229 stateCheck
= (stat
& wxODDisabled
) ? MC_CHECKMARKDISABLED
1230 : MC_CHECKMARKNORMAL
;
1234 stateCheck
= (stat
& wxODDisabled
) ? MC_BULLETDISABLED
1238 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPCHECK
,
1239 stateCheck
, rc
, NULL
);
1242 #endif // wxUSE_UXTHEME
1244 int cx
= rc
->right
- rc
->left
;
1245 int cy
= rc
->bottom
- rc
->top
;
1247 // first create mask of check mark
1248 MemoryHDC
hdcMask(hdc
);
1249 MonoBitmap
hbmpMask(cx
, cy
);
1250 SelectInHDC
selMask(hdcMask
,hbmpMask
);
1252 // then draw a check mark into it
1253 UINT stateCheck
= (GetKind() == wxITEM_CHECK
) ? DFCS_MENUCHECK
1255 RECT rect
= { 0, 0, cx
, cy
};
1256 ::DrawFrameControl(hdcMask
, &rect
, DFC_MENU
, stateCheck
);
1258 // first draw shadow if disabled
1259 if ( (stat
& wxODDisabled
) && !(stat
& wxODSelected
) )
1261 DrawColorCheckMark(hdc
, rc
->left
+ 1, rc
->top
+ 1,
1262 cx
, cy
, hdcMask
, COLOR_3DHILIGHT
);
1265 // then draw a check mark
1266 int color
= COLOR_MENUTEXT
;
1267 if ( stat
& wxODDisabled
)
1268 color
= COLOR_BTNSHADOW
;
1269 else if ( stat
& wxODSelected
)
1270 color
= COLOR_HIGHLIGHTTEXT
;
1272 DrawColorCheckMark(hdc
, rc
->left
, rc
->top
, cx
, cy
, hdcMask
, color
);
1276 void wxMenuItem::GetFontToUse(wxFont
& font
) const
1280 font
= MenuDrawData::Get()->Font
;
1283 void wxMenuItem::GetColourToUse(wxODStatus stat
, wxColour
& colText
, wxColour
& colBack
) const
1286 wxUxThemeEngine
* theme
= MenuDrawData::GetUxThemeEngine();
1289 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
1291 if ( stat
& wxODDisabled
)
1293 wxRGBToColour(colText
, theme
->GetThemeSysColor(hTheme
, COLOR_GRAYTEXT
));
1297 colText
= GetTextColour();
1298 if ( !colText
.IsOk() )
1299 wxRGBToColour(colText
, theme
->GetThemeSysColor(hTheme
, COLOR_MENUTEXT
));
1302 if ( stat
& wxODSelected
)
1304 wxRGBToColour(colBack
, theme
->GetThemeSysColor(hTheme
, COLOR_HIGHLIGHT
));
1308 colBack
= GetBackgroundColour();
1309 if ( !colBack
.IsOk() )
1310 wxRGBToColour(colBack
, theme
->GetThemeSysColor(hTheme
, COLOR_MENU
));
1314 #endif // wxUSE_UXTHEME
1316 wxOwnerDrawn::GetColourToUse(stat
, colText
, colBack
);
1319 #endif // wxUSE_OWNER_DRAWN
1321 // ----------------------------------------------------------------------------
1323 // ----------------------------------------------------------------------------
1325 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1327 const wxString
& name
,
1328 const wxString
& help
,
1332 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1335 #endif // wxUSE_MENUS