1 /////////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/mac/carbon/toplevel.cpp 
   3 // Purpose:     implements wxTopLevelWindow for Mac 
   4 // Author:      Stefan Csomor 
   8 // Copyright:   (c) 2001-2004 Stefan Csomor 
   9 // License:     wxWindows licence 
  10 /////////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  20 // For compilers that support precompilation, includes "wx.h". 
  21 #include "wx/wxprec.h" 
  27 #include "wx/toplevel.h" 
  32     #include "wx/string.h" 
  35     #include "wx/settings.h" 
  36     #include "wx/strconv.h" 
  37     #include "wx/control.h" 
  40 #include "wx/mac/uma.h" 
  41 #include "wx/mac/aga.h" 
  42 #include "wx/tooltip.h" 
  45 #if wxUSE_SYSTEM_OPTIONS 
  46     #include "wx/sysopt.h" 
  50 #include <ToolUtils.h> 
  54 #include "wx/mac/private.h" 
  56 // ---------------------------------------------------------------------------- 
  58 // ---------------------------------------------------------------------------- 
  60 // trace mask for activation tracing messages 
  61 static const wxChar 
*TRACE_ACTIVATE 
= _T("activation"); 
  63 // ---------------------------------------------------------------------------- 
  65 // ---------------------------------------------------------------------------- 
  67 // list of all frames and modeless dialogs 
  68 wxWindowList       wxModelessWindows
; 
  70 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
); 
  72 // ============================================================================ 
  73 // wxTopLevelWindowMac implementation 
  74 // ============================================================================ 
  76 BEGIN_EVENT_TABLE(wxTopLevelWindowMac
, wxTopLevelWindowBase
) 
  80 // --------------------------------------------------------------------------- 
  82 // --------------------------------------------------------------------------- 
  84 static const EventTypeSpec eventList
[] = 
  86     // TODO: remove control related event like key and mouse (except for WindowLeave events) 
  88     { kEventClassKeyboard
, kEventRawKeyDown 
} , 
  89     { kEventClassKeyboard
, kEventRawKeyRepeat 
} , 
  90     { kEventClassKeyboard
, kEventRawKeyUp 
} , 
  91     { kEventClassKeyboard
, kEventRawKeyModifiersChanged 
} , 
  93     { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent 
} , 
  94     { kEventClassTextInput
, kEventTextInputUpdateActiveInputArea 
} , 
  96     { kEventClassWindow 
, kEventWindowShown 
} , 
  97     { kEventClassWindow 
, kEventWindowActivated 
} , 
  98     { kEventClassWindow 
, kEventWindowDeactivated 
} , 
  99     { kEventClassWindow 
, kEventWindowBoundsChanging 
} , 
 100     { kEventClassWindow 
, kEventWindowBoundsChanged 
} , 
 101     { kEventClassWindow 
, kEventWindowClose 
} , 
 103     // we have to catch these events on the toplevel window level, 
 104     // as controls don't get the raw mouse events anymore 
 106     { kEventClassMouse 
, kEventMouseDown 
} , 
 107     { kEventClassMouse 
, kEventMouseUp 
} , 
 108     { kEventClassMouse 
, kEventMouseWheelMoved 
} , 
 109     { kEventClassMouse 
, kEventMouseMoved 
} , 
 110     { kEventClassMouse 
, kEventMouseDragged 
} , 
 113 static pascal OSStatus 
KeyboardEventHandler( EventHandlerCallRef handler 
, EventRef event 
, void *data 
) 
 115     OSStatus result 
= eventNotHandledErr 
; 
 116     // call DoFindFocus instead of FindFocus, because for Composite Windows(like WxGenericListCtrl) 
 117     // FindFocus does not return the actual focus window, but the enclosing window 
 118     wxWindow
* focus 
= wxWindow::DoFindFocus(); 
 120         focus 
= (wxTopLevelWindowMac
*) data 
; 
 122     unsigned char charCode 
; 
 130     UInt32 when 
= EventTimeToTicks( GetEventTime( event 
) ) ; 
 133     ByteCount dataSize 
= 0 ; 
 134     if ( GetEventParameter( event
, kEventParamKeyUnicodes
, typeUnicodeText
, NULL
, 0 , &dataSize
, NULL 
) == noErr 
) 
 137         int numChars 
= dataSize 
/ sizeof( UniChar
) + 1; 
 139         UniChar
* charBuf 
= buf 
; 
 141         if ( numChars 
* 2 > 4 ) 
 142             charBuf 
= new UniChar
[ numChars 
] ; 
 143         GetEventParameter( event
, kEventParamKeyUnicodes
, typeUnicodeText
, NULL
, dataSize 
, NULL 
, charBuf 
) ; 
 144         charBuf
[ numChars 
- 1 ] = 0; 
 146 #if SIZEOF_WCHAR_T == 2 
 147         uniChar 
= charBuf
[0] ; 
 149         wxMBConvUTF16 converter 
; 
 150         converter
.MB2WC( uniChar 
, (const char*)charBuf 
, 2 ) ; 
 153         if ( numChars 
* 2 > 4 ) 
 158     GetEventParameter( event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, sizeof(char), NULL
, &charCode 
); 
 159     GetEventParameter( event
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode 
); 
 160     GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers 
); 
 161     GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
, sizeof(Point
), NULL
, &point 
); 
 163     UInt32 message 
= (keyCode 
<< 8) + charCode
; 
 164     switch ( GetEventKind( event 
) ) 
 166         case kEventRawKeyRepeat 
: 
 167         case kEventRawKeyDown 
: 
 169                 WXEVENTREF formerEvent 
= wxTheApp
->MacGetCurrentEvent() ; 
 170                 WXEVENTHANDLERCALLREF formerHandler 
= wxTheApp
->MacGetCurrentEventHandlerCallRef() ; 
 171                 wxTheApp
->MacSetCurrentEvent( event 
, handler 
) ; 
 172                 if ( /* focus && */ wxTheApp
->MacSendKeyDownEvent( 
 173                     focus 
, message 
, modifiers 
, when 
, point
.h 
, point
.v 
, uniChar
[0] ) ) 
 177                 wxTheApp
->MacSetCurrentEvent( formerEvent 
, formerHandler 
) ; 
 181         case kEventRawKeyUp 
: 
 182             if ( /* focus && */ wxTheApp
->MacSendKeyUpEvent( 
 183                 focus 
, message 
, modifiers 
, when 
, point
.h 
, point
.v 
, uniChar
[0] ) ) 
 189         case kEventRawKeyModifiersChanged 
: 
 191                 wxKeyEvent 
event(wxEVT_KEY_DOWN
); 
 193                 event
.m_shiftDown 
= modifiers 
& shiftKey
; 
 194                 event
.m_controlDown 
= modifiers 
& controlKey
; 
 195                 event
.m_altDown 
= modifiers 
& optionKey
; 
 196                 event
.m_metaDown 
= modifiers 
& cmdKey
; 
 201                 event
.m_uniChar 
= uniChar
[0] ; 
 204                 event
.SetTimestamp(when
); 
 205                 event
.SetEventObject(focus
); 
 207                 if ( /* focus && */ (modifiers 
^ wxApp::s_lastModifiers 
) & controlKey 
) 
 209                     event
.m_keyCode 
= WXK_CONTROL 
; 
 210                     event
.SetEventType( ( modifiers 
& controlKey 
) ? wxEVT_KEY_DOWN 
: wxEVT_KEY_UP 
) ; 
 211                     focus
->GetEventHandler()->ProcessEvent( event 
) ; 
 213                 if ( /* focus && */ (modifiers 
^ wxApp::s_lastModifiers 
) & shiftKey 
) 
 215                     event
.m_keyCode 
= WXK_SHIFT 
; 
 216                     event
.SetEventType( ( modifiers 
& shiftKey 
) ? wxEVT_KEY_DOWN 
: wxEVT_KEY_UP 
) ; 
 217                     focus
