]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/uma.cpp
cleanup mac
[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 // since we have decided that we only support 8.6 upwards we are
24 // checking for these minimum requirements in the startup code of
25 // the application so all wxWidgets code can safely assume that appearance 1.1
26 // windows manager, control manager, navigation services etc. are
27 // present
28
29 static SInt32 sUMASystemVersion = 0 ;
30
31 long UMAGetSystemVersion() { return sUMASystemVersion ; }
32
33 void UMACleanupToolbox()
34 {
35 }
36
37 void UMAInitToolbox( UInt16 WXUNUSED(inMoreMastersCalls),
38 bool WXUNUSED(isEmbedded) )
39 {
40 ::InitCursor();
41
42 if ( Gestalt(gestaltSystemVersion, &sUMASystemVersion) != noErr)
43 sUMASystemVersion = 0x0000 ;
44
45 #ifndef __LP64__
46 {
47 FontFamilyID fontId ;
48 Str255 fontName ;
49 SInt16 fontSize ;
50 Style fontStyle ;
51
52 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
53 GetFNum( fontName, &fontId );
54
55 TXNMacOSPreferredFontDescription fontDescriptions[] =
56 {
57 { fontId , (fontSize << 16) , kTXNDefaultFontStyle, kTXNSystemDefaultEncoding }
58 } ;
59 int noOfFontDescriptions = sizeof( fontDescriptions ) / sizeof(TXNMacOSPreferredFontDescription) ;
60
61 OptionBits options = 0 ;
62
63 if ( UMAGetSystemVersion() < 0x1000 )
64 options |= kTXNAlwaysUseQuickDrawTextMask ;
65
66 TXNInitTextension( fontDescriptions, noOfFontDescriptions, options );
67 }
68 #endif
69
70 UMASetSystemIsInitialized( true );
71 }
72
73 // process manager
74 long UMAGetProcessMode()
75 {
76 OSErr err ;
77 ProcessInfoRec processinfo;
78 ProcessSerialNumber procno ;
79
80 procno.highLongOfPSN = 0 ;
81 procno.lowLongOfPSN = kCurrentProcess ;
82 processinfo.processInfoLength = sizeof(ProcessInfoRec);
83 processinfo.processName = NULL;
84 #ifndef __LP64__
85 processinfo.processAppSpec = NULL;
86 #endif
87
88 err = ::GetProcessInformation( &procno , &processinfo ) ;
89 wxASSERT( err == noErr ) ;
90
91 return processinfo.processMode ;
92 }
93
94 bool UMAGetProcessModeDoesActivateOnFGSwitch()
95 {
96 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch ;
97 }
98
99 // menu manager
100
101 MenuRef UMANewMenu( SInt16 id , const wxString& title , wxFontEncoding encoding )
102 {
103 wxString str = wxStripMenuCodes( title ) ;
104 MenuRef menu ;
105
106 CreateNewMenu( id , 0 , &menu ) ;
107 SetMenuTitleWithCFString( menu , wxMacCFStringHolder(str , encoding ) ) ;
108
109 return menu ;
110 }
111
112 void UMASetMenuTitle( MenuRef menu , const wxString& title , wxFontEncoding encoding )
113 {
114 wxString str = wxStripMenuCodes( title ) ;
115
116 SetMenuTitleWithCFString( menu , wxMacCFStringHolder(str , encoding) ) ;
117 }
118
119 void UMASetMenuItemText( MenuRef menu, MenuItemIndex item, const wxString& title, wxFontEncoding encoding )
120 {
121 // we don't strip the accels here anymore, must be done before
122 wxString str = title ;
123
124 SetMenuItemTextWithCFString( menu , item , wxMacCFStringHolder(str , encoding) ) ;
125 }
126
127 UInt32 UMAMenuEvent( EventRecord *inEvent )
128 {
129 return MenuEvent( inEvent ) ;
130 }
131
132 void UMAEnableMenuItem( MenuRef inMenu , MenuItemIndex inItem , bool enable)
133 {
134 if ( enable )
135 EnableMenuItem( inMenu , inItem ) ;
136 else
137 DisableMenuItem( inMenu , inItem ) ;
138 }
139
140 void UMAAppendSubMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , SInt16 id )
141 {
142 AppendMenuItemTextWithCFString( menu,
143 CFSTR("A"), 0, 0,NULL);
144 UMASetMenuItemText( menu, (SInt16) ::CountMenuItems(menu), title , encoding );
145 SetMenuItemHierarchicalID( menu , CountMenuItems( menu ) , id ) ;
146 }
147
148 void UMAInsertSubMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , MenuItemIndex item , SInt16 id )
149 {
150 InsertMenuItemTextWithCFString( menu,
151 CFSTR("A"), item, 0, 0);
152
153 UMASetMenuItemText( menu, item+1, title , encoding );
154 SetMenuItemHierarchicalID( menu , item+1 , id ) ;
155 }
156
157 void UMASetMenuItemShortcut( MenuRef menu , MenuItemIndex item , wxAcceleratorEntry *entry )
158 {
159 if ( !entry )
160 return ;
161
162 UInt8 modifiers = 0 ;
163 SInt16 key = entry->GetKeyCode() ;
164 if ( key )
165 {
166 bool explicitCommandKey = (entry->GetFlags() & wxACCEL_CTRL);
167
168 if (entry->GetFlags() & wxACCEL_ALT)
169 modifiers |= kMenuOptionModifier ;
170
171 if (entry->GetFlags() & wxACCEL_SHIFT)
172 modifiers |= kMenuShiftModifier ;
173
174 SInt16 glyph = 0 ;
175 SInt16 macKey = key ;
176 if ( key >= WXK_F1 && key <= WXK_F15 )
177 {
178 if ( !explicitCommandKey )
179 modifiers |= kMenuNoCommandModifier ;
180
181 // for some reasons this must be 0 right now
182 // everything else leads to just the first function key item
183 // to be selected. Thanks to Ryan Wilcox for finding out.
184 macKey = 0 ;
185 glyph = kMenuF1Glyph + ( key - WXK_F1 ) ;
186 if ( key >= WXK_F13 )
187 glyph += 12 ;
188 }
189 else
190 {
191 switch ( key )
192 {
193 case WXK_BACK :
194 macKey = kBackspaceCharCode ;
195 glyph = kMenuDeleteLeftGlyph ;
196 break ;
197
198 case WXK_TAB :
199 macKey = kTabCharCode ;
200 glyph = kMenuTabRightGlyph ;
201 break ;
202
203 case kEnterCharCode :
204 macKey = kEnterCharCode ;
205 glyph = kMenuEnterGlyph ;
206 break ;
207
208 case WXK_RETURN :
209 macKey = kReturnCharCode ;
210 glyph = kMenuReturnGlyph ;
211 break ;
212
213 case WXK_ESCAPE :
214 macKey = kEscapeCharCode ;
215 glyph = kMenuEscapeGlyph ;
216 break ;
217
218 case WXK_SPACE :
219 macKey = ' ' ;
220 glyph = kMenuSpaceGlyph ;
221 break ;
222
223 case WXK_DELETE :
224 macKey = kDeleteCharCode ;
225 glyph = kMenuDeleteRightGlyph ;
226 break ;
227
228 case WXK_CLEAR :
229 macKey = kClearCharCode ;
230 glyph = kMenuClearGlyph ;
231 break ;
232
233 case WXK_PAGEUP :
234 macKey = kPageUpCharCode ;
235 glyph = kMenuPageUpGlyph ;
236 break ;
237
238 case WXK_PAGEDOWN :
239 macKey = kPageDownCharCode ;
240 glyph = kMenuPageDownGlyph ;
241 break ;
242
243 case WXK_LEFT :
244 macKey = kLeftArrowCharCode ;
245 glyph = kMenuLeftArrowGlyph ;
246 break ;
247
248 case WXK_UP :
249 macKey = kUpArrowCharCode ;
250 glyph = kMenuUpArrowGlyph ;
251 break ;
252
253 case WXK_RIGHT :
254 macKey = kRightArrowCharCode ;
255 glyph = kMenuRightArrowGlyph ;
256 break ;
257
258 case WXK_DOWN :
259 macKey = kDownArrowCharCode ;
260 glyph = kMenuDownArrowGlyph ;
261 break ;
262
263 case WXK_HOME :
264 macKey = kHomeCharCode ;
265 glyph = kMenuNorthwestArrowGlyph ;
266 break ;
267
268 case WXK_END :
269 macKey = kEndCharCode ;
270 glyph = kMenuSoutheastArrowGlyph ;
271 break ;
272 default :
273 macKey = toupper( key ) ;
274 break ;
275 }
276
277 // we now allow non command key shortcuts
278 // remove in case this gives problems
279 if ( !explicitCommandKey )
280 modifiers |= kMenuNoCommandModifier ;
281 }
282
283 // 1d and 1e have special meaning to SetItemCmd, so
284 // do not use for these character codes.
285 if (key != WXK_UP && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_LEFT)
286 SetItemCmd( menu, item , macKey );
287
288 SetMenuItemModifiers( menu, item , modifiers ) ;
289
290 if ( glyph )
291 SetMenuItemKeyGlyph( menu, item , glyph ) ;
292 }
293 }
294
295 void UMAAppendMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , wxAcceleratorEntry *entry )
296 {
297 AppendMenuItemTextWithCFString( menu,
298 CFSTR("A"), 0, 0,NULL);
299 // don't attempt to interpret metacharacters like a '-' at the beginning (would become a separator otherwise)
300 ChangeMenuItemAttributes( menu , ::CountMenuItems(menu), kMenuItemAttrIgnoreMeta , 0 ) ;
301 UMASetMenuItemText(menu, (SInt16) ::CountMenuItems(menu), title , encoding );
302 UMASetMenuItemShortcut( menu , (SInt16) ::CountMenuItems(menu), entry ) ;
303 }
304
305 void UMAInsertMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , MenuItemIndex item , wxAcceleratorEntry *entry )
306 {
307 InsertMenuItemTextWithCFString( menu,
308 CFSTR("A"), item, 0, 0);
309
310 // don't attempt to interpret metacharacters like a '-' at the beginning (would become a separator otherwise)
311 ChangeMenuItemAttributes( menu , item+1, kMenuItemAttrIgnoreMeta , 0 ) ;
312 UMASetMenuItemText(menu, item+1 , title , encoding );
313 UMASetMenuItemShortcut( menu , item+1 , entry ) ;
314 }
315
316 void UMAShowWatchCursor()
317 {
318 SetThemeCursor(kThemeWatchCursor);
319 }
320
321 void UMAShowArrowCursor()
322 {
323 SetThemeCursor(kThemeArrowCursor);
324 }
325
326 // window manager
327
328 GrafPtr UMAGetWindowPort( WindowRef inWindowRef )
329 {
330 wxASSERT( inWindowRef != NULL ) ;
331
332 return (GrafPtr) GetWindowPort( inWindowRef ) ;
333 }
334
335 void UMADisposeWindow( WindowRef inWindowRef )
336 {
337 wxASSERT( inWindowRef != NULL ) ;
338
339 DisposeWindow( inWindowRef ) ;
340 }
341
342 void UMASetWTitle( WindowRef inWindowRef , const wxString& title , wxFontEncoding encoding )
343 {
344 SetWindowTitleWithCFString( inWindowRef , wxMacCFStringHolder(title , encoding) ) ;
345 }
346
347 // appearance additions
348
349 void UMASetControlTitle( ControlRef inControl , const wxString& title , wxFontEncoding encoding )
350 {
351 SetControlTitleWithCFString( inControl , wxMacCFStringHolder(title , encoding) ) ;
352 }
353
354 void UMAActivateControl( ControlRef inControl )
355 {
356 ::ActivateControl( inControl ) ;
357 }
358
359 void UMAMoveControl( ControlRef inControl , short x , short y )
360 {
361 ::MoveControl( inControl , x , y ) ;
362 }
363
364 void UMASizeControl( ControlRef inControl , short x , short y )
365 {
366 ::SizeControl( inControl , x , y ) ;
367 }
368
369 void UMADeactivateControl( ControlRef inControl )
370 {
371 ::DeactivateControl( inControl ) ;
372 }
373
374 // shows the control and adds the region to the update region
375 void UMAShowControl( ControlRef inControl )
376 {
377 SetControlVisibility( inControl , true , false ) ;
378 HIViewSetNeedsDisplay( inControl, true );
379 }
380
381 // hides the control and adds the region to the update region
382 void UMAHideControl( ControlRef inControl )
383 {
384 SetControlVisibility( inControl , false , false ) ;
385 HIViewSetNeedsDisplay( inControl, true );
386 }
387
388 // keyboard focus
389 OSErr UMASetKeyboardFocus( WindowPtr inWindow,
390 ControlRef inControl,
391 ControlFocusPart inPart )
392 {
393 OSErr err = noErr;
394 #ifndef __LP64__
395 GrafPtr port ;
396
397 GetPort( &port ) ;
398 SetPortWindowPort( inWindow ) ;
399 #endif
400
401 err = SetKeyboardFocus( inWindow , inControl , inPart ) ;
402 #ifndef __LP64__
403 SetPort( port ) ;
404 #endif
405
406 return err ;
407 }
408
409 bool UMAIsWindowFloating( WindowRef inWindow )
410 {
411 WindowClass cl ;
412
413 GetWindowClass( inWindow , &cl ) ;
414 return cl == kFloatingWindowClass ;
415 }
416
417 bool UMAIsWindowModal( WindowRef inWindow )
418 {
419 WindowClass cl ;
420
421 GetWindowClass( inWindow , &cl ) ;
422 return cl < kFloatingWindowClass ;
423 }
424
425 // others
426
427 void UMAHighlightAndActivateWindow( WindowRef inWindowRef , bool inActivate )
428 {
429 if ( inWindowRef )
430 {
431 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
432 // if ( inActivate != isHighlighted )
433 #ifndef __LP64__
434 GrafPtr port ;
435 GetPort( &port ) ;
436 SetPortWindowPort( inWindowRef ) ;
437 #endif
438 HiliteWindow( inWindowRef , inActivate ) ;
439 ControlRef control = NULL ;
440 ::GetRootControl( inWindowRef , &control ) ;
441 if ( control )
442 {
443 if ( inActivate )
444 UMAActivateControl( control ) ;
445 else
446 UMADeactivateControl( control ) ;
447 }
448 #ifndef __LP64__
449 SetPort( port ) ;
450 #endif
451 }
452 }
453
454 OSStatus UMADrawThemePlacard( const Rect *inRect , ThemeDrawState inState )
455 {
456 #ifndef __LP64__
457 return ::DrawThemePlacard( inRect , inState ) ;
458 #else
459 return noErr;
460 #endif
461 }
462
463 static OSStatus UMAGetHelpMenu(
464 MenuRef * outHelpMenu,
465 MenuItemIndex * outFirstCustomItemIndex,
466 bool allowHelpMenuCreation);
467
468 static OSStatus UMAGetHelpMenu(
469 MenuRef * outHelpMenu,
470 MenuItemIndex * outFirstCustomItemIndex,
471 bool allowHelpMenuCreation)
472 {
473 static bool s_createdHelpMenu = false ;
474
475 if ( !s_createdHelpMenu && !allowHelpMenuCreation )
476 {
477 return paramErr ;
478 }
479
480 OSStatus status = HMGetHelpMenu( outHelpMenu , outFirstCustomItemIndex ) ;
481 s_createdHelpMenu = ( status == noErr ) ;
482 return status ;
483 }
484
485 OSStatus UMAGetHelpMenu(
486 MenuRef * outHelpMenu,
487 MenuItemIndex * outFirstCustomItemIndex)
488 {
489 return UMAGetHelpMenu( outHelpMenu , outFirstCustomItemIndex , true );
490 }
491
492 OSStatus UMAGetHelpMenuDontCreate(
493 MenuRef * outHelpMenu,
494 MenuItemIndex * outFirstCustomItemIndex)
495 {
496 return UMAGetHelpMenu( outHelpMenu , outFirstCustomItemIndex , false );
497 }
498
499 #ifndef __LP64__
500
501 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport )
502 {
503 m_clip = NULL ;
504 Setup( newport ) ;
505 }
506
507 wxMacPortStateHelper::wxMacPortStateHelper()
508 {
509 m_clip = NULL ;
510 }
511
512 void wxMacPortStateHelper::Setup( GrafPtr newport )
513 {
514 GetPort( &m_oldPort ) ;
515 SetPort( newport ) ;
516 SetOrigin(0, 0);
517
518 wxASSERT_MSG( m_clip == NULL , wxT("Cannot call setup twice") ) ;
519 m_clip = NewRgn() ;
520 GetClip( m_clip );
521 m_textFont = GetPortTextFont( (CGrafPtr) newport );
522 m_textSize = GetPortTextSize( (CGrafPtr) newport );
523 m_textStyle = GetPortTextFace( (CGrafPtr) newport );
524 m_textMode = GetPortTextMode( (CGrafPtr) newport );
525 GetThemeDrawingState( &m_drawingState ) ;
526 m_currentPort = newport ;
527 }
528
529 void wxMacPortStateHelper::Clear()
530 {
531 if ( m_clip )
532 {
533 DisposeRgn( m_clip ) ;
534 DisposeThemeDrawingState( m_drawingState ) ;
535 m_clip = NULL ;
536 }
537 }
538
539 wxMacPortStateHelper::~wxMacPortStateHelper()
540 {
541 if ( m_clip )
542 {
543 SetPort( m_currentPort ) ;
544 SetClip( m_clip ) ;
545 DisposeRgn( m_clip ) ;
546 TextFont( m_textFont );
547 TextSize( m_textSize );
548 TextFace( m_textStyle );
549 TextMode( m_textMode );
550 SetThemeDrawingState( m_drawingState , true ) ;
551 SetPort( m_oldPort ) ;
552 }
553 }
554
555 #endif
556
557 Rect * UMAGetControlBoundsInWindowCoords( ControlRef theControl, Rect *bounds )
558 {
559 GetControlBounds( theControl , bounds ) ;
560
561 #if TARGET_API_MAC_OSX
562 WindowRef tlwref = GetControlOwner( theControl ) ;
563
564 wxTopLevelWindowMac* tlwwx = wxFindWinFromMacWindow( tlwref ) ;
565 if ( tlwwx != NULL )
566 {
567 ControlRef rootControl = tlwwx->GetPeer()->GetControlRef() ;
568 HIPoint hiPoint = CGPointMake( 0 , 0 ) ;
569 HIViewConvertPoint( &hiPoint , HIViewGetSuperview(theControl) , rootControl ) ;
570 OffsetRect( bounds , (short) hiPoint.x , (short) hiPoint.y ) ;
571 }
572 #endif
573
574 return bounds ;
575 }
576
577 size_t UMAPutBytesCFRefCallback( void *info, const void *bytes, size_t count )
578 {
579 CFMutableDataRef data = (CFMutableDataRef) info;
580 if ( data )
581 {
582 CFDataAppendBytes( data, (const UInt8*) bytes, count );
583 }
584 return count;
585 }
586
587 void UMAReleaseCFDataProviderCallback( void *info, const void *data, size_t count )
588 {
589 if ( info )
590 CFRelease( (CFDataRef) info );
591 }
592
593 void UMAReleaseCFDataConsumerCallback( void *info )
594 {
595 if ( info )
596 CFRelease( (CFDataRef) info );
597 }
598
599 CGDataProviderRef UMACGDataProviderCreateWithCFData( CFDataRef data )
600 {
601 if ( data == NULL )
602 return NULL;
603
604 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
605 if( &CGDataProviderCreateWithCFData != NULL )
606 {
607 return CGDataProviderCreateWithCFData( data );
608 }
609 #endif
610
611 // make sure we keep it until done
612 CFRetain( data );
613 CGDataProviderRef provider = CGDataProviderCreateWithData( (void*) data , CFDataGetBytePtr( data ) ,
614 CFDataGetLength( data ), UMAReleaseCFDataProviderCallback );
615 // if provider couldn't be created, release the data again
616 if ( provider == NULL )
617 CFRelease( data );
618 return provider;
619 }
620
621 CGDataConsumerRef UMACGDataConsumerCreateWithCFData( CFMutableDataRef data )
622 {
623 if ( data == NULL )
624 return NULL;
625
626 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
627 if( &CGDataConsumerCreateWithCFData != NULL )
628 {
629 return CGDataConsumerCreateWithCFData( data );
630 }
631 #endif
632 // make sure we keep it until done
633 CFRetain( data );
634 CGDataConsumerCallbacks callbacks;
635 callbacks.putBytes = UMAPutBytesCFRefCallback;
636 callbacks.releaseConsumer = UMAReleaseCFDataConsumerCallback;
637 CGDataConsumerRef consumer = CGDataConsumerCreate( data , &callbacks );
638 // if consumer couldn't be created, release the data again
639 if ( consumer == NULL )
640 CFRelease( data );
641 return consumer;
642 }
643 #endif // wxUSE_GUI
644
645 #if wxUSE_BASE
646
647 static bool sUMASystemInitialized = false ;
648
649 bool UMASystemIsInitialized()
650 {
651 return sUMASystemInitialized ;
652 }
653
654 void UMASetSystemIsInitialized(bool val)
655 {
656 sUMASystemInitialized = val;
657 }
658
659 #endif // wxUSE_BASE