[comp.sys.amiga] filemenu.c - 6 of 21

crunch@well.UUCP (John Draper) (12/03/86)

/*=======================================================================
  filemenu.c - contains the FILE menu functions
========================================================================*/
 
 
/*  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 <stdio.h>
#include "gad.h"
 
/* constants */
 
/* external functions */
 
extern struct Gadget *grabGad();    /* Gadio.c */
extern void dumpgad();
extern UBYTE *getrname();
extern void disp_render();
extern struct StringInfo *nam_info();
extern long Open();                    /* Using 16 bit ints */
 
/* external vars */
 
extern UBYTE *gadnames[];
extern struct Gadget *gads[];
extern int nextgad;
extern int curgad;
extern struct Gadget testgad;
extern UBYTE sbuf_1[];         /* Brent's stuff */
extern int link;
extern struct Window *w;
extern int nextlink;
UBYTE gadfile[NAM_SIZ];         /* Brent's stuff */
extern BOOL updated, saved;     /* Brent's stuff */
extern UBYTE names [MAX_NUM_GADGETS] [NAM_SIZ];
 
/* local vars */
 
 
/*---------- save as source ----------*/
 
dumpgadgets()
{
/*  Starting from the LAST gadget in the window's gadget list,  each of the
    Intuitext structures are displayed "FIRST", then the "Specialinfo"
    structures, the "Borders",  and finally the structure.   First,  a
    file is opened,  and the text is "fprintf'ed" into the file.
 
    Change history:
    11-17-86  RRL cleaned up this and pgad.c so it would work with
              all gadget types and with intuitext. Also added name
              parameters to all disp_ functions - Intuitext is now
              output as part of disp_renders.
 
*/
    int  i;
    int strnum = 1;       /* idents the string gadgets from each other */
    int propnum = 1;      /* idents proportional gads from each other */
    int rendnum = 1;      /* identifier for borders */
    static UBYTE fnam[NAM_SIZ];  /* Filename size */
    struct StringInfo *si;
    struct PropInfo *pi;
    FILE *fopen(), *txt;               /* $$$ File descriptor */
 
 
    /* Open up requester and get gadgets source file name */
    get_req_name("Save source file named as:", fnam);
 
    /* Open up the file for write access in current directory */
    txt = fopen(fnam, "w");
 
    /* First print out a header to the file, containing all the normal */
    /* includes, defines for all gadget IDs, and other 'standard'      */
    /* structures, such as text attributes to be used in intuitext.    */
    /* this and other disp_ routines are in pgad.c $$$NEW 11/16/86 RRL */
    disp_Header(txt);
 
    for (i=nextgad-1; i>=0; i--)
      switch(gads[i]->GadgetType) {
 
       case BOOLGADGET:
                 disp_render(txt, gads[i], rendnum, i);
                 disp_gadget(txt, gads[i], BORD, rendnum, 0, gadnames[i], i);
                 rendnum++;
                 break;
 
         case STRGADGET:
                 si = (struct StringInfo *)gads[i]->SpecialInfo;
                 disp_render(txt, gads[i], rendnum, i);
                 dispstring(txt, si, strnum, (SHORT)BUFSIZE,gadnames[i]);
                 disp_gadget(txt, gads[i], BORD, rendnum, strnum, gadnames[i],i)
;
                 strnum++;
                 rendnum++;
                 break;
 
         case PROPGADGET:
                 pi = (struct PropInfo *)gads[i]->SpecialInfo;
                 disp_render(txt, gads[i], propnum, i);
                 disp_props(txt, pi, propnum, gadnames[i]);
                 disp_gadget(txt, gads[i], 0, 0, propnum,  gadnames[i],i);
                 propnum++;
                 break;
    }
    fclose(txt);
}
 
