X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b5b208a1796ecf1b9ab267d200705b4f3b4d10e3..54889dae199cb509b525b4c911185d16bef3ea2c:/src/osx/window_osx.cpp diff --git a/src/osx/window_osx.cpp b/src/osx/window_osx.cpp index f69b1b717c..bba95e6e8a 100644 --- a/src/osx/window_osx.cpp +++ b/src/osx/window_osx.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: src/osx/carbon/window.cpp +// Name: src/osx/window_osx.cpp // Purpose: wxWindowMac // Author: Stefan Csomor // Modified by: @@ -58,15 +58,15 @@ #endif #if wxUSE_DRAG_AND_DROP -#include "wx/dnd.h" + #include "wx/dnd.h" #endif #include "wx/graphics.h" #if wxOSX_USE_CARBON -#include "wx/osx/uma.h" + #include "wx/osx/uma.h" #else -#include "wx/osx/private.h" + #include "wx/osx/private.h" #endif #define MAC_SCROLLBAR_SIZE 15 @@ -76,8 +76,6 @@ #ifdef __WXUNIVERSAL__ IMPLEMENT_ABSTRACT_CLASS(wxWindowMac, wxWindowBase) -#else - IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase) #endif BEGIN_EVENT_TABLE(wxWindowMac, wxWindowBase) @@ -89,6 +87,20 @@ END_EVENT_TABLE() #define wxMAC_DEBUG_REDRAW 0 #endif +wxWidgetImplType* kOSXNoWidgetImpl = (wxWidgetImplType*) -1L; + +#if wxUSE_HOTKEY && wxOSX_USE_COCOA_OR_CARBON + +typedef struct { + EventHotKeyRef ref; + int keyId; + wxWindow* window; +} wxHotKeyRec; + +wxVector s_hotkeys; + +#endif + // =========================================================================== // implementation // =========================================================================== @@ -204,7 +216,6 @@ void wxWindowMac::Init() m_vScrollBarAlwaysShown = false; m_growBox = NULL ; - m_macIsUserPane = true; m_clipChildren = false ; m_cachedClippedRectValid = false ; m_isNativeWindowWrapper = false; @@ -213,6 +224,21 @@ void wxWindowMac::Init() wxWindowMac::~wxWindowMac() { SendDestroyEvent(); + +#if wxUSE_HOTKEY && wxOSX_USE_COCOA_OR_CARBON + for ( int i = s_hotkeys.size()-1; i>=0; -- i ) + { + if ( s_hotkeys[i].window == this ) + { + EventHotKeyRef ref = s_hotkeys[i].ref; + s_hotkeys.erase(s_hotkeys.begin() + i); + if ( UnregisterEventHotKey(ref) != noErr ) + { + wxLogLastError(wxT("UnregisterHotKey")); + } + } + } +#endif MacInvalidateBorders() ; @@ -260,16 +286,100 @@ wxWindowMac::~wxWindowMac() wxDELETE(m_dropTarget); #endif - delete m_peer ; + delete GetPeer() ; } WXWidget wxWindowMac::GetHandle() const { - if ( m_peer ) - return (WXWidget) m_peer->GetWXWidget() ; + if ( GetPeer() ) + return (WXWidget) GetPeer()->GetWXWidget() ; return NULL; } +wxOSXWidgetImpl* wxWindowMac::GetPeer() const +{ + return m_peer == kOSXNoWidgetImpl ? NULL : m_peer ; +} + +bool wxWindowMac::ShouldCreatePeer() const +{ + return m_peer != kOSXNoWidgetImpl; +} + +void wxWindowMac::DontCreatePeer() +{ + m_peer = kOSXNoWidgetImpl; +} + +void wxWindowMac::SetWrappingPeer(wxOSXWidgetImpl* wrapper) +{ + wxOSXWidgetImpl* inner = GetPeer(); + wxASSERT_MSG( inner != NULL && inner->IsOk(), "missing or incomplete inner peer" ); + wxASSERT_MSG( wrapper != NULL && wrapper->IsOk(), "missing or incomplete wrapper" ); + + if ( !(inner != NULL && inner->IsOk() && wrapper != NULL && wrapper->IsOk()) ) + return; + + inner->RemoveFromParent(); + wrapper->InstallEventHandler(); + wrapper->Embed(inner); + m_peer = wrapper; +} + +void wxWindowMac::SetPeer(wxOSXWidgetImpl* peer) +{ + if ( GetPeer() ) + { + if ( !GetPeer()->IsRootControl() ) + GetPeer()->RemoveFromParent(); + wxDELETE(m_peer); + } + + m_peer = peer; + + if ( GetPeer() && !GetPeer()->IsRootControl()) + { + wxASSERT_MSG( GetPeer()->IsOk() , wxT("The native control must exist already") ) ; + + if (!GetParent()->GetChildren().Find((wxWindow*)this)) + GetParent()->AddChild( this ); + + GetPeer()->InstallEventHandler(); + GetPeer()->Embed(GetParent()->GetPeer()); + + GetParent()->MacChildAdded() ; + + // adjust font, controlsize etc + GetPeer()->SetControlSize( m_windowVariant ); + InheritAttributes(); + // in case nothing has been set, use the variant default fonts + if ( !m_hasFont ) + DoSetWindowVariant( m_windowVariant ); + + GetPeer()->SetLabel( wxStripMenuCodes(m_label, wxStrip_Mnemonics), GetFont().GetEncoding() ) ; + + // for controls we want to use best size for wxDefaultSize params ) + if ( !GetPeer()->IsUserPane() ) + SetInitialSize(GetMinSize()); + + SetCursor( *wxSTANDARD_CURSOR ) ; + } +} + +#if WXWIN_COMPATIBILITY_2_8 + +bool wxWindowMac::MacIsUserPane() +{ + return GetPeer() == NULL || GetPeer()->IsUserPane(); +} + +#endif + +bool wxWindowMac::MacIsUserPane() const +{ + return GetPeer() == NULL || GetPeer()->IsUserPane(); +} + // --------------------------------------------------------------------------- // Utility Routines to move between different coordinate systems // --------------------------------------------------------------------------- @@ -277,7 +387,7 @@ WXWidget wxWindowMac::GetHandle() const /* * Right now we have the following setup : * a border that is not part of the native control is always outside the - * control's border (otherwise we loose all native intelligence, future ways + * control's border (otherwise we lose all native intelligence, future ways * may be to have a second embedding control responsible for drawing borders * and backgrounds eventually) * so all this border calculations have to be taken into account when calling @@ -306,9 +416,9 @@ bool wxWindowMac::Create(wxWindowMac *parent, m_windowVariant = parent->GetWindowVariant() ; - if ( m_macIsUserPane ) + if ( m_peer != kOSXNoWidgetImpl ) { - m_peer = wxWidgetImpl::CreateUserPane( this, parent, id, pos, size , style, GetExtraStyle() ); + SetPeer(wxWidgetImpl::CreateUserPane( this, parent, id, pos, size , style, GetExtraStyle() )); MacPostControlCreate(pos, size) ; } @@ -343,74 +453,46 @@ void wxWindowMac::MacChildAdded() #endif } -void wxWindowMac::MacPostControlCreate(const wxPoint& WXUNUSED(pos), const wxSize& size) +void wxWindowMac::MacPostControlCreate(const wxPoint& WXUNUSED(pos), + const wxSize& WXUNUSED(size)) { - wxASSERT_MSG( m_peer != NULL && m_peer->IsOk() , wxT("No valid mac control") ) ; + // todo remove if refactoring works correctly +#if 0 + wxASSERT_MSG( GetPeer() != NULL && GetPeer()->IsOk() , wxT("No valid mac control") ) ; - GetParent()->AddChild( this ); + if (!GetParent()->GetChildren().Find((wxWindow*)this)) + GetParent()->AddChild( this ); - m_peer->InstallEventHandler(); - m_peer->Embed(GetParent()->GetPeer()); + GetPeer()->InstallEventHandler(); + GetPeer()->Embed(GetParent()->GetPeer()); GetParent()->MacChildAdded() ; // adjust font, controlsize etc DoSetWindowVariant( m_windowVariant ) ; - m_peer->SetLabel( wxStripMenuCodes(m_label, wxStrip_Mnemonics), GetFont().GetEncoding() ) ; + GetPeer()->SetLabel( wxStripMenuCodes(m_label, wxStrip_Mnemonics), GetFont().GetEncoding() ) ; // for controls we want to use best size for wxDefaultSize params ) - if ( !m_macIsUserPane ) + if ( !GetPeer()->IsUserPane() ) SetInitialSize(size); SetCursor( *wxSTANDARD_CURSOR ) ; +#endif } void wxWindowMac::DoSetWindowVariant( wxWindowVariant variant ) { // Don't assert, in case we set the window variant before // the window is created - // wxASSERT( m_peer->Ok() ) ; + // wxASSERT( GetPeer()->IsOk() ) ; m_windowVariant = variant ; - if (m_peer == NULL || !m_peer->IsOk()) + if (GetPeer() == NULL || !GetPeer()->IsOk()) return; - m_peer->SetControlSize( variant ); -#if wxOSX_USE_CARBON - ControlSize size ; - - // we will get that from the settings later - // and make this NORMAL later, but first - // we have a few calculations that we must fix - - switch ( variant ) - { - case wxWINDOW_VARIANT_NORMAL : - size = kControlSizeNormal; - break ; - - case wxWINDOW_VARIANT_SMALL : - size = kControlSizeSmall; - break ; - - case wxWINDOW_VARIANT_MINI : - // not always defined in the headers - size = 3 ; - break ; - - case wxWINDOW_VARIANT_LARGE : - size = kControlSizeLarge; - break ; - - default: - wxFAIL_MSG(wxT("unexpected window variant")); - break ; - } - m_peer->SetData(kControlEntireControl, kControlSizeTag, &size ) ; -#endif - + GetPeer()->SetControlSize( variant ); switch ( variant ) { @@ -442,8 +524,8 @@ void wxWindowMac::DoSetWindowVariant( wxWindowVariant variant ) void wxWindowMac::MacUpdateControlFont() { - if ( m_peer ) - m_peer->SetFont( GetFont() , GetForegroundColour() , GetWindowStyle() ) ; + if ( GetPeer() ) + GetPeer()->SetFont( GetFont() , GetForegroundColour() , GetWindowStyle() ) ; // do not trigger refreshes upon invisible and possible partly created objects if ( IsShownOnScreen() ) @@ -474,8 +556,8 @@ bool wxWindowMac::SetBackgroundStyle(wxBackgroundStyle style) if ( !wxWindowBase::SetBackgroundStyle(style) ) return false; - if ( m_peer ) - m_peer->SetBackgroundStyle(style); + if ( GetPeer() ) + GetPeer()->SetBackgroundStyle(style); return true; } @@ -483,7 +565,7 @@ bool wxWindowMac::SetBackgroundColour(const wxColour& col ) { if (m_growBox) { - if ( m_backgroundColour.Ok() ) + if ( m_backgroundColour.IsOk() ) m_growBox->SetBackgroundColour(m_backgroundColour); else m_growBox->SetBackgroundColour(*wxWHITE); @@ -492,8 +574,8 @@ bool wxWindowMac::SetBackgroundColour(const wxColour& col ) if ( !wxWindowBase::SetBackgroundColour(col) && m_hasBgCol ) return false ; - if ( m_peer ) - m_peer->SetBackgroundColour( col ) ; + if ( GetPeer() ) + GetPeer()->SetBackgroundColour( col ) ; return true ; } @@ -521,13 +603,34 @@ void wxWindowMac::SetFocus() if ( former == this ) return ; - m_peer->SetFocus() ; + GetPeer()->SetFocus() ; +} + +void wxWindowMac::OSXSimulateFocusEvents() +{ + wxWindow* former = FindFocus() ; + if ( former != NULL && former != this ) + { + { + wxFocusEvent event( wxEVT_KILL_FOCUS, former->GetId()); + event.SetEventObject(former); + event.SetWindow(this); + former->HandleWindowEvent(event) ; + } + + { + wxFocusEvent event(wxEVT_SET_FOCUS, former->GetId()); + event.SetEventObject(former); + event.SetWindow(this); + former->HandleWindowEvent(event); + } + } } void wxWindowMac::DoCaptureMouse() { wxApp::s_captureWindow = (wxWindow*) this ; - m_peer->CaptureMouse() ; + GetPeer()->CaptureMouse() ; } wxWindow * wxWindowBase::GetCapture() @@ -539,7 +642,7 @@ void wxWindowMac::DoReleaseMouse() { wxApp::s_captureWindow = NULL ; - m_peer->ReleaseMouse() ; + GetPeer()->ReleaseMouse() ; } #if wxUSE_DRAG_AND_DROP @@ -549,10 +652,8 @@ void wxWindowMac::SetDropTarget(wxDropTarget *pDropTarget) delete m_dropTarget; m_dropTarget = pDropTarget; - if ( m_dropTarget != NULL ) - { - // TODO: - } + + GetPeer()->SetDropTarget(m_dropTarget) ; } #endif @@ -600,7 +701,7 @@ bool wxWindowMac::MacGetBoundsForControl( void wxWindowMac::DoGetSize(int *x, int *y) const { int width, height; - m_peer->GetSize( width, height ); + GetPeer()->GetSize( width, height ); if (x) *x = width + MacGetLeftBorderSize() + MacGetRightBorderSize() ; @@ -613,7 +714,7 @@ void wxWindowMac::DoGetPosition(int *x, int *y) const { int x1, y1; - m_peer->GetPosition( x1, y1 ) ; + GetPeer()->GetPosition( x1, y1 ) ; // get the wx window position from the native one x1 -= MacGetLeftBorderSize() ; @@ -698,7 +799,7 @@ void wxWindowMac::MacWindowToRootWindow( int *x , int *y ) const { pt.x -= MacGetLeftBorderSize() ; pt.y -= MacGetTopBorderSize() ; - wxWidgetImpl::Convert( &pt , m_peer , top->m_peer ) ; + wxWidgetImpl::Convert( &pt , GetPeer() , top->GetPeer() ) ; } } @@ -722,7 +823,7 @@ void wxWindowMac::MacRootWindowToWindow( int *x , int *y ) const wxNonOwnedWindow* top = MacGetTopLevelWindow(); if (top) { - wxWidgetImpl::Convert( &pt , top->m_peer , m_peer ) ; + wxWidgetImpl::Convert( &pt , top->GetPeer() , GetPeer() ) ; pt.x += MacGetLeftBorderSize() ; pt.y += MacGetTopBorderSize() ; } @@ -742,8 +843,8 @@ wxSize wxWindowMac::DoGetSizeFromClientSize( const wxSize & size ) const int left, top; int outerwidth, outerheight; - m_peer->GetContentArea( left, top, innerwidth, innerheight ); - m_peer->GetSize( outerwidth, outerheight ); + GetPeer()->GetContentArea( left, top, innerwidth, innerheight ); + GetPeer()->GetSize( outerwidth, outerheight ); sizeTotal.x += outerwidth-innerwidth; sizeTotal.y += outerheight-innerheight; @@ -761,7 +862,7 @@ void wxWindowMac::DoGetClientSize( int *x, int *y ) const int left, top; - m_peer->GetContentArea( left, top, ww, hh ); + GetPeer()->GetContentArea( left, top, ww, hh ); #if wxUSE_SCROLLBAR if (m_hScrollBar && m_hScrollBar->IsShown() ) hh -= m_hScrollBar->GetSize().y ; @@ -771,9 +872,22 @@ void wxWindowMac::DoGetClientSize( int *x, int *y ) const #endif if (x) - *x = ww; + { + // we shouldn't return invalid width + if ( ww < 0 ) + ww = 0; + + *x = ww; + } + if (y) - *y = hh; + { + // we shouldn't return invalid height + if ( hh < 0 ) + hh = 0; + + *y = hh; + } } bool wxWindowMac::SetCursor(const wxCursor& cursor) @@ -792,7 +906,7 @@ bool wxWindowMac::SetCursor(const wxCursor& cursor) return false ; } - wxASSERT_MSG( m_cursor.Ok(), + wxASSERT_MSG( m_cursor.IsOk(), wxT("cursor must be valid after call to the base version")); if ( GetPeer() != NULL ) @@ -839,15 +953,15 @@ void wxWindowMac::DoSetToolTip(wxToolTip *tooltip) if ( m_tooltip ) m_tooltip->SetWindow(this); - if (m_peer) - m_peer->SetToolTip(tooltip); + if (GetPeer()) + GetPeer()->SetToolTip(tooltip); } #endif void wxWindowMac::MacInvalidateBorders() { - if ( m_peer == NULL ) + if ( GetPeer() == NULL ) return ; bool vis = IsShownOnScreen() ; @@ -856,7 +970,7 @@ void wxWindowMac::MacInvalidateBorders() int outerBorder = MacGetLeftBorderSize() ; - if ( m_peer->NeedsFocusRect() ) + if ( GetPeer()->NeedsFocusRect() ) outerBorder += 4 ; if ( outerBorder == 0 ) @@ -866,8 +980,8 @@ void wxWindowMac::MacInvalidateBorders() int tx,ty,tw,th; - m_peer->GetSize( tw, th ); - m_peer->GetPosition( tx, ty ); + GetPeer()->GetSize( tw, th ); + GetPeer()->GetPosition( tx, ty ); wxRect leftupdate( tx-outerBorder,ty,outerBorder,th ); wxRect rightupdate( tx+tw, ty, outerBorder, th ); @@ -875,10 +989,10 @@ void wxWindowMac::MacInvalidateBorders() wxRect bottomupdate( tx-outerBorder, ty + th, tw + 2 * outerBorder, outerBorder ); if (GetParent()) { - GetParent()->m_peer->SetNeedsDisplay(&leftupdate); - GetParent()->m_peer->SetNeedsDisplay(&rightupdate); - GetParent()->m_peer->SetNeedsDisplay(&topupdate); - GetParent()->m_peer->SetNeedsDisplay(&bottomupdate); + GetParent()->GetPeer()->SetNeedsDisplay(&leftupdate); + GetParent()->GetPeer()->SetNeedsDisplay(&rightupdate); + GetParent()->GetPeer()->SetNeedsDisplay(&topupdate); + GetParent()->GetPeer()->SetNeedsDisplay(&bottomupdate); } } @@ -941,7 +1055,7 @@ void wxWindowMac::DoMoveWindow(int x, int y, int width, int height) m_cachedClippedRectValid = false ; - m_peer->Move( bounds.x, bounds.y, bounds.width, bounds.height); + GetPeer()->Move( bounds.x, bounds.y, bounds.width, bounds.height); wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified @@ -959,17 +1073,14 @@ void wxWindowMac::DoMoveWindow(int x, int y, int width, int height) if ( doResize ) { MacRepositionScrollBars() ; - wxSize size(actualWidth, actualHeight); - wxSizeEvent event(size, m_windowId); - event.SetEventObject(this); - HandleWindowEvent(event); + SendSizeEvent(); } } } wxSize wxWindowMac::DoGetBestSize() const { - if ( m_macIsUserPane || IsTopLevel() ) + if ( GetPeer() == NULL || GetPeer()->IsUserPane() || IsTopLevel() ) { return wxWindowBase::DoGetBestSize() ; } @@ -977,7 +1088,7 @@ wxSize wxWindowMac::DoGetBestSize() const { wxRect r ; - m_peer->GetBestRect(&r); + GetPeer()->GetBestRect(&r); if ( r.GetWidth() == 0 && r.GetHeight() == 0 ) { @@ -1016,6 +1127,12 @@ wxSize wxWindowMac::DoGetBestSize() const } } +void wxWindowMac::SendSizeEvent(int flags) +{ + MacOnInternalSize(); + wxWindowBase::SendSizeEvent(flags); +} + // set the size of the window: if the dimensions are positive, just use them, // but if any of them is equal to -1, it means that we must find the value for // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in @@ -1042,9 +1159,7 @@ void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags) if (sizeFlags & wxSIZE_FORCE_EVENT) { - wxSizeEvent event( wxSize(width,height), GetId() ); - event.SetEventObject( this ); - HandleWindowEvent( event ); + SendSizeEvent(); } return; @@ -1098,7 +1213,7 @@ void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags) wxPoint wxWindowMac::GetClientAreaOrigin() const { int left,top,width,height; - m_peer->GetContentArea( left , top , width , height); + GetPeer()->GetContentArea( left , top , width , height); return wxPoint( left + MacGetLeftBorderSize() , top + MacGetTopBorderSize() ); } @@ -1117,6 +1232,11 @@ void wxWindowMac::DoSetClientSize(int clientwidth, int clientheight) } } +double wxWindowMac::GetContentScaleFactor() const +{ + return GetPeer()->GetContentScaleFactor(); +} + void wxWindowMac::SetLabel(const wxString& title) { if ( title == m_label ) @@ -1126,8 +1246,8 @@ void wxWindowMac::SetLabel(const wxString& title) InvalidateBestSize(); - if ( m_peer && m_peer->IsOk() ) - m_peer->SetLabel( wxStripMenuCodes(m_label, wxStrip_Mnemonics), GetFont().GetEncoding() ) ; + if ( GetPeer() && GetPeer()->IsOk() ) + GetPeer()->SetLabel( wxStripMenuCodes(m_label, wxStrip_Mnemonics), GetFont().GetEncoding() ) ; // do not trigger refreshes upon invisible and possible partly created objects if ( IsShownOnScreen() ) @@ -1141,11 +1261,17 @@ wxString wxWindowMac::GetLabel() const bool wxWindowMac::Show(bool show) { + if ( !show ) + MacInvalidateBorders(); + if ( !wxWindowBase::Show(show) ) return false; - if ( m_peer ) - m_peer->SetVisibility( show ) ; + if ( GetPeer() ) + GetPeer()->SetVisibility( show ) ; + + if ( show ) + MacInvalidateBorders(); #ifdef __WXOSX_IPHONE__ // only when there's no native event support @@ -1166,7 +1292,7 @@ bool wxWindowMac::OSXShowWithEffect(bool show, unsigned timeout) { if ( effect == wxSHOW_EFFECT_NONE || - !m_peer || !m_peer->ShowWithEffect(show, effect, timeout) ) + !GetPeer() || !GetPeer()->ShowWithEffect(show, effect, timeout) ) return Show(show); return true; @@ -1174,7 +1300,7 @@ bool wxWindowMac::OSXShowWithEffect(bool show, void wxWindowMac::DoEnable(bool enable) { - m_peer->Enable( enable ) ; + GetPeer()->Enable( enable ) ; MacInvalidateBorders(); } @@ -1192,7 +1318,6 @@ void wxWindowMac::MacHiliteChanged() void wxWindowMac::MacEnabledStateChanged() { - OnEnabled( m_peer->IsEnabled() ); } // @@ -1201,13 +1326,13 @@ void wxWindowMac::MacEnabledStateChanged() bool wxWindowMac::MacIsReallyEnabled() { - return m_peer->IsEnabled() ; + return GetPeer()->IsEnabled() ; } bool wxWindowMac::MacIsReallyHilited() { #if wxOSX_USE_CARBON - return m_peer->IsActive(); + return GetPeer()->IsActive(); #else return true; // TODO #endif @@ -1268,32 +1393,28 @@ void wxWindowMac::DoGetTextExtent(const wxString& str, void wxWindowMac::Refresh(bool WXUNUSED(eraseBack), const wxRect *rect) { - if ( m_peer == NULL ) + if ( GetPeer() == NULL ) return ; if ( !IsShownOnScreen() ) return ; + + if ( IsFrozen() ) + return; - m_peer->SetNeedsDisplay( rect ) ; + GetPeer()->SetNeedsDisplay( rect ) ; } void wxWindowMac::DoFreeze() { -#if wxOSX_USE_CARBON - if ( m_peer && m_peer->IsOk() ) - m_peer->SetDrawingEnabled( false ) ; -#endif + if ( GetPeer() && GetPeer()->IsOk() ) + GetPeer()->SetDrawingEnabled( false ) ; } void wxWindowMac::DoThaw() { -#if wxOSX_USE_CARBON - if ( m_peer && m_peer->IsOk() ) - { - m_peer->SetDrawingEnabled( true ) ; - m_peer->InvalidateWithChildren() ; - } -#endif + if ( GetPeer() && GetPeer()->IsOk() ) + GetPeer()->SetDrawingEnabled( true ) ; } wxWindow *wxGetActiveWindow() @@ -1440,8 +1561,8 @@ void wxWindowMac::MacPaintGrowBox() int tx,ty,tw,th; - m_peer->GetSize( tw, th ); - m_peer->GetPosition( tx, ty ); + GetPeer()->GetSize( tw, th ); + GetPeer()->GetPosition( tx, ty ); Rect rect = { ty,tx, ty+th, tx+tw }; @@ -1450,7 +1571,7 @@ void wxWindowMac::MacPaintGrowBox() CGRect cgrect = CGRectMake( rect.right - size , rect.bottom - size , size , size ) ; CGContextSaveGState( cgContext ); - if ( m_backgroundColour.Ok() ) + if ( m_backgroundColour.IsOk() ) { CGContextSetFillColorWithColor( cgContext, m_backgroundColour.GetCGColor() ); } @@ -1463,7 +1584,7 @@ void wxWindowMac::MacPaintGrowBox() #else if (m_growBox) { - if ( m_backgroundColour.Ok() ) + if ( m_backgroundColour.IsOk() ) m_growBox->SetBackgroundColour(m_backgroundColour); else m_growBox->SetBackgroundColour(*wxWHITE); @@ -1479,28 +1600,24 @@ void wxWindowMac::MacPaintBorders( int WXUNUSED(leftOrigin) , int WXUNUSED(right if ( IsTopLevel() ) return ; - bool hasFocus = m_peer->NeedsFocusRect() && m_peer->HasFocus() ; + bool hasFocus = GetPeer()->NeedsFocusRect() && HasFocus(); // back to the surrounding frame rectangle int tx,ty,tw,th; - m_peer->GetSize( tw, th ); - m_peer->GetPosition( tx, ty ); - - Rect rect = { ty,tx, ty+th, tx+tw }; + GetPeer()->GetSize( tw, th ); + GetPeer()->GetPosition( tx, ty ); #if wxOSX_USE_COCOA_OR_CARBON - InsetRect( &rect, -1 , -1 ) ; - { - CGRect cgrect = CGRectMake( rect.left , rect.top , rect.right - rect.left , - rect.bottom - rect.top ) ; + CGRect cgrect = CGRectMake( tx-1 , ty-1 , tw+2 , + th+2 ) ; CGContextRef cgContext = (CGContextRef) GetParent()->MacGetCGContextRef() ; wxASSERT( cgContext ) ; - if ( m_peer->NeedsFrame() ) + if ( GetPeer()->NeedsFrame() ) { HIThemeFrameDrawInfo info ; memset( &info, 0 , sizeof(info) ) ; @@ -1573,9 +1690,7 @@ void wxWindowMac::DoUpdateScrollbarVisibility() MacRepositionScrollBars() ; if ( triggerSizeEvent ) { - wxSizeEvent event(GetSize(), m_windowId); - event.SetEventObject(this); - HandleWindowEvent(event); + SendSizeEvent(); } #endif } @@ -1585,6 +1700,12 @@ void wxWindowMac::SetScrollbar(int orient, int pos, int thumb, int range, bool refresh) { #if wxUSE_SCROLLBAR + // Updating scrollbars when window is being deleted is useless and + // currently results in asserts in client-to-screen coordinates conversion + // code which is used by DoUpdateScrollbarVisibility() so just skip it. + if ( m_isBeingDeleted ) + return; + if ( orient == wxHORIZONTAL && m_hScrollBar ) m_hScrollBar->SetScrollbar(pos, thumb, range, thumb, refresh); else if ( orient == wxVERTICAL && m_vScrollBar ) @@ -1610,7 +1731,7 @@ void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect) // as the native control might be not a 0/0 wx window coordinates, we have to offset scrollrect.Offset( -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ; - m_peer->ScrollRect( &scrollrect, dx, dy ); + GetPeer()->ScrollRect( &scrollrect, dx, dy ); } wxWindowMac *child; @@ -1679,24 +1800,16 @@ wxWindow *wxWindowBase::DoFindFocus() return wxFindWindowFromWXWidget(wxWidgetImpl::FindFocus()); } -void wxWindowMac::OnInternalIdle() -{ - // This calls the UI-update mechanism (querying windows for - // menu/toolbar/control state information) - if (wxUpdateUIEvent::CanUpdate(this) && IsShownOnScreen()) - UpdateWindowUI(wxUPDATE_UI_FROMIDLE); -} - // Raise the window to the top of the Z order void wxWindowMac::Raise() { - m_peer->Raise(); + GetPeer()->Raise(); } // Lower the window to the bottom of the Z order void wxWindowMac::Lower() { - m_peer->Lower(); + GetPeer()->Lower(); } // static wxWindow *gs_lastWhich = NULL; @@ -1711,6 +1824,8 @@ bool wxWindowMac::MacSetupCursor( const wxPoint& pt ) if ( wxRect2DInt( clientorigin.x , clientorigin.y , clientsize.x , clientsize.y ).Contains( wxPoint2DInt( pt ) ) ) { wxSetCursorEvent event( pt.x , pt.y ); + event.SetId(GetId()); + event.SetEventObject(this); bool processedEvtSetCursor = HandleWindowEvent(event); if ( processedEvtSetCursor && event.HasCursor() ) @@ -1723,18 +1838,18 @@ bool wxWindowMac::MacSetupCursor( const wxPoint& pt ) // if the user code caught EVT_SET_CURSOR() and returned nothing from // it - this is a way to say that our cursor shouldn't be used for this // point - if ( !processedEvtSetCursor && m_cursor.Ok() ) + if ( !processedEvtSetCursor && m_cursor.IsOk() ) cursor = m_cursor ; if ( !wxIsBusy() && !GetParent() ) cursor = *wxSTANDARD_CURSOR ; } - if ( cursor.Ok() ) + if ( cursor.IsOk() ) cursor.MacInstall() ; } - return cursor.Ok() ; + return cursor.IsOk() ; } wxString wxWindowMac::MacGetToolTipString( wxPoint &WXUNUSED(pt) ) @@ -1837,8 +1952,8 @@ void wxWindowMac::MacUpdateClippedRects() const int tx,ty,tw,th; - m_peer->GetSize( tw, th ); - m_peer->GetPosition( tx, ty ); + GetPeer()->GetSize( tw, th ); + GetPeer()->GetPosition( tx, ty ); Rect r = { ty,tx, ty+th, tx+tw }; @@ -1994,6 +2109,11 @@ bool wxWindowMac::MacDoRedraw( long time ) } m_updateRegion = formerUpdateRgn; + + wxNonOwnedWindow* top = MacGetTopLevelWindow(); + if (top) + top->WindowWasPainted() ; + return handled; } @@ -2081,7 +2201,8 @@ bool wxWindowMac::MacHasScrollBarCorner() const const wxFrame *frame = wxDynamicCast( win, wxFrame ) ; if ( frame ) { - if ( frame->GetWindowStyleFlag() & wxRESIZE_BORDER ) + // starting from 10.7 there are no resize indicators anymore + if ( (frame->GetWindowStyleFlag() & wxRESIZE_BORDER) && UMAGetSystemVersion() < 0x1070) { // Parent frame has resize handle wxPoint frameBottomRight = frame->GetScreenRect().GetBottomRight(); @@ -2219,10 +2340,10 @@ void wxWindowMac::MacRepositionScrollBars() bool wxWindowMac::AcceptsFocus() const { - if ( MacIsUserPane() ) + if ( GetPeer() == NULL || GetPeer()->IsUserPane() ) return wxWindowBase::AcceptsFocus(); else - return m_peer->CanFocus(); + return GetPeer()->CanFocus(); } void wxWindowMac::MacSuperChangedPosition() @@ -2257,14 +2378,14 @@ void wxWindowMac::MacTopLevelWindowChangedPosition() } } -long wxWindowMac::MacGetLeftBorderSize() const +long wxWindowMac::MacGetWXBorderSize() const { if ( IsTopLevel() ) return 0 ; SInt32 border = 0 ; - if ( m_peer && m_peer->NeedsFrame() ) + if ( GetPeer() && GetPeer()->NeedsFrame() ) { if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER)) { @@ -2291,22 +2412,65 @@ long wxWindowMac::MacGetLeftBorderSize() const return border ; } +long wxWindowMac::MacGetLeftBorderSize() const +{ + // the wx borders are all symmetric in mac themes + long border = MacGetWXBorderSize() ; + + if ( GetPeer() ) + { + int left, top, right, bottom; + GetPeer()->GetLayoutInset( left, top, right, bottom ); + border -= left; + } + + return border; +} + + long wxWindowMac::MacGetRightBorderSize() const { - // they are all symmetric in mac themes - return MacGetLeftBorderSize() ; + // the wx borders are all symmetric in mac themes + long border = MacGetWXBorderSize() ; + + if ( GetPeer() ) + { + int left, top, right, bottom; + GetPeer()->GetLayoutInset( left, top, right, bottom ); + border -= right; + } + + return border; } long wxWindowMac::MacGetTopBorderSize() const { - // they are all symmetric in mac themes - return MacGetLeftBorderSize() ; + // the wx borders are all symmetric in mac themes + long border = MacGetWXBorderSize() ; + + if ( GetPeer() ) + { + int left, top, right, bottom; + GetPeer()->GetLayoutInset( left, top, right, bottom ); + border -= top; + } + + return border; } long wxWindowMac::MacGetBottomBorderSize() const { - // they are all symmetric in mac themes - return MacGetLeftBorderSize() ; + // the wx borders are all symmetric in mac themes + long border = MacGetWXBorderSize() ; + + if ( GetPeer() ) + { + int left, top, right, bottom; + GetPeer()->GetLayoutInset( left, top, right, bottom ); + border -= bottom; + } + + return border; } long wxWindowMac::MacRemoveBordersFromStyle( long style ) @@ -2369,7 +2533,7 @@ Rect wxMacGetBoundsForControl( wxWindowMac* window , const wxPoint& pos , const int x, y, w, h ; window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ; - Rect bounds = { y, x, y + h, x + w }; + Rect bounds = { static_cast(y), static_cast(x), static_cast(y + h), static_cast(x + w) }; return bounds ; } @@ -2397,8 +2561,8 @@ bool wxWindowMac::Reparent(wxWindowBase *newParentBase) if ( !wxWindowBase::Reparent(newParent) ) return false; - m_peer->RemoveFromParent(); - m_peer->Embed( GetParent()->GetPeer() ); + GetPeer()->RemoveFromParent(); + GetPeer()->Embed( GetParent()->GetPeer() ); MacChildAdded(); return true; @@ -2429,9 +2593,9 @@ wxByte wxWindowMac::GetTransparent() const bool wxWindowMac::IsShownOnScreen() const { - if ( m_peer && m_peer->IsOk() ) + if ( GetPeer() && GetPeer()->IsOk() ) { - bool peerVis = m_peer->IsVisible(); + bool peerVis = GetPeer()->IsVisible(); bool wxVis = wxWindowBase::IsShownOnScreen(); if( peerVis != wxVis ) { @@ -2445,16 +2609,137 @@ bool wxWindowMac::IsShownOnScreen() const return wxVis; } - return m_peer->IsVisible(); + return GetPeer()->IsVisible(); } return wxWindowBase::IsShownOnScreen(); } +#if wxUSE_HOTKEY && wxOSX_USE_COCOA_OR_CARBON + +OSStatus +wxHotKeyHandler(EventHandlerCallRef WXUNUSED(nextHandler), + EventRef event, + void* WXUNUSED(userData)) +{ + EventHotKeyID hotKeyId; + + GetEventParameter( event, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hotKeyId), NULL, &hotKeyId); + + for ( unsigned i = 0; i < s_hotkeys.size(); ++i ) + { + if ( s_hotkeys[i].keyId == static_cast(hotKeyId.id) ) + { + unsigned char charCode ; + UInt32 keyCode ; + UInt32 modifiers ; + UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ; + + GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode ); + GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode ); + GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers ); + + UInt32 keymessage = (keyCode << 8) + charCode; + + wxKeyEvent wxevent(wxEVT_HOTKEY); + wxevent.SetId(hotKeyId.id); + wxTheApp->MacCreateKeyEvent( wxevent, s_hotkeys[i].window , keymessage , + modifiers , when , 0 ) ; + + s_hotkeys[i].window->HandleWindowEvent(wxevent); + } + } + + return noErr; +} + +bool wxWindowMac::RegisterHotKey(int hotkeyId, int modifiers, int keycode) +{ + for ( unsigned i = 0; i < s_hotkeys.size(); ++i ) + { + if ( s_hotkeys[i].keyId == hotkeyId ) + { + wxLogLastError(wxT("hotkeyId already registered")); + + return false; + } + } + + static bool installed = false; + if ( !installed ) + { + EventTypeSpec eventType; + eventType.eventClass=kEventClassKeyboard; + eventType.eventKind=kEventHotKeyPressed; + + InstallApplicationEventHandler(&wxHotKeyHandler, 1, &eventType, NULL, NULL); + installed = true; + } + + UInt32 mac_modifiers=0; + if ( modifiers & wxMOD_ALT ) + mac_modifiers |= optionKey; + if ( modifiers & wxMOD_SHIFT ) + mac_modifiers |= shiftKey; + if ( modifiers & wxMOD_RAW_CONTROL ) + mac_modifiers |= controlKey; + if ( modifiers & wxMOD_CONTROL ) + mac_modifiers |= cmdKey; + + EventHotKeyRef hotKeyRef; + EventHotKeyID hotKeyIDmac; + + hotKeyIDmac.signature = 'WXMC'; + hotKeyIDmac.id = hotkeyId; + + if ( RegisterEventHotKey(wxCharCodeWXToOSX((wxKeyCode)keycode), mac_modifiers, hotKeyIDmac, + GetApplicationEventTarget(), 0, &hotKeyRef) != noErr ) + { + wxLogLastError(wxT("RegisterHotKey")); + + return false; + } + else + { + wxHotKeyRec v; + v.ref = hotKeyRef; + v.keyId = hotkeyId; + v.window = this; + + s_hotkeys.push_back(v); + } + + return true; +} + +bool wxWindowMac::UnregisterHotKey(int hotkeyId) +{ + for ( int i = ((int)s_hotkeys.size())-1; i>=0; -- i ) + { + if ( s_hotkeys[i].keyId == hotkeyId ) + { + EventHotKeyRef ref = s_hotkeys[i].ref; + s_hotkeys.erase(s_hotkeys.begin() + i); + if ( UnregisterEventHotKey(ref) != noErr ) + { + wxLogLastError(wxT("UnregisterHotKey")); + + return false; + } + else + return true; + } + } + + return false; +} + +#endif // wxUSE_HOTKEY + bool wxWindowMac::OSXHandleKeyEvent( wxKeyEvent& event ) { - bool handled = HandleWindowEvent( event ) ; - if ( handled && event.GetSkipped() ) - handled = false ; + bool handled = false; + + // moved the ordinary key event sending AFTER the accel evaluation #if wxUSE_ACCEL if ( !handled && event.GetEventType() == wxEVT_KEY_DOWN) @@ -2467,13 +2752,13 @@ bool wxWindowMac::OSXHandleKeyEvent( wxKeyEvent& event ) { wxEvtHandler * const handler = ancestor->GetEventHandler(); - wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command ); + wxCommandEvent command_event( wxEVT_MENU, command ); handled = handler->ProcessEvent( command_event ); if ( !handled ) { // accelerators can also be used with buttons, try them too - command_event.SetEventType(wxEVT_COMMAND_BUTTON_CLICKED); + command_event.SetEventType(wxEVT_BUTTON); handled = handler->ProcessEvent( command_event ); } @@ -2487,6 +2772,13 @@ bool wxWindowMac::OSXHandleKeyEvent( wxKeyEvent& event ) } } #endif // wxUSE_ACCEL + + if ( !handled ) + { + handled = HandleWindowEvent( event ) ; + if ( handled && event.GetSkipped() ) + handled = false ; + } return handled ; } @@ -2495,6 +2787,10 @@ bool wxWindowMac::OSXHandleKeyEvent( wxKeyEvent& event ) // wxWidgetImpl // +// we are maintaining a n:1 map from native controls (ControlRef / NSView*) to their wxWidgetImpl +// n:1 because we might have an embedded view eg within a scrollview, both being part of the same impl +// the impl is calling Associate with its newly created native control(s), e.g. in InstallHandler + WX_DECLARE_HASH_MAP(WXWidget, wxWidgetImpl*, wxPointerHash, wxPointerEqual, MacControlMap); static MacControlMap wxWinMacControlList; @@ -2549,10 +2845,11 @@ void wxWidgetImpl::RemoveAssociations(wxWidgetImpl* impl) IMPLEMENT_ABSTRACT_CLASS( wxWidgetImpl , wxObject ) -wxWidgetImpl::wxWidgetImpl( wxWindowMac* peer , bool isRootControl ) +wxWidgetImpl::wxWidgetImpl( wxWindowMac* peer , bool isRootControl, bool isUserPane ) { Init(); m_isRootControl = isRootControl; + m_isUserPane = isUserPane; m_wxPeer = peer; m_shouldSendEvents = true; } @@ -2593,3 +2890,7 @@ bool wxWidgetImpl::NeedsFrame() const { return m_needsFrame; } + +void wxWidgetImpl::SetDrawingEnabled(bool WXUNUSED(enabled)) +{ +}