]> git.saurik.com Git - wxWidgets.git/blobdiff - src/cocoa/slider.mm
missing commit
[wxWidgets.git] / src / cocoa / slider.mm
index c94fc98575d0c7edb2d49792e4eee6ffee2c7ec0..5ab19e01ef50b9768fc4cff43cae1f54bebe83b4 100644 (file)
@@ -8,7 +8,7 @@
 // RCS-ID:      $Id$
 // Copyright:   (c) 2003 David Elliott
 //              (c) 2007 Software 2000 Ltd.
-// Licence:     wxWidgets licence
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 #include "wx/wxprec.h"
     #include "wx/app.h"
 #endif //WX_PRECOMP
 
+#import <Foundation/NSString.h>
 #include "wx/cocoa/objc/NSSlider.h"
+#import <AppKit/NSEvent.h>
+#import <AppKit/NSWindow.h>
 
-IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
-    BEGIN_EVENT_TABLE(wxSlider, wxSliderBase)
+BEGIN_EVENT_TABLE(wxSlider, wxSliderBase)
 END_EVENT_TABLE()
 WX_IMPLEMENT_COCOA_OWNER(wxSlider,NSSlider,NSControl,NSView)
 
@@ -39,11 +41,14 @@ inline void AdjustDimension(
     const int dimension = (size.*GetDimension)();
     const int minSize = (isTicksStyle) ? 23 : 20;
 
+    // prevent clipping of overly "thin" sliders
     if (dimension < minSize)
     {
         (size.*SetDimension)(minSize);
     }
 
+    // move the slider control to the middle of the dimension that is not
+    // being used to define its length
     pos += (dimension - (size.*GetDimension)() + 1) / 2;
 }
 
@@ -65,17 +70,24 @@ bool wxSlider::Create(wxWindow *parent, wxWindowID winid,
         AdjustDimension(isTicksStyle, adjustedPos.x, adjustedSize, &wxSize::GetWidth, &wxSize::SetWidth);
     }
     
-    if(!CreateControl(parent,winid,pos,size,style,validator,name))
+    if(!CreateControl(parent,winid,adjustedPos,adjustedSize,style,validator,name))
         return false;
-    SetNSSlider([[WX_GET_OBJC_CLASS(WXNSSlider) alloc] initWithFrame: MakeDefaultNSRect(size)]);
+    SetNSSlider([[WX_GET_OBJC_CLASS(WXNSSlider) alloc] initWithFrame: MakeDefaultNSRect(adjustedSize)]);
     [m_cocoaNSView release];
     
     if(m_parent)
         m_parent->CocoaAddChild(this);
-    SetInitialFrameRect(pos,size);
+    SetInitialFrameRect(adjustedPos,adjustedSize);
     
     SetRange(minValue, maxValue);
     SetValue(value);
+    
+    // -1 default for wxSL_AUTOTICKS == false
+    int tickMarks = -1;
+    // minValue > maxValue not handled, tickMarks set to 0
+    if ( style & wxSL_AUTOTICKS )
+        tickMarks = ((maxValue - minValue >= 0) ? (maxValue - minValue) : 0);
+    SetTickFreq(tickMarks);
 
     return true;
 }
@@ -85,11 +97,64 @@ wxSlider::~wxSlider()
     DisassociateNSSlider(GetNSSlider());
 }
 
+void wxSlider::AssociateNSSlider(WX_NSSlider theSlider)
+{
+    wxCocoaNSSlider::AssociateNSSlider(theSlider);
+    // Set the target/action.. we don't really need to unset these
+    [theSlider setTarget:wxCocoaNSControl::sm_cocoaTarget];
+    [theSlider setAction:@selector(wxNSControlAction:)];
+}
+
 void wxSlider::ProcessEventType(wxEventType commandType)
 {
     wxScrollEvent event(commandType, GetId(), GetValue(), HasFlag(wxSL_VERTICAL)?wxVERTICAL:wxHORIZONTAL);
     event.SetEventObject(this);
-    GetEventHandler()->ProcessEvent(event);
+    HandleWindowEvent(event);
+}
+
+static inline wxEventType wxSliderEventTypeForKeyFromEvent(NSEvent *theEvent)
+{
+    NSString *theEventCharacters = [theEvent charactersIgnoringModifiers];
+
+    if ([theEventCharacters length] == 1)
+    {
+        switch ([theEventCharacters characterAtIndex:0])
+        {
+            case NSUpArrowFunctionKey:
+            case NSRightArrowFunctionKey:   return wxEVT_SCROLL_PAGEDOWN;
+            case NSDownArrowFunctionKey:
+            case NSLeftArrowFunctionKey:    return wxEVT_SCROLL_PAGEUP;
+            case NSPageUpFunctionKey:       return wxEVT_SCROLL_BOTTOM;
+            case NSPageDownFunctionKey:     return wxEVT_SCROLL_TOP;
+        }
+    }
+    // Overload wxEVT_ANY to mean we can't determine the event type.
+    return wxEVT_ANY;
+}
+
+void wxSlider::CocoaTarget_action()
+{
+    wxEventType sliderEventType;
+    SEL theSelector = wxCocoaNSSlider::GetLastResponderSelector();
+    
+    if(         theSelector == @selector(moveUp:)
+            ||  theSelector == @selector(moveRight:))
+        sliderEventType = wxEVT_SCROLL_PAGEDOWN;
+    else if(    theSelector == @selector(moveDown:)
+            ||  theSelector == @selector(moveLeft:))
+        sliderEventType = wxEVT_SCROLL_PAGEUP;
+    else if(    theSelector == @selector(pageUp:))
+        sliderEventType = wxEVT_SCROLL_BOTTOM;
+    else if(    theSelector == @selector(pageDown:))
+        sliderEventType = wxEVT_SCROLL_TOP;
+    else if(    theSelector == @selector(keyDown:))
+        // This case should ideally never be reached.
+        sliderEventType = wxSliderEventTypeForKeyFromEvent([[GetNSSlider() window] currentEvent]);
+    else
+        // Don't generate an event.
+        return;
+    if(sliderEventType != wxEVT_ANY)
+        ProcessEventType(sliderEventType);
 }
 
 void wxSlider::CocoaNotification_startTracking(WX_NSNotification notification)
@@ -140,7 +205,7 @@ int wxSlider::GetMax() const
     return [GetNSSlider() maxValue];
 }
 
-void wxSlider::SetTickFreq(int n, int pos)
+void wxSlider::DoSetTickFreq(int n)
 {
     const int numTicks = (n > 0) ? ((GetMax() - GetMin()) / n) + 1 : 0;
     [GetNSSlider() setNumberOfTickMarks:numTicks];