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