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
) ;
121 #if 0 // TARGET_CARBON
122 --noOfFontDescriptions
;
124 // kTXNAlwaysUseQuickDrawTextMask might be desirable because of speed increases but it crashes the app under OS X upon key stroke
125 OptionBits options
= kTXNWantMoviesMask
| kTXNWantSoundMask
| kTXNWantGraphicsMask
;
127 if ( !UMAHasAquaLayout() )
130 options
|= kTXNAlwaysUseQuickDrawTextMask
;
132 TXNInitTextension(fontDescriptions
, noOfFontDescriptions
, options
);
136 Gestalt( gestaltMenuMgrAttr
, &menuMgrAttr
) ;
137 if ( menuMgrAttr
& gestaltMenuMgrAquaLayoutMask
)
138 sUMAHasAquaLayout
= true ;
139 sUMASystemInitialized
= true ;
144 Boolean CanUseATSUI()
147 OSErr err = Gestalt(gestaltATSUVersion, &result);
148 return (err == noErr);
152 long UMAGetProcessMode()
155 ProcessInfoRec processinfo
;
156 ProcessSerialNumber procno
;
158 procno
.highLongOfPSN
= NULL
;
159 procno
.lowLongOfPSN
= kCurrentProcess
;
160 processinfo
.processInfoLength
= sizeof(ProcessInfoRec
);
161 processinfo
.processName
= NULL
;
162 processinfo
.processAppSpec
= NULL
;
164 err
= ::GetProcessInformation( &procno
, &processinfo
) ;
165 wxASSERT( err
== noErr
) ;
166 return processinfo
.processMode
;
169 bool UMAGetProcessModeDoesActivateOnFGSwitch()
171 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch
;
176 void UMASetMenuTitle( MenuRef menu
, StringPtr title
)
180 long size = GetHandleSize( (Handle) menu ) ;
181 const long headersize = 14 ;
182 int oldlen = (**menu).menuData[0] + 1;
183 int newlen = title[0] + 1 ;
185 if ( oldlen < newlen )
187 // enlarge before adjusting
188 SetHandleSize( (Handle) menu , size + (newlen - oldlen ) );
191 if ( oldlen != newlen )
192 memmove( (char*) (**menu).menuData + newlen , (char*) (**menu).menuData + oldlen , size - headersize - oldlen ) ;
194 memcpy( (char*) (**menu).menuData , title , newlen ) ;
195 if ( oldlen > newlen )
198 SetHandleSize( (Handle) menu , size + (newlen - oldlen ) ) ;
202 SetMenuTitle( menu
, title
) ;
206 UInt32
UMAMenuEvent( EventRecord
*inEvent
)
208 return MenuEvent( inEvent
) ;
211 void UMAEnableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
)
213 EnableMenuItem( inMenu
, inItem
) ;
216 void UMADisableMenuItem( MenuRef inMenu
, MenuItemIndex inItem
)
218 DisableMenuItem( inMenu
, inItem
) ;
221 void UMAAppendSubMenuItem( MenuRef menu
, StringPtr l
, SInt16 id
)
224 memcpy( label
, l
, l
[0]+1 ) ;
225 // hardcoded adding of the submenu combination for mac
227 int theEnd
= label
[0] + 1;
229 theEnd
= 251; // mac allows only 255 characters
230 label
[theEnd
++] = '/';
231 label
[theEnd
++] = hMenuCmd
;
232 label
[theEnd
++] = '!';
233 label
[theEnd
++] = id
;
234 label
[theEnd
] = 0x00;
236 MacAppendMenu(menu
, label
);
239 void UMAInsertSubMenuItem( MenuRef menu
, StringPtr l
, MenuItemIndex item
, SInt16 id
)
242 memcpy( label
, l
, l
[0]+1 ) ;
243 // hardcoded adding of the submenu combination for mac
245 int theEnd
= label
[0] + 1;
247 theEnd
= 251; // mac allows only 255 characters
248 label
[theEnd
++] = '/';
249 label
[theEnd
++] = hMenuCmd
;
250 label
[theEnd
++] = '!';
251 label
[theEnd
++] = id
;
252 label
[theEnd
] = 0x00;
254 MacInsertMenuItem(menu
, label
, item
);
257 void UMAAppendMenuItem( MenuRef menu
, StringPtr l
, SInt16 key
, UInt8 modifiers
)
260 memcpy( label
, l
, l
[0]+1 ) ;
265 label
[++pos
] = toupper( key
);
268 MacAppendMenu( menu
, label
) ;
271 void UMAInsertMenuItem( MenuRef menu
, StringPtr l
, MenuItemIndex item
, SInt16 key
, UInt8 modifiers
)
274 memcpy( label
, l
, l
[0]+1 ) ;
279 label
[++pos
] = toupper( key
);
282 MacInsertMenuItem( menu
, label
, item
) ;
287 int gPrOpenCounter
= 0 ;
289 OSStatus
UMAPrOpen(void *macPrintSession
)
294 if ( gPrOpenCounter
== 1 )
298 wxASSERT( err
== noErr
) ;
302 OSStatus err
= noErr
;
304 if ( gPrOpenCounter
== 1 )
306 #if PM_USE_SESSION_APIS
307 err
= PMCreateSession((PMPrintSession
*)macPrintSession
) ;
311 wxASSERT( err
== noErr
) ;
317 OSStatus
UMAPrClose(void *macPrintSession
)
321 wxASSERT( gPrOpenCounter
>= 1 ) ;
322 if ( gPrOpenCounter
== 1 )
326 wxASSERT( err
== noErr
) ;
331 OSStatus err
= noErr
;
332 wxASSERT( gPrOpenCounter
>= 1 ) ;
333 if ( gPrOpenCounter
== 1 )
335 #if PM_USE_SESSION_APIS
336 err
= PMRelease(*(PMPrintSession
*)macPrintSession
) ;
337 *(PMPrintSession
*)macPrintSession
= kPMNoReference
;
349 pascal QDGlobalsPtr
GetQDGlobalsPtr (void) ;
350 pascal QDGlobalsPtr
GetQDGlobalsPtr (void)
352 return QDGlobalsPtr (* (Ptr
*) LMGetCurrentA5 ( ) - 0xCA);
357 void UMAShowWatchCursor()
361 CursHandle watchFob
= GetCursor (watchCursor
);
368 // Cursor preservedArrow;
369 // GetQDGlobalsArrow (&preservedArrow);
370 // SetQDGlobalsArrow (*watchFob);
372 // SetQDGlobalsArrow (&preservedArrow);
373 SetCursor (*watchFob
);
375 SetCursor (*watchFob
);
380 void UMAShowArrowCursor()
384 SetCursor (GetQDGlobalsArrow (&arrow
));
386 SetCursor (&(qd
.arrow
));
392 GrafPtr
UMAGetWindowPort( WindowRef inWindowRef
)
394 wxASSERT( inWindowRef
!= NULL
) ;
396 return (GrafPtr
) GetWindowPort( inWindowRef
) ;
398 return (GrafPtr
) inWindowRef
;
402 void UMADisposeWindow( WindowRef inWindowRef
)
404 wxASSERT( inWindowRef
!= NULL
) ;
405 DisposeWindow( inWindowRef
) ;
408 void UMASetWTitleC( WindowRef inWindowRef
, const char *title
)
411 strncpy( (char*)ptitle
, title
, 96 ) ;
414 c2pstrcpy( ptitle
, (char *)ptitle
) ;
416 c2pstr( (char*)ptitle
) ;
418 SetWTitle( inWindowRef
, ptitle
) ;
421 void UMAGetWTitleC( WindowRef inWindowRef
, char *title
)
423 GetWTitle( inWindowRef
, (unsigned char*)title
) ;
425 p2cstrcpy( title
, (unsigned char *)title
) ;
427 p2cstr( (unsigned char*)title
) ;
431 // appearance additions
433 void UMAActivateControl( ControlHandle inControl
)
435 // we have to add the control after again to the update rgn
436 // otherwise updates get lost
437 if ( !IsControlActive( inControl
) )
439 bool visible
= IsControlVisible( inControl
) ;
441 SetControlVisibility( inControl
, false , false ) ;
442 ::ActivateControl( inControl
) ;
444 SetControlVisibility( inControl
, true , false ) ;
446 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
451 void UMADrawControl( ControlHandle inControl
)
453 WindowRef theWindow
= GetControlOwner(inControl
) ;
454 RgnHandle updateRgn
= NewRgn() ;
455 GetWindowUpdateRgn( theWindow
, updateRgn
) ;
456 Point zero
= { 0 , 0 } ;
457 LocalToGlobal( &zero
) ;
458 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
459 ::DrawControlInCurrentPort( inControl
) ;
460 InvalWindowRgn( theWindow
, updateRgn
) ;
461 DisposeRgn( updateRgn
) ;
464 void UMAMoveControl( ControlHandle inControl
, short x
, short y
)
466 bool visible
= IsControlVisible( inControl
) ;
468 SetControlVisibility( inControl
, false , false ) ;
470 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
472 ::MoveControl( inControl
, x
, y
) ;
474 SetControlVisibility( inControl
, true , false ) ;
476 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
480 void UMASizeControl( ControlHandle inControl
, short x
, short y
)
482 bool visible
= IsControlVisible( inControl
) ;
484 SetControlVisibility( inControl
, false , false ) ;
486 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
488 ::SizeControl( inControl
, x
, y
) ;
490 SetControlVisibility( inControl
, true , false ) ;
492 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
496 void UMADeactivateControl( ControlHandle inControl
)
498 // we have to add the control after again to the update rgn
499 // otherwise updates get lost
500 bool visible
= IsControlVisible( inControl
) ;
502 SetControlVisibility( inControl
, false , false ) ;
503 ::DeactivateControl( inControl
) ;
505 SetControlVisibility( inControl
, true , false ) ;
507 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
510 // shows the control and adds the region to the update region
511 void UMAShowControl (ControlHandle inControl
)
513 SetControlVisibility( inControl
, true , false ) ;
515 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
518 // shows the control and adds the region to the update region
519 void UMAHideControl (ControlHandle inControl
)
521 SetControlVisibility( inControl
, false , false ) ;
523 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
526 OSErr
UMASetKeyboardFocus (WindowPtr inWindow
,
527 ControlHandle inControl
,
528 ControlFocusPart inPart
)
534 SetPortWindowPort( inWindow
) ;
536 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
545 void UMAUpdateControls( WindowPtr inWindow
, RgnHandle inRgn
)
547 RgnHandle updateRgn
= NewRgn() ;
548 GetWindowUpdateRgn( inWindow
, updateRgn
) ;
550 Point zero
= { 0 , 0 } ;
551 LocalToGlobal( &zero
) ;
552 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
554 UpdateControls( inWindow
, inRgn
) ;
555 InvalWindowRgn( inWindow
, updateRgn
) ;
556 DisposeRgn( updateRgn
) ;
560 bool UMAIsWindowFloating( WindowRef inWindow
)
564 GetWindowClass( inWindow
, &cl
) ;
565 return cl
== kFloatingWindowClass
;
568 bool UMAIsWindowModal( WindowRef inWindow
)
572 GetWindowClass( inWindow
, &cl
) ;
573 return cl
< kFloatingWindowClass
;
578 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
582 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
583 // if ( inActivate != isHightlited )
586 SetPortWindowPort( inWindowRef
) ;
587 HiliteWindow( inWindowRef
, inActivate
) ;
588 ControlHandle control
= NULL
;
589 ::GetRootControl( inWindowRef
, & control
) ;
593 UMAActivateControl( control
) ;
595 UMADeactivateControl( control
) ;
600 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
602 return ::DrawThemePlacard( inRect
, inState
) ;
606 static OSStatus helpMenuStatus
= noErr
;
607 static MenuItemIndex firstCustomItemIndex
= 0 ;
610 OSStatus
UMAGetHelpMenu(
611 MenuRef
* outHelpMenu
,
612 MenuItemIndex
* outFirstCustomItemIndex
)
615 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
617 MenuRef helpMenuHandle
;
618 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
619 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
621 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
623 if ( outFirstCustomItemIndex
)
625 *outFirstCustomItemIndex
= firstCustomItemIndex
;
627 *outHelpMenu
= helpMenuHandle
;
628 return helpMenuStatus
;
632 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
638 wxMacPortStateHelper::wxMacPortStateHelper()
643 void wxMacPortStateHelper::Setup( GrafPtr newport
)
645 GetPort( &m_oldPort
) ;
647 wxASSERT_MSG( m_clip
== NULL
, "Cannot call setup twice" ) ;
650 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
651 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
652 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
653 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
654 GetThemeDrawingState( &m_drawingState
) ;
655 m_currentPort
= newport
;
657 void wxMacPortStateHelper::Clear()
661 DisposeRgn( m_clip
) ;
662 DisposeThemeDrawingState( m_drawingState
) ;
667 wxMacPortStateHelper::~wxMacPortStateHelper()
671 SetPort( m_currentPort
) ;
673 DisposeRgn( m_clip
) ;
674 TextFont( m_textFont
);
675 TextSize( m_textSize
);
676 TextFace( m_textStyle
);
677 TextMode( m_textMode
);
678 SetThemeDrawingState( m_drawingState
, true ) ;
679 SetPort( m_oldPort
) ;