#include "exec/types.h"
#include "exec/memory.h"
#include "graphics/gfxbase.h"
#include "graphics/gfxmacros.h"
#include "intuition/intuitionbase.h"
#include "intuition/intuition.h"
#include "libraries/dos.h"
#include "libraries/dosextens.h"
#include  <stdio.h>                 /*for printf*/
                                    /*structures*/
struct RastPort *rp;                /*RastPort for the window*/
struct ViewPort *WVP;               /*screen ViewPort*/
struct BitMap PicBitMap;            /*picture BitMap*/
struct BitMap ScreenBitMap;         /*screen BitMap*/
struct DiskfontBase *DiskfontBase;  /*diskfontbase*/
struct GfxBase *GfxBase;            /*graphics base*/
struct IntuitionBase *IntuitionBase;/*intuition base*/
struct Screen *CustScr;             /*pointer to screen structure*/
struct Window *Wdw;                 /*pointer to window structure*/
struct IntuiMessage *message;       /*pointer to message from window port*/
struct FileHandle *filehandle;      /*output filehandle for writing to screen*/
struct TextFont *FontPtr;           /*pointer to text font*/
                                    /*defined in gadgets.c*/
extern struct Gadget Gadget1;       /*pointer to first gadget*/
extern struct Menu Menu1;           /*pointer to first menu*/
                                    /*program variables*/
long i,k;                           /*loop variables*/
long libraries =0;                  /*libraries flag*/
long code;                          /*used in IntuiMessage*/
short quit_flag;                    /*flag*/          
 
/* these default text attributes are used for the menu text*/      
struct TextAttr DefaultAttr=
{
  "topaz.font",                    /*name of font*/                
  8,                               /*height of characters*/
  FS_NORMAL,                       /*normal style*/ 
  FPF_ROMFONT                      /*the font is in ROM*/
};
/*the text attributes are the same as the default font to start with*/                                    
struct TextAttr TextAttr=        
{
  "topaz.font",                 
  8,            
  FS_NORMAL,     
  FPF_ROMFONT   
};

 /*standard full size lo-res screen*/
static  struct NewScreen NewCustScr =
{
  0,270,
  320,
  256,
  5,
  1,8,
  SPRITES,
  CUSTOMSCREEN | CUSTOMBITMAP,
  &TextAttr,
  NULL,
  NULL,
  &ScreenBitMap,
};

 /*full size window for screen*/
static  struct NewWindow NewWdw =
{
  0,0,
  320,
  256,
  1,8,
  CLOSEWINDOW|MENUPICK|GADGETUP|MENUVERIFY|GADGETDOWN|MOUSEBUTTONS,
  SUPER_BITMAP | ACTIVATE
  | BORDERLESS,
  NULL,
  NULL,
  NULL,
  NULL,
  &ScreenBitMap,
  0,0,
  0,0,
  CUSTOMSCREEN
};

void main(),init(),cleanup(),closelibraries(),quit(),Handle_Key(),Handle_IDCMP();
void writetext(),normal(),bold(),underlined(),italic();

void main()
{
   libraries= openlibraries();    /*open the libraries*/
   if (libraries == 0) exit(1);
   if (fontsdirectory() == 1) exit(1); /*check for fonts directory*/
   init();                        /*initialise display*/ 
   SetMenuStrip(Wdw,&Menu1);      /*attach the menustrip to the window*/
   SetAPen(Wdw->RPort,1);         /*set the foreground pen*/
   writetext();                   /*display the text string*/
   MoveScreen(CustScr,0,-270);    /*move screen into place*/
   Handle_IDCMP();                /*main polling loop*/
   cleanup();                     /*free memory*/  
   closelibraries();               
}
openlibraries()                   /*open the libraries*/
{
   GfxBase= (struct GfxBase *)OpenLibrary("graphics.library",0);
   if (GfxBase == 0) return(0);
   IntuitionBase= (struct IntuitionBase *)OpenLibrary("intuition.library",0);
   if (IntuitionBase == 0) return(0);
   DiskfontBase= (struct DiskfontBase *)OpenLibrary("diskfont.library",0);
   if (DiskfontBase == 0)        /* open diskfont library*/ 
     {
        printf("no diskfont library");
        return(0);
     };
return(1);                      /*returns a value if all libraries open*/
}

/*set up a lo-res screen and window*/
void init()
{
   InitBitMap(&ScreenBitMap,5,320,256);  /*set up a bitmap */
   for(i=0;i<5;i++)                      /*allocate screen for it*/
      {
        ScreenBitMap.Planes[i] = (PLANEPTR)AllocRaster(320,256);
        if (ScreenBitMap.Planes[i] == 0) exit(2);
        BltClear(ScreenBitMap.Planes[i],40*256,0); /*clear the a screen area*/
      }
   CustScr= (struct Screen *) OpenScreen(&NewCustScr);
   if (CustScr == 0) exit(1);           /*open the screen*/
   NewWdw.FirstGadget=&Gadget1;         /*attach the first gadget*/
   NewWdw.BitMap=(struct BitMap *) &ScreenBitMap; /*attach the bitmap*/
   NewWdw.Screen = CustScr;             /*open the window*/
   Wdw = (struct Window *)OpenWindow(&NewWdw);
   if (Wdw  == NULL) exit(FALSE);
   WVP=(struct ViewPort *)ViewPortAddress(Wdw); /*find viewport address*/
   rp = Wdw->RPort;                    /*RastPort address of window*/
}
/*display the text string*/
void writetext()
{
  SetAPen(rp,0);               /*clear text area*/ 
  RectFill(rp,0,30,150,60);
  SetAPen(rp,1);
  Move(rp,0,50);              /*Move cursor to start of text area*/
  Text(rp,"text string",11);  /*write the text*/
}

