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 bool sUMAHasAquaLayout
= false ;
27 static bool sUMASystemInitialized
= false ;
29 extern int gAGABackgroundColor
;
30 bool UMAHasAppearance() { return sUMAHasAppearance
; }
31 long UMAGetAppearanceVersion() { return sUMAAppearanceVersion
; }
33 static bool sUMAHasWindowManager
= false ;
34 static long sUMAWindowManagerAttr
= 0 ;
36 bool UMAHasWindowManager() { return sUMAHasWindowManager
; }
37 long UMAGetWindowManagerAttr() { return sUMAWindowManagerAttr
; }
38 bool UMAHasAquaLayout() { return sUMAHasAquaLayout
; }
39 bool UMASystemIsInitialized() { return sUMASystemInitialized
; }
41 void UMACleanupToolbox()
43 if ( sUMAHasAppearance
)
45 UnregisterAppearanceClient() ;
47 if ( NavServicesAvailable() )
51 if ( TXNTerminateTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
52 TXNTerminateTextension( ) ;
54 void UMAInitToolbox( UInt16 inMoreMastersCalls
)
58 for (long i
= 1; i
<= inMoreMastersCalls
; i
++)
61 ::InitGraf(&qd
.thePort
);
66 ::FlushEvents(everyEvent
, 0);
69 PurgeSpace(&total
, &contig
);
75 if ( Gestalt( gestaltAppearanceAttr
, &theAppearance
) == noErr
)
77 sUMAHasAppearance
= true ;
78 RegisterAppearanceClient();
79 if ( Gestalt( gestaltAppearanceVersion
, &theAppearance
) == noErr
)
81 sUMAAppearanceVersion
= theAppearance
;
85 sUMAAppearanceVersion
= 0x0100 ;
88 if ( Gestalt( gestaltWindowMgrAttr
, &sUMAWindowManagerAttr
) == noErr
)
90 sUMAHasWindowManager
= sUMAWindowManagerAttr
& gestaltWindowMgrPresent
;
94 // Call currently implicitely done : InitFloatingWindows() ;
96 if ( sUMAHasWindowManager
)
97 InitFloatingWindows() ;
102 if ( NavServicesAvailable() )
108 Gestalt( gestaltMenuMgrAttr
, &menuMgrAttr
) ;
109 if ( menuMgrAttr
& gestaltMenuMgrAquaLayoutMask
)
110 sUMAHasAquaLayout
= true ;
112 if ( TXNInitTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
114 FontFamilyID fontId
;
118 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
119 GetFNum( fontName
, &fontId
);
121 TXNMacOSPreferredFontDescription fontDescriptions
[] =
123 { fontId
, (fontSize
<< 16) ,kTXNDefaultFontStyle
, kTXNSystemDefaultEncoding
} ,
125 int noOfFontDescriptions
= sizeof( fontDescriptions
) / sizeof(TXNMacOSPreferredFontDescription
) ;
126 #if 0 // TARGET_CARBON
127 --noOfFontDescriptions
;
129 // kTXNAlwaysUseQuickDrawTextMask might be desirable because of speed increases but it crashes the app under OS X upon key stroke
130 OptionBits options
= kTXNWantMoviesMask
| kTXNWantSoundMask
| kTXNWantGraphicsMask
;
132 if ( !UMAHasAquaLayout() )
135 options
|= kTXNAlwaysUseQuickDrawTextMask
;
137 TXNInitTextension(fontDescriptions
, noOfFontDescriptions
, options
);
141 sUMASystemInitialized
= true ;
146 Boolean CanUseATSUI()
149 OSErr err = Gestalt(gestaltATSUVersion, &result);
150 return (err == noErr);
154 long UMAGetProcessMode()
157 ProcessInfoRec processinfo
;
158 ProcessSerialNumber procno
;
160 procno
.highLongOfPSN
= NULL
;
161 procno
.lowLongOfPSN
= kCurrentProcess
;
162 processinfo
.processInfoLength
= sizeof(ProcessInfoRec
);
163 processinfo
.processName
= NULL
;
164 processinfo
.processAppSpec
= NULL
;
166 err
= ::GetProcessInformation( &procno
, &processinfo
) ;
167 wxASSERT( err
== noErr
) ;
168 return processinfo
.processMode
;
171 bool UMAGetProcessModeDoesActivateOnFGSwitch()
173 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch
;
178 void UMASetMenuTitle( MenuRef menu
, StringPtr title
)
182 long size = GetHandleSize( (Handle) menu ) ;
183 const long headersize = 14 ;
184 int oldlen = (**menu).menuData[0] + 1;
185 int newlen = title[0] + 1 ;
187 if ( oldlen < newlen )
189 // enlarge before adjusting
190 SetHandleSize( (Handle) menu , size + (newlen - oldlen ) );
193 if ( oldlen != newlen )
194 memmove( (char*) (**menu).menuData + newlen , (char*) (**menu).menuData + oldlen , size - headersize - oldlen ) ;
196 memcpy( (char*) (**menu).menuData , title , newlen ) ;
197 if ( oldlen > newlen )
200 SetHandleSize( (Handle) menu , size + (newlen - oldlen ) ) ;
204 SetMenuTitle( menu
, title
) ;
208 UInt32
UMAMenuEvent( EventRecord
*inEvent
)
210 return MenuEvent( inEvent
) ;
213 void UMAEnableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
)
215 EnableMenuItem( inMenu
, inItem
) ;
218 void UMADisableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
)
220 DisableMenuItem( inMenu
, inItem
) ;
223 void UMAAppendSubMenuItem( MenuRef menu
, StringPtr l
, SInt16 id
)
225 MacAppendMenu(menu
, l
);
226 SetMenuItemHierarchicalID( menu
, CountMenuItems( menu
) , id
) ;
229 void UMAInsertSubMenuItem( MenuRef menu
, StringPtr l
, MenuItemIndex item
, SInt16 id
)
231 MacInsertMenuItem(menu
, l
, item
);
232 SetMenuItemHierarchicalID( menu
, item
, id
) ;
236 void UMAAppendMenuItem( MenuRef menu
, StringPtr l
, SInt16 key
, UInt8 modifiers
)
239 memcpy( label
, l
, l
[0]+1 ) ;
244 label
[++pos
] = toupper( key
);
247 MacAppendMenu( menu
, label
) ;
250 void UMAInsertMenuItem( MenuRef menu
, StringPtr l
, MenuItemIndex item
, SInt16 key
, UInt8 modifiers
)
253 memcpy( label
, l
, l
[0]+1 ) ;
258 label
[++pos
] = toupper( key
);
261 MacInsertMenuItem( menu
, label
, item
) ;
266 int gPrOpenCounter
= 0 ;
268 OSStatus
UMAPrOpen(void *macPrintSession
)
273 if ( gPrOpenCounter
== 1 )
277 wxASSERT( err
== noErr
) ;
281 OSStatus err
= noErr
;
283 if ( gPrOpenCounter
== 1 )
285 #if PM_USE_SESSION_APIS
286 err
= PMCreateSession((PMPrintSession
*)macPrintSession
) ;
290 wxASSERT( err
== noErr
) ;
296 OSStatus
UMAPrClose(void *macPrintSession
)
300 wxASSERT( gPrOpenCounter
>= 1 ) ;
301 if ( gPrOpenCounter
== 1 )
305 wxASSERT( err
== noErr
) ;
310 OSStatus err
= noErr
;
311 wxASSERT( gPrOpenCounter
>= 1 ) ;
312 if ( gPrOpenCounter
== 1 )
314 #if PM_USE_SESSION_APIS
315 err
= PMRelease(*(PMPrintSession
*)macPrintSession
) ;
316 *(PMPrintSession
*)macPrintSession
= kPMNoReference
;
328 pascal QDGlobalsPtr
GetQDGlobalsPtr (void) ;
329 pascal QDGlobalsPtr
GetQDGlobalsPtr (void)
331 return QDGlobalsPtr (* (Ptr
*) LMGetCurrentA5 ( ) - 0xCA);
336 void UMAShowWatchCursor()
340 CursHandle watchFob
= GetCursor (watchCursor
);
347 // Cursor preservedArrow;
348 // GetQDGlobalsArrow (&preservedArrow);
349 // SetQDGlobalsArrow (*watchFob);
351 // SetQDGlobalsArrow (&preservedArrow);
352 SetCursor (*watchFob
);
354 SetCursor (*watchFob
);
359 void UMAShowArrowCursor()
363 SetCursor (GetQDGlobalsArrow (&arrow
));
365 SetCursor (&(qd
.arrow
));
371 GrafPtr
UMAGetWindowPort( WindowRef inWindowRef
)
373 wxASSERT( inWindowRef
!= NULL
) ;
375 return (GrafPtr
) GetWindowPort( inWindowRef
) ;
377 return (GrafPtr
) inWindowRef
;
381 void UMADisposeWindow( WindowRef inWindowRef
)
383 wxASSERT( inWindowRef
!= NULL
) ;
384 DisposeWindow( inWindowRef
) ;
387 void UMASetWTitleC( WindowRef inWindowRef
, const char *title
)
390 strncpy( (char*)ptitle
, title
, 96 ) ;
393 c2pstrcpy( ptitle
, (char *)ptitle
) ;
395 c2pstr( (char*)ptitle
) ;
397 SetWTitle( inWindowRef
, ptitle
) ;
400 void UMAGetWTitleC( WindowRef inWindowRef
, char *title
)
402 GetWTitle( inWindowRef
, (unsigned char*)title
) ;
404 p2cstrcpy( title
, (unsigned char *)title
) ;
406 p2cstr( (unsigned char*)title
) ;
410 // appearance additions
412 void UMAActivateControl( ControlHandle inControl
)
414 // we have to add the control after again to the update rgn
415 // otherwise updates get lost
416 if ( !IsControlActive( inControl
) )
418 bool visible
= IsControlVisible( inControl
) ;
420 SetControlVisibility( inControl
, false , false ) ;
421 ::ActivateControl( inControl
) ;
423 SetControlVisibility( inControl
, true , false ) ;
425 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
430 void UMADrawControl( ControlHandle inControl
)
432 WindowRef theWindow
= GetControlOwner(inControl
) ;
433 RgnHandle updateRgn
= NewRgn() ;
434 GetWindowUpdateRgn( theWindow
, updateRgn
) ;
435 Point zero
= { 0 , 0 } ;
436 LocalToGlobal( &zero
) ;
437 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
438 ::DrawControlInCurrentPort( inControl
) ;
439 InvalWindowRgn( theWindow
, updateRgn
) ;
440 DisposeRgn( updateRgn
) ;
443 void UMAMoveControl( ControlHandle inControl
, short x
, short y
)
445 bool visible
= IsControlVisible( inControl
) ;
447 SetControlVisibility( inControl
, false , false ) ;
449 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
451 ::MoveControl( inControl
, x
, y
) ;
453 SetControlVisibility( inControl
, true , false ) ;
455 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
459 void UMASizeControl( ControlHandle inControl
, short x
, short y
)
461 bool visible
= IsControlVisible( inControl
) ;
463 SetControlVisibility( inControl
, false , false ) ;
465 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
467 ::SizeControl( inControl
, x
, y
) ;
469 SetControlVisibility( inControl
, true , false ) ;
471 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
475 void UMADeactivateControl( ControlHandle inControl
)
477 // we have to add the control after again to the update rgn
478 // otherwise updates get lost
479 bool visible
= IsControlVisible( inControl
) ;
481 SetControlVisibility( inControl
, false , false ) ;
482 ::DeactivateControl( inControl
) ;
484 SetControlVisibility( inControl
, true , false ) ;
486 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
489 // shows the control and adds the region to the update region
490 void UMAShowControl (ControlHandle inControl
)
492 SetControlVisibility( inControl
, true , false ) ;
494 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
497 // shows the control and adds the region to the update region
498 void UMAHideControl (ControlHandle inControl
)
500 SetControlVisibility( inControl
, false , false ) ;
502 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
505 OSErr
UMASetKeyboardFocus (WindowPtr inWindow
,
506 ControlHandle inControl
,
507 ControlFocusPart inPart
)
513 SetPortWindowPort( inWindow
) ;
515 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
524 void UMAUpdateControls( WindowPtr inWindow
, RgnHandle inRgn
)
526 RgnHandle updateRgn
= NewRgn() ;
527 GetWindowUpdateRgn( inWindow
, updateRgn
) ;
529 Point zero
= { 0 , 0 } ;
530 LocalToGlobal( &zero
) ;
531 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
533 UpdateControls( inWindow
, inRgn
) ;
534 InvalWindowRgn( inWindow
, updateRgn
) ;
535 DisposeRgn( updateRgn
) ;
539 bool UMAIsWindowFloating( WindowRef inWindow
)
543 GetWindowClass( inWindow
, &cl
) ;
544 return cl
== kFloatingWindowClass
;
547 bool UMAIsWindowModal( WindowRef inWindow
)
551 GetWindowClass( inWindow
, &cl
) ;
552 return cl
< kFloatingWindowClass
;
557 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
561 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
562 // if ( inActivate != isHightlited )
565 SetPortWindowPort( inWindowRef
) ;
566 HiliteWindow( inWindowRef
, inActivate
) ;
567 ControlHandle control
= NULL
;
568 ::GetRootControl( inWindowRef
, & control
) ;
572 UMAActivateControl( control
) ;
574 UMADeactivateControl( control
) ;
579 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
581 return ::DrawThemePlacard( inRect
, inState
) ;
585 static OSStatus helpMenuStatus
= noErr
;
586 static MenuItemIndex firstCustomItemIndex
= 0 ;
589 OSStatus
UMAGetHelpMenu(
590 MenuRef
* outHelpMenu
,
591 MenuItemIndex
* outFirstCustomItemIndex
)
594 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
596 MenuRef helpMenuHandle
;
597 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
598 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
600 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
602 if ( outFirstCustomItemIndex
)
604 *outFirstCustomItemIndex
= firstCustomItemIndex
;
606 *outHelpMenu
= helpMenuHandle
;
607 return helpMenuStatus
;
611 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
617 wxMacPortStateHelper::wxMacPortStateHelper()
622 void wxMacPortStateHelper::Setup( GrafPtr newport
)
624 GetPort( &m_oldPort
) ;
626 wxASSERT_MSG( m_clip
== NULL
, "Cannot call setup twice" ) ;
629 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
630 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
631 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
632 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
633 GetThemeDrawingState( &m_drawingState
) ;
634 m_currentPort
= newport
;
636 void wxMacPortStateHelper::Clear()
640 DisposeRgn( m_clip
) ;
641 DisposeThemeDrawingState( m_drawingState
) ;
646 wxMacPortStateHelper::~wxMacPortStateHelper()
650 SetPort( m_currentPort
) ;
652 DisposeRgn( m_clip
) ;
653 TextFont( m_textFont
);
654 TextSize( m_textSize
);
655 TextFace( m_textStyle
);
656 TextMode( m_textMode
);
657 SetThemeDrawingState( m_drawingState
, true ) ;
658 SetPort( m_oldPort
) ;