]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/menuitem.cpp
Removed all duplicated and complicated #ifdefs for enabling Skip and Abort in progres...
[wxWidgets.git] / src / mac / carbon / menuitem.cpp
CommitLineData
e9576ca5
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: menuitem.cpp
3// Purpose: wxMenuItem implementation
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers & declarations
14// ============================================================================
15
03e11df5 16#include "wx/app.h"
e9576ca5
SC
17#include "wx/menu.h"
18#include "wx/menuitem.h"
19
d497dca4 20#include "wx/mac/uma.h"
e9576ca5
SC
21// ============================================================================
22// implementation
23// ============================================================================
24
25// ----------------------------------------------------------------------------
26// dynamic classes implementation
27// ----------------------------------------------------------------------------
28
2f1ae414 29#if !USE_SHARED_LIBRARY
e9576ca5 30 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
2f1ae414 31#endif //USE_SHARED_LIBRARY
37e2cb08 32
e9576ca5
SC
33// ----------------------------------------------------------------------------
34// wxMenuItem
35// ----------------------------------------------------------------------------
36
2f1ae414 37//
e9576ca5
SC
38// ctor & dtor
39// -----------
40
d65c269b
VZ
41wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
42 int id,
43 const wxString& text,
44 const wxString& strHelp,
45 wxItemKind kind,
e7549107 46 wxMenu *pSubMenu)
d65c269b 47 : wxMenuItemBase(pParentMenu, id, text, strHelp, kind, pSubMenu)
e9576ca5 48{
408aef5e 49 wxASSERT_MSG( id != 0 || pSubMenu != NULL , wxT("A MenuItem ID of Zero does not work under Mac") ) ;
69991fff 50
bf918b97
SC
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
427ff662 53 if ( wxStripMenuCodes(m_text).Upper() == wxT("EXIT") )
e7549107 54 {
427ff662 55 m_text =wxT("Quit\tCtrl+Q") ;
e7549107 56 }
bf918b97
SC
57
58 m_radioGroup.start = -1;
59 m_isRadioGroupStart = FALSE;
e9576ca5
SC
60}
61
62wxMenuItem::~wxMenuItem()
63{
64}
65
bf918b97
SC
66// change item state
67// -----------------
51abe921 68
bf918b97
SC
69void wxMenuItem::SetBitmap(const wxBitmap& bitmap)
70{
e40298d5
JS
71 m_bitmap = bitmap;
72 UpdateItemBitmap() ;
51abe921
SC
73}
74
bf918b97 75void wxMenuItem::UpdateItemBitmap()
51abe921 76{
e40298d5
JS
77 if ( !m_parentMenu )
78 return ;
79
80 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
81 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
82 if( mhandle == NULL || index == 0)
83 return ;
84
85 if ( m_bitmap.Ok() )
86 {
87 ControlButtonContentInfo info ;
88 wxMacCreateBitmapButton( &info , m_bitmap , kControlContentCIconHandle ) ;
89 if ( info.contentType != kControlNoContent )
90 {
91 if ( info.contentType == kControlContentCIconHandle )
92 SetMenuItemIconHandle( mhandle , index ,
93 kMenuColorIconType , (Handle) info.u.cIconHandle ) ;
94 }
95
96 }
51abe921
SC
97}
98
bf918b97 99void wxMenuItem::UpdateItemStatus()
e9576ca5 100{
e40298d5
JS
101 if ( !m_parentMenu )
102 return ;
103
756c2704
SC
104#if TARGET_CARBON
105 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId)
106 {
107 if ( !IsEnabled() )
108 DisableMenuCommand( NULL , kHICommandPreferences ) ;
109 else
110 EnableMenuCommand( NULL , kHICommandPreferences ) ;
111 }
112 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId)
113 {
114 if ( !IsEnabled() )
115 DisableMenuCommand( NULL , kHICommandQuit ) ;
116 else
117 EnableMenuCommand( NULL , kHICommandQuit ) ;
118 }
119#endif
120 {
121 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
122 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
123 if( mhandle == NULL || index == 0)
124 return ;
125
126 UMAEnableMenuItem( mhandle , index , m_isEnabled ) ;
127 if ( IsCheckable() && IsChecked() )
128 ::SetItemMark( mhandle , index , 0x12 ) ; // checkmark
129 else
130 ::SetItemMark( mhandle , index , 0 ) ; // no mark
131
a9412f8f 132 UMASetMenuItemText( mhandle , index , m_text , wxFont::GetDefaultEncoding() ) ;
756c2704
SC
133 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
134 UMASetMenuItemShortcut( mhandle , index , entry ) ;
135 delete entry ;
136 }
e9576ca5
SC
137}
138
bf918b97
SC
139void wxMenuItem::UpdateItemText()
140{
e40298d5
JS
141 if ( !m_parentMenu )
142 return ;
143
144 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
145 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
146 if( mhandle == NULL || index == 0)
147 return ;
148
a9412f8f 149 UMASetMenuItemText( mhandle , index , m_text , wxFont::GetDefaultEncoding() ) ;
e40298d5
JS
150 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
151 UMASetMenuItemShortcut( mhandle , index , entry ) ;
152 delete entry ;
bf918b97 153}
e7549107 154
e9576ca5
SC
155
156void wxMenuItem::Enable(bool bDoEnable)
157{
bdacb147
JS
158 if ( m_isEnabled != bDoEnable
159#if TARGET_CARBON
160 || GetId() == wxApp::s_macPreferencesMenuItemId
161 || GetId() == wxApp::s_macExitMenuItemId
162 || GetId() == wxApp::s_macAboutMenuItemId
163#endif
164 )
e40298d5
JS
165 {
166 wxMenuItemBase::Enable( bDoEnable ) ;
167 UpdateItemStatus() ;
168 }
bf918b97
SC
169}
170void wxMenuItem::UncheckRadio()
171{
e40298d5
JS
172 if ( m_isChecked )
173 {
174 wxMenuItemBase::Check( false ) ;
175 UpdateItemStatus() ;
176 }
e9576ca5
SC
177}
178
179void wxMenuItem::Check(bool bDoCheck)
180{
427ff662 181 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
e40298d5
JS
182
183 if ( m_isChecked != bDoCheck )
184 {
185 if ( GetKind() == wxITEM_RADIO )
186 {
187 if ( bDoCheck )
188 {
189 wxMenuItemBase::Check( bDoCheck ) ;
190 UpdateItemStatus() ;
191
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?") );
197
198 // get the radio group range
199 int start,
200 end;
201
202 if ( m_isRadioGroupStart )
203 {
204 // we already have all information we need
205 start = pos;
206 end = m_radioGroup.end;
207 }
208 else // next radio group item
209 {
210 // get the radio group end from the start item
211 start = m_radioGroup.start;
212 end = items.Item(start)->GetData()->m_radioGroup.end;
213 }
214
215 // also uncheck all the other items in this radio group
affd2611 216 wxMenuItemList::compatibility_iterator node = items.Item(start);
e40298d5
JS
217 for ( int n = start; n <= end && node; n++ )
218 {
219 if ( n != pos )
220 {
221 ((wxMenuItem*)node->GetData())->UncheckRadio();
222 }
223 node = node->GetNext();
224 }
225 }
226 }
227 else
228 {
229 wxMenuItemBase::Check( bDoCheck ) ;
230 UpdateItemStatus() ;
231 }
232 }
51abe921
SC
233}
234
235void wxMenuItem::SetText(const wxString& text)
236{
237 // don't do anything if label didn't change
238 if ( m_text == text )
239 return;
240
241 wxMenuItemBase::SetText(text);
bf918b97
SC
242
243 UpdateItemText() ;
244}
51abe921 245
bf918b97
SC
246// radio group stuff
247// -----------------
51abe921 248
bf918b97
SC
249void wxMenuItem::SetAsRadioGroupStart()
250{
251 m_isRadioGroupStart = TRUE;
51abe921 252}
bf918b97
SC
253
254void wxMenuItem::SetRadioGroupStart(int start)
51abe921 255{
bf918b97
SC
256 wxASSERT_MSG( !m_isRadioGroupStart,
257 _T("should only be called for the next radio items") );
258
259 m_radioGroup.start = start;
260}
261
262void wxMenuItem::SetRadioGroupEnd(int end)
263{
264 wxASSERT_MSG( m_isRadioGroupStart,
265 _T("should only be called for the first radio item") );
266
267 m_radioGroup.end = end;
51abe921
SC
268}
269
270// ----------------------------------------------------------------------------
271// wxMenuItemBase
272// ----------------------------------------------------------------------------
273
2f1ae414
SC
274/* static */
275wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
276{
277 return wxStripMenuCodes(text);
278}
279
51abe921
SC
280wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
281 int id,
282 const wxString& name,
283 const wxString& help,
d65c269b 284 wxItemKind kind,
51abe921
SC
285 wxMenu *subMenu)
286{
d65c269b 287 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
51abe921 288}