->GetEventHandler()->ProcessEvent( event 
) ; 
 219                 if ( /* focus && */ (modifiers 
^ wxApp::s_lastModifiers 
) & optionKey 
) 
 221                     event
.m_keyCode 
= WXK_ALT 
; 
 222                     event
.SetEventType( ( modifiers 
& optionKey 
) ? wxEVT_KEY_DOWN 
: wxEVT_KEY_UP 
) ; 
 223                     focus
->GetEventHandler()->ProcessEvent( event 
) ; 
 225                 if ( /* focus && */ (modifiers 
^ wxApp::s_lastModifiers 
) & cmdKey 
) 
 227                     event
.m_keyCode 
= WXK_COMMAND 
; 
 228                     event
.SetEventType( ( modifiers 
& cmdKey 
) ? wxEVT_KEY_DOWN 
: wxEVT_KEY_UP 
) ; 
 229                     focus
->GetEventHandler()->ProcessEvent( event 
) ; 
 232                 wxApp::s_lastModifiers 
= modifiers 
; 
 243 // we don't interfere with foreign controls on our toplevel windows, therefore we always give back eventNotHandledErr 
 244 // for windows that we didn't create (like eg Scrollbars in a databrowser), or for controls where we did not handle the 
 247 // This handler can also be called from app level where data (ie target window) may be null or a non wx window 
 249 wxWindow
* g_MacLastWindow 
= NULL 
; 
 251 EventMouseButton g_lastButton 
= 0 ; 
 252 bool g_lastButtonWasFakeRight 
= false ; 
 254 void SetupMouseEvent( wxMouseEvent 
&wxevent 
, wxMacCarbonEvent 
&cEvent 
) 
 256     UInt32 modifiers 
= cEvent
.GetParameter
<UInt32
>(kEventParamKeyModifiers
, typeUInt32
) ; 
 257     Point screenMouseLocation 
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ; 
 259     // this parameter are not given for all events 
 260     EventMouseButton button 
= 0 ; 
 261     UInt32 clickCount 
= 0 ; 
 262     cEvent
.GetParameter
<EventMouseButton
>( kEventParamMouseButton
, typeMouseButton 
, &button 
) ; 
 263     cEvent
.GetParameter
<UInt32
>( kEventParamClickCount
, typeUInt32 
, &clickCount 
) ; 
 265     wxevent
.m_x 
= screenMouseLocation
.h
; 
 266     wxevent
.m_y 
= screenMouseLocation
.v
; 
 267     wxevent
.m_shiftDown 
= modifiers 
& shiftKey
; 
 268     wxevent
.m_controlDown 
= modifiers 
& controlKey
; 
 269     wxevent
.m_altDown 
= modifiers 
& optionKey
; 
 270     wxevent
.m_metaDown 
= modifiers 
& cmdKey
; 
 271     wxevent
.SetTimestamp( cEvent
.GetTicks() ) ; 
 273     // a control click is interpreted as a right click 
 274     bool thisButtonIsFakeRight 
= false ; 
 275     if ( button 
== kEventMouseButtonPrimary 
&& (modifiers 
& controlKey
) ) 
 277         button 
= kEventMouseButtonSecondary 
; 
 278         thisButtonIsFakeRight 
= true ; 
 280     // otherwise we report double clicks by connecting a left click with a ctrl-left click 
 281     if ( clickCount 
> 1 && button 
!= g_lastButton 
) 
 284     // we must make sure that our synthetic 'right' button corresponds in 
 285     // mouse down, moved and mouse up, and does not deliver a right down and left up 
 287     if ( cEvent
.GetKind() == kEventMouseDown 
) 
 289         g_lastButton 
= button 
; 
 290         g_lastButtonWasFakeRight 
= thisButtonIsFakeRight 
; 
 296         g_lastButtonWasFakeRight 
= false ; 
 298     else if ( g_lastButton 
== kEventMouseButtonSecondary 
&& g_lastButtonWasFakeRight 
) 
 299         button 
= g_lastButton 
; 
 301     // determine the correct down state, wx does not want a 'down' for a mouseUp event, 
 302     // while mac delivers this button 
 303     if ( button 
!= 0 && cEvent
.GetKind() != kEventMouseUp 
) 
 307             case kEventMouseButtonPrimary 
: 
 308                 wxevent
.m_leftDown 
= true ; 
 311             case kEventMouseButtonSecondary 
: 
 312                 wxevent
.m_rightDown 
= true ; 
 315             case kEventMouseButtonTertiary 
: 
 316                 wxevent
.m_middleDown 
= true ; 
 324     // translate into wx types 
 325     switch ( cEvent
.GetKind() ) 
 327         case kEventMouseDown 
: 
 330                 case kEventMouseButtonPrimary 
: 
 331                     wxevent
.SetEventType( clickCount 
> 1 ? wxEVT_LEFT_DCLICK 
: wxEVT_LEFT_DOWN 
)  ; 
 334                 case kEventMouseButtonSecondary 
: 
 335                     wxevent
.SetEventType( clickCount 
> 1 ? wxEVT_RIGHT_DCLICK 
: wxEVT_RIGHT_DOWN 
) ; 
 338                 case kEventMouseButtonTertiary 
: 
 339                     wxevent
.SetEventType( clickCount 
> 1 ? wxEVT_MIDDLE_DCLICK 
: wxEVT_MIDDLE_DOWN 
) ; 
 350                 case kEventMouseButtonPrimary 
: 
 351                     wxevent
.SetEventType( wxEVT_LEFT_UP 
)  ; 
 354                 case kEventMouseButtonSecondary 
: 
 355                     wxevent
.SetEventType( wxEVT_RIGHT_UP 
) ; 
 358                 case kEventMouseButtonTertiary 
: 
 359                     wxevent
.SetEventType( wxEVT_MIDDLE_UP 
) ; 
 367      case kEventMouseWheelMoved 
: 
 369             wxevent
.SetEventType( wxEVT_MOUSEWHEEL 
) ; 
 371             EventMouseWheelAxis axis 
= cEvent
.GetParameter
<EventMouseWheelAxis
>(kEventParamMouseWheelAxis
, typeMouseWheelAxis
) ; 
 372             SInt32 delta 
= cEvent
.GetParameter
<SInt32
>(kEventParamMouseWheelDelta
, typeSInt32
) ; 
 374             wxevent
.m_wheelRotation 
= delta
; 
 375             wxevent
.m_wheelDelta 
= 1; 
 376             wxevent
.m_linesPerAction 
= 1; 
 377             if ( axis 
== kEventMouseWheelAxisX 
) 
 378                 wxevent
.m_wheelAxis 
= 1; 
 383             wxevent
.SetEventType( wxEVT_MOTION 
) ; 
 388 ControlRef 
wxMacFindSubControl( wxTopLevelWindowMac
* toplevelWindow
, const Point
& location 
, ControlRef superControl 
, ControlPartCode 
*outPart 
) 
 392         UInt16 childrenCount 
= 0 ; 
 393         ControlHandle sibling 
; 
 395         OSStatus err 
= CountSubControls( superControl 
, &childrenCount 
) ; 
 396         if ( err 
== errControlIsNotEmbedder 
) 
 399         wxASSERT_MSG( err 
== noErr 
, wxT("Unexpected error when accessing subcontrols") ) ; 
 401         for ( UInt16 i 
= childrenCount 
; i 
>=1  ; --i 
) 
 403             err 
= GetIndexedSubControl( superControl 
, i 
, & sibling 
) ; 
 404             if ( err 
== errControlIsNotEmbedder 
) 
 407             wxASSERT_MSG( err 
== noErr 
, wxT("Unexpected error when accessing subcontrols") ) ; 
 408             if ( IsControlVisible( sibling 
) ) 
 410                 UMAGetControlBoundsInWindowCoords( sibling 
, &r 
) ; 
 411                 if ( MacPtInRect( location 
, &r 
) ) 
 413                     ControlHandle child 
= wxMacFindSubControl( toplevelWindow 
, location 
, sibling 
, outPart 
) ; 
 420                         Point testLocation 
= location 
; 
 422                         if ( toplevelWindow 
) 
 424                             testLocation
