Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Savoir-faire Linux Inc. |
| 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) |
| 36 | self.imageInsets = 5.0f; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | - (void)drawRect:(NSRect)dirtyRect |
| 40 | { |
| 41 | [super drawRect:dirtyRect]; |
| 42 | |
| 43 | NSColor* backgroundColor6; |
| 44 | NSColor* backgroundStrokeColor5; |
| 45 | |
| 46 | if (self.mouseDown || self.state == NSOnState) { |
Alexandre Lision | 266fca0 | 2015-09-28 14:47:05 -0400 | [diff] [blame] | 47 | if (self.highlightColor) { |
| 48 | backgroundColor6 = self.highlightColor; |
| 49 | backgroundStrokeColor5 = [self.highlightColor darkenColorByValue:0.1]; |
| 50 | } else { |
| 51 | backgroundColor6 = [self.bgColor darkenColorByValue:0.3]; |
| 52 | backgroundStrokeColor5 = [self.bgColor darkenColorByValue:0.4]; |
| 53 | } |
| 54 | |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 55 | } else { |
| 56 | backgroundColor6 = self.bgColor; |
| 57 | backgroundStrokeColor5 = [self.bgColor darkenColorByValue:0.1]; |
| 58 | } |
| 59 | |
| 60 | //// Subframes |
| 61 | NSRect group = NSMakeRect(NSMinX(dirtyRect) + floor(NSWidth(dirtyRect) * 0.03333) + 0.5, NSMinY(dirtyRect) + floor(NSHeight(dirtyRect) * 0.03333) + 0.5, floor(NSWidth(dirtyRect) * 0.96667) - floor(NSWidth(dirtyRect) * 0.03333), floor(NSHeight(dirtyRect) * 0.96667) - floor(NSHeight(dirtyRect) * 0.03333)); |
| 62 | |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 63 | //// Group |
| 64 | { |
| 65 | //// Oval Drawing |
Alexandre Lision | 266fca0 | 2015-09-28 14:47:05 -0400 | [diff] [blame] | 66 | NSBezierPath* ovalPath = [NSBezierPath bezierPathWithRoundedRect:NSMakeRect(NSMinX(group) + floor(NSWidth(group) * 0.00000 + 0.5), NSMinY(group) + floor(NSHeight(group) * 0.00000 + 0.5), floor(NSWidth(group) * 1.00000 + 0.5) - floor(NSWidth(group) * 0.00000 + 0.5), floor(NSHeight(group) * 1.00000 + 0.5) - floor(NSHeight(group) * 0.00000 + 0.5)) xRadius:[self.cornerRadius floatValue] yRadius:[self.cornerRadius floatValue]]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 67 | |
| 68 | [backgroundColor6 setFill]; |
| 69 | [ovalPath fill]; |
| 70 | [backgroundStrokeColor5 setStroke]; |
| 71 | [ovalPath setLineWidth: 0.5]; |
| 72 | [ovalPath stroke]; |
| 73 | |
| 74 | [NSGraphicsContext saveGraphicsState]; |
| 75 | |
| 76 | NSBezierPath *path = [NSBezierPath bezierPathWithRect:dirtyRect]; |
| 77 | [path addClip]; |
| 78 | |
| 79 | [self setImagePosition:NSImageOnly]; |
Alexandre Lision | 266fca0 | 2015-09-28 14:47:05 -0400 | [diff] [blame] | 80 | auto rect2 = NSInsetRect(dirtyRect, self.imageInsets, self.imageInsets); |
| 81 | |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 82 | |
| 83 | [[NSColor image:self.image tintedWithColor:[NSColor whiteColor]] |
| 84 | drawInRect:rect2 |
| 85 | fromRect:NSZeroRect |
| 86 | operation:NSCompositeSourceOver |
| 87 | fraction:1.0 |
| 88 | respectFlipped:YES |
| 89 | hints:nil]; |
| 90 | |
| 91 | [NSGraphicsContext restoreGraphicsState]; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | -(void)mouseDown:(NSEvent *)theEvent |
| 96 | { |
| 97 | self.mouseDown = TRUE; |
| 98 | [self setNeedsDisplay:YES]; |
| 99 | |
| 100 | [super mouseDown:theEvent]; |
| 101 | |
| 102 | self.mouseDown = FALSE; |
| 103 | [self setNeedsDisplay:YES]; |
| 104 | } |
| 105 | |
| 106 | @end |