1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: UMA support
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: The wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
17 #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"
37 // since we have decided that we only support 8.6 upwards we are
38 // checking for these minimum requirements in the startup code of
39 // the application so all wxWindows code can safely assume that appearance 1.1
40 // windows manager, control manager, navigation services etc. are
43 static bool sUMAHasAppearance
= false ;
44 static long sUMAAppearanceVersion
= 0 ;
45 static long sUMASystemVersion
= 0 ;
46 static bool sUMAHasAquaLayout
= false ;
48 static bool sUMAHasInittedAppearance
= false;
50 extern int gAGABackgroundColor
;
51 bool UMAHasAppearance() { return sUMAHasAppearance
; }
52 long UMAGetAppearanceVersion() { return sUMAAppearanceVersion
; }
53 long UMAGetSystemVersion() { return sUMASystemVersion
; }
55 static bool sUMAHasWindowManager
= false ;
56 static long sUMAWindowManagerAttr
= 0 ;
58 bool UMAHasWindowManager() { return sUMAHasWindowManager
; }
59 long UMAGetWindowManagerAttr() { return sUMAWindowManagerAttr
; }
60 bool UMAHasAquaLayout() { return sUMAHasAquaLayout
; }
63 void UMACleanupToolbox()
65 if (sUMAHasInittedAppearance
)
67 UnregisterAppearanceClient() ;
69 if ( NavServicesAvailable() )
73 if ( TXNTerminateTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
74 TXNTerminateTextension( ) ;
76 void UMAInitToolbox( UInt16 inMoreMastersCalls
, bool isEmbedded
)
80 for (long i
= 1; i
<= inMoreMastersCalls
; i
++)
85 ::InitGraf(&qd
.thePort
);
90 ::FlushEvents(everyEvent
, 0);
94 PurgeSpace(&total
, &contig
);
99 if ( Gestalt(gestaltSystemVersion
, &sUMASystemVersion
) != noErr
)
100 sUMASystemVersion
= 0x0000 ;
103 if ( Gestalt( gestaltAppearanceAttr
, &theAppearance
) == noErr
)
105 sUMAHasAppearance
= true ;
106 OSStatus status
= RegisterAppearanceClient();
107 // If status equals appearanceProcessRegisteredErr it means the
108 // appearance client already was registered (For example if we run
109 // embedded, the host might have registered it). In such a case
110 // we don't unregister it later on.
111 if (status
!= appearanceProcessRegisteredErr
)
113 // Appearance client wasn't registered yet.
114 sUMAHasInittedAppearance
= true;
117 if ( Gestalt( gestaltAppearanceVersion
, &theAppearance
) == noErr
)
119 sUMAAppearanceVersion
= theAppearance
;
123 sUMAAppearanceVersion
= 0x0100 ;
126 if ( Gestalt( gestaltWindowMgrAttr
, &sUMAWindowManagerAttr
) == noErr
)
128 sUMAHasWindowManager
= sUMAWindowManagerAttr
& gestaltWindowMgrPresent
;
132 // Call currently implicitely done : InitFloatingWindows() ;
136 if ( sUMAHasWindowManager
)
137 InitFloatingWindows() ;
143 if ( NavServicesAvailable() )
149 Gestalt( gestaltMenuMgrAttr
, &menuMgrAttr
) ;
150 if ( menuMgrAttr
& gestaltMenuMgrAquaLayoutMask
)
151 sUMAHasAquaLayout
= true ;
153 if ( TXNInitTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
155 FontFamilyID fontId
;
159 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
160 GetFNum( fontName
, &fontId
);
162 TXNMacOSPreferredFontDescription fontDescriptions
[] =
164 { fontId
, (fontSize
<< 16) ,kTXNDefaultFontStyle
, kTXNSystemDefaultEncoding
}
166 int noOfFontDescriptions
= sizeof( fontDescriptions
) / sizeof(TXNMacOSPreferredFontDescription
) ;
167 #if 0 // TARGET_CARBON
168 --noOfFontDescriptions
;
170 // kTXNAlwaysUseQuickDrawTextMask might be desirable because of speed increases but it crashes the app under OS X upon key stroke
171 OptionBits options
= kTXNWantMoviesMask
| kTXNWantSoundMask
| kTXNWantGraphicsMask
;
173 if ( !UMAHasAquaLayout() )
176 options
|= kTXNAlwaysUseQuickDrawTextMask
;
178 TXNInitTextension(fontDescriptions
, noOfFontDescriptions
, options
);
182 UMASetSystemIsInitialized(true);
187 Boolean CanUseATSUI()
190 OSErr err = Gestalt(gestaltATSUVersion, &result);
191 return (err == noErr);
195 long UMAGetProcessMode()
198 ProcessInfoRec processinfo
;
199 ProcessSerialNumber procno
;
201 procno
.highLongOfPSN
= NULL
;
202 procno
.lowLongOfPSN
= kCurrentProcess
;
203 processinfo
.processInfoLength
= sizeof(ProcessInfoRec
);
204 processinfo
.processName
= NULL
;
205 processinfo
.processAppSpec
= NULL
;
207 err
= ::GetProcessInformation( &procno
, &processinfo
) ;
208 wxASSERT( err
== noErr
) ;
209 return processinfo
.processMode
;
212 bool UMAGetProcessModeDoesActivateOnFGSwitch()
214 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch
;
219 MenuRef
UMANewMenu( SInt16 id
, const wxString
& title
)
221 wxString str
= wxStripMenuCodes( title
) ;
224 CreateNewMenu( id
, 0 , &menu
) ;
225 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
) ) ;
228 wxMacStringToPascal( str
, ptitle
) ;
229 menu
= ::NewMenu( id
, ptitle
) ;
234 void UMASetMenuTitle( MenuRef menu
, const wxString
& title
)
236 wxString str
= wxStripMenuCodes( title
) ;
238 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
) ) ;
241 wxMacStringToPascal( str
, ptitle
) ;
242 SetMenuTitle( menu
, ptitle
) ;
246 void UMASetMenuItemText( MenuRef menu
, MenuItemIndex item
, const wxString
& title
)
248 wxString str
= wxStripMenuCodes( title
) ;
250 SetMenuItemTextWithCFString( menu
, item
, wxMacCFStringHolder(str
) ) ;
253 wxMacStringToPascal( str
, ptitle
) ;
254 SetMenuItemText( menu
, item
, ptitle
) ;
259 UInt32
UMAMenuEvent( EventRecord
*inEvent
)
261 return MenuEvent( inEvent
) ;
264 void UMAEnableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
, bool enable
)
267 EnableMenuItem( inMenu
, inItem
) ;
269 DisableMenuItem( inMenu
, inItem
) ;
272 void UMAAppendSubMenuItem( MenuRef menu
, const wxString
& title
, SInt16 id
)
274 MacAppendMenu(menu
, "\pA");
275 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
);
276 SetMenuItemHierarchicalID( menu
, CountMenuItems( menu
) , id
) ;
279 void UMAInsertSubMenuItem( MenuRef menu
, const wxString
& title
, MenuItemIndex item
, SInt16 id
)
281 MacInsertMenuItem(menu
, "\pA" , item
);
282 UMASetMenuItemText(menu
, item
, title
);
283 SetMenuItemHierarchicalID( menu
, item
, id
) ;
286 void UMASetMenuItemShortcut( MenuRef menu
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
291 UInt8 modifiers
= 0 ;
292 SInt16 key
= entry
->GetKeyCode() ;
295 bool explicitCommandKey
= false ;
297 if ( entry
->GetFlags() & wxACCEL_CTRL
)
299 explicitCommandKey
= true ;
302 if (entry
->GetFlags() & wxACCEL_ALT
)
304 modifiers
|= kMenuOptionModifier
;
307 if (entry
->GetFlags() & wxACCEL_SHIFT
)
309 modifiers
|= kMenuShiftModifier
;
313 SInt16 macKey
= key
;
314 if ( key
>= WXK_F1
&& key
<= WXK_F15
)
316 // for some reasons this must be 0 right now
317 // everything else leads to just the first function key item
318 // to be selected. Thanks to Ryan Wilcox for finding out.
320 glyph
= kMenuF1Glyph
+ ( key
- WXK_F1
) ;
321 if ( key
>= WXK_F13
)
323 if ( !explicitCommandKey
)
324 modifiers
|= kMenuNoCommandModifier
;
331 macKey
= kBackspaceCharCode
;
332 glyph
= kMenuDeleteLeftGlyph
;
335 macKey
= kTabCharCode
;
336 glyph
= kMenuTabRightGlyph
;
338 case kEnterCharCode
:
339 macKey
= kEnterCharCode
;
340 glyph
= kMenuEnterGlyph
;
343 macKey
= kReturnCharCode
;
344 glyph
= kMenuReturnGlyph
;
347 macKey
= kEscapeCharCode
;
348 glyph
= kMenuEscapeGlyph
;
352 glyph
= kMenuSpaceGlyph
;
355 macKey
= kDeleteCharCode
;
356 glyph
= kMenuDeleteRightGlyph
;
359 macKey
= kClearCharCode
;
360 glyph
= kMenuClearGlyph
;
362 case WXK_PRIOR
: // PAGE UP
363 macKey
= kPageUpCharCode
;
364 glyph
= kMenuPageUpGlyph
;
367 macKey
= kPageDownCharCode
;
368 glyph
= kMenuPageDownGlyph
;
371 macKey
= kLeftArrowCharCode
;
372 glyph
= kMenuLeftArrowGlyph
;
375 macKey
= kUpArrowCharCode
;
376 glyph
= kMenuUpArrowGlyph
;
379 macKey
= kRightArrowCharCode
;
380 glyph
= kMenuRightArrowGlyph
;
383 macKey
= kDownArrowCharCode
;
384 glyph
= kMenuDownArrowGlyph
;
389 SetItemCmd( menu
, item
, macKey
);
390 SetMenuItemModifiers(menu
, item
, modifiers
) ;
393 SetMenuItemKeyGlyph(menu
, item
, glyph
) ;
397 void UMAAppendMenuItem( MenuRef menu
, const wxString
& title
, wxAcceleratorEntry
*entry
)
399 MacAppendMenu(menu
, "\pA");
400 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
);
401 UMASetMenuItemShortcut( menu
, (SInt16
) ::CountMenuItems(menu
), entry
) ;
404 void UMAInsertMenuItem( MenuRef menu
, const wxString
& title
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
406 MacInsertMenuItem( menu
, "\pA" , item
) ;
407 UMASetMenuItemText(menu
, item
+1 , title
);
408 UMASetMenuItemShortcut( menu
, item
+1 , entry
) ;
415 int gPrOpenCounter
= 0 ;
421 if ( gPrOpenCounter
== 1 )
425 wxASSERT( err
== noErr
) ;
430 OSStatus
UMAPrClose()
433 wxASSERT( gPrOpenCounter
>= 1 ) ;
434 if ( gPrOpenCounter
== 1 )
438 wxASSERT( err
== noErr
) ;
444 pascal QDGlobalsPtr
GetQDGlobalsPtr (void) ;
445 pascal QDGlobalsPtr
GetQDGlobalsPtr (void)
447 return QDGlobalsPtr (* (Ptr
*) LMGetCurrentA5 ( ) - 0xCA);
452 void UMAShowWatchCursor()
456 CursHandle watchFob
= GetCursor (watchCursor
);
463 // Cursor preservedArrow;
464 // GetQDGlobalsArrow (&preservedArrow);
465 // SetQDGlobalsArrow (*watchFob);
467 // SetQDGlobalsArrow (&preservedArrow);
468 SetCursor (*watchFob
);
470 SetCursor (*watchFob
);
475 void UMAShowArrowCursor()
479 SetCursor (GetQDGlobalsArrow (&arrow
));
481 SetCursor (&(qd
.arrow
));
487 GrafPtr
UMAGetWindowPort( WindowRef inWindowRef
)
489 wxASSERT( inWindowRef
!= NULL
) ;
491 return (GrafPtr
) GetWindowPort( inWindowRef
) ;
493 return (GrafPtr
) inWindowRef
;
497 void UMADisposeWindow( WindowRef inWindowRef
)
499 wxASSERT( inWindowRef
!= NULL
) ;
500 DisposeWindow( inWindowRef
) ;
503 void UMASetWTitle( WindowRef inWindowRef
, const wxString
& title
)
506 SetWindowTitleWithCFString( inWindowRef
, wxMacCFStringHolder(title
) ) ;
509 wxMacStringToPascal( title
, ptitle
) ;
510 SetWTitle( inWindowRef
, ptitle
) ;
514 void UMAGetWTitleC( WindowRef inWindowRef
, char *title
)
516 GetWTitle( inWindowRef
, (unsigned char*)title
) ;
518 p2cstrcpy( title
, (unsigned char *)title
) ;
520 p2cstr( (unsigned char*)title
) ;
524 // appearance additions
526 void UMASetControlTitle( ControlHandle inControl
, const wxString
& title
)
529 SetControlTitleWithCFString( inControl
, wxMacCFStringHolder(title
) ) ;
532 wxMacStringToPascal( title
, ptitle
) ;
533 SetControlTitle( inControl
, ptitle
) ;
537 void UMAActivateControl( ControlHandle inControl
)
539 // we have to add the control after again to the update rgn
540 // otherwise updates get lost
541 if ( !IsControlActive( inControl
) )
543 bool visible
= IsControlVisible( inControl
) ;
545 SetControlVisibility( inControl
, false , false ) ;
546 ::ActivateControl( inControl
) ;
548 SetControlVisibility( inControl
, true , false ) ;
550 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
555 void UMADrawControl( ControlHandle inControl
)
557 WindowRef theWindow
= GetControlOwner(inControl
) ;
558 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort(theWindow
) ) ;
559 RgnHandle updateRgn
= NewRgn() ;
560 GetWindowUpdateRgn( theWindow
, updateRgn
) ;
561 Point zero
= { 0 , 0 } ;
562 LocalToGlobal( &zero
) ;
563 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
564 ::DrawControlInCurrentPort( inControl
) ;
565 InvalWindowRgn( theWindow
, updateRgn
) ;
566 DisposeRgn( updateRgn
) ;
569 void UMAMoveControl( ControlHandle inControl
, short x
, short y
)
571 bool visible
= IsControlVisible( inControl
) ;
573 SetControlVisibility( inControl
, false , false ) ;
575 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
577 ::MoveControl( inControl
, x
, y
) ;
579 SetControlVisibility( inControl
, true , false ) ;
581 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
585 void UMASizeControl( ControlHandle inControl
, short x
, short y
)
587 bool visible
= IsControlVisible( inControl
) ;
589 SetControlVisibility( inControl
, false , false ) ;
591 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
593 ::SizeControl( inControl
, x
, y
) ;
595 SetControlVisibility( inControl
, true , false ) ;
597 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
601 void UMADeactivateControl( ControlHandle inControl
)
603 // we have to add the control after again to the update rgn
604 // otherwise updates get lost
605 bool visible
= IsControlVisible( inControl
) ;
607 SetControlVisibility( inControl
, false , false ) ;
608 ::DeactivateControl( inControl
) ;
610 SetControlVisibility( inControl
, true , false ) ;
612 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
615 // shows the control and adds the region to the update region
616 void UMAShowControl (ControlHandle inControl
)
618 SetControlVisibility( inControl
, true , false ) ;
620 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
623 // shows the control and adds the region to the update region
624 void UMAHideControl (ControlHandle inControl
)
626 SetControlVisibility( inControl
, false , false ) ;
628 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
631 OSErr
UMASetKeyboardFocus (WindowPtr inWindow
,
632 ControlHandle inControl
,
633 ControlFocusPart inPart
)
639 SetPortWindowPort( inWindow
) ;
641 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
648 void UMAUpdateControls( WindowPtr inWindow
, RgnHandle inRgn
)
650 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) inWindow
) ) ;
651 RgnHandle updateRgn
= NewRgn() ;
652 GetWindowUpdateRgn( inWindow
, updateRgn
) ;
654 Point zero
= { 0 , 0 } ;
655 LocalToGlobal( &zero
) ;
656 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
658 UpdateControls( inWindow
, inRgn
) ;
659 InvalWindowRgn( inWindow
, updateRgn
) ;
660 DisposeRgn( updateRgn
) ;
663 bool UMAIsWindowFloating( WindowRef inWindow
)
667 GetWindowClass( inWindow
, &cl
) ;
668 return cl
== kFloatingWindowClass
;
671 bool UMAIsWindowModal( WindowRef inWindow
)
675 GetWindowClass( inWindow
, &cl
) ;
676 return cl
< kFloatingWindowClass
;
681 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
685 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
686 // if ( inActivate != isHightlited )
689 SetPortWindowPort( inWindowRef
) ;
690 HiliteWindow( inWindowRef
, inActivate
) ;
691 ControlHandle control
= NULL
;
692 ::GetRootControl( inWindowRef
, & control
) ;
696 UMAActivateControl( control
) ;
698 UMADeactivateControl( control
) ;
704 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
706 return ::DrawThemePlacard( inRect
, inState
) ;
710 static OSStatus helpMenuStatus
= noErr
;
711 static MenuItemIndex firstCustomItemIndex
= 0 ;
714 OSStatus
UMAGetHelpMenu(
715 MenuRef
* outHelpMenu
,
716 MenuItemIndex
* outFirstCustomItemIndex
)
719 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
721 MenuRef helpMenuHandle
;
722 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
723 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
725 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
727 if ( outFirstCustomItemIndex
)
729 *outFirstCustomItemIndex
= firstCustomItemIndex
;
731 *outHelpMenu
= helpMenuHandle
;
732 return helpMenuStatus
;
736 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
742 wxMacPortStateHelper::wxMacPortStateHelper()
747 void wxMacPortStateHelper::Setup( GrafPtr newport
)
749 GetPort( &m_oldPort
) ;
752 wxASSERT_MSG( m_clip
== NULL
, wxT("Cannot call setup twice") ) ;
755 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
756 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
757 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
758 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
759 GetThemeDrawingState( &m_drawingState
) ;
760 m_currentPort
= newport
;
762 void wxMacPortStateHelper::Clear()
766 DisposeRgn( m_clip
) ;
767 DisposeThemeDrawingState( m_drawingState
) ;
772 wxMacPortStateHelper::~wxMacPortStateHelper()
776 SetPort( m_currentPort
) ;
778 DisposeRgn( m_clip
) ;
779 TextFont( m_textFont
);
780 TextSize( m_textSize
);
781 TextFace( m_textStyle
);
782 TextMode( m_textMode
);
783 SetThemeDrawingState( m_drawingState
, true ) ;
784 SetPort( m_oldPort
) ;
788 OSStatus
UMAPutScrap( Size size
, OSType type
, void *data
)
790 OSStatus err
= noErr
;
792 err
= PutScrap( size
, type
, data
) ;
795 err
= GetCurrentScrap (&scrap
);
798 err
= PutScrapFlavor (scrap
, type
, 0, size
, data
);
808 static bool sUMASystemInitialized
= false ;
810 bool UMASystemIsInitialized()
812 return sUMASystemInitialized
;
815 void UMASetSystemIsInitialized(bool val
)
817 sUMASystemInitialized
= val
;