From 4dd9fdf82749bb5f9e2bf9c2ad1bbd59e0eeb00e Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Sat, 7 Feb 2009 13:25:58 +0000 Subject: [PATCH] reworking event handling to redirect to c++ virtual functions git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58704 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/osx/carbon/private.h | 2 +- include/wx/osx/cocoa/private.h | 217 ++--------- include/wx/osx/cursor.h | 2 +- include/wx/osx/dnd.h | 10 +- include/wx/osx/window.h | 2 +- src/osx/carbon/dnd.cpp | 33 +- src/osx/carbon/font.cpp | 2 +- src/osx/carbon/window.cpp | 2 - src/osx/cocoa/bmpbuttn.mm | 1 - src/osx/cocoa/button.mm | 23 +- src/osx/cocoa/checkbox.mm | 1 - src/osx/cocoa/choice.mm | 27 +- src/osx/cocoa/filedlg.mm | 186 +--------- src/osx/cocoa/gauge.mm | 17 +- src/osx/cocoa/glcanvas.mm | 19 +- src/osx/cocoa/listbox.mm | 94 +++-- src/osx/cocoa/notebook.mm | 19 +- src/osx/cocoa/radiobut.mm | 1 - src/osx/cocoa/scrolbar.mm | 136 ++++--- src/osx/cocoa/slider.mm | 64 ++-- src/osx/cocoa/spinbutt.mm | 111 +++--- src/osx/cocoa/srchctrl.mm | 18 +- src/osx/cocoa/statbox.mm | 12 +- src/osx/cocoa/statline.mm | 1 - src/osx/cocoa/stattext.mm | 1 - src/osx/cocoa/textctrl.mm | 12 +- src/osx/cocoa/tglbtn.mm | 2 - src/osx/cocoa/window.mm | 612 ++++++++++++++++++++++++++++---- src/osx/window_osx.cpp | 30 +- 29 files changed, 878 insertions(+), 779 deletions(-) diff --git a/include/wx/osx/carbon/private.h b/include/wx/osx/carbon/private.h index 5b36ffe55a..088fe282f3 100644 --- a/include/wx/osx/carbon/private.h +++ b/include/wx/osx/carbon/private.h @@ -455,7 +455,7 @@ public : virtual OSStatus SetTabEnabled( SInt16 tabNo , bool enable ); - void InstallEventHandler( WXWidget control = NULL ); + virtual void InstallEventHandler( WXWidget control = NULL ); protected : WXEVENTHANDLERREF m_macControlEventHandler ; ControlRef m_controlRef; diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index d1c7059d7e..507816b182 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -127,8 +127,28 @@ public : virtual bool DoHandleKeyEvent(NSEvent *event); virtual void DoNotifyFocusEvent(bool receivedFocus); + void SetFlipped(bool flipped); + + virtual unsigned int draggingEntered(void* sender, WXWidget slf, void* _cmd); + virtual void draggingExited(void* sender, WXWidget slf, void* _cmd); + virtual unsigned int draggingUpdated(void* sender, WXWidget slf, void* _cmd); + virtual bool performDragOperation(void* sender, WXWidget slf, void* _cmd); + virtual void mouseEvent(WX_NSEvent event, WXWidget slf, void* _cmd); + virtual void keyEvent(WX_NSEvent event, WXWidget slf, void* _cmd); + virtual bool performKeyEquivalent(WX_NSEvent event, WXWidget slf, void* _cmd); + virtual bool becomeFirstResponder(WXWidget slf, void* _cmd); + virtual bool resignFirstResponder(WXWidget slf, void* _cmd); + virtual void resetCursorRects(WXWidget slf, void* _cmd); + virtual bool isFlipped(WXWidget slf, void* _cmd); + virtual void drawRect(void* rect, WXWidget slf, void* _cmd); + + virtual void clickedAction(WXWidget slf, void* _cmd, void* sender); + virtual void doubleClickedAction(WXWidget slf, void* _cmd, void *sender); + protected: WXWidget m_osxView; + bool m_isFlipped; + DECLARE_DYNAMIC_CLASS_NO_COPY(wxWidgetCocoaImpl) }; @@ -203,217 +223,24 @@ protected : 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_EVENTS_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)mouseMoved:(NSEvent *)event;\ - - (void)mouseDragged:(NSEvent *)event;\ - - (void)rightMouseDragged:(NSEvent *)event;\ - - (void)otherMouseDragged:(NSEvent *)event;\ - - (void)scrollWheel:(NSEvent *)theEvent;\ - - (void)mouseEntered:(NSEvent *)event;\ - - (void)mouseExited:(NSEvent *)event;\ - - (void)keyDown:(NSEvent *)event;\ - - (void)keyUp:(NSEvent *)event;\ - - (BOOL)performKeyEquivalent:(NSEvent *)event;\ - - (void)flagsChanged:(NSEvent *)event;\ - - (BOOL)becomeFirstResponder;\ - - (BOOL)resignFirstResponder;\ - - (void)resetCursorRects; - - - #define WXCOCOAIMPL_COMMON_EVENTS_IMPLEMENTATION_NO_MOUSEDOWN -(void)rightMouseDown:(NSEvent *)event\ - {\ - if ( !impl->DoHandleMouseEvent(event) )\ - [super rightMouseDown:event];\ - }\ - -(void)otherMouseDown:(NSEvent *)event\ - {\ - if ( !impl->DoHandleMouseEvent(event) )\ - [super otherMouseDown:event];\ - }\ - -(void)mouseUp:(NSEvent *)event\ - {\ - if ( !impl->DoHandleMouseEvent(event) )\ - [super mouseUp:event];\ - }\ - -(void)rightMouseUp:(NSEvent *)event\ - {\ - if ( !impl->DoHandleMouseEvent(event) )\ - [super rightMouseUp:event];\ - }\ - -(void)otherMouseUp:(NSEvent *)event\ - {\ - if ( !impl->DoHandleMouseEvent(event) )\ - [super otherMouseUp:event];\ - }\ - -(void)mouseMoved:(NSEvent *)event\ - {\ - if ( !impl->DoHandleMouseEvent(event) )\ - [super mouseMoved:event];\ - }\ - -(void)mouseDragged:(NSEvent *)event\ - {\ - if ( !impl->DoHandleMouseEvent(event) )\ - [super mouseDragged:event];\ - }\ - -(void)rightMouseDragged:(NSEvent *)event\ - {\ - if ( !impl->DoHandleMouseEvent(event) )\ - [super rightMouseDragged:event];\ - }\ - -(void)otherMouseDragged:(NSEvent *)event\ - {\ - if ( !impl->DoHandleMouseEvent(event) )\ - [super otherMouseDragged:event];\ - }\ - -(void)scrollWheel:(NSEvent *)event\ - {\ - if ( !impl->DoHandleMouseEvent(event) )\ - [super scrollWheel:event];\ - }\ - -(void)mouseEntered:(NSEvent *)event\ - {\ - if ( !impl->DoHandleMouseEvent(event) )\ - [super mouseEntered:event];\ - }\ - -(void)mouseExited:(NSEvent *)event\ - {\ - if ( !impl->DoHandleMouseEvent(event) )\ - [super mouseExited:event];\ - }\ - -(BOOL)performKeyEquivalent:(NSEvent *)event\ - {\ - if ( !impl->DoHandleKeyEvent(event) )\ - return [super performKeyEquivalent:event];\ - return YES;\ - }\ - -(void)keyDown:(NSEvent *)event\ - {\ - if ( !impl->DoHandleKeyEvent(event) )\ - [super keyDown:event];\ - }\ - -(void)keyUp:(NSEvent *)event\ - {\ - if ( !impl->DoHandleKeyEvent(event) )\ - [super keyUp:event];\ - }\ - -(void)flagsChanged:(NSEvent *)event\ - {\ - if ( !impl->DoHandleKeyEvent(event) )\ - [super flagsChanged:event];\ - }\ - - (BOOL) becomeFirstResponder\ - {\ - BOOL r = [super becomeFirstResponder];\ - if ( r )\ - impl->DoNotifyFocusEvent( true );\ - return r;\ - }\ - - (BOOL) resignFirstResponder\ - {\ - BOOL r = [super resignFirstResponder];\ - if ( r )\ - impl->DoNotifyFocusEvent( false );\ - return r;\ - }\ - - (void) resetCursorRects\ - {\ - if ( impl )\ - {\ - wxWindow* wxpeer = impl->GetWXPeer();\ - if ( wxpeer )\ - {\ - NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();\ - if (cursor == NULL)\ - [super resetCursorRects];\ - else\ - [self addCursorRect: [self bounds]\ - cursor: cursor];\ - }\ - }\ - } - - #define WXCOCOAIMPL_COMMON_EVENTS_IMPLEMENTATION -(void)mouseDown:(NSEvent *)event \ - {\ - if ( !impl->DoHandleMouseEvent(event) )\ - [super mouseDown:event];\ - }\ - WXCOCOAIMPL_COMMON_EVENTS_IMPLEMENTATION_NO_MOUSEDOWN - - #define WXCOCOAIMPL_COMMON_MEMBERS wxWidgetCocoaImpl* impl; - - #define WXCOCOAIMPL_COMMON_INTERFACE \ - - (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation;\ - - (wxWidgetCocoaImpl*) implementation;\ - - (BOOL) isFlipped;\ - WXCOCOAIMPL_COMMON_EVENTS_INTERFACE - - #define WXCOCOAIMPL_COMMON_IMPLEMENTATION_BASE - (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation\ - {\ - impl = theImplementation;\ - }\ - - (wxWidgetCocoaImpl*) implementation\ - {\ - return impl;\ - }\ - - #define WXCOCOAIMPL_COMMON_IMPLEMENTATION WXCOCOAIMPL_COMMON_EVENTS_IMPLEMENTATION \ - WXCOCOAIMPL_COMMON_IMPLEMENTATION_BASE \ - - (BOOL) isFlipped\ - {\ - return YES;\ - } - - #define WXCOCOAIMPL_COMMON_IMPLEMENTATION_NO_MOUSEDOWN WXCOCOAIMPL_COMMON_EVENTS_IMPLEMENTATION_NO_MOUSEDOWN \ - WXCOCOAIMPL_COMMON_IMPLEMENTATION_BASE \ - - (BOOL) isFlipped\ - {\ - return YES;\ - } - - - #define WXCOCOAIMPL_COMMON_IMPLEMENTATION_NOT_FLIPPED WXCOCOAIMPL_COMMON_EVENTS_IMPLEMENTATION \ - WXCOCOAIMPL_COMMON_IMPLEMENTATION_BASE \ - - (BOOL) isFlipped\ - {\ - return NO;\ - } - // used for many wxControls @interface wxNSButton : NSButton { - WXCOCOAIMPL_COMMON_MEMBERS } - - WXCOCOAIMPL_COMMON_INTERFACE - - (void) clickedAction: (id) sender; - + @end @interface wxNSBox : NSBox { - WXCOCOAIMPL_COMMON_MEMBERS } - WXCOCOAIMPL_COMMON_INTERFACE - @end @interface wxNSTextField : NSTextField { - WXCOCOAIMPL_COMMON_MEMBERS } - WXCOCOAIMPL_COMMON_INTERFACE - @end @interface wxNSMenu : NSMenu @@ -438,6 +265,8 @@ protected : - (BOOL)validateMenuItem:(NSMenuItem *)menuItem; @end + + void wxOSXCocoaClassAddWXMethods(Class c); #endif // __OBJC__ diff --git a/include/wx/osx/cursor.h b/include/wx/osx/cursor.h index fe0c01391c..cf00e0c8e4 100644 --- a/include/wx/osx/cursor.h +++ b/include/wx/osx/cursor.h @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: wx/mac/carbon/cursor.h +// Name: wx/osx/cursor.h // Purpose: wxCursor class // Author: Stefan Csomor // Modified by: diff --git a/include/wx/osx/dnd.h b/include/wx/osx/dnd.h index dea6815a45..dd91f63789 100644 --- a/include/wx/osx/dnd.h +++ b/include/wx/osx/dnd.h @@ -57,10 +57,9 @@ class WXDLLIMPEXP_CORE wxDropTarget: public wxDropTargetBase virtual bool GetData(); bool CurrentDragHasSupportedFormat() ; - void SetCurrentDrag( void* drag ) { m_currentDrag = drag ; } - void* GetCurrentDrag() { return m_currentDrag ; } + void SetCurrentDragPasteboard( void* dragpasteboard ) { m_currentDragPasteboard = dragpasteboard ; } protected : - void* m_currentDrag ; + void* m_currentDragPasteboard ; }; //------------------------------------------------------------------------- @@ -93,13 +92,12 @@ public: virtual wxDragResult DoDragDrop(int flags = wxDrag_CopyOnly); wxWindow* GetWindow() { return m_window ; } - void SetCurrentDrag( void* drag ) { m_currentDrag = drag ; } - void* GetCurrentDrag() { return m_currentDrag ; } + void SetCurrentDragPasteboard( void* dragpasteboard ) { m_currentDragPasteboard = dragpasteboard ; } bool MacInstallDefaultCursor(wxDragResult effect) ; protected : wxWindow *m_window; - void* m_currentDrag ; + void* m_currentDragPasteboard ; }; #endif // wxUSE_DRAG_AND_DROP diff --git a/include/wx/osx/window.h b/include/wx/osx/window.h index 1137022d21..999f126e8a 100644 --- a/include/wx/osx/window.h +++ b/include/wx/osx/window.h @@ -138,7 +138,7 @@ public: // event handlers // -------------- - void OnPaint( wxPaintEvent& event ); + void OnNcPaint( wxNcPaintEvent& event ); void OnEraseBackground(wxEraseEvent& event ); void OnMouseEvent( wxMouseEvent &event ); diff --git a/src/osx/carbon/dnd.cpp b/src/osx/carbon/dnd.cpp index 172b99a437..0d195b1502 100644 --- a/src/osx/carbon/dnd.cpp +++ b/src/osx/carbon/dnd.cpp @@ -107,17 +107,10 @@ bool wxDropTarget::CurrentDragHasSupportedFormat() } } -#if wxOSX_USE_CARBON if ( !supported ) { - PasteboardRef pasteboard; - - if ( GetDragPasteboard( (DragReference)m_currentDrag, &pasteboard ) == noErr ) - { - supported = m_dataObject->HasDataInPasteboard( pasteboard ); - } + supported = m_dataObject->HasDataInPasteboard( m_currentDragPasteboard ); } -#endif return supported; } @@ -166,17 +159,10 @@ bool wxDropTarget::GetData() } } -#if wxOSX_USE_CARBON if ( !transferred ) { - PasteboardRef pasteboard; - - if ( GetDragPasteboard( (DragReference)m_currentDrag, &pasteboard ) == noErr ) - { - transferred = m_dataObject->GetFromPasteboard( pasteboard ); - } + transferred = m_dataObject->GetFromPasteboard( m_currentDragPasteboard ); } -#endif return transferred; } @@ -369,7 +355,8 @@ pascal OSErr wxMacWindowDragTrackingHandler( DragAttributes attributes; GetDragAttributes( theDrag, &attributes ); - + PasteboardRef pasteboard = 0; + GetDragPasteboard( theDrag, &pasteboard ); wxNonOwnedWindow* toplevel = wxNonOwnedWindow::GetFromWXWindow( (WXWindow) theWindow ); bool optionDown = GetCurrentKeyModifiers() & optionKey; @@ -427,7 +414,7 @@ pascal OSErr wxMacWindowDragTrackingHandler( #ifndef __LP64__ HideDragHilite( theDrag ); #endif - trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ); + trackingGlobals->m_currentTarget->SetCurrentDragPasteboard( pasteboard ); trackingGlobals->m_currentTarget->OnLeave(); trackingGlobals->m_currentTarget = NULL; trackingGlobals->m_currentTargetWindow = NULL; @@ -442,7 +429,7 @@ pascal OSErr wxMacWindowDragTrackingHandler( { if ( trackingGlobals->m_currentTarget ) { - trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ); + trackingGlobals->m_currentTarget->SetCurrentDragPasteboard( pasteboard ); result = trackingGlobals->m_currentTarget->OnEnter( localx, localy, result ); } @@ -467,7 +454,7 @@ pascal OSErr wxMacWindowDragTrackingHandler( { if ( trackingGlobals->m_currentTarget ) { - trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ); + trackingGlobals->m_currentTarget->SetCurrentDragPasteboard( pasteboard ); result = trackingGlobals->m_currentTarget->OnDragOver( localx, localy, result ); } } @@ -517,7 +504,7 @@ pascal OSErr wxMacWindowDragTrackingHandler( if (trackingGlobals->m_currentTarget) { - trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ); + trackingGlobals->m_currentTarget->SetCurrentDragPasteboard( pasteboard ); trackingGlobals->m_currentTarget->OnLeave(); #ifndef __LP64__ HideDragHilite( theDrag ); @@ -545,7 +532,9 @@ pascal OSErr wxMacWindowDragReceiveHandler( Point mouse, localMouse; int localx, localy; - trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ); + PasteboardRef pasteboard = 0; + GetDragPasteboard( theDrag, &pasteboard ); + trackingGlobals->m_currentTarget->SetCurrentDragPasteboard( pasteboard ); GetDragMouse( theDrag, &mouse, 0L ); localMouse = mouse; localx = localMouse.h; diff --git a/src/osx/carbon/font.cpp b/src/osx/carbon/font.cpp index d1c72dac07..3d295d4bb9 100644 --- a/src/osx/carbon/font.cpp +++ b/src/osx/carbon/font.cpp @@ -424,7 +424,7 @@ void wxFontRefData::MacFindFont() // QD selection algorithm is the fastest by orders of magnitude on 10.5 if ( m_faceName.IsAscii() ) { - uint8 qdstyle = 0; + uint8_t qdstyle = 0; if (m_weight == wxFONTWEIGHT_BOLD) qdstyle |= bold; if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT) diff --git a/src/osx/carbon/window.cpp b/src/osx/carbon/window.cpp index 67227916f3..f56dde4393 100644 --- a/src/osx/carbon/window.cpp +++ b/src/osx/carbon/window.cpp @@ -210,8 +210,6 @@ static pascal OSStatus wxMacWindowControlEventHandler( EventHandlerCallRef handl CGContextClearRect( cgContext, bounds ); } - - if ( thisWindow->MacDoRedraw( updateRgn , cEvent.GetTicks() ) ) result = noErr ; diff --git a/src/osx/cocoa/bmpbuttn.mm b/src/osx/cocoa/bmpbuttn.mm index ab3433bd7f..fd5f716ff8 100644 --- a/src/osx/cocoa/bmpbuttn.mm +++ b/src/osx/cocoa/bmpbuttn.mm @@ -50,7 +50,6 @@ wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer, [v setImage:bitmap.GetNSImage() ]; [v setButtonType:NSMomentaryPushInButton]; wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; return c; } diff --git a/src/osx/cocoa/button.mm b/src/osx/cocoa/button.mm index ee7070142c..7df36925de 100644 --- a/src/osx/cocoa/button.mm +++ b/src/osx/cocoa/button.mm @@ -123,24 +123,13 @@ wxSize wxButton::GetDefaultSize() @implementation wxNSButton -- (id)initWithFrame:(NSRect)frame ++ (void)initialize { - [super initWithFrame:frame]; - impl = NULL; - [self setTarget: self]; - [self setAction: @selector(clickedAction:)]; - return self; -} - -WXCOCOAIMPL_COMMON_IMPLEMENTATION - -- (void) clickedAction: (id) sender -{ - if ( impl ) + static BOOL initialized = NO; + if (!initialized) { - wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); - if ( wxpeer ) - wxpeer->OSXHandleClicked(0); + initialized = YES; + wxOSXCocoaClassAddWXMethods( self ); } } @@ -199,7 +188,6 @@ wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer, [v setButtonType:NSMomentaryPushInButton]; wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; return c; /* OSStatus err; @@ -278,6 +266,5 @@ wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer, [v setTitle:wxCFStringRef( label).AsNSString()]; [v setImagePosition:NSImageRight]; wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; return c; } diff --git a/src/osx/cocoa/checkbox.mm b/src/osx/cocoa/checkbox.mm index 8e79189ed1..b100875515 100644 --- a/src/osx/cocoa/checkbox.mm +++ b/src/osx/cocoa/checkbox.mm @@ -33,7 +33,6 @@ wxWidgetImplType* wxWidgetImpl::CreateCheckBox( wxWindowMac* wxpeer, [v setAllowsMixedState:YES]; wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; return c; } diff --git a/src/osx/cocoa/choice.mm b/src/osx/cocoa/choice.mm index 5ebf6a2445..197e4d758f 100644 --- a/src/osx/cocoa/choice.mm +++ b/src/osx/cocoa/choice.mm @@ -24,38 +24,22 @@ @interface wxNSPopUpButton : NSPopUpButton { - WXCOCOAIMPL_COMMON_MEMBERS } -WXCOCOAIMPL_COMMON_INTERFACE - -- (void) clickedAction: (id) sender; - @end @implementation wxNSPopUpButton -- (id)initWithFrame:(NSRect)frame pullsDown:(BOOL) pd ++ (void)initialize { - [super initWithFrame:frame pullsDown:pd]; - impl = NULL; - [self setTarget: self]; - [self setAction: @selector(clickedAction:)]; - return self; -} - -- (void) clickedAction: (id) sender -{ - if ( impl ) + static BOOL initialized = NO; + if (!initialized) { - wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); - if ( wxpeer ) - wxpeer->OSXHandleClicked(0); + initialized = YES; + wxOSXCocoaClassAddWXMethods( self ); } } -WXCOCOAIMPL_COMMON_IMPLEMENTATION - - (int) intValue { return [self indexOfSelectedItem]; @@ -81,7 +65,6 @@ wxWidgetImplType* wxWidgetImpl::CreateChoice( wxWindowMac* wxpeer, wxNSPopUpButton* v = [[wxNSPopUpButton alloc] initWithFrame:r pullsDown:NO]; [v setMenu: menu->GetHMenu()]; wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; return c; } diff --git a/src/osx/cocoa/filedlg.mm b/src/osx/cocoa/filedlg.mm index c212ecea36..462ee154e4 100644 --- a/src/osx/cocoa/filedlg.mm +++ b/src/osx/cocoa/filedlg.mm @@ -37,6 +37,10 @@ // implementation // ============================================================================ +// Open Items: +// - support for old style MacOS creator / type combos +// - parameter support for descending into packages as directories (setTreatsFilePackagesAsDirectories) + IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase) wxFileDialog::wxFileDialog( @@ -123,8 +127,8 @@ NSArray* GetTypesFromFilter( const wxString filter ) wxCFStringRef cfext(extension); [types addObject: (NSString*)cfext.AsNSString() ]; #if 0 + // add support for classic fileType / creator here wxUint32 fileType, creator; - // extension -> mactypes #endif } @@ -152,7 +156,10 @@ int wxFileDialog::ShowModal() // makes things more convenient: [sPanel setCanCreateDirectories:YES]; [sPanel setMessage:cf.AsNSString()]; + // if we should be able to descend into pacakges we must somehow + // be able to pass this in [sPanel setTreatsFilePackagesAsDirectories:NO]; + [sPanel setCanSelectHiddenExtension:YES]; if ( HasFlag(wxFD_OVERWRITE_PROMPT) ) { @@ -206,181 +213,4 @@ int wxFileDialog::ShowModal() return result; } -#if 0 - - wxASSERT(CreateBase(parent,wxID_ANY,pos,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr)); - - if ( parent ) - parent->AddChild(this); - - m_cocoaNSWindow = nil; - m_cocoaNSView = nil; - - //Init the wildcard array - m_wildcards = [[NSMutableArray alloc] initWithCapacity:0]; - - //If the user requests to save - use a NSSavePanel - //else use a NSOpenPanel - if (HasFlag(wxFD_SAVE)) - { - SetNSPanel([NSSavePanel savePanel]); - - [GetNSSavePanel() setTitle:wxNSStringWithWxString(message)]; - - [GetNSSavePanel() setPrompt:@"Save"]; - [GetNSSavePanel() setTreatsFilePackagesAsDirectories:YES]; - [GetNSSavePanel() setCanSelectHiddenExtension:YES]; - -// Cached as per-app in obj-c -// [GetNSSavePanel() setExtensionHidden:YES]; - - // - // NB: Note that only Panther supports wildcards - // with save dialogs - not that wildcards in save - // dialogs are all that useful, anyway :) - // - } - else //m_dialogStyle & wxFD_OPEN - { - SetNSPanel([NSOpenPanel openPanel]); - [m_cocoaNSWindow setTitle:wxNSStringWithWxString(message)]; - - [(NSOpenPanel*)m_cocoaNSWindow setAllowsMultipleSelection:(HasFlag(wxFD_MULTIPLE))]; - [(NSOpenPanel*)m_cocoaNSWindow setResolvesAliases:YES]; - [(NSOpenPanel*)m_cocoaNSWindow setCanChooseFiles:YES]; - [(NSOpenPanel*)m_cocoaNSWindow setCanChooseDirectories:NO]; - [GetNSSavePanel() setPrompt:@"Open"]; - - //convert wildcards - open panel only takes file extensions - - //no actual wildcards here :) - size_t lastwcpos = 0; - bool bDescription = true; - size_t i; - for(i = wildCard.find('|'); - i != wxString::npos; - i = wildCard.find('|', lastwcpos+1)) - { - size_t oldi = i; - - if(!bDescription) - { - bDescription = !bDescription; - - //work backwards looking for a period - while(i != lastwcpos && wildCard[--i] != '.') {} - - if(i == lastwcpos) - { - //no extension - can't use this wildcard - lastwcpos = oldi; - continue; - } - - [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))]; - } - else - bDescription = !bDescription; - lastwcpos = oldi; - } - - if (!bDescription) - { - //get last wildcard - size_t oldi = wildCard.length(); - i = oldi; - - //work backwards looking for a period - while(i != lastwcpos && wildCard[--i] != '.') {} - - if(i != lastwcpos) - [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))]; - } - - if ([m_wildcards count] == 0) - { - [m_wildcards release]; - m_wildcards = nil; - } - } -} - -wxFileDialog::~wxFileDialog() -{ - [m_wildcards release]; -} - -void wxFileDialog::GetPaths(wxArrayString& paths) const -{ - paths.Empty(); - - wxString dir(m_dir); - if ( m_dir.Last() != _T('\\') ) - dir += _T('\\'); - - size_t count = m_fileNames.GetCount(); - for ( size_t n = 0; n < count; n++ ) - { - if (wxFileName(m_fileNames[n]).IsAbsolute()) - paths.Add(m_fileNames[n]); - else - paths.Add(dir + m_fileNames[n]); - } -} - -void wxFileDialog::GetFilenames(wxArrayString& files) const -{ - files = m_fileNames; -} - -void wxFileDialog::SetPath(const wxString& path) -{ - wxString ext; - wxFileName::SplitPath(path, &m_dir, &m_fileName, &ext); - if ( !ext.empty() ) - m_fileName << _T('.') << ext; -} - -int wxFileDialog::ShowModal() -{ - wxAutoNSAutoreleasePool thePool; - - m_fileNames.Empty(); - - int nResult; - - if (HasFlag(wxFD_SAVE)) - { - nResult = [GetNSSavePanel() - runModalForDirectory:wxNSStringWithWxString(m_dir) - file:wxNSStringWithWxString(m_fileName)]; - - if (nResult == NSOKButton) - { - m_fileNames.Add(wxStringWithNSString([GetNSSavePanel() filename])); - m_path = m_fileNames[0]; - } - } - else //m_dialogStyle & wxFD_OPEN - { - nResult = [(NSOpenPanel*)m_cocoaNSWindow - runModalForDirectory:wxNSStringWithWxString(m_dir) - file:wxNSStringWithWxString(m_fileName) - types:m_wildcards]; - - if (nResult == NSOKButton) - { - for(unsigned i = 0; i < [[(NSOpenPanel*)m_cocoaNSWindow filenames] count]; ++i) - { - m_fileNames.Add(wxStringWithNSString([[(NSOpenPanel*)m_cocoaNSWindow filenames] objectAtIndex:(i)])); - } - - m_path = m_fileNames[0]; - } - } - - return nResult == NSOKButton ? wxID_OK : wxID_CANCEL; -} - -#endif - #endif // wxUSE_FILEDLG diff --git a/src/osx/cocoa/gauge.mm b/src/osx/cocoa/gauge.mm index 2620e53035..895a70cae8 100644 --- a/src/osx/cocoa/gauge.mm +++ b/src/osx/cocoa/gauge.mm @@ -19,24 +19,22 @@ @interface wxNSProgressIndicator : NSProgressIndicator { - WXCOCOAIMPL_COMMON_MEMBERS } -WXCOCOAIMPL_COMMON_INTERFACE - @end @implementation wxNSProgressIndicator -- (id)initWithFrame:(NSRect)frame ++ (void)initialize { - [super initWithFrame:frame]; - impl = NULL; - return self; + static BOOL initialized = NO; + if (!initialized) + { + initialized = YES; + wxOSXCocoaClassAddWXMethods( self ); + } } -WXCOCOAIMPL_COMMON_IMPLEMENTATION - @end class wxOSXGaugeCocoaImpl : public wxWidgetCocoaImpl @@ -98,7 +96,6 @@ wxWidgetImplType* wxWidgetImpl::CreateGauge( wxWindowMac* wxpeer, [v setIndeterminate:FALSE]; [v setDoubleValue: (double) value]; wxWidgetCocoaImpl* c = new wxOSXGaugeCocoaImpl( wxpeer, v ); - [v setImplementation:c]; return c; } diff --git a/src/osx/cocoa/glcanvas.mm b/src/osx/cocoa/glcanvas.mm index e088ede061..83c99ce833 100644 --- a/src/osx/cocoa/glcanvas.mm +++ b/src/osx/cocoa/glcanvas.mm @@ -229,27 +229,23 @@ bool wxGLContext::SetCurrent(const wxGLCanvas& win) const @interface wxNSCustomOpenGLView : NSView { - WXCOCOAIMPL_COMMON_MEMBERS NSOpenGLContext* context; } -- (id)initWithFrame:(NSRect)frame; - -WXCOCOAIMPL_COMMON_INTERFACE - @end @implementation wxNSCustomOpenGLView -- (id)initWithFrame:(NSRect)frame ++ (void)initialize { - [super initWithFrame:frame]; - impl = NULL; - return self; + static BOOL initialized = NO; + if (!initialized) + { + initialized = YES; + wxOSXCocoaClassAddWXMethods( self ); + } } -WXCOCOAIMPL_COMMON_IMPLEMENTATION - - (BOOL)isOpaque { return YES; @@ -279,7 +275,6 @@ bool wxGLCanvas::Create(wxWindow *parent, NSRect r = wxOSXGetFrameForControl( this, pos , size ) ; wxNSCustomOpenGLView* v = [[wxNSCustomOpenGLView alloc] initWithFrame:r]; m_peer = new wxWidgetCocoaImpl( this, v ); - [v setImplementation:m_peer]; MacPostControlCreate(pos, size) ; */ diff --git a/src/osx/cocoa/listbox.mm b/src/osx/cocoa/listbox.mm index 270d3ba4df..01bb7fc73f 100644 --- a/src/osx/cocoa/listbox.mm +++ b/src/osx/cocoa/listbox.mm @@ -14,6 +14,7 @@ #if wxUSE_LISTBOX #include "wx/listbox.h" +#include "wx/dnd.h" #ifndef WX_PRECOMP #include "wx/log.h" @@ -54,14 +55,8 @@ class wxListWidgetCocoaImpl; @interface wxNSTableView : NSTableView { - wxListWidgetCocoaImpl* impl; } -- (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation; -- (wxListWidgetCocoaImpl*) implementation; -- (void)clickedAction: (id) sender; -- (void)doubleClickedAction: (id) sender; - @end // @@ -147,6 +142,9 @@ public : } virtual void UpdateLine( unsigned int n, wxListWidgetColumn* col = NULL ) ; virtual void UpdateLineToEnd( unsigned int n); + + virtual void clickedAction(WXWidget slf, void* _cmd, void *sender); + virtual void doubleClickedAction(void* _cmd); protected : wxNSTableView* m_tableView ; @@ -274,55 +272,13 @@ protected: @implementation wxNSTableView -- (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation ++ (void)initialize { - impl = theImplementation; -} - -- (wxListWidgetCocoaImpl*) implementation -{ - return impl; -} - -- (id) init -{ - [super init]; - impl = NULL; - [self setTarget: self]; - [self setAction: @selector(clickedAction:)]; - [self setDoubleAction: @selector(doubleClickedAction:)]; - return self; -} - -- (void) clickedAction: (id) sender -{ - if ( impl ) + static BOOL initialized = NO; + if (!initialized) { - wxListBox *list = static_cast ( impl->GetWXPeer()); - wxCHECK_RET( list != NULL , wxT("Listbox expected")); - - wxCommandEvent event( wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() ); - - int sel = [self clickedRow]; - if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?) - return; - - list->HandleLineEvent( sel, false ); - } -} - -- (void) doubleClickedAction: (id) sender -{ - if ( impl ) - { - wxListBox *list = static_cast ( impl->GetWXPeer()); - wxCHECK_RET( list != NULL , wxT("Listbox expected")); - - int sel = [self clickedRow]; - if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?) - return; - - list->HandleLineEvent( sel, true ); + initialized = YES; + wxOSXCocoaClassAddWXMethods( self ); } } @@ -335,6 +291,7 @@ protected: wxListWidgetCocoaImpl::wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ) : wxWidgetCocoaImpl( peer, view ), m_tableView(tableview), m_dataSource(data) { + InstallEventHandler( tableview ); } wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl() @@ -514,6 +471,31 @@ void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int n) [m_tableView reloadData]; } +void wxListWidgetCocoaImpl::clickedAction(WXWidget slf,void* _cmd, void *sender) +{ + wxListBox *list = static_cast ( GetWXPeer()); + wxCHECK_RET( list != NULL , wxT("Listbox expected")); + + wxCommandEvent event( wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() ); + + int sel = [m_tableView clickedRow]; + if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?) + return; + + list->HandleLineEvent( sel, false ); +} + +void wxListWidgetCocoaImpl::doubleClickedAction(void* _cmd) +{ + wxListBox *list = static_cast ( GetWXPeer()); + wxCHECK_RET( list != NULL , wxT("Listbox expected")); + + int sel = [m_tableView clickedRow]; + if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?) + return; + + list->HandleLineEvent( sel, true ); +} // accessing content @@ -555,7 +537,11 @@ wxWidgetImplType* wxWidgetImpl::CreateListBox( wxWindowMac* wxpeer, wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init]; [tableview setDataSource:ds]; wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds ); - [tableview setImplementation:c]; + + // temporary hook for dnd + [tableview registerForDraggedTypes:[NSArray arrayWithObjects: + NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]]; + [ds setImplementation:c]; return c; } diff --git a/src/osx/cocoa/notebook.mm b/src/osx/cocoa/notebook.mm index 2c47ce8133..36abc047db 100644 --- a/src/osx/cocoa/notebook.mm +++ b/src/osx/cocoa/notebook.mm @@ -41,11 +41,8 @@ @interface wxNSTabView : NSTabView { - WXCOCOAIMPL_COMMON_MEMBERS } -WXCOCOAIMPL_COMMON_INTERFACE - @end @implementation wxTabViewController @@ -59,7 +56,8 @@ WXCOCOAIMPL_COMMON_INTERFACE - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem { wxNSTabView* view = (wxNSTabView*) tabView; - wxWidgetCocoaImpl* viewimpl = [view implementation]; + wxWidgetCocoaImpl* viewimpl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( view ); + if ( viewimpl ) { // wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer(); @@ -71,7 +69,7 @@ WXCOCOAIMPL_COMMON_INTERFACE { wxNSTabView* view = (wxNSTabView*) tabView; - wxWidgetCocoaImpl* viewimpl = [view implementation]; + wxWidgetCocoaImpl* viewimpl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( view ); if ( viewimpl ) { wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer(); @@ -83,7 +81,15 @@ WXCOCOAIMPL_COMMON_INTERFACE @implementation wxNSTabView -WXCOCOAIMPL_COMMON_IMPLEMENTATION ++ (void)initialize +{ + static BOOL initialized = NO; + if (!initialized) + { + initialized = YES; + wxOSXCocoaClassAddWXMethods( self ); + } +} @end @@ -244,7 +250,6 @@ wxWidgetImplType* wxWidgetImpl::CreateTabView( wxWindowMac* wxpeer, wxNSTabView* v = [[wxNSTabView alloc] initWithFrame:r]; [v setTabViewType:tabstyle]; wxWidgetCocoaImpl* c = new wxCocoaTabView( wxpeer, v ); - [v setImplementation:c]; [v setDelegate: controller]; return c; } diff --git a/src/osx/cocoa/radiobut.mm b/src/osx/cocoa/radiobut.mm index 681f6cf9c9..1ca43e6615 100644 --- a/src/osx/cocoa/radiobut.mm +++ b/src/osx/cocoa/radiobut.mm @@ -31,7 +31,6 @@ wxWidgetImplType* wxWidgetImpl::CreateRadioButton( wxWindowMac* wxpeer, [v setButtonType:NSRadioButton]; wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; return c; } diff --git a/src/osx/cocoa/scrolbar.mm b/src/osx/cocoa/scrolbar.mm index 74168cf6e0..533f5e92dc 100644 --- a/src/osx/cocoa/scrolbar.mm +++ b/src/osx/cocoa/scrolbar.mm @@ -23,80 +23,18 @@ @interface wxNSScroller : NSScroller { - WXCOCOAIMPL_COMMON_MEMBERS } - -WXCOCOAIMPL_COMMON_INTERFACE - - - (void) clickedAction: (id) sender; - @end @implementation wxNSScroller -- (id)initWithFrame:(NSRect)frame ++ (void)initialize { - [super initWithFrame:frame]; - impl = NULL; - [self setTarget: self]; - [self setAction: @selector(clickedAction:)]; - return self; -} - -WXCOCOAIMPL_COMMON_IMPLEMENTATION_NO_MOUSEDOWN - -// we will have a mouseDown, then in the native -// implementation of mouseDown the tracking code -// is calling clickedAction, therefore we wire this -// to thumbtrack and only after super mouseDown -// returns we will call the thumbrelease - -- (void) clickedAction: (id) sender -{ - if ( impl ) + static BOOL initialized = NO; + if (!initialized) { - wxEventType scrollEvent = wxEVT_NULL; - switch ([self hitPart]) - { - case NSScrollerIncrementLine: - scrollEvent = wxEVT_SCROLL_LINEDOWN; - break; - case NSScrollerIncrementPage: - scrollEvent = wxEVT_SCROLL_PAGEDOWN; - break; - case NSScrollerDecrementLine: - scrollEvent = wxEVT_SCROLL_LINEUP; - break; - case NSScrollerDecrementPage: - scrollEvent = wxEVT_SCROLL_PAGEUP; - break; - case NSScrollerKnob: - case NSScrollerKnobSlot: - scrollEvent = wxEVT_SCROLL_THUMBTRACK; - break; - case NSScrollerNoPart: - default: - return; - } - - wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); - if ( wxpeer ) - wxpeer->TriggerScrollEvent(scrollEvent); - } -} - --(void)mouseDown:(NSEvent *)event -{ - if ( !impl->DoHandleMouseEvent(event) ) - [super mouseDown:event]; - - // send a release event in case we've been tracking the thumb - NSScrollerPart hit = [self hitPart]; - if ( impl && (hit == NSScrollerKnob || hit == NSScrollerKnobSlot) ) - { - wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); - if ( wxpeer ) - wxpeer->TriggerScrollEvent(wxEVT_SCROLL_THUMBRELEASE); + initialized = YES; + wxOSXCocoaClassAddWXMethods(self); } } @@ -118,7 +56,7 @@ public : void SetScrollThumb( wxInt32 value, wxInt32 thumbSize ) { double v = ((double) value)/m_maximum; - double t = ((double) thumbSize)/m_maximum; + double t = ((double) thumbSize)/(m_maximum+thumbSize); #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 [(wxNSScroller*) m_osxView setFloatValue:v knobProportion:t]; #else @@ -127,19 +65,76 @@ public : #endif } - wxInt32 GetValue() const + virtual wxInt32 GetValue() const { return [(wxNSScroller*) m_osxView floatValue] * m_maximum; } - wxInt32 GetMaximum() const + virtual wxInt32 GetMaximum() const { return m_maximum; } + + virtual void clickedAction(WXWidget slf, void* _cmd, void *sender); + virtual void mouseEvent(WX_NSEvent event, WXWidget slf, void* _cmd); protected: wxInt32 m_maximum; }; +// we will have a mouseDown, then in the native +// implementation of mouseDown the tracking code +// is calling clickedAction, therefore we wire this +// to thumbtrack and only after super mouseDown +// returns we will call the thumbrelease + +void wxOSXScrollBarCocoaImpl::clickedAction( WXWidget slf, void *_cmd, void *sender) +{ + wxEventType scrollEvent = wxEVT_NULL; + switch ([(NSScroller*)m_osxView hitPart]) + { + case NSScrollerIncrementLine: + scrollEvent = wxEVT_SCROLL_LINEDOWN; + break; + case NSScrollerIncrementPage: + scrollEvent = wxEVT_SCROLL_PAGEDOWN; + break; + case NSScrollerDecrementLine: + scrollEvent = wxEVT_SCROLL_LINEUP; + break; + case NSScrollerDecrementPage: + scrollEvent = wxEVT_SCROLL_PAGEUP; + break; + case NSScrollerKnob: + case NSScrollerKnobSlot: + scrollEvent = wxEVT_SCROLL_THUMBTRACK; + break; + case NSScrollerNoPart: + default: + return; + } + + wxWindow* wxpeer = (wxWindow*) GetWXPeer(); + if ( wxpeer ) + wxpeer->TriggerScrollEvent(scrollEvent); +} + +void wxOSXScrollBarCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd) +{ + wxWidgetCocoaImpl::mouseEvent(event, slf, _cmd); + + // send a release event in case we've been tracking the thumb + if ( strcmp( sel_getName((SEL) _cmd) , "mouseDown:") == 0 ) + { + NSScrollerPart hit = [(NSScroller*)m_osxView hitPart]; + if ( (hit == NSScrollerKnob || hit == NSScrollerKnobSlot) ) + { + wxWindow* wxpeer = (wxWindow*) GetWXPeer(); + if ( wxpeer ) + wxpeer->OSXHandleClicked(0); + } + } +} + wxWidgetImplType* wxWidgetImpl::CreateScrollBar( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, @@ -152,7 +147,6 @@ wxWidgetImplType* wxWidgetImpl::CreateScrollBar( wxWindowMac* wxpeer, wxNSScroller* v = [[wxNSScroller alloc] initWithFrame:r]; wxWidgetCocoaImpl* c = new wxOSXScrollBarCocoaImpl( wxpeer, v ); - [v setImplementation:c]; [v setEnabled:YES]; return c; } diff --git a/src/osx/cocoa/slider.mm b/src/osx/cocoa/slider.mm index cb94d5cf52..a627ce9feb 100644 --- a/src/osx/cocoa/slider.mm +++ b/src/osx/cocoa/slider.mm @@ -18,27 +18,38 @@ @interface wxNSSlider : NSSlider { - WXCOCOAIMPL_COMMON_MEMBERS } - -WXCOCOAIMPL_COMMON_INTERFACE - - - (void) clickedAction: (id) sender; - @end @implementation wxNSSlider -- (id)initWithFrame:(NSRect)frame ++ (void)initialize { - [super initWithFrame:frame]; - impl = NULL; - [self setTarget: self]; - [self setAction: @selector(clickedAction:)]; - return self; + static BOOL initialized = NO; + if (!initialized) + { + initialized = YES; + wxOSXCocoaClassAddWXMethods(self); + } } -WXCOCOAIMPL_COMMON_IMPLEMENTATION_NO_MOUSEDOWN +@end + +class wxSliderCocoaImpl : public wxWidgetCocoaImpl +{ +public : + wxSliderCocoaImpl(wxWindowMac* peer , WXWidget w) : + wxWidgetCocoaImpl(peer, w) + { + } + + ~wxSliderCocoaImpl() + { + } + + virtual void clickedAction(WXWidget slf, void* _cmd, void *sender); + virtual void mouseEvent(WX_NSEvent event, WXWidget slf, void* _cmd); +}; // we will have a mouseDown, then in the native // implementation of mouseDown the tracking code @@ -46,30 +57,26 @@ WXCOCOAIMPL_COMMON_IMPLEMENTATION_NO_MOUSEDOWN // to thumbtrack and only after super mouseDown // returns we will call the thumbrelease -- (void) clickedAction: (id) sender +void wxSliderCocoaImpl::clickedAction( WXWidget slf, void *_cmd, void *sender) { - if ( impl ) - { - wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); - if ( wxpeer ) - wxpeer->TriggerScrollEvent(wxEVT_SCROLL_THUMBTRACK); - } + wxWindow* wxpeer = (wxWindow*) GetWXPeer(); + if ( wxpeer ) + wxpeer->TriggerScrollEvent(wxEVT_SCROLL_THUMBTRACK); } --(void)mouseDown:(NSEvent *)event +void wxSliderCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd) { - if ( !impl->DoHandleMouseEvent(event) ) - [super mouseDown:event]; - - if ( impl ) + wxWidgetCocoaImpl::mouseEvent(event, slf, _cmd); + + if ( strcmp( sel_getName((SEL) _cmd) , "mouseDown:") == 0 ) { - wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); + wxWindow* wxpeer = (wxWindow*) GetWXPeer(); if ( wxpeer ) wxpeer->OSXHandleClicked(0); } } -@end + wxWidgetImplType* wxWidgetImpl::CreateSlider( wxWindowMac* wxpeer, wxWindowMac* parent, @@ -102,8 +109,7 @@ wxWidgetImplType* wxWidgetImpl::CreateSlider( wxWindowMac* wxpeer, [v setMinValue: minimum]; [v setMaxValue: maximum]; [v setFloatValue: (double) value]; - wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; + wxWidgetCocoaImpl* c = new wxSliderCocoaImpl( wxpeer, v ); return c; } diff --git a/src/osx/cocoa/spinbutt.mm b/src/osx/cocoa/spinbutt.mm index da57bbea78..2104c0e7c2 100644 --- a/src/osx/cocoa/spinbutt.mm +++ b/src/osx/cocoa/spinbutt.mm @@ -18,68 +18,80 @@ @interface wxNSStepper : NSStepper { - WXCOCOAIMPL_COMMON_MEMBERS - int formerValue; } - -WXCOCOAIMPL_COMMON_INTERFACE - - - (void) clickedAction: (id) sender; - @end @implementation wxNSStepper -- (id)initWithFrame:(NSRect)frame ++ (void)initialize { - [super initWithFrame:frame]; - impl = NULL; - formerValue = 0; - [self setTarget: self]; - [self setAction: @selector(clickedAction:)]; - return self; -} - -- (void) clickedAction: (id) sender -{ - if ( impl ) + static BOOL initialized = NO; + if (!initialized) { - wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); - if ( wxpeer ) - { - // because wx expects to be able to veto - // a change we must revert the value change - // and expose it - int currentValue = [self intValue]; - [self setIntValue:formerValue]; - int inc = currentValue-formerValue; - - // adjust for wrap arounds - if ( inc > 1 ) - inc = -1; - else if (inc < -1 ) - inc = 1; - - if ( inc == 1 ) - wxpeer->TriggerScrollEvent(wxEVT_SCROLL_LINEUP); - else if ( inc == -1 ) - wxpeer->TriggerScrollEvent(wxEVT_SCROLL_LINEDOWN); - - formerValue = [self intValue]; - } + initialized = YES; + wxOSXCocoaClassAddWXMethods(self); } } --(void)mouseDown:(NSEvent *)event +@end + +class wxSpinButtonCocoaImpl : public wxWidgetCocoaImpl { - formerValue = [self intValue]; - if ( !impl->DoHandleMouseEvent(event) ) - [super mouseDown:event]; +public : + wxSpinButtonCocoaImpl(wxWindowMac* peer , WXWidget w) : + wxWidgetCocoaImpl(peer, w) + { + m_formerValue = 0; + } + + ~wxSpinButtonCocoaImpl() + { + } + + virtual void clickedAction(WXWidget slf, void* _cmd, void *sender); + virtual void mouseEvent(WX_NSEvent event, WXWidget slf, void* _cmd); +private: + int m_formerValue; +}; + +void wxSpinButtonCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd) +{ + + // send a release event in case we've been tracking the thumb + if ( strcmp( sel_getName((SEL) _cmd) , "mouseDown:") == 0 ) + { + m_formerValue = [(NSStepper*)m_osxView intValue]; + } + + wxWidgetCocoaImpl::mouseEvent(event, slf, _cmd); } -WXCOCOAIMPL_COMMON_IMPLEMENTATION_NO_MOUSEDOWN +void wxSpinButtonCocoaImpl::clickedAction( WXWidget slf, void *_cmd, void *sender) +{ + wxWindow* wxpeer = (wxWindow*) GetWXPeer(); + if ( wxpeer ) + { + // because wx expects to be able to veto + // a change we must revert the value change + // and expose it + int currentValue = [(NSStepper*)m_osxView intValue]; + [(NSStepper*)m_osxView setIntValue:m_formerValue]; + int inc = currentValue-m_formerValue; + + // adjust for wrap arounds + if ( inc > 1 ) + inc = -1; + else if (inc < -1 ) + inc = 1; + + if ( inc == 1 ) + wxpeer->TriggerScrollEvent(wxEVT_SCROLL_LINEUP); + else if ( inc == -1 ) + wxpeer->TriggerScrollEvent(wxEVT_SCROLL_LINEDOWN); -@end + m_formerValue = [(NSStepper*)m_osxView intValue]; + } +} wxWidgetImplType* wxWidgetImpl::CreateSpinButton( wxWindowMac* wxpeer, wxWindowMac* parent, @@ -102,8 +114,7 @@ wxWidgetImplType* wxWidgetImpl::CreateSpinButton( wxWindowMac* wxpeer, if ( style & wxSP_WRAP ) [v setValueWraps:YES]; - wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; + wxWidgetCocoaImpl* c = new wxSpinButtonCocoaImpl( wxpeer, v ); return c; } diff --git a/src/osx/cocoa/srchctrl.mm b/src/osx/cocoa/srchctrl.mm index c1abb250ff..861605298e 100644 --- a/src/osx/cocoa/srchctrl.mm +++ b/src/osx/cocoa/srchctrl.mm @@ -31,26 +31,30 @@ @interface wxNSSearchField : NSSearchField { - WXCOCOAIMPL_COMMON_MEMBERS } -WXCOCOAIMPL_COMMON_INTERFACE - @end @implementation wxNSSearchField ++ (void)initialize +{ + static BOOL initialized = NO; + if (!initialized) + { + initialized = YES; + wxOSXCocoaClassAddWXMethods( self ); + } +} + - (id)initWithFrame:(NSRect)frame { [super initWithFrame:frame]; - impl = NULL; [self setTarget: self]; [self setAction: @selector(searchAction:)]; return self; } -WXCOCOAIMPL_COMMON_IMPLEMENTATION - // use our common calls - (void) setTitle:(NSString *) title { @@ -59,6 +63,7 @@ WXCOCOAIMPL_COMMON_IMPLEMENTATION - (void) searchAction: (id) sender { + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); if ( impl ) { wxSearchCtrl* wxpeer = dynamic_cast( impl->GetWXPeer() ); @@ -168,7 +173,6 @@ wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxTextCtrl* wxpeer, wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v ); c->SetStringValue( str ); - [v setImplementation:c]; return c; } diff --git a/src/osx/cocoa/statbox.mm b/src/osx/cocoa/statbox.mm index 34c2f27246..6c5bff4a67 100644 --- a/src/osx/cocoa/statbox.mm +++ b/src/osx/cocoa/statbox.mm @@ -18,7 +18,15 @@ @implementation wxNSBox -WXCOCOAIMPL_COMMON_IMPLEMENTATION_NOT_FLIPPED ++ (void)initialize +{ + static BOOL initialized = NO; + if (!initialized) + { + initialized = YES; + wxOSXCocoaClassAddWXMethods( self ); + } +} @end @@ -34,7 +42,7 @@ wxWidgetImplType* wxWidgetImpl::CreateGroupBox( wxWindowMac* wxpeer, NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; wxNSBox* v = [[wxNSBox alloc] initWithFrame:r]; wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; + c->SetFlipped(false); return c; } diff --git a/src/osx/cocoa/statline.mm b/src/osx/cocoa/statline.mm index f57c1bb28f..fdff4b0c27 100644 --- a/src/osx/cocoa/statline.mm +++ b/src/osx/cocoa/statline.mm @@ -44,7 +44,6 @@ wxWidgetImplType* wxWidgetImpl::CreateStaticLine( wxWindowMac* wxpeer, NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; wxNSBox* v = [[wxNSBox alloc] initWithFrame:r]; wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; return c; } diff --git a/src/osx/cocoa/stattext.mm b/src/osx/cocoa/stattext.mm index b3d5f89ebc..efb6e56343 100644 --- a/src/osx/cocoa/stattext.mm +++ b/src/osx/cocoa/stattext.mm @@ -88,7 +88,6 @@ wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer, [v setDrawsBackground:NO]; wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; return c; /* Rect bounds = wxMacGetBoundsForControl( wxpeer, pos, size ); diff --git a/src/osx/cocoa/textctrl.mm b/src/osx/cocoa/textctrl.mm index 3e5b8d87e7..a9898bf349 100644 --- a/src/osx/cocoa/textctrl.mm +++ b/src/osx/cocoa/textctrl.mm @@ -51,12 +51,19 @@ @implementation wxNSTextField -WXCOCOAIMPL_COMMON_IMPLEMENTATION ++ (void)initialize +{ + static BOOL initialized = NO; + if (!initialized) + { + initialized = YES; + wxOSXCocoaClassAddWXMethods( self ); + } +} - (id)initWithFrame:(NSRect)frame { [super initWithFrame:frame]; - impl = NULL; [self setDelegate: self]; [self setTarget: self]; // [self setAction: @selector(enterAction:)]; @@ -190,7 +197,6 @@ wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer, //[v setDrawsBackground:NO]; wxWidgetCocoaImpl* c = new wxNSTextFieldControl( wxpeer, v ); - [v setImplementation:c]; return c; } diff --git a/src/osx/cocoa/tglbtn.mm b/src/osx/cocoa/tglbtn.mm index 5db9b8e456..f5d052809e 100644 --- a/src/osx/cocoa/tglbtn.mm +++ b/src/osx/cocoa/tglbtn.mm @@ -40,7 +40,6 @@ wxWidgetImplType* wxWidgetImpl::CreateToggleButton( wxWindowMac* wxpeer, [v setBezelStyle:NSRoundedBezelStyle]; [v setButtonType:NSOnOffButton]; wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; return c; } @@ -59,7 +58,6 @@ wxWidgetImplType* wxWidgetImpl::CreateBitmapToggleButton( wxWindowMac* wxpeer, [v setBezelStyle:NSRegularSquareBezelStyle]; [v setButtonType:NSOnOffButton]; wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; return c; } diff --git a/src/osx/cocoa/window.mm b/src/osx/cocoa/window.mm index 44b70b7f01..fafa57b77e 100644 --- a/src/osx/cocoa/window.mm +++ b/src/osx/cocoa/window.mm @@ -17,13 +17,19 @@ #endif #ifdef __WXMAC__ -#include "wx/osx/private.h" + #include "wx/osx/private.h" #endif #if wxUSE_CARET #include "wx/caret.h" #endif +#if wxUSE_DRAG_AND_DROP + #include "wx/dnd.h" +#endif + +#include + NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin ) { int x, y, w, h ; @@ -37,19 +43,13 @@ NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const @interface wxNSView : NSView { - WXCOCOAIMPL_COMMON_MEMBERS } -- (void)drawRect: (NSRect) rect; - -WXCOCOAIMPL_COMMON_INTERFACE - - (BOOL) canBecomeKeyView; @end // wxNSView @interface NSView(PossibleMethods) -- (void)setImplementation:(wxWidgetCocoaImpl *)theImplementation; - (void)setTitle:(NSString *)aString; - (void)setStringValue:(NSString *)aString; - (void)setIntValue:(int)anInt; @@ -70,6 +70,10 @@ WXCOCOAIMPL_COMMON_INTERFACE - (void)setControlSize:(NSControlSize)size; - (id)contentView; + +- (void)setTarget:(id)anObject; +- (void)setAction:(SEL)aSelector; +- (void)setDoubleAction:(SEL)aSelector; @end long wxOSXTranslateCocoaKey( int unichar ) @@ -336,60 +340,16 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent ) @implementation wxNSView -#define OSX_DEBUG_DRAWING 0 - -- (void)drawRect: (NSRect) rect ++ (void)initialize { - if ( impl ) + static BOOL initialized = NO; + if (!initialized) { - CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort]; - CGContextSaveGState( context ); -#if OSX_DEBUG_DRAWING - CGContextBeginPath( context ); - CGContextMoveToPoint(context, 0, 0); - NSRect bounds = [self bounds]; - CGContextAddLineToPoint(context, 10, 0); - CGContextMoveToPoint(context, 0, 0); - CGContextAddLineToPoint(context, 0, 10); - CGContextMoveToPoint(context, bounds.size.width, bounds.size.height); - CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10); - CGContextMoveToPoint(context, bounds.size.width, bounds.size.height); - CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height); - CGContextClosePath( context ); - CGContextStrokePath(context); -#endif - - if ( [ self isFlipped ] == NO ) - { - CGContextTranslateCTM( context, 0, [self bounds].size.height ); - CGContextScaleCTM( context, 1, -1 ); - } - - wxRegion updateRgn; - const NSRect *rects; - NSInteger count; - - [self getRectsBeingDrawn:&rects count:&count]; - for ( int i = 0 ; i < count ; ++i ) - { - updateRgn.Union(wxFromNSRect(self, rects[i]) ); - } - - wxWindow* wxpeer = impl->GetWXPeer(); - wxpeer->GetUpdateRegion() = updateRgn; - wxpeer->MacSetCGContextRef( context ); - - wxPaintEvent event; - event.SetTimestamp(0); // todo - event.SetEventObject(wxpeer); - wxpeer->HandleWindowEvent(event); - - CGContextRestoreGState( context ); + initialized = YES; + wxOSXCocoaClassAddWXMethods( self ); } } -WXCOCOAIMPL_COMMON_IMPLEMENTATION - - (BOOL) canBecomeKeyView { return YES; @@ -397,26 +357,533 @@ WXCOCOAIMPL_COMMON_IMPLEMENTATION @end // wxNSView +// +// event handlers +// + +#if wxUSE_DRAG_AND_DROP + +// see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html +// for details on the NSPasteboard -> PasteboardRef conversion + +NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id sender ) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return NSDragOperationNone; + + return impl->draggingEntered(sender, self, _cmd); +} + +void wxOSX_draggingExited( id self, SEL _cmd, id sender ) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return ; + + return impl->draggingExited(sender, self, _cmd); +} + +NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id sender ) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return NSDragOperationNone; + + return impl->draggingUpdated(sender, self, _cmd); +} + +BOOL wxOSX_performDragOperation( id self, SEL _cmd, id sender ) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return NSDragOperationNone; + + return impl->performDragOperation(sender, self, _cmd) ? YES:NO ; +} + +void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return; + + impl->mouseEvent(event, self, _cmd); +} + +void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return; + + impl->keyEvent(event, self, _cmd); +} + +BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return NO; + + return impl->performKeyEquivalent(event, self, _cmd); +} + +BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return NO; + + return impl->becomeFirstResponder(self, _cmd); +} + +BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return NO; + + return impl->resignFirstResponder(self, _cmd); +} + +void wxOSX_resetCursorRects(NSView* self, SEL _cmd) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return; + + impl->resetCursorRects(self, _cmd); +} + +BOOL wxOSX_isFlipped(NSView* self, SEL _cmd) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return NO; + + return impl->isFlipped(self, _cmd) ? YES:NO; +} + +void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return; + + return impl->drawRect(&rect, self, _cmd); +} + +void wxOSX_clickedAction(NSView* self, SEL _cmd, id sender) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return; + + impl->clickedAction(self, _cmd, sender); +} + +void wxOSX_doubleClickedAction(NSView* self, SEL _cmd, id sender) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return; + + impl->doubleClickedAction(self, _cmd, sender); +} + +unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd) +{ + id sender = (id ) s; + NSPasteboard *pboard = [sender draggingPasteboard]; + NSDragOperation sourceDragMask = [sender draggingSourceOperationMask]; + + wxWindow* wxpeer = GetWXPeer(); + if ( wxpeer == NULL ) + return NSDragOperationNone; + + wxDropTarget* target = wxpeer->GetDropTarget(); + if ( target == NULL ) + return NSDragOperationNone; + + wxDragResult result = wxDragNone; + wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] ); + + if ( sourceDragMask & NSDragOperationLink ) + result = wxDragLink; + else if ( sourceDragMask & NSDragOperationCopy ) + result = wxDragCopy; + else if ( sourceDragMask & NSDragOperationMove ) + result = wxDragMove; + + PasteboardRef pboardRef; + PasteboardCreate((CFStringRef)[pboard name], &pboardRef); + target->SetCurrentDragPasteboard(pboardRef); + result = target->OnEnter(pt.x, pt.y, result); + CFRelease(pboardRef); + + NSDragOperation nsresult = NSDragOperationNone; + switch (result ) + { + case wxDragLink: + nsresult = NSDragOperationLink; + case wxDragMove: + nsresult = NSDragOperationMove; + case wxDragCopy: + nsresult = NSDragOperationCopy; + default : + break; + } + return nsresult; +} + +void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd) +{ + id sender = (id ) s; + NSPasteboard *pboard = [sender draggingPasteboard]; + + wxWindow* wxpeer = GetWXPeer(); + if ( wxpeer == NULL ) + return; + + wxDropTarget* target = wxpeer->GetDropTarget(); + if ( target == NULL ) + return; + + PasteboardRef pboardRef; + PasteboardCreate((CFStringRef)[pboard name], &pboardRef); + target->SetCurrentDragPasteboard(pboardRef); + target->OnLeave(); + CFRelease(pboardRef); + } + +unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd) +{ + id sender = (id ) s; + NSPasteboard *pboard = [sender draggingPasteboard]; + NSDragOperation sourceDragMask = [sender draggingSourceOperationMask]; + + wxWindow* wxpeer = GetWXPeer(); + if ( wxpeer == NULL ) + return NSDragOperationNone; + + wxDropTarget* target = wxpeer->GetDropTarget(); + if ( target == NULL ) + return NSDragOperationNone; + + wxDragResult result = wxDragNone; + wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] ); + + if ( sourceDragMask & NSDragOperationLink ) + result = wxDragLink; + else if ( sourceDragMask & NSDragOperationCopy ) + result = wxDragCopy; + else if ( sourceDragMask & NSDragOperationMove ) + result = wxDragMove; + + PasteboardRef pboardRef; + PasteboardCreate((CFStringRef)[pboard name], &pboardRef); + target->SetCurrentDragPasteboard(pboardRef); + result = target->OnDragOver(pt.x, pt.y, result); + CFRelease(pboardRef); + + NSDragOperation nsresult = NSDragOperationNone; + switch (result ) + { + case wxDragLink: + nsresult = NSDragOperationLink; + case wxDragMove: + nsresult = NSDragOperationMove; + case wxDragCopy: + nsresult = NSDragOperationCopy; + default : + break; + } + return nsresult; +} + +bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd) +{ + id sender = (id ) s; + + NSPasteboard *pboard = [sender draggingPasteboard]; + NSDragOperation sourceDragMask = [sender draggingSourceOperationMask]; + + wxWindow* wxpeer = GetWXPeer(); + wxDropTarget* target = wxpeer->GetDropTarget(); + wxDragResult result = wxDragNone; + wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] ); + + if ( sourceDragMask & NSDragOperationLink ) + result = wxDragLink; + else if ( sourceDragMask & NSDragOperationCopy ) + result = wxDragCopy; + else if ( sourceDragMask & NSDragOperationMove ) + result = wxDragMove; + + PasteboardRef pboardRef; + PasteboardCreate((CFStringRef)[pboard name], &pboardRef); + target->SetCurrentDragPasteboard(pboardRef); + result = target->OnData(pt.x, pt.y, result); + CFRelease(pboardRef); + + return result != wxDragNone; +} + +#endif + +typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event); +typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event); +typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd); +typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd); +typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect); + +void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd) +{ + if ( !DoHandleMouseEvent(event) ) + { + wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; + superimpl(slf, (SEL)_cmd, event); + } +} + +void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd) +{ + if ( !DoHandleKeyEvent(event) ) + { + wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; + superimpl(slf, (SEL)_cmd, event); + } +} + +bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd) +{ + if ( !DoHandleKeyEvent(event) ) + { + wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; + return superimpl(slf, (SEL)_cmd, event); + } + + return YES; +} + +bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd) +{ + wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; + BOOL r = superimpl(slf, (SEL)_cmd); + if ( r ) + DoNotifyFocusEvent( true ); + return r; +} + +bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd) +{ + wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; + BOOL r = superimpl(slf, (SEL)_cmd); + if ( r ) + DoNotifyFocusEvent( false ); + return r; +} + +void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd) +{ + wxWindow* wxpeer = GetWXPeer(); + if ( wxpeer ) + { + NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR(); + if (cursor == NULL) + { + wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; + superimpl(slf, (SEL)_cmd); + } + else + [slf addCursorRect: [slf bounds] + cursor: cursor]; + } +} + +bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd) +{ + return m_isFlipped; +} + + +#define OSX_DEBUG_DRAWING 0 + +void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd) +{ + CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort]; + CGContextSaveGState( context ); + +#if OSX_DEBUG_DRAWING + CGContextBeginPath( context ); + CGContextMoveToPoint(context, 0, 0); + NSRect bounds = [self bounds]; + CGContextAddLineToPoint(context, 10, 0); + CGContextMoveToPoint(context, 0, 0); + CGContextAddLineToPoint(context, 0, 10); + CGContextMoveToPoint(context, bounds.size.width, bounds.size.height); + CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10); + CGContextMoveToPoint(context, bounds.size.width, bounds.size.height); + CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height); + CGContextClosePath( context ); + CGContextStrokePath(context); +#endif + + if ( !m_isFlipped ) + { + CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height ); + CGContextScaleCTM( context, 1, -1 ); + } + + wxRegion updateRgn; + const NSRect *rects; + NSInteger count; + + [slf getRectsBeingDrawn:&rects count:&count]; + for ( int i = 0 ; i < count ; ++i ) + { + updateRgn.Union(wxFromNSRect(slf, rects[i]) ); + } + + wxWindow* wxpeer = GetWXPeer(); + wxpeer->GetUpdateRegion() = updateRgn; + wxpeer->MacSetCGContextRef( context ); + + wxPaintEvent event; + event.SetTimestamp(0); // todo + event.SetEventObject(wxpeer); + bool handled = wxpeer->HandleWindowEvent(event); + + CGContextRestoreGState( context ); + + if ( !handled ) + { + // call super + SEL _cmd = @selector(drawRect:); + wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd]; + superimpl(slf, _cmd, *(NSRect*)rect); + } +} + +void wxWidgetCocoaImpl::clickedAction( WXWidget slf, void *_cmd, void *sender) +{ + wxWindow* wxpeer = (wxWindow*) GetWXPeer(); + if ( wxpeer ) + wxpeer->OSXHandleClicked(0); +} + +void wxWidgetCocoaImpl::doubleClickedAction( WXWidget slf, void *_cmd, void *sender) +{ +} + +// + +#if OBJC_API_VERSION >= 2 + +#define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \ + class_addMethod(c, s, i, t ); + +#else + +#define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \ + { s, t, i }, + +#endif + +void wxOSXCocoaClassAddWXMethods(Class c) +{ + +#if OBJC_API_VERSION < 2 + static objc_method wxmethods[] = + { +#endif + + wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" ) + + wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" ) + + wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" ) + + wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" ) + + wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" ) + + wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" ) + + wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" ) + + + wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" ) + + wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" ) + + wxOSX_CLASS_ADD_METHOD(c, @selector(clickedAction:), (IMP) wxOSX_clickedAction, "v@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(doubleClickedAction:), (IMP) wxOSX_doubleClickedAction, "v@:@" ) + +#if wxUSE_DRAG_AND_DROP + wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" ) +#endif + +#if OBJC_API_VERSION < 2 + } ; + static int method_count = WXSIZEOF( wxmethods ); + static objc_method_list *wxmethodlist = NULL; + if ( wxmethodlist == NULL ) + { + wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) ); + memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) ); + wxmethodlist->method_count = method_count; + wxmethodlist->obsolete = 0; + } + class_addMethods( c, wxmethodlist ); +#endif +} + +// +// C++ implementation class +// + IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl ) wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) : - wxWidgetImpl( peer, isRootControl ), m_osxView(w) + wxWidgetImpl( peer, isRootControl ) { + Init(); + m_osxView = w; } wxWidgetCocoaImpl::wxWidgetCocoaImpl() { + Init(); } void wxWidgetCocoaImpl::Init() { m_osxView = NULL; + m_isFlipped = true; } wxWidgetCocoaImpl::~wxWidgetCocoaImpl() { - if ( [m_osxView respondsToSelector:@selector(setImplementation:) ] ) - [m_osxView setImplementation:NULL]; + RemoveAssociations( this ); + if ( !IsRootControl() ) { NSView *sv = [m_osxView superview]; @@ -722,6 +1189,18 @@ void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool) void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control ) { + WXWidget c = control ? control : (WXWidget) m_osxView; + wxWidgetImpl::Associate( c, this ) ; + if ([c respondsToSelector:@selector(setAction:)]) + { + [c setTarget: c]; + [c setAction: @selector(clickedAction:)]; + if ([c respondsToSelector:@selector(setDoubleAction:)]) + { + [c setDoubleAction: @selector(doubleClickedAction:)]; + } + + } } bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event) @@ -810,6 +1289,11 @@ void wxWidgetCocoaImpl::ReleaseMouse() [[m_osxView window] enableCursorRects]; } +void wxWidgetCocoaImpl::SetFlipped(bool flipped) +{ + m_isFlipped = flipped; +} + // // Factory methods // @@ -817,13 +1301,14 @@ void wxWidgetCocoaImpl::ReleaseMouse() wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, long extraStyle) { - NSView* sv = (wxpeer->GetParent()->GetHandle() ); - NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; wxNSView* v = [[wxNSView alloc] initWithFrame:r]; - [sv addSubview:v]; + + // temporary hook for dnd + [v registerForDraggedTypes:[NSArray arrayWithObjects: + NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]]; + wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; return c; } @@ -832,7 +1317,6 @@ wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now ) NSWindow* tlw = now->GetWXWindow(); wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]]; wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true ); - [v setImplementation:c]; [tlw setContentView:v]; return c; } diff --git a/src/osx/window_osx.cpp b/src/osx/window_osx.cpp index b9817fbfc1..5e8a0b9f46 100644 --- a/src/osx/window_osx.cpp +++ b/src/osx/window_osx.cpp @@ -85,7 +85,6 @@ BEGIN_EVENT_TABLE(wxWindowMac, wxWindowBase) EVT_NC_PAINT(wxWindowMac::OnNcPaint) EVT_ERASE_BACKGROUND(wxWindowMac::OnEraseBackground) - EVT_PAINT(wxWindowMac::OnPaint) EVT_MOUSE_EVENTS(wxWindowMac::OnMouseEvent) END_EVENT_TABLE() @@ -1869,7 +1868,19 @@ bool wxWindowMac::MacDoRedraw( void* updatergnr , long time ) wxPaintEvent event; event.SetTimestamp(time); event.SetEventObject(this); - HandleWindowEvent(event); + if ( !HandleWindowEvent(event) ) + { + // for native controls: call their native paint method + if ( !MacIsUserPane() || ( IsTopLevel() && GetBackgroundStyle() == wxBG_STYLE_SYSTEM ) ) + { + if ( wxTheApp->MacGetCurrentEvent() != NULL && wxTheApp->MacGetCurrentEventHandlerCallRef() != NULL + && GetBackgroundStyle() != wxBG_STYLE_TRANSPARENT ) + CallNextEventHandler( + (EventHandlerCallRef)wxTheApp->MacGetCurrentEventHandlerCallRef() , + (EventRef) wxTheApp->MacGetCurrentEvent() ) ; + } + } + handled = true ; } @@ -2200,21 +2211,6 @@ void wxWindowMac::OnMouseEvent( wxMouseEvent &event ) } } -void wxWindowMac::OnPaint( wxPaintEvent & WXUNUSED(event) ) -{ -#if wxOSX_USE_COCOA_OR_CARBON - // for native controls: call their native paint method - if ( !MacIsUserPane() || ( IsTopLevel() && GetBackgroundStyle() == wxBG_STYLE_SYSTEM ) ) - { - if ( wxTheApp->MacGetCurrentEvent() != NULL && wxTheApp->MacGetCurrentEventHandlerCallRef() != NULL - && GetBackgroundStyle() != wxBG_STYLE_TRANSPARENT ) - CallNextEventHandler( - (EventHandlerCallRef)wxTheApp->MacGetCurrentEventHandlerCallRef() , - (EventRef) wxTheApp->MacGetCurrentEvent() ) ; - } -#endif -} - void wxWindowMac::TriggerScrollEvent( wxEventType WXUNUSED(scrollEvent) ) { }