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
|
|
|
|
|
|
|
|
- (id) initWithCoder: (NSCoder *) coder
|
|
|
|
{
|
|
|
|
if ((self = [super initWithCoder: coder]))
|
|
|
|
{
|
|
|
|
fEnabled = NO;
|
|
|
|
|
2006-07-15 16:33:39 +00:00
|
|
|
NSSize buttonSize = [self frame].size;
|
|
|
|
fButtonNormal = [[NSImage alloc] initWithSize: buttonSize];
|
|
|
|
fButtonIn = [[NSImage alloc] initWithSize: buttonSize];
|
|
|
|
fButtonDown = [[NSImage alloc] initWithSize: buttonSize];
|
2006-07-15 16:22:42 +00:00
|
|
|
|
|
|
|
//create shape
|
|
|
|
NSBezierPath * rect = [NSBezierPath bezierPath];
|
|
|
|
float ovalDiamater = 20.0;
|
|
|
|
[rect appendBezierPathWithOvalInRect:
|
2006-07-15 16:33:39 +00:00
|
|
|
NSMakeRect(0, 0, ovalDiamater, buttonSize.height)];
|
2006-07-15 16:22:42 +00:00
|
|
|
[rect appendBezierPathWithOvalInRect:
|
2006-07-15 16:33:39 +00:00
|
|
|
NSMakeRect(buttonSize.width - ovalDiamater, 0, ovalDiamater, buttonSize.height)];
|
2006-07-15 16:22:42 +00:00
|
|
|
[rect appendBezierPathWithRect:
|
2006-07-15 16:33:39 +00:00
|
|
|
NSMakeRect(ovalDiamater * 0.5, 0, buttonSize.width - ovalDiamater, buttonSize.height)];
|
2006-07-15 16:22:42 +00:00
|
|
|
|
|
|
|
//create highlighted button
|
|
|
|
[fButtonIn lockFocus];
|
2006-07-15 18:22:10 +00:00
|
|
|
[[NSColor colorWithCalibratedRed: 0.4941 green: 0.5647 blue: 0.6706 alpha: 1.0] set];
|
2006-07-15 16:22:42 +00:00
|
|
|
[rect fill];
|
|
|
|
[fButtonIn unlockFocus];
|
|
|
|
|
|
|
|
//create pushed button
|
|
|
|
[fButtonDown lockFocus];
|
2006-07-15 18:22:10 +00:00
|
|
|
[[NSColor colorWithCalibratedWhite: 0.0 alpha: 0.6] set];
|
2006-07-15 16:22:42 +00:00
|
|
|
[rect fill];
|
|
|
|
[fButtonDown unlockFocus];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) awakeFromNib
|
|
|
|
{
|
|
|
|
[self addTrackingRect: [self bounds] owner: self userData: nil assumeInside: NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[fButtonNormal release];
|
|
|
|
[fButtonIn release];
|
|
|
|
[fButtonDown release];
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
//call only once to avoid overlap
|
|
|
|
- (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-15 18:22:10 +00:00
|
|
|
[NSFont messageFontOfSize: 11.0], NSFontAttributeName,
|
|
|
|
stringShadow, NSShadowAttributeName, nil];
|
2006-07-15 16:22:42 +00:00
|
|
|
NSDictionary * highlightedAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSColor whiteColor], NSForegroundColorAttributeName,
|
2006-07-15 18:22:10 +00:00
|
|
|
[NSFont messageFontOfSize: 11.0], NSFontAttributeName,
|
|
|
|
stringShadow, NSShadowAttributeName, nil];
|
|
|
|
|
|
|
|
[stringShadow release];
|
2006-07-15 16:22:42 +00:00
|
|
|
|
|
|
|
NSSize textSize = [text sizeWithAttributes: normalAttributes];
|
|
|
|
NSRect textRect = NSMakeRect(([self frame].size.width - textSize.width) * 0.5,
|
|
|
|
([self frame].size.height - textSize.height) * 0.5, textSize.width, textSize.height);
|
|
|
|
|
|
|
|
//create normal button
|
|
|
|
[fButtonNormal lockFocus];
|
|
|
|
[text drawInRect: textRect withAttributes: normalAttributes];
|
|
|
|
[fButtonNormal unlockFocus];
|
|
|
|
|
|
|
|
//create highlighted button
|
|
|
|
[fButtonIn lockFocus];
|
|
|
|
[text drawInRect: textRect withAttributes: highlightedAttributes];
|
|
|
|
[fButtonIn unlockFocus];
|
|
|
|
|
|
|
|
//create pushed button
|
|
|
|
[fButtonDown lockFocus];
|
|
|
|
[text drawInRect: textRect withAttributes: highlightedAttributes];
|
|
|
|
[fButtonDown unlockFocus];
|
|
|
|
|
|
|
|
[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)
|
|
|
|
[self setImage: fButtonIn];
|
|
|
|
|
|
|
|
[super mouseEntered: event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseExited: (NSEvent *) event
|
|
|
|
{
|
|
|
|
if (!fEnabled)
|
|
|
|
[self setImage: fButtonNormal];
|
|
|
|
|
|
|
|
[super mouseExited: event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseDown: (NSEvent *) event
|
|
|
|
{
|
2006-07-15 16:51:38 +00:00
|
|
|
[self setImage: fButtonDown];
|
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 16:51:38 +00:00
|
|
|
[self setImage: fButtonIn];
|
2006-07-15 16:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setEnabled: (BOOL) enable
|
|
|
|
{
|
|
|
|
fEnabled = enable;
|
|
|
|
[self setImage: fEnabled ? fButtonIn : fButtonNormal];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|