Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 3 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #import "IconButton.h" |
| 21 | |
| 22 | #import "NSColor+RingTheme.h" |
| 23 | |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 24 | @implementation IconButton |
| 25 | |
| 26 | -(void) awakeFromNib { |
| 27 | if (!self.bgColor) { |
| 28 | self.bgColor = [NSColor ringBlue]; |
| 29 | } |
| 30 | |
| 31 | if (!self.cornerRadius) { |
| 32 | self.cornerRadius = @(NSWidth(self.frame) / 2); |
| 33 | } |
Alexandre Lision | 266fca0 | 2015-09-28 14:47:05 -0400 | [diff] [blame] | 34 | |
| 35 | if (self.imageInsets == 0) |
Nicolas Jager | eccf8ae | 2016-02-05 16:34:00 -0500 | [diff] [blame] | 36 | self.imageInsets = 8.0f; |
Alexandre Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 37 | |
| 38 | self.pressed = NO; |
| 39 | } |
| 40 | |
| 41 | -(void) setPressed:(BOOL)newVal |
| 42 | { |
| 43 | _pressed = newVal; |
| 44 | [self setNeedsDisplay:YES]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | - (void)drawRect:(NSRect)dirtyRect |
| 48 | { |
| 49 | [super drawRect:dirtyRect]; |
| 50 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 51 | NSColor* backgroundColor; |
| 52 | NSColor* backgroundStrokeColor; |
| 53 | NSColor* tintColor = [NSColor whiteColor]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 54 | |
Alexandre Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 55 | if (self.mouseDown || self.isPressed) { |
Alexandre Lision | 266fca0 | 2015-09-28 14:47:05 -0400 | [diff] [blame] | 56 | if (self.highlightColor) { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 57 | backgroundColor = self.highlightColor; |
| 58 | backgroundStrokeColor = [self.highlightColor darkenColorByValue:0.1]; |
Alexandre Lision | 266fca0 | 2015-09-28 14:47:05 -0400 | [diff] [blame] | 59 | } else { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 60 | backgroundColor = [self.bgColor darkenColorByValue:0.3]; |
| 61 | backgroundStrokeColor = [self.bgColor darkenColorByValue:0.4]; |
Alexandre Lision | 266fca0 | 2015-09-28 14:47:05 -0400 | [diff] [blame] | 62 | } |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 63 | } else if (!self.isEnabled) { |
| 64 | backgroundColor = [self.bgColor colorWithAlphaComponent:0.7]; |
| 65 | backgroundStrokeColor = [self.bgColor colorWithAlphaComponent:0.7]; |
| 66 | tintColor = [[NSColor grayColor] colorWithAlphaComponent:0.3]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 67 | } else { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 68 | backgroundColor = self.bgColor; |
| 69 | backgroundStrokeColor = [self.bgColor darkenColorByValue:0.1]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | //// Subframes |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 73 | NSRect group = NSMakeRect(NSMinX(dirtyRect) + floor(NSWidth(dirtyRect) * 0.03333) + 0.5, |
| 74 | NSMinY(dirtyRect) + floor(NSHeight(dirtyRect) * 0.03333) + 0.5, |
| 75 | floor(NSWidth(dirtyRect) * 0.96667) - floor(NSWidth(dirtyRect) * 0.03333), |
| 76 | floor(NSHeight(dirtyRect) * 0.96667) - floor(NSHeight(dirtyRect) * 0.03333)); |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 77 | |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 78 | //// Group |
| 79 | { |
| 80 | //// Oval Drawing |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 81 | NSBezierPath* ovalPath = [NSBezierPath bezierPathWithRoundedRect: |
| 82 | NSMakeRect(NSMinX(group) + floor(NSWidth(group) * 0.00000 + 0.5), |
| 83 | NSMinY(group) + floor(NSHeight(group) * 0.00000 + 0.5), |
| 84 | floor(NSWidth(group) * 1.00000 + 0.5) - floor(NSWidth(group) * 0.00000 + 0.5), |
| 85 | floor(NSHeight(group) * 1.00000 + 0.5) - floor(NSHeight(group) * 0.00000 + 0.5)) |
| 86 | xRadius:[self.cornerRadius floatValue] yRadius:[self.cornerRadius floatValue]]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 87 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 88 | [backgroundColor setFill]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 89 | [ovalPath fill]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 90 | [backgroundStrokeColor setStroke]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 91 | [ovalPath setLineWidth: 0.5]; |
| 92 | [ovalPath stroke]; |
| 93 | |
| 94 | [NSGraphicsContext saveGraphicsState]; |
| 95 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 96 | NSBezierPath* path = [NSBezierPath bezierPathWithRect:dirtyRect]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 97 | [path addClip]; |
| 98 | |
| 99 | [self setImagePosition:NSImageOnly]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 100 | auto rect = NSInsetRect(dirtyRect, self.imageInsets, self.imageInsets); |
Alexandre Lision | 266fca0 | 2015-09-28 14:47:05 -0400 | [diff] [blame] | 101 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 102 | [[NSColor image:self.image tintedWithColor:tintColor] drawInRect:rect |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 103 | fromRect:NSZeroRect |
| 104 | operation:NSCompositeSourceOver |
| 105 | fraction:1.0 |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 106 | respectFlipped:YES |
| 107 | hints:nil]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 108 | |
| 109 | [NSGraphicsContext restoreGraphicsState]; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | -(void)mouseDown:(NSEvent *)theEvent |
| 114 | { |
| 115 | self.mouseDown = TRUE; |
| 116 | [self setNeedsDisplay:YES]; |
| 117 | |
| 118 | [super mouseDown:theEvent]; |
| 119 | |
| 120 | self.mouseDown = FALSE; |
| 121 | [self setNeedsDisplay:YES]; |
| 122 | } |
| 123 | |
| 124 | @end |