1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/menuitem_osx.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/menuitem.h"
17 #include "wx/stockitem.h"
24 #include "wx/osx/private.h"
26 IMPLEMENT_ABSTRACT_CLASS( wxMenuItemImpl
, wxObject
)
28 wxMenuItemImpl::~wxMenuItemImpl()
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 (pParentMenu
!= NULL
&& !pParentMenu
->GetNoEventsMode())
45 if ( wxStripMenuCodes(m_text
).Upper() == wxT("EXIT") )
46 m_text
= wxT("Quit\tCtrl+Q") ;
48 m_radioGroup
.start
= -1;
49 m_isRadioGroupStart
= false;
51 wxString text
= wxStripMenuCodes(m_text
, (pParentMenu
!= NULL
&& pParentMenu
->GetNoEventsMode()) ? wxStrip_Accel
: wxStrip_All
);
52 if (text
.IsEmpty() && !IsSeparator())
54 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
55 text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|wxSTOCK_WITH_MNEMONIC
);
58 wxAcceleratorEntry
*entry
= wxAcceleratorEntry::Create( m_text
) ;
59 // use accessors for ID and Kind because they might have been changed in the base constructor
60 m_peer
= wxMenuItemImpl::Create( this, pParentMenu
, GetId(), text
, entry
, strHelp
, GetKind(), pSubMenu
);
64 wxMenuItem::~wxMenuItem()
72 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)
78 void wxMenuItem::Enable(bool bDoEnable
)
80 if (( m_isEnabled
!= bDoEnable
81 // avoid changing menuitem state when menu is disabled
82 // eg. BeginAppModalStateForWindow() will disable menus and ignore this change
83 // which in turn causes m_isEnabled to become out of sync with real menuitem state
85 && !(m_parentMenu
&& !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu
->GetHMenu()), 0))
88 // always update builtin menuitems
89 || ( GetId() == wxApp::s_macPreferencesMenuItemId
90 || GetId() == wxApp::s_macExitMenuItemId
91 || GetId() == wxApp::s_macAboutMenuItemId
94 wxMenuItemBase::Enable( bDoEnable
) ;
99 void wxMenuItem::UncheckRadio()
103 wxMenuItemBase::Check( false ) ;
108 void wxMenuItem::Check(bool bDoCheck
)
110 wxCHECK_RET( IsCheckable() && !IsSeparator(), wxT("only checkable items may be checked") );
112 if ( m_isChecked
!= bDoCheck
)
114 if ( GetKind() == wxITEM_RADIO
)
118 wxMenuItemBase::Check( bDoCheck
) ;
121 // get the index of this item in the menu
122 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
123 int pos
= items
.IndexOf(this);
124 wxCHECK_RET( pos
!= wxNOT_FOUND
,
125 wxT("menuitem not found in the menu items list?") );
127 // get the radio group range
130 if ( m_isRadioGroupStart
)
132 // we already have all information we need
134 end
= m_radioGroup
.end
;
136 else // next radio group item
138 // get the radio group end from the start item
139 start
= m_radioGroup
.start
;
140 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
143 // also uncheck all the other items in this radio group
144 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
145 for ( int n
= start
; n
<= end
&& node
; n
++ )
148 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
150 node
= node
->GetNext();
156 wxMenuItemBase::Check( bDoCheck
) ;
162 void wxMenuItem::SetItemLabel(const wxString
& text
)
164 // don't do anything if label didn't change
165 if ( m_text
== text
)
168 wxMenuItemBase::SetItemLabel(text
);
174 void wxMenuItem::UpdateItemBitmap()
181 GetPeer()->SetBitmap( m_bitmap
);
185 void wxMenuItem::UpdateItemStatus()
193 if ( IsCheckable() && IsChecked() )
194 GetPeer()->Check( true );
196 GetPeer()->Check( false );
198 GetPeer()->Enable( IsEnabled() );
201 void wxMenuItem::UpdateItemText()
206 wxString text
= wxStripMenuCodes(m_text
, m_parentMenu
!= NULL
&& m_parentMenu
->GetNoEventsMode() ? wxStrip_Accel
: wxStrip_All
);
207 if (text
.IsEmpty() && !IsSeparator())
209 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
210 text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|wxSTOCK_WITH_MNEMONIC
);
213 wxAcceleratorEntry
*entry
= wxAcceleratorEntry::Create( m_text
) ;
214 GetPeer()->SetLabel( text
, entry
);
221 void wxMenuItem::SetAsRadioGroupStart()
223 m_isRadioGroupStart
= true;
226 void wxMenuItem::SetRadioGroupStart(int start
)
228 wxASSERT_MSG( !m_isRadioGroupStart
,
229 wxT("should only be called for the next radio items") );
231 m_radioGroup
.start
= start
;
234 void wxMenuItem::SetRadioGroupEnd(int end
)
236 wxASSERT_MSG( m_isRadioGroupStart
,
237 wxT("should only be called for the first radio item") );
239 m_radioGroup
.end
= end
;
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
248 const wxString
& name
,
249 const wxString
& help
,
253 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);