macOS: Correct invalid Objective C code
[vlc.git] / modules / gui / macosx / VLCHUDStepperCell.m
1 //
2 //  VLCHUDStepperCell.m
3 //  BGHUDAppKit
4 //
5 //  Created by BinaryGod on 4/6/09.
6 //
7 //  Copyright 2009 Tyler Bunnell and Steve Audette
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 "VLCHUDStepperCell.h"
36 #import "CompatibilityFixes.h"
37
38 @interface VLCHUDStepperCell () {
39     int topButtonFlag;
40     int bottomButtonFlag;
41
42     BOOL topPressed;
43     BOOL bottomPressed;
44     BOOL isTopDown;
45     BOOL isBottomDown;
46 }
47
48 @end
49
50 @implementation VLCHUDStepperCell
51
52 - (instancetype)initWithCoder:(NSCoder *)coder
53 {
54     self = [super initWithCoder:coder];
55     if (self) {
56         _dropShadow = [[NSShadow alloc] init];
57         [_dropShadow setShadowColor: [NSColor blackColor]];
58         [_dropShadow setShadowBlurRadius: 2];
59         [_dropShadow setShadowOffset: NSMakeSize( 0, -1)];
60
61         _strokeColor = [NSColor colorWithDeviceRed: 0.749f green: 0.761f blue: 0.788f alpha: 1.0f];
62         _disabledStrokeColor = [NSColor colorWithDeviceRed: 0.749f green: 0.761f blue: 0.788f alpha:0.2f];
63
64         _normalGradient    = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.251f green:0.251f blue:0.255f alpha:1.0f]
65                                                            endingColor:[NSColor colorWithDeviceRed:0.118f green:0.118f blue:0.118f alpha:1.0f]];
66         _disabledNormalComplexGradient = [[NSGradient alloc] initWithColorsAndLocations: [NSColor colorWithDeviceRed: 0.324f green: 0.331f blue: 0.347f alpha:0.5f],
67                                            (CGFloat)0, [NSColor colorWithDeviceRed: 0.245f green: 0.253f blue: 0.269f alpha: 0.5f], (CGFloat).5,
68                                            [NSColor colorWithDeviceRed: 0.206f green: 0.214f blue: 0.233f alpha: 0.5f], (CGFloat).5,
69                                            [NSColor colorWithDeviceRed: 0.139f green: 0.147f blue: 0.167f alpha: 0.5f], (CGFloat)1.0f, nil];
70         _pushedSolidFill = [NSColor colorWithDeviceRed: 0.941f green: 0.941f blue: 0.941f alpha: 0.5f];
71         _darkStrokeColor = [NSColor colorWithDeviceRed: 0.141f green: 0.141f blue: 0.141f alpha: 0.5f];
72     }
73     return self;
74 }
75
76 -(void)drawWithFrame:(NSRect) frame inView:(NSView *) controlView {
77     if (OSX_YOSEMITE_AND_HIGHER) {
78         return [super drawWithFrame:frame inView:controlView];
79     }
80     [self drawRoundRectButtonInFrame:frame];
81     [self drawArrowsInRect:frame];
82 }
83
84 -(NSRect)adjustFrame:(NSRect)frame
85 {
86     frame.origin.x += 3.5f;
87     frame.origin.y += 2.0f;
88     frame.size.width -= 7.0f;
89     frame.size.height -= 4.0f;
90     return frame;
91 }
92
93 -(BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
94 {
95     if (OSX_YOSEMITE_AND_HIGHER) {
96         return [super startTrackingAt:startPoint inView:controlView];
97     }
98     NSRect frame = [controlView bounds];
99
100     NSRect bottomRect = NSMakeRect(frame.origin.x,frame.origin.y,frame.size.width,frame.size.height/2);
101     NSRect topRect = NSMakeRect(frame.origin.x,frame.origin.y+(frame.size.height/2),frame.size.width,frame.size.height/2);
102
103     if(NSPointInRect(startPoint,topRect))
104     {
105         topPressed = isTopDown = YES;
106     }
107     if(NSPointInRect(startPoint,bottomRect))
108     {
109         bottomPressed = isBottomDown = YES;
110     }
111     [[self controlView] setNeedsDisplay:YES];
112
113     return [super startTrackingAt:startPoint inView:controlView];
114 }
115 -(BOOL)continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint inView:(NSView *)controlView
116 {
117     if (OSX_YOSEMITE_AND_HIGHER) {
118         return [super continueTracking:lastPoint at:currentPoint inView:controlView];
119     }
120     NSRect frame = [controlView bounds];
121
122     NSRect bottomRect = NSMakeRect(frame.origin.x,frame.origin.y,frame.size.width,frame.size.height/2);
123     NSRect topRect = NSMakeRect(frame.origin.x,frame.origin.y+(frame.size.height/2),frame.size.width,frame.size.height/2);
124
125     if(isTopDown && topPressed && !NSPointInRect(currentPoint,topRect))
126     {
127         isTopDown = NO;
128         [[self controlView] setNeedsDisplay:YES];
129     }
130     if(!isTopDown && topPressed && NSPointInRect(currentPoint,topRect))
131     {
132         isTopDown = YES;
133         [[self controlView] setNeedsDisplay:YES];
134     }
135     if(isBottomDown && bottomPressed && !NSPointInRect(currentPoint,bottomRect))
136     {
137         isBottomDown = NO;
138         [[self controlView] setNeedsDisplay:YES];
139     }
140     if(!isBottomDown && bottomPressed && NSPointInRect(currentPoint,bottomRect))
141     {
142         isBottomDown = YES;
143         [[self controlView] setNeedsDisplay:YES];
144     }
145
146     return [super continueTracking:lastPoint at:currentPoint inView:controlView];
147 }
148 -(void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag
149 {
150     if (OSX_YOSEMITE_AND_HIGHER) {
151         return [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
152     }
153     isTopDown = isBottomDown = topPressed = bottomPressed = NO;
154     [[self controlView] setNeedsDisplay:YES];
155     [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
156 }
157
158 -(void)drawRoundRectButtonInFrame:(NSRect)frame {
159
160     frame = [self adjustFrame:frame];
161
162     float cornerRadius = 4.0f;
163
164     //Draw the complete button
165
166     //Create Path
167     NSBezierPath *path = [[NSBezierPath alloc] init];
168
169     //Bottom Right Corner
170     [path moveToPoint:NSMakePoint(NSMaxX(frame), NSMinY(frame) + cornerRadius)];
171
172     //Right Edge
173     [path lineToPoint:NSMakePoint(NSMaxX(frame), NSMaxY(frame) - cornerRadius)];
174
175     //Top Right Curve
176     [path appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(frame) - cornerRadius, NSMaxY(frame) - cornerRadius)
177                                      radius: cornerRadius
178                                  startAngle: 0
179                                    endAngle: 90];
180
181     //Top Edge
182     [path lineToPoint:NSMakePoint(NSMinX(frame) + cornerRadius, NSMaxY(frame))];
183
184     //Top Left Curve
185     [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMinX(frame) + cornerRadius, NSMaxY(frame) - cornerRadius)
186                                      radius: cornerRadius
187                                  startAngle: 90
188                                    endAngle: 180];
189
190     //Left Edge
191     [path lineToPoint:NSMakePoint(NSMinX(frame), NSMinY(frame) + cornerRadius)];
192
193     //Left Bottom Curve
194     [path appendBezierPathWithArcWithCenter: NSMakePoint(NSMinX(frame) + cornerRadius, NSMinY(frame) + cornerRadius)
195                                      radius: cornerRadius
196                                  startAngle: 180
197                                    endAngle: 270];
198
199     //Bottom Edge
200     [path lineToPoint:NSMakePoint(NSMaxX(frame) - cornerRadius, NSMinY(frame))];
201
202     //Right Bottom Curve
203     [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMaxX(frame) - cornerRadius, NSMinY(frame) + cornerRadius)
204                                      radius: cornerRadius
205                                  startAngle: 270
206                                    endAngle: 0];
207
208     [path closePath];
209
210
211     if([self isEnabled]) {
212
213         [_normalGradient drawInBezierPath:path relativeCenterPosition:NSZeroPoint];
214
215     } else {
216
217         [_disabledNormalComplexGradient drawInBezierPath:path relativeCenterPosition:NSZeroPoint];
218     }
219
220
221
222     //Check for anything being pushed
223     if([self isEnabled])
224     {
225         if(isTopDown)
226         {
227             NSRect topRect = NSMakeRect(frame.origin.x,frame.origin.y+(frame.size.height/2),frame.size.width,frame.size.height/2);
228             NSBezierPath *path = [[NSBezierPath alloc] init];
229             [path moveToPoint:NSMakePoint(NSMaxX(topRect), NSMinY(topRect))];
230             [path lineToPoint:NSMakePoint(NSMaxX(topRect), NSMaxY(topRect) - cornerRadius)];
231             [path appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(topRect) - cornerRadius, NSMaxY(topRect) - cornerRadius)
232                                              radius: cornerRadius
233                                          startAngle: 0
234                                            endAngle: 90];
235             [path lineToPoint:NSMakePoint(NSMinX(topRect) + cornerRadius, NSMaxY(topRect))];
236             [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMinX(topRect) + cornerRadius, NSMaxY(topRect) - cornerRadius)
237                                              radius: cornerRadius
238                                          startAngle: 90
239                                            endAngle: 180];
240             [path lineToPoint:NSMakePoint(NSMinX(topRect), NSMinY(topRect))];
241
242             [path closePath];
243
244             [_pushedSolidFill set];
245             [path fill];
246         }
247         else if(isBottomDown)
248         {
249             NSRect bottomRect = NSMakeRect(frame.origin.x,frame.origin.y,frame.size.width,frame.size.height/2);
250             NSBezierPath* path = [[NSBezierPath alloc] init];
251
252             [path moveToPoint:NSMakePoint(NSMinX(bottomRect), NSMaxY(bottomRect))];
253             [path lineToPoint:NSMakePoint(NSMinX(bottomRect), NSMinY(bottomRect) + cornerRadius)];
254             [path appendBezierPathWithArcWithCenter: NSMakePoint(NSMinX(bottomRect) + cornerRadius, NSMinY(bottomRect) + cornerRadius)
255                                              radius: cornerRadius
256                                          startAngle: 180
257                                            endAngle: 270];
258             [path lineToPoint:NSMakePoint(NSMaxX(bottomRect) - cornerRadius, NSMinY(bottomRect))];
259             [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMaxX(bottomRect) - cornerRadius, NSMinY(bottomRect) + cornerRadius)
260                                              radius: cornerRadius
261                                          startAngle: 270
262                                            endAngle: 0];
263             [path lineToPoint:NSMakePoint(NSMaxX(bottomRect), NSMaxY(bottomRect))];
264
265             [path closePath];
266             [_pushedSolidFill set];
267             [path fill];
268         }
269     }
270
271     [NSGraphicsContext saveGraphicsState];
272
273     //Draw dark border color
274     if([self isEnabled]) {
275
276         [_dropShadow set];
277     }
278     [_darkStrokeColor set];
279     [path stroke];
280
281     [NSGraphicsContext restoreGraphicsState];
282
283     if([self isEnabled]) {
284
285         [_strokeColor set];
286     } else {
287
288         [_disabledStrokeColor set];
289     }
290
291     [path setLineWidth: 1.0f];
292     [path stroke];
293
294
295 }
296
297 -(void)drawArrowsInRect:(NSRect) frame {
298
299     CGFloat arrowWidth = 2.5f;
300     CGFloat arrowHeight = 2.0f;
301
302     frame = [self adjustFrame:frame];
303
304     NSRect bottomRect = NSMakeRect(frame.origin.x,frame.origin.y,frame.size.width,frame.size.height/2);
305     NSRect topRect = NSMakeRect(frame.origin.x,frame.origin.y+(frame.size.height/2),frame.size.width,frame.size.height/2);
306
307     NSBezierPath *arrow = [[NSBezierPath alloc] init];
308
309     NSPoint points[3];
310
311     points[0] = NSMakePoint(topRect.origin.x + (topRect.size.width /2), topRect.origin.y + ((topRect.size.height /2) + arrowHeight));
312     points[1] = NSMakePoint(topRect.origin.x + ((topRect.size.width /2) - arrowWidth), topRect.origin.y + ((topRect.size.height /2)-arrowHeight));
313     points[2] = NSMakePoint(topRect.origin.x + ((topRect.size.width /2) + arrowWidth), topRect.origin.y + ((topRect.size.height /2)-arrowHeight));
314     
315     
316     [arrow appendBezierPathWithPoints: points count: 3];
317     
318     if([self isEnabled]) {
319         if(isTopDown)
320             [_darkStrokeColor set];
321         else
322             [_strokeColor set];
323     } else {
324         [_disabledStrokeColor set];
325     }
326     
327     
328     [arrow fill];
329     
330     
331     arrow = [[NSBezierPath alloc] init];
332     
333     points[0] = NSMakePoint(bottomRect.origin.x + ((bottomRect.size.width /2) - arrowWidth), bottomRect.origin.y + ((bottomRect.size.height /2)+arrowHeight));
334     points[1] = NSMakePoint(bottomRect.origin.x + ((bottomRect.size.width /2) + arrowWidth), bottomRect.origin.y + ((bottomRect.size.height /2)+arrowHeight));
335     points[2] = NSMakePoint(bottomRect.origin.x + (bottomRect.size.width /2), frame.origin.y + ((bottomRect.size.height /2) - arrowHeight));
336     
337     [arrow appendBezierPathWithPoints: points count: 3];
338     
339     if([self isEnabled]) {
340         if(isBottomDown)
341             [_darkStrokeColor set];
342         else
343             [_strokeColor set];
344     } else {
345         [_disabledStrokeColor set];
346     }
347     
348     [arrow fill];
349 }
350
351 @end