+#include "wx/tooltip.h"
+
+#include "wx/cocoa/autorelease.h"
+#include "wx/cocoa/string.h"
+
+#include "wx/cocoa/objc/NSView.h"
+#import <AppKit/NSEvent.h>
+#import <AppKit/NSScrollView.h>
+#import <AppKit/NSColor.h>
+#import <AppKit/NSClipView.h>
+#import <Foundation/NSException.h>
+#import <AppKit/NSApplication.h>
+#import <AppKit/NSWindow.h>
+
+// Turn this on to paint green over the dummy views for debugging
+#undef WXCOCOA_FILL_DUMMY_VIEW
+
+#ifdef WXCOCOA_FILL_DUMMY_VIEW
+#import <AppKit/NSBezierPath.h>
+#endif //def WXCOCOA_FILL_DUMMY_VIEW
+
+// A category for methods that are only present in Panther's SDK
+@interface NSView(wxNSViewPrePantherCompatibility)
+- (void)getRectsBeingDrawn:(const NSRect **)rects count:(int *)count;
+@end
+
+NSPoint CocoaTransformNSViewBoundsToWx(NSView *nsview, NSPoint pointBounds)
+{
+ wxCHECK_MSG(nsview, pointBounds, wxT("Need to have a Cocoa view to do translation"));
+ if([nsview isFlipped])
+ return pointBounds;
+ NSRect ourBounds = [nsview bounds];
+ return NSMakePoint
+ ( pointBounds.x
+ , ourBounds.size.height - pointBounds.y
+ );
+}
+
+NSRect CocoaTransformNSViewBoundsToWx(NSView *nsview, NSRect rectBounds)
+{
+ wxCHECK_MSG(nsview, rectBounds, wxT("Need to have a Cocoa view to do translation"));
+ if([nsview isFlipped])
+ return rectBounds;
+ NSRect ourBounds = [nsview bounds];
+ return NSMakeRect
+ ( rectBounds.origin.x
+ , ourBounds.size.height - (rectBounds.origin.y + rectBounds.size.height)
+ , rectBounds.size.width
+ , rectBounds.size.height
+ );
+}
+
+NSPoint CocoaTransformNSViewWxToBounds(NSView *nsview, NSPoint pointWx)
+{
+ wxCHECK_MSG(nsview, pointWx, wxT("Need to have a Cocoa view to do translation"));
+ if([nsview isFlipped])
+ return pointWx;
+ NSRect ourBounds = [nsview bounds];
+ return NSMakePoint
+ ( pointWx.x
+ , ourBounds.size.height - pointWx.y
+ );
+}
+
+NSRect CocoaTransformNSViewWxToBounds(NSView *nsview, NSRect rectWx)
+{
+ wxCHECK_MSG(nsview, rectWx, wxT("Need to have a Cocoa view to do translation"));
+ if([nsview isFlipped])
+ return rectWx;
+ NSRect ourBounds = [nsview bounds];
+ return NSMakeRect
+ ( rectWx.origin.x
+ , ourBounds.size.height - (rectWx.origin.y + rectWx.size.height)
+ , rectWx.size.width
+ , rectWx.size.height
+ );
+}
+
+// ========================================================================
+// wxWindowCocoaHider
+// ========================================================================
+class wxWindowCocoaHider: protected wxCocoaNSView
+{
+ DECLARE_NO_COPY_CLASS(wxWindowCocoaHider)
+public:
+ wxWindowCocoaHider(wxWindow *owner);
+ virtual ~wxWindowCocoaHider();
+ inline WX_NSView GetNSView() { return m_dummyNSView; }
+protected:
+ wxWindowCocoa *m_owner;
+ WX_NSView m_dummyNSView;
+ virtual void Cocoa_FrameChanged(void);
+#ifdef WXCOCOA_FILL_DUMMY_VIEW
+ virtual bool Cocoa_drawRect(const NSRect& rect);
+#endif //def WXCOCOA_FILL_DUMMY_VIEW
+private:
+ wxWindowCocoaHider();
+};
+
+// ========================================================================
+// wxWindowCocoaScrollView
+// ========================================================================
+class wxWindowCocoaScrollView: protected wxCocoaNSView
+{
+ DECLARE_NO_COPY_CLASS(wxWindowCocoaScrollView)
+public:
+ wxWindowCocoaScrollView(wxWindow *owner);
+ virtual ~wxWindowCocoaScrollView();
+ inline WX_NSScrollView GetNSScrollView() { return m_cocoaNSScrollView; }
+ void ClientSizeToSize(int &width, int &height);
+ void DoGetClientSize(int *x, int *y) const;
+ void Encapsulate();
+ void Unencapsulate();
+protected:
+ wxWindowCocoa *m_owner;
+ WX_NSScrollView m_cocoaNSScrollView;
+ virtual void Cocoa_FrameChanged(void);
+private:
+ wxWindowCocoaScrollView();
+};
+
+// ========================================================================
+// wxDummyNSView
+// ========================================================================
+@interface wxDummyNSView : NSView
+- (NSView *)hitTest:(NSPoint)aPoint;
+@end
+
+@implementation wxDummyNSView : NSView
+- (NSView *)hitTest:(NSPoint)aPoint
+{
+ return nil;
+}
+
+@end
+
+// ========================================================================
+// wxWindowCocoaHider
+// ========================================================================
+wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner)
+: m_owner(owner)
+{
+ wxASSERT(owner);
+ wxASSERT(owner->GetNSViewForHiding());
+ m_dummyNSView = [[wxDummyNSView alloc]
+ initWithFrame:[owner->GetNSViewForHiding() frame]];
+ [m_dummyNSView setAutoresizingMask: [owner->GetNSViewForHiding() autoresizingMask]];
+ AssociateNSView(m_dummyNSView);
+}
+
+wxWindowCocoaHider::~wxWindowCocoaHider()
+{
+ DisassociateNSView(m_dummyNSView);
+ [m_dummyNSView release];
+}
+
+void wxWindowCocoaHider::Cocoa_FrameChanged(void)
+{
+ // Keep the real window in synch with the dummy
+ wxASSERT(m_dummyNSView);
+ [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]];
+}
+
+
+#ifdef WXCOCOA_FILL_DUMMY_VIEW
+bool wxWindowCocoaHider::Cocoa_drawRect(const NSRect& rect)
+{
+ NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:rect];
+ [[NSColor greenColor] set];
+ [bezpath stroke];
+ [bezpath fill];
+ return true;
+}
+#endif //def WXCOCOA_FILL_DUMMY_VIEW
+
+// ========================================================================
+// wxFlippedNSClipView
+// ========================================================================
+@interface wxFlippedNSClipView : NSClipView
+- (BOOL)isFlipped;
+@end
+
+@implementation wxFlippedNSClipView : NSClipView
+- (BOOL)isFlipped
+{
+ return YES;
+}
+
+@end
+
+// ========================================================================
+// wxWindowCocoaScrollView
+// ========================================================================
+wxWindowCocoaScrollView::wxWindowCocoaScrollView(wxWindow *owner)
+: m_owner(owner)
+{
+ wxAutoNSAutoreleasePool pool;
+ wxASSERT(owner);
+ wxASSERT(owner->GetNSView());
+ m_cocoaNSScrollView = [[NSScrollView alloc]
+ initWithFrame:[owner->GetNSView() frame]];
+ AssociateNSView(m_cocoaNSScrollView);
+
+ /* Replace the default NSClipView with a flipped one. This ensures
+ scrolling is "pinned" to the top-left instead of bottom-right. */
+ NSClipView *flippedClip = [[wxFlippedNSClipView alloc]
+ initWithFrame: [[m_cocoaNSScrollView contentView] frame]];
+ [m_cocoaNSScrollView setContentView:flippedClip];
+ [flippedClip release];
+
+ [m_cocoaNSScrollView setBackgroundColor: [NSColor windowBackgroundColor]];
+ [m_cocoaNSScrollView setHasHorizontalScroller: YES];
+ [m_cocoaNSScrollView setHasVerticalScroller: YES];
+ Encapsulate();
+}
+
+void wxWindowCocoaScrollView::Encapsulate()
+{
+ // Set the scroll view autoresizingMask to match the current NSView
+ [m_cocoaNSScrollView setAutoresizingMask: [m_owner->GetNSView() autoresizingMask]];
+ [m_owner->GetNSView() setAutoresizingMask: NSViewNotSizable];
+ // NOTE: replaceSubView will cause m_cocaNSView to be released
+ // except when it hasn't been added into an NSView hierarchy in which
+ // case it doesn't need to be and this should work out to a no-op
+ m_owner->CocoaReplaceView(m_owner->GetNSView(), m_cocoaNSScrollView);
+ // The NSView is still retained by owner
+ [m_cocoaNSScrollView setDocumentView: m_owner->GetNSView()];
+ // Now it's also retained by the NSScrollView
+}
+
+void wxWindowCocoaScrollView::Unencapsulate()
+{
+ [m_cocoaNSScrollView setDocumentView: nil];
+ m_owner->CocoaReplaceView(m_cocoaNSScrollView, m_owner->GetNSView());
+ if(![[m_owner->GetNSView() superview] isFlipped])
+ [m_owner->GetNSView() setAutoresizingMask: NSViewMinYMargin];
+}
+
+wxWindowCocoaScrollView::~wxWindowCocoaScrollView()
+{
+ DisassociateNSView(m_cocoaNSScrollView);
+ [m_cocoaNSScrollView release];
+}
+
+void wxWindowCocoaScrollView::ClientSizeToSize(int &width, int &height)
+{
+ NSSize frameSize = [NSScrollView
+ frameSizeForContentSize: NSMakeSize(width,height)
+ hasHorizontalScroller: [m_cocoaNSScrollView hasHorizontalScroller]
+ hasVerticalScroller: [m_cocoaNSScrollView hasVerticalScroller]
+ borderType: [m_cocoaNSScrollView borderType]];
+ width = (int)frameSize.width;
+ height = (int)frameSize.height;
+}
+
+void wxWindowCocoaScrollView::DoGetClientSize(int *x, int *y) const
+{
+ NSSize nssize = [m_cocoaNSScrollView contentSize];
+ if(x)
+ *x = (int)nssize.width;
+ if(y)
+ *y = (int)nssize.height;
+}
+
+void wxWindowCocoaScrollView::Cocoa_FrameChanged(void)
+{
+ wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
+ wxSizeEvent event(m_owner->GetSize(), m_owner->GetId());
+ event.SetEventObject(m_owner);
+ m_owner->GetEventHandler()->ProcessEvent(event);
+}
+
+// ========================================================================
+// wxWindowCocoa
+// ========================================================================