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 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
33 #endif //USE_SHARED_LIBRARY
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
43 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
46 const wxString
& strHelp
,
49 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
51 wxASSERT_MSG( id
!= 0 || pSubMenu
!= NULL
, wxT("A MenuItem ID of Zero does not work under Mac") ) ;
53 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
54 // therefore these item must not be translated
55 if ( wxStripMenuCodes(m_text
).Upper() == wxT("EXIT") )
57 m_text
=wxT("Quit\tCtrl+Q") ;
60 m_radioGroup
.start
= -1;
61 m_isRadioGroupStart
= FALSE
;
64 wxMenuItem::~wxMenuItem()
71 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)
77 void wxMenuItem::UpdateItemBitmap()
82 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
83 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
84 if( mhandle
== NULL
|| index
== 0)
90 ControlButtonContentInfo info
;
91 wxMacCreateBitmapButton( &info
, m_bitmap
) ;
92 if ( info
.contentType
!= kControlNoContent
)
94 if ( info
.contentType
== kControlContentIconRef
)
95 SetMenuItemIconHandle( mhandle
, index
,
96 kMenuIconRefType
, (Handle
) info
.u
.iconRef
) ;
98 wxMacReleaseBitmapButton( &info
) ;
103 void wxMenuItem::UpdateItemStatus()
109 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
)
112 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
114 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
116 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
)
119 DisableMenuCommand( NULL
, kHICommandQuit
) ;
121 EnableMenuCommand( NULL
, kHICommandQuit
) ;
125 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
126 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
127 if( mhandle
== NULL
|| index
== 0)
130 UMAEnableMenuItem( mhandle
, index
, m_isEnabled
) ;
131 if ( IsCheckable() && IsChecked() )
132 ::SetItemMark( mhandle
, index
, 0x12 ) ; // checkmark
134 ::SetItemMark( mhandle
, index
, 0 ) ; // no mark
136 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
137 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
138 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
143 void wxMenuItem::UpdateItemText()
148 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
149 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
150 if( mhandle
== NULL
|| index
== 0)
153 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
154 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
155 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
160 void wxMenuItem::Enable(bool bDoEnable
)
162 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
175 wxMenuItemBase::Enable( bDoEnable
) ;
179 void wxMenuItem::UncheckRadio()
183 wxMenuItemBase::Check( false ) ;
188 void wxMenuItem::Check(bool bDoCheck
)
190 wxCHECK_RET( IsCheckable(), 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
211 if ( m_isRadioGroupStart
)
213 // we already have all information we need
215 end
= m_radioGroup
.end
;
217 else // next radio group item
219 // get the radio group end from the start item
220 start
= m_radioGroup
.start
;
221 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
224 // also uncheck all the other items in this radio group
225 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
226 for ( int n
= start
; n
<= end
&& node
; n
++ )
230 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
232 node
= node
->GetNext();
238 wxMenuItemBase::Check( bDoCheck
) ;
244 void wxMenuItem::SetText(const wxString
& text
)
246 // don't do anything if label didn't change
247 if ( m_text
== text
)
250 wxMenuItemBase::SetText(text
);
258 void wxMenuItem::SetAsRadioGroupStart()
260 m_isRadioGroupStart
= TRUE
;
263 void wxMenuItem::SetRadioGroupStart(int start
)
265 wxASSERT_MSG( !m_isRadioGroupStart
,
266 _T("should only be called for the next radio items") );
268 m_radioGroup
.start
= start
;
271 void wxMenuItem::SetRadioGroupEnd(int end
)
273 wxASSERT_MSG( m_isRadioGroupStart
,
274 _T("should only be called for the first radio item") );
276 m_radioGroup
.end
= end
;
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
284 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
286 return wxStripMenuCodes(text
);
289 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
291 const wxString
& name
,
292 const wxString
& help
,
296 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);