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"
21 #include "wx/mac/uma.h"
23 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
26 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
29 const wxString
& strHelp
,
32 :wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
34 wxASSERT_MSG( id
!= 0 || pSubMenu
!= NULL
, wxT("A MenuItem ID of Zero does not work under Mac") ) ;
36 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
37 // therefore these item must not be translated
38 if ( wxStripMenuCodes(m_text
).Upper() == wxT("EXIT") )
39 m_text
= wxT("Quit\tCtrl+Q") ;
41 m_radioGroup
.start
= -1;
42 m_isRadioGroupStart
= false;
45 wxMenuItem::~wxMenuItem()
52 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)
58 void wxMenuItem::UpdateItemBitmap()
63 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
64 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
65 if ( mhandle
== NULL
|| index
== 0)
71 ControlButtonContentInfo info
;
72 wxMacCreateBitmapButton( &info
, m_bitmap
) ;
73 if ( info
.contentType
!= kControlNoContent
)
75 if ( info
.contentType
== kControlContentIconRef
)
76 SetMenuItemIconHandle( mhandle
, index
,
77 kMenuIconRefType
, (Handle
) info
.u
.iconRef
) ;
79 wxMacReleaseBitmapButton( &info
) ;
84 void wxMenuItem::UpdateItemStatus()
93 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
)
96 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
98 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
101 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
)
104 DisableMenuCommand( NULL
, kHICommandQuit
) ;
106 EnableMenuCommand( NULL
, kHICommandQuit
) ;
111 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
112 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
113 if ( mhandle
== NULL
|| index
== 0)
116 UMAEnableMenuItem( mhandle
, index
, m_isEnabled
) ;
117 if ( IsCheckable() && IsChecked() )
118 ::SetItemMark( mhandle
, index
, 0x12 ) ; // checkmark
120 ::SetItemMark( mhandle
, index
, 0 ) ; // no mark
122 UMASetMenuItemText( mhandle
, index
, wxStripMenuCodes(m_text
) , wxFont::GetDefaultEncoding() ) ;
123 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
124 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
129 void wxMenuItem::UpdateItemText()
134 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
135 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
136 if (mhandle
== NULL
|| index
== 0)
139 UMASetMenuItemText( mhandle
, index
, wxStripMenuCodes(m_text
) , wxFont::GetDefaultEncoding() ) ;
140 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
141 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
145 void wxMenuItem::Enable(bool bDoEnable
)
147 if (( m_isEnabled
!= bDoEnable
149 // avoid changing menuitem state when menu is disabled
150 // eg. BeginAppModalStateForWindow() will disable menus and ignore this change
151 // which in turn causes m_isEnabled to become out of sync with real menuitem state
152 && !(m_parentMenu
&& !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu
->GetHMenu()), 0)) )
153 // always update builtin menuitems
154 || ( GetId() == wxApp::s_macPreferencesMenuItemId
155 || GetId() == wxApp::s_macExitMenuItemId
156 || GetId() == wxApp::s_macAboutMenuItemId
160 wxMenuItemBase::Enable( bDoEnable
) ;
165 void wxMenuItem::UncheckRadio()
169 wxMenuItemBase::Check( false ) ;
174 void wxMenuItem::Check(bool bDoCheck
)
176 wxCHECK_RET( IsCheckable() && !IsSeparator(), wxT("only checkable items may be checked") );
178 if ( m_isChecked
!= bDoCheck
)
180 if ( GetKind() == wxITEM_RADIO
)
184 wxMenuItemBase::Check( bDoCheck
) ;
187 // get the index of this item in the menu
188 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
189 int pos
= items
.IndexOf(this);
190 wxCHECK_RET( pos
!= wxNOT_FOUND
,
191 _T("menuitem not found in the menu items list?") );
193 // get the radio group range
196 if ( m_isRadioGroupStart
)
198 // we already have all information we need
200 end
= m_radioGroup
.end
;
202 else // next radio group item
204 // get the radio group end from the start item
205 start
= m_radioGroup
.start
;
206 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
209 // also uncheck all the other items in this radio group
210 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
211 for ( int n
= start
; n
<= end
&& node
; n
++ )
214 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
216 node
= node
->GetNext();
222 wxMenuItemBase::Check( bDoCheck
) ;
228 void wxMenuItem::SetText(const wxString
& text
)
230 // don't do anything if label didn't change
231 if ( m_text
== text
)
234 wxMenuItemBase::SetText(text
);
242 void wxMenuItem::SetAsRadioGroupStart()
244 m_isRadioGroupStart
= true;
247 void wxMenuItem::SetRadioGroupStart(int start
)
249 wxASSERT_MSG( !m_isRadioGroupStart
,
250 wxT("should only be called for the next radio items") );
252 m_radioGroup
.start
= start
;
255 void wxMenuItem::SetRadioGroupEnd(int end
)
257 wxASSERT_MSG( m_isRadioGroupStart
,
258 wxT("should only be called for the first radio item") );
260 m_radioGroup
.end
= end
;
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
268 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
270 return wxStripMenuCodes(text
);
273 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
275 const wxString
& name
,
276 const wxString
& help
,
280 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);