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