+pascal OSStatus wxMacTopLevelEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+
+ switch ( GetEventClass( event ) )
+ {
+ case kEventClassTextInput :
+ result = wxMacUnicodeTextEventHandler( handler, event , data ) ;
+ break ;
+
+ case kEventClassKeyboard :
+ result = KeyboardEventHandler( handler, event , data ) ;
+ break ;
+
+ case kEventClassWindow :
+ result = wxMacTopLevelWindowEventHandler( handler, event , data ) ;
+ break ;
+
+ case kEventClassMouse :
+ result = wxMacTopLevelMouseEventHandler( handler, event , data ) ;
+ break ;
+
+ default :
+ break ;
+ }
+
+ return result ;
+}
+
+DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler )
+
+// ---------------------------------------------------------------------------
+// wxWindowMac utility functions
+// ---------------------------------------------------------------------------
+
+// Find an item given the Macintosh Window Reference
+
+WX_DECLARE_HASH_MAP(WindowRef, wxTopLevelWindowMac*, wxPointerHash, wxPointerEqual, MacWindowMap);
+
+static MacWindowMap wxWinMacWindowList;
+
+wxTopLevelWindowMac *wxFindWinFromMacWindow(WindowRef inWindowRef)
+{
+ MacWindowMap::iterator node = wxWinMacWindowList.find(inWindowRef);
+
+ return (node == wxWinMacWindowList.end()) ? NULL : node->second;
+}
+
+void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win) ;
+void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win)
+{
+ // adding NULL WindowRef is (first) surely a result of an error and
+ // nothing else :-)
+ wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
+
+ wxWinMacWindowList[inWindowRef] = win;
+}
+
+void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win) ;
+void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
+{
+ MacWindowMap::iterator it;
+ for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
+ {
+ if ( it->second == win )
+ {
+ wxWinMacWindowList.erase(it);
+ break;
+ }
+ }
+}
+
+// ----------------------------------------------------------------------------
+// wxTopLevelWindowMac creation
+// ----------------------------------------------------------------------------
+
+wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
+
+typedef struct
+{
+ wxPoint m_position ;
+ wxSize m_size ;
+ bool m_wasResizable ;
+} FullScreenData ;
+
+void wxTopLevelWindowMac::Init()
+{
+ m_iconized =
+ m_maximizeOnShow = false;
+ m_macWindow = NULL ;
+
+ m_macEventHandler = NULL ;
+ m_macFullScreenData = NULL ;
+}
+
+wxMacDeferredWindowDeleter::wxMacDeferredWindowDeleter( WindowRef windowRef )
+{
+ m_macWindow = windowRef ;
+}
+
+wxMacDeferredWindowDeleter::~wxMacDeferredWindowDeleter()
+{
+ DisposeWindow( (WindowRef) m_macWindow ) ;
+}
+
+bool wxTopLevelWindowMac::Create(wxWindow *parent,
+ wxWindowID id,
+ const wxString& title,
+ const wxPoint& pos,
+ const wxSize& size,
+ long style,
+ const wxString& name)
+{
+ // init our fields
+ Init();
+
+ m_windowStyle = style;
+
+ SetName( name );
+
+ m_windowId = id == -1 ? NewControlId() : id;
+ wxWindow::SetLabel( title ) ;
+
+ DoMacCreateRealWindow( parent, title, pos , size , style , name ) ;
+
+ SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ));
+
+ if (GetExtraStyle() & wxFRAME_EX_METAL)
+ MacSetMetalAppearance(true);
+
+ wxTopLevelWindows.Append(this);
+
+ if ( parent )
+ parent->AddChild(this);
+
+ return true;
+}
+
+wxTopLevelWindowMac::~wxTopLevelWindowMac()
+{
+ if ( m_macWindow )
+ {
+#if wxUSE_TOOLTIPS
+ wxToolTip::NotifyWindowDelete(m_macWindow) ;
+#endif
+ wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
+ }
+
+ if ( m_macEventHandler )
+ {
+ ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
+ m_macEventHandler = NULL ;
+ }
+
+ wxRemoveMacWindowAssociation( this ) ;
+
+ if ( wxModelessWindows.Find(this) )
+ wxModelessWindows.DeleteObject(this);
+
+ FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
+ delete data ;
+ m_macFullScreenData = NULL ;
+
+ // avoid dangling refs
+ if ( s_macDeactivateWindow == this )
+ s_macDeactivateWindow = NULL;
+}
+
+
+// ----------------------------------------------------------------------------
+// wxTopLevelWindowMac maximize/minimize
+// ----------------------------------------------------------------------------
+
+void wxTopLevelWindowMac::Maximize(bool maximize)
+{
+ Point idealSize = { 0 , 0 } ;
+ if ( maximize )
+ {
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
+ HIRect bounds ;
+ HIWindowGetAvailablePositioningBounds(kCGNullDirectDisplay,kHICoordSpace72DPIGlobal,
+ &bounds);
+ idealSize.h = bounds.size.width;
+ idealSize.v = bounds.size.height;
+#else
+ Rect rect ;
+ GetAvailableWindowPositioningBounds(GetMainDevice(),&rect) ;
+ idealSize.h = rect.right - rect.left ;
+ idealSize.v = rect.bottom - rect.top ;
+#endif
+ }
+ ZoomWindowIdeal( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , &idealSize ) ;
+}
+
+bool wxTopLevelWindowMac::IsMaximized() const
+{
+ return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
+}
+
+void wxTopLevelWindowMac::Iconize(bool iconize)
+{
+ if ( IsWindowCollapsable( (WindowRef)m_macWindow) )
+ CollapseWindow( (WindowRef)m_macWindow , iconize ) ;
+}
+
+bool wxTopLevelWindowMac::IsIconized() const
+{
+ return IsWindowCollapsed((WindowRef)m_macWindow ) ;
+}
+
+void wxTopLevelWindowMac::Restore()
+{
+ if ( IsMaximized() )
+ Maximize(false);
+ else if ( IsIconized() )
+ Iconize(false);
+}
+
+// ----------------------------------------------------------------------------
+// wxTopLevelWindowMac misc
+// ----------------------------------------------------------------------------
+
+wxPoint wxTopLevelWindowMac::GetClientAreaOrigin() const
+{
+ return wxPoint(0, 0) ;
+}
+
+bool wxTopLevelWindowMac::SetBackgroundColour(const wxColour& col )
+{
+ if ( !wxTopLevelWindowBase::SetBackgroundColour(col) && m_hasBgCol )
+ return false ;
+
+ if ( col == wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ) || col == wxColour(wxMacCreateCGColorFromHITheme(kThemeBrushDocumentWindowBackground)) )
+ {
+ SetThemeWindowBackground( (WindowRef) m_macWindow, kThemeBrushDocumentWindowBackground, false ) ;
+ SetBackgroundStyle(wxBG_STYLE_CUSTOM);
+ }
+ else if ( col == wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ) || col == wxColour(wxMacCreateCGColorFromHITheme(kThemeBrushDialogBackgroundActive)) )
+ {
+ SetThemeWindowBackground( (WindowRef) m_macWindow, kThemeBrushDialogBackgroundActive, false ) ;
+ SetBackgroundStyle(wxBG_STYLE_CUSTOM);
+ }
+ return true;
+}
+
+void wxTopLevelWindowMacInstallTopLevelWindowEventHandler(WindowRef window, EventHandlerRef* handler, void *ref)
+{
+ InstallWindowEventHandler(window, GetwxMacTopLevelEventHandlerUPP(),
+ GetEventTypeCount(eventList), eventList, ref, handler );
+}
+
+void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
+{
+ if ( m_macEventHandler != NULL )
+ {
+ verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
+ }
+ wxTopLevelWindowMacInstallTopLevelWindowEventHandler(MAC_WXHWND(m_macWindow),(EventHandlerRef *)&m_macEventHandler,this);
+}
+
+void wxTopLevelWindowMac::MacCreateRealWindow(
+ const wxString& title,
+ const wxPoint& pos,
+ const wxSize& size,
+ long style,
+ const wxString& name )
+{
+ DoMacCreateRealWindow( NULL, title, pos, size, style, name );
+}
+
+void wxTopLevelWindowMac::DoMacCreateRealWindow(
+ wxWindow* parent,
+ const wxString& title,
+ const wxPoint& pos,
+ const wxSize& size,
+ long style,
+ const wxString& name )
+{
+ OSStatus err = noErr ;
+ SetName(name);
+ m_windowStyle = style;
+ m_isShown = false;
+
+ // create frame.
+ int x = (int)pos.x;
+ int y = (int)pos.y;
+
+ Rect theBoundsRect;
+ wxRect display = wxGetClientDisplayRect() ;
+
+ if ( x == wxDefaultPosition.x )
+ x = display.x ;
+
+ if ( y == wxDefaultPosition.y )
+ y = display.y ;
+
+ int w = WidthDefault(size.x);
+ int h = HeightDefault(size.y);
+
+ ::SetRect(&theBoundsRect, x, y , x + w, y + h);
+
+ // translate the window attributes in the appropriate window class and attributes
+ WindowClass wclass = 0;
+ WindowAttributes attr = kWindowNoAttributes ;
+ WindowGroupRef group = NULL ;
+ bool activationScopeSet = false;
+ WindowActivationScope activationScope = kWindowActivationScopeNone;
+
+ if ( HasFlag( wxFRAME_TOOL_WINDOW) )
+ {
+ if (
+ HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
+ HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
+ HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
+ )
+ {
+ if ( HasFlag( wxSTAY_ON_TOP ) )
+ wclass = kUtilityWindowClass;
+ else
+ wclass = kFloatingWindowClass ;
+
+ if ( HasFlag(wxTINY_CAPTION_VERT) )
+ attr |= kWindowSideTitlebarAttribute ;
+ }
+ else
+ {
+ wclass = kPlainWindowClass ;
+ activationScopeSet = true;
+ activationScope = kWindowActivationScopeNone;
+ }
+ }
+ else if ( HasFlag( wxPOPUP_WINDOW ) )
+ {
+ // TEMPORARY HACK!
+ // Until we've got a real wxPopupWindow class on wxMac make it a
+ // little easier for wxFrame to be used to emulate it and workaround
+ // the lack of wxPopupWindow.
+ if ( HasFlag( wxBORDER_NONE ) )
+ wclass = kHelpWindowClass ; // has no border
+ else
+ wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
+ //attr |= kWindowNoShadowAttribute; // turn off the shadow Should we??
+ group = GetWindowGroupOfClass( // float above other windows
+ kFloatingWindowClass) ;
+ }
+ else if ( HasFlag( wxCAPTION ) )
+ {
+ wclass = kDocumentWindowClass ;
+ attr |= kWindowInWindowMenuAttribute ;
+ }
+ else if ( HasFlag( wxFRAME_DRAWER ) )
+ {
+ wclass = kDrawerWindowClass;
+ }
+ else
+ {
+ if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
+ HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
+ {
+ wclass = kDocumentWindowClass ;
+ }
+ else if ( HasFlag( wxNO_BORDER ) )
+ {
+ wclass = kSimpleWindowClass ;
+ }
+ else
+ {
+ wclass = kPlainWindowClass ;
+ }
+ }
+
+ if ( wclass != kPlainWindowClass )
+ {
+ if ( HasFlag( wxMINIMIZE_BOX ) )
+ attr |= kWindowCollapseBoxAttribute ;
+
+ if ( HasFlag( wxMAXIMIZE_BOX ) )
+ attr |= kWindowFullZoomAttribute ;
+
+ if ( HasFlag( wxRESIZE_BORDER ) )
+ attr |= kWindowResizableAttribute ;
+
+ if ( HasFlag( wxCLOSE_BOX) )
+ attr |= kWindowCloseBoxAttribute ;
+ }
+ attr |= kWindowLiveResizeAttribute;
+
+ if ( HasFlag(wxSTAY_ON_TOP) )
+ group = GetWindowGroupOfClass(kUtilityWindowClass) ;
+
+ if ( HasFlag( wxFRAME_FLOAT_ON_PARENT ) )
+ group = GetWindowGroupOfClass(kFloatingWindowClass) ;
+
+ if ( group == NULL && parent != NULL )
+ {
+ WindowRef parenttlw = (WindowRef) parent->MacGetTopLevelWindowRef();
+ if( parenttlw )
+ group = GetWindowGroupParent( GetWindowGroup( parenttlw ) );
+ }
+
+ attr |= kWindowCompositingAttribute;
+#if 0 // wxMAC_USE_CORE_GRAPHICS ; TODO : decide on overall handling of high dpi screens (pixel vs userscale)
+ attr |= kWindowFrameworkScaledAttribute;
+#endif
+
+ if ( HasFlag(wxFRAME_SHAPED) )
+ {
+ WindowDefSpec customWindowDefSpec;
+ customWindowDefSpec.defType = kWindowDefProcPtr;
+ customWindowDefSpec.u.defProc =
+#ifdef __LP64__
+ (WindowDefUPP) wxShapedMacWindowDef;
+#else
+ NewWindowDefUPP(wxShapedMacWindowDef);
+#endif
+ err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
+ attr, &theBoundsRect,
+ (WindowRef*) &m_macWindow);
+ }
+ else
+ {
+ err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
+ }
+
+ if ( err == noErr && m_macWindow != NULL && group != NULL )
+ SetWindowGroup( (WindowRef) m_macWindow , group ) ;
+
+ wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
+
+ // setup a separate group for each window, so that overlays can be handled easily
+
+ WindowGroupRef overlaygroup = NULL;
+ verify_noerr( CreateWindowGroup( kWindowGroupAttrMoveTogether | kWindowGroupAttrLayerTogether | kWindowGroupAttrHideOnCollapse, &overlaygroup ));
+ verify_noerr( SetWindowGroupParent( overlaygroup, GetWindowGroup( (WindowRef) m_macWindow )));
+ verify_noerr( SetWindowGroup( (WindowRef) m_macWindow , overlaygroup ));
+
+ if ( activationScopeSet )
+ {
+ verify_noerr( SetWindowActivationScope( (WindowRef) m_macWindow , activationScope ));
+ }
+
+ // the create commands are only for content rect,
+ // so we have to set the size again as structure bounds
+ SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
+
+ wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
+ SetWindowTitleWithCFString( (WindowRef) m_macWindow , wxCFStringRef( title , GetFont().GetEncoding() ) );
+ m_peer = new wxMacControl(this , true /*isRootControl*/) ;
+
+ // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
+ // the content view, so we have to retrieve it explicitly
+ HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
+ m_peer->GetControlRefAddr() ) ;
+ if ( !m_peer->Ok() )
+ {
+ // compatibility mode fallback
+ GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
+ }
+
+ // the root control level handler
+ MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
+
+ // Causes the inner part of the window not to be metal
+ // if the style is used before window creation.
+#if 0 // TARGET_API_MAC_OSX
+ if ( m_macUsesCompositing && m_macWindow != NULL )
+ {
+ if ( GetExtraStyle() & wxFRAME_EX_METAL )
+ MacSetMetalAppearance( true ) ;
+ }
+#endif
+
+ if ( m_macWindow != NULL )
+ {
+ MacSetUnifiedAppearance( true ) ;
+ }
+
+ HIViewRef growBoxRef = 0 ;
+ err = HIViewFindByID( HIViewGetRoot( (WindowRef)m_macWindow ), kHIViewWindowGrowBoxID, &growBoxRef );
+ if ( err == noErr && growBoxRef != 0 )
+ HIGrowBoxViewSetTransparent( growBoxRef, true ) ;
+
+ // the frame window event handler
+ InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
+ MacInstallTopLevelWindowEventHandler() ;
+
+ DoSetWindowVariant( m_windowVariant ) ;
+
+ m_macFocus = NULL ;
+
+ if ( HasFlag(wxFRAME_SHAPED) )
+ {
+ // default shape matches the window size
+ wxRegion rgn( 0, 0, w, h );
+ SetShape( rgn );
+ }
+
+ wxWindowCreateEvent event(this);
+ HandleWindowEvent(event);
+}
+
+void wxTopLevelWindowMac::ClearBackground()
+{
+ wxWindow::ClearBackground() ;
+}
+
+// Raise the window to the top of the Z order
+void wxTopLevelWindowMac::Raise()
+{
+ ::SelectWindow( (WindowRef)m_macWindow ) ;
+}
+
+// Lower the window to the bottom of the Z order
+void wxTopLevelWindowMac::Lower()
+{
+ ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
+}
+
+void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
+{
+ if (s_macDeactivateWindow)
+ {
+ wxLogTrace(TRACE_ACTIVATE,
+ wxT("Doing delayed deactivation of %p"),
+ s_macDeactivateWindow);
+
+ s_macDeactivateWindow->MacActivate(timestamp, false);
+ }
+}
+
+void wxTopLevelWindowMac::MacActivate( long timestamp , bool WXUNUSED(inIsActivating) )
+{
+ wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
+
+ if (s_macDeactivateWindow == this)
+ s_macDeactivateWindow = NULL;
+
+ MacDelayedDeactivation(timestamp);
+}
+
+void wxTopLevelWindowMac::SetTitle(const wxString& title)
+{
+ wxWindow::SetLabel( title ) ;
+ SetWindowTitleWithCFString( (WindowRef) m_macWindow , wxCFStringRef( title , GetFont().GetEncoding() ) ) ;
+}
+
+wxString wxTopLevelWindowMac::GetTitle() const
+{
+ return wxWindow::GetLabel();
+}