/*	TViewDeclForm.cpp
 *
 *		Project form management
 */

/*  YAAF Constructor - Rapid application development for YAAF
 *  Copyright (C) 1998 William Edward Woody
 *  
 *  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.
 *  
 *  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 <XAppMenu.h>
#include <XWindow.h>
#include <XDocument.h>
#include <XMessages.h>
#include <XPopup.h>
#include "TResDocument.h"
#include "TConstructorApp.h"
#include "TViewDeclForm.h"
#include "LayoutViews.h"
#include "Inspector.h"


/************************************************************************/
/*																		*/
/*	Construction/Destruction											*/
/*																		*/
/************************************************************************/

/*	TViewDeclForm::TViewDeclForm
 *
 *		Construct me. Doesn't do much
 */

TViewDeclForm::TViewDeclForm()
{
}

/*	TViewDeclForm::~TViewDeclForm
 *
 *		Destruct me. Doesn't do much
 */

TViewDeclForm::~TViewDeclForm()
{
	TInspectorForm::SetInspectorSelect(NULL,fViewDecl->window,NULL);
	fViewDecl->window = NULL;
}

/*	TViewDeclForm::InitWindow
 *
 *		Intialize the window
 */

void TViewDeclForm::InitWindow(XGWindow *w, ViewDeclaration *vt)
{
	XGDocument *doc;
	TListView *lview;
	TRectEditView *rview;
	
	// Initialize view type pointer
	fViewDecl = vt;
	
	// Initialize window
	doc = w->GetDocument();
	doc->AttachWindow(w,true);
	SetWindowName(w);
	
	lview = dynamic_cast<TListView *>(w->FindViewByID(2));
	lview->InitViewEdit(vt);
	
	rview = dynamic_cast<TRectEditView *>(w->FindViewByID(3));
	rview->InitViewEdit(vt);
}

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

BEGINFORM(TViewDeclForm)
	DISPATCHFORM(KXGEventDocRenamed,TViewDeclForm::DoDocRenamed)
	DISPATCHFORM(KSelectViewItem,TViewDeclForm::DoSelectViewItem)
	DISPATCHFORM(KXGDoMenuCommand,TViewDeclForm::DoMenuCommand)
	DISPATCHFORM(KXGGetMenuStatus,TViewDeclForm::DoGetMenuStatus)
	DISPATCHFORM(KUpdateViewItem,TViewDeclForm::DoUpdateViewItem)
	DISPATCHFORM(KXGAppPeriodic,TViewDeclForm::DoAppPeriodic)
ENDFORM

/*	TViewDeclForm::DoAppPeriodic
 *
 *		Handle the periodic event by letting the inspector window know
 *	who is on first
 */

long TViewDeclForm::DoAppPeriodic(XGWindow *w, long, void *)
{
	TListView *lview;
	TResDocument *doc;
	
	lview = dynamic_cast<TListView *>(w->FindViewByID(2));
	doc = dynamic_cast<TResDocument *>(w->GetDocument());	
	TInspectorForm::SetInspectorSelect(doc,w,lview->GetSelect());
	return -1;
}
	

/*	TViewDeclForm::DoUpdateViewItem
 *
 *		Update this thing
 */

long TViewDeclForm::DoUpdateViewItem(XGWindow *w, long, void *)
{
	TRectEditView *rview;
	TListView *lview;
	TResDocument *doc;
	ViewItemDeclaration *item;
	
	rview = dynamic_cast<TRectEditView *>(w->FindViewByID(3));
	rview->UpdateContent();
	lview = dynamic_cast<TListView *>(w->FindViewByID(2));
	lview->UpdateContent();
	
	/*
	 *	Update the inspector if it needs updating
	 */
	
	item = lview->GetSelect();
	doc = dynamic_cast<TResDocument *>(w->GetDocument());
	TInspectorForm::UpdateInspector(doc,w,item);
	
	return 0;
}

/*	TViewDeclForm::DoDocRenamed
 *
 *		handle renaming this document by renaming this window
 */

long TViewDeclForm::DoDocRenamed(XGWindow *w, long, void *)
{
	SetWindowName(w);
	return 0;
}

/*	TViewDeclForm::DoSelectViewItem
 *
 *		Change the current selection. This tells the various
 *	components of this window that the view has changed.
 */

long TViewDeclForm::DoSelectViewItem(XGWindow *w, long, void *data)
{
	TListView *lview;
	TRectEditView *rview;
	
	lview = dynamic_cast<TListView *>(w->FindViewByID(2));
	lview->UpdateSelect((ViewItemDeclaration *)data);

	rview = dynamic_cast<TRectEditView *>(w->FindViewByID(3));
	rview->UpdateSelect((ViewItemDeclaration *)data);
	return -1;
}

/*	TViewDeclForm::DoMenuCommand
 *
 *		Process menu command
 */

long TViewDeclForm::DoMenuCommand(XGWindow *w, long arg, void *)
{
	TListView *lview;
	
	switch (arg) {
		case KMenuResourceCreate:
			lview = dynamic_cast<TListView *>(w->FindViewByID(2));
			if (lview) {
				lview->AddNewView();
			}
			return 0;
		case KMenuResourceEdit:
			return 0;
		case KXGEditClear:
			lview = dynamic_cast<TListView *>(w->FindViewByID(2));
			if (lview) {
				lview->DeleteView();
			}
			return 0;
	}
			
	return -1;
}

/*	TViewDeclForm::DoGetMenuStatus
 *
 *		Get menu status
 */

long TViewDeclForm::DoGetMenuStatus(XGWindow *, long arg, void *parg)
{
	XGSMenuStatus *status = (XGSMenuStatus *)parg;
	switch (arg) {
		case KMenuResourceCreate:
		case KMenuResourceEdit:
		case KXGEditClear:
			status->enable = true;
			return 0;
	}
			
	return -1;
}

/************************************************************************/
/*																		*/
/*	Internals															*/
/*																		*/
/************************************************************************/

/*	TViewDeclForm::SetWindowName
 *
 *		Set the name of this window based on the document's name
 */

void TViewDeclForm::SetWindowName(XGWindow *w)
{
	char buffer[256];
	char docname[256];
	XGDocument *doc;
	
	doc = w->GetDocument();
	if (doc) {
		doc->GetDocumentName(docname);
		sprintf(buffer,"%s: %s",docname,fViewDecl->viewResName);
		w->SetTitle(buffer);
	}
}
