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)
89 ControlButtonContentInfo info
;
90 wxMacCreateBitmapButton( &info
, m_bitmap
, kControlContentCIconHandle
) ;
91 if ( info
.contentType
!= kControlNoContent
)
93 if ( info
.contentType
== kControlContentCIconHandle
)
94 SetMenuItemIconHandle( mhandle
, index
,
95 kMenuColorIconType
, (Handle
) info
.u
.cIconHandle
) ;
101 void wxMenuItem::UpdateItemStatus()
107 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
)
110 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
112 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
114 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
)
117 DisableMenuCommand( NULL
, kHICommandQuit
) ;
119 EnableMenuCommand( NULL
, kHICommandQuit
) ;
123 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
124 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
125 if( mhandle
== NULL
|| index
== 0)
128 UMAEnableMenuItem( mhandle
, index
, m_isEnabled
) ;
129 if ( IsCheckable() && IsChecked() )
130 ::SetItemMark( mhandle
, index
, 0x12 ) ; // checkmark
132 ::SetItemMark( mhandle
, index
, 0 ) ; // no mark
134 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
135 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
136 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
141 void wxMenuItem::UpdateItemText()
146 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
147 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
148 if( mhandle
== NULL
|| index
== 0)
151 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
152 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
153 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
158 void wxMenuItem::Enable(bool bDoEnable
)
160 if ( m_isEnabled
!= bDoEnable
162 || GetId() == wxApp::s_macPreferencesMenuItemId
163 || GetId() == wxApp::s_macExitMenuItemId
164 || GetId() == wxApp::s_macAboutMenuItemId
168 wxMenuItemBase::Enable( bDoEnable
) ;
172 void wxMenuItem::UncheckRadio()
176 wxMenuItemBase::Check( false ) ;
181 void wxMenuItem::Check(bool bDoCheck
)
183 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
185 if ( m_isChecked
!= bDoCheck
)
187 if ( GetKind() == wxITEM_RADIO
)
191 wxMenuItemBase::Check( bDoCheck
) ;
194 // get the index of this item in the menu
195 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
196 int pos
= items
.IndexOf(this);
197 wxCHECK_RET( pos
!= wxNOT_FOUND
,
198 _T("menuitem not found in the menu items list?") );
200 // get the radio group range
204 if ( m_isRadioGroupStart
)
206 // we already have all information we need
208 end
= m_radioGroup
.end
;
210 else // next radio group item
212 // get the radio group end from the start item
213 start
= m_radioGroup
.start
;
214 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
217 // also uncheck all the other items in this radio group
218 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
219 for ( int n
= start
; n
<= end
&& node
; n
++ )
223 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
225 node
= node
->GetNext();
231 wxMenuItemBase::Check( bDoCheck
) ;
237 void wxMenuItem::SetText(const wxString
& text
)
239 // don't do anything if label didn't change
240 if ( m_text
== text
)
243 wxMenuItemBase::SetText(text
);
251 void wxMenuItem::SetAsRadioGroupStart()
253 m_isRadioGroupStart
= TRUE
;
256 void wxMenuItem::SetRadioGroupStart(int start
)
258 wxASSERT_MSG( !m_isRadioGroupStart
,
259 _T("should only be called for the next radio items") );
261 m_radioGroup
.start
= start
;
264 void wxMenuItem::SetRadioGroupEnd(int end
)
266 wxASSERT_MSG( m_isRadioGroupStart
,
267 _T("should only be called for the first radio item") );
269 m_radioGroup
.end
= end
;
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
277 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
279 return wxStripMenuCodes(text
);
282 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
284 const wxString
& name
,
285 const wxString
& help
,
289 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);