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/bitmap.h"
35 #include "wx/settings.h"
36 #include "wx/window.h"
38 #include "wx/string.h"
47 #include "wx/msw/private.h"
48 #include "wx/msw/dc.h"
51 // Implemented in menu.cpp
52 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
) ;
56 #include "wx/msw/uxtheme.h"
59 // ---------------------------------------------------------------------------
61 // ---------------------------------------------------------------------------
64 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
66 // ============================================================================
68 // ============================================================================
72 #include "wx/fontutil.h"
73 #include "wx/msw/private/metrics.h"
75 #ifndef SPI_GETKEYBOARDCUES
76 #define SPI_GETKEYBOARDCUES 0x100A
79 #ifndef DSS_HIDEPREFIX
80 #define DSS_HIDEPREFIX 0x0200
87 MENU_MENUITEM_TMSCHEMA
= 1,
88 MENU_SEPARATOR_TMSCHEMA
= 6,
89 MENU_POPUPBACKGROUND
= 9,
90 MENU_POPUPBORDERS
= 10,
92 MENU_POPUPCHECKBACKGROUND
= 12,
93 MENU_POPUPGUTTER
= 13,
95 MENU_POPUPSEPARATOR
= 15,
96 MENU_POPUPSUBMENU
= 16,
108 enum POPUPCHECKBACKGROUNDSTATES
115 enum POPUPCHECKSTATES
117 MC_CHECKMARKNORMAL
= 1,
118 MC_CHECKMARKDISABLED
= 2,
120 MC_BULLETDISABLED
= 4,
123 const int TMT_MENUFONT
= 803;
124 const int TMT_BORDERSIZE
= 2403;
125 const int TMT_CONTENTMARGINS
= 3602;
126 const int TMT_SIZINGMARGINS
= 3601;
128 #endif // wxUSE_UXTHEME
130 #endif // wxUSE_OWNER_DRAWN
132 // ----------------------------------------------------------------------------
133 // dynamic classes implementation
134 // ----------------------------------------------------------------------------
136 #if wxUSE_EXTENDED_RTTI
138 bool wxMenuItemStreamingCallback( const wxObject
*object
, wxWriter
* , wxPersister
* , wxxVariantArray
& )
140 const wxMenuItem
* mitem
= dynamic_cast<const wxMenuItem
*>(object
) ;
141 if ( mitem
->GetMenu() && !mitem
->GetMenu()->GetTitle().empty() )
143 // we don't stream out the first two items for menus with a title, they will be reconstructed
144 if ( mitem
->GetMenu()->FindItemByPosition(0) == mitem
|| mitem
->GetMenu()->FindItemByPosition(1) == mitem
)
150 wxBEGIN_ENUM( wxItemKind
)
151 wxENUM_MEMBER( wxITEM_SEPARATOR
)
152 wxENUM_MEMBER( wxITEM_NORMAL
)
153 wxENUM_MEMBER( wxITEM_CHECK
)
154 wxENUM_MEMBER( wxITEM_RADIO
)
155 wxEND_ENUM( wxItemKind
)
157 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem
, wxObject
,"wx/menuitem.h",wxMenuItemStreamingCallback
)
159 wxBEGIN_PROPERTIES_TABLE(wxMenuItem
)
160 wxPROPERTY( Parent
,wxMenu
*, SetMenu
, GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
161 wxPROPERTY( Id
,int, SetId
, GetId
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
162 wxPROPERTY( Text
, wxString
, SetText
, GetText
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
163 wxPROPERTY( Help
, wxString
, SetHelp
, GetHelp
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
164 wxREADONLY_PROPERTY( Kind
, wxItemKind
, GetKind
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
165 wxPROPERTY( SubMenu
,wxMenu
*, SetSubMenu
, GetSubMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
166 wxPROPERTY( Enabled
, bool , Enable
, IsEnabled
, wxxVariant((bool)true) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
167 wxPROPERTY( Checked
, bool , Check
, IsChecked
, wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
168 wxPROPERTY( Checkable
, bool , SetCheckable
, IsCheckable
, wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
169 wxEND_PROPERTIES_TABLE()
171 wxBEGIN_HANDLERS_TABLE(wxMenuItem
)
172 wxEND_HANDLERS_TABLE()
174 wxDIRECT_CONSTRUCTOR_6( wxMenuItem
, wxMenu
* , Parent
, int , Id
, wxString
, Text
, wxString
, Help
, wxItemKind
, Kind
, wxMenu
* , SubMenu
)
176 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 #if wxUSE_OWNER_DRAWN
188 // helper class to keep information about metrics and other stuff
189 // needed for measuring and drawing menu item
209 Margins ItemMargin
; // popup item margins
211 Margins CheckMargin
; // popup check margins
212 Margins CheckBgMargin
; // popup check background margins
214 Margins SeparatorMargin
; // popup separator margins
216 SIZE CheckSize
; // popup check size metric
217 SIZE SeparatorSize
; // popup separator size metric
219 int AccelBorder
; // popup border space between
220 // item text and accelerator
221 int TextBorder
; // popup border space between
222 // item text and gutter
224 wxFont Font
; // default menu font
226 bool AlwaysShowCues
; // must keyboard cues always be shown?
228 bool Theme
; // is data initialized for FullTheme?
230 static const MenuDrawData
* Get()
233 bool theme
= MenuLayout() == FullTheme
;
234 if ( ms_instance
->Theme
!= theme
)
236 #endif // wxUSE_UXTHEME
247 // get the theme engine or NULL if themes
248 // are not available or not supported on menu
249 static wxUxThemeEngine
*GetUxThemeEngine()
252 if ( MenuLayout() == FullTheme
)
253 return wxUxThemeEngine::GetIfActive();
254 #endif // wxUSE_UXTHEME
261 FullTheme
, // full menu themes (Vista or new)
262 PseudoTheme
, // pseudo menu themes (on XP)
266 static MenuLayoutType
MenuLayout()
268 MenuLayoutType menu
= Classic
;
270 if ( wxUxThemeEngine::GetIfActive() != NULL
)
272 static wxWinVersion ver
= wxGetWinVersion();
273 if ( ver
>= wxWinVersion_Vista
)
275 else if ( ver
== wxWinVersion_XP
)
278 #endif // wxUSE_UXTHEME
285 static MenuDrawData
* ms_instance
;
288 MenuDrawData
* MenuDrawData::ms_instance
= NULL
;
290 MenuDrawData s_menuData
;
292 void MenuDrawData::Init()
295 wxUxThemeEngine
* theme
= GetUxThemeEngine();
298 wxWindow
* window
= static_cast<wxApp
*>(wxApp::GetInstance())->GetTopWindow();
299 wxUxThemeHandle
hTheme(window
, L
"MENU");
301 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPITEM
, 0,
302 TMT_CONTENTMARGINS
, NULL
,
303 reinterpret_cast<MARGINS
*>(&ItemMargin
));
305 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPCHECK
, 0,
306 TMT_CONTENTMARGINS
, NULL
,
307 reinterpret_cast<MARGINS
*>(&CheckMargin
));
308 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPCHECKBACKGROUND
, 0,
309 TMT_CONTENTMARGINS
, NULL
,
310 reinterpret_cast<MARGINS
*>(&CheckBgMargin
));
312 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPSEPARATOR
, 0,
313 TMT_SIZINGMARGINS
, NULL
,
314 reinterpret_cast<MARGINS
*>(&SeparatorMargin
));
316 theme
->GetThemePartSize(hTheme
, NULL
, MENU_POPUPCHECK
, 0,
317 NULL
, TS_TRUE
, &CheckSize
);
319 theme
->GetThemePartSize(hTheme
, NULL
, MENU_POPUPSEPARATOR
, 0,
320 NULL
, TS_TRUE
, &SeparatorSize
);
322 theme
->GetThemeInt(hTheme
, MENU_POPUPBORDERS
, 0, TMT_BORDERSIZE
, &AccelBorder
);
323 theme
->GetThemeInt(hTheme
, MENU_POPUPBACKGROUND
, 0, TMT_BORDERSIZE
, &TextBorder
);
325 wxNativeFontInfo fontInfo
;
326 theme
->GetThemeSysFont(hTheme
, TMT_MENUFONT
, &fontInfo
.lf
);
327 Font
= wxFont(fontInfo
);
331 // native menu doesn't uses the vertical margins
332 ItemMargin
.top
= ItemMargin
.bottom
= 0;
334 // native menu uses small top margin for separator
335 if ( SeparatorMargin
.top
>= 2 )
336 SeparatorMargin
.top
-= 2;
339 #endif // wxUSE_UXTHEME
341 const NONCLIENTMETRICS
& metrics
= wxMSWImpl::GetNonClientMetrics();
343 ItemMargin
= Margins();
346 CheckMargin
.right
= ::GetSystemMetrics(SM_CXEDGE
);
348 CheckMargin
.bottom
= ::GetSystemMetrics(SM_CYEDGE
);
350 CheckBgMargin
= Margins();
352 CheckSize
.cx
= ::GetSystemMetrics(SM_CXMENUCHECK
);
353 CheckSize
.cy
= ::GetSystemMetrics(SM_CYMENUCHECK
);
355 // separator height with margins
356 int sepFullSize
= metrics
.iMenuHeight
/ 2;
358 SeparatorMargin
.left
=
359 SeparatorMargin
.right
= 1;
360 SeparatorMargin
.top
=
361 SeparatorMargin
.bottom
= sepFullSize
/ 2 - 1;
363 SeparatorSize
.cx
= 1;
364 SeparatorSize
.cy
= sepFullSize
- SeparatorMargin
.top
- SeparatorMargin
.bottom
;
369 Font
= wxFont(wxNativeFontInfo(metrics
.lfMenuFont
));
375 if ( ::SystemParametersInfo(SPI_GETKEYBOARDCUES
, 0, &value
, 0) == 0 )
377 // if it's not supported, we must be on an old Windows version
378 // which always shows them
382 AlwaysShowCues
= value
== 1;
386 } // anonymous namespace
388 #endif // wxUSE_OWNER_DRAWN
394 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
396 const wxString
& text
,
397 const wxString
& strHelp
,
400 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
405 #if WXWIN_COMPATIBILITY_2_8
406 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
408 const wxString
& text
,
409 const wxString
& help
,
412 : wxMenuItemBase(parentMenu
, id
, text
, help
,
413 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
419 void wxMenuItem::Init()
421 m_radioGroup
.start
= -1;
422 m_isRadioGroupStart
= false;
424 #if wxUSE_OWNER_DRAWN
426 // when the color is not valid, wxOwnerDraw takes the default ones.
427 // If we set the colors here and they are changed by the user during
428 // the execution, then the colors are not updated until the application
429 // is restarted and our menus look bad
430 SetTextColour(wxNullColour
);
431 SetBackgroundColour(wxNullColour
);
433 // setting default colors switched ownerdraw on: switch it off again
434 SetOwnerDrawn(false);
436 // switch ownerdraw back on if using a non default margin
437 if ( !IsSeparator() )
438 SetMarginWidth(GetMarginWidth());
440 #endif // wxUSE_OWNER_DRAWN
443 wxMenuItem::~wxMenuItem()
450 // return the id for calling Win32 API functions
451 WXWPARAM
wxMenuItem::GetMSWId() const
453 // we must use ids in unsigned short range with Windows functions, if we
454 // pass ids > USHRT_MAX to them they get very confused (e.g. start
455 // generating WM_COMMAND messages with negative high word of wParam), so
456 // use the cast to ensure the id is in range
457 return m_subMenu
? wxPtrToUInt(m_subMenu
->GetHMenu())
458 : static_cast<unsigned short>(GetId());
464 bool wxMenuItem::IsChecked() const
466 // fix that RTTI is always getting the correct state (separators cannot be
467 // checked, but the Windows call below returns true
471 // the item might not be attached to a menu yet
473 // TODO: shouldn't we just always call the base class version? It seems
474 // like it ought to always be in sync
476 return wxMenuItemBase::IsChecked();
478 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
479 int flag
= ::GetMenuState(hmenu
, GetMSWId(), MF_BYCOMMAND
);
481 return (flag
& MF_CHECKED
) != 0;
487 void wxMenuItem::SetAsRadioGroupStart()
489 m_isRadioGroupStart
= true;
492 void wxMenuItem::SetRadioGroupStart(int start
)
494 wxASSERT_MSG( !m_isRadioGroupStart
,
495 wxT("should only be called for the next radio items") );
497 m_radioGroup
.start
= start
;
500 void wxMenuItem::SetRadioGroupEnd(int end
)
502 wxASSERT_MSG( m_isRadioGroupStart
,
503 wxT("should only be called for the first radio item") );
505 m_radioGroup
.end
= end
;
511 void wxMenuItem::Enable(bool enable
)
513 if ( m_isEnabled
== enable
)
518 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
521 (enable
? MF_ENABLED
: MF_GRAYED
));
525 wxLogLastError(wxT("EnableMenuItem"));
529 wxMenuItemBase::Enable(enable
);
532 void wxMenuItem::Check(bool check
)
534 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
536 if ( m_isChecked
== check
)
541 int flags
= check
? MF_CHECKED
: MF_UNCHECKED
;
542 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
544 if ( GetKind() == wxITEM_RADIO
)
546 // it doesn't make sense to uncheck a radio item -- what would this
551 // get the index of this item in the menu
552 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
553 int pos
= items
.IndexOf(this);
554 wxCHECK_RET( pos
!= wxNOT_FOUND
,
555 wxT("menuitem not found in the menu items list?") );
557 // get the radio group range
561 if ( m_isRadioGroupStart
)
563 // we already have all information we need
565 end
= m_radioGroup
.end
;
567 else // next radio group item
569 // get the radio group end from the start item
570 start
= m_radioGroup
.start
;
571 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
575 // calling CheckMenuRadioItem() with such parameters hangs my system
576 // (NT4 SP6) and I suspect this could happen to the others as well,
578 wxCHECK_RET( start
!= -1 && end
!= -1,
579 wxT("invalid ::CheckMenuRadioItem() parameter(s)") );
581 if ( !::CheckMenuRadioItem(hmenu
,
582 start
, // the first radio group item
584 pos
, // the one to check
587 wxLogLastError(wxT("CheckMenuRadioItem"));
591 // also uncheck all the other items in this radio group
592 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
593 for ( int n
= start
; n
<= end
&& node
; n
++ )
597 node
->GetData()->m_isChecked
= false;
600 node
= node
->GetNext();
605 if ( ::CheckMenuItem(hmenu
,
607 MF_BYCOMMAND
| flags
) == (DWORD
)-1 )
609 wxFAIL_MSG(wxT("CheckMenuItem() failed, item not in the menu?"));
614 wxMenuItemBase::Check(check
);
617 void wxMenuItem::SetItemLabel(const wxString
& txt
)
621 // don't do anything if label didn't change
625 // wxMenuItemBase will do stock ID checks
626 wxMenuItemBase::SetItemLabel(text
);
628 // the item can be not attached to any menu yet and SetItemLabel() is still
629 // valid to call in this case and should do nothing else
634 m_parentMenu
->UpdateAccel(this);
635 #endif // wxUSE_ACCEL
637 const UINT id
= GetMSWId();
638 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
639 if ( !hMenu
|| ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
) == (UINT
)-1 )
642 #if wxUSE_OWNER_DRAWN
643 if ( IsOwnerDrawn() )
645 // we don't need to do anything for owner drawn items, they will redraw
646 // themselves using the new text the next time they're displayed
649 #endif // owner drawn
651 // update the text of the native menu item
652 WinStruct
<MENUITEMINFO
> info
;
654 // surprisingly, calling SetMenuItemInfo() with just MIIM_STRING doesn't
655 // work as it resets the menu bitmap, so we need to first get the old item
656 // state and then modify it
657 const bool isLaterThanWin95
= wxGetWinVersion() > wxWinVersion_95
;
658 info
.fMask
= MIIM_STATE
|
663 if ( isLaterThanWin95
)
664 info
.fMask
|= MIIM_BITMAP
| MIIM_FTYPE
;
666 info
.fMask
|= MIIM_TYPE
;
667 if ( !::GetMenuItemInfo(hMenu
, id
, FALSE
, &info
) )
669 wxLogLastError(wxT("GetMenuItemInfo"));
673 if ( isLaterThanWin95
)
674 info
.fMask
|= MIIM_STRING
;
675 //else: MIIM_TYPE already specified
676 info
.dwTypeData
= (LPTSTR
)m_text
.wx_str();
677 info
.cch
= m_text
.length();
678 if ( !::SetMenuItemInfo(hMenu
, id
, FALSE
, &info
) )
680 wxLogLastError(wxT("SetMenuItemInfo"));
684 #if wxUSE_OWNER_DRAWN
686 wxString
wxMenuItem::GetName() const
688 return GetItemLabelText();
691 bool wxMenuItem::OnMeasureItem(size_t *width
, size_t *height
)
693 const MenuDrawData
* data
= MenuDrawData::Get();
695 if ( IsOwnerDrawn() )
697 *width
= data
->ItemMargin
.left
+ data
->ItemMargin
.right
;
698 *height
= data
->ItemMargin
.top
+ data
->ItemMargin
.bottom
;
702 *width
+= data
->SeparatorSize
.cx
703 + data
->SeparatorMargin
.left
+ data
->SeparatorMargin
.right
;
704 *height
+= data
->SeparatorSize
.cy
705 + data
->SeparatorMargin
.top
+ data
->SeparatorMargin
.bottom
;
709 wxString str
= GetItemLabel();
711 // text and accel separator char removal
712 str
.Replace(wxT('\t'), wxEmptyString
);
720 dc
.GetTextExtent(str
, &w
, &h
);
722 *width
= w
+ data
->TextBorder
+ data
->AccelBorder
;
725 // system added space at the end of the menu for the submenu expansion
726 // arrow, but we must add a 4-pixel separator for better apperance
729 else // don't draw the text, just the bitmap (if any)
737 if ( IsOwnerDrawn() )
739 // width of menu icon in ownerdrawn menu
740 // if any bitmap is not set, the width of space reserved for icon
741 // image is equal to the width of std check mark,
742 // if bitmap is set, then the width is set to the width of the widest
743 // bitmap in menu (GetMarginWidth()) unless std check mark is wider,
744 // then it's is set to std mark's width
745 int imgWidth
= wxMax(GetMarginWidth(), data
->CheckSize
.cx
)
746 + data
->CheckMargin
.left
+ data
->CheckMargin
.right
;
748 *width
+= imgWidth
+ data
->CheckBgMargin
.left
+ data
->CheckBgMargin
.right
;
751 if ( m_bmpChecked
.IsOk() || m_bmpChecked
.IsOk() )
753 // get size of bitmap always return valid value (0 for invalid bitmap),
754 // so we don't needed check if bitmap is valid ;)
755 size_t heightBmp
= wxMax(m_bmpChecked
.GetHeight(), m_bmpUnchecked
.GetHeight());
756 size_t widthtBmp
= wxMax(m_bmpChecked
.GetWidth(), m_bmpUnchecked
.GetWidth());
758 if ( IsOwnerDrawn() )
760 heightBmp
+= data
->CheckMargin
.top
+ data
->CheckMargin
.bottom
;
764 // we must allocate enough space for the bitmap
768 // Is BMP height larger than text height?
769 if ( *height
< heightBmp
)
773 // make sure that this item is at least as tall as the system menu height
774 const size_t menuHeight
= data
->CheckMargin
.top
+ data
->CheckMargin
.bottom
775 + data
->CheckSize
.cy
;
776 if (*height
< menuHeight
)
777 *height
= menuHeight
;
782 bool wxMenuItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
783 wxODAction
WXUNUSED(act
), wxODStatus stat
)
785 const MenuDrawData
* data
= MenuDrawData::Get();
787 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
788 HDC hdc
= GetHdcOf(*impl
);
791 wxCopyRectToRECT(rc
, rect
);
793 int imgWidth
= wxMax(GetMarginWidth(), data
->CheckSize
.cx
)
794 + data
->CheckMargin
.left
+ data
->CheckMargin
.right
;
796 if ( IsOwnerDrawn() )
798 // font and colors to use
802 wxColour colText1
, colBack1
;
803 GetColourToUse(stat
, colText1
, colBack1
);
805 DWORD colText
= wxColourToPalRGB(colText1
);
806 DWORD colBack
= wxColourToPalRGB(colBack1
);
808 // calculate metrics of item parts
814 SetRect(&rcSelection
,
815 rect
.left
+ data
->ItemMargin
.left
,
816 rect
.top
+ data
->ItemMargin
.top
,
817 rect
.right
- data
->ItemMargin
.right
,
818 rect
.bottom
- data
->ItemMargin
.bottom
);
820 SetRect(&rcSeparator
,
821 rcSelection
.left
+ data
->SeparatorMargin
.left
,
822 rcSelection
.top
+ data
->SeparatorMargin
.top
,
823 rcSelection
.right
- data
->SeparatorMargin
.right
,
824 rcSelection
.bottom
- data
->SeparatorMargin
.bottom
);
826 CopyRect(&rcGutter
, &rcSelection
);
827 rcGutter
.right
= data
->ItemMargin
.left
828 + data
->CheckBgMargin
.left
830 + data
->CheckBgMargin
.right
;
832 CopyRect(&rcText
, &rcSelection
);
833 rcText
.left
= rcGutter
.right
+ data
->TextBorder
;
836 wxUxThemeEngine
* theme
= MenuDrawData::GetUxThemeEngine();
839 POPUPITEMSTATES state
;
840 if ( stat
& wxODDisabled
)
842 state
= (stat
& wxODSelected
) ? MPI_DISABLEDHOT
845 else if ( stat
& wxODSelected
)
854 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
856 if ( theme
->IsThemeBackgroundPartiallyTransparent(hTheme
,
857 MENU_POPUPITEM
, state
) )
859 theme
->DrawThemeBackground(hTheme
, hdc
,
860 MENU_POPUPBACKGROUND
,
864 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPGUTTER
,
869 rcSeparator
.left
= rcGutter
.right
;
870 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPSEPARATOR
,
871 0, &rcSeparator
, NULL
);
875 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPITEM
,
876 state
, &rcSelection
, NULL
);
880 #endif // wxUSE_UXTHEME
884 DrawEdge(hdc
, &rcSeparator
, EDGE_ETCHED
, BF_TOP
);
888 AutoHBRUSH
hbr(colBack
);
889 SelectInHDC
selBrush(hdc
, hbr
);
890 ::FillRect(hdc
, &rcSelection
, hbr
);
895 // using native API because it recognizes '&'
897 COLORREF colOldText
= ::SetTextColor(hdc
, colText
);
898 COLORREF colOldBack
= ::SetBkColor(hdc
, colBack
);
900 int prevMode
= SetBkMode(hdc
, TRANSPARENT
);
902 SelectInHDC
selFont(hdc
, GetHfontOf(font
));
905 // item text name without menemonic for calculating size
906 wxString text
= GetName();
909 ::GetTextExtentPoint32(hdc
, text
.c_str(), text
.length(), &textSize
);
911 // item text name with menemonic
912 text
= GetItemLabel().BeforeFirst('\t');
914 int flags
= DST_PREFIXTEXT
;
915 // themes menu is using specified color for disabled labels
916 if ( data
->MenuLayout() == MenuDrawData::Classic
&&
917 (stat
& wxODDisabled
) && !(stat
& wxODSelected
) )
918 flags
|= DSS_DISABLED
;
920 if ( (stat
& wxODHidePrefix
) && !data
->AlwaysShowCues
)
921 flags
|= DSS_HIDEPREFIX
;
924 int y
= rcText
.top
+ (rcText
.bottom
- rcText
.top
- textSize
.cy
) / 2;
926 ::DrawState(hdc
, NULL
, NULL
, (LPARAM
)text
.wx_str(),
927 text
.length(), x
, y
, 0, 0, flags
);
929 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
930 // as the last parameter in DrawState() (at least with Windows98). So we have
931 // to take care of right alignment ourselves.
932 wxString accel
= GetItemLabel().AfterFirst(wxT('\t'));
933 if ( !accel
.empty() )
936 ::GetTextExtentPoint32(hdc
, accel
.c_str(), accel
.length(), &accelSize
);
938 int flags
= DST_TEXT
;
939 // themes menu is using specified color for disabled labels
940 if ( data
->MenuLayout() == MenuDrawData::Classic
&&
941 (stat
& wxODDisabled
) && !(stat
& wxODSelected
) )
942 flags
|= DSS_DISABLED
;
944 // right align accel string with right edge of menu
945 // (offset by the margin width)
947 int x
= rcText
.right
- 16 - accelSize
.cx
;
948 int y
= rcText
.top
+ (rcText
.bottom
- rcText
.top
- accelSize
.cy
) / 2;
950 ::DrawState(hdc
, NULL
, NULL
, (LPARAM
)accel
.wx_str(),
951 accel
.length(), x
, y
, 0, 0, flags
);
954 ::SetBkMode(hdc
, prevMode
);
955 ::SetBkColor(hdc
, colOldBack
);
956 ::SetTextColor(hdc
, colOldText
);
964 rect
.left
+ data
->ItemMargin
.left
+ data
->CheckBgMargin
.left
,
965 rect
.top
+ data
->ItemMargin
.top
+ data
->CheckBgMargin
.top
,
966 rect
.left
+ data
->ItemMargin
.left
+ data
->CheckBgMargin
.left
968 rect
.bottom
- data
->ItemMargin
.bottom
- data
->CheckBgMargin
.bottom
);
970 if ( IsCheckable() && !m_bmpChecked
.Ok() )
972 if ( stat
& wxODChecked
)
975 wxUxThemeEngine
* theme
= MenuDrawData::GetUxThemeEngine();
978 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
980 POPUPCHECKBACKGROUNDSTATES stateCheckBg
= (stat
& wxODDisabled
)
984 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPCHECKBACKGROUND
,
985 stateCheckBg
, &rcImg
, NULL
);
987 // check mark will be drawn centered on the background
989 POPUPCHECKSTATES stateCheck
= (stat
& wxODDisabled
)
990 ? MC_CHECKMARKDISABLED
991 : MC_CHECKMARKNORMAL
;
993 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPCHECK
,
994 stateCheck
, &rcImg
, NULL
);
997 #endif // wxUSE_UXTHEME
1000 int cy
= rcImg
.bottom
- rcImg
.top
;
1002 // what goes on: DrawFrameControl creates a b/w mask,
1003 // then we copy it to screen to have right colors
1005 // first create a monochrome bitmap in a memory DC
1006 HDC hdcMem
= ::CreateCompatibleDC(hdc
);
1007 HBITMAP hbmpCheck
= ::CreateBitmap(cx
, cy
, 1, 1, 0);
1008 ::SelectObject(hdcMem
, hbmpCheck
);
1010 // then draw a check mark into it
1011 RECT rect
= { 0, 0, cx
, cy
};
1012 if ( rc
.GetHeight() > 0 )
1014 ::DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
1017 // finally copy it to screen DC and clean up
1018 ::BitBlt(hdc
, rcImg
.left
, rcImg
.top
, cx
, cy
, hdcMem
, 0, 0, SRCCOPY
);
1028 if ( stat
& wxODDisabled
)
1030 bmp
= GetDisabledBitmap();
1035 // for not checkable bitmaps we should always use unchecked one
1036 // because their checked bitmap is not set
1037 bmp
= GetBitmap(!IsCheckable() || (stat
& wxODChecked
));
1040 if ( bmp
.Ok() && stat
& wxODDisabled
)
1042 // we need to grey out the bitmap as we don't have any specific
1044 wxImage imgGrey
= bmp
.ConvertToImage().ConvertToGreyscale();
1046 bmp
= wxBitmap(imgGrey
);
1048 #endif // wxUSE_IMAGE
1053 wxMemoryDC
dcMem(&dc
);
1054 dcMem
.SelectObjectAsSource(bmp
);
1057 int nBmpWidth
= bmp
.GetWidth(),
1058 nBmpHeight
= bmp
.GetHeight();
1060 // there should be enough space!
1061 wxASSERT( nBmpWidth
<= imgWidth
&& nBmpHeight
<= (rcImg
.bottom
- rcImg
.top
) );
1063 int x
= rcImg
.left
+ (imgWidth
- nBmpWidth
) / 2;
1064 int y
= rcImg
.top
+ (rcImg
.bottom
- rcImg
.top
- nBmpHeight
) / 2;
1065 dc
.Blit(x
, y
, nBmpWidth
, nBmpHeight
, &dcMem
, 0, 0, wxCOPY
, true);
1073 void wxMenuItem::GetFontToUse(wxFont
& font
) const
1077 font
= MenuDrawData::Get()->Font
;
1080 void wxMenuItem::GetColourToUse(wxODStatus stat
, wxColour
& colText
, wxColour
& colBack
) const
1083 wxUxThemeEngine
* theme
= MenuDrawData::GetUxThemeEngine();
1086 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
1088 if ( stat
& wxODDisabled
)
1090 wxRGBToColour(colText
, theme
->GetThemeSysColor(hTheme
, COLOR_GRAYTEXT
));
1094 colText
= GetTextColour();
1095 if ( !colText
.IsOk() )
1096 wxRGBToColour(colText
, theme
->GetThemeSysColor(hTheme
, COLOR_MENUTEXT
));
1099 if ( stat
& wxODSelected
)
1101 wxRGBToColour(colBack
, theme
->GetThemeSysColor(hTheme
, COLOR_HIGHLIGHT
));
1105 colBack
= GetBackgroundColour();
1106 if ( !colBack
.IsOk() )
1107 wxRGBToColour(colBack
, theme
->GetThemeSysColor(hTheme
, COLOR_MENU
));
1111 #endif // wxUSE_UXTHEME
1113 wxOwnerDrawn::GetColourToUse(stat
, colText
, colBack
);
1116 #endif // wxUSE_OWNER_DRAWN
1118 // ----------------------------------------------------------------------------
1120 // ----------------------------------------------------------------------------
1122 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1124 const wxString
& name
,
1125 const wxString
& help
,
1129 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1132 #endif // wxUSE_MENUS