/////////////////////////////////////////////////////////////////////////////
// Name:        No names yet.
// Purpose:     Contrib. demo
// Author:      Aleksandras Gluchovas
// Modified by:
// Created:     3/01/99
// RCS-ID:      $Id: XCreationEditor.cpp,v 1.3 2001/01/29 13:21:00 georgetasker Exp $
// Copyright:   (c) No Magic, Inc.
// Licence:   	wxWindows license
/////////////////////////////////////////////////////////////////////////////
 
#ifdef __GNUG__
#pragma implementation "objstore.h"
// #pragma interface
#endif

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "XCreationEditor.h"

/***** Implementaton for class XCreationEditor *****/

void XCreationEditor::Init( XCollection* pScene, XObject* pObject )
{
	mpObject = pObject;
	mpPrevEditor = pScene->HasEditor() ? &pScene->GetEditor() : NULL;

	pScene->UnlinkEditor();

	pScene->SetEditor( this );
}

XCreationEditor::~XCreationEditor()
{
	if ( mpObject ) CancelCreation();
}

bool XCreationEditor::OnMouseDown( const XPoint& pos )
{
	XRect r = mpObject->GetBounds();

	r.x = pos.x;
	r.y = pos.y;

	mpObject->SetBounds( r );

	XCollection* pScene = (XCollection*)&GetParent();

	pScene->Insert( mpObject );

	mpObject->NotifyAdded( mpObject );

	// restore previous scene editor 

	((XObject*)pScene)->SetSelection( (XObject&)(*mpObject) );

	DetachFromScene();

	mpObject = NULL;

	delete this; // destroy ourselves!

	return TRUE;
}

bool XCreationEditor::OnMouseMove( const XPoint& pos )
{ 
	return TRUE; // "eat" movement events
}

bool XCreationEditor::OnRightClick( const XPoint& pos )
{
	CancelCreation();

	return TRUE;
}

void XCreationEditor::CancelCreation()
{
	DetachFromScene();

	delete mpObject;

	mpObject = NULL;

	delete this; // destroy ourselves!
}

void XCreationEditor::DetachFromScene()
{
	XCollection* pScene = (XCollection*)&GetParent();

	pScene->UnlinkEditor();

	pScene->SetEditor( mpPrevEditor );
}