]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menuitem.cpp
aligning definition of rtti
[wxWidgets.git] / src / msw / menuitem.cpp
CommitLineData
2bda0e17 1///////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/msw/menuitem.cpp
2bda0e17
KB
3// Purpose: wxMenuItem implementation
4// Author: Vadim Zeitlin
c2dcfdef 5// Modified by:
2bda0e17
KB
6// Created: 11.11.97
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10///////////////////////////////////////////////////////////////////////////////
11
c2dcfdef
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
c2dcfdef 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
1e6feb95
VZ
27#if wxUSE_MENUS
28
3b3dc801 29#include "wx/menuitem.h"
ee0a94cf 30#include "wx/stockitem.h"
e4db172a 31
2bda0e17 32#ifndef WX_PRECOMP
9d043a92
VZ
33 #include "wx/app.h"
34 #include "wx/dcmemory.h"
c2dcfdef
VZ
35 #include "wx/font.h"
36 #include "wx/bitmap.h"
37 #include "wx/settings.h"
4e938f5b 38 #include "wx/window.h"
0c589ad0 39 #include "wx/accel.h"
0c589ad0 40 #include "wx/string.h"
e4db172a 41 #include "wx/log.h"
3b3dc801 42 #include "wx/menu.h"
2bda0e17
KB
43#endif
44
717a57c2
VZ
45#if wxUSE_ACCEL
46 #include "wx/accel.h"
47#endif // wxUSE_ACCEL
48
42e69d6b 49#include "wx/msw/private.h"
98fbab9e 50#include "wx/msw/dc.h"
ce3ed50d 51
4676948b
JS
52#ifdef __WXWINCE__
53// Implemented in menu.cpp
54UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) ;
55#endif
56
aa4919ed
VZ
57#if wxUSE_UXTHEME
58 #include "wx/msw/uxtheme.h"
59#endif
60
c2dcfdef 61// ---------------------------------------------------------------------------
974e8d94 62// macro
c2dcfdef
VZ
63// ---------------------------------------------------------------------------
64
974e8d94 65// hide the ugly cast
c2dcfdef
VZ
66#define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
67
b1466486
VZ
68// ----------------------------------------------------------------------------
69// helper classes for temporarily changing HDC parameters
70// ----------------------------------------------------------------------------
71
72namespace
73{
74
75// This class just stores an HDC.
76class HDCHandler
77{
78protected:
79 HDCHandler(HDC hdc) : m_hdc(hdc) { }
80
81 const HDC m_hdc;
82};
83
84class HDCTextColChanger : HDCHandler
85{
86public:
87 HDCTextColChanger(HDC hdc, COLORREF col)
88 : HDCHandler(hdc),
89 m_colOld(::SetTextColor(hdc, col))
90 {
91 }
92
93 ~HDCTextColChanger()
94 {
95 ::SetTextColor(m_hdc, m_colOld);
96 }
97
98private:
99 COLORREF m_colOld;
100};
101
102class HDCBgColChanger : HDCHandler
103{
104public:
105 HDCBgColChanger(HDC hdc, COLORREF col)
106 : HDCHandler(hdc),
107 m_colOld(::SetBkColor(hdc, col))
108 {
109 }
110
111 ~HDCBgColChanger()
112 {
113 ::SetBkColor(m_hdc, m_colOld);
114 }
115
116private:
117 COLORREF m_colOld;
118};
119
120class HDCBgModeChanger : HDCHandler
121{
122public:
123 HDCBgModeChanger(HDC hdc, int mode)
124 : HDCHandler(hdc),
125 m_modeOld(::SetBkMode(hdc, mode))
126 {
127 }
128
129 ~HDCBgModeChanger()
130 {
131 ::SetBkMode(m_hdc, m_modeOld);
132 }
133
134private:
135 int m_modeOld;
136};
137
138} // anonymous namespace
139
2bda0e17
KB
140// ============================================================================
141// implementation
142// ============================================================================
143
98fbab9e
VZ
144#if wxUSE_OWNER_DRAWN
145
146#include "wx/fontutil.h"
147#include "wx/msw/private/metrics.h"
148
149#ifndef SPI_GETKEYBOARDCUES
150#define SPI_GETKEYBOARDCUES 0x100A
151#endif
152
153#ifndef DSS_HIDEPREFIX
154#define DSS_HIDEPREFIX 0x0200
155#endif
156
aa4919ed
VZ
157#if wxUSE_UXTHEME
158
159enum MENUPARTS
160{
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,
168 MENU_POPUPITEM = 14,
169 MENU_POPUPSEPARATOR = 15,
170 MENU_POPUPSUBMENU = 16,
171};
172
173
174enum POPUPITEMSTATES
175{
176 MPI_NORMAL = 1,
177 MPI_HOT = 2,
178 MPI_DISABLED = 3,
179 MPI_DISABLEDHOT = 4,
180};
181
182enum POPUPCHECKBACKGROUNDSTATES
183{
184 MCB_DISABLED = 1,
185 MCB_NORMAL = 2,
186 MCB_BITMAP = 3,
187};
188
189enum POPUPCHECKSTATES
190{
191 MC_CHECKMARKNORMAL = 1,
192 MC_CHECKMARKDISABLED = 2,
193 MC_BULLETNORMAL = 3,
194 MC_BULLETDISABLED = 4,
195};
196
197const int TMT_MENUFONT = 803;
198const int TMT_BORDERSIZE = 2403;
199const int TMT_CONTENTMARGINS = 3602;
200const int TMT_SIZINGMARGINS = 3601;
201
202#endif // wxUSE_UXTHEME
203
98fbab9e
VZ
204#endif // wxUSE_OWNER_DRAWN
205
2bda0e17
KB
206// ----------------------------------------------------------------------------
207// dynamic classes implementation
208// ----------------------------------------------------------------------------
209
e70b4f10
SC
210#if wxUSE_EXTENDED_RTTI
211
212bool wxMenuItemStreamingCallback( const wxObject *object, wxWriter * , wxPersister * , wxxVariantArray & )
213{
214 const wxMenuItem * mitem = dynamic_cast<const wxMenuItem*>(object) ;
3b3dc801 215 if ( mitem->GetMenu() && !mitem->GetMenu()->GetTitle().empty() )
e70b4f10
SC
216 {
217 // we don't stream out the first two items for menus with a title, they will be reconstructed
218 if ( mitem->GetMenu()->FindItemByPosition(0) == mitem || mitem->GetMenu()->FindItemByPosition(1) == mitem )
219 return false ;
220 }
221 return true ;
222}
223
3ff066a4 224wxBEGIN_ENUM( wxItemKind )
598ddd96
WS
225 wxENUM_MEMBER( wxITEM_SEPARATOR )
226 wxENUM_MEMBER( wxITEM_NORMAL )
227 wxENUM_MEMBER( wxITEM_CHECK )
228 wxENUM_MEMBER( wxITEM_RADIO )
3ff066a4 229wxEND_ENUM( wxItemKind )
e70b4f10
SC
230
231IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem, wxObject,"wx/menuitem.h",wxMenuItemStreamingCallback)
232
3ff066a4 233wxBEGIN_PROPERTIES_TABLE(wxMenuItem)
598ddd96
WS
234 wxPROPERTY( Parent,wxMenu*, SetMenu, GetMenu, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
235 wxPROPERTY( Id,int, SetId, GetId, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
3ff066a4
SC
236 wxPROPERTY( Text, wxString , SetText, GetText, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
237 wxPROPERTY( Help, wxString , SetHelp, GetHelp, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
af498247 238 wxREADONLY_PROPERTY( Kind, wxItemKind , GetKind , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
598ddd96
WS
239 wxPROPERTY( SubMenu,wxMenu*, SetSubMenu, GetSubMenu, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
240 wxPROPERTY( Enabled , bool , Enable , IsEnabled , wxxVariant((bool)true) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
241 wxPROPERTY( Checked , bool , Check , IsChecked , wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
242 wxPROPERTY( Checkable , bool , SetCheckable , IsCheckable , wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
3ff066a4
SC
243wxEND_PROPERTIES_TABLE()
244
245wxBEGIN_HANDLERS_TABLE(wxMenuItem)
246wxEND_HANDLERS_TABLE()
247
248wxDIRECT_CONSTRUCTOR_6( wxMenuItem , wxMenu* , Parent , int , Id , wxString , Text , wxString , Help , wxItemKind , Kind , wxMenu* , SubMenu )
e70b4f10 249#else
621b3e21 250IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
e70b4f10 251#endif
2bda0e17
KB
252
253// ----------------------------------------------------------------------------
254// wxMenuItem
255// ----------------------------------------------------------------------------
256
98fbab9e
VZ
257#if wxUSE_OWNER_DRAWN
258
aa4919ed
VZ
259namespace
260{
261
262// helper class to keep information about metrics and other stuff
263// needed for measuring and drawing menu item
264class MenuDrawData
265{
266public:
abaa31e7
VZ
267 // Wrapper around standard MARGINS structure providing some helper
268 // functions and automatically initializing the margin fields to 0.
269 struct Margins : MARGINS
aa4919ed 270 {
aa4919ed 271 Margins()
abaa31e7
VZ
272 {
273 cxLeftWidth =
274 cxRightWidth =
275 cyTopHeight =
276 cyBottomHeight = 0;
277 }
278
279 int GetTotalX() const { return cxLeftWidth + cxRightWidth; }
280 int GetTotalY() const { return cyTopHeight + cyBottomHeight; }
281
282 void ApplyTo(RECT& rect) const
283 {
284 rect.top += cyTopHeight;
285 rect.left += cxLeftWidth;
286 rect.right -= cyTopHeight;
287 rect.bottom -= cyBottomHeight;
288 }
289
290 void UnapplyFrom(RECT& rect) const
291 {
292 rect.top -= cyTopHeight;
293 rect.left -= cxLeftWidth;
294 rect.right += cyTopHeight;
295 rect.bottom += cyBottomHeight;
296 }
aa4919ed
VZ
297 };
298
299 Margins ItemMargin; // popup item margins
300
301 Margins CheckMargin; // popup check margins
302 Margins CheckBgMargin; // popup check background margins
303
9c32ed26
VZ
304 Margins ArrowMargin; // popup submenu arrow margins
305
aa4919ed
VZ
306 Margins SeparatorMargin; // popup separator margins
307
308 SIZE CheckSize; // popup check size metric
9c32ed26 309 SIZE ArrowSize; // popup submenu arrow size metric
aa4919ed
VZ
310 SIZE SeparatorSize; // popup separator size metric
311
aa4919ed
VZ
312 int TextBorder; // popup border space between
313 // item text and gutter
314
9c32ed26
VZ
315 int AccelBorder; // popup border space between
316 // item text and accelerator
317
318 int ArrowBorder; // popup border space between
319 // item accelerator and submenu arrow
320
321 int Offset; // system added space at the end of the menu,
322 // add this offset for remove the extra space
323
aa4919ed
VZ
324 wxFont Font; // default menu font
325
326 bool AlwaysShowCues; // must keyboard cues always be shown?
327
328 bool Theme; // is data initialized for FullTheme?
329
330 static const MenuDrawData* Get()
331 {
60a7194e
VZ
332 // notice that s_menuData can't be created as a global variable because
333 // it needs a window to initialize and no windows exist at the time of
334 // globals initialization yet
335 if ( !ms_instance )
336 {
337 static MenuDrawData s_menuData;
338 ms_instance = &s_menuData;
339 }
340
aa4919ed
VZ
341 #if wxUSE_UXTHEME
342 bool theme = MenuLayout() == FullTheme;
343 if ( ms_instance->Theme != theme )
344 ms_instance->Init();
345 #endif // wxUSE_UXTHEME
346 return ms_instance;
347 }
348
349 MenuDrawData()
350 {
aa4919ed
VZ
351 Init();
352 }
353
354
355 // get the theme engine or NULL if themes
356 // are not available or not supported on menu
357 static wxUxThemeEngine *GetUxThemeEngine()
358 {
359 #if wxUSE_UXTHEME
360 if ( MenuLayout() == FullTheme )
361 return wxUxThemeEngine::GetIfActive();
362 #endif // wxUSE_UXTHEME
363 return NULL;
364 }
365
366
367 enum MenuLayoutType
368 {
369 FullTheme, // full menu themes (Vista or new)
370 PseudoTheme, // pseudo menu themes (on XP)
371 Classic
372 };
373
374 static MenuLayoutType MenuLayout()
375 {
376 MenuLayoutType menu = Classic;
377 #if wxUSE_UXTHEME
378 if ( wxUxThemeEngine::GetIfActive() != NULL )
379 {
380 static wxWinVersion ver = wxGetWinVersion();
381 if ( ver >= wxWinVersion_Vista )
382 menu = FullTheme;
383 else if ( ver == wxWinVersion_XP )
384 menu = PseudoTheme;
385 }
386 #endif // wxUSE_UXTHEME
387 return menu;
388 }
389
390private:
391 void Init();
392
393 static MenuDrawData* ms_instance;
394};
395
396MenuDrawData* MenuDrawData::ms_instance = NULL;
397
aa4919ed
VZ
398void MenuDrawData::Init()
399{
400#if wxUSE_UXTHEME
401 wxUxThemeEngine* theme = GetUxThemeEngine();
402 if ( theme )
403 {
404 wxWindow* window = static_cast<wxApp*>(wxApp::GetInstance())->GetTopWindow();
405 wxUxThemeHandle hTheme(window, L"MENU");
406
407 theme->GetThemeMargins(hTheme, NULL, MENU_POPUPITEM, 0,
408 TMT_CONTENTMARGINS, NULL,
abaa31e7 409 &ItemMargin);
aa4919ed
VZ
410
411 theme->GetThemeMargins(hTheme, NULL, MENU_POPUPCHECK, 0,
412 TMT_CONTENTMARGINS, NULL,
abaa31e7 413 &CheckMargin);
aa4919ed
VZ
414 theme->GetThemeMargins(hTheme, NULL, MENU_POPUPCHECKBACKGROUND, 0,
415 TMT_CONTENTMARGINS, NULL,
abaa31e7 416 &CheckBgMargin);
aa4919ed 417
9c32ed26
VZ
418 theme->GetThemeMargins(hTheme, NULL, MENU_POPUPSUBMENU, 0,
419 TMT_CONTENTMARGINS, NULL,
abaa31e7 420 &ArrowMargin);
9c32ed26 421
aa4919ed
VZ
422 theme->GetThemeMargins(hTheme, NULL, MENU_POPUPSEPARATOR, 0,
423 TMT_SIZINGMARGINS, NULL,
abaa31e7 424 &SeparatorMargin);
aa4919ed
VZ
425
426 theme->GetThemePartSize(hTheme, NULL, MENU_POPUPCHECK, 0,
427 NULL, TS_TRUE, &CheckSize);
428
9c32ed26
VZ
429 theme->GetThemePartSize(hTheme, NULL, MENU_POPUPSUBMENU, 0,
430 NULL, TS_TRUE, &ArrowSize);
431
aa4919ed
VZ
432 theme->GetThemePartSize(hTheme, NULL, MENU_POPUPSEPARATOR, 0,
433 NULL, TS_TRUE, &SeparatorSize);
434
aa4919ed
VZ
435 theme->GetThemeInt(hTheme, MENU_POPUPBACKGROUND, 0, TMT_BORDERSIZE, &TextBorder);
436
9c32ed26
VZ
437 AccelBorder = 34;
438 ArrowBorder = 0;
439
440 Offset = -14;
441
3120eccf
VZ
442 wxUxThemeFont themeFont;
443 theme->GetThemeSysFont(hTheme, TMT_MENUFONT, themeFont.GetPtr());
444 Font = wxFont(themeFont.GetLOGFONT());
aa4919ed
VZ
445
446 Theme = true;
447
448 // native menu doesn't uses the vertical margins
abaa31e7
VZ
449 ItemMargin.cyTopHeight =
450 ItemMargin.cyBottomHeight = 0;
aa4919ed
VZ
451
452 // native menu uses small top margin for separator
abaa31e7
VZ
453 if ( SeparatorMargin.cyTopHeight >= 2 )
454 SeparatorMargin.cyTopHeight -= 2;
aa4919ed
VZ
455 }
456 else
457#endif // wxUSE_UXTHEME
458 {
459 const NONCLIENTMETRICS& metrics = wxMSWImpl::GetNonClientMetrics();
460
abaa31e7
VZ
461 CheckMargin.cxLeftWidth =
462 CheckMargin.cxRightWidth = ::GetSystemMetrics(SM_CXEDGE);
463 CheckMargin.cyTopHeight =
464 CheckMargin.cyBottomHeight = ::GetSystemMetrics(SM_CYEDGE);
aa4919ed
VZ
465
466 CheckSize.cx = ::GetSystemMetrics(SM_CXMENUCHECK);
467 CheckSize.cy = ::GetSystemMetrics(SM_CYMENUCHECK);
468
9c32ed26
VZ
469 ArrowSize = CheckSize;
470
aa4919ed
VZ
471 // separator height with margins
472 int sepFullSize = metrics.iMenuHeight / 2;
473
abaa31e7
VZ
474 SeparatorMargin.cxLeftWidth =
475 SeparatorMargin.cxRightWidth = 1;
476 SeparatorMargin.cyTopHeight =
477 SeparatorMargin.cyBottomHeight = sepFullSize / 2 - 1;
aa4919ed
VZ
478
479 SeparatorSize.cx = 1;
abaa31e7 480 SeparatorSize.cy = sepFullSize - SeparatorMargin.GetTotalY();
aa4919ed
VZ
481
482 TextBorder = 0;
483 AccelBorder = 8;
9c32ed26
VZ
484 ArrowBorder = 6;
485
486 Offset = -12;
aa4919ed
VZ
487
488 Font = wxFont(wxNativeFontInfo(metrics.lfMenuFont));
489
490 Theme = false;
491 }
492
493 int value;
494 if ( ::SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &value, 0) == 0 )
495 {
496 // if it's not supported, we must be on an old Windows version
497 // which always shows them
498 value = 1;
499 }
500
501 AlwaysShowCues = value == 1;
502
503}
504
505} // anonymous namespace
98fbab9e
VZ
506
507#endif // wxUSE_OWNER_DRAWN
508
509
2bda0e17
KB
510// ctor & dtor
511// -----------
512
974e8d94
VZ
513wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
514 int id,
515 const wxString& text,
516 const wxString& strHelp,
d65c269b 517 wxItemKind kind,
90002c49 518 wxMenu *pSubMenu)
d65c269b 519 : wxMenuItemBase(pParentMenu, id, text, strHelp, kind, pSubMenu)
2bda0e17 520{
2368dcda
VZ
521 Init();
522}
2bda0e17 523
efebabb7 524#if WXWIN_COMPATIBILITY_2_8
2368dcda
VZ
525wxMenuItem::wxMenuItem(wxMenu *parentMenu,
526 int id,
527 const wxString& text,
528 const wxString& help,
529 bool isCheckable,
530 wxMenu *subMenu)
531 : wxMenuItemBase(parentMenu, id, text, help,
532 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
2368dcda
VZ
533{
534 Init();
535}
efebabb7 536#endif
2368dcda
VZ
537
538void wxMenuItem::Init()
539{
be15b995 540 m_radioGroup.start = -1;
598ddd96 541 m_isRadioGroupStart = false;
0472ece7 542
47d67540 543#if wxUSE_OWNER_DRAWN
c2dcfdef 544
51d2fa37
VZ
545 // when the color is not valid, wxOwnerDraw takes the default ones.
546 // If we set the colors here and they are changed by the user during
547 // the execution, then the colors are not updated until the application
548 // is restarted and our menus look bad
549 SetTextColour(wxNullColour);
550 SetBackgroundColour(wxNullColour);
6d5b2a57 551
271fa250 552 // setting default colors switched ownerdraw on: switch it off again
98fbab9e 553 SetOwnerDrawn(false);
2bda0e17 554
271fa250 555 // switch ownerdraw back on if using a non default margin
fa7134b0 556 if ( !IsSeparator() )
271fa250
JS
557 SetMarginWidth(GetMarginWidth());
558
974e8d94 559#endif // wxUSE_OWNER_DRAWN
2bda0e17
KB
560}
561
c2dcfdef 562wxMenuItem::~wxMenuItem()
2bda0e17
KB
563{
564}
565
566// misc
567// ----
568
c2dcfdef 569// return the id for calling Win32 API functions
dca0f651 570WXWPARAM wxMenuItem::GetMSWId() const
c2dcfdef 571{
660e7fda
VZ
572 // we must use ids in unsigned short range with Windows functions, if we
573 // pass ids > USHRT_MAX to them they get very confused (e.g. start
574 // generating WM_COMMAND messages with negative high word of wParam), so
575 // use the cast to ensure the id is in range
dca0f651 576 return m_subMenu ? wxPtrToUInt(m_subMenu->GetHMenu())
5c33522f 577 : static_cast<unsigned short>(GetId());
c2dcfdef
VZ
578}
579
3dfac970
VZ
580// get item state
581// --------------
582
a8cfd0cb 583bool wxMenuItem::IsChecked() const
3dfac970 584{
654c223b
VZ
585 // fix that RTTI is always getting the correct state (separators cannot be
586 // checked, but the Windows call below returns true
fa7134b0 587 if ( IsSeparator() )
654c223b 588 return false;
e70b4f10 589
654c223b
VZ
590 // the item might not be attached to a menu yet
591 //
592 // TODO: shouldn't we just always call the base class version? It seems
593 // like it ought to always be in sync
594 if ( !m_parentMenu )
595 return wxMenuItemBase::IsChecked();
596
597 HMENU hmenu = GetHMenuOf(m_parentMenu);
598 int flag = ::GetMenuState(hmenu, GetMSWId(), MF_BYCOMMAND);
3dfac970 599
4aee367e 600 return (flag & MF_CHECKED) != 0;
3dfac970
VZ
601}
602
be15b995
VZ
603// radio group stuff
604// -----------------
605
606void wxMenuItem::SetAsRadioGroupStart()
607{
598ddd96 608 m_isRadioGroupStart = true;
be15b995
VZ
609}
610
611void wxMenuItem::SetRadioGroupStart(int start)
612{
613 wxASSERT_MSG( !m_isRadioGroupStart,
9a83f860 614 wxT("should only be called for the next radio items") );
be15b995
VZ
615
616 m_radioGroup.start = start;
617}
618
619void wxMenuItem::SetRadioGroupEnd(int end)
620{
621 wxASSERT_MSG( m_isRadioGroupStart,
9a83f860 622 wxT("should only be called for the first radio item") );
be15b995
VZ
623
624 m_radioGroup.end = end;
625}
626
2bda0e17
KB
627// change item state
628// -----------------
629
717a57c2 630void wxMenuItem::Enable(bool enable)
2bda0e17 631{
717a57c2
VZ
632 if ( m_isEnabled == enable )
633 return;
634
654c223b
VZ
635 if ( m_parentMenu )
636 {
637 long rc = EnableMenuItem(GetHMenuOf(m_parentMenu),
638 GetMSWId(),
639 MF_BYCOMMAND |
640 (enable ? MF_ENABLED : MF_GRAYED));
c2dcfdef 641
654c223b
VZ
642 if ( rc == -1 )
643 {
644 wxLogLastError(wxT("EnableMenuItem"));
645 }
c2dcfdef 646 }
717a57c2
VZ
647
648 wxMenuItemBase::Enable(enable);
2bda0e17
KB
649}
650
717a57c2 651void wxMenuItem::Check(bool check)
2bda0e17 652{
d65c269b 653 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
c2dcfdef 654
717a57c2
VZ
655 if ( m_isChecked == check )
656 return;
c2dcfdef 657
654c223b 658 if ( m_parentMenu )
0472ece7 659 {
654c223b
VZ
660 int flags = check ? MF_CHECKED : MF_UNCHECKED;
661 HMENU hmenu = GetHMenuOf(m_parentMenu);
0472ece7 662
654c223b 663 if ( GetKind() == wxITEM_RADIO )
be15b995 664 {
654c223b
VZ
665 // it doesn't make sense to uncheck a radio item -- what would this
666 // do?
667 if ( !check )
668 return;
669
670 // get the index of this item in the menu
671 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
672 int pos = items.IndexOf(this);
673 wxCHECK_RET( pos != wxNOT_FOUND,
9a83f860 674 wxT("menuitem not found in the menu items list?") );
654c223b
VZ
675
676 // get the radio group range
677 int start,
678 end;
679
680 if ( m_isRadioGroupStart )
681 {
682 // we already have all information we need
683 start = pos;
684 end = m_radioGroup.end;
685 }
686 else // next radio group item
687 {
688 // get the radio group end from the start item
689 start = m_radioGroup.start;
690 end = items.Item(start)->GetData()->m_radioGroup.end;
691 }
be15b995 692
0472ece7 693#ifdef __WIN32__
654c223b
VZ
694 // calling CheckMenuRadioItem() with such parameters hangs my system
695 // (NT4 SP6) and I suspect this could happen to the others as well,
696 // so don't do it!
697 wxCHECK_RET( start != -1 && end != -1,
9a83f860 698 wxT("invalid ::CheckMenuRadioItem() parameter(s)") );
654c223b
VZ
699
700 if ( !::CheckMenuRadioItem(hmenu,
701 start, // the first radio group item
702 end, // the last one
703 pos, // the one to check
704 MF_BYPOSITION) )
705 {
9a83f860 706 wxLogLastError(wxT("CheckMenuRadioItem"));
654c223b 707 }
0472ece7
VZ
708#endif // __WIN32__
709
654c223b
VZ
710 // also uncheck all the other items in this radio group
711 wxMenuItemList::compatibility_iterator node = items.Item(start);
712 for ( int n = start; n <= end && node; n++ )
0472ece7 713 {
654c223b
VZ
714 if ( n != pos )
715 {
716 node->GetData()->m_isChecked = false;
717 }
0472ece7 718
654c223b
VZ
719 node = node->GetNext();
720 }
0472ece7 721 }
654c223b 722 else // check item
0472ece7 723 {
654c223b
VZ
724 if ( ::CheckMenuItem(hmenu,
725 GetMSWId(),
726 MF_BYCOMMAND | flags) == (DWORD)-1 )
727 {
9a83f860 728 wxFAIL_MSG(wxT("CheckMenuItem() failed, item not in the menu?"));
654c223b 729 }
0472ece7 730 }
c2dcfdef 731 }
717a57c2
VZ
732
733 wxMenuItemBase::Check(check);
c2dcfdef
VZ
734}
735
52af3158 736void wxMenuItem::SetItemLabel(const wxString& txt)
c2dcfdef 737{
ee0a94cf
RR
738 wxString text = txt;
739
c2dcfdef 740 // don't do anything if label didn't change
ee0a94cf 741 if ( m_text == txt )
c2dcfdef
VZ
742 return;
743
345319d6 744 // wxMenuItemBase will do stock ID checks
52af3158 745 wxMenuItemBase::SetItemLabel(text);
345319d6 746
5c6aad47
VZ
747 // the item can be not attached to any menu yet and SetItemLabel() is still
748 // valid to call in this case and should do nothing else
3350ab0c
VZ
749 if ( !m_parentMenu )
750 return;
751
654c223b
VZ
752#if wxUSE_ACCEL
753 m_parentMenu->UpdateAccel(this);
754#endif // wxUSE_ACCEL
755
5c6aad47
VZ
756 const UINT id = GetMSWId();
757 HMENU hMenu = GetHMenuOf(m_parentMenu);
758 if ( !hMenu || ::GetMenuState(hMenu, id, MF_BYCOMMAND) == (UINT)-1 )
759 return;
760
3d45718d
VZ
761#if wxUSE_OWNER_DRAWN
762 if ( IsOwnerDrawn() )
c2dcfdef 763 {
3d45718d
VZ
764 // we don't need to do anything for owner drawn items, they will redraw
765 // themselves using the new text the next time they're displayed
766 return;
c2dcfdef 767 }
3d45718d 768#endif // owner drawn
c2dcfdef 769
3d45718d 770 // update the text of the native menu item
3d45718d 771 WinStruct<MENUITEMINFO> info;
c2dcfdef 772
3d45718d
VZ
773 // surprisingly, calling SetMenuItemInfo() with just MIIM_STRING doesn't
774 // work as it resets the menu bitmap, so we need to first get the old item
775 // state and then modify it
776 const bool isLaterThanWin95 = wxGetWinVersion() > wxWinVersion_95;
d4290fa5
VZ
777 info.fMask = MIIM_STATE |
778 MIIM_ID |
779 MIIM_SUBMENU |
780 MIIM_CHECKMARKS |
781 MIIM_DATA;
3d45718d
VZ
782 if ( isLaterThanWin95 )
783 info.fMask |= MIIM_BITMAP | MIIM_FTYPE;
784 else
d4290fa5 785 info.fMask |= MIIM_TYPE;
3d45718d
VZ
786 if ( !::GetMenuItemInfo(hMenu, id, FALSE, &info) )
787 {
788 wxLogLastError(wxT("GetMenuItemInfo"));
789 return;
790 }
791
792 if ( isLaterThanWin95 )
793 info.fMask |= MIIM_STRING;
794 //else: MIIM_TYPE already specified
795 info.dwTypeData = (LPTSTR)m_text.wx_str();
796 info.cch = m_text.length();
797 if ( !::SetMenuItemInfo(hMenu, id, FALSE, &info) )
798 {
799 wxLogLastError(wxT("SetMenuItemInfo"));
c2dcfdef
VZ
800 }
801}
2bda0e17 802
98fbab9e
VZ
803#if wxUSE_OWNER_DRAWN
804
9c32ed26
VZ
805int wxMenuItem::MeasureAccelWidth() const
806{
807 wxString accel = GetItemLabel().AfterFirst(wxT('\t'));
808
809 wxMemoryDC dc;
810 wxFont font;
811 GetFontToUse(font);
812 dc.SetFont(font);
813
814 wxCoord w;
815 dc.GetTextExtent(accel, &w, NULL);
816
817 return w;
818}
819
98fbab9e 820wxString wxMenuItem::GetName() const
974e8d94 821{
98fbab9e 822 return GetItemLabelText();
974e8d94
VZ
823}
824
98fbab9e
VZ
825bool wxMenuItem::OnMeasureItem(size_t *width, size_t *height)
826{
aa4919ed
VZ
827 const MenuDrawData* data = MenuDrawData::Get();
828
98fbab9e
VZ
829 if ( IsOwnerDrawn() )
830 {
abaa31e7
VZ
831 *width = data->ItemMargin.GetTotalX();
832 *height = data->ItemMargin.GetTotalY();
98fbab9e 833
aa4919ed 834 if ( IsSeparator() )
98fbab9e 835 {
aa4919ed 836 *width += data->SeparatorSize.cx
abaa31e7 837 + data->SeparatorMargin.GetTotalX();
aa4919ed 838 *height += data->SeparatorSize.cy
abaa31e7 839 + data->SeparatorMargin.GetTotalY();
aa4919ed 840 return true;
98fbab9e
VZ
841 }
842
9c32ed26 843 wxString str = GetName();
aa4919ed 844
98fbab9e
VZ
845 wxMemoryDC dc;
846 wxFont font;
847 GetFontToUse(font);
848 dc.SetFont(font);
849
850 wxCoord w, h;
851 dc.GetTextExtent(str, &w, &h);
aa4919ed 852
9c32ed26 853 *width = data->TextBorder + w + data->AccelBorder;
98fbab9e 854 *height = h;
aa4919ed 855
9c32ed26
VZ
856 w = m_parentMenu->GetMaxAccelWidth();
857 if ( w > 0 )
858 *width += w + data->ArrowBorder;
859
860 *width += data->Offset;
abaa31e7 861 *width += data->ArrowMargin.GetTotalX() + data->ArrowSize.cx;
98fbab9e
VZ
862 }
863 else // don't draw the text, just the bitmap (if any)
864 {
865 *width = 0;
866 *height = 0;
867 }
868
aa4919ed
VZ
869 // bitmap
870
871 if ( IsOwnerDrawn() )
98fbab9e 872 {
03cc2991 873 // width of menu icon with margins in ownerdrawn menu
aa4919ed
VZ
874 // if any bitmap is not set, the width of space reserved for icon
875 // image is equal to the width of std check mark,
876 // if bitmap is set, then the width is set to the width of the widest
877 // bitmap in menu (GetMarginWidth()) unless std check mark is wider,
878 // then it's is set to std mark's width
879 int imgWidth = wxMax(GetMarginWidth(), data->CheckSize.cx)
abaa31e7 880 + data->CheckMargin.GetTotalX();
aa4919ed 881
abaa31e7 882 *width += imgWidth + data->CheckBgMargin.GetTotalX();
aa4919ed
VZ
883 }
884
bbce6969 885 if ( m_bmpChecked.IsOk() || m_bmpUnchecked.IsOk() )
aa4919ed
VZ
886 {
887 // get size of bitmap always return valid value (0 for invalid bitmap),
888 // so we don't needed check if bitmap is valid ;)
889 size_t heightBmp = wxMax(m_bmpChecked.GetHeight(), m_bmpUnchecked.GetHeight());
95908499 890 size_t widthBmp = wxMax(m_bmpChecked.GetWidth(), m_bmpUnchecked.GetWidth());
98fbab9e 891
98fbab9e
VZ
892 if ( IsOwnerDrawn() )
893 {
abaa31e7 894 heightBmp += data->CheckMargin.GetTotalY();
98fbab9e 895 }
aa4919ed 896 else
98fbab9e 897 {
aa4919ed 898 // we must allocate enough space for the bitmap
95908499 899 *width += widthBmp;
98fbab9e 900 }
98fbab9e 901
aa4919ed
VZ
902 // Is BMP height larger than text height?
903 if ( *height < heightBmp )
904 *height = heightBmp;
98fbab9e
VZ
905 }
906
907 // make sure that this item is at least as tall as the system menu height
abaa31e7 908 const size_t menuHeight = data->CheckMargin.GetTotalY()
aa4919ed
VZ
909 + data->CheckSize.cy;
910 if (*height < menuHeight)
911 *height = menuHeight;
98fbab9e
VZ
912
913 return true;
914}
915
916bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc,
917 wxODAction WXUNUSED(act), wxODStatus stat)
918{
aa4919ed 919 const MenuDrawData* data = MenuDrawData::Get();
98fbab9e 920
aa4919ed
VZ
921 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
922 HDC hdc = GetHdcOf(*impl);
98fbab9e 923
aa4919ed
VZ
924 RECT rect;
925 wxCopyRectToRECT(rc, rect);
98fbab9e 926
03cc2991 927 int imgWidth = wxMax(GetMarginWidth(), data->CheckSize.cx);
98fbab9e
VZ
928
929 if ( IsOwnerDrawn() )
930 {
aa4919ed
VZ
931 // font and colors to use
932 wxFont font;
933 GetFontToUse(font);
934
b1466486
VZ
935 wxColour colText, colBack;
936 GetColourToUse(stat, colText, colBack);
aa4919ed
VZ
937
938 // calculate metrics of item parts
abaa31e7
VZ
939 RECT rcSelection = rect;
940 data->ItemMargin.ApplyTo(rcSelection);
941
942 RECT rcSeparator = rcSelection;
943 data->SeparatorMargin.ApplyTo(rcSeparator);
944
945 RECT rcGutter = rcSelection;
946 rcGutter.right = data->ItemMargin.cxLeftWidth
947 + data->CheckBgMargin.cxLeftWidth
948 + data->CheckMargin.cxLeftWidth
aa4919ed 949 + imgWidth
abaa31e7
VZ
950 + data->CheckMargin.cxRightWidth
951 + data->CheckBgMargin.cxRightWidth;
aa4919ed 952
abaa31e7 953 RECT rcText = rcSelection;
aa4919ed
VZ
954 rcText.left = rcGutter.right + data->TextBorder;
955
03cc2991
VZ
956 // we draw the text label vertically centered, but this results in it
957 // being 1px too low compared to native menus for some reason, fix it
958 if ( data->MenuLayout() != MenuDrawData::FullTheme )
959 rcText.top--;
960
aa4919ed 961#if wxUSE_UXTHEME
e8015245
VZ
962 // If a custom background colour is explicitly specified, we should use
963 // it instead of the default theme background.
964 wxUxThemeEngine* const theme = GetBackgroundColour().IsOk()
965 ? NULL
966 : MenuDrawData::GetUxThemeEngine();
aa4919ed 967 if ( theme )
98fbab9e 968 {
aa4919ed
VZ
969 POPUPITEMSTATES state;
970 if ( stat & wxODDisabled )
971 {
972 state = (stat & wxODSelected) ? MPI_DISABLEDHOT
973 : MPI_DISABLED;
974 }
975 else if ( stat & wxODSelected )
976 {
977 state = MPI_HOT;
978 }
979 else
980 {
981 state = MPI_NORMAL;
982 }
98fbab9e 983
aa4919ed 984 wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU");
98fbab9e 985
aa4919ed
VZ
986 if ( theme->IsThemeBackgroundPartiallyTransparent(hTheme,
987 MENU_POPUPITEM, state) )
988 {
989 theme->DrawThemeBackground(hTheme, hdc,
990 MENU_POPUPBACKGROUND,
991 0, &rect, NULL);
992 }
98fbab9e 993
aa4919ed
VZ
994 theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPGUTTER,
995 0, &rcGutter, NULL);
98fbab9e 996
aa4919ed
VZ
997 if ( IsSeparator() )
998 {
999 rcSeparator.left = rcGutter.right;
1000 theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPSEPARATOR,
1001 0, &rcSeparator, NULL);
1002 return true;
1003 }
98fbab9e 1004
aa4919ed
VZ
1005 theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPITEM,
1006 state, &rcSelection, NULL);
98fbab9e 1007
aa4919ed
VZ
1008 }
1009 else
1010#endif // wxUSE_UXTHEME
1011 {
1012 if ( IsSeparator() )
1013 {
1014 DrawEdge(hdc, &rcSeparator, EDGE_ETCHED, BF_TOP);
1015 return true;
1016 }
98fbab9e 1017
b1466486 1018 AutoHBRUSH hbr(colBack.GetPixel());
aa4919ed
VZ
1019 SelectInHDC selBrush(hdc, hbr);
1020 ::FillRect(hdc, &rcSelection, hbr);
1021 }
98fbab9e 1022
98fbab9e 1023
aa4919ed
VZ
1024 // draw text label
1025 // using native API because it recognizes '&'
98fbab9e 1026
b1466486
VZ
1027 HDCTextColChanger changeTextCol(hdc, colText.GetPixel());
1028 HDCBgColChanger changeBgCol(hdc, colBack.GetPixel());
1029 HDCBgModeChanger changeBgMode(hdc, TRANSPARENT);
98fbab9e 1030
98fbab9e
VZ
1031 SelectInHDC selFont(hdc, GetHfontOf(font));
1032
98fbab9e 1033
b1466486 1034 // item text name without mnemonic for calculating size
aa4919ed
VZ
1035 wxString text = GetName();
1036
1037 SIZE textSize;
1038 ::GetTextExtentPoint32(hdc, text.c_str(), text.length(), &textSize);
98fbab9e 1039
b1466486 1040 // item text name with mnemonic
aa4919ed 1041 text = GetItemLabel().BeforeFirst('\t');
98fbab9e
VZ
1042
1043 int flags = DST_PREFIXTEXT;
aa4919ed
VZ
1044 // themes menu is using specified color for disabled labels
1045 if ( data->MenuLayout() == MenuDrawData::Classic &&
1046 (stat & wxODDisabled) && !(stat & wxODSelected) )
98fbab9e
VZ
1047 flags |= DSS_DISABLED;
1048
aa4919ed 1049 if ( (stat & wxODHidePrefix) && !data->AlwaysShowCues )
98fbab9e
VZ
1050 flags |= DSS_HIDEPREFIX;
1051
aa4919ed
VZ
1052 int x = rcText.left;
1053 int y = rcText.top + (rcText.bottom - rcText.top - textSize.cy) / 2;
98fbab9e
VZ
1054
1055 ::DrawState(hdc, NULL, NULL, (LPARAM)text.wx_str(),
aa4919ed 1056 text.length(), x, y, 0, 0, flags);
98fbab9e
VZ
1057
1058 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
1059 // as the last parameter in DrawState() (at least with Windows98). So we have
1060 // to take care of right alignment ourselves.
1061 wxString accel = GetItemLabel().AfterFirst(wxT('\t'));
1062 if ( !accel.empty() )
1063 {
aa4919ed
VZ
1064 SIZE accelSize;
1065 ::GetTextExtentPoint32(hdc, accel.c_str(), accel.length(), &accelSize);
98fbab9e
VZ
1066
1067 int flags = DST_TEXT;
aa4919ed
VZ
1068 // themes menu is using specified color for disabled labels
1069 if ( data->MenuLayout() == MenuDrawData::Classic &&
1070 (stat & wxODDisabled) && !(stat & wxODSelected) )
98fbab9e
VZ
1071 flags |= DSS_DISABLED;
1072
abaa31e7 1073 int x = rcText.right - data->ArrowMargin.GetTotalX()
9c32ed26 1074 - data->ArrowSize.cx
9c32ed26
VZ
1075 - data->ArrowBorder;
1076
1077 // right align accel on FullTheme menu, left otherwise
1078 if ( data->MenuLayout() == MenuDrawData::FullTheme)
1079 x -= accelSize.cx;
1080 else
1081 x -= m_parentMenu->GetMaxAccelWidth();
98fbab9e 1082
aa4919ed
VZ
1083 int y = rcText.top + (rcText.bottom - rcText.top - accelSize.cy) / 2;
1084
98fbab9e
VZ
1085 ::DrawState(hdc, NULL, NULL, (LPARAM)accel.wx_str(),
1086 accel.length(), x, y, 0, 0, flags);
1087 }
98fbab9e
VZ
1088 }
1089
1090
1091 // draw the bitmap
aa4919ed
VZ
1092
1093 RECT rcImg;
1094 SetRect(&rcImg,
abaa31e7
VZ
1095 rect.left + data->ItemMargin.cxLeftWidth
1096 + data->CheckBgMargin.cxLeftWidth
1097 + data->CheckMargin.cxLeftWidth,
1098 rect.top + data->ItemMargin.cyTopHeight
1099 + data->CheckBgMargin.cyTopHeight
1100 + data->CheckMargin.cyTopHeight,
1101 rect.left + data->ItemMargin.cxLeftWidth
1102 + data->CheckBgMargin.cxLeftWidth
1103 + data->CheckMargin.cxLeftWidth
03cc2991 1104 + imgWidth,
abaa31e7
VZ
1105 rect.bottom - data->ItemMargin.cyBottomHeight
1106 - data->CheckBgMargin.cyBottomHeight
1107 - data->CheckMargin.cyBottomHeight);
aa4919ed 1108
98fbab9e
VZ
1109 if ( IsCheckable() && !m_bmpChecked.Ok() )
1110 {
1111 if ( stat & wxODChecked )
1112 {
b8190883 1113 DrawStdCheckMark((WXHDC)hdc, &rcImg, stat);
98fbab9e
VZ
1114 }
1115 }
1116 else
1117 {
1118 wxBitmap bmp;
1119
1120 if ( stat & wxODDisabled )
1121 {
1122 bmp = GetDisabledBitmap();
1123 }
1124
1125 if ( !bmp.Ok() )
1126 {
1127 // for not checkable bitmaps we should always use unchecked one
1128 // because their checked bitmap is not set
1129 bmp = GetBitmap(!IsCheckable() || (stat & wxODChecked));
1130
1131#if wxUSE_IMAGE
1132 if ( bmp.Ok() && stat & wxODDisabled )
1133 {
1134 // we need to grey out the bitmap as we don't have any specific
1135 // disabled bitmap
1136 wxImage imgGrey = bmp.ConvertToImage().ConvertToGreyscale();
1137 if ( imgGrey.Ok() )
1138 bmp = wxBitmap(imgGrey);
1139 }
1140#endif // wxUSE_IMAGE
1141 }
1142
1143 if ( bmp.Ok() )
1144 {
1145 wxMemoryDC dcMem(&dc);
1146 dcMem.SelectObjectAsSource(bmp);
1147
1148 // center bitmap
aa4919ed 1149 int nBmpWidth = bmp.GetWidth(),
98fbab9e
VZ
1150 nBmpHeight = bmp.GetHeight();
1151
1152 // there should be enough space!
aa4919ed 1153 wxASSERT( nBmpWidth <= imgWidth && nBmpHeight <= (rcImg.bottom - rcImg.top) );
98fbab9e 1154
aa4919ed
VZ
1155 int x = rcImg.left + (imgWidth - nBmpWidth) / 2;
1156 int y = rcImg.top + (rcImg.bottom - rcImg.top - nBmpHeight) / 2;
1157 dc.Blit(x, y, nBmpWidth, nBmpHeight, &dcMem, 0, 0, wxCOPY, true);
98fbab9e
VZ
1158 }
1159 }
1160
98fbab9e
VZ
1161 return true;
1162
1163}
1164
ee009313
VZ
1165namespace
1166{
1167
1168// helper function for draw coloured check mark
1169void DrawColorCheckMark(HDC hdc, int x, int y, int cx, int cy, HDC hdcCheckMask, int idxColor)
1170{
1171 const COLORREF colBlack = RGB(0, 0, 0);
1172 const COLORREF colWhite = RGB(255, 255, 255);
1173
b1466486
VZ
1174 HDCTextColChanger changeTextCol(hdc, colBlack);
1175 HDCBgColChanger changeBgCol(hdc, colWhite);
1176 HDCBgModeChanger changeBgMode(hdc, TRANSPARENT);
ee009313
VZ
1177
1178 // memory DC for color bitmap
1179 MemoryHDC hdcMem(hdc);
1180 CompatibleBitmap hbmpMem(hdc, cx, cy);
1181 SelectInHDC selMem(hdcMem, hbmpMem);
1182
1183 RECT rect = { 0, 0, cx, cy };
1184 ::FillRect(hdcMem, &rect, ::GetSysColorBrush(idxColor));
1185
1186 const COLORREF colCheck = ::GetSysColor(idxColor);
1187 if ( colCheck == colWhite )
1188 {
1189 ::BitBlt(hdc, x, y, cx, cy, hdcCheckMask, 0, 0, MERGEPAINT);
1190 ::BitBlt(hdc, x, y, cx, cy, hdcMem, 0, 0, SRCAND);
1191 }
1192 else
1193 {
1194 if ( colCheck != colBlack )
1195 {
1196 const DWORD ROP_DSna = 0x00220326; // dest = (NOT src) AND dest
1197 ::BitBlt(hdcMem, 0, 0, cx, cy, hdcCheckMask, 0, 0, ROP_DSna);
1198 }
1199
1200 ::BitBlt(hdc, x, y, cx, cy, hdcCheckMask, 0, 0, SRCAND);
1201 ::BitBlt(hdc, x, y, cx, cy, hdcMem, 0, 0, SRCPAINT);
1202 }
ee009313
VZ
1203}
1204
1205} // anonymous namespace
1206
b8190883 1207void wxMenuItem::DrawStdCheckMark(WXHDC hdc_, const RECT* rc, wxODStatus stat)
ee009313 1208{
b8190883
VZ
1209 HDC hdc = (HDC)hdc_;
1210
ee009313
VZ
1211#if wxUSE_UXTHEME
1212 wxUxThemeEngine* theme = MenuDrawData::GetUxThemeEngine();
1213 if ( theme )
1214 {
1215 wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU");
1216
03cc2991
VZ
1217 const MenuDrawData* data = MenuDrawData::Get();
1218
1219 // rect for background must be without check margins
abaa31e7
VZ
1220 RECT rcBg = *rc;
1221 data->CheckMargin.UnapplyFrom(rcBg);
03cc2991 1222
ee009313
VZ
1223 POPUPCHECKBACKGROUNDSTATES stateCheckBg = (stat & wxODDisabled)
1224 ? MCB_DISABLED
1225 : MCB_NORMAL;
1226
1227 theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPCHECKBACKGROUND,
03cc2991 1228 stateCheckBg, &rcBg, NULL);
ee009313
VZ
1229
1230 POPUPCHECKSTATES stateCheck;
1231 if ( GetKind() == wxITEM_CHECK )
1232 {
1233 stateCheck = (stat & wxODDisabled) ? MC_CHECKMARKDISABLED
1234 : MC_CHECKMARKNORMAL;
1235 }
1236 else
1237 {
1238 stateCheck = (stat & wxODDisabled) ? MC_BULLETDISABLED
1239 : MC_BULLETNORMAL;
1240 }
1241
1242 theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPCHECK,
1243 stateCheck, rc, NULL);
1244 }
1245 else
1246#endif // wxUSE_UXTHEME
1247 {
1248 int cx = rc->right - rc->left;
1249 int cy = rc->bottom - rc->top;
1250
1251 // first create mask of check mark
1252 MemoryHDC hdcMask(hdc);
1253 MonoBitmap hbmpMask(cx, cy);
1254 SelectInHDC selMask(hdcMask,hbmpMask);
1255
1256 // then draw a check mark into it
1257 UINT stateCheck = (GetKind() == wxITEM_CHECK) ? DFCS_MENUCHECK
1258 : DFCS_MENUBULLET;
1259 RECT rect = { 0, 0, cx, cy };
1260 ::DrawFrameControl(hdcMask, &rect, DFC_MENU, stateCheck);
1261
1262 // first draw shadow if disabled
1263 if ( (stat & wxODDisabled) && !(stat & wxODSelected) )
1264 {
1265 DrawColorCheckMark(hdc, rc->left + 1, rc->top + 1,
1266 cx, cy, hdcMask, COLOR_3DHILIGHT);
1267 }
1268
1269 // then draw a check mark
1270 int color = COLOR_MENUTEXT;
1271 if ( stat & wxODDisabled )
1272 color = COLOR_BTNSHADOW;
1273 else if ( stat & wxODSelected )
1274 color = COLOR_HIGHLIGHTTEXT;
1275
1276 DrawColorCheckMark(hdc, rc->left, rc->top, cx, cy, hdcMask, color);
1277 }
1278}
1279
98fbab9e
VZ
1280void wxMenuItem::GetFontToUse(wxFont& font) const
1281{
1282 font = GetFont();
1283 if ( !font.IsOk() )
aa4919ed 1284 font = MenuDrawData::Get()->Font;
98fbab9e
VZ
1285}
1286
aa4919ed
VZ
1287void wxMenuItem::GetColourToUse(wxODStatus stat, wxColour& colText, wxColour& colBack) const
1288{
1289#if wxUSE_UXTHEME
1290 wxUxThemeEngine* theme = MenuDrawData::GetUxThemeEngine();
1291 if ( theme )
1292 {
1293 wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU");
1294
1295 if ( stat & wxODDisabled)
1296 {
1297 wxRGBToColour(colText, theme->GetThemeSysColor(hTheme, COLOR_GRAYTEXT));
1298 }
1299 else
1300 {
1301 colText = GetTextColour();
1302 if ( !colText.IsOk() )
1303 wxRGBToColour(colText, theme->GetThemeSysColor(hTheme, COLOR_MENUTEXT));
1304 }
1305
1306 if ( stat & wxODSelected )
1307 {
1308 wxRGBToColour(colBack, theme->GetThemeSysColor(hTheme, COLOR_HIGHLIGHT));
1309 }
1310 else
1311 {
1312 colBack = GetBackgroundColour();
1313 if ( !colBack.IsOk() )
1314 wxRGBToColour(colBack, theme->GetThemeSysColor(hTheme, COLOR_MENU));
1315 }
1316 }
1317 else
1318#endif // wxUSE_UXTHEME
1319 {
1320 wxOwnerDrawn::GetColourToUse(stat, colText, colBack);
1321 }
1322}
98fbab9e
VZ
1323#endif // wxUSE_OWNER_DRAWN
1324
974e8d94
VZ
1325// ----------------------------------------------------------------------------
1326// wxMenuItemBase
1327// ----------------------------------------------------------------------------
1328
1329wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
1330 int id,
1331 const wxString& name,
1332 const wxString& help,
d65c269b 1333 wxItemKind kind,
974e8d94
VZ
1334 wxMenu *subMenu)
1335{
d65c269b 1336 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
974e8d94 1337}
1e6feb95
VZ
1338
1339#endif // wxUSE_MENUS