/**********************************************************************
 
	Copyright (C) 2003-2004
	Hirohisa MORI <joshua@nichibun.ac.jp>
	Tomoki SEKIYAMA <sekiyama@yahoo.co.jp>
 
	This program is free software; you can redistribute it 
	and/or modify it under the terms of the GLOBALBASE 
	Library General Public License (G-LGPL) as published by 

	http://www.globalbase.org/
 
	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.

**********************************************************************/



#include <LEditText.h>

#include "VReflector.h"
#include "v/VEditText.h"
#include "CClickCheckAttachment.h"

extern "C" {
#include "memory_debug.h"
#include "utils.h"
#include "machine/v_types.h"


class CEditText : public LEditText
{
public:
						CEditText(
								const SPaneInfo&	inPaneInfo,
								LCommander*			inSuperCommander,
								ConstStringPtr		inInitialText,
								ResIDT				inTextTraitsID,
								MessageT			inMessage,
								SInt16				inMaxChars,
								UInt8				inAttributes,
								TEKeyFilterFunc		inKeyFilter,
								VEditText *			mVEditText,
								bool				inPasswordField = false,
								ClassIDT			inImpID = imp_class_ID)
							: LEditText(inPaneInfo, inSuperCommander, inInitialText,
										inTextTraitsID, inMessage, inMaxChars,
										inAttributes, inKeyFilter, inPasswordField, inImpID),
							mVEditText(mVEditText)
						{
						}
protected:
	virtual void		BeTarget()		{ LEditText::BeTarget();	 mVEditText->focus_event(true);  }
	virtual void		DontBeTarget()	{ LEditText::DontBeTarget(); mVEditText->focus_event(false); }

	VEditText *			mVEditText;
};


VFONT* _v_set_descriptor_get_font(const L_CHAR * descriptor,
			const LC_WRITING_STYLE * ws,
			int fsize, int *size);

LTabGroup* GetTabGroupForWindow(LView* view);
}

VExError
VEditText::create_do(const VObjectStatus* s, int flags,VObject * nmp, void * arg)
{
	edit_text_set *set = (edit_text_set*)arg;
	min_len = set->min_len;

	SPaneInfo paneInfo;
	paneInfo.superView = dynamic_cast<LView*>(nmp->get_info_this());
	if ( paneInfo.superView == 0 )
		er_panic("non-LView object cannot be parent");
	paneInfo.paneID = sts.id;
	paneInfo.width = 0;
	paneInfo.height = 0;
	paneInfo.visible = true;
	paneInfo.enabled = true;
	paneInfo.left = 0;
	paneInfo.top = 0;
	paneInfo.bindings = (SBooleanRect){false,false,false,false};
	paneInfo.userCon = (long)this;
	SInt8 filter = (flags & VSF_ATTR && s->attr == integer_only) ?
								keyFilter_Integer : keyFilter_PrintingChar;
	LCommander *com = GetTabGroupForWindow(paneInfo.superView);
	CEditText *v = new CEditText(paneInfo, com, "\p", 0, msg_DescriptorChanged,
								set->max_len, editAttr_AutoScroll,
								UKeyFilters::SelectTEKeyFilter(filter), this);
	v->AddAttachment(new CClickCheckAttachment);
	v->FinishCreate();
	v->AddListener(new VReflector(this));
	v->FocusDraw();
	sts.min_size = (VSize){set->min_len*12,25};
	
	info = v;
	return nmp->add_child_do(this);
}

void
VEditText::destroy_do(VObject * nmp)
{
	nmp->remove_child_do(this);
	delete info;
	nmp->redraw();
}

VEditText::~VEditText()
{
}


VExError
VEditText::get_status(VObjectStatus *s, int flags) const
{
	V_OP_START_EX
	VExError err = v_get_status_standard(s, &flags, info);

	if ( (flags & VSF_VALUE) && sts.attr == integer_only ) {
		s->value = info->GetValue();
		flags &= ~VSF_VALUE;
	}
	if ( flags & VSF_DESC ) {
		long len = 1024, len2;
		char * buf = (char*)d_alloc(len);
		while ( 1 ) {
			((CEditText*)info)->GetText(buf, len, &len2);
			if ( len > len2 )
				break;
			len += 1024;
			buf = (char*)d_re_alloc(buf, len);
		}
		buf[len2] = 0;
		s->descriptor = l_string(std_cm, buf);
		d_f_ree(buf);
		flags &= ~VSF_DESC;
	}
	err.subcode1 = flags;

	VExError err2 = VObject::get_status(s,flags);
	if ( err2.code )
		err = merge_VExError_vstatus_type(err,err2);
	V_OP_END
	return err;
};

