]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/uma.cpp
make sure we catch invalid control refs better
[wxWidgets.git] / src / mac / carbon / uma.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/uma.cpp
3 // Purpose: UMA support
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: The wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_GUI
15
16 #include "wx/toplevel.h"
17 #include "wx/dc.h"
18
19 #ifndef __DARWIN__
20 # include <MacTextEditor.h>
21 # include <Navigation.h>
22 # if defined(TARGET_CARBON)
23 # if PM_USE_SESSION_APIS
24 # include <PMCore.h>
25 # endif
26 # include <PMApplication.h>
27 # else
28 # include <Printing.h>
29 # endif
30 #endif
31
32 #ifndef __DARWIN__
33 # include <Scrap.h>
34 #endif
35
36 #include "wx/mac/uma.h"
37
38 // since we have decided that we only support 8.6 upwards we are
39 // checking for these minimum requirements in the startup code of
40 // the application so all wxWidgets code can safely assume that appearance 1.1
41 // windows manager, control manager, navigation services etc. are
42 // present
43
44 static bool sUMAHasAppearance = false ;
45 static long sUMAAppearanceVersion = 0 ;
46 static long sUMASystemVersion = 0 ;
47 static bool sUMAHasAquaLayout = false ;
48
49 static bool sUMAHasInittedAppearance = false;
50
51 extern int gAGABackgroundColor ;
52
53 bool UMAHasAppearance() { return sUMAHasAppearance ; }
54 long UMAGetAppearanceVersion() { return sUMAAppearanceVersion ; }
55 long UMAGetSystemVersion() { return sUMASystemVersion ; }
56
57 static bool sUMAHasWindowManager = false ;
58 static long sUMAWindowManagerAttr = 0 ;
59
60 bool UMAHasWindowManager() { return sUMAHasWindowManager ; }
61 long UMAGetWindowManagerAttr() { return sUMAWindowManagerAttr ; }
62 bool UMAHasAquaLayout() { return sUMAHasAquaLayout ; }
63
64
65 void UMACleanupToolbox()
66 {
67 if (sUMAHasInittedAppearance)
68 UnregisterAppearanceClient() ;
69
70 if ( NavServicesAvailable() )
71 NavUnload() ;
72
73 if ( TXNTerminateTextension != (void*) kUnresolvedCFragSymbolAddress )
74 TXNTerminateTextension( ) ;
75 }
76
77 void UMAInitToolbox( UInt16 inMoreMastersCalls, bool isEmbedded )
78 {
79 #if !TARGET_CARBON
80 ::MaxApplZone();
81 for (long i = 1; i <= inMoreMastersCalls; i++)
82 ::MoreMasters();
83
84 if (!isEmbedded)
85 {
86 ::InitGraf(&qd.thePort);
87 ::InitFonts();
88 ::InitMenus();
89 ::TEInit();
90 ::InitDialogs(0L);
91 ::FlushEvents(everyEvent, 0);
92 }
93
94 long total,contig;
95 PurgeSpace(&total, &contig);
96 #endif
97
98 ::InitCursor();
99
100 if ( Gestalt(gestaltSystemVersion, &sUMASystemVersion) != noErr)
101 sUMASystemVersion = 0x0000 ;
102
103 long theAppearance ;
104 if ( Gestalt( gestaltAppearanceAttr, &theAppearance ) == noErr )
105 {
106 // If status equals appearanceProcessRegisteredErr it means the
107 // appearance client already was registered (For example if we run
108 // embedded, the host might have registered it). In such a case
109 // we don't unregister it later on.
110
111 sUMAHasAppearance = true ;
112 OSStatus status = RegisterAppearanceClient();
113 if (status != appearanceProcessRegisteredErr)
114 {
115 // Appearance client wasn't registered yet.
116 sUMAHasInittedAppearance = true;
117 }
118
119 if ( Gestalt( gestaltAppearanceVersion, &theAppearance ) == noErr )
120 sUMAAppearanceVersion = theAppearance ;
121 else
122 sUMAAppearanceVersion = 0x0100 ;
123 }
124
125 if ( Gestalt( gestaltWindowMgrAttr, &sUMAWindowManagerAttr ) == noErr )
126 sUMAHasWindowManager = sUMAWindowManagerAttr & gestaltWindowMgrPresent ;
127
128 #if TARGET_CARBON
129 // Call currently implicitely done : InitFloatingWindows() ;
130 #else
131 if (!isEmbedded)
132 {
133 if ( sUMAHasWindowManager )
134 InitFloatingWindows() ;
135 else
136 InitWindows();
137 }
138 #endif
139
140 if ( NavServicesAvailable() )
141 NavLoad() ;
142
143 long menuMgrAttr ;
144 Gestalt( gestaltMenuMgrAttr , &menuMgrAttr ) ;
145 if ( menuMgrAttr & gestaltMenuMgrAquaLayoutMask )
146 sUMAHasAquaLayout = true ;
147
148 if ( TXNInitTextension != (void*) kUnresolvedCFragSymbolAddress )
149 {
150 FontFamilyID fontId ;
151 Str255 fontName ;
152 SInt16 fontSize ;
153 Style fontStyle ;
154
155 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
156 GetFNum( fontName, &fontId );
157
158 TXNMacOSPreferredFontDescription fontDescriptions[] =
159 {
160 { fontId , (fontSize << 16) , kTXNDefaultFontStyle, kTXNSystemDefaultEncoding }
161 } ;
162 int noOfFontDescriptions = sizeof( fontDescriptions ) / sizeof(TXNMacOSPreferredFontDescription) ;
163
164 OptionBits options = 0 ;
165
166 if ( UMAGetSystemVersion() < 0x1000 )
167 options |= kTXNAlwaysUseQuickDrawTextMask ;
168
169 TXNInitTextension( fontDescriptions, noOfFontDescriptions, options );
170 }
171
172 UMASetSystemIsInitialized( true );
173 }
174
175 #if 0
176 Boolean CanUseATSUI()
177 {
178 long result;
179 OSErr err = Gestalt(gestaltATSUVersion, &result);
180 return (err == noErr);
181 }
182 #endif
183
184 // process manager
185 long UMAGetProcessMode()
186 {
187 OSErr err ;
188 ProcessInfoRec processinfo;
189 ProcessSerialNumber procno ;
190
191 procno.highLongOfPSN = 0 ;
192 procno.lowLongOfPSN = kCurrentProcess ;
193 processinfo.processInfoLength = sizeof(ProcessInfoRec);
194 processinfo.processName = NULL;
195 processinfo.processAppSpec = NULL;
196
197 err = ::GetProcessInformation( &procno , &processinfo ) ;
198 wxASSERT( err == noErr ) ;
199
200 return processinfo.processMode ;
201 }
202
203 bool UMAGetProcessModeDoesActivateOnFGSwitch()
204 {
205 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch ;
206 }
207
208 // menu manager
209
210 MenuRef UMANewMenu( SInt16 id , const wxString& title , wxFontEncoding encoding )
211 {
212 wxString str = wxStripMenuCodes( title ) ;
213 MenuRef menu ;
214
215 #if TARGET_CARBON
216 CreateNewMenu( id , 0 , &menu ) ;
217 SetMenuTitleWithCFString( menu , wxMacCFStringHolder(str , encoding ) ) ;
218 #else
219 Str255 ptitle ;
220 wxMacStringToPascal( str , ptitle ) ;
221 menu = ::NewMenu( id , ptitle ) ;
222 #endif
223
224 return menu ;
225 }
226
227 void UMASetMenuTitle( MenuRef menu , const wxString& title , wxFontEncoding encoding )
228 {
229 wxString str = wxStripMenuCodes( title ) ;
230
231 #if TARGET_CARBON
232 SetMenuTitleWithCFString( menu , wxMacCFStringHolder(str , encoding) ) ;
233
234 #else
235 Str255 ptitle ;
236 wxMacStringToPascal( str , ptitle ) ;
237 SetMenuTitle( menu , ptitle ) ;
238 #endif
239 }
240
241 void UMASetMenuItemText( MenuRef menu, MenuItemIndex item, const wxString& title, wxFontEncoding encoding )
242 {
243 // we don't strip the accels here anymore, must be done before
244 wxString str = title ;
245
246 #if TARGET_CARBON
247 SetMenuItemTextWithCFString( menu , item , wxMacCFStringHolder(str , encoding) ) ;
248
249 #else
250 Str255 ptitle ;
251 wxMacStringToPascal( str , ptitle ) ;
252 SetMenuItemText( menu , item , ptitle ) ;
253 #endif
254 }
255
256 UInt32 UMAMenuEvent( EventRecord *inEvent )
257 {
258 return MenuEvent( inEvent ) ;
259 }
260
261 void UMAEnableMenuItem( MenuRef inMenu , MenuItemIndex inItem , bool enable)
262 {
263 if ( enable )
264 EnableMenuItem( inMenu , inItem ) ;
265 else
266 DisableMenuItem( inMenu , inItem ) ;
267 }
268
269 void UMAAppendSubMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , SInt16 id )
270 {
271 MacAppendMenu( menu, "\pA" );
272 UMASetMenuItemText( menu, (SInt16) ::CountMenuItems(menu), title , encoding );
273 SetMenuItemHierarchicalID( menu , CountMenuItems( menu ) , id ) ;
274 }
275
276 void UMAInsertSubMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , MenuItemIndex item , SInt16 id )
277 {
278 MacInsertMenuItem( menu, "\pA" , item );
279 UMASetMenuItemText( menu, item+1, title , encoding );
280 SetMenuItemHierarchicalID( menu , item+1 , id ) ;
281 }
282
283 void UMASetMenuItemShortcut( MenuRef menu , MenuItemIndex item , wxAcceleratorEntry *entry )
284 {
285 if ( !entry )
286 return ;
287
288 UInt8 modifiers = 0 ;
289 SInt16 key = entry->GetKeyCode() ;
290 if ( key )
291 {
292 bool explicitCommandKey = (entry->GetFlags() & wxACCEL_CTRL);
293
294 if (entry->GetFlags() & wxACCEL_ALT)
295 modifiers |= kMenuOptionModifier ;
296
297 if (entry->GetFlags() & wxACCEL_SHIFT)
298 modifiers |= kMenuShiftModifier ;
299
300 SInt16 glyph = 0 ;
301 SInt16 macKey = key ;
302 if ( key >= WXK_F1 && key <= WXK_F15 )
303 {
304 if ( !explicitCommandKey )
305 modifiers |= kMenuNoCommandModifier ;
306
307 // for some reasons this must be 0 right now
308 // everything else leads to just the first function key item
309 // to be selected. Thanks to Ryan Wilcox for finding out.
310 macKey = 0 ;
311 glyph = kMenuF1Glyph + ( key - WXK_F1 ) ;
312 if ( key >= WXK_F13 )
313 glyph += 13 ;
314 }
315 else
316 {
317 switch ( key )
318 {
319 case WXK_BACK :
320 macKey = kBackspaceCharCode ;
321 glyph = kMenuDeleteLeftGlyph ;
322 break ;
323
324 case WXK_TAB :
325 macKey = kTabCharCode ;
326 glyph = kMenuTabRightGlyph ;
327 break ;
328
329 case kEnterCharCode :
330 macKey = kEnterCharCode ;
331 glyph = kMenuEnterGlyph ;
332 break ;
333
334 case WXK_RETURN :
335 macKey = kReturnCharCode ;
336 glyph = kMenuReturnGlyph ;
337 break ;
338
339 case WXK_ESCAPE :
340 macKey = kEscapeCharCode ;
341 glyph = kMenuEscapeGlyph ;
342 break ;
343
344 case WXK_SPACE :
345 macKey = ' ' ;
346 glyph = kMenuSpaceGlyph ;
347 break ;
348
349 case WXK_DELETE :
350 macKey = kDeleteCharCode ;
351 glyph = kMenuDeleteRightGlyph ;
352 break ;
353
354 case WXK_CLEAR :
355 macKey = kClearCharCode ;
356 glyph = kMenuClearGlyph ;
357 break ;
358
359 case WXK_PAGEUP :
360 macKey = kPageUpCharCode ;
361 glyph = kMenuPageUpGlyph ;
362 break ;
363
364 case WXK_PAGEDOWN :
365 macKey = kPageDownCharCode ;
366 glyph = kMenuPageDownGlyph ;
367 break ;
368
369 case WXK_LEFT :
370 macKey = kLeftArrowCharCode ;
371 glyph = kMenuLeftArrowGlyph ;
372 break ;
373
374 case WXK_UP :
375 macKey = kUpArrowCharCode ;
376 glyph = kMenuUpArrowGlyph ;
377 break ;
378
379 case WXK_RIGHT :
380 macKey = kRightArrowCharCode ;
381 glyph = kMenuRightArrowGlyph ;
382 break ;
383
384 case WXK_DOWN :
385 macKey = kDownArrowCharCode ;
386 glyph = kMenuDownArrowGlyph ;
387 break ;
388
389 case WXK_HOME :
390 macKey = kHomeCharCode ;
391 glyph = kMenuNorthwestArrowGlyph ;
392 break ;
393
394 case WXK_END :
395 macKey = kEndCharCode ;
396 glyph = kMenuSoutheastArrowGlyph ;
397 break ;
398 default :
399 macKey = toupper( key ) ;
400 break ;
401 }
402
403 // we now allow non command key shortcuts
404 // remove in case this gives problems
405 if ( !explicitCommandKey )
406 modifiers |= kMenuNoCommandModifier ;
407 }
408
409 // 1d and 1e have special meaning to SetItemCmd, so
410 // do not use for these character codes.
411 if (key != WXK_UP && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_LEFT)
412 SetItemCmd( menu, item , macKey );
413
414 SetMenuItemModifiers( menu, item , modifiers ) ;
415
416 if ( glyph )
417 SetMenuItemKeyGlyph( menu, item , glyph ) ;
418 }
419 }
420
421 void UMAAppendMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , wxAcceleratorEntry *entry )
422 {
423 MacAppendMenu(menu, "\pA");
424
425 // don't attempt to interpret metacharacters like a '-' at the beginning (would become a separator otherwise)
426 ChangeMenuItemAttributes( menu , ::CountMenuItems(menu), kMenuItemAttrIgnoreMeta , 0 ) ;
427 UMASetMenuItemText(menu, (SInt16) ::CountMenuItems(menu), title , encoding );
428 UMASetMenuItemShortcut( menu , (SInt16) ::CountMenuItems(menu), entry ) ;
429 }
430
431 void UMAInsertMenuItem( MenuRef menu , const wxString& title, wxFontEncoding encoding , MenuItemIndex item , wxAcceleratorEntry *entry )
432 {
433 MacInsertMenuItem( menu , "\pA" , item) ;
434
435 // don't attempt to interpret metacharacters like a '-' at the beginning (would become a separator otherwise)
436 ChangeMenuItemAttributes( menu , item+1, kMenuItemAttrIgnoreMeta , 0 ) ;
437 UMASetMenuItemText(menu, item+1 , title , encoding );
438 UMASetMenuItemShortcut( menu , item+1 , entry ) ;
439 }
440
441 // quickdraw
442
443 #if !TARGET_CARBON
444
445 int gPrOpenCounter = 0 ;
446
447 OSStatus UMAPrOpen()
448 {
449 OSErr err = noErr ;
450
451 ++gPrOpenCounter ;
452
453 if ( gPrOpenCounter == 1 )
454 {
455 PrOpen() ;
456 err = PrError() ;
457 wxASSERT( err == noErr ) ;
458 }
459
460 return err ;
461 }
462
463 OSStatus UMAPrClose()
464 {
465 OSErr err = noErr ;
466
467 wxASSERT( gPrOpenCounter >= 1 ) ;
468
469 if ( gPrOpenCounter == 1 )
470 {
471 PrClose() ;
472 err = PrError() ;
473 wxASSERT( err == noErr ) ;
474 }
475
476 --gPrOpenCounter ;
477
478 return err ;
479 }
480
481 pascal QDGlobalsPtr GetQDGlobalsPtr() ;
482 pascal QDGlobalsPtr GetQDGlobalsPtr()
483 {
484 return QDGlobalsPtr (* (Ptr*) LMGetCurrentA5 ( ) - 0xCA);
485 }
486
487 #endif
488
489 void UMAShowWatchCursor()
490 {
491 OSErr err = noErr;
492
493 CursHandle watchFob = GetCursor(watchCursor);
494
495 if (watchFob == NULL)
496 {
497 err = nilHandleErr;
498 }
499 else
500 {
501 #if TARGET_CARBON
502 // Cursor preservedArrow;
503 // GetQDGlobalsArrow(&preservedArrow);
504 // SetQDGlobalsArrow(*watchFob);
505 // InitCursor();
506 // SetQDGlobalsArrow(&preservedArrow);
507 SetCursor(*watchFob);
508 #else
509 SetCursor(*watchFob);
510 #endif
511 }
512 }
513
514 void UMAShowArrowCursor()
515 {
516 #if TARGET_CARBON
517 Cursor arrow;
518 SetCursor( GetQDGlobalsArrow(&arrow) );
519 #else
520 SetCursor (&(qd.arrow));
521 #endif
522 }
523
524 // window manager
525
526 GrafPtr UMAGetWindowPort( WindowRef inWindowRef )
527 {
528 wxASSERT( inWindowRef != NULL ) ;
529
530 #if TARGET_CARBON
531 return (GrafPtr) GetWindowPort( inWindowRef ) ;
532 #else
533 return (GrafPtr) inWindowRef ;
534 #endif
535 }
536
537 void UMADisposeWindow( WindowRef inWindowRef )
538 {
539 wxASSERT( inWindowRef != NULL ) ;
540
541 DisposeWindow( inWindowRef ) ;
542 }
543
544 void UMASetWTitle( WindowRef inWindowRef , const wxString& title , wxFontEncoding encoding )
545 {
546 #if TARGET_CARBON
547 SetWindowTitleWithCFString( inWindowRef , wxMacCFStringHolder(title , encoding) ) ;
548
549 #else
550 Str255 ptitle ;
551 wxMacStringToPascal( title , ptitle ) ;
552 SetWTitle( inWindowRef , ptitle ) ;
553 #endif
554 }
555
556 // appearance additions
557
558 void UMASetControlTitle( ControlRef inControl , const wxString& title , wxFontEncoding encoding )
559 {
560 #if TARGET_CARBON
561 SetControlTitleWithCFString( inControl , wxMacCFStringHolder(title , encoding) ) ;
562
563 #else
564 Str255 ptitle ;
565 wxMacStringToPascal( title , ptitle ) ;
566 SetControlTitle( inControl , ptitle ) ;
567 #endif
568 }
569
570 void UMAActivateControl( ControlRef inControl )
571 {
572 #if TARGET_API_MAC_OSX
573 ::ActivateControl( inControl ) ;
574
575 #else
576 // we have to add the control after again to the update rgn
577 // otherwise updates get lost
578 if ( !IsControlActive( inControl ) )
579 {
580 bool visible = IsControlVisible( inControl ) ;
581 if ( visible )
582 SetControlVisibility( inControl , false , false ) ;
583
584 ::ActivateControl( inControl ) ;
585
586 if ( visible )
587 {
588 SetControlVisibility( inControl , true , false ) ;
589
590 Rect ctrlBounds ;
591 InvalWindowRect( GetControlOwner(inControl), UMAGetControlBoundsInWindowCoords(inControl, &ctrlBounds) ) ;
592 }
593 }
594 #endif
595 }
596
597 void UMAMoveControl( ControlRef inControl , short x , short y )
598 {
599 #if TARGET_API_MAC_OSX
600 ::MoveControl( inControl , x , y ) ;
601
602 #else
603 bool visible = IsControlVisible( inControl ) ;
604 if ( visible )
605 {
606 SetControlVisibility( inControl , false , false ) ;
607 Rect ctrlBounds ;
608 InvalWindowRect( GetControlOwner(inControl), GetControlBounds(inControl, &ctrlBounds) ) ;
609 }
610
611 ::MoveControl( inControl , x , y ) ;
612
613 if ( visible )
614 {
615 SetControlVisibility( inControl , true , false ) ;
616 Rect ctrlBounds ;
617 InvalWindowRect( GetControlOwner(inControl), GetControlBounds(inControl, &ctrlBounds) ) ;
618 }
619 #endif
620 }
621
622 void UMASizeControl( ControlRef inControl , short x , short y )
623 {
624 #if TARGET_API_MAC_OSX
625 ::SizeControl( inControl , x , y ) ;
626
627 #else
628 bool visible = IsControlVisible( inControl ) ;
629 if ( visible )
630 {
631 SetControlVisibility( inControl , false , false ) ;
632 Rect ctrlBounds ;
633 InvalWindowRect( GetControlOwner(inControl), GetControlBounds(inControl, &ctrlBounds) ) ;
634 }
635
636 ::SizeControl( inControl , x , y ) ;
637
638 if ( visible )
639 {
640 SetControlVisibility( inControl , true , false ) ;
641 Rect ctrlBounds ;
642 InvalWindowRect( GetControlOwner(inControl), GetControlBounds(inControl, &ctrlBounds) ) ;
643 }
644 #endif
645 }
646
647 void UMADeactivateControl( ControlRef inControl )
648 {
649 #if TARGET_API_MAC_OSX
650 ::DeactivateControl( inControl ) ;
651
652 #else
653 // we have to add the control after again to the update rgn
654 // otherwise updates get lost
655 bool visible = IsControlVisible( inControl ) ;
656 if ( visible )
657 SetControlVisibility( inControl , false , false ) ;
658
659 ::DeactivateControl( inControl ) ;
660
661 if ( visible )
662 {
663 SetControlVisibility( inControl , true , false ) ;
664 Rect ctrlBounds ;
665 InvalWindowRect( GetControlOwner(inControl), UMAGetControlBoundsInWindowCoords(inControl, &ctrlBounds) ) ;
666 }
667 #endif
668 }
669
670 // shows the control and adds the region to the update region
671 void UMAShowControl( ControlRef inControl )
672 {
673 SetControlVisibility( inControl , true , false ) ;
674 Rect ctrlBounds ;
675 InvalWindowRect( GetControlOwner(inControl), UMAGetControlBoundsInWindowCoords(inControl, &ctrlBounds) ) ;
676 }
677
678 // hides the control and adds the region to the update region
679 void UMAHideControl( ControlRef inControl )
680 {
681 SetControlVisibility( inControl , false , false ) ;
682 Rect ctrlBounds ;
683 InvalWindowRect( GetControlOwner(inControl), UMAGetControlBoundsInWindowCoords(inControl, &ctrlBounds) ) ;
684 }
685
686 // keyboard focus
687 OSErr UMASetKeyboardFocus( WindowPtr inWindow,
688 ControlRef inControl,
689 ControlFocusPart inPart )
690 {
691 OSErr err = noErr;
692 GrafPtr port ;
693
694 GetPort( &port ) ;
695 SetPortWindowPort( inWindow ) ;
696
697 err = SetKeyboardFocus( inWindow , inControl , inPart ) ;
698 SetPort( port ) ;
699
700 return err ;
701 }
702
703 bool UMAIsWindowFloating( WindowRef inWindow )
704 {
705 WindowClass cl ;
706
707 GetWindowClass( inWindow , &cl ) ;
708 return cl == kFloatingWindowClass ;
709 }
710
711 bool UMAIsWindowModal( WindowRef inWindow )
712 {
713 WindowClass cl ;
714
715 GetWindowClass( inWindow , &cl ) ;
716 return cl < kFloatingWindowClass ;
717 }
718
719 // others
720
721 void UMAHighlightAndActivateWindow( WindowRef inWindowRef , bool inActivate )
722 {
723 if ( inWindowRef )
724 {
725 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
726 // if ( inActivate != isHighlighted )
727
728 GrafPtr port ;
729 GetPort( &port ) ;
730 SetPortWindowPort( inWindowRef ) ;
731 HiliteWindow( inWindowRef , inActivate ) ;
732 ControlRef control = NULL ;
733 ::GetRootControl( inWindowRef , &control ) ;
734 if ( control )
735 {
736 if ( inActivate )
737 UMAActivateControl( control ) ;
738 else
739 UMADeactivateControl( control ) ;
740 }
741
742 SetPort( port ) ;
743 }
744 }
745
746 OSStatus UMADrawThemePlacard( const Rect *inRect , ThemeDrawState inState )
747 {
748 return ::DrawThemePlacard( inRect , inState ) ;
749 }
750
751 #if !TARGET_CARBON
752 static OSStatus helpMenuStatus = noErr ;
753 static MenuItemIndex firstCustomItemIndex = 0 ;
754 #endif
755
756 OSStatus UMAGetHelpMenu(
757 MenuRef * outHelpMenu,
758 MenuItemIndex * outFirstCustomItemIndex)
759 {
760 #if TARGET_CARBON
761 return HMGetHelpMenu( outHelpMenu , outFirstCustomItemIndex ) ;
762
763 #else
764 MenuRef helpMenuHandle ;
765
766 helpMenuStatus = HMGetHelpMenuHandle( &helpMenuHandle ) ;
767 if ( firstCustomItemIndex == 0 && helpMenuStatus == noErr )
768 firstCustomItemIndex = CountMenuItems( helpMenuHandle ) + 1 ;
769
770 if ( outFirstCustomItemIndex )
771 *outFirstCustomItemIndex = firstCustomItemIndex ;
772
773 *outHelpMenu = helpMenuHandle ;
774
775 return helpMenuStatus ;
776 #endif
777 }
778
779 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport )
780 {
781 m_clip = NULL ;
782 Setup( newport ) ;
783 }
784
785 wxMacPortStateHelper::wxMacPortStateHelper()
786 {
787 m_clip = NULL ;
788 }
789
790 void wxMacPortStateHelper::Setup( GrafPtr newport )
791 {
792 GetPort( &m_oldPort ) ;
793 SetPort( newport ) ;
794 SetOrigin(0, 0);
795
796 wxASSERT_MSG( m_clip == NULL , wxT("Cannot call setup twice") ) ;
797 m_clip = NewRgn() ;
798 GetClip( m_clip );
799 m_textFont = GetPortTextFont( (CGrafPtr) newport );
800 m_textSize = GetPortTextSize( (CGrafPtr) newport );
801 m_textStyle = GetPortTextFace( (CGrafPtr) newport );
802 m_textMode = GetPortTextMode( (CGrafPtr) newport );
803 GetThemeDrawingState( &m_drawingState ) ;
804 m_currentPort = newport ;
805 }
806
807 void wxMacPortStateHelper::Clear()
808 {
809 if ( m_clip )
810 {
811 DisposeRgn( m_clip ) ;
812 DisposeThemeDrawingState( m_drawingState ) ;
813 m_clip = NULL ;
814 }
815 }
816
817 wxMacPortStateHelper::~wxMacPortStateHelper()
818 {
819 if ( m_clip )
820 {
821 SetPort( m_currentPort ) ;
822 SetClip( m_clip ) ;
823 DisposeRgn( m_clip ) ;
824 TextFont( m_textFont );
825 TextSize( m_textSize );
826 TextFace( m_textStyle );
827 TextMode( m_textMode );
828 SetThemeDrawingState( m_drawingState , true ) ;
829 SetPort( m_oldPort ) ;
830 }
831 }
832
833 OSStatus UMAPutScrap( Size size , OSType type , void *data )
834 {
835 OSStatus err = noErr ;
836
837 #if !TARGET_CARBON
838 err = PutScrap( size , type , data ) ;
839 #else
840 ScrapRef scrap;
841 err = GetCurrentScrap( &scrap );
842 if ( err == noErr )
843 err = PutScrapFlavor( scrap, type , 0, size, data );
844 #endif
845
846 return err ;
847 }
848
849 Rect * UMAGetControlBoundsInWindowCoords( ControlRef theControl, Rect *bounds )
850 {
851 GetControlBounds( theControl , bounds ) ;
852
853 #if TARGET_API_MAC_OSX
854 WindowRef tlwref = GetControlOwner( theControl ) ;
855
856 wxTopLevelWindowMac* tlwwx = wxFindWinFromMacWindow( tlwref ) ;
857 if ( tlwwx != NULL )
858 {
859 ControlRef rootControl = tlwwx->GetPeer()->GetControlRef() ;
860 HIPoint hiPoint = CGPointMake( 0 , 0 ) ;
861 HIViewConvertPoint( &hiPoint , HIViewGetSuperview(theControl) , rootControl ) ;
862 OffsetRect( bounds , (short) hiPoint.x , (short) hiPoint.y ) ;
863 }
864 #endif
865
866 return bounds ;
867 }
868
869 #endif // wxUSE_GUI
870
871 #if wxUSE_BASE
872
873 static bool sUMASystemInitialized = false ;
874
875 bool UMASystemIsInitialized()
876 {
877 return sUMASystemInitialized ;
878 }
879
880 void UMASetSystemIsInitialized(bool val)
881 {
882 sUMASystemInitialized = val;
883 }
884
885 #endif // wxUSE_BASE