/*	This file is part of the XText package (version 0.8)
	Mike Dixon, April 1992
	
	Copyright (c) 1992 Xerox Corporation.  All rights reserved.

	Use and copying of this software and preparation of derivative works based
	upon this software are permitted.  This software is made available AS IS,
	and Xerox Corporation makes no warranty about the software or its
	performance.
*/

#import "XTDemo.h"
#import <appkit/Control.h>
#import <appkit/Font.h>
#import "XTScroller.h"
#import "XText.h"
#import "ErrorStream.h"

@implementation XText(XTDemo_extras)
- insertKeyCombination:(NXEvent *)event
{
	char code[6];
	int code_len = 0;
	int last_digit = event->data.key.keyCode & 0xf;

	if (event->flags & NX_CONTROLMASK)   code[code_len++] = 'c';
	if (event->flags & NX_SHIFTMASK)     code[code_len++] = 's';
	if (event->flags & NX_ALTERNATEMASK) code[code_len++] = 'a';
	if (event->flags & NX_COMMANDMASK)   code[code_len++] = 'm';
	code[code_len++] = '0' + (event->data.key.keyCode >> 4);
	code[code_len++] = (last_digit < 10) ? '0'+last_digit : 'a'+last_digit-10;
	[self replaceSel:code length:code_len];
	return self;
}

- insertKeyCombOfNextKey
{
	static id action = nil;

	if (!action)
		action = [[XTEventMsgAction alloc]
						initSel:@selector(insertKeyCombination:)];
	nextAction = action;
	return self;
}
@end

@implementation XTDemo

- init
{
	[super init];
	initialAction = [[XTDispatchAction alloc] initBase:"" estream:nil];
	[initialAction addBindings:"c'Q=insertKeyCombOfNextKey"
				   estream:nil];
	return self;
}

- doIt:sender
{
	[initialAction addBindings: [cmdField stringValue]
				   estream:[xtext errorStream]];
    return self;
}

- appDidInit:sender
{
	id my_font = [Font newFont:"Ohlfs" size:14.0];
	
	[cmdField setFont:my_font];
	
	xtext = [xtext docView];
	[xtext setFont:my_font];
	[xtext setInitialAction:initialAction];
	[xtext setSel:0 :0];
	[[xtext window] makeKeyAndOrderFront:self];
	return self;
}

- windowWillReturnFieldEditor:sender toObject:client
{
	return [XText newFieldEditorFor:sender
					initialAction:initialAction
					estream:nil];
}

@end
