[comp.sys.amiga] 3 of 21 - addgad.c

crunch@well.UUCP (John Draper) (11/30/86)

/*=========================================================================
  addgad.c - Add gadgets and allocate the new pointers into a global
  array called "gads[]".   It also increments "nextgad".
  addgad() - Adds the gadget to the array of gadget pointers
 
  Changes:
   11/17/86 - RRL fixed some problems with proportional gadgets
=========================================================================*/
 
/*  The usual header files to be inserted later  */
#include <intuition/intuition.h>
#include <libraries/dosextens.h>
#include <graphics/gfxbase.h>
#include <graphics/gfx.h>
#include <graphics/display.h>
#include <exec/memory.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <devices/narrator.h>
#include <devices/audio.h>
#include <libraries/translator.h>
#include "gad.h"
 
/*================== external functions ===================*/
 
extern struct Gadget testgad;
extern struct Gadget *SelectedGadget;
extern struct PropInfo Prop;
extern struct StringInfo String;
extern struct Window *w;
extern struct RastPort *rp;
 
extern USHORT oldtype;
extern struct Gadget *gads[];
extern char gadimage[MAX_PROPINFOS][NAM_SIZ];
extern char selectimage[MAX_PROPINFOS][NAM_SIZ];
extern struct PropInfo *props[];
extern struct StringInfo *strings[];
extern APTR AllocRemember();
extern struct Remember *rememberBase;
 
/*==================== external vars ======================*/
 
extern int nextgad;
extern int curgad;
extern int pflag; /* holds propinfo flags from menu selection */
 
/*=================== global vars ==================*/
 
char teststr[] = "12345678";
 
/*-------- Add gadget ------*/
addgad()
{
    /*  Allocates memory  into the array */
    if ((gads[nextgad] =  (struct Gadget *)AllocRemember(rememberBase,
               (long)sizeof(struct Gadget),
               MEMF_PUBLIC | MEMF_CLEAR)) == NULL)
       kprintf("Cannot allocate gadget at index %d", nextgad);
 
    /* Makes curgad = nextgad */
       curgad = nextgad;
 
    /* Bumps up the "nextgad" index pointer */
      nextgad++;
 
    /* Deal with the particular gadget type */
      switch(testgad.GadgetType) {
        case BOOLGADGET:  add_bool_gad(); makeborder();  break;
        case STRGADGET :  add_str_gad();  makeborder();  break;
        case PROPGADGET:  add_prop_gad();  break;
    }
   *gads[curgad] = testgad;  /* copy the structure */
}
/*==========================================================================
  Add the StringInfo structure and buffer to the current gadget.   If the
  gadget is already another type of gadget,   De-Allocate the memory used
  by the gadget such as the PropInfo structure and other attached data
  structures
=========================================================================*/
add_str_gad()
{
  struct StringInfo *si;
 
  if ((testgad.SpecialInfo = AllocRemember(rememberBase,
               (long)sizeof(struct StringInfo),
               MEMF_PUBLIC | MEMF_CLEAR)) == NULL)
      kprintf("Stringinfo allocation failed in 'add_str_gad'\n");
 
  si = (struct StringInfo *)testgad.SpecialInfo;
 
  /* Allocate text buffer */
  if ((si->Buffer = (UBYTE *)
    AllocRemember(rememberBase,BUFSIZE, MEMF_PUBLIC | MEMF_CLEAR)) == NULL)
     kprintf("Buffer Alloc failed at 'add_str_gad'\n");
 
  /* Init the SpecialInfo field and the Buffer pointer */
     strcpy(si->Buffer, teststr);
     si->MaxChars = BUFSIZE;
     si->NumChars = 8;
}
 
/*=========================================================================
  add_prop_gad - Adds a proportional type gadget.  Allocates PropInfo
  structure.
========================================================================*/
 
