/**
An NSSliderCell controls the behaviour and appearance of an associated NSSlider, or a single slider in an NSMatrix. Tick marks are defined in the official standard, but are not implemented in GNUstep.
An NSSliderCell can be customized through its
set...
methods. If these do not provide enough
customization, a subclass can be created, which overrides any of the
follwing methods: knobRectFlipped:
,
drawBarInside:flipped:
, drawKnob:
, or
prefersTrackingUntilMouseUp
.
Draws the slider's track, not including the bezel, in aRect flipped indicates whether the control view has a flipped coordinate system.
Do not call this method directly, it is provided for subclassing only.
*/ - (void) drawBarInside: (NSRect)rect flipped: (BOOL)flipped { [[GSTheme theme] drawBarInside: rect inCell: self flipped: flipped]; } /**Returns the rect in which to draw the knob, based on the
coordinate system of the NSSlider or NSMatrix this NSSliderCell is
associated with. flipped indicates whether or not that
coordinate system is flipped, which can be determined by sending the
isFlipped
message to the associated NSSlider or
NSMatrix.
Do not call this method directly. It is included for subclassing only.
*/ - (NSRect) knobRectFlipped: (BOOL)flipped { NSImage *image = [_knobCell image]; NSSize size; NSPoint origin; float floatValue = [self floatValue]; // FIXME: this method needs to be refactored out to GSTheme if (_isVertical && flipped) { floatValue = _maxValue + _minValue - floatValue; } floatValue = (floatValue - _minValue) / (_maxValue - _minValue); if (image != nil) { size = [image size]; } else { size = NSZeroSize; } if (_isVertical == YES) { origin = _trackRect.origin; origin.y += (_trackRect.size.height - size.height) * floatValue; } else { origin = _trackRect.origin; origin.x += (_trackRect.size.width - size.width) * floatValue; } return NSMakeRect (origin.x, origin.y, size.width, size.height); } /**Calculates the rect in which to draw the knob, then calls
drawKnob:
Before calling this method, a
lockFocus
message must be sent to the cell's control
view.
When subclassing NSSliderCell, do not override this method.
Override drawKnob:
instead.
See Also: -drawKnob:
*/ - (void) drawKnob { [[GSTheme theme] drawKnobInCell: self]; } /**Draws the knob in knobRect. Before calling this
method, a lockFocus
message must be sent to the cell's
control view.
Do not call this method directly. It is included for subclassing only.
See Also: -drawKnob
*/ - (void) drawKnob: (NSRect)knobRect { NSColor* knobBackgroundColor = [NSColor controlBackgroundColor]; [knobBackgroundColor set]; NSRectFill (knobRect); [_knobCell drawInteriorWithFrame: knobRect inView: _control_view]; } - (void) drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView*)controlView { cellFrame = [self drawingRectForBounds: cellFrame]; _trackRect = cellFrame; if (_type == NSCircularSlider) { NSBezierPath *circle; NSPoint knobCenter; NSPoint point; NSRect knobRect; float fraction, angle, radius; if (cellFrame.size.width > cellFrame.size.height) { knobRect = NSMakeRect(cellFrame.origin.x + ((cellFrame.size.width - cellFrame.size.height) / 2.0), cellFrame.origin.y, cellFrame.size.height, cellFrame.size.height); } else { knobRect = NSMakeRect(cellFrame.origin.x, cellFrame.origin.y + ((cellFrame.size.height - cellFrame.size.width) / 2.0), cellFrame.size.width, cellFrame.size.width); } knobCenter = NSMakePoint(NSMidX(knobRect), NSMidY(knobRect)); circle = [NSBezierPath bezierPathWithOvalInRect: knobRect]; [[NSColor controlBackgroundColor] set]; [circle fill]; [[NSColor blackColor] set]; [circle stroke]; fraction = ([self floatValue] - [self minValue]) / ([self maxValue] - [self minValue]); angle = (fraction * (2.0 * M_PI)) - (M_PI / 2.0); radius = (knobRect.size.height / 2) - 4; point = NSMakePoint((radius * cos(angle)) + knobCenter.x, (radius * sin(angle)) + knobCenter.y); [[NSBezierPath bezierPathWithOvalInRect: NSMakeRect(point.x - 2, point.y - 2, 4, 4)] stroke]; } else if (_type == NSLinearSlider) { BOOL vertical = (cellFrame.size.height > cellFrame.size.width); NSImage *image; NSSize size; if (vertical != _isVertical) { if (vertical == YES) { image = [NSImage imageNamed: @"common_SliderVert"]; if (image != nil) { size = [image size]; [image setScalesWhenResized: YES]; [image setSize: NSMakeSize(cellFrame.size.width, size.height)]; } } else { image = [NSImage imageNamed: @"common_SliderHoriz"]; if (image != nil) { size = [image size]; [image setScalesWhenResized: YES]; [image setSize: NSMakeSize(size.width, cellFrame.size.height)]; } } [_knobCell setImage: image]; } _isVertical = vertical; [self drawBarInside: cellFrame flipped: [controlView isFlipped]]; /* Draw title - Uhmmm - shouldn't this better go into drawBarInside:flipped: ? */ if (_isVertical == NO) { [_titleCell drawInteriorWithFrame: cellFrame inView: controlView]; } [self drawKnob]; } } - (BOOL) isOpaque { return NO; } /**Returns the thickness of the slider's knob. This value is in pixels, and is the size of the knob along the slider's track.
See Also: -setKnobThickness:
*/ - (float) knobThickness { NSImage *image = [_knobCell image]; NSSize size; if (image != nil) { size = [image size]; } else { return 0; } return _isVertical ? size.height : size.width; } /**Sets the thickness of the knob to thickness, in pixels. This value sets the amount of space which the knob takes up in the slider's track.
See Also: -knobThickness
*/ - (void) setKnobThickness: (float)thickness { NSImage *image = [_knobCell image]; NSSize size; if (image != nil) { size = [image size]; } else { return; } if (_isVertical == YES) size.height = thickness; else size.width = thickness; [image setSize: size]; if ((_control_view != nil) && ([_control_view isKindOfClass: [NSControl class]])) { [(NSControl*)_control_view updateCell: self]; } } /**Sets the value by which the slider will be be incremented when with the ALT key down to increment.
See Also: -altIncrementValue
*/ - (void) setAltIncrementValue: (double)increment { _altIncrementValue = increment; } /**Sets the minimum value that the sliders represents to maxValue.
See Also: -minValue
*/ - (void) setMinValue: (double)aDouble { _minValue = aDouble; if ([self doubleValue] < _minValue) [self setDoubleValue: _minValue]; } /**Sets the maximum value that the sliders represents to maxValue.
See Also: -maxValue
*/ - (void) setMaxValue: (double)aDouble { _maxValue = aDouble; if ([self doubleValue] > _maxValue) [self setDoubleValue: _maxValue]; } - (void) setObjectValue: (id)anObject { // If the provided object doesn't respond to doubeValue, or our minValue // is greater than our maxValue, we set our value to our minValue // (this arbitrary choice matches OS X) if ([anObject respondsToSelector: @selector(doubleValue)] == NO || _minValue > _maxValue) [super setObjectValue: [NSNumber numberWithDouble: _minValue]]; else { double aDouble = [anObject doubleValue]; if (aDouble < _minValue) [super setObjectValue: [NSNumber numberWithDouble: _minValue]]; else if (aDouble > _maxValue) [super setObjectValue: [NSNumber numberWithDouble: _maxValue]]; else [super setObjectValue: anObject]; } } /**Returns the cell used to draw the title.
See Also: -setTitleCell:
*/ - (id) titleCell { return _titleCell; } /**Returns the colour used to draw the title.
See Also: -setTitleColor:
*/ - (NSColor*) titleColor { return [_titleCell textColor]; } /**Returns the font used to draw the title.
See Also: -setTitleFont:
*/ - (NSFont*) titleFont { return [_titleCell font]; } /**Sets the title of the slider to barTitle. This title is displayed on the slider's track, behind the knob.
See Also: -title
*/ - (void) setTitle: (NSString*)title { [_titleCell setStringValue: title]; } /**Returns the title of the slider. This title is displayed on the slider's track, behind the knob.
See Also: -setTitle:
*/ - (NSString*) title { return [_titleCell stringValue]; } /**Sets the cell used to draw the title to titleCell.
See Also: -titleCell
*/ - (void) setTitleCell: (NSCell*)aCell { ASSIGN(_titleCell, aCell); } /**Sets the colour with which the title will be drawn to color.
See Also: -titleColor
*/ - (void) setTitleColor: (NSColor*)color { [_titleCell setTextColor: color]; } /**Sets the font with which the title will be drawm to font.
See Also: -titleFont
*/ - (void) setTitleFont: (NSFont*)font { [_titleCell setFont: font]; } /**Returns the slider type: linear or circular.
See Also: -setSliderType:
*/ - (NSSliderType)sliderType { return _type; } /**Sets the type of the slider: linear or circular.
See Also: -sliderType
*/ - (void) setSliderType: (NSSliderType)type { _type = type; if (_type == NSLinearSlider) { [self setBordered: YES]; [self setBezeled: NO]; } else if (_type == NSCircularSlider) { [self setBordered: NO]; [self setBezeled: NO]; } } /**Returns whether or not the slider is vertical. If, for some reason, this cannot be determined, for such reasons as the slider is not yet displayed, this method returns -1. Generally, a slider is considered vertical if its height is greater than its width. */ - (int) isVertical { return _isVertical; } /**Returns the value by which the slider is incremented when the user holds down the ALT key.
See Also: -setAltIncrementValue:
*/ - (double) altIncrementValue { return _altIncrementValue; } /**The default implementation returns YES
, so that the
slider continues to track the user's movement even if the cursor
leaves the slider's track.
Do not call this method directly. Override it in subclasses where the tracking behaviour needs to be different.
*/ + (BOOL) prefersTrackingUntilMouseUp { return YES; } /** Returns the rect of the track, minus the bezel. */ - (NSRect) trackRect { return _trackRect; } /**Returns the minimum value that the slider represents.
See Also: -setMinValue:
*/ - (double) minValue { return _minValue; } /**Returns the maximum value that the slider represents.
See Also: -setMaxValue:
*/ - (double) maxValue { return _maxValue; } // ticks - (BOOL) allowsTickMarkValuesOnly { return _allowsTickMarkValuesOnly; } - (double) closestTickMarkValueToValue: (double)aValue { double d, f; if (_numberOfTickMarks == 0) return aValue; else if (_numberOfTickMarks == 1) return (_maxValue + _minValue) / 2; if (aValue < _minValue) { aValue = _minValue; } else if (aValue > _maxValue) { aValue = _maxValue; } d = _maxValue - _minValue; f = ((aValue - _minValue) * (_numberOfTickMarks - 1)) / d; f = ((rint(f) * d) / (_numberOfTickMarks - 1)) + _minValue; return f; } - (int) indexOfTickMarkAtPoint: (NSPoint)point { int i; for (i = 0; i < _numberOfTickMarks; i++) { if (NSPointInRect(point, [self rectOfTickMarkAtIndex: i])) { return i; } } return NSNotFound; } - (int) numberOfTickMarks { return _numberOfTickMarks; } - (NSRect) rectOfTickMarkAtIndex: (int)index { NSRect rect = _trackRect; float d; if ((index < 0) || (index >= _numberOfTickMarks)) { [NSException raise: NSRangeException format: @"Index of tick mark out of bounds."]; } if (_numberOfTickMarks > 1) { if (_isVertical) { d = NSHeight(rect) / (_numberOfTickMarks - 1); rect.size.height = d; rect.origin.y += d * index; } else { d = NSWidth(rect) / (_numberOfTickMarks - 1); rect.size.width = d; rect.origin.x += d * index; } } return rect; } - (void) setAllowsTickMarkValuesOnly: (BOOL)flag { _allowsTickMarkValuesOnly = flag; } - (void) setNumberOfTickMarks: (int)numberOfTickMarks { _numberOfTickMarks = numberOfTickMarks; if ((_control_view != nil) && ([_control_view isKindOfClass: [NSControl class]])) { [(NSControl*)_control_view updateCell: self]; } } - (void) setTickMarkPosition: (NSTickMarkPosition)position { _tickMarkPosition = position; if ((_control_view != nil) && ([_control_view isKindOfClass: [NSControl class]])) { [(NSControl*)_control_view updateCell: self]; } } - (NSTickMarkPosition) tickMarkPosition { return _tickMarkPosition; } - (double) tickMarkValueAtIndex: (int)index { if ((index < 0) || (index >= _numberOfTickMarks)) { [NSException raise: NSRangeException format: @"Index of tick mark out of bounds."]; } if (_numberOfTickMarks == 1) return (_maxValue + _minValue) / 2; if (index >= _numberOfTickMarks) return _maxValue; if (index <= 0) return _minValue; return _minValue + index * (_maxValue - _minValue) / (_numberOfTickMarks - 1); } - (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, (_type == NSCircularSlider)); [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: [NSDate distantFuture] 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, (_type == NSCircularSlider)); if (floatValue != oldFloatValue) { if (_allowsTickMarkValuesOnly) { floatValue = [self closestTickMarkValueToValue:floatValue]; } [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]; if (self == nil) return nil; if ([decoder allowsKeyedCoding]) { _allowsTickMarkValuesOnly = [decoder decodeBoolForKey: @"NSAllowsTickMarkValuesOnly"]; _numberOfTickMarks = [decoder decodeIntForKey: @"NSNumberOfTickMarks"]; _tickMarkPosition = [decoder decodeIntForKey: @"NSTickMarkPosition"]; [self setMinValue: [decoder decodeFloatForKey: @"NSMinValue"]]; [self setMaxValue: [decoder decodeFloatForKey: @"NSMaxValue"]]; [self setFloatValue: [decoder decodeFloatForKey: @"NSValue"]]; _altIncrementValue = [decoder decodeFloatForKey: @"NSAltIncValue"]; [self setSliderType: [decoder decodeIntForKey: @"NSSliderType"]]; // do these here, since the Cocoa version of the class does not save these values... _knobCell = [NSCell new]; _titleCell = [NSTextFieldCell new]; [_titleCell setTextColor: [NSColor controlTextColor]]; [_titleCell setStringValue: @""]; [_titleCell setAlignment: NSCenterTextAlignment]; _isVertical = -1; } else { float minValue, maxValue; [decoder decodeValuesOfObjCTypes: "fffi", &minValue, &maxValue, &_altIncrementValue, &_isVertical]; [self setMinValue: minValue]; [self setMaxValue: maxValue]; [decoder decodeValueOfObjCType: @encode(id) at: &_titleCell]; [decoder decodeValueOfObjCType: @encode(id) at: &_knobCell]; if ([decoder versionForClassName: @"NSSliderCell"] >= 2) { [decoder decodeValueOfObjCType: @encode(BOOL) at: &_allowsTickMarkValuesOnly]; [decoder decodeValueOfObjCType: @encode(int) at: &_numberOfTickMarks]; [decoder decodeValueOfObjCType: @encode(int) at: &_tickMarkPosition]; } } return self; } - (void) encodeWithCoder: (NSCoder*)coder { [super encodeWithCoder: coder]; if ([coder allowsKeyedCoding]) { [coder encodeBool: _allowsTickMarkValuesOnly forKey: @"NSAllowsTickMarkValuesOnly"]; [coder encodeInt: _numberOfTickMarks forKey: @"NSNumberOfTickMarks"]; [coder encodeInt: _tickMarkPosition forKey: @"NSTickMarkPosition"]; [coder encodeFloat: _minValue forKey: @"NSMinValue"]; [coder encodeFloat: _maxValue forKey: @"NSMaxValue"]; [coder encodeFloat: _altIncrementValue forKey: @"NSAltIncValue"]; [coder encodeFloat: _minValue forKey: @"NSValue"]; // encoded for compatibility [coder encodeInt: _type forKey: @"NSSliderType"]; } else { [coder encodeValuesOfObjCTypes: "fffi", &_minValue, &_maxValue, &_altIncrementValue, &_isVertical]; [coder encodeValueOfObjCType: @encode(id) at: &_titleCell]; [coder encodeValueOfObjCType: @encode(id) at: &_knobCell]; // New for version 2 [coder encodeValueOfObjCType: @encode(BOOL) at: &_allowsTickMarkValuesOnly]; [coder encodeValueOfObjCType: @encode(int) at: &_numberOfTickMarks]; [coder encodeValueOfObjCType: @encode(int) at: &_tickMarkPosition]; } } @end