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
) ;
168 // kTXNAlwaysUseQuickDrawTextMask might be desirable because of speed increases but it crashes the app under OS X upon key stroke
170 // leads to unexpected content for clients, TODO configurable
171 OptionBits options
= kTXNWantMoviesMask
| kTXNWantSoundMask
| kTXNWantGraphicsMask
;
173 OptionBits options
= 0 ;
177 if ( !UMAHasAquaLayout() )
180 options
|= kTXNAlwaysUseQuickDrawTextMask
;
182 TXNInitTextension(fontDescriptions
, noOfFontDescriptions
, options
);
186 UMASetSystemIsInitialized(true);
191 Boolean CanUseATSUI()
194 OSErr err = Gestalt(gestaltATSUVersion, &result);
195 return (err == noErr);
199 long UMAGetProcessMode()
202 ProcessInfoRec processinfo
;
203 ProcessSerialNumber procno
;
205 procno
.highLongOfPSN
= NULL
;
206 procno
.lowLongOfPSN
= kCurrentProcess
;
207 processinfo
.processInfoLength
= sizeof(ProcessInfoRec
);
208 processinfo
.processName
= NULL
;
209 processinfo
.processAppSpec
= NULL
;
211 err
= ::GetProcessInformation( &procno
, &processinfo
) ;
212 wxASSERT( err
== noErr
) ;
213 return processinfo
.processMode
;
216 bool UMAGetProcessModeDoesActivateOnFGSwitch()
218 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch
;
223 MenuRef
UMANewMenu( SInt16 id
, const wxString
& title
, wxFontEncoding encoding
)
225 wxString str
= wxStripMenuCodes( title
) ;
228 CreateNewMenu( id
, 0 , &menu
) ;
229 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
, encoding
) ) ;
232 wxMacStringToPascal( str
, ptitle
) ;
233 menu
= ::NewMenu( id
, ptitle
) ;
238 void UMASetMenuTitle( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
)
240 wxString str
= wxStripMenuCodes( title
) ;
242 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
, encoding
) ) ;
245 wxMacStringToPascal( str
, ptitle
) ;
246 SetMenuTitle( menu
, ptitle
) ;
250 void UMASetMenuItemText( MenuRef menu
, MenuItemIndex item
, const wxString
& title
, wxFontEncoding encoding
)
252 wxString str
= wxStripMenuCodes( title
) ;
254 SetMenuItemTextWithCFString( menu
, item
, wxMacCFStringHolder(str
, encoding
) ) ;
257 wxMacStringToPascal( str
, ptitle
) ;
258 SetMenuItemText( menu
, item
, ptitle
) ;
263 UInt32
UMAMenuEvent( EventRecord
*inEvent
)
265 return MenuEvent( inEvent
) ;
268 void UMAEnableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
, bool enable
)
271 EnableMenuItem( inMenu
, inItem
) ;
273 DisableMenuItem( inMenu
, inItem
) ;
276 void UMAAppendSubMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, SInt16 id
)
278 MacAppendMenu(menu
, "\pA");
279 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
, encoding
);
280 SetMenuItemHierarchicalID( menu
, CountMenuItems( menu
) , id
) ;
283 void UMAInsertSubMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, MenuItemIndex item
, SInt16 id
)
285 MacInsertMenuItem(menu
, "\pA" , item
);
286 UMASetMenuItemText(menu
, item
, title
, encoding
);
287 SetMenuItemHierarchicalID( menu
, item
, id
) ;
290 void UMASetMenuItemShortcut( MenuRef menu
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
295 UInt8 modifiers
= 0 ;
296 SInt16 key
= entry
->GetKeyCode() ;
299 bool explicitCommandKey
= false ;
301 if ( entry
->GetFlags() & wxACCEL_CTRL
)
303 explicitCommandKey
= true ;
306 if (entry
->GetFlags() & wxACCEL_ALT
)
308 modifiers
|= kMenuOptionModifier
;
311 if (entry
->GetFlags() & wxACCEL_SHIFT
)
313 modifiers
|= kMenuShiftModifier
;
317 SInt16 macKey
= key
;
318 if ( key
>= WXK_F1
&& key
<= WXK_F15
)
320 // for some reasons this must be 0 right now
321 // everything else leads to just the first function key item
322 // to be selected. Thanks to Ryan Wilcox for finding out.
324 glyph
= kMenuF1Glyph
+ ( key
- WXK_F1
) ;
325 if ( key
>= WXK_F13
)
327 if ( !explicitCommandKey
)
328 modifiers
|= kMenuNoCommandModifier
;
335 macKey
= kBackspaceCharCode
;
336 glyph
= kMenuDeleteLeftGlyph
;
339 macKey
= kTabCharCode
;
340 glyph
= kMenuTabRightGlyph
;
342 case kEnterCharCode
:
343 macKey
= kEnterCharCode
;
344 glyph
= kMenuEnterGlyph
;
347 macKey
= kReturnCharCode
;
348 glyph
= kMenuReturnGlyph
;
351 macKey
= kEscapeCharCode
;
352 glyph
= kMenuEscapeGlyph
;
356 glyph
= kMenuSpaceGlyph
;
359 macKey
= kDeleteCharCode
;
360 glyph
= kMenuDeleteRightGlyph
;
363 macKey
= kClearCharCode
;
364 glyph
= kMenuClearGlyph
;
366 case WXK_PRIOR
: // PAGE UP
367 macKey
= kPageUpCharCode
;
368 glyph
= kMenuPageUpGlyph
;
371 macKey
= kPageDownCharCode
;
372 glyph
= kMenuPageDownGlyph
;
375 macKey
= kLeftArrowCharCode
;
376 glyph
= kMenuLeftArrowGlyph
;
379 macKey
= kUpArrowCharCode
;
380 glyph
= kMenuUpArrowGlyph
;
383 macKey
= kRightArrowCharCode
;
384 glyph
= kMenuRightArrowGlyph
;
387 macKey
= kDownArrowCharCode
;
388 glyph
= kMenuDownArrowGlyph
;
393 SetItemCmd( menu
, item
, macKey
);
394 SetMenuItemModifiers(menu
, item
, modifiers
) ;
397 SetMenuItemKeyGlyph(menu
, item
, glyph
) ;
401 void UMAAppendMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, wxAcceleratorEntry
*entry
)
403 MacAppendMenu(menu
, "\pA");
404 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
, encoding
);
405 UMASetMenuItemShortcut( menu
, (SInt16
) ::CountMenuItems(menu
), entry
) ;
408 void UMAInsertMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
410 MacInsertMenuItem( menu
, "\pA" , item
) ;
411 UMASetMenuItemText(menu
, item
+1 , title
, encoding
);
412 UMASetMenuItemShortcut( menu
, item
+1 , entry
) ;
419 int gPrOpenCounter
= 0 ;
425 if ( gPrOpenCounter
== 1 )
429 wxASSERT( err
== noErr
) ;
434 OSStatus
UMAPrClose()
437 wxASSERT( gPrOpenCounter
>= 1 ) ;
438 if ( gPrOpenCounter
== 1 )
442 wxASSERT( err
== noErr
) ;
448 pascal QDGlobalsPtr
GetQDGlobalsPtr (void) ;
449 pascal QDGlobalsPtr
GetQDGlobalsPtr (void)
451 return QDGlobalsPtr (* (Ptr
*) LMGetCurrentA5 ( ) - 0xCA);
456 void UMAShowWatchCursor()
460 CursHandle watchFob
= GetCursor (watchCursor
);
467 // Cursor preservedArrow;
468 // GetQDGlobalsArrow (&preservedArrow);
469 // SetQDGlobalsArrow (*watchFob);
471 // SetQDGlobalsArrow (&preservedArrow);
472 SetCursor (*watchFob
);
474 SetCursor (*watchFob
);
479 void UMAShowArrowCursor()
483 SetCursor (GetQDGlobalsArrow (&arrow
));
485 SetCursor (&(qd
.arrow
));
491 GrafPtr
UMAGetWindowPort( WindowRef inWindowRef
)
493 wxASSERT( inWindowRef
!= NULL
) ;
495 return (GrafPtr
) GetWindowPort( inWindowRef
) ;
497 return (GrafPtr
) inWindowRef
;
501 void UMADisposeWindow( WindowRef inWindowRef
)
503 wxASSERT( inWindowRef
!= NULL
) ;
504 DisposeWindow( inWindowRef
) ;
507 void UMASetWTitle( WindowRef inWindowRef
, const wxString
& title
, wxFontEncoding encoding
)
510 SetWindowTitleWithCFString( inWindowRef
, wxMacCFStringHolder(title
, encoding
) ) ;
513 wxMacStringToPascal( title
, ptitle
) ;
514 SetWTitle( inWindowRef
, ptitle
) ;
518 // appearance additions
520 void UMASetControlTitle( ControlHandle inControl
, const wxString
& title
, wxFontEncoding encoding
)
523 SetControlTitleWithCFString( inControl
, wxMacCFStringHolder(title
, encoding
) ) ;
526 wxMacStringToPascal( title
, ptitle
) ;
527 SetControlTitle( inControl
, ptitle
) ;
531 void UMAActivateControl( ControlHandle inControl
)
533 // we have to add the control after again to the update rgn
534 // otherwise updates get lost
535 if ( !IsControlActive( inControl
) )
537 bool visible
= IsControlVisible( inControl
) ;
539 SetControlVisibility( inControl
, false , false ) ;
540 ::ActivateControl( inControl
) ;
542 SetControlVisibility( inControl
, true , false ) ;
544 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
549 void UMADrawControl( ControlHandle inControl
)
551 WindowRef theWindow
= GetControlOwner(inControl
) ;
552 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort(theWindow
) ) ;
553 RgnHandle updateRgn
= NewRgn() ;
554 GetWindowUpdateRgn( theWindow
, updateRgn
) ;
555 Point zero
= { 0 , 0 } ;
556 LocalToGlobal( &zero
) ;
557 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
558 ::DrawControlInCurrentPort( inControl
) ;
559 InvalWindowRgn( theWindow
, updateRgn
) ;
560 DisposeRgn( updateRgn
) ;
563 void UMAMoveControl( ControlHandle inControl
, short x
, short y
)
565 bool visible
= IsControlVisible( inControl
) ;
567 SetControlVisibility( inControl
, false , false ) ;
569 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
571 ::MoveControl( inControl
, x
, y
) ;
573 SetControlVisibility( inControl
, true , false ) ;
575 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
579 void UMASizeControl( ControlHandle inControl
, short x
, short y
)
581 bool visible
= IsControlVisible( inControl
) ;
583 SetControlVisibility( inControl
, false , false ) ;
585 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
587 ::SizeControl( inControl
, x
, y
) ;
589 SetControlVisibility( inControl
, true , false ) ;
591 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
595 void UMADeactivateControl( ControlHandle inControl
)
597 // we have to add the control after again to the update rgn
598 // otherwise updates get lost
599 bool visible
= IsControlVisible( inControl
) ;
601 SetControlVisibility( inControl
, false , false ) ;
602 ::DeactivateControl( inControl
) ;
604 SetControlVisibility( inControl
, true , false ) ;
606 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
609 // shows the control and adds the region to the update region
610 void UMAShowControl (ControlHandle inControl
)
612 SetControlVisibility( inControl
, true , false ) ;
614 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
617 // shows the control and adds the region to the update region
618 void UMAHideControl (ControlHandle inControl
)
620 SetControlVisibility( inControl
, false , false ) ;
622 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
625 OSErr
UMASetKeyboardFocus (WindowPtr inWindow
,
626 ControlHandle inControl
,
627 ControlFocusPart inPart
)
633 SetPortWindowPort( inWindow
) ;
635 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
642 void UMAUpdateControls( WindowPtr inWindow
, RgnHandle inRgn
)
644 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) inWindow
) ) ;
645 RgnHandle updateRgn
= NewRgn() ;
646 GetWindowUpdateRgn( inWindow
, updateRgn
) ;
648 Point zero
= { 0 , 0 } ;
649 LocalToGlobal( &zero
) ;
650 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
652 UpdateControls( inWindow
, inRgn
) ;
653 InvalWindowRgn( inWindow
, updateRgn
) ;
654 DisposeRgn( updateRgn
) ;
657 bool UMAIsWindowFloating( WindowRef inWindow
)
661 GetWindowClass( inWindow
, &cl
) ;
662 return cl
== kFloatingWindowClass
;
665 bool UMAIsWindowModal( WindowRef inWindow
)
669 GetWindowClass( inWindow
, &cl
) ;
670 return cl
< kFloatingWindowClass
;
675 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
679 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
680 // if ( inActivate != isHightlited )
683 SetPortWindowPort( inWindowRef
) ;
684 HiliteWindow( inWindowRef
, inActivate
) ;
685 ControlHandle control
= NULL
;
686 ::GetRootControl( inWindowRef
, & control
) ;
690 UMAActivateControl( control
) ;
692 UMADeactivateControl( control
) ;
698 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
700 return ::DrawThemePlacard( inRect
, inState
) ;
704 static OSStatus helpMenuStatus
= noErr
;
705 static MenuItemIndex firstCustomItemIndex
= 0 ;
708 OSStatus
UMAGetHelpMenu(
709 MenuRef
* outHelpMenu
,
710 MenuItemIndex
* outFirstCustomItemIndex
)
713 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
715 MenuRef helpMenuHandle
;
716 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
717 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
719 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
721 if ( outFirstCustomItemIndex
)
723 *outFirstCustomItemIndex
= firstCustomItemIndex
;
725 *outHelpMenu
= helpMenuHandle
;
726 return helpMenuStatus
;
730 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
736 wxMacPortStateHelper::wxMacPortStateHelper()
741 void wxMacPortStateHelper::Setup( GrafPtr newport
)
743 GetPort( &m_oldPort
) ;
746 wxASSERT_MSG( m_clip
== NULL
, wxT("Cannot call setup twice") ) ;
749 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
750 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
751 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
752 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
753 GetThemeDrawingState( &m_drawingState
) ;
754 m_currentPort
= newport
;
756 void wxMacPortStateHelper::Clear()
760 DisposeRgn( m_clip
) ;
761 DisposeThemeDrawingState( m_drawingState
) ;
766 wxMacPortStateHelper::~wxMacPortStateHelper()
770 SetPort( m_currentPort
) ;
772 DisposeRgn( m_clip
) ;
773 TextFont( m_textFont
);
774 TextSize( m_textSize
);
775 TextFace( m_textStyle
);
776 TextMode( m_textMode
);
777 SetThemeDrawingState( m_drawingState
, true ) ;
778 SetPort( m_oldPort
) ;
782 OSStatus
UMAPutScrap( Size size
, OSType type
, void *data
)
784 OSStatus err
= noErr
;
786 err
= PutScrap( size
, type
, data
) ;
789 err
= GetCurrentScrap (&scrap
);
792 err
= PutScrapFlavor (scrap
, type
, 0, size
, data
);
802 static bool sUMASystemInitialized
= false ;
804 bool UMASystemIsInitialized()
806 return sUMASystemInitialized
;
809 void UMASetSystemIsInitialized(bool val
)
811 sUMASystemInitialized
= val
;