/* Command.c */
/* David Weenink, 10/7/95 */

#include "Command.h"

class_methods (Command, Thing)
class_methods_end

int Command_init (I, char *name, Any data, int (*execute)(Any), int (*undo)(Any))
{
	iam (Command);
	Melder_assert (execute && undo);
	Thing_setName (me, name);
	my data = data; my execute = execute; my undo = undo;
	return 1;
}

int Command_do (I)
{
	iam (Command);
	return my execute (me);
}

int Command_undo (I)
{
	iam (Command);
	return my undo (me);
}

class_methods (CommandHistory, Ordered)
class_methods_end

Any CommandHistory_create (long maximumCapacity)
{
	CommandHistory me = new (CommandHistory);
	if (! me || ! Collection_init (me, classCommand, maximumCapacity)) forget (me);
	return me;
}

void CommandHistory_forth (I)
{
	iam (CommandHistory);
	my current++;
}

void CommandHistory_back (I)
{
	iam (CommandHistory);
	my current--;
}

Any CommandHistory_getItem (I)
{
	iam (CommandHistory);
	Melder_assert (my current > 0 && my current <= my size);
	return my item[my current];
}

void CommandHistory_insertItem (I, Any data)
{
	iam (CommandHistory); long i;
	Melder_assert (data && (Thing_member (data, my itemClass) || my itemClass == NULL));
	if (my current < my size)
	{
		for (i=my current+1; i <= my size; i++) forget (my item[i]);
		my size = my current;
	}
	if (my size >= my _capacity) Collection_removeItem (me, 1);
	my item[++my size] = data;
	my current = my size;
}

int CommandHistory_empty (I)
{
	iam (CommandHistory);
	return my size == 0;
}

int CommandHistory_offleft (I)
{
	iam (CommandHistory);
	return my current == 0;
}

int CommandHistory_offright (I)
{
	iam (CommandHistory);
	return my size == 0 || my current == my size + 1;
}

char *CommandHistory_commandName (I, long offsetFromCurrent)
{
	iam (CommandHistory); long pos = my current + offsetFromCurrent;
	return pos >= 1 && pos <= my size ? Thing_getName (my item[pos]) : NULL;
}

/* End of file Command.c */
