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