#include <intuition/intuition.h>

#ifdef LATTICE
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#else
#include <functions.h>
#endif

#define INOM "intuition.library"
#define GNOM "graphics.library"

int ypos = 10; /* Stampa Text() in finestra */

struct GfxBase *GfxBase;
struct IntuitionBase *IntuitionBase;
struct Window *wind;

struct IntuiText IText3 = {
3, 1, COMPLEMENT, 0, 0, NULL, (UBYTE *) "Opzione3", NULL };

struct IntuiText IText2 = {
3, 1, COMPLEMENT, 0, 0, NULL, (UBYTE *) "Opzione2", NULL };

struct IntuiText IText1 = {
3, 1, COMPLEMENT, 0, 0, NULL, (UBYTE *) "Opzione1", NULL };

struct MenuItem MenuItem3 = {
NULL, 0, 16, 72, 8, ITEMTEXT | ITEMENABLED | HIGHCOMP,
0, (APTR) &IText3, NULL, NULL, NULL, MENUNULL };

struct MenuItem MenuItem2 = {
&MenuItem3, 0, 8, 72, 8, ITEMTEXT | ITEMENABLED | HIGHCOMP,
0, (APTR) &IText2, NULL, NULL, NULL, MENUNULL };

struct MenuItem MenuItem1 = {
&MenuItem2, 0, 0, 72, 8, ITEMTEXT | ITEMENABLED | HIGHCOMP,
0, (APTR) &IText1, NULL, NULL, NULL, MENUNULL };

struct Menu Menu1 = {
NULL, 0, 0, 55, 0, MENUENABLED, "Lista1", &MenuItem1 };

struct NewWindow NewWindowStructure1 = {
35, 35, 450, 85, 0, 1, MENUPICK + CLOSEWINDOW,
WINDOWDEPTH + WINDOWCLOSE + ACTIVATE + NOCAREREFRESH,
NULL, NULL, (UBYTE *) "MenuDemo", NULL,
NULL, 0, 0, 0, 0, WBENCHSCREEN };


void die( n )
SHORT n;
{
 if ( wind ) CloseWindow( wind );
 if ( GfxBase ) CloseLibrary ( GfxBase );
 if ( IntuitionBase ) CloseLibrary( IntuitionBase );
 _exit( ( LONG ) n );
}

void printw( w, s, n )
struct Window *w; char *s; int n;
{
 register struct RastPort *rp = w -> RPort;
 char buff[ 40 ];
 register int l;
 int ular, ualt, xoff, yoff;

 ualt = w -> Height - w -> BorderTop - w -> BorderBottom;
 
 if ( ypos >= ualt )
   {
   ular = w-> Width - w-> BorderLeft - w-> BorderRight;
   xoff = w-> BorderLeft; yoff = w-> BorderTop;
   SetAPen( rp, 0 ); ypos = 10;
   RectFill( rp, xoff, yoff, xoff+ular-1, yoff+ualt-1);  
   }
 SetAPen( rp, 1 ); SetDrMd( rp, JAM2 );
 l = sprintf( &buff[0], "%20s%6d", s, n );
 Move( rp, 19, ypos += 8 );
 Text( rp, buff, l );
}

void _main()
{
 struct IntuiMessage *msg; /* Messaggio IDCMP */
 ULONG class; /* Classe messaggio IDCMP */
 USHORT num; /* Codice messaggio IDCMP, opzione menu */

 IntuitionBase=(struct IntuitionBase *) OpenLibrary( INOM, 33L );
 if ( IntuitionBase == NULL ) die( 10 );

 GfxBase = (struct GfxBase *) OpenLibrary( GNOM, 33L );
 if ( NOT GfxBase  ) die( 11 );

 if ( !( wind = OpenWindow( &NewWindowStructure1 ) ) ) die( 12 );

 SetMenuStrip( wind, &Menu1 );

 FOREVER {
   Wait( 1L << wind -> UserPort -> mp_SigBit );
   while(( msg=(struct IntuiMessage *) GetMsg( wind->UserPort )))
     {
     class = msg -> Class;
     num  = msg -> Code;
     ReplyMsg( (struct Message *) msg );
     switch( class )
       {
       case CLOSEWINDOW:
         ClearMenuStrip( wind );
         die( 0 );
         break;
   
       case MENUPICK:
         if ( num == MENUNULL ) break;
         printw( wind, "Numero menu = ", MENUNUM(num) );
         printw( wind, "Numero item = ", ITEMNUM(num) );
         break;
       default:
         break;
       } /* switch */
     } /* while */
  } /* FOREVER */
}/* main */
