]> git.saurik.com Git - wxWidgets.git/blob - src/msw/menuitem.cpp
Use theme functions for drawing owner-drawn menus.
[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 SeparatorMargin; // popup separator margins
215
216 SIZE CheckSize; // popup check size metric
217 SIZE SeparatorSize; // popup separator size metric
218
219 int AccelBorder; // popup border space between
220 // item text and accelerator
221 int TextBorder; // popup border space between
222 // item text and gutter
223
224 wxFont Font; // default menu font
225
226 bool AlwaysShowCues; // must keyboard cues always be shown?
227
228 bool Theme; // is data initialized for FullTheme?
229
230 static const MenuDrawData* Get()
231 {
232 #if wxUSE_UXTHEME
233 bool theme = MenuLayout() == FullTheme;
234 if ( ms_instance->Theme != theme )
235 ms_instance->Init();
236 #endif // wxUSE_UXTHEME
237 return ms_instance;
238 }
239
240 MenuDrawData()
241 {
242 ms_instance = this;
243 Init();
244 }
245
246
247 // get the theme engine or NULL if themes
248 // are not available or not supported on menu
249 static wxUxThemeEngine *GetUxThemeEngine()
250 {
251 #if wxUSE_UXTHEME
252 if ( MenuLayout() == FullTheme )
253 return wxUxThemeEngine::GetIfActive();
254 #endif // wxUSE_UXTHEME
255 return NULL;
256 }
257
258
259 enum MenuLayoutType
260 {
261 FullTheme, // full menu themes (Vista or new)
262 PseudoTheme, // pseudo menu themes (on XP)
263 Classic
264 };
265
266 static MenuLayoutType MenuLayout()
267 {
268 MenuLayoutType menu = Classic;
269 #if wxUSE_UXTHEME
270 if ( wxUxThemeEngine::GetIfActive() != NULL )
271 {
272 static wxWinVersion ver = wxGetWinVersion();
273 if ( ver >= wxWinVersion_Vista )
274 menu = FullTheme;
275 else if ( ver == wxWinVersion_XP )
276 menu = PseudoTheme;
277 }
278 #endif // wxUSE_UXTHEME
279 return menu;
280 }
281
282 private:
283 void Init();
284
285 static MenuDrawData* ms_instance;
286 };
287
288 MenuDrawData* MenuDrawData::ms_instance = NULL;
289
290 MenuDrawData s_menuData;
291
292 void MenuDrawData::Init()
293 {
294 #if wxUSE_UXTHEME
295 wxUxThemeEngine* theme = GetUxThemeEngine();
296 if ( theme )
297 {
298 wxWindow* window = static_cast<wxApp*>(wxApp::GetInstance())->GetTopWindow();
299 wxUxThemeHandle hTheme(window, L"MENU");
300
301 theme->GetThemeMargins(hTheme, NULL, MENU_POPUPITEM, 0,
302 TMT_CONTENTMARGINS, NULL,
303 reinterpret_cast<MARGINS*>(&ItemMargin));
304
305 theme->GetThemeMargins(hTheme, NULL, MENU_POPUPCHECK, 0,
306 TMT_CONTENTMARGINS, NULL,
307 reinterpret_cast<MARGINS*>(&CheckMargin));
308 theme->GetThemeMargins(hTheme, NULL, MENU_POPUPCHECKBACKGROUND, 0,
309 TMT_CONTENTMARGINS, NULL,
310 reinterpret_cast<MARGINS*>(&CheckBgMargin));
311
312 theme->GetThemeMargins(hTheme, NULL, MENU_POPUPSEPARATOR, 0,
313 TMT_SIZINGMARGINS, NULL,
314 reinterpret_cast<MARGINS*>(&SeparatorMargin));
315
316 theme->GetThemePartSize(hTheme, NULL, MENU_POPUPCHECK, 0,
317 NULL, TS_TRUE, &CheckSize);
318
319 theme->GetThemePartSize(hTheme, NULL, MENU_POPUPSEPARATOR, 0,
320 NULL, TS_TRUE, &SeparatorSize);
321
322 theme->GetThemeInt(hTheme, MENU_POPUPBORDERS, 0, TMT_BORDERSIZE, &AccelBorder);
323 theme->GetThemeInt(hTheme, MENU_POPUPBACKGROUND, 0, TMT_BORDERSIZE, &TextBorder);
324
325 wxNativeFontInfo fontInfo;
326 theme->GetThemeSysFont(hTheme, TMT_MENUFONT, &fontInfo.lf);
327 Font = wxFont(fontInfo);
328
329 Theme = true;
330
331 // native menu doesn't uses the vertical margins
332 ItemMargin.top = ItemMargin.bottom = 0;
333
334 // native menu uses small top margin for separator
335 if ( SeparatorMargin.top >= 2 )
336 SeparatorMargin.top -= 2;
337 }
338 else
339 #endif // wxUSE_UXTHEME
340 {
341 const NONCLIENTMETRICS& metrics = wxMSWImpl::GetNonClientMetrics();
342
343 ItemMargin = Margins();
344
345 CheckMargin.left =
346 CheckMargin.right = ::GetSystemMetrics(SM_CXEDGE);
347 CheckMargin.top =
348 CheckMargin.bottom = ::GetSystemMetrics(SM_CYEDGE);
349
350 CheckBgMargin = Margins();
351
352 CheckSize.cx = ::GetSystemMetrics(SM_CXMENUCHECK);
353 CheckSize.cy = ::GetSystemMetrics(SM_CYMENUCHECK);
354
355 // separator height with margins
356 int sepFullSize = metrics.iMenuHeight / 2;
357
358 SeparatorMargin.left =
359 SeparatorMargin.right = 1;
360 SeparatorMargin.top =
361 SeparatorMargin.bottom = sepFullSize / 2 - 1;
362
363 SeparatorSize.cx = 1;
364 SeparatorSize.cy = sepFullSize - SeparatorMargin.top - SeparatorMargin.bottom;
365
366 TextBorder = 0;
367 AccelBorder = 8;
368
369 Font = wxFont(wxNativeFontInfo(metrics.lfMenuFont));
370
371 Theme = false;
372 }
373
374 int value;
375 if ( ::SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &value, 0) == 0 )
376 {
377 // if it's not supported, we must be on an old Windows version
378 // which always shows them
379 value = 1;
380 }
381
382 AlwaysShowCues = value == 1;
383
384 }
385
386 } // anonymous namespace
387
388 #endif // wxUSE_OWNER_DRAWN
389
390
391 // ctor & dtor
392 // -----------
393
394 wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
395 int id,
396 const wxString& text,
397 const wxString& strHelp,
398 wxItemKind kind,
399 wxMenu *pSubMenu)
400 : wxMenuItemBase(pParentMenu, id, text, strHelp, kind, pSubMenu)
401 {
402 Init();
403 }
404
405 #if WXWIN_COMPATIBILITY_2_8
406 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
407 int id,
408 const wxString& text,
409 const wxString& help,
410 bool isCheckable,
411 wxMenu *subMenu)
412 : wxMenuItemBase(parentMenu, id, text, help,
413 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
414 {
415 Init();
416 }
417 #endif
418
419 void wxMenuItem::Init()
420 {
421 m_radioGroup.start = -1;
422 m_isRadioGroupStart = false;
423
424 #if wxUSE_OWNER_DRAWN
425
426 // when the color is not valid, wxOwnerDraw takes the default ones.
427 // If we set the colors here and they are changed by the user during
428 // the execution, then the colors are not updated until the application
429 // is restarted and our menus look bad
430 SetTextColour(wxNullColour);
431 SetBackgroundColour(wxNullColour);
432
433 // setting default colors switched ownerdraw on: switch it off again
434 SetOwnerDrawn(false);
435
436 // switch ownerdraw back on if using a non default margin
437 if ( !IsSeparator() )
438 SetMarginWidth(GetMarginWidth());
439
440 #endif // wxUSE_OWNER_DRAWN
441 }
442
443 wxMenuItem::~wxMenuItem()
444 {
445 }
446
447 // misc
448 // ----
449
450 // return the id for calling Win32 API functions
451 WXWPARAM wxMenuItem::GetMSWId() const
452 {
453 // we must use ids in unsigned short range with Windows functions, if we
454 // pass ids > USHRT_MAX to them they get very confused (e.g. start
455 // generating WM_COMMAND messages with negative high word of wParam), so
456 // use the cast to ensure the id is in range
457 return m_subMenu ? wxPtrToUInt(m_subMenu->GetHMenu())
458 : static_cast<unsigned short>(GetId());
459 }
460
461 // get item state
462 // --------------
463
464 bool wxMenuItem::IsChecked() const
465 {
466 // fix that RTTI is always getting the correct state (separators cannot be
467 // checked, but the Windows call below returns true
468 if ( IsSeparator() )
469 return false;
470
471 // the item might not be attached to a menu yet
472 //
473 // TODO: shouldn't we just always call the base class version? It seems
474 // like it ought to always be in sync
475 if ( !m_parentMenu )
476 return wxMenuItemBase::IsChecked();
477
478 HMENU hmenu = GetHMenuOf(m_parentMenu);
479 int flag = ::GetMenuState(hmenu, GetMSWId(), MF_BYCOMMAND);
480
481 return (flag & MF_CHECKED) != 0;
482 }
483
484 // radio group stuff
485 // -----------------
486
487 void wxMenuItem::SetAsRadioGroupStart()
488 {
489 m_isRadioGroupStart = true;
490 }
491
492 void wxMenuItem::SetRadioGroupStart(int start)
493 {
494 wxASSERT_MSG( !m_isRadioGroupStart,
495 wxT("should only be called for the next radio items") );
496
497 m_radioGroup.start = start;
498 }
499
500 void wxMenuItem::SetRadioGroupEnd(int end)
501 {
502 wxASSERT_MSG( m_isRadioGroupStart,
503 wxT("should only be called for the first radio item") );
504
505 m_radioGroup.end = end;
506 }
507
508 // change item state
509 // -----------------
510
511 void wxMenuItem::Enable(bool enable)
512 {
513 if ( m_isEnabled == enable )
514 return;
515
516 if ( m_parentMenu )
517 {
518 long rc = EnableMenuItem(GetHMenuOf(m_parentMenu),
519 GetMSWId(),
520 MF_BYCOMMAND |
521 (enable ? MF_ENABLED : MF_GRAYED));
522
523 if ( rc == -1 )
524 {
525 wxLogLastError(wxT("EnableMenuItem"));
526 }
527 }
528
529 wxMenuItemBase::Enable(enable);
530 }
531
532 void wxMenuItem::Check(bool check)
533 {
534 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
535
536 if ( m_isChecked == check )
537 return;
538
539 if ( m_parentMenu )
540 {
541 int flags = check ? MF_CHECKED : MF_UNCHECKED;
542 HMENU hmenu = GetHMenuOf(m_parentMenu);
543
544 if ( GetKind() == wxITEM_RADIO )
545 {
546 // it doesn't make sense to uncheck a radio item -- what would this
547 // do?
548 if ( !check )
549 return;
550
551 // get the index of this item in the menu
552 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
553 int pos = items.IndexOf(this);
554 wxCHECK_RET( pos != wxNOT_FOUND,
555 wxT("menuitem not found in the menu items list?") );
556
557 // get the radio group range
558 int start,
559 end;
560
561 if ( m_isRadioGroupStart )
562 {
563 // we already have all information we need
564 start = pos;
565 end = m_radioGroup.end;
566 }
567 else // next radio group item
568 {
569 // get the radio group end from the start item
570 start = m_radioGroup.start;
571 end = items.Item(start)->GetData()->m_radioGroup.end;
572 }
573
574 #ifdef __WIN32__
575 // calling CheckMenuRadioItem() with such parameters hangs my system
576 // (NT4 SP6) and I suspect this could happen to the others as well,
577 // so don't do it!
578 wxCHECK_RET( start != -1 && end != -1,
579 wxT("invalid ::CheckMenuRadioItem() parameter(s)") );
580
581 if ( !::CheckMenuRadioItem(hmenu,
582 start, // the first radio group item
583 end, // the last one
584 pos, // the one to check
585 MF_BYPOSITION) )
586 {
587 wxLogLastError(wxT("CheckMenuRadioItem"));
588 }
589 #endif // __WIN32__
590
591 // also uncheck all the other items in this radio group
592 wxMenuItemList::compatibility_iterator node = items.Item(start);
593 for ( int n = start; n <= end && node; n++ )
594 {
595 if ( n != pos )
596 {
597 node->GetData()->m_isChecked = false;
598 }
599
600 node = node->GetNext();
601 }
602 }
603 else // check item
604 {
605 if ( ::CheckMenuItem(hmenu,
606 GetMSWId(),
607 MF_BYCOMMAND | flags) == (DWORD)-1 )
608 {
609 wxFAIL_MSG(wxT("CheckMenuItem() failed, item not in the menu?"));
610 }
611 }
612 }
613
614 wxMenuItemBase::Check(check);
615 }
616
617 void wxMenuItem::SetItemLabel(const wxString& txt)
618 {
619 wxString text = txt;
620
621 // don't do anything if label didn't change
622 if ( m_text == txt )
623 return;
624
625 // wxMenuItemBase will do stock ID checks
626 wxMenuItemBase::SetItemLabel(text);
627
628 // the item can be not attached to any menu yet and SetItemLabel() is still
629 // valid to call in this case and should do nothing else
630 if ( !m_parentMenu )
631 return;
632
633 #if wxUSE_ACCEL
634 m_parentMenu->UpdateAccel(this);
635 #endif // wxUSE_ACCEL
636
637 const UINT id = GetMSWId();
638 HMENU hMenu = GetHMenuOf(m_parentMenu);
639 if ( !hMenu || ::GetMenuState(hMenu, id, MF_BYCOMMAND) == (UINT)-1 )
640 return;
641
642 #if wxUSE_OWNER_DRAWN
643 if ( IsOwnerDrawn() )
644 {
645 // we don't need to do anything for owner drawn items, they will redraw
646 // themselves using the new text the next time they're displayed
647 return;
648 }
649 #endif // owner drawn
650
651 // update the text of the native menu item
652 WinStruct<MENUITEMINFO> info;
653
654 // surprisingly, calling SetMenuItemInfo() with just MIIM_STRING doesn't
655 // work as it resets the menu bitmap, so we need to first get the old item
656 // state and then modify it
657 const bool isLaterThanWin95 = wxGetWinVersion() > wxWinVersion_95;
658 info.fMask = MIIM_STATE |
659 MIIM_ID |
660 MIIM_SUBMENU |
661 MIIM_CHECKMARKS |
662 MIIM_DATA;
663 if ( isLaterThanWin95 )
664 info.fMask |= MIIM_BITMAP | MIIM_FTYPE;
665 else
666 info.fMask |= MIIM_TYPE;
667 if ( !::GetMenuItemInfo(hMenu, id, FALSE, &info) )
668 {
669 wxLogLastError(wxT("GetMenuItemInfo"));
670 return;
671 }
672
673 if ( isLaterThanWin95 )
674 info.fMask |= MIIM_STRING;
675 //else: MIIM_TYPE already specified
676 info.dwTypeData = (LPTSTR)m_text.wx_str();
677 info.cch = m_text.length();
678 if ( !::SetMenuItemInfo(hMenu, id, FALSE, &info) )
679 {
680 wxLogLastError(wxT("SetMenuItemInfo"));
681 }
682 }
683
684 #if wxUSE_OWNER_DRAWN
685
686 wxString wxMenuItem::GetName() const
687 {
688 return GetItemLabelText();
689 }
690
691 bool wxMenuItem::OnMeasureItem(size_t *width, size_t *height)
692 {
693 const MenuDrawData* data = MenuDrawData::Get();
694
695 if ( IsOwnerDrawn() )
696 {
697 *width = data->ItemMargin.left + data->ItemMargin.right;
698 *height = data->ItemMargin.top + data->ItemMargin.bottom;
699
700 if ( IsSeparator() )
701 {
702 *width += data->SeparatorSize.cx
703 + data->SeparatorMargin.left + data->SeparatorMargin.right;
704 *height += data->SeparatorSize.cy
705 + data->SeparatorMargin.top + data->SeparatorMargin.bottom;
706 return true;
707 }
708
709 wxString str = GetItemLabel();
710
711 // text and accel separator char removal
712 str.Replace(wxT('\t'), wxEmptyString);
713
714 wxMemoryDC dc;
715 wxFont font;
716 GetFontToUse(font);
717 dc.SetFont(font);
718
719 wxCoord w, h;
720 dc.GetTextExtent(str, &w, &h);
721
722 *width = w + data->TextBorder + data->AccelBorder;
723 *height = h;
724
725 // system added space at the end of the menu for the submenu expansion
726 // arrow, but we must add a 4-pixel separator for better apperance
727 *width += 4;
728 }
729 else // don't draw the text, just the bitmap (if any)
730 {
731 *width = 0;
732 *height = 0;
733 }
734
735 // bitmap
736
737 if ( IsOwnerDrawn() )
738 {
739 // width of menu icon in ownerdrawn menu
740 // if any bitmap is not set, the width of space reserved for icon
741 // image is equal to the width of std check mark,
742 // if bitmap is set, then the width is set to the width of the widest
743 // bitmap in menu (GetMarginWidth()) unless std check mark is wider,
744 // then it's is set to std mark's width
745 int imgWidth = wxMax(GetMarginWidth(), data->CheckSize.cx)
746 + data->CheckMargin.left + data->CheckMargin.right;
747
748 *width += imgWidth + data->CheckBgMargin.left + data->CheckBgMargin.right;
749 }
750
751 if ( m_bmpChecked.IsOk() || m_bmpChecked.IsOk() )
752 {
753 // get size of bitmap always return valid value (0 for invalid bitmap),
754 // so we don't needed check if bitmap is valid ;)
755 size_t heightBmp = wxMax(m_bmpChecked.GetHeight(), m_bmpUnchecked.GetHeight());
756 size_t widthtBmp = wxMax(m_bmpChecked.GetWidth(), m_bmpUnchecked.GetWidth());
757
758 if ( IsOwnerDrawn() )
759 {
760 heightBmp += data->CheckMargin.top + data->CheckMargin.bottom;
761 }
762 else
763 {
764 // we must allocate enough space for the bitmap
765 *width += widthtBmp;
766 }
767
768 // Is BMP height larger than text height?
769 if ( *height < heightBmp )
770 *height = heightBmp;
771 }
772
773 // make sure that this item is at least as tall as the system menu height
774 const size_t menuHeight = data->CheckMargin.top + data->CheckMargin.bottom
775 + data->CheckSize.cy;
776 if (*height < menuHeight)
777 *height = menuHeight;
778
779 return true;
780 }
781
782 bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc,
783 wxODAction WXUNUSED(act), wxODStatus stat)
784 {
785 const MenuDrawData* data = MenuDrawData::Get();
786
787 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
788 HDC hdc = GetHdcOf(*impl);
789
790 RECT rect;
791 wxCopyRectToRECT(rc, rect);
792
793 int imgWidth = wxMax(GetMarginWidth(), data->CheckSize.cx)
794 + data->CheckMargin.left + data->CheckMargin.right;
795
796 if ( IsOwnerDrawn() )
797 {
798 // font and colors to use
799 wxFont font;
800 GetFontToUse(font);
801
802 wxColour colText1, colBack1;
803 GetColourToUse(stat, colText1, colBack1);
804
805 DWORD colText = wxColourToPalRGB(colText1);
806 DWORD colBack = wxColourToPalRGB(colBack1);
807
808 // calculate metrics of item parts
809 RECT rcSelection;
810 RECT rcSeparator;
811 RECT rcGutter;
812 RECT rcText;
813
814 SetRect(&rcSelection,
815 rect.left + data->ItemMargin.left,
816 rect.top + data->ItemMargin.top,
817 rect.right - data->ItemMargin.right,
818 rect.bottom - data->ItemMargin.bottom);
819
820 SetRect(&rcSeparator,
821 rcSelection.left + data->SeparatorMargin.left,
822 rcSelection.top + data->SeparatorMargin.top,
823 rcSelection.right - data->SeparatorMargin.right,
824 rcSelection.bottom - data->SeparatorMargin.bottom);
825
826 CopyRect(&rcGutter, &rcSelection);
827 rcGutter.right = data->ItemMargin.left
828 + data->CheckBgMargin.left
829 + imgWidth
830 + data->CheckBgMargin.right;
831
832 CopyRect(&rcText, &rcSelection);
833 rcText.left = rcGutter.right + data->TextBorder;
834
835 #if wxUSE_UXTHEME
836 wxUxThemeEngine* theme = MenuDrawData::GetUxThemeEngine();
837 if ( theme )
838 {
839 POPUPITEMSTATES state;
840 if ( stat & wxODDisabled )
841 {
842 state = (stat & wxODSelected) ? MPI_DISABLEDHOT
843 : MPI_DISABLED;
844 }
845 else if ( stat & wxODSelected )
846 {
847 state = MPI_HOT;
848 }
849 else
850 {
851 state = MPI_NORMAL;
852 }
853
854 wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU");
855
856 if ( theme->IsThemeBackgroundPartiallyTransparent(hTheme,
857 MENU_POPUPITEM, state) )
858 {
859 theme->DrawThemeBackground(hTheme, hdc,
860 MENU_POPUPBACKGROUND,
861 0, &rect, NULL);
862 }
863
864 theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPGUTTER,
865 0, &rcGutter, NULL);
866
867 if ( IsSeparator() )
868 {
869 rcSeparator.left = rcGutter.right;
870 theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPSEPARATOR,
871 0, &rcSeparator, NULL);
872 return true;
873 }
874
875 theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPITEM,
876 state, &rcSelection, NULL);
877
878 }
879 else
880 #endif // wxUSE_UXTHEME
881 {
882 if ( IsSeparator() )
883 {
884 DrawEdge(hdc, &rcSeparator, EDGE_ETCHED, BF_TOP);
885 return true;
886 }
887
888 AutoHBRUSH hbr(colBack);
889 SelectInHDC selBrush(hdc, hbr);
890 ::FillRect(hdc, &rcSelection, hbr);
891 }
892
893
894 // draw text label
895 // using native API because it recognizes '&'
896
897 COLORREF colOldText = ::SetTextColor(hdc, colText);
898 COLORREF colOldBack = ::SetBkColor(hdc, colBack);
899
900 int prevMode = SetBkMode(hdc, TRANSPARENT);
901
902 SelectInHDC selFont(hdc, GetHfontOf(font));
903
904
905 // item text name without menemonic for calculating size
906 wxString text = GetName();
907
908 SIZE textSize;
909 ::GetTextExtentPoint32(hdc, text.c_str(), text.length(), &textSize);
910
911 // item text name with menemonic
912 text = GetItemLabel().BeforeFirst('\t');
913
914 int flags = DST_PREFIXTEXT;
915 // themes menu is using specified color for disabled labels
916 if ( data->MenuLayout() == MenuDrawData::Classic &&
917 (stat & wxODDisabled) && !(stat & wxODSelected) )
918 flags |= DSS_DISABLED;
919
920 if ( (stat & wxODHidePrefix) && !data->AlwaysShowCues )
921 flags |= DSS_HIDEPREFIX;
922
923 int x = rcText.left;
924 int y = rcText.top + (rcText.bottom - rcText.top - textSize.cy) / 2;
925
926 ::DrawState(hdc, NULL, NULL, (LPARAM)text.wx_str(),
927 text.length(), x, y, 0, 0, flags);
928
929 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
930 // as the last parameter in DrawState() (at least with Windows98). So we have
931 // to take care of right alignment ourselves.
932 wxString accel = GetItemLabel().AfterFirst(wxT('\t'));
933 if ( !accel.empty() )
934 {
935 SIZE accelSize;
936 ::GetTextExtentPoint32(hdc, accel.c_str(), accel.length(), &accelSize);
937
938 int flags = DST_TEXT;
939 // themes menu is using specified color for disabled labels
940 if ( data->MenuLayout() == MenuDrawData::Classic &&
941 (stat & wxODDisabled) && !(stat & wxODSelected) )
942 flags |= DSS_DISABLED;
943
944 // right align accel string with right edge of menu
945 // (offset by the margin width)
946
947 int x = rcText.right - 16 - accelSize.cx;
948 int y = rcText.top + (rcText.bottom - rcText.top - accelSize.cy) / 2;
949
950 ::DrawState(hdc, NULL, NULL, (LPARAM)accel.wx_str(),
951 accel.length(), x, y, 0, 0, flags);
952 }
953
954 ::SetBkMode(hdc, prevMode);
955 ::SetBkColor(hdc, colOldBack);
956 ::SetTextColor(hdc, colOldText);
957 }
958
959
960 // draw the bitmap
961
962 RECT rcImg;
963 SetRect(&rcImg,
964 rect.left + data->ItemMargin.left + data->CheckBgMargin.left,
965 rect.top + data->ItemMargin.top + data->CheckBgMargin.top,
966 rect.left + data->ItemMargin.left + data->CheckBgMargin.left
967 + imgWidth,
968 rect.bottom - data->ItemMargin.bottom - data->CheckBgMargin.bottom);
969
970 if ( IsCheckable() && !m_bmpChecked.Ok() )
971 {
972 if ( stat & wxODChecked )
973 {
974 #if wxUSE_UXTHEME
975 wxUxThemeEngine* theme = MenuDrawData::GetUxThemeEngine();
976 if ( theme )
977 {
978 wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU");
979
980 POPUPCHECKBACKGROUNDSTATES stateCheckBg = (stat & wxODDisabled)
981 ? MCB_DISABLED
982 : MCB_NORMAL;
983
984 theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPCHECKBACKGROUND,
985 stateCheckBg, &rcImg, NULL);
986
987 // check mark will be drawn centered on the background
988
989 POPUPCHECKSTATES stateCheck = (stat & wxODDisabled)
990 ? MC_CHECKMARKDISABLED
991 : MC_CHECKMARKNORMAL;
992
993 theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPCHECK,
994 stateCheck, &rcImg, NULL);
995 }
996 else
997 #endif // wxUSE_UXTHEME
998 {
999 int cx = imgWidth;
1000 int cy = rcImg.bottom - rcImg.top;
1001
1002 // what goes on: DrawFrameControl creates a b/w mask,
1003 // then we copy it to screen to have right colors
1004
1005 // first create a monochrome bitmap in a memory DC
1006 HDC hdcMem = ::CreateCompatibleDC(hdc);
1007 HBITMAP hbmpCheck = ::CreateBitmap(cx, cy, 1, 1, 0);
1008 ::SelectObject(hdcMem, hbmpCheck);
1009
1010 // then draw a check mark into it
1011 RECT rect = { 0, 0, cx, cy };
1012 if ( rc.GetHeight() > 0 )
1013 {
1014 ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
1015 }
1016
1017 // finally copy it to screen DC and clean up
1018 ::BitBlt(hdc, rcImg.left, rcImg.top, cx, cy, hdcMem, 0, 0, SRCCOPY);
1019
1020 ::DeleteDC(hdcMem);
1021 }
1022 }
1023 }
1024 else
1025 {
1026 wxBitmap bmp;
1027
1028 if ( stat & wxODDisabled )
1029 {
1030 bmp = GetDisabledBitmap();
1031 }
1032
1033 if ( !bmp.Ok() )
1034 {
1035 // for not checkable bitmaps we should always use unchecked one
1036 // because their checked bitmap is not set
1037 bmp = GetBitmap(!IsCheckable() || (stat & wxODChecked));
1038
1039 #if wxUSE_IMAGE
1040 if ( bmp.Ok() && stat & wxODDisabled )
1041 {
1042 // we need to grey out the bitmap as we don't have any specific
1043 // disabled bitmap
1044 wxImage imgGrey = bmp.ConvertToImage().ConvertToGreyscale();
1045 if ( imgGrey.Ok() )
1046 bmp = wxBitmap(imgGrey);
1047 }
1048 #endif // wxUSE_IMAGE
1049 }
1050
1051 if ( bmp.Ok() )
1052 {
1053 wxMemoryDC dcMem(&dc);
1054 dcMem.SelectObjectAsSource(bmp);
1055
1056 // center bitmap
1057 int nBmpWidth = bmp.GetWidth(),
1058 nBmpHeight = bmp.GetHeight();
1059
1060 // there should be enough space!
1061 wxASSERT( nBmpWidth <= imgWidth && nBmpHeight <= (rcImg.bottom - rcImg.top) );
1062
1063 int x = rcImg.left + (imgWidth - nBmpWidth) / 2;
1064 int y = rcImg.top + (rcImg.bottom - rcImg.top - nBmpHeight) / 2;
1065 dc.Blit(x, y, nBmpWidth, nBmpHeight, &dcMem, 0, 0, wxCOPY, true);
1066 }
1067 }
1068
1069 return true;
1070
1071 }
1072
1073 void wxMenuItem::GetFontToUse(wxFont& font) const
1074 {
1075 font = GetFont();
1076 if ( !font.IsOk() )
1077 font = MenuDrawData::Get()->Font;
1078 }
1079
1080 void wxMenuItem::GetColourToUse(wxODStatus stat, wxColour& colText, wxColour& colBack) const
1081 {
1082 #if wxUSE_UXTHEME
1083 wxUxThemeEngine* theme = MenuDrawData::GetUxThemeEngine();
1084 if ( theme )
1085 {
1086 wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU");
1087
1088 if ( stat & wxODDisabled)
1089 {
1090 wxRGBToColour(colText, theme->GetThemeSysColor(hTheme, COLOR_GRAYTEXT));
1091 }
1092 else
1093 {
1094 colText = GetTextColour();
1095 if ( !colText.IsOk() )
1096 wxRGBToColour(colText, theme->GetThemeSysColor(hTheme, COLOR_MENUTEXT));
1097 }
1098
1099 if ( stat & wxODSelected )
1100 {
1101 wxRGBToColour(colBack, theme->GetThemeSysColor(hTheme, COLOR_HIGHLIGHT));
1102 }
1103 else
1104 {
1105 colBack = GetBackgroundColour();
1106 if ( !colBack.IsOk() )
1107 wxRGBToColour(colBack, theme->GetThemeSysColor(hTheme, COLOR_MENU));
1108 }
1109 }
1110 else
1111 #endif // wxUSE_UXTHEME
1112 {
1113 wxOwnerDrawn::GetColourToUse(stat, colText, colBack);
1114 }
1115 }
1116 #endif // wxUSE_OWNER_DRAWN
1117
1118 // ----------------------------------------------------------------------------
1119 // wxMenuItemBase
1120 // ----------------------------------------------------------------------------
1121
1122 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
1123 int id,
1124 const wxString& name,
1125 const wxString& help,
1126 wxItemKind kind,
1127 wxMenu *subMenu)
1128 {
1129 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
1130 }
1131
1132 #endif // wxUSE_MENUS