Use explicit menu item background if it's given under MSW.
[wxWidgets.git] / src / msw / menuitem.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/menuitem.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 11.11.97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_MENUS
28
29 #include "wx/menuitem.h"
30 #include "wx/stockitem.h"
31
32 #ifndef WX_PRECOMP
33 #include "wx/app.h"
34 #include "wx/dcmemory.h"
35 #include "wx/font.h"
36 #include "wx/bitmap.h"
37 #include "wx/settings.h"
38 #include "wx/window.h"
39 #include "wx/accel.h"
40 #include "wx/string.h"
41 #include "wx/log.h"
42 #include "wx/menu.h"
43 #endif
44
45 #if wxUSE_ACCEL
46 #include "wx/accel.h"
47 #endif // wxUSE_ACCEL
48
49 #include "wx/msw/private.h"
50 #include "wx/msw/dc.h"
51
52 #ifdef __WXWINCE__
53 // Implemented in menu.cpp
54 UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) ;
55 #endif
56
57 #if wxUSE_UXTHEME
58 #include "wx/msw/uxtheme.h"
59 #endif
60
61 // ---------------------------------------------------------------------------
62 // macro
63 // ---------------------------------------------------------------------------
64
65 // hide the ugly cast
66 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
67
68 // ----------------------------------------------------------------------------
69 // helper classes for temporarily changing HDC parameters
70 // ----------------------------------------------------------------------------
71
72 namespace
73 {
74
75 // This class just stores an HDC.
76 class HDCHandler
77 {
78 protected:
79 HDCHandler(HDC hdc) : m_hdc(hdc) { }
80
81 const HDC m_hdc;
82 };
83
84 class HDCTextColChanger : HDCHandler
85 {
86 public:
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
98 private:
99 COLORREF m_colOld;
100 };
101
102 class HDCBgColChanger : HDCHandler
103 {
104 public:
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
116 private:
117 COLORREF m_colOld;
118 };
119
120 class HDCBgModeChanger : HDCHandler
121 {
122 public:
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
134 private:
135 int m_modeOld;
136 };
137
138 } // anonymous namespace
139
140 // ============================================================================
141 // implementation
142 // ============================================================================
143
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
157 #if wxUSE_UXTHEME
158
159 enum 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
174 enum POPUPITEMSTATES
175 {
176 MPI_NORMAL = 1,
177 MPI_HOT = 2,
178 MPI_DISABLED = 3,
179 MPI_DISABLEDHOT = 4,
180 };
181
182 enum POPUPCHECKBACKGROUNDSTATES
183 {
184 MCB_DISABLED = 1,
185 MCB_NORMAL = 2,
186 MCB_BITMAP = 3,
187 };
188
189 enum POPUPCHECKSTATES
190 {
191 MC_CHECKMARKNORMAL = 1,
192 MC_CHECKMARKDISABLED = 2,
193 MC_BULLETNORMAL = 3,
194 MC_BULLETDISABLED = 4,
195 };
196
197 const int TMT_MENUFONT = 803;
198 const int TMT_BORDERSIZE = 2403;
199 const int TMT_CONTENTMARGINS = 3602;
200 const int TMT_SIZINGMARGINS = 3601;
201
202 #endif // wxUSE_UXTHEME
203
204 #endif // wxUSE_OWNER_DRAWN
205
206 // ----------------------------------------------------------------------------
207 // dynamic classes implementation
208 // ----------------------------------------------------------------------------
209
210 #if wxUSE_EXTENDED_RTTI
211
212 bool wxMenuItemStreamingCallback( const wxObject *object, wxWriter * , wxPersister * , wxxVariantArray & )
213 {
214 const wxMenuItem * mitem = dynamic_cast<const wxMenuItem*>(object) ;
215 if ( mitem->GetMenu() && !mitem->GetMenu()->GetTitle().empty() )
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
224 wxBEGIN_ENUM( wxItemKind )
225 wxENUM_MEMBER( wxITEM_SEPARATOR )
226 wxENUM_MEMBER( wxITEM_NORMAL )
227 wxENUM_MEMBER( wxITEM_CHECK )
228 wxENUM_MEMBER( wxITEM_RADIO )
229 wxEND_ENUM( wxItemKind )
230
231 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem, wxObject,"wx/menuitem.h",wxMenuItemStreamingCallback)
232
233 wxBEGIN_PROPERTIES_TABLE(wxMenuItem)
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") )
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") )
238 wxREADONLY_PROPERTY( Kind, wxItemKind , GetKind , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
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"))
243 wxEND_PROPERTIES_TABLE()
244
245 wxBEGIN_HANDLERS_TABLE(wxMenuItem)
246 wxEND_HANDLERS_TABLE()
247
248 wxDIRECT_CONSTRUCTOR_6( wxMenuItem , wxMenu* , Parent , int , Id , wxString , Text , wxString , Help , wxItemKind , Kind , wxMenu* , SubMenu )
249 #else
250 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
251 #endif
252
253 // ----------------------------------------------------------------------------
254 // wxMenuItem
255 // ----------------------------------------------------------------------------
256
257 #if wxUSE_OWNER_DRAWN
258
259 namespace
260 {
261
262 // helper class to keep information about metrics and other stuff
263 // needed for measuring and drawing menu item
264 class MenuDrawData
265 {
266 public:
267 // Wrapper around standard MARGINS structure providing some helper
268 // functions and automatically initializing the margin fields to 0.
269 struct Margins : MARGINS
270 {
271 Margins()
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 }
297 };
298
299 Margins ItemMargin; // popup item margins
300
301 Margins CheckMargin; // popup check margins
302 Margins CheckBgMargin; // popup check background margins
303
304 Margins ArrowMargin; // popup submenu arrow margins
305
306 Margins SeparatorMargin; // popup separator margins
307
308 SIZE CheckSize; // popup check size metric
309 SIZE ArrowSize; // popup submenu arrow size metric
310 SIZE SeparatorSize; // popup separator size metric
311
312 int TextBorder; // popup border space between
313 // item text and gutter
314
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
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 {
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
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 {
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
390 private:
391 void Init();
392
393 static MenuDrawData* ms_instance;
394 };
395
396 MenuDrawData* MenuDrawData::ms_instance = NULL;
397
398 void 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,
409 &ItemMargin);
410
411 theme->GetThemeMargins(hTheme, NULL, MENU_POPUPCHECK, 0,
412 TMT_CONTENTMARGINS, NULL,
413 &CheckMargin);
414 theme->GetThemeMargins(hTheme, NULL, MENU_POPUPCHECKBACKGROUND, 0,
415 TMT_CONTENTMARGINS, NULL,
416 &CheckBgMargin);
417
418 theme->GetThemeMargins(hTheme, NULL, MENU_POPUPSUBMENU, 0,
419 TMT_CONTENTMARGINS, NULL,
420 &ArrowMargin);
421
422 theme->GetThemeMargins(hTheme, NULL, MENU_POPUPSEPARATOR, 0,
423 TMT_SIZINGMARGINS, NULL,
424 &SeparatorMargin);
425
426 theme->GetThemePartSize(hTheme, NULL, MENU_POPUPCHECK, 0,
427 NULL, TS_TRUE, &CheckSize);
428
429 theme->GetThemePartSize(hTheme, NULL, MENU_POPUPSUBMENU, 0,
430 NULL, TS_TRUE, &ArrowSize);
431
432 theme->GetThemePartSize(hTheme, NULL, MENU_POPUPSEPARATOR, 0,
433 NULL, TS_TRUE, &SeparatorSize);
434
435 theme->GetThemeInt(hTheme, MENU_POPUPBACKGROUND, 0, TMT_BORDERSIZE, &TextBorder);
436
437 AccelBorder = 34;
438 ArrowBorder = 0;
439
440 Offset = -14;
441
442 wxUxThemeFont themeFont;
443 theme->GetThemeSysFont(hTheme, TMT_MENUFONT, themeFont.GetPtr());
444 Font = wxFont(themeFont.GetLOGFONT());
445
446 Theme = true;
447
448 // native menu doesn't uses the vertical margins
449 ItemMargin.cyTopHeight =
450 ItemMargin.cyBottomHeight = 0;
451
452 // native menu uses small top margin for separator
453 if ( SeparatorMargin.cyTopHeight >= 2 )
454 SeparatorMargin.cyTopHeight -= 2;
455 }
456 else
457 #endif // wxUSE_UXTHEME
458 {
459 const NONCLIENTMETRICS& metrics = wxMSWImpl::GetNonClientMetrics();
460
461 CheckMargin.cxLeftWidth =
462 CheckMargin.cxRightWidth = ::GetSystemMetrics(SM_CXEDGE);
463 CheckMargin.cyTopHeight =
464 CheckMargin.cyBottomHeight = ::GetSystemMetrics(SM_CYEDGE);
465
466 CheckSize.cx = ::GetSystemMetrics(SM_CXMENUCHECK);
467 CheckSize.cy = ::GetSystemMetrics(SM_CYMENUCHECK);
468
469 ArrowSize = CheckSize;
470
471 // separator height with margins
472 int sepFullSize = metrics.iMenuHeight / 2;
473
474 SeparatorMargin.cxLeftWidth =
475 SeparatorMargin.cxRightWidth = 1;
476 SeparatorMargin.cyTopHeight =
477 SeparatorMargin.cyBottomHeight = sepFullSize / 2 - 1;
478
479 SeparatorSize.cx = 1;
480 SeparatorSize.cy = sepFullSize - SeparatorMargin.GetTotalY();
481
482 TextBorder = 0;
483 AccelBorder = 8;
484 ArrowBorder = 6;
485
486 Offset = -12;
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
506
507 #endif // wxUSE_OWNER_DRAWN
508
509
510 // ctor & dtor
511 // -----------
512
513 wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
514 int id,
515 const wxString& text,
516 const wxString& strHelp,
517 wxItemKind kind,
518 wxMenu *pSubMenu)
519 : wxMenuItemBase(pParentMenu, id, text, strHelp, kind, pSubMenu)
520 {
521 Init();
522 }
523
524 #if WXWIN_COMPATIBILITY_2_8
525 wxMenuItem::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)
533 {
534 Init();
535 }
536 #endif
537
538 void wxMenuItem::Init()
539 {
540 m_radioGroup.start = -1;
541 m_isRadioGroupStart = false;
542
543 #if wxUSE_OWNER_DRAWN
544
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);
551
552 // setting default colors switched ownerdraw on: switch it off again
553 SetOwnerDrawn(false);
554
555 // switch ownerdraw back on if using a non default margin
556 if ( !IsSeparator() )
557 SetMarginWidth(GetMarginWidth());
558
559 #endif // wxUSE_OWNER_DRAWN
560 }
561
562 wxMenuItem::~wxMenuItem()
563 {
564 }
565
566 // misc
567 // ----
568
569 // return the id for calling Win32 API functions
570 WXWPARAM wxMenuItem::GetMSWId() const
571 {
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
576 return m_subMenu ? wxPtrToUInt(m_subMenu->GetHMenu())
577 : static_cast<unsigned short>(GetId());
578 }
579
580 // get item state
581 // --------------
582
583 bool wxMenuItem::IsChecked() const
584 {
585 // fix that RTTI is always getting the correct state (separators cannot be
586 // checked, but the Windows call below returns true
587 if ( IsSeparator() )
588 return false;
589
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);
599
600 return (flag & MF_CHECKED) != 0;
601 }
602
603 // radio group stuff
604 // -----------------
605
606 void wxMenuItem::SetAsRadioGroupStart()
607 {
608 m_isRadioGroupStart = true;
609 }
610
611 void wxMenuItem::SetRadioGroupStart(int start)
612 {
613 wxASSERT_MSG( !m_isRadioGroupStart,
614 wxT("should only be called for the next radio items") );
615
616 m_radioGroup.start = start;
617 }
618
619 void wxMenuItem::SetRadioGroupEnd(int end)
620 {
621 wxASSERT_MSG( m_isRadioGroupStart,
622 wxT("should only be called for the first radio item") );
623
624 m_radioGroup.end = end;
625 }
626
627 // change item state
628 // -----------------
629
630 void wxMenuItem::Enable(bool enable)
631 {
632 if ( m_isEnabled == enable )
633 return;
634
635 if ( m_parentMenu )
636 {
637 long rc = EnableMenuItem(GetHMenuOf(m_parentMenu),
638 GetMSWId(),
639 MF_BYCOMMAND |
640 (enable ? MF_ENABLED : MF_GRAYED));
641
642 if ( rc == -1 )
643 {
644 wxLogLastError(wxT("EnableMenuItem"));
645 }
646 }
647
648 wxMenuItemBase::Enable(enable);
649 }
650
651 void wxMenuItem::Check(bool check)
652 {
653 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
654
655 if ( m_isChecked == check )
656 return;
657
658 if ( m_parentMenu )
659 {
660 int flags = check ? MF_CHECKED : MF_UNCHECKED;
661 HMENU hmenu = GetHMenuOf(m_parentMenu);
662
663 if ( GetKind() == wxITEM_RADIO )
664 {
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,
674 wxT("menuitem not found in the menu items list?") );
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 }
692
693 #ifdef __WIN32__
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,
698 wxT("invalid ::CheckMenuRadioItem() parameter(s)") );
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 {
706 wxLogLastError(wxT("CheckMenuRadioItem"));
707 }
708 #endif // __WIN32__
709
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++ )
713 {
714 if ( n != pos )
715 {
716 node->GetData()->m_isChecked = false;
717 }
718
719 node = node->GetNext();
720 }
721 }
722 else // check item
723 {
724 if ( ::CheckMenuItem(hmenu,
725 GetMSWId(),
726 MF_BYCOMMAND | flags) == (DWORD)-1 )
727 {
728 wxFAIL_MSG(wxT("CheckMenuItem() failed, item not in the menu?"));
729 }
730 }
731 }
732
733 wxMenuItemBase::Check(check);
734 }
735
736 void wxMenuItem::SetItemLabel(const wxString& txt)
737 {
738 wxString text = txt;
739
740 // don't do anything if label didn't change
741 if ( m_text == txt )
742 return;
743
744 // wxMenuItemBase will do stock ID checks
745 wxMenuItemBase::SetItemLabel(text);
746
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
749 if ( !m_parentMenu )
750 return;
751
752 #if wxUSE_ACCEL
753 m_parentMenu->UpdateAccel(this);
754 #endif // wxUSE_ACCEL
755
756 const UINT id = GetMSWId();
757 HMENU hMenu = GetHMenuOf(m_parentMenu);
758 if ( !hMenu || ::GetMenuState(hMenu, id, MF_BYCOMMAND) == (UINT)-1 )
759 return;
760
761 #if wxUSE_OWNER_DRAWN
762 if ( IsOwnerDrawn() )
763 {
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;
767 }
768 #endif // owner drawn
769
770 // update the text of the native menu item
771 WinStruct<MENUITEMINFO> info;
772
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;
777 info.fMask = MIIM_STATE |
778 MIIM_ID |
779 MIIM_SUBMENU |
780 MIIM_CHECKMARKS |
781 MIIM_DATA;
782 if ( isLaterThanWin95 )
783 info.fMask |= MIIM_BITMAP | MIIM_FTYPE;
784 else
785 info.fMask |= MIIM_TYPE;
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"));
800 }
801 }
802
803 #if wxUSE_OWNER_DRAWN
804
805 int 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
820 wxString wxMenuItem::GetName() const
821 {
822 return GetItemLabelText();
823 }
824
825 bool wxMenuItem::OnMeasureItem(size_t *width, size_t *height)
826 {
827 const MenuDrawData* data = MenuDrawData::Get();
828
829 if ( IsOwnerDrawn() )
830 {
831 *width = data->ItemMargin.GetTotalX();
832 *height = data->ItemMargin.GetTotalY();
833
834 if ( IsSeparator() )
835 {
836 *width += data->SeparatorSize.cx
837 + data->SeparatorMargin.GetTotalX();
838 *height += data->SeparatorSize.cy
839 + data->SeparatorMargin.GetTotalY();
840 return true;
841 }
842
843 wxString str = GetName();
844
845 wxMemoryDC dc;
846 wxFont font;
847 GetFontToUse(font);
848 dc.SetFont(font);
849
850 wxCoord w, h;
851 dc.GetTextExtent(str, &w, &h);
852
853 *width = data->TextBorder + w + data->AccelBorder;
854 *height = h;
855
856 w = m_parentMenu->GetMaxAccelWidth();
857 if ( w > 0 )
858 *width += w + data->ArrowBorder;
859
860 *width += data->Offset;
861 *width += data->ArrowMargin.GetTotalX() + data->ArrowSize.cx;
862 }
863 else // don't draw the text, just the bitmap (if any)
864 {
865 *width = 0;
866 *height = 0;
867 }
868
869 // bitmap
870
871 if ( IsOwnerDrawn() )
872 {
873 // width of menu icon with margins in ownerdrawn menu
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)
880 + data->CheckMargin.GetTotalX();
881
882 *width += imgWidth + data->CheckBgMargin.GetTotalX();
883 }
884
885 if ( m_bmpChecked.IsOk() || m_bmpChecked.IsOk() )
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());
890 size_t widthtBmp = wxMax(m_bmpChecked.GetWidth(), m_bmpUnchecked.GetWidth());
891
892 if ( IsOwnerDrawn() )
893 {
894 heightBmp += data->CheckMargin.GetTotalY();
895 }
896 else
897 {
898 // we must allocate enough space for the bitmap
899 *width += widthtBmp;
900 }
901
902 // Is BMP height larger than text height?
903 if ( *height < heightBmp )
904 *height = heightBmp;
905 }
906
907 // make sure that this item is at least as tall as the system menu height
908 const size_t menuHeight = data->CheckMargin.GetTotalY()
909 + data->CheckSize.cy;
910 if (*height < menuHeight)
911 *height = menuHeight;
912
913 return true;
914 }
915
916 bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc,
917 wxODAction WXUNUSED(act), wxODStatus stat)
918 {
919 const MenuDrawData* data = MenuDrawData::Get();
920
921 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
922 HDC hdc = GetHdcOf(*impl);
923
924 RECT rect;
925 wxCopyRectToRECT(rc, rect);
926
927 int imgWidth = wxMax(GetMarginWidth(), data->CheckSize.cx);
928
929 if ( IsOwnerDrawn() )
930 {
931 // font and colors to use
932 wxFont font;
933 GetFontToUse(font);
934
935 wxColour colText, colBack;
936 GetColourToUse(stat, colText, colBack);
937
938 // calculate metrics of item parts
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
949 + imgWidth
950 + data->CheckMargin.cxRightWidth
951 + data->CheckBgMargin.cxRightWidth;
952
953 RECT rcText = rcSelection;
954 rcText.left = rcGutter.right + data->TextBorder;
955
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
961 #if wxUSE_UXTHEME
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();
967 if ( theme )
968 {
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 }
983
984 wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU");
985
986 if ( theme->IsThemeBackgroundPartiallyTransparent(hTheme,
987 MENU_POPUPITEM, state) )
988 {
989 theme->DrawThemeBackground(hTheme, hdc,
990 MENU_POPUPBACKGROUND,
991 0, &rect, NULL);
992 }
993
994 theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPGUTTER,
995 0, &rcGutter, NULL);
996
997 if ( IsSeparator() )
998 {
999 rcSeparator.left = rcGutter.right;
1000 theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPSEPARATOR,
1001 0, &rcSeparator, NULL);
1002 return true;
1003 }
1004
1005 theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPITEM,
1006 state, &rcSelection, NULL);
1007
1008 }
1009 else
1010 #endif // wxUSE_UXTHEME
1011 {
1012 if ( IsSeparator() )
1013 {
1014 DrawEdge(hdc, &rcSeparator, EDGE_ETCHED, BF_TOP);
1015 return true;
1016 }
1017
1018 AutoHBRUSH hbr(colBack.GetPixel());
1019 SelectInHDC selBrush(hdc, hbr);
1020 ::FillRect(hdc, &rcSelection, hbr);
1021 }
1022
1023
1024 // draw text label
1025 // using native API because it recognizes '&'
1026
1027 HDCTextColChanger changeTextCol(hdc, colText.GetPixel());
1028 HDCBgColChanger changeBgCol(hdc, colBack.GetPixel());
1029 HDCBgModeChanger changeBgMode(hdc, TRANSPARENT);
1030
1031 SelectInHDC selFont(hdc, GetHfontOf(font));
1032
1033
1034 // item text name without mnemonic for calculating size
1035 wxString text = GetName();
1036
1037 SIZE textSize;
1038 ::GetTextExtentPoint32(hdc, text.c_str(), text.length(), &textSize);
1039
1040 // item text name with mnemonic
1041 text = GetItemLabel().BeforeFirst('\t');
1042
1043 int flags = DST_PREFIXTEXT;
1044 // themes menu is using specified color for disabled labels
1045 if ( data->MenuLayout() == MenuDrawData::Classic &&
1046 (stat & wxODDisabled) && !(stat & wxODSelected) )
1047 flags |= DSS_DISABLED;
1048
1049 if ( (stat & wxODHidePrefix) && !data->AlwaysShowCues )
1050 flags |= DSS_HIDEPREFIX;
1051
1052 int x = rcText.left;
1053 int y = rcText.top + (rcText.bottom - rcText.top - textSize.cy) / 2;
1054
1055 ::DrawState(hdc, NULL, NULL, (LPARAM)text.wx_str(),
1056 text.length(), x, y, 0, 0, flags);
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 {
1064 SIZE accelSize;
1065 ::GetTextExtentPoint32(hdc, accel.c_str(), accel.length(), &accelSize);
1066
1067 int flags = DST_TEXT;
1068 // themes menu is using specified color for disabled labels
1069 if ( data->MenuLayout() == MenuDrawData::Classic &&
1070 (stat & wxODDisabled) && !(stat & wxODSelected) )
1071 flags |= DSS_DISABLED;
1072
1073 int x = rcText.right - data->ArrowMargin.GetTotalX()
1074 - data->ArrowSize.cx
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();
1082
1083 int y = rcText.top + (rcText.bottom - rcText.top - accelSize.cy) / 2;
1084
1085 ::DrawState(hdc, NULL, NULL, (LPARAM)accel.wx_str(),
1086 accel.length(), x, y, 0, 0, flags);
1087 }
1088 }
1089
1090
1091 // draw the bitmap
1092
1093 RECT rcImg;
1094 SetRect(&rcImg,
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
1104 + imgWidth,
1105 rect.bottom - data->ItemMargin.cyBottomHeight
1106 - data->CheckBgMargin.cyBottomHeight
1107 - data->CheckMargin.cyBottomHeight);
1108
1109 if ( IsCheckable() && !m_bmpChecked.Ok() )
1110 {
1111 if ( stat & wxODChecked )
1112 {
1113 DrawStdCheckMark((WXHDC)hdc, &rcImg, stat);
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
1149 int nBmpWidth = bmp.GetWidth(),
1150 nBmpHeight = bmp.GetHeight();
1151
1152 // there should be enough space!
1153 wxASSERT( nBmpWidth <= imgWidth && nBmpHeight <= (rcImg.bottom - rcImg.top) );
1154
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);
1158 }
1159 }
1160
1161 return true;
1162
1163 }
1164
1165 namespace
1166 {
1167
1168 // helper function for draw coloured check mark
1169 void 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
1174 HDCTextColChanger changeTextCol(hdc, colBlack);
1175 HDCBgColChanger changeBgCol(hdc, colWhite);
1176 HDCBgModeChanger changeBgMode(hdc, TRANSPARENT);
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 }
1203 }
1204
1205 } // anonymous namespace
1206
1207 void wxMenuItem::DrawStdCheckMark(WXHDC hdc_, const RECT* rc, wxODStatus stat)
1208 {
1209 HDC hdc = (HDC)hdc_;
1210
1211 #if wxUSE_UXTHEME
1212 wxUxThemeEngine* theme = MenuDrawData::GetUxThemeEngine();
1213 if ( theme )
1214 {
1215 wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU");
1216
1217 const MenuDrawData* data = MenuDrawData::Get();
1218
1219 // rect for background must be without check margins
1220 RECT rcBg = *rc;
1221 data->CheckMargin.UnapplyFrom(rcBg);
1222
1223 POPUPCHECKBACKGROUNDSTATES stateCheckBg = (stat & wxODDisabled)
1224 ? MCB_DISABLED
1225 : MCB_NORMAL;
1226
1227 theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPCHECKBACKGROUND,
1228 stateCheckBg, &rcBg, NULL);
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
1280 void wxMenuItem::GetFontToUse(wxFont& font) const
1281 {
1282 font = GetFont();
1283 if ( !font.IsOk() )
1284 font = MenuDrawData::Get()->Font;
1285 }
1286
1287 void 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 }
1323 #endif // wxUSE_OWNER_DRAWN
1324
1325 // ----------------------------------------------------------------------------
1326 // wxMenuItemBase
1327 // ----------------------------------------------------------------------------
1328
1329 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
1330 int id,
1331 const wxString& name,
1332 const wxString& help,
1333 wxItemKind kind,
1334 wxMenu *subMenu)
1335 {
1336 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
1337 }
1338
1339 #endif // wxUSE_MENUS