/*
**  AttributesOutlineView.m
**
**  Copyright (c) 2004
**
**  Author: Yen-Ju Chen <yjchenx@hotmail.com>
**
**  This program 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.
**
**  This program 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 this program; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "AttributesOutlineView.h"
#include "GNUstep.h"
#include "Node.h"
#include <AppKit/AppKit.h>

@implementation AttributesOutlineView

#ifndef GNUSTEP
/* Prevent from editing next cell */
- (void) textDidEndEditing: (NSNotification *) not
{
  if ([[[not userInfo] objectForKey: @"NSTextMovement"] intValue] == NSReturnTextMovement)
  {
    NSMutableDictionary *info;
    info = [NSMutableDictionary dictionaryWithDictionary: [not userInfo]];
    [info setObject: [NSNumber numberWithInt: NSIllegalTextMovement]
	     forKey: @"NSTextMovement"];
    NSNotification *newNot;
    newNot = [NSNotification notificationWithName: [not name]
	                                   object: [not object]
				         userInfo: info];

    [super textDidEndEditing: newNot];
    [[self window] makeFirstResponder: self];
  }
  else
    [super textDidEndEditing: not];
}
#endif /* -textDidEndEditing: */

#ifndef GNUSTEP
- (void) cancelOperation: (id) sender
{
  unsigned int rowIndex = [self editedRow];
  unsigned int columnIndex = [self editedColumn];
  if ((rowIndex == -1) || (columnIndex == -1)) return;

  NSTableColumn *column = [[self tableColumns] objectAtIndex: columnIndex];
  id item = [self itemAtRow: rowIndex];

  [self abortEditing];

  /* Trigger setObjectValue: */
  id object = [[self dataSource] outlineView: self
	                         objectValueForTableColumn: column
				 byItem: item];
  [[self dataSource] outlineView: self
	             setObjectValue: object
		     forTableColumn: column
		     byItem: item];
}
#endif

- (void) mouseDown: (NSEvent *) event
{
  if ([event type] == NSLeftMouseDown)
    {
      NSPoint p = [self convertPoint: [event locationInWindow]
                            fromView: [[self window] contentView]];
      int rowIndex = [self rowAtPoint: p];
      if (rowIndex < 0) return;
      NSRect drawingRect = [self frameOfCellAtColumn: 1
                                 row: rowIndex];
      Node *item = [self itemAtRow: rowIndex];

      if ([event clickCount] == 1)
        {
          if (p.x > (drawingRect.origin.x+drawingRect.size.width))
	    {
              if ([[self delegate] respondsToSelector: @selector(outlineView:iconForItem:)] == YES)
                {
                  NSImage *icon = [[self delegate] outlineView: self
                                                   iconForItem: item]; 
		  if (icon)
		    {
	              if ([[self delegate] respondsToSelector: @selector(outlineView:didClickIconOfItem:)] == YES)
	                {
	                  [[self delegate] outlineView: self
		                    didClickIconOfItem: [self itemAtRow: rowIndex]];
	
                          return;
	                }
		    }
		}
	    }
        }
      else if ([event clickCount] == 2)
        {
          if (p.x > (drawingRect.origin.x+drawingRect.size.width))
	    {
              if ([[self delegate] respondsToSelector: @selector(outlineView:iconForItem:)] == YES)
                {
                  NSImage *icon = [[self delegate] outlineView: self
                                                   iconForItem: item]; 
		  if (icon)
	            return;
		}
	    }
	}
    }

  [super mouseDown: event];
}

- (void)drawRow: (int)rowIndex clipRect: (NSRect)aRect
{
  [super drawRow: rowIndex clipRect: aRect];

  NSTableColumn *tb;
  NSRect drawingRect;
  NSImage *icon;
  Node *item;
  NSSize iconSize;
  id cell;
  int i;

  tb = [[self tableColumns] objectAtIndex: 1];
  cell = [tb dataCellForRow: rowIndex];
  item = [self itemAtRow: rowIndex];

  drawingRect = [self frameOfCellAtColumn: 1
                      row: rowIndex];

  if ([[self delegate] respondsToSelector: @selector(outlineView:iconForItem:)] == YES)
    {
      icon = [[self delegate] outlineView: self
                              iconForItem: item]; 
    }
  else
    return;

  if (icon) {
    iconSize = [icon size];
#ifdef GNUSTEP
    drawingRect = NSMakeRect(drawingRect.origin.x+drawingRect.size.width,
                             drawingRect.origin.y,
                             drawingRect.size.height,
                             drawingRect.size.height);
    if ([self isFlipped])
      drawingRect.origin.y += drawingRect.size.height;

    [icon compositeToPoint: drawingRect.origin
                 operation: NSCompositeSourceOver];
#else
    drawingRect = NSMakeRect(drawingRect.origin.x+drawingRect.size.width+1,
			     drawingRect.origin.y+1,
                             drawingRect.size.height-2,
			     drawingRect.size.height-2);

    if ([self isFlipped])
      {
        [icon setFlipped: YES];
      } 

    [icon drawInRect: drawingRect 
	    fromRect: NSMakeRect(0, 0, iconSize.width, iconSize.height)
           operation: NSCompositeSourceOver
	    fraction: 1.0];
#endif
  }
}

- (NSRect) frameOfCellAtColumn: (int)columnIndex 
                           row: (int)rowIndex
{
  if ((columnIndex < 0) || (rowIndex < 0) ||
      (columnIndex > ([self numberOfColumns] - 1)) ||
      (rowIndex > ([self numberOfRows] - 1)))
    return NSZeroRect;

  if (columnIndex == 1)
    {
      NSRect result = [super frameOfCellAtColumn: columnIndex
                      row:rowIndex];
      result.size.width -= result.size.height;
      return result;
    }
  else
    {
      return [super frameOfCellAtColumn: columnIndex
                    row: rowIndex];
    }
}

@end
