const char *PlzDir=0;

#import "NuiController.h"
#import "MyBrowserCell.h"

@implementation NuiController

- convert:sender
{
    ConvertCallback(cd);
    return self;
}

- strassenverzeichnis:sender
{
    StrassenverzeichnisCallback(cd);
    return self;
}

- listSelected:sender
{
    if (callback && [[wahl selectedCell] stringValue]) {
    	(*callback)(cd);
    }
    return self;
}

- listSelectedDouble:sender
{
    if (callback) {
    	(*callback)(cd);
    }
    [button performClick:self];
    
    return self;
}

- (MyString)getListSelection
{
    return [list objectAt:[[wahl selectedCell] tag]];
}

- (const char *)getStrasse
{
    return [strasse stringValue];
}

- (const char *)getOrt
{
    return [ort stringValue];
}

- setCallback:(CallbackFunction *)cb;
{
    callback=cb;
    return self;
}

- setOrtTo:(char *)derOrt
{
    return [ort setStringValue:derOrt];
}

- setPlz_neuTo:(char *)diePlz
{
    return [plz_neu setStringValue:diePlz];
}

- setStrasseTo:(char *)dieStrasse
{
    return [strasse setStringValue:dieStrasse];
}

- setListTo:(MyString *)stringList withLen:(long)len
{
    int i,j,n;
    List *l,*nl;
    
    [list makeObjectsPerform:@selector(freeObjects)];
    [list freeObjects];
    for (i=0; i<len; i++) {
    	l = stringList[i];
        n = [l count];
	nl = [[List alloc] initCount:n];
	for (j=0; j<n; j++) {
	   [nl addObject:[[l objectAt:j] copy]];
	}
    	[list addObject:nl];
    }
    [wahl loadColumnZero];
    return self;
}

- enableStrassenverzeichnis:(BOOL)flag
{
    [menuStrassen setEnabled:flag];
    return self;
}


- appDidInit:sender
{
    [wahl setCellClass:[MyBrowserCell class]];
    list = [[List alloc] init];
    PlzDir = NXGetDefaultValue ([NXApp appName], "PlzDir");
    if (!PlzDir) {
       PlzDir="/NextLibrary/Plz";
       NXWriteDefault([NXApp appName], "PlzDir", PlzDir);
    }
    if (!*PlzDir) {
       PlzDir="/NextLibrary/Plz";
       NXWriteDefault([NXApp appName], "PlzDir", PlzDir);
    }
    [plzdir setStringValue:PlzDir];
    
    [wahl setDoubleAction:@selector(listSelectedDouble:)];
    
    cd = NeXTInterface(self);
        
    return self;
}

- getPlzDirFrom:sender
{
    PlzDir=[sender stringValue];
    if (!*PlzDir) {
       PlzDir="/NextLibrary/Plz";
       [sender setStringValue:PlzDir];
    }
    NXWriteDefault([NXApp appName], "PlzDir", PlzDir);
    
    return self;
}


- (int)browser:sender fillMatrix:matrix inColumn:(int)column
{
    return [list count];
}


- browser:sender loadCell:cell atRow:(int)row inColumn:(int)column
/* This delegate method loads the cell for a given row.  The stringValue
 * for that row comes from the list.
 */
{
    char s[300];
    MyString stringlist;
    MyStringSegment *segment;
    int len,i;
        
    if (list) {
        *s='\0';
    	if (row < [list count]) {
		stringlist = [list objectAt:row];
		len = [stringlist count];
		for (i=0; i<len; i++) {
		   segment=[stringlist objectAt:i];
		   if ([segment typ] != SEPARATOR && [segment typ] != FREE) {
		   	strcat(s,[segment string]);
		   }
		   if ([segment typ] == FREE) 
		         NXLogError("freed string segment!\n");
		}
		[cell setStringValue:s];
		[cell setLeaf:YES];
		[cell setTag:row];
	}
    }
    return self;
}

@end