.h 
-= r
.left 
; 
 425                             testLocation
.v 
-= r
.top 
; 
 428                         *outPart 
= TestControl( sibling 
, testLocation 
) ; 
 440 ControlRef 
wxMacFindControlUnderMouse( wxTopLevelWindowMac
* toplevelWindow 
, const Point
& location 
, WindowRef window 
, ControlPartCode 
*outPart 
) 
 442 #if TARGET_API_MAC_OSX 
 443     if ( UMAGetSystemVersion() >= 0x1030 ) 
 444         return FindControlUnderMouse( location 
, window 
, outPart 
) ; 
 447     ControlRef rootControl 
= NULL 
; 
 448     verify_noerr( GetRootControl( window 
, &rootControl 
) ) ; 
 450     return wxMacFindSubControl( toplevelWindow 
, location 
, rootControl 
, outPart 
) ; 
 453 #define NEW_CAPTURE_HANDLING 1 
 455 pascal OSStatus 
wxMacTopLevelMouseEventHandler( EventHandlerCallRef handler 
, EventRef event 
, void *data 
) 
 457     wxTopLevelWindowMac
* toplevelWindow 
= (wxTopLevelWindowMac
*) data 
; 
 459     OSStatus result 
= eventNotHandledErr 
; 
 461     wxMacCarbonEvent 
cEvent( event 
) ; 
 463     Point screenMouseLocation 
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ; 
 464     Point windowMouseLocation 
= screenMouseLocation 
; 
 466     WindowRef window 
= NULL
; 
 467     short windowPart 
= ::FindWindow(screenMouseLocation
, &window
); 
 469     wxWindow
* currentMouseWindow 
= NULL 
; 
 470     ControlRef control 
= NULL 
; 
 472 #if NEW_CAPTURE_HANDLING 
 473     if ( wxApp::s_captureWindow 
) 
 475         window 
= (WindowRef
) wxApp::s_captureWindow
->MacGetTopLevelWindowRef() ; 
 476         windowPart 
= inContent 
; 
 482         wxMacGlobalToLocal( window
,  &windowMouseLocation 
) ; 
 484         if ( wxApp::s_captureWindow
 
 485 #if !NEW_CAPTURE_HANDLING 
 486              && wxApp::s_captureWindow
->MacGetTopLevelWindowRef() == (WXWindow
) window 
&& windowPart 
== inContent
 
 490             currentMouseWindow 
= wxApp::s_captureWindow 
; 
 492         else if ( (IsWindowActive(window
) && windowPart 
== inContent
) ) 
 494             ControlPartCode part 
; 
 495             control 
= wxMacFindControlUnderMouse( toplevelWindow 
, windowMouseLocation 
, window 
, &part 
) ; 
 496             // if there is no control below the mouse position, send the event to the toplevel window itself 
 499                 currentMouseWindow 
= (wxWindow
*) data 
; 
 503                 currentMouseWindow 
= wxFindControlFromMacControl( control 
) ; 
 504                 if ( currentMouseWindow 
== NULL 
&& cEvent
.GetKind() == kEventMouseMoved 
) 
 507                     // for wxToolBar to function we have to send certaint events to it 
 508                     // instead of its children (wxToolBarTools) 
 510                     GetSuperControl(control
, &parent 
); 
 511                     wxWindow 
*wxParent 
= wxFindControlFromMacControl( parent 
) ; 
 512                     if ( wxParent 
&& wxParent
->IsKindOf( CLASSINFO( wxToolBar 
) ) ) 
 513                         currentMouseWindow 
= wxParent 
; 
 518             // disabled windows must not get any input messages 
 519             if ( currentMouseWindow 
&& !currentMouseWindow
->MacIsReallyEnabled() ) 
 520                 currentMouseWindow 
= NULL
; 
 524     wxMouseEvent 
wxevent(wxEVT_LEFT_DOWN
); 
 525     SetupMouseEvent( wxevent 
, cEvent 
) ; 
 527     // handle all enter / leave events 
 529     if ( currentMouseWindow 
!= g_MacLastWindow 
) 
 531         if ( g_MacLastWindow 
) 
 533             wxMouseEvent 
eventleave(wxevent
); 
 534             eventleave
.SetEventType( wxEVT_LEAVE_WINDOW 
); 
 535             g_MacLastWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y 
); 
 536             eventleave
.SetEventObject( g_MacLastWindow 
) ; 
 537             wxevent
.SetId( g_MacLastWindow
->GetId() ) ; 
 540             wxToolTip::RelayEvent( g_MacLastWindow 
, eventleave
); 
 543             g_MacLastWindow
->GetEventHandler()->ProcessEvent(eventleave
); 
 546         if ( currentMouseWindow 
) 
 548             wxMouseEvent 
evententer(wxevent
); 
 549             evententer
.SetEventType( wxEVT_ENTER_WINDOW 
); 
 550             currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y 
); 
 551             evententer
.SetEventObject( currentMouseWindow 
) ; 
 552             wxevent
.SetId( currentMouseWindow
->GetId() ) ; 
 555             wxToolTip::RelayEvent( currentMouseWindow 
, evententer 
); 
 558             currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
); 
 561         g_MacLastWindow 
= currentMouseWindow 
; 
 564     if ( windowPart 
== inMenuBar 
) 
 566         // special case menu bar, as we are having a low-level runloop we must do it ourselves 
 567         if ( cEvent
.GetKind() == kEventMouseDown 
) 
 569             ::MenuSelect( screenMouseLocation 
) ; 
 573     else if ( currentMouseWindow 
) 
 575         wxWindow 
*currentMouseWindowParent 
= currentMouseWindow
->GetParent(); 
 577         currentMouseWindow
->ScreenToClient( &wxevent
.m_x 
, &wxevent
.m_y 
) ; 
 579         wxevent
.SetEventObject( currentMouseWindow 
) ; 
 580         wxevent
.SetId( currentMouseWindow
->GetId() ) ; 
 582         // make tooltips current 
 585         if ( wxevent
.GetEventType() == wxEVT_MOTION 
) 
 586             wxToolTip::RelayEvent( currentMouseWindow 
, wxevent 
); 
 589         if ( currentMouseWindow
->GetEventHandler()->ProcessEvent(wxevent
) ) 
 591             if ((currentMouseWindowParent 
!= NULL
) && 
 592                 (currentMouseWindowParent
->GetChildren().Find(currentMouseWindow
) == NULL
)) 
 593                 currentMouseWindow 
= NULL
; 
 599             // if the user code did _not_ handle the event, then perform the 
 600             // default processing 
 601             if ( wxevent
.GetEventType() == wxEVT_LEFT_DOWN 
) 
 603                 // ... that is set focus to this window 
 604                 if (currentMouseWindow
->CanAcceptFocus() && wxWindow::FindFocus()!=currentMouseWindow
) 
 605                     currentMouseWindow
->SetFocus(); 
 608             ControlPartCode dummyPart 
