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