VLCDefaultValueSliderCell.m 7.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*****************************************************************************
 * VLCDefaultValueSliderCell.m: SliderCell subclass for VLCDefaultValueSlider
 *****************************************************************************
 * Copyright (C) 2016 VLC authors and VideoLAN
 * $Id$
 *
 * Authors: Marvin Scholz <epirat07 -at- gmail -dot- com>
 *
 * This file uses parts of code from the GNUstep NSSliderCell code:
 * 
 *   Copyright (C) 1996,1999 Free Software Foundation, Inc.
 *   Author: Ovidiu Predescu <ovidiu@net-community.com>
 *   Date: September 1997
 *   Rewrite: Richard Frith-Macdonald <richard@brainstorm.co.uk>
 *   Date: 1999
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#import "VLCDefaultValueSliderCell.h"
33
#import "CompatibilityFixes.h"
34 35 36 37 38

@interface VLCDefaultValueSliderCell (){
    BOOL _isRTL;
    BOOL _isFlipped;
    double _defaultValue;
39
    double _normalizedDefaultValue;
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    NSColor *_defaultTickMarkColor;
}
@end

@implementation VLCDefaultValueSliderCell

#pragma mark -
#pragma mark Public interface

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSelf];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setupSelf];
    }
    return self;
}

- (void)setDefaultValue:(double)value
{
    if (value > _maxValue || value < _minValue)
        value = DBL_MAX;

    if (_defaultValue == DBL_MAX && value != DBL_MAX) {
        _drawTickMarkForDefault = YES;
        _snapsToDefault = YES;
    } else if (value == DBL_MAX) {
        _drawTickMarkForDefault = NO;
        _snapsToDefault = NO;
    }
    _defaultValue = value;
80
    _normalizedDefaultValue = (value == DBL_MAX) ? DBL_MAX : [self normalizedValue:_defaultValue];
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    [[self controlView] setNeedsDisplay:YES];
}

- (double)defaultValue
{
    return _defaultValue;
}

- (void)setDefaultTickMarkColor:(NSColor *)color
{
    _defaultTickMarkColor = color;
    [[self controlView] setNeedsDisplay:YES];

}

- (NSColor *)defaultTickMarkColor
{
    return _defaultTickMarkColor;
}

- (void)drawDefaultTickMarkWithFrame:(NSRect)rect
{
    [_defaultTickMarkColor setFill];
    NSRectFill(rect);
}

#pragma mark -
#pragma mark Internal helpers

- (void)setupSelf
{
    _defaultValue = DBL_MAX;
113
    _normalizedDefaultValue = DBL_MAX;
114 115 116 117 118 119 120 121 122 123
    _isRTL = ([self userInterfaceLayoutDirection] == NSUserInterfaceLayoutDirectionRightToLeft);
    _isFlipped = [[self controlView] isFlipped];
    _defaultTickMarkColor = [NSColor grayColor];
}

/*
 * Calculates the knobRect for a given position
 * This is later used to draw the default tick mark in the center of
 * where the knob would be, when it is at the default value.
 */
124 125
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpartial-availability"
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
- (NSRect)knobRectFlipped:(BOOL)flipped forValue:(double)doubleValue
 {
     NSRect resultRect;
     double val = [self normalizedValue:doubleValue] / 100;

     if (self.isVertical) {
         resultRect.origin.x = -1;
         resultRect.origin.y = (NSHeight(_trackRect) - self.knobThickness) * val;
         if (_isRTL)
             resultRect.origin.y = (NSHeight(_trackRect) - self.knobThickness) - resultRect.origin.y;
     } else {
         resultRect.origin.x = (NSWidth(_trackRect) - self.knobThickness) * val;
         resultRect.origin.y = -1;
         if (_isRTL)
             resultRect.origin.x = (NSWidth(_trackRect) - self.knobThickness) - resultRect.origin.x;
     }

     resultRect.size.height = self.knobThickness;
     resultRect.size.width = self.knobThickness;

     return [self.controlView backingAlignedRect:resultRect options:NSAlignAllEdgesNearest];
 }
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

#pragma mark -
#pragma mark Overwritten super methods

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    // Do all other drawing
    [super drawWithFrame:cellFrame inView:controlView];

    // Default tick mark
    if (_drawTickMarkForDefault && _defaultValue != DBL_MAX) {

        // Calculate rect for default tick mark
        CGFloat tickThickness = 1.0;

        NSRect tickFrame = [self knobRectFlipped:_isFlipped
                                        forValue:_defaultValue];

        if ([self isVertical]) {
            CGFloat mid = NSMidY(tickFrame);
            tickFrame.origin.x = cellFrame.origin.x;
            tickFrame.origin.y = mid - tickThickness/2.0;
            tickFrame.size.width = cellFrame.size.width;
            tickFrame.size.height = tickThickness;
        } else {
            CGFloat mid = NSMidX(tickFrame);
175 176 177 178
            // Ugly workaround
            // Corrects minor alignment issue on non-retina
            CGFloat scale = [[[self controlView] window] backingScaleFactor];
            tickFrame.size.height = cellFrame.size.height;
179
            tickFrame.origin.y = cellFrame.origin.y;
180 181 182 183 184 185 186 187 188
            if (scale > 1.0) {
                tickFrame.origin.x = mid;
            } else {
                tickFrame.origin.x = mid - tickThickness;
                if (OSX_YOSEMITE_AND_HIGHER) {
                    tickFrame.size.height = cellFrame.size.height - 1;
                    tickFrame.origin.y = cellFrame.origin.y - 1;
                }
            }
189 190 191
            tickFrame.size.width = tickThickness;
        }

192 193 194 195
        NSAlignmentOptions alignOpts = NSAlignMinXOutward | NSAlignMinYOutward |
                                       NSAlignWidthOutward | NSAlignMaxYOutward;
        NSRect alignedRect = [[self controlView] backingAlignedRect:tickFrame options:alignOpts];

196
        // Draw default tick mark
197
        [self drawDefaultTickMarkWithFrame:alignedRect];
198 199 200 201 202
    }

    // Redraw knob
    [super drawKnob];
}
203
#pragma clang diagnostic pop
204

205 206 207 208 209 210 211 212 213 214 215
- (double)normalizedValue:(double)value
{
    double min = [self minValue];
    double max = [self maxValue];

    max -= min;
    value -= min;

    return (value / max) * 100;
}

216 217
- (BOOL)continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint inView:(NSView *)controlView
{
218
    double oldValue = [self normalizedValue:self.doubleValue];
219
    BOOL result = [super continueTracking:lastPoint at:currentPoint inView:controlView];
220
    double newValue = [self normalizedValue:self.doubleValue];
221 222 223 224 225 226 227 228

    // If no change, nothing to do.
    if (newValue == oldValue)
        return result;

    // Determine in which direction slider is moving
    BOOL sliderMovingForward = (oldValue > newValue) ? NO : YES;

229 230
    // Claculate snap-threshhold
    double thresh = 100 * (self.knobThickness/3) / _trackRect.size.width;
231 232

    // Snap to default value
233 234
    if (_snapsToDefault && ABS(newValue - _normalizedDefaultValue) < thresh) {
        if (sliderMovingForward && newValue > _normalizedDefaultValue) {
235
            [self setDoubleValue:_defaultValue];
236
        } else if (!sliderMovingForward && newValue < _normalizedDefaultValue) {
237 238 239 240 241 242 243 244
            [self setDoubleValue:_defaultValue];
        }
    }

    return result;
}

@end