From 4850cc8b2e1037cd394806db4ddcf73868588a4c Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Tue, 13 Jan 2009 18:35:23 +0000 Subject: [PATCH] using subclass as impl ptr, common code in macro because mix-in are not possible in obj-c git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58078 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/osx/cocoa/private.h | 143 +++++++++++++++++++++------------ src/osx/cocoa/bmpbuttn.mm | 14 +++- src/osx/cocoa/button.mm | 4 +- src/osx/cocoa/choice.mm | 10 +-- src/osx/cocoa/gauge.mm | 8 +- src/osx/cocoa/glcanvas.mm | 10 +-- src/osx/cocoa/scrolbar.mm | 10 +-- src/osx/cocoa/slider.mm | 10 +-- src/osx/cocoa/spinbutt.mm | 10 +-- src/osx/cocoa/srchctrl.mm | 6 +- src/osx/cocoa/statbox.mm | 4 +- src/osx/cocoa/stattext.mm | 62 +++++--------- src/osx/cocoa/textctrl.mm | 4 +- src/osx/cocoa/window.mm | 73 +++++------------ 14 files changed, 185 insertions(+), 183 deletions(-) diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index d56945236f..d9ccabc652 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -26,58 +26,8 @@ #endif #ifdef __OBJC__ - #import - - #if wxUSE_GUI - - extern NSRect wxToNSRect( NSView* parent, const wxRect& r ); - extern wxRect wxFromNSRect( NSView* parent, const NSRect& rect ); - extern NSPoint wxToNSPoint( NSView* parent, const wxPoint& p ); - extern wxPoint wxFromNSPoint( NSView* parent, const NSPoint& p ); - - // used for many wxControls - - @interface wxNSButton : NSButton - { - wxWidgetImpl* impl; - } - - - (void)setImplementation: (wxWidgetImpl *) theImplementation; - - (wxWidgetImpl*) implementation; - - (BOOL) isFlipped; - - (void) clickedAction: (id) sender; - - @end - - @interface wxNSBox : NSBox - { - wxWidgetImpl* impl; - } - - - (void)setImplementation: (wxWidgetImpl *) theImplementation; - - (wxWidgetImpl*) implementation; - - (BOOL) isFlipped; - - @end - - @interface wxNSTextField : NSTextField - { - wxWidgetImpl* impl; - } - - - (void)setImplementation: (wxWidgetImpl *) theImplementation; - - (wxWidgetImpl*) implementation; - - (BOOL) isFlipped; - - @end - - NSRect WXDLLIMPEXP_CORE wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , - bool adjustForOrigin = true ); - - #endif // wxUSE_GUI - -#endif // __OBJC__ +#endif // // shared between Cocoa and Carbon @@ -166,6 +116,9 @@ public : void SetFont( const wxFont & font , const wxColour& foreground , long windowStyle, bool ignoreBlack = true ); void InstallEventHandler( WXWidget control = NULL ); + + virtual void DoHandleMouseEvent(NSEvent *event); + protected: WXWidget m_osxView; DECLARE_DYNAMIC_CLASS_NO_COPY(wxWidgetCocoaImpl) @@ -230,6 +183,94 @@ protected : DECLARE_DYNAMIC_CLASS_NO_COPY(wxNonOwnedWindowCocoaImpl) }; +#ifdef __OBJC__ + + extern NSRect wxToNSRect( NSView* parent, const wxRect& r ); + extern wxRect wxFromNSRect( NSView* parent, const NSRect& rect ); + extern NSPoint wxToNSPoint( NSView* parent, const wxPoint& p ); + extern wxPoint wxFromNSPoint( NSView* parent, const NSPoint& p ); + + // used for many wxControls + + @interface wxNSButton : NSButton + { + wxWidgetCocoaImpl* impl; + } + + - (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation; + - (wxWidgetCocoaImpl*) implementation; + - (BOOL) isFlipped; + - (void) clickedAction: (id) sender; + + @end + + @interface wxNSBox : NSBox + { + wxWidgetCocoaImpl* impl; + } + + - (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation; + - (wxWidgetCocoaImpl*) implementation; + - (BOOL) isFlipped; + + @end + + @interface wxNSTextField : NSTextField + { + wxWidgetCocoaImpl* impl; + } + + - (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation; + - (wxWidgetCocoaImpl*) implementation; + - (BOOL) isFlipped; + + @end + + NSRect WXDLLIMPEXP_CORE wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , + bool adjustForOrigin = true ); + + // common code snippets for cocoa implementations + // later to be done using injection in method table + + #define WXCOCOAIMPL_COMMON_MOUSE_INTERFACE -(void)mouseDown:(NSEvent *)event ;\ + -(void)rightMouseDown:(NSEvent *)event ;\ + -(void)otherMouseDown:(NSEvent *)event ;\ + -(void)mouseUp:(NSEvent *)event ;\ + -(void)rightMouseUp:(NSEvent *)event ;\ + -(void)otherMouseUp:(NSEvent *)event ;\ + -(void)handleMouseEvent:(NSEvent *)event; + + #define WXCOCOAIMPL_COMMON_MOUSE_IMPLEMENTATION -(void)mouseDown:(NSEvent *)event \ + {\ + [self handleMouseEvent:event];\ + }\ + -(void)rightMouseDown:(NSEvent *)event\ + {\ + [self handleMouseEvent:event];\ + }\ + -(void)otherMouseDown:(NSEvent *)event\ + {\ + [self handleMouseEvent:event];\ + }\ + -(void)mouseUp:(NSEvent *)event\ + {\ + [self handleMouseEvent:event];\ + }\ + -(void)rightMouseUp:(NSEvent *)event\ + {\ + [self handleMouseEvent:event];\ + }\ + -(void)otherMouseUp:(NSEvent *)event\ + {\ + [self handleMouseEvent:event];\ + }\ + -(void)handleMouseEvent:(NSEvent *)event\ + {\ + impl->DoHandleMouseEvent(event);\ + } + +#endif // __OBJC__ + // NSCursor WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type ); diff --git a/src/osx/cocoa/bmpbuttn.mm b/src/osx/cocoa/bmpbuttn.mm index 0c1f471a3e..ab3433bd7f 100644 --- a/src/osx/cocoa/bmpbuttn.mm +++ b/src/osx/cocoa/bmpbuttn.mm @@ -33,8 +33,20 @@ wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer, { NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; wxNSButton* v = [[wxNSButton alloc] initWithFrame:r]; + + // trying to get as close as possible to flags + if ( style & wxBORDER_NONE ) + { + [v setBezelStyle:NSShadowlessSquareBezelStyle]; + } + else + { + if ( style & wxBU_AUTODRAW ) + [v setBezelStyle:NSShadowlessSquareBezelStyle]; + else + [v setBezelStyle:NSRegularSquareBezelStyle]; + } - [v setBezelStyle:NSRegularSquareBezelStyle]; [v setImage:bitmap.GetNSImage() ]; [v setButtonType:NSMomentaryPushInButton]; wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); diff --git a/src/osx/cocoa/button.mm b/src/osx/cocoa/button.mm index bbf249361f..60c1c45bf4 100644 --- a/src/osx/cocoa/button.mm +++ b/src/osx/cocoa/button.mm @@ -142,12 +142,12 @@ wxSize wxButton::GetDefaultSize() } } -- (void)setImplementation: (wxWidgetImpl *) theImplementation +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation { impl = theImplementation; } -- (wxWidgetImpl*) implementation +- (wxWidgetCocoaImpl*) implementation { return impl; } diff --git a/src/osx/cocoa/choice.mm b/src/osx/cocoa/choice.mm index cf583d5fee..931a7308d4 100644 --- a/src/osx/cocoa/choice.mm +++ b/src/osx/cocoa/choice.mm @@ -24,11 +24,11 @@ @interface wxNSPopUpButton : NSPopUpButton { - wxWidgetImpl* impl; + wxWidgetCocoaImpl* impl; } -- (void)setImplementation: (wxWidgetImpl *) theImplementation; -- (wxWidgetImpl*) implementation; +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation; +- (wxWidgetCocoaImpl*) implementation; - (BOOL) isFlipped; - (void) clickedAction: (id) sender; @@ -55,12 +55,12 @@ } } -- (void)setImplementation: (wxWidgetImpl *) theImplementation +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation { impl = theImplementation; } -- (wxWidgetImpl*) implementation +- (wxWidgetCocoaImpl*) implementation { return impl; } diff --git a/src/osx/cocoa/gauge.mm b/src/osx/cocoa/gauge.mm index 71b9043b0a..77189ce3d5 100644 --- a/src/osx/cocoa/gauge.mm +++ b/src/osx/cocoa/gauge.mm @@ -19,10 +19,10 @@ @interface wxNSProgressIndicator : NSProgressIndicator { - wxWidgetImpl* impl; + wxWidgetCocoaImpl* impl; } -- (void)setImplementation: (wxWidgetImpl *) theImplementation; +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation; - (wxWidgetImpl*) implementation; - (BOOL) isFlipped; @@ -37,12 +37,12 @@ return self; } -- (void)setImplementation: (wxWidgetImpl *) theImplementation +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation { impl = theImplementation; } -- (wxWidgetImpl*) implementation +- (wxWidgetCocoaImpl*) implementation { return impl; } diff --git a/src/osx/cocoa/glcanvas.mm b/src/osx/cocoa/glcanvas.mm index ee5d515c4e..4e6654452c 100644 --- a/src/osx/cocoa/glcanvas.mm +++ b/src/osx/cocoa/glcanvas.mm @@ -229,13 +229,13 @@ bool wxGLContext::SetCurrent(const wxGLCanvas& win) const @interface wxNSCustomOpenGLView : NSView { - wxWidgetImpl* impl; + wxWidgetCocoaImpl* impl; NSOpenGLContext* context; } - (id)initWithFrame:(NSRect)frame; -- (void)setImplementation: (wxWidgetImpl *) theImplementation; -- (wxWidgetImpl*) implementation; +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation; +- (wxWidgetCocoaImpl*) implementation; - (BOOL) isFlipped; @end @@ -249,12 +249,12 @@ bool wxGLContext::SetCurrent(const wxGLCanvas& win) const return self; } -- (void)setImplementation: (wxWidgetImpl *) theImplementation +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation { impl = theImplementation; } -- (wxWidgetImpl*) implementation +- (wxWidgetCocoaImpl*) implementation { return impl; } diff --git a/src/osx/cocoa/scrolbar.mm b/src/osx/cocoa/scrolbar.mm index c3e7b73b6d..e2a7b01f98 100644 --- a/src/osx/cocoa/scrolbar.mm +++ b/src/osx/cocoa/scrolbar.mm @@ -23,11 +23,11 @@ @interface wxNSScroller : NSScroller { - wxWidgetImpl* impl; + wxWidgetCocoaImpl* impl; } -- (void)setImplementation: (wxWidgetImpl *) theImplementation; -- (wxWidgetImpl*) implementation; +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation; +- (wxWidgetCocoaImpl*) implementation; - (BOOL) isFlipped; - (void) clickedAction: (id) sender; @@ -54,12 +54,12 @@ } } -- (void)setImplementation: (wxWidgetImpl *) theImplementation +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation { impl = theImplementation; } -- (wxWidgetImpl*) implementation +- (wxWidgetCocoaImpl*) implementation { return impl ; } diff --git a/src/osx/cocoa/slider.mm b/src/osx/cocoa/slider.mm index 8fc664c26c..227435b77d 100644 --- a/src/osx/cocoa/slider.mm +++ b/src/osx/cocoa/slider.mm @@ -18,11 +18,11 @@ @interface wxNSSlider : NSSlider { - wxWidgetImpl* impl; + wxWidgetCocoaImpl* impl; } -- (void)setImplementation: (wxWidgetImpl *) theImplementation; -- (wxWidgetImpl*) implementation; +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation; +- (wxWidgetCocoaImpl*) implementation; - (BOOL) isFlipped; - (void) clickedAction: (id) sender; @@ -49,12 +49,12 @@ } } -- (void)setImplementation: (wxWidgetImpl *) theImplementation +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation { impl = theImplementation; } -- (wxWidgetImpl*) implementation +- (wxWidgetCocoaImpl*) implementation { return impl; } diff --git a/src/osx/cocoa/spinbutt.mm b/src/osx/cocoa/spinbutt.mm index 436576b8b3..980fc81dda 100644 --- a/src/osx/cocoa/spinbutt.mm +++ b/src/osx/cocoa/spinbutt.mm @@ -18,11 +18,11 @@ @interface wxNSStepper : NSStepper { - wxWidgetImpl* impl; + wxWidgetCocoaImpl* impl; } -- (void)setImplementation: (wxWidgetImpl *) theImplementation; -- (wxWidgetImpl*) implementation; +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation; +- (wxWidgetCocoaImpl*) implementation; - (BOOL) isFlipped; - (void) clickedAction: (id) sender; @@ -49,12 +49,12 @@ } } -- (void)setImplementation: (wxWidgetImpl *) theImplementation +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation { impl = theImplementation; } -- (wxWidgetImpl*) implementation +- (wxWidgetCocoaImpl*) implementation { return impl; } diff --git a/src/osx/cocoa/srchctrl.mm b/src/osx/cocoa/srchctrl.mm index 6cda0bfab5..34758780b9 100644 --- a/src/osx/cocoa/srchctrl.mm +++ b/src/osx/cocoa/srchctrl.mm @@ -31,7 +31,7 @@ @interface wxNSSearchField : NSSearchField { - wxWidgetImpl* impl; + wxWidgetCocoaImpl* impl; } @end @@ -47,12 +47,12 @@ return self; } -- (void)setImplementation: (wxWidgetImpl *) theImplementation +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation { impl = theImplementation; } -- (wxWidgetImpl*) implementation +- (wxWidgetCocoaImpl*) implementation { return impl; } diff --git a/src/osx/cocoa/statbox.mm b/src/osx/cocoa/statbox.mm index 3eca187518..6f7fecfc38 100644 --- a/src/osx/cocoa/statbox.mm +++ b/src/osx/cocoa/statbox.mm @@ -18,12 +18,12 @@ @implementation wxNSBox -- (void)setImplementation: (wxWidgetImpl *) theImplementation +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation { impl = theImplementation; } -- (wxWidgetImpl*) implementation +- (wxWidgetCocoaImpl*) implementation { return impl; } diff --git a/src/osx/cocoa/stattext.mm b/src/osx/cocoa/stattext.mm index c1ed173666..b3d5f89ebc 100644 --- a/src/osx/cocoa/stattext.mm +++ b/src/osx/cocoa/stattext.mm @@ -29,53 +29,35 @@ wxSize wxStaticText::DoGetBestSize() const { - Point bounds; -#if wxOSX_USE_CARBON - Rect bestsize = { 0 , 0 , 0 , 0 } ; - - // try the built-in best size if available - Boolean former = m_peer->GetData( kControlStaticTextIsMultilineTag); - m_peer->SetData( kControlStaticTextIsMultilineTag, (Boolean)0 ); - m_peer->GetBestRect( &bestsize ) ; - m_peer->SetData( kControlStaticTextIsMultilineTag, former ); - if ( !EmptyRect( &bestsize ) ) +Point bounds; + +#if wxOSX_USE_ATSU_TEXT + OSStatus err = noErr; + wxCFStringRef str( m_label, GetFont().GetEncoding() ); + + SInt16 baseline; + if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont ) { - bounds.h = bestsize.right - bestsize.left ; - bounds.v = bestsize.bottom - bestsize.top ; + err = GetThemeTextDimensions( + (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")), + m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline ); + verify_noerr( err ); } else #endif { -#if wxOSX_USE_CARBON - ControlFontStyleRec controlFont; - OSStatus err = m_peer->GetData( kControlEntireControl, kControlFontStyleTag, &controlFont ); - verify_noerr( err ); + wxClientDC dc(const_cast(this)); + wxCoord width, height ; + dc.GetTextExtent( m_label , &width, &height); + // Some labels seem to have their last characters + // stripped out. Adding 4 pixels seems to be enough to fix this. + bounds.h = width+4; + bounds.v = height; + } - wxCFStringRef str( m_label, GetFont().GetEncoding() ); + if ( m_label.empty() ) + bounds.h = 0; -#if wxOSX_USE_ATSU_TEXT - SInt16 baseline; - if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont ) - { - err = GetThemeTextDimensions( - (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")), - m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline ); - verify_noerr( err ); - } - else -#endif -#endif - { - wxClientDC dc(const_cast(this)); - wxCoord width, height ; - dc.GetTextExtent( m_label , &width, &height); - bounds.h = width; - bounds.v = height; - } - - if ( m_label.empty() ) - bounds.h = 0; - } bounds.h += MacGetLeftBorderSize() + MacGetRightBorderSize(); bounds.v += MacGetTopBorderSize() + MacGetBottomBorderSize(); diff --git a/src/osx/cocoa/textctrl.mm b/src/osx/cocoa/textctrl.mm index 6a2b2946de..199f6de543 100644 --- a/src/osx/cocoa/textctrl.mm +++ b/src/osx/cocoa/textctrl.mm @@ -51,12 +51,12 @@ @implementation wxNSTextField -- (void)setImplementation: (wxWidgetImpl *) theImplementation +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation { impl = theImplementation; } -- (wxWidgetImpl*) implementation +- (wxWidgetCocoaImpl*) implementation { return impl; } diff --git a/src/osx/cocoa/window.mm b/src/osx/cocoa/window.mm index 4594a4931d..ce2fa679f8 100644 --- a/src/osx/cocoa/window.mm +++ b/src/osx/cocoa/window.mm @@ -32,26 +32,20 @@ NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const @interface wxNSView : NSView { - wxWidgetImpl* impl; + wxWidgetCocoaImpl* impl; } - (void)drawRect: (NSRect) rect; --(void)mouseDown:(NSEvent *)event ; --(void)rightMouseDown:(NSEvent *)event ; --(void)otherMouseDown:(NSEvent *)event ; --(void)mouseUp:(NSEvent *)event ; --(void)rightMouseUp:(NSEvent *)event ; --(void)otherMouseUp:(NSEvent *)event ; --(void)handleMouseEvent:(NSEvent *)event; +WXCOCOAIMPL_COMMON_MOUSE_INTERFACE - (void)keyDown:(NSEvent *)event; - (void)keyUp:(NSEvent *)event; - (void)flagsChanged:(NSEvent *)event; - (void)handleKeyEvent:(NSEvent *)event; -- (void)setImplementation: (wxWidgetImpl *) theImplementation; -- (wxWidgetImpl*) implementation; +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation; +- (wxWidgetCocoaImpl*) implementation; - (BOOL) isFlipped; - (BOOL) becomeFirstResponder; - (BOOL) resignFirstResponder; @@ -348,47 +342,7 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent ) } } --(void)mouseDown:(NSEvent *)event -{ - [self handleMouseEvent:event]; -} - --(void)rightMouseDown:(NSEvent *)event -{ - [self handleMouseEvent:event]; -} - --(void)otherMouseDown:(NSEvent *)event -{ - [self handleMouseEvent:event]; -} - --(void)mouseUp:(NSEvent *)event -{ - [self handleMouseEvent:event]; -} - --(void)rightMouseUp:(NSEvent *)event -{ - [self handleMouseEvent:event]; -} - --(void)otherMouseUp:(NSEvent *)event -{ - [self handleMouseEvent:event]; -} - --(void)handleMouseEvent:(NSEvent *)event -{ - NSPoint clickLocation; - clickLocation = [self convertPoint:[event locationInWindow] fromView:nil]; - wxPoint pt = wxFromNSPoint( self, clickLocation ); - wxMouseEvent wxevent(wxEVT_LEFT_DOWN); - SetupMouseEvent( wxevent , event ) ; - wxevent.m_x = pt.x; - wxevent.m_y = pt.y; - impl->GetWXPeer()->HandleWindowEvent(wxevent); -} +WXCOCOAIMPL_COMMON_MOUSE_IMPLEMENTATION - (void)keyDown:(NSEvent *)event { @@ -413,12 +367,12 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent ) } -- (void)setImplementation: (wxWidgetImpl *) theImplementation +- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation { impl = theImplementation; } -- (wxWidgetImpl*) implementation +- (wxWidgetCocoaImpl*) implementation { return impl; } @@ -722,6 +676,19 @@ void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control ) { } +void wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event) +{ + NSPoint clickLocation; + clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil]; + wxPoint pt = wxFromNSPoint( m_osxView, clickLocation ); + wxMouseEvent wxevent(wxEVT_LEFT_DOWN); + SetupMouseEvent( wxevent , event ) ; + wxevent.m_x = pt.x; + wxevent.m_y = pt.y; + GetWXPeer()->HandleWindowEvent(wxevent); +} + + // // Factory methods // -- 2.45.2