/*	TConstructorApp.cpp
 *
 *		The edit application class declaration
 */

/*  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 <XMessages.h>
#include <XScroll.h>
#include <XPrinter.h>
#include <XDialog.h>
#include <XAutoPtr.h>
#include <XList.h>
#include <XPopup.h>
#include <XPreferences.h>

#include <XTextControls.h>
#include <XButtons.h>

#include "TConstructorApp.h"
#include "TResDocument.h"
#include "TProjectForm.h"

#include "TViewTypeList.h"
#include "TProjectView.h"
#include "LayoutViews.h"
#include "TErrorView.h"
#include "Inspector.h"

/************************************************************************/
/*																		*/
/*	Globals																*/
/*																		*/
/************************************************************************/

bool		GSnapGridFlag;
short		GSnapGridSize;

/************************************************************************/
/*																		*/
/*	Application Core													*/
/*																		*/
/************************************************************************/

/*	TConstructorApp::TConstructorApp
 *
 *		The main entry point for constructing me; this knows how to
 *	initialize things
 */

TConstructorApp::TConstructorApp() : XGAppMultiWindow("YAAF Constructor")
{
	RegisterView(XGScrollView);	
	RegisterView(XGEditText);
	RegisterView(XGGroupBox);
	RegisterView(XGStaticText);
	RegisterView(XGListControl);
	RegisterView(XGPopControl);
	RegisterView(XGCommandButton);
	RegisterView(XGCheckButton);
	RegisterWindow(XGDialog);
	
	RegisterView(TViewTypeList);
	RegisterView(TProjectView);
	RegisterView(TBackgroundView);
	RegisterView(TItemListView);
	RegisterView(TListView);
	RegisterView(TRectEditView);
	RegisterView(TErrorView);
	
	LoadPreferences();
}

/*	TConstructorApp::~TConstructorApp
 *
 *		Destroy me
 */

TConstructorApp::~TConstructorApp()
{
	SavePreferences();
}

/************************************************************************/
/*																		*/
/*	Event Processing													*/
/*																		*/
/************************************************************************/

BEGINDISPATCH(TConstructorApp,XGAppMultiWindow)
	DISPATCHTABLE(KXGGetMenuStatus,TConstructorApp::DoGetMenuStatus)
	DISPATCHTABLE(KXGDoMenuCommand,TConstructorApp::DoMenuCommand)
	DISPATCHTABLE(KXGOpenApplication,TConstructorApp::DoOpenApplication)
	DISPATCHTABLE(KXGOpenDocument,TConstructorApp::DoOpenDocument)
ENDDISPATCH

/*	TConstructorApp::DoGetMenuStatus
 *
 *		Get the menus tatus
 */

long TConstructorApp::DoGetMenuStatus(long arg, void *parg)
{
	XGSMenuStatus *status = (XGSMenuStatus *)parg;
	
	switch (arg) {
		case KMenuFileNew:
		case KMenuFileOpen:
		case KMenuEditPrefs:
			status->enable = true;
			return 0;
		case KMenuWindowInspector:
			status->enable = true;
			if (TInspectorForm::IsInspectorWindowOpen()) {
				status->check = true;
			}
			return 0;
	}
	return -1;
}

/*	TConstructorApp::DoMenuCommand
 *
 *		Handle the menu commands
 */

long TConstructorApp::DoMenuCommand(long arg, void *)
{
	switch (arg) {
		case KMenuFileNew:
			NewWindow();
			return 0;
		case KMenuFileOpen:
			OpenWindow();
			return 0;
		case KMenuEditPrefs:
			RunPreferences();
			return 0;
		case KMenuWindowInspector:
			if (TInspectorForm::IsInspectorWindowOpen()) {
				TInspectorForm::CloseInspectorWindow();
			} else {
				TInspectorForm::OpenInspectorWindow();
			}
			return 0;
	}
	return -1;
}

/*	TConstructorApp::DoOpenApplication
 *
 *		Handle open application event by creating a new, empty window
 */

long TConstructorApp::DoOpenApplication(long, void *)
{
#if OPT_XWIN				// I must throw up *something*...
	NewWindow();
#endif

	return 0;
}

/*	TConstructorApp::DoOpenDocument
 *
 *		Handle open document event by creating a new, empty window
 */

long TConstructorApp::DoOpenDocument(long, void *parg)
{
#if OPT_MACOS
	OpenWindow((FSSpec *)parg);
#else
	OpenWindow((char *)parg);
#endif
	return 0;
}

/************************************************************************/
/*																		*/
/*	Commands															*/
/*																		*/
/************************************************************************/

/*	TConstructorApp::NewWindow
 *
 *		Create a new window.
 */

