3 #include "wx/mac/uma.h"
4 #include <MacTextEditor.h>
7 # include <Navigation.h>
8 # if defined(TARGET_CARBON)
9 # if PM_USE_SESSION_APIS
12 # include <PMApplication.h>
14 # include <Printing.h>
18 // since we have decided that we only support 8.6 upwards we are
19 // checking for these minimum requirements in the startup code of
20 // the application so all wxWindows code can safely assume that appearance 1.1
21 // windows manager, control manager, navigation services etc. are
24 static bool sUMAHasAppearance
= false ;
25 static long sUMAAppearanceVersion
= 0 ;
26 static long sUMASystemVersion
= 0 ;
27 static bool sUMAHasAquaLayout
= false ;
28 static bool sUMASystemInitialized
= false ;
30 extern int gAGABackgroundColor
;
31 bool UMAHasAppearance() { return sUMAHasAppearance
; }
32 long UMAGetAppearanceVersion() { return sUMAAppearanceVersion
; }
33 long UMAGetSystemVersion() { return sUMASystemVersion
; }
35 static bool sUMAHasWindowManager
= false ;
36 static long sUMAWindowManagerAttr
= 0 ;
38 bool UMAHasWindowManager() { return sUMAHasWindowManager
; }
39 long UMAGetWindowManagerAttr() { return sUMAWindowManagerAttr
; }
40 bool UMAHasAquaLayout() { return sUMAHasAquaLayout
; }
41 bool UMASystemIsInitialized() { return sUMASystemInitialized
; }
43 void UMACleanupToolbox()
45 if ( sUMAHasAppearance
)
47 UnregisterAppearanceClient() ;
49 if ( NavServicesAvailable() )
53 if ( TXNTerminateTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
54 TXNTerminateTextension( ) ;
56 void UMAInitToolbox( UInt16 inMoreMastersCalls
)
60 for (long i
= 1; i
<= inMoreMastersCalls
; i
++)
63 ::InitGraf(&qd
.thePort
);
68 ::FlushEvents(everyEvent
, 0);
71 PurgeSpace(&total
, &contig
);
76 if ( Gestalt(gestaltSystemVersion
, &sUMASystemVersion
) != noErr
)
77 sUMASystemVersion
= 0x0000 ;
80 if ( Gestalt( gestaltAppearanceAttr
, &theAppearance
) == noErr
)
82 sUMAHasAppearance
= true ;
83 RegisterAppearanceClient();
84 if ( Gestalt( gestaltAppearanceVersion
, &theAppearance
) == noErr
)
86 sUMAAppearanceVersion
= theAppearance
;
90 sUMAAppearanceVersion
= 0x0100 ;
93 if ( Gestalt( gestaltWindowMgrAttr
, &sUMAWindowManagerAttr
) == noErr
)
95 sUMAHasWindowManager
= sUMAWindowManagerAttr
& gestaltWindowMgrPresent
;
99 // Call currently implicitely done : InitFloatingWindows() ;
101 if ( sUMAHasWindowManager
)
102 InitFloatingWindows() ;
107 if ( NavServicesAvailable() )
113 Gestalt( gestaltMenuMgrAttr
, &menuMgrAttr
) ;
114 if ( menuMgrAttr
& gestaltMenuMgrAquaLayoutMask
)
115 sUMAHasAquaLayout
= true ;
117 if ( TXNInitTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
119 FontFamilyID fontId
;
123 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
124 GetFNum( fontName
, &fontId
);
126 TXNMacOSPreferredFontDescription fontDescriptions
[] =
128 { fontId
, (fontSize
<< 16) ,kTXNDefaultFontStyle
, kTXNSystemDefaultEncoding
} ,
130 int noOfFontDescriptions
= sizeof( fontDescriptions
) / sizeof(TXNMacOSPreferredFontDescription
) ;
131 #if 0 // TARGET_CARBON
132 --noOfFontDescriptions
;
134 // kTXNAlwaysUseQuickDrawTextMask might be desirable because of speed increases but it crashes the app under OS X upon key stroke
135 OptionBits options
= kTXNWantMoviesMask
| kTXNWantSoundMask
| kTXNWantGraphicsMask
;
137 if ( !UMAHasAquaLayout() )
140 options
|= kTXNAlwaysUseQuickDrawTextMask
;
142 TXNInitTextension(fontDescriptions
, noOfFontDescriptions
, options
);
146 sUMASystemInitialized
= true ;
151 Boolean CanUseATSUI()
154 OSErr err = Gestalt(gestaltATSUVersion, &result);
155 return (err == noErr);
159 long UMAGetProcessMode()
162 ProcessInfoRec processinfo
;
163 ProcessSerialNumber procno
;
165 procno
.highLongOfPSN
= NULL
;
166 procno
.lowLongOfPSN
= kCurrentProcess
;
167 processinfo
.processInfoLength
= sizeof(ProcessInfoRec
);
168 processinfo
.processName
= NULL
;
169 processinfo
.processAppSpec
= NULL
;
171 err
= ::GetProcessInformation( &procno
, &processinfo
) ;
172 wxASSERT( err
== noErr
) ;
173 return processinfo
.processMode
;
176 bool UMAGetProcessModeDoesActivateOnFGSwitch()
178 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch
;
183 MenuRef
UMANewMenu( SInt16 id
, const wxString
& title
)
185 wxString str
= wxStripMenuCodes( title
) ;
188 CFStringRef cfs
= wxMacCreateCFString( str
) ;
189 CreateNewMenu( id
, 0 , &menu
) ;
190 SetMenuTitleWithCFString( menu
, cfs
) ;
194 wxMacStringToPascal( str
, ptitle
) ;
195 menu
= ::NewMenu( id
, ptitle
) ;
200 void UMASetMenuTitle( MenuRef menu
, const wxString
& title
)
202 wxString str
= wxStripMenuCodes( title
) ;
204 CFStringRef cfs
= wxMacCreateCFString( str
) ;
205 SetMenuTitleWithCFString( menu
, cfs
) ;
209 wxMacStringToPascal( str
, ptitle
) ;
210 SetMenuTitle( menu
, ptitle
) ;
214 void UMASetMenuItemText( MenuRef menu
, MenuItemIndex item
, const wxString
& title
)
216 wxString str
= wxStripMenuCodes( title
) ;
218 CFStringRef cfs
= wxMacCreateCFString( str
) ;
219 SetMenuItemTextWithCFString( menu
, item
, cfs
) ;
223 wxMacStringToPascal( str
, ptitle
) ;
224 SetMenuItemText( menu
, item
, ptitle
) ;
229 UInt32
UMAMenuEvent( EventRecord
*inEvent
)
231 return MenuEvent( inEvent
) ;
234 void UMAEnableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
, bool enable
)
237 EnableMenuItem( inMenu
, inItem
) ;
239 DisableMenuItem( inMenu
, inItem
) ;
242 void UMAAppendSubMenuItem( MenuRef menu
, const wxString
& title
, SInt16 id
)
244 MacAppendMenu(menu
, "\pA");
245 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
);
246 SetMenuItemHierarchicalID( menu
, CountMenuItems( menu
) , id
) ;
249 void UMAInsertSubMenuItem( MenuRef menu
, const wxString
& title
, MenuItemIndex item
, SInt16 id
)
251 MacInsertMenuItem(menu
, "\pA" , item
);
252 UMASetMenuItemText(menu
, item
, title
);
253 SetMenuItemHierarchicalID( menu
, item
, id
) ;
256 void UMASetMenuItemShortcut( MenuRef menu
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
261 UInt8 modifiers
= 0 ;
262 SInt16 key
= entry
->GetKeyCode() ;
265 bool explicitCommandKey
= false ;
267 if ( entry
->GetFlags() & wxACCEL_CTRL
)
269 explicitCommandKey
= true ;
272 if (entry
->GetFlags() & wxACCEL_ALT
)
274 modifiers
|= kMenuOptionModifier
;
277 if (entry
->GetFlags() & wxACCEL_SHIFT
)
279 modifiers
|= kMenuShiftModifier
;
283 SInt16 macKey
= key
;
284 if ( key
>= WXK_F1
&& key
<= WXK_F15
)
286 macKey
= kFunctionKeyCharCode
;
287 glyph
= kMenuF1Glyph
+ ( key
- WXK_F1
) ;
288 if ( key
>= WXK_F13
)
290 if ( !explicitCommandKey
)
291 modifiers
|= kMenuNoCommandModifier
;
296 macKey
+= ( 0x7a << 8 ) ;
299 macKey
+= ( 0x78 << 8 ) ;
302 macKey
+= ( 0x63 << 8 ) ;
305 macKey
+= ( 0x76 << 8 ) ;
308 macKey
+= ( 0x60 << 8 ) ;
311 macKey
+= ( 0x61 << 8 ) ;
314 macKey
+= ( 0x62 << 8 ) ;
317 macKey
+= ( 0x64 << 8 ) ;
320 macKey
+= ( 0x65 << 8 ) ;
323 macKey
+= ( 0x6D << 8 ) ;
326 macKey
+= ( 0x67 << 8 ) ;
329 macKey
+= ( 0x6F << 8 ) ;
332 macKey
+= ( 0x69 << 8 ) ;
335 macKey
+= ( 0x6B << 8 ) ;
338 macKey
+= ( 0x71 << 8 ) ;
343 // unfortunately this does not yet trigger the right key ,
344 // for some reason mac justs picks the first function key menu
345 // defined, so we turn this off
354 macKey
= kBackspaceCharCode
;
355 glyph
= kMenuDeleteLeftGlyph
;
358 macKey
= kTabCharCode
;
359 glyph
= kMenuTabRightGlyph
;
361 case kEnterCharCode
:
362 macKey
= kEnterCharCode
;
363 glyph
= kMenuEnterGlyph
;
366 macKey
= kReturnCharCode
;
367 glyph
= kMenuReturnGlyph
;
370 macKey
= kEscapeCharCode
;
371 glyph
= kMenuEscapeGlyph
;
375 glyph
= kMenuSpaceGlyph
;
378 macKey
= kDeleteCharCode
;
379 glyph
= kMenuDeleteRightGlyph
;
382 macKey
= kClearCharCode
;
383 glyph
= kMenuClearGlyph
;
385 case WXK_PRIOR
: // PAGE UP
386 macKey
= kPageUpCharCode
;
387 glyph
= kMenuPageUpGlyph
;
390 macKey
= kPageDownCharCode
;
391 glyph
= kMenuPageDownGlyph
;
394 macKey
= kLeftArrowCharCode
;
395 glyph
= kMenuLeftArrowGlyph
;
398 macKey
= kUpArrowCharCode
;
399 glyph
= kMenuUpArrowGlyph
;
402 macKey
= kRightArrowCharCode
;
403 glyph
= kMenuRightArrowGlyph
;
406 macKey
= kDownArrowCharCode
;
407 glyph
= kMenuDownArrowGlyph
;
412 SetItemCmd( menu
, item
, macKey
);
413 SetMenuItemModifiers(menu
, item
, modifiers
) ;
416 SetMenuItemKeyGlyph(menu
, item
, glyph
) ;
420 void UMAAppendMenuItem( MenuRef menu
, const wxString
& title
, wxAcceleratorEntry
*entry
)
422 MacAppendMenu(menu
, "\pA");
423 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
);
424 UMASetMenuItemShortcut( menu
, (SInt16
) ::CountMenuItems(menu
), entry
) ;
427 void UMAInsertMenuItem( MenuRef menu
, const wxString
& title
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
429 MacInsertMenuItem( menu
, "\p" , item
) ;
430 UMASetMenuItemText(menu
, item
, title
);
431 UMASetMenuItemShortcut( menu
, item
, entry
) ;
436 int gPrOpenCounter
= 0 ;
438 OSStatus
UMAPrOpen(void *macPrintSession
)
443 if ( gPrOpenCounter
== 1 )
447 wxASSERT( err
== noErr
) ;
451 OSStatus err
= noErr
;
453 if ( gPrOpenCounter
== 1 )
455 #if PM_USE_SESSION_APIS
456 err
= PMCreateSession((PMPrintSession
*)macPrintSession
) ;
460 wxASSERT( err
== noErr
) ;
466 OSStatus
UMAPrClose(void *macPrintSession
)
470 wxASSERT( gPrOpenCounter
>= 1 ) ;
471 if ( gPrOpenCounter
== 1 )
475 wxASSERT( err
== noErr
) ;
480 OSStatus err
= noErr
;
481 wxASSERT( gPrOpenCounter
>= 1 ) ;
482 if ( gPrOpenCounter
== 1 )
484 #if PM_USE_SESSION_APIS
485 err
= PMRelease(*(PMPrintSession
*)macPrintSession
) ;
486 *(PMPrintSession
*)macPrintSession
= kPMNoReference
;
498 pascal QDGlobalsPtr
GetQDGlobalsPtr (void) ;
499 pascal QDGlobalsPtr
GetQDGlobalsPtr (void)
501 return QDGlobalsPtr (* (Ptr
*) LMGetCurrentA5 ( ) - 0xCA);
506 void UMAShowWatchCursor()
510 CursHandle watchFob
= GetCursor (watchCursor
);
517 // Cursor preservedArrow;
518 // GetQDGlobalsArrow (&preservedArrow);
519 // SetQDGlobalsArrow (*watchFob);
521 // SetQDGlobalsArrow (&preservedArrow);
522 SetCursor (*watchFob
);
524 SetCursor (*watchFob
);
529 void UMAShowArrowCursor()
533 SetCursor (GetQDGlobalsArrow (&arrow
));
535 SetCursor (&(qd
.arrow
));
541 GrafPtr
UMAGetWindowPort( WindowRef inWindowRef
)
543 wxASSERT( inWindowRef
!= NULL
) ;
545 return (GrafPtr
) GetWindowPort( inWindowRef
) ;
547 return (GrafPtr
) inWindowRef
;
551 void UMADisposeWindow( WindowRef inWindowRef
)
553 wxASSERT( inWindowRef
!= NULL
) ;
554 DisposeWindow( inWindowRef
) ;
557 void UMASetWTitleC( WindowRef inWindowRef
, const char *title
)
560 strncpy( (char*)ptitle
, title
, 96 ) ;
563 c2pstrcpy( ptitle
, (char *)ptitle
) ;
565 c2pstr( (char*)ptitle
) ;
567 SetWTitle( inWindowRef
, ptitle
) ;
570 void UMAGetWTitleC( WindowRef inWindowRef
, char *title
)
572 GetWTitle( inWindowRef
, (unsigned char*)title
) ;
574 p2cstrcpy( title
, (unsigned char *)title
) ;
576 p2cstr( (unsigned char*)title
) ;
580 // appearance additions
582 void UMAActivateControl( ControlHandle inControl
)
584 // we have to add the control after again to the update rgn
585 // otherwise updates get lost
586 if ( !IsControlActive( inControl
) )
588 bool visible
= IsControlVisible( inControl
) ;
590 SetControlVisibility( inControl
, false , false ) ;
591 ::ActivateControl( inControl
) ;
593 SetControlVisibility( inControl
, true , false ) ;
595 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
600 void UMADrawControl( ControlHandle inControl
)
602 WindowRef theWindow
= GetControlOwner(inControl
) ;
603 RgnHandle updateRgn
= NewRgn() ;
604 GetWindowUpdateRgn( theWindow
, updateRgn
) ;
605 Point zero
= { 0 , 0 } ;
606 LocalToGlobal( &zero
) ;
607 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
608 ::DrawControlInCurrentPort( inControl
) ;
609 InvalWindowRgn( theWindow
, updateRgn
) ;
610 DisposeRgn( updateRgn
) ;
613 void UMAMoveControl( ControlHandle inControl
, short x
, short y
)
615 bool visible
= IsControlVisible( inControl
) ;
617 SetControlVisibility( inControl
, false , false ) ;
619 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
621 ::MoveControl( inControl
, x
, y
) ;
623 SetControlVisibility( inControl
, true , false ) ;
625 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
629 void UMASizeControl( ControlHandle inControl
, short x
, short y
)
631 bool visible
= IsControlVisible( inControl
) ;
633 SetControlVisibility( inControl
, false , false ) ;
635 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
637 ::SizeControl( inControl
, x
, y
) ;
639 SetControlVisibility( inControl
, true , false ) ;
641 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
645 void UMADeactivateControl( ControlHandle inControl
)
647 // we have to add the control after again to the update rgn
648 // otherwise updates get lost
649 bool visible
= IsControlVisible( inControl
) ;
651 SetControlVisibility( inControl
, false , false ) ;
652 ::DeactivateControl( inControl
) ;
654 SetControlVisibility( inControl
, true , false ) ;
656 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
659 // shows the control and adds the region to the update region
660 void UMAShowControl (ControlHandle inControl
)
662 SetControlVisibility( inControl
, true , false ) ;
664 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
667 // shows the control and adds the region to the update region
668 void UMAHideControl (ControlHandle inControl
)
670 SetControlVisibility( inControl
, false , false ) ;
672 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
675 OSErr
UMASetKeyboardFocus (WindowPtr inWindow
,
676 ControlHandle inControl
,
677 ControlFocusPart inPart
)
683 SetPortWindowPort( inWindow
) ;
685 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
694 void UMAUpdateControls( WindowPtr inWindow
, RgnHandle inRgn
)
696 RgnHandle updateRgn
= NewRgn() ;
697 GetWindowUpdateRgn( inWindow
, updateRgn
) ;
699 Point zero
= { 0 , 0 } ;
700 LocalToGlobal( &zero
) ;
701 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
703 UpdateControls( inWindow
, inRgn
) ;
704 InvalWindowRgn( inWindow
, updateRgn
) ;
705 DisposeRgn( updateRgn
) ;
709 bool UMAIsWindowFloating( WindowRef inWindow
)
713 GetWindowClass( inWindow
, &cl
) ;
714 return cl
== kFloatingWindowClass
;
717 bool UMAIsWindowModal( WindowRef inWindow
)
721 GetWindowClass( inWindow
, &cl
) ;
722 return cl
< kFloatingWindowClass
;
727 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
731 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
732 // if ( inActivate != isHightlited )
735 SetPortWindowPort( inWindowRef
) ;
736 HiliteWindow( inWindowRef
, inActivate
) ;
737 ControlHandle control
= NULL
;
738 ::GetRootControl( inWindowRef
, & control
) ;
742 UMAActivateControl( control
) ;
744 UMADeactivateControl( control
) ;
749 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
751 return ::DrawThemePlacard( inRect
, inState
) ;
755 static OSStatus helpMenuStatus
= noErr
;
756 static MenuItemIndex firstCustomItemIndex
= 0 ;
759 OSStatus
UMAGetHelpMenu(
760 MenuRef
* outHelpMenu
,
761 MenuItemIndex
* outFirstCustomItemIndex
)
764 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
766 MenuRef helpMenuHandle
;
767 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
768 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
770 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
772 if ( outFirstCustomItemIndex
)
774 *outFirstCustomItemIndex
= firstCustomItemIndex
;
776 *outHelpMenu
= helpMenuHandle
;
777 return helpMenuStatus
;
781 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
787 wxMacPortStateHelper::wxMacPortStateHelper()
792 void wxMacPortStateHelper::Setup( GrafPtr newport
)
794 GetPort( &m_oldPort
) ;
796 wxASSERT_MSG( m_clip
== NULL
, "Cannot call setup twice" ) ;
799 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
800 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
801 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
802 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
803 GetThemeDrawingState( &m_drawingState
) ;
804 m_currentPort
= newport
;
806 void wxMacPortStateHelper::Clear()
810 DisposeRgn( m_clip
) ;
811 DisposeThemeDrawingState( m_drawingState
) ;
816 wxMacPortStateHelper::~wxMacPortStateHelper()
820 SetPort( m_currentPort
) ;
822 DisposeRgn( m_clip
) ;
823 TextFont( m_textFont
);
824 TextSize( m_textSize
);
825 TextFace( m_textStyle
);
826 TextMode( m_textMode
);
827 SetThemeDrawingState( m_drawingState
, true ) ;
828 SetPort( m_oldPort
) ;