]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/cocoa/window.mm
adapting to SDK 3.0
[wxWidgets.git] / src / osx / cocoa / window.mm
index 9a10b8874352f5d695f992aa415c9bfc75bc8968..b26f59df7bcb33f49fee8f988b27ca598b299c3c 100644 (file)
 
 // Get the window with the focus
 
-WXWidget wxWidgetImpl::FindFocus()
+NSView* GetViewFromResponder( NSResponder* responder )
 {
-    NSView* focusedView = nil;
-    NSWindow* keyWindow = [[NSApplication sharedApplication] keyWindow];
-    if ( keyWindow != nil )
+    NSView* view = nil;
+    if ( [responder isKindOfClass:[NSTextView class]] )
     {
-        NSResponder* responder = [keyWindow firstResponder];
-        if ( [responder isKindOfClass:[NSTextView class]] && 
-            [keyWindow fieldEditor:NO forObject:nil] != nil )
-        {
-            focusedView = [(NSTextView*)responder delegate];
-        }
+        NSView* delegate =  [(NSTextView*)responder delegate];
+        if ( [delegate isKindOfClass:[NSTextField class] ] )
+            view = delegate;
         else
-        {
-            if ( [responder isKindOfClass:[NSView class]] )
-                focusedView = (NSView*) responder;
-        }
+            view =  (NSView*) responder;
     }
+    else
+    {
+        if ( [responder isKindOfClass:[NSView class]] )
+            view = (NSView*) responder;
+    }
+    return view;
+}
+
+NSView* GetFocusedViewInWindow( NSWindow* keyWindow )
+{
+    NSView* focusedView = nil;
+    if ( keyWindow != nil )
+        focusedView = GetViewFromResponder([keyWindow firstResponder]);
+
     return focusedView;
 }
 
+WXWidget wxWidgetImpl::FindFocus()
+{
+    return GetFocusedViewInWindow( [[NSApplication sharedApplication] keyWindow] );
+}
+
 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
 {
     int x, y, w, h ;
@@ -272,7 +284,7 @@ void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charStrin
     wxevent.m_rawCode = [nsEvent keyCode];
     wxevent.m_rawFlags = modifiers;
     
-    wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
+    wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
 
     wxString chars;
     if ( eventType != NSFlagsChanged )
@@ -361,7 +373,7 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
     wxevent.m_controlDown = modifiers & NSControlKeyMask;
     wxevent.m_altDown = modifiers & NSAlternateKeyMask;
     wxevent.m_metaDown = modifiers & NSCommandKeyMask;
-    wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
+    wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
 
     UInt32 mouseChord = 0; 
 
@@ -485,11 +497,11 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
             if ( fabs([nsEvent deltaX]) > fabs([nsEvent deltaY]) )
             {
                 wxevent.m_wheelAxis = 1;
-                wxevent.m_wheelRotation = [nsEvent deltaX] * 10.0;
+                wxevent.m_wheelRotation = (int)([nsEvent deltaX] * 10);
             }
             else
             {
-                wxevent.m_wheelRotation = [nsEvent deltaY] * 10.0;
+                wxevent.m_wheelRotation = (int)([nsEvent deltaY] * 10);
             }
         }
         break ;
@@ -583,6 +595,8 @@ BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
     return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
 }
 
+#endif
+
 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event) 
 {
     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
@@ -706,7 +720,8 @@ unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf),
         return NSDragOperationNone;
 
     wxDragResult result = wxDragNone;
-    wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
+    NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
+    wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
 
     if ( sourceDragMask & NSDragOperationLink )
         result = wxDragLink;
@@ -771,7 +786,8 @@ unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf),
         return NSDragOperationNone;
 
     wxDragResult result = wxDragNone;
-    wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
+    NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
+    wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
 
     if ( sourceDragMask & NSDragOperationLink )
         result = wxDragLink;
@@ -811,7 +827,8 @@ bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), vo
     wxWindow* wxpeer = GetWXPeer();
     wxDropTarget* target = wxpeer->GetDropTarget();
     wxDragResult result = wxDragNone;
-    wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
+    NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
+    wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
 
     if ( sourceDragMask & NSDragOperationLink )
         result = wxDragLink;
@@ -823,33 +840,38 @@ bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), vo
     PasteboardRef pboardRef;    
     PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
     target->SetCurrentDragPasteboard(pboardRef);
-    result = target->OnData(pt.x, pt.y, result);
+
+    if (target->OnDrop(pt.x, pt.y))
+        result = target->OnData(pt.x, pt.y, result);
+
     CFRelease(pboardRef);
      
     return result != wxDragNone;
 }
 
-#endif
-
 typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
 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);
