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"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
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-29 02:00:23 +00:00
|
|
|
fCount = -1;
|
|
|
|
[self createButtonsWithCount: 0];
|
2006-07-23 22:23:59 +00:00
|
|
|
|
2006-08-05 13:22:30 +00:00
|
|
|
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
|
|
|
|
|
|
|
|
[nc addObserver: self selector: @selector(setForActive:)
|
2006-09-05 02:12:07 +00:00
|
|
|
name: NSWindowDidBecomeKeyNotification object: [self window]];
|
2006-08-05 13:22:30 +00:00
|
|
|
|
|
|
|
[nc addObserver: self selector: @selector(setForInactive:)
|
2006-09-05 02:12:07 +00:00
|
|
|
name: NSWindowDidResignKeyNotification object: [self window]];
|
2006-08-05 13:22:30 +00:00
|
|
|
|
|
|
|
[nc addObserver: self selector: @selector(resetBounds:)
|
2006-08-07 00:11:07 +00:00
|
|
|
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];
|
|
|
|
|
|
|
|
[fButtonNormal release];
|
|
|
|
[fButtonOver release];
|
|
|
|
[fButtonPressed release];
|
|
|
|
[fButtonSelected release];
|
|
|
|
[fButtonSelectedDim release];
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2007-05-29 02:57:29 +00:00
|
|
|
#warning rename
|
2007-05-29 02:00:23 +00:00
|
|
|
- (void) createButtonsWithCount: (int) count
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-05-29 02:00:23 +00:00
|
|
|
if (fCount == count)
|
|
|
|
return;
|
|
|
|
fCount = count;
|
|
|
|
|
2007-05-29 02:57:29 +00:00
|
|
|
if (fButtonNormal)
|
|
|
|
{
|
|
|
|
[fButtonNormal release];
|
|
|
|
[fButtonOver release];
|
|
|
|
[fButtonPressed release];
|
|
|
|
[fButtonSelected release];
|
|
|
|
[fButtonSelectedDim release];
|
|
|
|
}
|
|
|
|
|
2007-05-29 02:00:23 +00:00
|
|
|
//create attributes
|
|
|
|
NSFont * boldFont = [[NSFontManager sharedFontManager] convertFont:
|
|
|
|
[NSFont fontWithName: @"Lucida Grande" size: 12.0] toHaveTrait: NSBoldFontMask];
|
|
|
|
|
|
|
|
NSSize shadowOffset = NSMakeSize(0.0, -1.0);
|
|
|
|
|
2007-05-29 14:06:25 +00:00
|
|
|
NSShadow * shadowNormal = [NSShadow alloc];
|
|
|
|
[shadowNormal setShadowOffset: shadowOffset];
|
|
|
|
[shadowNormal setShadowBlurRadius: 1.0];
|
|
|
|
[shadowNormal setShadowColor: [NSColor colorWithDeviceWhite: 1.0 alpha: 0.4]];
|
2007-05-29 02:00:23 +00:00
|
|
|
|
|
|
|
NSShadow * shadowDim = [NSShadow alloc];
|
|
|
|
[shadowDim setShadowOffset: shadowOffset];
|
|
|
|
[shadowDim setShadowBlurRadius: 1.0];
|
|
|
|
[shadowDim setShadowColor: [NSColor colorWithDeviceWhite: 1.0 alpha: 0.2]];
|
|
|
|
|
2007-05-29 14:06:25 +00:00
|
|
|
NSShadow * shadowHighlighted = [NSShadow alloc];
|
|
|
|
[shadowHighlighted setShadowOffset: shadowOffset];
|
|
|
|
[shadowHighlighted setShadowBlurRadius: 1.0];
|
|
|
|
[shadowHighlighted setShadowColor: [NSColor colorWithDeviceWhite: 0.0 alpha: 0.4]];
|
|
|
|
|
2007-05-29 02:00:23 +00:00
|
|
|
NSDictionary * normalAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSColor colorWithCalibratedRed: 0.259 green: 0.259 blue: 0.259 alpha: 1.0],
|
|
|
|
NSForegroundColorAttributeName,
|
|
|
|
boldFont, NSFontAttributeName,
|
2007-05-29 14:06:25 +00:00
|
|
|
shadowNormal, NSShadowAttributeName, nil],
|
2007-05-29 02:00:23 +00:00
|
|
|
* normalDimAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSColor disabledControlTextColor], NSForegroundColorAttributeName,
|
|
|
|
boldFont, NSFontAttributeName,
|
|
|
|
shadowDim, NSShadowAttributeName, nil],
|
|
|
|
* highlightedAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSColor whiteColor], NSForegroundColorAttributeName,
|
|
|
|
boldFont, NSFontAttributeName,
|
2007-05-29 14:06:25 +00:00
|
|
|
shadowHighlighted, NSShadowAttributeName, nil],
|
2007-05-29 02:00:23 +00:00
|
|
|
* highlightedDimAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSColor colorWithCalibratedRed: 0.9 green: 0.9 blue: 0.9 alpha: 1.0], NSForegroundColorAttributeName,
|
|
|
|
boldFont, NSFontAttributeName,
|
2007-05-29 14:06:25 +00:00
|
|
|
shadowHighlighted, NSShadowAttributeName, nil];
|
2007-05-29 02:00:23 +00:00
|
|
|
|
2007-05-29 14:06:25 +00:00
|
|
|
[shadowNormal release];
|
2007-05-29 02:00:23 +00:00
|
|
|
[shadowDim release];
|
2007-05-29 14:06:25 +00:00
|
|
|
[shadowHighlighted release];
|
2007-05-29 02:00:23 +00:00
|
|
|
|
|
|
|
//create button text
|
|
|
|
NSString * text = [self title];
|
|
|
|
if (fCount > 0)
|
|
|
|
text = [text stringByAppendingString: [NSString stringWithFormat: @" (%d)", fCount]];
|
|
|
|
|
|
|
|
//get images
|
|
|
|
NSImage * leftOver = [NSImage imageNamed: @"FilterButtonOverLeft.png"],
|
|
|
|
* rightOver = [NSImage imageNamed: @"FilterButtonOverRight.png"],
|
|
|
|
* mainOver = [NSImage imageNamed: @"FilterButtonOverMain.png"];
|
|
|
|
|
|
|
|
NSImage * leftPressed = [NSImage imageNamed: @"FilterButtonPressedLeft.png"],
|
|
|
|
* rightPressed = [NSImage imageNamed: @"FilterButtonPressedRight.png"],
|
|
|
|
* mainPressed = [NSImage imageNamed: @"FilterButtonPressedMain.png"];
|
|
|
|
|
|
|
|
NSImage * leftSelected = [NSImage imageNamed: @"FilterButtonSelectedLeft.png"],
|
|
|
|
* rightSelected = [NSImage imageNamed: @"FilterButtonSelectedRight.png"],
|
|
|
|
* mainSelected = [NSImage imageNamed: @"FilterButtonSelectedMain.png"];
|
|
|
|
|
|
|
|
//get button sizes and placement
|
2007-05-29 14:06:25 +00:00
|
|
|
NSSize textSize = [text sizeWithAttributes: normalAttributes];
|
|
|
|
textSize.width = ceilf(textSize.width);
|
2007-05-29 02:00:23 +00:00
|
|
|
|
2007-05-29 14:06:25 +00:00
|
|
|
float overlap = 7.0;
|
2007-05-29 02:00:23 +00:00
|
|
|
NSSize endSize = [leftOver size],
|
|
|
|
mainSize = NSMakeSize(textSize.width - (overlap * 2.0), endSize.height),
|
|
|
|
buttonSize = NSMakeSize(mainSize.width + 2.0 * endSize.width, endSize.height);
|
|
|
|
NSRect textRect = NSMakeRect(endSize.width - overlap, (buttonSize.height - textSize.height) * 0.5 + 1.5,
|
|
|
|
textSize.width, textSize.height);
|
|
|
|
|
|
|
|
NSPoint leftPoint = NSZeroPoint,
|
|
|
|
mainPoint = NSMakePoint(endSize.width, 0),
|
|
|
|
rightPoint = NSMakePoint(mainPoint.x + mainSize.width, 0);
|
|
|
|
|
2006-07-23 22:58:04 +00:00
|
|
|
fButtonNormal = [[NSImage alloc] initWithSize: buttonSize];
|
|
|
|
fButtonNormalDim = [[NSImage alloc] initWithSize: buttonSize];
|
|
|
|
fButtonOver = [[NSImage alloc] initWithSize: buttonSize];
|
|
|
|
fButtonPressed = [[NSImage alloc] initWithSize: buttonSize];
|
|
|
|
fButtonSelected = [[NSImage alloc] initWithSize: buttonSize];
|
|
|
|
fButtonSelectedDim = [[NSImage alloc] initWithSize: buttonSize];
|
|
|
|
|
|
|
|
//rolled over button
|
|
|
|
[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];
|
|
|
|
|
|
|
|
//pressed button
|
|
|
|
[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];
|
|
|
|
|
|
|
|
//selected button
|
|
|
|
[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];
|
|
|
|
|
|
|
|
//selected and dimmed button
|
|
|
|
fButtonSelectedDim = [fButtonSelected copy];
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
//normal button
|
|
|
|
[fButtonNormal lockFocus];
|
2006-07-19 11:30:05 +00:00
|
|
|
[text drawInRect: textRect withAttributes: normalAttributes];
|
2006-07-16 19:39:23 +00:00
|
|
|
[fButtonNormal unlockFocus];
|
|
|
|
|
2006-07-19 00:06:32 +00:00
|
|
|
//normal and dim button
|
|
|
|
[fButtonNormalDim lockFocus];
|
2006-07-19 11:30:05 +00:00
|
|
|
[text drawInRect: textRect withAttributes: normalDimAttributes];
|
2006-07-19 00:06:32 +00:00
|
|
|
[fButtonNormalDim unlockFocus];
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
//rolled over button
|
|
|
|
[fButtonOver lockFocus];
|
2006-07-19 11:30:05 +00:00
|
|
|
[text drawInRect: textRect withAttributes: highlightedAttributes];
|
2006-07-16 19:39:23 +00:00
|
|
|
[fButtonOver unlockFocus];
|
|
|
|
|
|
|
|
//pressed button
|
|
|
|
[fButtonPressed lockFocus];
|
2006-07-19 11:30:05 +00:00
|
|
|
[text drawInRect: textRect withAttributes: highlightedAttributes];
|
2006-07-16 19:39:23 +00:00
|
|
|
[fButtonPressed unlockFocus];
|
|
|
|
|
|
|
|
//selected button
|
|
|
|
[fButtonSelected lockFocus];
|
2006-07-19 11:30:05 +00:00
|
|
|
[text drawInRect: textRect withAttributes: highlightedAttributes];
|
2006-07-16 19:39:23 +00:00
|
|
|
[fButtonSelected unlockFocus];
|
|
|
|
|
2006-07-19 00:06:32 +00:00
|
|
|
//selected and dim button
|
|
|
|
[fButtonSelectedDim lockFocus];
|
2006-07-19 11:30:05 +00:00
|
|
|
[text drawInRect: textRect withAttributes: highlightedDimAttributes];
|
2006-07-19 00:06:32 +00:00
|
|
|
[fButtonSelectedDim unlockFocus];
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
[normalAttributes release];
|
2006-07-19 00:06:32 +00:00
|
|
|
[normalDimAttributes release];
|
2006-07-16 19:39:23 +00:00
|
|
|
[highlightedAttributes release];
|
2006-07-19 00:06:32 +00:00
|
|
|
[highlightedDimAttributes release];
|
2007-05-29 02:00:23 +00:00
|
|
|
|
|
|
|
//resize button
|
|
|
|
NSPoint point = [self frame].origin;
|
|
|
|
[self setFrame: NSMakeRect(point.x, point.y, buttonSize.width, buttonSize.height)];
|
2007-05-29 02:57:29 +00:00
|
|
|
|
|
|
|
//set image
|
|
|
|
[self setAlternateImage: fButtonPressed];
|
|
|
|
|
|
|
|
if ([[self window] isKeyWindow])
|
|
|
|
[self setImage: fEnabled ? fButtonSelected : fButtonNormal];
|
|
|
|
else
|
|
|
|
[self setImage: fEnabled ? fButtonSelectedDim : fButtonNormalDim];
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseEntered: (NSEvent *) event
|
|
|
|
{
|
|
|
|
if (!fEnabled)
|
|
|
|
[self setImage: fButtonOver];
|
2006-08-07 00:11:07 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
[super mouseEntered: event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseExited: (NSEvent *) event
|
|
|
|
{
|
|
|
|
if (!fEnabled)
|
|
|
|
[self setImage: fButtonNormal];
|
|
|
|
|
|
|
|
[super mouseExited: event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setEnabled: (BOOL) enable
|
|
|
|
{
|
|
|
|
fEnabled = enable;
|
|
|
|
[self setImage: fEnabled ? fButtonSelected : fButtonNormal];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) resetBounds: (NSNotification *) notification
|
|
|
|
{
|
|
|
|
if (fTrackingTag)
|
|
|
|
[self removeTrackingRect: fTrackingTag];
|
|
|
|
fTrackingTag = [self addTrackingRect: [self bounds] owner: self userData: nil assumeInside: NO];
|
|
|
|
}
|
|
|
|
|
2006-08-05 13:22:30 +00:00
|
|
|
- (void) setForActive: (NSNotification *) notification
|
2006-07-19 00:06:32 +00:00
|
|
|
{
|
2006-09-05 02:23:54 +00:00
|
|
|
[self setImage: fEnabled ? fButtonSelected : fButtonNormal];
|
2006-07-19 00:06:32 +00:00
|
|
|
[self resetBounds: nil];
|
|
|
|
}
|
|
|
|
|
2006-08-05 13:22:30 +00:00
|
|
|
- (void) setForInactive: (NSNotification *) notification
|
2006-07-19 00:06:32 +00:00
|
|
|
{
|
2006-09-05 02:21:17 +00:00
|
|
|
[self setImage: fEnabled ? fButtonSelectedDim : fButtonNormalDim];
|
2006-07-19 00:06:32 +00:00
|
|
|
|
|
|
|
if (fTrackingTag)
|
2006-09-05 02:12:07 +00:00
|
|
|
{
|
2006-07-19 00:06:32 +00:00
|
|
|
[self removeTrackingRect: fTrackingTag];
|
2006-09-05 02:12:07 +00:00
|
|
|
fTrackingTag = 0;
|
|
|
|
}
|
2006-07-19 00:06:32 +00:00
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
@end
|