/*close windows and screens, free rasters,close file*/
void cleanup()
{
  int i;                               /*close window and screen*/
  ClearMenuStrip(Wdw);
  CloseWindow(Wdw);
  CloseScreen(CustScr);
  CloseFont(FontPtr);
  for (i=0;i<5;i++)                   /*free the screen memory*/
    {
       FreeRaster(ScreenBitMap.Planes[i],320,256);
    }
}

void closelibraries()               /*close libraries*/
{
  CloseLibrary(GfxBase);
  CloseLibrary(IntuitionBase);
  CloseLibrary(DiskfontBase);
}

/*handle gadgets for load screen*/
void Handle_IDCMP()
{
  APTR object;
  long class;
  for (quit_flag = FALSE;!quit_flag;)
    {
    if (1<<Wdw->UserPort->mp_SigBit)
       {
    while (!(message=(struct IntuiMessage *)GetMsg(Wdw->UserPort)))
         { /*any other processing*/         
         }
       }
    class = message->Class;    
    code = message->Code;    
    object=message->IAddress;
    ReplyMsg((struct Message *) message );   
    switch ( class )
       {
       case MENUPICK:             /*menu item selected*/  
          HandleMenuEvent(object);  
          break;

       case GADGETUP:             /*gadget pressed */
          HandleEvent(object);
          break;

       case GADGETDOWN:           /*gadget released*/
          HandleEvent(object);
          break;

       case MOUSEBUTTONS:
	    break; 
       }                           /* end switch */
    }                              /* end forloop */
}


/*leave program*/
void quit()
{
   quit_flag=TRUE;
}


void underlined()             /*set text style to underlined */   
{                             
   SetSoftStyle(rp,FSF_UNDERLINED,255);
   writetext();
}
void normal()               /*set text style to normal */
{
   SetSoftStyle(rp,FS_NORMAL,255);
   writetext();
}
void italic()              /*set text style ti italic*/                 
{
   SetSoftStyle(rp,FSF_ITALIC,255);
   writetext();
}
void bold()               /*set text style to bold*/
{
   SetSoftStyle(rp,FSF_BOLD,255);
   writetext();
}
sapphire()
{
   TextAttr.ta_Flags = FPF_DISKFONT;     /*diskfont*/
   TextAttr.ta_Style = FS_NORMAL;        /*reset style to normal*/ 
   TextAttr.ta_YSize=14;                 /*font height*/
   TextAttr.ta_Name="sapphire.font";     /*font name*/  
   FontPtr=(struct TextFont *)OpenDiskFont(&TextAttr); /*find font pointer*/
   if (FontPtr==0)                       /*message if font not available*/
     {
       printf("no sapphire font");
       return(1);
     }
   SetFont(rp,FontPtr);                  /*set the font*/
   writetext();                          /*write the text in the new font*/
   return(0);
}

garnet()
{
   TextAttr.ta_Style=FS_NORMAL;          /*reset style to normal*/
   TextAttr.ta_Flags=FPF_DISKFONT;       /*disk font*/
   TextAttr.ta_YSize=16;                 /*font height*/ 
   TextAttr.ta_Name="garnet.font";       /*font name*/
   FontPtr=(struct TextFont *)OpenDiskFont(&TextAttr); /*find font pointer*/
   if (FontPtr==0)                       /*message if font not available*/
      {
         printf("no garnet font");
         return(1);
      }
   SetFont(rp,FontPtr);                 /*set the font*/  
   writetext();                         /*write the text in the new font*/
   return(0);
}

topaz()
{
   TextAttr.ta_Style=FS_NORMAL;        /*reset the style to normal*/
   TextAttr.ta_Flags=FPF_ROMFONT;      /*ROM font*/
   TextAttr.ta_Name="topaz.font";      /*font name*/
   TextAttr.ta_YSize =  9;             /*font height*/
   FontPtr = (struct TextFont *)OpenFont(&TextAttr); /*find font pointer*/
   if (FontPtr==0)
      {
         printf("no topaz font");
         return(1);
      }
   SetFont(rp,FontPtr);                /*set the font*/
   writetext();                        /*write the text in the new font*/
   return(0);
}

fontsdirectory()                       /*check for fonts directory*/
{
  struct Lock *rootlock;
  rootlock=(struct Lock *)Lock("df0:fonts",ACCESS_READ);
  if (rootlock==0)
    {
       printf("no fonts directory on system disc"); 
       return(1);
    }
  UnLock(rootlock);
  return(0);
}
