2001-12-17 16:51:51 +00:00
|
|
|
/** <title>NSSliderCell</title>
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-12-29 07:14:00 +00:00
|
|
|
Copyright (C) 1996,1999 Free Software Foundation, Inc.
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1997-10-09 22:55:31 +00:00
|
|
|
Author: Ovidiu Predescu <ovidiu@net-community.com>
|
|
|
|
Date: September 1997
|
1999-12-29 07:14:00 +00:00
|
|
|
Rewrite: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
|
|
|
Date: 1999
|
1999-03-02 08:58:30 +00:00
|
|
|
|
1996-05-30 20:03:15 +00:00
|
|
|
This file is part of the GNUstep GUI Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-10-29 21:16:17 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1996-05-30 20:03:15 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-10 04:01:49 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
2007-10-29 21:16:17 +00:00
|
|
|
|
1996-05-30 20:03:15 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2007-10-29 21:16:17 +00:00
|
|
|
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 <http://www.gnu.org/licenses/> or write to the
|
|
|
|
Free Software Foundation, 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
1997-10-09 22:55:31 +00:00
|
|
|
*/
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2003-04-25 22:55:18 +00:00
|
|
|
#include <math.h> // (float)rintf(float x)
|
2003-07-31 23:52:10 +00:00
|
|
|
#include "config.h"
|
2009-11-16 12:26:38 +00:00
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
#import <Foundation/NSException.h>
|
2009-12-19 00:05:52 +00:00
|
|
|
#import <Foundation/NSValue.h>
|
2009-11-16 12:26:38 +00:00
|
|
|
|
|
|
|
#import "AppKit/NSApplication.h"
|
2010-03-17 00:18:30 +00:00
|
|
|
#import "AppKit/NSBezierPath.h"
|
2009-11-16 12:26:38 +00:00
|
|
|
#import "AppKit/NSColor.h"
|
|
|
|
#import "AppKit/NSControl.h"
|
|
|
|
#import "AppKit/NSEvent.h"
|
|
|
|
#import "AppKit/NSGraphics.h"
|
|
|
|
#import "AppKit/NSImage.h"
|
|
|
|
#import "AppKit/NSSliderCell.h"
|
|
|
|
#import "AppKit/NSTextFieldCell.h"
|
|
|
|
#import "AppKit/NSWindow.h"
|
2010-11-26 22:48:46 +00:00
|
|
|
#import <GNUstepGUI/GSTheme.h>
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2011-07-12 21:12:22 +00:00
|
|
|
#import "GSGuiPrivate.h"
|
|
|
|
|
2010-04-28 18:14:43 +00:00
|
|
|
#ifndef HAVE_ATAN2F
|
|
|
|
#define atan2f atan2
|
|
|
|
#endif
|
1998-11-25 17:16:48 +00:00
|
|
|
|
2010-03-17 00:18:30 +00:00
|
|
|
#ifndef M_PI
|
|
|
|
#define M_PI 3.1415926535897932384626434
|
|
|
|
#endif
|
|
|
|
|
2006-08-11 14:08:32 +00:00
|
|
|
static inline
|
2018-03-11 18:09:08 +00:00
|
|
|
double _doubleValueForMousePoint (NSPoint point, NSRect knobRect,
|
2006-08-11 14:08:32 +00:00
|
|
|
NSRect slotRect, BOOL isVertical,
|
2013-02-17 22:44:47 +00:00
|
|
|
double minValue, double maxValue,
|
2010-03-17 00:18:30 +00:00
|
|
|
NSSliderCell *theCell, BOOL flipped,
|
|
|
|
BOOL isCircular)
|
2006-08-11 14:08:32 +00:00
|
|
|
{
|
2018-03-11 18:09:08 +00:00
|
|
|
double doubleValue = 0;
|
|
|
|
double position;
|
2006-08-11 14:08:32 +00:00
|
|
|
|
2010-03-17 00:18:30 +00:00
|
|
|
if (isCircular)
|
2006-08-11 14:08:32 +00:00
|
|
|
{
|
2010-03-17 00:18:30 +00:00
|
|
|
NSPoint slotCenter = NSMakePoint(NSMidX(slotRect), NSMidY(slotRect));
|
|
|
|
NSPoint pointRelativeToKnobCenter = NSMakePoint(point.x - slotCenter.x,
|
|
|
|
point.y - slotCenter.y);
|
|
|
|
if (flipped)
|
2006-08-11 14:08:32 +00:00
|
|
|
{
|
2010-03-17 00:18:30 +00:00
|
|
|
pointRelativeToKnobCenter.y *= -1.0;
|
2006-08-11 14:08:32 +00:00
|
|
|
}
|
2018-03-11 18:09:08 +00:00
|
|
|
doubleValue = atan2f(pointRelativeToKnobCenter.x,
|
2010-03-17 00:18:30 +00:00
|
|
|
pointRelativeToKnobCenter.y) / (2.0 * M_PI);
|
2018-03-11 18:09:08 +00:00
|
|
|
if (doubleValue < 0)
|
2006-08-11 14:08:32 +00:00
|
|
|
{
|
2018-03-11 18:09:08 +00:00
|
|
|
doubleValue += 1.0;
|
2006-08-11 14:08:32 +00:00
|
|
|
}
|
2018-03-11 18:09:08 +00:00
|
|
|
// doubleValue is 0 for up, 0.25 for right, 0.5 for down, 0.75 for left, etc.
|
2006-08-11 14:08:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-17 00:18:30 +00:00
|
|
|
// Adjust the point to lie inside the knob slot. We don't
|
|
|
|
// have to worry whether the view is flipped or not.
|
|
|
|
if (isVertical)
|
2006-08-11 14:08:32 +00:00
|
|
|
{
|
2010-03-17 00:18:30 +00:00
|
|
|
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;
|
2018-03-11 18:09:08 +00:00
|
|
|
// Compute the double value
|
|
|
|
doubleValue = (position - (slotRect.origin.y + knobRect.size.height/2))
|
2010-03-17 00:18:30 +00:00
|
|
|
/ (slotRect.size.height - knobRect.size.height);
|
|
|
|
if (flipped)
|
2018-03-11 18:09:08 +00:00
|
|
|
doubleValue = 1 - doubleValue;
|
2006-08-11 14:08:32 +00:00
|
|
|
}
|
|
|
|
else
|
2010-03-17 00:18:30 +00:00
|
|
|
{
|
|
|
|
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;
|
2006-08-11 14:08:32 +00:00
|
|
|
|
2018-03-11 18:09:08 +00:00
|
|
|
// Compute the double value given the knob size
|
|
|
|
doubleValue = (position - (slotRect.origin.x + knobRect.size.width / 2))
|
2010-03-17 00:18:30 +00:00
|
|
|
/ (slotRect.size.width - knobRect.size.width);
|
|
|
|
}
|
2006-08-11 14:08:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:09:08 +00:00
|
|
|
return doubleValue * (maxValue - minValue) + minValue;
|
2006-08-11 14:08:32 +00:00
|
|
|
}
|
|
|
|
|
2001-12-17 16:51:51 +00:00
|
|
|
/**
|
|
|
|
<unit>
|
|
|
|
<heading>Class Description</heading>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
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.
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
An NSSliderCell can be customized through its
|
|
|
|
<code>set...</code> methods. If these do not provide enough
|
|
|
|
customization, a subclass can be created, which overrides any of the
|
|
|
|
follwing methods: <code>knobRectFlipped:</code>,
|
|
|
|
<code>drawBarInside:flipped:</code>, <code>drawKnob:</code>, or
|
|
|
|
<code>prefersTrackingUntilMouseUp</code>.
|
|
|
|
</p>
|
|
|
|
</unit>
|
|
|
|
*/
|
1997-10-09 22:55:31 +00:00
|
|
|
@implementation NSSliderCell
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2003-04-25 22:55:18 +00:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [NSSliderCell class])
|
|
|
|
{
|
|
|
|
/* Set the class version to 2, as tick information is now
|
|
|
|
stored in the encoding */
|
|
|
|
[self setVersion: 2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-03-16 22:43:34 +00:00
|
|
|
- (id) init
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2000-04-13 07:55:19 +00:00
|
|
|
self = [self initImageCell: nil];
|
2009-11-16 12:26:38 +00:00
|
|
|
if (self == nil)
|
|
|
|
return nil;
|
|
|
|
|
1997-10-09 22:55:31 +00:00
|
|
|
_altIncrementValue = -1;
|
|
|
|
_isVertical = -1;
|
2018-03-11 18:09:08 +00:00
|
|
|
[self setDoubleValue: 0];
|
2009-12-19 00:05:52 +00:00
|
|
|
[self setMinValue: 0];
|
|
|
|
[self setMaxValue: 1];
|
1999-12-13 02:49:13 +00:00
|
|
|
_cell.is_bordered = YES;
|
2009-11-16 12:26:38 +00:00
|
|
|
_cell.is_bezeled = NO;
|
2009-12-19 23:05:48 +00:00
|
|
|
[self setContinuous: YES];
|
2010-03-17 00:18:30 +00:00
|
|
|
[self setSliderType: NSLinearSlider];
|
2009-12-19 23:05:48 +00:00
|
|
|
|
1997-10-09 22:55:31 +00:00
|
|
|
_knobCell = [NSCell new];
|
2000-09-03 19:27:02 +00:00
|
|
|
_titleCell = [NSTextFieldCell new];
|
|
|
|
[_titleCell setTextColor: [NSColor controlTextColor]];
|
|
|
|
[_titleCell setStringValue: @""];
|
|
|
|
[_titleCell setAlignment: NSCenterTextAlignment];
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1999-03-16 22:43:34 +00:00
|
|
|
- (void) dealloc
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2000-04-13 07:55:19 +00:00
|
|
|
RELEASE(_titleCell);
|
|
|
|
RELEASE(_knobCell);
|
1996-05-30 20:03:15 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2006-11-11 18:11:21 +00:00
|
|
|
- (id) copyWithZone:(NSZone *)zone
|
|
|
|
{
|
2009-11-16 12:26:38 +00:00
|
|
|
NSSliderCell *cpy = [super copyWithZone: zone];
|
2006-11-11 18:11:21 +00:00
|
|
|
|
2009-11-16 12:26:38 +00:00
|
|
|
if (cpy != nil)
|
|
|
|
{
|
|
|
|
/* since NSCells call to NSCopyObject only copies object addresses */
|
|
|
|
cpy->_titleCell = [_titleCell copyWithZone: zone];
|
|
|
|
cpy->_knobCell = [_knobCell copyWithZone: zone];
|
|
|
|
}
|
2006-11-11 18:11:21 +00:00
|
|
|
|
2009-11-16 12:26:38 +00:00
|
|
|
return cpy;
|
1999-03-16 22:43:34 +00:00
|
|
|
}
|
|
|
|
|
2009-12-20 22:21:40 +00:00
|
|
|
- (BOOL) isContinuous
|
|
|
|
{
|
|
|
|
return (_action_mask & NSLeftMouseDraggedMask) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setContinuous: (BOOL)flag
|
|
|
|
{
|
|
|
|
if (flag)
|
|
|
|
{
|
|
|
|
_action_mask |= NSLeftMouseDraggedMask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_action_mask &= ~NSLeftMouseDraggedMask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-03 04:51:10 +00:00
|
|
|
- (void) _drawBorderAndBackgroundWithFrame: (NSRect)cellFrame
|
|
|
|
inView: (NSView*)controlView
|
|
|
|
{
|
|
|
|
NSBorderType aType;
|
|
|
|
|
|
|
|
if (_cell.is_bordered)
|
|
|
|
aType = NSLineBorder;
|
|
|
|
else if (_cell.is_bezeled)
|
|
|
|
aType = NSBezelBorder;
|
|
|
|
else
|
|
|
|
aType = NSNoBorder;
|
|
|
|
|
|
|
|
[[GSTheme theme] drawSliderBorderAndBackground: aType
|
|
|
|
frame: cellFrame
|
|
|
|
inCell: self
|
|
|
|
isHorizontal: ![self isVertical]];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/** <p>Draws the slider's track, not including the bezel, in <var>aRect</var>
|
2001-12-17 16:51:51 +00:00
|
|
|
<var>flipped</var> indicates whether the control view has a flipped
|
|
|
|
coordinate system.</p>
|
|
|
|
|
|
|
|
<p>Do not call this method directly, it is provided for subclassing
|
|
|
|
only.</p> */
|
1999-03-16 22:43:34 +00:00
|
|
|
- (void) drawBarInside: (NSRect)rect flipped: (BOOL)flipped
|
|
|
|
{
|
2010-11-26 22:48:46 +00:00
|
|
|
[[GSTheme theme] drawBarInside: rect
|
|
|
|
inCell: self
|
|
|
|
flipped: flipped];
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p>Returns the rect in which to draw the knob, based on the
|
2001-12-17 16:51:51 +00:00
|
|
|
coordinate system of the NSSlider or NSMatrix this NSSliderCell is
|
|
|
|
associated with. <var>flipped</var> indicates whether or not that
|
|
|
|
coordinate system is flipped, which can be determined by sending the
|
|
|
|
<code>isFlipped</code> message to the associated NSSlider or
|
|
|
|
NSMatrix.</p>
|
|
|
|
|
|
|
|
<p>Do not call this method directly. It is included for subclassing
|
|
|
|
only.</p> */
|
1999-03-16 22:43:34 +00:00
|
|
|
- (NSRect) knobRectFlipped: (BOOL)flipped
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2009-11-16 12:26:38 +00:00
|
|
|
NSImage *image = [_knobCell image];
|
|
|
|
NSSize size;
|
|
|
|
NSPoint origin;
|
2018-03-11 18:09:08 +00:00
|
|
|
double doubleValue = _value;
|
|
|
|
|
2010-03-17 00:18:30 +00:00
|
|
|
// FIXME: this method needs to be refactored out to GSTheme
|
1999-03-02 08:58:30 +00:00
|
|
|
if (_isVertical && flipped)
|
2000-04-13 07:55:19 +00:00
|
|
|
{
|
2018-03-11 18:09:08 +00:00
|
|
|
doubleValue = _maxValue + _minValue - doubleValue;
|
2000-04-13 07:55:19 +00:00
|
|
|
}
|
1999-03-02 08:58:30 +00:00
|
|
|
|
2018-03-11 18:09:08 +00:00
|
|
|
doubleValue = (doubleValue - _minValue) / (_maxValue - _minValue);
|
1999-03-02 08:58:30 +00:00
|
|
|
|
2006-01-04 12:41:05 +00:00
|
|
|
if (image != nil)
|
|
|
|
{
|
|
|
|
size = [image size];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size = NSZeroSize;
|
|
|
|
}
|
1999-03-02 08:58:30 +00:00
|
|
|
|
2000-04-13 07:55:19 +00:00
|
|
|
if (_isVertical == YES)
|
1999-03-02 08:58:30 +00:00
|
|
|
{
|
2000-04-12 17:20:21 +00:00
|
|
|
origin = _trackRect.origin;
|
2013-10-03 04:51:10 +00:00
|
|
|
origin.x += (_trackRect.size.width - size.width) / 2.0; // center horizontally
|
2018-03-11 18:09:08 +00:00
|
|
|
origin.y += (_trackRect.size.height - size.height) * doubleValue;
|
1999-03-02 08:58:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-04-12 17:20:21 +00:00
|
|
|
origin = _trackRect.origin;
|
2018-03-11 18:09:08 +00:00
|
|
|
origin.x += (_trackRect.size.width - size.width) * doubleValue;
|
2013-10-03 04:51:10 +00:00
|
|
|
origin.y += (_trackRect.size.height - size.height) / 2.0; // center vertically
|
1999-03-02 08:58:30 +00:00
|
|
|
}
|
|
|
|
|
2013-10-03 04:51:10 +00:00
|
|
|
{
|
|
|
|
NSRect result = NSMakeRect (origin.x, origin.y, size.width, size.height);
|
|
|
|
|
|
|
|
if ([self controlView])
|
|
|
|
{
|
|
|
|
result = [[self controlView] centerScanRect: result];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/** <p>Calculates the rect in which to draw the knob, then calls
|
2001-12-17 16:51:51 +00:00
|
|
|
<code>drawKnob:</code> Before calling this method, a
|
|
|
|
<code>lockFocus</code> message must be sent to the cell's control
|
|
|
|
view.</p>
|
|
|
|
|
|
|
|
<p>When subclassing NSSliderCell, do not override this method.
|
2005-11-18 09:14:44 +00:00
|
|
|
Override <code>drawKnob:</code> instead.</p> <p>See Also: -drawKnob:</p>
|
|
|
|
*/
|
1999-03-16 22:43:34 +00:00
|
|
|
- (void) drawKnob
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2010-11-26 22:48:46 +00:00
|
|
|
[[GSTheme theme] drawKnobInCell: self];
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p>Draws the knob in <var>knobRect</var>. Before calling this
|
2001-12-17 16:51:51 +00:00
|
|
|
method, a <code>lockFocus</code> message must be sent to the cell's
|
|
|
|
control view.</p>
|
|
|
|
|
|
|
|
<p>Do not call this method directly. It is included for subclassing
|
2005-11-18 09:14:44 +00:00
|
|
|
only.</p> <p>See Also: -drawKnob</p>
|
|
|
|
*/
|
1999-03-16 22:43:34 +00:00
|
|
|
- (void) drawKnob: (NSRect)knobRect
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2000-09-03 19:27:02 +00:00
|
|
|
[_knobCell drawInteriorWithFrame: knobRect inView: _control_view];
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
1999-03-16 22:43:34 +00:00
|
|
|
- (void) drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView*)controlView
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2000-04-12 21:44:07 +00:00
|
|
|
cellFrame = [self drawingRectForBounds: cellFrame];
|
2010-03-17 00:18:30 +00:00
|
|
|
_trackRect = cellFrame;
|
2000-04-12 17:20:21 +00:00
|
|
|
|
2010-03-17 00:18:30 +00:00
|
|
|
if (_type == NSCircularSlider)
|
2000-04-13 07:55:19 +00:00
|
|
|
{
|
2010-03-17 00:18:30 +00:00
|
|
|
NSBezierPath *circle;
|
|
|
|
NSPoint knobCenter;
|
|
|
|
NSPoint point;
|
|
|
|
NSRect knobRect;
|
2018-03-11 18:09:08 +00:00
|
|
|
double fraction, angle, radius;
|
2013-10-03 06:55:45 +00:00
|
|
|
NSImage *image;
|
2010-03-17 00:18:30 +00:00
|
|
|
|
|
|
|
if (cellFrame.size.width > cellFrame.size.height)
|
2000-04-13 07:55:19 +00:00
|
|
|
{
|
2010-03-17 00:18:30 +00:00
|
|
|
knobRect = NSMakeRect(cellFrame.origin.x + ((cellFrame.size.width -
|
|
|
|
cellFrame.size.height) / 2.0),
|
|
|
|
cellFrame.origin.y,
|
|
|
|
cellFrame.size.height,
|
|
|
|
cellFrame.size.height);
|
2000-04-13 07:55:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-17 00:18:30 +00:00
|
|
|
knobRect = NSMakeRect(cellFrame.origin.x,
|
|
|
|
cellFrame.origin.y + ((cellFrame.size.height -
|
|
|
|
cellFrame.size.width) / 2.0),
|
|
|
|
cellFrame.size.width,
|
|
|
|
cellFrame.size.width);
|
|
|
|
}
|
|
|
|
|
2013-10-03 06:55:45 +00:00
|
|
|
if ([self controlView])
|
|
|
|
knobRect = [[self controlView] centerScanRect: knobRect];
|
|
|
|
|
|
|
|
image = [NSImage imageNamed: @"common_CircularSlider"];
|
|
|
|
if (image != nil)
|
|
|
|
{
|
|
|
|
[image drawInRect: knobRect
|
|
|
|
fromRect: NSZeroRect
|
|
|
|
operation: NSCompositeSourceOver
|
|
|
|
fraction: 1.0
|
|
|
|
respectFlipped: YES
|
|
|
|
hints: nil];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
knobRect = NSInsetRect(knobRect, 1, 1);
|
|
|
|
circle = [NSBezierPath bezierPathWithOvalInRect: knobRect];
|
|
|
|
[[NSColor controlBackgroundColor] set];
|
|
|
|
[circle fill];
|
|
|
|
[[NSColor blackColor] set];
|
|
|
|
[circle stroke];
|
|
|
|
}
|
|
|
|
|
|
|
|
knobCenter = NSMakePoint(NSMidX(knobRect), NSMidY(knobRect));
|
2010-03-17 00:18:30 +00:00
|
|
|
|
2018-03-11 18:09:08 +00:00
|
|
|
fraction = ([self doubleValue] - [self minValue]) /
|
2010-03-17 00:18:30 +00:00
|
|
|
([self maxValue] - [self minValue]);
|
|
|
|
angle = (fraction * (2.0 * M_PI)) - (M_PI / 2.0);
|
2013-10-03 06:55:45 +00:00
|
|
|
radius = (knobRect.size.height / 2) - 6;
|
2010-03-17 00:18:30 +00:00
|
|
|
point = NSMakePoint((radius * cos(angle)) + knobCenter.x,
|
|
|
|
(radius * sin(angle)) + knobCenter.y);
|
|
|
|
|
2013-10-03 06:55:45 +00:00
|
|
|
image = [NSImage imageNamed: @"common_Dimple"];
|
|
|
|
{
|
|
|
|
NSSize size = [image size];
|
|
|
|
NSRect dimpleRect = NSMakeRect(point.x - (size.width / 2.0),
|
|
|
|
point.y - (size.height / 2.0),
|
|
|
|
size.width,
|
|
|
|
size.height);
|
|
|
|
|
|
|
|
if ([self controlView])
|
|
|
|
dimpleRect = [[self controlView] centerScanRect: dimpleRect];
|
|
|
|
|
|
|
|
[image drawInRect: dimpleRect
|
|
|
|
fromRect: NSZeroRect
|
|
|
|
operation: NSCompositeSourceOver
|
|
|
|
fraction: 1.0
|
|
|
|
respectFlipped: YES
|
|
|
|
hints: nil];
|
|
|
|
}
|
2010-03-17 00:18:30 +00:00
|
|
|
}
|
|
|
|
else if (_type == NSLinearSlider)
|
|
|
|
{
|
|
|
|
BOOL vertical = (cellFrame.size.height > cellFrame.size.width);
|
|
|
|
|
|
|
|
if (vertical != _isVertical)
|
|
|
|
{
|
2013-10-03 04:51:10 +00:00
|
|
|
NSImage *image;
|
2010-03-17 00:18:30 +00:00
|
|
|
if (vertical == YES)
|
|
|
|
{
|
|
|
|
image = [NSImage imageNamed: @"common_SliderVert"];
|
|
|
|
}
|
|
|
|
else
|
2006-01-04 12:41:05 +00:00
|
|
|
{
|
2010-03-17 00:18:30 +00:00
|
|
|
image = [NSImage imageNamed: @"common_SliderHoriz"];
|
2006-01-04 12:41:05 +00:00
|
|
|
}
|
2010-03-17 00:18:30 +00:00
|
|
|
[_knobCell setImage: image];
|
2000-04-13 07:55:19 +00:00
|
|
|
}
|
2010-03-17 00:18:30 +00:00
|
|
|
_isVertical = vertical;
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2010-03-17 00:18:30 +00:00
|
|
|
[self drawBarInside: cellFrame flipped: [controlView isFlipped]];
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2010-03-17 00:18:30 +00:00
|
|
|
/* Draw title - Uhmmm - shouldn't this better go into
|
|
|
|
drawBarInside:flipped: ? */
|
|
|
|
if (_isVertical == NO)
|
|
|
|
{
|
|
|
|
[_titleCell drawInteriorWithFrame: cellFrame inView: controlView];
|
|
|
|
}
|
2000-09-03 19:27:02 +00:00
|
|
|
|
2010-03-17 00:18:30 +00:00
|
|
|
[self drawKnob];
|
2000-04-13 07:55:19 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
1999-11-16 23:54:10 +00:00
|
|
|
- (BOOL) isOpaque
|
|
|
|
{
|
2010-03-17 00:18:30 +00:00
|
|
|
return NO;
|
1999-11-16 23:54:10 +00:00
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p> 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.</p>
|
|
|
|
<p>See Also: -setKnobThickness:</p>
|
|
|
|
*/
|
2013-02-17 22:44:47 +00:00
|
|
|
- (CGFloat) knobThickness
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2006-01-04 12:41:05 +00:00
|
|
|
NSImage *image = [_knobCell image];
|
|
|
|
NSSize size;
|
|
|
|
|
|
|
|
if (image != nil)
|
|
|
|
{
|
|
|
|
size = [image size];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-16 12:26:38 +00:00
|
|
|
return 0;
|
2006-01-04 12:41:05 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1997-10-09 22:55:31 +00:00
|
|
|
return _isVertical ? size.height : size.width;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p>Sets the thickness of the knob to <var>thickness</var>, in pixels.
|
2001-12-17 16:51:51 +00:00
|
|
|
This value sets the amount of space which the knob takes up in the
|
2005-11-18 09:14:44 +00:00
|
|
|
slider's track.</p><p>See Also: -knobThickness</p>
|
|
|
|
*/
|
2013-02-17 22:44:47 +00:00
|
|
|
- (void) setKnobThickness: (CGFloat)thickness
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2006-01-04 12:41:05 +00:00
|
|
|
NSImage *image = [_knobCell image];
|
|
|
|
NSSize size;
|
|
|
|
|
|
|
|
if (image != nil)
|
|
|
|
{
|
|
|
|
size = [image size];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2000-04-13 07:55:19 +00:00
|
|
|
if (_isVertical == YES)
|
1997-10-09 22:55:31 +00:00
|
|
|
size.height = thickness;
|
|
|
|
else
|
|
|
|
size.width = thickness;
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-03-16 22:43:34 +00:00
|
|
|
[image setSize: size];
|
2003-04-25 22:55:18 +00:00
|
|
|
|
|
|
|
if ((_control_view != nil) &&
|
|
|
|
([_control_view isKindOfClass: [NSControl class]]))
|
|
|
|
{
|
|
|
|
[(NSControl*)_control_view updateCell: self];
|
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p> Sets the value by which the slider will be be incremented when with the
|
|
|
|
ALT key down to <var>increment</var>.</p>
|
|
|
|
<p>See Also: -altIncrementValue</p>
|
|
|
|
*/
|
1999-03-16 22:43:34 +00:00
|
|
|
- (void) setAltIncrementValue: (double)increment
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
1997-10-09 22:55:31 +00:00
|
|
|
_altIncrementValue = increment;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:09:08 +00:00
|
|
|
/** <p>Returns the minimum value that the slider represents.</p>
|
|
|
|
<p>See Also: -setMinValue:</p>
|
|
|
|
*/
|
|
|
|
- (double) minValue
|
|
|
|
{
|
|
|
|
return _minValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**<p>Returns the maximum value that the slider represents.</p>
|
|
|
|
<p>See Also: -setMaxValue:</p>
|
|
|
|
*/
|
|
|
|
- (double) maxValue
|
|
|
|
{
|
|
|
|
return _maxValue;
|
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p> Sets the minimum value that the sliders represents to
|
|
|
|
<var>maxValue</var>.</p><p>See Also: -minValue</p>
|
2001-12-17 16:51:51 +00:00
|
|
|
*/
|
1999-03-16 22:43:34 +00:00
|
|
|
- (void) setMinValue: (double)aDouble
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
1997-10-09 22:55:31 +00:00
|
|
|
_minValue = aDouble;
|
2018-03-11 18:09:08 +00:00
|
|
|
if (_minValue > _maxValue)
|
|
|
|
{
|
|
|
|
_value = _minValue;
|
|
|
|
}
|
|
|
|
else if (_value < _minValue)
|
|
|
|
{
|
|
|
|
_value = _minValue;
|
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/** <p>Sets the maximum value that the sliders represents to
|
|
|
|
<var>maxValue</var>.</p><p>See Also: -maxValue</p>
|
2001-12-17 16:51:51 +00:00
|
|
|
*/
|
1999-03-16 22:43:34 +00:00
|
|
|
- (void) setMaxValue: (double)aDouble
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
1997-10-09 22:55:31 +00:00
|
|
|
_maxValue = aDouble;
|
2018-03-11 18:09:08 +00:00
|
|
|
if (_minValue > _maxValue)
|
|
|
|
{
|
|
|
|
_value = _minValue;
|
|
|
|
}
|
|
|
|
else if (_value > _maxValue)
|
|
|
|
{
|
|
|
|
_value = _maxValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (double) doubleValue
|
|
|
|
{
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setDoubleValue: (double)aDouble
|
|
|
|
{
|
|
|
|
if (_minValue > _maxValue)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (aDouble < _minValue)
|
|
|
|
{
|
|
|
|
_value = _minValue;
|
|
|
|
}
|
|
|
|
else if (aDouble > _maxValue)
|
|
|
|
{
|
|
|
|
_value = _maxValue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_value = aDouble;
|
|
|
|
}
|
|
|
|
if ((_control_view != nil) &&
|
|
|
|
([_control_view isKindOfClass: [NSControl class]]))
|
|
|
|
{
|
|
|
|
[(NSControl*)_control_view updateCell: self];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (float) floatValue
|
|
|
|
{
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setFloatValue: (float)aFloat
|
|
|
|
{
|
|
|
|
if (_minValue > _maxValue)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (aFloat < _minValue)
|
|
|
|
{
|
|
|
|
_value = _minValue;
|
|
|
|
}
|
|
|
|
else if (aFloat > _maxValue)
|
|
|
|
{
|
|
|
|
_value = _maxValue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_value = aFloat;
|
|
|
|
}
|
|
|
|
if ((_control_view != nil) &&
|
|
|
|
([_control_view isKindOfClass: [NSControl class]]))
|
|
|
|
{
|
|
|
|
[(NSControl*)_control_view updateCell: self];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) intValue
|
|
|
|
{
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setIntValue: (int)anInt
|
|
|
|
{
|
|
|
|
if (_minValue > _maxValue)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (anInt < _minValue)
|
|
|
|
{
|
|
|
|
_value = _minValue;
|
|
|
|
}
|
|
|
|
else if (anInt > _maxValue)
|
|
|
|
{
|
|
|
|
_value = _maxValue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_value = anInt;
|
|
|
|
}
|
|
|
|
if ((_control_view != nil) &&
|
|
|
|
([_control_view isKindOfClass: [NSControl class]]))
|
|
|
|
{
|
|
|
|
[(NSControl*)_control_view updateCell: self];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 07:14:45 +00:00
|
|
|
- (NSInteger) integerValue
|
|
|
|
{
|
|
|
|
return (NSInteger)_value;
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:09:08 +00:00
|
|
|
- (id) objectValue
|
|
|
|
{
|
|
|
|
return [NSNumber numberWithDouble: _value];
|
2009-12-19 00:05:52 +00:00
|
|
|
}
|
|
|
|
|
2009-12-19 23:05:48 +00:00
|
|
|
- (void) setObjectValue: (id)anObject
|
2009-12-19 00:05:52 +00:00
|
|
|
{
|
2009-12-20 22:21:40 +00:00
|
|
|
// 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)
|
2018-03-11 18:09:08 +00:00
|
|
|
{
|
|
|
|
_value = _minValue;
|
|
|
|
}
|
|
|
|
else
|
2009-12-19 00:05:52 +00:00
|
|
|
{
|
|
|
|
double aDouble = [anObject doubleValue];
|
|
|
|
if (aDouble < _minValue)
|
2018-03-11 18:09:08 +00:00
|
|
|
{
|
|
|
|
_value = _minValue;
|
|
|
|
}
|
2009-12-19 00:05:52 +00:00
|
|
|
else if (aDouble > _maxValue)
|
2018-03-11 18:09:08 +00:00
|
|
|
{
|
|
|
|
_value = _maxValue;
|
|
|
|
}
|
2009-12-19 00:05:52 +00:00
|
|
|
else
|
2018-03-11 18:09:08 +00:00
|
|
|
{
|
|
|
|
_value = aDouble;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((_control_view != nil) &&
|
|
|
|
([_control_view isKindOfClass: [NSControl class]]))
|
|
|
|
{
|
|
|
|
[(NSControl*)_control_view updateCell: self];
|
2009-12-19 00:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p>Returns the cell used to draw the title.</p>
|
|
|
|
<p>See Also: -setTitleCell:</p> */
|
1999-12-29 07:14:00 +00:00
|
|
|
- (id) titleCell
|
|
|
|
{
|
|
|
|
return _titleCell;
|
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p>Returns the colour used to draw the title.</p>
|
|
|
|
<p>See Also: -setTitleColor:</p>
|
|
|
|
*/
|
1999-12-29 07:14:00 +00:00
|
|
|
- (NSColor*) titleColor
|
|
|
|
{
|
|
|
|
return [_titleCell textColor];
|
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p>Returns the font used to draw the title.</p>
|
|
|
|
<p>See Also: -setTitleFont:</p>
|
|
|
|
*/
|
1999-12-29 07:14:00 +00:00
|
|
|
- (NSFont*) titleFont
|
|
|
|
{
|
|
|
|
return [_titleCell font];
|
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p>Sets the title of the slider to <var>barTitle</var>. This title is
|
|
|
|
displayed on the slider's track, behind the knob.</p>
|
|
|
|
<p>See Also: -title</p>*/
|
1999-12-29 07:14:00 +00:00
|
|
|
- (void) setTitle: (NSString*)title
|
|
|
|
{
|
|
|
|
[_titleCell setStringValue: title];
|
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p>Returns the title of the slider. This title is
|
|
|
|
displayed on the slider's track, behind the knob.</p>
|
|
|
|
<p>See Also: -setTitle:</p>
|
|
|
|
*/
|
1999-12-29 07:14:00 +00:00
|
|
|
- (NSString*) title
|
|
|
|
{
|
|
|
|
return [_titleCell stringValue];
|
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p>Sets the cell used to draw the title to <var>titleCell</var>.</p>
|
|
|
|
<p>See Also: -titleCell</p>*/
|
1999-12-29 07:14:00 +00:00
|
|
|
- (void) setTitleCell: (NSCell*)aCell
|
|
|
|
{
|
|
|
|
ASSIGN(_titleCell, aCell);
|
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p>Sets the colour with which the title will be drawn to <var>color</var>.
|
|
|
|
</p><p>See Also: -titleColor</p>
|
|
|
|
*/
|
1999-12-29 07:14:00 +00:00
|
|
|
- (void) setTitleColor: (NSColor*)color
|
|
|
|
{
|
|
|
|
[_titleCell setTextColor: color];
|
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p> Sets the font with which the title will be drawm to <var>font</var>.
|
|
|
|
</p><p>See Also: -titleFont</p>
|
|
|
|
*/
|
1999-12-29 07:14:00 +00:00
|
|
|
- (void) setTitleFont: (NSFont*)font
|
|
|
|
{
|
|
|
|
[_titleCell setFont: font];
|
|
|
|
}
|
|
|
|
|
2010-03-17 00:18:30 +00:00
|
|
|
/**<p>Returns the slider type: linear or circular.</p>
|
|
|
|
<p>See Also: -setSliderType:</p>
|
|
|
|
*/
|
|
|
|
- (NSSliderType)sliderType
|
|
|
|
{
|
|
|
|
return _type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**<p> Sets the type of the slider: linear or circular.
|
|
|
|
</p><p>See Also: -sliderType</p>
|
|
|
|
*/
|
|
|
|
- (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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**Returns whether or not the slider is vertical. If, for some
|
2001-12-17 16:51:51 +00:00
|
|
|
reason, this cannot be determined, for such reasons as the slider is
|
|
|
|
not yet displayed, this method returns -1. Generally, a slider is
|
2005-11-18 09:14:44 +00:00
|
|
|
considered vertical if its height is greater than its width.
|
|
|
|
*/
|
2013-02-17 22:44:47 +00:00
|
|
|
- (NSInteger) isVertical
|
1999-12-29 07:14:00 +00:00
|
|
|
{
|
|
|
|
return _isVertical;
|
|
|
|
}
|
|
|
|
|
2005-11-18 09:14:44 +00:00
|
|
|
/**<p>Returns the value by which the slider is incremented when the user
|
|
|
|
holds down the ALT key.</p><p>See Also: -setAltIncrementValue:</p>
|
2001-12-17 16:51:51 +00:00
|
|
|
*/
|
1999-12-29 07:14:00 +00:00
|
|
|
- (double) altIncrementValue
|
|
|
|
{
|
|
|
|
return _altIncrementValue;
|
|
|
|
}
|
|
|
|
|
2001-12-17 16:51:51 +00:00
|
|
|
/**
|
|
|
|
<p>The default implementation returns <code>YES</code>, so that the
|
|
|
|
slider continues to track the user's movement even if the cursor
|
|
|
|
leaves the slider's track.</p>
|
|
|
|
|
|
|
|
<p>Do not call this method directly. Override it in subclasses
|
|
|
|
where the tracking behaviour needs to be different.</p>
|
|
|
|
*/
|
1999-12-29 07:14:00 +00:00
|
|
|
+ (BOOL) prefersTrackingUntilMouseUp
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2001-12-17 16:51:51 +00:00
|
|
|
/** Returns the rect of the track, minus the bezel. */
|
1999-12-29 07:14:00 +00:00
|
|
|
- (NSRect) trackRect
|
|
|
|
{
|
|
|
|
return _trackRect;
|
|
|
|
}
|
|
|
|
|
2001-11-02 13:06:20 +00:00
|
|
|
// ticks
|
|
|
|
- (BOOL) allowsTickMarkValuesOnly
|
|
|
|
{
|
|
|
|
return _allowsTickMarkValuesOnly;
|
|
|
|
}
|
|
|
|
|
2011-07-02 19:04:11 +00:00
|
|
|
/* verified on Cocoa that a circular slider with one tick has two values: 0, 50 */
|
2001-11-02 13:06:20 +00:00
|
|
|
- (double) closestTickMarkValueToValue: (double)aValue
|
|
|
|
{
|
2010-06-21 19:42:08 +00:00
|
|
|
double d, f;
|
2011-07-02 19:04:11 +00:00
|
|
|
int effectiveTicks;
|
2003-04-25 22:55:18 +00:00
|
|
|
|
2001-11-02 13:06:20 +00:00
|
|
|
if (_numberOfTickMarks == 0)
|
|
|
|
return aValue;
|
2011-07-02 22:01:23 +00:00
|
|
|
|
2011-07-02 19:04:11 +00:00
|
|
|
effectiveTicks = _numberOfTickMarks;
|
|
|
|
if (_type == NSCircularSlider)
|
|
|
|
effectiveTicks++;
|
|
|
|
|
|
|
|
if (effectiveTicks == 1)
|
2010-06-21 19:42:08 +00:00
|
|
|
return (_maxValue + _minValue) / 2;
|
2001-11-02 13:06:20 +00:00
|
|
|
|
2003-04-25 22:55:18 +00:00
|
|
|
if (aValue < _minValue)
|
|
|
|
{
|
|
|
|
aValue = _minValue;
|
|
|
|
}
|
|
|
|
else if (aValue > _maxValue)
|
|
|
|
{
|
|
|
|
aValue = _maxValue;
|
|
|
|
}
|
|
|
|
|
2010-06-21 19:42:08 +00:00
|
|
|
d = _maxValue - _minValue;
|
2011-07-02 19:04:11 +00:00
|
|
|
f = ((aValue - _minValue) * (effectiveTicks - 1)) / d;
|
2011-07-12 21:12:22 +00:00
|
|
|
f = ((GSRoundTowardsInfinity(f) * d) / (effectiveTicks - 1)) + _minValue;
|
2003-04-25 22:55:18 +00:00
|
|
|
|
2011-07-02 22:01:23 +00:00
|
|
|
/* never return the maximum value, tested on Apple */
|
|
|
|
if (_type == NSCircularSlider && (f >= _maxValue))
|
|
|
|
f = _minValue;
|
|
|
|
|
2003-04-25 22:55:18 +00:00
|
|
|
return f;
|
2001-11-02 13:06:20 +00:00
|
|
|
}
|
|
|
|
|
2011-12-17 17:16:09 +00:00
|
|
|
- (NSInteger) indexOfTickMarkAtPoint: (NSPoint)point
|
2001-11-02 13:06:20 +00:00
|
|
|
{
|
2011-12-17 17:16:09 +00:00
|
|
|
NSInteger i;
|
2003-04-25 22:55:18 +00:00
|
|
|
|
|
|
|
for (i = 0; i < _numberOfTickMarks; i++)
|
|
|
|
{
|
|
|
|
if (NSPointInRect(point, [self rectOfTickMarkAtIndex: i]))
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSNotFound;
|
2001-11-02 13:06:20 +00:00
|
|
|
}
|
|
|
|
|
2011-12-17 17:16:09 +00:00
|
|
|
- (NSInteger) numberOfTickMarks
|
2001-11-02 13:06:20 +00:00
|
|
|
{
|
|
|
|
return _numberOfTickMarks;
|
|
|
|
}
|
|
|
|
|
2011-12-17 17:16:09 +00:00
|
|
|
- (NSRect) rectOfTickMarkAtIndex: (NSInteger)index
|
2001-11-02 13:06:20 +00:00
|
|
|
{
|
2003-04-25 22:55:18 +00:00
|
|
|
NSRect rect = _trackRect;
|
2013-02-17 22:44:47 +00:00
|
|
|
CGFloat d;
|
2003-04-25 22:55:18 +00:00
|
|
|
|
|
|
|
if ((index < 0) || (index >= _numberOfTickMarks))
|
|
|
|
{
|
|
|
|
[NSException raise: NSRangeException
|
2010-06-21 19:42:08 +00:00
|
|
|
format: @"Index of tick mark out of bounds."];
|
2003-04-25 22:55:18 +00:00
|
|
|
}
|
|
|
|
|
2010-06-21 19:42:08 +00:00
|
|
|
if (_numberOfTickMarks > 1)
|
2003-04-25 22:55:18 +00:00
|
|
|
{
|
2010-06-21 19:42:08 +00:00
|
|
|
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;
|
|
|
|
}
|
2003-04-25 22:55:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rect;
|
2001-11-02 13:06:20 +00:00
|
|
|
}
|
|
|
|
|
2001-12-17 16:51:51 +00:00
|
|
|
- (void) setAllowsTickMarkValuesOnly: (BOOL)flag
|
2001-11-02 13:06:20 +00:00
|
|
|
{
|
|
|
|
_allowsTickMarkValuesOnly = flag;
|
|
|
|
}
|
|
|
|
|
2011-12-17 17:16:09 +00:00
|
|
|
- (void) setNumberOfTickMarks: (NSInteger)numberOfTickMarks
|
2001-11-02 13:06:20 +00:00
|
|
|
{
|
|
|
|
_numberOfTickMarks = numberOfTickMarks;
|
2003-04-25 22:55:18 +00:00
|
|
|
if ((_control_view != nil) &&
|
|
|
|
([_control_view isKindOfClass: [NSControl class]]))
|
|
|
|
{
|
|
|
|
[(NSControl*)_control_view updateCell: self];
|
|
|
|
}
|
2001-11-02 13:06:20 +00:00
|
|
|
}
|
|
|
|
|
2001-12-17 16:51:51 +00:00
|
|
|
- (void) setTickMarkPosition: (NSTickMarkPosition)position
|
2001-11-02 13:06:20 +00:00
|
|
|
{
|
|
|
|
_tickMarkPosition = position;
|
2003-04-25 22:55:18 +00:00
|
|
|
if ((_control_view != nil) &&
|
|
|
|
([_control_view isKindOfClass: [NSControl class]]))
|
|
|
|
{
|
|
|
|
[(NSControl*)_control_view updateCell: self];
|
|
|
|
}
|
2001-11-02 13:06:20 +00:00
|
|
|
}
|
|
|
|
|
2001-12-17 16:51:51 +00:00
|
|
|
- (NSTickMarkPosition) tickMarkPosition
|
2001-11-02 13:06:20 +00:00
|
|
|
{
|
|
|
|
return _tickMarkPosition;
|
|
|
|
}
|
|
|
|
|
2011-12-17 17:16:09 +00:00
|
|
|
- (double) tickMarkValueAtIndex: (NSInteger)index
|
2001-11-02 13:06:20 +00:00
|
|
|
{
|
2010-06-21 19:42:08 +00:00
|
|
|
if ((index < 0) || (index >= _numberOfTickMarks))
|
|
|
|
{
|
|
|
|
[NSException raise: NSRangeException
|
|
|
|
format: @"Index of tick mark out of bounds."];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_numberOfTickMarks == 1)
|
|
|
|
return (_maxValue + _minValue) / 2;
|
2001-11-02 13:06:20 +00:00
|
|
|
if (index >= _numberOfTickMarks)
|
|
|
|
return _maxValue;
|
|
|
|
if (index <= 0)
|
|
|
|
return _minValue;
|
|
|
|
|
2011-07-02 19:04:11 +00:00
|
|
|
if (_type == NSCircularSlider)
|
|
|
|
return _minValue + index * (_maxValue - _minValue) / _numberOfTickMarks;
|
|
|
|
if (_type == NSLinearSlider)
|
|
|
|
return _minValue + index * (_maxValue - _minValue) / (_numberOfTickMarks - 1);
|
|
|
|
|
|
|
|
return 0.0;
|
2001-11-02 13:06:20 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 22:01:41 +00:00
|
|
|
- (BOOL) startTrackingAt: (NSPoint)startPoint inView: (NSView*)controlView
|
|
|
|
{
|
|
|
|
// If the point is in the view then yes start tracking
|
|
|
|
if ([controlView mouse: startPoint inRect: [controlView bounds]])
|
2006-08-11 14:08:32 +00:00
|
|
|
{
|
2015-10-16 22:01:41 +00:00
|
|
|
BOOL isFlipped = [controlView isFlipped];
|
|
|
|
NSRect knobRect = [self knobRectFlipped: isFlipped];
|
2006-08-11 14:08:32 +00:00
|
|
|
|
2015-10-16 22:01:41 +00:00
|
|
|
if (![controlView mouse: startPoint inRect: knobRect])
|
2006-08-11 14:08:32 +00:00
|
|
|
{
|
2015-10-16 22:01:41 +00:00
|
|
|
// Mouse is not on the knob, move the knob to the mouse position
|
2018-03-11 18:09:08 +00:00
|
|
|
double doubleValue;
|
2015-10-16 22:01:41 +00:00
|
|
|
NSRect slotRect = [self trackRect];
|
|
|
|
BOOL isVertical = [self isVertical];
|
|
|
|
double minValue = [self minValue];
|
|
|
|
double maxValue = [self maxValue];
|
|
|
|
|
2018-03-11 18:09:08 +00:00
|
|
|
doubleValue = _doubleValueForMousePoint(startPoint, knobRect,
|
2015-10-16 22:01:41 +00:00
|
|
|
slotRect, isVertical,
|
|
|
|
minValue, maxValue,
|
|
|
|
self, isFlipped,
|
|
|
|
(_type == NSCircularSlider));
|
|
|
|
if (_allowsTickMarkValuesOnly)
|
|
|
|
{
|
2018-03-11 18:09:08 +00:00
|
|
|
doubleValue = [self closestTickMarkValueToValue: doubleValue];
|
2015-10-16 22:01:41 +00:00
|
|
|
}
|
2018-03-11 18:09:08 +00:00
|
|
|
[self setDoubleValue: doubleValue];
|
2015-10-16 22:01:41 +00:00
|
|
|
if ([self isContinuous])
|
|
|
|
{
|
|
|
|
[(NSControl*)controlView sendAction: [self action] to: [self target]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
2006-08-11 14:08:32 +00:00
|
|
|
}
|
2015-10-16 22:01:41 +00:00
|
|
|
else
|
2006-08-11 14:08:32 +00:00
|
|
|
{
|
2015-10-16 22:01:41 +00:00
|
|
|
return NO;
|
2006-08-11 14:08:32 +00:00
|
|
|
}
|
2015-10-16 22:01:41 +00:00
|
|
|
}
|
2006-08-11 14:08:32 +00:00
|
|
|
|
2015-10-16 22:01:41 +00:00
|
|
|
- (BOOL) continueTracking: (NSPoint)lastPoint
|
|
|
|
at: (NSPoint)currentPoint
|
|
|
|
inView: (NSView*)controlView
|
|
|
|
{
|
|
|
|
if (currentPoint.x != lastPoint.x || currentPoint.y != lastPoint.y)
|
2006-08-11 14:08:32 +00:00
|
|
|
{
|
2018-03-11 18:09:08 +00:00
|
|
|
double doubleValue;
|
2015-10-16 22:01:41 +00:00
|
|
|
BOOL isFlipped = [controlView isFlipped];
|
|
|
|
NSRect knobRect = [self knobRectFlipped: isFlipped];
|
2018-03-11 18:09:08 +00:00
|
|
|
double oldDoubleValue = [self doubleValue];
|
2015-10-16 22:01:41 +00:00
|
|
|
NSRect slotRect = [self trackRect];
|
|
|
|
BOOL isVertical = [self isVertical];
|
|
|
|
double minValue = [self minValue];
|
|
|
|
double maxValue = [self maxValue];
|
|
|
|
|
2018-03-11 18:09:08 +00:00
|
|
|
doubleValue = _doubleValueForMousePoint(currentPoint, knobRect,
|
|
|
|
slotRect, isVertical,
|
|
|
|
minValue, maxValue,
|
|
|
|
self, isFlipped,
|
|
|
|
(_type == NSCircularSlider));
|
2015-10-16 22:01:41 +00:00
|
|
|
if (_allowsTickMarkValuesOnly)
|
2006-08-11 14:08:32 +00:00
|
|
|
{
|
2018-03-11 18:09:08 +00:00
|
|
|
doubleValue = [self closestTickMarkValueToValue: doubleValue];
|
2015-10-16 22:01:41 +00:00
|
|
|
}
|
2018-03-11 18:09:08 +00:00
|
|
|
if (doubleValue != oldDoubleValue)
|
2006-08-11 14:08:32 +00:00
|
|
|
{
|
2018-03-11 18:09:08 +00:00
|
|
|
[self setDoubleValue: doubleValue];
|
2015-10-16 22:01:41 +00:00
|
|
|
// The action gets triggered in trackMouse:...untilMouseUp:
|
|
|
|
}
|
2006-08-11 14:08:32 +00:00
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
1999-03-02 08:58:30 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)decoder
|
1997-10-09 22:55:31 +00:00
|
|
|
{
|
1999-03-02 08:58:30 +00:00
|
|
|
self = [super initWithCoder: decoder];
|
2009-11-16 12:26:38 +00:00
|
|
|
if (self == nil)
|
|
|
|
return nil;
|
|
|
|
|
2006-10-15 08:34:47 +00:00
|
|
|
if ([decoder allowsKeyedCoding])
|
2003-04-25 22:55:18 +00:00
|
|
|
{
|
2006-05-20 22:12:46 +00:00
|
|
|
_allowsTickMarkValuesOnly = [decoder decodeBoolForKey: @"NSAllowsTickMarkValuesOnly"];
|
|
|
|
_numberOfTickMarks = [decoder decodeIntForKey: @"NSNumberOfTickMarks"];
|
|
|
|
_tickMarkPosition = [decoder decodeIntForKey: @"NSTickMarkPosition"];
|
2018-03-11 18:09:08 +00:00
|
|
|
[self setMinValue: [decoder decodeDoubleForKey: @"NSMinValue"]];
|
|
|
|
[self setMaxValue: [decoder decodeDoubleForKey: @"NSMaxValue"]];
|
|
|
|
[self setDoubleValue: [decoder decodeDoubleForKey: @"NSValue"]];
|
|
|
|
_altIncrementValue = [decoder decodeDoubleForKey: @"NSAltIncValue"];
|
2010-03-17 00:18:30 +00:00
|
|
|
[self setSliderType: [decoder decodeIntForKey: @"NSSliderType"]];
|
|
|
|
|
2006-05-20 22:12:46 +00:00
|
|
|
// 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
|
|
|
|
{
|
2021-01-08 16:18:36 +00:00
|
|
|
float tmp_float;
|
2021-01-10 21:40:29 +00:00
|
|
|
NSInteger tmp_int;
|
2019-07-28 18:12:26 +00:00
|
|
|
|
|
|
|
[self setDoubleValue: 0];
|
2021-01-08 16:18:36 +00:00
|
|
|
[decoder decodeValueOfObjCType: @encode(float) at: &tmp_float];
|
|
|
|
[self setMinValue: tmp_float];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(float) at: &tmp_float];
|
|
|
|
[self setMaxValue: tmp_float];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(float) at: &tmp_float];
|
|
|
|
[self setAltIncrementValue: tmp_float];
|
2021-01-10 21:40:29 +00:00
|
|
|
decode_NSInteger(decoder, &tmp_int);
|
2021-01-08 16:18:36 +00:00
|
|
|
_isVertical = tmp_int;
|
2006-05-20 22:12:46 +00:00
|
|
|
[decoder decodeValueOfObjCType: @encode(id) at: &_titleCell];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(id) at: &_knobCell];
|
|
|
|
if ([decoder versionForClassName: @"NSSliderCell"] >= 2)
|
|
|
|
{
|
|
|
|
[decoder decodeValueOfObjCType: @encode(BOOL) at: &_allowsTickMarkValuesOnly];
|
2021-01-10 21:40:29 +00:00
|
|
|
decode_NSInteger(decoder, &_numberOfTickMarks);
|
|
|
|
decode_NSInteger(decoder, &tmp_int);
|
2021-01-08 16:18:36 +00:00
|
|
|
_tickMarkPosition = tmp_int;
|
2006-05-20 22:12:46 +00:00
|
|
|
}
|
2003-04-25 22:55:18 +00:00
|
|
|
}
|
1997-10-09 22:55:31 +00:00
|
|
|
return self;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:58:30 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder*)coder
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
1999-03-02 08:58:30 +00:00
|
|
|
[super encodeWithCoder: coder];
|
2006-10-15 08:34:47 +00:00
|
|
|
if ([coder allowsKeyedCoding])
|
2006-05-20 22:12:46 +00:00
|
|
|
{
|
|
|
|
[coder encodeBool: _allowsTickMarkValuesOnly forKey: @"NSAllowsTickMarkValuesOnly"];
|
|
|
|
[coder encodeInt: _numberOfTickMarks forKey: @"NSNumberOfTickMarks"];
|
|
|
|
[coder encodeInt: _tickMarkPosition forKey: @"NSTickMarkPosition"];
|
2018-03-11 18:09:08 +00:00
|
|
|
[coder encodeDouble: _minValue forKey: @"NSMinValue"];
|
|
|
|
[coder encodeDouble: _maxValue forKey: @"NSMaxValue"];
|
|
|
|
[coder encodeDouble: _value forKey: @"NSValue"];
|
|
|
|
[coder encodeDouble: _altIncrementValue forKey: @"NSAltIncValue"];
|
2010-03-17 00:18:30 +00:00
|
|
|
[coder encodeInt: _type forKey: @"NSSliderType"];
|
2006-05-20 22:12:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-08 16:18:36 +00:00
|
|
|
float tmp_float;
|
2021-01-10 21:40:29 +00:00
|
|
|
NSInteger tmp_int;
|
2021-01-08 16:18:36 +00:00
|
|
|
|
|
|
|
tmp_float = _minValue;
|
|
|
|
[coder encodeValueOfObjCType: @encode(float) at: &tmp_float];
|
|
|
|
tmp_float = _maxValue;
|
|
|
|
[coder encodeValueOfObjCType: @encode(float) at: &tmp_float];
|
|
|
|
tmp_float = _altIncrementValue;
|
|
|
|
[coder encodeValueOfObjCType: @encode(float) at: &tmp_float];
|
|
|
|
tmp_int = _isVertical;
|
2021-01-10 21:40:29 +00:00
|
|
|
encode_NSInteger(coder, &tmp_int);
|
2006-05-20 22:12:46 +00:00
|
|
|
[coder encodeValueOfObjCType: @encode(id) at: &_titleCell];
|
|
|
|
[coder encodeValueOfObjCType: @encode(id) at: &_knobCell];
|
|
|
|
// New for version 2
|
|
|
|
[coder encodeValueOfObjCType: @encode(BOOL) at: &_allowsTickMarkValuesOnly];
|
2021-01-10 21:40:29 +00:00
|
|
|
encode_NSInteger(coder, &_numberOfTickMarks);
|
2021-01-08 16:18:36 +00:00
|
|
|
tmp_int = _tickMarkPosition;
|
2021-01-10 21:40:29 +00:00
|
|
|
encode_NSInteger(coder, &tmp_int);
|
2006-05-20 22:12:46 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
2006-05-20 22:12:46 +00:00
|
|
|
|
1996-05-30 20:03:15 +00:00
|
|
|
@end
|