#include <intuition/intuition.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/dos.h>
#define WIDTH     120
#define HEIGHT    20
#define SHINE     2     // Bianco
#define SHADOW    1     // Nero
#define OFFSET    5

/* Inizializzazione coordinate bordo */
WORD shadowXY[] = {-1,1,-1,HEIGHT - 1,-WIDTH,HEIGHT - 1};
WORD shineXY[] = {0,-1,0,-HEIGHT,WIDTH - 1,-HEIGHT};

/* Inizializzazione struttura Border */
struct Border shadow_normal = {WIDTH,0,SHADOW,0,JAM1,3,shadowXY,NULL};
struct Border shine_normal = {0,HEIGHT,SHINE,0,JAM1,3,shineXY,&shadow_normal};
struct Border shadow_selected = {WIDTH,0,SHINE,0,JAM1,3,shadowXY,NULL};
struct Border shine_selected = {0,HEIGHT,SHADOW,0,JAM1,3,shineXY,&shadow_selected};

struct IntuiText testo = {SHADOW,0,JAM1,NULL,NULL,NULL,"Premi quì",NULL};

struct Gadget gad = {NULL,OFFSET,OFFSET,WIDTH,HEIGHT,
	GFLG_GADGHIMAGE | GFLG_LABELITEXT,GACT_RELVERIFY | GACT_IMMEDIATE,
	GTYP_BOOLGADGET,&shine_normal,&shine_selected,&testo,NULL,NULL,1,NULL};

void DisegnaCornice(struct Window *win)
{
	struct Border brd;
	WORD coords[10];

	brd.LeftEdge = win -> BorderLeft + 4;
	brd.TopEdge = win -> BorderTop + 4;
	brd.FrontPen = 1;
	brd.BackPen = 2;
	brd.DrawMode = JAM1;
	brd.Count = 10;
	brd.XY = coords;
	brd.NextBorder = NULL;

	/* Punto di inizio */
	coords[0] = coords[1] = 0;

	coords[2] = win -> Width - win -> BorderRight - 8 - win -> BorderLeft;
	coords[3] = 0;

	coords[4] = coords[2];
	coords[5] = win -> Height - win -> BorderBottom - 8 - win -> BorderTop;

	coords[6] = win -> BorderLeft - 4;
	coords[7] = coords[5];

	coords[8] = coords[9] = coords[0];

	DrawBorder(win -> RPort,&brd,0,0);
}

void main(void)
{
	struct Screen *scr;
	struct Window *win;
	struct IntuiMessage *msg;
	BOOL finito = FALSE;

	if((scr = OpenScreenTags(NULL,
		SA_LikeWorkbench, TRUE,
		SA_Title,         "Schermo di prova",
		TAG_DONE)))
	{
		/* Aggiusto in base alla dimensione del font */
		testo.LeftEdge = gad.Width / 2 - IntuiTextLength(&testo) / 2;
		testo.TopEdge = gad.Height / 2 - scr -> Font -> ta_YSize / 2;

		if((win = OpenWindowTags(NULL,
			WA_Top,        scr -> BarHeight + scr -> BarVBorder,
			WA_Height,     scr -> Height - scr -> BarHeight - scr -> BarVBorder,
			WA_IDCMP,      IDCMP_CLOSEWINDOW | IDCMP_GADGETUP /*|
								IDCMP_REFRESHWINDOW*/,
			WA_Flags,      WFLG_CLOSEGADGET | WFLG_SIZEGADGET |
								WFLG_DRAGBAR | WFLG_DEPTHGADGET,
			WA_Title,      "Finestra di prova",
			WA_PubScreen,  scr,
			TAG_DONE)))
		{
			/* Aggiusto in base alla dimensione del font */
			gad.LeftEdge += win -> BorderLeft;
			gad.TopEdge += win -> BorderTop;

			/* Più piccola di così no */
			WindowLimits(win,
				gad.LeftEdge + gad.Width + win -> BorderRight + OFFSET,
				gad.TopEdge + gad.Height + win -> BorderBottom + OFFSET,0,0);

			/* Aggiungo il bottone alla finestra */
			AddGadget(win,&gad,0);

			/* Rinfresco i bottoni della finestra */
			RefreshGadgets(&gad,win,NULL);
			//DisegnaCornice(win);
			while(!finito)
			{
				WaitPort(win -> UserPort);
				while((msg = (struct IntuiMessage *)GetMsg(win -> UserPort)))
				{
					switch(msg -> Class)
					{
						case IDCMP_CLOSEWINDOW:
							finito = TRUE;
						break;
						case IDCMP_GADGETUP:
							Printf("Bottone premuto!\n");
						break;
						//case IDCMP_REFRESHWINDOW:
						//   DisegnaCornice(win);
						//break;
					}
					ReplyMsg((struct Message *)msg);
				}
			}
			RemoveGadget(win,&gad);
			CloseWindow(win);
		}
		CloseScreen(scr);
	}
}
