1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/uma.cpp
3 // Purpose: UMA support
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: The wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/toplevel.h"
20 # include <MacTextEditor.h>
21 # include <Navigation.h>
22 # if defined(TARGET_CARBON)
23 # if PM_USE_SESSION_APIS
26 # include <PMApplication.h>
28 # include <Printing.h>
36 #include "wx/mac/uma.h"
38 // since we have decided that we only support 8.6 upwards we are
39 // checking for these minimum requirements in the startup code of
40 // the application so all wxWidgets code can safely assume that appearance 1.1
41 // windows manager, control manager, navigation services etc. are
44 static bool sUMAHasAppearance
= false ;
45 static long sUMAAppearanceVersion
= 0 ;
46 static long sUMASystemVersion
= 0 ;
47 static bool sUMAHasAquaLayout
= false ;
49 static bool sUMAHasInittedAppearance
= false;
51 extern int gAGABackgroundColor
;
53 bool UMAHasAppearance() { return sUMAHasAppearance
; }
54 long UMAGetAppearanceVersion() { return sUMAAppearanceVersion
; }
55 long UMAGetSystemVersion() { return sUMASystemVersion
; }
57 static bool sUMAHasWindowManager
= false ;
58 static long sUMAWindowManagerAttr
= 0 ;
60 bool UMAHasWindowManager() { return sUMAHasWindowManager
; }
61 long UMAGetWindowManagerAttr() { return sUMAWindowManagerAttr
; }
62 bool UMAHasAquaLayout() { return sUMAHasAquaLayout
; }
65 void UMACleanupToolbox()
67 if (sUMAHasInittedAppearance
)
68 UnregisterAppearanceClient() ;
70 if ( NavServicesAvailable() )
73 if ( TXNTerminateTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
74 TXNTerminateTextension( ) ;
77 void UMAInitToolbox( UInt16 inMoreMastersCalls
, bool isEmbedded
)
81 for (long i
= 1; i
<= inMoreMastersCalls
; i
++)
86 ::InitGraf(&qd
.thePort
);
91 ::FlushEvents(everyEvent
, 0);
95 PurgeSpace(&total
, &contig
);
100 if ( Gestalt(gestaltSystemVersion
, &sUMASystemVersion
) != noErr
)
101 sUMASystemVersion
= 0x0000 ;
104 if ( Gestalt( gestaltAppearanceAttr
, &theAppearance
) == noErr
)
106 // If status equals appearanceProcessRegisteredErr it means the
107 // appearance client already was registered (For example if we run
108 // embedded, the host might have registered it). In such a case
109 // we don't unregister it later on.
111 sUMAHasAppearance
= true ;
112 OSStatus status
= RegisterAppearanceClient();
113 if (status
!= appearanceProcessRegisteredErr
)
115 // Appearance client wasn't registered yet.
116 sUMAHasInittedAppearance
= true;
119 if ( Gestalt( gestaltAppearanceVersion
, &theAppearance
) == noErr
)
120 sUMAAppearanceVersion
= theAppearance
;
122 sUMAAppearanceVersion
= 0x0100 ;
125 if ( Gestalt( gestaltWindowMgrAttr
, &sUMAWindowManagerAttr
) == noErr
)
126 sUMAHasWindowManager
= sUMAWindowManagerAttr
& gestaltWindowMgrPresent
;
129 // Call currently implicitely done : InitFloatingWindows() ;
133 if ( sUMAHasWindowManager
)
134 InitFloatingWindows() ;
140 if ( NavServicesAvailable() )
144 Gestalt( gestaltMenuMgrAttr
, &menuMgrAttr
) ;
145 if ( menuMgrAttr
& gestaltMenuMgrAquaLayoutMask
)
146 sUMAHasAquaLayout
= true ;
148 if ( TXNInitTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
150 FontFamilyID fontId
;
155 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
156 GetFNum( fontName
, &fontId
);
158 TXNMacOSPreferredFontDescription fontDescriptions
[] =
160 { fontId
, (fontSize
<< 16) , kTXNDefaultFontStyle
, kTXNSystemDefaultEncoding
}
162 int noOfFontDescriptions
= sizeof( fontDescriptions
) / sizeof(TXNMacOSPreferredFontDescription
) ;
164 OptionBits options
= 0 ;
166 if ( UMAGetSystemVersion() < 0x1000 )
167 options
|= kTXNAlwaysUseQuickDrawTextMask
;
169 TXNInitTextension( fontDescriptions
, noOfFontDescriptions
, options
);
172 UMASetSystemIsInitialized( true );
176 Boolean
CanUseATSUI()
179 OSErr err
= Gestalt(gestaltATSUVersion
, &result
);
180 return (err
== noErr
);
185 long UMAGetProcessMode()
188 ProcessInfoRec processinfo
;
189 ProcessSerialNumber procno
;
191 procno
.highLongOfPSN
= 0 ;
192 procno
.lowLongOfPSN
= kCurrentProcess
;
193 processinfo
.processInfoLength
= sizeof(ProcessInfoRec
);
194 processinfo
.processName
= NULL
;
195 processinfo
.processAppSpec
= NULL
;
197 err
= ::GetProcessInformation( &procno
, &processinfo
) ;
198 wxASSERT( err
== noErr
) ;
200 return processinfo
.processMode
;
203 bool UMAGetProcessModeDoesActivateOnFGSwitch()
205 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch
;
210 MenuRef
UMANewMenu( SInt16 id
, const wxString
& title
, wxFontEncoding encoding
)
212 wxString str
= wxStripMenuCodes( title
) ;
216 CreateNewMenu( id
, 0 , &menu
) ;
217 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
, encoding
) ) ;
220 wxMacStringToPascal( str
, ptitle
) ;
221 menu
= ::NewMenu( id
, ptitle
) ;
227 void UMASetMenuTitle( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
)
229 wxString str
= wxStripMenuCodes( title
) ;
232 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
, encoding
) ) ;
236 wxMacStringToPascal( str
, ptitle
) ;
237 SetMenuTitle( menu
, ptitle
) ;
241 void UMASetMenuItemText( MenuRef menu
, MenuItemIndex item
, const wxString
& title
, wxFontEncoding encoding
)
243 // we don't strip the accels here anymore, must be done before
244 wxString str
= title
;
247 SetMenuItemTextWithCFString( menu
, item
, wxMacCFStringHolder(str
, encoding
) ) ;
251 wxMacStringToPascal( str
, ptitle
) ;
252 SetMenuItemText( menu
, item
, ptitle
) ;
256 UInt32
UMAMenuEvent( EventRecord
*inEvent
)
258 return MenuEvent( inEvent
) ;
261 void UMAEnableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
, bool enable
)
264 EnableMenuItem( inMenu
, inItem
) ;
266 DisableMenuItem( inMenu
, inItem
) ;
269 void UMAAppendSubMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, SInt16 id
)
271 MacAppendMenu( menu
, "\pA" );
272 UMASetMenuItemText( menu
, (SInt16
) ::CountMenuItems(menu
), title
, encoding
);
273 SetMenuItemHierarchicalID( menu
, CountMenuItems( menu
) , id
) ;
276 void UMAInsertSubMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, MenuItemIndex item
, SInt16 id
)
278 MacInsertMenuItem( menu
, "\pA" , item
);
279 UMASetMenuItemText( menu
, item
+1, title
, encoding
);
280 SetMenuItemHierarchicalID( menu
, item
+1 , id
) ;
283 void UMASetMenuItemShortcut( MenuRef menu
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
288 UInt8 modifiers
= 0 ;
289 SInt16 key
= entry
->GetKeyCode() ;
292 bool explicitCommandKey
= (entry
->GetFlags() & wxACCEL_CTRL
);
294 if (entry
->GetFlags() & wxACCEL_ALT
)
295 modifiers
|= kMenuOptionModifier
;
297 if (entry
->GetFlags() & wxACCEL_SHIFT
)
298 modifiers
|= kMenuShiftModifier
;
301 SInt16 macKey
= key
;
302 if ( key
>= WXK_F1
&& key
<= WXK_F15
)
304 if ( !explicitCommandKey
)
305 modifiers
|= kMenuNoCommandModifier
;
307 // for some reasons this must be 0 right now
308 // everything else leads to just the first function key item
309 // to be selected. Thanks to Ryan Wilcox for finding out.
311 glyph
= kMenuF1Glyph
+ ( key
- WXK_F1
) ;
312 if ( key
>= WXK_F13
)
320 macKey
= kBackspaceCharCode
;
321 glyph
= kMenuDeleteLeftGlyph
;
325 macKey
= kTabCharCode
;
326 glyph
= kMenuTabRightGlyph
;
329 case kEnterCharCode
:
330 macKey
= kEnterCharCode
;
331 glyph
= kMenuEnterGlyph
;
335 macKey
= kReturnCharCode
;
336 glyph
= kMenuReturnGlyph
;
340 macKey
= kEscapeCharCode
;
341 glyph
= kMenuEscapeGlyph
;
346 glyph
= kMenuSpaceGlyph
;
350 macKey
= kDeleteCharCode
;
351 glyph
= kMenuDeleteRightGlyph
;
355 macKey
= kClearCharCode
;
356 glyph
= kMenuClearGlyph
;
360 macKey
= kPageUpCharCode
;
361 glyph
= kMenuPageUpGlyph
;
365 macKey
= kPageDownCharCode
;
366 glyph
= kMenuPageDownGlyph
;
370 macKey
= kLeftArrowCharCode
;
371 glyph
= kMenuLeftArrowGlyph
;
375 macKey
= kUpArrowCharCode
;
376 glyph
= kMenuUpArrowGlyph
;
380 macKey
= kRightArrowCharCode
;
381 glyph
= kMenuRightArrowGlyph
;
385 macKey
= kDownArrowCharCode
;
386 glyph
= kMenuDownArrowGlyph
;
390 macKey
= kHomeCharCode
;
391 glyph
= kMenuNorthwestArrowGlyph
;
395 macKey
= kEndCharCode
;
396 glyph
= kMenuSoutheastArrowGlyph
;
399 macKey
= toupper( key
) ;
403 // we now allow non command key shortcuts
404 // remove in case this gives problems
405 if ( !explicitCommandKey
)
406 modifiers
|= kMenuNoCommandModifier
;
409 // 1d and 1e have special meaning to SetItemCmd, so
410 // do not use for these character codes.
411 if (key
!= WXK_UP
&& key
!= WXK_RIGHT
&& key
!= WXK_DOWN
&& key
!= WXK_LEFT
)
412 SetItemCmd( menu
, item
, macKey
);
414 SetMenuItemModifiers( menu
, item
, modifiers
) ;
417 SetMenuItemKeyGlyph( menu
, item
, glyph
) ;
421 void UMAAppendMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, wxAcceleratorEntry
*entry
)
423 MacAppendMenu(menu
, "\pA");
425 // don't attempt to interpret metacharacters like a '-' at the beginning (would become a separator otherwise)
426 ChangeMenuItemAttributes( menu
, ::CountMenuItems(menu
), kMenuItemAttrIgnoreMeta
, 0 ) ;
427 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
, encoding
);
428 UMASetMenuItemShortcut( menu
, (SInt16
) ::CountMenuItems(menu
), entry
) ;
431 void UMAInsertMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
433 MacInsertMenuItem( menu
, "\pA" , item
) ;
435 // don't attempt to interpret metacharacters like a '-' at the beginning (would become a separator otherwise)
436 ChangeMenuItemAttributes( menu
, item
+1, kMenuItemAttrIgnoreMeta
, 0 ) ;
437 UMASetMenuItemText(menu
, item
+1 , title
, encoding
);
438 UMASetMenuItemShortcut( menu
, item
+1 , entry
) ;
445 int gPrOpenCounter
= 0 ;
453 if ( gPrOpenCounter
== 1 )
457 wxASSERT( err
== noErr
) ;
463 OSStatus
UMAPrClose()
467 wxASSERT( gPrOpenCounter
>= 1 ) ;
469 if ( gPrOpenCounter
== 1 )
473 wxASSERT( err
== noErr
) ;
481 pascal QDGlobalsPtr
GetQDGlobalsPtr() ;
482 pascal QDGlobalsPtr
GetQDGlobalsPtr()
484 return QDGlobalsPtr (* (Ptr
*) LMGetCurrentA5 ( ) - 0xCA);
489 void UMAShowWatchCursor()
493 CursHandle watchFob
= GetCursor(watchCursor
);
495 if (watchFob
== NULL
)
502 // Cursor preservedArrow;
503 // GetQDGlobalsArrow(&preservedArrow);
504 // SetQDGlobalsArrow(*watchFob);
506 // SetQDGlobalsArrow(&preservedArrow);
507 SetCursor(*watchFob
);
509 SetCursor(*watchFob
);
514 void UMAShowArrowCursor()
518 SetCursor( GetQDGlobalsArrow(&arrow
) );
520 SetCursor (&(qd
.arrow
));
526 GrafPtr
UMAGetWindowPort( WindowRef inWindowRef
)
528 wxASSERT( inWindowRef
!= NULL
) ;
531 return (GrafPtr
) GetWindowPort( inWindowRef
) ;
533 return (GrafPtr
) inWindowRef
;
537 void UMADisposeWindow( WindowRef inWindowRef
)
539 wxASSERT( inWindowRef
!= NULL
) ;
541 DisposeWindow( inWindowRef
) ;
544 void UMASetWTitle( WindowRef inWindowRef
, const wxString
& title
, wxFontEncoding encoding
)
547 SetWindowTitleWithCFString( inWindowRef
, wxMacCFStringHolder(title
, encoding
) ) ;
551 wxMacStringToPascal( title
, ptitle
) ;
552 SetWTitle( inWindowRef
, ptitle
) ;
556 // appearance additions
558 void UMASetControlTitle( ControlRef inControl
, const wxString
& title
, wxFontEncoding encoding
)
561 SetControlTitleWithCFString( inControl
, wxMacCFStringHolder(title
, encoding
) ) ;
565 wxMacStringToPascal( title
, ptitle
) ;
566 SetControlTitle( inControl
, ptitle
) ;
570 void UMAActivateControl( ControlRef inControl
)
572 #if TARGET_API_MAC_OSX
573 ::ActivateControl( inControl
) ;
576 // we have to add the control after again to the update rgn
577 // otherwise updates get lost
578 if ( !IsControlActive( inControl
) )
580 bool visible
= IsControlVisible( inControl
) ;
582 SetControlVisibility( inControl
, false , false ) ;
584 ::ActivateControl( inControl
) ;
588 SetControlVisibility( inControl
, true , false ) ;
591 InvalWindowRect( GetControlOwner(inControl
), UMAGetControlBoundsInWindowCoords(inControl
, &ctrlBounds
) ) ;
597 void UMAMoveControl( ControlRef inControl
, short x
, short y
)
599 #if TARGET_API_MAC_OSX
600 ::MoveControl( inControl
, x
, y
) ;
603 bool visible
= IsControlVisible( inControl
) ;
606 SetControlVisibility( inControl
, false , false ) ;
608 InvalWindowRect( GetControlOwner(inControl
), GetControlBounds(inControl
, &ctrlBounds
) ) ;
611 ::MoveControl( inControl
, x
, y
) ;
615 SetControlVisibility( inControl
, true , false ) ;
617 InvalWindowRect( GetControlOwner(inControl
), GetControlBounds(inControl
, &ctrlBounds
) ) ;
622 void UMASizeControl( ControlRef inControl
, short x
, short y
)
624 #if TARGET_API_MAC_OSX
625 ::SizeControl( inControl
, x
, y
) ;
628 bool visible
= IsControlVisible( inControl
) ;
631 SetControlVisibility( inControl
, false , false ) ;
633 InvalWindowRect( GetControlOwner(inControl
), GetControlBounds(inControl
, &ctrlBounds
) ) ;
636 ::SizeControl( inControl
, x
, y
) ;
640 SetControlVisibility( inControl
, true , false ) ;
642 InvalWindowRect( GetControlOwner(inControl
), GetControlBounds(inControl
, &ctrlBounds
) ) ;
647 void UMADeactivateControl( ControlRef inControl
)
649 #if TARGET_API_MAC_OSX
650 ::DeactivateControl( inControl
) ;
653 // we have to add the control after again to the update rgn
654 // otherwise updates get lost
655 bool visible
= IsControlVisible( inControl
) ;
657 SetControlVisibility( inControl
, false , false ) ;
659 ::DeactivateControl( inControl
) ;
663 SetControlVisibility( inControl
, true , false ) ;
665 InvalWindowRect( GetControlOwner(inControl
), UMAGetControlBoundsInWindowCoords(inControl
, &ctrlBounds
) ) ;
670 // shows the control and adds the region to the update region
671 void UMAShowControl( ControlRef inControl
)
673 SetControlVisibility( inControl
, true , false ) ;
675 InvalWindowRect( GetControlOwner(inControl
), UMAGetControlBoundsInWindowCoords(inControl
, &ctrlBounds
) ) ;
678 // hides the control and adds the region to the update region
679 void UMAHideControl( ControlRef inControl
)
681 SetControlVisibility( inControl
, false , false ) ;
683 InvalWindowRect( GetControlOwner(inControl
), UMAGetControlBoundsInWindowCoords(inControl
, &ctrlBounds
) ) ;
687 OSErr
UMASetKeyboardFocus( WindowPtr inWindow
,
688 ControlRef inControl
,
689 ControlFocusPart inPart
)
695 SetPortWindowPort( inWindow
) ;
697 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
703 bool UMAIsWindowFloating( WindowRef inWindow
)
707 GetWindowClass( inWindow
, &cl
) ;
708 return cl
== kFloatingWindowClass
;
711 bool UMAIsWindowModal( WindowRef inWindow
)
715 GetWindowClass( inWindow
, &cl
) ;
716 return cl
< kFloatingWindowClass
;
721 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
725 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
726 // if ( inActivate != isHighlighted )
730 SetPortWindowPort( inWindowRef
) ;
731 HiliteWindow( inWindowRef
, inActivate
) ;
732 ControlRef control
= NULL
;
733 ::GetRootControl( inWindowRef
, &control
) ;
737 UMAActivateControl( control
) ;
739 UMADeactivateControl( control
) ;
746 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
748 return ::DrawThemePlacard( inRect
, inState
) ;
752 static OSStatus helpMenuStatus
= noErr
;
753 static MenuItemIndex firstCustomItemIndex
= 0 ;
756 OSStatus
UMAGetHelpMenu(
757 MenuRef
* outHelpMenu
,
758 MenuItemIndex
* outFirstCustomItemIndex
)
761 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
764 MenuRef helpMenuHandle
;
766 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
767 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
768 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
770 if ( outFirstCustomItemIndex
)
771 *outFirstCustomItemIndex
= firstCustomItemIndex
;
773 *outHelpMenu
= helpMenuHandle
;
775 return helpMenuStatus
;
779 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
785 wxMacPortStateHelper::wxMacPortStateHelper()
790 void wxMacPortStateHelper::Setup( GrafPtr newport
)
792 GetPort( &m_oldPort
) ;
796 wxASSERT_MSG( m_clip
== NULL
, wxT("Cannot call setup twice") ) ;
799 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
800 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
801 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
802 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
803 GetThemeDrawingState( &m_drawingState
) ;
804 m_currentPort
= newport
;
807 void wxMacPortStateHelper::Clear()
811 DisposeRgn( m_clip
) ;
812 DisposeThemeDrawingState( m_drawingState
) ;
817 wxMacPortStateHelper::~wxMacPortStateHelper()
821 SetPort( m_currentPort
) ;
823 DisposeRgn( m_clip
) ;
824 TextFont( m_textFont
);
825 TextSize( m_textSize
);
826 TextFace( m_textStyle
);
827 TextMode( m_textMode
);
828 SetThemeDrawingState( m_drawingState
, true ) ;
829 SetPort( m_oldPort
) ;
833 OSStatus
UMAPutScrap( Size size
, OSType type
, void *data
)
835 OSStatus err
= noErr
;
838 err
= PutScrap( size
, type
, data
) ;
841 err
= GetCurrentScrap( &scrap
);
843 err
= PutScrapFlavor( scrap
, type
, 0, size
, data
);
849 Rect
* UMAGetControlBoundsInWindowCoords( ControlRef theControl
, Rect
*bounds
)
851 GetControlBounds( theControl
, bounds
) ;
853 #if TARGET_API_MAC_OSX
854 WindowRef tlwref
= GetControlOwner( theControl
) ;
856 wxTopLevelWindowMac
* tlwwx
= wxFindWinFromMacWindow( tlwref
) ;
857 if ( tlwwx
!= NULL
&& tlwwx
->MacUsesCompositing() )
859 ControlRef rootControl
= tlwwx
->GetPeer()->GetControlRef() ;
860 HIPoint hiPoint
= CGPointMake( 0 , 0 ) ;
861 HIViewConvertPoint( &hiPoint
, HIViewGetSuperview(theControl
) , rootControl
) ;
862 OffsetRect( bounds
, (short) hiPoint
.x
, (short) hiPoint
.y
) ;
873 static bool sUMASystemInitialized
= false ;
875 bool UMASystemIsInitialized()
877 return sUMASystemInitialized
;
880 void UMASetSystemIsInitialized(bool val
)
882 sUMASystemInitialized
= val
;