Skip to content
This repository
branch: Without-Plugin
Fetching contributors…

Cannot retrieve contributors at this time

file 375 lines (260 sloc) 10.472 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
//
// BGHUDSegmentedCell.m
// BGHUDAppKit
//
// Created by BinaryGod on 7/1/08.
//
// Copyright (c) 2008, Tim Davis (BinaryMethod.com, binary.god@gmail.com)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
//
// Neither the name of the BinaryMethod.com nor the names of its contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#import "BGHUDSegmentedCell.h"

@interface NSSegmentedCell (private)
-(NSRect)rectForSegment:(NSInteger)segment inFrame:(NSRect)frame;
@end

@implementation BGHUDSegmentedCell

@synthesize themeKey;

-(id)init {

self = [super init];

if(self) {

self.themeKey = @"gradientTheme";
}

return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {

self = [super initWithCoder: aDecoder];

if(self) {

if([aDecoder containsValueForKey: @"themeKey"]) {

self.themeKey = [aDecoder decodeObjectForKey: @"themeKey"];
} else {

self.themeKey = @"gradientTheme";
}
}

return self;
}

-(void)encodeWithCoder: (NSCoder *)coder {

[super encodeWithCoder: coder];

[coder encodeObject: self.themeKey forKey: @"themeKey"];
}

-(id)copyWithZone:(NSZone *) zone {

BGHUDSegmentedCell *copy = [super copyWithZone: zone];

copy->themeKey = nil;
[copy setThemeKey: [self themeKey]];

return copy;
}

-(NSSegmentStyle)segmentStyle {

return [super segmentStyle];
}

-(void)setSegmentStyle:(NSSegmentStyle) style {

if(style == NSSegmentStyleSmallSquare || style == NSSegmentStyleRounded) {

[super setSegmentStyle: style];
}
}

- (void)drawWithFrame:(NSRect)frame inView:(NSView *)view {

NSBezierPath *border;

switch ([self segmentStyle]) {
default: // Silence uninitialized variable warnings
case NSSegmentStyleSmallSquare:

//Adjust frame for shadow
frame.origin.x += 1.5f;
frame.origin.y += .5f;
frame.size.width -= 3;
frame.size.height -= 3;

border = [[NSBezierPath alloc] init];
[border appendBezierPathWithRect: frame];

break;

case NSSegmentStyleRounded: //NSSegmentStyleTexturedRounded:

//Adjust frame for shadow
frame.origin.x += 1.5f;
frame.origin.y += .5f;
frame.size.width -= 3;
frame.size.height -= 3;

border = [[NSBezierPath alloc] init];

[border appendBezierPathWithRoundedRect: frame
xRadius: 4.0f yRadius: 4.0f];
break;
}

//Setup to Draw Border
[NSGraphicsContext saveGraphicsState];

//Set Shadow + Border Color
if([self isEnabled])
{
[[[[BGThemeManager keyedManager] themeForKey: self.themeKey] dropShadow] set];
}
[[[[BGThemeManager keyedManager] themeForKey: self.themeKey] strokeColor] set];

//Draw Border + Shadow
[border stroke];

[NSGraphicsContext restoreGraphicsState];

[border release];

int segCount = 0;

while (segCount <= [self segmentCount] -1) {

[self drawSegment: segCount inFrame: frame withView: view];
segCount++;
}
}

- (void)drawSegment:(NSInteger)segment inFrame:(NSRect)frame withView:(NSView *)view {

//Calculate rect for this segment
NSRect fillRect = [self rectForSegment: segment inFrame: frame];
NSBezierPath *fillPath;

switch ([self segmentStyle]) {
default: // Silence uninitialized variable warnings
case NSSegmentStyleSmallSquare:

if(segment == ([self segmentCount] -1)) {

if(![self hasText]) { fillRect.size.width -= 3; }
}

fillPath = [[NSBezierPath alloc] init];
[fillPath appendBezierPathWithRect: fillRect];

break;

case NSSegmentStyleRounded: //NSSegmentStyleTexturedRounded:

//If this is the first segment, draw rounded corners
if(segment == 0) {

fillPath = [[NSBezierPath alloc] init];

[fillPath appendBezierPathWithRoundedRect: fillRect xRadius: 3 yRadius: 3];

//Setup our joining rect
NSRect joinRect = fillRect;
joinRect.origin.x += 4;
joinRect.size.width -= 4;

[fillPath appendBezierPathWithRect: joinRect];

//If this is the last segment, draw rounded corners
} else if (segment == ([self segmentCount] -1)) {

fillPath = [[NSBezierPath alloc] init];

if(![self hasText]) { fillRect.size.width -= 3; }

[fillPath appendBezierPathWithRoundedRect: fillRect xRadius: 3 yRadius: 3];

//Setup our joining rect
NSRect joinRect = fillRect;
joinRect.size.width -= 4;

[fillPath appendBezierPathWithRect: joinRect];

} else {
NSAssert(segment != 0 && segment != ([self segmentCount] -1), @"should be a middle segment");
fillPath = [[NSBezierPath alloc] init];
[fillPath appendBezierPathWithRect: fillRect];
}

break;
}

//Fill our pathss

NSGradient *gradient = nil;

if([self isEnabled])
{
if([self selectedSegment] == segment) {

gradient = [[[BGThemeManager keyedManager] themeForKey: self.themeKey] highlightGradient];
} else {

gradient = [[[BGThemeManager keyedManager] themeForKey: self.themeKey] normalGradient];
}
}
else {
gradient = [[[BGThemeManager keyedManager] themeForKey: self.themeKey] disabledNormalGradient];
}

[gradient drawInBezierPath: fillPath angle: 90];
[fillPath release];

//Draw Segment dividers ONLY if they are
//inside segments
if(segment != ([self segmentCount] -1)) {

[[[[BGThemeManager keyedManager] themeForKey: self.themeKey] strokeColor] set];
[NSBezierPath strokeLineFromPoint: NSMakePoint(fillRect.origin.x + fillRect.size.width , fillRect.origin.y)
toPoint: NSMakePoint(fillRect.origin.x + fillRect.size.width, fillRect.origin.y + fillRect.size.height)];
}

[self drawInteriorForSegment: segment withFrame: fillRect];
}

-(void)drawInteriorForSegment:(NSInteger)segment withFrame:(NSRect)rect {

NSAttributedString *newTitle;

//if([self labelForSegment: segment] != nil) {

NSMutableDictionary *textAttributes = [[NSMutableDictionary alloc] initWithCapacity: 0];

[textAttributes setValue: [NSFont controlContentFontOfSize: [NSFont systemFontSizeForControlSize: [self controlSize]]] forKey: NSFontAttributeName];
if([self isEnabled])
{
[textAttributes setValue: [[[BGThemeManager keyedManager] themeForKey: self.themeKey] textColor] forKey: NSForegroundColorAttributeName];
}
else {
[textAttributes setValue:[[[BGThemeManager keyedManager] themeForKey: self.themeKey] disabledTextColor] forKey: NSForegroundColorAttributeName];
}


if([self labelForSegment: segment]) {

newTitle = [[NSAttributedString alloc] initWithString: [self labelForSegment: segment] attributes: textAttributes];
} else {

newTitle = [[NSAttributedString alloc] initWithString: @"" attributes: textAttributes];
}

[textAttributes release];
//}

NSRect textRect = rect;
NSRect imageRect = rect;

if([super imageForSegment: segment] != nil) {

NSImage *image = [self imageForSegment: segment];
[image setFlipped: YES];

if([self imageScalingForSegment: segment] == NSImageScaleProportionallyDown) {

CGFloat resizeRatio = (rect.size.height - 4) / [image size].height;

[image setScalesWhenResized: YES];
[image setSize: NSMakeSize([image size].width * resizeRatio, rect.size.height -4)];
}

if([self labelForSegment: segment] != nil && ![[self labelForSegment: segment] isEqualToString: @""]) {

imageRect.origin.y += (BGCenterY(rect) - ([image size].height /2));
imageRect.origin.x += (BGCenterX(rect) - (([image size].width + [newTitle size].width + 5) /2));
imageRect.size.height = [image size].height;
imageRect.size.width = [image size].width;

textRect.origin.y += (BGCenterY(rect) - ([newTitle size].height /2));
textRect.origin.x += imageRect.origin.x + [image size].width + 5;
textRect.size.height = [newTitle size].height;
textRect.size.width = [newTitle size].width;

[image drawInRect: imageRect fromRect: NSZeroRect operation: NSCompositeSourceAtop fraction: [[[BGThemeManager keyedManager] themeForKey: self.themeKey] alphaValue]];
[newTitle drawInRect: textRect];

} else {

//Draw Image Alone
imageRect.origin.y += (BGCenterY(rect) - ([image size].height /2));
imageRect.origin.x += (BGCenterX(rect) - ([image size].width /2));
imageRect.size.height = [image size].height;
imageRect.size.width = [image size].width;

[image drawInRect: imageRect fromRect: NSZeroRect operation: NSCompositeSourceAtop fraction: [[[BGThemeManager keyedManager] themeForKey: self.themeKey] alphaValue]];
}
} else {

textRect.origin.y += (BGCenterY(rect) - ([newTitle size].height /2));
textRect.origin.x += (BGCenterX(rect) - ([newTitle size].width /2));
textRect.size.height = [newTitle size].height;
textRect.size.width = [newTitle size].width;

if(textRect.origin.x < 3) { textRect.origin.x = 3; }

[newTitle drawInRect: textRect];
}

[newTitle release];
}

-(BOOL)hasText {

int i = 0;
BOOL flag = NO;

while(i <= [self segmentCount] -1) {

if([self labelForSegment: i] != nil && ![[self labelForSegment: i] isEqualToString: @""]) {

flag = YES;
}
i++;
}

return flag;
}

- (BOOL)_canAnimate {
    
    return NO;
}

- (BOOL)_isSliderStyle {
    return NO;
}

-(void)dealloc {

[themeKey release];
[super dealloc];
}

@end
Something went wrong with that request. Please try again.