]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/uma.cpp
(hopefully) workaround for a carbon bug not always setting the modifiers event record...
[wxWidgets.git] / src / mac / carbon / uma.cpp
1 #include "wx/defs.h"
2 #include "wx/dc.h"
3 #include "wx/mac/uma.h"
4 #include <MacTextEditor.h>
5
6 #ifndef __DARWIN__
7 # include <Navigation.h>
8 # if defined(TARGET_CARBON)
9 # if PM_USE_SESSION_APIS
10 # include <PMCore.h>
11 # endif
12 # include <PMApplication.h>
13 # else
14 # include <Printing.h>
15 # endif
16 #endif
17
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
22 // present
23
24 static bool sUMAHasAppearance = false ;
25 static long sUMAAppearanceVersion = 0 ;
26 static bool sUMAHasAquaLayout = false ;
27 static bool sUMASystemInitialized = false ;
28
29 extern int gAGABackgroundColor ;
30 bool UMAHasAppearance() { return sUMAHasAppearance ; }
31 long UMAGetAppearanceVersion() { return sUMAAppearanceVersion ; }
32
33 static bool sUMAHasWindowManager = false ;
34 static long sUMAWindowManagerAttr = 0 ;
35
36 bool UMAHasWindowManager() { return sUMAHasWindowManager ; }
37 long UMAGetWindowManagerAttr() { return sUMAWindowManagerAttr ; }
38 bool UMAHasAquaLayout() { return sUMAHasAquaLayout ; }
39 bool UMASystemIsInitialized() { return sUMASystemInitialized ; }
40
41 void UMACleanupToolbox()
42 {
43 if ( sUMAHasAppearance )
44 {
45 UnregisterAppearanceClient() ;
46 }
47 if ( NavServicesAvailable() )
48 {
49 NavUnload() ;
50 }
51 if ( TXNTerminateTextension != (void*) kUnresolvedCFragSymbolAddress )
52 TXNTerminateTextension( ) ;
53 }
54 void UMAInitToolbox( UInt16 inMoreMastersCalls )
55 {
56 #if !TARGET_CARBON
57 ::MaxApplZone();
58 for (long i = 1; i <= inMoreMastersCalls; i++)
59 ::MoreMasters();
60
61 ::InitGraf(&qd.thePort);
62 ::InitFonts();
63 ::InitMenus();
64 ::TEInit();
65 ::InitDialogs(0L);
66 ::FlushEvents(everyEvent, 0);
67 ::InitCursor();
68 long total,contig;
69 PurgeSpace(&total, &contig);
70 #else
71 InitCursor();
72 #endif
73
74 long theAppearance ;
75 if ( Gestalt( gestaltAppearanceAttr, &theAppearance ) == noErr )
76 {
77 sUMAHasAppearance = true ;
78 RegisterAppearanceClient();
79 if ( Gestalt( gestaltAppearanceVersion, &theAppearance ) == noErr )
80 {
81 sUMAAppearanceVersion = theAppearance ;
82 }
83 else
84 {
85 sUMAAppearanceVersion = 0x0100 ;
86 }
87 }
88 if ( Gestalt( gestaltWindowMgrAttr, &sUMAWindowManagerAttr ) == noErr )
89 {
90 sUMAHasWindowManager = sUMAWindowManagerAttr & gestaltWindowMgrPresent ;
91 }
92
93 #if TARGET_CARBON
94 // Call currently implicitely done : InitFloatingWindows() ;
95 #else
96 if ( sUMAHasWindowManager )
97 InitFloatingWindows() ;
98 else
99 InitWindows();
100 #endif
101
102 if ( NavServicesAvailable() )
103 {
104 NavLoad() ;
105 }
106
107 long menuMgrAttr ;
108 Gestalt( gestaltMenuMgrAttr , &menuMgrAttr ) ;
109 if ( menuMgrAttr & gestaltMenuMgrAquaLayoutMask )
110 sUMAHasAquaLayout = true ;
111
112 if ( TXNInitTextension != (void*) kUnresolvedCFragSymbolAddress )
113 {
114 FontFamilyID fontId ;
115 Str255 fontName ;
116 SInt16 fontSize ;
117 Style fontStyle ;
118 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
119 GetFNum( fontName, &fontId );
120
121 TXNMacOSPreferredFontDescription fontDescriptions[] =
122 {
123 { fontId , (fontSize << 16) ,kTXNDefaultFontStyle, kTXNSystemDefaultEncoding } ,
124 } ;
125 int noOfFontDescriptions = sizeof( fontDescriptions ) / sizeof(TXNMacOSPreferredFontDescription) ;
126 #if 0 // TARGET_CARBON
127 --noOfFontDescriptions ;
128 #endif
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 ;
131 #if TARGET_CARBON
132 if ( !UMAHasAquaLayout() )
133 #endif
134 {
135 options |= kTXNAlwaysUseQuickDrawTextMask ;
136 }
137 TXNInitTextension(fontDescriptions, noOfFontDescriptions, options );
138 }
139
140
141 sUMASystemInitialized = true ;
142
143 }
144
145 /*
146 Boolean CanUseATSUI()
147 {
148 long result;
149 OSErr err = Gestalt(gestaltATSUVersion, &result);
150 return (err == noErr);
151 }
152 */
153 // process manager
154 long UMAGetProcessMode()
155 {
156 OSErr err ;
157 ProcessInfoRec processinfo;
158 ProcessSerialNumber procno ;
159
160 procno.highLongOfPSN = NULL ;
161 procno.lowLongOfPSN = kCurrentProcess ;
162 processinfo.processInfoLength = sizeof(ProcessInfoRec);
163 processinfo.processName = NULL;
164 processinfo.processAppSpec = NULL;
165
166 err = ::GetProcessInformation( &procno , &processinfo ) ;
167 wxASSERT( err == noErr ) ;
168 return processinfo.processMode ;
169 }
170
171 bool UMAGetProcessModeDoesActivateOnFGSwitch()
172 {
173 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch ;
174 }
175
176 // menu manager
177
178 void UMASetMenuTitle( MenuRef menu , StringPtr title )
179 {
180 /*
181 #if !TARGET_CARBON
182 long size = GetHandleSize( (Handle) menu ) ;
183 const long headersize = 14 ;
184 int oldlen = (**menu).menuData[0] + 1;
185 int newlen = title[0] + 1 ;
186
187 if ( oldlen < newlen )
188 {
189 // enlarge before adjusting
190 SetHandleSize( (Handle) menu , size + (newlen - oldlen ) );
191 }
192
193 if ( oldlen != newlen )
194 memmove( (char*) (**menu).menuData + newlen , (char*) (**menu).menuData + oldlen , size - headersize - oldlen ) ;
195
196 memcpy( (char*) (**menu).menuData , title , newlen ) ;
197 if ( oldlen > newlen )
198 {
199 // shrink after
200 SetHandleSize( (Handle) menu , size + (newlen - oldlen ) ) ;
201 }
202 #else
203 */
204 SetMenuTitle( menu , title ) ;
205 //#endif
206 }
207
208 UInt32 UMAMenuEvent( EventRecord *inEvent )
209 {
210 return MenuEvent( inEvent ) ;
211 }
212
213 void UMAEnableMenuItem( MenuRef inMenu , MenuItemIndex inItem )
214 {
215 EnableMenuItem( inMenu , inItem ) ;
216 }
217
218 void UMADisableMenuItem( MenuRef inMenu , MenuItemIndex inItem )
219 {
220 DisableMenuItem( inMenu , inItem ) ;
221 }
222
223 void UMAAppendSubMenuItem( MenuRef menu , StringPtr l , SInt16 id )
224 {
225 Str255 label ;
226 memcpy( label , l , l[0]+1 ) ;
227 // hardcoded adding of the submenu combination for mac
228
229 int theEnd = label[0] + 1;
230 if (theEnd > 251)
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;
237 label[0] = theEnd;
238 MacAppendMenu(menu, label);
239 }
240
241 void UMAInsertSubMenuItem( MenuRef menu , StringPtr l , MenuItemIndex item , SInt16 id )
242 {
243 Str255 label ;
244 memcpy( label , l , l[0]+1 ) ;
245 // hardcoded adding of the submenu combination for mac
246
247 int theEnd = label[0] + 1;
248 if (theEnd > 251)
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;
255 label[0] = theEnd;
256 MacInsertMenuItem(menu, label , item);
257 }
258
259 void UMAAppendMenuItem( MenuRef menu , StringPtr l , SInt16 key, UInt8 modifiers )
260 {
261 Str255 label ;
262 memcpy( label , l , l[0]+1 ) ;
263 if ( key )
264 {
265 int pos = label[0] ;
266 label[++pos] = '/';
267 label[++pos] = toupper( key );
268 label[0] = pos ;
269 }
270 MacAppendMenu( menu , label ) ;
271 }
272
273 void UMAInsertMenuItem( MenuRef menu , StringPtr l , MenuItemIndex item , SInt16 key, UInt8 modifiers )
274 {
275 Str255 label ;
276 memcpy( label , l , l[0]+1 ) ;
277 if ( key )
278 {
279 int pos = label[0] ;
280 label[++pos] = '/';
281 label[++pos] = toupper( key );
282 label[0] = pos ;
283 }
284 MacInsertMenuItem( menu , label , item) ;
285 }
286
287 // quickdraw
288
289 int gPrOpenCounter = 0 ;
290
291 OSStatus UMAPrOpen(void *macPrintSession)
292 {
293 #if !TARGET_CARBON
294 OSErr err = noErr ;
295 ++gPrOpenCounter ;
296 if ( gPrOpenCounter == 1 )
297 {
298 PrOpen() ;
299 err = PrError() ;
300 wxASSERT( err == noErr ) ;
301 }
302 return err ;
303 #else
304 OSStatus err = noErr ;
305 ++gPrOpenCounter ;
306 if ( gPrOpenCounter == 1 )
307 {
308 #if PM_USE_SESSION_APIS
309 err = PMCreateSession((PMPrintSession *)macPrintSession) ;
310 #else
311 err = PMBegin() ;
312 #endif
313 wxASSERT( err == noErr ) ;
314 }
315 return err ;
316 #endif
317 }
318
319 OSStatus UMAPrClose(void *macPrintSession)
320 {
321 #if !TARGET_CARBON
322 OSErr err = noErr ;
323 wxASSERT( gPrOpenCounter >= 1 ) ;
324 if ( gPrOpenCounter == 1 )
325 {
326 PrClose() ;
327 err = PrError() ;
328 wxASSERT( err == noErr ) ;
329 }
330 --gPrOpenCounter ;
331 return err ;
332 #else
333 OSStatus err = noErr ;
334 wxASSERT( gPrOpenCounter >= 1 ) ;
335 if ( gPrOpenCounter == 1 )
336 {
337 #if PM_USE_SESSION_APIS
338 err = PMRelease(*(PMPrintSession *)macPrintSession) ;
339 *(PMPrintSession *)macPrintSession = kPMNoReference;
340 #else
341 err = PMEnd() ;
342 #endif
343 }
344 --gPrOpenCounter ;
345 return err ;
346 #endif
347 }
348
349 #if !TARGET_CARBON
350
351 pascal QDGlobalsPtr GetQDGlobalsPtr (void) ;
352 pascal QDGlobalsPtr GetQDGlobalsPtr (void)
353 {
354 return QDGlobalsPtr (* (Ptr*) LMGetCurrentA5 ( ) - 0xCA);
355 }
356
357 #endif
358
359 void UMAShowWatchCursor()
360 {
361 OSErr err = noErr;
362
363 CursHandle watchFob = GetCursor (watchCursor);
364
365 if (!watchFob)
366 err = nilHandleErr;
367 else
368 {
369 #if TARGET_CARBON
370 // Cursor preservedArrow;
371 // GetQDGlobalsArrow (&preservedArrow);
372 // SetQDGlobalsArrow (*watchFob);
373 // InitCursor ( );
374 // SetQDGlobalsArrow (&preservedArrow);
375 SetCursor (*watchFob);
376 #else
377 SetCursor (*watchFob);
378 #endif
379 }
380 }
381
382 void UMAShowArrowCursor()
383 {
384 #if TARGET_CARBON
385 Cursor arrow;
386 SetCursor (GetQDGlobalsArrow (&arrow));
387 #else
388 SetCursor (&(qd.arrow));
389 #endif
390 }
391
392 // window manager
393
394 GrafPtr UMAGetWindowPort( WindowRef inWindowRef )
395 {
396 wxASSERT( inWindowRef != NULL ) ;
397 #if TARGET_CARBON
398 return (GrafPtr) GetWindowPort( inWindowRef ) ;
399 #else
400 return (GrafPtr) inWindowRef ;
401 #endif
402 }
403
404 void UMADisposeWindow( WindowRef inWindowRef )
405 {
406 wxASSERT( inWindowRef != NULL ) ;
407 DisposeWindow( inWindowRef ) ;
408 }
409
410 void UMASetWTitleC( WindowRef inWindowRef , const char *title )
411 {
412 Str255 ptitle ;
413 strncpy( (char*)ptitle , title , 96 ) ;
414 ptitle[96] = 0 ;
415 #if TARGET_CARBON
416 c2pstrcpy( ptitle, (char *)ptitle ) ;
417 #else
418 c2pstr( (char*)ptitle ) ;
419 #endif
420 SetWTitle( inWindowRef , ptitle ) ;
421 }
422
423 void UMAGetWTitleC( WindowRef inWindowRef , char *title )
424 {
425 GetWTitle( inWindowRef , (unsigned char*)title ) ;
426 #if TARGET_CARBON
427 p2cstrcpy( title, (unsigned char *)title ) ;
428 #else
429 p2cstr( (unsigned char*)title ) ;
430 #endif
431 }
432
433 // appearance additions
434
435 void UMAActivateControl( ControlHandle inControl )
436 {
437 // we have to add the control after again to the update rgn
438 // otherwise updates get lost
439 if ( !IsControlActive( inControl ) )
440 {
441 bool visible = IsControlVisible( inControl ) ;
442 if ( visible )
443 SetControlVisibility( inControl , false , false ) ;
444 ::ActivateControl( inControl ) ;
445 if ( visible ) {
446 SetControlVisibility( inControl , true , false ) ;
447 Rect ctrlBounds ;
448 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
449 }
450 }
451 }
452
453 void UMADrawControl( ControlHandle inControl )
454 {
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 ) ;
464 }
465
466 void UMAMoveControl( ControlHandle inControl , short x , short y )
467 {
468 bool visible = IsControlVisible( inControl ) ;
469 if ( visible ) {
470 SetControlVisibility( inControl , false , false ) ;
471 Rect ctrlBounds ;
472 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
473 }
474 ::MoveControl( inControl , x , y ) ;
475 if ( visible ) {
476 SetControlVisibility( inControl , true , false ) ;
477 Rect ctrlBounds ;
478 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
479 }
480 }
481
482 void UMASizeControl( ControlHandle inControl , short x , short y )
483 {
484 bool visible = IsControlVisible( inControl ) ;
485 if ( visible ) {
486 SetControlVisibility( inControl , false , false ) ;
487 Rect ctrlBounds ;
488 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
489 }
490 ::SizeControl( inControl , x , y ) ;
491 if ( visible ) {
492 SetControlVisibility( inControl , true , false ) ;
493 Rect ctrlBounds ;
494 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
495 }
496 }
497
498 void UMADeactivateControl( ControlHandle inControl )
499 {
500 // we have to add the control after again to the update rgn
501 // otherwise updates get lost
502 bool visible = IsControlVisible( inControl ) ;
503 if ( visible )
504 SetControlVisibility( inControl , false , false ) ;
505 ::DeactivateControl( inControl ) ;
506 if ( visible ) {
507 SetControlVisibility( inControl , true , false ) ;
508 Rect ctrlBounds ;
509 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
510 }
511 }
512 // shows the control and adds the region to the update region
513 void UMAShowControl (ControlHandle inControl)
514 {
515 SetControlVisibility( inControl , true , false ) ;
516 Rect ctrlBounds ;
517 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
518 }
519
520 // shows the control and adds the region to the update region
521 void UMAHideControl (ControlHandle inControl)
522 {
523 SetControlVisibility( inControl , false , false ) ;
524 Rect ctrlBounds ;
525 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
526 }
527 // keyboard focus
528 OSErr UMASetKeyboardFocus (WindowPtr inWindow,
529 ControlHandle inControl,
530 ControlFocusPart inPart)
531 {
532 OSErr err = noErr;
533 GrafPtr port ;
534 GetPort( &port ) ;
535
536 SetPortWindowPort( inWindow ) ;
537
538 err = SetKeyboardFocus( inWindow , inControl , inPart ) ;
539 SetPort( port ) ;
540 return err ;
541 }
542
543
544
545
546 // events
547 void UMAUpdateControls( WindowPtr inWindow , RgnHandle inRgn )
548 {
549 RgnHandle updateRgn = NewRgn() ;
550 GetWindowUpdateRgn( inWindow , updateRgn ) ;
551
552 Point zero = { 0 , 0 } ;
553 LocalToGlobal( &zero ) ;
554 OffsetRgn( updateRgn , -zero.h , -zero.v ) ;
555
556 UpdateControls( inWindow , inRgn ) ;
557 InvalWindowRgn( inWindow, updateRgn) ;
558 DisposeRgn( updateRgn ) ;
559
560 }
561
562 bool UMAIsWindowFloating( WindowRef inWindow )
563 {
564 WindowClass cl ;
565
566 GetWindowClass( inWindow , &cl ) ;
567 return cl == kFloatingWindowClass ;
568 }
569
570 bool UMAIsWindowModal( WindowRef inWindow )
571 {
572 WindowClass cl ;
573
574 GetWindowClass( inWindow , &cl ) ;
575 return cl < kFloatingWindowClass ;
576 }
577
578 // others
579
580 void UMAHighlightAndActivateWindow( WindowRef inWindowRef , bool inActivate )
581 {
582 if ( inWindowRef )
583 {
584 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
585 // if ( inActivate != isHightlited )
586 GrafPtr port ;
587 GetPort( &port ) ;
588 SetPortWindowPort( inWindowRef ) ;
589 HiliteWindow( inWindowRef , inActivate ) ;
590 ControlHandle control = NULL ;
591 ::GetRootControl( inWindowRef , & control ) ;
592 if ( control )
593 {
594 if ( inActivate )
595 UMAActivateControl( control ) ;
596 else
597 UMADeactivateControl( control ) ;
598 }
599 SetPort( port ) ;
600 }
601 }
602 OSStatus UMADrawThemePlacard( const Rect *inRect , ThemeDrawState inState )
603 {
604 return ::DrawThemePlacard( inRect , inState ) ;
605 }
606
607 #if !TARGET_CARBON
608 static OSStatus helpMenuStatus = noErr ;
609 static MenuItemIndex firstCustomItemIndex = 0 ;
610 #endif
611
612 OSStatus UMAGetHelpMenu(
613 MenuRef * outHelpMenu,
614 MenuItemIndex * outFirstCustomItemIndex)
615 {
616 #if TARGET_CARBON
617 return HMGetHelpMenu( outHelpMenu , outFirstCustomItemIndex ) ;
618 #else
619 MenuRef helpMenuHandle ;
620 helpMenuStatus = HMGetHelpMenuHandle( &helpMenuHandle ) ;
621 if ( firstCustomItemIndex == 0 && helpMenuStatus == noErr )
622 {
623 firstCustomItemIndex = CountMenuItems( helpMenuHandle ) + 1 ;
624 }
625 if ( outFirstCustomItemIndex )
626 {
627 *outFirstCustomItemIndex = firstCustomItemIndex ;
628 }
629 *outHelpMenu = helpMenuHandle ;
630 return helpMenuStatus ;
631 #endif
632 }
633
634 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport)
635 {
636 m_clip = NULL ;
637 Setup( newport ) ;
638 }
639
640 wxMacPortStateHelper::wxMacPortStateHelper()
641 {
642 m_clip = NULL ;
643 }
644
645 void wxMacPortStateHelper::Setup( GrafPtr newport )
646 {
647 GetPort( &m_oldPort ) ;
648 SetPort( newport ) ;
649 wxASSERT_MSG( m_clip == NULL , "Cannot call setup twice" ) ;
650 m_clip = NewRgn() ;
651 GetClip( m_clip );
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 ;
658 }
659 void wxMacPortStateHelper::Clear()
660 {
661 if ( m_clip )
662 {
663 DisposeRgn( m_clip ) ;
664 DisposeThemeDrawingState( m_drawingState ) ;
665 m_clip = NULL ;
666 }
667 }
668
669 wxMacPortStateHelper::~wxMacPortStateHelper()
670 {
671 if ( m_clip )
672 {
673 SetPort( m_currentPort ) ;
674 SetClip( m_clip ) ;
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 ) ;
682 }
683 }
684