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()
103 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
)
106 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
108 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
111 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
)
114 DisableMenuCommand( NULL
, kHICommandQuit
) ;
116 EnableMenuCommand( NULL
, kHICommandQuit
) ;
120 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
121 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
122 if ( mhandle
== NULL
|| index
== 0)
125 UMAEnableMenuItem( mhandle
, index
, m_isEnabled
) ;
126 if ( IsCheckable() && IsChecked() )
127 ::SetItemMark( mhandle
, index
, 0x12 ) ; // checkmark
129 ::SetItemMark( mhandle
, index
, 0 ) ; // no mark
131 UMASetMenuItemText( mhandle
, index
, wxStripMenuCodes(m_text
) , wxFont::GetDefaultEncoding() ) ;
132 wxAcceleratorEntry
*entry
= wxAcceleratorEntry::Create( m_text
) ;
133 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
138 void wxMenuItem::UpdateItemText()
143 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
144 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
145 if (mhandle
== NULL
|| index
== 0)
148 wxString text
= m_text
;
149 if (text
.IsEmpty() && !IsSeparator())
151 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
152 text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|wxSTOCK_WITH_MNEMONIC
);
155 UMASetMenuItemText( mhandle
, index
, wxStripMenuCodes(text
) , wxFont::GetDefaultEncoding() ) ;
156 wxAcceleratorEntry
*entry
= wxAcceleratorEntry::Create( text
) ;
157 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
161 void wxMenuItem::Enable(bool bDoEnable
)
163 if (( m_isEnabled
!= bDoEnable
164 // avoid changing menuitem state when menu is disabled
165 // eg. BeginAppModalStateForWindow() will disable menus and ignore this change
166 // which in turn causes m_isEnabled to become out of sync with real menuitem state
167 && !(m_parentMenu
&& !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu
->GetHMenu()), 0)) )
168 // always update builtin menuitems
169 || ( GetId() == wxApp::s_macPreferencesMenuItemId
170 || GetId() == wxApp::s_macExitMenuItemId
171 || GetId() == wxApp::s_macAboutMenuItemId
174 wxMenuItemBase::Enable( bDoEnable
) ;
179 void wxMenuItem::UncheckRadio()
183 wxMenuItemBase::Check( false ) ;
188 void wxMenuItem::Check(bool bDoCheck
)
190 wxCHECK_RET( IsCheckable() && !IsSeparator(), wxT("only checkable items may be checked") );
192 if ( m_isChecked
!= bDoCheck
)
194 if ( GetKind() == wxITEM_RADIO
)
198 wxMenuItemBase::Check( bDoCheck
) ;
201 // get the index of this item in the menu
202 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
203 int pos
= items
.IndexOf(this);
204 wxCHECK_RET( pos
!= wxNOT_FOUND
,
205 _T("menuitem not found in the menu items list?") );
207 // get the radio group range
210 if ( m_isRadioGroupStart
)
212 // we already have all information we need
214 end
= m_radioGroup
.end
;
216 else // next radio group item
218 // get the radio group end from the start item
219 start
= m_radioGroup
.start
;
220 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
223 // also uncheck all the other items in this radio group
224 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
225 for ( int n
= start
; n
<= end
&& node
; n
++ )
228 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
230 node
= node
->GetNext();
236 wxMenuItemBase::Check( bDoCheck
) ;
242 void wxMenuItem::SetItemLabel(const wxString
& text
)
244 // don't do anything if label didn't change
245 if ( m_text
== text
)
248 wxMenuItemBase::SetItemLabel(text
);
256 void wxMenuItem::SetAsRadioGroupStart()
258 m_isRadioGroupStart
= true;
261 void wxMenuItem::SetRadioGroupStart(int start
)
263 wxASSERT_MSG( !m_isRadioGroupStart
,
264 wxT("should only be called for the next radio items") );
266 m_radioGroup
.start
= start
;
269 void wxMenuItem::SetRadioGroupEnd(int end
)
271 wxASSERT_MSG( m_isRadioGroupStart
,
272 wxT("should only be called for the first radio item") );
274 m_radioGroup
.end
= end
;
277 // ----------------------------------------------------------------------------
279 // ----------------------------------------------------------------------------
282 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
284 return wxStripMenuCodes(text
);
287 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
289 const wxString
& name
,
290 const wxString
& help
,
294 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);