void TConstructorApp::NewWindow()
{
	XGWindow *w;
	TResDocument *doc;
	char buffer[256];
	TProjectForm *form;
	XGView *view;
	
	// First, construct the document
	doc = TResDocument::NewDocument();
	if (!doc) return;					// no document?
	
	// Next, construct the form
	form = new TProjectForm;
	
	// And last, construct the window
	w = XGWindowFactory::CreateFromResource(128,doc,form);
	doc->AttachWindow(w,false);
	
	// Initialize window
	doc->GetDocumentName(buffer);
	w->SetTitle(buffer);
	view = w->FindViewByID(2);
	doc->AddReceiver(view);
	view->SetFocus();
}

/*	TConstructorApp::OpenWindow
 *
 *		Open an existing window.
 */

#if OPT_MACOS
void TConstructorApp::OpenWindow(FSSpec *filename)
#else
void TConstructorApp::OpenWindow(char *filename)
#endif
{
	XGWindow *w;
	TResDocument *doc;
	TProjectForm *form;
	char buffer[256];
	XGView *view;
	
	/*
	 *	First, construct the document
	 */
	
	if (filename) doc = TResDocument::OpenDocument(filename);
	else doc = TResDocument::OpenDocument();
	if (!doc) return;
	
	/*
	 *	Next, create the form
	 */
	
	form = new TProjectForm;
	
	/*
	 *	Create window
	 */
	
	w = XGWindowFactory::CreateFromResource(128,doc,form);
	doc->AttachWindow(w,false);
	
	/*
	 *	Initialize window
	 */
	
	doc->GetDocumentName(buffer);
	w->SetTitle(buffer);
	view = w->FindViewByID(2);
	doc->AddReceiver(view);
	view->SetFocus();
}

/************************************************************************/
/*																		*/
/*	Preferences															*/
/*																		*/
/************************************************************************/

/*	TConstructorApp::RunPrefDlog
 *
 *		Run the preferences dialog
 */

long TConstructorApp::RunPrefDlog(XGDialog *dlog, void *, long msg, long arg, void *)
{
	switch (msg) {
		case KXGDialogMessage:
			switch (arg) {
				case KXGDMInitDialog:
					dlog->SetValue(1,GSnapGridFlag ? true : false);
					dlog->SetValue(2,GSnapGridSize);
					return -1;
				default:
					break;
			}
			break;
			
		case KXGControlClick:
			switch (arg) {
				case 1:					// 'snap?'
					dlog->SetValue(1,(dlog->GetValue(1)) ? 0 : 1);
					return 0;
				case 101:				// cancel
					dlog->EndDialog(0);
					return 0;
				case 100:				// ok
					GSnapGridFlag = (dlog->GetValue(1)) ? true : false;
					GSnapGridSize = dlog->GetValue(2);
					if ((GSnapGridSize < 2) || (GSnapGridSize > 32)) {
						dlog->FindViewByID(2)->SetFocus();
						return 0;
					}
					dlog->EndDialog(1);
					return 0;
				default:
					break;
			}
			break;
		
		default:
			break;
	}
	
	return -1;
}

/*	TConstructorApp::RunPreferences
 *
 *		Run the preferences dialog
 */

void TConstructorApp::RunPreferences()
{
	XGDialog::ModalDialog(134,RunPrefDlog,NULL);
}

/*	TContructorApp::LoadPreferences
 *
 *		Load the preferences from the preferences file
 */

void TConstructorApp::LoadPreferences()
{
#if OPT_MACOS
	XGPreferences pref("YAAFCon Preferences",'YCON');
#endif

#if OPT_WINOS
	XGPreferences pref("YAAF","YAAF Constructor","1.0");
#endif

#if OPT_XWIN
	XGPreferences pref("YAAFConRC");			// ~/.YAAFConRC
#endif
	char buffer[256];
	
	GSnapGridFlag = true;
	GSnapGridSize = 8;
	
	pref.GetString("GSnapGridFlag",buffer);
	if (buffer[0]) {
		GSnapGridFlag = ((buffer[0] == 't') || (buffer[0] == 'T'));
		GSnapGridSize = pref.GetInteger("GSnapGridSize");
		if (GSnapGridSize < 2) GSnapGridSize = 2;
		if (GSnapGridSize > 32) GSnapGridSize = 32;
	}
}

/*	TContructorApp::SavePreferences
 *
 *		Save the preferences to the preferences file
 */

void TConstructorApp::SavePreferences()
{
#if OPT_MACOS
	XGPreferences pref("YAAFCon Preferences",'YCON');
#endif

#if OPT_WINOS
	XGPreferences pref("YAAF","YAAF Constructor","1.0");
#endif

#if OPT_XWIN
	XGPreferences pref("YAAFConRC");			// ~/.YAAFConRC
#endif

	pref.SetBoolean("GSnapGridFlag",GSnapGridFlag);
	pref.SetInteger("GSnapGridSize",GSnapGridSize);
}
