]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/menuitem.cpp
stubs for wxUSE_DBGHELP == 0
[wxWidgets.git] / src / mac / carbon / 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
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers & declarations
14// ============================================================================
15
3d1a4878
SC
16#include "wx/wxprec.h"
17
03e11df5 18#include "wx/app.h"
e9576ca5
SC
19#include "wx/menu.h"
20#include "wx/menuitem.h"
21
d497dca4 22#include "wx/mac/uma.h"
e9576ca5
SC
23// ============================================================================
24// implementation
25// ============================================================================
26
27// ----------------------------------------------------------------------------
28// dynamic classes implementation
29// ----------------------------------------------------------------------------
30
2f1ae414 31#if !USE_SHARED_LIBRARY
e9576ca5 32 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
2f1ae414 33#endif //USE_SHARED_LIBRARY
37e2cb08 34
e9576ca5
SC
35// ----------------------------------------------------------------------------
36// wxMenuItem
37// ----------------------------------------------------------------------------
38
2f1ae414 39//
e9576ca5
SC
40// ctor & dtor
41// -----------
42
d65c269b
VZ
43wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
44 int id,
45 const wxString& text,
46 const wxString& strHelp,
47 wxItemKind kind,
e7549107 48 wxMenu *pSubMenu)
d65c269b 49 : wxMenuItemBase(pParentMenu, id, text, strHelp, kind, pSubMenu)
e9576ca5 50{
408aef5e 51 wxASSERT_MSG( id != 0 || pSubMenu != NULL , wxT("A MenuItem ID of Zero does not work under Mac") ) ;
69991fff 52
bf918b97
SC
53 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
54 // therefore these item must not be translated
427ff662 55 if ( wxStripMenuCodes(m_text).Upper() == wxT("EXIT") )
e7549107 56 {
427ff662 57 m_text =wxT("Quit\tCtrl+Q") ;
e7549107 58 }
bf918b97
SC
59
60 m_radioGroup.start = -1;
61 m_isRadioGroupStart = FALSE;
e9576ca5
SC
62}
63
64wxMenuItem::~wxMenuItem()
65{
66}
67
bf918b97
SC
68// change item state
69// -----------------
51abe921 70
bf918b97
SC
71void wxMenuItem::SetBitmap(const wxBitmap& bitmap)
72{
e40298d5
JS
73 m_bitmap = bitmap;
74 UpdateItemBitmap() ;
51abe921
SC
75}
76
bf918b97 77void wxMenuItem::UpdateItemBitmap()
51abe921 78{
e40298d5
JS
79 if ( !m_parentMenu )
80 return ;
81
82 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
83 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
84 if( mhandle == NULL || index == 0)
85 return ;
86
87 if ( m_bitmap.Ok() )
88 {
89 ControlButtonContentInfo info ;
90 wxMacCreateBitmapButton( &info , m_bitmap , kControlContentCIconHandle ) ;
91 if ( info.contentType != kControlNoContent )
92 {
93 if ( info.contentType == kControlContentCIconHandle )
94 SetMenuItemIconHandle( mhandle , index ,
95 kMenuColorIconType , (Handle) info.u.cIconHandle ) ;
96 }
97
98 }
51abe921
SC
99}
100
bf918b97 101void wxMenuItem::UpdateItemStatus()
e9576ca5 102{
e40298d5
JS
103 if ( !m_parentMenu )
104 return ;
105
756c2704
SC
106#if TARGET_CARBON
107 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId)
108 {
109 if ( !IsEnabled() )
110 DisableMenuCommand( NULL , kHICommandPreferences ) ;
111 else
112 EnableMenuCommand( NULL , kHICommandPreferences ) ;
113 }
114 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId)
115 {
116 if ( !IsEnabled() )
117 DisableMenuCommand( NULL , kHICommandQuit ) ;
118 else
119 EnableMenuCommand( NULL , kHICommandQuit ) ;
120 }
121#endif
122 {
123 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
124 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
125 if( mhandle == NULL || index == 0)
126 return ;
127
128 UMAEnableMenuItem( mhandle , index , m_isEnabled ) ;
129 if ( IsCheckable() && IsChecked() )
130 ::SetItemMark( mhandle , index , 0x12 ) ; // checkmark
131 else
132 ::SetItemMark( mhandle , index , 0 ) ; // no mark
133
a9412f8f 134 UMASetMenuItemText( mhandle , index , m_text , wxFont::GetDefaultEncoding() ) ;
756c2704
SC
135 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
136 UMASetMenuItemShortcut( mhandle , index , entry ) ;
137 delete entry ;
138 }
e9576ca5
SC
139}
140
bf918b97
SC
141void wxMenuItem::UpdateItemText()
142{
e40298d5
JS
143 if ( !m_parentMenu )
144 return ;
145
146 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
147 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
148 if( mhandle == NULL || index == 0)
149 return ;
150
a9412f8f 151 UMASetMenuItemText( mhandle , index , m_text , wxFont::GetDefaultEncoding() ) ;
e40298d5
JS
152 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
153 UMASetMenuItemShortcut( mhandle , index , entry ) ;
154 delete entry ;
bf918b97 155}
e7549107 156
e9576ca5
SC
157
158void wxMenuItem::Enable(bool bDoEnable)
159{
bdacb147
JS
160 if ( m_isEnabled != bDoEnable
161#if TARGET_CARBON
162 || GetId() == wxApp::s_macPreferencesMenuItemId
163 || GetId() == wxApp::s_macExitMenuItemId
164 || GetId() == wxApp::s_macAboutMenuItemId
165#endif
166 )
e40298d5
JS
167 {
168 wxMenuItemBase::Enable( bDoEnable ) ;
169 UpdateItemStatus() ;
170 }
bf918b97
SC
171}
172void wxMenuItem::UncheckRadio()
173{
e40298d5
JS
174 if ( m_isChecked )
175 {
176 wxMenuItemBase::Check( false ) ;
177 UpdateItemStatus() ;
178 }
e9576ca5
SC
179}
180
181void wxMenuItem::Check(bool bDoCheck)
182{
427ff662 183 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
e40298d5
JS
184
185 if ( m_isChecked != bDoCheck )
186 {
187 if ( GetKind() == wxITEM_RADIO )
188 {
189 if ( bDoCheck )
190 {
191 wxMenuItemBase::Check( bDoCheck ) ;
192 UpdateItemStatus() ;
193
194 // get the index of this item in the menu
195 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
196 int pos = items.IndexOf(this);
197 wxCHECK_RET( pos != wxNOT_FOUND,
198 _T("menuitem not found in the menu items list?") );
199
200 // get the radio group range
201 int start,
202 end;
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 )
222 {
223 ((wxMenuItem*)node->GetData())->UncheckRadio();
224 }
225 node = node->GetNext();
226 }
227 }
228 }
229 else
230 {
231 wxMenuItemBase::Check( bDoCheck ) ;
232 UpdateItemStatus() ;
233 }
234 }
51abe921
SC
235}
236
237void wxMenuItem::SetText(const wxString& text)
238{
239 // don't do anything if label didn't change
240 if ( m_text == text )
241 return;
242
243 wxMenuItemBase::SetText(text);
bf918b97
SC
244
245 UpdateItemText() ;
246}
51abe921 247
bf918b97
SC
248// radio group stuff
249// -----------------
51abe921 250
bf918b97
SC
251void wxMenuItem::SetAsRadioGroupStart()
252{
253 m_isRadioGroupStart = TRUE;
51abe921 254}
bf918b97
SC
255
256void wxMenuItem::SetRadioGroupStart(int start)
51abe921 257{
bf918b97
SC
258 wxASSERT_MSG( !m_isRadioGroupStart,
259 _T("should only be called for the next radio items") );
260
261 m_radioGroup.start = start;
262}
263
264void wxMenuItem::SetRadioGroupEnd(int end)
265{
266 wxASSERT_MSG( m_isRadioGroupStart,
267 _T("should only be called for the first radio item") );
268
269 m_radioGroup.end = end;
51abe921
SC
270}
271
272// ----------------------------------------------------------------------------
273// wxMenuItemBase
274// ----------------------------------------------------------------------------
275
2f1ae414
SC
276/* static */
277wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
278{
279 return wxStripMenuCodes(text);
280}
281
51abe921
SC
282wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
283 int id,
284 const wxString& name,
285 const wxString& help,
d65c269b 286 wxItemKind kind,
51abe921
SC
287 wxMenu *subMenu)
288{
d65c269b 289 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
51abe921 290}