/*
		Nome del programma . . . WBench Creator ( NewWb.c )
		Autore . . . . . . . . . Graziano Pavone
		Linguaggio . . . . . . . Lattice C
		Hardware . . . . . . . . Amiga in qualsiasi configurazione
		Compilazione . . . . . . LC -L newwb
*/

#include <exec/types.h>
#include <intuition/intuition.h>
#include <proto/intuition.h>
#include <libraries/dos.h>

struct IntuitionBase *IntuitionBase;

struct Screen *screen,*scr;
struct Window *window;

struct NewScreen newscr={0,0,640,256,2,0,1,HIRES,WBENCHSCREEN,NULL,
	"WBench Creator 1.0 - by Graziano Pavone - ",NULL,NULL};

struct NewWindow newwin = {485,0,100,10, 0,1,CLOSEWINDOW,
	WINDOWCLOSE|ACTIVATE,NULL,NULL,"Close WB",NULL,NULL,
	0,0,0,0,WBENCHSCREEN};

struct View *view;
struct ViewPort vp;

/* Sono usati argc e argv per accettare comandi da CLI */
main(argc,argv)
int argc;
char *argv[];
{
	int c,s,a,w,o,i,n,l;
	c=s=a=w=o=i=n=l=0;
	
	/* Apre la libreria intuition */
	if((IntuitionBase=(struct IntuitionBase *)
		OpenLibrary("intuition.library",NULL))==NULL)
	exit(FALSE);

	/* Ricava la view */
	view=(struct View *)ViewAddress();
	
	for(i=1;i<=argc;i++)
	{
        	switch (argv[i][0])
		{
			/* Uso */
			case '?':
				uso();
				break;
				
			/* Apre una finestra CLI */
			case 's':
				s=1;
				break;
			
			/* Carica il Workbench */
			case 'w':
				w=1;
				break;
						
			/* Rende attivo il nuovoworkbench screen */
			case '0': /* Chiude subito il vecchio workbench screen */
				c=1;
			case '1': /* Aspetta che l'utente chiuda il vecchio WB screen */
				a=1;
				break;
			
			/* Interlacciato */
			case 'l':
				newscr.ViewModes=HIRES|LACE;
				newscr.Height=512;
				l=1;
				break;
				
			/* 8 colori */
			case 'c':
				newscr.Depth=3;
				break;
				
			/* Screen NTSC  */
			case 'n':
				newscr.Height=200;
				n=1;
				break;
				
			/* Overscan */
			case 'o':
				newscr.Width=704;
				newscr.Height=283;
				o=1;
				break;
			
			default:
				break;
		}
	}

	/* Imposta le dimensioni dello schermo secondo le opzioni scelte */
	if (n && l)	 newscr.Height=400;
	if (l && o)	 newscr.Height=566;
	if (n && o)	 newscr.Height=241;
	if (n && l && o) newscr.Height=482;

	/* Apre la window per chiudere lo screen sul vecchio screen */
	if (a) window=(struct Window *)OpenWindow(&newwin);
	/* Apre il nuovo Workbench */
	screen=(struct Screen *)OpenScreen(&newscr);

	vp=screen->ViewPort; /* Ricava la viewport dello screen */

/* Se si vuole uno screen overscan, il nuovo screen è centrato sul monitor */
	if (o) 
	{
		view->DxOffset=110;
		view->DyOffset=27;
		RemakeDisplay();
	}
	
	/* Se si sono usate le opzioni S o W vengono eseguiti i comandi 
       Loadwb o newcli nel nuovo screen */
	if (w) Execute("Loadwb",0,0);	
	if (s) Execute("Newcli",0,0);
	
	if (!a)
	{
		/* Puntatore allo screen in cui deve comparire la finestra */
		newwin.Screen=screen;
		window=(struct Window *)OpenWindow(&newwin); /* Apre la finestra */

		/* Aspetta che l'utente selezioni il close gadget e chiude window
           e screen */
		Wait(1<<window->UserPort->mp_SigBit); 
		CloseWindow(window);
		CloseScreen(screen);
	}
	else
	{
		/* Chiude il vecchio screen aspettando che l'utente selezioni il 
			close gadget se si è usata l'opzione 1 */
		if (!c)	Wait(1<<window->UserPort->mp_SigBit);
		scr=window->WScreen;
		CloseWindow(window);
		CloseScreen(scr);
	}
    /* Chiude la libreria Intuition */
	CloseLibrary(IntuitionBase);
	exit(TRUE);
}

uso()
{
	printf(" Workbench Creator v1.0 - by Graziano Pavone. \n\n");
	printf(" Uso: \n\n");
	printf("   NewWB s w 0 1 l c n o \n\n");
	printf(" s = NewCLI nel nuovo screen.\n"); 
	printf(" w = LoadWB nel nuovo screen.\n"); 
	printf(" 0 = Il nuovo screen è attivo e il vecchio è chiuso.\n"); 
	printf(" 1 = Il nuovo screen è attivo e il vecchio può essere
                 chiuso dall'utente.\n"); 
	printf(" l = Nuovo screen interlacciato.\n"); 
	printf(" c = Nuovo screen 8 colori.\n"); 
	printf(" n = Nuovo screen NTSC.\n"); 
	printf(" o = Nuovo screen overscan.\n\n"); 
	CloseLibrary(IntuitionBase);
	exit(TRUE);
	return(TRUE);
}