/*-------- Get a file containing gadgets --------*/
getgad()
/*  Gets a filename, stores it in the variable 'gadfile'.  Opens the file and
    reads the gadgets into the gads[] array while putting their names in the
    gadnames[] array.  Clears the 'updated' flag, sets the 'saved' flag.
 
    WHAT'S NOT DONE YET:
    After the gadgets have been read in, they are displayed after they are
    linked to the window.
*/
{
    UBYTE           tempname[NAM_SIZ], *gadname;
    long              gadfd;
    int                   i;
    int              numgad;    /* Number of gadgets to read in */
    struct Gadget   *newgad;
    struct StringInfo   *si;    /* StringInfo structure */
    UBYTE              *str;    /* pointer to req buffer string */
 
    /* Get file name.  Not a scrolling requester, but maybe soon... */
    si = nam_info();              /* Not supposed to access directly */
    if (gadfile[0] != '\0') {
       strcpy(si->Buffer, gadfile);   /* Copy string to req buffer */
       si->BufferPos = strlen(si->Buffer) + 1;
    }
    else {
       *si->Buffer = '\0';
       si->BufferPos = 0;
    }
    if ((str = getrname("Gadget file name")) == (UBYTE *) NULL)
        return(0);
 
    strcpy(gadfile, str);
    /* Try to open the existing file... */
    if ((gadfd = Open(gadfile, MODE_OLDFILE)) == NULL) {
        kprintf("Can't open file %s",gadfile);
        return(0);
    }
    /* Initialize "nextgad" */
    nextgad = curgad = NULL;
    link = FALSE;
    /* Get the number of gadgets to read */
    Read(gadfd, &numgad, (long)sizeof(numgad));
 
    for (i=0; i<numgad; i++) {
 
        /* Get the Gadget name */
        Read(gadfd, names[i], (long)NAM_SIZ);
        /* Now get the gadget... */
        newgad = grabGad(gadfd, i);
 
        curgad = nextgad++;
        gads[i] = newgad;    /* Copy Gadget structure pointer */
        AddGadget(w, gads[i], -1L);   /* Add it to window */
        nextlink++;
        link = TRUE;                  /* OK to refresh gadgets */
 
        /* Save the gadget name, too! */
        gadnames[i] = names[i];
    }
 
    /* Don't forget to close the file. */
    Close(gadfd);
 
    /* Clear 'updated' flag and set 'saved' flag. */
    updated = FALSE;
    saved = TRUE;
 
    /* Now refresh the Gadgets */
    if (link) RefreshGadgets(gads[0], w, NULL);
}
/*--------- Save a file containing gadgets --------*/
savegad()
{
/*  Checks 'updated' and 'saved' flags,  if necessary, the program should save
    the gadget info out to the file.   Prior to saving each gadget structure to
    disk, writes the gadget's name.
*/
    long             gadfd;
    UBYTE             *str;
    struct StringInfo  *si;
    int                  i;
 
    /* Get "StringInfo" pointer for Name requestor */
    si = nam_info();
 
    /* Get file name.  Not a scrolling requester, but maybe soon... */
    if (gadfile[0] != '\0') {
       strcpy(si->Buffer, gadfile);
       si->BufferPos = strlen(si->Buffer) + 1;  /* Posn cur at end */
    }
    else {
       *si->Buffer = '\0';
       si->BufferPos = 0;
    }
    if ((str = getrname("Gadget file name")) == NULL)
        return(0);
 
    strcpy(gadfile, str);
 
    /* Try to open the existing file... */
    if ((gadfd = Open(gadfile, MODE_OLDFILE)) == 0L)
        /* No go... try to open a new file... */
        if ((gadfd = Open(gadfile, MODE_NEWFILE)) == 0L) {
            kprintf("Can't open the file:  '%s'. -- ", gadfile);
            return(0);
        }
    /* Write out the number of gadgets to read, curgad is num of gadgets */
 
    Write(gadfd, &nextgad, (long)sizeof(nextgad));
 
    for (i = 0; i < nextgad; i++) {
        /* ### Write the gadget name. */
        Write(gadfd, gadnames[i], (long)NAM_SIZ);
 
        /* Now save the gadget... */
        dumpGad(gads[i], gadfd, i);
    }
 
    /* Close file. */
    Close(gadfd);
 
    /* Clear 'updated' flag and set 'saved' flag. */
    updated = FALSE;
    saved = TRUE;
}