1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: UMA support
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: The wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
14 #include <MacTextEditor.h>
17 # include <Navigation.h>
18 # if defined(TARGET_CARBON)
19 # if PM_USE_SESSION_APIS
22 # include <PMApplication.h>
24 # include <Printing.h>
31 #include "wx/mac/uma.h"
33 // since we have decided that we only support 8.6 upwards we are
34 // checking for these minimum requirements in the startup code of
35 // the application so all wxWindows code can safely assume that appearance 1.1
36 // windows manager, control manager, navigation services etc. are
39 static bool sUMAHasAppearance
= false ;
40 static long sUMAAppearanceVersion
= 0 ;
41 static long sUMASystemVersion
= 0 ;
42 static bool sUMAHasAquaLayout
= false ;
43 static bool sUMASystemInitialized
= false ;
45 extern int gAGABackgroundColor
;
46 bool UMAHasAppearance() { return sUMAHasAppearance
; }
47 long UMAGetAppearanceVersion() { return sUMAAppearanceVersion
; }
48 long UMAGetSystemVersion() { return sUMASystemVersion
; }
50 static bool sUMAHasWindowManager
= false ;
51 static long sUMAWindowManagerAttr
= 0 ;
53 bool UMAHasWindowManager() { return sUMAHasWindowManager
; }
54 long UMAGetWindowManagerAttr() { return sUMAWindowManagerAttr
; }
55 bool UMAHasAquaLayout() { return sUMAHasAquaLayout
; }
56 bool UMASystemIsInitialized() { return sUMASystemInitialized
; }
58 void UMACleanupToolbox()
60 if ( sUMAHasAppearance
)
62 UnregisterAppearanceClient() ;
64 if ( NavServicesAvailable() )
68 if ( TXNTerminateTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
69 TXNTerminateTextension( ) ;
71 void UMAInitToolbox( UInt16 inMoreMastersCalls
)
75 for (long i
= 1; i
<= inMoreMastersCalls
; i
++)
78 ::InitGraf(&qd
.thePort
);
83 ::FlushEvents(everyEvent
, 0);
86 PurgeSpace(&total
, &contig
);
91 if ( Gestalt(gestaltSystemVersion
, &sUMASystemVersion
) != noErr
)
92 sUMASystemVersion
= 0x0000 ;
95 if ( Gestalt( gestaltAppearanceAttr
, &theAppearance
) == noErr
)
97 sUMAHasAppearance
= true ;
98 RegisterAppearanceClient();
99 if ( Gestalt( gestaltAppearanceVersion
, &theAppearance
) == noErr
)
101 sUMAAppearanceVersion
= theAppearance
;
105 sUMAAppearanceVersion
= 0x0100 ;
108 if ( Gestalt( gestaltWindowMgrAttr
, &sUMAWindowManagerAttr
) == noErr
)
110 sUMAHasWindowManager
= sUMAWindowManagerAttr
& gestaltWindowMgrPresent
;
114 // Call currently implicitely done : InitFloatingWindows() ;
116 if ( sUMAHasWindowManager
)
117 InitFloatingWindows() ;
122 if ( NavServicesAvailable() )
128 Gestalt( gestaltMenuMgrAttr
, &menuMgrAttr
) ;
129 if ( menuMgrAttr
& gestaltMenuMgrAquaLayoutMask
)
130 sUMAHasAquaLayout
= true ;
132 if ( TXNInitTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
134 FontFamilyID fontId
;
138 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
139 GetFNum( fontName
, &fontId
);
141 TXNMacOSPreferredFontDescription fontDescriptions
[] =
143 { fontId
, (fontSize
<< 16) ,kTXNDefaultFontStyle
, kTXNSystemDefaultEncoding
} ,
145 int noOfFontDescriptions
= sizeof( fontDescriptions
) / sizeof(TXNMacOSPreferredFontDescription
) ;
146 #if 0 // TARGET_CARBON
147 --noOfFontDescriptions
;
149 // kTXNAlwaysUseQuickDrawTextMask might be desirable because of speed increases but it crashes the app under OS X upon key stroke
150 OptionBits options
= kTXNWantMoviesMask
| kTXNWantSoundMask
| kTXNWantGraphicsMask
;
152 if ( !UMAHasAquaLayout() )
155 options
|= kTXNAlwaysUseQuickDrawTextMask
;
157 TXNInitTextension(fontDescriptions
, noOfFontDescriptions
, options
);
161 sUMASystemInitialized
= true ;
166 Boolean CanUseATSUI()
169 OSErr err = Gestalt(gestaltATSUVersion, &result);
170 return (err == noErr);
174 long UMAGetProcessMode()
177 ProcessInfoRec processinfo
;
178 ProcessSerialNumber procno
;
180 procno
.highLongOfPSN
= NULL
;
181 procno
.lowLongOfPSN
= kCurrentProcess
;
182 processinfo
.processInfoLength
= sizeof(ProcessInfoRec
);
183 processinfo
.processName
= NULL
;
184 processinfo
.processAppSpec
= NULL
;
186 err
= ::GetProcessInformation( &procno
, &processinfo
) ;
187 wxASSERT( err
== noErr
) ;
188 return processinfo
.processMode
;
191 bool UMAGetProcessModeDoesActivateOnFGSwitch()
193 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch
;
198 MenuRef
UMANewMenu( SInt16 id
, const wxString
& title
)
200 wxString str
= wxStripMenuCodes( title
) ;
203 CreateNewMenu( id
, 0 , &menu
) ;
204 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
) ) ;
207 wxMacStringToPascal( str
, ptitle
) ;
208 menu
= ::NewMenu( id
, ptitle
) ;
213 void UMASetMenuTitle( MenuRef menu
, const wxString
& title
)
215 wxString str
= wxStripMenuCodes( title
) ;
217 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
) ) ;
220 wxMacStringToPascal( str
, ptitle
) ;
221 SetMenuTitle( menu
, ptitle
) ;
225 void UMASetMenuItemText( MenuRef menu
, MenuItemIndex item
, const wxString
& title
)
227 wxString str
= wxStripMenuCodes( title
) ;
229 SetMenuItemTextWithCFString( menu
, item
, wxMacCFStringHolder(str
) ) ;
232 wxMacStringToPascal( str
, ptitle
) ;
233 SetMenuItemText( menu
, item
, ptitle
) ;
238 UInt32
UMAMenuEvent( EventRecord
*inEvent
)
240 return MenuEvent( inEvent
) ;
243 void UMAEnableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
, bool enable
)
246 EnableMenuItem( inMenu
, inItem
) ;
248 DisableMenuItem( inMenu
, inItem
) ;
251 void UMAAppendSubMenuItem( MenuRef menu
, const wxString
& title
, SInt16 id
)
253 MacAppendMenu(menu
, "\pA");
254 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
);
255 SetMenuItemHierarchicalID( menu
, CountMenuItems( menu
) , id
) ;
258 void UMAInsertSubMenuItem( MenuRef menu
, const wxString
& title
, MenuItemIndex item
, SInt16 id
)
260 MacInsertMenuItem(menu
, "\pA" , item
);
261 UMASetMenuItemText(menu
, item
, title
);
262 SetMenuItemHierarchicalID( menu
, item
, id
) ;
265 void UMASetMenuItemShortcut( MenuRef menu
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
270 UInt8 modifiers
= 0 ;
271 SInt16 key
= entry
->GetKeyCode() ;
274 bool explicitCommandKey
= false ;
276 if ( entry
->GetFlags() & wxACCEL_CTRL
)
278 explicitCommandKey
= true ;
281 if (entry
->GetFlags() & wxACCEL_ALT
)
283 modifiers
|= kMenuOptionModifier
;
286 if (entry
->GetFlags() & wxACCEL_SHIFT
)
288 modifiers
|= kMenuShiftModifier
;
292 SInt16 macKey
= key
;
293 if ( key
>= WXK_F1
&& key
<= WXK_F15
)
295 // for some reasons this must be 0 right now
296 // everything else leads to just the first function key item
297 // to be selected. Thanks to Ryan Wilcox for finding out.
299 glyph
= kMenuF1Glyph
+ ( key
- WXK_F1
) ;
300 if ( key
>= WXK_F13
)
302 if ( !explicitCommandKey
)
303 modifiers
|= kMenuNoCommandModifier
;
310 macKey
= kBackspaceCharCode
;
311 glyph
= kMenuDeleteLeftGlyph
;
314 macKey
= kTabCharCode
;
315 glyph
= kMenuTabRightGlyph
;
317 case kEnterCharCode
:
318 macKey
= kEnterCharCode
;
319 glyph
= kMenuEnterGlyph
;
322 macKey
= kReturnCharCode
;
323 glyph
= kMenuReturnGlyph
;
326 macKey
= kEscapeCharCode
;
327 glyph
= kMenuEscapeGlyph
;
331 glyph
= kMenuSpaceGlyph
;
334 macKey
= kDeleteCharCode
;
335 glyph
= kMenuDeleteRightGlyph
;
338 macKey
= kClearCharCode
;
339 glyph
= kMenuClearGlyph
;
341 case WXK_PRIOR
: // PAGE UP
342 macKey
= kPageUpCharCode
;
343 glyph
= kMenuPageUpGlyph
;
346 macKey
= kPageDownCharCode
;
347 glyph
= kMenuPageDownGlyph
;
350 macKey
= kLeftArrowCharCode
;
351 glyph
= kMenuLeftArrowGlyph
;
354 macKey
= kUpArrowCharCode
;
355 glyph
= kMenuUpArrowGlyph
;
358 macKey
= kRightArrowCharCode
;
359 glyph
= kMenuRightArrowGlyph
;
362 macKey
= kDownArrowCharCode
;
363 glyph
= kMenuDownArrowGlyph
;
368 SetItemCmd( menu
, item
, macKey
);
369 SetMenuItemModifiers(menu
, item
, modifiers
) ;
372 SetMenuItemKeyGlyph(menu
, item
, glyph
) ;
376 void UMAAppendMenuItem( MenuRef menu
, const wxString
& title
, wxAcceleratorEntry
*entry
)
378 MacAppendMenu(menu
, "\pA");
379 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
);
380 UMASetMenuItemShortcut( menu
, (SInt16
) ::CountMenuItems(menu
), entry
) ;
383 void UMAInsertMenuItem( MenuRef menu
, const wxString
& title
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
385 MacInsertMenuItem( menu
, "\pA" , item
) ;
386 UMASetMenuItemText(menu
, item
+1 , title
);
387 UMASetMenuItemShortcut( menu
, item
+1 , entry
) ;
394 int gPrOpenCounter
= 0 ;
400 if ( gPrOpenCounter
== 1 )
404 wxASSERT( err
== noErr
) ;
409 OSStatus
UMAPrClose()
412 wxASSERT( gPrOpenCounter
>= 1 ) ;
413 if ( gPrOpenCounter
== 1 )
417 wxASSERT( err
== noErr
) ;
423 pascal QDGlobalsPtr
GetQDGlobalsPtr (void) ;
424 pascal QDGlobalsPtr
GetQDGlobalsPtr (void)
426 return QDGlobalsPtr (* (Ptr
*) LMGetCurrentA5 ( ) - 0xCA);
431 void UMAShowWatchCursor()
435 CursHandle watchFob
= GetCursor (watchCursor
);
442 // Cursor preservedArrow;
443 // GetQDGlobalsArrow (&preservedArrow);
444 // SetQDGlobalsArrow (*watchFob);
446 // SetQDGlobalsArrow (&preservedArrow);
447 SetCursor (*watchFob
);
449 SetCursor (*watchFob
);
454 void UMAShowArrowCursor()
458 SetCursor (GetQDGlobalsArrow (&arrow
));
460 SetCursor (&(qd
.arrow
));
466 GrafPtr
UMAGetWindowPort( WindowRef inWindowRef
)
468 wxASSERT( inWindowRef
!= NULL
) ;
470 return (GrafPtr
) GetWindowPort( inWindowRef
) ;
472 return (GrafPtr
) inWindowRef
;
476 void UMADisposeWindow( WindowRef inWindowRef
)
478 wxASSERT( inWindowRef
!= NULL
) ;
479 DisposeWindow( inWindowRef
) ;
482 void UMASetWTitle( WindowRef inWindowRef
, const wxString
& title
)
485 SetWindowTitleWithCFString( inWindowRef
, wxMacCFStringHolder(title
) ) ;
488 wxMacStringToPascal( title
, ptitle
) ;
489 SetWTitle( inWindowRef
, ptitle
) ;
493 void UMAGetWTitleC( WindowRef inWindowRef
, char *title
)
495 GetWTitle( inWindowRef
, (unsigned char*)title
) ;
497 p2cstrcpy( title
, (unsigned char *)title
) ;
499 p2cstr( (unsigned char*)title
) ;
503 // appearance additions
505 void UMASetControlTitle( ControlHandle inControl
, const wxString
& title
)
508 SetControlTitleWithCFString( inControl
, wxMacCFStringHolder(title
) ) ;
511 wxMacStringToPascal( title
, ptitle
) ;
512 SetControlTitle( inControl
, ptitle
) ;
516 void UMAActivateControl( ControlHandle inControl
)
518 // we have to add the control after again to the update rgn
519 // otherwise updates get lost
520 if ( !IsControlActive( inControl
) )
522 bool visible
= IsControlVisible( inControl
) ;
524 SetControlVisibility( inControl
, false , false ) ;
525 ::ActivateControl( inControl
) ;
527 SetControlVisibility( inControl
, true , false ) ;
529 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
534 void UMADrawControl( ControlHandle inControl
)
536 WindowRef theWindow
= GetControlOwner(inControl
) ;
537 RgnHandle updateRgn
= NewRgn() ;
538 GetWindowUpdateRgn( theWindow
, updateRgn
) ;
539 Point zero
= { 0 , 0 } ;
540 LocalToGlobal( &zero
) ;
541 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
542 ::DrawControlInCurrentPort( inControl
) ;
543 InvalWindowRgn( theWindow
, updateRgn
) ;
544 DisposeRgn( updateRgn
) ;
547 void UMAMoveControl( ControlHandle inControl
, short x
, short y
)
549 bool visible
= IsControlVisible( inControl
) ;
551 SetControlVisibility( inControl
, false , false ) ;
553 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
555 ::MoveControl( inControl
, x
, y
) ;
557 SetControlVisibility( inControl
, true , false ) ;
559 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
563 void UMASizeControl( ControlHandle inControl
, short x
, short y
)
565 bool visible
= IsControlVisible( inControl
) ;
567 SetControlVisibility( inControl
, false , false ) ;
569 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
571 ::SizeControl( inControl
, x
, y
) ;
573 SetControlVisibility( inControl
, true , false ) ;
575 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
579 void UMADeactivateControl( ControlHandle inControl
)
581 // we have to add the control after again to the update rgn
582 // otherwise updates get lost
583 bool visible
= IsControlVisible( inControl
) ;
585 SetControlVisibility( inControl
, false , false ) ;
586 ::DeactivateControl( inControl
) ;
588 SetControlVisibility( inControl
, true , false ) ;
590 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
593 // shows the control and adds the region to the update region
594 void UMAShowControl (ControlHandle inControl
)
596 SetControlVisibility( inControl
, true , false ) ;
598 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
601 // shows the control and adds the region to the update region
602 void UMAHideControl (ControlHandle inControl
)
604 SetControlVisibility( inControl
, false , false ) ;
606 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
609 OSErr
UMASetKeyboardFocus (WindowPtr inWindow
,
610 ControlHandle inControl
,
611 ControlFocusPart inPart
)
617 SetPortWindowPort( inWindow
) ;
619 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
626 void UMAUpdateControls( WindowPtr inWindow
, RgnHandle inRgn
)
628 RgnHandle updateRgn
= NewRgn() ;
629 GetWindowUpdateRgn( inWindow
, updateRgn
) ;
631 Point zero
= { 0 , 0 } ;
632 LocalToGlobal( &zero
) ;
633 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
635 UpdateControls( inWindow
, inRgn
) ;
636 InvalWindowRgn( inWindow
, updateRgn
) ;
637 DisposeRgn( updateRgn
) ;
640 bool UMAIsWindowFloating( WindowRef inWindow
)
644 GetWindowClass( inWindow
, &cl
) ;
645 return cl
== kFloatingWindowClass
;
648 bool UMAIsWindowModal( WindowRef inWindow
)
652 GetWindowClass( inWindow
, &cl
) ;
653 return cl
< kFloatingWindowClass
;
658 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
662 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
663 // if ( inActivate != isHightlited )
666 SetPortWindowPort( inWindowRef
) ;
667 HiliteWindow( inWindowRef
, inActivate
) ;
668 ControlHandle control
= NULL
;
669 ::GetRootControl( inWindowRef
, & control
) ;
673 UMAActivateControl( control
) ;
675 UMADeactivateControl( control
) ;
681 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
683 return ::DrawThemePlacard( inRect
, inState
) ;
687 static OSStatus helpMenuStatus
= noErr
;
688 static MenuItemIndex firstCustomItemIndex
= 0 ;
691 OSStatus
UMAGetHelpMenu(
692 MenuRef
* outHelpMenu
,
693 MenuItemIndex
* outFirstCustomItemIndex
)
696 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
698 MenuRef helpMenuHandle
;
699 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
700 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
702 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
704 if ( outFirstCustomItemIndex
)
706 *outFirstCustomItemIndex
= firstCustomItemIndex
;
708 *outHelpMenu
= helpMenuHandle
;
709 return helpMenuStatus
;
713 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
719 wxMacPortStateHelper::wxMacPortStateHelper()
724 void wxMacPortStateHelper::Setup( GrafPtr newport
)
726 GetPort( &m_oldPort
) ;
728 wxASSERT_MSG( m_clip
== NULL
, wxT("Cannot call setup twice") ) ;
731 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
732 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
733 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
734 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
735 GetThemeDrawingState( &m_drawingState
) ;
736 m_currentPort
= newport
;
738 void wxMacPortStateHelper::Clear()
742 DisposeRgn( m_clip
) ;
743 DisposeThemeDrawingState( m_drawingState
) ;
748 wxMacPortStateHelper::~wxMacPortStateHelper()
752 SetPort( m_currentPort
) ;
754 DisposeRgn( m_clip
) ;
755 TextFont( m_textFont
);
756 TextSize( m_textSize
);
757 TextFace( m_textStyle
);
758 TextMode( m_textMode
);
759 SetThemeDrawingState( m_drawingState
, true ) ;
760 SetPort( m_oldPort
) ;
764 OSStatus
UMAPutScrap( Size size
, OSType type
, void *data
)
766 OSStatus err
= noErr
;
768 err
= PutScrap( size
, type
, data
) ;
771 err
= GetCurrentScrap (&scrap
);
774 err
= PutScrapFlavor (scrap
, type
, 0, size
, data
);