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"
15 #include "wx/stockitem.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 DoUpdateItemBitmap( mhandle
, index
);
69 void wxMenuItem::DoUpdateItemBitmap( WXHMENU menu
, wxUint16 index
)
71 MenuHandle mhandle
= (MenuHandle
) menu
;
73 if ( mhandle
== NULL
|| index
== 0)
79 ControlButtonContentInfo info
;
80 wxMacCreateBitmapButton( &info
, m_bitmap
) ;
81 if ( info
.contentType
!= kControlNoContent
)
83 if ( info
.contentType
== kControlContentIconRef
)
84 SetMenuItemIconHandle( mhandle
, index
,
85 kMenuIconRefType
, (Handle
) info
.u
.iconRef
) ;
86 else if ( info
.contentType
== kControlContentCGImageRef
)
87 SetMenuItemIconHandle( mhandle
, index
,
88 kMenuCGImageRefType
, (Handle
) info
.u
.imageRef
) ;
90 wxMacReleaseBitmapButton( &info
) ;
95 void wxMenuItem::UpdateItemStatus()
104 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
)
107 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
109 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
112 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
)
115 DisableMenuCommand( NULL
, kHICommandQuit
) ;
117 EnableMenuCommand( NULL
, kHICommandQuit
) ;
122 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
123 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
124 if ( mhandle
== NULL
|| index
== 0)
127 UMAEnableMenuItem( mhandle
, index
, m_isEnabled
) ;
128 if ( IsCheckable() && IsChecked() )
129 ::SetItemMark( mhandle
, index
, 0x12 ) ; // checkmark
131 ::SetItemMark( mhandle
, index
, 0 ) ; // no mark
133 UMASetMenuItemText( mhandle
, index
, wxStripMenuCodes(m_text
) , wxFont::GetDefaultEncoding() ) ;
134 wxAcceleratorEntry
*entry
= wxAcceleratorEntry::Create( m_text
) ;
135 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
140 void wxMenuItem::UpdateItemText()
145 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
146 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
147 if (mhandle
== NULL
|| index
== 0)
150 wxString text
= m_text
;
151 if (text
.IsEmpty() && !IsSeparator())
153 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
154 text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|wxSTOCK_WITH_MNEMONIC
);
157 UMASetMenuItemText( mhandle
, index
, wxStripMenuCodes(text
) , wxFont::GetDefaultEncoding() ) ;
158 wxAcceleratorEntry
*entry
= wxAcceleratorEntry::Create( text
) ;
159 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
163 void wxMenuItem::Enable(bool bDoEnable
)
165 if (( m_isEnabled
!= bDoEnable
167 // avoid changing menuitem state when menu is disabled
168 // eg. BeginAppModalStateForWindow() will disable menus and ignore this change
169 // which in turn causes m_isEnabled to become out of sync with real menuitem state
170 && !(m_parentMenu
&& !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu
->GetHMenu()), 0)) )
171 // always update builtin menuitems
172 || ( GetId() == wxApp::s_macPreferencesMenuItemId
173 || GetId() == wxApp::s_macExitMenuItemId
174 || GetId() == wxApp::s_macAboutMenuItemId
178 wxMenuItemBase::Enable( bDoEnable
) ;
183 void wxMenuItem::UncheckRadio()
187 wxMenuItemBase::Check( false ) ;
192 void wxMenuItem::Check(bool bDoCheck
)
194 wxCHECK_RET( IsCheckable() && !IsSeparator(), wxT("only checkable items may be checked") );
196 if ( m_isChecked
!= bDoCheck
)
198 if ( GetKind() == wxITEM_RADIO
)
202 wxMenuItemBase::Check( bDoCheck
) ;
205 // get the index of this item in the menu
206 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
207 int pos
= items
.IndexOf(this);
208 wxCHECK_RET( pos
!= wxNOT_FOUND
,
209 _T("menuitem not found in the menu items list?") );
211 // get the radio group range
214 if ( m_isRadioGroupStart
)
216 // we already have all information we need
218 end
= m_radioGroup
.end
;
220 else // next radio group item
222 // get the radio group end from the start item
223 start
= m_radioGroup
.start
;
224 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
227 // also uncheck all the other items in this radio group
228 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
229 for ( int n
= start
; n
<= end
&& node
; n
++ )
232 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
234 node
= node
->GetNext();
240 wxMenuItemBase::Check( bDoCheck
) ;
246 void wxMenuItem::SetItemLabel(const wxString
& text
)
248 // don't do anything if label didn't change
249 if ( m_text
== text
)
252 wxMenuItemBase::SetItemLabel(text
);
260 void wxMenuItem::SetAsRadioGroupStart()
262 m_isRadioGroupStart
= true;
265 void wxMenuItem::SetRadioGroupStart(int start
)
267 wxASSERT_MSG( !m_isRadioGroupStart
,
268 wxT("should only be called for the next radio items") );
270 m_radioGroup
.start
= start
;
273 void wxMenuItem::SetRadioGroupEnd(int end
)
275 wxASSERT_MSG( m_isRadioGroupStart
,
276 wxT("should only be called for the first radio item") );
278 m_radioGroup
.end
= end
;
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
286 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
288 return wxStripMenuCodes(text
);
291 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
293 const wxString
& name
,
294 const wxString
& help
,
298 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);