1 | /****************************************************************************** |
---|
2 | * $Id$ |
---|
3 | * |
---|
4 | * Copyright (c) 2006 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #import "BarButton.h" |
---|
26 | |
---|
27 | @implementation BarButton |
---|
28 | |
---|
29 | - (id) initWithCoder: (NSCoder *) coder |
---|
30 | { |
---|
31 | if ((self = [super initWithCoder: coder])) |
---|
32 | { |
---|
33 | fEnabled = NO; |
---|
34 | |
---|
35 | NSSize buttonSize = [self frame].size; |
---|
36 | fButtonNormal = [[NSImage alloc] initWithSize: buttonSize]; |
---|
37 | fButtonIn = [[NSImage alloc] initWithSize: buttonSize]; |
---|
38 | fButtonDown = [[NSImage alloc] initWithSize: buttonSize]; |
---|
39 | |
---|
40 | //create shape |
---|
41 | NSBezierPath * rect = [NSBezierPath bezierPath]; |
---|
42 | float ovalDiamater = 20.0; |
---|
43 | [rect appendBezierPathWithOvalInRect: |
---|
44 | NSMakeRect(0, 0, ovalDiamater, buttonSize.height)]; |
---|
45 | [rect appendBezierPathWithOvalInRect: |
---|
46 | NSMakeRect(buttonSize.width - ovalDiamater, 0, ovalDiamater, buttonSize.height)]; |
---|
47 | [rect appendBezierPathWithRect: |
---|
48 | NSMakeRect(ovalDiamater * 0.5, 0, buttonSize.width - ovalDiamater, buttonSize.height)]; |
---|
49 | |
---|
50 | //create highlighted button |
---|
51 | [fButtonIn lockFocus]; |
---|
52 | [[NSColor colorWithCalibratedRed: 0.4941 green: 0.5647 blue: 0.6706 alpha: 1.0] set]; |
---|
53 | [rect fill]; |
---|
54 | [fButtonIn unlockFocus]; |
---|
55 | |
---|
56 | //create pushed button |
---|
57 | [fButtonDown lockFocus]; |
---|
58 | [[NSColor colorWithCalibratedWhite: 0.0 alpha: 0.6] set]; |
---|
59 | [rect fill]; |
---|
60 | [fButtonDown unlockFocus]; |
---|
61 | |
---|
62 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(resetBounds:) |
---|
63 | name: NSViewBoundsDidChangeNotification object: nil]; |
---|
64 | } |
---|
65 | return self; |
---|
66 | } |
---|
67 | |
---|
68 | - (void) awakeFromNib |
---|
69 | { |
---|
70 | trackingTag = [self addTrackingRect: [self bounds] owner: self userData: nil assumeInside: NO]; |
---|
71 | } |
---|
72 | |
---|
73 | - (void) dealloc |
---|
74 | { |
---|
75 | [fButtonNormal release]; |
---|
76 | [fButtonIn release]; |
---|
77 | [fButtonDown release]; |
---|
78 | |
---|
79 | [super dealloc]; |
---|
80 | } |
---|
81 | |
---|
82 | //call only once to avoid overlap |
---|
83 | - (void) setText: (NSString *) text |
---|
84 | { |
---|
85 | NSShadow * stringShadow = [[NSShadow alloc] init]; |
---|
86 | [stringShadow setShadowOffset: NSMakeSize(1.0, -1.0)]; |
---|
87 | [stringShadow setShadowBlurRadius: 1.0]; |
---|
88 | |
---|
89 | NSDictionary * normalAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
90 | [NSColor blackColor], NSForegroundColorAttributeName, |
---|
91 | [NSFont messageFontOfSize: 11.0], NSFontAttributeName, |
---|
92 | stringShadow, NSShadowAttributeName, nil]; |
---|
93 | NSDictionary * highlightedAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
94 | [NSColor whiteColor], NSForegroundColorAttributeName, |
---|
95 | [NSFont messageFontOfSize: 11.0], NSFontAttributeName, |
---|
96 | stringShadow, NSShadowAttributeName, nil]; |
---|
97 | |
---|
98 | [stringShadow release]; |
---|
99 | |
---|
100 | NSSize textSize = [text sizeWithAttributes: normalAttributes]; |
---|
101 | NSRect textRect = NSMakeRect(([self frame].size.width - textSize.width) * 0.5, |
---|
102 | ([self frame].size.height - textSize.height) * 0.5, textSize.width, textSize.height); |
---|
103 | |
---|
104 | //create normal button |
---|
105 | [fButtonNormal lockFocus]; |
---|
106 | [text drawInRect: textRect withAttributes: normalAttributes]; |
---|
107 | [fButtonNormal unlockFocus]; |
---|
108 | |
---|
109 | //create highlighted button |
---|
110 | [fButtonIn lockFocus]; |
---|
111 | [text drawInRect: textRect withAttributes: highlightedAttributes]; |
---|
112 | [fButtonIn unlockFocus]; |
---|
113 | |
---|
114 | //create pushed button |
---|
115 | [fButtonDown lockFocus]; |
---|
116 | [text drawInRect: textRect withAttributes: highlightedAttributes]; |
---|
117 | [fButtonDown unlockFocus]; |
---|
118 | |
---|
119 | [self setImage: fButtonNormal]; |
---|
120 | |
---|
121 | [normalAttributes release]; |
---|
122 | [highlightedAttributes release]; |
---|
123 | } |
---|
124 | |
---|
125 | - (void) mouseEntered: (NSEvent *) event |
---|
126 | { |
---|
127 | if (!fEnabled) |
---|
128 | [self setImage: fButtonIn]; |
---|
129 | |
---|
130 | [super mouseEntered: event]; |
---|
131 | } |
---|
132 | |
---|
133 | - (void) mouseExited: (NSEvent *) event |
---|
134 | { |
---|
135 | if (!fEnabled) |
---|
136 | [self setImage: fButtonNormal]; |
---|
137 | |
---|
138 | [super mouseExited: event]; |
---|
139 | } |
---|
140 | |
---|
141 | - (void) mouseDown: (NSEvent *) event |
---|
142 | { |
---|
143 | [self setImage: fButtonDown]; |
---|
144 | |
---|
145 | [super mouseDown: event]; |
---|
146 | |
---|
147 | //mouse up after mouse down |
---|
148 | if (NSPointInRect([self convertPoint: [[self window] convertScreenToBase: |
---|
149 | [NSEvent mouseLocation]] fromView: nil], [self bounds])) |
---|
150 | [NSApp sendAction: [self action] to: [self target] from: self]; |
---|
151 | |
---|
152 | [self setImage: fButtonIn]; |
---|
153 | } |
---|
154 | |
---|
155 | - (void) setEnabled: (BOOL) enable |
---|
156 | { |
---|
157 | fEnabled = enable; |
---|
158 | [self setImage: fEnabled ? fButtonIn : fButtonNormal]; |
---|
159 | } |
---|
160 | |
---|
161 | - (void) resetBounds: (NSNotification *) notification |
---|
162 | { |
---|
163 | [self removeTrackingRect: trackingTag]; |
---|
164 | [self addTrackingRect: [self bounds] owner: self userData: nil assumeInside: NO]; |
---|
165 | } |
---|
166 | |
---|
167 | @end |
---|