diff --git a/include/wx/cocoa/window.h b/include/wx/cocoa/window.h index e1a69eba19..bedf65dcc9 100644 --- a/include/wx/cocoa/window.h +++ b/include/wx/cocoa/window.h @@ -111,6 +111,12 @@ protected: // NOTE: position is 10,10 to make it "obvious" that it's out of place return NSMakeRect(10.0,10.0,WidthDefault(size.x),HeightDefault(size.y)); } + // These functions translate NSPoint or NSRect between the coordinate + // system of Cocoa's boudns rect and wx's coordinate system. + NSPoint CocoaTransformBoundsToWx(NSPoint pointBounds); + NSRect CocoaTransformBoundsToWx(NSRect rectBounds); + NSPoint CocoaTransformWxToBounds(NSPoint pointWx); + NSRect CocoaTransformWxToBounds(NSRect rectWx); #endif //def __OBJC__ // ------------------------------------------------------------------------ // Implementation diff --git a/src/cocoa/window.mm b/src/cocoa/window.mm index 00f89e22d1..4f9af646e0 100644 --- a/src/cocoa/window.mm +++ b/src/cocoa/window.mm @@ -344,6 +344,62 @@ WX_NSView wxWindowCocoa::GetNSViewForHiding() const : m_cocoaNSView; } +NSPoint wxWindowCocoa::CocoaTransformBoundsToWx(NSPoint pointBounds) +{ + // TODO: Handle scrolling offset + wxCHECK_MSG(GetNSView(), pointBounds, wxT("Need to have a Cocoa view to do translation")); + if([GetNSView() isFlipped]) + return pointBounds; + NSRect ourBounds = [GetNSView() bounds]; + return NSMakePoint + ( pointBounds.x + , ourBounds.size.height - pointBounds.y + ); +} + +NSRect wxWindowCocoa::CocoaTransformBoundsToWx(NSRect rectBounds) +{ + // TODO: Handle scrolling offset + wxCHECK_MSG(GetNSView(), rectBounds, wxT("Need to have a Cocoa view to do translation")); + if([GetNSView() isFlipped]) + return rectBounds; + NSRect ourBounds = [GetNSView() bounds]; + return NSMakeRect + ( rectBounds.origin.x + , ourBounds.size.height - (rectBounds.origin.y + rectBounds.size.height) + , rectBounds.size.width + , rectBounds.size.height + ); +} + +NSPoint wxWindowCocoa::CocoaTransformWxToBounds(NSPoint pointWx) +{ + // TODO: Handle scrolling offset + wxCHECK_MSG(GetNSView(), pointWx, wxT("Need to have a Cocoa view to do translation")); + if([GetNSView() isFlipped]) + return pointWx; + NSRect ourBounds = [GetNSView() bounds]; + return NSMakePoint + ( pointWx.x + , ourBounds.size.height - pointWx.y + ); +} + +NSRect wxWindowCocoa::CocoaTransformWxToBounds(NSRect rectWx) +{ + // TODO: Handle scrolling offset + wxCHECK_MSG(GetNSView(), rectWx, wxT("Need to have a Cocoa view to do translation")); + if([GetNSView() isFlipped]) + return rectWx; + NSRect ourBounds = [GetNSView() bounds]; + return NSMakeRect + ( rectWx.origin.x + , ourBounds.size.height - (rectWx.origin.y + rectWx.size.height) + , rectWx.size.width + , rectWx.size.height + ); +} + bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect) { wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_drawRect")); @@ -363,7 +419,14 @@ bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect) // Try replacing the larger rectangle with a list of smaller ones: if ([GetNSView() respondsToSelector:@selector(getRectsBeingDrawn:count:)]) [GetNSView() getRectsBeingDrawn:&rects count:&countRects]; - m_updateRegion = wxRegion(rects,countRects); + + NSRect *transformedRects = (NSRect*)malloc(sizeof(NSRect)*countRects); + for(int i=0; iCocoaTransformWxToBounds(NSMakeRect(x,y,width,height)); + [nsview setFrame:newFrameRect]; // Be sure to redraw the parent to reflect the changed position - [superview setNeedsDisplay:YES]; + [superview setNeedsDisplayInRect:oldFrameRect]; + [superview setNeedsDisplayInRect:newFrameRect]; } void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size) @@ -646,14 +714,14 @@ void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size) NSView *nsview = GetNSViewForSuperview(); NSView *superview = [nsview superview]; wxCHECK_RET(superview,wxT("NSView does not have a superview")); - NSRect parentRect = [superview bounds]; + wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent")); NSRect frameRect = [nsview frame]; if(size.x!=-1) frameRect.size.width = size.x; if(size.y!=-1) frameRect.size.height = size.y; frameRect.origin.x = pos.x; - frameRect.origin.y = parentRect.size.height-(pos.y+frameRect.size.height); + frameRect.origin.y = pos.y; // Tell Cocoa to change the margin between the bottom of the superview // and the bottom of the control. Keeps the control pinned to the top // of its superview so that its position in the wxWidgets coordinate @@ -662,6 +730,7 @@ void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size) [nsview setAutoresizingMask: NSViewMinYMargin]; // MUST set the mask before setFrame: which can generate a size event // and cause a scroller to be added! + frameRect = GetParent()->CocoaTransformWxToBounds(frameRect); [nsview setFrame: frameRect]; } @@ -679,15 +748,13 @@ void wxWindow::DoGetSize(int *w, int *h) const void wxWindow::DoGetPosition(int *x, int *y) const { NSView *nsview = GetNSViewForSuperview(); - NSView *superview = [nsview superview]; - wxCHECK_RET(superview,wxT("NSView does not have a superview")); - NSRect parentRect = [superview bounds]; NSRect cocoaRect = [nsview frame]; + NSRect rectWx = GetParent()->CocoaTransformBoundsToWx(cocoaRect); if(x) - *x=(int)cocoaRect.origin.x; + *x=(int)rectWx.origin.x; if(y) - *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height)); + *y=(int)rectWx.origin.y; wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y); }