1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: UMA support
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: The wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
19 # include <MacTextEditor.h>
20 # include <Navigation.h>
21 # if defined(TARGET_CARBON)
22 # if PM_USE_SESSION_APIS
25 # include <PMApplication.h>
27 # include <Printing.h>
34 #include "wx/mac/uma.h"
36 #if TARGET_API_MAC_OSX
37 #include "wx/toplevel.h"
40 // since we have decided that we only support 8.6 upwards we are
41 // checking for these minimum requirements in the startup code of
42 // the application so all wxWidgets code can safely assume that appearance 1.1
43 // windows manager, control manager, navigation services etc. are
46 static bool sUMAHasAppearance
= false ;
47 static long sUMAAppearanceVersion
= 0 ;
48 static long sUMASystemVersion
= 0 ;
49 static bool sUMAHasAquaLayout
= false ;
51 static bool sUMAHasInittedAppearance
= false;
53 extern int gAGABackgroundColor
;
55 bool UMAHasAppearance() { return sUMAHasAppearance
; }
56 long UMAGetAppearanceVersion() { return sUMAAppearanceVersion
; }
57 long UMAGetSystemVersion() { return sUMASystemVersion
; }
59 static bool sUMAHasWindowManager
= false ;
60 static long sUMAWindowManagerAttr
= 0 ;
62 bool UMAHasWindowManager() { return sUMAHasWindowManager
; }
63 long UMAGetWindowManagerAttr() { return sUMAWindowManagerAttr
; }
64 bool UMAHasAquaLayout() { return sUMAHasAquaLayout
; }
67 void UMACleanupToolbox()
69 if (sUMAHasInittedAppearance
)
70 UnregisterAppearanceClient() ;
72 if ( NavServicesAvailable() )
75 if ( TXNTerminateTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
76 TXNTerminateTextension( ) ;
79 void UMAInitToolbox( UInt16 inMoreMastersCalls
, bool isEmbedded
)
83 for (long i
= 1; i
<= inMoreMastersCalls
; i
++)
88 ::InitGraf(&qd
.thePort
);
93 ::FlushEvents(everyEvent
, 0);
97 PurgeSpace(&total
, &contig
);
102 if ( Gestalt(gestaltSystemVersion
, &sUMASystemVersion
) != noErr
)
103 sUMASystemVersion
= 0x0000 ;
106 if ( Gestalt( gestaltAppearanceAttr
, &theAppearance
) == noErr
)
108 // If status equals appearanceProcessRegisteredErr it means the
109 // appearance client already was registered (For example if we run
110 // embedded, the host might have registered it). In such a case
111 // we don't unregister it later on.
113 sUMAHasAppearance
= true ;
114 OSStatus status
= RegisterAppearanceClient();
115 if (status
!= appearanceProcessRegisteredErr
)
117 // Appearance client wasn't registered yet.
118 sUMAHasInittedAppearance
= true;
121 if ( Gestalt( gestaltAppearanceVersion
, &theAppearance
) == noErr
)
122 sUMAAppearanceVersion
= theAppearance
;
124 sUMAAppearanceVersion
= 0x0100 ;
127 if ( Gestalt( gestaltWindowMgrAttr
, &sUMAWindowManagerAttr
) == noErr
)
128 sUMAHasWindowManager
= sUMAWindowManagerAttr
& gestaltWindowMgrPresent
;
131 // Call currently implicitely done : InitFloatingWindows() ;
135 if ( sUMAHasWindowManager
)
136 InitFloatingWindows() ;
142 if ( NavServicesAvailable() )
146 Gestalt( gestaltMenuMgrAttr
, &menuMgrAttr
) ;
147 if ( menuMgrAttr
& gestaltMenuMgrAquaLayoutMask
)
148 sUMAHasAquaLayout
= true ;
150 if ( TXNInitTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
152 FontFamilyID fontId
;
157 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
158 GetFNum( fontName
, &fontId
);
160 TXNMacOSPreferredFontDescription fontDescriptions
[] =
162 { fontId
, (fontSize
<< 16) , kTXNDefaultFontStyle
, kTXNSystemDefaultEncoding
}
164 int noOfFontDescriptions
= sizeof( fontDescriptions
) / sizeof(TXNMacOSPreferredFontDescription
) ;
166 OptionBits options
= 0 ;
168 if ( UMAGetSystemVersion() < 0x1000 )
169 options
|= kTXNAlwaysUseQuickDrawTextMask
;
171 TXNInitTextension( fontDescriptions
, noOfFontDescriptions
, options
);
174 UMASetSystemIsInitialized( true );
178 Boolean
CanUseATSUI()
181 OSErr err
= Gestalt(gestaltATSUVersion
, &result
);
182 return (err
== noErr
);
187 long UMAGetProcessMode()
190 ProcessInfoRec processinfo
;
191 ProcessSerialNumber procno
;
193 procno
.highLongOfPSN
= 0 ;
194 procno
.lowLongOfPSN
= kCurrentProcess
;
195 processinfo
.processInfoLength
= sizeof(ProcessInfoRec
);
196 processinfo
.processName
= NULL
;
197 processinfo
.processAppSpec
= NULL
;
199 err
= ::GetProcessInformation( &procno
, &processinfo
) ;
200 wxASSERT( err
== noErr
) ;
202 return processinfo
.processMode
;
205 bool UMAGetProcessModeDoesActivateOnFGSwitch()
207 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch
;
212 MenuRef
UMANewMenu( SInt16 id
, const wxString
& title
, wxFontEncoding encoding
)
214 wxString str
= wxStripMenuCodes( title
) ;
218 CreateNewMenu( id
, 0 , &menu
) ;
219 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
, encoding
) ) ;
222 wxMacStringToPascal( str
, ptitle
) ;
223 menu
= ::NewMenu( id
, ptitle
) ;
229 void UMASetMenuTitle( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
)
231 wxString str
= wxStripMenuCodes( title
) ;
234 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
, encoding
) ) ;
238 wxMacStringToPascal( str
, ptitle
) ;
239 SetMenuTitle( menu
, ptitle
) ;
243 void UMASetMenuItemText( MenuRef menu
, MenuItemIndex item
, const wxString
& title
, wxFontEncoding encoding
)
245 wxString str
= wxStripMenuCodes( title
) ;
248 SetMenuItemTextWithCFString( menu
, item
, wxMacCFStringHolder(str
, encoding
) ) ;
252 wxMacStringToPascal( str
, ptitle
) ;
253 SetMenuItemText( menu
, item
, ptitle
) ;
257 UInt32
UMAMenuEvent( EventRecord
*inEvent
)
259 return MenuEvent( inEvent
) ;
262 void UMAEnableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
, bool enable
)
265 EnableMenuItem( inMenu
, inItem
) ;
267 DisableMenuItem( inMenu
, inItem
) ;
270 void UMAAppendSubMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, SInt16 id
)
272 MacAppendMenu( menu
, "\pA" );
273 UMASetMenuItemText( menu
, (SInt16
) ::CountMenuItems(menu
), title
, encoding
);
274 SetMenuItemHierarchicalID( menu
, CountMenuItems( menu
) , id
) ;
277 void UMAInsertSubMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, MenuItemIndex item
, SInt16 id
)
279 MacInsertMenuItem( menu
, "\pA" , item
);
280 UMASetMenuItemText( menu
, item
+1, title
, encoding
);
281 SetMenuItemHierarchicalID( menu
, item
+1 , id
) ;
284 void UMASetMenuItemShortcut( MenuRef menu
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
289 UInt8 modifiers
= 0 ;
290 SInt16 key
= entry
->GetKeyCode() ;
293 bool explicitCommandKey
= (entry
->GetFlags() & wxACCEL_CTRL
);
295 if (entry
->GetFlags() & wxACCEL_ALT
)
296 modifiers
|= kMenuOptionModifier
;
298 if (entry
->GetFlags() & wxACCEL_SHIFT
)
299 modifiers
|= kMenuShiftModifier
;
302 SInt16 macKey
= key
;
303 if ( key
>= WXK_F1
&& key
<= WXK_F15
)
305 if ( !explicitCommandKey
)
306 modifiers
|= kMenuNoCommandModifier
;
308 // for some reasons this must be 0 right now
309 // everything else leads to just the first function key item
310 // to be selected. Thanks to Ryan Wilcox for finding out.
312 glyph
= kMenuF1Glyph
+ ( key
- WXK_F1
) ;
313 if ( key
>= WXK_F13
)
321 macKey
= kBackspaceCharCode
;
322 glyph
= kMenuDeleteLeftGlyph
;
326 macKey
= kTabCharCode
;
327 glyph
= kMenuTabRightGlyph
;
330 case kEnterCharCode
:
331 macKey
= kEnterCharCode
;
332 glyph
= kMenuEnterGlyph
;
336 macKey
= kReturnCharCode
;
337 glyph
= kMenuReturnGlyph
;
341 macKey
= kEscapeCharCode
;
342 glyph
= kMenuEscapeGlyph
;
347 glyph
= kMenuSpaceGlyph
;
351 macKey
= kDeleteCharCode
;
352 glyph
= kMenuDeleteRightGlyph
;
356 macKey
= kClearCharCode
;
357 glyph
= kMenuClearGlyph
;
360 case WXK_PRIOR
: // PAGE UP
361 macKey
= kPageUpCharCode
;
362 glyph
= kMenuPageUpGlyph
;
366 macKey
= kPageDownCharCode
;
367 glyph
= kMenuPageDownGlyph
;
371 macKey
= kLeftArrowCharCode
;
372 glyph
= kMenuLeftArrowGlyph
;
376 macKey
= kUpArrowCharCode
;
377 glyph
= kMenuUpArrowGlyph
;
381 macKey
= kRightArrowCharCode
;
382 glyph
= kMenuRightArrowGlyph
;
386 macKey
= kDownArrowCharCode
;
387 glyph
= kMenuDownArrowGlyph
;
391 macKey
= toupper( key
) ;
395 // we now allow non command key shortcuts
396 // remove in case this gives problems
397 if ( !explicitCommandKey
)
398 modifiers
|= kMenuNoCommandModifier
;
401 // 1d and 1e have special meaning to SetItemCmd, so
402 // do not use for these character codes.
403 if (key
!= WXK_UP
&& key
!= WXK_RIGHT
)
404 SetItemCmd( menu
, item
, macKey
);
406 SetMenuItemModifiers( menu
, item
, modifiers
) ;
409 SetMenuItemKeyGlyph( menu
, item
, glyph
) ;
413 void UMAAppendMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, wxAcceleratorEntry
*entry
)
415 MacAppendMenu(menu
, "\pA");
417 // don't attempt to interpret metacharacters like a '-' at the beginning (would become a separator otherwise)
418 ChangeMenuItemAttributes( menu
, ::CountMenuItems(menu
), kMenuItemAttrIgnoreMeta
, 0 ) ;
419 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
, encoding
);
420 UMASetMenuItemShortcut( menu
, (SInt16
) ::CountMenuItems(menu
), entry
) ;
423 void UMAInsertMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
425 MacInsertMenuItem( menu
, "\pA" , item
) ;
427 // don't attempt to interpret metacharacters like a '-' at the beginning (would become a separator otherwise)
428 ChangeMenuItemAttributes( menu
, item
+1, kMenuItemAttrIgnoreMeta
, 0 ) ;
429 UMASetMenuItemText(menu
, item
+1 , title
, encoding
);
430 UMASetMenuItemShortcut( menu
, item
+1 , entry
) ;
437 int gPrOpenCounter
= 0 ;
445 if ( gPrOpenCounter
== 1 )
449 wxASSERT( err
== noErr
) ;
455 OSStatus
UMAPrClose()
459 wxASSERT( gPrOpenCounter
>= 1 ) ;
461 if ( gPrOpenCounter
== 1 )
465 wxASSERT( err
== noErr
) ;
473 pascal QDGlobalsPtr
GetQDGlobalsPtr() ;
474 pascal QDGlobalsPtr
GetQDGlobalsPtr()
476 return QDGlobalsPtr (* (Ptr
*) LMGetCurrentA5 ( ) - 0xCA);
481 void UMAShowWatchCursor()
485 CursHandle watchFob
= GetCursor(watchCursor
);
487 if (watchFob
== NULL
)
494 // Cursor preservedArrow;
495 // GetQDGlobalsArrow(&preservedArrow);
496 // SetQDGlobalsArrow(*watchFob);
498 // SetQDGlobalsArrow(&preservedArrow);
499 SetCursor(*watchFob
);
501 SetCursor(*watchFob
);
506 void UMAShowArrowCursor()
510 SetCursor( GetQDGlobalsArrow(&arrow
) );
512 SetCursor (&(qd
.arrow
));
518 GrafPtr
UMAGetWindowPort( WindowRef inWindowRef
)
520 wxASSERT( inWindowRef
!= NULL
) ;
523 return (GrafPtr
) GetWindowPort( inWindowRef
) ;
525 return (GrafPtr
) inWindowRef
;
529 void UMADisposeWindow( WindowRef inWindowRef
)
531 wxASSERT( inWindowRef
!= NULL
) ;
533 DisposeWindow( inWindowRef
) ;
536 void UMASetWTitle( WindowRef inWindowRef
, const wxString
& title
, wxFontEncoding encoding
)
539 SetWindowTitleWithCFString( inWindowRef
, wxMacCFStringHolder(title
, encoding
) ) ;
543 wxMacStringToPascal( title
, ptitle
) ;
544 SetWTitle( inWindowRef
, ptitle
) ;
548 // appearance additions
550 void UMASetControlTitle( ControlRef inControl
, const wxString
& title
, wxFontEncoding encoding
)
553 SetControlTitleWithCFString( inControl
, wxMacCFStringHolder(title
, encoding
) ) ;
557 wxMacStringToPascal( title
, ptitle
) ;
558 SetControlTitle( inControl
, ptitle
) ;
562 void UMAActivateControl( ControlRef inControl
)
564 #if TARGET_API_MAC_OSX
565 ::ActivateControl( inControl
) ;
568 // we have to add the control after again to the update rgn
569 // otherwise updates get lost
570 if ( !IsControlActive( inControl
) )
572 bool visible
= IsControlVisible( inControl
) ;
574 SetControlVisibility( inControl
, false , false ) ;
576 ::ActivateControl( inControl
) ;
580 SetControlVisibility( inControl
, true , false ) ;
583 InvalWindowRect( GetControlOwner(inControl
), UMAGetControlBoundsInWindowCoords(inControl
, &ctrlBounds
) ) ;
589 void UMAMoveControl( ControlRef inControl
, short x
, short y
)
591 #if TARGET_API_MAC_OSX
592 ::MoveControl( inControl
, x
, y
) ;
595 bool visible
= IsControlVisible( inControl
) ;
598 SetControlVisibility( inControl
, false , false ) ;
600 InvalWindowRect( GetControlOwner(inControl
), GetControlBounds(inControl
, &ctrlBounds
) ) ;
603 ::MoveControl( inControl
, x
, y
) ;
607 SetControlVisibility( inControl
, true , false ) ;
609 InvalWindowRect( GetControlOwner(inControl
), GetControlBounds(inControl
, &ctrlBounds
) ) ;
614 void UMASizeControl( ControlRef inControl
, short x
, short y
)
616 #if TARGET_API_MAC_OSX
617 ::SizeControl( inControl
, x
, y
) ;
620 bool visible
= IsControlVisible( inControl
) ;
623 SetControlVisibility( inControl
, false , false ) ;
625 InvalWindowRect( GetControlOwner(inControl
), GetControlBounds(inControl
, &ctrlBounds
) ) ;
628 ::SizeControl( inControl
, x
, y
) ;
632 SetControlVisibility( inControl
, true , false ) ;
634 InvalWindowRect( GetControlOwner(inControl
), GetControlBounds(inControl
, &ctrlBounds
) ) ;
639 void UMADeactivateControl( ControlRef inControl
)
641 #if TARGET_API_MAC_OSX
642 ::DeactivateControl( inControl
) ;
645 // we have to add the control after again to the update rgn
646 // otherwise updates get lost
647 bool visible
= IsControlVisible( inControl
) ;
649 SetControlVisibility( inControl
, false , false ) ;
651 ::DeactivateControl( inControl
) ;
655 SetControlVisibility( inControl
, true , false ) ;
657 InvalWindowRect( GetControlOwner(inControl
), UMAGetControlBoundsInWindowCoords(inControl
, &ctrlBounds
) ) ;
662 // shows the control and adds the region to the update region
663 void UMAShowControl( ControlRef inControl
)
665 SetControlVisibility( inControl
, true , false ) ;
667 InvalWindowRect( GetControlOwner(inControl
), UMAGetControlBoundsInWindowCoords(inControl
, &ctrlBounds
) ) ;
670 // hides the control and adds the region to the update region
671 void UMAHideControl( ControlRef inControl
)
673 SetControlVisibility( inControl
, false , false ) ;
675 InvalWindowRect( GetControlOwner(inControl
), UMAGetControlBoundsInWindowCoords(inControl
, &ctrlBounds
) ) ;
679 OSErr
UMASetKeyboardFocus( WindowPtr inWindow
,
680 ControlRef inControl
,
681 ControlFocusPart inPart
)
687 SetPortWindowPort( inWindow
) ;
689 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
695 bool UMAIsWindowFloating( WindowRef inWindow
)
699 GetWindowClass( inWindow
, &cl
) ;
700 return cl
== kFloatingWindowClass
;
703 bool UMAIsWindowModal( WindowRef inWindow
)
707 GetWindowClass( inWindow
, &cl
) ;
708 return cl
< kFloatingWindowClass
;
713 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
717 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
718 // if ( inActivate != isHighlighted )
722 SetPortWindowPort( inWindowRef
) ;
723 HiliteWindow( inWindowRef
, inActivate
) ;
724 ControlRef control
= NULL
;
725 ::GetRootControl( inWindowRef
, &control
) ;
729 UMAActivateControl( control
) ;
731 UMADeactivateControl( control
) ;
738 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
740 return ::DrawThemePlacard( inRect
, inState
) ;
744 static OSStatus helpMenuStatus
= noErr
;
745 static MenuItemIndex firstCustomItemIndex
= 0 ;
748 OSStatus
UMAGetHelpMenu(
749 MenuRef
* outHelpMenu
,
750 MenuItemIndex
* outFirstCustomItemIndex
)
753 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
756 MenuRef helpMenuHandle
;
758 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
759 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
760 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
762 if ( outFirstCustomItemIndex
)
763 *outFirstCustomItemIndex
= firstCustomItemIndex
;
765 *outHelpMenu
= helpMenuHandle
;
767 return helpMenuStatus
;
771 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
777 wxMacPortStateHelper::wxMacPortStateHelper()
782 void wxMacPortStateHelper::Setup( GrafPtr newport
)
784 GetPort( &m_oldPort
) ;
788 wxASSERT_MSG( m_clip
== NULL
, wxT("Cannot call setup twice") ) ;
791 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
792 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
793 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
794 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
795 GetThemeDrawingState( &m_drawingState
) ;
796 m_currentPort
= newport
;
799 void wxMacPortStateHelper::Clear()
803 DisposeRgn( m_clip
) ;
804 DisposeThemeDrawingState( m_drawingState
) ;
809 wxMacPortStateHelper::~wxMacPortStateHelper()
813 SetPort( m_currentPort
) ;
815 DisposeRgn( m_clip
) ;
816 TextFont( m_textFont
);
817 TextSize( m_textSize
);
818 TextFace( m_textStyle
);
819 TextMode( m_textMode
);
820 SetThemeDrawingState( m_drawingState
, true ) ;
821 SetPort( m_oldPort
) ;
825 OSStatus
UMAPutScrap( Size size
, OSType type
, void *data
)
827 OSStatus err
= noErr
;
830 err
= PutScrap( size
, type
, data
) ;
833 err
= GetCurrentScrap( &scrap
);
835 err
= PutScrapFlavor( scrap
, type
, 0, size
, data
);
841 Rect
* UMAGetControlBoundsInWindowCoords( ControlRef theControl
, Rect
*bounds
)
843 GetControlBounds( theControl
, bounds
) ;
845 #if TARGET_API_MAC_OSX
846 WindowRef tlwref
= GetControlOwner( theControl
) ;
848 wxTopLevelWindowMac
* tlwwx
= wxFindWinFromMacWindow( tlwref
) ;
849 if ( tlwwx
!= NULL
&& tlwwx
->MacUsesCompositing() )
851 ControlRef rootControl
= tlwwx
->GetPeer()->GetControlRef() ;
852 HIPoint hiPoint
= CGPointMake( 0 , 0 ) ;
853 HIViewConvertPoint( &hiPoint
, HIViewGetSuperview(theControl
) , rootControl
) ;
854 OffsetRect( bounds
, (short) hiPoint
.x
, (short) hiPoint
.y
) ;
865 static bool sUMASystemInitialized
= false ;
867 bool UMASystemIsInitialized()
869 return sUMASystemInitialized
;
872 void UMASetSystemIsInitialized(bool val
)
874 sUMASystemInitialized
= val
;