Source

headstak / StakItemView.m

Full commit
//
//  StakItem.m
//  headstak
//
//  Created by Veghead on 3/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "StakItemView.h"
#import "StakView.h"
#import "StakItem.h"

@implementation StakItemView

@synthesize content = _content,
        stakView = _stakView,
        background = _background;


- (id)initWithStakItem:(StakItem *)item inStakView:(StakView *)myStakView
{
    NSRect rect = [myStakView frame];
    rect.size.width -= 10;
    rect.origin.x += 5;
    rect.size.height = 40;
    self = [super initWithFrame:rect];
    if (self) {

        _origin_y = rect.origin.y;
        _stakView = myStakView;
        
        // Make me look nice
        [self setBoxType:NSBoxCustom];
        [self setBorderType:NSLineBorder];
        [self setCornerRadius:10.0];
        
        // FIXME: use the colours from the database
        switch ([item itemType]) {
            default:
            case 1:
               [self setFillColor:[NSColor colorWithSRGBRed:0.0 green:1.0 blue:0.6 alpha:1.0]];
                break;
            case 2:
                [self setFillColor:[NSColor colorWithSRGBRed:1.0 green:0.8 blue:0.0 alpha:1.0]];
                break;
            case 3:
                [self setFillColor:[NSColor colorWithSRGBRed:0.0 green:0.6 blue:1.0 alpha:1.0]];
                break;
                
        }        
        rect.size.width -= 10;
        rect.origin.y = -10;
        
        // Create the Text View for the description
        _content = [[NSTextView alloc] initWithFrame:rect];
        [_content setBackgroundColor:[NSColor colorWithSRGBRed:0.0 green:0.0 blue:0.0 alpha:0.0]];
        [_content insertText:[NSString stringWithFormat:item.description]];
        [_content setEditable:YES];
        
        // Observe changes to the container size so that we can adjust the view size
        [_content addObserver:self forKeyPath:@"textContainer.containerSize" options:NSKeyValueObservingOptionNew context:nil];

        [self addSubview:_content];
        _background = [NSImage imageNamed:@"taskbg"];
    }
    return self;
}

- (void)dealloc
{
    [_content removeObserver:self forKeyPath:@"textContainer.containerSize" context:nil];
}

- (BOOL)isFlipped
{
    return NO;
}

#pragma mark KVO

- (void)observeValueForKeyPath:(NSString *)keyPath 
                      ofObject:(id)object 
                        change:(NSDictionary *)change 
                       context:(id)context 
{
    NSRect textViewRect = [object frame];
    NSSize mySize = [self frame].size;
    mySize.height = textViewRect.size.height + 10;
    textViewRect.origin.y = 0;
    [[self content] setFrameOrigin:textViewRect.origin];
    [self setFrameSize:mySize];
    [[self stakView] itemChanged:self];
}

#pragma mark Gloss

- (void)drawRect:(NSRect)rect {
    [super drawRect:rect];
    NSRect r = [self bounds];
    r.origin.y = r.size.height - [[self background] size].height;
    [[self background] drawAtPoint:r.origin fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}
@end