2006-07-15 16:22:42 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Transmission authors and contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#import "BarButton.h"
|
|
|
|
|
|
|
|
@implementation BarButton
|
|
|
|
|
2006-07-15 21:34:15 +00:00
|
|
|
//height of button should be made 17.0
|
2006-07-15 16:22:42 +00:00
|
|
|
- (id) initWithCoder: (NSCoder *) coder
|
|
|
|
{
|
|
|
|
if ((self = [super initWithCoder: coder]))
|
|
|
|
{
|
|
|
|
fEnabled = NO;
|
2006-07-16 04:50:32 +00:00
|
|
|
fTrackingTag = 0;
|
2006-07-15 16:22:42 +00:00
|
|
|
|
2006-07-15 16:33:39 +00:00
|
|
|
NSSize buttonSize = [self frame].size;
|
|
|
|
fButtonNormal = [[NSImage alloc] initWithSize: buttonSize];
|
2006-07-15 21:34:15 +00:00
|
|
|
fButtonOver = [[NSImage alloc] initWithSize: buttonSize];
|
|
|
|
fButtonPressed = [[NSImage alloc] initWithSize: buttonSize];
|
|
|
|
fButtonSelected = [[NSImage alloc] initWithSize: buttonSize];
|
2006-07-15 16:22:42 +00:00
|
|
|
|
2006-07-16 04:55:02 +00:00
|
|
|
//rolled over button
|
2006-07-15 21:34:15 +00:00
|
|
|
NSImage * leftOver = [NSImage imageNamed: @"FilterButtonOverLeft.png"],
|
|
|
|
* rightOver = [NSImage imageNamed: @"FilterButtonOverRight.png"],
|
|
|
|
* mainOver = [NSImage imageNamed: @"FilterButtonOverMain.png"];
|
2006-07-15 16:22:42 +00:00
|
|
|
|
2006-07-16 05:01:14 +00:00
|
|
|
NSSize endSize = [leftOver size],
|
|
|
|
mainSize = NSMakeSize(buttonSize.width - endSize.width * 2.0, endSize.height);
|
2006-07-15 21:34:15 +00:00
|
|
|
NSPoint leftPoint = NSMakePoint(0, 0),
|
2006-07-16 05:01:14 +00:00
|
|
|
mainPoint = NSMakePoint(endSize.width, 0),
|
|
|
|
rightPoint = NSMakePoint(buttonSize.width - endSize.width, 0);
|
2006-07-15 21:34:15 +00:00
|
|
|
|
|
|
|
[mainOver setScalesWhenResized: YES];
|
|
|
|
[mainOver setSize: mainSize];
|
|
|
|
|
|
|
|
[fButtonOver lockFocus];
|
|
|
|
[leftOver compositeToPoint: leftPoint operation: NSCompositeSourceOver];
|
|
|
|
[mainOver compositeToPoint: mainPoint operation: NSCompositeSourceOver];
|
|
|
|
[rightOver compositeToPoint: rightPoint operation: NSCompositeSourceOver];
|
|
|
|
[fButtonOver unlockFocus];
|
|
|
|
|
2006-07-16 04:55:02 +00:00
|
|
|
//pressed button
|
2006-07-15 21:34:15 +00:00
|
|
|
NSImage * leftPressed = [NSImage imageNamed: @"FilterButtonPressedLeft.png"],
|
|
|
|
* rightPressed = [NSImage imageNamed: @"FilterButtonPressedRight.png"],
|
|
|
|
* mainPressed = [NSImage imageNamed: @"FilterButtonPressedMain.png"];
|
|
|
|
|
|
|
|
[mainPressed setScalesWhenResized: YES];
|
|
|
|
[mainPressed setSize: mainSize];
|
|
|
|
|
|
|
|
[fButtonPressed lockFocus];
|
|
|
|
[leftPressed compositeToPoint: leftPoint operation: NSCompositeSourceOver];
|
|
|
|
[mainPressed compositeToPoint: mainPoint operation: NSCompositeSourceOver];
|
|
|
|
[rightPressed compositeToPoint: rightPoint operation: NSCompositeSourceOver];
|
|
|
|
[fButtonPressed unlockFocus];
|
|
|
|
|
2006-07-16 04:55:02 +00:00
|
|
|
//selected button
|
2006-07-15 21:34:15 +00:00
|
|
|
NSImage * leftSelected = [NSImage imageNamed: @"FilterButtonSelectedLeft.png"],
|
|
|
|
* rightSelected = [NSImage imageNamed: @"FilterButtonSelectedRight.png"],
|
|
|
|
* mainSelected = [NSImage imageNamed: @"FilterButtonSelectedMain.png"];
|
|
|
|
|
|
|
|
[mainSelected setScalesWhenResized: YES];
|
|
|
|
[mainSelected setSize: mainSize];
|
|
|
|
|
|
|
|
[fButtonSelected lockFocus];
|
|
|
|
[leftSelected compositeToPoint: leftPoint operation: NSCompositeSourceOver];
|
|
|
|
[mainSelected compositeToPoint: mainPoint operation: NSCompositeSourceOver];
|
|
|
|
[rightSelected compositeToPoint: rightPoint operation: NSCompositeSourceOver];
|
|
|
|
[fButtonSelected unlockFocus];
|
2006-07-15 18:42:22 +00:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(resetBounds:)
|
|
|
|
name: NSViewBoundsDidChangeNotification object: nil];
|
2006-07-15 16:22:42 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[fButtonNormal release];
|
2006-07-15 21:34:15 +00:00
|
|
|
[fButtonOver release];
|
|
|
|
[fButtonPressed release];
|
|
|
|
[fButtonSelected release];
|
2006-07-15 16:22:42 +00:00
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2006-07-16 04:55:02 +00:00
|
|
|
//call only once to avoid overlapping text
|
2006-07-15 16:22:42 +00:00
|
|
|
- (void) setText: (NSString *) text
|
|
|
|
{
|
2006-07-15 18:22:10 +00:00
|
|
|
NSShadow * stringShadow = [[NSShadow alloc] init];
|
|
|
|
[stringShadow setShadowOffset: NSMakeSize(1.0, -1.0)];
|
|
|
|
[stringShadow setShadowBlurRadius: 1.0];
|
|
|
|
|
2006-07-15 16:22:42 +00:00
|
|
|
NSDictionary * normalAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSColor blackColor], NSForegroundColorAttributeName,
|
2006-07-16 03:45:55 +00:00
|
|
|
[NSFont messageFontOfSize: 12.0], NSFontAttributeName,
|
2006-07-15 18:22:10 +00:00
|
|
|
stringShadow, NSShadowAttributeName, nil];
|
2006-07-15 16:22:42 +00:00
|
|
|
NSDictionary * highlightedAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSColor whiteColor], NSForegroundColorAttributeName,
|
2006-07-16 03:45:55 +00:00
|
|
|
[NSFont messageFontOfSize: 12.0], NSFontAttributeName,
|
2006-07-15 18:22:10 +00:00
|
|
|
stringShadow, NSShadowAttributeName, nil];
|
|
|
|
|
|
|
|
[stringShadow release];
|
2006-07-15 16:22:42 +00:00
|
|
|
|
2006-07-15 18:57:50 +00:00
|
|
|
NSSize textSize = [text sizeWithAttributes: normalAttributes],
|
|
|
|
buttonSize = [self frame].size;
|
|
|
|
NSRect textRect = NSMakeRect((buttonSize.width - textSize.width) * 0.5,
|
2006-07-16 03:52:10 +00:00
|
|
|
(buttonSize.height - textSize.height) * 0.5 + 1.5, textSize.width, textSize.height);
|
2006-07-15 16:22:42 +00:00
|
|
|
|
2006-07-16 04:55:02 +00:00
|
|
|
//normal button
|
2006-07-15 16:22:42 +00:00
|
|
|
[fButtonNormal lockFocus];
|
|
|
|
[text drawInRect: textRect withAttributes: normalAttributes];
|
|
|
|
[fButtonNormal unlockFocus];
|
|
|
|
|
2006-07-16 04:55:02 +00:00
|
|
|
//rolled over button
|
2006-07-15 21:34:15 +00:00
|
|
|
[fButtonOver lockFocus];
|
|
|
|
[text drawInRect: textRect withAttributes: highlightedAttributes];
|
|
|
|
[fButtonOver unlockFocus];
|
|
|
|
|
2006-07-16 04:55:02 +00:00
|
|
|
//pressed button
|
2006-07-15 21:34:15 +00:00
|
|
|
[fButtonPressed lockFocus];
|
2006-07-15 16:22:42 +00:00
|
|
|
[text drawInRect: textRect withAttributes: highlightedAttributes];
|
2006-07-15 21:34:15 +00:00
|
|
|
[fButtonPressed unlockFocus];
|
2006-07-15 16:22:42 +00:00
|
|
|
|
2006-07-16 04:55:02 +00:00
|
|
|
//selected button
|
2006-07-15 21:34:15 +00:00
|
|
|
[fButtonSelected lockFocus];
|
2006-07-15 16:22:42 +00:00
|
|
|
[text drawInRect: textRect withAttributes: highlightedAttributes];
|
2006-07-15 21:34:15 +00:00
|
|
|
[fButtonSelected unlockFocus];
|
2006-07-15 16:22:42 +00:00
|
|
|
|
|
|
|
[self setImage: fButtonNormal];
|
2006-07-15 16:57:06 +00:00
|
|
|
|
|
|
|
[normalAttributes release];
|
|
|
|
[highlightedAttributes release];
|
2006-07-15 16:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseEntered: (NSEvent *) event
|
|
|
|
{
|
|
|
|
if (!fEnabled)
|
2006-07-15 21:34:15 +00:00
|
|
|
[self setImage: fButtonOver];
|
2006-07-15 16:22:42 +00:00
|
|
|
|
|
|
|
[super mouseEntered: event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseExited: (NSEvent *) event
|
|
|
|
{
|
|
|
|
if (!fEnabled)
|
|
|
|
[self setImage: fButtonNormal];
|
|
|
|
|
|
|
|
[super mouseExited: event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseDown: (NSEvent *) event
|
|
|
|
{
|
2006-07-15 21:34:15 +00:00
|
|
|
[self setImage: fButtonPressed];
|
2006-07-15 16:22:42 +00:00
|
|
|
|
|
|
|
[super mouseDown: event];
|
|
|
|
|
|
|
|
//mouse up after mouse down
|
|
|
|
if (NSPointInRect([self convertPoint: [[self window] convertScreenToBase:
|
|
|
|
[NSEvent mouseLocation]] fromView: nil], [self bounds]))
|
|
|
|
[NSApp sendAction: [self action] to: [self target] from: self];
|
|
|
|
|
2006-07-15 21:34:15 +00:00
|
|
|
[self setImage: fEnabled ? fButtonSelected : fButtonOver];
|
2006-07-15 16:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setEnabled: (BOOL) enable
|
|
|
|
{
|
|
|
|
fEnabled = enable;
|
2006-07-15 21:34:15 +00:00
|
|
|
[self setImage: fEnabled ? fButtonSelected : fButtonNormal];
|
2006-07-15 16:22:42 +00:00
|
|
|
}
|
|
|
|
|
2006-07-15 18:42:22 +00:00
|
|
|
- (void) resetBounds: (NSNotification *) notification
|
|
|
|
{
|
2006-07-16 04:50:32 +00:00
|
|
|
if (fTrackingTag)
|
|
|
|
[self removeTrackingRect: fTrackingTag];
|
2006-07-15 19:38:32 +00:00
|
|
|
fTrackingTag = [self addTrackingRect: [self bounds] owner: self userData: nil assumeInside: NO];
|
2006-07-15 18:42:22 +00:00
|
|
|
}
|
|
|
|
|
2006-07-15 16:22:42 +00:00
|
|
|
@end
|