/************************************************************************/
/*       isup.c        ::: Intuition supplement :::                     */
/*    contains:                                                         */
/*             open_libraries()  -- returns true if intuition and       */
/*                                  graphics open up                    */
/*             make_window()     -- returns a window pointer            */
/*             make_screen()     -- returns a screen pointer            */
/*             copy_chip()       -- allocates chip mem. and copies      */
/*                                  data into it                        */
/*             init_itext()      -- no frills IntuiText initialization  */
/*       programmed by Brian Neal   - 3/4/89 & 5/23/89 & 6/4/89         */
/************************************************************************/

#include <exec/types.h>
#include <intuition/intuition.h>
#include <exec/memory.h>

struct Library *OpenLibrary();
struct Screen  *OpenScreen(), *make_screen();
struct Window  *OpenWindow(), *make_window();
BOOL open_libraries();
void *AllocMem(), *copy_chip(), init_itext();


extern struct IntuitionBase *IntuitionBase;     /*  declared elsewhere! */
extern struct GfxBase       *GfxBase;


BOOL open_libraries(int_rev, gra_rev)
LONG int_rev, gra_rev;
{
      IntuitionBase = GfxBase = NULL;
      if (!(IntuitionBase = (struct IntuitionBase *)
                            OpenLibrary("intuition.library", int_rev)))
         return (FALSE);
      if (!(GfxBase = (struct GfxBase *)
                      OpenLibrary("graphics.library", gra_rev)))
         return (FALSE);

      return (TRUE);
}


struct Screen *make_screen(x, y, width, height, depth, fpen, bpen,
                           viewmodes, font, title, gadgets, bitmap)
SHORT x, y, width, height, depth;
UBYTE fpen, bpen;
ULONG viewmodes;
struct TextAttr *font;
UBYTE *title;
struct Gadget *gadgets;
struct BitMap *bitmap;
{
   struct NewScreen ns;

      ns.LeftEdge    = x;
      ns.TopEdge     = y;
      ns.Width       = width;
      ns.Height      = height;
      ns.Depth       = depth;
      ns.DetailPen   = fpen;
      ns.BlockPen    = bpen;
      ns.ViewModes   = viewmodes;
      ns.Type        = CUSTOMSCREEN;
      ns.Font        = font;
      ns.DefaultTitle= title;
      ns.Gadgets     = gadgets;
      ns.CustomBitMap= bitmap;
      return (OpenScreen(&ns));
}


struct Window *make_window(x, y, width, height, fpen, bpen, iflags, flags,
                           gadget, checkmark, title, screen, bitmap,
                           minw, minh, maxw, maxh)
SHORT x, y, width, height;
UBYTE fpen, bpen;
ULONG iflags, flags;
struct Gadget *gadget;
struct Image *checkmark;
UBYTE *title;
struct Screen *screen;
struct BitMap *bitmap;
SHORT minw, minh;
USHORT maxw, maxh;
{
   struct NewWindow nw;

      nw.LeftEdge    = x;
      nw.TopEdge     = y;
      nw.Width       = width;
      nw.Height      = height;
      nw.DetailPen   = fpen;
      nw.BlockPen    = bpen;
      nw.Title       = title;
      nw.Flags       = flags;
      nw.IDCMPFlags  = iflags;
      nw.Screen      = screen;
      nw.Type        = (screen == NULL) ? WBENCHSCREEN : CUSTOMSCREEN;
      nw.FirstGadget = gadget;
      nw.CheckMark   = checkmark;
      nw.BitMap      = bitmap;
      nw.MinWidth    = minw;
      nw.MinHeight   = minh;
      nw.MaxWidth    = maxw;
      nw.MaxHeight   = maxh;
      return (OpenWindow(&nw));
}


void *copy_chip(dataptr, size)
BYTE *dataptr;
USHORT size;               /* size in bytes of requested CHIP mem */
{
   UCOUNT i;
   BYTE *chipptr, *p;

      if (!(chipptr = (BYTE *) AllocMem((LONG) size, MEMF_CHIP)))
         return ((void *) NULL);

      p = chipptr;

      for (i = 0; i < size; ++i)
         *p++ = *dataptr++;

      return ((void *) chipptr);
}


/*
**    init_itext() -
**       A plain IntuiText initializer.  Assumes single pen color,
**    JAM1 type display, using default font.  User must link IntuiTexts
**    together, if desired.
**/

void init_itext(itext, fpen, x, y, string)
struct IntuiText *itext;
UBYTE fpen;
WORD x, y;
char *string;
{
   itext -> FrontPen  = fpen;
   itext -> BackPen   = 0;
   itext -> DrawMode  = JAM1;
   itext -> LeftEdge  = x;
   itext -> TopEdge   = y;
   itext -> ITextFont = NULL;
   itext -> IText     = (UBYTE *) string;
   itext -> NextText  = NULL;
}
