/*	XGInlineEdit.cpp
 *
 *		inline editing of this thing
 */
 
/*  YAAF - Yet another application framework
 *  Copyright (C) 1997, 1998 William Edward Woody
 *  
 *  This library is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Library
 *  General Public License as published by the Free Software
 *  Foundation; either version 2 of the License, or any
 *  later version.
 *  
 *  This library is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied
 *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
 *  PURPOSE. See the GNU Library General Public License for
 *  more details.
 *  
 *  You should have received a copy of the GNU Library General
 *  Public License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *  
 *  To contact the author, either e-mail me at woody@pandawave.com, 
 *	or write to
 *  
 *          William Edward Woody
 *          In Phase Consulting
 *          1545 Ard Eevin Avenue
 *          Glendale, CA 91202
 *
 *	Or visit us on the web at http://www.pandawave.com
 */

#include <string.h>
#include <XDataUtil.h>
#include <XInlineEdit.h>
#include <XMessages.h>

/************************************************************************/
/*																		*/
/*	Standard Classes													*/
/*																		*/
/************************************************************************/

/*	@F"XGInlineEdit::XGInlineEdit" @I
 *
 *		Inline editing support
 */

XGInlineEdit::XGInlineEdit(XGOwner *parent, XGSEditInitRecord &init) :
	XGEditText(parent,init)
{
}

/*	@F"XGInlineEdit::~XGInlineEdit" @I
 *
 *		Delete me
 */

XGInlineEdit::~XGInlineEdit()
{
}

/************************************************************************/
/*																		*/
/*	Standard Classes													*/
/*																		*/
/************************************************************************/

/*	@F"XGInlineEdit::Create" @I
 *
 *		Create one of me. If I couldn't get the focus, then immediately
 *	destroy me and return.
 */

XGInlineEdit *XGInlineEdit::Create(XGView *parent, 
								   Rect &where,
								   short font,
								   char *text)
{
	XGSEditInitRecord einit;
	XGInlineEdit *iedit;
	
	einit.password = false;
	einit.maxchars = 256;
	einit.chartype = KXGEAnyChar;
	if (text) strcpy(einit.v.text,text);
	else einit.v.text[0] = 0;
	einit.v.font = font;
	einit.v.v.fViewType = 'iedt';
	einit.v.v.fViewID = 0;
	einit.v.v.fRefNum = -1;
	einit.v.v.fLockLeft = true;
	einit.v.v.fLockTop = true;
	einit.v.v.fLockRight = false;
	einit.v.v.fLockBottom = false;
	einit.v.v.fAutoLeft = where.left-3;
	einit.v.v.fAutoTop = where.top-3;
	einit.v.v.fAutoRight = where.right+3;
	einit.v.v.fAutoBottom = where.bottom+3;
	einit.v.v.fVisible = true;
	einit.v.v.fEnabled = true;
	einit.v.v.fTabStop = false;
	
	SetCTorDispatch(parent);	
	iedit = new XGInlineEdit(parent,einit);
	if (!(iedit->SetFocus())) {
		delete iedit;
		return NULL;
	}
	return iedit;
}

/************************************************************************/
/*																		*/
/*	Autodestruction														*/
/*																		*/
/************************************************************************/

/*	@F"XGInlineEdit::DoActivate" @I
 *
 *		Handle activation/deactivation by sending up a message. If on
 *	deactivation I can go away, then I do.
 */

void XGInlineEdit::DoActivate(bool a)
{
	XGSInline i;
	
	if (a) return;
	
	if (!a) {
		i.edit = this;
		if (1 != DoDispatch(KXGInlineClose,GetRefNum(),&i)) delete this;
	}
}

/*	@F"XGInlineEdit::CanLoseFocus" @I
 *
 *		Can I lose the focus?
 */

bool XGInlineEdit::CanLoseFocus()
{
	XGSInline i;
	
	i.edit = this;
	return 1 != DoDispatch(KXGInlineClose,GetRefNum(),&i);
}

/*	@F"XGInlineEdit::LoseFocus" @I
 *
 *		Lose focus.
 */

void XGInlineEdit::LoseFocus()
{
	delete this;
}

/************************************************************************/
/*																		*/
/*	Events																*/
/*																		*/
/************************************************************************/

BEGINDISPATCH(XGInlineEdit,XGEditText)
	DISPATCHTABLE(KXGKeyEvent,XGInlineEdit::DoKey)
ENDDISPATCH

/*	XGInlineEdit::DoKey
 *
 *		If the enter key is pressed, this sets the focus to my parent
 */

long XGInlineEdit::DoKey(long arg, void *)
{
	short c;
	short mods;
	
	mods = GETHIWORD(arg);
	c = GETLOWORD(arg);
	
	if ((0 == (mods & KSpecialKey)) && (c == 0x0D)) {
		GetParent()->SetFocus();
		return 0;
	}
	return -1;
}
