Kateryna Kostiuk | d9039e9 | 2017-05-24 14:29:54 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015-2017 Savoir-faire Linux Inc. |
| 3 | * Author: Kateryna Kostiuk <kateryna.kostiuk@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 | #import "RoundedTextField.h" |
| 20 | #import "NSColor+RingTheme.h" |
| 21 | |
| 22 | @implementation RoundedTextField |
| 23 | |
| 24 | -(void) awakeFromNib { |
| 25 | if (!self.bgColor) { |
| 26 | self.bgColor = [NSColor controlColor]; |
| 27 | } |
| 28 | |
| 29 | if (!self.cornerRadius) { |
| 30 | self.cornerRadius = @(NSWidth(self.frame) / 2); |
| 31 | } |
| 32 | |
| 33 | if(!self.borderColor) { |
| 34 | self.borderColor = [self.bgColor darkenColorByValue:0.1]; |
| 35 | } |
| 36 | |
| 37 | self.backgroundColor = [NSColor controlColor]; |
| 38 | } |
| 39 | |
| 40 | - (void)drawRect:(NSRect)dirtyRect { |
| 41 | [super drawRect:dirtyRect]; |
| 42 | |
| 43 | NSColor* backgroundColor = self.bgColor; |
| 44 | NSColor* borderColor = self.borderColor; |
| 45 | |
Kateryna Kostiuk | c14b584 | 2017-06-14 16:31:21 -0400 | [diff] [blame] | 46 | NSRect group = NSMakeRect(NSMinX(dirtyRect) + floor(NSWidth(dirtyRect) * 0.03333) + 0.5, |
| 47 | NSMinY(dirtyRect) + floor(NSHeight(dirtyRect) * 0.03333) + 0.5, |
| 48 | floor(NSWidth(dirtyRect) * 0.96667) - floor(NSWidth(dirtyRect) * 0.03333), |
| 49 | floor(NSHeight(dirtyRect) * 0.96667) - floor(NSHeight(dirtyRect) * 0.03333)); |
Kateryna Kostiuk | d9039e9 | 2017-05-24 14:29:54 -0400 | [diff] [blame] | 50 | |
Kateryna Kostiuk | c14b584 | 2017-06-14 16:31:21 -0400 | [diff] [blame] | 51 | NSBezierPath* ovalPath = [NSBezierPath bezierPathWithRoundedRect: |
| 52 | NSMakeRect(NSMinX(group) + floor(NSWidth(group) * 0.00000 + 0.5), |
| 53 | NSMinY(group) + floor(NSHeight(group) * 0.00000 + 0.5), |
| 54 | floor(NSWidth(group) * 1.00000 + 0.5) - floor(NSWidth(group) * 0.00000 + 0.5), |
| 55 | floor(NSHeight(group) * 1.00000 + 0.5) - floor(NSHeight(group) * 0.00000 + 0.5)) |
| 56 | xRadius:[self.cornerRadius floatValue] yRadius:[self.cornerRadius floatValue]]; |
Kateryna Kostiuk | d9039e9 | 2017-05-24 14:29:54 -0400 | [diff] [blame] | 57 | [backgroundColor setFill]; |
| 58 | [ovalPath fill]; |
| 59 | [borderColor setStroke]; |
| 60 | [ovalPath setLineWidth: 0.5]; |
| 61 | [ovalPath stroke]; |
| 62 | NSDictionary *att = nil; |
| 63 | |
| 64 | NSMutableParagraphStyle *style = |
| 65 | [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
| 66 | [style setLineBreakMode:NSLineBreakByWordWrapping]; |
| 67 | [style setAlignment:NSCenterTextAlignment]; |
| 68 | att = [[NSDictionary alloc] initWithObjectsAndKeys: |
| 69 | style, NSParagraphStyleAttributeName, |
| 70 | [self textColor], |
| 71 | NSForegroundColorAttributeName, nil]; |
| 72 | [[self stringValue] drawInRect:dirtyRect withAttributes:att]; |
| 73 | } |
| 74 | |
| 75 | @end |