; 
 609             // if built-in find control is finding the wrong control (ie static box instead of overlaid 
 610             // button, we cannot let the standard handler do its job, but must handle manually 
 612             if ( ( cEvent
.GetKind() == kEventMouseDown 
) 
 615                 (FindControlUnderMouse(windowMouseLocation 
, window 
, &dummyPart
) != 
 616                 wxMacFindControlUnderMouse( toplevelWindow 
, windowMouseLocation 
, window 
, &dummyPart 
) ) 
 620                 if ( currentMouseWindow
->MacIsReallyEnabled() ) 
 622                     EventModifiers modifiers 
= cEvent
.GetParameter
<EventModifiers
>(kEventParamKeyModifiers
, typeUInt32
) ; 
 623                     Point clickLocation 
= windowMouseLocation 
; 
 625                     currentMouseWindow
->MacRootWindowToWindow( &clickLocation
.h 
, &clickLocation
.v 
) ; 
 627                     HandleControlClick( (ControlRef
) currentMouseWindow
->GetHandle() , clickLocation 
, 
 628                         modifiers 
, (ControlActionUPP 
) -1 ) ; 
 630                     if ((currentMouseWindowParent 
!= NULL
) && 
 631                         (currentMouseWindowParent
->GetChildren().Find(currentMouseWindow
) == NULL
)) 
 633                         currentMouseWindow 
= NULL
; 
 641         if ( cEvent
.GetKind() == kEventMouseUp 
&& wxApp::s_captureWindow 
) 
 643             wxApp::s_captureWindow 
= NULL 
; 
 649         wxWindow
* cursorTarget 
= currentMouseWindow 
; 
 650         wxPoint 
cursorPoint( wxevent
.m_x 
, wxevent
.m_y 
) ; 
 652         while ( cursorTarget 
&& !cursorTarget
->MacSetupCursor( cursorPoint 
) ) 
 654             cursorTarget 
= cursorTarget
->GetParent() ; 
 656                 cursorPoint 
+= cursorTarget
->GetPosition(); 
 660     else // currentMouseWindow == NULL 
 662         // don't mess with controls we don't know about 
 663         // for some reason returning eventNotHandledErr does not lead to the correct behaviour 
 664         // so we try sending them the correct control directly 
 665         if ( cEvent
.GetKind() == kEventMouseDown 
&& toplevelWindow 
&& control 
) 
 667             EventModifiers modifiers 
= cEvent
.GetParameter
<EventModifiers
>(kEventParamKeyModifiers
, typeUInt32
) ; 
 668             Point clickLocation 
= windowMouseLocation 
; 
 669 #if TARGET_API_MAC_OSX 
 671             hiPoint
.x 
= clickLocation
.h 
; 
 672             hiPoint
.y 
= clickLocation
.v 
; 
 673             HIViewConvertPoint( &hiPoint 
, (ControlRef
) toplevelWindow
->GetHandle() , control  
) ; 
 674             clickLocation
.h 
= (int)hiPoint
.x 
; 
 675             clickLocation
.v 
= (int)hiPoint
.y 
; 
 676 #endif // TARGET_API_MAC_OSX 
 678             HandleControlClick( control 
, clickLocation 
, modifiers 
, (ControlActionUPP 
) -1 ) ; 
 686 static pascal OSStatus 
wxMacTopLevelWindowEventHandler( EventHandlerCallRef handler 
, EventRef event 
, void *data 
) 
 688     OSStatus result 
= eventNotHandledErr 
; 
 690     wxMacCarbonEvent 
cEvent( event 
) ; 
 692     // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ; 
 693     wxTopLevelWindowMac
* toplevelWindow 
= (wxTopLevelWindowMac
*) data 
; 
 695     switch ( GetEventKind( event 
) ) 
 697         case kEventWindowActivated 
: 
 699             toplevelWindow
->MacActivate( cEvent
.GetTicks() , true) ; 
 700             wxActivateEvent 
wxevent(wxEVT_ACTIVATE
, true , toplevelWindow
->GetId()); 
 701             wxevent
.SetTimestamp( cEvent
.GetTicks() ) ; 
 702             wxevent
.SetEventObject(toplevelWindow
); 
 703             toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
); 
 704             // we still sending an eventNotHandledErr in order to allow for default processing 
 708         case kEventWindowDeactivated 
: 
 710             toplevelWindow
->MacActivate(cEvent
.GetTicks() , false) ; 
 711             wxActivateEvent 
wxevent(wxEVT_ACTIVATE
, false , toplevelWindow
->GetId()); 
 712             wxevent
.SetTimestamp( cEvent
.GetTicks() ) ; 
 713             wxevent
.SetEventObject(toplevelWindow
); 
 714             toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
); 
 715             // we still sending an eventNotHandledErr in order to allow for default processing 
 719         case kEventWindowShown 
: 
 720             toplevelWindow
->Refresh() ; 
 724         case kEventWindowClose 
: 
 725             toplevelWindow
->Close() ; 
 729         case kEventWindowBoundsChanged 
: 
 731             UInt32 attributes 
= cEvent
.GetParameter
<UInt32
>(kEventParamAttributes
, typeUInt32
) ; 
 732             Rect newRect 
= cEvent
.GetParameter
<Rect
>(kEventParamCurrentBounds
) ; 
 733             wxRect 
r( newRect
.left 
, newRect
.top 
, newRect
.right 
- newRect
.left 
, newRect
.bottom 
- newRect
.top 
) ; 
 734             if ( attributes 
& kWindowBoundsChangeSizeChanged 
) 
 736                 // according to the other ports we handle this within the OS level 
 737                 // resize event, not within a wxSizeEvent 
 738                 wxFrame 
*frame 
= wxDynamicCast( toplevelWindow 
, wxFrame 
) ; 
 741                     frame
->PositionBars(); 
 744                 wxSizeEvent 
event( r
.GetSize() , toplevelWindow
->GetId() ) ; 
 745                 event
.SetEventObject( toplevelWindow 
) ; 
 747                 toplevelWindow
->GetEventHandler()->ProcessEvent(event
) ; 
 748                 toplevelWindow
->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified 
 751             if ( attributes 
& kWindowBoundsChangeOriginChanged 
) 
 753                 wxMoveEvent 
event( r
.GetLeftTop() , toplevelWindow
->GetId() ) ; 
 754                 event
.SetEventObject( toplevelWindow 
) ; 
 755                 toplevelWindow
->GetEventHandler()->ProcessEvent(event
) ; 
 762         case kEventWindowBoundsChanging 
: 
 764             UInt32 attributes 
= cEvent
.GetParameter
<UInt32
>(kEventParamAttributes
,typeUInt32
) ; 
 765             Rect newRect 
= cEvent
.GetParameter
<Rect
>(kEventParamCurrentBounds
) ; 
 767             if ( (attributes 
& kWindowBoundsChangeSizeChanged
) || (attributes 
& kWindowBoundsChangeOriginChanged
) ) 
 769                 // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates 
 770                 int left 
, top 
, right 
, bottom 
; 
 771                 toplevelWindow
->MacGetContentAreaInset( left 
, top 
, right 
, bottom 
) ; 
 776                     newRect
.right 
- newRect
.left 
+ left 
+ right
, 
 777                     newRect
.bottom 
- newRect
.top 
+ top 
+ bottom 
) ; 
 779                 // this is a EVT_SIZING not a EVT_SIZE type ! 
 780                 wxSizeEvent 
wxevent( r 
, toplevelWindow
->GetId() ) ; 
 781                 wxevent
.SetEventObject( toplevelWindow 
) ; 
 783                 if ( toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
) ) 
 784                     adjustR 
= wxevent
.GetRect() ; 
 786                 if ( toplevelWindow
->GetMaxWidth() != -1 && adjustR
.GetWidth() > toplevelWindow
->GetMaxWidth() ) 
 787                     adjustR
.SetWidth( toplevelWindow
->GetMaxWidth() ) ; 
 788                 if ( toplevelWindow
->GetMaxHeight() != -1 && adjustR
.GetHeight() > toplevelWindow
->GetMaxHeight() ) 
 789                     adjustR
.SetHeight( toplevelWindow
->GetMaxHeight() ) ; 
 790                 if ( toplevelWindow
->GetMinWidth() != -1 && adjustR
.GetWidth() < toplevelWindow
->GetMinWidth() ) 
 791                     adjustR
.SetWidth( toplevelWindow
->GetMinWidth() ) ; 
 792                 if ( toplevelWindow
->GetMinHeight() != -1 && adjustR
.GetHeight() < toplevelWindow
->GetMinHeight() ) 
 793                     adjustR
.SetHeight( toplevelWindow
->GetMinHeight() ) ; 
 794                 const Rect adjustedRect 
= { adjustR
.y 
+ top  
, adjustR
.x 
+ left 
, adjustR
.y 
+ adjustR
.height 
- bottom 
, adjustR
.x 
+ adjustR
.width 
- right 
} ; 
 795                 if ( !EqualRect( &newRect 
, &adjustedRect 
) ) 
 796                     cEvent
