macosx: Improve VLCHUDCheckboxCell
[vlc.git] / modules / gui / macosx / VLCHUDCheckboxCell.m
1 /*****************************************************************************
2  * VLCHUDCheckboxCell.m: Custom checkbox cell UI for dark HUD Panels
3  *****************************************************************************
4  * Copyright (C) 2016 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Marvin Scholz <epirat07 -at- gmail -dot- com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #import "VLCHUDCheckboxCell.h"
25
26 @implementation VLCHUDCheckboxCell
27
28 - (instancetype) initWithCoder:(NSCoder *)coder
29 {
30     self = [super initWithCoder:coder];
31     if (self) {
32         _normalGradient    = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.251f green:0.251f blue:0.255f alpha:1.0f]
33                                                            endingColor:[NSColor colorWithDeviceRed:0.118f green:0.118f blue:0.118f alpha:1.0f]];
34         _highlightGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.451f green:0.451f blue:0.455f alpha:1.0f]
35                                                            endingColor:[NSColor colorWithDeviceRed:0.318f green:0.318f blue:0.318f alpha:1.0f]];
36         _pushedGradient    = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.451f green:0.451f blue:0.455f alpha:1.0f]
37                                                            endingColor:[NSColor colorWithDeviceRed:0.318f green:0.318f blue:0.318f alpha:1.0f]];
38     }
39     return self;
40 }
41
42 - (void) drawImage:(NSImage *)image withFrame:(NSRect)frame inView:(NSView *)controlView
43 {
44     // Set frame size correctly
45     NSRect backgroundFrame = frame;
46     if (self.controlSize == NSSmallControlSize) {
47         frame.origin.x += 4;
48         frame.origin.y += 2.5;
49         frame.size.width -= 5;
50         frame.size.height -= 5;
51         backgroundFrame = NSInsetRect(frame, 1.5, 1.5);
52     } else if (self.controlSize == NSMiniControlSize) {
53         frame.origin.x += 2.5;
54         frame.origin.y += 4;
55         frame.size.width -= 5;
56         frame.size.height -= 5;
57         backgroundFrame = NSInsetRect(frame, 2, 2);
58     }
59
60     // Set fill frame
61     NSBezierPath *backgroundPath = [NSBezierPath bezierPathWithRoundedRect:backgroundFrame
62                                                                    xRadius:2
63                                                                    yRadius:2];
64
65     // Draw background and stroke
66     if([self isEnabled]) {
67         if ([self isHighlighted]) {
68             [_highlightGradient drawInBezierPath:backgroundPath angle:90.0];
69         } else {
70             [_normalGradient drawInBezierPath:backgroundPath angle:90.0];
71         }
72         [[NSColor whiteColor] setStroke];
73     } else {
74         [[NSColor colorWithCalibratedWhite:0.25 alpha:1.0] setStroke];
75     }
76
77     [backgroundPath setLineWidth:1.0];
78     [backgroundPath stroke];
79
80     // Now drawing tick
81     if ([self intValue]) {
82         NSBezierPath* bezierPath = [NSBezierPath bezierPath];
83         [bezierPath moveToPoint: NSMakePoint(NSMinX(frame) + 3.0, NSMidY(frame) - 2.0)];
84         [bezierPath lineToPoint: NSMakePoint(NSMidX(frame), NSMidY(frame) + 2.0)];
85         [bezierPath lineToPoint: NSMakePoint((NSMinX(frame) + NSWidth(frame) - 1), NSMinY(frame) - 2.0)];
86         [bezierPath setLineWidth: 1.5];
87
88         if([self isEnabled]) {
89             [[NSColor whiteColor] setStroke];
90         } else {
91             [[NSColor colorWithCalibratedWhite:0.25 alpha:1.0] setStroke];
92         }
93         [bezierPath stroke];
94     }
95 }
96
97 @end