From ccc72cc74f194d51c714a2bff121443e39424a26 Mon Sep 17 00:00:00 2001 From: Fred Kiefer Date: Fri, 11 Aug 2006 14:08:32 +0000 Subject: [PATCH] Moved tracking code from controll to cell class to allow the use in a matrix. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@23255 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 8 ++ Source/NSSlider.m | 154 -------------------------------------- Source/NSSliderCell.m | 167 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 173 insertions(+), 156 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1188a21d8..96dc39368 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2006-08-11 Fred Kiefer + + * Source/NSSlider.m (mouseDown:, trackKnob:knobRect:, + _floatValueForMousePoint()): Moved all track related code to cell class. + * Source/NSSliderCell.m (_floatValueForMousePoint(), + trackMouse:...untilMouseUp:): Code previously in the NSSlider class. + Reworked slightly to be more similar to [NSCell trackMouse:...untilMouseUp:]. + 2006-08-11 01:50-EDT Gregory John Casamento * Source/GSNibCompatibility.m: Assign copies of the parameters to diff --git a/Source/NSSlider.m b/Source/NSSlider.m index f3ad482b3..a29d8acaa 100644 --- a/Source/NSSlider.m +++ b/Source/NSSlider.m @@ -30,62 +30,6 @@ #include "AppKit/NSSlider.h" #include "AppKit/NSSliderCell.h" #include "AppKit/NSWindow.h" -#include "AppKit/NSApplication.h" - -static inline -float _floatValueForMousePoint (NSPoint point, NSRect knobRect, - NSRect slotRect, BOOL isVertical, - float minValue, float maxValue, - NSSliderCell *theCell, BOOL flipped) -{ - float floatValue = 0; - float position; - - // Adjust the point to lie inside the knob slot. We don't - // have to worry whether the view is flipped or not. - if (isVertical) - { - if (point.y < slotRect.origin.y + knobRect.size.height / 2) - { - position = slotRect.origin.y + knobRect.size.height / 2; - } - else if (point.y > slotRect.origin.y + slotRect.size.height - - knobRect.size.height / 2) - { - position = slotRect.origin.y + slotRect.size.height - - knobRect.size.height / 2; - } - else - position = point.y; - // Compute the float value - floatValue = (position - (slotRect.origin.y + knobRect.size.height/2)) - / (slotRect.size.height - knobRect.size.height); - if (flipped) - floatValue = 1 - floatValue; - } - else - { - if (point.x < slotRect.origin.x + knobRect.size.width / 2) - { - position = slotRect.origin.x + knobRect.size.width / 2; - } - else if (point.x > slotRect.origin.x + slotRect.size.width - - knobRect.size.width / 2) - { - position = slotRect.origin.x + slotRect.size.width - - knobRect.size.width / 2; - } - else - position = point.x; - - // Compute the float value given the knob size - floatValue = (position - (slotRect.origin.x + knobRect.size.width / 2)) - / (slotRect.size.width - knobRect.size.width); - } - - return floatValue * (maxValue - minValue) + minValue; -} - /** @@ -361,102 +305,4 @@ static Class cellClass; return [_cell tickMarkValueAtIndex: index]; } - -- (void) trackKnob: (NSEvent*)theEvent knobRect: (NSRect)knobRect -{ - NSApplication *app = [NSApplication sharedApplication]; - unsigned int eventMask = NSLeftMouseDownMask | NSLeftMouseUpMask - | NSLeftMouseDraggedMask | NSMouseMovedMask - | NSPeriodicMask; - NSPoint point = [self convertPoint: [theEvent locationInWindow] - fromView: nil]; - NSEventType eventType = [theEvent type]; - BOOL isContinuous = [_cell isContinuous]; - float oldFloatValue = [_cell floatValue]; - id target = [_cell target]; - SEL action = [_cell action]; - NSDate *distantFuture = [NSDate distantFuture]; - NSRect slotRect = [_cell trackRect]; - BOOL isVertical = [_cell isVertical]; - float minValue = [_cell minValue]; - float maxValue = [_cell maxValue]; - - [NSEvent startPeriodicEventsAfterDelay: 0.05 withPeriod: 0.05]; - [[NSRunLoop currentRunLoop] limitDateForMode: NSEventTrackingRunLoopMode]; - - while (eventType != NSLeftMouseUp) - { - theEvent = [app nextEventMatchingMask: eventMask - untilDate: distantFuture - inMode: NSEventTrackingRunLoopMode - dequeue: YES]; - eventType = [theEvent type]; - - if (eventType != NSPeriodic) - { - point = [self convertPoint: [theEvent locationInWindow] - fromView: nil]; - } - else - { - if (point.x != knobRect.origin.x || point.y != knobRect.origin.y) - { - float floatValue; - floatValue = _floatValueForMousePoint (point, knobRect, - slotRect, isVertical, - minValue, maxValue, - _cell, - _rFlags.flipped_view); - if (floatValue != oldFloatValue) - { - [_cell setFloatValue: floatValue]; - if (isContinuous) - { - [self sendAction: action to: target]; - } - oldFloatValue = floatValue; - } - knobRect.origin = point; - } - } - } - - // If the control is not continuous send the action at the end of the drag - if (!isContinuous) - { - [self sendAction: action to: target]; - } - [NSEvent stopPeriodicEvents]; -} - -- (void) mouseDown: (NSEvent *)theEvent -{ - if ([_cell isEnabled]) - { - NSPoint location = [self convertPoint: [theEvent locationInWindow] - fromView: nil]; - NSRect rect; - - rect = [_cell knobRectFlipped: _rFlags.flipped_view]; - if (![self mouse: location inRect: rect]) - { - // Mouse is not on the knob, move the knob to the mouse position - float floatValue; - floatValue = _floatValueForMousePoint (location, rect, - [_cell trackRect], - [_cell isVertical], - [_cell minValue], - [_cell maxValue], _cell, - _rFlags.flipped_view); - [_cell setFloatValue: floatValue]; - if ([_cell isContinuous]) - { - [self sendAction: [_cell action] to: [_cell target]]; - } - } - - [self trackKnob: theEvent knobRect: rect]; - } -} - @end diff --git a/Source/NSSliderCell.m b/Source/NSSliderCell.m index b66c7dc6b..9f93247b8 100644 --- a/Source/NSSliderCell.m +++ b/Source/NSSliderCell.m @@ -29,15 +29,72 @@ #include #include -#include "AppKit/NSSliderCell.h" +#include "AppKit/NSApplication.h" #include "AppKit/NSColor.h" -#include "AppKit/NSGraphics.h" #include "AppKit/NSControl.h" +#include "AppKit/NSEvent.h" +#include "AppKit/NSGraphics.h" #include "AppKit/NSImage.h" +#include "AppKit/NSSliderCell.h" #include "AppKit/NSTextFieldCell.h" +#include "AppKit/NSWindow.h" DEFINE_RINT_IF_MISSING +static inline +float _floatValueForMousePoint (NSPoint point, NSRect knobRect, + NSRect slotRect, BOOL isVertical, + float minValue, float maxValue, + NSSliderCell *theCell, BOOL flipped) +{ + float floatValue = 0; + float position; + + // Adjust the point to lie inside the knob slot. We don't + // have to worry whether the view is flipped or not. + if (isVertical) + { + if (point.y < slotRect.origin.y + knobRect.size.height / 2) + { + position = slotRect.origin.y + knobRect.size.height / 2; + } + else if (point.y > slotRect.origin.y + slotRect.size.height + - knobRect.size.height / 2) + { + position = slotRect.origin.y + slotRect.size.height + - knobRect.size.height / 2; + } + else + position = point.y; + // Compute the float value + floatValue = (position - (slotRect.origin.y + knobRect.size.height/2)) + / (slotRect.size.height - knobRect.size.height); + if (flipped) + floatValue = 1 - floatValue; + } + else + { + if (point.x < slotRect.origin.x + knobRect.size.width / 2) + { + position = slotRect.origin.x + knobRect.size.width / 2; + } + else if (point.x > slotRect.origin.x + slotRect.size.width + - knobRect.size.width / 2) + { + position = slotRect.origin.x + slotRect.size.width + - knobRect.size.width / 2; + } + else + position = point.x; + + // Compute the float value given the knob size + floatValue = (position - (slotRect.origin.x + knobRect.size.width / 2)) + / (slotRect.size.width - knobRect.size.width); + } + + return floatValue * (maxValue - minValue) + minValue; +} + /** Class Description @@ -561,6 +618,112 @@ DEFINE_RINT_IF_MISSING return _minValue + index * (_maxValue - _minValue) / _numberOfTickMarks; } +- (BOOL) trackMouse: (NSEvent*)theEvent + inRect: (NSRect)cellFrame + ofView: (NSView*)controlView + untilMouseUp: (BOOL)flag +{ + float delay; + float interval; + id target = [self target]; + SEL action = [self action]; + unsigned int eventMask = NSLeftMouseDownMask | NSLeftMouseUpMask + | NSLeftMouseDraggedMask | NSMouseMovedMask; + NSEventType eventType = [theEvent type]; + BOOL isContinuous = [self isContinuous]; + float oldFloatValue = [self floatValue]; + NSRect slotRect = [self trackRect]; + BOOL isVertical = [self isVertical]; + float minValue = [self minValue]; + float maxValue = [self maxValue]; + BOOL isFlipped = [controlView isFlipped]; + NSPoint location = [theEvent locationInWindow]; + NSPoint point = [controlView convertPoint: location fromView: nil]; + NSRect knobRect = [self knobRectFlipped: isFlipped]; + + _mouse_down_flags = [theEvent modifierFlags]; + if (![self isEnabled]) + { + return NO; + } + + if (![controlView mouse: point inRect: knobRect]) + { + // Mouse is not on the knob, move the knob to the mouse position + float floatValue; + + floatValue = _floatValueForMousePoint(point, knobRect, + slotRect, isVertical, + minValue, maxValue, + self, isFlipped); + [self setFloatValue: floatValue]; + if (isContinuous) + { + [(NSControl*)controlView sendAction: action to: target]; + } + } + + if (isContinuous) + { + [self getPeriodicDelay: &delay interval: &interval]; + [NSEvent startPeriodicEventsAfterDelay: delay withPeriod: interval]; + eventMask |= NSPeriodicMask; + } + + while (eventType != NSLeftMouseUp) + { + theEvent = [NSApp nextEventMatchingMask: eventMask + untilDate: nil + inMode: NSEventTrackingRunLoopMode + dequeue: YES]; + eventType = [theEvent type]; + + if (eventType == NSPeriodic) + { + NSWindow *w = [controlView window]; + + location = [w mouseLocationOutsideOfEventStream]; + } + else + { + location = [theEvent locationInWindow]; + } + point = [controlView convertPoint: location fromView: nil]; + + if (point.x != knobRect.origin.x || point.y != knobRect.origin.y) + { + float floatValue; + + floatValue = _floatValueForMousePoint(point, knobRect, + slotRect, isVertical, + minValue, maxValue, + self, isFlipped); + if (floatValue != oldFloatValue) + { + [self setFloatValue: floatValue]; + if (isContinuous) + { + [(NSControl*)controlView sendAction: action to: target]; + } + oldFloatValue = floatValue; + } + knobRect.origin = point; + } + } + + // If the cell is not continuous send the action at the end of the drag + if (!isContinuous) + { + [(NSControl*)controlView sendAction: action to: target]; + } + else + { + [NSEvent stopPeriodicEvents]; + } + + return YES; +} + - (id) initWithCoder: (NSCoder*)decoder { self = [super initWithCoder: decoder];