]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/menuitem.cpp
popup activation scope and l&f
[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 ) ;
6239ee05
SC
66 DoUpdateItemBitmap( mhandle, index );
67}
68
69void wxMenuItem::DoUpdateItemBitmap( WXHMENU menu, wxUint16 index)
70{
71 MenuHandle mhandle = (MenuHandle) menu;
72
43524b15 73 if ( mhandle == NULL || index == 0)
e40298d5 74 return ;
3dee36ae 75
e40298d5
JS
76 if ( m_bitmap.Ok() )
77 {
23af264e 78#if wxUSE_BMPBUTTON
e40298d5 79 ControlButtonContentInfo info ;
b2f3b00b 80 wxMacCreateBitmapButton( &info , m_bitmap ) ;
e40298d5
JS
81 if ( info.contentType != kControlNoContent )
82 {
b2f3b00b 83 if ( info.contentType == kControlContentIconRef )
3dee36ae 84 SetMenuItemIconHandle( mhandle , index ,
b2f3b00b 85 kMenuIconRefType , (Handle) info.u.iconRef ) ;
6239ee05
SC
86 else if ( info.contentType == kControlContentCGImageRef )
87 SetMenuItemIconHandle( mhandle , index ,
88 kMenuCGImageRefType , (Handle) info.u.imageRef ) ;
e40298d5 89 }
20b69855 90 wxMacReleaseBitmapButton( &info ) ;
3dee36ae 91#endif
e40298d5 92 }
51abe921
SC
93}
94
3dee36ae 95void wxMenuItem::UpdateItemStatus()
e9576ca5 96{
e40298d5
JS
97 if ( !m_parentMenu )
98 return ;
43524b15 99
ea41ff3a
DS
100 if ( IsSeparator() )
101 return ;
3dee36ae 102
03561a3c 103 if ( GetId() == wxApp::s_macPreferencesMenuItemId)
756c2704
SC
104 {
105 if ( !IsEnabled() )
106 DisableMenuCommand( NULL , kHICommandPreferences ) ;
107 else
108 EnableMenuCommand( NULL , kHICommandPreferences ) ;
109 }
ea41ff3a 110
03561a3c 111 if ( GetId() == wxApp::s_macExitMenuItemId)
756c2704
SC
112 {
113 if ( !IsEnabled() )
114 DisableMenuCommand( NULL , kHICommandQuit ) ;
115 else
116 EnableMenuCommand( NULL , kHICommandQuit ) ;
117 }
ea41ff3a 118
3dee36ae 119 {
756c2704
SC
120 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
121 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
ea41ff3a 122 if ( mhandle == NULL || index == 0)
756c2704
SC
123 return ;
124
3dee36ae
WS
125 UMAEnableMenuItem( mhandle , index , m_isEnabled ) ;
126 if ( IsCheckable() && IsChecked() )
756c2704
SC
127 ::SetItemMark( mhandle , index , 0x12 ) ; // checkmark
128 else
129 ::SetItemMark( mhandle , index , 0 ) ; // no mark
130
4c5dae08 131 UMASetMenuItemText( mhandle , index , wxStripMenuCodes(m_text) , wxFont::GetDefaultEncoding() ) ;
90527a50 132 wxAcceleratorEntry *entry = wxAcceleratorEntry::Create( m_text ) ;
756c2704
SC
133 UMASetMenuItemShortcut( mhandle , index , entry ) ;
134 delete entry ;
135 }
e9576ca5
SC
136}
137
3dee36ae 138void wxMenuItem::UpdateItemText()
bf918b97 139{
e40298d5
JS
140 if ( !m_parentMenu )
141 return ;
3dee36ae 142
e40298d5
JS
143 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
144 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
43524b15 145 if (mhandle == NULL || index == 0)
e40298d5
JS
146 return ;
147
ee0a94cf
RR
148 wxString text = m_text;
149 if (text.IsEmpty() && !IsSeparator())
150 {
151 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
152 text = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR|wxSTOCK_WITH_MNEMONIC);
153 }
154
155 UMASetMenuItemText( mhandle , index , wxStripMenuCodes(text) , wxFont::GetDefaultEncoding() ) ;
90527a50 156 wxAcceleratorEntry *entry = wxAcceleratorEntry::Create( text ) ;
e40298d5
JS
157 UMASetMenuItemShortcut( mhandle , index , entry ) ;
158 delete entry ;
bf918b97 159}
e7549107 160
e9576ca5
SC
161void wxMenuItem::Enable(bool bDoEnable)
162{
a2d3a3d1 163 if (( m_isEnabled != bDoEnable
a2d3a3d1
JS
164 // avoid changing menuitem state when menu is disabled
165 // eg. BeginAppModalStateForWindow() will disable menus and ignore this change
166 // which in turn causes m_isEnabled to become out of sync with real menuitem state
167 && !(m_parentMenu && !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu->GetHMenu()), 0)) )
168 // always update builtin menuitems
169 || ( GetId() == wxApp::s_macPreferencesMenuItemId
170 || GetId() == wxApp::s_macExitMenuItemId
171 || GetId() == wxApp::s_macAboutMenuItemId
a2d3a3d1 172 ))
e40298d5
JS
173 {
174 wxMenuItemBase::Enable( bDoEnable ) ;
175 UpdateItemStatus() ;
176 }
bf918b97 177}
ea41ff3a 178
bf918b97
SC
179void wxMenuItem::UncheckRadio()
180{
3dee36ae 181 if ( m_isChecked )
e40298d5
JS
182 {
183 wxMenuItemBase::Check( false ) ;
184 UpdateItemStatus() ;
185 }
e9576ca5
SC
186}
187
188void wxMenuItem::Check(bool bDoCheck)
189{
ea41ff3a 190 wxCHECK_RET( IsCheckable() && !IsSeparator(), wxT("only checkable items may be checked") );
e40298d5 191
3dee36ae 192 if ( m_isChecked != bDoCheck )
e40298d5
JS
193 {
194 if ( GetKind() == wxITEM_RADIO )
195 {
196 if ( bDoCheck )
197 {
198 wxMenuItemBase::Check( bDoCheck ) ;
199 UpdateItemStatus() ;
3dee36ae 200
e40298d5
JS
201 // get the index of this item in the menu
202 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
203 int pos = items.IndexOf(this);
204 wxCHECK_RET( pos != wxNOT_FOUND,
205 _T("menuitem not found in the menu items list?") );
206
207 // get the radio group range
ea41ff3a 208 int start, end;
e40298d5
JS
209
210 if ( m_isRadioGroupStart )
211 {
212 // we already have all information we need
213 start = pos;
214 end = m_radioGroup.end;
215 }
216 else // next radio group item
217 {
218 // get the radio group end from the start item
219 start = m_radioGroup.start;
220 end = items.Item(start)->GetData()->m_radioGroup.end;
221 }
222
223 // also uncheck all the other items in this radio group
affd2611 224 wxMenuItemList::compatibility_iterator node = items.Item(start);
e40298d5
JS
225 for ( int n = start; n <= end && node; n++ )
226 {
227 if ( n != pos )
e40298d5 228 ((wxMenuItem*)node->GetData())->UncheckRadio();
ea41ff3a 229
e40298d5
JS
230 node = node->GetNext();
231 }
232 }
233 }
234 else
235 {
236 wxMenuItemBase::Check( bDoCheck ) ;
237 UpdateItemStatus() ;
238 }
239 }
51abe921
SC
240}
241
52af3158 242void wxMenuItem::SetItemLabel(const wxString& text)
51abe921
SC
243{
244 // don't do anything if label didn't change
245 if ( m_text == text )
246 return;
247
52af3158 248 wxMenuItemBase::SetItemLabel(text);
3dee36ae 249
bf918b97
SC
250 UpdateItemText() ;
251}
51abe921 252
bf918b97
SC
253// radio group stuff
254// -----------------
51abe921 255
bf918b97
SC
256void wxMenuItem::SetAsRadioGroupStart()
257{
3dee36ae 258 m_isRadioGroupStart = true;
51abe921 259}
bf918b97
SC
260
261void wxMenuItem::SetRadioGroupStart(int start)
51abe921 262{
bf918b97 263 wxASSERT_MSG( !m_isRadioGroupStart,
43524b15 264 wxT("should only be called for the next radio items") );
bf918b97
SC
265
266 m_radioGroup.start = start;
267}
268
269void wxMenuItem::SetRadioGroupEnd(int end)
270{
271 wxASSERT_MSG( m_isRadioGroupStart,
43524b15 272 wxT("should only be called for the first radio item") );
bf918b97
SC
273
274 m_radioGroup.end = end;
51abe921
SC
275}
276
277// ----------------------------------------------------------------------------
278// wxMenuItemBase
279// ----------------------------------------------------------------------------
280
2f1ae414 281/* static */
52af3158 282wxString wxMenuItemBase::GetLabelText(const wxString& text)
2f1ae414
SC
283{
284 return wxStripMenuCodes(text);
285}
286
51abe921
SC
287wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
288 int id,
289 const wxString& name,
290 const wxString& help,
d65c269b 291 wxItemKind kind,
51abe921
SC
292 wxMenu *subMenu)
293{
d65c269b 294 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
51abe921 295}