3 #include <MacTextEditor.h>
6 # include <Navigation.h>
7 # if defined(TARGET_CARBON)
8 # if PM_USE_SESSION_APIS
11 # include <PMApplication.h>
13 # include <Printing.h>
20 #include "wx/mac/uma.h"
22 // since we have decided that we only support 8.6 upwards we are
23 // checking for these minimum requirements in the startup code of
24 // the application so all wxWindows code can safely assume that appearance 1.1
25 // windows manager, control manager, navigation services etc. are
28 static bool sUMAHasAppearance
= false ;
29 static long sUMAAppearanceVersion
= 0 ;
30 static long sUMASystemVersion
= 0 ;
31 static bool sUMAHasAquaLayout
= false ;
32 static bool sUMASystemInitialized
= false ;
34 extern int gAGABackgroundColor
;
35 bool UMAHasAppearance() { return sUMAHasAppearance
; }
36 long UMAGetAppearanceVersion() { return sUMAAppearanceVersion
; }
37 long UMAGetSystemVersion() { return sUMASystemVersion
; }
39 static bool sUMAHasWindowManager
= false ;
40 static long sUMAWindowManagerAttr
= 0 ;
42 bool UMAHasWindowManager() { return sUMAHasWindowManager
; }
43 long UMAGetWindowManagerAttr() { return sUMAWindowManagerAttr
; }
44 bool UMAHasAquaLayout() { return sUMAHasAquaLayout
; }
45 bool UMASystemIsInitialized() { return sUMASystemInitialized
; }
47 void UMACleanupToolbox()
49 if ( sUMAHasAppearance
)
51 UnregisterAppearanceClient() ;
53 if ( NavServicesAvailable() )
57 if ( TXNTerminateTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
58 TXNTerminateTextension( ) ;
60 void UMAInitToolbox( UInt16 inMoreMastersCalls
)
64 for (long i
= 1; i
<= inMoreMastersCalls
; i
++)
67 ::InitGraf(&qd
.thePort
);
72 ::FlushEvents(everyEvent
, 0);
75 PurgeSpace(&total
, &contig
);
80 if ( Gestalt(gestaltSystemVersion
, &sUMASystemVersion
) != noErr
)
81 sUMASystemVersion
= 0x0000 ;
84 if ( Gestalt( gestaltAppearanceAttr
, &theAppearance
) == noErr
)
86 sUMAHasAppearance
= true ;
87 RegisterAppearanceClient();
88 if ( Gestalt( gestaltAppearanceVersion
, &theAppearance
) == noErr
)
90 sUMAAppearanceVersion
= theAppearance
;
94 sUMAAppearanceVersion
= 0x0100 ;
97 if ( Gestalt( gestaltWindowMgrAttr
, &sUMAWindowManagerAttr
) == noErr
)
99 sUMAHasWindowManager
= sUMAWindowManagerAttr
& gestaltWindowMgrPresent
;
103 // Call currently implicitely done : InitFloatingWindows() ;
105 if ( sUMAHasWindowManager
)
106 InitFloatingWindows() ;
111 if ( NavServicesAvailable() )
117 Gestalt( gestaltMenuMgrAttr
, &menuMgrAttr
) ;
118 if ( menuMgrAttr
& gestaltMenuMgrAquaLayoutMask
)
119 sUMAHasAquaLayout
= true ;
121 if ( TXNInitTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
123 FontFamilyID fontId
;
127 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
128 GetFNum( fontName
, &fontId
);
130 TXNMacOSPreferredFontDescription fontDescriptions
[] =
132 { fontId
, (fontSize
<< 16) ,kTXNDefaultFontStyle
, kTXNSystemDefaultEncoding
} ,
134 int noOfFontDescriptions
= sizeof( fontDescriptions
) / sizeof(TXNMacOSPreferredFontDescription
) ;
135 #if 0 // TARGET_CARBON
136 --noOfFontDescriptions
;
138 // kTXNAlwaysUseQuickDrawTextMask might be desirable because of speed increases but it crashes the app under OS X upon key stroke
139 OptionBits options
= kTXNWantMoviesMask
| kTXNWantSoundMask
| kTXNWantGraphicsMask
;
141 if ( !UMAHasAquaLayout() )
144 options
|= kTXNAlwaysUseQuickDrawTextMask
;
146 TXNInitTextension(fontDescriptions
, noOfFontDescriptions
, options
);
150 sUMASystemInitialized
= true ;
155 Boolean CanUseATSUI()
158 OSErr err = Gestalt(gestaltATSUVersion, &result);
159 return (err == noErr);
163 long UMAGetProcessMode()
166 ProcessInfoRec processinfo
;
167 ProcessSerialNumber procno
;
169 procno
.highLongOfPSN
= NULL
;
170 procno
.lowLongOfPSN
= kCurrentProcess
;
171 processinfo
.processInfoLength
= sizeof(ProcessInfoRec
);
172 processinfo
.processName
= NULL
;
173 processinfo
.processAppSpec
= NULL
;
175 err
= ::GetProcessInformation( &procno
, &processinfo
) ;
176 wxASSERT( err
== noErr
) ;
177 return processinfo
.processMode
;
180 bool UMAGetProcessModeDoesActivateOnFGSwitch()
182 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch
;
187 MenuRef
UMANewMenu( SInt16 id
, const wxString
& title
)
189 wxString str
= wxStripMenuCodes( title
) ;
192 CreateNewMenu( id
, 0 , &menu
) ;
193 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
) ) ;
196 wxMacStringToPascal( str
, ptitle
) ;
197 menu
= ::NewMenu( id
, ptitle
) ;
202 void UMASetMenuTitle( MenuRef menu
, const wxString
& title
)
204 wxString str
= wxStripMenuCodes( title
) ;
206 SetMenuTitleWithCFString( menu
, wxMacCFStringHolder(str
) ) ;
209 wxMacStringToPascal( str
, ptitle
) ;
210 SetMenuTitle( menu
, ptitle
) ;
214 void UMASetMenuItemText( MenuRef menu
, MenuItemIndex item
, const wxString
& title
)
216 wxString str
= wxStripMenuCodes( title
) ;
218 SetMenuItemTextWithCFString( menu
, item
, wxMacCFStringHolder(str
) ) ;
221 wxMacStringToPascal( str
, ptitle
) ;
222 SetMenuItemText( menu
, item
, ptitle
) ;
227 UInt32
UMAMenuEvent( EventRecord
*inEvent
)
229 return MenuEvent( inEvent
) ;
232 void UMAEnableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
, bool enable
)
235 EnableMenuItem( inMenu
, inItem
) ;
237 DisableMenuItem( inMenu
, inItem
) ;
240 void UMAAppendSubMenuItem( MenuRef menu
, const wxString
& title
, SInt16 id
)
242 MacAppendMenu(menu
, "\pA");
243 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
);
244 SetMenuItemHierarchicalID( menu
, CountMenuItems( menu
) , id
) ;
247 void UMAInsertSubMenuItem( MenuRef menu
, const wxString
& title
, MenuItemIndex item
, SInt16 id
)
249 MacInsertMenuItem(menu
, "\pA" , item
);
250 UMASetMenuItemText(menu
, item
, title
);
251 SetMenuItemHierarchicalID( menu
, item
, id
) ;
254 void UMASetMenuItemShortcut( MenuRef menu
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
259 UInt8 modifiers
= 0 ;
260 SInt16 key
= entry
->GetKeyCode() ;
263 bool explicitCommandKey
= false ;
265 if ( entry
->GetFlags() & wxACCEL_CTRL
)
267 explicitCommandKey
= true ;
270 if (entry
->GetFlags() & wxACCEL_ALT
)
272 modifiers
|= kMenuOptionModifier
;
275 if (entry
->GetFlags() & wxACCEL_SHIFT
)
277 modifiers
|= kMenuShiftModifier
;
281 SInt16 macKey
= key
;
282 if ( key
>= WXK_F1
&& key
<= WXK_F15
)
284 macKey
= kFunctionKeyCharCode
;
285 glyph
= kMenuF1Glyph
+ ( key
- WXK_F1
) ;
286 if ( key
>= WXK_F13
)
288 if ( !explicitCommandKey
)
289 modifiers
|= kMenuNoCommandModifier
;
294 macKey
+= ( 0x7a << 8 ) ;
297 macKey
+= ( 0x78 << 8 ) ;
300 macKey
+= ( 0x63 << 8 ) ;
303 macKey
+= ( 0x76 << 8 ) ;
306 macKey
+= ( 0x60 << 8 ) ;
309 macKey
+= ( 0x61 << 8 ) ;
312 macKey
+= ( 0x62 << 8 ) ;
315 macKey
+= ( 0x64 << 8 ) ;
318 macKey
+= ( 0x65 << 8 ) ;
321 macKey
+= ( 0x6D << 8 ) ;
324 macKey
+= ( 0x67 << 8 ) ;
327 macKey
+= ( 0x6F << 8 ) ;
330 macKey
+= ( 0x69 << 8 ) ;
333 macKey
+= ( 0x6B << 8 ) ;
336 macKey
+= ( 0x71 << 8 ) ;
341 // unfortunately this does not yet trigger the right key ,
342 // for some reason mac justs picks the first function key menu
343 // defined, so we turn this off
352 macKey
= kBackspaceCharCode
;
353 glyph
= kMenuDeleteLeftGlyph
;
356 macKey
= kTabCharCode
;
357 glyph
= kMenuTabRightGlyph
;
359 case kEnterCharCode
:
360 macKey
= kEnterCharCode
;
361 glyph
= kMenuEnterGlyph
;
364 macKey
= kReturnCharCode
;
365 glyph
= kMenuReturnGlyph
;
368 macKey
= kEscapeCharCode
;
369 glyph
= kMenuEscapeGlyph
;
373 glyph
= kMenuSpaceGlyph
;
376 macKey
= kDeleteCharCode
;
377 glyph
= kMenuDeleteRightGlyph
;
380 macKey
= kClearCharCode
;
381 glyph
= kMenuClearGlyph
;
383 case WXK_PRIOR
: // PAGE UP
384 macKey
= kPageUpCharCode
;
385 glyph
= kMenuPageUpGlyph
;
388 macKey
= kPageDownCharCode
;
389 glyph
= kMenuPageDownGlyph
;
392 macKey
= kLeftArrowCharCode
;
393 glyph
= kMenuLeftArrowGlyph
;
396 macKey
= kUpArrowCharCode
;
397 glyph
= kMenuUpArrowGlyph
;
400 macKey
= kRightArrowCharCode
;
401 glyph
= kMenuRightArrowGlyph
;
404 macKey
= kDownArrowCharCode
;
405 glyph
= kMenuDownArrowGlyph
;
410 SetItemCmd( menu
, item
, macKey
);
411 SetMenuItemModifiers(menu
, item
, modifiers
) ;
414 SetMenuItemKeyGlyph(menu
, item
, glyph
) ;
418 void UMAAppendMenuItem( MenuRef menu
, const wxString
& title
, wxAcceleratorEntry
*entry
)
420 MacAppendMenu(menu
, "\pA");
421 UMASetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), title
);
422 UMASetMenuItemShortcut( menu
, (SInt16
) ::CountMenuItems(menu
), entry
) ;
425 void UMAInsertMenuItem( MenuRef menu
, const wxString
& title
, MenuItemIndex item
, wxAcceleratorEntry
*entry
)
427 MacInsertMenuItem( menu
, "\p" , item
) ;
428 UMASetMenuItemText(menu
, item
, title
);
429 UMASetMenuItemShortcut( menu
, item
, entry
) ;
434 int gPrOpenCounter
= 0 ;
436 OSStatus
UMAPrOpen(void *macPrintSession
)
441 if ( gPrOpenCounter
== 1 )
445 wxASSERT( err
== noErr
) ;
449 OSStatus err
= noErr
;
451 if ( gPrOpenCounter
== 1 )
453 #if PM_USE_SESSION_APIS
454 err
= PMCreateSession((PMPrintSession
*)macPrintSession
) ;
458 wxASSERT( err
== noErr
) ;
464 OSStatus
UMAPrClose(void *macPrintSession
)
468 wxASSERT( gPrOpenCounter
>= 1 ) ;
469 if ( gPrOpenCounter
== 1 )
473 wxASSERT( err
== noErr
) ;
478 OSStatus err
= noErr
;
479 wxASSERT( gPrOpenCounter
>= 1 ) ;
480 if ( gPrOpenCounter
== 1 )
482 #if PM_USE_SESSION_APIS
483 err
= PMRelease(*(PMPrintSession
*)macPrintSession
) ;
484 *(PMPrintSession
*)macPrintSession
= kPMNoReference
;
496 pascal QDGlobalsPtr
GetQDGlobalsPtr (void) ;
497 pascal QDGlobalsPtr
GetQDGlobalsPtr (void)
499 return QDGlobalsPtr (* (Ptr
*) LMGetCurrentA5 ( ) - 0xCA);
504 void UMAShowWatchCursor()
508 CursHandle watchFob
= GetCursor (watchCursor
);
515 // Cursor preservedArrow;
516 // GetQDGlobalsArrow (&preservedArrow);
517 // SetQDGlobalsArrow (*watchFob);
519 // SetQDGlobalsArrow (&preservedArrow);
520 SetCursor (*watchFob
);
522 SetCursor (*watchFob
);
527 void UMAShowArrowCursor()
531 SetCursor (GetQDGlobalsArrow (&arrow
));
533 SetCursor (&(qd
.arrow
));
539 GrafPtr
UMAGetWindowPort( WindowRef inWindowRef
)
541 wxASSERT( inWindowRef
!= NULL
) ;
543 return (GrafPtr
) GetWindowPort( inWindowRef
) ;
545 return (GrafPtr
) inWindowRef
;
549 void UMADisposeWindow( WindowRef inWindowRef
)
551 wxASSERT( inWindowRef
!= NULL
) ;
552 DisposeWindow( inWindowRef
) ;
555 void UMASetWTitle( WindowRef inWindowRef
, const wxString
& title
)
558 SetWindowTitleWithCFString( inWindowRef
, wxMacCFStringHolder(title
) ) ;
561 wxMacStringToPascal( title
, ptitle
) ;
562 SetWTitle( inWindowRef
, ptitle
) ;
566 void UMAGetWTitleC( WindowRef inWindowRef
, char *title
)
568 GetWTitle( inWindowRef
, (unsigned char*)title
) ;
570 p2cstrcpy( title
, (unsigned char *)title
) ;
572 p2cstr( (unsigned char*)title
) ;
576 // appearance additions
578 void UMASetControlTitle( ControlHandle inControl
, const wxString
& title
)
581 SetControlTitleWithCFString( inControl
, wxMacCFStringHolder(title
) ) ;
584 wxMacStringToPascal( title
, ptitle
) ;
585 SetControlTitle( inControl
, ptitle
) ;
589 void UMAActivateControl( ControlHandle inControl
)
591 // we have to add the control after again to the update rgn
592 // otherwise updates get lost
593 if ( !IsControlActive( inControl
) )
595 bool visible
= IsControlVisible( inControl
) ;
597 SetControlVisibility( inControl
, false , false ) ;
598 ::ActivateControl( inControl
) ;
600 SetControlVisibility( inControl
, true , false ) ;
602 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
607 void UMADrawControl( ControlHandle inControl
)
609 WindowRef theWindow
= GetControlOwner(inControl
) ;
610 RgnHandle updateRgn
= NewRgn() ;
611 GetWindowUpdateRgn( theWindow
, updateRgn
) ;
612 Point zero
= { 0 , 0 } ;
613 LocalToGlobal( &zero
) ;
614 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
615 ::DrawControlInCurrentPort( inControl
) ;
616 InvalWindowRgn( theWindow
, updateRgn
) ;
617 DisposeRgn( updateRgn
) ;
620 void UMAMoveControl( ControlHandle inControl
, short x
, short y
)
622 bool visible
= IsControlVisible( inControl
) ;
624 SetControlVisibility( inControl
, false , false ) ;
626 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
628 ::MoveControl( inControl
, x
, y
) ;
630 SetControlVisibility( inControl
, true , false ) ;
632 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
636 void UMASizeControl( ControlHandle inControl
, short x
, short y
)
638 bool visible
= IsControlVisible( inControl
) ;
640 SetControlVisibility( inControl
, false , false ) ;
642 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
644 ::SizeControl( inControl
, x
, y
) ;
646 SetControlVisibility( inControl
, true , false ) ;
648 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
652 void UMADeactivateControl( ControlHandle inControl
)
654 // we have to add the control after again to the update rgn
655 // otherwise updates get lost
656 bool visible
= IsControlVisible( inControl
) ;
658 SetControlVisibility( inControl
, false , false ) ;
659 ::DeactivateControl( inControl
) ;
661 SetControlVisibility( inControl
, true , false ) ;
663 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
666 // shows the control and adds the region to the update region
667 void UMAShowControl (ControlHandle inControl
)
669 SetControlVisibility( inControl
, true , false ) ;
671 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
674 // shows the control and adds the region to the update region
675 void UMAHideControl (ControlHandle inControl
)
677 SetControlVisibility( inControl
, false , false ) ;
679 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
682 OSErr
UMASetKeyboardFocus (WindowPtr inWindow
,
683 ControlHandle inControl
,
684 ControlFocusPart inPart
)
690 SetPortWindowPort( inWindow
) ;
692 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
699 void UMAUpdateControls( WindowPtr inWindow
, RgnHandle inRgn
)
701 RgnHandle updateRgn
= NewRgn() ;
702 GetWindowUpdateRgn( inWindow
, updateRgn
) ;
704 Point zero
= { 0 , 0 } ;
705 LocalToGlobal( &zero
) ;
706 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
708 UpdateControls( inWindow
, inRgn
) ;
709 InvalWindowRgn( inWindow
, updateRgn
) ;
710 DisposeRgn( updateRgn
) ;
713 bool UMAIsWindowFloating( WindowRef inWindow
)
717 GetWindowClass( inWindow
, &cl
) ;
718 return cl
== kFloatingWindowClass
;
721 bool UMAIsWindowModal( WindowRef inWindow
)
725 GetWindowClass( inWindow
, &cl
) ;
726 return cl
< kFloatingWindowClass
;
731 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
735 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
736 // if ( inActivate != isHightlited )
739 SetPortWindowPort( inWindowRef
) ;
740 HiliteWindow( inWindowRef
, inActivate
) ;
741 ControlHandle control
= NULL
;
742 ::GetRootControl( inWindowRef
, & control
) ;
746 UMAActivateControl( control
) ;
748 UMADeactivateControl( control
) ;
754 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
756 return ::DrawThemePlacard( inRect
, inState
) ;
760 static OSStatus helpMenuStatus
= noErr
;
761 static MenuItemIndex firstCustomItemIndex
= 0 ;
764 OSStatus
UMAGetHelpMenu(
765 MenuRef
* outHelpMenu
,
766 MenuItemIndex
* outFirstCustomItemIndex
)
769 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
771 MenuRef helpMenuHandle
;
772 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
773 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
775 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
777 if ( outFirstCustomItemIndex
)
779 *outFirstCustomItemIndex
= firstCustomItemIndex
;
781 *outHelpMenu
= helpMenuHandle
;
782 return helpMenuStatus
;
786 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
792 wxMacPortStateHelper::wxMacPortStateHelper()
797 void wxMacPortStateHelper::Setup( GrafPtr newport
)
799 GetPort( &m_oldPort
) ;
801 wxASSERT_MSG( m_clip
== NULL
, wxT("Cannot call setup twice") ) ;
804 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
805 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
806 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
807 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
808 GetThemeDrawingState( &m_drawingState
) ;
809 m_currentPort
= newport
;
811 void wxMacPortStateHelper::Clear()
815 DisposeRgn( m_clip
) ;
816 DisposeThemeDrawingState( m_drawingState
) ;
821 wxMacPortStateHelper::~wxMacPortStateHelper()
825 SetPort( m_currentPort
) ;
827 DisposeRgn( m_clip
) ;
828 TextFont( m_textFont
);
829 TextSize( m_textSize
);
830 TextFace( m_textStyle
);
831 TextMode( m_textMode
);
832 SetThemeDrawingState( m_drawingState
, true ) ;
833 SetPort( m_oldPort
) ;
837 OSStatus
UMAPutScrap( Size size
, OSType type
, void *data
)
839 OSStatus err
= noErr
;
841 err
= PutScrap( size
, type
, data
) ;
844 err
= GetCurrentScrap (&scrap
);
847 err
= PutScrapFlavor (scrap
, type
, 0, size
, data
);