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
, wxFontEncoding encoding
)
221 wxString str
= wxStripMenuCodes( title
) ;
224 CreateNewMenu( id
, 0 , &menu
) ;
225 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
, encoding
) ) ;
228 wxMacStringToPascal( str
, ptitle
) ;
229 menu
= ::NewMenu( id
, ptitle
) ;
234 void UMASetMenuTitle( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
)
236 wxString str
= wxStripMenuCodes( title
) ;
238 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
, encoding
) ) ;
241 wxMacStringToPascal( str
, ptitle
) ;
242 SetMenuTitle( menu
, ptitle
) ;
246 void UMASetMenuItemText( MenuRef menu
, MenuItemIndex item
, const wxString
& title
, wxFontEncoding encoding
)
248 wxString str
= wxStripMenuCodes( title
) ;
250 SetMenuItemTextWithCFString( menu
, item
, wxMacCFStringHolder(str
, encoding
) ) ;
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
, wxFontEncoding encoding
, SInt16 id
)
274 MacAppendMenu(menu
, "\pA");
275 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
, encoding
);
276 SetMenuItemHierarchicalID( menu
, CountMenuItems( menu
) , id
) ;
279 void UMAInsertSubMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, MenuItemIndex item
, SInt16 id
)
281 MacInsertMenuItem(menu
, "\pA" , item
);
282 UMASetMenuItemText(menu
, item
, title
, encoding
);
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
, wxFontEncoding encoding
, wxAcceleratorEntry
*entry
)
399 MacAppendMenu(menu
, "\pA");
400 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
, encoding
);
401 UMASetMenuItemShortcut( menu
, (SInt16
) ::CountMenuItems(menu
), entry
) ;
404 void UMAInsertMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
406 MacInsertMenuItem( menu
, "\pA" , item
) ;
407 UMASetMenuItemText(menu
, item
+1 , title
, encoding
);
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
, wxFontEncoding encoding
)
506 SetWindowTitleWithCFString( inWindowRef
, wxMacCFStringHolder(title
, encoding
) ) ;
509 wxMacStringToPascal( title
, ptitle
) ;
510 SetWTitle( inWindowRef
, ptitle
) ;
514 // appearance additions
516 void UMASetControlTitle( ControlHandle inControl
, const wxString
& title
, wxFontEncoding encoding
)
519 SetControlTitleWithCFString( inControl
, wxMacCFStringHolder(title
, encoding
) ) ;
522 wxMacStringToPascal( title
, ptitle
) ;
523 SetControlTitle( inControl
, ptitle
) ;
527 void UMAActivateControl( ControlHandle inControl
)
529 // we have to add the control after again to the update rgn
530 // otherwise updates get lost
531 if ( !IsControlActive( inControl
) )
533 bool visible
= IsControlVisible( inControl
) ;
535 SetControlVisibility( inControl
, false , false ) ;
536 ::ActivateControl( inControl
) ;
538 SetControlVisibility( inControl
, true , false ) ;
540 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
545 void UMADrawControl( ControlHandle inControl
)
547 WindowRef theWindow
= GetControlOwner(inControl
) ;
548 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort(theWindow
) ) ;
549 RgnHandle updateRgn
= NewRgn() ;
550 GetWindowUpdateRgn( theWindow
, updateRgn
) ;
551 Point zero
= { 0 , 0 } ;
552 LocalToGlobal( &zero
) ;
553 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
554 ::DrawControlInCurrentPort( inControl
) ;
555 InvalWindowRgn( theWindow
, updateRgn
) ;
556 DisposeRgn( updateRgn
) ;
559 void UMAMoveControl( ControlHandle inControl
, short x
, short y
)
561 bool visible
= IsControlVisible( inControl
) ;
563 SetControlVisibility( inControl
, false , false ) ;
565 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
567 ::MoveControl( inControl
, x
, y
) ;
569 SetControlVisibility( inControl
, true , false ) ;
571 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
575 void UMASizeControl( ControlHandle inControl
, short x
, short y
)
577 bool visible
= IsControlVisible( inControl
) ;
579 SetControlVisibility( inControl
, false , false ) ;
581 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
583 ::SizeControl( inControl
, x
, y
) ;
585 SetControlVisibility( inControl
, true , false ) ;
587 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
591 void UMADeactivateControl( ControlHandle inControl
)
593 // we have to add the control after again to the update rgn
594 // otherwise updates get lost
595 bool visible
= IsControlVisible( inControl
) ;
597 SetControlVisibility( inControl
, false , false ) ;
598 ::DeactivateControl( inControl
) ;
600 SetControlVisibility( inControl
, true , false ) ;
602 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
605 // shows the control and adds the region to the update region
606 void UMAShowControl (ControlHandle inControl
)
608 SetControlVisibility( inControl
, true , false ) ;
610 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
613 // shows the control and adds the region to the update region
614 void UMAHideControl (ControlHandle inControl
)
616 SetControlVisibility( inControl
, false , false ) ;
618 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
621 OSErr
UMASetKeyboardFocus (WindowPtr inWindow
,
622 ControlHandle inControl
,
623 ControlFocusPart inPart
)
629 SetPortWindowPort( inWindow
) ;
631 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
638 void UMAUpdateControls( WindowPtr inWindow
, RgnHandle inRgn
)
640 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) inWindow
) ) ;
641 RgnHandle updateRgn
= NewRgn() ;
642 GetWindowUpdateRgn( inWindow
, updateRgn
) ;
644 Point zero
= { 0 , 0 } ;
645 LocalToGlobal( &zero
) ;
646 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
648 UpdateControls( inWindow
, inRgn
) ;
649 InvalWindowRgn( inWindow
, updateRgn
) ;
650 DisposeRgn( updateRgn
) ;
653 bool UMAIsWindowFloating( WindowRef inWindow
)
657 GetWindowClass( inWindow
, &cl
) ;
658 return cl
== kFloatingWindowClass
;
661 bool UMAIsWindowModal( WindowRef inWindow
)
665 GetWindowClass( inWindow
, &cl
) ;
666 return cl
< kFloatingWindowClass
;
671 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
675 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
676 // if ( inActivate != isHightlited )
679 SetPortWindowPort( inWindowRef
) ;
680 HiliteWindow( inWindowRef
, inActivate
) ;
681 ControlHandle control
= NULL
;
682 ::GetRootControl( inWindowRef
, & control
) ;
686 UMAActivateControl( control
) ;
688 UMADeactivateControl( control
) ;
694 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
696 return ::DrawThemePlacard( inRect
, inState
) ;
700 static OSStatus helpMenuStatus
= noErr
;
701 static MenuItemIndex firstCustomItemIndex
= 0 ;
704 OSStatus
UMAGetHelpMenu(
705 MenuRef
* outHelpMenu
,
706 MenuItemIndex
* outFirstCustomItemIndex
)
709 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
711 MenuRef helpMenuHandle
;
712 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
713 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
715 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
717 if ( outFirstCustomItemIndex
)
719 *outFirstCustomItemIndex
= firstCustomItemIndex
;
721 *outHelpMenu
= helpMenuHandle
;
722 return helpMenuStatus
;
726 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
732 wxMacPortStateHelper::wxMacPortStateHelper()
737 void wxMacPortStateHelper::Setup( GrafPtr newport
)
739 GetPort( &m_oldPort
) ;
742 wxASSERT_MSG( m_clip
== NULL
, wxT("Cannot call setup twice") ) ;
745 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
746 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
747 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
748 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
749 GetThemeDrawingState( &m_drawingState
) ;
750 m_currentPort
= newport
;
752 void wxMacPortStateHelper::Clear()
756 DisposeRgn( m_clip
) ;
757 DisposeThemeDrawingState( m_drawingState
) ;
762 wxMacPortStateHelper::~wxMacPortStateHelper()
766 SetPort( m_currentPort
) ;
768 DisposeRgn( m_clip
) ;
769 TextFont( m_textFont
);
770 TextSize( m_textSize
);
771 TextFace( m_textStyle
);
772 TextMode( m_textMode
);
773 SetThemeDrawingState( m_drawingState
, true ) ;
774 SetPort( m_oldPort
) ;
778 OSStatus
UMAPutScrap( Size size
, OSType type
, void *data
)
780 OSStatus err
= noErr
;
782 err
= PutScrap( size
, type
, data
) ;
785 err
= GetCurrentScrap (&scrap
);
788 err
= PutScrapFlavor (scrap
, type
, 0, size
, data
);
798 static bool sUMASystemInitialized
= false ;
800 bool UMASystemIsInitialized()
802 return sUMASystemInitialized
;
805 void UMASetSystemIsInitialized(bool val
)
807 sUMASystemInitialized
= val
;