
/* MiscTreeBrowserController.m
 *
 */

#import <stdio.h>
#import <stdlib.h>
#import <appkit/Font.h>
#import <misckit/MiscTreeBrowser.h> 
#import <misckit/MiscTreeBrowserCell.h> 

#import "_MiscTreeBrowserNode.h"

#import "TestBed.h"

@implementation MiscTreeBrowserController

- init
{
	[super init];
	nodeC = 0;
	return self;
}

- free
{
	return [super free];
}

char *childString = "Child %d";
char *sibString = "Sibling %d";
char *nodeString = "Node %d";

/*(
Initialise MiscTreeBrowser.
)*/
- appDidInit:sender;
{
	id root = [[_MiscTreeBrowserNode alloc] initWithString:"Root" parent:nil];

//	printf("App did init.  TreeBrowser is 0x%8x.\n", (unsigned)treeBrowser);

								// add an initial root node
	[treeBrowser addNode:root toParent:nil after:nil];

	[treeBrowser setFont:[Font newFont:"Helvetica"
							   size:12
							   style:0
							   matrix:NX_FLIPPEDMATRIX]];
	[textf setStringValue:nodeString];

	return self;
}

/*(
Add a new child node or the selected cell programmatically
)*/
- addChild:sender
/*"
Add a child to the selected node (if any).  Example showing how to do it programmatically
"*/
{
	char *str = [self getString:childString];
	id newNode = [[_MiscTreeBrowserNode alloc] initWithString:str parent:nil];

	[treeBrowser addChild:newNode];

	return self;
}

- addSibling:sender
/*"
Add a sibling to the selected node (if any).  Example showing how to do it programmatically
"*/
{
	char *str = [self getString:sibString];
	id newNode = [[_MiscTreeBrowserNode alloc] initWithString:str parent:nil];

	[treeBrowser addSibling:newNode];

	return self;
}

- deleteSelected:sender
/*"
Delete selected node (if any).  Example showing how to do it programmatically
"*/
{
	[treeBrowser removeNode:[treeBrowser selectedNode]];

	return self;
}

- (char *)getString:(char *)defstr
{
	static char *theString = NULL;
	const char *guiString = [textf stringValue];

	if (!theString) theString = (char *)malloc(1024);
	nodeC++;
	sprintf(theString, (guiString ? guiString : defstr), nodeC);
	return theString;
}

@end
