]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/menuitem.cpp
using new API (no visual difference)
[wxWidgets.git] / src / mac / carbon / menuitem.cpp
CommitLineData
e9576ca5
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: menuitem.cpp
3// Purpose: wxMenuItem implementation
a31a5f85 4// Author: Stefan Csomor
3dee36ae 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
3d1a4878
SC
12#include "wx/wxprec.h"
13
03e11df5 14#include "wx/app.h"
e9576ca5
SC
15#include "wx/menu.h"
16#include "wx/menuitem.h"
17
d497dca4 18#include "wx/mac/uma.h"
e9576ca5 19
fd04970a 20IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
37e2cb08 21
e9576ca5 22
d65c269b
VZ
23wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
24 int id,
25 const wxString& text,
26 const wxString& strHelp,
27 wxItemKind kind,
3dee36ae
WS
28 wxMenu *pSubMenu)
29 :wxMenuItemBase(pParentMenu, id, text, strHelp, kind, pSubMenu)
e9576ca5 30{
408aef5e 31 wxASSERT_MSG( id != 0 || pSubMenu != NULL , wxT("A MenuItem ID of Zero does not work under Mac") ) ;
3dee36ae 32
bf918b97
SC
33 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
34 // therefore these item must not be translated
43524b15
DS
35 if ( wxStripMenuCodes(m_text).Upper() == wxT("EXIT") )
36 m_text = wxT("Quit\tCtrl+Q") ;
bf918b97
SC
37
38 m_radioGroup.start = -1;
3dee36ae 39 m_isRadioGroupStart = false;
e9576ca5
SC
40}
41
3dee36ae 42wxMenuItem::~wxMenuItem()
e9576ca5
SC
43{
44}
45
bf918b97
SC
46// change item state
47// -----------------
51abe921 48
3dee36ae
WS
49void wxMenuItem::SetBitmap(const wxBitmap& bitmap)
50{
51 m_bitmap = bitmap;
52 UpdateItemBitmap();
51abe921
SC
53}
54
3dee36ae 55void wxMenuItem::UpdateItemBitmap()
51abe921 56{
e40298d5
JS
57 if ( !m_parentMenu )
58 return ;
3dee36ae 59
e40298d5
JS
60 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
61 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
43524b15 62 if ( mhandle == NULL || index == 0)
e40298d5 63 return ;
3dee36ae 64
e40298d5
JS
65 if ( m_bitmap.Ok() )
66 {
23af264e 67#if wxUSE_BMPBUTTON
e40298d5 68 ControlButtonContentInfo info ;
b2f3b00b 69 wxMacCreateBitmapButton( &info , m_bitmap ) ;
e40298d5
JS
70 if ( info.contentType != kControlNoContent )
71 {
b2f3b00b 72 if ( info.contentType == kControlContentIconRef )
3dee36ae 73 SetMenuItemIconHandle( mhandle , index ,
b2f3b00b 74 kMenuIconRefType , (Handle) info.u.iconRef ) ;
e40298d5 75 }
20b69855 76 wxMacReleaseBitmapButton( &info ) ;
3dee36ae 77#endif
e40298d5 78 }
51abe921
SC
79}
80
3dee36ae 81void wxMenuItem::UpdateItemStatus()
e9576ca5 82{
e40298d5
JS
83 if ( !m_parentMenu )
84 return ;
43524b15 85
ea41ff3a
DS
86 if ( IsSeparator() )
87 return ;
3dee36ae 88
756c2704
SC
89#if TARGET_CARBON
90 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId)
91 {
92 if ( !IsEnabled() )
93 DisableMenuCommand( NULL , kHICommandPreferences ) ;
94 else
95 EnableMenuCommand( NULL , kHICommandPreferences ) ;
96 }
ea41ff3a 97
756c2704
SC
98 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId)
99 {
100 if ( !IsEnabled() )
101 DisableMenuCommand( NULL , kHICommandQuit ) ;
102 else
103 EnableMenuCommand( NULL , kHICommandQuit ) ;
104 }
105#endif
ea41ff3a 106
3dee36ae 107 {
756c2704
SC
108 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
109 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
ea41ff3a 110 if ( mhandle == NULL || index == 0)
756c2704
SC
111 return ;
112
3dee36ae
WS
113 UMAEnableMenuItem( mhandle , index , m_isEnabled ) ;
114 if ( IsCheckable() && IsChecked() )
756c2704
SC
115 ::SetItemMark( mhandle , index , 0x12 ) ; // checkmark
116 else
117 ::SetItemMark( mhandle , index , 0 ) ; // no mark
118
4c5dae08 119 UMASetMenuItemText( mhandle , index , wxStripMenuCodes(m_text) , wxFont::GetDefaultEncoding() ) ;
3dee36ae 120 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
756c2704
SC
121 UMASetMenuItemShortcut( mhandle , index , entry ) ;
122 delete entry ;
123 }
e9576ca5
SC
124}
125
3dee36ae 126void wxMenuItem::UpdateItemText()
bf918b97 127{
e40298d5
JS
128 if ( !m_parentMenu )
129 return ;
3dee36ae 130
e40298d5
JS
131 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
132 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
43524b15 133 if (mhandle == NULL || index == 0)
e40298d5
JS
134 return ;
135
4c5dae08 136 UMASetMenuItemText( mhandle , index , wxStripMenuCodes(m_text) , wxFont::GetDefaultEncoding() ) ;
3dee36ae 137 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
e40298d5
JS
138 UMASetMenuItemShortcut( mhandle , index , entry ) ;
139 delete entry ;
bf918b97 140}
e7549107 141
e9576ca5
SC
142void wxMenuItem::Enable(bool bDoEnable)
143{
a2d3a3d1 144 if (( m_isEnabled != bDoEnable
bdacb147 145#if TARGET_CARBON
a2d3a3d1
JS
146 // avoid changing menuitem state when menu is disabled
147 // eg. BeginAppModalStateForWindow() will disable menus and ignore this change
148 // which in turn causes m_isEnabled to become out of sync with real menuitem state
149 && !(m_parentMenu && !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu->GetHMenu()), 0)) )
150 // always update builtin menuitems
151 || ( GetId() == wxApp::s_macPreferencesMenuItemId
152 || GetId() == wxApp::s_macExitMenuItemId
153 || GetId() == wxApp::s_macAboutMenuItemId
bdacb147 154#endif
a2d3a3d1 155 ))
e40298d5
JS
156 {
157 wxMenuItemBase::Enable( bDoEnable ) ;
158 UpdateItemStatus() ;
159 }
bf918b97 160}
ea41ff3a 161
bf918b97
SC
162void wxMenuItem::UncheckRadio()
163{
3dee36ae 164 if ( m_isChecked )
e40298d5
JS
165 {
166 wxMenuItemBase::Check( false ) ;
167 UpdateItemStatus() ;
168 }
e9576ca5
SC
169}
170
171void wxMenuItem::Check(bool bDoCheck)
172{
ea41ff3a 173 wxCHECK_RET( IsCheckable() && !IsSeparator(), wxT("only checkable items may be checked") );
e40298d5 174
3dee36ae 175 if ( m_isChecked != bDoCheck )
e40298d5
JS
176 {
177 if ( GetKind() == wxITEM_RADIO )
178 {
179 if ( bDoCheck )
180 {
181 wxMenuItemBase::Check( bDoCheck ) ;
182 UpdateItemStatus() ;
3dee36ae 183
e40298d5
JS
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
ea41ff3a 191 int start, end;
e40298d5
JS
192
193 if ( m_isRadioGroupStart )
194 {
195 // we already have all information we need
196 start = pos;
197 end = m_radioGroup.end;
198 }
199 else // next radio group item
200 {
201 // get the radio group end from the start item
202 start = m_radioGroup.start;
203 end = items.Item(start)->GetData()->m_radioGroup.end;
204 }
205
206 // also uncheck all the other items in this radio group
affd2611 207 wxMenuItemList::compatibility_iterator node = items.Item(start);
e40298d5
JS
208 for ( int n = start; n <= end && node; n++ )
209 {
210 if ( n != pos )
e40298d5 211 ((wxMenuItem*)node->GetData())->UncheckRadio();
ea41ff3a 212
e40298d5
JS
213 node = node->GetNext();
214 }
215 }
216 }
217 else
218 {
219 wxMenuItemBase::Check( bDoCheck ) ;
220 UpdateItemStatus() ;
221 }
222 }
51abe921
SC
223}
224
225void wxMenuItem::SetText(const wxString& text)
226{
227 // don't do anything if label didn't change
228 if ( m_text == text )
229 return;
230
231 wxMenuItemBase::SetText(text);
3dee36ae 232
bf918b97
SC
233 UpdateItemText() ;
234}
51abe921 235
bf918b97
SC
236// radio group stuff
237// -----------------
51abe921 238
bf918b97
SC
239void wxMenuItem::SetAsRadioGroupStart()
240{
3dee36ae 241 m_isRadioGroupStart = true;
51abe921 242}
bf918b97
SC
243
244void wxMenuItem::SetRadioGroupStart(int start)
51abe921 245{
bf918b97 246 wxASSERT_MSG( !m_isRadioGroupStart,
43524b15 247 wxT("should only be called for the next radio items") );
bf918b97
SC
248
249 m_radioGroup.start = start;
250}
251
252void wxMenuItem::SetRadioGroupEnd(int end)
253{
254 wxASSERT_MSG( m_isRadioGroupStart,
43524b15 255 wxT("should only be called for the first radio item") );
bf918b97
SC
256
257 m_radioGroup.end = end;
51abe921
SC
258}
259
260// ----------------------------------------------------------------------------
261// wxMenuItemBase
262// ----------------------------------------------------------------------------
263
2f1ae414
SC
264/* static */
265wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
266{
267 return wxStripMenuCodes(text);
268}
269
51abe921
SC
270wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
271 int id,
272 const wxString& name,
273 const wxString& help,
d65c269b 274 wxItemKind kind,
51abe921
SC
275 wxMenu *subMenu)
276{
d65c269b 277 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
51abe921 278}