1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 09:37:56 +00:00
transmission/macosx/BarButton.m

168 lines
5.7 KiB
Mathematica
Raw Normal View History

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;
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:
NSMakeRect(0, 0, ovalDiamater, buttonSize.height)];
2006-07-15 16:22:42 +00:00
[rect appendBezierPathWithOvalInRect:
NSMakeRect(buttonSize.width - ovalDiamater, 0, ovalDiamater, buttonSize.height)];
2006-07-15 16:22:42 +00:00
[rect appendBezierPathWithRect:
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];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(resetBounds:)
name: NSViewBoundsDidChangeNotification object: nil];
2006-07-15 16:22:42 +00:00
}
return self;
}
- (void) awakeFromNib
{
2006-07-15 18:49:33 +00:00
fTrackingTag = [self addTrackingRect: [self bounds] owner: self userData: nil assumeInside: NO];
2006-07-15 16:22:42 +00:00
}
- (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];
[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
{
[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];
[self setImage: fButtonIn];
2006-07-15 16:22:42 +00:00
}
- (void) setEnabled: (BOOL) enable
{
fEnabled = enable;
[self setImage: fEnabled ? fButtonIn : fButtonNormal];
}
- (void) resetBounds: (NSNotification *) notification
{
2006-07-15 18:49:33 +00:00
[self removeTrackingRect: fTrackingTag];
[self addTrackingRect: [self bounds] owner: self userData: nil assumeInside: NO];
}
2006-07-15 16:22:42 +00:00
@end