/*  Copyright (C) 2002 Tom Parker (tom@carrott.org),
                       Matthias Münch (matthias@amigaworld.de)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    In addition, as a special exception, Tom Parker and Matthias Münch give
    permission to link the code of this program with a TCP stack of your 
    choice, any official MUI libraries or classes and any custom MUI classes
    that should be necessary for the operation of this program.  This 
    exception also gives you permission to distribute linked combinations 
    including this software with any of the before-mentioned libraries and 
    classes.  You must obey the GNU General Public License in all respects for
    all of the code used other than that provided by the before-mentioned 
    libraries and classes.  As part of this exception you are obliged to 
    follow the license terms of the before-mentioned libraries, this license
    does not compel you to follow those terms, but if you do not then you may
    not link with those libraries.  If you modify this file, you may extend
    this exception to your version of the file, but you are not obligated to
    do so.  If you do not wish to do so, delete this exception statement from
    your version.
*/
/*
** Edit mcc (string subclass with history)
*/

#include "muihelp.h"
#include "edit.h"

#include <stdlib.h>
#include <string.h>

#include <proto/exec.h>
#include <exec/memory.h>

static ULONG edit_new(struct IClass *cl, Object *obj, struct opSet *msg);
static void edit_add(struct editdata *data, char *txt);
static int edit_input(Object *obj, struct editdata *data, struct MUIP_HandleEvent *msg);


MUI_DISPATCH(edit_dispatch)
{
	struct editdata *data;

	switch(msg->MethodID)
	{
	case OM_NEW:
		return edit_new(cl, obj, (APTR)msg);

	case OM_DISPOSE:
		data = INST_DATA(cl,obj);
		if(data->pool) DeletePool(data->pool);
		break;

	case MUIM_HandleEvent:
		if(edit_input(obj, INST_DATA(cl,obj), (APTR)msg) == 0) break;
		return MUI_EventHandlerRC_Eat;

	case MUIM_Textinput_Acknowledge:
		data = INST_DATA(cl,obj);
		if(data->pool) edit_add(data, (char *)MARG1);
		break;

	}
	return DoSuperMethodA(cl,obj,msg);
}


static ULONG edit_new(struct IClass *cl, Object *obj, struct opSet *msg)
{
	struct editdata *data;

	obj = (Object *)DoSuperNew(cl, obj,
		StringFrame,
		MUIA_Textinput_AutoExpand, TRUE,
		TAG_MORE, msg->ops_AttrList);

	if(!obj) return(0);

	data = INST_DATA(cl,obj);
	memset(data, 0, sizeof(struct editdata));

	data->type = (int)GetTagData(EDIT_TYPE, 0, msg->ops_AttrList);

	if(data->type == EDIT_TYPE_CHAT)
	{
		data->pool = CreatePool(MEMF_PUBLIC, 1024, 900);
/*		if(!data->pool)
		{
			CoerceMethod(cl, obj, OM_DISPOSE);
			return 0;
		}
*/		set(obj, MUIA_Textinput_RemainActive, TRUE);
	}

	data->tabhook = (editTabHook *)GetTagData(EDIT_TABHOOK, NULL, msg->ops_AttrList);
	data->hookdata = (void *)GetTagData(EDIT_HOOKDATA, NULL, msg->ops_AttrList);


	return (ULONG)obj;
}


static void edit_add(struct editdata *data, char *txt)
{
	struct editline *el;
	int len;

	if(!txt || *txt == '\0') return;

	len = strlen(txt);
	el = AllocPooled(data->pool, sizeof(struct editline) + len + 1);
	if(!el) return;
	memset(el, 0, sizeof(struct editline));
	el->len = len;
	strcpy(&el->text[0], txt);

	if(data->lastline)
	{
		data->lastline->next = el;
		el->prev = data->lastline;
	}
	else
	{
		data->lines = el;
	}
	data->lastline = el;
	data->cur = NULL;
}


static int edit_input(Object *obj, struct editdata *data, struct MUIP_HandleEvent *msg)
{
	u_long t;
	int code, qual;

	if(!msg->imsg || msg->imsg->Class != IDCMP_RAWKEY) return(0);

	code = msg->imsg->Code;
	qual = msg->imsg->Qualifier;

	if(data->tabhook && code == 0x42 && qual == (qual & 0x8004))
	{
		/* lala */
		return 0;
	}

	if(data->type == EDIT_TYPE_PLAIN) return(0);
	get(_win(obj), MUIA_Window_ActiveObject, &t);
	if((Object *)t != obj) return(0);

	switch(code)
	{
	case 0x4c:
		if(qual & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT))
		{
			/* shift up arrow */
			data->cur = data->lines;
			set(obj, MUIA_String_Contents, &data->cur->text[0]);
		}
		else
		{
			/* up arrow */
			if(data->cur)
				data->cur = data->cur->prev;
			else
				data->cur = data->lastline;

			if(data->cur) set(obj, MUIA_String_Contents, &data->cur->text[0]);
		}
		return 1;

	case 0x4d:
		if(qual & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT))
		{
			/* shift down arrow */
			data->cur = NULL;
			set(obj, MUIA_String_Contents, NULL);
		}
		else
		{
			/* down arrow */
			if(data->cur) data->cur = data->cur->next;

			if(data->cur)
				set(obj, MUIA_String_Contents, &data->cur->text[0]);
			else
				set(obj, MUIA_String_Contents, NULL);
		}
		return 1;
	}

	return 0;
}
