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() )
107 if ( TXNInitTextension
!= (void*) kUnresolvedCFragSymbolAddress
)
109 FontFamilyID fontId
;
113 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
114 GetFNum( fontName
, &fontId
);
116 TXNMacOSPreferredFontDescription fontDescriptions
[] =
118 { fontId
, (fontSize
<< 16) ,kTXNDefaultFontStyle
, kTXNSystemDefaultEncoding
} ,
120 int noOfFontDescriptions
= sizeof( fontDescriptions
) / sizeof(TXNMacOSPreferredFontDescription
) ;
122 --noOfFontDescriptions
;
124 // kTXNAlwaysUseQuickDrawTextMask might be desirable because of speed increases but it crashes the app under OS X upon key stroke
125 TXNInitTextension(fontDescriptions
, noOfFontDescriptions
, ( kTXNWantMoviesMask
| kTXNWantSoundMask
| kTXNWantGraphicsMask
));
129 Gestalt( gestaltMenuMgrAttr
, &menuMgrAttr
) ;
130 if ( menuMgrAttr
& gestaltMenuMgrAquaLayoutMask
)
131 sUMAHasAquaLayout
= true ;
132 sUMASystemInitialized
= true ;
137 Boolean CanUseATSUI()
140 OSErr err = Gestalt(gestaltATSUVersion, &result);
141 return (err == noErr);
145 long UMAGetProcessMode()
148 ProcessInfoRec processinfo
;
149 ProcessSerialNumber procno
;
151 procno
.highLongOfPSN
= NULL
;
152 procno
.lowLongOfPSN
= kCurrentProcess
;
153 processinfo
.processInfoLength
= sizeof(ProcessInfoRec
);
154 processinfo
.processName
= NULL
;
155 processinfo
.processAppSpec
= NULL
;
157 err
= ::GetProcessInformation( &procno
, &processinfo
) ;
158 wxASSERT( err
== noErr
) ;
159 return processinfo
.processMode
;
162 bool UMAGetProcessModeDoesActivateOnFGSwitch()
164 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch
;
169 void UMASetMenuTitle( MenuRef menu
, StringPtr title
)
173 long size = GetHandleSize( (Handle) menu ) ;
174 const long headersize = 14 ;
175 int oldlen = (**menu).menuData[0] + 1;
176 int newlen = title[0] + 1 ;
178 if ( oldlen < newlen )
180 // enlarge before adjusting
181 SetHandleSize( (Handle) menu , size + (newlen - oldlen ) );
184 if ( oldlen != newlen )
185 memmove( (char*) (**menu).menuData + newlen , (char*) (**menu).menuData + oldlen , size - headersize - oldlen ) ;
187 memcpy( (char*) (**menu).menuData , title , newlen ) ;
188 if ( oldlen > newlen )
191 SetHandleSize( (Handle) menu , size + (newlen - oldlen ) ) ;
195 SetMenuTitle( menu
, title
) ;
199 UInt32
UMAMenuEvent( EventRecord
*inEvent
)
201 return MenuEvent( inEvent
) ;
204 void UMAEnableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
)
206 EnableMenuItem( inMenu
, inItem
) ;
209 void UMADisableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
)
211 DisableMenuItem( inMenu
, inItem
) ;
214 void UMAAppendSubMenuItem( MenuRef menu
, StringPtr l
, SInt16 id
)
217 memcpy( label
, l
, l
[0]+1 ) ;
218 // hardcoded adding of the submenu combination for mac
220 int theEnd
= label
[0] + 1;
222 theEnd
= 251; // mac allows only 255 characters
223 label
[theEnd
++] = '/';
224 label
[theEnd
++] = hMenuCmd
;
225 label
[theEnd
++] = '!';
226 label
[theEnd
++] = id
;
227 label
[theEnd
] = 0x00;
229 MacAppendMenu(menu
, label
);
232 void UMAInsertSubMenuItem( MenuRef menu
, StringPtr l
, MenuItemIndex item
, SInt16 id
)
235 memcpy( label
, l
, l
[0]+1 ) ;
236 // hardcoded adding of the submenu combination for mac
238 int theEnd
= label
[0] + 1;
240 theEnd
= 251; // mac allows only 255 characters
241 label
[theEnd
++] = '/';
242 label
[theEnd
++] = hMenuCmd
;
243 label
[theEnd
++] = '!';
244 label
[theEnd
++] = id
;
245 label
[theEnd
] = 0x00;
247 MacInsertMenuItem(menu
, label
, item
);
250 void UMAAppendMenuItem( MenuRef menu
, StringPtr l
, SInt16 key
, UInt8 modifiers
)
253 memcpy( label
, l
, l
[0]+1 ) ;
258 label
[++pos
] = toupper( key
);
261 MacAppendMenu( menu
, label
) ;
264 void UMAInsertMenuItem( MenuRef menu
, StringPtr l
, MenuItemIndex item
, SInt16 key
, UInt8 modifiers
)
267 memcpy( label
, l
, l
[0]+1 ) ;
272 label
[++pos
] = toupper( key
);
275 MacInsertMenuItem( menu
, label
, item
) ;
280 int gPrOpenCounter
= 0 ;
282 OSStatus
UMAPrOpen(void *macPrintSession
)
287 if ( gPrOpenCounter
== 1 )
291 wxASSERT( err
== noErr
) ;
295 OSStatus err
= noErr
;
297 if ( gPrOpenCounter
== 1 )
299 #if PM_USE_SESSION_APIS
300 err
= PMCreateSession((PMPrintSession
*)macPrintSession
) ;
304 wxASSERT( err
== noErr
) ;
310 OSStatus
UMAPrClose(void *macPrintSession
)
314 wxASSERT( gPrOpenCounter
>= 1 ) ;
315 if ( gPrOpenCounter
== 1 )
319 wxASSERT( err
== noErr
) ;
324 OSStatus err
= noErr
;
325 wxASSERT( gPrOpenCounter
>= 1 ) ;
326 if ( gPrOpenCounter
== 1 )
328 #if PM_USE_SESSION_APIS
329 err
= PMRelease(*(PMPrintSession
*)macPrintSession
) ;
330 *(PMPrintSession
*)macPrintSession
= kPMNoReference
;
342 pascal QDGlobalsPtr
GetQDGlobalsPtr (void) ;
343 pascal QDGlobalsPtr
GetQDGlobalsPtr (void)
345 return QDGlobalsPtr (* (Ptr
*) LMGetCurrentA5 ( ) - 0xCA);
350 void UMAShowWatchCursor()
354 CursHandle watchFob
= GetCursor (watchCursor
);
361 // Cursor preservedArrow;
362 // GetQDGlobalsArrow (&preservedArrow);
363 // SetQDGlobalsArrow (*watchFob);
365 // SetQDGlobalsArrow (&preservedArrow);
366 SetCursor (*watchFob
);
368 SetCursor (*watchFob
);
373 void UMAShowArrowCursor()
377 SetCursor (GetQDGlobalsArrow (&arrow
));
379 SetCursor (&(qd
.arrow
));
385 GrafPtr
UMAGetWindowPort( WindowRef inWindowRef
)
387 wxASSERT( inWindowRef
!= NULL
) ;
389 return (GrafPtr
) GetWindowPort( inWindowRef
) ;
391 return (GrafPtr
) inWindowRef
;
395 void UMADisposeWindow( WindowRef inWindowRef
)
397 wxASSERT( inWindowRef
!= NULL
) ;
398 DisposeWindow( inWindowRef
) ;
401 void UMASetWTitleC( WindowRef inWindowRef
, const char *title
)
404 strncpy( (char*)ptitle
, title
, 96 ) ;
407 c2pstrcpy( ptitle
, (char *)ptitle
) ;
409 c2pstr( (char*)ptitle
) ;
411 SetWTitle( inWindowRef
, ptitle
) ;
414 void UMAGetWTitleC( WindowRef inWindowRef
, char *title
)
416 GetWTitle( inWindowRef
, (unsigned char*)title
) ;
418 p2cstrcpy( title
, (unsigned char *)title
) ;
420 p2cstr( (unsigned char*)title
) ;
424 // appearance additions
426 void UMAActivateControl( ControlHandle inControl
)
428 // we have to add the control after again to the update rgn
429 // otherwise updates get lost
430 if ( !IsControlActive( inControl
) )
432 bool visible
= IsControlVisible( inControl
) ;
434 SetControlVisibility( inControl
, false , false ) ;
435 ::ActivateControl( inControl
) ;
437 SetControlVisibility( inControl
, true , false ) ;
439 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
444 void UMADrawControl( ControlHandle inControl
)
446 WindowRef theWindow
= GetControlOwner(inControl
) ;
447 RgnHandle updateRgn
= NewRgn() ;
448 GetWindowUpdateRgn( theWindow
, updateRgn
) ;
449 Point zero
= { 0 , 0 } ;
450 LocalToGlobal( &zero
) ;
451 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
452 ::DrawControlInCurrentPort( inControl
) ;
453 InvalWindowRgn( theWindow
, updateRgn
) ;
454 DisposeRgn( updateRgn
) ;
457 void UMAMoveControl( ControlHandle inControl
, short x
, short y
)
459 bool visible
= IsControlVisible( inControl
) ;
461 SetControlVisibility( inControl
, false , false ) ;
463 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
465 ::MoveControl( inControl
, x
, y
) ;
467 SetControlVisibility( inControl
, true , false ) ;
469 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
473 void UMASizeControl( ControlHandle inControl
, short x
, short y
)
475 bool visible
= IsControlVisible( inControl
) ;
477 SetControlVisibility( inControl
, false , false ) ;
479 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
481 ::SizeControl( inControl
, x
, y
) ;
483 SetControlVisibility( inControl
, true , false ) ;
485 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
489 void UMADeactivateControl( ControlHandle inControl
)
491 // we have to add the control after again to the update rgn
492 // otherwise updates get lost
493 bool visible
= IsControlVisible( inControl
) ;
495 SetControlVisibility( inControl
, false , false ) ;
496 ::DeactivateControl( inControl
) ;
498 SetControlVisibility( inControl
, true , false ) ;
500 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
503 // shows the control and adds the region to the update region
504 void UMAShowControl (ControlHandle inControl
)
506 SetControlVisibility( inControl
, true , false ) ;
508 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
511 // shows the control and adds the region to the update region
512 void UMAHideControl (ControlHandle inControl
)
514 SetControlVisibility( inControl
, false , false ) ;
516 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
519 OSErr
UMASetKeyboardFocus (WindowPtr inWindow
,
520 ControlHandle inControl
,
521 ControlFocusPart inPart
)
527 SetPortWindowPort( inWindow
) ;
529 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
538 void UMAUpdateControls( WindowPtr inWindow
, RgnHandle inRgn
)
540 RgnHandle updateRgn
= NewRgn() ;
541 GetWindowUpdateRgn( inWindow
, updateRgn
) ;
543 Point zero
= { 0 , 0 } ;
544 LocalToGlobal( &zero
) ;
545 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
547 UpdateControls( inWindow
, inRgn
) ;
548 InvalWindowRgn( inWindow
, updateRgn
) ;
549 DisposeRgn( updateRgn
) ;
553 bool UMAIsWindowFloating( WindowRef inWindow
)
557 GetWindowClass( inWindow
, &cl
) ;
558 return cl
== kFloatingWindowClass
;
561 bool UMAIsWindowModal( WindowRef inWindow
)
565 GetWindowClass( inWindow
, &cl
) ;
566 return cl
< kFloatingWindowClass
;
571 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
575 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
576 // if ( inActivate != isHightlited )
579 SetPortWindowPort( inWindowRef
) ;
580 HiliteWindow( inWindowRef
, inActivate
) ;
581 ControlHandle control
= NULL
;
582 ::GetRootControl( inWindowRef
, & control
) ;
586 UMAActivateControl( control
) ;
588 UMADeactivateControl( control
) ;
593 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
595 return ::DrawThemePlacard( inRect
, inState
) ;
599 static OSStatus helpMenuStatus
= noErr
;
600 static MenuItemIndex firstCustomItemIndex
= 0 ;
603 OSStatus
UMAGetHelpMenu(
604 MenuRef
* outHelpMenu
,
605 MenuItemIndex
* outFirstCustomItemIndex
)
608 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
610 MenuRef helpMenuHandle
;
611 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
612 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
614 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
616 if ( outFirstCustomItemIndex
)
618 *outFirstCustomItemIndex
= firstCustomItemIndex
;
620 *outHelpMenu
= helpMenuHandle
;
621 return helpMenuStatus
;
625 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
631 wxMacPortStateHelper::wxMacPortStateHelper()
636 void wxMacPortStateHelper::Setup( GrafPtr newport
)
638 GetPort( &m_oldPort
) ;
640 wxASSERT_MSG( m_clip
== NULL
, "Cannot call setup twice" ) ;
643 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
644 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
645 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
646 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
647 GetThemeDrawingState( &m_drawingState
) ;
648 m_currentPort
= newport
;
650 void wxMacPortStateHelper::Clear()
654 DisposeRgn( m_clip
) ;
655 DisposeThemeDrawingState( m_drawingState
) ;
660 wxMacPortStateHelper::~wxMacPortStateHelper()
664 SetPort( m_currentPort
) ;
666 DisposeRgn( m_clip
) ;
667 TextFont( m_textFont
);
668 TextSize( m_textSize
);
669 TextFace( m_textStyle
);
670 TextMode( m_textMode
);
671 SetThemeDrawingState( m_drawingState
, true ) ;
672 SetPort( m_oldPort
) ;