/*
** Amster - Clickable URL Class
** Copyright © 2000 by Gürer Özen
**
** 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 "config.h"

#include <string.h>

#include "mui.h"
#include <proto/graphics.h>
#include <proto/openurl.h>

#include "url.h"

struct Library *OpenURLBase;

ULONG url_new(struct IClass *cl, Object *obj, struct opSet *msg);
ULONG url_setup(struct IClass *cl, Object *obj, Msg msg);
ULONG url_minmax(struct IClass *cl, Object *obj, struct MUIP_AskMinMax *msg);
ULONG url_draw(struct IClass *cl, Object *obj, struct MUIP_Draw *msg);
ULONG url_cleanup(struct IClass *cl, Object *obj, Msg msg);
ULONG url_clicked(struct IClass *cl, Object *obj, Msg msg);


MUI_DISPATCH(url_dispatch)
{
	switch(msg->MethodID) {
		case OM_NEW:         return(url_new(cl,obj,(APTR)msg));
		case MUIM_Setup:     return(url_setup(cl,obj,(APTR)msg));
		case MUIM_AskMinMax: return(url_minmax(cl,obj,(APTR)msg));
		case MUIM_Draw:      return(url_draw(cl,obj,(APTR)msg));
		case MUIM_Cleanup:   return(url_cleanup(cl,obj,(APTR)msg));
		case URL_CLICKED:    return(url_clicked(cl,obj,(APTR)msg));
	}
	return(DoSuperMethodA(cl,obj,msg));
}


ULONG url_new(struct IClass *cl, Object *obj, struct opSet *msg)
{
	struct urldata *data;
	char *name, *href;
/*	char *help;*/

	name = (char *)GetTagData(URL_NAME,0,msg->ops_AttrList);
	href = (char *)GetTagData(URL_HREF,0,msg->ops_AttrList);
/*	help = (char *)GetTagData(URL_HELP,0,msg->ops_AttrList);*/

	obj = (Object *)DoSuperNew(cl,obj,
		MUIA_Font, MUIV_Font_Button,
		MUIA_ShowSelState, FALSE,
		MUIA_InputMode, MUIV_InputMode_RelVerify,
		/* MUIA_ShortHelp, help, */
		TAG_MORE, msg->ops_AttrList);

	if(!obj) return(NULL);

	data = INST_DATA(cl,obj);
	data->name = name;
	data->href = href;
	data->len = strlen(name);
	data->color = -1;

	DoMethod(obj,MUIM_Notify,MUIA_Pressed,FALSE,obj,1,URL_CLICKED);
	return((ULONG)obj);
}


ULONG url_setup(struct IClass *cl, Object *obj, Msg msg)
{
	struct urldata *data = INST_DATA(cl,obj);

	if (!DoSuperMethodA(cl,obj,msg))
		return(FALSE);

	data->color = ObtainBestPen(_screen(obj)->ViewPort.ColorMap,0,0,0,OBP_Precision,PRECISION_GUI,TAG_DONE);

	return(TRUE);
}


ULONG url_minmax(struct IClass *cl, Object *obj, struct MUIP_AskMinMax *msg)
{
	struct urldata *data = INST_DATA(cl,obj);
	int x,y;

	DoSuperMethodA(cl,obj,(APTR)msg);

	x = _font(obj)->tf_XSize * data->len;
	y = _font(obj)->tf_YSize;

	msg->MinMaxInfo->MinWidth  += x;
	msg->MinMaxInfo->DefWidth  += x + (x/10);
	msg->MinMaxInfo->MaxWidth  += MUI_MAXMAX;
	msg->MinMaxInfo->MinHeight += y;
	msg->MinMaxInfo->DefHeight += y;
	msg->MinMaxInfo->MaxHeight += y;

	return(0);
}


ULONG url_draw(struct IClass *cl, Object *obj, struct MUIP_Draw *msg)
{
	struct urldata *data = INST_DATA(cl,obj);
	int x,y,i;

	DoSuperMethodA(cl,obj,(APTR)msg);
	if(!(msg->flags & MADF_DRAWOBJECT)) return(0);

	SetFont(_rp(obj),_font(obj));

	if(!data->pixlen) data->pixlen=TextLength(_rp(obj),data->name,data->len);
	x = _mleft(obj) + ( (_mwidth(obj) - data->pixlen) / 2);
	y = _mtop(obj) + _font(obj)->tf_Baseline + ( (_mheight(obj) - _font(obj)->tf_YSize) / 2 );

	Move(_rp(obj),x,y);
	if(data->color != -1) SetAPen(_rp(obj),data->color); else SetAPen(_rp(obj),1);
	Text(_rp(obj),data->name,data->len);

	for (i=x; i<=x+data->pixlen; i+=4) {
 		Move(_rp(obj), i, y+3);
		Draw(_rp(obj), i+1, y+3);
	}

	return(0);
}


ULONG url_cleanup(struct IClass *cl, Object *obj, Msg msg)
{
	struct urldata *data = INST_DATA(cl,obj);

	if(data->color != -1) ReleasePen(_screen(obj)->ViewPort.ColorMap,data->color);

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


ULONG url_clicked(struct IClass *cl, Object *obj, Msg msg)
{
	struct urldata *data = INST_DATA(cl,obj);

	OpenURLBase = OpenLibrary("openurl.library", 1);
	if (!OpenURLBase) return(0);
	URL_Open(data->href, TAG_DONE);
	CloseLibrary(OpenURLBase);
	return(0);
}
