
/* Generated by Interface Builder */

#import "Controller.h"
#import "SwapView.h"
#import <appkit/Application.h>
#import <appkit/ButtonCell.h>
#import <appkit/Matrix.h>
#import <appkit/Panel.h>
#import <appkit/ScrollView.h>
#import <appkit/Text.h>
#import <objc/HashTable.h>
#import "string.h"
#import "stdio.h"
#import "stdlib.h"

@implementation Controller

/* get the text of the selection as a string */
const char *copySel(id target)
{
    NXSelPt start, end;
    char *buf;
    [target getSel:&start :&end];
    if (start.cp > end.cp) { start.cp= end.cp= 0; }
    buf= (char *) malloc(end.cp - start.cp +1);
    [target getSubstring:buf start:start.cp length:(end.cp - start.cp)];
    buf[end.cp - start.cp] = '\0';
    return (const char *) buf;
}

/* notify the delegate of the text object that the text changed */
void notify(id target)
{
    id delegate= [target delegate];
    if (delegate && [delegate respondsTo:@selector(textDidGetKeys:isEmpty:)]) {
	[delegate 
	    perform:@selector(textDidGetKeys:isEmpty:)
	    with:target with:(id)NO]; /* ugh,ugh */
    }
}

- useLabelAsDecl:sender
{
    const char *n= [[sender selectedCell] title];
    const char *sel= copySel([text_view docView]);
    char *buf;
    if (!n) fprintf(stderr,"no label\n");
    else {
    	buf= malloc(strlen(sel)+ strlen(n) + 6);
    	sprintf(buf,"{\\%s %s}", n, sel);
	notify([[text_view docView] replaceSel:buf]);
	free(buf); free((char*)sel);
    }	
    return self;
}

- useIconNameAsDecl:sender
{
    const char *n= [[sender selectedCell] icon];
    const char *sel= copySel([text_view docView]);
    char *buf;
    if (!n) fprintf(stderr,"no icon name\n");
    else {
    	buf= malloc(strlen(sel)+ strlen(n) + 6);
    	sprintf(buf,"{%s %s}", n, sel);
	notify([[text_view docView] replaceSel:buf]);
	free(buf); free((char*)sel);
    }	
    return self;
}
- useIconNameAsPrintf:sender
{
    const char *n= [[sender selectedCell] icon];
    const char *sel= copySel([text_view docView]);
    char *buf;
    if (!n) fprintf(stderr,"no icon name\n");
    else {
    	buf= malloc(strlen(sel)+ strlen(n) + 6);
    	sprintf(buf, n, sel);
	notify([[text_view docView] replaceSel:buf]);
	free(buf); free((char*)sel);
    }	
    return self;
}
- useLabelAsCmd:sender
{
    const char *n= [[sender selectedCell] title];
    const char *sel= copySel([text_view docView]);
    char *buf;
    if (!n) fprintf(stderr,"no label\n");
    else {
    	buf= malloc(strlen(sel)+ strlen(n) + 6);
    	sprintf(buf,"\\%s{%s}", n, sel);
	notify([[text_view docView] replaceSel:buf]);
	free(buf); free((char*)sel);
    }	
    return self;
}

- useIconNameAsCmd:sender
{
    const char *n= [[sender selectedCell] icon];
    const char *sel= copySel([text_view docView]);
    char *buf;
    if (!n) fprintf(stderr,"no icon name\n");
    else {
    	buf= malloc(strlen(sel)+ strlen(n) + 6);
    	sprintf(buf,"%s{%s}", n, sel);
	notify([[text_view docView] replaceSel:buf]);
	free(buf); free((char*)sel);
    }	
    return self;
}

- insertLabel:sender
{
    const char *n= [[sender selectedCell] title];
    char *buf;
    if (!n) fprintf(stderr,"no label\n");
    else {
    	buf= malloc(strlen(n) + 6);
	sprintf(buf,"\\%s ", n);
	notify([[text_view docView] replaceSel:buf]);
	free(buf);
    }
    return self;
}

- insertIconName:sender
{
    const char *n= [[sender selectedCell] icon];
    char *buf;
    if (!n) fprintf(stderr,"no icon name\n");
    else {
    	buf= malloc(strlen(n) + 6);
	sprintf(buf,"%s ", n);
	notify([[text_view docView] replaceSel:buf]);
	free(buf);
    }
    return self;
}

- setup
{
    [[swap_place window] setBecomeKeyOnlyIfNeeded:YES];
    panel_table= [[HashTable alloc] initKeyDesc:"*" valueDesc:"@"]; 
    return self;
}

- setup:sender
{
    return [self setup];
}

- appDidInit:sender
{
    return [self setup:sender];
}

/* swap in new panel */
- switchToIconName:sender
{
    panel_name= [[sender selectedCell] icon];
    if (!panel_name) {
        fprintf(stderr,"switchToIconName: no icon name\n");
    } else {
    	[swap_place perform:@selector(swapIt:) with:self 
			afterDelay:10 cancelPrevious:NO];
    }
    return self;
}

/* swapView delegate method */
- whatPanel
{
    id target_panel= (id) [panel_table valueForKey:panel_name];
    char *buf;

    if (target_panel) return target_panel;
    else {
    	buf= (char*) malloc(strlen(panel_name) + 6);
	sprintf(buf,"%s.nib", panel_name);
	[NXApp loadNibSection:(const char *)buf owner:self];
	free(buf);
	if (!returned_panel) {
	    NXRunAlertPanel("loTeX", "Can't load requested Nib section",
	    			0, 0, 0);
	    return nil;
	}
	[panel_table insertKey:(const void *) panel_name 
	             value: (void *) returned_panel];
	target_panel= returned_panel;
	returned_panel= nil;
	return target_panel;
    }
}

@end
