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 // ============================================================================
70 // ============================================================================
74 #include "wx/fontutil.h"
75 #include "wx/msw/private/metrics.h"
77 #ifndef SPI_GETKEYBOARDCUES
78 #define SPI_GETKEYBOARDCUES 0x100A
81 #ifndef DSS_HIDEPREFIX
82 #define DSS_HIDEPREFIX 0x0200
89 MENU_MENUITEM_TMSCHEMA
= 1,
90 MENU_SEPARATOR_TMSCHEMA
= 6,
91 MENU_POPUPBACKGROUND
= 9,
92 MENU_POPUPBORDERS
= 10,
94 MENU_POPUPCHECKBACKGROUND
= 12,
95 MENU_POPUPGUTTER
= 13,
97 MENU_POPUPSEPARATOR
= 15,
98 MENU_POPUPSUBMENU
= 16,
110 enum POPUPCHECKBACKGROUNDSTATES
117 enum POPUPCHECKSTATES
119 MC_CHECKMARKNORMAL
= 1,
120 MC_CHECKMARKDISABLED
= 2,
122 MC_BULLETDISABLED
= 4,
125 const int TMT_MENUFONT
= 803;
126 const int TMT_BORDERSIZE
= 2403;
127 const int TMT_CONTENTMARGINS
= 3602;
128 const int TMT_SIZINGMARGINS
= 3601;
130 #endif // wxUSE_UXTHEME
132 #endif // wxUSE_OWNER_DRAWN
134 // ----------------------------------------------------------------------------
135 // dynamic classes implementation
136 // ----------------------------------------------------------------------------
138 #if wxUSE_EXTENDED_RTTI
140 bool wxMenuItemStreamingCallback( const wxObject
*object
, wxWriter
* , wxPersister
* , wxxVariantArray
& )
142 const wxMenuItem
* mitem
= dynamic_cast<const wxMenuItem
*>(object
) ;
143 if ( mitem
->GetMenu() && !mitem
->GetMenu()->GetTitle().empty() )
145 // we don't stream out the first two items for menus with a title, they will be reconstructed
146 if ( mitem
->GetMenu()->FindItemByPosition(0) == mitem
|| mitem
->GetMenu()->FindItemByPosition(1) == mitem
)
152 wxBEGIN_ENUM( wxItemKind
)
153 wxENUM_MEMBER( wxITEM_SEPARATOR
)
154 wxENUM_MEMBER( wxITEM_NORMAL
)
155 wxENUM_MEMBER( wxITEM_CHECK
)
156 wxENUM_MEMBER( wxITEM_RADIO
)
157 wxEND_ENUM( wxItemKind
)
159 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem
, wxObject
,"wx/menuitem.h",wxMenuItemStreamingCallback
)
161 wxBEGIN_PROPERTIES_TABLE(wxMenuItem
)
162 wxPROPERTY( Parent
,wxMenu
*, SetMenu
, GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
163 wxPROPERTY( Id
,int, SetId
, GetId
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
164 wxPROPERTY( Text
, wxString
, SetText
, GetText
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
165 wxPROPERTY( Help
, wxString
, SetHelp
, GetHelp
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
166 wxREADONLY_PROPERTY( Kind
, wxItemKind
, GetKind
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
167 wxPROPERTY( SubMenu
,wxMenu
*, SetSubMenu
, GetSubMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
168 wxPROPERTY( Enabled
, bool , Enable
, IsEnabled
, wxxVariant((bool)true) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
169 wxPROPERTY( Checked
, bool , Check
, IsChecked
, wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
170 wxPROPERTY( Checkable
, bool , SetCheckable
, IsCheckable
, wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
171 wxEND_PROPERTIES_TABLE()
173 wxBEGIN_HANDLERS_TABLE(wxMenuItem
)
174 wxEND_HANDLERS_TABLE()
176 wxDIRECT_CONSTRUCTOR_6( wxMenuItem
, wxMenu
* , Parent
, int , Id
, wxString
, Text
, wxString
, Help
, wxItemKind
, Kind
, wxMenu
* , SubMenu
)
178 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 #if wxUSE_OWNER_DRAWN
190 // helper class to keep information about metrics and other stuff
191 // needed for measuring and drawing menu item
211 Margins ItemMargin
; // popup item margins
213 Margins CheckMargin
; // popup check margins
214 Margins CheckBgMargin
; // popup check background margins
216 Margins ArrowMargin
; // popup submenu arrow margins
218 Margins SeparatorMargin
; // popup separator margins
220 SIZE CheckSize
; // popup check size metric
221 SIZE ArrowSize
; // popup submenu arrow size metric
222 SIZE SeparatorSize
; // popup separator size metric
224 int TextBorder
; // popup border space between
225 // item text and gutter
227 int AccelBorder
; // popup border space between
228 // item text and accelerator
230 int ArrowBorder
; // popup border space between
231 // item accelerator and submenu arrow
233 int Offset
; // system added space at the end of the menu,
234 // add this offset for remove the extra space
236 wxFont Font
; // default menu font
238 bool AlwaysShowCues
; // must keyboard cues always be shown?
240 bool Theme
; // is data initialized for FullTheme?
242 static const MenuDrawData
* Get()
245 bool theme
= MenuLayout() == FullTheme
;
246 if ( ms_instance
->Theme
!= theme
)
248 #endif // wxUSE_UXTHEME
259 // get the theme engine or NULL if themes
260 // are not available or not supported on menu
261 static wxUxThemeEngine
*GetUxThemeEngine()
264 if ( MenuLayout() == FullTheme
)
265 return wxUxThemeEngine::GetIfActive();
266 #endif // wxUSE_UXTHEME
273 FullTheme
, // full menu themes (Vista or new)
274 PseudoTheme
, // pseudo menu themes (on XP)
278 static MenuLayoutType
MenuLayout()
280 MenuLayoutType menu
= Classic
;
282 if ( wxUxThemeEngine::GetIfActive() != NULL
)
284 static wxWinVersion ver
= wxGetWinVersion();
285 if ( ver
>= wxWinVersion_Vista
)
287 else if ( ver
== wxWinVersion_XP
)
290 #endif // wxUSE_UXTHEME
297 static MenuDrawData
* ms_instance
;
300 MenuDrawData
* MenuDrawData::ms_instance
= NULL
;
302 MenuDrawData s_menuData
;
304 void MenuDrawData::Init()
307 wxUxThemeEngine
* theme
= GetUxThemeEngine();
310 wxWindow
* window
= static_cast<wxApp
*>(wxApp::GetInstance())->GetTopWindow();
311 wxUxThemeHandle
hTheme(window
, L
"MENU");
313 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPITEM
, 0,
314 TMT_CONTENTMARGINS
, NULL
,
315 reinterpret_cast<MARGINS
*>(&ItemMargin
));
317 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPCHECK
, 0,
318 TMT_CONTENTMARGINS
, NULL
,
319 reinterpret_cast<MARGINS
*>(&CheckMargin
));
320 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPCHECKBACKGROUND
, 0,
321 TMT_CONTENTMARGINS
, NULL
,
322 reinterpret_cast<MARGINS
*>(&CheckBgMargin
));
324 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPSUBMENU
, 0,
325 TMT_CONTENTMARGINS
, NULL
,
326 reinterpret_cast<MARGINS
*>(&ArrowMargin
));
328 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPSEPARATOR
, 0,
329 TMT_SIZINGMARGINS
, NULL
,
330 reinterpret_cast<MARGINS
*>(&SeparatorMargin
));
332 theme
->GetThemePartSize(hTheme
, NULL
, MENU_POPUPCHECK
, 0,
333 NULL
, TS_TRUE
, &CheckSize
);
335 theme
->GetThemePartSize(hTheme
, NULL
, MENU_POPUPSUBMENU
, 0,
336 NULL
, TS_TRUE
, &ArrowSize
);
338 theme
->GetThemePartSize(hTheme
, NULL
, MENU_POPUPSEPARATOR
, 0,
339 NULL
, TS_TRUE
, &SeparatorSize
);
341 theme
->GetThemeInt(hTheme
, MENU_POPUPBACKGROUND
, 0, TMT_BORDERSIZE
, &TextBorder
);
348 wxNativeFontInfo fontInfo
;
349 theme
->GetThemeSysFont(hTheme
, TMT_MENUFONT
, &fontInfo
.lf
);
350 Font
= wxFont(fontInfo
);
354 // native menu doesn't uses the vertical margins
355 ItemMargin
.top
= ItemMargin
.bottom
= 0;
357 // native menu uses small top margin for separator
358 if ( SeparatorMargin
.top
>= 2 )
359 SeparatorMargin
.top
-= 2;
362 #endif // wxUSE_UXTHEME
364 const NONCLIENTMETRICS
& metrics
= wxMSWImpl::GetNonClientMetrics();
366 ItemMargin
= Margins();
369 CheckMargin
.right
= ::GetSystemMetrics(SM_CXEDGE
);
371 CheckMargin
.bottom
= ::GetSystemMetrics(SM_CYEDGE
);
373 CheckBgMargin
= Margins();
375 CheckSize
.cx
= ::GetSystemMetrics(SM_CXMENUCHECK
);
376 CheckSize
.cy
= ::GetSystemMetrics(SM_CYMENUCHECK
);
378 ArrowMargin
= Margins();
380 ArrowSize
= CheckSize
;
382 // separator height with margins
383 int sepFullSize
= metrics
.iMenuHeight
/ 2;
385 SeparatorMargin
.left
=
386 SeparatorMargin
.right
= 1;
387 SeparatorMargin
.top
=
388 SeparatorMargin
.bottom
= sepFullSize
/ 2 - 1;
390 SeparatorSize
.cx
= 1;
391 SeparatorSize
.cy
= sepFullSize
- SeparatorMargin
.top
- SeparatorMargin
.bottom
;
399 Font
= wxFont(wxNativeFontInfo(metrics
.lfMenuFont
));
405 if ( ::SystemParametersInfo(SPI_GETKEYBOARDCUES
, 0, &value
, 0) == 0 )
407 // if it's not supported, we must be on an old Windows version
408 // which always shows them
412 AlwaysShowCues
= value
== 1;
416 } // anonymous namespace
418 #endif // wxUSE_OWNER_DRAWN
424 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
426 const wxString
& text
,
427 const wxString
& strHelp
,
430 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
435 #if WXWIN_COMPATIBILITY_2_8
436 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
438 const wxString
& text
,
439 const wxString
& help
,
442 : wxMenuItemBase(parentMenu
, id
, text
, help
,
443 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
449 void wxMenuItem::Init()
451 m_radioGroup
.start
= -1;
452 m_isRadioGroupStart
= false;
454 #if wxUSE_OWNER_DRAWN
456 // when the color is not valid, wxOwnerDraw takes the default ones.
457 // If we set the colors here and they are changed by the user during
458 // the execution, then the colors are not updated until the application
459 // is restarted and our menus look bad
460 SetTextColour(wxNullColour
);
461 SetBackgroundColour(wxNullColour
);
463 // setting default colors switched ownerdraw on: switch it off again
464 SetOwnerDrawn(false);
466 // switch ownerdraw back on if using a non default margin
467 if ( !IsSeparator() )
468 SetMarginWidth(GetMarginWidth());
470 #endif // wxUSE_OWNER_DRAWN
473 wxMenuItem::~wxMenuItem()
480 // return the id for calling Win32 API functions
481 WXWPARAM
wxMenuItem::GetMSWId() const
483 // we must use ids in unsigned short range with Windows functions, if we
484 // pass ids > USHRT_MAX to them they get very confused (e.g. start
485 // generating WM_COMMAND messages with negative high word of wParam), so
486 // use the cast to ensure the id is in range
487 return m_subMenu
? wxPtrToUInt(m_subMenu
->GetHMenu())
488 : static_cast<unsigned short>(GetId());
494 bool wxMenuItem::IsChecked() const
496 // fix that RTTI is always getting the correct state (separators cannot be
497 // checked, but the Windows call below returns true
501 // the item might not be attached to a menu yet
503 // TODO: shouldn't we just always call the base class version? It seems
504 // like it ought to always be in sync
506 return wxMenuItemBase::IsChecked();
508 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
509 int flag
= ::GetMenuState(hmenu
, GetMSWId(), MF_BYCOMMAND
);
511 return (flag
& MF_CHECKED
) != 0;
517 void wxMenuItem::SetAsRadioGroupStart()
519 m_isRadioGroupStart
= true;
522 void wxMenuItem::SetRadioGroupStart(int start
)
524 wxASSERT_MSG( !m_isRadioGroupStart
,
525 wxT("should only be called for the next radio items") );
527 m_radioGroup
.start
= start
;
530 void wxMenuItem::SetRadioGroupEnd(int end
)
532 wxASSERT_MSG( m_isRadioGroupStart
,
533 wxT("should only be called for the first radio item") );
535 m_radioGroup
.end
= end
;
541 void wxMenuItem::Enable(bool enable
)
543 if ( m_isEnabled
== enable
)
548 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
551 (enable
? MF_ENABLED
: MF_GRAYED
));
555 wxLogLastError(wxT("EnableMenuItem"));
559 wxMenuItemBase::Enable(enable
);
562 void wxMenuItem::Check(bool check
)
564 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
566 if ( m_isChecked
== check
)
571 int flags
= check
? MF_CHECKED
: MF_UNCHECKED
;
572 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
574 if ( GetKind() == wxITEM_RADIO
)
576 // it doesn't make sense to uncheck a radio item -- what would this
581 // get the index of this item in the menu
582 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
583 int pos
= items
.IndexOf(this);
584 wxCHECK_RET( pos
!= wxNOT_FOUND
,
585 wxT("menuitem not found in the menu items list?") );
587 // get the radio group range
591 if ( m_isRadioGroupStart
)
593 // we already have all information we need
595 end
= m_radioGroup
.end
;
597 else // next radio group item
599 // get the radio group end from the start item
600 start
= m_radioGroup
.start
;
601 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
605 // calling CheckMenuRadioItem() with such parameters hangs my system
606 // (NT4 SP6) and I suspect this could happen to the others as well,
608 wxCHECK_RET( start
!= -1 && end
!= -1,
609 wxT("invalid ::CheckMenuRadioItem() parameter(s)") );
611 if ( !::CheckMenuRadioItem(hmenu
,
612 start
, // the first radio group item
614 pos
, // the one to check
617 wxLogLastError(wxT("CheckMenuRadioItem"));
621 // also uncheck all the other items in this radio group
622 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
623 for ( int n
= start
; n
<= end
&& node
; n
++ )
627 node
->GetData()->m_isChecked
= false;
630 node
= node
->GetNext();
635 if ( ::CheckMenuItem(hmenu
,
637 MF_BYCOMMAND
| flags
) == (DWORD
)-1 )
639 wxFAIL_MSG(wxT("CheckMenuItem() failed, item not in the menu?"));
644 wxMenuItemBase::Check(check
);
647 void wxMenuItem::SetItemLabel(const wxString
& txt
)
651 // don't do anything if label didn't change
655 // wxMenuItemBase will do stock ID checks
656 wxMenuItemBase::SetItemLabel(text
);
658 // the item can be not attached to any menu yet and SetItemLabel() is still
659 // valid to call in this case and should do nothing else
664 m_parentMenu
->UpdateAccel(this);
665 #endif // wxUSE_ACCEL
667 const UINT id
= GetMSWId();
668 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
669 if ( !hMenu
|| ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
) == (UINT
)-1 )
672 #if wxUSE_OWNER_DRAWN
673 if ( IsOwnerDrawn() )
675 // we don't need to do anything for owner drawn items, they will redraw
676 // themselves using the new text the next time they're displayed
679 #endif // owner drawn
681 // update the text of the native menu item
682 WinStruct
<MENUITEMINFO
> info
;
684 // surprisingly, calling SetMenuItemInfo() with just MIIM_STRING doesn't
685 // work as it resets the menu bitmap, so we need to first get the old item
686 // state and then modify it
687 const bool isLaterThanWin95
= wxGetWinVersion() > wxWinVersion_95
;
688 info
.fMask
= MIIM_STATE
|
693 if ( isLaterThanWin95
)
694 info
.fMask
|= MIIM_BITMAP
| MIIM_FTYPE
;
696 info
.fMask
|= MIIM_TYPE
;
697 if ( !::GetMenuItemInfo(hMenu
, id
, FALSE
, &info
) )
699 wxLogLastError(wxT("GetMenuItemInfo"));
703 if ( isLaterThanWin95
)
704 info
.fMask
|= MIIM_STRING
;
705 //else: MIIM_TYPE already specified
706 info
.dwTypeData
= (LPTSTR
)m_text
.wx_str();
707 info
.cch
= m_text
.length();
708 if ( !::SetMenuItemInfo(hMenu
, id
, FALSE
, &info
) )
710 wxLogLastError(wxT("SetMenuItemInfo"));
714 #if wxUSE_OWNER_DRAWN
716 int wxMenuItem::MeasureAccelWidth() const
718 wxString accel
= GetItemLabel().AfterFirst(wxT('\t'));
726 dc
.GetTextExtent(accel
, &w
, NULL
);
731 wxString
wxMenuItem::GetName() const
733 return GetItemLabelText();
736 bool wxMenuItem::OnMeasureItem(size_t *width
, size_t *height
)
738 const MenuDrawData
* data
= MenuDrawData::Get();
740 if ( IsOwnerDrawn() )
742 *width
= data
->ItemMargin
.left
+ data
->ItemMargin
.right
;
743 *height
= data
->ItemMargin
.top
+ data
->ItemMargin
.bottom
;
747 *width
+= data
->SeparatorSize
.cx
748 + data
->SeparatorMargin
.left
+ data
->SeparatorMargin
.right
;
749 *height
+= data
->SeparatorSize
.cy
750 + data
->SeparatorMargin
.top
+ data
->SeparatorMargin
.bottom
;
754 wxString str
= GetName();
762 dc
.GetTextExtent(str
, &w
, &h
);
764 *width
= data
->TextBorder
+ w
+ data
->AccelBorder
;
767 w
= m_parentMenu
->GetMaxAccelWidth();
769 *width
+= w
+ data
->ArrowBorder
;
771 *width
+= data
->Offset
;
772 *width
+= data
->ArrowMargin
.left
+ data
->ArrowSize
.cx
+ data
->ArrowMargin
.right
;
774 else // don't draw the text, just the bitmap (if any)
782 if ( IsOwnerDrawn() )
784 // width of menu icon with margins in ownerdrawn menu
785 // if any bitmap is not set, the width of space reserved for icon
786 // image is equal to the width of std check mark,
787 // if bitmap is set, then the width is set to the width of the widest
788 // bitmap in menu (GetMarginWidth()) unless std check mark is wider,
789 // then it's is set to std mark's width
790 int imgWidth
= wxMax(GetMarginWidth(), data
->CheckSize
.cx
)
791 + data
->CheckMargin
.left
+ data
->CheckMargin
.right
;
793 *width
+= imgWidth
+ data
->CheckBgMargin
.left
+ data
->CheckBgMargin
.right
;
796 if ( m_bmpChecked
.IsOk() || m_bmpChecked
.IsOk() )
798 // get size of bitmap always return valid value (0 for invalid bitmap),
799 // so we don't needed check if bitmap is valid ;)
800 size_t heightBmp
= wxMax(m_bmpChecked
.GetHeight(), m_bmpUnchecked
.GetHeight());
801 size_t widthtBmp
= wxMax(m_bmpChecked
.GetWidth(), m_bmpUnchecked
.GetWidth());
803 if ( IsOwnerDrawn() )
805 heightBmp
+= data
->CheckMargin
.top
+ data
->CheckMargin
.bottom
;
809 // we must allocate enough space for the bitmap
813 // Is BMP height larger than text height?
814 if ( *height
< heightBmp
)
818 // make sure that this item is at least as tall as the system menu height
819 const size_t menuHeight
= data
->CheckMargin
.top
+ data
->CheckMargin
.bottom
820 + data
->CheckSize
.cy
;
821 if (*height
< menuHeight
)
822 *height
= menuHeight
;
827 bool wxMenuItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
828 wxODAction
WXUNUSED(act
), wxODStatus stat
)
830 const MenuDrawData
* data
= MenuDrawData::Get();
832 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
833 HDC hdc
= GetHdcOf(*impl
);
836 wxCopyRectToRECT(rc
, rect
);
838 int imgWidth
= wxMax(GetMarginWidth(), data
->CheckSize
.cx
);
840 if ( IsOwnerDrawn() )
842 // font and colors to use
846 wxColour colText1
, colBack1
;
847 GetColourToUse(stat
, colText1
, colBack1
);
849 DWORD colText
= wxColourToPalRGB(colText1
);
850 DWORD colBack
= wxColourToPalRGB(colBack1
);
852 // calculate metrics of item parts
858 SetRect(&rcSelection
,
859 rect
.left
+ data
->ItemMargin
.left
,
860 rect
.top
+ data
->ItemMargin
.top
,
861 rect
.right
- data
->ItemMargin
.right
,
862 rect
.bottom
- data
->ItemMargin
.bottom
);
864 SetRect(&rcSeparator
,
865 rcSelection
.left
+ data
->SeparatorMargin
.left
,
866 rcSelection
.top
+ data
->SeparatorMargin
.top
,
867 rcSelection
.right
- data
->SeparatorMargin
.right
,
868 rcSelection
.bottom
- data
->SeparatorMargin
.bottom
);
870 CopyRect(&rcGutter
, &rcSelection
);
871 rcGutter
.right
= data
->ItemMargin
.left
872 + data
->CheckBgMargin
.left
873 + data
->CheckMargin
.left
875 + data
->CheckMargin
.right
876 + data
->CheckBgMargin
.right
;
878 CopyRect(&rcText
, &rcSelection
);
879 rcText
.left
= rcGutter
.right
+ data
->TextBorder
;
881 // we draw the text label vertically centered, but this results in it
882 // being 1px too low compared to native menus for some reason, fix it
883 if ( data
->MenuLayout() != MenuDrawData::FullTheme
)
887 wxUxThemeEngine
* theme
= MenuDrawData::GetUxThemeEngine();
890 POPUPITEMSTATES state
;
891 if ( stat
& wxODDisabled
)
893 state
= (stat
& wxODSelected
) ? MPI_DISABLEDHOT
896 else if ( stat
& wxODSelected
)
905 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
907 if ( theme
->IsThemeBackgroundPartiallyTransparent(hTheme
,
908 MENU_POPUPITEM
, state
) )
910 theme
->DrawThemeBackground(hTheme
, hdc
,
911 MENU_POPUPBACKGROUND
,
915 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPGUTTER
,
920 rcSeparator
.left
= rcGutter
.right
;
921 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPSEPARATOR
,
922 0, &rcSeparator
, NULL
);
926 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPITEM
,
927 state
, &rcSelection
, NULL
);
931 #endif // wxUSE_UXTHEME
935 DrawEdge(hdc
, &rcSeparator
, EDGE_ETCHED
, BF_TOP
);
939 AutoHBRUSH
hbr(colBack
);
940 SelectInHDC
selBrush(hdc
, hbr
);
941 ::FillRect(hdc
, &rcSelection
, hbr
);
946 // using native API because it recognizes '&'
948 COLORREF colOldText
= ::SetTextColor(hdc
, colText
);
949 COLORREF colOldBack
= ::SetBkColor(hdc
, colBack
);
951 int prevMode
= SetBkMode(hdc
, TRANSPARENT
);
953 SelectInHDC
selFont(hdc
, GetHfontOf(font
));
956 // item text name without menemonic for calculating size
957 wxString text
= GetName();
960 ::GetTextExtentPoint32(hdc
, text
.c_str(), text
.length(), &textSize
);
962 // item text name with menemonic
963 text
= GetItemLabel().BeforeFirst('\t');
965 int flags
= DST_PREFIXTEXT
;
966 // themes menu is using specified color for disabled labels
967 if ( data
->MenuLayout() == MenuDrawData::Classic
&&
968 (stat
& wxODDisabled
) && !(stat
& wxODSelected
) )
969 flags
|= DSS_DISABLED
;
971 if ( (stat
& wxODHidePrefix
) && !data
->AlwaysShowCues
)
972 flags
|= DSS_HIDEPREFIX
;
975 int y
= rcText
.top
+ (rcText
.bottom
- rcText
.top
- textSize
.cy
) / 2;
977 ::DrawState(hdc
, NULL
, NULL
, (LPARAM
)text
.wx_str(),
978 text
.length(), x
, y
, 0, 0, flags
);
980 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
981 // as the last parameter in DrawState() (at least with Windows98). So we have
982 // to take care of right alignment ourselves.
983 wxString accel
= GetItemLabel().AfterFirst(wxT('\t'));
984 if ( !accel
.empty() )
987 ::GetTextExtentPoint32(hdc
, accel
.c_str(), accel
.length(), &accelSize
);
989 int flags
= DST_TEXT
;
990 // themes menu is using specified color for disabled labels
991 if ( data
->MenuLayout() == MenuDrawData::Classic
&&
992 (stat
& wxODDisabled
) && !(stat
& wxODSelected
) )
993 flags
|= DSS_DISABLED
;
995 int x
= rcText
.right
- data
->ArrowMargin
.left
997 - data
->ArrowMargin
.right
1000 // right align accel on FullTheme menu, left otherwise
1001 if ( data
->MenuLayout() == MenuDrawData::FullTheme
)
1004 x
-= m_parentMenu
->GetMaxAccelWidth();
1006 int y
= rcText
.top
+ (rcText
.bottom
- rcText
.top
- accelSize
.cy
) / 2;
1008 ::DrawState(hdc
, NULL
, NULL
, (LPARAM
)accel
.wx_str(),
1009 accel
.length(), x
, y
, 0, 0, flags
);
1012 ::SetBkMode(hdc
, prevMode
);
1013 ::SetBkColor(hdc
, colOldBack
);
1014 ::SetTextColor(hdc
, colOldText
);
1022 rect
.left
+ data
->ItemMargin
.left
1023 + data
->CheckBgMargin
.left
1024 + data
->CheckMargin
.left
,
1025 rect
.top
+ data
->ItemMargin
.top
1026 + data
->CheckBgMargin
.top
1027 + data
->CheckMargin
.top
,
1028 rect
.left
+ data
->ItemMargin
.left
1029 + data
->CheckBgMargin
.left
1030 + data
->CheckMargin
.left
1032 rect
.bottom
- data
->ItemMargin
.bottom
1033 - data
->CheckBgMargin
.bottom
1034 - data
->CheckMargin
.bottom
);
1036 if ( IsCheckable() && !m_bmpChecked
.Ok() )
1038 if ( stat
& wxODChecked
)
1040 DrawStdCheckMark((WXHDC
)hdc
, &rcImg
, stat
);
1047 if ( stat
& wxODDisabled
)
1049 bmp
= GetDisabledBitmap();
1054 // for not checkable bitmaps we should always use unchecked one
1055 // because their checked bitmap is not set
1056 bmp
= GetBitmap(!IsCheckable() || (stat
& wxODChecked
));
1059 if ( bmp
.Ok() && stat
& wxODDisabled
)
1061 // we need to grey out the bitmap as we don't have any specific
1063 wxImage imgGrey
= bmp
.ConvertToImage().ConvertToGreyscale();
1065 bmp
= wxBitmap(imgGrey
);
1067 #endif // wxUSE_IMAGE
1072 wxMemoryDC
dcMem(&dc
);
1073 dcMem
.SelectObjectAsSource(bmp
);
1076 int nBmpWidth
= bmp
.GetWidth(),
1077 nBmpHeight
= bmp
.GetHeight();
1079 // there should be enough space!
1080 wxASSERT( nBmpWidth
<= imgWidth
&& nBmpHeight
<= (rcImg
.bottom
- rcImg
.top
) );
1082 int x
= rcImg
.left
+ (imgWidth
- nBmpWidth
) / 2;
1083 int y
= rcImg
.top
+ (rcImg
.bottom
- rcImg
.top
- nBmpHeight
) / 2;
1084 dc
.Blit(x
, y
, nBmpWidth
, nBmpHeight
, &dcMem
, 0, 0, wxCOPY
, true);
1095 // helper function for draw coloured check mark
1096 void DrawColorCheckMark(HDC hdc
, int x
, int y
, int cx
, int cy
, HDC hdcCheckMask
, int idxColor
)
1098 const COLORREF colBlack
= RGB(0, 0, 0);
1099 const COLORREF colWhite
= RGB(255, 255, 255);
1101 COLORREF colOldText
= ::SetTextColor(hdc
, colBlack
);
1102 COLORREF colOldBack
= ::SetBkColor(hdc
, colWhite
);
1103 int prevMode
= SetBkMode(hdc
, TRANSPARENT
);
1105 // memory DC for color bitmap
1106 MemoryHDC
hdcMem(hdc
);
1107 CompatibleBitmap
hbmpMem(hdc
, cx
, cy
);
1108 SelectInHDC
selMem(hdcMem
, hbmpMem
);
1110 RECT rect
= { 0, 0, cx
, cy
};
1111 ::FillRect(hdcMem
, &rect
, ::GetSysColorBrush(idxColor
));
1113 const COLORREF colCheck
= ::GetSysColor(idxColor
);
1114 if ( colCheck
== colWhite
)
1116 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcCheckMask
, 0, 0, MERGEPAINT
);
1117 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcMem
, 0, 0, SRCAND
);
1121 if ( colCheck
!= colBlack
)
1123 const DWORD ROP_DSna
= 0x00220326; // dest = (NOT src) AND dest
1124 ::BitBlt(hdcMem
, 0, 0, cx
, cy
, hdcCheckMask
, 0, 0, ROP_DSna
);
1127 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcCheckMask
, 0, 0, SRCAND
);
1128 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcMem
, 0, 0, SRCPAINT
);
1131 ::SetBkMode(hdc
, prevMode
);
1132 ::SetBkColor(hdc
, colOldBack
);
1133 ::SetTextColor(hdc
, colOldText
);
1136 } // anonymous namespace
1138 void wxMenuItem::DrawStdCheckMark(WXHDC hdc_
, const RECT
* rc
, wxODStatus stat
)
1140 HDC hdc
= (HDC
)hdc_
;
1143 wxUxThemeEngine
* theme
= MenuDrawData::GetUxThemeEngine();
1146 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
1148 const MenuDrawData
* data
= MenuDrawData::Get();
1150 // rect for background must be without check margins
1153 rc
->left
- data
->CheckMargin
.left
,
1154 rc
->top
- data
->CheckMargin
.top
,
1155 rc
->right
+ data
->CheckMargin
.right
,
1156 rc
->bottom
+ data
->CheckMargin
.bottom
);
1158 POPUPCHECKBACKGROUNDSTATES stateCheckBg
= (stat
& wxODDisabled
)
1162 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPCHECKBACKGROUND
,
1163 stateCheckBg
, &rcBg
, NULL
);
1165 POPUPCHECKSTATES stateCheck
;
1166 if ( GetKind() == wxITEM_CHECK
)
1168 stateCheck
= (stat
& wxODDisabled
) ? MC_CHECKMARKDISABLED
1169 : MC_CHECKMARKNORMAL
;
1173 stateCheck
= (stat
& wxODDisabled
) ? MC_BULLETDISABLED
1177 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPCHECK
,
1178 stateCheck
, rc
, NULL
);
1181 #endif // wxUSE_UXTHEME
1183 int cx
= rc
->right
- rc
->left
;
1184 int cy
= rc
->bottom
- rc
->top
;
1186 // first create mask of check mark
1187 MemoryHDC
hdcMask(hdc
);
1188 MonoBitmap
hbmpMask(cx
, cy
);
1189 SelectInHDC
selMask(hdcMask
,hbmpMask
);
1191 // then draw a check mark into it
1192 UINT stateCheck
= (GetKind() == wxITEM_CHECK
) ? DFCS_MENUCHECK
1194 RECT rect
= { 0, 0, cx
, cy
};
1195 ::DrawFrameControl(hdcMask
, &rect
, DFC_MENU
, stateCheck
);
1197 // first draw shadow if disabled
1198 if ( (stat
& wxODDisabled
) && !(stat
& wxODSelected
) )
1200 DrawColorCheckMark(hdc
, rc
->left
+ 1, rc
->top
+ 1,
1201 cx
, cy
, hdcMask
, COLOR_3DHILIGHT
);
1204 // then draw a check mark
1205 int color
= COLOR_MENUTEXT
;
1206 if ( stat
& wxODDisabled
)
1207 color
= COLOR_BTNSHADOW
;
1208 else if ( stat
& wxODSelected
)
1209 color
= COLOR_HIGHLIGHTTEXT
;
1211 DrawColorCheckMark(hdc
, rc
->left
, rc
->top
, cx
, cy
, hdcMask
, color
);
1215 void wxMenuItem::GetFontToUse(wxFont
& font
) const
1219 font
= MenuDrawData::Get()->Font
;
1222 void wxMenuItem::GetColourToUse(wxODStatus stat
, wxColour
& colText
, wxColour
& colBack
) const
1225 wxUxThemeEngine
* theme
= MenuDrawData::GetUxThemeEngine();
1228 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
1230 if ( stat
& wxODDisabled
)
1232 wxRGBToColour(colText
, theme
->GetThemeSysColor(hTheme
, COLOR_GRAYTEXT
));
1236 colText
= GetTextColour();
1237 if ( !colText
.IsOk() )
1238 wxRGBToColour(colText
, theme
->GetThemeSysColor(hTheme
, COLOR_MENUTEXT
));
1241 if ( stat
& wxODSelected
)
1243 wxRGBToColour(colBack
, theme
->GetThemeSysColor(hTheme
, COLOR_HIGHLIGHT
));
1247 colBack
= GetBackgroundColour();
1248 if ( !colBack
.IsOk() )
1249 wxRGBToColour(colBack
, theme
->GetThemeSysColor(hTheme
, COLOR_MENU
));
1253 #endif // wxUSE_UXTHEME
1255 wxOwnerDrawn::GetColourToUse(stat
, colText
, colBack
);
1258 #endif // wxUSE_OWNER_DRAWN
1260 // ----------------------------------------------------------------------------
1262 // ----------------------------------------------------------------------------
1264 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1266 const wxString
& name
,
1267 const wxString
& help
,
1271 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1274 #endif // wxUSE_MENUS