.SetParameter
<Rect
>( kEventParamCurrentBounds 
, &adjustedRect 
) ; 
 797                 toplevelWindow
->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified 
 811 // mix this in from window.cpp 
 812 pascal OSStatus 
wxMacUnicodeTextEventHandler( EventHandlerCallRef handler 
, EventRef event 
, void *data 
) ; 
 814 pascal OSStatus 
wxMacTopLevelEventHandler( EventHandlerCallRef handler 
, EventRef event 
, void *data 
) 
 816     OSStatus result 
= eventNotHandledErr 
; 
 818     switch ( GetEventClass( event 
) ) 
 820         case kEventClassTextInput 
: 
 821             result 
= wxMacUnicodeTextEventHandler( handler
, event 
, data 
) ; 
 824         case kEventClassKeyboard 
: 
 825             result 
= KeyboardEventHandler( handler
, event 
, data 
) ; 
 828         case kEventClassWindow 
: 
 829             result 
= wxMacTopLevelWindowEventHandler( handler
, event 
, data 
) ; 
 832         case kEventClassMouse 
: 
 833             result 
= wxMacTopLevelMouseEventHandler( handler
, event 
, data 
) ; 
 843 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler 
) 
 845 // --------------------------------------------------------------------------- 
 846 // wxWindowMac utility functions 
 847 // --------------------------------------------------------------------------- 
 849 // Find an item given the Macintosh Window Reference 
 851 WX_DECLARE_HASH_MAP(WindowRef
, wxTopLevelWindowMac
*, wxPointerHash
, wxPointerEqual
, MacWindowMap
); 
 853 static MacWindowMap wxWinMacWindowList
; 
 855 wxTopLevelWindowMac 
*wxFindWinFromMacWindow(WindowRef inWindowRef
) 
 857     MacWindowMap::iterator node 
= wxWinMacWindowList
.find(inWindowRef
); 
 859     return (node 
== wxWinMacWindowList
.end()) ? NULL 
: node
->second
; 
 862 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac 
*win
) ; 
 863 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac 
*win
) 
 865     // adding NULL WindowRef is (first) surely a result of an error and 
 867     wxCHECK_RET( inWindowRef 
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") ); 
 869     wxWinMacWindowList
[inWindowRef
] = win
; 
 872 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac 
*win
) ; 
 873 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac 
*win
) 
 875     MacWindowMap::iterator it
; 
 876     for ( it 
= wxWinMacWindowList
.begin(); it 
!= wxWinMacWindowList
.end(); ++it 
) 
 878         if ( it
->second 
== win 
) 
 880             wxWinMacWindowList
.erase(it
); 
 886 // ---------------------------------------------------------------------------- 
 887 // wxTopLevelWindowMac creation 
 888 // ---------------------------------------------------------------------------- 
 890 wxTopLevelWindowMac 
*wxTopLevelWindowMac::s_macDeactivateWindow 
= NULL
; 
 896     bool m_wasResizable 
; 
 899 void wxTopLevelWindowMac::Init() 
 902     m_maximizeOnShow 
= false; 
 905     m_macEventHandler 
= NULL 
; 
 906     m_macFullScreenData 
= NULL 
; 
 909 class wxMacDeferredWindowDeleter 
: public wxObject
 
 912     wxMacDeferredWindowDeleter( WindowRef windowRef 
) 
 914         m_macWindow 
= windowRef 
; 
 917     virtual ~wxMacDeferredWindowDeleter() 
 919         UMADisposeWindow( (WindowRef
) m_macWindow 
) ; 
 923     WindowRef m_macWindow 
; 
 926 bool wxTopLevelWindowMac::Create(wxWindow 
*parent
, 
 928                                  const wxString
& title
, 
 932                                  const wxString
& name
) 
 937     m_windowStyle 
= style
; 
 941     m_windowId 
= id 
== -1 ? NewControlId() : id
; 
 942     wxWindow::SetLabel( title 
) ; 
 944     MacCreateRealWindow( title
, pos 
, size 
, style 
, name 
) ; 
 946     SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)); 
 948     if (GetExtraStyle() & wxFRAME_EX_METAL
) 
 949         MacSetMetalAppearance(true); 
 951     wxTopLevelWindows
.Append(this); 
 954         parent
->AddChild(this); 
 959 wxTopLevelWindowMac::~wxTopLevelWindowMac() 
 964         wxToolTip::NotifyWindowDelete(m_macWindow
) ; 
 966         wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow 
) ) ; 
 969     if ( m_macEventHandler 
) 
 971         ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
); 
 972         m_macEventHandler 
= NULL 
; 
 975     wxRemoveMacWindowAssociation( this ) ; 
 977     if ( wxModelessWindows
.Find(this) ) 
 978         wxModelessWindows
.DeleteObject(this); 
 980     FullScreenData 
*data 
= (FullScreenData 
*) m_macFullScreenData 
; 
 982     m_macFullScreenData 
= NULL 
; 
 984     // avoid dangling refs 
 985     if ( s_macDeactivateWindow 
== this ) 
 986         s_macDeactivateWindow 
= NULL
; 
 990 // ---------------------------------------------------------------------------- 
 991 // wxTopLevelWindowMac maximize/minimize 
 992 // ---------------------------------------------------------------------------- 
 994 void wxTopLevelWindowMac::Maximize(bool maximize
) 
 996     Point idealSize 
= { 0 , 0 } ; 
 999 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 
