]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/menuitem.cpp
commented section removed
[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
e9576ca5
SC
9// Licence: wxWindows licence
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{
bf918b97
SC
49 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
50 // therefore these item must not be translated
427ff662 51 if ( wxStripMenuCodes(m_text).Upper() == wxT("EXIT") )
e7549107 52 {
427ff662 53 m_text =wxT("Quit\tCtrl+Q") ;
e7549107 54 }
bf918b97
SC
55
56 m_radioGroup.start = -1;
57 m_isRadioGroupStart = FALSE;
e9576ca5
SC
58}
59
60wxMenuItem::~wxMenuItem()
61{
62}
63
bf918b97
SC
64// change item state
65// -----------------
51abe921 66
bf918b97
SC
67void wxMenuItem::SetBitmap(const wxBitmap& bitmap)
68{
e40298d5
JS
69 m_bitmap = bitmap;
70 UpdateItemBitmap() ;
51abe921
SC
71}
72
bf918b97 73void wxMenuItem::UpdateItemBitmap()
51abe921 74{
e40298d5
JS
75 if ( !m_parentMenu )
76 return ;
77
78 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
79 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
80 if( mhandle == NULL || index == 0)
81 return ;
82
83 if ( m_bitmap.Ok() )
84 {
85 ControlButtonContentInfo info ;
86 wxMacCreateBitmapButton( &info , m_bitmap , kControlContentCIconHandle ) ;
87 if ( info.contentType != kControlNoContent )
88 {
89 if ( info.contentType == kControlContentCIconHandle )
90 SetMenuItemIconHandle( mhandle , index ,
91 kMenuColorIconType , (Handle) info.u.cIconHandle ) ;
92 }
93
94 }
51abe921
SC
95}
96
bf918b97 97void wxMenuItem::UpdateItemStatus()
e9576ca5 98{
e40298d5
JS
99 if ( !m_parentMenu )
100 return ;
101
756c2704
SC
102#if TARGET_CARBON
103 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId)
104 {
105 if ( !IsEnabled() )
106 DisableMenuCommand( NULL , kHICommandPreferences ) ;
107 else
108 EnableMenuCommand( NULL , kHICommandPreferences ) ;
109 }
110 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId)
111 {
112 if ( !IsEnabled() )
113 DisableMenuCommand( NULL , kHICommandQuit ) ;
114 else
115 EnableMenuCommand( NULL , kHICommandQuit ) ;
116 }
117#endif
118 {
119 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
120 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
121 if( mhandle == NULL || index == 0)
122 return ;
123
124 UMAEnableMenuItem( mhandle , index , m_isEnabled ) ;
125 if ( IsCheckable() && IsChecked() )
126 ::SetItemMark( mhandle , index , 0x12 ) ; // checkmark
127 else
128 ::SetItemMark( mhandle , index , 0 ) ; // no mark
129
130 UMASetMenuItemText( mhandle , index , m_text ) ;
131 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
132 UMASetMenuItemShortcut( mhandle , index , entry ) ;
133 delete entry ;
134 }
e9576ca5
SC
135}
136
bf918b97
SC
137void wxMenuItem::UpdateItemText()
138{
e40298d5
JS
139 if ( !m_parentMenu )
140 return ;
141
142 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
143 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
144 if( mhandle == NULL || index == 0)
145 return ;
146
147 UMASetMenuItemText( mhandle , index , m_text ) ;
148 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
149 UMASetMenuItemShortcut( mhandle , index , entry ) ;
150 delete entry ;
bf918b97 151}
e7549107 152
e9576ca5
SC
153
154void wxMenuItem::Enable(bool bDoEnable)
155{
e40298d5
JS
156 if ( m_isEnabled != bDoEnable )
157 {
158 wxMenuItemBase::Enable( bDoEnable ) ;
159 UpdateItemStatus() ;
160 }
bf918b97
SC
161}
162void wxMenuItem::UncheckRadio()
163{
e40298d5
JS
164 if ( m_isChecked )
165 {
166 wxMenuItemBase::Check( false ) ;
167 UpdateItemStatus() ;
168 }
e9576ca5
SC
169}
170
171void wxMenuItem::Check(bool bDoCheck)
172{
427ff662 173 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
e40298d5
JS
174
175 if ( m_isChecked != bDoCheck )
176 {
177 if ( GetKind() == wxITEM_RADIO )
178 {
179 if ( bDoCheck )
180 {
181 wxMenuItemBase::Check( bDoCheck ) ;
182 UpdateItemStatus() ;
183
184 // get the index of this item in the menu
185 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
186 int pos = items.IndexOf(this);
187 wxCHECK_RET( pos != wxNOT_FOUND,
188 _T("menuitem not found in the menu items list?") );
189
190 // get the radio group range
191 int start,
192 end;
193
194 if ( m_isRadioGroupStart )
195 {
196 // we already have all information we need
197 start = pos;
198 end = m_radioGroup.end;
199 }
200 else // next radio group item
201 {
202 // get the radio group end from the start item
203 start = m_radioGroup.start;
204 end = items.Item(start)->GetData()->m_radioGroup.end;
205 }
206
207 // also uncheck all the other items in this radio group
208 wxMenuItemList::Node *node = items.Item(start);
209 for ( int n = start; n <= end && node; n++ )
210 {
211 if ( n != pos )
212 {
213 ((wxMenuItem*)node->GetData())->UncheckRadio();
214 }
215 node = node->GetNext();
216 }
217 }
218 }
219 else
220 {
221 wxMenuItemBase::Check( bDoCheck ) ;
222 UpdateItemStatus() ;
223 }
224 }
51abe921
SC
225}
226
227void wxMenuItem::SetText(const wxString& text)
228{
229 // don't do anything if label didn't change
230 if ( m_text == text )
231 return;
232
233 wxMenuItemBase::SetText(text);
bf918b97
SC
234
235 UpdateItemText() ;
236}
51abe921 237
bf918b97
SC
238// radio group stuff
239// -----------------
51abe921 240
bf918b97
SC
241void wxMenuItem::SetAsRadioGroupStart()
242{
243 m_isRadioGroupStart = TRUE;
51abe921 244}
bf918b97
SC
245
246void wxMenuItem::SetRadioGroupStart(int start)
51abe921 247{
bf918b97
SC
248 wxASSERT_MSG( !m_isRadioGroupStart,
249 _T("should only be called for the next radio items") );
250
251 m_radioGroup.start = start;
252}
253
254void wxMenuItem::SetRadioGroupEnd(int end)
255{
256 wxASSERT_MSG( m_isRadioGroupStart,
257 _T("should only be called for the first radio item") );
258
259 m_radioGroup.end = end;
51abe921
SC
260}
261
262// ----------------------------------------------------------------------------
263// wxMenuItemBase
264// ----------------------------------------------------------------------------
265
2f1ae414
SC
266/* static */
267wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
268{
269 return wxStripMenuCodes(text);
270}
271
51abe921
SC
272wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
273 int id,
274 const wxString& name,
275 const wxString& help,
d65c269b 276 wxItemKind kind,
51abe921
SC
277 wxMenu *subMenu)
278{
d65c269b 279 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
51abe921 280}