]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/uma.cpp
7fcd7abee38414194824ea570bebc76ba6d4a93e
[wxWidgets.git] / src / mac / carbon / uma.cpp
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
14 #include "wx/mac/uma.h"
15
16 #if wxUSE_GUI
17
18 #include "wx/toplevel.h"
19 #include "wx/dc.h"
20
21 #include "wx/mac/uma.h"
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
34 void UMAInitToolbox( UInt16 WXUNUSED(inMoreMastersCalls),
35 bool WXUNUSED(isEmbedded) )
36 {
37 #if 0 // ndef __LP64__
38 {
39 FontFamilyID fontId ;
40 Str255 fontName ;
41 SInt16 fontSize ;
42 Style fontStyle ;
43
44 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
45 GetFNum( fontName, &fontId );
46
47 TXNMacOSPreferredFontDescription fontDescriptions[] =
48 {
49 { fontId , (fontSize << 16) , kTXNDefaultFontStyle, kTXNSystemDefaultEncoding }
50 } ;
51 int noOfFontDescriptions = sizeof( fontDescriptions ) / sizeof(TXNMacOSPreferredFontDescription) ;
52
53 OptionBits options = 0 ;
54
55 TXNInitTextension( fontDescriptions, noOfFontDescriptions, options );
56 }
57 #endif
58 }
59
60 // process manager
61 long UMAGetProcessMode()
62 {
63 OSErr err ;
64 ProcessInfoRec processinfo;
65 ProcessSerialNumber procno ;
66
67 procno.highLongOfPSN = 0 ;
68 procno.lowLongOfPSN = kCurrentProcess ;
69 processinfo.processInfoLength = sizeof(ProcessInfoRec);
70 processinfo.processName = NULL;
71 #ifndef __LP64__
72 processinfo.processAppSpec = NULL;
73 #endif
74
75 err = ::GetProcessInformation( &procno , &processinfo ) ;
76 wxASSERT( err == noErr ) ;
77
78 return processinfo.processMode ;
79 }
80
81 bool UMAGetProcessModeDoesActivateOnFGSwitch()
82 {
83 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch ;
84 }
85
86 // menu manager
87
88 #if wxMAC_USE_COCOA == 0
89
90 MenuRef UMANewMenu( SInt16 id , const wxString& title , wxFontEncoding encoding )
91 {
92 wxString str = wxStripMenuCodes( title ) ;
93 MenuRef menu ;
94
95 CreateNewMenu( id , 0 , &menu ) ;
96 SetMenuTitleWithCFString( menu , wxMacCFStringHolder(str , encoding ) ) ;
97
98 return menu ;
99 }
100
101 void UMASetMenuTitle( MenuRef menu , const wxString& title , wxFontEncoding encoding )
102 {
103 wxString str = wxStripMenuCodes( title ) ;
104
105 SetMenuTitleWithCFString( menu , wxMacCFStringHolder(str , encoding) ) ;
106 }
107
108 void UMASetMenuItemText( MenuRef menu, MenuItemIndex item, const wxString& title, wxFontEncoding encoding )
109 {
110 // we don't strip the accels here anymore, must be done before
111 wxString str = title ;
112
113 SetMenuItemTextWithCFString( menu , item , wxMacCFStringHolder(str , encoding) ) ;
114 }
115
116 void UMAEnableMenuItem( MenuRef inMenu , MenuItemIndex inItem , bool enable)
117 {
118 if ( enable )
119 EnableMenuItem( inMenu , inItem ) ;
120 else
121 DisableMenuItem( inMenu , inItem ) ;
122 }
123
124 void UMAAppendSubMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , SInt16 id )
125 {
126 AppendMenuItemTextWithCFString( menu,
127 CFSTR("A"), 0, 0,NULL);
128 UMASetMenuItemText( menu, (SInt16) ::CountMenuItems(menu), title , encoding );
129 SetMenuItemHierarchicalID( menu , CountMenuItems( menu ) , id ) ;
130 }
131
132 void UMAInsertSubMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , MenuItemIndex item , SInt16 id )
133 {
134 InsertMenuItemTextWithCFString( menu,
135 CFSTR("A"), item, 0, 0);
136
137 UMASetMenuItemText( menu, item+1, title , encoding );
138 SetMenuItemHierarchicalID( menu , item+1 , id ) ;
139 }
140
141 void UMASetMenuItemShortcut( MenuRef menu , MenuItemIndex item , wxAcceleratorEntry *entry )
142 {
143 if ( !entry )
144 return ;
145
146 UInt8 modifiers = 0 ;
147 SInt16 key = entry->GetKeyCode() ;
148 if ( key )
149 {
150 bool explicitCommandKey = (entry->GetFlags() & wxACCEL_CTRL);
151
152 if (entry->GetFlags() & wxACCEL_ALT)
153 modifiers |= kMenuOptionModifier ;
154
155 if (entry->GetFlags() & wxACCEL_SHIFT)
156 modifiers |= kMenuShiftModifier ;
157
158 SInt16 glyph = 0 ;
159 SInt16 macKey = key ;
160 if ( key >= WXK_F1 && key <= WXK_F15 )
161 {
162 if ( !explicitCommandKey )
163 modifiers |= kMenuNoCommandModifier ;
164
165 // for some reasons this must be 0 right now
166 // everything else leads to just the first function key item
167 // to be selected. Thanks to Ryan Wilcox for finding out.
168 macKey = 0 ;
169 glyph = kMenuF1Glyph + ( key - WXK_F1 ) ;
170 if ( key >= WXK_F13 )
171 glyph += 12 ;
172 }
173 else
174 {
175 switch ( key )
176 {
177 case WXK_BACK :
178 macKey = kBackspaceCharCode ;
179 glyph = kMenuDeleteLeftGlyph ;
180 break ;
181
182 case WXK_TAB :
183 macKey = kTabCharCode ;
184 glyph = kMenuTabRightGlyph ;
185 break ;
186
187 case kEnterCharCode :
188 macKey = kEnterCharCode ;
189 glyph = kMenuEnterGlyph ;
190 break ;
191
192 case WXK_RETURN :
193 macKey = kReturnCharCode ;
194 glyph = kMenuReturnGlyph ;
195 break ;
196
197 case WXK_ESCAPE :
198 macKey = kEscapeCharCode ;
199 glyph = kMenuEscapeGlyph ;
200 break ;
201
202 case WXK_SPACE :
203 macKey = ' ' ;
204 glyph = kMenuSpaceGlyph ;
205 break ;
206
207 case WXK_DELETE :
208 macKey = kDeleteCharCode ;
209 glyph = kMenuDeleteRightGlyph ;
210 break ;
211
212 case WXK_CLEAR :
213 macKey = kClearCharCode ;
214 glyph = kMenuClearGlyph ;
215 break ;
216
217 case WXK_PAGEUP :
218 macKey = kPageUpCharCode ;
219 glyph = kMenuPageUpGlyph ;
220 break ;
221
222 case WXK_PAGEDOWN :
223 macKey = kPageDownCharCode ;
224 glyph = kMenuPageDownGlyph ;
225 break ;
226
227 case WXK_LEFT :
228 macKey = kLeftArrowCharCode ;
229 glyph = kMenuLeftArrowGlyph ;
230 break ;
231
232 case WXK_UP :
233 macKey = kUpArrowCharCode ;
234 glyph = kMenuUpArrowGlyph ;
235 break ;
236
237 case WXK_RIGHT :
238 macKey = kRightArrowCharCode ;
239 glyph = kMenuRightArrowGlyph ;
240 break ;
241
242 case WXK_DOWN :
243 macKey = kDownArrowCharCode ;
244 glyph = kMenuDownArrowGlyph ;
245 break ;
246
247 case WXK_HOME :
248 macKey = kHomeCharCode ;
249 glyph = kMenuNorthwestArrowGlyph ;
250 break ;
251
252 case WXK_END :
253 macKey = kEndCharCode ;
254 glyph = kMenuSoutheastArrowGlyph ;
255 break ;
256 default :
257 macKey = toupper( key ) ;
258 break ;
259 }
260
261 // we now allow non command key shortcuts
262 // remove in case this gives problems
263 if ( !explicitCommandKey )
264 modifiers |= kMenuNoCommandModifier ;
265 }
266
267 // 1d and 1e have special meaning to SetItemCmd, so
268 // do not use for these character codes.
269 if (key != WXK_UP && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_LEFT)
270 SetItemCmd( menu, item , macKey );
271
272 SetMenuItemModifiers( menu, item , modifiers ) ;
273
274 if ( glyph )
275 SetMenuItemKeyGlyph( menu, item , glyph ) ;
276 }
277 }
278
279 void UMAAppendMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , wxAcceleratorEntry *entry )
280 {
281 AppendMenuItemTextWithCFString( menu,
282 CFSTR("A"), 0, 0,NULL);
283 // don't attempt to interpret metacharacters like a '-' at the beginning (would become a separator otherwise)
284 ChangeMenuItemAttributes( menu , ::CountMenuItems(menu), kMenuItemAttrIgnoreMeta , 0 ) ;
285 UMASetMenuItemText(menu, (SInt16) ::CountMenuItems(menu), title , encoding );
286 UMASetMenuItemShortcut( menu , (SInt16) ::CountMenuItems(menu), entry ) ;
287 }
288
289 void UMAInsertMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , MenuItemIndex item , wxAcceleratorEntry *entry )
290 {
291 InsertMenuItemTextWithCFString( menu,
292 CFSTR("A"), item, 0, 0);
293
294 // don't attempt to interpret metacharacters like a '-' at the beginning (would become a separator otherwise)
295 ChangeMenuItemAttributes( menu , item+1, kMenuItemAttrIgnoreMeta , 0 ) ;
296 UMASetMenuItemText(menu, item+1 , title , encoding );
297 UMASetMenuItemShortcut( menu , item+1 , entry ) ;
298 }
299
300 #endif
301
302 #if wxMAC_USE_COCOA == 0
303
304 static OSStatus UMAGetHelpMenu(
305 MenuRef * outHelpMenu,
306 MenuItemIndex * outFirstCustomItemIndex,
307 bool allowHelpMenuCreation);
308
309 static OSStatus UMAGetHelpMenu(
310 MenuRef * outHelpMenu,
311 MenuItemIndex * outFirstCustomItemIndex,
312 bool allowHelpMenuCreation)
313 {
314 static bool s_createdHelpMenu = false ;
315
316 if ( !s_createdHelpMenu && !allowHelpMenuCreation )
317 {
318 return paramErr ;
319 }
320
321 OSStatus status = HMGetHelpMenu( outHelpMenu , outFirstCustomItemIndex ) ;
322 s_createdHelpMenu = ( status == noErr ) ;
323 return status ;
324 }
325
326 OSStatus UMAGetHelpMenu(
327 MenuRef * outHelpMenu,
328 MenuItemIndex * outFirstCustomItemIndex)
329 {
330 return UMAGetHelpMenu( outHelpMenu , outFirstCustomItemIndex , true );
331 }
332
333 OSStatus UMAGetHelpMenuDontCreate(
334 MenuRef * outHelpMenu,
335 MenuItemIndex * outFirstCustomItemIndex)
336 {
337 return UMAGetHelpMenu( outHelpMenu , outFirstCustomItemIndex , false );
338 }
339
340 #endif
341
342 // window manager
343
344 #if wxMAC_USE_QUICKDRAW
345
346 void UMAActivateControl( ControlRef inControl )
347 {
348 ::ActivateControl( inControl ) ;
349 }
350
351 void UMADeactivateControl( ControlRef inControl )
352 {
353 ::DeactivateControl( inControl ) ;
354 }
355
356 // others
357
358 void UMAHighlightAndActivateWindow( WindowRef inWindowRef , bool inActivate )
359 {
360 #if 1 // TODO REMOVE
361 if ( inWindowRef )
362 {
363 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
364 // if ( inActivate != isHighlighted )
365 #ifndef __LP64__
366 GrafPtr port ;
367 GetPort( &port ) ;
368 SetPortWindowPort( inWindowRef ) ;
369 #endif
370 HiliteWindow( inWindowRef , inActivate ) ;
371 ControlRef control = NULL ;
372 ::GetRootControl( inWindowRef , &control ) ;
373 if ( control )
374 {
375 if ( inActivate )
376 UMAActivateControl( control ) ;
377 else
378 UMADeactivateControl( control ) ;
379 }
380 #ifndef __LP64__
381 SetPort( port ) ;
382 #endif
383 }
384 #endif
385 }
386
387 Rect * UMAGetControlBoundsInWindowCoords( ControlRef theControl, Rect *bounds )
388 {
389 GetControlBounds( theControl , bounds ) ;
390
391 WindowRef tlwref = GetControlOwner( theControl ) ;
392
393 wxTopLevelWindowMac* tlwwx = wxFindWinFromMacWindow( tlwref ) ;
394 if ( tlwwx != NULL )
395 {
396 ControlRef rootControl = tlwwx->GetPeer()->GetControlRef() ;
397 HIPoint hiPoint = CGPointMake( 0 , 0 ) ;
398 HIViewConvertPoint( &hiPoint , HIViewGetSuperview(theControl) , rootControl ) ;
399 OffsetRect( bounds , (short) hiPoint.x , (short) hiPoint.y ) ;
400 }
401
402 return bounds ;
403 }
404
405 #endif
406
407 size_t UMAPutBytesCFRefCallback( void *info, const void *bytes, size_t count )
408 {
409 CFMutableDataRef data = (CFMutableDataRef) info;
410 if ( data )
411 {
412 CFDataAppendBytes( data, (const UInt8*) bytes, count );
413 }
414 return count;
415 }
416
417 void UMAReleaseCFDataProviderCallback(void *info,
418 const void *WXUNUSED(data),
419 size_t WXUNUSED(count))
420 {
421 if ( info )
422 CFRelease( (CFDataRef) info );
423 }
424
425 void UMAReleaseCFDataConsumerCallback( void *info )
426 {
427 if ( info )
428 CFRelease( (CFDataRef) info );
429 }
430
431 CGDataProviderRef UMACGDataProviderCreateWithCFData( CFDataRef data )
432 {
433 if ( data == NULL )
434 return NULL;
435
436 return CGDataProviderCreateWithCFData( data );
437 }
438
439 CGDataConsumerRef UMACGDataConsumerCreateWithCFData( CFMutableDataRef data )
440 {
441 if ( data == NULL )
442 return NULL;
443
444 return CGDataConsumerCreateWithCFData( data );
445 }
446 #endif // wxUSE_GUI