/*
** Amster - Information
** Copyright © 1999-2000 by Gürer Özen
** Copyright © 2000-2001 by Jacob Laursen
**
** 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
*/

#include "amster.h"

#include <MUI/NListview_mcc.h>

#include "amster_Cat.h"

#ifdef AMSTER_DEBUG
#include <proto/dos.h>
#endif

ULONG info_new(struct IClass *cl, Object *obj, struct opSet *msg);

#ifdef AMSTER_DEBUG
BPTR fh;
#endif


MUI_DISPATCH(info_dispatch)
{
	switch(msg->MethodID) {
		case OM_NEW: return(info_new(cl,obj,(APTR)msg));
#ifdef AMSTER_DEBUG
case OM_DISPOSE:
if (fh) Close(fh);
break;
#endif
		case INFO_MSG:
			{
			struct infodata *data = INST_DATA(cl, obj);
#ifdef AMSTER_DEBUG
			struct DateStamp *ds, *rds;
			char line[1024];

			if (strlen((char *)((muimsg)msg)->arg1) > 800) {
				MUI_Request(gui->app, gui->win, 0L,
				"Danger!",
				(char *)MSG_OK_GAD,
				"Please report this to Laursen immediately:\nINFO_MSG - length: %ld\n(when did it happen and what was\nwritten to the debug window at the time?)",
				strlen((char *)((muimsg)msg)->arg1));
			}

			if (ds = malloc(sizeof(struct DateStamp))) {
				memset(ds, 0, sizeof(struct DateStamp));
				if (rds = DateStamp(ds)) {
					sprintf(line, "[%02d:%02d:%02d.%02d] %s", rds->ds_Minute/60, rds->ds_Minute%60, rds->ds_Tick/50, rds->ds_Tick%50, (char *)((muimsg)msg)->arg1);

					DoMethod(data->msglist, MUIM_NList_InsertSingle, line, MUIV_NList_Insert_Bottom);
					set(data->msglist, MUIA_NList_First, MUIV_NList_Active_Bottom);
				}
				free(ds);
			}

			if (fh) {
				Write(fh, line, strlen(line));
				Write(fh, "\n", 1);
			}
#else
			DoMethod(data->msglist, MUIM_NList_InsertSingle, (char *)((muimsg)msg)->arg1, MUIV_NList_Insert_Bottom);
			set(data->msglist, MUIA_NList_First, MUIV_NList_Active_Bottom);
#endif
			return(NULL);
			}
	}
	return(DoSuperMethodA(cl, obj, msg));
}


ULONG info_new(struct IClass *cl, Object *obj, struct opSet *msg)
{
	struct infodata *data;
	Object *msglist,*clrbut;

	if (obj = (Object *)DoSuperNew(cl,obj,
		MUIA_HelpNode, "info",
		WindowContents, VGroup,
			Child, NListviewObject,
				MUIA_NListview_NList, msglist = NListObject,
					ReadListFrame,
					MUIA_NList_Input, FALSE,
					MUIA_NList_AutoCopyToClip, TRUE,
					MUIA_NList_TypeSelect, MUIV_NList_TypeSelect_Char,
					MUIA_NList_ConstructHook, MUIV_NList_ConstructHook_String,
					MUIA_NList_DestructHook, MUIV_NList_DestructHook_String,
					MUIA_NList_AutoVisible, TRUE,
				End,
			End,
			Child, HGroup,
				Child, HSpace(0),
				Child, clrbut = SimpleButton(MSG_INFO_CLEAR),
			End,
		End,
		TAG_MORE, msg->ops_AttrList))
	{
		data = INST_DATA(cl,obj);
		data->msglist = msglist;

		set(obj,MUIA_Window_ID,MAKE_ID('I','N','F','O'));
		set(obj,MUIA_Window_Title,MSG_INFO_TITLE),

		DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, gui->iconpanel, 1, PANEL_CLOSEDEBUG);
		DoMethod(clrbut, MUIM_Notify, MUIA_Pressed, FALSE, msglist, 1, MUIM_NList_Clear);

#ifdef AMSTER_DEBUG
fh = Open("RAM:Amster.debug", MODE_READWRITE);
if (fh) Seek(fh, 0, OFFSET_END);
#endif

		return((ULONG)obj);
	}
	return(0);
}


void gui_debug(char *msg)
{
	DoMethod(gui->iwin, INFO_MSG, msg);
}


void gui_debugf(char *msg, ...)
{
	static char buf[1024];
	va_list ap;

#ifdef AMSTER_DEBUG
if (strlen(msg) > 800) {
	MUI_Request(gui->app, gui->win, 0L,
		"Danger!",
		(char *)MSG_OK_GAD,
		"Please report this to Laursen immediately:\ngui_debugf - length: %ld\n(when did it happen and what was\nwritten to the debug window at the time?)",
		strlen(msg));
}
#endif

	va_start(ap,msg);
	vsprintf(buf,msg,ap);
	va_end(ap);
	gui_debug(buf);
}
