//
//  Controller.m
//  Titlebar Insert
//
//  Created by Wevah on 2005.08.03.
//  Copyright 2005 Derailer. All rights reserved.
//

#import "Controller.h"


@implementation Controller

- (id)initWithWindow:(NSWindow *)window {
	if (self = [super initWithWindow:window]) {
		NSImage *icon = [NSImage imageNamed:@"secure"];
		NSSize imgSize = [icon size];
		secureIconView = [[NSImageView alloc] initWithFrame:NSMakeRect(0.0, 0.0, imgSize.width, imgSize.height)];
		[secureIconView setImage:icon];
		[secureIconView setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];
		[secureIconView setToolTip:@"Secure"];
	}
	
	return self;
}

- (void)awakeFromNib {
	NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"Toolbar"];
	[[self window] setToolbar:toolbar];
}

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
    return nil;
}

- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar {
	return [NSArray array];
}

- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
	return [NSArray array];
}

- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar {
	return [NSArray array];
}

- (IBAction)toggleSecureIcon:(id)sender {
	if ([secureIconView superview]) {
		[secureIconView removeFromSuperview];
	} else {
		NSWindow *window = [self window];
		NSButton *toolbarButton = [window standardWindowButton:NSWindowToolbarButton];
		
		NSButton *closeButton = [window standardWindowButton:NSWindowCloseButton];
		NSView *themeView = [closeButton superview];
		NSLog(@"%@", [themeView subviews]);
		NSSize viewSize = [themeView frame].size;
		NSRect closeFrame = [closeButton frame];
		NSRect secureFrame = [secureIconView frame];
			
		float rightDist = closeFrame.origin.x;
		
		if (toolbarButton)
			rightDist += rightDist + [toolbarButton frame].size.width;
				
		[secureIconView setFrameOrigin:NSMakePoint(viewSize.width - secureFrame.size.width - rightDist, [closeButton frame].origin.y + ceilf((closeFrame.size.height - secureFrame.size.height) / 2.0) + 1.0)];
		
		[themeView addSubview:secureIconView];
	}
}

@end
