]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/uma.cpp
added internal callback when toplevel windows move for opengl canvas
[wxWidgets.git] / src / mac / carbon / uma.cpp
CommitLineData
03e11df5 1#include "wx/defs.h"
5fde6fcc 2#include "wx/dc.h"
03e11df5 3#include "wx/mac/uma.h"
72e7876b 4
f5c6eb5c 5#ifndef __DARWIN__
03e11df5
GD
6 #include <Navigation.h>
7#endif
5b781a67 8
72055702
SC
9// since we have decided that we only support 8.6 upwards we are
10// checking for these minimum requirements in the startup code of
11// the application so all wxWindows code can safely assume that appearance 1.1
12// windows manager, control manager, navigation services etc. are
13// present
e7b596fb 14
72e7876b
SC
15static bool sUMAHasAppearance = false ;
16static long sUMAAppearanceVersion = 0 ;
17extern int gAGABackgroundColor ;
18bool UMAHasAppearance() { return sUMAHasAppearance ; }
19long UMAGetAppearanceVersion() { return sUMAAppearanceVersion ; }
20
21static bool sUMAHasWindowManager = false ;
22static long sUMAWindowManagerAttr = 0 ;
23
24bool UMAHasWindowManager() { return sUMAHasWindowManager ; }
25long UMAGetWindowManagerAttr() { return sUMAWindowManagerAttr ; }
72055702 26
5b781a67
SC
27void UMACleanupToolbox()
28{
5b781a67
SC
29 if ( sUMAHasAppearance )
30 {
31 UnregisterAppearanceClient() ;
32 }
5b781a67
SC
33 if ( NavServicesAvailable() )
34 {
35 NavUnload() ;
36 }
37}
72e7876b
SC
38void UMAInitToolbox( UInt16 inMoreMastersCalls )
39{
40#if !TARGET_CARBON
41 ::MaxApplZone();
42 for (long i = 1; i <= inMoreMastersCalls; i++)
43 ::MoreMasters();
44
45 ::InitGraf(&qd.thePort);
46 ::InitFonts();
72e7876b
SC
47 ::InitMenus();
48 ::TEInit();
49 ::InitDialogs(0L);
50 ::FlushEvents(everyEvent, 0);
51 ::InitCursor();
52 long total,contig;
53 PurgeSpace(&total, &contig);
54#else
2f1ae414 55 InitCursor();
72e7876b
SC
56#endif
57
72e7876b
SC
58 long theAppearance ;
59 if ( Gestalt( gestaltAppearanceAttr, &theAppearance ) == noErr )
60 {
61 sUMAHasAppearance = true ;
62 RegisterAppearanceClient();
63 if ( Gestalt( gestaltAppearanceVersion, &theAppearance ) == noErr )
64 {
65 sUMAAppearanceVersion = theAppearance ;
66 }
67 else
68 {
69 sUMAAppearanceVersion = 0x0100 ;
70 }
71 }
72e7876b
SC
72 if ( Gestalt( gestaltWindowMgrAttr, &sUMAWindowManagerAttr ) == noErr )
73 {
74 sUMAHasWindowManager = sUMAWindowManagerAttr & gestaltWindowMgrPresent ;
75 }
5b781a67 76
f5c6eb5c 77#ifndef __DARWIN__
8e8d3ba8
SC
78#if TARGET_CARBON
79// Call currently implicitely done : InitFloatingWindows() ;
80#else
5b781a67
SC
81 if ( sUMAHasWindowManager )
82 InitFloatingWindows() ;
83 else
84 InitWindows();
8e8d3ba8 85#endif
03e11df5 86#endif
5b781a67
SC
87
88 if ( NavServicesAvailable() )
89 {
90 NavLoad() ;
91 }
72e7876b
SC
92}
93
94// process manager
95long UMAGetProcessMode()
96{
97 OSErr err ;
98 ProcessInfoRec processinfo;
99 ProcessSerialNumber procno ;
100
101 procno.highLongOfPSN = NULL ;
102 procno.lowLongOfPSN = kCurrentProcess ;
103 processinfo.processInfoLength = sizeof(ProcessInfoRec);
104 processinfo.processName = NULL;
105 processinfo.processAppSpec = NULL;
106
107 err = ::GetProcessInformation( &procno , &processinfo ) ;
108 wxASSERT( err == noErr ) ;
109 return processinfo.processMode ;
110}
111
112bool UMAGetProcessModeDoesActivateOnFGSwitch()
113{
114 return UMAGetProcessMode() & modeDoesActivateOnFGSwitch ;
115}
116
117// menu manager
118
2f1ae414 119void UMASetMenuTitle( MenuRef menu , StringPtr title )
72e7876b
SC
120{
121#if !TARGET_CARBON
122 long size = GetHandleSize( (Handle) menu ) ;
123 const long headersize = 14 ;
124 int oldlen = (**menu).menuData[0] + 1;
125 int newlen = title[0] + 1 ;
126
127 if ( oldlen < newlen )
128 {
129 // enlarge before adjusting
130 SetHandleSize( (Handle) menu , size + (newlen - oldlen ) );
131 }
132
133 if ( oldlen != newlen )
134 memmove( (char*) (**menu).menuData + newlen , (char*) (**menu).menuData + oldlen , size - headersize - oldlen ) ;
135
136 memcpy( (char*) (**menu).menuData , title , newlen ) ;
137 if ( oldlen > newlen )
138 {
139 // shrink after
140 SetHandleSize( (Handle) menu , size + (newlen - oldlen ) ) ;
141 }
142#else
143 SetMenuTitle( menu , title ) ;
144#endif
145}
146
147UInt32 UMAMenuEvent( EventRecord *inEvent )
148{
72e7876b 149 return MenuEvent( inEvent ) ;
72e7876b
SC
150}
151
152void UMAEnableMenuItem( MenuRef inMenu , MenuItemIndex inItem )
153{
72e7876b 154 EnableMenuItem( inMenu , inItem ) ;
72e7876b
SC
155}
156
157void UMADisableMenuItem( MenuRef inMenu , MenuItemIndex inItem )
158{
72e7876b 159 DisableMenuItem( inMenu , inItem ) ;
72e7876b 160}
2f1ae414
SC
161
162void UMAAppendSubMenuItem( MenuRef menu , StringPtr l , SInt16 id )
163{
164 Str255 label ;
165 memcpy( label , l , l[0]+1 ) ;
166 // hardcoded adding of the submenu combination for mac
167
168 int theEnd = label[0] + 1;
169 if (theEnd > 251)
170 theEnd = 251; // mac allows only 255 characters
171 label[theEnd++] = '/';
172 label[theEnd++] = hMenuCmd;
173 label[theEnd++] = '!';
174 label[theEnd++] = id ;
175 label[theEnd] = 0x00;
176 label[0] = theEnd;
177 MacAppendMenu(menu, label);
178}
179
180void UMAInsertSubMenuItem( MenuRef menu , StringPtr l , MenuItemIndex item , SInt16 id )
181{
182 Str255 label ;
183 memcpy( label , l , l[0]+1 ) ;
184 // hardcoded adding of the submenu combination for mac
185
186 int theEnd = label[0] + 1;
187 if (theEnd > 251)
188 theEnd = 251; // mac allows only 255 characters
189 label[theEnd++] = '/';
190 label[theEnd++] = hMenuCmd;
191 label[theEnd++] = '!';
192 label[theEnd++] = id;
193 label[theEnd] = 0x00;
194 label[0] = theEnd;
195 MacInsertMenuItem(menu, label , item);
196}
197
198void UMAAppendMenuItem( MenuRef menu , StringPtr l , SInt16 key, UInt8 modifiers )
199{
200 Str255 label ;
201 memcpy( label , l , l[0]+1 ) ;
202 if ( key )
203 {
204 int pos = label[0] ;
205 label[++pos] = '/';
206 label[++pos] = toupper( key );
207 label[0] = pos ;
208 }
209 MacAppendMenu( menu , label ) ;
210}
211
212void UMAInsertMenuItem( MenuRef menu , StringPtr l , MenuItemIndex item , SInt16 key, UInt8 modifiers )
213{
214 Str255 label ;
215 memcpy( label , l , l[0]+1 ) ;
216 if ( key )
217 {
218 int pos = label[0] ;
219 label[++pos] = '/';
220 label[++pos] = toupper( key );
221 label[0] = pos ;
222 }
223 MacInsertMenuItem( menu , label , item) ;
224}
225
72e7876b
SC
226// quickdraw
227
2f1ae414
SC
228int gPrOpenCounter = 0 ;
229
93f0fe75
GD
230#if TARGET_CARBON && PM_USE_SESSION_APIS
231OSStatus UMAPrOpen(PMPrintSession *macPrintSession)
87df17a1
GD
232#else
233OSStatus UMAPrOpen()
a689a4d0 234#endif
2f1ae414
SC
235{
236#if !TARGET_CARBON
237 OSErr err = noErr ;
238 ++gPrOpenCounter ;
239 if ( gPrOpenCounter == 1 )
240 {
241 PrOpen() ;
242 err = PrError() ;
243 wxASSERT( err == noErr ) ;
244 }
5b781a67 245 return err ;
2f1ae414 246#else
5b781a67
SC
247 OSStatus err = noErr ;
248 ++gPrOpenCounter ;
249 if ( gPrOpenCounter == 1 )
250 {
a689a4d0 251 #if PM_USE_SESSION_APIS
93f0fe75 252 err = PMCreateSession(macPrintSession) ;
a689a4d0
GD
253 #else
254 err = PMBegin() ;
255 #endif
5b781a67
SC
256 wxASSERT( err == noErr ) ;
257 }
258 return err ;
2f1ae414
SC
259#endif
260}
261
93f0fe75
GD
262#if TARGET_CARBON && PM_USE_SESSION_APIS
263OSStatus UMAPrClose(PMPrintSession *macPrintSession)
87df17a1
GD
264#else
265OSStatus UMAPrClose()
a689a4d0 266#endif
2f1ae414
SC
267{
268#if !TARGET_CARBON
269 OSErr err = noErr ;
270 wxASSERT( gPrOpenCounter >= 1 ) ;
271 if ( gPrOpenCounter == 1 )
272 {
273 PrClose() ;
274 err = PrError() ;
275 wxASSERT( err == noErr ) ;
276 }
277 --gPrOpenCounter ;
5b781a67 278 return err ;
2f1ae414 279#else
5b781a67
SC
280 OSStatus err = noErr ;
281 wxASSERT( gPrOpenCounter >= 1 ) ;
282 if ( gPrOpenCounter == 1 )
283 {
a689a4d0 284 #if PM_USE_SESSION_APIS
93f0fe75
GD
285 err = PMRelease(*macPrintSession) ;
286 *macPrintSession = kPMNoReference;
a689a4d0
GD
287 #else
288 err = PMEnd() ;
289 #endif
5b781a67
SC
290 }
291 --gPrOpenCounter ;
292 return err ;
2f1ae414
SC
293#endif
294}
295
72e7876b
SC
296#if !TARGET_CARBON
297
72055702 298pascal QDGlobalsPtr GetQDGlobalsPtr (void) ;
72e7876b
SC
299pascal QDGlobalsPtr GetQDGlobalsPtr (void)
300{
301 return QDGlobalsPtr (* (Ptr*) LMGetCurrentA5 ( ) - 0xCA);
302}
303
304#endif
305
306void UMAShowWatchCursor()
307{
308 OSErr err = noErr;
309
310 CursHandle watchFob = GetCursor (watchCursor);
311
312 if (!watchFob)
313 err = nilHandleErr;
314 else
315 {
316 #if TARGET_CARBON
72055702
SC
317// Cursor preservedArrow;
318// GetQDGlobalsArrow (&preservedArrow);
319// SetQDGlobalsArrow (*watchFob);
320// InitCursor ( );
321// SetQDGlobalsArrow (&preservedArrow);
322 SetCursor (*watchFob);
72e7876b
SC
323 #else
324 SetCursor (*watchFob);
325 #endif
326 }
327}
328
329void UMAShowArrowCursor()
330{
331#if TARGET_CARBON
332 Cursor arrow;
333 SetCursor (GetQDGlobalsArrow (&arrow));
334#else
335 SetCursor (&(qd.arrow));
336#endif
337}
338
339// window manager
340
341GrafPtr UMAGetWindowPort( WindowRef inWindowRef )
342{
343 wxASSERT( inWindowRef != NULL ) ;
344#if TARGET_CARBON
345 return GetWindowPort( inWindowRef ) ;
346#else
347 return (GrafPtr) inWindowRef ;
348#endif
349}
350
351void UMADisposeWindow( WindowRef inWindowRef )
352{
353 wxASSERT( inWindowRef != NULL ) ;
354 DisposeWindow( inWindowRef ) ;
355}
356
357void UMASetWTitleC( WindowRef inWindowRef , const char *title )
358{
359 Str255 ptitle ;
360 strncpy( (char*)ptitle , title , 96 ) ;
361 ptitle[96] = 0 ;
03e11df5
GD
362#if TARGET_CARBON
363 c2pstrcpy( ptitle, (char *)ptitle ) ;
364#else
72e7876b 365 c2pstr( (char*)ptitle ) ;
03e11df5 366#endif
72e7876b
SC
367 SetWTitle( inWindowRef , ptitle ) ;
368}
03e11df5 369
72e7876b
SC
370void UMAGetWTitleC( WindowRef inWindowRef , char *title )
371{
372 GetWTitle( inWindowRef , (unsigned char*)title ) ;
03e11df5
GD
373#if TARGET_CARBON
374 p2cstrcpy( title, (unsigned char *)title ) ;
375#else
72e7876b 376 p2cstr( (unsigned char*)title ) ;
03e11df5 377#endif
72e7876b
SC
378}
379
72e7876b
SC
380// appearance additions
381
382void UMAActivateControl( ControlHandle inControl )
383{
72055702 384 if ( !IsControlActive( inControl ) )
fdaf613a
SC
385 {
386 bool visible = IsControlVisible( inControl ) ;
387 if ( visible )
388 SetControlVisibility( inControl , false , false ) ;
3f4902f5 389 ::ActivateControl( inControl ) ;
fdaf613a
SC
390 if ( visible ) {
391 SetControlVisibility( inControl , true , false ) ;
c36f0244
SC
392 Rect ctrlBounds ;
393 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
fdaf613a
SC
394 }
395 }
72e7876b
SC
396}
397
398void UMADrawControl( ControlHandle inControl )
399{
3f4902f5
GD
400 WindowRef theWindow = GetControlOwner(inControl) ;
401 RgnHandle updateRgn = NewRgn() ;
3f4902f5 402 GetWindowUpdateRgn( theWindow , updateRgn ) ;
be57fda6
SC
403 Point zero = { 0 , 0 } ;
404 LocalToGlobal( &zero ) ;
405 OffsetRgn( updateRgn , -zero.h , -zero.v ) ;
72055702
SC
406 ::DrawControlInCurrentPort( inControl ) ;
407 InvalWindowRgn( theWindow, updateRgn) ;
be57fda6
SC
408 DisposeRgn( updateRgn ) ;
409
72e7876b
SC
410}
411
412void UMAMoveControl( ControlHandle inControl , short x , short y )
413{
72055702
SC
414 bool visible = IsControlVisible( inControl ) ;
415 if ( visible ) {
416 SetControlVisibility( inControl , false , false ) ;
417 Rect ctrlBounds ;
418 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
419 }
420 ::MoveControl( inControl , x , y ) ;
421 if ( visible ) {
422 SetControlVisibility( inControl , true , false ) ;
423 Rect ctrlBounds ;
424 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
425 }
72e7876b
SC
426}
427
428void UMASizeControl( ControlHandle inControl , short x , short y )
429{
72055702
SC
430 bool visible = IsControlVisible( inControl ) ;
431 if ( visible ) {
432 SetControlVisibility( inControl , false , false ) ;
433 Rect ctrlBounds ;
434 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
435 }
436 ::SizeControl( inControl , x , y ) ;
437 if ( visible ) {
438 SetControlVisibility( inControl , true , false ) ;
439 Rect ctrlBounds ;
440 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
441 }
72e7876b
SC
442}
443
444void UMADeactivateControl( ControlHandle inControl )
445{
72055702 446 if ( IsControlActive( inControl ) )
2f1ae414 447 {
72055702
SC
448 bool visible = IsControlVisible( inControl ) ;
449 if ( visible )
450 SetControlVisibility( inControl , false , false ) ;
451 ::DeactivateControl( inControl ) ;
452 if ( visible ) {
453 SetControlVisibility( inControl , true , false ) ;
454 Rect ctrlBounds ;
455 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
456 }
3f4902f5 457 }
72e7876b 458}
fdaf613a 459// shows the control and adds the region to the update region
3f4902f5 460void UMAShowControl (ControlHandle inControl)
72e7876b 461{
fdaf613a 462 SetControlVisibility( inControl , true , false ) ;
c36f0244
SC
463 Rect ctrlBounds ;
464 InvalWindowRect(GetControlOwner(inControl),GetControlBounds(inControl,&ctrlBounds) ) ;
72e7876b
SC
465}
466
72e7876b
SC
467// keyboard focus
468OSErr UMASetKeyboardFocus (WindowPtr inWindow,
469 ControlHandle inControl,
470 ControlFocusPart inPart)
471{
2f1ae414
SC
472 OSErr err = noErr;
473 GrafPtr port ;
474 GetPort( &port ) ;
72055702
SC
475
476 SetPortWindowPort( inWindow ) ;
477
2f1ae414 478 SetOrigin( 0 , 0 ) ;
72055702 479 err = SetKeyboardFocus( inWindow , inControl , inPart ) ;
2f1ae414
SC
480 SetPort( port ) ;
481 return err ;
72e7876b
SC
482}
483
484
485
486
487// events
72e7876b
SC
488void UMAUpdateControls( WindowPtr inWindow , RgnHandle inRgn )
489{
72055702
SC
490 RgnHandle updateRgn = NewRgn() ;
491 GetWindowUpdateRgn( inWindow , updateRgn ) ;
492
be57fda6
SC
493 Point zero = { 0 , 0 } ;
494 LocalToGlobal( &zero ) ;
495 OffsetRgn( updateRgn , -zero.h , -zero.v ) ;
72e7876b 496
72055702
SC
497 UpdateControls( inWindow , inRgn ) ;
498 InvalWindowRgn( inWindow, updateRgn) ;
499 DisposeRgn( updateRgn ) ;
72e7876b 500
72e7876b
SC
501}
502
503bool UMAIsWindowFloating( WindowRef inWindow )
504{
505 WindowClass cl ;
506
72055702 507 GetWindowClass( inWindow , &cl ) ;
72e7876b
SC
508 return cl == kFloatingWindowClass ;
509}
510
511bool UMAIsWindowModal( WindowRef inWindow )
512{
513 WindowClass cl ;
514
72055702 515 GetWindowClass( inWindow , &cl ) ;
72e7876b
SC
516 return cl < kFloatingWindowClass ;
517}
518
519// others
520
521void UMAHighlightAndActivateWindow( WindowRef inWindowRef , bool inActivate )
522{
523 if ( inWindowRef )
524 {
525// bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
526// if ( inActivate != isHightlited )
c809f3be
SC
527 GrafPtr port ;
528 GetPort( &port ) ;
72055702 529 SetPortWindowPort( inWindowRef ) ;
c809f3be
SC
530 SetOrigin( 0 , 0 ) ;
531 HiliteWindow( inWindowRef , inActivate ) ;
532 ControlHandle control = NULL ;
72055702 533 ::GetRootControl( inWindowRef , & control ) ;
c809f3be
SC
534 if ( control )
535 {
536 if ( inActivate )
537 UMAActivateControl( control ) ;
538 else
539 UMADeactivateControl( control ) ;
540 }
541 SetPort( port ) ;
72e7876b
SC
542 }
543}
2f1ae414
SC
544OSStatus UMADrawThemePlacard( const Rect *inRect , ThemeDrawState inState )
545{
72055702 546 return ::DrawThemePlacard( inRect , inState ) ;
2f1ae414 547}
72e7876b 548