/*****************************************************************************/
/*===========================================================================*/
/*=                                                                         =*/	
/*=                                                                         =*/
/*=   Trinomic presents:                                                    =*/
/*=                                                                         =*/	
/*=                                                                         =*/
/*=    PC-Heini  Release 4                                                  =*/
/*=                                                                         =*/	
/*=    den ultimativen Textanzeiger                                         =*/
/*=    Jetzt mit objektorientiertem Quelltext!!!                            =*/
/*=                                                                         =*/
/*=    ©1995 - 1996 by Ctryxx/Trinomic (Stephan Quitzsch)                   =*/
/*=                                                                         =*/
/*=    Write any comments to: wir95ccv@manitu.rz.uni-leipzig.de             =*/
/*=                                                                         =*/	
/*=                                                                         =*/
/*===========================================================================*/
/*****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <stat.h>

#include "exec/types.h"
#include "exec/memory.h"

#include "dos/dos.h"
#include "proto/dos.h"

#include "libraries/asl.h"
#include "proto/asl.h"

#include "intuition/intuition.h"
#include "proto/intuition.h"

#include "graphics/gfx.h"
#include "proto/graphics.h"

#include "libraries/mui.h"
#include "proto/muimaster.h"

/*============================================================================*/

#define PCH_VERS "PC-Heini V0.56 by Ctryxx"
#define PCH_VSTRING "$VER: PC-Heini V0.56 (02.08.96)"
#define PCH_USAGE "USAGE: PCH [-h|-?|?] | [-n] [-l <TextFile>|<TextFile>]"

/*============================================================================*/

BOOL initclasses( void );			// function prototypes
void exitclasses( void );
BOOL initlibs( void );
void exitlibs( void );
void ctryxxerror( char *str_error );
void shutdown( char *str_error );

#include "ctryxxclasses.h"			// custom classes
#include "textwd.h"
#include "popgroup.h"
#include "mainwindow.h"

/*============================================================================*/

					__stack = 8192;				// !!!!!!!!!!!!!!!!!

APTR		mui_app1;					// Application Object
APTR		mui_window;				// Main Window

/*============================================================================*/

BOOL initclasses( void )				// Create Custom Classes
{
	CL_Mainwin  = MUI_CreateCustomClass( NULL, MUIC_Window  , NULL, sizeof(struct Mainwin_Data  ), Mainwin_Dispatcher  );
	CL_Popgroup = MUI_CreateCustomClass( NULL, MUIC_Group   , NULL, sizeof(struct Popgroup_Data ), Popgroup_Dispatcher );
	CL_Textwd	  = MUI_CreateCustomClass( NULL, MUIC_Listview, NULL, sizeof(struct Textwd_Data   ), Textwd_Dispatcher   );

	if( CL_Mainwin && CL_Popgroup && CL_Textwd ) return TRUE;
	else return FALSE;
}

void exitclasses( void )				// Free Classes
{
	if ( CL_Textwd   ) MUI_DeleteCustomClass( CL_Textwd   );
	if ( CL_Popgroup ) MUI_DeleteCustomClass( CL_Popgroup );
	if ( CL_Mainwin  ) MUI_DeleteCustomClass( CL_Mainwin  );
}

BOOL initlibs( void )				// Open Libs
{
	MUIMasterBase = (struct Library *) OpenLibrary( "muimaster.library", 11 );

	if( MUIMasterBase ) return TRUE;
	else return FALSE;
}

void exitlibs( void )				// Close Libs
{
	if ( MUIMasterBase ) CloseLibrary( MUIMasterBase );
}

/*===========================================================================*/

void ctryxxerror( char *str_error )	// Print Error Message
								// If Requester available...
{	if( MUIMasterBase )
	{	MUI_RequestA( 0l, 0l, 0l, "An Error came up!", "Ok|Cancel", str_error, 0 );
	}
	else							// If not...
	{	printf( "%s\n", str_error );
	}
}

/*============================================================================*/

void shutdown( char *str_error )		// End Program

{	if ( str_error ) ctryxxerror( str_error );
	if ( mui_app1  ) MUI_DisposeObject( mui_app1 );

	exitclasses();
	exitlibs();
	exit( TRUE );					// Exit
}

/*===========================================================================*/




/*===========================================================================*/

int main( int argc, char *argv[] )
{
	BOOL running = TRUE;			// Exit if FALSE

	if( argc>1 )					// Get Arguments
	{	int i;
		for ( i=1; i<argc; i++ )
		{	if( argv[i][0] == '-' )
			{	switch( argv[i][1] )
				{	case 'h':
					case '?':
						printf( "\n%s\n\n%s\n", PCH_VERS, PCH_USAGE );
						shutdown( 0l );
					break;
				}
			}
			if( argv[i][0] == '?' )
			{	printf( "\n%s\n\n%s\n", PCH_VERS, PCH_USAGE );
				shutdown( 0l );
			}
		}
	}


	if( !(initlibs()   ) ) shutdown( "Can't open libraries!"     );		// Open Resources
	if( !(initclasses()) ) shutdown( "Can't initialise classes!" );

	mui_app1 = ApplicationObject,									// Open Application Object
		MUIA_Application_Title   , "PC-Heini",
      	MUIA_Application_Version    , PCH_VSTRING,
      	MUIA_Application_Copyright  , "©1995-1996, Ctryxx/Trinomic",
      	MUIA_Application_Author   , "Stephan Quitzsch",
      	MUIA_Application_Description, "MUI-Textviewer",
      	MUIA_Application_Base    , "PCHEINI",
	End;
	if( !mui_app1 ) shutdown( "Can't open MUI-Application!" );


	if( !(mui_window = NewObject( CL_Mainwin->mcc_Class, NULL, TAG_DONE )) ) shutdown( "Can't open Mainwindow!" );

	DoMethod( mui_app1, OM_ADDMEMBER, mui_window );

	if(argc>1)					// Get the rest of Argument Line
	{	int i;
		for ( i=1; i<argc; i++ )
		{	if( argv[i][0] == '-' )
			{	switch( argv[i][1] )
				{	case 'l':
						DoMethod( mui_window, MUIM_Textwd_Load, argv[i+1] );
					break;

					case 'n':
						DoMethod( mui_window, MUIM_Popgroup_Pop, "u" );
					break;

					default:
						printf( "unknown option: %s\n", argv[i] );
						shutdown(0l);
					break;
				}
			}
			else
			{	DoMethod( mui_window, MUIM_Textwd_Load, argv[i] );
			}
		}
	}
	set( mui_window, MUIA_Window_Open, TRUE );

	while ( running )
	{	ULONG signals;

		switch ( DoMethod(mui_app1, MUIM_Application_Input, &signals) )
		{
			case MUIV_Application_ReturnID_Quit:	// Quit?
          		running = FALSE;
        		break;
		}
		if( running && signals ) Wait( signals );
	}

	shutdown( 0l );							// Exit
}
