//---------------------------------------------------------------------------------------------------------
//
//	UsingColorBrowserController
//
//	Inherits From:		Object
//
//	Declared In:		UsingColorBrowserController.h
//
//
//	Disclaimer
//
//		You may freely copy, distribute and reuse this software and its
//		associated documentation. I disclaim any warranty of any kind, 
//		expressed or implied, as to its fitness for any particular use.
//
//---------------------------------------------------------------------------------------------------------
#import "UsingColorBrowserController.h"
#import "ColorBrowser.h"
#import <objc/objc-runtime.h>


@implementation UsingColorBrowserController

//---------------------------------------------------------------------------------------------------------
//	Private Methods
//---------------------------------------------------------------------------------------------------------
- _subclassListFor: (const char*) aClass
{
	id		subclassList = [[List alloc] init];
	Class 	currentClass;
		
	if (classes == NULL) classes = objc_getClasses();

	state = NXInitHashState(classes);

	while ( NXNextHashState(classes, &state, (void **)&currentClass) )
		{
		if (strcmp (object_getClassName (currentClass), aClass) == 0) continue;
		if (strcmp (object_getClassName (currentClass->super_class), aClass) == 0)
			[subclassList addObject: (id)object_getClassName(currentClass)];
		}

	return subclassList;
}


- _subclassListForNil
{
	id		subclassList = [[List alloc] init];
	Class 	currentClass;
		
	if (classes == NULL) classes = objc_getClasses();

	state = NXInitHashState(classes);

	while ( NXNextHashState(classes, &state, (void **)&currentClass) )
		if (currentClass->super_class == NULL) 
			[subclassList addObject: (id)object_getClassName(currentClass)];

	return subclassList;
}


- (BOOL) _isLeaf: (const char*) aClass
{
	id		subclassList = [self _subclassListFor: aClass];
	BOOL	isLeaf;
		
	isLeaf = ([subclassList count] ? NO : YES);
	
	if (subclassList) [subclassList free];
	return isLeaf;
}


static int _Comparator (const void *arg1, const void *arg2)
{
   	return NXOrderStrings (*(char**)arg1, *(char**)arg2, YES, -1, NULL);
}


- _sort: (List*) aList
{
	void*	dataPtr = NX_ADDRESS (aList);
	qsort (dataPtr, [aList count], sizeof (char*), _Comparator);
	return self;
}


//---------------------------------------------------------------------------------------------------------
//	Action Methods
//---------------------------------------------------------------------------------------------------------
- setColor: sender
{
	color = [sender color];
	return self;
}


- allCellsInColumn: sender
{
	int	column = ([browser selectedColumn] < 0 ? 0 : [browser selectedColumn]);
	[browser setTextColor:color inColumn:column];
	return self;
}


- selectedCells: sender
{
	[browser setTextColorOfSelectedCells: color];
	return self;
}


- stringValue: sender
{
	int	column = ([browser selectedColumn] < 0 ? 0 : [browser selectedColumn]);
	[browser setTextColor:color forCellWithStringValue:[sender stringValue] inColumn:column];
	return self;
}


//---------------------------------------------------------------------------------------------------------
//	Application Delegate Methods
//---------------------------------------------------------------------------------------------------------
- appDidInit: sender
{
	color = NX_COLORRED;
	return self;
}


//---------------------------------------------------------------------------------------------------------
//	Browser Delegate Method
//---------------------------------------------------------------------------------------------------------
- (int) browser:aBrowser fillMatrix:aMatrix inColumn:(int)aColumn
{
	id	subclassList;
	int	iterator;
	
	if (aColumn == 0)
		subclassList = [self _subclassListForNil];
	else
		subclassList = [self _subclassListFor: [[browser selectedCell] stringValue]];

	[self _sort: subclassList];

	for (iterator = 0; iterator < [subclassList count]; iterator++)
		{
		[aMatrix renewRows: (iterator + 1) cols:1];
		[[aMatrix cellAt: iterator :0] setStringValue: (char*)[subclassList objectAt: iterator]];
		[[aMatrix cellAt: iterator :0] setLeaf: [self _isLeaf: (char*)[subclassList objectAt: iterator]]];
		}
		
	if (subclassList) [subclassList free];
	return iterator;
}
	
	
@end