#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*/

struct RastPort *rptext;            /*structures*/
struct RastPort *rp;                /*RastPort for the window*/
struct BitMap ScreenBitMap;         /*screen BitMap*/
struct BitMap TextBitMap;           /*BitMap for text window*/

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 Window *TextWdw;
struct IntuiMessage *message;       /*pointer to message from window port*/
struct TextFont *FontPtr;           /*pointer to text font*/
struct TextFont *DefaultPtr; 
                                    /*defined in menugad.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 main screen and menus*/      
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*/ 
/*this font is for the text window rastport*/                                   
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,
  &DefaultAttr,
  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,
  NULL,
  NULL,
  NULL,
  NULL,
  &ScreenBitMap,
  0,0,
  0,0,
  CUSTOMSCREEN
};

 /*small window for displaying text*/
static  struct NewWindow NewTextWdw =
{
  100,40,
  160,
  60,
  1,8,
  NULL,
  SUPER_BITMAP,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  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*/ 
   firstfont();
   availfonts();
   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*/
      }
   InitBitMap(&TextBitMap,5,160,60);  /*set up a bitmap */
   for(i=0;i<5;i++)                      /*allocate screen for it*/
      {
        TextBitMap.Planes[i] = (PLANEPTR)AllocRaster(160,60);
        if (TextBitMap.Planes[i] == 0) exit(2);
        BltClear(TextBitMap.Planes[i],20*60,0); /*clear the a screen area*/
      }
   CustScr= (struct Screen *) OpenScreen(&NewCustScr);
   if (CustScr == 0) exit(1);           /*open the screen*/
   NewWdw.Title = "Main Window";
   NewWdw.FirstGadget=&Gadget1;         /*attach the first gadget*/
   NewWdw.BitMap=(struct BitMap *) &ScreenBitMap; /*attach the bitmap*/
   NewWdw.Screen = CustScr;             /*open the main window*/
   Wdw = (struct Window *)OpenWindow(&NewWdw);
   if (Wdw  == NULL) exit(FALSE);
   rp = Wdw->RPort;                    /*RastPort address of main window*/
   NewTextWdw.BitMap = (struct BitMap *) &TextBitMap;  
   NewTextWdw.Screen = CustScr; 
   NewTextWdw.Title = "Text Window";            /*open the text window*/
   TextWdw = (struct Window *)OpenWindow(&NewTextWdw);
   if (TextWdw  == NULL) exit(FALSE);
   rptext = TextWdw->RPort;             /*RastPort address of text window*/

}
/*display the text string in the text window*/
void writetext()
{
  SetAPen(rptext,0);               /*clear text area*/ 
  RectFill(rptext,5,10,140,49);
  SetAPen(rptext,1);
  Move(rptext,5,40);              /*Move cursor to start of text area*/
  Text(rptext,"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);
  CloseWindow(TextWdw);
  CloseScreen(CustScr);
  CloseFont(FontPtr);
  for (i=0;i<5;i++)                   /*free the screen memory*/
    {
       FreeRaster(ScreenBitMap.Planes[i],320,256);
    }
  for (i=0;i<5;i++)                   /*free the text window memory*/
    {
       FreeRaster(TextBitMap.Planes[i],160,60);
    }
}

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(rptext,FSF_UNDERLINED,255);
   writetext();
}
void normal()               /*set text style to normal */
{
   SetSoftStyle(rptext,FS_NORMAL,255);
   writetext();
}
void italic()              /*set text style ti italic*/                 
{
   SetSoftStyle(rptext,FSF_ITALIC,255);
   writetext();
}
void bold()               /*set text style to bold*/
{
   SetSoftStyle(rptext,FSF_BOLD,255);
   writetext();
}

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);
}
