// COPYRIGHT (C) 1990-1999.  All Rights Reserved. - HELLO2.CPP
// Zinc Software Incorporated.  Pleasant Grove, Utah  USA
// May be freely copied, used and distributed.

#include <zaf.hpp>
#include <z_prst.hpp>

int ZafApplication::Main(void)
{
	// This line is only needed for linkers that don't automatically look
	// for unresolved references to main() or WinMain() inside of libraries.
	// (main() or WinMain() is found in a ZAF library)
	LinkMain();

	// Create a storage object for reading the data file.
	// (This is the low level mechanism for talking to the disk file.)
	// (The optional ZAF_ITEXT macro guarantees unicode compatibility.)
	ZafStorage *storage = new ZafStorage(ZAF_ITEXT("hello2.znc"), ZAF_FILE_READ);
	if (storage->Error())
	{
		zafErrorSystem->ReportError(ZAF_NULLP(ZafWindowObject),
			ZAF_ITEXT("File Error"), ZAF_DIALOG_OK,
			ZAF_ITEXT("Error: hello2.znc could not be found."));
		delete storage;
		return (-1);
	}

	// Create a persistence object for loading the hello window.
	// Persistence provides the link between persitable objects and an
	// abstract file system (ZafStorage in this case).
	// Important: Because ZafObjectPersistence currently maintains a large
	//   amount of static data, those with stack size limitations should use
	//   the "new" operator to allocate it in the heap.
	//
	// One other caveat; using the default constructors, zafDefaultObjectConstructor
	// and zafDefaultDataConstructor will pull *all* the persistence constructors
	// for all ZAF objects.  This may make your executable rather large.  Look at
	// hello3, or all other examples which use persistence, to see how to use just
 	// the minimum of persistence constructors.
	zafObjectPersistence = new ZafObjectPersistence(storage,
		zafDefaultDataConstructor, zafDefaultObjectConstructor);

	// Load the hello window from the data (.znc) file.
	ZafWindow *helloWindow = new ZafWindow(ZAF_ITEXT("HelloWindow"), *zafObjectPersistence);

	// Center the window on the screen.
	zafWindowManager->Center(helloWindow);

	// Attach the hello window to the window manager, or in other words,
	// make it appear on the screen.
	zafWindowManager->Add(helloWindow);

	// Process events.
	// (This functions passes events from the event manager to the
	// window manager until an S_EXIT is received or until there on no
	// more windows attached to the window manager.)
	Control();

	// Free the zafObjectPersistence allocated above.
	delete zafObjectPersistence;
	zafObjectPersistence = ZAF_NULLP(ZafObjectPersistence);
	delete storage;

	// Return an exit code.
	// (This is the exit code returned to the OS by the application.)
	return (0);
}
