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"
26 #include "wx/mac/uma.h"
27 // ============================================================================
29 // ============================================================================
31 // ----------------------------------------------------------------------------
32 // dynamic classes implementation
33 // ----------------------------------------------------------------------------
35 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
45 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
48 const wxString
& strHelp
,
51 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
53 // TO DISCUSS on dev : whether we can veto id 0
54 // wxASSERT_MSG( id != 0 || pSubMenu != NULL , wxT("A MenuItem ID of Zero does not work under Mac") ) ;
56 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
57 // therefore these item must not be translated
58 if ( wxStripMenuCodes(m_text
).Upper() == wxT("EXIT") )
60 m_text
=wxT("Quit\tCtrl+Q") ;
63 m_radioGroup
.start
= -1;
64 m_isRadioGroupStart
= false;
67 wxMenuItem::~wxMenuItem()
74 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)
80 void wxMenuItem::UpdateItemBitmap()
85 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
86 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
87 if( mhandle
== NULL
|| index
== 0)
92 ControlButtonContentInfo info
;
93 wxMacCreateBitmapButton( &info
, m_bitmap
, kControlContentCIconHandle
) ;
94 if ( info
.contentType
!= kControlNoContent
)
96 if ( info
.contentType
== kControlContentCIconHandle
)
97 SetMenuItemIconHandle( mhandle
, index
,
98 kMenuColorIconType
, (Handle
) info
.u
.cIconHandle
) ;
104 void wxMenuItem::UpdateItemStatus()
110 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
)
113 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
115 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
117 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
)
120 DisableMenuCommand( NULL
, kHICommandQuit
) ;
122 EnableMenuCommand( NULL
, kHICommandQuit
) ;
126 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
127 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
128 if( mhandle
== NULL
|| index
== 0)
131 UMAEnableMenuItem( mhandle
, index
, m_isEnabled
) ;
132 if ( IsCheckable() && IsChecked() )
133 ::SetItemMark( mhandle
, index
, 0x12 ) ; // checkmark
135 ::SetItemMark( mhandle
, index
, 0 ) ; // no mark
137 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
138 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
139 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
144 void wxMenuItem::UpdateItemText()
149 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
150 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
151 if( mhandle
== NULL
|| index
== 0)
154 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
155 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
156 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
161 void wxMenuItem::Enable(bool bDoEnable
)
163 if ( m_isEnabled
!= bDoEnable
)
165 wxMenuItemBase::Enable( bDoEnable
) ;
169 void wxMenuItem::UncheckRadio()
173 wxMenuItemBase::Check( false ) ;
178 void wxMenuItem::Check(bool bDoCheck
)
180 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
182 if ( m_isChecked
!= bDoCheck
)
184 if ( GetKind() == wxITEM_RADIO
)
188 wxMenuItemBase::Check( bDoCheck
) ;
191 // get the index of this item in the menu
192 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
193 int pos
= items
.IndexOf(this);
194 wxCHECK_RET( pos
!= wxNOT_FOUND
,
195 _T("menuitem not found in the menu items list?") );
197 // get the radio group range
201 if ( m_isRadioGroupStart
)
203 // we already have all information we need
205 end
= m_radioGroup
.end
;
207 else // next radio group item
209 // get the radio group end from the start item
210 start
= m_radioGroup
.start
;
211 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
214 // also uncheck all the other items in this radio group
215 wxMenuItemList::Node
*node
= items
.Item(start
);
216 for ( int n
= start
; n
<= end
&& node
; n
++ )
220 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
222 node
= node
->GetNext();
228 wxMenuItemBase::Check( bDoCheck
) ;
234 void wxMenuItem::SetText(const wxString
& text
)
236 // don't do anything if label didn't change
237 if ( m_text
== text
)
240 wxMenuItemBase::SetText(text
);
248 void wxMenuItem::SetAsRadioGroupStart()
250 m_isRadioGroupStart
= true;
253 void wxMenuItem::SetRadioGroupStart(int start
)
255 wxASSERT_MSG( !m_isRadioGroupStart
,
256 _T("should only be called for the next radio items") );
258 m_radioGroup
.start
= start
;
261 void wxMenuItem::SetRadioGroupEnd(int end
)
263 wxASSERT_MSG( m_isRadioGroupStart
,
264 _T("should only be called for the first radio item") );
266 m_radioGroup
.end
= end
;
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
274 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
276 return wxStripMenuCodes(text
);
279 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
281 const wxString
& name
,
282 const wxString
& help
,
286 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);