2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-04-03 02:22:25 +00:00
|
|
|
* Copyright (c) 2006-2007 Transmission authors and contributors
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-05-25 03:33:55 +00:00
|
|
|
#import "FilterBarButton.h"
|
2007-08-09 18:55:22 +00:00
|
|
|
#import "CTGradient.h"
|
|
|
|
#import "CTGradientAdditions.h"
|
2007-08-20 16:34:21 +00:00
|
|
|
#import "NSBezierPathAdditions.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-05-29 21:56:38 +00:00
|
|
|
@interface FilterBarButton (Private)
|
|
|
|
|
2007-08-09 18:55:22 +00:00
|
|
|
- (void) createPaths;
|
|
|
|
- (void) createFontAttributes;
|
2007-05-29 21:56:38 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2007-05-25 03:33:55 +00:00
|
|
|
@implementation FilterBarButton
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
- (id) initWithCoder: (NSCoder *) coder
|
|
|
|
{
|
2006-08-06 17:06:05 +00:00
|
|
|
if ((self = [super initWithCoder: coder]))
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
fEnabled = NO;
|
|
|
|
fTrackingTag = 0;
|
2007-05-31 23:17:46 +00:00
|
|
|
fCount = -1;
|
|
|
|
[self setCount: 0];
|
2007-08-09 18:55:22 +00:00
|
|
|
[self createPaths];
|
|
|
|
[self createFontAttributes];
|
2007-05-31 23:17:46 +00:00
|
|
|
|
2006-08-05 13:22:30 +00:00
|
|
|
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
|
2007-08-09 18:55:22 +00:00
|
|
|
[nc addObserver: self selector: @selector(setForActive:) name: NSWindowDidBecomeKeyNotification object: [self window]];
|
|
|
|
[nc addObserver: self selector: @selector(setForInactive:) name: NSWindowDidResignKeyNotification object: [self window]];
|
|
|
|
[nc addObserver: self selector: @selector(resetBounds:) name: NSViewFrameDidChangeNotification object: nil];
|
2006-08-06 17:06:05 +00:00
|
|
|
}
|
|
|
|
return self;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2006-07-30 20:53:25 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver: self];
|
|
|
|
|
2007-08-09 18:55:22 +00:00
|
|
|
[fPath release];
|
|
|
|
[fEdgePath release];
|
|
|
|
[fStepPath release];
|
|
|
|
[fNormalAttributes release];
|
|
|
|
[fNormalDimAttributes release];
|
|
|
|
[fHighlightedAttributes release];
|
|
|
|
[fHighlightedDimAttributes release];
|
2006-07-30 20:53:25 +00:00
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2007-08-14 05:21:02 +00:00
|
|
|
- (void) sizeToFit
|
2007-08-09 18:55:22 +00:00
|
|
|
{
|
|
|
|
NSSize size = [[self title] sizeWithAttributes: fNormalAttributes];
|
|
|
|
size.width = floorf(size.width + 18.5);
|
|
|
|
size.height += 1;
|
|
|
|
[self setFrameSize: size];
|
|
|
|
[self setBoundsSize: size];
|
|
|
|
[self createPaths];
|
|
|
|
}
|
|
|
|
|
2007-08-14 05:21:02 +00:00
|
|
|
- (BOOL) isFlipped
|
2007-08-09 18:55:22 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2007-08-14 05:21:02 +00:00
|
|
|
- (void) drawRect: (NSRect) rect
|
2007-08-09 18:55:22 +00:00
|
|
|
{
|
|
|
|
// draw background
|
|
|
|
if ([[self cell] isHighlighted])
|
|
|
|
{
|
|
|
|
[[CTGradient gradientWithBeginningColor: [NSColor colorWithCalibratedRed: 134.0/255 green: 151.0/255
|
|
|
|
blue: 176.0/255 alpha: 1.0]
|
|
|
|
endingColor: [NSColor colorWithCalibratedRed: 104.0/255 green: 125.0/255
|
|
|
|
blue: 157.0/255 alpha: 1.0]]
|
|
|
|
fillBezierPath: fPath angle: -90.0];
|
|
|
|
[[CTGradient gradientWithBeginningColor: [NSColor colorWithCalibratedWhite: 0.0 alpha: 0.25]
|
|
|
|
// middleColor: [NSColor colorWithCalibratedWhite: 0.5 alpha: 0.00]
|
|
|
|
middleColor1: [NSColor colorWithCalibratedWhite: 0.0 alpha: 0.25]
|
|
|
|
middleColor2: [NSColor colorWithCalibratedWhite: 1.0 alpha: 0.50]
|
|
|
|
endingColor: [NSColor colorWithCalibratedWhite: 1.0 alpha: 0.50]]
|
|
|
|
fillBezierPath: fStepPath angle: -90.0];
|
|
|
|
}
|
|
|
|
else switch (fState)
|
|
|
|
{
|
|
|
|
case 1: // active
|
|
|
|
[[CTGradient gradientWithBeginningColor: [NSColor colorWithCalibratedRed: 151.0/255 green: 166.0/255
|
|
|
|
blue: 188.0/255 alpha: 1.0]
|
|
|
|
endingColor: [NSColor colorWithCalibratedRed: 126.0/255 green: 144.0/255
|
|
|
|
blue: 171.0/255 alpha: 1.0]]
|
|
|
|
fillBezierPath: fPath angle: -90.0];
|
|
|
|
[[CTGradient gradientWithBeginningColor: [NSColor colorWithCalibratedWhite: 0.0 alpha: 0.25]
|
|
|
|
// middleColor: [NSColor colorWithCalibratedWhite: 0.5 alpha: 0.00]
|
|
|
|
middleColor1: [NSColor colorWithCalibratedWhite: 0.0 alpha: 0.25]
|
|
|
|
middleColor2: [NSColor colorWithCalibratedWhite: 1.0 alpha: 0.50]
|
|
|
|
endingColor: [NSColor colorWithCalibratedWhite: 1.0 alpha: 0.50]]
|
|
|
|
fillBezierPath: fStepPath angle: -90.0];
|
|
|
|
break;
|
|
|
|
case 2: // hovering
|
|
|
|
[[CTGradient gradientWithBeginningColor: [NSColor colorWithCalibratedRed: 164.0/255 green: 177.0/255
|
|
|
|
blue: 196.0/255 alpha: 1.0]
|
|
|
|
endingColor: [NSColor colorWithCalibratedRed: 141.0/255 green: 158.0/255
|
|
|
|
blue: 182.0/255 alpha: 1.0]]
|
|
|
|
fillBezierPath: fPath angle: -90.0];
|
|
|
|
[[NSColor colorWithCalibratedWhite: 0.0 alpha: 0.075] set];
|
|
|
|
[fEdgePath stroke];
|
|
|
|
break;
|
|
|
|
case 3: // clicked but cell is not highlighted
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw title
|
|
|
|
NSSize titleSize = [[self title] sizeWithAttributes: fNormalAttributes];
|
|
|
|
NSPoint titlePos = NSMakePoint(([self bounds].size.width - titleSize.width) * 0.5,
|
|
|
|
([self bounds].size.height - titleSize.height) * 0.5 + 1.5);
|
|
|
|
if (fEnabled)
|
|
|
|
{
|
|
|
|
if (fState && !(fState == 3 && ![[self cell] isHighlighted]))
|
|
|
|
[[self title] drawAtPoint: titlePos withAttributes: fHighlightedAttributes];
|
|
|
|
else
|
|
|
|
[[self title] drawAtPoint: titlePos withAttributes: fNormalAttributes];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (fState)
|
|
|
|
[[self title] drawAtPoint: titlePos withAttributes: fHighlightedDimAttributes];
|
|
|
|
else
|
|
|
|
[[self title] drawAtPoint: titlePos withAttributes: fNormalDimAttributes];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-30 06:10:05 +00:00
|
|
|
- (void) setCount: (int) count
|
|
|
|
{
|
2007-05-30 06:25:32 +00:00
|
|
|
if (count == fCount)
|
|
|
|
return;
|
|
|
|
fCount = count;
|
|
|
|
|
|
|
|
[self setToolTip: fCount == 1 ? NSLocalizedString(@"1 Transfer", "Filter Bar Button -> tool tip")
|
|
|
|
: [NSString stringWithFormat: NSLocalizedString(@"%d Transfers", "Filter Bar Button -> tool tip"), fCount]];
|
2007-05-30 06:10:05 +00:00
|
|
|
}
|
|
|
|
|
2007-08-09 18:55:22 +00:00
|
|
|
- (void) mouseDown: (NSEvent *) event
|
|
|
|
{
|
|
|
|
if ([self state] != 1)
|
|
|
|
[self setState: 3];
|
|
|
|
[super mouseDown: event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseUp: (NSEvent *) event
|
|
|
|
{
|
|
|
|
[super mouseUp: event];
|
|
|
|
if ([self state] != 1)
|
|
|
|
[self setState: 1];
|
|
|
|
}
|
|
|
|
|
2007-05-30 06:10:05 +00:00
|
|
|
- (void) mouseEntered: (NSEvent *) event
|
|
|
|
{
|
|
|
|
[super mouseEntered: event];
|
2007-08-09 18:55:22 +00:00
|
|
|
if ([self state] == 0)
|
|
|
|
[self setState: 2];
|
2007-05-30 06:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseExited: (NSEvent *) event
|
|
|
|
{
|
|
|
|
[super mouseExited: event];
|
2007-08-09 18:55:22 +00:00
|
|
|
if ([self state] >= 2)
|
|
|
|
[self setState: 0];
|
2007-05-30 06:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setEnabled: (BOOL) enable
|
|
|
|
{
|
|
|
|
fEnabled = enable;
|
2007-08-09 18:55:22 +00:00
|
|
|
[self setNeedsDisplay: YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) state
|
|
|
|
{
|
|
|
|
return fState;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setState: (int) state
|
|
|
|
{
|
|
|
|
fState = state;
|
|
|
|
[self setNeedsDisplay: YES];
|
2007-05-30 06:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) resetBounds: (NSNotification *) notification
|
|
|
|
{
|
|
|
|
if (fTrackingTag)
|
|
|
|
[self removeTrackingRect: fTrackingTag];
|
|
|
|
fTrackingTag = [self addTrackingRect: [self bounds] owner: self userData: nil assumeInside: NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setForActive: (NSNotification *) notification
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-09 18:55:22 +00:00
|
|
|
[self setEnabled: YES];
|
2007-05-30 06:10:05 +00:00
|
|
|
[self resetBounds: nil];
|
2007-08-09 18:55:22 +00:00
|
|
|
[self setNeedsDisplay: YES];
|
2007-05-30 06:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setForInactive: (NSNotification *) notification
|
|
|
|
{
|
|
|
|
if (fTrackingTag)
|
2007-05-29 02:57:29 +00:00
|
|
|
{
|
2007-05-30 06:10:05 +00:00
|
|
|
[self removeTrackingRect: fTrackingTag];
|
|
|
|
fTrackingTag = 0;
|
2007-05-29 02:57:29 +00:00
|
|
|
}
|
2007-08-09 18:55:22 +00:00
|
|
|
[self setEnabled: NO];
|
|
|
|
[self setNeedsDisplay: YES];
|
2007-05-30 06:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation FilterBarButton (Private)
|
|
|
|
|
2007-08-09 18:55:22 +00:00
|
|
|
- (void) createPaths
|
2007-05-30 06:10:05 +00:00
|
|
|
{
|
2007-08-09 18:55:22 +00:00
|
|
|
NSSize buttonSize = [self frame].size;
|
2007-05-29 02:00:23 +00:00
|
|
|
|
2007-08-09 18:55:22 +00:00
|
|
|
// the main button path
|
|
|
|
[fPath release];
|
|
|
|
fPath = [[NSBezierPath bezierPathWithRoundedRect: NSMakeRect(0.0, 1.0, buttonSize.width, buttonSize.height - 1.0)
|
|
|
|
radius: (buttonSize.height - 1.0) / 2.0] retain];
|
2007-05-29 02:00:23 +00:00
|
|
|
|
2007-08-09 18:55:22 +00:00
|
|
|
// the path used to draw the hover edging
|
|
|
|
[fEdgePath release];
|
|
|
|
fEdgePath = [[NSBezierPath bezierPathWithRoundedRect: NSMakeRect(0.5, 1.5, buttonSize.width - 1.0, buttonSize.height - 2.0)
|
|
|
|
radius: (buttonSize.height - 2.0) / 2.0] retain];
|
2007-05-29 02:00:23 +00:00
|
|
|
|
2007-08-09 18:55:22 +00:00
|
|
|
// the path used to draw the depressed shading/highlights of the active button
|
|
|
|
[fStepPath release];
|
|
|
|
fStepPath = [[NSBezierPath bezierPathWithRoundedRect: NSMakeRect(0.0, 0.0, buttonSize.width, buttonSize.height - 1.0)
|
|
|
|
radius: (buttonSize.height - 1.0) / 2.0] retain];
|
|
|
|
[fStepPath appendBezierPath: fPath];
|
|
|
|
[fStepPath setWindingRule: NSEvenOddWindingRule];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) createFontAttributes
|
|
|
|
{
|
|
|
|
NSFont * boldSystemFont = [NSFont boldSystemFontOfSize: 12.0];
|
|
|
|
NSSize shadowOffset = NSMakeSize(0.0, -1.0);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-09 18:55:22 +00:00
|
|
|
NSShadow * shadowNormal = [[[NSShadow alloc] init] autorelease];
|
|
|
|
[shadowNormal setShadowOffset: shadowOffset];
|
|
|
|
[shadowNormal setShadowBlurRadius: 1.0];
|
|
|
|
[shadowNormal setShadowColor: [NSColor colorWithCalibratedWhite: 1.0 alpha: 0.4]];
|
2006-07-19 00:06:32 +00:00
|
|
|
|
2007-08-09 18:55:22 +00:00
|
|
|
NSShadow * shadowNormalDim = [[[NSShadow alloc] init] autorelease];
|
|
|
|
[shadowNormalDim setShadowOffset: shadowOffset];
|
|
|
|
[shadowNormalDim setShadowBlurRadius: 1.0];
|
|
|
|
[shadowNormalDim setShadowColor: [NSColor colorWithCalibratedWhite: 1.0 alpha: 0.2]];
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-09 18:55:22 +00:00
|
|
|
NSShadow * shadowHighlighted = [[[NSShadow alloc] init] autorelease];
|
|
|
|
[shadowHighlighted setShadowOffset: shadowOffset];
|
|
|
|
[shadowHighlighted setShadowBlurRadius: 1.0];
|
|
|
|
[shadowHighlighted setShadowColor: [NSColor colorWithCalibratedWhite: 0.0 alpha: 0.4]];
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-09 18:55:22 +00:00
|
|
|
fNormalAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSColor colorWithCalibratedWhite: 0.259 alpha: 1.0], NSForegroundColorAttributeName,
|
|
|
|
boldSystemFont, NSFontAttributeName,
|
|
|
|
shadowNormal, NSShadowAttributeName, nil];
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-09 18:55:22 +00:00
|
|
|
fNormalDimAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSColor disabledControlTextColor], NSForegroundColorAttributeName,
|
|
|
|
boldSystemFont, NSFontAttributeName,
|
|
|
|
shadowNormalDim, NSShadowAttributeName, nil];
|
2006-07-19 00:06:32 +00:00
|
|
|
|
2007-08-09 18:55:22 +00:00
|
|
|
fHighlightedAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSColor whiteColor], NSForegroundColorAttributeName,
|
|
|
|
boldSystemFont, NSFontAttributeName,
|
|
|
|
shadowHighlighted, NSShadowAttributeName, nil];
|
2007-05-29 02:00:23 +00:00
|
|
|
|
2007-08-09 18:55:22 +00:00
|
|
|
fHighlightedDimAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSColor colorWithCalibratedWhite: 0.9 alpha: 1.0], NSForegroundColorAttributeName,
|
|
|
|
boldSystemFont, NSFontAttributeName,
|
|
|
|
shadowHighlighted, NSShadowAttributeName, nil];
|
2007-05-29 21:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|