// ............................................................ provabottoni.cc
#include <curses.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <corso.h>

extern XVideoData xvd;

void func(double *t,double *y,double *dydt);

main()
{
	int ButtonHeight,i,iFont,iMov,iPress,iRel,nButt,x,y;
	int ButtonWidth[2];
	clock_t musec1,musec2;
	timespec ts1,ts2;
	XButton butt[2];
	
	// ........................................................... tempo di sonno
	ts1.tv_sec=0;
	ts1.tv_nsec=10000000;
	// ............................................................. modo grafico
	StartXWindow(800,600,0,0,"Provabottoni",1);
	// ................................................................ caratteri
	iFont=1;
	// ....................................................... numero dei bottoni
	nButt=2;
	// .................................................. posizione primo bottone
	XButtonSize("Bottone",iFont,&ButtonWidth[0],&ButtonHeight);
	butt[0].x1=(xvd.width[0]-ButtonWidth[0])/2;
	butt[0].x2=butt[0].x1+ButtonWidth[0];
	butt[0].y1=xvd.height[0]/2-2*ButtonHeight;
	butt[0].y2=butt[0].y1+ButtonHeight;
	butt[0].txt="Bottone";
	// ................................................. posizione second bottone
	XButtonSize("Fine",iFont,&ButtonWidth[1],&ButtonHeight);
	butt[1].x1=(xvd.width[0]-ButtonWidth[1])/2;
	butt[1].x2=butt[1].x1+ButtonWidth[1];
	butt[1].y1=xvd.height[0]/2+ButtonHeight;
	butt[1].y2=butt[1].y1+ButtonHeight;
	butt[1].txt="Fine";
	// ........................................................ disegna i bottoni
	for (i=0;i<nButt;i++) XPaintButton(&butt[i],iFont,0,0);
	XRedraw();
	// ....................................... seleziona gli eventi da registrare
	XSelectInput(xvd.display,xvd.win[0],ButtonPressMask|ButtonReleaseMask|
			PointerMotionMask|ExposureMask);
	// ===================================================================== loop
	for (;;)
	{
		XSync(xvd.display,FALSE);
		nanosleep(&ts1,&ts2);
		if (XCheckTypedWindowEvent(xvd.display,xvd.win[0],Expose,&xvd.ev))
		{ // ........................... ridisegna la finestra se era stata coperta
			if (xvd.ev.xexpose.count==0) XRedraw();
			continue;
		}
		if (XCheckTypedWindowEvent(xvd.display,xvd.win[0],ButtonRelease,
						 &xvd.ev))
		{ // ....................................... un bottone e' stato rilasciato
			x=xvd.ev.xbutton.x;
			y=xvd.ev.xbutton.y;
			iRel=-1;
			for (i=0;i<nButt;i++)
			{ // ............................ cerca quale bottone e' stato rliasciato
				if (x<butt[i].x1 || x>butt[i].x2) continue;
				if (y<butt[i].y1 || y>butt[i].y2) continue;
				iRel=i;
				break;
			}
			// ........................................ disegna i bottoni non premuti
			if (iRel>=0) for (i=0;i<nButt;i++) XPaintButton(&butt[i],iFont,0,0);
			// ........................ esci se e' stato rilasciato il bottone <Fine>
			if (iRel==1) break;
			// ........................................................................
			XRedraw();
			continue;
		}
		if (XCheckTypedWindowEvent(xvd.display,xvd.win[0],ButtonPress,
						 &xvd.ev))
		{ // .......................................... un bottone e' stato premuto
			x=xvd.ev.xbutton.x;
			y=xvd.ev.xbutton.y;
			iPress=-1;
			for (i=0;i<nButt;i++)
			{ // ............................... cerca quale bottone e' stato premuto
				if (x<butt[i].x1 || x>butt[i].x2) continue;
				if (y<butt[i].y1 || y>butt[i].y2) continue;
				iPress=i;
				break;
			}
			if (iPress>=0)
			{ // ......................................... disegna il bottone premuto
				XPaintButton(&butt[iPress],iFont,2,0);
				// ...................................... e' stato premuto il bottone 0
				if (iPress==0) printf("Hai premuto <Bottone>\n");
				else if (iPress==1) printf("Hai permuto <Fine>\n");
			}
			// .......................................................................
			XRedraw();
			continue;
		}
		if (XCheckTypedWindowEvent(xvd.display,xvd.win[0],MotionNotify,
						 &xvd.ev))
		{ // .............................................. il mouse e' stato mosso
			x=xvd.ev.xbutton.x;
			y=xvd.ev.xbutton.y;
			iMov=-1;
			for (i=0;i<nButt;i++)
			{ // ................ cerca se il cursore del mouse e' sopra a un bottone
				if (x<butt[i].x1 || x>butt[i].x2) continue;
				if (y<butt[i].y1 || y>butt[i].y2) continue;
				iMov=i;
				break;
			}
			// ........................................ disegna i bottoni non premuti
			for (i=0;i<nButt;i++) if (i!=iMov) XPaintButton(&butt[i],iFont,0,0);
			// ................................. disegna il bottone coperto dal mouse
			if (iMov>=0) XPaintButton(&butt[iMov],iFont,1,0);
			XRedraw();
			continue;
		}
		XRedraw();
	}
	// ..........................................................................
	CloseXWindow();
}
