Merge in from trunk r67662 to r64801
[wxWidgets.git] / src / osx / carbon / uma.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/uma.cpp
3 // Purpose: UMA support
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: The wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/osx/uma.h"
15
16 #if wxUSE_GUI
17
18 #include "wx/toplevel.h"
19 #include "wx/dc.h"
20
21 #include "wx/osx/uma.h"
22
23 // menu manager
24
25 #if wxOSX_USE_CARBON
26
27 MenuRef UMANewMenu( SInt16 id , const wxString& title , wxFontEncoding encoding )
28 {
29 wxString str = wxStripMenuCodes( title ) ;
30 MenuRef menu ;
31
32 CreateNewMenu( id , 0 , &menu ) ;
33 SetMenuTitleWithCFString( menu , wxCFStringRef(str , encoding ) ) ;
34
35 return menu ;
36 }
37
38 void UMASetMenuTitle( MenuRef menu , const wxString& title , wxFontEncoding encoding )
39 {
40 wxString str = wxStripMenuCodes( title ) ;
41
42 SetMenuTitleWithCFString( menu , wxCFStringRef(str , encoding) ) ;
43 }
44
45 void UMASetMenuItemText( MenuRef menu, MenuItemIndex item, const wxString& title, wxFontEncoding encoding )
46 {
47 // we don't strip the accels here anymore, must be done before
48 wxString str = title ;
49
50 SetMenuItemTextWithCFString( menu , item , wxCFStringRef(str , encoding) ) ;
51 }
52
53 void UMAEnableMenuItem( MenuRef inMenu , MenuItemIndex inItem , bool enable)
54 {
55 if ( enable )
56 EnableMenuItem( inMenu , inItem ) ;
57 else
58 DisableMenuItem( inMenu , inItem ) ;
59 }
60
61 void UMAAppendSubMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , MenuRef submenu )
62 {
63 AppendMenuItemTextWithCFString( menu,
64 CFSTR("A"), 0, 0,NULL);
65 UMASetMenuItemText( menu, (SInt16) ::CountMenuItems(menu), title , encoding );
66 SetMenuItemHierarchicalMenu( menu , CountMenuItems( menu ) , submenu ) ;
67 SetMenuTitleWithCFString(submenu , wxCFStringRef(title , encoding) );
68 }
69
70 void UMAInsertSubMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , MenuItemIndex item , SInt16 id )
71 {
72 InsertMenuItemTextWithCFString( menu,
73 CFSTR("A"), item, 0, 0);
74
75 UMASetMenuItemText( menu, item+1, title , encoding );
76 SetMenuItemHierarchicalID( menu , item+1 , id ) ;
77 }
78
79 void UMASetMenuItemShortcut( MenuRef menu , MenuItemIndex item , wxAcceleratorEntry *entry )
80 {
81 if ( !entry )
82 {
83 SetMenuItemCommandKey(menu, item, false, 0);
84 return ;
85 }
86
87 UInt8 modifiers = 0 ;
88 SInt16 key = entry->GetKeyCode() ;
89 if ( key )
90 {
91 bool explicitCommandKey = (entry->GetFlags() & wxACCEL_CTRL);
92
93 if (entry->GetFlags() & wxACCEL_ALT)
94 modifiers |= kMenuOptionModifier ;
95
96 if (entry->GetFlags() & wxACCEL_SHIFT)
97 modifiers |= kMenuShiftModifier ;
98
99 SInt16 glyph = 0 ;
100 SInt16 macKey = key ;
101 if ( key >= WXK_F1 && key <= WXK_F15 )
102 {
103 if ( !explicitCommandKey )
104 modifiers |= kMenuNoCommandModifier ;
105
106 // for some reasons this must be 0 right now
107 // everything else leads to just the first function key item
108 // to be selected. Thanks to Ryan Wilcox for finding out.
109 macKey = 0 ;
110 glyph = kMenuF1Glyph + ( key - WXK_F1 ) ;
111 if ( key >= WXK_F13 )
112 glyph += 12 ;
113 }
114 else
115 {
116 switch ( key )
117 {
118 case WXK_BACK :
119 macKey = kBackspaceCharCode ;
120 glyph = kMenuDeleteLeftGlyph ;
121 break ;
122
123 case WXK_TAB :
124 macKey = kTabCharCode ;
125 glyph = kMenuTabRightGlyph ;
126 break ;
127
128 case kEnterCharCode :
129 macKey = kEnterCharCode ;
130 glyph = kMenuEnterGlyph ;
131 break ;
132
133 case WXK_RETURN :
134 macKey = kReturnCharCode ;
135 glyph = kMenuReturnGlyph ;
136 break ;
137
138 case WXK_ESCAPE :
139 macKey = kEscapeCharCode ;
140 glyph = kMenuEscapeGlyph ;
141 break ;
142
143 case WXK_SPACE :
144 macKey = ' ' ;
145 glyph = kMenuSpaceGlyph ;
146 break ;
147
148 case WXK_DELETE :
149 macKey = kDeleteCharCode ;
150 glyph = kMenuDeleteRightGlyph ;
151 break ;
152
153 case WXK_CLEAR :
154 macKey = kClearCharCode ;
155 glyph = kMenuClearGlyph ;
156 break ;
157
158 case WXK_PAGEUP :
159 macKey = kPageUpCharCode ;
160 glyph = kMenuPageUpGlyph ;
161 break ;
162
163 case WXK_PAGEDOWN :
164 macKey = kPageDownCharCode ;
165 glyph = kMenuPageDownGlyph ;
166 break ;
167
168 case WXK_LEFT :
169 macKey = kLeftArrowCharCode ;
170 glyph = kMenuLeftArrowGlyph ;
171 break ;
172
173 case WXK_UP :
174 macKey = kUpArrowCharCode ;
175 glyph = kMenuUpArrowGlyph ;
176 break ;
177
178 case WXK_RIGHT :
179 macKey = kRightArrowCharCode ;
180 glyph = kMenuRightArrowGlyph ;
181 break ;
182
183 case WXK_DOWN :
184 macKey = kDownArrowCharCode ;
185 glyph = kMenuDownArrowGlyph ;
186 break ;
187
188 case WXK_HOME :
189 macKey = kHomeCharCode ;
190 glyph = kMenuNorthwestArrowGlyph ;
191 break ;
192
193 case WXK_END :
194 macKey = kEndCharCode ;
195 glyph = kMenuSoutheastArrowGlyph ;
196 break ;
197 default :
198 macKey = toupper( key ) ;
199 break ;
200 }
201
202 // we now allow non command key shortcuts
203 // remove in case this gives problems
204 if ( !explicitCommandKey )
205 modifiers |= kMenuNoCommandModifier ;
206 }
207
208 // 1d and 1e have special meaning to SetItemCmd, so
209 // do not use for these character codes.
210 if (key != WXK_UP && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_LEFT)
211 SetItemCmd( menu, item , macKey );
212
213 SetMenuItemModifiers( menu, item , modifiers ) ;
214
215 if ( glyph )
216 SetMenuItemKeyGlyph( menu, item , glyph ) ;
217 }
218 }
219
220 void UMAAppendMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , wxAcceleratorEntry *entry )
221 {
222 AppendMenuItemTextWithCFString( menu,
223 CFSTR("A"), 0, 0,NULL);
224 // don't attempt to interpret metacharacters like a '-' at the beginning (would become a separator otherwise)
225 ChangeMenuItemAttributes( menu , ::CountMenuItems(menu), kMenuItemAttrIgnoreMeta , 0 ) ;
226 UMASetMenuItemText(menu, (SInt16) ::CountMenuItems(menu), title , encoding );
227 UMASetMenuItemShortcut( menu , (SInt16) ::CountMenuItems(menu), entry ) ;
228 }
229
230 void UMAInsertMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , MenuItemIndex item , wxAcceleratorEntry *entry )
231 {
232 InsertMenuItemTextWithCFString( menu,
233 CFSTR("A"), item, 0, 0);
234
235 // don't attempt to interpret metacharacters like a '-' at the beginning (would become a separator otherwise)
236 ChangeMenuItemAttributes( menu , item+1, kMenuItemAttrIgnoreMeta , 0 ) ;
237 UMASetMenuItemText(menu, item+1 , title , encoding );
238 UMASetMenuItemShortcut( menu , item+1 , entry ) ;
239 }
240
241 static OSStatus UMAGetHelpMenu(
242 MenuRef * outHelpMenu,
243 MenuItemIndex * outFirstCustomItemIndex,
244 bool allowHelpMenuCreation);
245
246 static OSStatus UMAGetHelpMenu(
247 MenuRef * outHelpMenu,
248 MenuItemIndex * outFirstCustomItemIndex,
249 bool allowHelpMenuCreation)
250 {
251 static bool s_createdHelpMenu = false ;
252
253 if ( !s_createdHelpMenu && !allowHelpMenuCreation )
254 {
255 return paramErr ;
256 }
257
258 OSStatus status = HMGetHelpMenu( outHelpMenu , outFirstCustomItemIndex ) ;
259 s_createdHelpMenu = ( status == noErr ) ;
260 return status ;
261 }
262
263 OSStatus UMAGetHelpMenu(
264 MenuRef * outHelpMenu,
265 MenuItemIndex * outFirstCustomItemIndex)
266 {
267 return UMAGetHelpMenu( outHelpMenu , outFirstCustomItemIndex , true );
268 }
269
270 OSStatus UMAGetHelpMenuDontCreate(
271 MenuRef * outHelpMenu,
272 MenuItemIndex * outFirstCustomItemIndex)
273 {
274 return UMAGetHelpMenu( outHelpMenu , outFirstCustomItemIndex , false );
275 }
276
277 #endif
278
279 #endif // wxUSE_GUI