
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>

#import "Controller.h"
#import "TextWindow.h"
#import "ColorAsRGBADictionary.h"
#import "DictionaryAdditions.h"
#import "StringAdditions.h"
#import "HeadlineView.h"

@implementation TextWindow

static int _count = 0;
#define MAX_OFFSET     10
#define WINDOW_SHIFT   40
#define WINDOW_BASE_X 100
#define WINDOW_BASE_Y 700
#define MARGIN         10

- initWithTickerWindow:(TickerWindow *)twin
	       article:(GenericArticle *)art  
		number:(int)val
	    attributes:(NSDictionary *)attr
{
    ticker = twin;
    RETAIN(ticker);

    articleNumber = val;

    NSRect frame;

    frame.origin.x = WINDOW_BASE_X 
	+ (_count % MAX_OFFSET)*WINDOW_SHIFT;
    frame.origin.y = WINDOW_BASE_Y -TEXTWINDOW_HEIGHT
	- ((_count / MAX_OFFSET) % MAX_OFFSET)*WINDOW_SHIFT;
    frame.size.width = TEXTWINDOW_WIDTH;
    frame.size.height = TEXTWINDOW_HEIGHT;
    _count++;

    [super 
	initWithContentRect:frame
	styleMask:(NSResizableWindowMask | 
		   NSClosableWindowMask | NSTitledWindowMask)
	backing:NSBackingStoreRetained 
	defer:NO];
    [self setMinSize:NSMakeSize(TEXTWINDOW_WIDTH, TEXTWINDOW_HEIGHT)];
    
    frame.origin.x = MARGIN;
    frame.origin.y = MARGIN;

    frame.size.width  -= 2*MARGIN;
    frame.size.height -= 2*MARGIN;

    NSScrollView *scrollView =
	[[NSScrollView alloc] initWithFrame:frame];
    AUTORELEASE(scrollView);

    [scrollView setHasHorizontalScroller:NO];
    [scrollView setHasVerticalScroller:YES]; 
    [scrollView setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
    [[scrollView contentView] 
	setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
    [[scrollView contentView] setAutoresizesSubviews:YES];

    // Build up the text network
    NSRect textRect = [[scrollView contentView] frame];
    NSTextView *textView = [[NSTextView alloc] initWithFrame:textRect];
    AUTORELEASE(textView);

    [textView setEditable:NO];

    [textView setHorizontallyResizable:NO];
    [textView setVerticallyResizable:YES];

    [textView setMinSize:NSMakeSize(0, 0)];
    [textView setMaxSize:NSMakeSize(1E7, 1E7)];
    [textView setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];

    [[textView textContainer] 
	setContainerSize:NSMakeSize(frame.size.width, 1E7)];
    [[textView textContainer] setWidthTracksTextView:YES];

    [textView setMenu:[HeadlineView defaultMenu]];

    [textView setRichText:YES];
    [textView setUsesFontPanel:NO];

    NSColor 
	*fg = [NSColor colorFromRGBADictionary:[attr objectForKey:ATTR_FG]],
	*bg = [NSColor colorFromRGBADictionary:[attr objectForKey:ATTR_BG]];

    [textView setBackgroundColor:bg];
    [textView setTextColor:fg];
    NSFont 
	*font =
	[NSFont 
	    fontWithName:[attr objectForKey:ATTR_FONTNAME]
	    size:[attr floatForKey:ATTR_FONTSIZE]];
    [textView
	replaceCharactersInRange:NSMakeRange(0, 0)
	withAttributedString:
	    [art contentWithTitleFont:font
		 foreground:fg background:bg]];

    [scrollView setDocumentView:textView];
    [[self contentView] addSubview:scrollView];

    [self setTitle:[[art title] firstComponent]];
    
    [[NSNotificationCenter defaultCenter]
	addObserver:self
	selector:@selector(tickerDidUpdate:)
	name:TickerDidUpdateNotification object:twin];
    [[NSNotificationCenter defaultCenter]
	addObserver:self
	selector:@selector(tickerWindowWillClose:)
	name:NSWindowWillCloseNotification object:twin];

    [self setHidesOnDeactivate:YES];
    [self setReleasedWhenClosed:YES];

    return self;
}

- (void)tickerDidUpdate:(NSNotification *)aNotification
{
    [self close];
}

- (void)tickerWindowWillClose:(NSNotification *)aNotification
{
    [self close];
}

- articleText:(id)sender
{
    NSBeep();
    return self;
}

- articleBrowse:(id)sender
{
    [ticker setExternalChoice:articleNumber];
    [ticker articleBrowse:self];
    return self;
}

- articleLink:(id)sender
{
    [ticker setExternalChoice:articleNumber];
    [ticker articleLink:self];
    return self;
}

- articleShowAll:(id)sender
{
    [ticker setExternalChoice:articleNumber];
    [ticker articleShowAll:self];
    return self;
}

- articleUpdate:(id)sender
{
    NSBeep();
    return self;
}


- (void)dealloc
{
    NSLog(@"dealloc %@", self);
    RELEASE(ticker);

    [super dealloc];
}

@end
