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