/**
An NSSlider displays, and allows control of, some value in the
application. It represents a continuous stream of values of type
float
, which can be retrieved by the method
floatValue
and set by the method
setFloatValue:
.
This control is a continuous control. It sends its action
message as long as the user is manipulating it. This can be changed
by passing NO
to the setContinuous:
message of a given NSSlider.
Although methods for adding and managing a title are provided, the slider's knob can cover this title, so it is recommended that a label be added near the slider, for identification.
As with many controls, NSSlider relies on its cell counterpart, NSSliderCell. For more information, please see the specification for NSSliderCell.
Use of an NSSlider to do the role of an NSScroller is not recommended. A scroller is intended to represent the visible portion of a view, whereas a slider is intended to represent some value.
nil
if this has not been set. */
- (NSImage *) image
{
return [_cell image];
}
/**
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 [_cell isVertical];
}
/**
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. */
- (float) knobThickness
{
return [_cell knobThickness];
}
/**
Sets the value by which the slider will be incremented, when the
ALT key is held down, to increment. */
- (void) setAltIncrementValue: (double)increment
{
[_cell setAltIncrementValue: increment];
}
/** Sets the image to be displayed in the slider's track to barImage.
*/
- (void) setImage: (NSImage *)backgroundImage
{
[_cell setImage: backgroundImage];
}
/**
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. */
- (void) setKnobThickness: (float)aFloat
{
[_cell setKnobThickness: aFloat];
}
/**
Sets the title of the slider to barTitle. This title is displayed
on the slider's track, behind the knob.
*/
- (void) setTitle: (NSString *)aString
{
[_cell setTitle: aString];
}
/** Sets the cell used to draw the title to titleCell. */
- (void) setTitleCell: (NSCell *)aCell
{
[_cell setTitleCell: aCell];
}
/** Sets the colour with which the title will be drawn to color. */
- (void) setTitleColor: (NSColor *)aColor
{
[_cell setTitleColor: aColor];
}
/** Sets the font with which the title will be drawm to font. */
- (void) setTitleFont: (NSFont *)fontObject
{
[_cell setTitleFont: fontObject];
}
/** Returns the title of the slider as an NSString
. */
- (NSString *) title
{
return [_cell title];
}
/** Returns the cell used to draw the title. */
- (id) titleCell
{
return [_cell titleCell];
}
/** Returns the colour used to draw the title. */
- (NSColor *) titleColor
{
return [_cell titleColor];
}
/** Returns the font used to draw the title. */
- (NSFont *) titleFont
{
return [_cell titleFont];
}
/** Returns the maximum value that the slider represents. */
- (double) maxValue
{
return [_cell maxValue];
}
/** Returns the minimum value that the slider represents. */
- (double) minValue
{
return [_cell minValue];
}
/**
Sets the maximum value that the sliders represents to maxValue. */
- (void) setMaxValue: (double)aDouble
{
[_cell setMaxValue: aDouble];
}
/** Sets the minimum value that the slider represents to minValue. */
- (void) setMinValue: (double)aDouble
{
[_cell setMinValue: aDouble];
}
/**
Returns YES
by default. This will allow the first
click sent to the slider, when in an inactive window, to both bring
the window into focus and manipulate the slider. */
- (BOOL) acceptsFirstMouse: (NSEvent *)theEvent
{
return YES;
}
// ticks
- (BOOL) allowsTickMarkValuesOnly
{
return [_cell allowsTickMarkValuesOnly];
}
- (double) closestTickMarkValueToValue: (double)aValue
{
return [_cell closestTickMarkValueToValue: aValue];
}
- (int) indexOfTickMarkAtPoint: (NSPoint)point
{
return [_cell indexOfTickMarkAtPoint: point];
}
- (int) numberOfTickMarks
{
return [_cell numberOfTickMarks];
}
- (NSRect) rectOfTickMarkAtIndex: (int)index
{
return [_cell rectOfTickMarkAtIndex: index];
}
- (void) setAllowsTickMarkValuesOnly: (BOOL)flag
{
[_cell setAllowsTickMarkValuesOnly: flag];
}
- (void) setNumberOfTickMarks: (int)numberOfTickMarks
{
[_cell setNumberOfTickMarks: numberOfTickMarks];
}
- (void) setTickMarkPosition: (NSTickMarkPosition)position
{
[_cell setTickMarkPosition: position];
}
- (NSTickMarkPosition) tickMarkPosition
{
return [_cell tickMarkPosition];
}
- (double) tickMarkValueAtIndex: (int)index
{
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];
[_cell drawWithFrame: _bounds inView: self];
[_window flushWindow];
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]];
}
[_cell drawWithFrame: _bounds inView: self];
[_window flushWindow];
}
[self trackKnob: theEvent knobRect: rect];
}
}
@end