VExError
VEditText::set_status(const VObjectStatus *s, int flags)
{
	V_OP_START_EX
	VExError err = v_set_status_standard(s, flags, &sts, info);
	VExError err2 = VObject::set_status(s,flags);
	if ( err2.code )
		err = merge_VExError_vstatus_type(err,err2);
	
	if ( flags & VSF_CALC_MIN ) {
		// min width must be specified by the user.
		// min height is specified automatically on creation.
		err.subcode1 &= ~VSF_CALC_MIN;
	}

	if ( (flags & VSF_VALUE) && sts.attr == integer_only ) {
		((CEditText*)info)->SetValue(s->value);
		value_changed();
		err.subcode1 &= ~VSF_VALUE;
	}
	if ( flags & VSF_DESC ) {
		if ( s->descriptor ) {
			char *buf = n_string(std_cm, const_cast<L_CHAR*>(s->descriptor));
			((CEditText*)info)->SetText(buf, strlen(buf));
		}
		else {
			((CEditText*)info)->SetText(0, 0);
		}
		descriptor_changed();
		err.subcode1 &= ~VSF_DESC;
	}
	if ( flags & (VSF_WS | VSF_FSIZE) ) {
		int size = 0;
		VFONT *vf = 0;
		if ( sts.ws ) {
			long len = 1024;
			char * buf = (char*)d_alloc(len);
			((CEditText*)info)->GetText(buf, len-1, &len);
			buf[len] = 0;
			L_CHAR *lc = l_string(std_cm, buf);
			d_f_ree(buf);
			vf = _v_set_descriptor_get_font(lc, sts.ws, sts.fsize, &size);
		}
		if ( size <= 0 )
			size = sts.fsize;
		
		TEHandle teh = ((CEditText*)info)->GetMacTEH();

		if ( vf )
			(*teh)->txFont = vf->id;
		if ( size >= 0 )
			(*teh)->txSize = size / 10;

		FMInput	fontSpecs;
		fontSpecs.family	= (*teh)->txFont;
		fontSpecs.size		= (*teh)->txSize;
		fontSpecs.face		= (*teh)->txFace;
		fontSpecs.needBits	= false;
		fontSpecs.device	= 0;
		fontSpecs.numer.h	= 1;
		fontSpecs.numer.v	= 1;
		fontSpecs.denom.h	= 1;
		fontSpecs.denom.v	= 1;

		FMOutPtr	info = ::FMSwapFont(&fontSpecs);

		(*teh)->lineHeight = (SInt16) (info->ascent + info->descent + info->leading);
		(*teh)->fontAscent = info->ascent;
		
		sts.min_size.h = (*teh)->lineHeight + 7;
		sts.min_size.w = info->widMax * min_len;

		::TECalText(teh);

		err.subcode1 &= ~(VSF_WS | VSF_FSIZE);
	}

	V_OP_END

	if ( flags & (VSF_ALIGN | VSF_PADDING | VSF_DESC | VSF_VERTD | VSF_WS | VSF_FSIZE | VSF_VISIBLE) )
		VLayout::mark(this);
	return err;
}

void
VEditText::redraw(VRect* rect) const
{
	_V_OP_START()
	if ( rect )
		info->RefreshRect((Rect){rect->t, rect->l, rect->b, rect->r});
	else
		info->Refresh();
	V_OP_END
}

void
VEditText::focus()
{
	LCommander::SwitchTarget((CEditText*)info);
}

bool
VEditText::obey_command(int type)
{
	return ((CEditText*)(info))->ObeyCommand(type, 0);
}


bool
VEditText::command_status(int type) const
{
	Boolean enabled, usesMark;
	UInt16 mark;
	Str255 name;
	((CEditText*)(info))->FindCommandStatus(type, enabled, usesMark, mark, name);
	return enabled;
}