1001         HIWindowGetAvailablePositioningBounds(kCGNullDirectDisplay
,kHICoordSpace72DPIGlobal
, 
1003         idealSize
.h 
= bounds
.size
.width
; 
1004         idealSize
.v 
= bounds
.size
.height
; 
1007         GetAvailableWindowPositioningBounds(GetMainDevice(),&rect
) ; 
1008         idealSize
.h 
= rect
.right 
- rect
.left 
; 
1009         idealSize
.v 
= rect
.bottom 
- rect
.top 
; 
1012     ZoomWindowIdeal( (WindowRef
)m_macWindow 
, maximize 
? inZoomOut 
: inZoomIn 
, &idealSize 
) ; 
1015 bool wxTopLevelWindowMac::IsMaximized() const 
1017     return IsWindowInStandardState( (WindowRef
)m_macWindow 
, NULL 
, NULL 
) ; 
1020 void wxTopLevelWindowMac::Iconize(bool iconize
) 
1022     if ( IsWindowCollapsable( (WindowRef
)m_macWindow
) ) 
1023         CollapseWindow( (WindowRef
)m_macWindow 
, iconize 
) ; 
1026 bool wxTopLevelWindowMac::IsIconized() const 
1028     return IsWindowCollapsed((WindowRef
)m_macWindow 
) ; 
1031 void wxTopLevelWindowMac::Restore() 
1033     if ( IsMaximized() ) 
1035     else if ( IsIconized() ) 
1039 // ---------------------------------------------------------------------------- 
1040 // wxTopLevelWindowMac misc 
1041 // ---------------------------------------------------------------------------- 
1043 wxPoint 
wxTopLevelWindowMac::GetClientAreaOrigin() const 
1045     return wxPoint(0, 0) ; 
1048 void  wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush 
&brush 
) 
1050     wxTopLevelWindowBase::MacSetBackgroundBrush( brush 
) ; 
1052     if ( m_macBackgroundBrush
.Ok() && m_macBackgroundBrush
.GetStyle() != wxTRANSPARENT 
&& m_macBackgroundBrush
.MacGetBrushKind() == kwxMacBrushTheme 
) 
1054         SetThemeWindowBackground( (WindowRef
) m_macWindow 
, m_macBackgroundBrush
.MacGetTheme() , false ) ; 
1058 void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler() 
1060     if ( m_macEventHandler 
!= NULL 
) 
1062         verify_noerr( ::RemoveEventHandler( (EventHandlerRef
) m_macEventHandler 
) ) ; 
1065     InstallWindowEventHandler( 
1066         MAC_WXHWND(m_macWindow
), GetwxMacTopLevelEventHandlerUPP(), 
1067         GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef 
*)&m_macEventHandler 
); 
1070 void  wxTopLevelWindowMac::MacCreateRealWindow( 
1071     const wxString
& title
, 
1075     const wxString
& name 
) 
1077     OSStatus err 
= noErr 
; 
1079     m_windowStyle 
= style
; 
1087     wxRect display 
= wxGetClientDisplayRect() ; 
1089     if ( x 
== wxDefaultPosition
.x 
) 
1092     if ( y 
== wxDefaultPosition
.y 
) 
1095     int w 
= WidthDefault(size
.x
); 
1096     int h 
= HeightDefault(size
.y
); 
1098     ::SetRect(&theBoundsRect
, x
, y 
, x 
+ w
, y 
+ h
); 
1100     // translate the window attributes in the appropriate window class and attributes 
1101     WindowClass wclass 
= 0; 
1102     WindowAttributes attr 
= kWindowNoAttributes 
; 
1103     WindowGroupRef group 
= NULL 
; 
1105     if ( HasFlag( wxFRAME_TOOL_WINDOW
) ) 
1108             HasFlag( wxMINIMIZE_BOX 
) || HasFlag( wxMAXIMIZE_BOX 
) || 
1109             HasFlag( wxSYSTEM_MENU 
) || HasFlag( wxCAPTION 
) || 
1110             HasFlag(wxTINY_CAPTION_HORIZ
) ||  HasFlag(wxTINY_CAPTION_VERT
) 
1113             wclass 
= kFloatingWindowClass 
; 
1115             if ( HasFlag(wxTINY_CAPTION_VERT
) ) 
1116                 attr 
|= kWindowSideTitlebarAttribute 
; 
1120             wclass 
= kPlainWindowClass 
; 
1123     else if ( HasFlag( wxPOPUP_WINDOW 
) ) 
1126         // Until we've got a real wxPopupWindow class on wxMac make it a 
1127         // little easier for wxFrame to be used to emulate it and workaround 
1128         // the lack of wxPopupWindow. 
1129         if ( HasFlag( wxBORDER_NONE 
) ) 
1130             wclass 
= kHelpWindowClass 
;   // has no border 
1132             wclass 
= kPlainWindowClass 
;  // has a single line border, it will have to do for now 
1133         //attr |= kWindowNoShadowAttribute; // turn off the shadow  Should we?? 
1134         group 
= GetWindowGroupOfClass(    // float above other windows    
1135             kFloatingWindowClass
) ; 
1137     else if ( HasFlag( wxCAPTION 
) ) 
1139         wclass 
= kDocumentWindowClass 
; 
1140         attr 
|= kWindowInWindowMenuAttribute 
; 
1142 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 ) 
1143     else if ( HasFlag( wxFRAME_DRAWER 
) ) 
1145         wclass 
= kDrawerWindowClass
; 
1147 #endif  //10.2 and up 
1150         if ( HasFlag( wxMINIMIZE_BOX 
) || HasFlag( wxMAXIMIZE_BOX 
) || 
1151             HasFlag( wxCLOSE_BOX 
) || HasFlag( wxSYSTEM_MENU 
) ) 
1153             wclass 
= kDocumentWindowClass 
; 
1157             wclass 
= kPlainWindowClass 
; 
1161     if ( wclass 
!= kPlainWindowClass 
) 
1163         if ( HasFlag( wxMINIMIZE_BOX 
) ) 
1164             attr 
|= kWindowCollapseBoxAttribute 
; 
1166         if ( HasFlag( wxMAXIMIZE_BOX 
) ) 
1167             attr 
|= kWindowFullZoomAttribute 
; 
1169         if ( HasFlag( wxRESIZE_BORDER 
) ) 
1170             attr 
|= kWindowResizableAttribute 
; 
1172         if ( HasFlag( wxCLOSE_BOX
) ) 
1173             attr 
|= kWindowCloseBoxAttribute 
; 
1176     // turn on live resizing (OS X only) 
1177     if (UMAGetSystemVersion() >= 0x1000) 
1178         attr 
|= kWindowLiveResizeAttribute
; 
1180     if ( HasFlag(wxSTAY_ON_TOP
) ) 
1181         group 
= GetWindowGroupOfClass(kUtilityWindowClass
) ; 
1183     if ( HasFlag( wxFRAME_FLOAT_ON_PARENT 
) ) 
1184         group 
= GetWindowGroupOfClass(kFloatingWindowClass
) ; 
1186     attr 
|= kWindowCompositingAttribute
; 
1187 #if 0 // wxMAC_USE_CORE_GRAPHICS ; TODO : decide on overall handling of high dpi screens (pixel vs userscale) 
1188     attr 
|= kWindowFrameworkScaledAttribute
; 
1191     if ( HasFlag(wxFRAME_SHAPED
) ) 
1193         WindowDefSpec customWindowDefSpec
; 
1194         customWindowDefSpec
.defType 
= kWindowDefProcPtr
; 
1195         customWindowDefSpec
.u
.defProc 
=  
1197             (WindowDefUPP
) wxShapedMacWindowDef
; 
1199             NewWindowDefUPP(wxShapedMacWindowDef
); 
1201         err 
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
, 
1202                               attr
, &theBoundsRect
, 
1203                               (WindowRef
*) &m_macWindow
); 
1207         err 
= ::CreateNewWindow( wclass 
, attr 
, &theBoundsRect 
, (WindowRef
*)&m_macWindow 
) ; 
1210     if ( err 
== noErr 
&& m_macWindow 
!= NULL 
&& group 
!= NULL 
) 
1211         SetWindowGroup( (WindowRef
) m_macWindow 
, group 
) ; 
1213     wxCHECK_RET( err 
== noErr
, wxT("Mac OS error when trying to create new window") ); 
1215     // setup a separate group for each window, so that overlays can be handled easily 
1216     verify_noerr( CreateWindowGroup( kWindowGroupAttrMoveTogether 
| kWindowGroupAttrLayerTogether 
| kWindowGroupAttrHideOnCollapse
, &group 
)); 
1217     verify_noerr( SetWindowGroupParent( group
, GetWindowGroup( (WindowRef
) m_macWindow 
))); 
1218     verify_noerr( SetWindowGroup( (WindowRef
) m_macWindow 
, group 
)); 
1220     // the create commands are only for content rect, 
1221     // so we have to set the size again as structure bounds 
1222     SetWindowBounds(  (WindowRef
) m_macWindow 
, kWindowStructureRgn 
, &theBoundsRect 
) ; 
1224     wxAssociateWinWithMacWindow( (WindowRef
) m_macWindow 
, this ) ; 
1225     UMASetWTitle( (WindowRef
) m_macWindow 
, title 
, m_font
.GetEncoding() ) ; 
1226     m_peer 
= new wxMacControl(this , true /*isRootControl*/) ; 
1228     // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of 
1229     // the content view, so we have to retrieve it explicitly 
1230     HIViewFindByID( HIViewGetRoot( (WindowRef
) m_macWindow 
) , kHIViewWindowContentID 
, 
1231         m_peer
->GetControlRefAddr() ) ; 
1232     if ( !m_peer
->Ok() ) 
1234         // compatibility mode fallback 
1235         GetRootControl( (WindowRef
) m_macWindow 
, m_peer
->GetControlRefAddr() ) ; 
1238     // the root control level handler 
1239     MacInstallEventHandler( (WXWidget
) m_peer
->GetControlRef() ) ; 
1241     // Causes the inner part of the window not to be metal 
1242     // if the style is used before window creation. 
1243 #if 0 // TARGET_API_MAC_OSX 
1244     if ( m_macUsesCompositing 
&& m_macWindow 
!= NULL 
) 
1246         if ( GetExtraStyle() & wxFRAME_EX_METAL 
) 
1247             MacSetMetalAppearance( true ) ; 
1251     // the frame window event handler 
1252     InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ; 
1253     MacInstallTopLevelWindowEventHandler() ; 
1255     DoSetWindowVariant( m_windowVariant 
) ; 
1259     if ( HasFlag(wxFRAME_SHAPED
) ) 
1261         // default shape matches the window size 
1262         wxRegion 
rgn( 0, 0, w
, h 
); 
1266     wxWindowCreateEvent 
event(this); 
1267     GetEventHandler()->ProcessEvent(event
); 
1270 void wxTopLevelWindowMac::ClearBackground() 
1272     wxWindow::ClearBackground() ; 
1275 // Raise the window to the top of the Z order 
1276 void wxTopLevelWindowMac::Raise() 
1278     ::SelectWindow( (WindowRef
)m_macWindow 
) ; 
1281 // Lower the window to the bottom of the Z order 
1282 void wxTopLevelWindowMac::Lower() 
1284     ::SendBehind( (WindowRef
)m_macWindow 
, NULL 
) ; 
1287 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
) 
1289     if (s_macDeactivateWindow
) 
1291         wxLogTrace(TRACE_ACTIVATE
, 
1292                    wxT("Doing delayed deactivation of %p"), 
1293                    s_macDeactivateWindow
); 
1295         s_macDeactivateWindow
->MacActivate(timestamp
, false); 
1299 void wxTopLevelWindowMac::MacActivate( long timestamp 
, bool inIsActivating 
) 
1301     wxLogTrace(TRACE_ACTIVATE
, wxT("TopLevel=%p::MacActivate"), this); 
1303     if (s_macDeactivateWindow 
== this) 
1304         s_macDeactivateWindow 
= NULL
; 
1306     MacDelayedDeactivation(timestamp
); 
1307     MacPropagateHiliteChanged() ; 
1310 void wxTopLevelWindowMac::SetTitle(const wxString
& title
) 
1312     wxWindow::SetLabel( title 
) ; 
1313     UMASetWTitle( (WindowRef
)m_macWindow 
, title 
, m_font
.GetEncoding() ) ; 
1316 wxString 
wxTopLevelWindowMac::GetTitle() const 
1318     return wxWindow::GetLabel(); 
1321 bool wxTopLevelWindowMac::Show(bool show
) 
1323     if ( !wxTopLevelWindowBase::Show(show
) ) 
1326     bool plainTransition 
= false; 
1328 #if wxUSE_SYSTEM_OPTIONS 
1329     // code contributed by Ryan Wilcox December 18, 2003 
1330     plainTransition 
= UMAGetSystemVersion() >= 0x1000 ; 
1331     if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) 
1332         plainTransition 
= ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION 
) == 1 ) ; 
1337         if ( plainTransition 
) 
1338            ::ShowWindow( (WindowRef
)m_macWindow 
); 
1340            ::TransitionWindow( (WindowRef
)m_macWindow
, kWindowZoomTransitionEffect
, kWindowShowTransitionAction
, NULL 
); 
1342         ::SelectWindow( (WindowRef
)m_macWindow 
) ; 
1344         // because apps expect a size event to occur at this moment 
1345         wxSizeEvent 
event(GetSize() , m_windowId
); 
1346         event
.SetEventObject(this); 
1347         GetEventHandler()->ProcessEvent(event
); 
1351         if ( plainTransition 
) 
1352            ::HideWindow( (WindowRef
)m_macWindow 
); 
1354            ::TransitionWindow( (WindowRef
)m_macWindow
, kWindowZoomTransitionEffect
, kWindowHideTransitionAction
, NULL 
); 
1357     MacPropagateVisibilityChanged() ; 
1362 bool wxTopLevelWindowMac::ShowFullScreen(bool show
, long style
) 
1366         FullScreenData 
*data 
= (FullScreenData 
*)m_macFullScreenData 
; 
1368         data 
= new FullScreenData() ; 
1370         m_macFullScreenData 
= data 
; 
1371         data
->m_position 
= GetPosition() ; 
1372         data
->m_size 
= GetSize() ; 
1373         data
->m_wasResizable 
= MacGetWindowAttributes() & kWindowResizableAttribute 
; 
1375         if ( style 
& wxFULLSCREEN_NOMENUBAR 
) 
1378         wxRect client 
= wxGetClientDisplayRect() ; 
1380         int left 
, top 
, right 
, bottom 
; 
1388         MacGetContentAreaInset( left 
, top 
, right 
, bottom 
) ; 
1390         if ( style 
& wxFULLSCREEN_NOCAPTION 
) 
1396         if ( style 
& wxFULLSCREEN_NOBORDER 
) 
1403         if ( style 
& wxFULLSCREEN_NOTOOLBAR 
) 
1408         if ( style 
& wxFULLSCREEN_NOSTATUSBAR 
) 
1413         SetSize( x 
, y 
, w
, h 
) ; 
1414         if ( data
->m_wasResizable 
) 
1415             MacChangeWindowAttributes( kWindowNoAttributes 
, kWindowResizableAttribute 
) ; 
1420         FullScreenData 
*data 
= (FullScreenData 
*) m_macFullScreenData 
; 
1421         if ( data
->m_wasResizable 
) 
1422             MacChangeWindowAttributes( kWindowResizableAttribute 
,  kWindowNoAttributes 
) ; 
1423         SetPosition( data
->m_position 
) ; 
1424         SetSize( data
->m_size 
) ; 
1427         m_macFullScreenData 
= NULL 
; 
1433 bool wxTopLevelWindowMac::IsFullScreen() const 
1435     return m_macFullScreenData 
!= NULL 
; 
1439 bool wxTopLevelWindowMac::SetTransparent(wxByte alpha
) 
1441     OSStatus result 
= SetWindowAlpha((WindowRef
)m_macWindow
, float(alpha
)/255.0); 
1442     return result 
== noErr
; 
1446 bool wxTopLevelWindowMac::CanSetTransparent() 
1452 void wxTopLevelWindowMac::SetExtraStyle(long exStyle
) 
1454     if ( GetExtraStyle() == exStyle 
) 
1457     wxTopLevelWindowBase::SetExtraStyle( exStyle 
) ; 
1459 #if TARGET_API_MAC_OSX 
1460     if ( m_macWindow 
!= NULL 
) 
1462         bool metal 
= GetExtraStyle() & wxFRAME_EX_METAL 
; 
1463         if ( MacGetMetalAppearance() != metal 
) 
1464             MacSetMetalAppearance( metal 
) ; 
1469 // TODO: switch to structure bounds - 
1470 // we are still using coordinates of the content view 
1472 void wxTopLevelWindowMac::MacGetContentAreaInset( int &left 
, int &top 
, int &right 
, int &bottom 
) 
1474     Rect content
, structure 
; 
1476     GetWindowBounds( (WindowRef
) m_macWindow
, kWindowStructureRgn 
, &structure 
) ; 
1477     GetWindowBounds( (WindowRef
) m_macWindow
, kWindowContentRgn 
, &content 
) ; 
1479     left 
= content
.left 
- structure
.left 
; 
1480     top 
= content
.top  
- structure
.top 
; 
1481     right 
= structure
.right 
- content
.right 
; 
1482     bottom 
= structure
.bottom 
- content
.bottom 
; 
1485 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
) 
1487     m_cachedClippedRectValid 
= false ; 
1488     Rect bounds 
= { y 
, x 
, y 
+ height 
, x 
+ width 
} ; 
1489     verify_noerr(SetWindowBounds( (WindowRef
) m_macWindow
, kWindowStructureRgn 
, &bounds 
)) ; 
1490     wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified 
1493 void wxTopLevelWindowMac::DoGetPosition( int *x
, int *y 
) const 
1497     verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowStructureRgn 
, &bounds 
)) ; 
1505 void wxTopLevelWindowMac::DoGetSize( int *width
, int *height 
) const 
1509     verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowStructureRgn 
, &bounds 
)) ; 
1512        *width 
= bounds
.right 
- bounds
.left 
; 
1514        *height 
= bounds
.bottom 
- bounds
.top 
; 
1517 void wxTopLevelWindowMac::DoGetClientSize( int *width
, int *height 
) const 
1521     verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowContentRgn 
, &bounds 
)) ; 
1524        *width 
= bounds
.right 
- bounds
.left 
; 
1526        *height 
= bounds
.bottom 
- bounds
.top 
; 
1529 void wxTopLevelWindowMac::MacSetMetalAppearance( bool set 
) 
1531 #if TARGET_API_MAC_OSX 
1532     MacChangeWindowAttributes( set 
? kWindowMetalAttribute 
: kWindowNoAttributes 
, 
1533         set 
? kWindowNoAttributes 
: kWindowMetalAttribute 
) ; 
1537 bool wxTopLevelWindowMac::MacGetMetalAppearance() const 
1539 #if TARGET_API_MAC_OSX 
1540     return MacGetWindowAttributes() & kWindowMetalAttribute 
; 
1546 void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet 
, wxUint32 attributesToClear 
) 
1548     ChangeWindowAttributes( (WindowRef
)m_macWindow
, attributesToSet
, attributesToClear 
) ; 
1551 wxUint32 
wxTopLevelWindowMac::MacGetWindowAttributes() const 
1554     GetWindowAttributes( (WindowRef
) m_macWindow
, &attr 
) ; 
1559 void wxTopLevelWindowMac::MacPerformUpdates() 
1561 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 
1562     // for composited windows this also triggers a redraw of all 
1563     // invalid views in the window 
1564     if ( UMAGetSystemVersion() >= 0x1030 ) 
1565         HIWindowFlush((WindowRef
) m_macWindow
) ; 
1569         // the only way to trigger the redrawing on earlier systems is to call 
1572         EventRef currentEvent 
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ; 
1573         UInt32 currentEventClass 
= 0 ; 
1574         if ( currentEvent 
!= NULL 
) 
1576             currentEventClass 
= ::GetEventClass( currentEvent 
) ; 
1577             ::GetEventKind( currentEvent 
) ; 
1580         if ( currentEventClass 
!= kEventClassMenu 
) 
1582             // when tracking a menu, strange redraw errors occur if we flush now, so leave.. 
1584             ReceiveNextEvent( 0 , NULL 
, kEventDurationNoWait 
, false , &theEvent 
) ; 
1589 // Attracts the users attention to this window if the application is 
1590 // inactive (should be called when a background event occurs) 
1592 static pascal void wxMacNMResponse( NMRecPtr ptr 
) 
1595     DisposePtr( (Ptr
)ptr 
) ; 
1598 void wxTopLevelWindowMac::RequestUserAttention(int flags 
) 
1600     NMRecPtr notificationRequest 
= (NMRecPtr
) NewPtr( sizeof( NMRec
) ) ; 
1601     static wxMacNMUPP 
nmupp( wxMacNMResponse 
); 
1603     memset( notificationRequest 
, 0 , sizeof(*notificationRequest
) ) ; 
1604     notificationRequest
->qType 
= nmType 
; 
1605     notificationRequest
->nmMark 
= 1 ; 
1606     notificationRequest
->nmIcon 
= 0 ; 
1607     notificationRequest
->nmSound 
= 0 ; 
1608     notificationRequest
->nmStr 
= NULL 
; 
1609     notificationRequest
->nmResp 
= nmupp 
; 
1611     verify_noerr( NMInstall( notificationRequest 
) ) ; 
1614 // --------------------------------------------------------------------------- 
1615 // Shape implementation 
1616 // --------------------------------------------------------------------------- 
1619 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
) 
1621     wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), false, 
1622                  _T("Shaped windows must be created with the wxFRAME_SHAPED style.")); 
1624     // The empty region signifies that the shape 
1625     // should be removed from the window. 
1626     if ( region
.IsEmpty() ) 
1628         wxSize sz 
= GetClientSize(); 
1629         wxRegion 
rgn(0, 0, sz
.x
, sz
.y
); 
1630         if ( rgn
.IsEmpty() ) 
1633             return SetShape(rgn
); 
1636     // Make a copy of the region 
1637     RgnHandle  shapeRegion 
= NewRgn(); 
1638     CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion 
); 
1640     // Dispose of any shape region we may already have 
1641     RgnHandle oldRgn 
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() ); 
1645     // Save the region so we can use it later 
1646     SetWRefCon((WindowRef
)MacGetWindowRef(), (URefCon
)shapeRegion
); 
1648     // inform the window manager that the window has changed shape 
1649     ReshapeCustomWindow((WindowRef
)MacGetWindowRef()); 
1654 // --------------------------------------------------------------------------- 
1655 // Support functions for shaped windows, based on Apple's CustomWindow sample at 
1656 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm 
1657 // --------------------------------------------------------------------------- 
1659 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
) 
1661     GetWindowPortBounds(window
, inRect
); 
1662     Point pt 
= { inRect
->top 
,inRect
->left 
}; 
1663     wxMacLocalToGlobal( window
, &pt 
) ; 
1664     inRect
->bottom 
+= pt
.v 
- inRect
->top
; 
1665     inRect
->right 
+= pt
.h 
- inRect
->left
; 
1667     inRect
->left 
= pt
.h
; 
1670 static SInt32 
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
) 
1672     /*------------------------------------------------------ 
1673         Define which options your custom window supports. 
1674     --------------------------------------------------------*/ 
1675     //just enable everything for our demo 
1676     *(OptionBits
*)param 
= 
1679         //kWindowCanCollapse | 
1680         //kWindowCanGetWindowRegion | 
1681         //kWindowHasTitleBar | 
1682         //kWindowSupportsDragHilite | 
1683         kWindowCanDrawInCurrentPort 
| 
1684         //kWindowCanMeasureTitle | 
1685         kWindowWantsDisposeAtProcessDeath 
| 
1686         kWindowSupportsGetGrowImageRegion 
| 
1687         kWindowDefSupportsColorGrafPort
; 
1692 // The content region is left as a rectangle matching the window size, this is 
1693 // so the origin in the paint event, and etc. still matches what the 
1694 // programmer expects. 
1695 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
) 
1698     wxTopLevelWindowMac
* win 
= wxFindWinFromMacWindow(window
); 
1702         wxShapedMacWindowGetPos( window
, &r 
) ; 
1703         RectRgn( rgn 
, &r 
) ; 
1707 // The structure region is set to the shape given to the SetShape method. 
1708 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
) 
1710     RgnHandle cachedRegion 
= (RgnHandle
) GetWRefCon(window
); 
1716         wxShapedMacWindowGetPos(window
, &windowRect
);    // how big is the window 
1717         CopyRgn(cachedRegion
, rgn
);                      // make a copy of our cached region 
1718         OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window 
1719         //MapRgn(rgn, &mMaskSize, &windowRect);          //scale it to our actual window size 
1723 static SInt32 
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
) 
1725     GetWindowRegionPtr rgnRec 
= (GetWindowRegionPtr
)param
; 
1730     switch (rgnRec
->regionCode
) 
1732         case kWindowStructureRgn
: 
1733             wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
); 
1736         case kWindowContentRgn
: 
1737             wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
); 
1741             SetEmptyRgn(rgnRec
->winRgn
); 
1748 // Determine the region of the window which was hit 
1750 static SInt32 
wxShapedMacWindowHitTest(WindowRef window
, SInt32 param
) 
1753     static RgnHandle tempRgn 
= NULL
; 
1755     if (tempRgn 
== NULL
) 
1758     // get the point clicked 
1759     SetPt( &hitPoint
, LoWord(param
), HiWord(param
) ); 
1761      // Mac OS 8.5 or later 
1762     wxShapedMacWindowStructureRegion(window
, tempRgn
); 
1763     if (PtInRgn( hitPoint
, tempRgn 
)) //in window content region? 
1766     // no significant area was hit 
1770 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
) 
1774         case kWindowMsgHitTest
: 
1775             return wxShapedMacWindowHitTest(window
, param
); 
1777         case kWindowMsgGetFeatures
: 
1778             return wxShapedMacWindowGetFeatures(window
, param
); 
1780         // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow 
1781         case kWindowMsgGetRegion
: 
1782             return wxShapedMacWindowGetRegion(window
, param
);