]> git.saurik.com Git - wxWidgets.git/blob - src/mac/uma.cpp
Applied patch [ 549256 ] fix minor bug in widgets sample
[wxWidgets.git] / src / mac / 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 MacAppendMenu(menu, l);
226 SetMenuItemHierarchicalID( menu , CountMenuItems( menu ) , id ) ;
227 }
228
229 void UMAInsertSubMenuItem( MenuRef menu , StringPtr l , MenuItemIndex item , SInt16 id )
230 {
231 MacInsertMenuItem(menu, l , item);
232 SetMenuItemHierarchicalID( menu , item , id ) ;
233 }
234
235
236 void UMAAppendMenuItem( MenuRef menu , StringPtr l , SInt16 key, UInt8 modifiers )
237 {
238 Str255 label ;
239 memcpy( label , l , l[0]+1 ) ;
240 if ( key )
241 {
242 int pos = label[0] ;
243 label[++pos] = '/';
244 label[++pos] = toupper( key );
245 label[0] = pos ;
246 }
247 MacAppendMenu( menu , label ) ;
248 }
249
250 void UMAInsertMenuItem( MenuRef menu , StringPtr l , MenuItemIndex item , SInt16 key, UInt8 modifiers )
251 {
252 Str255 label ;
253 memcpy( label , l , l[0]+1 ) ;
254 if ( key )
255 {
256 int pos = label[0] ;
257 label[++pos] = '/';
258 label[++pos] = toupper( key );
259 label[0] = pos ;
260 }
261 MacInsertMenuItem( menu , label , item) ;
262 }
263
264 // quickdraw
265
266 int gPrOpenCounter = 0 ;
267
268 OSStatus UMAPrOpen(void *macPrintSession)
269 {
270 #if !TARGET_CARBON
271 OSErr err = noErr ;
272 ++gPrOpenCounter ;
273 if ( gPrOpenCounter == 1 )
274 {
275 PrOpen() ;
276 err = PrError() ;
277 wxASSERT( err == noErr ) ;
278 }
279 return err ;
280 #else
281 OSStatus err = noErr ;
282 ++gPrOpenCounter ;
283 if ( gPrOpenCounter == 1 )
284 {
285 #if PM_USE_SESSION_APIS
286 err = PMCreateSession((PMPrintSession *)macPrintSession) ;
287 #else
288 err = PMBegin() ;
289 #endif
290 wxASSERT( err == noErr ) ;
291 }
292 return err ;
293 #endif
294 }
295
296 OSStatus UMAPrClose(void *macPrintSession)
297 {
298 #if !TARGET_CARBON
299 OSErr err = noErr ;
300 wxASSERT( gPrOpenCounter >= 1 ) ;
301 if ( gPrOpenCounter == 1 )
302 {
303 PrClose() ;
304 err = PrError() ;
305 wxASSERT( err == noErr ) ;
306 }
307 --gPrOpenCounter ;
308 return err ;
309 #else
310 OSStatus err = noErr ;
311 wxASSERT( gPrOpenCounter >= 1 ) ;
312 if ( gPrOpenCounter == 1 )
313 {
314 #if PM_USE_SESSION_APIS
315 err = PMRelease(*(PMPrintSession *)macPrintSession) ;
316 *(PMPrintSession *)macPrintSession = kPMNoReference;
317 #else
318 err = PMEnd() ;
319 #endif
320 }
321 --gPrOpenCounter ;
322 return err ;
323 #endif
324 }
325
326 #if !TARGET_CARBON
327
328 pascal QDGlobalsPtr GetQDGlobalsPtr (void) ;
329 pascal QDGlobalsPtr GetQDGlobalsPtr (void)
330 {
331 return QDGlobalsPtr (* (Ptr*) LMGetCurrentA5 ( ) - 0xCA);
332 }
333
334 #endif
335
336 void UMAShowWatchCursor()
337 {
338 OSErr err = noErr;
339
340 CursHandle watchFob = GetCursor (watchCursor);
341
342 if (!watchFob)
343 err = nilHandleErr;
344 else
345 {
346 #if TARGET_CARBON
347 // Cursor preservedArrow;
348 // GetQDGlobalsArrow (&preservedArrow);
349 // SetQDGlobalsArrow (*watchFob);
350 // InitCursor ( );
351 // SetQDGlobalsArrow (&preservedArrow);
352 SetCursor (*watchFob);
353 #else
354 SetCursor (*watchFob);
355 #endif
356 }
357 }
358
359 void UMAShowArrowCursor()
360 {
361 #if TARGET_CARBON
362 Cursor arrow;
363 SetCursor (GetQDGlobalsArrow (&arrow));
364 #else
365 SetCursor (&(qd.arrow));
366 #endif
367 }
368
369 // window manager
370
371 GrafPtr UMAGetWindowPort( WindowRef inWindowRef )
372 {
373 wxASSERT( inWindowRef != NULL ) ;
374 #if TARGET_CARBON
375 return (GrafPtr) GetWindowPort( inWindowRef ) ;
376 #else
377 return (GrafPtr) inWindowRef ;
378 #endif
379 }
380
381 void UMADisposeWindow( WindowRef inWindowRef )
382 {
383 wxASSERT( inWindowRef != NULL ) ;
384 DisposeWindow( inWindowRef ) ;
385 }
386
387 void UMASetWTitleC( WindowRef inWindowRef , const char *title )
388 {
389 Str255 ptitle ;
390 strncpy( (char*)ptitle , title , 96 ) ;
391 ptitle[96] = 0 ;
392 #if TARGET_CARBON
393 c2pstrcpy( ptitle, (char *)ptitle ) ;
394 #else
395 c2pstr( (char*)ptitle ) ;
396 #endif
397 SetWTitle( inWindowRef , ptitle ) ;
398 }
399
400 void UMAGetWTitleC( WindowRef inWindowRef , char *title )
401 {
402 GetWTitle( inWindowRef , (unsigned char*)title ) ;
403 #if TARGET_CARBON
404 p2cstrcpy( title, (unsigned char *)title ) ;
405 #else
406 p2cstr( (unsigned char*)title ) ;
407 #endif
408 }
409
410 // appearance additions
411
412 void UMAActivateControl( ControlHandle inControl )
413 {
414 // we have to add the control after again to the update rgn
415 // otherwise updates get lost
416 if ( !IsControlActive( inControl ) )
417 {
418 bool visible = IsControlVisible( inControl ) ;
419 if ( visible )
420 SetControlVisibility( inControl , false , false ) ;
421 ::ActivateControl( inControl ) ;
422 if ( visible ) {
423 SetControlVisibility( inControl , true , false ) ;
424 Rect ctrlBounds ;
425 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
426 }
427 }
428 }
429
430 void UMADrawControl( ControlHandle inControl )
431 {
432 WindowRef theWindow = GetControlOwner(inControl) ;
433 RgnHandle updateRgn = NewRgn() ;
434 GetWindowUpdateRgn( theWindow , updateRgn ) ;
435 Point zero = { 0 , 0 } ;
436 LocalToGlobal( &zero ) ;
437 OffsetRgn( updateRgn , -zero.h , -zero.v ) ;
438 ::DrawControlInCurrentPort( inControl ) ;
439 InvalWindowRgn( theWindow, updateRgn) ;
440 DisposeRgn( updateRgn ) ;
441 }
442
443 void UMAMoveControl( ControlHandle inControl , short x , short y )
444 {
445 bool visible = IsControlVisible( inControl ) ;
446 if ( visible ) {
447 SetControlVisibility( inControl , false , false ) ;
448 Rect ctrlBounds ;
449 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
450 }
451 ::MoveControl( inControl , x , y ) ;
452 if ( visible ) {
453 SetControlVisibility( inControl , true , false ) ;
454 Rect ctrlBounds ;
455 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
456 }
457 }
458
459 void UMASizeControl( ControlHandle inControl , short x , short y )
460 {
461 bool visible = IsControlVisible( inControl ) ;
462 if ( visible ) {
463 SetControlVisibility( inControl , false , false ) ;
464 Rect ctrlBounds ;
465 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
466 }
467 ::SizeControl( inControl , x , y ) ;
468 if ( visible ) {
469 SetControlVisibility( inControl , true , false ) ;
470 Rect ctrlBounds ;
471 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
472 }
473 }
474
475 void UMADeactivateControl( ControlHandle inControl )
476 {
477 // we have to add the control after again to the update rgn
478 // otherwise updates get lost
479 bool visible = IsControlVisible( inControl ) ;
480 if ( visible )
481 SetControlVisibility( inControl , false , false ) ;
482 ::DeactivateControl( inControl ) ;
483 if ( visible ) {
484 SetControlVisibility( inControl , true , false ) ;
485 Rect ctrlBounds ;
486 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
487 }
488 }
489 // shows the control and adds the region to the update region
490 void UMAShowControl (ControlHandle inControl)
491 {
492 SetControlVisibility( inControl , true , false ) ;
493 Rect ctrlBounds ;
494 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
495 }
496
497 // shows the control and adds the region to the update region
498 void UMAHideControl (ControlHandle inControl)
499 {
500 SetControlVisibility( inControl , false , false ) ;
501 Rect ctrlBounds ;
502 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
503 }
504 // keyboard focus
505 OSErr UMASetKeyboardFocus (WindowPtr inWindow,
506 ControlHandle inControl,
507 ControlFocusPart inPart)
508 {
509 OSErr err = noErr;
510 GrafPtr port ;
511 GetPort( &port ) ;
512
513 SetPortWindowPort( inWindow ) ;
514
515 err = SetKeyboardFocus( inWindow , inControl , inPart ) ;
516 SetPort( port ) ;
517 return err ;
518 }
519
520
521
522
523 // events
524 void UMAUpdateControls( WindowPtr inWindow , RgnHandle inRgn )
525 {
526 RgnHandle updateRgn = NewRgn() ;
527 GetWindowUpdateRgn( inWindow , updateRgn ) ;
528
529 Point zero = { 0 , 0 } ;
530 LocalToGlobal( &zero ) ;
531 OffsetRgn( updateRgn , -zero.h , -zero.v ) ;
532
533 UpdateControls( inWindow , inRgn ) ;
534 InvalWindowRgn( inWindow, updateRgn) ;
535 DisposeRgn( updateRgn ) ;
536
537 }
538
539 bool UMAIsWindowFloating( WindowRef inWindow )
540 {
541 WindowClass cl ;
542
543 GetWindowClass( inWindow , &cl ) ;
544 return cl == kFloatingWindowClass ;
545 }
546
547 bool UMAIsWindowModal( WindowRef inWindow )
548 {
549 WindowClass cl ;
550
551 GetWindowClass( inWindow , &cl ) ;
552 return cl < kFloatingWindowClass ;
553 }
554
555 // others
556
557 void UMAHighlightAndActivateWindow( WindowRef inWindowRef , bool inActivate )
558 {
559 if ( inWindowRef )
560 {
561 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
562 // if ( inActivate != isHightlited )
563 GrafPtr port ;
564 GetPort( &port ) ;
565 SetPortWindowPort( inWindowRef ) ;
566 HiliteWindow( inWindowRef , inActivate ) ;
567 ControlHandle control = NULL ;
568 ::GetRootControl( inWindowRef , & control ) ;
569 if ( control )
570 {
571 if ( inActivate )
572 UMAActivateControl( control ) ;
573 else
574 UMADeactivateControl( control ) ;
575 }
576 SetPort( port ) ;
577 }
578 }
579 OSStatus UMADrawThemePlacard( const Rect *inRect , ThemeDrawState inState )
580 {
581 return ::DrawThemePlacard( inRect , inState ) ;
582 }
583
584 #if !TARGET_CARBON
585 static OSStatus helpMenuStatus = noErr ;
586 static MenuItemIndex firstCustomItemIndex = 0 ;
587 #endif
588
589 OSStatus UMAGetHelpMenu(
590 MenuRef * outHelpMenu,
591 MenuItemIndex * outFirstCustomItemIndex)
592 {
593 #if TARGET_CARBON
594 return HMGetHelpMenu( outHelpMenu , outFirstCustomItemIndex ) ;
595 #else
596 MenuRef helpMenuHandle ;
597 helpMenuStatus = HMGetHelpMenuHandle( &helpMenuHandle ) ;
598 if ( firstCustomItemIndex == 0 && helpMenuStatus == noErr )
599 {
600 firstCustomItemIndex = CountMenuItems( helpMenuHandle ) + 1 ;
601 }
602 if ( outFirstCustomItemIndex )
603 {
604 *outFirstCustomItemIndex = firstCustomItemIndex ;
605 }
606 *outHelpMenu = helpMenuHandle ;
607 return helpMenuStatus ;
608 #endif
609 }
610
611 wxMacPortStateHelper::wxMacPortStateHelper( GrafPtr newport)
612 {
613 m_clip = NULL ;
614 Setup( newport ) ;
615 }
616
617 wxMacPortStateHelper::wxMacPortStateHelper()
618 {
619 m_clip = NULL ;
620 }
621
622 void wxMacPortStateHelper::Setup( GrafPtr newport )
623 {
624 GetPort( &m_oldPort ) ;
625 SetPort( newport ) ;
626 wxASSERT_MSG( m_clip == NULL , "Cannot call setup twice" ) ;
627 m_clip = NewRgn() ;
628 GetClip( m_clip );
629 m_textFont = GetPortTextFont( (CGrafPtr) newport);
630 m_textSize = GetPortTextSize( (CGrafPtr) newport);
631 m_textStyle = GetPortTextFace( (CGrafPtr) newport);
632 m_textMode = GetPortTextMode( (CGrafPtr) newport);
633 GetThemeDrawingState( &m_drawingState ) ;
634 m_currentPort = newport ;
635 }
636 void wxMacPortStateHelper::Clear()
637 {
638 if ( m_clip )
639 {
640 DisposeRgn( m_clip ) ;
641 DisposeThemeDrawingState( m_drawingState ) ;
642 m_clip = NULL ;
643 }
644 }
645
646 wxMacPortStateHelper::~wxMacPortStateHelper()
647 {
648 if ( m_clip )
649 {
650 SetPort( m_currentPort ) ;
651 SetClip( m_clip ) ;
652 DisposeRgn( m_clip ) ;
653 TextFont( m_textFont );
654 TextSize( m_textSize );
655 TextFace( m_textStyle );
656 TextMode( m_textMode );
657 SetThemeDrawingState( m_drawingState , true ) ;
658 SetPort( m_oldPort ) ;
659 }
660 }
661