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