Updated ITButtonCell with custom drawing of title. Stupid NSButtonCell
[ITKit.git] / ITButtonCell.m
1 #import "ITButtonCell.h"
2 #import "ITTextFieldCell.h"
3
4
5 #define GRAY_EXTRA_PAD_H 60.0
6
7
8 @interface ITButtonCell (Private)
9 - (void)drawGrayRoundedBezelWithFrame:(NSRect)rect inView:(NSView *)controlView;
10 @end
11
12
13 @implementation ITButtonCell
14
15 - (id)init
16 {
17     if ( ( self = [super init] ) ) {
18         _subStyle = 0;
19     }
20     
21     return self;
22 }
23
24
25
26 /*************************************************************************/
27 #pragma mark -
28 #pragma mark INSTANCE METHODS
29 /*************************************************************************/
30
31 - (void)setBezelStyle:(NSBezelStyle)bezelStyle
32 {
33     if ( bezelStyle == ITGrayRoundedBezelStyle ) {
34         _subStyle  = bezelStyle;
35         bezelStyle = NSRegularSquareBezelStyle;
36     } else {
37         _subStyle = 0;
38     }
39
40     [super setBezelStyle:bezelStyle];
41 }
42
43
44 /*************************************************************************/
45 #pragma mark -
46 #pragma mark DRAWING METHODS
47 /*************************************************************************/
48
49 - (void)drawWithFrame:(NSRect)rect inView:(NSView *)controlView
50 {
51     if ( _subStyle == ITGrayRoundedBezelStyle ) {
52         [self drawGrayRoundedBezelWithFrame:rect inView:controlView];
53         if ( [self attributedTitle] ) {
54             NSPoint stringOrigin;
55             NSSize stringSize;
56             stringSize = [[self attributedTitle] size];
57             stringOrigin.x = rect.origin.x + (rect.size.width - stringSize.width)/2;
58             stringOrigin.y = (rect.origin.y + (rect.size.height - stringSize.height)/2) - 2;
59             [controlView lockFocus];
60             [[self attributedTitle] drawAtPoint:stringOrigin];
61             [controlView unlockFocus];
62         } else {
63             [super drawInteriorWithFrame:rect inView:controlView];
64         }
65         [[controlView superview] setNeedsDisplay:YES];
66     } else {
67         [super drawWithFrame:rect inView:controlView];
68     }
69 }
70
71 - (void)drawGrayRoundedBezelWithFrame:(NSRect)rect inView:(NSView *)controlView
72 {
73     NSBezierPath *path   = [NSBezierPath bezierPath];
74     float         ch     = rect.size.height;
75     float         cw     = rect.size.width;
76     float         radius = ( ch / 2 );
77     NSPoint       pointA = NSMakePoint( (ch / 2)        , 0.0      );
78     NSPoint       pointB = NSMakePoint( (cw - (ch / 2)) , 0.0      );
79 //  NSPoint       pointC = NSMakePoint( (cw - (ch / 2)) , ch       );
80     NSPoint       pointD = NSMakePoint( (ch / 2)        , ch       );
81     NSPoint       lCtr   = NSMakePoint( (ch / 2)        , (ch / 2) );
82     NSPoint       rCtr   = NSMakePoint( (cw - (ch / 2)) , (ch / 2) );
83     float         alpha  = 0.45;
84     
85     [path moveToPoint:pointA];
86     [path lineToPoint:pointB];
87     [path appendBezierPathWithArcWithCenter:rCtr
88                                      radius:radius
89                                  startAngle:270.0
90                                    endAngle:90.0];
91     [path lineToPoint:pointD];
92     [path appendBezierPathWithArcWithCenter:lCtr
93                                      radius:radius
94                                  startAngle:90.0
95                                    endAngle:270.0];
96
97     if ( [self isHighlighted] ) {
98         alpha = 0.60;
99     }
100     
101     [[NSColor colorWithCalibratedWhite:0.15 alpha:alpha] set];
102     [path fill];
103 }
104
105 - (BOOL)isOpaque
106 {
107     return NO;
108 }
109
110 @end