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