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 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)
87 ControlButtonContentInfo info
;
88 wxMacCreateBitmapButton( &info
, m_bitmap
, kControlContentCIconHandle
) ;
89 if ( info
.contentType
!= kControlNoContent
)
91 if ( info
.contentType
== kControlContentCIconHandle
)
92 SetMenuItemIconHandle( mhandle
, index
,
93 kMenuColorIconType
, (Handle
) info
.u
.cIconHandle
) ;
99 void wxMenuItem::UpdateItemStatus()
105 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
)
108 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
110 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
112 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
)
115 DisableMenuCommand( NULL
, kHICommandQuit
) ;
117 EnableMenuCommand( NULL
, kHICommandQuit
) ;
121 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
122 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
123 if( mhandle
== NULL
|| index
== 0)
126 UMAEnableMenuItem( mhandle
, index
, m_isEnabled
) ;
127 if ( IsCheckable() && IsChecked() )
128 ::SetItemMark( mhandle
, index
, 0x12 ) ; // checkmark
130 ::SetItemMark( mhandle
, index
, 0 ) ; // no mark
132 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
133 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
134 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
139 void wxMenuItem::UpdateItemText()
144 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
145 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
146 if( mhandle
== NULL
|| index
== 0)
149 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
150 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
151 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
156 void wxMenuItem::Enable(bool bDoEnable
)
158 if ( m_isEnabled
!= bDoEnable
160 || GetId() == wxApp::s_macPreferencesMenuItemId
161 || GetId() == wxApp::s_macExitMenuItemId
162 || GetId() == wxApp::s_macAboutMenuItemId
166 wxMenuItemBase::Enable( bDoEnable
) ;
170 void wxMenuItem::UncheckRadio()
174 wxMenuItemBase::Check( false ) ;
179 void wxMenuItem::Check(bool bDoCheck
)
181 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
183 if ( m_isChecked
!= bDoCheck
)
185 if ( GetKind() == wxITEM_RADIO
)
189 wxMenuItemBase::Check( bDoCheck
) ;
192 // get the index of this item in the menu
193 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
194 int pos
= items
.IndexOf(this);
195 wxCHECK_RET( pos
!= wxNOT_FOUND
,
196 _T("menuitem not found in the menu items list?") );
198 // get the radio group range
202 if ( m_isRadioGroupStart
)
204 // we already have all information we need
206 end
= m_radioGroup
.end
;
208 else // next radio group item
210 // get the radio group end from the start item
211 start
= m_radioGroup
.start
;
212 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
215 // also uncheck all the other items in this radio group
216 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
217 for ( int n
= start
; n
<= end
&& node
; n
++ )
221 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
223 node
= node
->GetNext();
229 wxMenuItemBase::Check( bDoCheck
) ;
235 void wxMenuItem::SetText(const wxString
& text
)
237 // don't do anything if label didn't change
238 if ( m_text
== text
)
241 wxMenuItemBase::SetText(text
);
249 void wxMenuItem::SetAsRadioGroupStart()
251 m_isRadioGroupStart
= TRUE
;
254 void wxMenuItem::SetRadioGroupStart(int start
)
256 wxASSERT_MSG( !m_isRadioGroupStart
,
257 _T("should only be called for the next radio items") );
259 m_radioGroup
.start
= start
;
262 void wxMenuItem::SetRadioGroupEnd(int end
)
264 wxASSERT_MSG( m_isRadioGroupStart
,
265 _T("should only be called for the first radio item") );
267 m_radioGroup
.end
= end
;
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
275 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
277 return wxStripMenuCodes(text
);
280 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
282 const wxString
& name
,
283 const wxString
& help
,
287 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);