]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toplevel.cpp
correcting window variant
[wxWidgets.git] / src / mac / carbon / toplevel.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mac/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for Mac
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 24.09.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001-2004 Stefan Csomor
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "toplevel.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/toplevel.h"
34 #include "wx/frame.h"
35 #include "wx/string.h"
36 #include "wx/log.h"
37 #include "wx/intl.h"
38 #include "wx/settings.h"
39 #endif //WX_PRECOMP
40
41 #include "wx/mac/uma.h"
42 #include "wx/mac/aga.h"
43 #include "wx/app.h"
44 #include "wx/tooltip.h"
45 #include "wx/dnd.h"
46 #if wxUSE_SYSTEM_OPTIONS
47 #include "wx/sysopt.h"
48 #endif
49
50 #include <ToolUtils.h>
51
52 // ----------------------------------------------------------------------------
53 // globals
54 // ----------------------------------------------------------------------------
55
56 // list of all frames and modeless dialogs
57 wxWindowList wxModelessWindows;
58
59 static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param);
60
61 // ============================================================================
62 // wxTopLevelWindowMac implementation
63 // ============================================================================
64
65 BEGIN_EVENT_TABLE(wxTopLevelWindowMac, wxTopLevelWindowBase)
66 END_EVENT_TABLE()
67
68
69 // ---------------------------------------------------------------------------
70 // Carbon Events
71 // ---------------------------------------------------------------------------
72
73 extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
74
75 static const EventTypeSpec eventList[] =
76 {
77 // TODO remove control related event like key and mouse (except for WindowLeave events)
78 #if 1
79 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
80
81 { kEventClassKeyboard, kEventRawKeyDown } ,
82 { kEventClassKeyboard, kEventRawKeyRepeat } ,
83 { kEventClassKeyboard, kEventRawKeyUp } ,
84 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
85 #endif
86
87 { kEventClassWindow , kEventWindowShown } ,
88 { kEventClassWindow , kEventWindowActivated } ,
89 { kEventClassWindow , kEventWindowDeactivated } ,
90 { kEventClassWindow , kEventWindowBoundsChanging } ,
91 { kEventClassWindow , kEventWindowBoundsChanged } ,
92 { kEventClassWindow , kEventWindowClose } ,
93
94 // we have to catch these events on the toplevel window level, as controls don't get the
95 // raw mouse events anymore
96
97 { kEventClassMouse , kEventMouseDown } ,
98 { kEventClassMouse , kEventMouseUp } ,
99 { kEventClassMouse , kEventMouseWheelMoved } ,
100 { kEventClassMouse , kEventMouseMoved } ,
101 { kEventClassMouse , kEventMouseDragged } ,
102 } ;
103
104 static pascal OSStatus TextInputEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
105 {
106 OSStatus result = eventNotHandledErr ;
107
108 wxWindow* focus = wxWindow::FindFocus() ;
109 char charCode ;
110 UInt32 keyCode ;
111 UInt32 modifiers ;
112 Point point ;
113
114 EventRef rawEvent ;
115
116 GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
117
118 GetEventParameter( rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL,sizeof(char), NULL,&charCode );
119 GetEventParameter( rawEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
120 GetEventParameter( rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers);
121 GetEventParameter( rawEvent, kEventParamMouseLocation, typeQDPoint, NULL,
122 sizeof( Point ), NULL, &point );
123
124 switch ( GetEventKind( event ) )
125 {
126 case kEventTextInputUnicodeForKeyEvent :
127 // this is only called when no default handler has jumped in, eg a wxControl on a floater window does not
128 // get its own kEventTextInputUnicodeForKeyEvent, so we route back the
129 wxControl* control = wxDynamicCast( focus , wxControl ) ;
130 if ( control )
131 {
132 ControlRef macControl = (ControlRef) control->GetHandle() ;
133 if ( macControl )
134 {
135 ::HandleControlKey( macControl , keyCode , charCode , modifiers ) ;
136 result = noErr ;
137 }
138 }
139 /*
140 // this may lead to double events sent to a window in case all handlers have skipped the key down event
141 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
142 UInt32 message = (keyCode << 8) + charCode;
143
144 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
145 focus , message , modifiers , when , point.h , point.v ) )
146 {
147 result = noErr ;
148 }
149 */
150 break ;
151 }
152
153 return result ;
154 }
155
156 static pascal OSStatus KeyboardEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
157 {
158 OSStatus result = eventNotHandledErr ;
159
160 wxWindow* focus = wxWindow::FindFocus() ;
161 if ( focus == NULL )
162 return result ;
163
164 char charCode ;
165 UInt32 keyCode ;
166 UInt32 modifiers ;
167 Point point ;
168 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
169
170 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL,sizeof(char), NULL,&charCode );
171 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
172 GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers);
173 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
174 sizeof( Point ), NULL, &point );
175
176 UInt32 message = (keyCode << 8) + charCode;
177 switch( GetEventKind( event ) )
178 {
179 case kEventRawKeyRepeat :
180 case kEventRawKeyDown :
181 {
182 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
183 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
184 wxTheApp->MacSetCurrentEvent( event , handler ) ;
185 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
186 focus , message , modifiers , when , point.h , point.v ) )
187 {
188 result = noErr ;
189 }
190 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
191 }
192 break ;
193 case kEventRawKeyUp :
194 if ( (focus != NULL) && wxTheApp->MacSendKeyUpEvent(
195 focus , message , modifiers , when , point.h , point.v ) )
196 {
197 result = noErr ;
198 }
199 break ;
200 case kEventRawKeyModifiersChanged :
201 {
202 wxKeyEvent event(wxEVT_KEY_DOWN);
203
204 event.m_shiftDown = modifiers & shiftKey;
205 event.m_controlDown = modifiers & controlKey;
206 event.m_altDown = modifiers & optionKey;
207 event.m_metaDown = modifiers & cmdKey;
208
209 event.m_x = point.h;
210 event.m_y = point.v;
211 event.m_timeStamp = when;
212 wxWindow* focus = wxWindow::FindFocus() ;
213 event.SetEventObject(focus);
214
215 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & controlKey )
216 {
217 event.m_keyCode = WXK_CONTROL ;
218 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
219 focus->GetEventHandler()->ProcessEvent( event ) ;
220 }
221 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & shiftKey )
222 {
223 event.m_keyCode = WXK_SHIFT ;
224 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
225 focus->GetEventHandler()->ProcessEvent( event ) ;
226 }
227 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & optionKey )
228 {
229 event.m_keyCode = WXK_ALT ;
230 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
231 focus->GetEventHandler()->ProcessEvent( event ) ;
232 }
233 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & cmdKey )
234 {
235 event.m_keyCode = WXK_COMMAND ;
236 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
237 focus->GetEventHandler()->ProcessEvent( event ) ;
238 }
239 wxTheApp->s_lastModifiers = modifiers ;
240 }
241 break ;
242 }
243
244 return result ;
245 }
246
247 // we don't interfere with foreign controls on our toplevel windows, therefore we always give back eventNotHandledErr
248 // for windows that we didn't create (like eg Scrollbars in a databrowser) , or for controls where we did not handle the
249 // mouse down at all
250
251 // This handler can also be called from app level where data (ie target window) may be null or a non wx window
252
253 wxWindow* g_MacLastWindow = NULL ;
254
255 static EventMouseButton lastButton = 0 ;
256
257 static void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent )
258 {
259 UInt32 modifiers = cEvent.GetParameter<UInt32>(kEventParamKeyModifiers, typeUInt32) ;
260 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
261
262 // this parameter are not given for all events
263 EventMouseButton button = 0 ;
264 UInt32 clickCount = 0 ;
265 cEvent.GetParameter<EventMouseButton>(kEventParamMouseButton, typeMouseButton , &button) ;
266 cEvent.GetParameter<UInt32>(kEventParamClickCount, typeUInt32 , &clickCount ) ;
267
268 wxevent.m_x = screenMouseLocation.h;
269 wxevent.m_y = screenMouseLocation.v;
270 wxevent.m_shiftDown = modifiers & shiftKey;
271 wxevent.m_controlDown = modifiers & controlKey;
272 wxevent.m_altDown = modifiers & optionKey;
273 wxevent.m_metaDown = modifiers & cmdKey;
274 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
275 // a control click is interpreted as a right click
276 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
277 {
278 button = kEventMouseButtonSecondary ;
279 }
280
281 // we must make sure that our synthetic 'right' button corresponds in
282 // mouse down, moved and mouse up, and does not deliver a right down and left up
283
284 if ( cEvent.GetKind() == kEventMouseDown )
285 lastButton = button ;
286
287 if ( button == 0 )
288 lastButton = 0 ;
289 else if ( lastButton )
290 button = lastButton ;
291
292 // determinate the correct down state, wx does not want a 'down' for a mouseUp event, while mac delivers
293 // this button
294 if ( button != 0 && cEvent.GetKind() != kEventMouseUp )
295 {
296 switch( button )
297 {
298 case kEventMouseButtonPrimary :
299 wxevent.m_leftDown = true ;
300 break ;
301 case kEventMouseButtonSecondary :
302 wxevent.m_rightDown = true ;
303 break ;
304 case kEventMouseButtonTertiary :
305 wxevent.m_middleDown = true ;
306 break ;
307 }
308 }
309 // translate into wx types
310 switch ( cEvent.GetKind() )
311 {
312 case kEventMouseDown :
313 switch( button )
314 {
315 case kEventMouseButtonPrimary :
316 wxevent.SetEventType(clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
317 break ;
318 case kEventMouseButtonSecondary :
319 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
320 break ;
321 case kEventMouseButtonTertiary :
322 wxevent.SetEventType(clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
323 break ;
324 }
325 break ;
326 case kEventMouseUp :
327 switch( button )
328 {
329 case kEventMouseButtonPrimary :
330 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
331 break ;
332 case kEventMouseButtonSecondary :
333 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
334 break ;
335 case kEventMouseButtonTertiary :
336 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
337 break ;
338 }
339 break ;
340 case kEventMouseWheelMoved :
341 {
342 wxevent.SetEventType(wxEVT_MOUSEWHEEL ) ;
343
344 // EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
345 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeLongInteger) ;
346
347 wxevent.m_wheelRotation = delta;
348 wxevent.m_wheelDelta = 1;
349 wxevent.m_linesPerAction = 1;
350 break ;
351 }
352 default :
353 wxevent.SetEventType(wxEVT_MOTION ) ;
354 break ;
355 }
356 }
357
358 ControlRef wxMacFindSubControl( Point location , ControlRef superControl , ControlPartCode *outPart )
359 {
360 if ( superControl )
361 {
362 UInt16 childrenCount = 0 ;
363 OSStatus err = CountSubControls( superControl , &childrenCount ) ;
364 if ( err == errControlIsNotEmbedder )
365 return NULL ;
366 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
367
368 for ( UInt16 i = childrenCount ; i >=1 ; --i )
369 {
370 ControlHandle sibling ;
371 err = GetIndexedSubControl( superControl , i , & sibling ) ;
372 if ( err == errControlIsNotEmbedder )
373 return NULL ;
374
375 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
376 if ( IsControlVisible( sibling ) )
377 {
378 Rect r ;
379 UMAGetControlBoundsInWindowCoords( sibling , &r ) ;
380 if ( MacPtInRect( location , &r ) )
381 {
382 ControlHandle child = wxMacFindSubControl( location , sibling , outPart ) ;
383 if ( child )
384 return child ;
385 else
386 {
387 Point testLocation = location ;
388 #if TARGET_API_MAC_OSX
389 testLocation.h -= r.left ;
390 testLocation.v -= r.top ;
391 #endif
392 *outPart = TestControl( sibling , testLocation ) ;
393 return sibling ;
394 }
395 }
396 }
397 }
398 }
399 return NULL ;
400 }
401
402 ControlRef wxMacFindControlUnderMouse( Point location , WindowRef window , ControlPartCode *outPart )
403 {
404 #if TARGET_API_MAC_OSX
405 if ( UMAGetSystemVersion() >= 0x1030 )
406 return FindControlUnderMouse( location , window , outPart ) ;
407 #endif
408 ControlRef rootControl = NULL ;
409 verify_noerr( GetRootControl( window , &rootControl ) ) ;
410 return wxMacFindSubControl( location , rootControl , outPart ) ;
411
412 }
413 pascal OSStatus wxMacTopLevelMouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
414 {
415
416 OSStatus result = eventNotHandledErr ;
417
418 wxMacCarbonEvent cEvent( event ) ;
419
420 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
421 Point windowMouseLocation = screenMouseLocation ;
422
423 WindowRef window ;
424 short windowPart = ::FindWindow(screenMouseLocation, &window);
425
426 wxWindow* currentMouseWindow = NULL ;
427 ControlRef control = NULL ;
428
429 if ( window )
430 {
431 QDGlobalToLocalPoint( UMAGetWindowPort(window ) , &windowMouseLocation ) ;
432
433 if ( wxTheApp->s_captureWindow && wxTheApp->s_captureWindow->MacGetTopLevelWindowRef() == (WXWindow) window && windowPart == inContent )
434 {
435 currentMouseWindow = wxTheApp->s_captureWindow ;
436 }
437 else if ( (IsWindowActive(window) && windowPart == inContent) )
438 {
439 ControlPartCode part ;
440 control = wxMacFindControlUnderMouse( windowMouseLocation , window , &part ) ;
441 // if there is no control below the mouse position, send the event to the toplevel window itself
442 if ( control == 0 )
443 currentMouseWindow = (wxWindow*) data ;
444 else
445 currentMouseWindow = wxFindControlFromMacControl( control ) ;
446 }
447 }
448
449 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
450 SetupMouseEvent( wxevent , cEvent ) ;
451
452 // handle all enter / leave events
453
454 if ( currentMouseWindow != g_MacLastWindow )
455 {
456 if ( g_MacLastWindow )
457 {
458 wxMouseEvent eventleave(wxevent);
459 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
460 g_MacLastWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
461 eventleave.SetEventObject( g_MacLastWindow ) ;
462
463 #if wxUSE_TOOLTIPS
464 wxToolTip::RelayEvent( g_MacLastWindow , eventleave);
465 #endif // wxUSE_TOOLTIPS
466 g_MacLastWindow->GetEventHandler()->ProcessEvent(eventleave);
467 }
468 if ( currentMouseWindow )
469 {
470 wxMouseEvent evententer(wxevent);
471 evententer.SetEventType( wxEVT_ENTER_WINDOW );
472 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
473 evententer.SetEventObject( currentMouseWindow ) ;
474 #if wxUSE_TOOLTIPS
475 wxToolTip::RelayEvent( currentMouseWindow , evententer);
476 #endif // wxUSE_TOOLTIPS
477 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
478 }
479 g_MacLastWindow = currentMouseWindow ;
480 }
481
482 if ( windowPart == inMenuBar )
483 {
484 // special case menu bar, as we are having a low-level runloop we must do it ourselves
485 if ( cEvent.GetKind() == kEventMouseDown )
486 {
487 ::MenuSelect( screenMouseLocation ) ;
488 result = noErr ;
489 }
490 } // if ( windowPart == inMenuBar )
491 else if ( currentMouseWindow )
492 {
493 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
494
495 wxevent.SetEventObject( currentMouseWindow ) ;
496
497 // update cursor
498
499 wxWindow* cursorTarget = currentMouseWindow ;
500 wxPoint cursorPoint( wxevent.m_x , wxevent.m_y ) ;
501
502 while( cursorTarget && !cursorTarget->MacSetupCursor( cursorPoint ) )
503 {
504 cursorTarget = cursorTarget->GetParent() ;
505 if ( cursorTarget )
506 cursorPoint += cursorTarget->GetPosition() ;
507 }
508
509 // update focus
510
511 if ( wxevent.GetEventType() == wxEVT_LEFT_DOWN )
512 {
513 // set focus to this window
514 if (currentMouseWindow->AcceptsFocus() && wxWindow::FindFocus()!=currentMouseWindow)
515 currentMouseWindow->SetFocus();
516 }
517
518 // make tooltips current
519
520 #if wxUSE_TOOLTIPS
521 if ( wxevent.GetEventType() == wxEVT_MOTION
522 || wxevent.GetEventType() == wxEVT_ENTER_WINDOW
523 || wxevent.GetEventType() == wxEVT_LEAVE_WINDOW )
524 wxToolTip::RelayEvent( currentMouseWindow , wxevent);
525 #endif // wxUSE_TOOLTIPS
526 if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
527 result = noErr;
528 else
529 {
530 ControlPartCode dummyPart ;
531 // if built-in find control is finding the wrong control (ie static box instead of overlaid
532 // button, we cannot let the standard handler do its job, but must handle manually
533
534 if ( ( cEvent.GetKind() == kEventMouseDown ) &&
535 (FindControlUnderMouse(windowMouseLocation , window , &dummyPart) !=
536 wxMacFindControlUnderMouse( windowMouseLocation , window , &dummyPart ) ) )
537 {
538 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
539 Point clickLocation = windowMouseLocation ;
540 #if TARGET_API_MAC_OSX
541 currentMouseWindow->MacRootWindowToWindow( &clickLocation.h , &clickLocation.v ) ;
542 #endif
543 HandleControlClick( (ControlRef) currentMouseWindow->GetHandle() , clickLocation ,
544 modifiers , (ControlActionUPP ) -1 ) ;
545 result = noErr ;
546 }
547 }
548 if ( cEvent.GetKind() == kEventMouseUp && wxTheApp->s_captureWindow )
549 {
550 wxTheApp->s_captureWindow = NULL ;
551 // update cursor ?
552 }
553 } // else if ( currentMouseWindow )
554 else
555 {
556 // don't mess with controls we don't know about
557 // for some reason returning eventNotHandledErr does not lead to the correct behaviour
558 // so we try sending them the correct control directly
559 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
560 if ( toplevelWindow && control )
561 {
562 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
563 Point clickLocation = windowMouseLocation ;
564 #if TARGET_API_MAC_OSX
565 HIPoint hiPoint ;
566 hiPoint.x = clickLocation.h ;
567 hiPoint.y = clickLocation.v ;
568 HIViewConvertPoint( &hiPoint , (ControlRef) toplevelWindow->GetHandle() , control ) ;
569 clickLocation.h = (int)hiPoint.x ;
570 clickLocation.v = (int)hiPoint.y ;
571 #endif
572 HandleControlClick( control , clickLocation ,
573 modifiers , (ControlActionUPP ) -1 ) ;
574 result = noErr ;
575 }
576 }
577 return result ;
578 }
579
580 static pascal OSStatus wxMacTopLevelWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
581 {
582 OSStatus result = eventNotHandledErr ;
583
584 wxMacCarbonEvent cEvent( event ) ;
585
586 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
587 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
588
589 switch( GetEventKind( event ) )
590 {
591 case kEventWindowActivated :
592 {
593 toplevelWindow->MacActivate( cEvent.GetTicks() , true) ;
594 wxActivateEvent wxevent(wxEVT_ACTIVATE, true , toplevelWindow->GetId());
595 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
596 wxevent.SetEventObject(toplevelWindow);
597 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
598 // we still sending an eventNotHandledErr in order to allow for default processing
599 break ;
600 }
601 case kEventWindowDeactivated :
602 {
603 toplevelWindow->MacActivate(cEvent.GetTicks() , false) ;
604 wxActivateEvent wxevent(wxEVT_ACTIVATE, false , toplevelWindow->GetId());
605 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
606 wxevent.SetEventObject(toplevelWindow);
607 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
608 // we still sending an eventNotHandledErr in order to allow for default processing
609 break ;
610 }
611 case kEventWindowShown :
612 toplevelWindow->Refresh() ;
613 result = noErr ;
614 break ;
615 case kEventWindowClose :
616 toplevelWindow->Close() ;
617 result = noErr ;
618 break ;
619 case kEventWindowBoundsChanged :
620 {
621 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
622 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
623 wxRect r( newRect.left , newRect.top , newRect.right - newRect.left , newRect.bottom - newRect.top ) ;
624 if ( attributes & kWindowBoundsChangeSizeChanged )
625 {
626 // according to the other ports we handle this within the OS level
627 // resize event, not within a wxSizeEvent
628 wxFrame *frame = wxDynamicCast( toplevelWindow , wxFrame ) ;
629 if ( frame )
630 {
631 #if wxUSE_STATUSBAR
632 frame->PositionStatusBar();
633 #endif
634 #if wxUSE_TOOLBAR
635 frame->PositionToolBar();
636 #endif
637 }
638
639 wxSizeEvent event( r.GetSize() , toplevelWindow->GetId() ) ;
640 event.SetEventObject( toplevelWindow ) ;
641
642 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
643 }
644 if ( attributes & kWindowBoundsChangeOriginChanged )
645 {
646 wxMoveEvent event( r.GetLeftTop() , toplevelWindow->GetId() ) ;
647 event.SetEventObject( toplevelWindow ) ;
648 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
649 }
650 result = noErr ;
651 break ;
652 }
653 case kEventWindowBoundsChanging :
654 {
655 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
656 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
657
658 if ( (attributes & kWindowBoundsChangeSizeChanged) || (attributes & kWindowBoundsChangeOriginChanged) )
659 {
660 // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates
661 int left , top , right , bottom ;
662 toplevelWindow->MacGetContentAreaInset( left , top , right , bottom ) ;
663 wxRect r( newRect.left - left , newRect.top - top ,
664 newRect.right - newRect.left + left + right , newRect.bottom - newRect.top + top + bottom ) ;
665 // this is a EVT_SIZING not a EVT_SIZE type !
666 wxSizeEvent wxevent( r , toplevelWindow->GetId() ) ;
667 wxevent.SetEventObject( toplevelWindow ) ;
668 wxRect adjustR = r ;
669 if ( toplevelWindow->GetEventHandler()->ProcessEvent(wxevent) )
670 adjustR = wxevent.GetRect() ;
671
672 if ( toplevelWindow->GetMaxWidth() != -1 && adjustR.GetWidth() > toplevelWindow->GetMaxWidth() )
673 adjustR.SetWidth( toplevelWindow->GetMaxWidth() ) ;
674 if ( toplevelWindow->GetMaxHeight() != -1 && adjustR.GetHeight() > toplevelWindow->GetMaxHeight() )
675 adjustR.SetHeight( toplevelWindow->GetMaxHeight() ) ;
676 if ( toplevelWindow->GetMinWidth() != -1 && adjustR.GetWidth() < toplevelWindow->GetMinWidth() )
677 adjustR.SetWidth( toplevelWindow->GetMinWidth() ) ;
678 if ( toplevelWindow->GetMinHeight() != -1 && adjustR.GetHeight() < toplevelWindow->GetMinHeight() )
679 adjustR.SetHeight( toplevelWindow->GetMinHeight() ) ;
680 const Rect adjustedRect = { adjustR.y + top , adjustR.x + left , adjustR.y + adjustR.height - bottom , adjustR.x + adjustR.width - right } ;
681 if ( !EqualRect( &newRect , &adjustedRect ) )
682 cEvent.SetParameter<Rect>( kEventParamCurrentBounds , &adjustedRect ) ;
683 }
684
685 result = noErr ;
686 break ;
687 }
688 default :
689 break ;
690 }
691 return result ;
692 }
693
694 pascal OSStatus wxMacTopLevelEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
695 {
696 OSStatus result = eventNotHandledErr ;
697
698 switch ( GetEventClass( event ) )
699 {
700 case kEventClassKeyboard :
701 result = KeyboardEventHandler( handler, event , data ) ;
702 break ;
703 case kEventClassTextInput :
704 result = TextInputEventHandler( handler, event , data ) ;
705 break ;
706 case kEventClassWindow :
707 result = wxMacTopLevelWindowEventHandler( handler, event , data ) ;
708 break ;
709 case kEventClassMouse :
710 result = wxMacTopLevelMouseEventHandler( handler, event , data ) ;
711 break ;
712 default :
713 break ;
714 }
715 return result ;
716 }
717
718 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler )
719
720 // ---------------------------------------------------------------------------
721 // wxWindowMac utility functions
722 // ---------------------------------------------------------------------------
723
724 // Find an item given the Macintosh Window Reference
725
726 wxList wxWinMacWindowList(wxKEY_INTEGER);
727 wxTopLevelWindowMac *wxFindWinFromMacWindow(WindowRef inWindowRef)
728 {
729 wxNode *node = wxWinMacWindowList.Find((long)inWindowRef);
730 if (!node)
731 return NULL;
732 return (wxTopLevelWindowMac *)node->GetData();
733 }
734
735 void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win) ;
736 void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win)
737 {
738 // adding NULL WindowRef is (first) surely a result of an error and
739 // (secondly) breaks menu command processing
740 wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
741
742 if ( !wxWinMacWindowList.Find((long)inWindowRef) )
743 wxWinMacWindowList.Append((long)inWindowRef, win);
744 }
745
746 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win) ;
747 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
748 {
749 wxWinMacWindowList.DeleteObject(win);
750 }
751
752
753 // ----------------------------------------------------------------------------
754 // wxTopLevelWindowMac creation
755 // ----------------------------------------------------------------------------
756
757 wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
758
759 void wxTopLevelWindowMac::Init()
760 {
761 m_iconized =
762 m_maximizeOnShow = FALSE;
763 m_macWindow = NULL ;
764 #if TARGET_API_MAC_OSX
765 m_macUsesCompositing = TRUE;
766 #else
767 m_macUsesCompositing = FALSE;
768 #endif
769 m_macEventHandler = NULL ;
770 }
771
772 class wxMacDeferredWindowDeleter : public wxObject
773 {
774 public :
775 wxMacDeferredWindowDeleter( WindowRef windowRef )
776 {
777 m_macWindow = windowRef ;
778 }
779 virtual ~wxMacDeferredWindowDeleter()
780 {
781 UMADisposeWindow( (WindowRef) m_macWindow ) ;
782 }
783 protected :
784 WindowRef m_macWindow ;
785 } ;
786
787 bool wxTopLevelWindowMac::Create(wxWindow *parent,
788 wxWindowID id,
789 const wxString& title,
790 const wxPoint& pos,
791 const wxSize& size,
792 long style,
793 const wxString& name)
794 {
795 // init our fields
796 Init();
797
798 m_windowStyle = style;
799
800 SetName(name);
801
802 m_windowId = id == -1 ? NewControlId() : id;
803
804 MacCreateRealWindow( title, pos , size , MacRemoveBordersFromStyle(style) , name ) ;
805
806 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
807
808 wxTopLevelWindows.Append(this);
809
810 if ( parent )
811 parent->AddChild(this);
812
813 return TRUE;
814 }
815
816 wxTopLevelWindowMac::~wxTopLevelWindowMac()
817 {
818 if ( m_macWindow )
819 {
820 wxToolTip::NotifyWindowDelete(m_macWindow) ;
821 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
822 }
823
824 if ( m_macEventHandler )
825 {
826 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
827 m_macEventHandler = NULL ;
828 }
829
830 wxRemoveMacWindowAssociation( this ) ;
831
832 if ( wxModelessWindows.Find(this) )
833 wxModelessWindows.DeleteObject(this);
834 }
835
836
837 // ----------------------------------------------------------------------------
838 // wxTopLevelWindowMac maximize/minimize
839 // ----------------------------------------------------------------------------
840
841 void wxTopLevelWindowMac::Maximize(bool maximize)
842 {
843 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
844 wxMacWindowClipper clip (this);
845 ZoomWindow( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , false ) ;
846 }
847
848 bool wxTopLevelWindowMac::IsMaximized() const
849 {
850 return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
851 }
852
853 void wxTopLevelWindowMac::Iconize(bool iconize)
854 {
855 if ( IsWindowCollapsable((WindowRef)m_macWindow) )
856 CollapseWindow((WindowRef)m_macWindow , iconize ) ;
857 }
858
859 bool wxTopLevelWindowMac::IsIconized() const
860 {
861 return IsWindowCollapsed((WindowRef)m_macWindow ) ;
862 }
863
864 void wxTopLevelWindowMac::Restore()
865 {
866 // not available on mac
867 }
868
869 // ----------------------------------------------------------------------------
870 // wxTopLevelWindowMac misc
871 // ----------------------------------------------------------------------------
872
873 wxPoint wxTopLevelWindowMac::GetClientAreaOrigin() const
874 {
875 return wxPoint(0,0) ;
876 }
877
878 void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
879 {
880 // this sets m_icon
881 wxTopLevelWindowBase::SetIcon(icon);
882 }
883
884 void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
885 {
886 wxTopLevelWindowBase::MacSetBackgroundBrush( brush ) ;
887
888 if ( m_macBackgroundBrush.Ok() && m_macBackgroundBrush.GetStyle() != wxTRANSPARENT && m_macBackgroundBrush.MacGetBrushKind() == kwxMacBrushTheme )
889 {
890 SetThemeWindowBackground( (WindowRef) m_macWindow , m_macBackgroundBrush.MacGetTheme() , false ) ;
891 }
892 }
893
894 void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
895 {
896 if ( m_macEventHandler != NULL )
897 {
898 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
899 }
900 InstallWindowEventHandler(MAC_WXHWND(m_macWindow), GetwxMacTopLevelEventHandlerUPP(),
901 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler);
902 }
903
904 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString& title,
905 const wxPoint& pos,
906 const wxSize& size,
907 long style,
908 const wxString& name )
909 {
910 OSStatus err = noErr ;
911 SetName(name);
912 m_windowStyle = style;
913 m_isShown = FALSE;
914
915 // create frame.
916
917 Rect theBoundsRect;
918
919 int x = (int)pos.x;
920 int y = (int)pos.y;
921 if ( y < 50 )
922 y = 50 ;
923 if ( x < 20 )
924 x = 20 ;
925
926 int w = WidthDefault(size.x);
927 int h = HeightDefault(size.y);
928
929 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
930
931 // translate the window attributes in the appropriate window class and attributes
932
933 WindowClass wclass = 0;
934 WindowAttributes attr = kWindowNoAttributes ;
935
936 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
937 {
938 if (
939 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
940 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
941 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
942 )
943 {
944 wclass = kFloatingWindowClass ;
945 if ( HasFlag(wxTINY_CAPTION_VERT) )
946 {
947 attr |= kWindowSideTitlebarAttribute ;
948 }
949 }
950 else
951 {
952 wclass = kPlainWindowClass ;
953 }
954 }
955 else if ( HasFlag( wxCAPTION ) )
956 {
957 wclass = kDocumentWindowClass ;
958 }
959 else
960 {
961 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
962 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
963 {
964 wclass = kDocumentWindowClass ;
965 }
966 else
967 {
968 wclass = kPlainWindowClass ;
969 }
970 }
971
972 if ( HasFlag( wxMINIMIZE_BOX ) )
973 {
974 attr |= kWindowCollapseBoxAttribute ;
975 }
976 if ( HasFlag( wxMAXIMIZE_BOX ) )
977 {
978 attr |= kWindowFullZoomAttribute ;
979 }
980 if ( HasFlag( wxRESIZE_BORDER ) )
981 {
982 attr |= kWindowResizableAttribute ;
983 }
984 if ( HasFlag( wxCLOSE_BOX) )
985 {
986 attr |= kWindowCloseBoxAttribute ;
987 }
988
989 if (UMAGetSystemVersion() >= 0x1000)
990 {
991 // turn on live resizing (OS X only)
992 attr |= kWindowLiveResizeAttribute;
993 }
994
995 if (HasFlag(wxSTAY_ON_TOP))
996 wclass = kUtilityWindowClass;
997
998 #if TARGET_API_MAC_OSX
999 attr |= kWindowCompositingAttribute;
1000 #endif
1001
1002 if ( HasFlag(wxFRAME_SHAPED) )
1003 {
1004 WindowDefSpec customWindowDefSpec;
1005 customWindowDefSpec.defType = kWindowDefProcPtr;
1006 customWindowDefSpec.u.defProc = NewWindowDefUPP(wxShapedMacWindowDef);
1007
1008 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
1009 attr, &theBoundsRect,
1010 (WindowRef*) &m_macWindow);
1011 }
1012 else
1013 {
1014 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
1015 }
1016
1017 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
1018
1019 // the create commands are only for content rect, so we have to set the size again as
1020 // structure bounds
1021 SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1022
1023 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
1024 UMASetWTitle( (WindowRef) m_macWindow , title , m_font.GetEncoding() ) ;
1025 m_peer = new wxMacControl() ;
1026 #if TARGET_API_MAC_OSX
1027 // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
1028 // the content view, so we have to retrieve it explicitely
1029 HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
1030 m_peer->GetControlRefAddr() ) ;
1031 if ( !m_peer->Ok() )
1032 {
1033 // compatibility mode fallback
1034 GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
1035 }
1036 #else
1037 ::CreateRootControl( (WindowRef)m_macWindow , m_peer->GetControlRefAddr() ) ;
1038 #endif
1039 // the root control level handleer
1040 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
1041
1042 // the frame window event handler
1043 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
1044 MacInstallTopLevelWindowEventHandler() ;
1045
1046 m_macFocus = NULL ;
1047
1048 if ( HasFlag(wxFRAME_SHAPED) )
1049 {
1050 // default shape matches the window size
1051 wxRegion rgn(0, 0, w, h);
1052 SetShape(rgn);
1053 }
1054
1055 wxWindowCreateEvent event(this);
1056 GetEventHandler()->ProcessEvent(event);
1057 }
1058
1059 void wxTopLevelWindowMac::ClearBackground()
1060 {
1061 wxWindow::ClearBackground() ;
1062 }
1063
1064 // Raise the window to the top of the Z order
1065 void wxTopLevelWindowMac::Raise()
1066 {
1067 ::SelectWindow( (WindowRef)m_macWindow ) ;
1068 }
1069
1070 // Lower the window to the bottom of the Z order
1071 void wxTopLevelWindowMac::Lower()
1072 {
1073 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
1074 }
1075
1076
1077 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
1078 {
1079 if(s_macDeactivateWindow)
1080 {
1081 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow);
1082 s_macDeactivateWindow->MacActivate(timestamp, false);
1083 }
1084 }
1085
1086 void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
1087 {
1088 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
1089
1090 if(s_macDeactivateWindow==this)
1091 s_macDeactivateWindow=NULL;
1092 MacDelayedDeactivation(timestamp);
1093 MacPropagateHiliteChanged() ;
1094 }
1095
1096 void wxTopLevelWindowMac::SetTitle(const wxString& title)
1097 {
1098 wxWindow::SetTitle( title ) ;
1099 UMASetWTitle( (WindowRef)m_macWindow , title , m_font.GetEncoding() ) ;
1100 }
1101
1102 bool wxTopLevelWindowMac::Show(bool show)
1103 {
1104 if ( !wxTopLevelWindowBase::Show(show) )
1105 return FALSE;
1106
1107 if (show)
1108 {
1109 #if wxUSE_SYSTEM_OPTIONS //code contributed by Ryan Wilcox December 18, 2003
1110 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1) )
1111 {
1112 ::ShowWindow( (WindowRef)m_macWindow );
1113 }
1114 else
1115 #endif
1116 {
1117 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowShowTransitionAction,nil);
1118 }
1119 ::SelectWindow( (WindowRef)m_macWindow ) ;
1120 // as apps expect a size event to occur at this moment
1121 wxSizeEvent event( GetSize() , m_windowId);
1122 event.SetEventObject(this);
1123 GetEventHandler()->ProcessEvent(event);
1124 }
1125 else
1126 {
1127 #if wxUSE_SYSTEM_OPTIONS
1128 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1) )
1129 {
1130 ::HideWindow((WindowRef) m_macWindow );
1131 }
1132 else
1133 #endif
1134 {
1135 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowHideTransitionAction,nil);
1136 }
1137 }
1138
1139 MacPropagateVisibilityChanged() ;
1140
1141 return TRUE ;
1142 }
1143
1144 // we are still using coordinates of the content view, todo switch to structure bounds
1145
1146 void wxTopLevelWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1147 {
1148 Rect content ;
1149 Rect structure ;
1150 GetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &structure ) ;
1151 GetWindowBounds( (WindowRef) m_macWindow, kWindowContentRgn , &content ) ;
1152
1153 left = content.left - structure.left ;
1154 top = content.top - structure.top ;
1155 right = structure.right - content.right ;
1156 bottom = structure.bottom - content.bottom ;
1157 }
1158
1159 void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1160 {
1161 Rect bounds = { y , x , y + height , x + width } ;
1162 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1163 }
1164
1165 void wxTopLevelWindowMac::DoGetPosition( int *x, int *y ) const
1166 {
1167 Rect bounds ;
1168 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1169 if(x) *x = bounds.left ;
1170 if(y) *y = bounds.top ;
1171 }
1172 void wxTopLevelWindowMac::DoGetSize( int *width, int *height ) const
1173 {
1174 Rect bounds ;
1175 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1176 if(width) *width = bounds.right - bounds.left ;
1177 if(height) *height = bounds.bottom - bounds.top ;
1178 }
1179
1180 void wxTopLevelWindowMac::DoGetClientSize( int *width, int *height ) const
1181 {
1182 Rect bounds ;
1183 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowContentRgn , &bounds )) ;
1184 if(width) *width = bounds.right - bounds.left ;
1185 if(height) *height = bounds.bottom - bounds.top ;
1186 }
1187
1188 void wxTopLevelWindowMac::MacSetMetalAppearance( bool set )
1189 {
1190 #if TARGET_API_MAC_OSX
1191 UInt32 attr = 0 ;
1192 GetWindowAttributes((WindowRef) m_macWindow , &attr ) ;
1193 wxASSERT_MSG( attr & kWindowCompositingAttribute ,
1194 wxT("Cannot set metal appearance on a non-compositing window") ) ;
1195
1196 MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
1197 set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
1198 #endif
1199 }
1200
1201 bool wxTopLevelWindowMac::MacGetMetalAppearance() const
1202 {
1203 #if TARGET_API_MAC_OSX
1204 return MacGetWindowAttributes() & kWindowMetalAttribute ;
1205 #else
1206 return false ;
1207 #endif
1208 }
1209
1210 void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
1211 {
1212 ChangeWindowAttributes ( (WindowRef) m_macWindow , attributesToSet, attributesToClear ) ;
1213 }
1214
1215 wxUint32 wxTopLevelWindowMac::MacGetWindowAttributes() const
1216 {
1217 UInt32 attr = 0 ;
1218 GetWindowAttributes((WindowRef) m_macWindow , &attr ) ;
1219 return attr ;
1220 }
1221
1222 // ---------------------------------------------------------------------------
1223 // Shape implementation
1224 // ---------------------------------------------------------------------------
1225
1226
1227 bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1228 {
1229 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), FALSE,
1230 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1231
1232 // The empty region signifies that the shape should be removed from the
1233 // window.
1234 if ( region.IsEmpty() )
1235 {
1236 wxSize sz = GetClientSize();
1237 wxRegion rgn(0, 0, sz.x, sz.y);
1238 return SetShape(rgn);
1239 }
1240
1241 // Make a copy of the region
1242 RgnHandle shapeRegion = NewRgn();
1243 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1244
1245 // Dispose of any shape region we may already have
1246 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1247 if ( oldRgn )
1248 DisposeRgn(oldRgn);
1249
1250 // Save the region so we can use it later
1251 SetWRefCon((WindowRef)MacGetWindowRef(), (SInt32)shapeRegion);
1252
1253 // Tell the window manager that the window has changed shape
1254 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
1255 return TRUE;
1256 }
1257
1258 // ---------------------------------------------------------------------------
1259 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1260 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1261 // ---------------------------------------------------------------------------
1262
1263 static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1264 {
1265 GetWindowPortBounds(window, inRect);
1266 Point pt = {inRect->left, inRect->top};
1267 QDLocalToGlobalPoint( GetWindowPort(window) , &pt ) ;
1268 inRect->top = pt.v;
1269 inRect->left = pt.h;
1270 inRect->bottom += pt.v;
1271 inRect->right += pt.h;
1272 }
1273
1274
1275 static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1276 {
1277 /*------------------------------------------------------
1278 Define which options your custom window supports.
1279 --------------------------------------------------------*/
1280 //just enable everything for our demo
1281 *(OptionBits*)param=//kWindowCanGrow|
1282 //kWindowCanZoom|
1283 //kWindowCanCollapse|
1284 //kWindowCanGetWindowRegion|
1285 //kWindowHasTitleBar|
1286 //kWindowSupportsDragHilite|
1287 kWindowCanDrawInCurrentPort|
1288 //kWindowCanMeasureTitle|
1289 kWindowWantsDisposeAtProcessDeath|
1290 kWindowSupportsGetGrowImageRegion|
1291 kWindowDefSupportsColorGrafPort;
1292 return 1;
1293 }
1294
1295 // The content region is left as a rectangle matching the window size, this is
1296 // so the origin in the paint event, and etc. still matches what the
1297 // programmer expects.
1298 static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1299 {
1300 SetEmptyRgn(rgn);
1301 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1302 if (win)
1303 {
1304 Rect r ;
1305 wxShapedMacWindowGetPos(window, &r ) ;
1306 RectRgn( rgn , &r ) ;
1307 }
1308 }
1309
1310 // The structure region is set to the shape given to the SetShape method.
1311 static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1312 {
1313 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1314
1315 SetEmptyRgn(rgn);
1316 if (cachedRegion)
1317 {
1318 Rect windowRect;
1319 wxShapedMacWindowGetPos(window, &windowRect); //how big is the window
1320 CopyRgn(cachedRegion, rgn); //make a copy of our cached region
1321 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
1322 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1323 }
1324 }
1325
1326
1327
1328 static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1329 {
1330 GetWindowRegionPtr rgnRec=(GetWindowRegionPtr)param;
1331
1332 switch(rgnRec->regionCode)
1333 {
1334 case kWindowStructureRgn:
1335 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1336 break;
1337 case kWindowContentRgn:
1338 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1339 break;
1340 default:
1341 SetEmptyRgn(rgnRec->winRgn);
1342 } //switch
1343
1344 return noErr;
1345 }
1346
1347
1348 static SInt32 wxShapedMacWindowHitTest(WindowRef window,SInt32 param)
1349 {
1350 /*------------------------------------------------------
1351 Determine the region of the window which was hit
1352 --------------------------------------------------------*/
1353 Point hitPoint;
1354 static RgnHandle tempRgn=nil;
1355
1356 if(!tempRgn)
1357 tempRgn=NewRgn();
1358
1359 SetPt(&hitPoint,LoWord(param),HiWord(param));//get the point clicked
1360
1361 //Mac OS 8.5 or later
1362 wxShapedMacWindowStructureRegion(window, tempRgn);
1363 if (PtInRgn(hitPoint, tempRgn)) //in window content region?
1364 return wInContent;
1365
1366 return wNoHit;//no significant area was hit.
1367 }
1368
1369
1370 static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1371 {
1372 switch(message)
1373 {
1374 case kWindowMsgHitTest:
1375 return wxShapedMacWindowHitTest(window,param);
1376
1377 case kWindowMsgGetFeatures:
1378 return wxShapedMacWindowGetFeatures(window,param);
1379
1380 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1381 case kWindowMsgGetRegion:
1382 return wxShapedMacWindowGetRegion(window,param);
1383 }
1384
1385 return 0;
1386 }
1387
1388 // ---------------------------------------------------------------------------
1389