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 RgnHandle updateRgn
= NewRgn() ;
559 GetWindowUpdateRgn( theWindow
, updateRgn
) ;
560 Point zero
= { 0 , 0 } ;
561 LocalToGlobal( &zero
) ;
562 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
563 ::DrawControlInCurrentPort( inControl
) ;
564 InvalWindowRgn( theWindow
, updateRgn
) ;
565 DisposeRgn( updateRgn
) ;
568 void UMAMoveControl( ControlHandle inControl
, short x
, short y
)
570 bool visible
= IsControlVisible( inControl
) ;
572 SetControlVisibility( inControl
, false , false ) ;
574 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
576 ::MoveControl( inControl
, x
, y
) ;
578 SetControlVisibility( inControl
, true , false ) ;
580 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
584 void UMASizeControl( ControlHandle inControl
, short x
, short y
)
586 bool visible
= IsControlVisible( inControl
) ;
588 SetControlVisibility( inControl
, false , false ) ;
590 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
592 ::SizeControl( inControl
, x
, y
) ;
594 SetControlVisibility( inControl
, true , false ) ;
596 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
600 void UMADeactivateControl( ControlHandle inControl
)
602 // we have to add the control after again to the update rgn
603 // otherwise updates get lost
604 bool visible
= IsControlVisible( inControl
) ;
606 SetControlVisibility( inControl
, false , false ) ;
607 ::DeactivateControl( inControl
) ;
609 SetControlVisibility( inControl
, true , false ) ;
611 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
614 // shows the control and adds the region to the update region
615 void UMAShowControl (ControlHandle inControl
)
617 SetControlVisibility( inControl
, true , false ) ;
619 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
622 // shows the control and adds the region to the update region
623 void UMAHideControl (ControlHandle inControl
)
625 SetControlVisibility( inControl
, false , false ) ;
627 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
630 OSErr
UMASetKeyboardFocus (WindowPtr inWindow
,
631 ControlHandle inControl
,
632 ControlFocusPart inPart
)
638 SetPortWindowPort( inWindow
) ;
640 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
647 void UMAUpdateControls( WindowPtr inWindow
, RgnHandle inRgn
)
649 RgnHandle updateRgn
= NewRgn() ;
650 GetWindowUpdateRgn( inWindow
, updateRgn
) ;
652 Point zero
= { 0 , 0 } ;
653 LocalToGlobal( &zero
) ;
654 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
656 UpdateControls( inWindow
, inRgn
) ;
657 InvalWindowRgn( inWindow
, updateRgn
) ;
658 DisposeRgn( updateRgn
) ;
661 bool UMAIsWindowFloating( WindowRef inWindow
)
665 GetWindowClass( inWindow
, &cl
) ;
666 return cl
== kFloatingWindowClass
;
669 bool UMAIsWindowModal( WindowRef inWindow
)
673 GetWindowClass( inWindow
, &cl
) ;
674 return cl
< kFloatingWindowClass
;
679 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
683 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
684 // if ( inActivate != isHightlited )
687 SetPortWindowPort( inWindowRef
) ;
688 HiliteWindow( inWindowRef
, inActivate
) ;
689 ControlHandle control
= NULL
;
690 ::GetRootControl( inWindowRef
, & control
) ;
694 UMAActivateControl( control
) ;
696 UMADeactivateControl( control
) ;
702 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
704 return ::DrawThemePlacard( inRect
, inState
) ;
708 static OSStatus helpMenuStatus
= noErr
;
709 static MenuItemIndex firstCustomItemIndex
= 0 ;
712 OSStatus
UMAGetHelpMenu(
713 MenuRef
* outHelpMenu
,
714 MenuItemIndex
* outFirstCustomItemIndex
)
717 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
719 MenuRef helpMenuHandle
;
720 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
721 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
723 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
725 if ( outFirstCustomItemIndex
)
727 *outFirstCustomItemIndex
= firstCustomItemIndex
;
729 *outHelpMenu
= helpMenuHandle
;
730 return helpMenuStatus
;
734 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
740 wxMacPortStateHelper::wxMacPortStateHelper()
745 void wxMacPortStateHelper::Setup( GrafPtr newport
)
747 GetPort( &m_oldPort
) ;
749 wxASSERT_MSG( m_clip
== NULL
, wxT("Cannot call setup twice") ) ;
752 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
753 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
754 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
755 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
756 GetThemeDrawingState( &m_drawingState
) ;
757 m_currentPort
= newport
;
759 void wxMacPortStateHelper::Clear()
763 DisposeRgn( m_clip
) ;
764 DisposeThemeDrawingState( m_drawingState
) ;
769 wxMacPortStateHelper::~wxMacPortStateHelper()
773 SetPort( m_currentPort
) ;
775 DisposeRgn( m_clip
) ;
776 TextFont( m_textFont
);
777 TextSize( m_textSize
);
778 TextFace( m_textStyle
);
779 TextMode( m_textMode
);
780 SetThemeDrawingState( m_drawingState
, true ) ;
781 SetPort( m_oldPort
) ;
785 OSStatus
UMAPutScrap( Size size
, OSType type
, void *data
)
787 OSStatus err
= noErr
;
789 err
= PutScrap( size
, type
, data
) ;
792 err
= GetCurrentScrap (&scrap
);
795 err
= PutScrapFlavor (scrap
, type
, 0, size
, data
);
805 static bool sUMASystemInitialized
= false ;
807 bool UMASystemIsInitialized()
809 return sUMASystemInitialized
;
812 void UMASetSystemIsInitialized(bool val
)
814 sUMASystemInitialized
= val
;