1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/menuitem.cpp
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"
18 #include "wx/menuitem.h"
25 #include "wx/mac/uma.h"
26 // ============================================================================
28 // ============================================================================
30 // ----------------------------------------------------------------------------
31 // dynamic classes implementation
32 // ----------------------------------------------------------------------------
34 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
44 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
47 const wxString
& strHelp
,
50 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
52 // TO DISCUSS on dev : whether we can veto id 0
53 // wxASSERT_MSG( id != 0 || pSubMenu != NULL , wxT("A MenuItem ID of Zero does not work under Mac") ) ;
55 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
56 // therefore these item must not be translated
57 if ( wxStripMenuCodes(m_text
).Upper() == wxT("EXIT") )
59 m_text
=wxT("Quit\tCtrl+Q") ;
62 m_radioGroup
.start
= -1;
63 m_isRadioGroupStart
= false;
66 wxMenuItem::~wxMenuItem()
73 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)
79 void wxMenuItem::UpdateItemBitmap()
84 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
85 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
86 if( mhandle
== NULL
|| index
== 0)
91 ControlButtonContentInfo info
;
92 wxMacCreateBitmapButton( &info
, m_bitmap
, kControlContentCIconHandle
) ;
93 if ( info
.contentType
!= kControlNoContent
)
95 if ( info
.contentType
== kControlContentCIconHandle
)
96 SetMenuItemIconHandle( mhandle
, index
,
97 kMenuColorIconType
, (Handle
) info
.u
.cIconHandle
) ;
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 wxMenuItemBase::Enable( bDoEnable
) ;
168 void wxMenuItem::UncheckRadio()
172 wxMenuItemBase::Check( false ) ;
177 void wxMenuItem::Check(bool bDoCheck
)
179 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
181 if ( m_isChecked
!= bDoCheck
)
183 if ( GetKind() == wxITEM_RADIO
)
187 wxMenuItemBase::Check( bDoCheck
) ;
190 // get the index of this item in the menu
191 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
192 int pos
= items
.IndexOf(this);
193 wxCHECK_RET( pos
!= wxNOT_FOUND
,
194 _T("menuitem not found in the menu items list?") );
196 // get the radio group range
200 if ( m_isRadioGroupStart
)
202 // we already have all information we need
204 end
= m_radioGroup
.end
;
206 else // next radio group item
208 // get the radio group end from the start item
209 start
= m_radioGroup
.start
;
210 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
213 // also uncheck all the other items in this radio group
214 wxMenuItemList::Node
*node
= items
.Item(start
);
215 for ( int n
= start
; n
<= end
&& node
; n
++ )
219 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
221 node
= node
->GetNext();
227 wxMenuItemBase::Check( bDoCheck
) ;
233 void wxMenuItem::SetText(const wxString
& text
)
235 // don't do anything if label didn't change
236 if ( m_text
== text
)
239 wxMenuItemBase::SetText(text
);
247 void wxMenuItem::SetAsRadioGroupStart()
249 m_isRadioGroupStart
= true;
252 void wxMenuItem::SetRadioGroupStart(int start
)
254 wxASSERT_MSG( !m_isRadioGroupStart
,
255 _T("should only be called for the next radio items") );
257 m_radioGroup
.start
= start
;
260 void wxMenuItem::SetRadioGroupEnd(int end
)
262 wxASSERT_MSG( m_isRadioGroupStart
,
263 _T("should only be called for the first radio item") );
265 m_radioGroup
.end
= end
;
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
273 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
275 return wxStripMenuCodes(text
);
278 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
280 const wxString
& name
,
281 const wxString
& help
,
285 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);