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 wxWidgets 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 OptionBits options
= 0 ;
170 if ( UMAGetSystemVersion() < 0x1000 )
172 options
|= kTXNAlwaysUseQuickDrawTextMask
;
174 TXNInitTextension(fontDescriptions
, noOfFontDescriptions
, options
);
178 UMASetSystemIsInitialized(true);
183 Boolean CanUseATSUI()
186 OSErr err = Gestalt(gestaltATSUVersion, &result);
187 return (err == noErr);
191 long UMAGetProcessMode()
194 ProcessInfoRec processinfo
;
195 ProcessSerialNumber procno
;
197 procno
.highLongOfPSN
= NULL
;
198 procno
.lowLongOfPSN
= kCurrentProcess
;
199 processinfo
.processInfoLength
= sizeof(ProcessInfoRec
);
200 processinfo
.processName
= NULL
;
201 processinfo
.processAppSpec
= NULL
;
203 err
= ::GetProcessInformation( &procno
, &processinfo
) ;
204 wxASSERT( err
== noErr
) ;
205 return processinfo
.processMode
;
208 bool UMAGetProcessModeDoesActivateOnFGSwitch()
210 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch
;
215 MenuRef
UMANewMenu( SInt16 id
, const wxString
& title
, wxFontEncoding encoding
)
217 wxString str
= wxStripMenuCodes( title
) ;
220 CreateNewMenu( id
, 0 , &menu
) ;
221 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
, encoding
) ) ;
224 wxMacStringToPascal( str
, ptitle
) ;
225 menu
= ::NewMenu( id
, ptitle
) ;
230 void UMASetMenuTitle( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
)
232 wxString str
= wxStripMenuCodes( title
) ;
234 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
, encoding
) ) ;
237 wxMacStringToPascal( str
, ptitle
) ;
238 SetMenuTitle( menu
, ptitle
) ;
242 void UMASetMenuItemText( MenuRef menu
, MenuItemIndex item
, const wxString
& title
, wxFontEncoding encoding
)
244 wxString str
= wxStripMenuCodes( title
) ;
246 SetMenuItemTextWithCFString( menu
, item
, wxMacCFStringHolder(str
, encoding
) ) ;
249 wxMacStringToPascal( str
, ptitle
) ;
250 SetMenuItemText( menu
, item
, ptitle
) ;
255 UInt32
UMAMenuEvent( EventRecord
*inEvent
)
257 return MenuEvent( inEvent
) ;
260 void UMAEnableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
, bool enable
)
263 EnableMenuItem( inMenu
, inItem
) ;
265 DisableMenuItem( inMenu
, inItem
) ;
268 void UMAAppendSubMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, SInt16 id
)
270 MacAppendMenu(menu
, "\pA");
271 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
, encoding
);
272 SetMenuItemHierarchicalID( menu
, CountMenuItems( menu
) , id
) ;
275 void UMAInsertSubMenuItem( MenuRef menu
, const wxString
& title
, wxFontEncoding encoding
, MenuItemIndex item
, SInt16 id
)
277 MacInsertMenuItem(menu
, "\pA" , item
);
278 UMASetMenuItemText(menu
, item
+1, title
, encoding
);
279 SetMenuItemHierarchicalID( menu
, item
+1 , id
) ;
282 void UMASetMenuItemShortcut( MenuRef menu
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
287 UInt8 modifiers
= 0 ;
288 SInt16 key
= entry
->GetKeyCode() ;
291 bool explicitCommandKey
= false ;
293 if ( entry
->GetFlags() & wxACCEL_CTRL
)
295 explicitCommandKey
= true ;
298 if (entry
->GetFlags() & wxACCEL_ALT
)
300 modifiers
|= kMenuOptionModifier
;
303 if (entry
->GetFlags() & wxACCEL_SHIFT
)
305 modifiers
|= kMenuShiftModifier
;
309 SInt16 macKey
= key
;
310 if ( key
>= WXK_F1
&& key
<= WXK_F15
)
312 // for some reasons this must be 0 right now
313 // everything else leads to just the first function key item
314 // to be selected. Thanks to Ryan Wilcox for finding out.
316 glyph
= kMenuF1Glyph
+ ( key
- WXK_F1
) ;
317 if ( key
>= WXK_F13
)
319 if ( !explicitCommandKey
)
320 modifiers
|= kMenuNoCommandModifier
;
327 macKey
= kBackspaceCharCode
;
328 glyph
= kMenuDeleteLeftGlyph
;
331 macKey
= kTabCharCode
;
332 glyph
= kMenuTabRightGlyph
;
334 case kEnterCharCode
:
335 macKey
= kEnterCharCode
;
336 glyph
= kMenuEnterGlyph
;
339 macKey
= kReturnCharCode
;
340 glyph
= kMenuReturnGlyph
;
343 macKey
= kEscapeCharCode
;
344 glyph
= kMenuEscapeGlyph
;
348 glyph
= kMenuSpaceGlyph
;
351 macKey
= kDeleteCharCode
;
352 glyph
= kMenuDeleteRightGlyph
;
355 macKey
= kClearCharCode
;
356 glyph
= kMenuClearGlyph
;
358 case WXK_PRIOR
: // PAGE UP
359 macKey
= kPageUpCharCode
;
360 glyph
= kMenuPageUpGlyph
;
363 macKey
= kPageDownCharCode
;
364 glyph
= kMenuPageDownGlyph
;
367 macKey
= kLeftArrowCharCode
;
368 glyph
= kMenuLeftArrowGlyph
;
371 macKey
= kUpArrowCharCode
;
372 glyph
= kMenuUpArrowGlyph
;
375 macKey
= kRightArrowCharCode
;
376 glyph
= kMenuRightArrowGlyph
;
379 macKey
= kDownArrowCharCode
;
380 glyph
= kMenuDownArrowGlyph
;
385 // 1d and 1e have special meaning to SetItemCmd, so
386 // do not use for these character codes.
387 if (key
!= WXK_UP
&& key
!= WXK_RIGHT
)
388 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( ControlRef inControl
, const wxString
& title
, wxFontEncoding encoding
)
519 SetControlTitleWithCFString( inControl
, wxMacCFStringHolder(title
, encoding
) ) ;
522 wxMacStringToPascal( title
, ptitle
) ;
523 SetControlTitle( inControl
, ptitle
) ;
527 void UMAActivateControl( ControlRef inControl
)
529 #if !TARGET_API_MAC_OSX
530 // we have to add the control after again to the update rgn
531 // otherwise updates get lost
532 if ( !IsControlActive( inControl
) )
534 bool visible
= IsControlVisible( inControl
) ;
536 SetControlVisibility( inControl
, false , false ) ;
538 ::ActivateControl( inControl
) ;
539 #if !TARGET_API_MAC_OSX
541 SetControlVisibility( inControl
, true , false ) ;
544 InvalWindowRect(GetControlOwner(inControl
),UMAGetControlBoundsInWindowCoords(inControl
,&ctrlBounds
) ) ;
550 void UMAMoveControl( ControlRef inControl
, short x
, short y
)
552 #if !TARGET_API_MAC_OSX
553 bool visible
= IsControlVisible( inControl
) ;
555 SetControlVisibility( inControl
, false , false ) ;
557 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
560 ::MoveControl( inControl
, x
, y
) ;
561 #if !TARGET_API_MAC_OSX
563 SetControlVisibility( inControl
, true , false ) ;
565 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
570 void UMASizeControl( ControlRef inControl
, short x
, short y
)
572 #if !TARGET_API_MAC_OSX
573 bool visible
= IsControlVisible( inControl
) ;
575 SetControlVisibility( inControl
, false , false ) ;
577 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
580 ::SizeControl( inControl
, x
, y
) ;
581 #if !TARGET_API_MAC_OSX
583 SetControlVisibility( inControl
, true , false ) ;
585 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
590 void UMADeactivateControl( ControlRef inControl
)
592 #if !TARGET_API_MAC_OSX
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 ) ;
599 ::DeactivateControl( inControl
) ;
600 #if !TARGET_API_MAC_OSX
602 SetControlVisibility( inControl
, true , false ) ;
604 InvalWindowRect(GetControlOwner(inControl
),UMAGetControlBoundsInWindowCoords(inControl
,&ctrlBounds
) ) ;
608 // shows the control and adds the region to the update region
609 void UMAShowControl (ControlRef inControl
)
611 SetControlVisibility( inControl
, true , false ) ;
613 InvalWindowRect(GetControlOwner(inControl
),UMAGetControlBoundsInWindowCoords(inControl
,&ctrlBounds
) ) ;
616 // hides the control and adds the region to the update region
617 void UMAHideControl (ControlRef inControl
)
619 SetControlVisibility( inControl
, false , false ) ;
621 InvalWindowRect(GetControlOwner(inControl
),UMAGetControlBoundsInWindowCoords(inControl
,&ctrlBounds
) ) ;
624 OSErr
UMASetKeyboardFocus (WindowPtr inWindow
,
625 ControlRef inControl
,
626 ControlFocusPart inPart
)
632 SetPortWindowPort( inWindow
) ;
634 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
640 void UMAUpdateControls( WindowPtr inWindow
, RgnHandle inRgn
)
642 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) inWindow
) ) ;
643 RgnHandle updateRgn
= NewRgn() ;
644 GetWindowUpdateRgn( inWindow
, updateRgn
) ;
646 Point zero
= { 0 , 0 } ;
647 LocalToGlobal( &zero
) ;
648 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
650 UpdateControls( inWindow
, inRgn
) ;
651 InvalWindowRgn( inWindow
, updateRgn
) ;
652 DisposeRgn( updateRgn
) ;
655 bool UMAIsWindowFloating( WindowRef inWindow
)
659 GetWindowClass( inWindow
, &cl
) ;
660 return cl
== kFloatingWindowClass
;
663 bool UMAIsWindowModal( WindowRef inWindow
)
667 GetWindowClass( inWindow
, &cl
) ;
668 return cl
< kFloatingWindowClass
;
673 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
677 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
678 // if ( inActivate != isHightlited )
681 SetPortWindowPort( inWindowRef
) ;
682 HiliteWindow( inWindowRef
, inActivate
) ;
683 ControlRef control
= NULL
;
684 ::GetRootControl( inWindowRef
, & control
) ;
688 UMAActivateControl( control
) ;
690 UMADeactivateControl( control
) ;
696 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
698 return ::DrawThemePlacard( inRect
, inState
) ;
702 static OSStatus helpMenuStatus
= noErr
;
703 static MenuItemIndex firstCustomItemIndex
= 0 ;
706 OSStatus
UMAGetHelpMenu(
707 MenuRef
* outHelpMenu
,
708 MenuItemIndex
* outFirstCustomItemIndex
)
711 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
713 MenuRef helpMenuHandle
;
714 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
715 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
717 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
719 if ( outFirstCustomItemIndex
)
721 *outFirstCustomItemIndex
= firstCustomItemIndex
;
723 *outHelpMenu
= helpMenuHandle
;
724 return helpMenuStatus
;
728 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
734 wxMacPortStateHelper::wxMacPortStateHelper()
739 void wxMacPortStateHelper::Setup( GrafPtr newport
)
741 GetPort( &m_oldPort
) ;
744 wxASSERT_MSG( m_clip
== NULL
, wxT("Cannot call setup twice") ) ;
747 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
748 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
749 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
750 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
751 GetThemeDrawingState( &m_drawingState
) ;
752 m_currentPort
= newport
;
754 void wxMacPortStateHelper::Clear()
758 DisposeRgn( m_clip
) ;
759 DisposeThemeDrawingState( m_drawingState
) ;
764 wxMacPortStateHelper::~wxMacPortStateHelper()
768 SetPort( m_currentPort
) ;
770 DisposeRgn( m_clip
) ;
771 TextFont( m_textFont
);
772 TextSize( m_textSize
);
773 TextFace( m_textStyle
);
774 TextMode( m_textMode
);
775 SetThemeDrawingState( m_drawingState
, true ) ;
776 SetPort( m_oldPort
) ;
780 OSStatus
UMAPutScrap( Size size
, OSType type
, void *data
)
782 OSStatus err
= noErr
;
784 err
= PutScrap( size
, type
, data
) ;
787 err
= GetCurrentScrap (&scrap
);
790 err
= PutScrapFlavor (scrap
, type
, 0, size
, data
);
796 Rect
* UMAGetControlBoundsInWindowCoords(ControlRef theControl
, Rect
*bounds
)
798 wxWindow
* win
= wxFindControlFromMacControl( theControl
) ;
800 GetControlBounds( theControl
, bounds
) ;
801 #if TARGET_API_MAC_OSX
802 if ( win
!= NULL
&& win
->MacGetTopLevelWindow() != NULL
)
808 win
->GetParent()->MacWindowToRootWindow( &x
, & y
) ;
812 bounds
->bottom
+= y
;
823 static bool sUMASystemInitialized
= false ;
825 bool UMASystemIsInitialized()
827 return sUMASystemInitialized
;
830 void UMASetSystemIsInitialized(bool val
)
832 sUMASystemInitialized
= val
;