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

/*  XT - An example text editor application built with the YAAF library
 *
 *	PUBLIC DOMAIN: The source kit of 'xt' has been placed into the
 *	public domain by the author, William Edward Woody, on the hope
 *	that the source kit will provide a useful example of using the
 *	YAAF Library.
 *  
 *  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 <XTextControls.h>
#include <XButtons.h>

#include "TTextEditApp.h"
#include "TEditView.h"
#include "TEditForm.h"

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

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

TTextEditApp::TTextEditApp() : XGAppMultiWindow()
{
	RegisterView(XGScrollView);	
	RegisterView(TEditView);
	RegisterView(XGEditText);
	RegisterView(XGStaticText);
	RegisterView(XGListControl);
	RegisterView(XGPopControl);
	RegisterView(XGCommandButton);
	RegisterView(XGCheckButton);
	RegisterWindow(XGDialog);
}

/*	TTextEditApp::~TTextEditApp
 *
 *		Destroy me
 */

TTextEditApp::~TTextEditApp()
{
}

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

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

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

long TTextEditApp::DoGetMenuStatus(long arg, void *parg)
{
	XGSMenuStatus *status = (XGSMenuStatus *)parg;
	
	switch (arg) {
		case KMenuFileNew:
		case KMenuFileOpen:
			status->enable = true;
			return 0;
	}
	return -1;
}

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

long TTextEditApp::DoMenuCommand(long arg, void *)
{
	switch (arg) {
		case KMenuFileNew:
			NewWindow();
			return 0;
		case KMenuFileOpen:
			OpenWindow();
			return 0;
	}
	return -1;
}

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

long TTextEditApp::DoOpenApplication(long, void *)
{
	NewWindow();
	return 0;
}

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

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

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

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

void TTextEditApp::NewWindow()
{
	autoptr<TEditForm> form;
	autoptr<TEditDoc> doc;
	autoptr<XGWindow> w;
	TEditDoc *docptr;
	TEditView *v;
	char buffer[256];
	
	/*
	 *	First, create the empty document
	 */
	
	doc = TEditDoc::NewDocument();
	if (doc.Ptr() == NULL) return;
	
	/*
	 *	Next, create the form
	 */
	
	form = new TEditForm;
	
	/*
	 *	Create the window
	 */
	
	w = XGWindowFactory::CreateFromResource(128,
											docptr = doc.Return(),
											form.Return());
	
	/*
	 *	And wrap up the initialization
	 */
	
	v = dynamic_cast<TEditView *>(w->FindViewByID(2));
	v->InitEdit(docptr->GetText());
	v->SetFocus();
	docptr->GetDocumentName(buffer);
	w->SetTitle(buffer);
	docptr->AttachWindow(w.Ptr(),false);
	w.Detach();
}

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

#if OPT_MACOS
void TTextEditApp::OpenWindow(FSSpec *filename)
#else
void TTextEditApp::OpenWindow(char *filename)
#endif
{
	autoptr<TEditForm> form;
	autoptr<TEditDoc> doc;
	autoptr<XGWindow> w;
	TEditDoc *docptr;
	TEditView *v;
	char buffer[256];
	
	/*
	 *	First, create the empty document
	 */
	
	if (filename) doc = TEditDoc::OpenDocument(filename);
	else doc = TEditDoc::OpenDocument();
	if (doc.Ptr() == NULL) return;
	
	/*
	 *	Next, create the form
	 */
	
	form = new TEditForm;
	
	/*
	 *	Create the window
	 */
	
	w = XGWindowFactory::CreateFromResource(128,
											docptr = doc.Return(),
											form.Return());
	
	/*
	 *	And wrap up the initialization
	 */
	
	v = dynamic_cast<TEditView *>(w->FindViewByID(2));
	v->InitEdit(docptr->GetText());
	v->SetFocus();
	docptr->GetDocumentName(buffer);
	w->SetTitle(buffer);
	docptr->AttachWindow(w.Ptr(),false);
	w.Detach();
}
