1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem implementation
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/menuitem.h"
18 #include "wx/mac/uma.h"
20 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
23 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
26 const wxString
& strHelp
,
29 :wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
31 wxASSERT_MSG( id
!= 0 || pSubMenu
!= NULL
, wxT("A MenuItem ID of Zero does not work under Mac") ) ;
33 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
34 // therefore these item must not be translated
35 if ( wxStripMenuCodes(m_text
).Upper() == wxT("EXIT") )
36 m_text
= wxT("Quit\tCtrl+Q") ;
38 m_radioGroup
.start
= -1;
39 m_isRadioGroupStart
= false;
42 wxMenuItem::~wxMenuItem()
49 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)
55 void wxMenuItem::UpdateItemBitmap()
60 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
61 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
62 if ( mhandle
== NULL
|| index
== 0)
68 ControlButtonContentInfo info
;
69 wxMacCreateBitmapButton( &info
, m_bitmap
) ;
70 if ( info
.contentType
!= kControlNoContent
)
72 if ( info
.contentType
== kControlContentIconRef
)
73 SetMenuItemIconHandle( mhandle
, index
,
74 kMenuIconRefType
, (Handle
) info
.u
.iconRef
) ;
76 wxMacReleaseBitmapButton( &info
) ;
81 void wxMenuItem::UpdateItemStatus()
90 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
)
93 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
95 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
98 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
)
101 DisableMenuCommand( NULL
, kHICommandQuit
) ;
103 EnableMenuCommand( NULL
, kHICommandQuit
) ;
108 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
109 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
110 if ( mhandle
== NULL
|| index
== 0)
113 UMAEnableMenuItem( mhandle
, index
, m_isEnabled
) ;
114 if ( IsCheckable() && IsChecked() )
115 ::SetItemMark( mhandle
, index
, 0x12 ) ; // checkmark
117 ::SetItemMark( mhandle
, index
, 0 ) ; // no mark
119 UMASetMenuItemText( mhandle
, index
, wxStripMenuCodes(m_text
) , wxFont::GetDefaultEncoding() ) ;
120 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
121 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
126 void wxMenuItem::UpdateItemText()
131 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
132 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
133 if (mhandle
== NULL
|| index
== 0)
136 UMASetMenuItemText( mhandle
, index
, wxStripMenuCodes(m_text
) , wxFont::GetDefaultEncoding() ) ;
137 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
138 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
142 void wxMenuItem::Enable(bool bDoEnable
)
144 if (( m_isEnabled
!= bDoEnable
146 // avoid changing menuitem state when menu is disabled
147 // eg. BeginAppModalStateForWindow() will disable menus and ignore this change
148 // which in turn causes m_isEnabled to become out of sync with real menuitem state
149 && !(m_parentMenu
&& !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu
->GetHMenu()), 0)) )
150 // always update builtin menuitems
151 || ( GetId() == wxApp::s_macPreferencesMenuItemId
152 || GetId() == wxApp::s_macExitMenuItemId
153 || GetId() == wxApp::s_macAboutMenuItemId
157 wxMenuItemBase::Enable( bDoEnable
) ;
162 void wxMenuItem::UncheckRadio()
166 wxMenuItemBase::Check( false ) ;
171 void wxMenuItem::Check(bool bDoCheck
)
173 wxCHECK_RET( IsCheckable() && !IsSeparator(), wxT("only checkable items may be checked") );
175 if ( m_isChecked
!= bDoCheck
)
177 if ( GetKind() == wxITEM_RADIO
)
181 wxMenuItemBase::Check( bDoCheck
) ;
184 // get the index of this item in the menu
185 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
186 int pos
= items
.IndexOf(this);
187 wxCHECK_RET( pos
!= wxNOT_FOUND
,
188 _T("menuitem not found in the menu items list?") );
190 // get the radio group range
193 if ( m_isRadioGroupStart
)
195 // we already have all information we need
197 end
= m_radioGroup
.end
;
199 else // next radio group item
201 // get the radio group end from the start item
202 start
= m_radioGroup
.start
;
203 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
206 // also uncheck all the other items in this radio group
207 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
208 for ( int n
= start
; n
<= end
&& node
; n
++ )
211 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
213 node
= node
->GetNext();
219 wxMenuItemBase::Check( bDoCheck
) ;
225 void wxMenuItem::SetText(const wxString
& text
)
227 // don't do anything if label didn't change
228 if ( m_text
== text
)
231 wxMenuItemBase::SetText(text
);
239 void wxMenuItem::SetAsRadioGroupStart()
241 m_isRadioGroupStart
= true;
244 void wxMenuItem::SetRadioGroupStart(int start
)
246 wxASSERT_MSG( !m_isRadioGroupStart
,
247 wxT("should only be called for the next radio items") );
249 m_radioGroup
.start
= start
;
252 void wxMenuItem::SetRadioGroupEnd(int end
)
254 wxASSERT_MSG( m_isRadioGroupStart
,
255 wxT("should only be called for the first radio item") );
257 m_radioGroup
.end
= end
;
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
265 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
267 return wxStripMenuCodes(text
);
270 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
272 const wxString
& name
,
273 const wxString
& help
,
277 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);