1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/menuitem.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/menuitem.h"
22 #include "wx/mac/uma.h"
24 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
27 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
30 const wxString
& strHelp
,
33 :wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
35 wxASSERT_MSG( id
!= 0 || pSubMenu
!= NULL
, wxT("A MenuItem ID of Zero does not work under Mac") ) ;
37 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
38 // therefore these item must not be translated
39 if ( wxStripMenuCodes(m_text
).Upper() == wxT("EXIT") )
40 m_text
= wxT("Quit\tCtrl+Q") ;
42 m_radioGroup
.start
= -1;
43 m_isRadioGroupStart
= false;
46 wxMenuItem::~wxMenuItem()
53 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)
59 void wxMenuItem::UpdateItemBitmap()
64 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
65 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
66 if ( mhandle
== NULL
|| index
== 0)
72 ControlButtonContentInfo info
;
73 wxMacCreateBitmapButton( &info
, m_bitmap
) ;
74 if ( info
.contentType
!= kControlNoContent
)
76 if ( info
.contentType
== kControlContentIconRef
)
77 SetMenuItemIconHandle( mhandle
, index
,
78 kMenuIconRefType
, (Handle
) info
.u
.iconRef
) ;
80 wxMacReleaseBitmapButton( &info
) ;
85 void wxMenuItem::UpdateItemStatus()
94 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
)
97 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
99 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
102 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
)
105 DisableMenuCommand( NULL
, kHICommandQuit
) ;
107 EnableMenuCommand( NULL
, kHICommandQuit
) ;
112 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
113 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
114 if ( mhandle
== NULL
|| index
== 0)
117 UMAEnableMenuItem( mhandle
, index
, m_isEnabled
) ;
118 if ( IsCheckable() && IsChecked() )
119 ::SetItemMark( mhandle
, index
, 0x12 ) ; // checkmark
121 ::SetItemMark( mhandle
, index
, 0 ) ; // no mark
123 UMASetMenuItemText( mhandle
, index
, wxStripMenuCodes(m_text
) , wxFont::GetDefaultEncoding() ) ;
124 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
125 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
130 void wxMenuItem::UpdateItemText()
135 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
136 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
137 if (mhandle
== NULL
|| index
== 0)
140 UMASetMenuItemText( mhandle
, index
, wxStripMenuCodes(m_text
) , wxFont::GetDefaultEncoding() ) ;
141 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
142 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
146 void wxMenuItem::Enable(bool bDoEnable
)
148 if (( m_isEnabled
!= bDoEnable
150 // avoid changing menuitem state when menu is disabled
151 // eg. BeginAppModalStateForWindow() will disable menus and ignore this change
152 // which in turn causes m_isEnabled to become out of sync with real menuitem state
153 && !(m_parentMenu
&& !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu
->GetHMenu()), 0)) )
154 // always update builtin menuitems
155 || ( GetId() == wxApp::s_macPreferencesMenuItemId
156 || GetId() == wxApp::s_macExitMenuItemId
157 || GetId() == wxApp::s_macAboutMenuItemId
161 wxMenuItemBase::Enable( bDoEnable
) ;
166 void wxMenuItem::UncheckRadio()
170 wxMenuItemBase::Check( false ) ;
175 void wxMenuItem::Check(bool bDoCheck
)
177 wxCHECK_RET( IsCheckable() && !IsSeparator(), wxT("only checkable items may be checked") );
179 if ( m_isChecked
!= bDoCheck
)
181 if ( GetKind() == wxITEM_RADIO
)
185 wxMenuItemBase::Check( bDoCheck
) ;
188 // get the index of this item in the menu
189 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
190 int pos
= items
.IndexOf(this);
191 wxCHECK_RET( pos
!= wxNOT_FOUND
,
192 _T("menuitem not found in the menu items list?") );
194 // get the radio group range
197 if ( m_isRadioGroupStart
)
199 // we already have all information we need
201 end
= m_radioGroup
.end
;
203 else // next radio group item
205 // get the radio group end from the start item
206 start
= m_radioGroup
.start
;
207 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
210 // also uncheck all the other items in this radio group
211 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
212 for ( int n
= start
; n
<= end
&& node
; n
++ )
215 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
217 node
= node
->GetNext();
223 wxMenuItemBase::Check( bDoCheck
) ;
229 void wxMenuItem::SetText(const wxString
& text
)
231 // don't do anything if label didn't change
232 if ( m_text
== text
)
235 wxMenuItemBase::SetText(text
);
243 void wxMenuItem::SetAsRadioGroupStart()
245 m_isRadioGroupStart
= true;
248 void wxMenuItem::SetRadioGroupStart(int start
)
250 wxASSERT_MSG( !m_isRadioGroupStart
,
251 wxT("should only be called for the next radio items") );
253 m_radioGroup
.start
= start
;
256 void wxMenuItem::SetRadioGroupEnd(int end
)
258 wxASSERT_MSG( m_isRadioGroupStart
,
259 wxT("should only be called for the first radio item") );
261 m_radioGroup
.end
= end
;
264 // ----------------------------------------------------------------------------
266 // ----------------------------------------------------------------------------
269 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
271 return wxStripMenuCodes(text
);
274 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
276 const wxString
& name
,
277 const wxString
& help
,
281 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);