]> git.saurik.com Git - wxWidgets.git/blame - src/mac/menuitem.cpp
centralized key handling, control key handling
[wxWidgets.git] / src / mac / menuitem.cpp
CommitLineData
e9576ca5
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: menuitem.cpp
3// Purpose: wxMenuItem implementation
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
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
SC
37//
38// Helper Functions to get Mac Menus the way they should be ;-)
39//
40
41void wxMacCtoPString(const char* theCString, Str255 thePString);
42
43// remove inappropriate characters, if useShortcuts is false, the ampersand will not auto-generate a mac menu-shortcut
44
e437cc53 45int wxMenuItem::MacBuildMenuString(StringPtr outMacItemText, SInt16 *outMacShortcutChar , UInt8 *outMacModifiers , const char *inItemText , bool useShortcuts )
2f1ae414
SC
46{
47 char *p = (char *) &outMacItemText[1] ;
48 short macModifiers = 0 ;
49 char macShortCut = 0 ;
50 const char *inItemName ;
51 wxString inItemTextMac ;
52
53 if (wxApp::s_macDefaultEncodingIsPC)
54 {
55 inItemTextMac = wxMacMakeMacStringFromPC( inItemText ) ;
56 inItemName = inItemTextMac ;
57 }
58 else
59 {
60 inItemName = inItemText ;
61 }
62
63 if ( useShortcuts && !wxApp::s_macSupportPCMenuShortcuts )
64 useShortcuts = false ;
65
66 // we have problems with a leading hypen - it will be taken as a separator
67
68 while ( *inItemName == '-' )
69 inItemName++ ;
70
71 while( *inItemName )
72 {
73 switch ( *inItemName )
74 {
75 // special characters for macintosh menus -> use some replacement
76 case ';' :
77 *p++ = ',' ;
78 break ;
79 case '^' :
80 *p++ = ' ' ;
81 break ;
82 case '!' :
83 *p++ = ' ' ;
84 break ;
85 case '<' :
86 *p++ = '[' ;
87 break ;
88 case '>' :
89 *p++ = ']' ;
90 break ;
91 case '/' :
92 *p++ = '|' ;
93 break ;
94 case '(' :
95 *p++ = '[' ;
96 break ;
97 case ')' :
98 *p++ = ']' ;
99 break ;
100 // shortcuts
101 case '&' :
102 {
103 ++inItemName ;
104 if ( *inItemName )
105 {
106 *p++ = *inItemName ;
107 if ( useShortcuts )
108 macShortCut = *inItemName ;
109 }
110 else
111 --inItemName ;
112 }
113 break ;
114 // win-like accelerators
115 case '\t' :
116 {
117 ++inItemName ;
118 while( *inItemName )
119 {
120 if (strncmp("Ctrl", inItemName, 4) == 0)
121 {
122 inItemName = inItemName + 5;
123 macShortCut = *inItemName;
124 }
125 else if (strncmp("Cntrl", inItemName, 5) == 0)
126 {
127 inItemName = inItemName + 6;
128 macShortCut = *inItemName;
129 }
130 else if (strncmp("Alt", inItemName, 3) == 0)
131 {
132 inItemName = inItemName + 4;
133 macModifiers |= kMenuOptionModifier ;
134 macShortCut = *inItemName ;
135 }
136 else if (strncmp("Shift", inItemName, 5) == 0)
137 {
138 inItemName = inItemName + 6;
139 macModifiers |= kMenuShiftModifier ;
140 macShortCut = *inItemName ;
141 }
142 else if (strncmp("F", inItemName, 1) == 0)
143 {
144 inItemName += strlen( inItemName ) ;
145 // no function keys at the moment
146 // macModifiers |= kMenuShiftModifier ;
147 // macShortCut = *inItemName ;
148 }
149 else
150 {
151 break ;
152 }
153 }
154
155 if ( *inItemName == 0 )
156 --inItemName ;
157
158 }
159 break ;
160 default :
161 *p++ = *inItemName ;
162 }
163 ++inItemName ;
164 }
165
166 outMacItemText[0] = (p - (char *)outMacItemText) - 1;
167 if ( outMacShortcutChar )
168 *outMacShortcutChar = macShortCut ;
169 if ( outMacModifiers )
170 *outMacModifiers = macModifiers ;
171
172 return 0 ;
173}
174
e9576ca5
SC
175// ctor & dtor
176// -----------
177
d65c269b
VZ
178wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
179 int id,
180 const wxString& text,
181 const wxString& strHelp,
182 wxItemKind kind,
e7549107 183 wxMenu *pSubMenu)
d65c269b 184 : wxMenuItemBase(pParentMenu, id, text, strHelp, kind, pSubMenu)
e9576ca5 185{
d65c269b 186 // VZ: what about translations?? (FIXME)
f1ba2ee8 187 if ( m_text == "E&xit" ||m_text == "Exit" ||m_text.Left(5) == "Exit\t" || m_text.Left(6) == "E&xit\t" )
e7549107
SC
188 {
189 m_text = "Quit\tCtrl+Q" ;
190 }
e9576ca5
SC
191}
192
193wxMenuItem::~wxMenuItem()
194{
195}
196
51abe921
SC
197bool wxMenuItem::IsChecked() const
198{
199 return wxMenuItemBase::IsChecked() ;
200}
201
202wxString wxMenuItem::GetLabel() const
203{
204 return wxStripMenuCodes(m_text);
205}
206
207// accelerators
208// ------------
209
210#if wxUSE_ACCEL
211
212wxAcceleratorEntry *wxMenuItem::GetAccel() const
213{
214 return wxGetAccelFromString(GetText());
215}
216
217#endif // wxUSE_ACCEL
218
e9576ca5
SC
219// misc
220// ----
221
e7549107
SC
222/*
223
e9576ca5
SC
224// delete the sub menu
225void wxMenuItem::DeleteSubMenu()
226{
e7549107 227 wxASSERT( m_subMenu != NULL );
e9576ca5 228
e7549107
SC
229 delete m_subMenu;
230 m_subMenu = NULL;
e9576ca5
SC
231}
232
e7549107
SC
233*/
234
e9576ca5
SC
235// change item state
236// -----------------
237
238void wxMenuItem::Enable(bool bDoEnable)
239{
e7549107
SC
240 if ( m_isEnabled != bDoEnable ) {
241 if ( m_subMenu == NULL )
519cb848
SC
242 {
243 // normal menu item
76a5e5d2 244 if ( MAC_WXHMENU(m_parentMenu->GetHMenu()) )
519cb848 245 {
e7549107 246 int index = m_parentMenu->MacGetIndexFromItem( this ) ;
519cb848
SC
247 if ( index >= 1 )
248 {
249 if ( bDoEnable )
76a5e5d2 250 UMAEnableMenuItem( MAC_WXHMENU(m_parentMenu->GetHMenu()) , index ) ;
519cb848 251 else
76a5e5d2 252 UMADisableMenuItem( MAC_WXHMENU(m_parentMenu->GetHMenu()) , index ) ;
519cb848
SC
253 }
254 }
e9576ca5 255 }
519cb848 256 else
e9576ca5 257 {
519cb848 258 // submenu
76a5e5d2 259 if ( MAC_WXHMENU(m_parentMenu->GetHMenu()) )
519cb848 260 {
e7549107 261 int index = m_parentMenu->MacGetIndexFromItem( this ) ;
519cb848
SC
262 if ( index >= 1 )
263 {
264 if ( bDoEnable )
76a5e5d2 265 UMAEnableMenuItem( MAC_WXHMENU(m_parentMenu->GetHMenu()) , index ) ;
519cb848 266 else
76a5e5d2 267 UMADisableMenuItem( MAC_WXHMENU(m_parentMenu->GetHMenu()) , index ) ;
519cb848
SC
268 }
269 }
e9576ca5
SC
270 }
271
e7549107 272 m_isEnabled = bDoEnable;
e9576ca5
SC
273 }
274}
275
276void wxMenuItem::Check(bool bDoCheck)
277{
278 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
279
e7549107 280 if ( m_isChecked != bDoCheck )
519cb848 281 {
e7549107 282 m_isChecked = bDoCheck;
76a5e5d2 283 if ( MAC_WXHMENU(m_parentMenu->GetHMenu()) )
519cb848 284 {
e7549107 285 int index = m_parentMenu->MacGetIndexFromItem( this ) ;
519cb848
SC
286 if ( index >= 1 )
287 {
288 if ( bDoCheck )
76a5e5d2 289 ::SetItemMark( MAC_WXHMENU(m_parentMenu->GetHMenu()) , index , 0x12 ) ; // checkmark
519cb848 290 else
76a5e5d2 291 ::SetItemMark( MAC_WXHMENU(m_parentMenu->GetHMenu()) , index , 0 ) ; // no mark
519cb848
SC
292 }
293 }
e9576ca5 294 }
51abe921
SC
295}
296
297void wxMenuItem::SetText(const wxString& text)
298{
299 // don't do anything if label didn't change
300 if ( m_text == text )
301 return;
302
303 wxMenuItemBase::SetText(text);
304// OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) );
305
306 wxCHECK_RET( m_parentMenu && m_parentMenu->GetHMenu(), wxT("menuitem without menu") );
76a5e5d2 307 if ( MAC_WXHMENU(m_parentMenu->GetHMenu()) )
51abe921
SC
308 {
309 int index = m_parentMenu->MacGetIndexFromItem( this ) ;
310 if ( index >= 1 )
311 {
312 Str255 label;
2f1ae414 313 MacBuildMenuString( label , NULL , NULL , text ,false);
76a5e5d2 314 ::SetMenuItemText( MAC_WXHMENU(m_parentMenu->GetHMenu()) , index , label ) ; // checkmark
51abe921
SC
315 }
316 }
317
318#if wxUSE_ACCEL
319 m_parentMenu->UpdateAccel(this);
320#endif // wxUSE_ACCEL
321
322}
323void wxMenuItem::SetCheckable(bool checkable)
324{
325 wxMenuItemBase::SetCheckable(checkable);
326 // OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable) );
327}
328
329// ----------------------------------------------------------------------------
330// wxMenuItemBase
331// ----------------------------------------------------------------------------
332
2f1ae414
SC
333/* static */
334wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
335{
336 return wxStripMenuCodes(text);
337}
338
51abe921
SC
339wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
340 int id,
341 const wxString& name,
342 const wxString& help,
d65c269b 343 wxItemKind kind,
51abe921
SC
344 wxMenu *subMenu)
345{
d65c269b 346 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
51abe921 347}