1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem implementation
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
16 #include "wx/wxprec.h"
20 #include "wx/menuitem.h"
22 #include "wx/mac/uma.h"
23 // ============================================================================
25 // ============================================================================
27 // ----------------------------------------------------------------------------
28 // dynamic classes implementation
29 // ----------------------------------------------------------------------------
31 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
41 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
44 const wxString
& strHelp
,
47 :wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
49 wxASSERT_MSG( id
!= 0 || pSubMenu
!= NULL
, wxT("A MenuItem ID of Zero does not work under Mac") ) ;
51 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
52 // therefore these item must not be translated
53 if ( wxStripMenuCodes(m_text
).Upper() == wxT("EXIT") )
55 m_text
=wxT("Quit\tCtrl+Q") ;
58 m_radioGroup
.start
= -1;
59 m_isRadioGroupStart
= false;
62 wxMenuItem::~wxMenuItem()
69 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)
75 void wxMenuItem::UpdateItemBitmap()
80 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
81 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
82 if( mhandle
== NULL
|| index
== 0)
88 ControlButtonContentInfo info
;
89 wxMacCreateBitmapButton( &info
, m_bitmap
) ;
90 if ( info
.contentType
!= kControlNoContent
)
92 if ( info
.contentType
== kControlContentIconRef
)
93 SetMenuItemIconHandle( mhandle
, index
,
94 kMenuIconRefType
, (Handle
) info
.u
.iconRef
) ;
96 wxMacReleaseBitmapButton( &info
) ;
101 void wxMenuItem::UpdateItemStatus()
110 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
)
113 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
115 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
118 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
)
121 DisableMenuCommand( NULL
, kHICommandQuit
) ;
123 EnableMenuCommand( NULL
, kHICommandQuit
) ;
128 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
129 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
130 if ( mhandle
== NULL
|| index
== 0)
133 UMAEnableMenuItem( mhandle
, index
, m_isEnabled
) ;
134 if ( IsCheckable() && IsChecked() )
135 ::SetItemMark( mhandle
, index
, 0x12 ) ; // checkmark
137 ::SetItemMark( mhandle
, index
, 0 ) ; // no mark
139 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
140 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
141 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
146 void wxMenuItem::UpdateItemText()
151 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
152 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
153 if( mhandle
== NULL
|| index
== 0)
156 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
157 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
158 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
162 void wxMenuItem::Enable(bool bDoEnable
)
164 if (( m_isEnabled
!= bDoEnable
166 // avoid changing menuitem state when menu is disabled
167 // eg. BeginAppModalStateForWindow() will disable menus and ignore this change
168 // which in turn causes m_isEnabled to become out of sync with real menuitem state
169 && !(m_parentMenu
&& !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu
->GetHMenu()), 0)) )
170 // always update builtin menuitems
171 || ( GetId() == wxApp::s_macPreferencesMenuItemId
172 || GetId() == wxApp::s_macExitMenuItemId
173 || GetId() == wxApp::s_macAboutMenuItemId
177 wxMenuItemBase::Enable( bDoEnable
) ;
182 void wxMenuItem::UncheckRadio()
186 wxMenuItemBase::Check( false ) ;
191 void wxMenuItem::Check(bool bDoCheck
)
193 wxCHECK_RET( IsCheckable() && !IsSeparator(), wxT("only checkable items may be checked") );
195 if ( m_isChecked
!= bDoCheck
)
197 if ( GetKind() == wxITEM_RADIO
)
201 wxMenuItemBase::Check( bDoCheck
) ;
204 // get the index of this item in the menu
205 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
206 int pos
= items
.IndexOf(this);
207 wxCHECK_RET( pos
!= wxNOT_FOUND
,
208 _T("menuitem not found in the menu items list?") );
210 // get the radio group range
213 if ( m_isRadioGroupStart
)
215 // we already have all information we need
217 end
= m_radioGroup
.end
;
219 else // next radio group item
221 // get the radio group end from the start item
222 start
= m_radioGroup
.start
;
223 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
226 // also uncheck all the other items in this radio group
227 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
228 for ( int n
= start
; n
<= end
&& node
; n
++ )
231 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
233 node
= node
->GetNext();
239 wxMenuItemBase::Check( bDoCheck
) ;
245 void wxMenuItem::SetText(const wxString
& text
)
247 // don't do anything if label didn't change
248 if ( m_text
== text
)
251 wxMenuItemBase::SetText(text
);
259 void wxMenuItem::SetAsRadioGroupStart()
261 m_isRadioGroupStart
= true;
264 void wxMenuItem::SetRadioGroupStart(int start
)
266 wxASSERT_MSG( !m_isRadioGroupStart
,
267 _T("should only be called for the next radio items") );
269 m_radioGroup
.start
= start
;
272 void wxMenuItem::SetRadioGroupEnd(int end
)
274 wxASSERT_MSG( m_isRadioGroupStart
,
275 _T("should only be called for the first radio item") );
277 m_radioGroup
.end
= end
;
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
285 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
287 return wxStripMenuCodes(text
);
290 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
292 const wxString
& name
,
293 const wxString
& help
,
297 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);