1 | /****************************************************************************** |
---|
2 | * $Id: BarButton.m 638 2006-07-17 21:40:49Z 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 | @implementation BarButton |
---|
28 | |
---|
29 | //height of button should be made 17.0 |
---|
30 | - (id) initWithCoder: (NSCoder *) coder |
---|
31 | { |
---|
32 | if ((self = [super initWithCoder: coder])) |
---|
33 | { |
---|
34 | fEnabled = NO; |
---|
35 | fTrackingTag = 0; |
---|
36 | |
---|
37 | NSSize buttonSize = [self frame].size; |
---|
38 | fButtonNormal = [[NSImage alloc] initWithSize: buttonSize]; |
---|
39 | fButtonOver = [[NSImage alloc] initWithSize: buttonSize]; |
---|
40 | fButtonPressed = [[NSImage alloc] initWithSize: buttonSize]; |
---|
41 | fButtonSelected = [[NSImage alloc] initWithSize: buttonSize]; |
---|
42 | |
---|
43 | //rolled over button |
---|
44 | NSImage * leftOver = [NSImage imageNamed: @"FilterButtonOverLeft.png"], |
---|
45 | * rightOver = [NSImage imageNamed: @"FilterButtonOverRight.png"], |
---|
46 | * mainOver = [NSImage imageNamed: @"FilterButtonOverMain.png"]; |
---|
47 | |
---|
48 | NSSize endSize = [leftOver size], |
---|
49 | mainSize = NSMakeSize(buttonSize.width - endSize.width * 2.0, endSize.height); |
---|
50 | NSPoint leftPoint = NSMakePoint(0, 0), |
---|
51 | mainPoint = NSMakePoint(endSize.width, 0), |
---|
52 | rightPoint = NSMakePoint(buttonSize.width - endSize.width, 0); |
---|
53 | |
---|
54 | [mainOver setScalesWhenResized: YES]; |
---|
55 | [mainOver setSize: mainSize]; |
---|
56 | |
---|
57 | [fButtonOver lockFocus]; |
---|
58 | [leftOver compositeToPoint: leftPoint operation: NSCompositeSourceOver]; |
---|
59 | [mainOver compositeToPoint: mainPoint operation: NSCompositeSourceOver]; |
---|
60 | [rightOver compositeToPoint: rightPoint operation: NSCompositeSourceOver]; |
---|
61 | [fButtonOver unlockFocus]; |
---|
62 | |
---|
63 | //pressed button |
---|
64 | NSImage * leftPressed = [NSImage imageNamed: @"FilterButtonPressedLeft.png"], |
---|
65 | * rightPressed = [NSImage imageNamed: @"FilterButtonPressedRight.png"], |
---|
66 | * mainPressed = [NSImage imageNamed: @"FilterButtonPressedMain.png"]; |
---|
67 | |
---|
68 | [mainPressed setScalesWhenResized: YES]; |
---|
69 | [mainPressed setSize: mainSize]; |
---|
70 | |
---|
71 | [fButtonPressed lockFocus]; |
---|
72 | [leftPressed compositeToPoint: leftPoint operation: NSCompositeSourceOver]; |
---|
73 | [mainPressed compositeToPoint: mainPoint operation: NSCompositeSourceOver]; |
---|
74 | [rightPressed compositeToPoint: rightPoint operation: NSCompositeSourceOver]; |
---|
75 | [fButtonPressed unlockFocus]; |
---|
76 | |
---|
77 | //selected button |
---|
78 | NSImage * leftSelected = [NSImage imageNamed: @"FilterButtonSelectedLeft.png"], |
---|
79 | * rightSelected = [NSImage imageNamed: @"FilterButtonSelectedRight.png"], |
---|
80 | * mainSelected = [NSImage imageNamed: @"FilterButtonSelectedMain.png"]; |
---|
81 | |
---|
82 | [mainSelected setScalesWhenResized: YES]; |
---|
83 | [mainSelected setSize: mainSize]; |
---|
84 | |
---|
85 | [fButtonSelected lockFocus]; |
---|
86 | [leftSelected compositeToPoint: leftPoint operation: NSCompositeSourceOver]; |
---|
87 | [mainSelected compositeToPoint: mainPoint operation: NSCompositeSourceOver]; |
---|
88 | [rightSelected compositeToPoint: rightPoint operation: NSCompositeSourceOver]; |
---|
89 | [fButtonSelected unlockFocus]; |
---|
90 | |
---|
91 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(resetBounds:) |
---|
92 | name: NSViewBoundsDidChangeNotification object: nil]; |
---|
93 | } |
---|
94 | return self; |
---|
95 | } |
---|
96 | |
---|
97 | - (void) dealloc |
---|
98 | { |
---|
99 | [fButtonNormal release]; |
---|
100 | [fButtonOver release]; |
---|
101 | [fButtonPressed release]; |
---|
102 | [fButtonSelected release]; |
---|
103 | |
---|
104 | [super dealloc]; |
---|
105 | } |
---|
106 | |
---|
107 | //call only once to avoid overlapping text |
---|
108 | - (void) setText: (NSString *) text |
---|
109 | { |
---|
110 | NSDictionary * normalAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
111 | [NSColor blackColor], NSForegroundColorAttributeName, |
---|
112 | [NSFont fontWithName: @"Lucida Grande" size: 12.0], NSFontAttributeName, nil]; |
---|
113 | |
---|
114 | NSFont * boldFont = [[NSFontManager sharedFontManager] convertFont: |
---|
115 | [NSFont fontWithName: @"Lucida Grande" size: 12.0] toHaveTrait: NSBoldFontMask]; |
---|
116 | |
---|
117 | NSDictionary * highlightedAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
118 | [NSColor whiteColor], NSForegroundColorAttributeName, |
---|
119 | boldFont, NSFontAttributeName, nil]; |
---|
120 | |
---|
121 | NSSize textSizeNormal = [text sizeWithAttributes: normalAttributes], |
---|
122 | textSizeBold = [text sizeWithAttributes: highlightedAttributes], |
---|
123 | buttonSize = [self frame].size; |
---|
124 | |
---|
125 | NSRect textRectNormal = NSMakeRect((buttonSize.width - textSizeNormal.width) * 0.5, |
---|
126 | (buttonSize.height - textSizeNormal.height) * 0.5 + 1.5, textSizeNormal.width, textSizeNormal.height), |
---|
127 | textRectBold = NSMakeRect((buttonSize.width - textSizeBold.width) * 0.5, |
---|
128 | (buttonSize.height - textSizeBold.height) * 0.5 + 1.5, textSizeBold.width, textSizeBold.height); |
---|
129 | |
---|
130 | //normal button |
---|
131 | [fButtonNormal lockFocus]; |
---|
132 | [text drawInRect: textRectNormal withAttributes: normalAttributes]; |
---|
133 | [fButtonNormal unlockFocus]; |
---|
134 | |
---|
135 | //rolled over button |
---|
136 | [fButtonOver lockFocus]; |
---|
137 | [text drawInRect: textRectBold withAttributes: highlightedAttributes]; |
---|
138 | [fButtonOver unlockFocus]; |
---|
139 | |
---|
140 | //pressed button |
---|
141 | [fButtonPressed lockFocus]; |
---|
142 | [text drawInRect: textRectBold withAttributes: highlightedAttributes]; |
---|
143 | [fButtonPressed unlockFocus]; |
---|
144 | |
---|
145 | //selected button |
---|
146 | [fButtonSelected lockFocus]; |
---|
147 | [text drawInRect: textRectBold withAttributes: highlightedAttributes]; |
---|
148 | [fButtonSelected unlockFocus]; |
---|
149 | |
---|
150 | [self setImage: fButtonNormal]; |
---|
151 | |
---|
152 | [normalAttributes release]; |
---|
153 | [highlightedAttributes release]; |
---|
154 | |
---|
155 | //NSLog(@"%@ %f", text, textSizeBold.width); |
---|
156 | } |
---|
157 | |
---|
158 | - (void) mouseEntered: (NSEvent *) event |
---|
159 | { |
---|
160 | if (!fEnabled) |
---|
161 | [self setImage: fButtonOver]; |
---|
162 | |
---|
163 | [super mouseEntered: event]; |
---|
164 | } |
---|
165 | |
---|
166 | - (void) mouseExited: (NSEvent *) event |
---|
167 | { |
---|
168 | if (!fEnabled) |
---|
169 | [self setImage: fButtonNormal]; |
---|
170 | |
---|
171 | [super mouseExited: event]; |
---|
172 | } |
---|
173 | |
---|
174 | - (void) mouseDown: (NSEvent *) event |
---|
175 | { |
---|
176 | [self setImage: fButtonPressed]; |
---|
177 | |
---|
178 | [super mouseDown: event]; |
---|
179 | |
---|
180 | //mouse up after mouse down |
---|
181 | if (NSPointInRect([self convertPoint: [[self window] convertScreenToBase: |
---|
182 | [NSEvent mouseLocation]] fromView: nil], [self bounds])) |
---|
183 | [NSApp sendAction: [self action] to: [self target] from: self]; |
---|
184 | |
---|
185 | [self setImage: fEnabled ? fButtonSelected : fButtonOver]; |
---|
186 | } |
---|
187 | |
---|
188 | - (void) setEnabled: (BOOL) enable |
---|
189 | { |
---|
190 | fEnabled = enable; |
---|
191 | [self setImage: fEnabled ? fButtonSelected : fButtonNormal]; |
---|
192 | } |
---|
193 | |
---|
194 | - (void) resetBounds: (NSNotification *) notification |
---|
195 | { |
---|
196 | if (fTrackingTag) |
---|
197 | [self removeTrackingRect: fTrackingTag]; |
---|
198 | fTrackingTag = [self addTrackingRect: [self bounds] owner: self userData: nil assumeInside: NO]; |
---|
199 | } |
---|
200 | |
---|
201 | @end |
---|