1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/menuitem_osx.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: menuitem.cpp 54129 2008-06-11 19:30:52Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/menuitem.h"
15 #include "wx/stockitem.h"
22 #include "wx/osx/private.h"
24 IMPLEMENT_ABSTRACT_CLASS( wxMenuItemImpl
, wxObject
)
26 wxMenuItemImpl::~wxMenuItemImpl()
30 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
32 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
35 const wxString
& strHelp
,
38 :wxMenuItemBase(pParentMenu
, id
, t
, strHelp
, kind
, pSubMenu
)
40 wxASSERT_MSG( id
!= 0 || pSubMenu
!= NULL
, wxT("A MenuItem ID of Zero does not work under Mac") ) ;
42 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
43 // therefore these item must not be translated
44 if ( wxStripMenuCodes(m_text
).Upper() == wxT("EXIT") )
45 m_text
= wxT("Quit\tCtrl+Q") ;
47 m_radioGroup
.start
= -1;
48 m_isRadioGroupStart
= false;
50 wxString text
= wxStripMenuCodes(m_text
);
51 if (text
.IsEmpty() && !IsSeparator())
53 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
54 text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|wxSTOCK_WITH_MNEMONIC
);
57 wxAcceleratorEntry
*entry
= wxAcceleratorEntry::Create( m_text
) ;
58 m_peer
= wxMenuItemImpl::Create( this, pParentMenu
, id
, text
, entry
, strHelp
, kind
, pSubMenu
);
62 wxMenuItem::~wxMenuItem()
70 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)
76 void wxMenuItem::Enable(bool bDoEnable
)
78 if (( m_isEnabled
!= bDoEnable
79 // avoid changing menuitem state when menu is disabled
80 // eg. BeginAppModalStateForWindow() will disable menus and ignore this change
81 // which in turn causes m_isEnabled to become out of sync with real menuitem state
83 && !(m_parentMenu
&& !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu
->GetHMenu()), 0))
86 // always update builtin menuitems
87 || ( GetId() == wxApp::s_macPreferencesMenuItemId
88 || GetId() == wxApp::s_macExitMenuItemId
89 || GetId() == wxApp::s_macAboutMenuItemId
92 wxMenuItemBase::Enable( bDoEnable
) ;
97 void wxMenuItem::UncheckRadio()
101 wxMenuItemBase::Check( false ) ;
106 void wxMenuItem::Check(bool bDoCheck
)
108 wxCHECK_RET( IsCheckable() && !IsSeparator(), wxT("only checkable items may be checked") );
110 if ( m_isChecked
!= bDoCheck
)
112 if ( GetKind() == wxITEM_RADIO
)
116 wxMenuItemBase::Check( bDoCheck
) ;
119 // get the index of this item in the menu
120 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
121 int pos
= items
.IndexOf(this);
122 wxCHECK_RET( pos
!= wxNOT_FOUND
,
123 _T("menuitem not found in the menu items list?") );
125 // get the radio group range
128 if ( m_isRadioGroupStart
)
130 // we already have all information we need
132 end
= m_radioGroup
.end
;
134 else // next radio group item
136 // get the radio group end from the start item
137 start
= m_radioGroup
.start
;
138 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
141 // also uncheck all the other items in this radio group
142 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
143 for ( int n
= start
; n
<= end
&& node
; n
++ )
146 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
148 node
= node
->GetNext();
154 wxMenuItemBase::Check( bDoCheck
) ;
160 void wxMenuItem::SetItemLabel(const wxString
& text
)
162 // don't do anything if label didn't change
163 if ( m_text
== text
)
166 wxMenuItemBase::SetItemLabel(text
);
172 void wxMenuItem::UpdateItemBitmap()
179 m_peer
->SetBitmap( m_bitmap
);
183 void wxMenuItem::UpdateItemStatus()
191 if ( IsCheckable() && IsChecked() )
192 m_peer
->Check( true );
194 m_peer
->Check( false );
196 m_peer
->Enable( IsEnabled() );
199 void wxMenuItem::UpdateItemText()
204 wxString text
= wxStripMenuCodes(m_text
);
205 if (text
.IsEmpty() && !IsSeparator())
207 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
208 text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|wxSTOCK_WITH_MNEMONIC
);
211 wxAcceleratorEntry
*entry
= wxAcceleratorEntry::Create( m_text
) ;
212 m_peer
->SetLabel( text
, entry
);
219 void wxMenuItem::SetAsRadioGroupStart()
221 m_isRadioGroupStart
= true;
224 void wxMenuItem::SetRadioGroupStart(int start
)
226 wxASSERT_MSG( !m_isRadioGroupStart
,
227 wxT("should only be called for the next radio items") );
229 m_radioGroup
.start
= start
;
232 void wxMenuItem::SetRadioGroupEnd(int end
)
234 wxASSERT_MSG( m_isRadioGroupStart
,
235 wxT("should only be called for the first radio item") );
237 m_radioGroup
.end
= end
;
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
246 const wxString
& name
,
247 const wxString
& help
,
251 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);