/*
 *   This sourcecode is part of FileSpy, a logfile observing utility.
 *   Copyright (C) 1996  Felix Rauch
 *
 *   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.
 *   
 *   Notice that this program may not be possessed, used, copied,
 *   distributed or modified by people having to do with nuclear
 *   weapons. See the file CONDITIONS for details.
 *
 *   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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *   To contact the original author, try:
 *   e-mail: Felix.Rauch@nice.ch
 *   Traditional mail:	Felix Rauch
 *			Sempacherstrasse 33
 *			8032 Zurich
 *			Switzerland
 */

// Most of this file is from:
// CustomCell.m
// By Jayson Adams, NeXT Developer Support Team
// You may freely copy, distribute and reuse the code in this example.
// NeXT disclaims any warranty of any kind, expressed or implied, as to its
// fitness for any particular use.

// This subclass of cell draws a dot image to the right of the text
// and provides fancy, printable highlighting.

#import <appkit/appkit.h>
#import "ExistenceTextFieldCell.h"

@implementation ExistenceTextFieldCell

  
#define IMAGEMARGIN 4.0
  
- drawInside:(const NXRect *)cellFrame inView:controlView
{
    /* every CustomCell needs these two */
    static id plusImage = nil, minusImage = nil, questionImage = nil;
    static id sharedTextCell = nil;

    NXRect rect = *cellFrame;
    NXPoint imageOrigin;
    NXSize plusSize, minusSize, questionSize;
    
    char *str, tmpStr[6];
    int t;
        
    if (!plusImage) {
	plusImage = [NXImage findImageNamed:"plus"];
    }
    [plusImage getSize:&plusSize];

    if (!minusImage) {
	minusImage = [NXImage findImageNamed:"minus"];
    }
    [minusImage getSize:&minusSize];

    if (!questionImage) {
	questionImage = [NXImage findImageNamed:"questionmark"];
    }
    [questionImage getSize:&questionSize];

  /* erase the cell */
    PSsetgray((cFlags1.state || cFlags1.highlighted) ? NX_WHITE : NX_LTGRAY);
    NXRectFill(cellFrame);

    if(![myDelegate permissionOk]) {
	/* draw the "attachment" image */
	imageOrigin.x = NX_X(cellFrame) + IMAGEMARGIN;
	imageOrigin.y = NX_Y(cellFrame) + NX_HEIGHT(cellFrame) -
			(NX_HEIGHT(cellFrame) - questionSize.height) / 2.0;
    
	[questionImage composite:NX_SOVER toPoint:&imageOrigin];
	    
	NX_WIDTH(&rect) -= (questionSize.width + IMAGEMARGIN * 2.0 - NX_X(&rect));
	NX_X(&rect) += questionSize.width + IMAGEMARGIN * 2.0;
    } else if([myDelegate fileExists]) {
	/* draw the "attachment" image */
	imageOrigin.x = NX_X(cellFrame) + IMAGEMARGIN;
	imageOrigin.y = NX_Y(cellFrame) + NX_HEIGHT(cellFrame) -
			(NX_HEIGHT(cellFrame) - plusSize.height) / 2.0;
    
	[plusImage composite:NX_SOVER toPoint:&imageOrigin];
	    
	NX_WIDTH(&rect) -= (plusSize.width + IMAGEMARGIN * 2.0 - NX_X(&rect));
	NX_X(&rect) += plusSize.width + IMAGEMARGIN * 2.0;
    } else {
	/* draw the "attachment" image */
	imageOrigin.x = NX_X(cellFrame) + IMAGEMARGIN;
	imageOrigin.y = NX_Y(cellFrame) + NX_HEIGHT(cellFrame) -
			(NX_HEIGHT(cellFrame) - plusSize.height) / 2.0;
    
	[minusImage composite:NX_SOVER toPoint:&imageOrigin];
	    
	NX_WIDTH(&rect) -= (minusSize.width + IMAGEMARGIN * 2.0 - NX_X(&rect));
	NX_X(&rect) += minusSize.width + IMAGEMARGIN * 2.0;
    }
    
    if (!sharedTextCell) {
	sharedTextCell = [[Cell alloc] init];
	[sharedTextCell setWrap:NO];
    }
    if([myDelegate showTime]) {
	BOOL didPrint = NO;
	str = (char *)alloca(strlen([self stringValue]) + 17);	// + " [1000d24h60m60s]"
	    // strcpy and strcat should be thread-safe, as they're no i/o-functions (?)
	strcpy(str, [self stringValue]);
	strcat(str, " [");
	if((t = timeDiff/(60*60*24)) > 0) {
	    sprintf(tmpStr, "%dd", t);
	    strcat(str, tmpStr);
	    didPrint = YES;
	}
	if((t = (timeDiff/(60*60) % 24)) > 0) {
	    sprintf(tmpStr, didPrint ? " %dh" : "%dh", t);
	    strcat(str, tmpStr);
	    didPrint = YES;
	}
	if((t = ((timeDiff/60) % 60)) > 0) {
	    sprintf(tmpStr, didPrint ? " %dm" : "%dm", t);
	    strcat(str, tmpStr);
	    didPrint = YES;
	}
	t = (timeDiff % 60);
	sprintf(tmpStr, didPrint ? " %ds]" : "%ds]", t);
	strcat(str, tmpStr);
    } else {
	str = (char *)[self stringValue];
    }
    [sharedTextCell setFont:[self font]];
    [sharedTextCell setStringValue:str];
    [sharedTextCell drawInside:&rect inView:controlView];
    
  /* all drawing from now on will be in dark gray */
    PSsetgray(NX_DKGRAY);
    
  /* draw the two dark gray lines above and below the cell */
    if (cFlags1.state || cFlags1.highlighted) {
        NXRect rectArray[2];
      /*
       * draw 1-pixel tall rectangles instead of lines (this is faster than
       * PSmoveto(); PSlineto()).
       */
	NXSetRect(&(rectArray[0]), NX_X(cellFrame), NX_Y(cellFrame),
		NX_WIDTH(cellFrame), 1.0);
	NXSetRect(&(rectArray[1]), NX_X(cellFrame), NX_MAXY(cellFrame) - 1.0,
		NX_WIDTH(cellFrame), 1.0);

      /* using NXRectFillList is faster than separate calls to NXRectFill */
	NXRectFillList(rectArray, 2);
    }

    return self;
}

- highlight:(const NXRect *)cellFrame inView:controlView lit:(BOOL)flag
{
    if (cFlags1.highlighted != flag) {
	cFlags1.highlighted = flag;
	[self drawInside:cellFrame inView:controlView];
    }
    return self;
}

- setMyDelegate:del
{
    myDelegate = del;
    return self;
}

- setTime:(time_t)newTime
{
    time = newTime;
    timeDiff = 0;
    return self;
}

- changeTime:(time_t)newTime
{
    timeDiff = newTime - time;
//    time = newTime;
    return self;
}

@end
