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 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 #if wxUSE_OWNER_DRAWN
219 // helper class to keep information about metrics and other stuff
220 // needed for measuring and drawing menu item
224 // Wrapper around standard MARGINS structure providing some helper
225 // functions and automatically initializing the margin fields to 0.
226 struct Margins
: MARGINS
236 int GetTotalX() const { return cxLeftWidth
+ cxRightWidth
; }
237 int GetTotalY() const { return cyTopHeight
+ cyBottomHeight
; }
239 void ApplyTo(RECT
& rect
) const
241 rect
.top
+= cyTopHeight
;
242 rect
.left
+= cxLeftWidth
;
243 rect
.right
-= cyTopHeight
;
244 rect
.bottom
-= cyBottomHeight
;
247 void UnapplyFrom(RECT
& rect
) const
249 rect
.top
-= cyTopHeight
;
250 rect
.left
-= cxLeftWidth
;
251 rect
.right
+= cyTopHeight
;
252 rect
.bottom
+= cyBottomHeight
;
256 Margins ItemMargin
; // popup item margins
258 Margins CheckMargin
; // popup check margins
259 Margins CheckBgMargin
; // popup check background margins
261 Margins ArrowMargin
; // popup submenu arrow margins
263 Margins SeparatorMargin
; // popup separator margins
265 SIZE CheckSize
; // popup check size metric
266 SIZE ArrowSize
; // popup submenu arrow size metric
267 SIZE SeparatorSize
; // popup separator size metric
269 int TextBorder
; // popup border space between
270 // item text and gutter
272 int AccelBorder
; // popup border space between
273 // item text and accelerator
275 int ArrowBorder
; // popup border space between
276 // item accelerator and submenu arrow
278 int Offset
; // system added space at the end of the menu,
279 // add this offset for remove the extra space
281 wxFont Font
; // default menu font
283 bool AlwaysShowCues
; // must keyboard cues always be shown?
285 bool Theme
; // is data initialized for FullTheme?
287 static const MenuDrawData
* Get()
289 // notice that s_menuData can't be created as a global variable because
290 // it needs a window to initialize and no windows exist at the time of
291 // globals initialization yet
294 static MenuDrawData s_menuData
;
295 ms_instance
= &s_menuData
;
299 bool theme
= MenuLayout() == FullTheme
;
300 if ( ms_instance
->Theme
!= theme
)
302 #endif // wxUSE_UXTHEME
312 // get the theme engine or NULL if themes
313 // are not available or not supported on menu
314 static wxUxThemeEngine
*GetUxThemeEngine()
317 if ( MenuLayout() == FullTheme
)
318 return wxUxThemeEngine::GetIfActive();
319 #endif // wxUSE_UXTHEME
326 FullTheme
, // full menu themes (Vista or new)
327 PseudoTheme
, // pseudo menu themes (on XP)
331 static MenuLayoutType
MenuLayout()
333 MenuLayoutType menu
= Classic
;
335 if ( wxUxThemeEngine::GetIfActive() != NULL
)
337 static wxWinVersion ver
= wxGetWinVersion();
338 if ( ver
>= wxWinVersion_Vista
)
340 else if ( ver
== wxWinVersion_XP
)
343 #endif // wxUSE_UXTHEME
350 static MenuDrawData
* ms_instance
;
353 MenuDrawData
* MenuDrawData::ms_instance
= NULL
;
355 void MenuDrawData::Init()
358 wxUxThemeEngine
* theme
= GetUxThemeEngine();
361 wxWindow
* window
= static_cast<wxApp
*>(wxApp::GetInstance())->GetTopWindow();
362 wxUxThemeHandle
hTheme(window
, L
"MENU");
364 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPITEM
, 0,
365 TMT_CONTENTMARGINS
, NULL
,
368 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPCHECK
, 0,
369 TMT_CONTENTMARGINS
, NULL
,
371 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPCHECKBACKGROUND
, 0,
372 TMT_CONTENTMARGINS
, NULL
,
375 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPSUBMENU
, 0,
376 TMT_CONTENTMARGINS
, NULL
,
379 theme
->GetThemeMargins(hTheme
, NULL
, MENU_POPUPSEPARATOR
, 0,
380 TMT_SIZINGMARGINS
, NULL
,
383 theme
->GetThemePartSize(hTheme
, NULL
, MENU_POPUPCHECK
, 0,
384 NULL
, TS_TRUE
, &CheckSize
);
386 theme
->GetThemePartSize(hTheme
, NULL
, MENU_POPUPSUBMENU
, 0,
387 NULL
, TS_TRUE
, &ArrowSize
);
389 theme
->GetThemePartSize(hTheme
, NULL
, MENU_POPUPSEPARATOR
, 0,
390 NULL
, TS_TRUE
, &SeparatorSize
);
392 theme
->GetThemeInt(hTheme
, MENU_POPUPBACKGROUND
, 0, TMT_BORDERSIZE
, &TextBorder
);
399 wxUxThemeFont themeFont
;
400 theme
->GetThemeSysFont(hTheme
, TMT_MENUFONT
, themeFont
.GetPtr());
401 Font
= wxFont(themeFont
.GetLOGFONT());
405 // native menu doesn't uses the vertical margins
406 ItemMargin
.cyTopHeight
=
407 ItemMargin
.cyBottomHeight
= 0;
409 // native menu uses small top margin for separator
410 if ( SeparatorMargin
.cyTopHeight
>= 2 )
411 SeparatorMargin
.cyTopHeight
-= 2;
414 #endif // wxUSE_UXTHEME
416 const NONCLIENTMETRICS
& metrics
= wxMSWImpl::GetNonClientMetrics();
418 CheckMargin
.cxLeftWidth
=
419 CheckMargin
.cxRightWidth
= ::GetSystemMetrics(SM_CXEDGE
);
420 CheckMargin
.cyTopHeight
=
421 CheckMargin
.cyBottomHeight
= ::GetSystemMetrics(SM_CYEDGE
);
423 CheckSize
.cx
= ::GetSystemMetrics(SM_CXMENUCHECK
);
424 CheckSize
.cy
= ::GetSystemMetrics(SM_CYMENUCHECK
);
426 ArrowSize
= CheckSize
;
428 // separator height with margins
429 int sepFullSize
= metrics
.iMenuHeight
/ 2;
431 SeparatorMargin
.cxLeftWidth
=
432 SeparatorMargin
.cxRightWidth
= 1;
433 SeparatorMargin
.cyTopHeight
=
434 SeparatorMargin
.cyBottomHeight
= sepFullSize
/ 2 - 1;
436 SeparatorSize
.cx
= 1;
437 SeparatorSize
.cy
= sepFullSize
- SeparatorMargin
.GetTotalY();
445 Font
= wxFont(wxNativeFontInfo(metrics
.lfMenuFont
));
451 if ( ::SystemParametersInfo(SPI_GETKEYBOARDCUES
, 0, &value
, 0) == 0 )
453 // if it's not supported, we must be on an old Windows version
454 // which always shows them
458 AlwaysShowCues
= value
== 1;
462 } // anonymous namespace
464 #endif // wxUSE_OWNER_DRAWN
470 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
472 const wxString
& text
,
473 const wxString
& strHelp
,
476 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
481 #if WXWIN_COMPATIBILITY_2_8
482 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
484 const wxString
& text
,
485 const wxString
& help
,
488 : wxMenuItemBase(parentMenu
, id
, text
, help
,
489 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
495 void wxMenuItem::Init()
497 m_radioGroup
.start
= -1;
498 m_isRadioGroupStart
= false;
500 #if wxUSE_OWNER_DRAWN
502 // when the color is not valid, wxOwnerDraw takes the default ones.
503 // If we set the colors here and they are changed by the user during
504 // the execution, then the colors are not updated until the application
505 // is restarted and our menus look bad
506 SetTextColour(wxNullColour
);
507 SetBackgroundColour(wxNullColour
);
509 // setting default colors switched ownerdraw on: switch it off again
510 SetOwnerDrawn(false);
512 // switch ownerdraw back on if using a non default margin
513 if ( !IsSeparator() )
514 SetMarginWidth(GetMarginWidth());
516 #endif // wxUSE_OWNER_DRAWN
519 wxMenuItem::~wxMenuItem()
526 // return the id for calling Win32 API functions
527 WXWPARAM
wxMenuItem::GetMSWId() const
529 // we must use ids in unsigned short range with Windows functions, if we
530 // pass ids > USHRT_MAX to them they get very confused (e.g. start
531 // generating WM_COMMAND messages with negative high word of wParam), so
532 // use the cast to ensure the id is in range
533 return m_subMenu
? wxPtrToUInt(m_subMenu
->GetHMenu())
534 : static_cast<unsigned short>(GetId());
540 bool wxMenuItem::IsChecked() const
542 // fix that RTTI is always getting the correct state (separators cannot be
543 // checked, but the Windows call below returns true
547 // the item might not be attached to a menu yet
549 // TODO: shouldn't we just always call the base class version? It seems
550 // like it ought to always be in sync
552 return wxMenuItemBase::IsChecked();
554 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
555 int flag
= ::GetMenuState(hmenu
, GetMSWId(), MF_BYCOMMAND
);
557 return (flag
& MF_CHECKED
) != 0;
563 void wxMenuItem::SetAsRadioGroupStart()
565 m_isRadioGroupStart
= true;
568 void wxMenuItem::SetRadioGroupStart(int start
)
570 wxASSERT_MSG( !m_isRadioGroupStart
,
571 wxT("should only be called for the next radio items") );
573 m_radioGroup
.start
= start
;
576 void wxMenuItem::SetRadioGroupEnd(int end
)
578 wxASSERT_MSG( m_isRadioGroupStart
,
579 wxT("should only be called for the first radio item") );
581 m_radioGroup
.end
= end
;
587 void wxMenuItem::Enable(bool enable
)
589 if ( m_isEnabled
== enable
)
594 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
597 (enable
? MF_ENABLED
: MF_GRAYED
));
601 wxLogLastError(wxT("EnableMenuItem"));
605 wxMenuItemBase::Enable(enable
);
608 void wxMenuItem::Check(bool check
)
610 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
612 if ( m_isChecked
== check
)
617 int flags
= check
? MF_CHECKED
: MF_UNCHECKED
;
618 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
620 if ( GetKind() == wxITEM_RADIO
)
622 // it doesn't make sense to uncheck a radio item -- what would this
627 // get the index of this item in the menu
628 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
629 int pos
= items
.IndexOf(this);
630 wxCHECK_RET( pos
!= wxNOT_FOUND
,
631 wxT("menuitem not found in the menu items list?") );
633 // get the radio group range
637 if ( m_isRadioGroupStart
)
639 // we already have all information we need
641 end
= m_radioGroup
.end
;
643 else // next radio group item
645 // get the radio group end from the start item
646 start
= m_radioGroup
.start
;
647 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
651 // calling CheckMenuRadioItem() with such parameters hangs my system
652 // (NT4 SP6) and I suspect this could happen to the others as well,
654 wxCHECK_RET( start
!= -1 && end
!= -1,
655 wxT("invalid ::CheckMenuRadioItem() parameter(s)") );
657 if ( !::CheckMenuRadioItem(hmenu
,
658 start
, // the first radio group item
660 pos
, // the one to check
663 wxLogLastError(wxT("CheckMenuRadioItem"));
667 // also uncheck all the other items in this radio group
668 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
669 for ( int n
= start
; n
<= end
&& node
; n
++ )
673 node
->GetData()->m_isChecked
= false;
676 node
= node
->GetNext();
681 if ( ::CheckMenuItem(hmenu
,
683 MF_BYCOMMAND
| flags
) == (DWORD
)-1 )
685 wxFAIL_MSG(wxT("CheckMenuItem() failed, item not in the menu?"));
690 wxMenuItemBase::Check(check
);
693 void wxMenuItem::SetItemLabel(const wxString
& txt
)
697 // don't do anything if label didn't change
701 // wxMenuItemBase will do stock ID checks
702 wxMenuItemBase::SetItemLabel(text
);
704 // the item can be not attached to any menu yet and SetItemLabel() is still
705 // valid to call in this case and should do nothing else
710 m_parentMenu
->UpdateAccel(this);
711 #endif // wxUSE_ACCEL
713 const UINT id
= GetMSWId();
714 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
715 if ( !hMenu
|| ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
) == (UINT
)-1 )
718 #if wxUSE_OWNER_DRAWN
719 if ( IsOwnerDrawn() )
721 // we don't need to do anything for owner drawn items, they will redraw
722 // themselves using the new text the next time they're displayed
725 #endif // owner drawn
727 // update the text of the native menu item
728 WinStruct
<MENUITEMINFO
> info
;
730 // surprisingly, calling SetMenuItemInfo() with just MIIM_STRING doesn't
731 // work as it resets the menu bitmap, so we need to first get the old item
732 // state and then modify it
733 const bool isLaterThanWin95
= wxGetWinVersion() > wxWinVersion_95
;
734 info
.fMask
= MIIM_STATE
|
739 if ( isLaterThanWin95
)
740 info
.fMask
|= MIIM_BITMAP
| MIIM_FTYPE
;
742 info
.fMask
|= MIIM_TYPE
;
743 if ( !::GetMenuItemInfo(hMenu
, id
, FALSE
, &info
) )
745 wxLogLastError(wxT("GetMenuItemInfo"));
749 if ( isLaterThanWin95
)
750 info
.fMask
|= MIIM_STRING
;
751 //else: MIIM_TYPE already specified
752 info
.dwTypeData
= (LPTSTR
)m_text
.wx_str();
753 info
.cch
= m_text
.length();
754 if ( !::SetMenuItemInfo(hMenu
, id
, FALSE
, &info
) )
756 wxLogLastError(wxT("SetMenuItemInfo"));
760 #if wxUSE_OWNER_DRAWN
762 int wxMenuItem::MeasureAccelWidth() const
764 wxString accel
= GetItemLabel().AfterFirst(wxT('\t'));
772 dc
.GetTextExtent(accel
, &w
, NULL
);
777 wxString
wxMenuItem::GetName() const
779 return GetItemLabelText();
782 bool wxMenuItem::OnMeasureItem(size_t *width
, size_t *height
)
784 const MenuDrawData
* data
= MenuDrawData::Get();
786 if ( IsOwnerDrawn() )
788 *width
= data
->ItemMargin
.GetTotalX();
789 *height
= data
->ItemMargin
.GetTotalY();
793 *width
+= data
->SeparatorSize
.cx
794 + data
->SeparatorMargin
.GetTotalX();
795 *height
+= data
->SeparatorSize
.cy
796 + data
->SeparatorMargin
.GetTotalY();
800 wxString str
= GetName();
808 dc
.GetTextExtent(str
, &w
, &h
);
810 *width
= data
->TextBorder
+ w
+ data
->AccelBorder
;
813 w
= m_parentMenu
->GetMaxAccelWidth();
815 *width
+= w
+ data
->ArrowBorder
;
817 *width
+= data
->Offset
;
818 *width
+= data
->ArrowMargin
.GetTotalX() + data
->ArrowSize
.cx
;
820 else // don't draw the text, just the bitmap (if any)
828 if ( IsOwnerDrawn() )
830 // width of menu icon with margins in ownerdrawn menu
831 // if any bitmap is not set, the width of space reserved for icon
832 // image is equal to the width of std check mark,
833 // if bitmap is set, then the width is set to the width of the widest
834 // bitmap in menu (GetMarginWidth()) unless std check mark is wider,
835 // then it's is set to std mark's width
836 int imgWidth
= wxMax(GetMarginWidth(), data
->CheckSize
.cx
)
837 + data
->CheckMargin
.GetTotalX();
839 *width
+= imgWidth
+ data
->CheckBgMargin
.GetTotalX();
842 if ( m_bmpChecked
.IsOk() || m_bmpUnchecked
.IsOk() )
844 // get size of bitmap always return valid value (0 for invalid bitmap),
845 // so we don't needed check if bitmap is valid ;)
846 size_t heightBmp
= wxMax(m_bmpChecked
.GetHeight(), m_bmpUnchecked
.GetHeight());
847 size_t widthBmp
= wxMax(m_bmpChecked
.GetWidth(), m_bmpUnchecked
.GetWidth());
849 if ( IsOwnerDrawn() )
851 heightBmp
+= data
->CheckMargin
.GetTotalY();
855 // we must allocate enough space for the bitmap
859 // Is BMP height larger than text height?
860 if ( *height
< heightBmp
)
864 // make sure that this item is at least as tall as the system menu height
865 const size_t menuHeight
= data
->CheckMargin
.GetTotalY()
866 + data
->CheckSize
.cy
;
867 if (*height
< menuHeight
)
868 *height
= menuHeight
;
873 bool wxMenuItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
874 wxODAction
WXUNUSED(act
), wxODStatus stat
)
876 const MenuDrawData
* data
= MenuDrawData::Get();
878 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
879 HDC hdc
= GetHdcOf(*impl
);
882 wxCopyRectToRECT(rc
, rect
);
884 int imgWidth
= wxMax(GetMarginWidth(), data
->CheckSize
.cx
);
886 if ( IsOwnerDrawn() )
888 // font and colors to use
892 wxColour colText
, colBack
;
893 GetColourToUse(stat
, colText
, colBack
);
895 // calculate metrics of item parts
896 RECT rcSelection
= rect
;
897 data
->ItemMargin
.ApplyTo(rcSelection
);
899 RECT rcSeparator
= rcSelection
;
900 data
->SeparatorMargin
.ApplyTo(rcSeparator
);
902 RECT rcGutter
= rcSelection
;
903 rcGutter
.right
= data
->ItemMargin
.cxLeftWidth
904 + data
->CheckBgMargin
.cxLeftWidth
905 + data
->CheckMargin
.cxLeftWidth
907 + data
->CheckMargin
.cxRightWidth
908 + data
->CheckBgMargin
.cxRightWidth
;
910 RECT rcText
= rcSelection
;
911 rcText
.left
= rcGutter
.right
+ data
->TextBorder
;
913 // we draw the text label vertically centered, but this results in it
914 // being 1px too low compared to native menus for some reason, fix it
915 if ( data
->MenuLayout() != MenuDrawData::FullTheme
)
919 // If a custom background colour is explicitly specified, we should use
920 // it instead of the default theme background.
921 wxUxThemeEngine
* const theme
= GetBackgroundColour().IsOk()
923 : MenuDrawData::GetUxThemeEngine();
926 POPUPITEMSTATES state
;
927 if ( stat
& wxODDisabled
)
929 state
= (stat
& wxODSelected
) ? MPI_DISABLEDHOT
932 else if ( stat
& wxODSelected
)
941 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
943 if ( theme
->IsThemeBackgroundPartiallyTransparent(hTheme
,
944 MENU_POPUPITEM
, state
) )
946 theme
->DrawThemeBackground(hTheme
, hdc
,
947 MENU_POPUPBACKGROUND
,
951 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPGUTTER
,
956 rcSeparator
.left
= rcGutter
.right
;
957 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPSEPARATOR
,
958 0, &rcSeparator
, NULL
);
962 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPITEM
,
963 state
, &rcSelection
, NULL
);
967 #endif // wxUSE_UXTHEME
971 DrawEdge(hdc
, &rcSeparator
, EDGE_ETCHED
, BF_TOP
);
975 AutoHBRUSH
hbr(colBack
.GetPixel());
976 SelectInHDC
selBrush(hdc
, hbr
);
977 ::FillRect(hdc
, &rcSelection
, hbr
);
982 // using native API because it recognizes '&'
984 HDCTextColChanger
changeTextCol(hdc
, colText
.GetPixel());
985 HDCBgColChanger
changeBgCol(hdc
, colBack
.GetPixel());
986 HDCBgModeChanger
changeBgMode(hdc
, TRANSPARENT
);
988 SelectInHDC
selFont(hdc
, GetHfontOf(font
));
991 // item text name without mnemonic for calculating size
992 wxString text
= GetName();
995 ::GetTextExtentPoint32(hdc
, text
.c_str(), text
.length(), &textSize
);
997 // item text name with mnemonic
998 text
= GetItemLabel().BeforeFirst('\t');
1000 int flags
= DST_PREFIXTEXT
;
1001 // themes menu is using specified color for disabled labels
1002 if ( data
->MenuLayout() == MenuDrawData::Classic
&&
1003 (stat
& wxODDisabled
) && !(stat
& wxODSelected
) )
1004 flags
|= DSS_DISABLED
;
1006 if ( (stat
& wxODHidePrefix
) && !data
->AlwaysShowCues
)
1007 flags
|= DSS_HIDEPREFIX
;
1009 int x
= rcText
.left
;
1010 int y
= rcText
.top
+ (rcText
.bottom
- rcText
.top
- textSize
.cy
) / 2;
1012 ::DrawState(hdc
, NULL
, NULL
, (LPARAM
)text
.wx_str(),
1013 text
.length(), x
, y
, 0, 0, flags
);
1015 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
1016 // as the last parameter in DrawState() (at least with Windows98). So we have
1017 // to take care of right alignment ourselves.
1018 wxString accel
= GetItemLabel().AfterFirst(wxT('\t'));
1019 if ( !accel
.empty() )
1022 ::GetTextExtentPoint32(hdc
, accel
.c_str(), accel
.length(), &accelSize
);
1024 int flags
= DST_TEXT
;
1025 // themes menu is using specified color for disabled labels
1026 if ( data
->MenuLayout() == MenuDrawData::Classic
&&
1027 (stat
& wxODDisabled
) && !(stat
& wxODSelected
) )
1028 flags
|= DSS_DISABLED
;
1030 int x
= rcText
.right
- data
->ArrowMargin
.GetTotalX()
1031 - data
->ArrowSize
.cx
1032 - data
->ArrowBorder
;
1034 // right align accel on FullTheme menu, left otherwise
1035 if ( data
->MenuLayout() == MenuDrawData::FullTheme
)
1038 x
-= m_parentMenu
->GetMaxAccelWidth();
1040 int y
= rcText
.top
+ (rcText
.bottom
- rcText
.top
- accelSize
.cy
) / 2;
1042 ::DrawState(hdc
, NULL
, NULL
, (LPARAM
)accel
.wx_str(),
1043 accel
.length(), x
, y
, 0, 0, flags
);
1052 rect
.left
+ data
->ItemMargin
.cxLeftWidth
1053 + data
->CheckBgMargin
.cxLeftWidth
1054 + data
->CheckMargin
.cxLeftWidth
,
1055 rect
.top
+ data
->ItemMargin
.cyTopHeight
1056 + data
->CheckBgMargin
.cyTopHeight
1057 + data
->CheckMargin
.cyTopHeight
,
1058 rect
.left
+ data
->ItemMargin
.cxLeftWidth
1059 + data
->CheckBgMargin
.cxLeftWidth
1060 + data
->CheckMargin
.cxLeftWidth
1062 rect
.bottom
- data
->ItemMargin
.cyBottomHeight
1063 - data
->CheckBgMargin
.cyBottomHeight
1064 - data
->CheckMargin
.cyBottomHeight
);
1066 if ( IsCheckable() && !m_bmpChecked
.Ok() )
1068 if ( stat
& wxODChecked
)
1070 DrawStdCheckMark((WXHDC
)hdc
, &rcImg
, stat
);
1077 if ( stat
& wxODDisabled
)
1079 bmp
= GetDisabledBitmap();
1084 // for not checkable bitmaps we should always use unchecked one
1085 // because their checked bitmap is not set
1086 bmp
= GetBitmap(!IsCheckable() || (stat
& wxODChecked
));
1089 if ( bmp
.Ok() && stat
& wxODDisabled
)
1091 // we need to grey out the bitmap as we don't have any specific
1093 wxImage imgGrey
= bmp
.ConvertToImage().ConvertToGreyscale();
1095 bmp
= wxBitmap(imgGrey
);
1097 #endif // wxUSE_IMAGE
1102 wxMemoryDC
dcMem(&dc
);
1103 dcMem
.SelectObjectAsSource(bmp
);
1106 int nBmpWidth
= bmp
.GetWidth(),
1107 nBmpHeight
= bmp
.GetHeight();
1109 // there should be enough space!
1110 wxASSERT( nBmpWidth
<= imgWidth
&& nBmpHeight
<= (rcImg
.bottom
- rcImg
.top
) );
1112 int x
= rcImg
.left
+ (imgWidth
- nBmpWidth
) / 2;
1113 int y
= rcImg
.top
+ (rcImg
.bottom
- rcImg
.top
- nBmpHeight
) / 2;
1114 dc
.Blit(x
, y
, nBmpWidth
, nBmpHeight
, &dcMem
, 0, 0, wxCOPY
, true);
1125 // helper function for draw coloured check mark
1126 void DrawColorCheckMark(HDC hdc
, int x
, int y
, int cx
, int cy
, HDC hdcCheckMask
, int idxColor
)
1128 const COLORREF colBlack
= RGB(0, 0, 0);
1129 const COLORREF colWhite
= RGB(255, 255, 255);
1131 HDCTextColChanger
changeTextCol(hdc
, colBlack
);
1132 HDCBgColChanger
changeBgCol(hdc
, colWhite
);
1133 HDCBgModeChanger
changeBgMode(hdc
, TRANSPARENT
);
1135 // memory DC for color bitmap
1136 MemoryHDC
hdcMem(hdc
);
1137 CompatibleBitmap
hbmpMem(hdc
, cx
, cy
);
1138 SelectInHDC
selMem(hdcMem
, hbmpMem
);
1140 RECT rect
= { 0, 0, cx
, cy
};
1141 ::FillRect(hdcMem
, &rect
, ::GetSysColorBrush(idxColor
));
1143 const COLORREF colCheck
= ::GetSysColor(idxColor
);
1144 if ( colCheck
== colWhite
)
1146 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcCheckMask
, 0, 0, MERGEPAINT
);
1147 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcMem
, 0, 0, SRCAND
);
1151 if ( colCheck
!= colBlack
)
1153 const DWORD ROP_DSna
= 0x00220326; // dest = (NOT src) AND dest
1154 ::BitBlt(hdcMem
, 0, 0, cx
, cy
, hdcCheckMask
, 0, 0, ROP_DSna
);
1157 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcCheckMask
, 0, 0, SRCAND
);
1158 ::BitBlt(hdc
, x
, y
, cx
, cy
, hdcMem
, 0, 0, SRCPAINT
);
1162 } // anonymous namespace
1164 void wxMenuItem::DrawStdCheckMark(WXHDC hdc_
, const RECT
* rc
, wxODStatus stat
)
1166 HDC hdc
= (HDC
)hdc_
;
1169 wxUxThemeEngine
* theme
= MenuDrawData::GetUxThemeEngine();
1172 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
1174 const MenuDrawData
* data
= MenuDrawData::Get();
1176 // rect for background must be without check margins
1178 data
->CheckMargin
.UnapplyFrom(rcBg
);
1180 POPUPCHECKBACKGROUNDSTATES stateCheckBg
= (stat
& wxODDisabled
)
1184 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPCHECKBACKGROUND
,
1185 stateCheckBg
, &rcBg
, NULL
);
1187 POPUPCHECKSTATES stateCheck
;
1188 if ( GetKind() == wxITEM_CHECK
)
1190 stateCheck
= (stat
& wxODDisabled
) ? MC_CHECKMARKDISABLED
1191 : MC_CHECKMARKNORMAL
;
1195 stateCheck
= (stat
& wxODDisabled
) ? MC_BULLETDISABLED
1199 theme
->DrawThemeBackground(hTheme
, hdc
, MENU_POPUPCHECK
,
1200 stateCheck
, rc
, NULL
);
1203 #endif // wxUSE_UXTHEME
1205 int cx
= rc
->right
- rc
->left
;
1206 int cy
= rc
->bottom
- rc
->top
;
1208 // first create mask of check mark
1209 MemoryHDC
hdcMask(hdc
);
1210 MonoBitmap
hbmpMask(cx
, cy
);
1211 SelectInHDC
selMask(hdcMask
,hbmpMask
);
1213 // then draw a check mark into it
1214 UINT stateCheck
= (GetKind() == wxITEM_CHECK
) ? DFCS_MENUCHECK
1216 RECT rect
= { 0, 0, cx
, cy
};
1217 ::DrawFrameControl(hdcMask
, &rect
, DFC_MENU
, stateCheck
);
1219 // first draw shadow if disabled
1220 if ( (stat
& wxODDisabled
) && !(stat
& wxODSelected
) )
1222 DrawColorCheckMark(hdc
, rc
->left
+ 1, rc
->top
+ 1,
1223 cx
, cy
, hdcMask
, COLOR_3DHILIGHT
);
1226 // then draw a check mark
1227 int color
= COLOR_MENUTEXT
;
1228 if ( stat
& wxODDisabled
)
1229 color
= COLOR_BTNSHADOW
;
1230 else if ( stat
& wxODSelected
)
1231 color
= COLOR_HIGHLIGHTTEXT
;
1233 DrawColorCheckMark(hdc
, rc
->left
, rc
->top
, cx
, cy
, hdcMask
, color
);
1237 void wxMenuItem::GetFontToUse(wxFont
& font
) const
1241 font
= MenuDrawData::Get()->Font
;
1244 void wxMenuItem::GetColourToUse(wxODStatus stat
, wxColour
& colText
, wxColour
& colBack
) const
1247 wxUxThemeEngine
* theme
= MenuDrawData::GetUxThemeEngine();
1250 wxUxThemeHandle
hTheme(GetMenu()->GetWindow(), L
"MENU");
1252 if ( stat
& wxODDisabled
)
1254 wxRGBToColour(colText
, theme
->GetThemeSysColor(hTheme
, COLOR_GRAYTEXT
));
1258 colText
= GetTextColour();
1259 if ( !colText
.IsOk() )
1260 wxRGBToColour(colText
, theme
->GetThemeSysColor(hTheme
, COLOR_MENUTEXT
));
1263 if ( stat
& wxODSelected
)
1265 wxRGBToColour(colBack
, theme
->GetThemeSysColor(hTheme
, COLOR_HIGHLIGHT
));
1269 colBack
= GetBackgroundColour();
1270 if ( !colBack
.IsOk() )
1271 wxRGBToColour(colBack
, theme
->GetThemeSysColor(hTheme
, COLOR_MENU
));
1275 #endif // wxUSE_UXTHEME
1277 wxOwnerDrawn::GetColourToUse(stat
, colText
, colBack
);
1280 #endif // wxUSE_OWNER_DRAWN
1282 // ----------------------------------------------------------------------------
1284 // ----------------------------------------------------------------------------
1286 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1288 const wxString
& name
,
1289 const wxString
& help
,
1293 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1296 #endif // wxUSE_MENUS