
#import <Foundation/NSUtilities.h>
#import <AppKit/AppKit.h>
#ifdef WIN32
#import <ntunix.h>
#else
#import <libc.h>
#import <sys/time.h>
#endif
#import "iconkit.h"


#define WARNING(s)									\
	{												\
 		NSLog(s, [(NSObject *)object description]);				\
 		ok = NO;									\
	}


BOOL IKCheckConformance (id object)
{
	BOOL
		ok = YES;
	
	if (object != nil)
	{
		if (![object  conformsToProtocol: @protocol(IKDependency)])
						WARNING (@"%@ does not conform to IKDependency")
		
		if (![object  conformsToProtocol: @protocol(IKSimpleObject)])
						WARNING (@"%@ does not conform to IKSimpleObject")
		else
		{
			if ([object  isEditable] &&
				![object  conformsToProtocol: @protocol(IKEditableObject)])
						WARNING (@"%@ should conform to IKEditableObject")
			
			if ([object  isDraggable] &&
				![object  conformsToProtocol: @protocol(IKDraggableObject)])
						WARNING (@"%@ should conform to IKDraggableObject")
			
			if ([object  isDragAccepting] &&
				![object  conformsToProtocol: @protocol(IKDragAcceptingObject)])
						WARNING (@"%@ should conform to IKDragAcceptingObject")
		}
	}
	
	return (ok);
}


NSString *MyIKidPboardType = NULL;

NSString *IKidPboardType(void)
{
	IKInitIDpboardType ();
	return MyIKidPboardType;
}

#ifdef WIN32
#define srandom srand
#define random rand
#endif

void IKInitIDpboardType ()
{
	if (MyIKidPboardType == NULL) {
		struct timeval			tp;
		struct timezone			tzp;
		long				randomString[3];
		int				i;
		
		gettimeofday(&tp, &tzp);
		srandom(tp.tv_usec);
		for (i = 0; i < 2; i++) randomString[i] = random();
		randomString[2] = 0;
		MyIKidPboardType = [[NSString stringWithCString:(const char*)randomString] retain];
	}
}


void IKCopyID (NSPasteboard * pboard, id object)
{
    NSArray *pasteTypes = IKIDPasteTypes();
	
    [pboard declareTypes:pasteTypes owner:nil];
    [pboard setString:[NSString stringWithFormat:@"%x", object] forType:IKidPboardType()];
}


id IKReadID (NSPasteboard * pboard)
{
    NSArray *pasteTypes = IKIDPasteTypes();
    id      object = nil;

    if ([pboard availableTypeFromArray:pasteTypes]) {
        NSString *string = [pboard stringForType:IKidPboardType()];
        if(string && [string length])
            sscanf([string cString], "%x", (unsigned int *)(&object));
    }

    return object;
}


NSArray * IKIDPasteTypes (void)
{
    static NSMutableArray *pasteTypes = nil;
    if(!pasteTypes) {
        pasteTypes = [[NSMutableArray array] retain];
        IKInitIDpboardType();
        [pasteTypes addObject:IKidPboardType()];
    }
    return pasteTypes;
}


void IKShortenTitle (NSCell * text,  float  maxWidth)
{
    NSMutableString *textBuffer = [NSMutableString stringWithString:[text stringValue]];
    NSSize          size;
    BOOL            deleted = NO;
    int             len = [textBuffer length];

    size = [text cellSize];
    while((size.width > maxWidth) && (len > 1)) {
        [textBuffer deleteCharactersInRange:NSMakeRange(--len, 1)];
        [text setStringValue:textBuffer];
        size = [text cellSize];
        deleted = YES;
    }
    if(deleted) {
        if(len > 2) {
            [textBuffer deleteCharactersInRange:NSMakeRange(len-2, 2)];
        }
        [text setStringValue:[textBuffer stringByAppendingString:@"..."]];
    }
}
