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
)
226 memcpy( label
, l
, l
[0]+1 ) ;
227 // hardcoded adding of the submenu combination for mac
229 int theEnd
= label
[0] + 1;
231 theEnd
= 251; // mac allows only 255 characters
232 label
[theEnd
++] = '/';
233 label
[theEnd
++] = hMenuCmd
;
234 label
[theEnd
++] = '!';
235 label
[theEnd
++] = id
;
236 label
[theEnd
] = 0x00;
238 MacAppendMenu(menu
, label
);
241 void UMAInsertSubMenuItem( MenuRef menu
, StringPtr l
, MenuItemIndex item
, SInt16 id
)
244 memcpy( label
, l
, l
[0]+1 ) ;
245 // hardcoded adding of the submenu combination for mac
247 int theEnd
= label
[0] + 1;
249 theEnd
= 251; // mac allows only 255 characters
250 label
[theEnd
++] = '/';
251 label
[theEnd
++] = hMenuCmd
;
252 label
[theEnd
++] = '!';
253 label
[theEnd
++] = id
;
254 label
[theEnd
] = 0x00;
256 MacInsertMenuItem(menu
, label
, item
);
259 void UMAAppendMenuItem( MenuRef menu
, StringPtr l
, SInt16 key
, UInt8 modifiers
)
262 memcpy( label
, l
, l
[0]+1 ) ;
267 label
[++pos
] = toupper( key
);
270 MacAppendMenu( menu
, label
) ;
273 void UMAInsertMenuItem( MenuRef menu
, StringPtr l
, MenuItemIndex item
, SInt16 key
, UInt8 modifiers
)
276 memcpy( label
, l
, l
[0]+1 ) ;
281 label
[++pos
] = toupper( key
);
284 MacInsertMenuItem( menu
, label
, item
) ;
289 int gPrOpenCounter
= 0 ;
291 OSStatus
UMAPrOpen(void *macPrintSession
)
296 if ( gPrOpenCounter
== 1 )
300 wxASSERT( err
== noErr
) ;
304 OSStatus err
= noErr
;
306 if ( gPrOpenCounter
== 1 )
308 #if PM_USE_SESSION_APIS
309 err
= PMCreateSession((PMPrintSession
*)macPrintSession
) ;
313 wxASSERT( err
== noErr
) ;
319 OSStatus
UMAPrClose(void *macPrintSession
)
323 wxASSERT( gPrOpenCounter
>= 1 ) ;
324 if ( gPrOpenCounter
== 1 )
328 wxASSERT( err
== noErr
) ;
333 OSStatus err
= noErr
;
334 wxASSERT( gPrOpenCounter
>= 1 ) ;
335 if ( gPrOpenCounter
== 1 )
337 #if PM_USE_SESSION_APIS
338 err
= PMRelease(*(PMPrintSession
*)macPrintSession
) ;
339 *(PMPrintSession
*)macPrintSession
= kPMNoReference
;
351 pascal QDGlobalsPtr
GetQDGlobalsPtr (void) ;
352 pascal QDGlobalsPtr
GetQDGlobalsPtr (void)
354 return QDGlobalsPtr (* (Ptr
*) LMGetCurrentA5 ( ) - 0xCA);
359 void UMAShowWatchCursor()
363 CursHandle watchFob
= GetCursor (watchCursor
);
370 // Cursor preservedArrow;
371 // GetQDGlobalsArrow (&preservedArrow);
372 // SetQDGlobalsArrow (*watchFob);
374 // SetQDGlobalsArrow (&preservedArrow);
375 SetCursor (*watchFob
);
377 SetCursor (*watchFob
);
382 void UMAShowArrowCursor()
386 SetCursor (GetQDGlobalsArrow (&arrow
));
388 SetCursor (&(qd
.arrow
));
394 GrafPtr
UMAGetWindowPort( WindowRef inWindowRef
)
396 wxASSERT( inWindowRef
!= NULL
) ;
398 return (GrafPtr
) GetWindowPort( inWindowRef
) ;
400 return (GrafPtr
) inWindowRef
;
404 void UMADisposeWindow( WindowRef inWindowRef
)
406 wxASSERT( inWindowRef
!= NULL
) ;
407 DisposeWindow( inWindowRef
) ;
410 void UMASetWTitleC( WindowRef inWindowRef
, const char *title
)
413 strncpy( (char*)ptitle
, title
, 96 ) ;
416 c2pstrcpy( ptitle
, (char *)ptitle
) ;
418 c2pstr( (char*)ptitle
) ;
420 SetWTitle( inWindowRef
, ptitle
) ;
423 void UMAGetWTitleC( WindowRef inWindowRef
, char *title
)
425 GetWTitle( inWindowRef
, (unsigned char*)title
) ;
427 p2cstrcpy( title
, (unsigned char *)title
) ;
429 p2cstr( (unsigned char*)title
) ;
433 // appearance additions
435 void UMAActivateControl( ControlHandle inControl
)
437 // we have to add the control after again to the update rgn
438 // otherwise updates get lost
439 if ( !IsControlActive( inControl
) )
441 bool visible
= IsControlVisible( inControl
) ;
443 SetControlVisibility( inControl
, false , false ) ;
444 ::ActivateControl( inControl
) ;
446 SetControlVisibility( inControl
, true , false ) ;
448 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
453 void UMADrawControl( ControlHandle inControl
)
455 WindowRef theWindow
= GetControlOwner(inControl
) ;
456 RgnHandle updateRgn
= NewRgn() ;
457 GetWindowUpdateRgn( theWindow
, updateRgn
) ;
458 Point zero
= { 0 , 0 } ;
459 LocalToGlobal( &zero
) ;
460 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
461 ::DrawControlInCurrentPort( inControl
) ;
462 InvalWindowRgn( theWindow
, updateRgn
) ;
463 DisposeRgn( updateRgn
) ;
466 void UMAMoveControl( ControlHandle inControl
, short x
, short y
)
468 bool visible
= IsControlVisible( inControl
) ;
470 SetControlVisibility( inControl
, false , false ) ;
472 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
474 ::MoveControl( inControl
, x
, y
) ;
476 SetControlVisibility( inControl
, true , false ) ;
478 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
482 void UMASizeControl( ControlHandle inControl
, short x
, short y
)
484 bool visible
= IsControlVisible( inControl
) ;
486 SetControlVisibility( inControl
, false , false ) ;
488 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
490 ::SizeControl( inControl
, x
, y
) ;
492 SetControlVisibility( inControl
, true , false ) ;
494 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
498 void UMADeactivateControl( ControlHandle inControl
)
500 // we have to add the control after again to the update rgn
501 // otherwise updates get lost
502 bool visible
= IsControlVisible( inControl
) ;
504 SetControlVisibility( inControl
, false , false ) ;
505 ::DeactivateControl( inControl
) ;
507 SetControlVisibility( inControl
, true , false ) ;
509 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
512 // shows the control and adds the region to the update region
513 void UMAShowControl (ControlHandle inControl
)
515 SetControlVisibility( inControl
, true , false ) ;
517 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
520 // shows the control and adds the region to the update region
521 void UMAHideControl (ControlHandle inControl
)
523 SetControlVisibility( inControl
, false , false ) ;
525 InvalWindowRect(GetControlOwner(inControl
),GetControlBounds(inControl
,&ctrlBounds
) ) ;
528 OSErr
UMASetKeyboardFocus (WindowPtr inWindow
,
529 ControlHandle inControl
,
530 ControlFocusPart inPart
)
536 SetPortWindowPort( inWindow
) ;
538 err
= SetKeyboardFocus( inWindow
, inControl
, inPart
) ;
547 void UMAUpdateControls( WindowPtr inWindow
, RgnHandle inRgn
)
549 RgnHandle updateRgn
= NewRgn() ;
550 GetWindowUpdateRgn( inWindow
, updateRgn
) ;
552 Point zero
= { 0 , 0 } ;
553 LocalToGlobal( &zero
) ;
554 OffsetRgn( updateRgn
, -zero
.h
, -zero
.v
) ;
556 UpdateControls( inWindow
, inRgn
) ;
557 InvalWindowRgn( inWindow
, updateRgn
) ;
558 DisposeRgn( updateRgn
) ;
562 bool UMAIsWindowFloating( WindowRef inWindow
)
566 GetWindowClass( inWindow
, &cl
) ;
567 return cl
== kFloatingWindowClass
;
570 bool UMAIsWindowModal( WindowRef inWindow
)
574 GetWindowClass( inWindow
, &cl
) ;
575 return cl
< kFloatingWindowClass
;
580 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
584 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
585 // if ( inActivate != isHightlited )
588 SetPortWindowPort( inWindowRef
) ;
589 HiliteWindow( inWindowRef
, inActivate
) ;
590 ControlHandle control
= NULL
;
591 ::GetRootControl( inWindowRef
, & control
) ;
595 UMAActivateControl( control
) ;
597 UMADeactivateControl( control
) ;
602 OSStatus
UMADrawThemePlacard( const Rect
*inRect
, ThemeDrawState inState
)
604 return ::DrawThemePlacard( inRect
, inState
) ;
608 static OSStatus helpMenuStatus
= noErr
;
609 static MenuItemIndex firstCustomItemIndex
= 0 ;
612 OSStatus
UMAGetHelpMenu(
613 MenuRef
* outHelpMenu
,
614 MenuItemIndex
* outFirstCustomItemIndex
)
617 return HMGetHelpMenu( outHelpMenu
, outFirstCustomItemIndex
) ;
619 MenuRef helpMenuHandle
;
620 helpMenuStatus
= HMGetHelpMenuHandle( &helpMenuHandle
) ;
621 if ( firstCustomItemIndex
== 0 && helpMenuStatus
== noErr
)
623 firstCustomItemIndex
= CountMenuItems( helpMenuHandle
) + 1 ;
625 if ( outFirstCustomItemIndex
)
627 *outFirstCustomItemIndex
= firstCustomItemIndex
;
629 *outHelpMenu
= helpMenuHandle
;
630 return helpMenuStatus
;
634 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport
)
640 wxMacPortStateHelper::wxMacPortStateHelper()
645 void wxMacPortStateHelper::Setup( GrafPtr newport
)
647 GetPort( &m_oldPort
) ;
649 wxASSERT_MSG( m_clip
== NULL
, "Cannot call setup twice" ) ;
652 m_textFont
= GetPortTextFont( (CGrafPtr
) newport
);
653 m_textSize
= GetPortTextSize( (CGrafPtr
) newport
);
654 m_textStyle
= GetPortTextFace( (CGrafPtr
) newport
);
655 m_textMode
= GetPortTextMode( (CGrafPtr
) newport
);
656 GetThemeDrawingState( &m_drawingState
) ;
657 m_currentPort
= newport
;
659 void wxMacPortStateHelper::Clear()
663 DisposeRgn( m_clip
) ;
664 DisposeThemeDrawingState( m_drawingState
) ;
669 wxMacPortStateHelper::~wxMacPortStateHelper()
673 SetPort( m_currentPort
) ;
675 DisposeRgn( m_clip
) ;
676 TextFont( m_textFont
);
677 TextSize( m_textSize
);
678 TextFace( m_textStyle
);
679 TextMode( m_textMode
);
680 SetThemeDrawingState( m_drawingState
, true ) ;
681 SetPort( m_oldPort
) ;