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 // we don't strip the accels here anymore, must be done before
246 wxString str
= title
;
249 SetMenuItemTextWithCFString( menu
, item
, wxMacCFStringHolder(str
, encoding
) ) ;
253 wxMacStringToPascal( str
, ptitle
) ;
254 SetMenuItemText( menu
, item
, ptitle
) ;
258 UInt32
UMAMenuEvent( EventRecord
*inEvent
)
260 return MenuEvent( inEvent
) ;
263 void UMAEnableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
, bool enable
)
266 EnableMenuItem( inMenu
, inItem
) ;
268 DisableMenuItem( inMenu
, inItem
) ;
271 void UMAAppendSubMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, SInt16 id
)
273 MacAppendMenu( menu
, "\pA" );
274 UMASetMenuItemText( menu
, (SInt16
) ::CountMenuItems(menu
), title
, encoding
);
275 SetMenuItemHierarchicalID( menu
, CountMenuItems( menu
) , id
) ;
278 void UMAInsertSubMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, MenuItemIndex item
, SInt16 id
)
280 MacInsertMenuItem( menu
, "\pA" , item
);
281 UMASetMenuItemText( menu
, item
+1, title
, encoding
);
282 SetMenuItemHierarchicalID( menu
, item
+1 , id
) ;
285 void UMASetMenuItemShortcut( MenuRef menu
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
290 UInt8 modifiers
= 0 ;
291 SInt16 key
= entry
->GetKeyCode() ;
294 bool explicitCommandKey
= (entry
->GetFlags() & wxACCEL_CTRL
);
296 if (entry
->GetFlags() & wxACCEL_ALT
)
297 modifiers
|= kMenuOptionModifier
;
299 if (entry
->GetFlags() & wxACCEL_SHIFT
)
300 modifiers
|= kMenuShiftModifier
;
303 SInt16 macKey
= key
;
304 if ( key
>= WXK_F1
&& key
<= WXK_F15
)
306 if ( !explicitCommandKey
)
307 modifiers
|= kMenuNoCommandModifier
;
309 // for some reasons this must be 0 right now
310 // everything else leads to just the first function key item
311 // to be selected. Thanks to Ryan Wilcox for finding out.
313 glyph
= kMenuF1Glyph
+ ( key
- WXK_F1
) ;
314 if ( key
>= WXK_F13
)
322 macKey
= kBackspaceCharCode
;
323 glyph
= kMenuDeleteLeftGlyph
;
327 macKey
= kTabCharCode
;
328 glyph
= kMenuTabRightGlyph
;
331 case kEnterCharCode
:
332 macKey
= kEnterCharCode
;
333 glyph
= kMenuEnterGlyph
;
337 macKey
= kReturnCharCode
;
338 glyph
= kMenuReturnGlyph
;
342 macKey
= kEscapeCharCode
;
343 glyph
= kMenuEscapeGlyph
;
348 glyph
= kMenuSpaceGlyph
;
352 macKey
= kDeleteCharCode
;
353 glyph
= kMenuDeleteRightGlyph
;
357 macKey
= kClearCharCode
;
358 glyph
= kMenuClearGlyph
;
361 case WXK_PRIOR
: // PAGE UP
362 macKey
= kPageUpCharCode
;
363 glyph
= kMenuPageUpGlyph
;
367 macKey
= kPageDownCharCode
;
368 glyph
= kMenuPageDownGlyph
;
372 macKey
= kLeftArrowCharCode
;
373 glyph
= kMenuLeftArrowGlyph
;
377 macKey
= kUpArrowCharCode
;
378 glyph
= kMenuUpArrowGlyph
;
382 macKey
= kRightArrowCharCode
;
383 glyph
= kMenuRightArrowGlyph
;
387 macKey
= kDownArrowCharCode
;
388 glyph
= kMenuDownArrowGlyph
;
392 macKey
= toupper( key
) ;
396 // we now allow non command key shortcuts
397 // remove in case this gives problems
398 if ( !explicitCommandKey
)
399 modifiers
|= kMenuNoCommandModifier
;
402 // 1d and 1e have special meaning to SetItemCmd, so
403 // do not use for these character codes.
404 if (key
!= WXK_UP
&& key
!= WXK_RIGHT
)
405 SetItemCmd( menu
, item
, macKey
);
407 SetMenuItemModifiers( menu
, item
, modifiers
) ;
410 SetMenuItemKeyGlyph( menu
, item
, glyph
) ;
414 void UMAAppendMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, wxAcceleratorEntry
*entry
)
416 MacAppendMenu(menu
, "\pA");
418 // don't attempt to interpret metacharacters like a '-' at the beginning (would become a separator otherwise)
419 ChangeMenuItemAttributes( menu
, ::CountMenuItems(menu
), kMenuItemAttrIgnoreMeta
, 0 ) ;
420 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
, encoding
);
421 UMASetMenuItemShortcut( menu
, (SInt16
) ::CountMenuItems(menu
), entry
) ;
424 void UMAInsertMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
426 MacInsertMenuItem( menu
, "\pA" , item
) ;
428 // don't attempt to interpret metacharacters like a '-' at the beginning (would become a separator otherwise)
429 ChangeMenuItemAttributes( menu
, item
+1, kMenuItemAttrIgnoreMeta
, 0 ) ;
430 UMASetMenuItemText(menu
, item
+1 , title
, encoding
);
431 UMASetMenuItemShortcut( menu
, item
+1 , entry
) ;
438 int gPrOpenCounter
= 0 ;
446 if ( gPrOpenCounter
== 1 )
450 wxASSERT( err
== noErr
) ;
456 OSStatus
UMAPrClose()
460 wxASSERT( gPrOpenCounter
>= 1 ) ;
462 if ( gPrOpenCounter
== 1 )
466 wxASSERT( err
== noErr
) ;
474 pascal QDGlobalsPtr
GetQDGlobalsPtr() ;
475 pascal QDGlobalsPtr
GetQDGlobalsPtr()
477 return QDGlobalsPtr (* (Ptr
*) LMGetCurrentA5 ( ) - 0xCA);
482 void UMAShowWatchCursor()
486 CursHandle watchFob
= GetCursor(watchCursor
);
488 if (watchFob
== NULL
)
495 // Cursor preservedArrow;
496 // GetQDGlobalsArrow(&preservedArrow);
497 // SetQDGlobalsArrow(*watchFob);
499 // SetQDGlobalsArrow(&preservedArrow);
500 SetCursor(*watchFob
);
502 SetCursor(*watchFob
);
507 void UMAShowArrowCursor()
511 SetCursor( GetQDGlobalsArrow(&arrow
) );
513 SetCursor (&(qd
.arrow
));
519 GrafPtr
UMAGetWindowPort( WindowRef inWindowRef
)
521 wxASSERT( inWindowRef
!= NULL
) ;
524 return (GrafPtr
) GetWindowPort( inWindowRef
) ;
526 return (GrafPtr
) inWindowRef
;
530 void UMADisposeWindow( WindowRef inWindowRef
)
532 wxASSERT( inWindowRef
!= NULL
) ;
534 DisposeWindow( inWindowRef
) ;
537 void UMASetWTitle( WindowRef inWindowRef
, const wxString
& title
, wxFontEncoding encoding
)
540 SetWindowTitleWithCFString( inWindowRef
, wxMacCFStringHolder(title
, encoding
) ) ;
544 wxMacStringToPascal( title
, ptitle
) ;
545 SetWTitle( inWindowRef
, ptitle
) ;
549 // appearance additions
551 void UMASetControlTitle( ControlRef inControl
, const wxString
& title
, wxFontEncoding encoding
)
554 SetControlTitleWithCFString( inControl
, wxMacCFStringHolder(title
, encoding
) ) ;
558 wxMacStringToPascal( title
, ptitle
) ;
559 SetControlTitle( inControl
, ptitle
) ;
563 void UMAActivateControl( ControlRef inControl
)
565 #if TARGET_API_MAC_OSX
566 ::ActivateControl( inControl
) ;
569 // we have to add the control after again to the update rgn
570 // otherwise updates get lost
571 if ( !IsControlActive( inControl
) )
573 bool visible
= IsControlVisible( inControl
) ;
575 SetControlVisibility( inControl
, false , false ) ;
577 ::ActivateControl( inControl
) ;
581 SetControlVisibility( inControl
, true , false ) ;
584 InvalWindowRect( GetControlOwner(inControl
), UMAGetControlBoundsInWindowCoords(inControl
, &ctrlBounds
) ) ;
590 void UMAMoveControl( ControlRef inControl
, short x
, short y
)
592 #if TARGET_API_MAC_OSX
593 ::MoveControl( inControl
, x
, y
) ;
596 bool visible
= IsControlVisible( inControl
) ;
599 SetControlVisibility( inControl
, false , false ) ;
601 InvalWindowRect( GetControlOwner(inControl
), GetControlBounds(inControl
, &ctrlBounds
) ) ;
604 ::MoveControl( inControl
, x
, y
) ;
608 SetControlVisibility( inControl
, true , false ) ;
610 InvalWindowRect( GetControlOwner(inControl
), GetControlBounds(inControl
, &ctrlBounds
) ) ;
615 void UMASizeControl( ControlRef inControl
, short x
, short y
)
617 #if TARGET_API_MAC_OSX
618 ::SizeControl( inControl
, x
, y
) ;
621 bool visible
= IsControlVisible( inControl
) ;
624 SetControlVisibility( inControl
, false , false ) ;
626 InvalWindowRect( GetControlOwner(inControl
), GetControlBounds(inControl
, &ctrlBounds
) ) ;
629 ::SizeControl( inControl
, x
, y
) ;
633 SetControlVisibility( inControl
, true , false ) ;
635 InvalWindowRect( GetControlOwner(inControl
), GetControlBounds(inControl
, &ctrlBounds
) ) ;
640 void UMADeactivateControl( ControlRef inControl
)
642 #if TARGET_API_MAC_OSX
643 ::DeactivateControl( inControl
) ;
646 // we have to add the control after again to the update rgn
647 // otherwise updates get lost
648 bool visible
= IsControlVisible( inControl
) ;
650 SetControlVisibility( inControl
, false , false ) ;
652 ::DeactivateControl( inControl
) ;
656 SetControlVisibility( inControl
, true , false ) ;
658 InvalWindowRect( GetControlOwner(inControl
), UMAGetControlBoundsInWindowCoords(inControl
, &ctrlBounds
) ) ;
663 // shows the control and adds the region to the update region
664 void UMAShowControl( ControlRef inControl
)
666 SetControlVisibility( inControl
, true , false ) ;
668 InvalWindowRect( GetControlOwner(inControl
), UMAGetControlBoundsInWindowCoords(inControl
, &ctrlBounds
) ) ;
671 // hides the control and adds the region to the update region
672 void UMAHideControl( ControlRef inControl
)
674 SetControlVisibility( inControl
, false , false ) ;
676 InvalWindowRect( GetControlOwner(inControl
), UMAGetControlBoundsInWindowCoords(inControl
, &ctrlBounds
) ) ;
680 OSErr
UMASetKeyboardFocus( WindowPtr inWindow
,
681 ControlRef inControl
,
682 ControlFocusPart inPart
)
688 SetPortWindowPort( inWindow
) ;
690 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
696 bool UMAIsWindowFloating( WindowRef inWindow
)
700 GetWindowClass( inWindow
, &cl
) ;
701 return cl
== kFloatingWindowClass
;
704 bool UMAIsWindowModal( WindowRef inWindow
)
708 GetWindowClass( inWindow
, &cl
) ;
709 return cl
< kFloatingWindowClass
;
714 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
718 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
719 // if ( inActivate != isHighlighted )
723 SetPortWindowPort( inWindowRef
) ;
724 HiliteWindow( inWindowRef
, inActivate
) ;
725 ControlRef control
= NULL
;
726 ::GetRootControl( inWindowRef
, &control
) ;
730 UMAActivateControl( control
) ;
732 UMADeactivateControl( control
) ;
739 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
741 return ::DrawThemePlacard( inRect
, inState
) ;
745 static OSStatus helpMenuStatus
= noErr
;
746 static MenuItemIndex firstCustomItemIndex
= 0 ;
749 OSStatus
UMAGetHelpMenu(
750 MenuRef
* outHelpMenu
,
751 MenuItemIndex
* outFirstCustomItemIndex
)
754 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
757 MenuRef helpMenuHandle
;
759 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
760 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
761 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
763 if ( outFirstCustomItemIndex
)
764 *outFirstCustomItemIndex
= firstCustomItemIndex
;
766 *outHelpMenu
= helpMenuHandle
;
768 return helpMenuStatus
;
772 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
778 wxMacPortStateHelper::wxMacPortStateHelper()
783 void wxMacPortStateHelper::Setup( GrafPtr newport
)
785 GetPort( &m_oldPort
) ;
789 wxASSERT_MSG( m_clip
== NULL
, wxT("Cannot call setup twice") ) ;
792 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
793 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
794 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
795 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
796 GetThemeDrawingState( &m_drawingState
) ;
797 m_currentPort
= newport
;
800 void wxMacPortStateHelper::Clear()
804 DisposeRgn( m_clip
) ;
805 DisposeThemeDrawingState( m_drawingState
) ;
810 wxMacPortStateHelper::~wxMacPortStateHelper()
814 SetPort( m_currentPort
) ;
816 DisposeRgn( m_clip
) ;
817 TextFont( m_textFont
);
818 TextSize( m_textSize
);
819 TextFace( m_textStyle
);
820 TextMode( m_textMode
);
821 SetThemeDrawingState( m_drawingState
, true ) ;
822 SetPort( m_oldPort
) ;
826 OSStatus
UMAPutScrap( Size size
, OSType type
, void *data
)
828 OSStatus err
= noErr
;
831 err
= PutScrap( size
, type
, data
) ;
834 err
= GetCurrentScrap( &scrap
);
836 err
= PutScrapFlavor( scrap
, type
, 0, size
, data
);
842 Rect
* UMAGetControlBoundsInWindowCoords( ControlRef theControl
, Rect
*bounds
)
844 GetControlBounds( theControl
, bounds
) ;
846 #if TARGET_API_MAC_OSX
847 WindowRef tlwref
= GetControlOwner( theControl
) ;
849 wxTopLevelWindowMac
* tlwwx
= wxFindWinFromMacWindow( tlwref
) ;
850 if ( tlwwx
!= NULL
&& tlwwx
->MacUsesCompositing() )
852 ControlRef rootControl
= tlwwx
->GetPeer()->GetControlRef() ;
853 HIPoint hiPoint
= CGPointMake( 0 , 0 ) ;
854 HIViewConvertPoint( &hiPoint
, HIViewGetSuperview(theControl
) , rootControl
) ;
855 OffsetRect( bounds
, (short) hiPoint
.x
, (short) hiPoint
.y
) ;
866 static bool sUMASystemInitialized
= false ;
868 bool UMASystemIsInitialized()
870 return sUMASystemInitialized
;
873 void UMASetSystemIsInitialized(bool val
)
875 sUMASystemInitialized
= val
;