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"
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 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
34 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
37 const wxString
& strHelp
,
40 :wxMenuItemBase(pParentMenu
, id
, t
, strHelp
, kind
, pSubMenu
)
42 wxASSERT_MSG( id
!= 0 || pSubMenu
!= NULL
, wxT("A MenuItem ID of Zero does not work under Mac") ) ;
44 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
45 // therefore these item must not be translated
46 if ( wxStripMenuCodes(m_text
).Upper() == wxT("EXIT") )
47 m_text
= wxT("Quit\tCtrl+Q") ;
49 m_radioGroup
.start
= -1;
50 m_isRadioGroupStart
= false;
52 wxString text
= wxStripMenuCodes(m_text
);
53 if (text
.IsEmpty() && !IsSeparator())
55 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
56 text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|wxSTOCK_WITH_MNEMONIC
);
59 wxAcceleratorEntry
*entry
= wxAcceleratorEntry::Create( m_text
) ;
60 // use accessors for ID and Kind because they might have been changed in the base constructor
61 m_peer
= wxMenuItemImpl::Create( this, pParentMenu
, GetId(), text
, entry
, strHelp
, GetKind(), pSubMenu
);
65 wxMenuItem::~wxMenuItem()
73 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)
79 void wxMenuItem::Enable(bool bDoEnable
)
81 if (( m_isEnabled
!= bDoEnable
82 // avoid changing menuitem state when menu is disabled
83 // eg. BeginAppModalStateForWindow() will disable menus and ignore this change
84 // which in turn causes m_isEnabled to become out of sync with real menuitem state
86 && !(m_parentMenu
&& !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu
->GetHMenu()), 0))
89 // always update builtin menuitems
90 || ( GetId() == wxApp::s_macPreferencesMenuItemId
91 || GetId() == wxApp::s_macExitMenuItemId
92 || GetId() == wxApp::s_macAboutMenuItemId
95 wxMenuItemBase::Enable( bDoEnable
) ;
100 void wxMenuItem::UncheckRadio()
104 wxMenuItemBase::Check( false ) ;
109 void wxMenuItem::Check(bool bDoCheck
)
111 wxCHECK_RET( IsCheckable() && !IsSeparator(), wxT("only checkable items may be checked") );
113 if ( m_isChecked
!= bDoCheck
)
115 if ( GetKind() == wxITEM_RADIO
)
119 wxMenuItemBase::Check( bDoCheck
) ;
122 // get the index of this item in the menu
123 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
124 int pos
= items
.IndexOf(this);
125 wxCHECK_RET( pos
!= wxNOT_FOUND
,
126 wxT("menuitem not found in the menu items list?") );
128 // get the radio group range
131 if ( m_isRadioGroupStart
)
133 // we already have all information we need
135 end
= m_radioGroup
.end
;
137 else // next radio group item
139 // get the radio group end from the start item
140 start
= m_radioGroup
.start
;
141 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
144 // also uncheck all the other items in this radio group
145 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
146 for ( int n
= start
; n
<= end
&& node
; n
++ )
149 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
151 node
= node
->GetNext();
157 wxMenuItemBase::Check( bDoCheck
) ;
163 void wxMenuItem::SetItemLabel(const wxString
& text
)
165 // don't do anything if label didn't change
166 if ( m_text
== text
)
169 wxMenuItemBase::SetItemLabel(text
);
175 void wxMenuItem::UpdateItemBitmap()
182 m_peer
->SetBitmap( m_bitmap
);
186 void wxMenuItem::UpdateItemStatus()
194 if ( IsCheckable() && IsChecked() )
195 m_peer
->Check( true );
197 m_peer
->Check( false );
199 m_peer
->Enable( IsEnabled() );
202 void wxMenuItem::UpdateItemText()
207 wxString text
= wxStripMenuCodes(m_text
);
208 if (text
.IsEmpty() && !IsSeparator())
210 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
211 text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|wxSTOCK_WITH_MNEMONIC
);
214 wxAcceleratorEntry
*entry
= wxAcceleratorEntry::Create( m_text
) ;
215 m_peer
->SetLabel( text
, entry
);
222 void wxMenuItem::SetAsRadioGroupStart()
224 m_isRadioGroupStart
= true;
227 void wxMenuItem::SetRadioGroupStart(int start
)
229 wxASSERT_MSG( !m_isRadioGroupStart
,
230 wxT("should only be called for the next radio items") );
232 m_radioGroup
.start
= start
;
235 void wxMenuItem::SetRadioGroupEnd(int end
)
237 wxASSERT_MSG( m_isRadioGroupStart
,
238 wxT("should only be called for the first radio item") );
240 m_radioGroup
.end
= end
;
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
247 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
249 const wxString
& name
,
250 const wxString
& help
,
254 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);