add_prop_gad()
{
    long            flags;
    struct PropInfo *prop;
 
    /* Allocate 2 image structures - Add 2 more tables in globals */
    /*   Nope - one will do. RRL                                  */
    if ((testgad.GadgetRender =
       AllocRemember(rememberBase, (long)sizeof(struct Image),
         MEMF_CLEAR | MEMF_PUBLIC)) == NULL)
           kprintf("No image allocated\n");
 
 /****************************************************
  Commented out by RRL - This is not needed for an AUTOKNOB propgad
  and will be generated automatically if a custom image is created.
    if ((testgad.SelectRender =
      AllocRemember(rememberBase,(long)sizeof(struct Image),
         MEMF_CLEAR | MEMF_PUBLIC)) == NULL)
           kprintf("No select image allocated\n");
  ****************************************************/
 
    testgad.SelectRender = NULL; /*rrl*/
 
    /* Allocate a PropInfo structure */
    if ((testgad.SpecialInfo =
      AllocRemember(rememberBase,(long)sizeof(struct PropInfo),
        MEMF_CLEAR | MEMF_PUBLIC)) == NULL)
         kprintf("No Propinfo Allocated\n");
 
    /* ### We set the "UserData" flag fields to a special type */
   flags = KNOB_IMAGE_TYPE; /* this is NEW  RRL */
   testgad.UserData = (APTR)flags;
 
 /***************************************************
    Commented out by RRL.
     These flags are NOT set for a propgad unless a true custom image
     has been created. AUTOKNOB handles its own image flags.
   testgad.Flags = GADGHIMAGE | GADGIMAGE;
 ****************************************************/
 
    /* Fill in the PropInfo structure */
    prop = (struct PropInfo *)testgad.SpecialInfo;
 
    if (prop != NULL) {
         /* Set Both height and width for all propgads - RRL */
         prop->HorizBody = 0x1000;
         prop->HorizPot = 0x8000;
         prop->VertBody = 0x1000;
         prop->VertPot  = 0x8000;
         prop->Flags = pflag; /* the global set by menu selections */
     }
 
}
 
add_bool_gad()
{
}
/*========================================================================
  makeborder - Make a border around boolean and string gadget.   User
  can always remove them later if they don't want it
=========================================================================*/
makeborder()
{
    struct Border *bord;          /* Pointer to a border structure */
    SHORT *pairs;
    int  i;
    long flags;                   /* flags that go into UserData field */
 
    /* ###Allocate memory for the border and the "pairs" */
    if ((bord = (struct Border *)AllocRemember(rememberBase,
         (long)sizeof(struct Border),
         MEMF_PUBLIC | MEMF_CLEAR)) == NULL)
        kprintf("Border Allocation failed in 'makeborder'\n");
 
   /* AllocMemory for the Border Pairs */
  if ((pairs = (SHORT *) AllocRemember(rememberBase,
       20L, MEMF_PUBLIC | MEMF_CLEAR)) == NULL )
    kprintf("No pairs allocated in makeborder\n");
 
 
   /* Calculation of the border pairs */
   pairs[2] = testgad.Width;
   pairs[4] = testgad.Width;
   pairs[5] = testgad.Height;
   pairs[7] = testgad.Height;
 
   /* Init the Border structure */
   bord->LeftEdge = -1;
   bord->TopEdge  = -1;
   bord->Count = 5;
   bord->FrontPen = 1;
   bord->BackPen = 3;
   bord->XY = pairs;
   bord->DrawMode = JAM1;
 
   /* Put Border pointer into BOTH Gadget and Select render  */
 
   /* Well.. not any more - if the gadget had some alternate */
   /* border this might be worthwhile, but since they are the*/
   /* same it just caused problems in freeing memory.   RRL  */
   testgad.GadgetRender = (APTR)bord;
   testgad.SelectRender = NULL;
   flags = GAD_BORDER_TYPE;
   testgad.UserData = (APTR)flags;
}