1 | /****************************************************************************** |
---|
2 | * $Id: BarButton.m 732 2006-08-07 00:11:07Z livings124 $ |
---|
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 | @interface BarButton (Private) |
---|
28 | |
---|
29 | - (void) createButtons; |
---|
30 | |
---|
31 | @end |
---|
32 | |
---|
33 | @implementation BarButton |
---|
34 | |
---|
35 | //height of button should be 17.0 |
---|
36 | - (id) initWithCoder: (NSCoder *) coder |
---|
37 | { |
---|
38 | if ((self = [super initWithCoder: coder])) |
---|
39 | { |
---|
40 | fEnabled = NO; |
---|
41 | fTrackingTag = 0; |
---|
42 | |
---|
43 | [self createButtons]; |
---|
44 | |
---|
45 | [self setImage: fButtonNormal]; |
---|
46 | [self setAlternateImage: fButtonPressed]; |
---|
47 | |
---|
48 | NSNotificationCenter * nc = [NSNotificationCenter defaultCenter]; |
---|
49 | |
---|
50 | [nc addObserver: self selector: @selector(setForActive:) |
---|
51 | name: NSWindowDidBecomeKeyNotification object: nil]; |
---|
52 | |
---|
53 | [nc addObserver: self selector: @selector(setForInactive:) |
---|
54 | name: NSWindowDidResignKeyNotification object: nil]; |
---|
55 | |
---|
56 | [nc addObserver: self selector: @selector(resetBounds:) |
---|
57 | name: NSViewFrameDidChangeNotification object: nil]; |
---|
58 | } |
---|
59 | return self; |
---|
60 | } |
---|
61 | |
---|
62 | - (void) dealloc |
---|
63 | { |
---|
64 | [[NSNotificationCenter defaultCenter] removeObserver: self]; |
---|
65 | |
---|
66 | [fButtonNormal release]; |
---|
67 | [fButtonOver release]; |
---|
68 | [fButtonPressed release]; |
---|
69 | [fButtonSelected release]; |
---|
70 | [fButtonSelectedDim release]; |
---|
71 | |
---|
72 | [super dealloc]; |
---|
73 | } |
---|
74 | |
---|
75 | //call only once |
---|
76 | - (void) createButtons |
---|
77 | { |
---|
78 | NSSize buttonSize = [self frame].size; |
---|
79 | fButtonNormal = [[NSImage alloc] initWithSize: buttonSize]; |
---|
80 | fButtonNormalDim = [[NSImage alloc] initWithSize: buttonSize]; |
---|
81 | fButtonOver = [[NSImage alloc] initWithSize: buttonSize]; |
---|
82 | fButtonPressed = [[NSImage alloc] initWithSize: buttonSize]; |
---|
83 | fButtonSelected = [[NSImage alloc] initWithSize: buttonSize]; |
---|
84 | fButtonSelectedDim = [[NSImage alloc] initWithSize: buttonSize]; |
---|
85 | |
---|
86 | //rolled over button |
---|
87 | NSImage * leftOver = [NSImage imageNamed: @"FilterButtonOverLeft.png"], |
---|
88 | * rightOver = [NSImage imageNamed: @"FilterButtonOverRight.png"], |
---|
89 | * mainOver = [NSImage imageNamed: @"FilterButtonOverMain.png"]; |
---|
90 | |
---|
91 | NSSize endSize = [leftOver size], |
---|
92 | mainSize = NSMakeSize(buttonSize.width - endSize.width * 2.0, endSize.height); |
---|
93 | NSPoint leftPoint = NSMakePoint(0, 0), |
---|
94 | rightPoint = NSMakePoint(buttonSize.width - endSize.width, 0), |
---|
95 | mainPoint = NSMakePoint(endSize.width, 0); |
---|
96 | |
---|
97 | [mainOver setScalesWhenResized: YES]; |
---|
98 | [mainOver setSize: mainSize]; |
---|
99 | |
---|
100 | [fButtonOver lockFocus]; |
---|
101 | [leftOver compositeToPoint: leftPoint operation: NSCompositeSourceOver]; |
---|
102 | [mainOver compositeToPoint: mainPoint operation: NSCompositeSourceOver]; |
---|
103 | [rightOver compositeToPoint: rightPoint operation: NSCompositeSourceOver]; |
---|
104 | [fButtonOver unlockFocus]; |
---|
105 | |
---|
106 | //pressed button |
---|
107 | NSImage * leftPressed = [NSImage imageNamed: @"FilterButtonPressedLeft.png"], |
---|
108 | * rightPressed = [NSImage imageNamed: @"FilterButtonPressedRight.png"], |
---|
109 | * mainPressed = [NSImage imageNamed: @"FilterButtonPressedMain.png"]; |
---|
110 | |
---|
111 | [mainPressed setScalesWhenResized: YES]; |
---|
112 | [mainPressed setSize: mainSize]; |
---|
113 | |
---|
114 | [fButtonPressed lockFocus]; |
---|
115 | [leftPressed compositeToPoint: leftPoint operation: NSCompositeSourceOver]; |
---|
116 | [mainPressed compositeToPoint: mainPoint operation: NSCompositeSourceOver]; |
---|
117 | [rightPressed compositeToPoint: rightPoint operation: NSCompositeSourceOver]; |
---|
118 | [fButtonPressed unlockFocus]; |
---|
119 | |
---|
120 | //selected button |
---|
121 | NSImage * leftSelected = [NSImage imageNamed: @"FilterButtonSelectedLeft.png"], |
---|
122 | * rightSelected = [NSImage imageNamed: @"FilterButtonSelectedRight.png"], |
---|
123 | * mainSelected = [NSImage imageNamed: @"FilterButtonSelectedMain.png"]; |
---|
124 | |
---|
125 | [mainSelected setScalesWhenResized: YES]; |
---|
126 | [mainSelected setSize: mainSize]; |
---|
127 | |
---|
128 | [fButtonSelected lockFocus]; |
---|
129 | [leftSelected compositeToPoint: leftPoint operation: NSCompositeSourceOver]; |
---|
130 | [mainSelected compositeToPoint: mainPoint operation: NSCompositeSourceOver]; |
---|
131 | [rightSelected compositeToPoint: rightPoint operation: NSCompositeSourceOver]; |
---|
132 | [fButtonSelected unlockFocus]; |
---|
133 | |
---|
134 | //selected and dimmed button |
---|
135 | fButtonSelectedDim = [fButtonSelected copy]; |
---|
136 | |
---|
137 | //create button text |
---|
138 | NSString * text = [self title]; |
---|
139 | |
---|
140 | NSFont * boldFont = [[NSFontManager sharedFontManager] convertFont: |
---|
141 | [NSFont fontWithName: @"Lucida Grande" size: 12.0] toHaveTrait: NSBoldFontMask]; |
---|
142 | |
---|
143 | NSSize shadowOffset = NSMakeSize(0.0, -1.0); |
---|
144 | |
---|
145 | NSShadow * shadowNormal = [NSShadow alloc]; |
---|
146 | [shadowNormal setShadowOffset: shadowOffset]; |
---|
147 | [shadowNormal setShadowBlurRadius: 1.0]; |
---|
148 | [shadowNormal setShadowColor: [NSColor colorWithDeviceWhite: 1.0 alpha: 0.4]]; |
---|
149 | |
---|
150 | NSShadow * shadowNormalDim = [NSShadow alloc]; |
---|
151 | [shadowNormalDim setShadowOffset: shadowOffset]; |
---|
152 | [shadowNormalDim setShadowBlurRadius: 1.0]; |
---|
153 | [shadowNormalDim setShadowColor: [NSColor colorWithDeviceWhite: 1.0 alpha: 0.2]]; |
---|
154 | |
---|
155 | NSShadow * shadowHighlighted = [NSShadow alloc]; |
---|
156 | [shadowHighlighted setShadowOffset: shadowOffset]; |
---|
157 | [shadowHighlighted setShadowBlurRadius: 1.0]; |
---|
158 | [shadowHighlighted setShadowColor: [NSColor colorWithDeviceWhite: 0.0 alpha: 0.4]]; |
---|
159 | |
---|
160 | NSDictionary * normalAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
161 | [NSColor colorWithCalibratedRed: 0.259 green: 0.259 blue: 0.259 alpha: 1.0], |
---|
162 | NSForegroundColorAttributeName, |
---|
163 | boldFont, NSFontAttributeName, |
---|
164 | shadowNormal, NSShadowAttributeName, nil], |
---|
165 | * normalDimAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
166 | [NSColor disabledControlTextColor], NSForegroundColorAttributeName, |
---|
167 | boldFont, NSFontAttributeName, |
---|
168 | shadowNormalDim, NSShadowAttributeName, nil], |
---|
169 | * highlightedAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
170 | [NSColor whiteColor], NSForegroundColorAttributeName, |
---|
171 | boldFont, NSFontAttributeName, |
---|
172 | shadowHighlighted, NSShadowAttributeName, nil], |
---|
173 | * highlightedDimAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
174 | [NSColor colorWithCalibratedRed: 0.9 green: 0.9 blue: 0.9 alpha: 1.0], NSForegroundColorAttributeName, |
---|
175 | boldFont, NSFontAttributeName, |
---|
176 | shadowHighlighted, NSShadowAttributeName, nil]; |
---|
177 | |
---|
178 | NSSize textSizeNormal = [text sizeWithAttributes: normalAttributes], |
---|
179 | textSizeBold = [text sizeWithAttributes: highlightedAttributes]; |
---|
180 | |
---|
181 | NSRect textRect = NSMakeRect((buttonSize.width - textSizeNormal.width) * 0.5, |
---|
182 | (buttonSize.height - textSizeNormal.height) * 0.5 + 1.5, textSizeNormal.width, textSizeNormal.height); |
---|
183 | |
---|
184 | [shadowNormal release]; |
---|
185 | [shadowNormalDim release]; |
---|
186 | [shadowHighlighted release]; |
---|
187 | |
---|
188 | //normal button |
---|
189 | [fButtonNormal lockFocus]; |
---|
190 | [text drawInRect: textRect withAttributes: normalAttributes]; |
---|
191 | [fButtonNormal unlockFocus]; |
---|
192 | |
---|
193 | //normal and dim button |
---|
194 | [fButtonNormalDim lockFocus]; |
---|
195 | [text drawInRect: textRect withAttributes: normalDimAttributes]; |
---|
196 | [fButtonNormalDim unlockFocus]; |
---|
197 | |
---|
198 | //rolled over button |
---|
199 | [fButtonOver lockFocus]; |
---|
200 | [text drawInRect: textRect withAttributes: highlightedAttributes]; |
---|
201 | [fButtonOver unlockFocus]; |
---|
202 | |
---|
203 | //pressed button |
---|
204 | [fButtonPressed lockFocus]; |
---|
205 | [text drawInRect: textRect withAttributes: highlightedAttributes]; |
---|
206 | [fButtonPressed unlockFocus]; |
---|
207 | |
---|
208 | //selected button |
---|
209 | [fButtonSelected lockFocus]; |
---|
210 | [text drawInRect: textRect withAttributes: highlightedAttributes]; |
---|
211 | [fButtonSelected unlockFocus]; |
---|
212 | |
---|
213 | //selected and dim button |
---|
214 | [fButtonSelectedDim lockFocus]; |
---|
215 | [text drawInRect: textRect withAttributes: highlightedDimAttributes]; |
---|
216 | [fButtonSelectedDim unlockFocus]; |
---|
217 | |
---|
218 | [normalAttributes release]; |
---|
219 | [normalDimAttributes release]; |
---|
220 | [highlightedAttributes release]; |
---|
221 | [highlightedDimAttributes release]; |
---|
222 | } |
---|
223 | |
---|
224 | - (void) mouseEntered: (NSEvent *) event |
---|
225 | { |
---|
226 | if (!fEnabled) |
---|
227 | [self setImage: fButtonOver]; |
---|
228 | |
---|
229 | [super mouseEntered: event]; |
---|
230 | } |
---|
231 | |
---|
232 | - (void) mouseExited: (NSEvent *) event |
---|
233 | { |
---|
234 | if (!fEnabled) |
---|
235 | [self setImage: fButtonNormal]; |
---|
236 | |
---|
237 | [super mouseExited: event]; |
---|
238 | } |
---|
239 | |
---|
240 | - (void) setEnabled: (BOOL) enable |
---|
241 | { |
---|
242 | fEnabled = enable; |
---|
243 | [self setImage: fEnabled ? fButtonSelected : fButtonNormal]; |
---|
244 | } |
---|
245 | |
---|
246 | - (void) resetBounds: (NSNotification *) notification |
---|
247 | { |
---|
248 | if (fTrackingTag) |
---|
249 | [self removeTrackingRect: fTrackingTag]; |
---|
250 | fTrackingTag = [self addTrackingRect: [self bounds] owner: self userData: nil assumeInside: NO]; |
---|
251 | } |
---|
252 | |
---|
253 | - (void) setForActive: (NSNotification *) notification |
---|
254 | { |
---|
255 | if ([notification object] != [self window]) |
---|
256 | return; |
---|
257 | |
---|
258 | if ([self image] == fButtonSelectedDim) |
---|
259 | [self setImage: fButtonSelected]; |
---|
260 | else if ([self image] == fButtonNormalDim) |
---|
261 | [self setImage: fButtonNormal]; |
---|
262 | else; |
---|
263 | |
---|
264 | [self resetBounds: nil]; |
---|
265 | } |
---|
266 | |
---|
267 | - (void) setForInactive: (NSNotification *) notification |
---|
268 | { |
---|
269 | if ([notification object] != [self window]) |
---|
270 | return; |
---|
271 | |
---|
272 | [self setImage: [self image] == fButtonSelected ? fButtonSelectedDim : fButtonNormalDim]; |
---|
273 | |
---|
274 | if (fTrackingTag) |
---|
275 | [self removeTrackingRect: fTrackingTag]; |
---|
276 | } |
---|
277 | |
---|
278 | @end |
---|