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 void UMASetMenuTitle( MenuRef menu
, StringPtr title
)
187 long size = GetHandleSize( (Handle) menu ) ;
188 const long headersize = 14 ;
189 int oldlen = (**menu).menuData[0] + 1;
190 int newlen = title[0] + 1 ;
192 if ( oldlen < newlen )
194 // enlarge before adjusting
195 SetHandleSize( (Handle) menu , size + (newlen - oldlen ) );
198 if ( oldlen != newlen )
199 memmove( (char*) (**menu).menuData + newlen , (char*) (**menu).menuData + oldlen , size - headersize - oldlen ) ;
201 memcpy( (char*) (**menu).menuData , title , newlen ) ;
202 if ( oldlen > newlen )
205 SetHandleSize( (Handle) menu , size + (newlen - oldlen ) ) ;
209 SetMenuTitle( menu
, title
) ;
213 UInt32
UMAMenuEvent( EventRecord
*inEvent
)
215 return MenuEvent( inEvent
) ;
218 void UMAEnableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
)
220 EnableMenuItem( inMenu
, inItem
) ;
223 void UMADisableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
)
225 DisableMenuItem( inMenu
, inItem
) ;
228 void UMAAppendSubMenuItem( MenuRef menu
, StringPtr l
, SInt16 id
)
230 MacAppendMenu(menu
, l
);
231 SetMenuItemHierarchicalID( menu
, CountMenuItems( menu
) , id
) ;
234 void UMAInsertSubMenuItem( MenuRef menu
, StringPtr l
, MenuItemIndex item
, SInt16 id
)
236 MacInsertMenuItem(menu
, l
, item
);
237 SetMenuItemHierarchicalID( menu
, item
, id
) ;
240 void UMASetMenuItemShortcut( MenuRef menu
, MenuItemIndex item
, SInt16 key
, UInt8 modifiers
)
245 SInt16 macKey
= key
;
246 if ( key
>= WXK_F1
&& key
<= WXK_F15
)
248 macKey
= kFunctionKeyCharCode
;
249 glyph
= kMenuF1Glyph
+ ( key
- WXK_F1
) ;
250 if ( key
>= WXK_F13
)
255 macKey
+= ( 0x7a << 8 ) ;
258 macKey
+= ( 0x78 << 8 ) ;
261 macKey
+= ( 0x63 << 8 ) ;
264 macKey
+= ( 0x76 << 8 ) ;
267 macKey
+= ( 0x60 << 8 ) ;
270 macKey
+= ( 0x61 << 8 ) ;
273 macKey
+= ( 0x62 << 8 ) ;
276 macKey
+= ( 0x64 << 8 ) ;
279 macKey
+= ( 0x65 << 8 ) ;
282 macKey
+= ( 0x6D << 8 ) ;
285 macKey
+= ( 0x67 << 8 ) ;
288 macKey
+= ( 0x6F << 8 ) ;
291 macKey
+= ( 0x69 << 8 ) ;
294 macKey
+= ( 0x6B << 8 ) ;
297 macKey
+= ( 0x71 << 8 ) ;
302 // unfortunately this does not yet trigger the right key ,
303 // for some reason mac justs picks the first function key menu
304 // defined, so we turn this off
313 macKey
= kBackspaceCharCode
;
314 glyph
= kMenuDeleteLeftGlyph
;
317 macKey
= kTabCharCode
;
318 glyph
= kMenuTabRightGlyph
;
320 case kEnterCharCode
:
321 macKey
= kEnterCharCode
;
322 glyph
= kMenuEnterGlyph
;
325 macKey
= kReturnCharCode
;
326 glyph
= kMenuReturnGlyph
;
329 macKey
= kEscapeCharCode
;
330 glyph
= kMenuEscapeGlyph
;
334 glyph
= kMenuSpaceGlyph
;
337 macKey
= kDeleteCharCode
;
338 glyph
= kMenuDeleteRightGlyph
;
341 macKey
= kClearCharCode
;
342 glyph
= kMenuClearGlyph
;
344 case WXK_PRIOR
: // PAGE UP
345 macKey
= kPageUpCharCode
;
346 glyph
= kMenuPageUpGlyph
;
349 macKey
= kPageDownCharCode
;
350 glyph
= kMenuPageDownGlyph
;
353 macKey
= kLeftArrowCharCode
;
354 glyph
= kMenuLeftArrowGlyph
;
357 macKey
= kUpArrowCharCode
;
358 glyph
= kMenuUpArrowGlyph
;
361 macKey
= kRightArrowCharCode
;
362 glyph
= kMenuRightArrowGlyph
;
365 macKey
= kDownArrowCharCode
;
366 glyph
= kMenuDownArrowGlyph
;
371 SetItemCmd( menu
, item
, macKey
);
372 SetMenuItemModifiers(menu
, item
, modifiers
) ;
375 SetMenuItemKeyGlyph(menu
, item
, glyph
) ;
379 void UMAAppendMenuItem( MenuRef menu
, StringPtr l
, SInt16 key
, UInt8 modifiers
)
381 MacAppendMenu(menu
, "\pA");
382 SetMenuItemText(menu
, (SInt16
) ::CountMenuItems(menu
), l
);
383 UMASetMenuItemShortcut( menu
, (SInt16
) ::CountMenuItems(menu
), key
, modifiers
) ;
386 void UMAInsertMenuItem( MenuRef menu
, StringPtr l
, MenuItemIndex item
, SInt16 key
, UInt8 modifiers
)
388 MacInsertMenuItem( menu
, "\p" , item
) ;
389 SetMenuItemText(menu
, item
, l
);
390 UMASetMenuItemShortcut( menu
, item
, key
, modifiers
) ;
395 int gPrOpenCounter
= 0 ;
397 OSStatus
UMAPrOpen(void *macPrintSession
)
402 if ( gPrOpenCounter
== 1 )
406 wxASSERT( err
== noErr
) ;
410 OSStatus err
= noErr
;
412 if ( gPrOpenCounter
== 1 )
414 #if PM_USE_SESSION_APIS
415 err
= PMCreateSession((PMPrintSession
*)macPrintSession
) ;
419 wxASSERT( err
== noErr
) ;
425 OSStatus
UMAPrClose(void *macPrintSession
)
429 wxASSERT( gPrOpenCounter
>= 1 ) ;
430 if ( gPrOpenCounter
== 1 )
434 wxASSERT( err
== noErr
) ;
439 OSStatus err
= noErr
;
440 wxASSERT( gPrOpenCounter
>= 1 ) ;
441 if ( gPrOpenCounter
== 1 )
443 #if PM_USE_SESSION_APIS
444 err
= PMRelease(*(PMPrintSession
*)macPrintSession
) ;
445 *(PMPrintSession
*)macPrintSession
= kPMNoReference
;
457 pascal QDGlobalsPtr
GetQDGlobalsPtr (void) ;
458 pascal QDGlobalsPtr
GetQDGlobalsPtr (void)
460 return QDGlobalsPtr (* (Ptr
*) LMGetCurrentA5 ( ) - 0xCA);
465 void UMAShowWatchCursor()
469 CursHandle watchFob
= GetCursor (watchCursor
);
476 // Cursor preservedArrow;
477 // GetQDGlobalsArrow (&preservedArrow);
478 // SetQDGlobalsArrow (*watchFob);
480 // SetQDGlobalsArrow (&preservedArrow);
481 SetCursor (*watchFob
);
483 SetCursor (*watchFob
);
488 void UMAShowArrowCursor()
492 SetCursor (GetQDGlobalsArrow (&arrow
));
494 SetCursor (&(qd
.arrow
));
500 GrafPtr
UMAGetWindowPort( WindowRef inWindowRef
)
502 wxASSERT( inWindowRef
!= NULL
) ;
504 return (GrafPtr
) GetWindowPort( inWindowRef
) ;
506 return (GrafPtr
) inWindowRef
;
510 void UMADisposeWindow( WindowRef inWindowRef
)
512 wxASSERT( inWindowRef
!= NULL
) ;
513 DisposeWindow( inWindowRef
) ;
516 void UMASetWTitleC( WindowRef inWindowRef
, const char *title
)
519 strncpy( (char*)ptitle
, title
, 96 ) ;
522 c2pstrcpy( ptitle
, (char *)ptitle
) ;
524 c2pstr( (char*)ptitle
) ;
526 SetWTitle( inWindowRef
, ptitle
) ;
529 void UMAGetWTitleC( WindowRef inWindowRef
, char *title
)
531 GetWTitle( inWindowRef
, (unsigned char*)title
) ;
533 p2cstrcpy( title
, (unsigned char *)title
) ;
535 p2cstr( (unsigned char*)title
) ;
539 // appearance additions
541 void UMAActivateControl( ControlHandle inControl
)
543 // we have to add the control after again to the update rgn
544 // otherwise updates get lost
545 if ( !IsControlActive( inControl
) )
547 bool visible
= IsControlVisible( inControl
) ;
549 SetControlVisibility( inControl
, false , false ) ;
550 ::ActivateControl( inControl
) ;
552 SetControlVisibility( inControl
, true , false ) ;
554 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
559 void UMADrawControl( ControlHandle inControl
)
561 WindowRef theWindow
= GetControlOwner(inControl
) ;
562 RgnHandle updateRgn
= NewRgn() ;
563 GetWindowUpdateRgn( theWindow
, updateRgn
) ;
564 Point zero
= { 0 , 0 } ;
565 LocalToGlobal( &zero
) ;
566 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
567 ::DrawControlInCurrentPort( inControl
) ;
568 InvalWindowRgn( theWindow
, updateRgn
) ;
569 DisposeRgn( updateRgn
) ;
572 void UMAMoveControl( ControlHandle inControl
, short x
, short y
)
574 bool visible
= IsControlVisible( inControl
) ;
576 SetControlVisibility( inControl
, false , false ) ;
578 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
580 ::MoveControl( inControl
, x
, y
) ;
582 SetControlVisibility( inControl
, true , false ) ;
584 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
588 void UMASizeControl( ControlHandle inControl
, short x
, short y
)
590 bool visible
= IsControlVisible( inControl
) ;
592 SetControlVisibility( inControl
, false , false ) ;
594 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
596 ::SizeControl( inControl
, x
, y
) ;
598 SetControlVisibility( inControl
, true , false ) ;
600 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
604 void UMADeactivateControl( ControlHandle inControl
)
606 // we have to add the control after again to the update rgn
607 // otherwise updates get lost
608 bool visible
= IsControlVisible( inControl
) ;
610 SetControlVisibility( inControl
, false , false ) ;
611 ::DeactivateControl( inControl
) ;
613 SetControlVisibility( inControl
, true , false ) ;
615 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
618 // shows the control and adds the region to the update region
619 void UMAShowControl (ControlHandle inControl
)
621 SetControlVisibility( inControl
, true , false ) ;
623 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
626 // shows the control and adds the region to the update region
627 void UMAHideControl (ControlHandle inControl
)
629 SetControlVisibility( inControl
, false , false ) ;
631 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
634 OSErr
UMASetKeyboardFocus (WindowPtr inWindow
,
635 ControlHandle inControl
,
636 ControlFocusPart inPart
)
642 SetPortWindowPort( inWindow
) ;
644 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
653 void UMAUpdateControls( WindowPtr inWindow
, RgnHandle inRgn
)
655 RgnHandle updateRgn
= NewRgn() ;
656 GetWindowUpdateRgn( inWindow
, updateRgn
) ;
658 Point zero
= { 0 , 0 } ;
659 LocalToGlobal( &zero
) ;
660 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
662 UpdateControls( inWindow
, inRgn
) ;
663 InvalWindowRgn( inWindow
, updateRgn
) ;
664 DisposeRgn( updateRgn
) ;
668 bool UMAIsWindowFloating( WindowRef inWindow
)
672 GetWindowClass( inWindow
, &cl
) ;
673 return cl
== kFloatingWindowClass
;
676 bool UMAIsWindowModal( WindowRef inWindow
)
680 GetWindowClass( inWindow
, &cl
) ;
681 return cl
< kFloatingWindowClass
;
686 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
690 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
691 // if ( inActivate != isHightlited )
694 SetPortWindowPort( inWindowRef
) ;
695 HiliteWindow( inWindowRef
, inActivate
) ;
696 ControlHandle control
= NULL
;
697 ::GetRootControl( inWindowRef
, & control
) ;
701 UMAActivateControl( control
) ;
703 UMADeactivateControl( control
) ;
708 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
710 return ::DrawThemePlacard( inRect
, inState
) ;
714 static OSStatus helpMenuStatus
= noErr
;
715 static MenuItemIndex firstCustomItemIndex
= 0 ;
718 OSStatus
UMAGetHelpMenu(
719 MenuRef
* outHelpMenu
,
720 MenuItemIndex
* outFirstCustomItemIndex
)
723 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
725 MenuRef helpMenuHandle
;
726 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
727 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
729 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
731 if ( outFirstCustomItemIndex
)
733 *outFirstCustomItemIndex
= firstCustomItemIndex
;
735 *outHelpMenu
= helpMenuHandle
;
736 return helpMenuStatus
;
740 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
746 wxMacPortStateHelper::wxMacPortStateHelper()
751 void wxMacPortStateHelper::Setup( GrafPtr newport
)
753 GetPort( &m_oldPort
) ;
755 wxASSERT_MSG( m_clip
== NULL
, "Cannot call setup twice" ) ;
758 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
759 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
760 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
761 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
762 GetThemeDrawingState( &m_drawingState
) ;
763 m_currentPort
= newport
;
765 void wxMacPortStateHelper::Clear()
769 DisposeRgn( m_clip
) ;
770 DisposeThemeDrawingState( m_drawingState
) ;
775 wxMacPortStateHelper::~wxMacPortStateHelper()
779 SetPort( m_currentPort
) ;
781 DisposeRgn( m_clip
) ;
782 TextFont( m_textFont
);
783 TextSize( m_textSize
);
784 TextFace( m_textStyle
);
785 TextMode( m_textMode
);
786 SetThemeDrawingState( m_drawingState
, true ) ;
787 SetPort( m_oldPort
) ;