/** NSSlider Copyright (C) 1996 Free Software Foundation, Inc. Author: Ovidiu Predescu Date: September 1997 Author: Felipe A. Rodriguez Date: August 1998 This file is part of the GNUstep GUI Library. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; see the file COPYING.LIB. If not, see or write to the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "math.h" // fabs #import #import "AppKit/NSEvent.h" #import "AppKit/NSSlider.h" #import "AppKit/NSSliderCell.h" /** Class Description

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.

*/ @implementation NSSlider static Class cellClass; + (void) initialize { if (self == [NSSlider class]) { // Initial version [self setVersion: 1]; // Set our cell class to NSSliderCell [self setCellClass: [NSSliderCell class]]; } } + (void) setCellClass: (Class)class { cellClass = class; } + (Class) cellClass { return cellClass; } - (id) initWithFrame: (NSRect)frameRect { self = [super initWithFrame: frameRect]; if (self == nil) return nil; [_cell setState: 1]; [_cell setContinuous: YES]; return self; } - (BOOL) isFlipped { return YES; } /**

Returns the value by which the slider will be incremented if the user holds down the ALT key.

See Also: -setAltIncrementValue:

*/ - (double) altIncrementValue { return [_cell altIncrementValue]; } /**

Returns the image drawn in the slider's track. Returns nil if this has not been set.

See Also: -setImage:

*/ - (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. */ - (NSInteger) 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. */ - (CGFloat) knobThickness { return [_cell knobThickness]; } /**

Sets the value by which the slider will be incremented, when the ALT key is held down, to increment.

See Also: -altIncrementValue

*/ - (void) setAltIncrementValue: (double)increment { [_cell setAltIncrementValue: increment]; } /**

Sets the image to be displayed in the slider's track to barImage.

See Also: -image

*/ - (void) setImage: (NSImage *)backgroundImage { [_cell setImage: backgroundImage]; } /**

Sets the thickness of the knob to aFloat, in pixels. This value sets the amount of space which the knob takes up in the slider's track.

See Also: -knobThickness

*/ - (void) setKnobThickness: (CGFloat)aFloat { [_cell setKnobThickness: aFloat]; } /**

Sets the title of the slider to aString. This title is displayed on the slider's track, behind the knob.

See Also: -title

*/ - (void) setTitle: (NSString *)aString { [_cell setTitle: aString]; } /**

Sets the cell used to draw the title to aCell.

See Also: -titleCell

*/ - (void) setTitleCell: (NSCell *)aCell { [_cell setTitleCell: aCell]; } /**

Sets the colour with which the title will be drawn to aColor.

See Also -titleColor

*/ - (void) setTitleColor: (NSColor *)aColor { [_cell setTitleColor: aColor]; } /**

Sets the font with which the title will be drawm to fontObject.

See Also: -titleFont

*/ - (void) setTitleFont: (NSFont *)fontObject { [_cell setTitleFont: fontObject]; } /**

Returns the title of the slider as an NSString.

See Also: -setTitle:

*/ - (NSString *) title { return [_cell title]; } /**

Returns the cell used to draw the title.

See Also: -setTitleCell:

*/ - (id) titleCell { return [_cell titleCell]; } /**

Returns the colour used to draw the title.

See Also: -setTitleColor:

*/ - (NSColor *) titleColor { return [_cell titleColor]; } /**

Returns the font used to draw the title.

See Also: -setTitleFont:

*/ - (NSFont *) titleFont { return [_cell titleFont]; } /**

Returns the maximum value that the slider represents.

See Also: -setMaxValue:

*/ - (double) maxValue { return [_cell maxValue]; } /**

Returns the minimum value that the slider represents.

See Also: -setMinValue:

*/ - (double) minValue { return [_cell minValue]; } /**

Sets the maximum value that the sliders represents to aDouble.

See Also: -maxValue

*/ - (void) setMaxValue: (double)aDouble { [_cell setMaxValue: aDouble]; } /**

Sets the minimum value that the slider represents to aDouble.

See Also: -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; } - (void) keyDown: (NSEvent *)ev { NSString *characters = [ev characters]; int i, length = [characters length]; double value = [self doubleValue]; double min = [_cell minValue]; double max = [_cell maxValue]; double altValue = [_cell altIncrementValue]; NSUInteger alt_down = ([ev modifierFlags] & NSAlternateKeyMask); BOOL only_ticks = [_cell allowsTickMarkValuesOnly]; BOOL valueChanged = NO; double diff; if (alt_down && altValue != -1) { diff = altValue; } else if (only_ticks) { if ([_cell numberOfTickMarks]) { double tick0 = [_cell tickMarkValueAtIndex: 0]; double tick1 = [_cell tickMarkValueAtIndex: 1]; diff = tick1 - tick0; } else { diff = 0.0; } } else { diff = fabs(min - max) / 20; } for (i = 0; i < length; i++) { switch ([characters characterAtIndex: i]) { case NSLeftArrowFunctionKey: case NSDownArrowFunctionKey: value -= diff; valueChanged = YES; break; case NSUpArrowFunctionKey: case NSRightArrowFunctionKey: value += diff; valueChanged = YES; break; case NSPageDownFunctionKey: value -= diff * 2; valueChanged = YES; break; case NSPageUpFunctionKey: value += diff * 2; valueChanged = YES; break; case NSHomeFunctionKey: value = min; valueChanged = YES; break; case NSEndFunctionKey: value = max; valueChanged = YES; break; } } if (valueChanged) { if (only_ticks) value = [_cell closestTickMarkValueToValue: value]; if (value < min) { value = min; } else if (value > max) { value = max; } [self setDoubleValue: value]; [self sendAction: [self action] to: [self target]]; return; } [super keyDown: ev]; } // ticks - (BOOL) allowsTickMarkValuesOnly { return [_cell allowsTickMarkValuesOnly]; } - (double) closestTickMarkValueToValue: (double)aValue { return [_cell closestTickMarkValueToValue: aValue]; } - (NSInteger) indexOfTickMarkAtPoint: (NSPoint)point { return [_cell indexOfTickMarkAtPoint: point]; } - (NSInteger) numberOfTickMarks { return [_cell numberOfTickMarks]; } - (NSRect) rectOfTickMarkAtIndex: (NSInteger)index { return [_cell rectOfTickMarkAtIndex: index]; } - (void) setAllowsTickMarkValuesOnly: (BOOL)flag { [_cell setAllowsTickMarkValuesOnly: flag]; } - (void) setNumberOfTickMarks: (NSInteger)numberOfTickMarks { [_cell setNumberOfTickMarks: numberOfTickMarks]; } - (void) setTickMarkPosition: (NSTickMarkPosition)position { [_cell setTickMarkPosition: position]; } - (NSTickMarkPosition) tickMarkPosition { return [_cell tickMarkPosition]; } - (double) tickMarkValueAtIndex: (NSInteger)index { return [_cell tickMarkValueAtIndex: index]; } @end