1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/menuitem.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ===========================================================================
13 // ===========================================================================
15 // ---------------------------------------------------------------------------
17 // ---------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/menuitem.h"
29 #include "wx/stockitem.h"
33 #include "wx/dcmemory.h"
35 #include "wx/bitmap.h"
36 #include "wx/settings.h"
37 #include "wx/window.h"
39 #include "wx/string.h"
48 #include "wx/msw/private.h"
49 #include "wx/msw/dc.h"
52 // Implemented in menu.cpp
53 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
) ;
57 #include "wx/msw/uxtheme.h"
60 // ---------------------------------------------------------------------------
62 // ---------------------------------------------------------------------------
65 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
67 // ----------------------------------------------------------------------------
68 // helper classes for temporarily changing HDC parameters
69 // ----------------------------------------------------------------------------
74 // This class just stores an HDC.
78 HDCHandler(HDC hdc
) : m_hdc(hdc
) { }
83 class HDCTextColChanger
: HDCHandler
86 HDCTextColChanger(HDC hdc
, COLORREF col
)
88 m_colOld(::SetTextColor(hdc
, col
))
94 ::SetTextColor(m_hdc
, m_colOld
);
101 class HDCBgColChanger
: HDCHandler
104 HDCBgColChanger(HDC hdc
, COLORREF col
)
106 m_colOld(::SetBkColor(hdc
, col
))
112 ::SetBkColor(m_hdc
, m_colOld
);
119 class HDCBgModeChanger
: HDCHandler
122 HDCBgModeChanger(HDC hdc
, int mode
)
124 m_modeOld(::SetBkMode(hdc
, mode
))
130 ::SetBkMode(m_hdc
, m_modeOld
);
137 } // anonymous namespace
139 // ============================================================================
141 // ============================================================================
143 #if wxUSE_OWNER_DRAWN
145 #include "wx/fontutil.h"
146 #include "wx/msw/private/metrics.h"
148 #ifndef SPI_GETKEYBOARDCUES
149 #define SPI_GETKEYBOARDCUES 0x100A
152 #ifndef DSS_HIDEPREFIX
153 #define DSS_HIDEPREFIX 0x0200
160 MENU_MENUITEM_TMSCHEMA
= 1,
161 MENU_SEPARATOR_TMSCHEMA
= 6,
162 MENU_POPUPBACKGROUND
= 9,
163 MENU_POPUPBORDERS
= 10,
164 MENU_POPUPCHECK
= 11,
165 MENU_POPUPCHECKBACKGROUND
= 12,
166 MENU_POPUPGUTTER
= 13,
168 MENU_POPUPSEPARATOR
= 15,
169 MENU_POPUPSUBMENU
= 16,
181 enum POPUPCHECKBACKGROUNDSTATES
188 enum POPUPCHECKSTATES
190 MC_CHECKMARKNORMAL
= 1,
191 MC_CHECKMARKDISABLED
= 2,
193 MC_BULLETDISABLED
= 4,
196 const int TMT_MENUFONT
= 803;
197 const int TMT_BORDERSIZE
= 2403;
198 const int TMT_CONTENTMARGINS
= 3602;
199 const int TMT_SIZINGMARGINS
= 3601;
201 #endif // wxUSE_UXTHEME
203 #endif // wxUSE_OWNER_DRAWN
205 // ----------------------------------------------------------------------------
206 // dynamic classes implementation
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
213 #if wxUSE_OWNER_DRAWN
218 // helper class to keep information about metrics and other stuff
219 // needed for measuring and drawing menu item
223 // Wrapper around standard MARGINS structure providing some helper
224 // functions and automatically initializing the margin fields to 0.
225 struct Margins
: MARGINS
235 int GetTotalX() const { return cxLeftWidth
+ cxRightWidth
; }
236 int GetTotalY() const { return cyTopHeight
+ cyBottomHeight
; }
238 void ApplyTo(RECT
& rect
) const
240 rect
.top
+= cyTopHeight
;
241 rect
.left
+= cxLeftWidth
;
242 rect
.right
-= cyTopHeight
;
243 rect
.bottom
-= cyBottomHeight
;
246 void UnapplyFrom(RECT
& rect
) const
248 rect
.top
-= cyTopHeight
;
249 rect
.left
-= cxLeftWidth
;
250 rect
.right
+= cyTopHeight
;
251 rect
.bottom
+= cyBottomHeight
;
255 Margins ItemMargin
; // popup item margins
257 Margins CheckMargin
; // popup check margins
258 Margins CheckBgMargin
; // popup check background margins
260 Margins ArrowMargin
; // popup submenu arrow margins
262 Margins SeparatorMargin
; // popup separator margins
264 SIZE CheckSize
; // popup check size metric
265 SIZE ArrowSize
; // popup submenu arrow size metric
266 SIZE SeparatorSize
; // popup separator size metric
268 int TextBorder
; // popup border space between
269 // item text and gutter
271 int AccelBorder
; // popup border space between
272 // item text and accelerator
274 int ArrowBorder
; // popup border space between
275 // item accelerator and submenu arrow
277 int Offset
; // system added space at the end of the menu,
278 // add this offset for remove the extra space
280 wxFont Font
; // default menu font
282 bool AlwaysShowCues
; // must keyboard cues always be shown?
284 bool Theme
; // is data initialized for FullTheme?
286 static const MenuDrawData
* Get()
288 // notice that s_menuData can't be created as a global variable because
289 // it needs a window to initialize and no windows exist at the time of
290 // globals initialization yet
293 static MenuDrawData s_menuData
;
294 ms_instance
= &s_menuData
;
298 bool theme
= MenuLayout() == FullTheme
;
299 if ( ms_instance
->Theme
!= theme
)
301 #endif // wxUSE_UXTHEME
311 // get the theme engine or NULL if themes
312 // are not available or not supported on menu
313 static wxUxThemeEngine
*GetUxThemeEngine()
316 if ( MenuLayout() == FullTheme
)
317 return wxUxThemeEngine::GetIfActive();
318 #endif // wxUSE_UXTHEME
325 FullTheme
, // full menu themes (Vista or new)
326 PseudoTheme
, // pseudo menu themes (on XP)
330 static MenuLayoutType
MenuLayout()
332 MenuLayoutType menu
= Classic
;
334 if ( wxUxThemeEngine::GetIfActive() != NULL
)
336 static wxWinVersion ver
= wxGetWinVersion();
337 if ( ver
>= wxWinVersion_Vista
)
339 else if ( ver
== wxWinVersion_XP
)
342 #endif // wxUSE_UXTHEME
349 static MenuDrawData
* ms_instance
;
352 MenuDrawData
* MenuDrawData::ms_instance
= NULL
;
354 void MenuDrawData::Init()
357 wxUxThemeEngine
* theme
= GetUxThemeEngine();
360 wxWindow
* window
= static_cast<wxApp
*>(wxApp::GetInstance())->GetTopWindow();
361 wxUxThemeHandle
hTheme(window
, L
"MENU");
363 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPITEM
, 0,
364 TMT_CONTENTMARGINS
, NULL
,
367 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPCHECK
, 0,
368 TMT_CONTENTMARGINS
, NULL
,
370 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPCHECKBACKGROUND
, 0,
371 TMT_CONTENTMARGINS
, NULL
,
374 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPSUBMENU
, 0,
375 TMT_CONTENTMARGINS
, NULL
,
378 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPSEPARATOR
, 0,
379 TMT_SIZINGMARGINS
, NULL
,
382 theme
->GetThemePartSize(hTheme
, NULL
, MENU_POPUPCHECK
, 0,
383 NULL
, TS_TRUE
, &CheckSize
);
385 theme
->GetThemePartSize(hTheme
, NULL
, MENU_POPUPSUBMENU
, 0,
386 NULL
, TS_TRUE
, &ArrowSize
);
388 theme
->GetThemePartSize(hTheme
, NULL
, MENU_POPUPSEPARATOR
, 0,
389 NULL
, TS_TRUE
, &SeparatorSize
);
391 theme
->GetThemeInt(hTheme
, MENU_POPUPBACKGROUND
, 0, TMT_BORDERSIZE
, &TextBorder
);
398 wxUxThemeFont themeFont
;
399 theme
->GetThemeSysFont(hTheme
, TMT_MENUFONT
, themeFont
.GetPtr());
400 Font
= wxFont(themeFont
.GetLOGFONT());
404 // native menu doesn't uses the vertical margins
405 ItemMargin
.cyTopHeight
=
406 ItemMargin
.cyBottomHeight
= 0;
408 // native menu uses small top margin for separator
409 if ( SeparatorMargin
.cyTopHeight
>= 2 )
410 SeparatorMargin
.cyTopHeight
-= 2;
413 #endif // wxUSE_UXTHEME
415 const NONCLIENTMETRICS
& metrics
= wxMSWImpl::GetNonClientMetrics();
417 CheckMargin
.cxLeftWidth
=
418 CheckMargin
.cxRightWidth
= ::GetSystemMetrics(SM_CXEDGE
);
419 CheckMargin
.cyTopHeight
=
420 CheckMargin
.cyBottomHeight
= ::GetSystemMetrics(SM_CYEDGE
);
422 CheckSize
.cx
= ::GetSystemMetrics(SM_CXMENUCHECK
);
423 CheckSize
.cy
= ::GetSystemMetrics(SM_CYMENUCHECK
);
425 ArrowSize
= CheckSize
;
427 // separator height with margins
428 int sepFullSize
= metrics
.iMenuHeight
/ 2;
430 SeparatorMargin
.cxLeftWidth
=
431 SeparatorMargin
.cxRightWidth
= 1;
432 SeparatorMargin
.cyTopHeight
=
433 SeparatorMargin
.cyBottomHeight
= sepFullSize
/ 2 - 1;
435 SeparatorSize
.cx
= 1;
436 SeparatorSize
.cy
= sepFullSize
- SeparatorMargin
.GetTotalY();
444 Font
= wxFont(wxNativeFontInfo(metrics
.lfMenuFont
));
450 if ( ::SystemParametersInfo(SPI_GETKEYBOARDCUES
, 0, &value
, 0) == 0 )
452 // if it's not supported, we must be on an old Windows version
453 // which always shows them
457 AlwaysShowCues
= value
== 1;
461 } // anonymous namespace
463 #endif // wxUSE_OWNER_DRAWN
469 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
471 const wxString
& text
,
472 const wxString
& strHelp
,
475 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
480 #if WXWIN_COMPATIBILITY_2_8
481 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
483 const wxString
& text
,
484 const wxString
& help
,
487 : wxMenuItemBase(parentMenu
, id
, text
, help
,
488 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
494 void wxMenuItem::Init()
496 #if wxUSE_OWNER_DRAWN
498 // when the color is not valid, wxOwnerDraw takes the default ones.
499 // If we set the colors here and they are changed by the user during
500 // the execution, then the colors are not updated until the application
501 // is restarted and our menus look bad
502 SetTextColour(wxNullColour
);
503 SetBackgroundColour(wxNullColour
);
505 // setting default colors switched ownerdraw on: switch it off again
506 SetOwnerDrawn(false);
508 // switch ownerdraw back on if using a non default margin
509 if ( !IsSeparator() )
510 SetMarginWidth(GetMarginWidth());
512 #endif // wxUSE_OWNER_DRAWN
515 wxMenuItem::~wxMenuItem()
522 // return the id for calling Win32 API functions
523 WXWPARAM
wxMenuItem::GetMSWId() const
525 // we must use ids in unsigned short range with Windows functions, if we
526 // pass ids > USHRT_MAX to them they get very confused (e.g. start
527 // generating WM_COMMAND messages with negative high word of wParam), so
528 // use the cast to ensure the id is in range
529 return m_subMenu
? wxPtrToUInt(m_subMenu
->GetHMenu())
530 : static_cast<unsigned short>(GetId());
536 bool wxMenuItem::IsChecked() const
538 // fix that RTTI is always getting the correct state (separators cannot be
539 // checked, but the Windows call below returns true
543 // the item might not be attached to a menu yet
545 // TODO: shouldn't we just always call the base class version? It seems
546 // like it ought to always be in sync
548 return wxMenuItemBase::IsChecked();
550 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
551 int flag
= ::GetMenuState(hmenu
, GetMSWId(), MF_BYCOMMAND
);
553 return (flag
& MF_CHECKED
) != 0;
559 void wxMenuItem::Enable(bool enable
)
561 if ( m_isEnabled
== enable
)
566 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
569 (enable
? MF_ENABLED
: MF_GRAYED
));
573 wxLogLastError(wxT("EnableMenuItem"));
577 wxMenuItemBase::Enable(enable
);
580 void wxMenuItem::Check(bool check
)
582 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
584 if ( m_isChecked
== check
)
589 int flags
= check
? MF_CHECKED
: MF_UNCHECKED
;
590 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
592 if ( GetKind() == wxITEM_RADIO
)
594 // it doesn't make sense to uncheck a radio item -- what would this
599 // get the index of this item in the menu
600 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
601 int pos
= items
.IndexOf(this);
602 wxCHECK_RET( pos
!= wxNOT_FOUND
,
603 wxT("menuitem not found in the menu items list?") );
605 // get the radio group range
609 if ( !m_parentMenu
->MSWGetRadioGroupRange(pos
, &start
, &end
) )
611 wxFAIL_MSG( wxT("Menu radio item not part of radio group?") );
616 // calling CheckMenuRadioItem() with such parameters hangs my system
617 // (NT4 SP6) and I suspect this could happen to the others as well,
619 wxCHECK_RET( start
!= -1 && end
!= -1,
620 wxT("invalid ::CheckMenuRadioItem() parameter(s)") );
622 if ( !::CheckMenuRadioItem(hmenu
,
623 start
, // the first radio group item
625 pos
, // the one to check
628 wxLogLastError(wxT("CheckMenuRadioItem"));
632 // also uncheck all the other items in this radio group
633 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
634 for ( int n
= start
; n
<= end
&& node
; n
++ )
638 node
->GetData()->m_isChecked
= false;
641 node
= node
->GetNext();
646 if ( ::CheckMenuItem(hmenu
,
648 MF_BYCOMMAND
| flags
) == (DWORD
)-1 )
650 wxFAIL_MSG(wxT("CheckMenuItem() failed, item not in the menu?"));
655 wxMenuItemBase::Check(check
);
658 void wxMenuItem::SetItemLabel(const wxString
& txt
)
662 // don't do anything if label didn't change
666 // wxMenuItemBase will do stock ID checks
667 wxMenuItemBase::SetItemLabel(text
);
669 // the item can be not attached to any menu yet and SetItemLabel() is still
670 // valid to call in this case and should do nothing else
675 m_parentMenu
->UpdateAccel(this);
676 #endif // wxUSE_ACCEL
678 const UINT id
= GetMSWId();
679 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
680 if ( !hMenu
|| ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
) == (UINT
)-1 )
683 // update the text of the native menu item
684 WinStruct
<MENUITEMINFO
> info
;
686 // surprisingly, calling SetMenuItemInfo() with just MIIM_STRING doesn't
687 // work as it resets the menu bitmap, so we need to first get the old item
688 // state and then modify it
689 const bool isLaterThanWin95
= wxGetWinVersion() > wxWinVersion_95
;
690 info
.fMask
= MIIM_STATE
|
695 if ( isLaterThanWin95
)
696 info
.fMask
|= MIIM_BITMAP
| MIIM_FTYPE
;
698 info
.fMask
|= MIIM_TYPE
;
699 if ( !::GetMenuItemInfo(hMenu
, id
, FALSE
, &info
) )
701 wxLogLastError(wxT("GetMenuItemInfo"));
705 #if wxUSE_OWNER_DRAWN
706 // Don't set the text for the owner drawn items, they don't use it and even
707 // though setting it doesn't seem to actually do any harm under Windows 7,
708 // avoid doing this relatively nonsensical operation just in case it does
709 // break something on other, past or future, Windows versions.
711 // Notice that we do need to call SetMenuItemInfo() even for the ownerdrawn
712 // items however as otherwise their size wouldn't be recalculated as
713 // WM_MEASUREITEM wouldn't be sent and this could result in display
714 // problems if the length of the menu item changed significantly.
715 if ( !IsOwnerDrawn() )
716 #endif // wxUSE_OWNER_DRAWN
718 if ( isLaterThanWin95
)
719 info
.fMask
|= MIIM_STRING
;
720 //else: MIIM_TYPE already specified
721 info
.dwTypeData
= wxMSW_CONV_LPTSTR(m_text
);
722 info
.cch
= m_text
.length();
725 if ( !::SetMenuItemInfo(hMenu
, id
, FALSE
, &info
) )
727 wxLogLastError(wxT("SetMenuItemInfo"));
731 #if wxUSE_OWNER_DRAWN
733 int wxMenuItem::MeasureAccelWidth() const
735 wxString accel
= GetItemLabel().AfterFirst(wxT('\t'));
743 dc
.GetTextExtent(accel
, &w
, NULL
);
748 wxString
wxMenuItem::GetName() const
750 return GetItemLabelText();
753 bool wxMenuItem::OnMeasureItem(size_t *width
, size_t *height
)
755 const MenuDrawData
* data
= MenuDrawData::Get();
757 if ( IsOwnerDrawn() )
759 *width
= data
->ItemMargin
.GetTotalX();
760 *height
= data
->ItemMargin
.GetTotalY();
764 *width
+= data
->SeparatorSize
.cx
765 + data
->SeparatorMargin
.GetTotalX();
766 *height
+= data
->SeparatorSize
.cy
767 + data
->SeparatorMargin
.GetTotalY();
771 wxString str
= GetName();
779 dc
.GetTextExtent(str
, &w
, &h
);
781 *width
= data
->TextBorder
+ w
+ data
->AccelBorder
;
784 w
= m_parentMenu
->GetMaxAccelWidth();
786 *width
+= w
+ data
->ArrowBorder
;
788 *width
+= data
->Offset
;
789 *width
+= data
->ArrowMargin
.GetTotalX() + data
->ArrowSize
.cx
;
791 else // don't draw the text, just the bitmap (if any)
799 if ( IsOwnerDrawn() )
801 // width of menu icon with margins in ownerdrawn menu
802 // if any bitmap is not set, the width of space reserved for icon
803 // image is equal to the width of std check mark,
804 // if bitmap is set, then the width is set to the width of the widest
805 // bitmap in menu (GetMarginWidth()) unless std check mark is wider,
806 // then it's is set to std mark's width
807 int imgWidth
= wxMax(GetMarginWidth(), data
->CheckSize
.cx
)
808 + data
->CheckMargin
.GetTotalX();
810 *width
+= imgWidth
+ data
->CheckBgMargin
.GetTotalX();
813 if ( m_bmpChecked
.IsOk() || m_bmpUnchecked
.IsOk() )
815 // get size of bitmap always return valid value (0 for invalid bitmap),
816 // so we don't needed check if bitmap is valid ;)
817 size_t heightBmp
= wxMax(m_bmpChecked
.GetHeight(), m_bmpUnchecked
.GetHeight());
818 size_t widthBmp
= wxMax(m_bmpChecked
.GetWidth(), m_bmpUnchecked
.GetWidth());
820 if ( IsOwnerDrawn() )
822 heightBmp
+= data
->CheckMargin
.GetTotalY();
826 // we must allocate enough space for the bitmap
830 // Is BMP height larger than text height?
831 if ( *height
< heightBmp
)
835 // make sure that this item is at least as tall as the system menu height
836 const size_t menuHeight
= data
->CheckMargin
.GetTotalY()
837 + data
->CheckSize
.cy
;
838 if (*height
< menuHeight
)
839 *height
= menuHeight
;
844 bool wxMenuItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
845 wxODAction
WXUNUSED(act
), wxODStatus stat
)
847 const MenuDrawData
* data
= MenuDrawData::Get();
849 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
850 HDC hdc
= GetHdcOf(*impl
);
853 wxCopyRectToRECT(rc
, rect
);
855 int imgWidth
= wxMax(GetMarginWidth(), data
->CheckSize
.cx
);
857 if ( IsOwnerDrawn() )
859 // font and colors to use
863 wxColour colText
, colBack
;
864 GetColourToUse(stat
, colText
, colBack
);
866 // calculate metrics of item parts
867 RECT rcSelection
= rect
;
868 data
->ItemMargin
.ApplyTo(rcSelection
);
870 RECT rcSeparator
= rcSelection
;
871 data
->SeparatorMargin
.ApplyTo(rcSeparator
);
873 RECT rcGutter
= rcSelection
;
874 rcGutter
.right
= data
->ItemMargin
.cxLeftWidth
875 + data
->CheckBgMargin
.cxLeftWidth
876 + data
->CheckMargin
.cxLeftWidth
878 + data
->CheckMargin
.cxRightWidth
879 + data
->CheckBgMargin
.cxRightWidth
;
881 RECT rcText
= rcSelection
;
882 rcText
.left
= rcGutter
.right
+ data
->TextBorder
;
884 // we draw the text label vertically centered, but this results in it
885 // being 1px too low compared to native menus for some reason, fix it
886 if ( data
->MenuLayout() != MenuDrawData::FullTheme
)
890 // If a custom background colour is explicitly specified, we should use
891 // it instead of the default theme background.
892 wxUxThemeEngine
* const theme
= GetBackgroundColour().IsOk()
894 : MenuDrawData::GetUxThemeEngine();
897 POPUPITEMSTATES state
;
898 if ( stat
& wxODDisabled
)
900 state
= (stat
& wxODSelected
) ? MPI_DISABLEDHOT
903 else if ( stat
& wxODSelected
)
912 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
914 if ( theme
->IsThemeBackgroundPartiallyTransparent(hTheme
,
915 MENU_POPUPITEM
, state
) )
917 theme
->DrawThemeBackground(hTheme
, hdc
,
918 MENU_POPUPBACKGROUND
,
922 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPGUTTER
,
927 rcSeparator
.left
= rcGutter
.right
;
928 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPSEPARATOR
,
929 0, &rcSeparator
, NULL
);
933 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPITEM
,
934 state
, &rcSelection
, NULL
);
938 #endif // wxUSE_UXTHEME
942 DrawEdge(hdc
, &rcSeparator
, EDGE_ETCHED
, BF_TOP
);
946 AutoHBRUSH
hbr(colBack
.GetPixel());
947 SelectInHDC
selBrush(hdc
, hbr
);
948 ::FillRect(hdc
, &rcSelection
, hbr
);
953 // using native API because it recognizes '&'
955 HDCTextColChanger
changeTextCol(hdc
, colText
.GetPixel());
956 HDCBgColChanger
changeBgCol(hdc
, colBack
.GetPixel());
957 HDCBgModeChanger
changeBgMode(hdc
, TRANSPARENT
);
959 SelectInHDC
selFont(hdc
, GetHfontOf(font
));
962 // item text name without mnemonic for calculating size
963 wxString text
= GetName();
966 ::GetTextExtentPoint32(hdc
, text
.c_str(), text
.length(), &textSize
);
968 // item text name with mnemonic
969 text
= GetItemLabel().BeforeFirst('\t');
971 int flags
= DST_PREFIXTEXT
;
972 // themes menu is using specified color for disabled labels
973 if ( data
->MenuLayout() == MenuDrawData::Classic
&&
974 (stat
& wxODDisabled
) && !(stat
& wxODSelected
) )
975 flags
|= DSS_DISABLED
;
977 if ( (stat
& wxODHidePrefix
) && !data
->AlwaysShowCues
)
978 flags
|= DSS_HIDEPREFIX
;
981 int y
= rcText
.top
+ (rcText
.bottom
- rcText
.top
- textSize
.cy
) / 2;
983 ::DrawState(hdc
, NULL
, NULL
, wxMSW_CONV_LPARAM(text
),
984 text
.length(), x
, y
, 0, 0, flags
);
986 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
987 // as the last parameter in DrawState() (at least with Windows98). So we have
988 // to take care of right alignment ourselves.
989 wxString accel
= GetItemLabel().AfterFirst(wxT('\t'));
990 if ( !accel
.empty() )
993 ::GetTextExtentPoint32(hdc
, accel
.c_str(), accel
.length(), &accelSize
);
995 int flags
= DST_TEXT
;
996 // themes menu is using specified color for disabled labels
997 if ( data
->MenuLayout() == MenuDrawData::Classic
&&
998 (stat
& wxODDisabled
) && !(stat
& wxODSelected
) )
999 flags
|= DSS_DISABLED
;
1001 int x
= rcText
.right
- data
->ArrowMargin
.GetTotalX()
1002 - data
->ArrowSize
.cx
1003 - data
->ArrowBorder
;
1005 // right align accel on FullTheme menu, left otherwise
1006 if ( data
->MenuLayout() == MenuDrawData::FullTheme
)
1009 x
-= m_parentMenu
->GetMaxAccelWidth();
1011 int y
= rcText
.top
+ (rcText
.bottom
- rcText
.top
- accelSize
.cy
) / 2;
1013 ::DrawState(hdc
, NULL
, NULL
, wxMSW_CONV_LPARAM(accel
),
1014 accel
.length(), x
, y
, 0, 0, flags
);
1023 rect
.left
+ data
->ItemMargin
.cxLeftWidth
1024 + data
->CheckBgMargin
.cxLeftWidth
1025 + data
->CheckMargin
.cxLeftWidth
,
1026 rect
.top
+ data
->ItemMargin
.cyTopHeight
1027 + data
->CheckBgMargin
.cyTopHeight
1028 + data
->CheckMargin
.cyTopHeight
,
1029 rect
.left
+ data
->ItemMargin
.cxLeftWidth
1030 + data
->CheckBgMargin
.cxLeftWidth
1031 + data
->CheckMargin
.cxLeftWidth
1033 rect
.bottom
- data
->ItemMargin
.cyBottomHeight
1034 - data
->CheckBgMargin
.cyBottomHeight
1035 - data
->CheckMargin
.cyBottomHeight
);
1037 if ( IsCheckable() && !m_bmpChecked
.IsOk() )
1039 if ( stat
& wxODChecked
)
1041 DrawStdCheckMark((WXHDC
)hdc
, &rcImg
, stat
);
1048 if ( stat
& wxODDisabled
)
1050 bmp
= GetDisabledBitmap();
1055 // for not checkable bitmaps we should always use unchecked one
1056 // because their checked bitmap is not set
1057 bmp
= GetBitmap(!IsCheckable() || (stat
& wxODChecked
));
1060 if ( bmp
.IsOk() && stat
& wxODDisabled
)
1062 // we need to grey out the bitmap as we don't have any specific
1064 wxImage imgGrey
= bmp
.ConvertToImage().ConvertToGreyscale();
1065 if ( imgGrey
.IsOk() )
1066 bmp
= wxBitmap(imgGrey
);
1068 #endif // wxUSE_IMAGE
1073 wxMemoryDC
dcMem(&dc
);
1074 dcMem
.SelectObjectAsSource(bmp
);
1077 int nBmpWidth
= bmp
.GetWidth(),
1078 nBmpHeight
= bmp
.GetHeight();
1080 int x
= rcImg
.left
+ (imgWidth
- nBmpWidth
) / 2;
1081 int y
= rcImg
.top
+ (rcImg
.bottom
- rcImg
.top
- nBmpHeight
) / 2;
1082 dc
.Blit(x
, y
, nBmpWidth
, nBmpHeight
, &dcMem
, 0, 0, wxCOPY
, true);
1093 // helper function for draw coloured check mark
1094 void DrawColorCheckMark(HDC hdc
, int x
, int y
, int cx
, int cy
, HDC hdcCheckMask
, int idxColor
)
1096 const COLORREF colBlack
= RGB(0, 0, 0);
1097 const COLORREF colWhite
= RGB(255, 255, 255);
1099 HDCTextColChanger
changeTextCol(hdc
, colBlack
);
1100 HDCBgColChanger
changeBgCol(hdc
, colWhite
);
1101 HDCBgModeChanger
changeBgMode(hdc
, TRANSPARENT
);
1103 // memory DC for color bitmap
1104 MemoryHDC
hdcMem(hdc
);
1105 CompatibleBitmap
hbmpMem(hdc
, cx
, cy
);
1106 SelectInHDC
selMem(hdcMem
, hbmpMem
);
1108 RECT rect
= { 0, 0, cx
, cy
};
1109 ::FillRect(hdcMem
, &rect
, ::GetSysColorBrush(idxColor
));
1111 const COLORREF colCheck
= ::GetSysColor(idxColor
);
1112 if ( colCheck
== colWhite
)
1114 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcCheckMask
, 0, 0, MERGEPAINT
);
1115 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcMem
, 0, 0, SRCAND
);
1119 if ( colCheck
!= colBlack
)
1121 const DWORD ROP_DSna
= 0x00220326; // dest = (NOT src) AND dest
1122 ::BitBlt(hdcMem
, 0, 0, cx
, cy
, hdcCheckMask
, 0, 0, ROP_DSna
);
1125 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcCheckMask
, 0, 0, SRCAND
);
1126 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcMem
, 0, 0, SRCPAINT
);
1130 } // anonymous namespace
1132 void wxMenuItem::DrawStdCheckMark(WXHDC hdc_
, const RECT
* rc
, wxODStatus stat
)
1134 HDC hdc
= (HDC
)hdc_
;
1137 wxUxThemeEngine
* theme
= MenuDrawData::GetUxThemeEngine();
1140 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
1142 const MenuDrawData
* data
= MenuDrawData::Get();
1144 // rect for background must be without check margins
1146 data
->CheckMargin
.UnapplyFrom(rcBg
);
1148 POPUPCHECKBACKGROUNDSTATES stateCheckBg
= (stat
& wxODDisabled
)
1152 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPCHECKBACKGROUND
,
1153 stateCheckBg
, &rcBg
, NULL
);
1155 POPUPCHECKSTATES stateCheck
;
1156 if ( GetKind() == wxITEM_CHECK
)
1158 stateCheck
= (stat
& wxODDisabled
) ? MC_CHECKMARKDISABLED
1159 : MC_CHECKMARKNORMAL
;
1163 stateCheck
= (stat
& wxODDisabled
) ? MC_BULLETDISABLED
1167 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPCHECK
,
1168 stateCheck
, rc
, NULL
);
1171 #endif // wxUSE_UXTHEME
1173 int cx
= rc
->right
- rc
->left
;
1174 int cy
= rc
->bottom
- rc
->top
;
1176 // first create mask of check mark
1177 MemoryHDC
hdcMask(hdc
);
1178 MonoBitmap
hbmpMask(cx
, cy
);
1179 SelectInHDC
selMask(hdcMask
,hbmpMask
);
1181 // then draw a check mark into it
1182 UINT stateCheck
= (GetKind() == wxITEM_CHECK
) ? DFCS_MENUCHECK
1184 RECT rect
= { 0, 0, cx
, cy
};
1185 ::DrawFrameControl(hdcMask
, &rect
, DFC_MENU
, stateCheck
);
1187 // first draw shadow if disabled
1188 if ( (stat
& wxODDisabled
) && !(stat
& wxODSelected
) )
1190 DrawColorCheckMark(hdc
, rc
->left
+ 1, rc
->top
+ 1,
1191 cx
, cy
, hdcMask
, COLOR_3DHILIGHT
);
1194 // then draw a check mark
1195 int color
= COLOR_MENUTEXT
;
1196 if ( stat
& wxODDisabled
)
1197 color
= COLOR_BTNSHADOW
;
1198 else if ( stat
& wxODSelected
)
1199 color
= COLOR_HIGHLIGHTTEXT
;
1201 DrawColorCheckMark(hdc
, rc
->left
, rc
->top
, cx
, cy
, hdcMask
, color
);
1205 void wxMenuItem::GetFontToUse(wxFont
& font
) const
1209 font
= MenuDrawData::Get()->Font
;
1212 void wxMenuItem::GetColourToUse(wxODStatus stat
, wxColour
& colText
, wxColour
& colBack
) const
1215 wxUxThemeEngine
* theme
= MenuDrawData::GetUxThemeEngine();
1218 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
1220 if ( stat
& wxODDisabled
)
1222 wxRGBToColour(colText
, theme
->GetThemeSysColor(hTheme
, COLOR_GRAYTEXT
));
1226 colText
= GetTextColour();
1227 if ( !colText
.IsOk() )
1228 wxRGBToColour(colText
, theme
->GetThemeSysColor(hTheme
, COLOR_MENUTEXT
));
1231 if ( stat
& wxODSelected
)
1233 wxRGBToColour(colBack
, theme
->GetThemeSysColor(hTheme
, COLOR_HIGHLIGHT
));
1237 colBack
= GetBackgroundColour();
1238 if ( !colBack
.IsOk() )
1239 wxRGBToColour(colBack
, theme
->GetThemeSysColor(hTheme
, COLOR_MENU
));
1243 #endif // wxUSE_UXTHEME
1245 wxOwnerDrawn::GetColourToUse(stat
, colText
, colBack
);
1248 #endif // wxUSE_OWNER_DRAWN
1250 // ----------------------------------------------------------------------------
1252 // ----------------------------------------------------------------------------
1254 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1256 const wxString
& name
,
1257 const wxString
& help
,
1261 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1264 #endif // wxUSE_MENUS