source: trunk/macosx/BarButton.m @ 668

Last change on this file since 668 was 668, checked in by livings124, 16 years ago

Resize window when filtering with auto resize enabled.

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