/*
 * DiffView.m
 *
 * Copyright (c) 2002 Pierre-Yves Rivaille <pyrivail@ens-lyon.fr>
 *
 * This file is part of EasyDiff.app.
 *
 * EasyDiff.app is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * EasyDiff.app is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with EasyDiff.app; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#import <AppKit/AppKit.h>
#import "DiffView.h"
#include <math.h>

@implementation DiffView

- (id) initWithFrame: (NSRect) aRect
{
  [super initWithFrame: aRect];


  middleView = [[DiffMiddleView alloc]
		 initWithFrame:NSZeroRect];

  {
    leftView = [[NSScrollView alloc] 
		    initWithFrame: NSMakeRect(0, 0, 1000, 100)];
    [leftView setHasHorizontalScroller: YES];
    [leftView setHasVerticalScroller: YES];
  }
  {
    rightView = [[NSScrollView alloc] 
		    initWithFrame: NSMakeRect(0, 0, 1000, 100)];
    [rightView setHasHorizontalScroller: YES];
    [rightView setHasVerticalScroller: YES];
  }
 
  {
    leftTextView = [[DiffTextView alloc]
		 initWithFrame: NSMakeRect(0, 0, 1000, 100)];
    
    [leftTextView setVerticallyResizable: YES];
    [leftTextView setHorizontallyResizable: NO];
    [[leftTextView textContainer] setContainerSize: NSMakeSize(5000, 1e7)];
    
    [[leftTextView textContainer] setHeightTracksTextView: NO];
    [[leftTextView textContainer] setWidthTracksTextView: NO];
    [leftTextView setString: @" "];
    [leftTextView setFont: [NSFont userFixedPitchFontOfSize: 12]];
  }


  {
    rightTextView = [[DiffTextView alloc]
		  initWithFrame: NSMakeRect(0, 0, 1000, 100)];
    
    [rightTextView setVerticallyResizable: YES];
    [rightTextView setHorizontallyResizable: NO];
    [[rightTextView textContainer] setContainerSize: NSMakeSize(5000, 1e7)];
    
    [[rightTextView textContainer] setHeightTracksTextView: NO];
    [[rightTextView textContainer] setWidthTracksTextView: NO];
    [rightTextView setString: @" "];
    [rightTextView setFont: [NSFont userFixedPitchFontOfSize: 12]];
  }

  {
    [leftView setDocumentView: leftTextView];
    [[leftView contentView] setPostsBoundsChangedNotifications: YES];
    [[leftView contentView] setPostsFrameChangedNotifications: YES];
  }
  {
    [rightView setDocumentView: rightTextView];
    [[rightView contentView] setPostsBoundsChangedNotifications: YES];
    [[rightView contentView] setPostsFrameChangedNotifications: YES];
  }


  [[NSNotificationCenter defaultCenter]
    addObserver: self
    selector: @selector(leftViewBoundsDidChange:)
    name: NSViewBoundsDidChangeNotification
    object: [leftView contentView]];

  [[NSNotificationCenter defaultCenter]
    addObserver: leftTextView
    selector: @selector(superviewFrameChanged:)
    name: NSViewFrameDidChangeNotification
    object: [leftView contentView]];

  [[NSNotificationCenter defaultCenter]
    addObserver: self
    selector: @selector(rightViewBoundsDidChange:)
    name: NSViewBoundsDidChangeNotification
    object: [rightView contentView]];

  [[NSNotificationCenter defaultCenter]
    addObserver: rightTextView
    selector: @selector(superviewFrameChanged:)
    name: NSViewFrameDidChangeNotification
    object: [rightView contentView]];


  [middleView setLeftView: leftTextView];
  [middleView setRightView: rightTextView];
  [self addSubview: middleView];
  [self addSubview: rightView];
  [self addSubview: leftView];
  return self;
}

- (void) setLeftString: (NSString *) aString
{
  [leftTextView setString: aString];
  [leftTextView setSelectedRange: NSMakeRange(0,0)];
  [leftTextView computeAllLineRanges];
}

- (void) setRightString: (NSString *) aString
{
  [rightTextView setString: aString];
  
  [rightTextView computeAllLineRanges];
  
}


- (void) setLeftChanges: (NSArray *) anArray
{
  [middleView setLeftChanges: anArray];
  [leftTextView setChanges: anArray];
  [leftTextView computeLineRangesFromUpTo: 1000];
}

- (void) setRightChanges: (NSArray *) anArray
{
  [middleView setRightChanges: anArray];
  [rightTextView setChanges: anArray];
  [rightTextView computeLineRangesFromUpTo: 1000];
}

- (void) resizeSubviewsWithOldSize: (NSSize) oldFrameSize
{
  [self tile];
}


- (void) tile
{
  NSSize size = [self bounds].size;
  float width;

  width = (size.width - 80) / 2;

  width = ceilf(width);
  
  [leftView setFrame: 
	      NSMakeRect(0, 0,
			 width, size.height)];
  [rightView setFrame:
	       NSMakeRect(size.width - width, 0,
			  width, size.height)];
  [middleView setFrame:
		NSMakeRect(width, 21,
			   size.width - 2 * width, 
			   size.height - 23)];
}

- (void) rightViewBoundsDidChange: (NSNotification *) aNotification
{
  [middleView display];
}

- (void) leftViewBoundsDidChange: (NSNotification *) aNotification
{
  [middleView display];
}
@end
