macOS: Minor code style adjustments for VLCHUD classes
[vlc.git] / modules / gui / macosx / VLCHUDCheckboxCell.m
1 //
2 //  VLCHUDCheckboxCell.m
3 //  BGHUDAppKit
4 //
5 //  Created by BinaryGod on 5/25/08.
6 //
7 //  Copyright (c) 2008, Tim Davis (BinaryMethod.com, binary.god@gmail.com)
8 //  All rights reserved.
9 //
10 //  Redistribution and use in source and binary forms, with or without modification,
11 //  are permitted provided that the following conditions are met:
12 //
13 //              Redistributions of source code must retain the above copyright notice, this
14 //      list of conditions and the following disclaimer.
15 //
16 //              Redistributions in binary form must reproduce the above copyright notice,
17 //      this list of conditions and the following disclaimer in the documentation and/or
18 //      other materials provided with the distribution.
19 //
20 //              Neither the name of the BinaryMethod.com nor the names of its contributors
21 //      may be used to endorse or promote products derived from this software without
22 //      specific prior written permission.
23 //
24 //      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND
25 //      ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 //      WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 //      IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 //      INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 //      BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
30 //      OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 //      WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 //      ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 //      POSSIBILITY OF SUCH DAMAGE.
34
35 #import "VLCHUDCheckboxCell.h"
36
37 @implementation VLCHUDCheckboxCell
38
39 - (instancetype)initWithCoder:(NSCoder *)coder
40 {
41     self = [super initWithCoder:coder];
42     if (self) {
43         /* Colors */
44         _cellTextColor         = [NSColor whiteColor];
45         _disabledCellTextColor = [NSColor colorWithCalibratedWhite:1.0f alpha:0.5f];
46         _strokeColor           = [NSColor whiteColor];
47         _disabledStrokeColor   = [NSColor colorWithCalibratedWhite:1.0f alpha:0.5f];
48
49         /* Gradients */
50         _normalGradient    = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.251f green:0.251f blue:0.255f alpha:1.0f]
51                                                            endingColor:[NSColor colorWithDeviceRed:0.118f green:0.118f blue:0.118f alpha:1.0f]];
52         _highlightGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.451f green:0.451f blue:0.455f alpha:1.0f]
53                                                            endingColor:[NSColor colorWithDeviceRed:0.318f green:0.318f blue:0.318f alpha:1.0f]];
54         _pushedGradient    = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.451f green:0.451f blue:0.455f alpha:1.0f]
55                                                            endingColor:[NSColor colorWithDeviceRed:0.318f green:0.318f blue:0.318f alpha:1.0f]];
56     }
57     return self;
58 }
59
60 - (void)drawImage:(NSImage *)image withFrame:(NSRect)frame inView:(NSView *)controlView
61 {
62     // Set frame size correctly
63     NSRect backgroundFrame = frame;
64     if (self.controlSize == NSSmallControlSize) {
65         frame.origin.x += 4;
66         frame.origin.y += 2.5;
67         frame.size.width -= 5;
68         frame.size.height -= 5;
69         backgroundFrame = NSInsetRect(frame, 1.5, 1.5);
70     } else if (self.controlSize == NSMiniControlSize) {
71         frame.origin.x += 2.5;
72         frame.origin.y += 4;
73         frame.size.width -= 5;
74         frame.size.height -= 5;
75         backgroundFrame = NSInsetRect(frame, 2, 2);
76     }
77
78     // Set fill frame
79     NSBezierPath *backgroundPath = [NSBezierPath bezierPathWithRoundedRect:backgroundFrame
80                                                                    xRadius:2
81                                                                    yRadius:2];
82
83     // Draw background and stroke
84     if([self isEnabled]) {
85         if ([self isHighlighted]) {
86             [_highlightGradient drawInBezierPath:backgroundPath angle:90.0];
87         } else {
88             [_normalGradient drawInBezierPath:backgroundPath angle:90.0];
89         }
90         [_strokeColor setStroke];
91     } else {
92         [_disabledStrokeColor setStroke];
93     }
94
95     [NSGraphicsContext saveGraphicsState];
96     if ([super showsFirstResponder] && [[[self controlView] window] isKeyWindow] &&
97        ([self focusRingType] == NSFocusRingTypeDefault ||
98         [self focusRingType] == NSFocusRingTypeExterior)) {
99         NSSetFocusRingStyle(NSFocusRingOnly);
100     }
101     [backgroundPath setLineWidth:1.0];
102     [backgroundPath stroke];
103     [NSGraphicsContext restoreGraphicsState];
104
105     // Now drawing tick
106     if ([self intValue]) {
107         NSBezierPath* bezierPath = [NSBezierPath bezierPath];
108         [bezierPath moveToPoint: NSMakePoint(NSMinX(frame) + 3.0, NSMidY(frame) - 2.0)];
109         [bezierPath lineToPoint: NSMakePoint(NSMidX(frame), NSMidY(frame) + 2.0)];
110         [bezierPath lineToPoint: NSMakePoint((NSMinX(frame) + NSWidth(frame) - 1), NSMinY(frame) - 2.0)];
111         [bezierPath setLineWidth: 1.5];
112
113         if([self isEnabled]) {
114             [_strokeColor setStroke];
115         } else {
116             [_disabledStrokeColor setStroke];
117         }
118         [bezierPath stroke];
119     }
120 }
121
122 - (NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView
123 {
124     NSMutableAttributedString *newTitle = [title mutableCopy];
125
126     if([self isEnabled]) {
127         [newTitle addAttribute:NSForegroundColorAttributeName
128                          value:_cellTextColor
129                          range:NSMakeRange(0, [newTitle length])];
130     } else {
131         [newTitle addAttribute:NSForegroundColorAttributeName
132                          value:_disabledCellTextColor
133                          range:NSMakeRange(0, [newTitle length])];
134     }
135
136     return [super drawTitle: newTitle withFrame: frame inView: controlView];
137 }
138
139 @end