+typedef void (*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);
+        // for plain NSView mouse events would propagate to parents otherwise
+        if (!m_wxPeer->MacIsUserPane())
+        {
+            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 ( [[slf window] firstResponder] != slf || !DoHandleKeyEvent(event) )
+    if ( GetFocusedViewInWindow([slf window]) != slf || m_hasEditor || !DoHandleKeyEvent(event) )
     {
         wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
         superimpl(slf, (SEL)_cmd, event);
@@ -858,10 +880,10 @@ void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
 
 void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
 {
-    if (m_lastKeyDownEvent && !DoHandleCharEvent(m_lastKeyDownEvent, text) )
+    if ( m_lastKeyDownEvent==NULL || m_hasEditor || !DoHandleCharEvent(m_lastKeyDownEvent, text) )
     {
-            wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
-            superimpl(slf, (SEL)_cmd, text);
+        wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
+        superimpl(slf, (SEL)_cmd, text);
     }
     m_lastKeyDownEvent = NULL;
 }
@@ -889,12 +911,14 @@ bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
     wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
     // get the current focus before running becomeFirstResponder
     NSView* otherView = FindFocus(); 
+
     wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
     BOOL r = superimpl(slf, (SEL)_cmd);
     if ( r )
     {
         DoNotifyFocusEvent( true, otherWindow );
     }
+
     return r;
 }
 
@@ -903,11 +927,13 @@ bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
     wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
     BOOL r = superimpl(slf, (SEL)_cmd);
     // get the current focus after running resignFirstResponder
+    // note that this value isn't reliable, it might return the same view that
+    // is resigning
     NSView* otherView = FindFocus(); 
     wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
     // NSTextViews have an editor as true responder, therefore the might get the
-    // resign notification if their editor takes over, don't trigger any event hen
-    if ( r && otherWindow != this)
+    // resign notification if their editor takes over, don't trigger any event then
+    if ( r && !m_hasEditor)
     {
         DoNotifyFocusEvent( false, otherWindow );
     }
@@ -1057,7 +1083,7 @@ void wxOSXCocoaClassAddWXMethods(Class c)
     
     wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
 
-    wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
+    wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "c@:@" )
 
     wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" )
     wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
@@ -1115,6 +1141,7 @@ void wxWidgetCocoaImpl::Init()
     m_osxView = NULL;
     m_isFlipped = true;
     m_lastKeyDownEvent = NULL;
+    m_hasEditor = false;
 }
 
 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
@@ -1142,10 +1169,12 @@ void wxWidgetCocoaImpl::SetVisibility( bool visible )
 
 void wxWidgetCocoaImpl::Raise()
 {
+    // Not implemented
 }
     
 void wxWidgetCocoaImpl::Lower()
 {
+    // Not implemented
 }
 
 void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) )
@@ -1203,8 +1232,8 @@ void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
 {
     NSRect rect = [m_osxView frame];
-    width = rect.size.width;
-    height = rect.size.height;
+    width = (int)rect.size.width;
+    height = (int)rect.size.height;
 }
 
 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
@@ -1216,14 +1245,14 @@ void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &hei
         NSRect bounds = [m_osxView bounds];
         NSRect rect = [cv frame];
         
-        int y = rect.origin.y;
-        int x = rect.origin.x;
+        int y = (int)rect.origin.y;
+        int x = (int)rect.origin.x;
         if ( ![ m_osxView isFlipped ] )
-            y = bounds.size.height - (rect.origin.y + rect.size.height);
+            y = (int)(bounds.size.height - (rect.origin.y + rect.size.height));
         left = x;
         top = y;
-        width = rect.size.width;
-        height = rect.size.height;
+        width = (int)rect.size.width;
+        height = (int)rect.size.height;
     }
     else
     {
@@ -1261,6 +1290,7 @@ bool wxWidgetCocoaImpl::SetFocus()
         return false;
         
     [[m_osxView window] makeFirstResponder: m_osxView] ;
+    [[m_osxView window] makeKeyAndOrderFront:nil] ;
     return true;
 }
 
@@ -1345,7 +1375,7 @@ wxInt32 wxWidgetCocoaImpl::GetMinimum() const
 {
     if (  [m_osxView respondsToSelector:@selector(getMinValue:)] )
     {
-        return [m_osxView minValue];
+        return (int)[m_osxView minValue];
     }
     return 0;
 }
@@ -1354,11 +1384,24 @@ wxInt32 wxWidgetCocoaImpl::GetMaximum() const
 {
     if (  [m_osxView respondsToSelector:@selector(getMaxValue:)] )
     {
-        return [m_osxView maxValue];
+        return (int)[m_osxView maxValue];
     }
     return 0;
 }
 
+wxBitmap wxWidgetCocoaImpl::GetBitmap() const
+{
+    wxBitmap bmp;
+
+    // TODO: how to create a wxBitmap from NSImage?
+#if 0
+    if ( [m_osxView respondsToSelector:@selector(image:)] )
+        bmp = [m_osxView image];
+#endif
+
+    return bmp;
+}
+
 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
 {
     if (  [m_osxView respondsToSelector:@selector(setImage:)] )
@@ -1367,6 +1410,38 @@ void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
     }
 }
 
+void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir )
+{
+    if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] )
+    {
+        NSCellImagePosition pos;
+        switch ( dir )
+        {
+            case wxLEFT:
+                pos = NSImageLeft;
+                break;
+
+            case wxRIGHT:
+                pos = NSImageRight;
+                break;
+
+            case wxTOP:
+                pos = NSImageAbove;
+                break;
+
+            case wxBOTTOM:
+                pos = NSImageBelow;
+                break;
+
+            default:
+                wxFAIL_MSG( "invalid image position" );
+                pos = NSNoImage;
+        }
+
+        [m_osxView setImagePosition:pos];
+    }
+}
+
 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook))
 {
     // implementation in subclass
@@ -1382,8 +1457,8 @@ void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
         [m_osxView sizeToFit];
         NSRect best = [m_osxView frame];
         [m_osxView setFrame:former];
-        r->width = best.size.width;
-        r->height = best.size.height;
+        r->width = (int)best.size.width;
+        r->height = (int)best.size.height;
     }
 }
 
@@ -1478,11 +1553,17 @@ bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
 
     // this will fire higher level events, like insertText, to help
     // us handle EVT_CHAR, etc.
-    if ([event type] == NSKeyDown)
+    if ( !m_hasEditor && [event type] == NSKeyDown)
     {
         m_lastKeyDownEvent = event;
         if ( !result )
-            [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
+        {
+            if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
+                [[(NSScrollView*)m_osxView documentView] interpretKeyEvents:[NSArray arrayWithObject:event]];
+            else
+                [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
+            result = true;
+        }
     }
     return result;
 }