macOS: Change license of some VLCHUD classes
[vlc.git] / modules / gui / macosx / VLCHUDRadiobuttonCell.m
1 //
2 //  VLCHUDRadiobuttonCell.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 "VLCHUDRadiobuttonCell.h"
36
37 @implementation VLCHUDRadiobuttonCell
38
39 - (instancetype) initWithCoder:(NSCoder *)coder
40 {
41     self = [super initWithCoder:coder];
42     if (self) {
43         _normalGradient    = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.251f green:0.251f blue:0.255f alpha:1.0f]
44                                                            endingColor:[NSColor colorWithDeviceRed:0.118f green:0.118f blue:0.118f alpha:1.0f]];
45         _highlightGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.451f green:0.451f blue:0.455f alpha:1.0f]
46                                                            endingColor:[NSColor colorWithDeviceRed:0.318f green:0.318f blue:0.318f alpha:1.0f]];
47         _pushedGradient    = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.451f green:0.451f blue:0.455f alpha:1.0f]
48                                                            endingColor:[NSColor colorWithDeviceRed:0.318f green:0.318f blue:0.318f alpha:1.0f]];
49         _textColor         = [NSColor whiteColor];
50     }
51     return self;
52 }
53
54 - (void) drawImage:(NSImage *)image withFrame:(NSRect)frame inView:(NSView *)controlView
55 {
56     // Set text color
57     NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedTitle];
58     NSRange titleRange = NSMakeRange(0, [colorTitle length]);
59     [colorTitle addAttribute:NSForegroundColorAttributeName value:_textColor range:titleRange];
60     [self setAttributedTitle:colorTitle];
61
62     // Set frame size correctly
63     NSRect innerFrame = frame;
64     if (self.controlSize == NSSmallControlSize) {
65         frame.origin.x += 3.5;
66         frame.origin.y += 4;
67         frame.size.width -= 7;
68         frame.size.height -= 7;
69         innerFrame = NSInsetRect(frame, 2, 2);
70     } else if (self.controlSize == NSMiniControlSize) {
71         frame.origin.x += 2;
72         frame.origin.y += 4;
73         frame.size.width -= 6.5;
74         frame.size.height -= 6.5;
75         innerFrame = NSInsetRect(frame, 4, 4);
76     }
77
78     // Set fill frame
79     NSBezierPath *backgroundPath = [NSBezierPath bezierPathWithOvalInRect:frame];
80
81     // Draw border and background
82     [NSColor.whiteColor setStroke];
83     [backgroundPath setLineWidth:1.5];
84     [backgroundPath stroke];
85
86     if ([self isEnabled]) {
87         if ([self isHighlighted]) {
88             [_normalGradient drawInBezierPath:backgroundPath angle:90.0];
89         } else {
90             [_pushedGradient drawInBezierPath:backgroundPath angle:90.0];
91         }
92     } else {
93         [[NSColor lightGrayColor] setFill];
94         [backgroundPath fill];
95     }
96
97     // Draw dot
98     if ([self integerValue] && [self isEnabled]) {
99         NSBezierPath* bezierPath = [NSBezierPath bezierPathWithOvalInRect:innerFrame];
100         [[NSColor whiteColor] setFill];
101         [bezierPath fill];
102     }
103 }
104
105 - (NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView
106 {
107     NSMutableAttributedString *coloredTitle = [[NSMutableAttributedString alloc]
108                                                initWithAttributedString:title];
109     if (self.isEnabled) {
110         [coloredTitle addAttribute:NSForegroundColorAttributeName
111                              value:[NSColor whiteColor]
112                              range:NSMakeRange(0, coloredTitle.length)];
113     } else {
114         [coloredTitle addAttribute:NSForegroundColorAttributeName
115                              value:[NSColor grayColor]
116                              range:NSMakeRange(0, coloredTitle.length)];
117     }
118
119     return [super drawTitle:coloredTitle withFrame:frame inView:controlView];
120 }
121
122 @end