Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 161 lines (122 sloc) 5.178 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
// Copyright 2010 Todd Ditchendorf
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import "UMETabBarItemButtonCell.h"
#import "UMEImageCache.h"
#import <UMEKit/UMETabBarItem.h>

#define MIN_WIDTH 40
#define TITLE_OFFSET_X 2
#define TITLE_OFFSET_Y 1
#define IMAGE_OFFSET_Y 4

static NSShadow *sTitleShadow = nil;

@interface UMETabBarItemButtonCell ()
- (void)commonInit;
@end

@implementation UMETabBarItemButtonCell

+ (void)initialize {
    if ([UMETabBarItemButtonCell class] == self) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        
        sTitleShadow = [[NSShadow alloc] init];
        [sTitleShadow setShadowColor:[[NSColor blackColor] colorWithAlphaComponent:0.5]];
        [sTitleShadow setShadowOffset:NSMakeSize(0, 1)];
        [sTitleShadow setShadowBlurRadius:0];
        
        [pool release];
    }
}


- (id)initTextCell:(NSString *)s {
    if (self = [super initTextCell:s]) {
        [self commonInit];
    }
    return self;
}


- (id)initImageCell:(NSImage *)i {
    if (self = [super initImageCell:i]) {
        [self commonInit];
    }
    return self;
}


- (void)commonInit {
    [self setImagePosition:NSImageAbove];
}


- (void)dealloc {
    [super dealloc];
}


- (BOOL)isOpaque {
    return YES;
}


- (void)drawWithFrame:(NSRect)r inView:(NSView *)cv {
    [self drawInteriorWithFrame:r inView:cv];
}


- (void)drawInteriorWithFrame:(NSRect)r inView:(NSView *)cv {
    //UMETabBarItemButton *b = (UMETabBarItemButton *)cv;
    //UMETabBarItem *item = b.item;
    
    // if below the min width, just clear and return (dont draw borked background image)
    if (r.size.width < MIN_WIDTH) {
        return;
    }
    
    NSDrawThreePartImage(r, UMEIMG(@"tabbar_bg"), UMEIMG(@"tabbar_bg"), UMEIMG(@"tabbar_bg"), NO, NSCompositeSourceOver, 1, YES);
    
    // draw bg image
    if (NSOnState == [self state]) {
        NSImage *leftImage = UMEIMG(@"tabbar_button_bg_hi_01");
        NSImage *centerImage = UMEIMG(@"tabbar_button_bg_hi_02");
        NSImage *rightImage = UMEIMG(@"tabbar_button_bg_hi_03");
        
        [leftImage setFlipped:[cv isFlipped]];
        [centerImage setFlipped:[cv isFlipped]];
        [rightImage setFlipped:[cv isFlipped]];
        
        NSDrawThreePartImage(r, leftImage, centerImage, rightImage, NO, NSCompositeSourceOver, 1, NO);
    }
    
    // draw image
    if ([self imagePosition] != NSNoImage) {
        NSImage *img = nil;
        if (NSOnState == [self state]) {
            img = [self alternateImage];
        } else {
            img = [self image];
        }
        
        [img setFlipped:[cv isFlipped]];
        
        NSSize size = [img size];
        NSPoint p = NSMakePoint(r.origin.x + round((r.size.width - size.width) / 2.0) + TITLE_OFFSET_X, IMAGE_OFFSET_Y);
        
        [img drawAtPoint:p fromRect:NSMakeRect(0, 0, size.width, size.height) operation:NSCompositeSourceOver fraction:1];
    }
    
    // draw title
    if ([self imagePosition] != NSImageOnly) {
        NSString *title = [self title];
        if ([title length]) {
            NSColor *color = nil;
            if ([self isEnabled]) {
                if (NSOnState == [self state]) {
                    color = [NSColor whiteColor];
                } else {
                    color = [NSColor colorWithCalibratedWhite:1.0 alpha:0.6];
                }
            } else {
                color = [NSColor colorWithCalibratedWhite:1.0 alpha:0.4];
            }
            
            NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [self font], NSFontAttributeName,
                                        color, NSForegroundColorAttributeName,
                                        sTitleShadow, NSShadowAttributeName,
                                        nil];
            
            NSSize size = [title sizeWithAttributes:attributes];
            
// NSPoint p = NSMakePoint(r.origin.x + round((r.size.width - size.width) / 2.0) + TITLE_OFFSET_X,
// r.size.height - size.height - TITLE_OFFSET_Y);
//
// [title drawAtPoint:p withAttributes:attributes];

            NSRect d = NSMakeRect(r.origin.x + round((r.size.width - size.width) / 2.0) + TITLE_OFFSET_X,
                                  r.size.height - size.height - TITLE_OFFSET_Y,
                                  r.size.width - TITLE_OFFSET_X * 2, size.height);
            
            [title drawWithRect:d options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:attributes];
        }
    }
}

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