/*	XGInlineEdit.cpp
 *
 *		inline editing of this thing
 */

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

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

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

#include <XConfig.h>			// must be first to optimize compile on MSVC++
#include <string.h>
#include <XDataUtil.h>
#include <XInline.h>
#include <XMessages.h>
#include <XWindow.h>

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

/*$	XGInlineEdit::XGInlineEdit
 *
 *		Inline editing support
 */

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

/*$	XGInlineEdit::~XGInlineEdit
 *
 *		Delete me
 */

XGInlineEdit::~XGInlineEdit()
{
}

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

/*$	XGInlineEdit::Create
 *
 *		Create one of me. This creates the view in the parent's window, 
 *	not in the parent view. This has the advantage of making this thing
 *	appear regardless of the clipping boundaries of the parent, which is
 *	exactly what I want...
 */

XGInlineEdit *XGInlineEdit::Create(XGView *parent, 
								   XRect &where,
								   short font,
								   char *text,
								   bool wrap)
{
	XGSEditInitRecord einit;
	XGInlineEdit *iedit;
	XRect r;
	
	r = where;
	parent->ViewToGlobal(&r);
	parent->GetWindow()->GlobalToView(&r);
	
	einit.password = false;
	einit.maxchars = 256;
	einit.chartype = KXGEAnyChar;
	einit.wordwrap = wrap;
	einit.autoindent = false;
	einit.editor = false;
	einit.tabstop = 4;
	einit.noframe = false;

	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.fVisible = true;
	einit.v.v.fEnabled = true;
	einit.v.v.fTabStop = false;
	einit.v.v.fAutoLeft = r.left-3;
	einit.v.v.fAutoTop = r.top-3;
	einit.v.v.fAutoRight = r.right+3;
	einit.v.v.fAutoBottom = r.bottom+3;
		
	iedit = new XGInlineEdit(parent->GetWindow(),einit);
	iedit->fParentEvent = parent;
	iedit->SetFocus();
	return iedit;
}

/*	XGInlineEdit::DeleteSelf
 *
 *		Self-destruction support; this invalidates the area around me to
 *	make sure things get drawn correctly.
 */

void XGInlineEdit::DeleteSelf(bool adjfocus)
{
	XRect r;
	XGView *v;
	
	r = GetContentRect();
	ViewToGlobal(&r);
	InsetRect(&r,-1,-1);
	v = GetWindow();
	v->GlobalToView(&r);
	v->InvalView(&r);
	
	/*
	 *	Make sure the focus is changed to my parent if I had it
	 *	before.
	 */
	
	if (HasFocus() && adjfocus) {
		v = fParentEvent;
		delete this;
		v->SetFocus();
	} else {
		delete this;
	}
}

/*	XGInlineEdit::GetParentDispatch
 *
 *		Get the dispatch parent. This is different from the view parent
 *	for this view; that enables me to host my editor display in the
 *	root window, yet send events to the "container view"
 */

XGDispatch *XGInlineEdit::GetParentDispatch()
{
	if (fParentEvent) return fParentEvent;
	return XGView::GetParentDispatch();
}

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

/*$	XGInlineEdit::DoActivate
 *
 *		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)) {
			DeleteSelf();
		}
	}
}

/*$	XGInlineEdit::DoLoseFocus
 *
 *		Lose focus.
 */

void XGInlineEdit::DoLoseFocus()
{
	XGSInline i;
	
	i.edit = this;
	DoDispatch(KXGInlineClose,GetRefNum(),&i);
	DeleteSelf(false);
}

/************************************************************************/
/*																		*/
/*	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)) {
		fParentEvent->SetFocus();
		return 0;
	}
	return -1;
}

