#include "WAinclude.h"
#define D(x)
#include "vsion.h"

struct Library * MUIMasterBase;
struct Library * ImagePoolBase;
struct MUI_CustomClass * DTImage;

/** Hooks for about window and mui about */

extern const struct Hook aboutmuiHook, aboutHook, debugHook;
#include "menues.h"

int main(int argc,char ** argv)
{
	
	/** A few variables. ctrlwin = main window, app = application object
		menustrip = the programs menue, titlewin = the 'open while loading' window.
		about button is the programs only gadget. */
	
	Object * ctrlwin, *app, *menustrip, *TitleWin;
	Object *AboutButton, *debugButton;
	
	/* Open muimaster library */
	
	D(KPrintF("init()\n");)
	init();

	
	/* Create customclass */
	
	D(KPrintF("creating dtimage\n");)
	if (!( DTImage = MUI_CreateCustomClass(NULL,MUIC_Area,NULL,sizeof(struct DTImageData),DTImageDispatcher)))
		fail(NULL,"Could not create DTImage class.");

	/* Create apploication object */
	/* I chose to make this a small thing with only a simple window with a text.
		This way I can open the window quickly to confirm that the program is started.
		Especially when the 'real' main windows contains alot of datatypes and other
		'slow' stuff this makes sence. And especially on slow machines. */
		
	D(KPrintF("Create app\n");)
	app=ApplicationObject,
		MUIA_Application_Title	, progname,
		MUIA_Application_Version    , "$VER: "progname" "vsion,
		MUIA_Application_Copyright  , "©1998, Linus McCabe.",
		MUIA_Application_Author     , "Linus McCabe",
		MUIA_Application_Description, "A datatype example.", 
		MUIA_Application_Base       , progbase,
		MUIA_Application_Menustrip	 , menustrip=MUI_MakeObject(MUIO_MenustripNM,MenuData1,0),
		MUIA_Application_Commands	 ,	commands,
	
		SubWindow,TitleWin=WindowObject,
			MUIA_Window_SizeGadget,FALSE,
			MUIA_Window_DragBar,FALSE,
			MUIA_Window_CloseGadget,FALSE,
			MUIA_Window_DepthGadget,FALSE,

			MUIA_Window_Activate,FALSE,

			WindowContents,
				VGroup,
					Child,TextObject,
						MUIA_Text_Contents,"Datatype example, by Linus McCabe",
					End,
				End,
			End,
		End;
		
		if(app)	/* If the application could be opened */
		{
			
			/* Open the title window */
			set(TitleWin, MUIA_Window_Open, TRUE);	
			
			/* Create main window */
			ctrlwin=WindowObject,
				MUIA_Window_Title,progname" "vsion" by Linus McCabe",
				MUIA_Window_ID   , MAKE_ID('M','A','I','N'),
				
				WindowContents,
					HGroup,MUIA_Background,"2:00000000,00000000,00000000",
						Child, debugButton=NewObject(DTImage->mcc_Class,NULL,					/* The animation customclass*/
							MUIA_DTImage_File,"images/about",
							MUIA_InputMode,MUIV_InputMode_RelVerify,
						End,
						Child, TextObject,
							MUIA_Text_SetMax,TRUE,
							MUIA_Text_Contents,"\338"progname" "vsion"\nby Linus McCabe",
						End,
						Child, AboutButton=NewObject(DTImage->mcc_Class,NULL,	/*I've designed the class to be usable with both animations and stills */
											MUIA_DTImage_File,(ULONG)"images/about",/* to be used in gadgets etc */
											//MUIA_DTImage_AltText,(ULONG)"About",
											//MUIA_DTImage_NoAnim,TRUE,
											MUIA_InputMode,MUIV_InputMode_RelVerify,
						End,
					End,
				End;
	
			if (ctrlwin)	/*If the creation was successful */
			{
				/* add to application */
				DoMethod(app, OM_ADDMEMBER, ctrlwin);	
	
				D(KPrintF("do main methods\n");)

			/*Main methods*/

				D(KPrintF("ctrl, menu: %ld, %ld\n",ctrlwin,menustrip);)
			
				/* set up some basic notifiactions, closegadget, menues etc*/
				DoMethod(ctrlwin,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
				DoMethod(app,MUIM_Notify,MUIA_Application_MenuAction,MenQuit,app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
				DoMethod(app,MUIM_Notify,MUIA_Application_MenuAction,MenAbout,app,4,MUIM_CallHook,&aboutHook,menustrip,AboutButton);
				DoMethod(app,MUIM_Notify,MUIA_Application_MenuAction,MenAboutMUI,app,4,MUIM_CallHook,&aboutmuiHook,ctrlwin,menustrip);
				DoMethod(app,MUIM_Notify,MUIA_Application_MenuAction,MenMUIPrefs,app,2,MUIM_Application_OpenConfigWindow,0);

				DoMethod(AboutButton,MUIM_Notify,MUIA_Pressed,FALSE,app,4,MUIM_CallHook,&aboutHook,menustrip,AboutButton);
				DoMethod(debugButton,MUIM_Notify,MUIA_Pressed,FALSE,app,4,MUIM_CallHook,&debugHook);
						
				
				D(KPrintF("open main window\n");)	
				
				/* CLose the title win and open main window instead.
				On this little example, the title window only flashes if I
				do this, so I'll just let it remain open for now... */
				
//				set(TitleWin,MUIA_Window_Open,FALSE);   
				set(ctrlwin,MUIA_Window_Open,TRUE);

				D(KPrintF("ctrlwin: %ld\napp: %ld\n\n",ctrlwin,app);)
				D(KPrintF("&ctrlwin: %ld\n&app: %ld\n\n",&ctrlwin,&app);)

				
				/* The usual wait for exit loop */
				{
				   ULONG sigs = 0;
	
				   while (DoMethod(app,MUIM_Application_NewInput,&sigs)	
			          !=MUIV_Application_ReturnID_Quit)
				   {
				      if (sigs)
				      {
				         sigs = Wait(sigs | SIGBREAKF_CTRL_C);	
				         if (sigs & SIGBREAKF_CTRL_C) break;		
				      }
				   }
				}

				/* dispose the application and delete the customclass */
			}
			else
			{
			
			
			}
		
			MUI_DisposeObject(app); app=NULL;
			MUI_DeleteCustomClass(DTImage); 
	
			fail(NULL,NULL);
			
		}
	return(0);
}

