1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem implementation
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
18 #include "wx/menuitem.h"
20 #include "wx/mac/uma.h"
21 // ============================================================================
23 // ============================================================================
25 // ----------------------------------------------------------------------------
26 // dynamic classes implementation
27 // ----------------------------------------------------------------------------
29 #if !USE_SHARED_LIBRARY
30 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
31 #endif //USE_SHARED_LIBRARY
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
41 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
44 const wxString
& strHelp
,
47 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
49 // TO DISCUSS on dev : whether we can veto id 0
50 // wxASSERT_MSG( id != 0 || pSubMenu != NULL , wxT("A MenuItem ID of Zero does not work under Mac") ) ;
52 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
53 // therefore these item must not be translated
54 if ( wxStripMenuCodes(m_text
).Upper() == wxT("EXIT") )
56 m_text
=wxT("Quit\tCtrl+Q") ;
59 m_radioGroup
.start
= -1;
60 m_isRadioGroupStart
= FALSE
;
63 wxMenuItem::~wxMenuItem()
70 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)
76 void wxMenuItem::UpdateItemBitmap()
81 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
82 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
83 if( mhandle
== NULL
|| index
== 0)
88 ControlButtonContentInfo info
;
89 wxMacCreateBitmapButton( &info
, m_bitmap
, kControlContentCIconHandle
) ;
90 if ( info
.contentType
!= kControlNoContent
)
92 if ( info
.contentType
== kControlContentCIconHandle
)
93 SetMenuItemIconHandle( mhandle
, index
,
94 kMenuColorIconType
, (Handle
) info
.u
.cIconHandle
) ;
100 void wxMenuItem::UpdateItemStatus()
106 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
)
109 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
111 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
113 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
)
116 DisableMenuCommand( NULL
, kHICommandQuit
) ;
118 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
, m_text
, wxFont::GetDefaultEncoding() ) ;
134 wxAcceleratorEntry
*entry
= wxGetAccelFromString( 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 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
151 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
152 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
157 void wxMenuItem::Enable(bool bDoEnable
)
159 if ( m_isEnabled
!= bDoEnable
161 || GetId() == wxApp::s_macPreferencesMenuItemId
162 || GetId() == wxApp::s_macExitMenuItemId
163 || GetId() == wxApp::s_macAboutMenuItemId
167 wxMenuItemBase::Enable( bDoEnable
) ;
171 void wxMenuItem::UncheckRadio()
175 wxMenuItemBase::Check( false ) ;
180 void wxMenuItem::Check(bool bDoCheck
)
182 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
184 if ( m_isChecked
!= bDoCheck
)
186 if ( GetKind() == wxITEM_RADIO
)
190 wxMenuItemBase::Check( bDoCheck
) ;
193 // get the index of this item in the menu
194 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
195 int pos
= items
.IndexOf(this);
196 wxCHECK_RET( pos
!= wxNOT_FOUND
,
197 _T("menuitem not found in the menu items list?") );
199 // get the radio group range
203 if ( m_isRadioGroupStart
)
205 // we already have all information we need
207 end
= m_radioGroup
.end
;
209 else // next radio group item
211 // get the radio group end from the start item
212 start
= m_radioGroup
.start
;
213 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
216 // also uncheck all the other items in this radio group
217 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
218 for ( int n
= start
; n
<= end
&& node
; n
++ )
222 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
224 node
= node
->GetNext();
230 wxMenuItemBase::Check( bDoCheck
) ;
236 void wxMenuItem::SetText(const wxString
& text
)
238 // don't do anything if label didn't change
239 if ( m_text
== text
)
242 wxMenuItemBase::SetText(text
);
250 void wxMenuItem::SetAsRadioGroupStart()
252 m_isRadioGroupStart
= TRUE
;
255 void wxMenuItem::SetRadioGroupStart(int start
)
257 wxASSERT_MSG( !m_isRadioGroupStart
,
258 _T("should only be called for the next radio items") );
260 m_radioGroup
.start
= start
;
263 void wxMenuItem::SetRadioGroupEnd(int end
)
265 wxASSERT_MSG( m_isRadioGroupStart
,
266 _T("should only be called for the first radio item") );
268 m_radioGroup
.end
= end
;
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
276 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
278 return wxStripMenuCodes(text
);
281 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
283 const wxString
& name
,
284 const wxString
& help
,
288 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);