[comp.sys.amiga] 5 of 21 - egad.c

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

/*=========================================================================
  EGad.c -- This is the root module of the Gadget Editor program
  it opens up the editor window, and may open a custom screen if
  there is a numeric command line argument giving the number of
  bitplanes for the screen (default is 2 - a workbench screen).
  This module also includes the main IDCMP loop for the program. In
  that loop other modules are called to do the actual processing for
  the editor.
 
  Credits for the Gadget Editor:
         John Draper    - Initial design, coordination, and integration.
         Ray Larson     - Images and Intuitext, and additional integration.
         Brent Southard - Saving and restoring gadgets in binary form.
         Dave Milligan  - Gadget Editor Main menu.
 
 
   Change List:
     11/17/86: RRL added custom screen support and use of FreeRemember
               to de-allocate gadget memory. Also added was the
               SelectedGadget pointer, used to store the address of
               the last gadget clicked. This pointer is used to select
               a gadget for adding images, intuitext, or deletion in
               the main menu processing module (men.c).
=========================================================================*/
 
/*  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 function declarations ------------*/
 
extern void *OpenLibrary();
extern struct Screen *OpenScreen();
extern struct Window *OpenWindow();
extern struct IntuiMessage *GetMsg();
extern char *gadnames[];
 
/*------------------ extern vars ------------------*/
 
extern int nextgad;
extern int nextprop;
extern int nexttext;
extern int nx_strings;
extern struct Gadget *gads[];
extern struct StringInfo *strings[];
struct Remember *rememberBase; /* used to allocate all dynamic memory - RRL*/
 
/*------------------- global variables -------------------*/
 
struct GfxBase *GfxBase;
struct IntuitionBase *IntuitionBase;
struct RastPort *rp;
struct Gadget *SelectedGadget;
long mask;
int holding = FALSE;     /* Set "TRUE" when new gadget is created */
struct Window *w;
BOOL updated, saved;     /* Gadget status flags used in men.c     */
struct Screen *scr;      /* pointer to the custom screen    - RRL */
SHORT N_Bitplanes;       /* number of bit planes in screen   -RRL */
 
/*------------ Test gadget ------------*/
struct Gadget testgad;
struct PropInfo Prop;
struct StringInfo String;
 
/* The main screen  */
struct NewScreen newscr =
   { 0,             /* LeftEdge at zero           */
     0,             /* TopEdge      "             */
     640,           /* Width - set later          */
     200,           /* Height - non interlace     */
     4,             /* depth - set later          */
     0, 1,          /* DetailPen and BlockPen     */
     0,             /* special display mode-later */
     CUSTOMSCREEN,  /* The screen type            */
     NULL,          /* use the default font       */
     (UBYTE *)"Gadget Editor: Version 1.0 ", /* Screen title       */
     NULL,          /* No special gadgets         */
     NULL,          /* Not a custom bitmap        */
   };
 
 
/* new window structure */
struct NewWindow nw = {
   0, 0, 640, 200, 0, 1,
 
/* IDCMP Flags */
 
   MOUSEMOVE
|  MENUPICK
|  MOUSEBUTTONS
|  CLOSEWINDOW
|  GADGETDOWN
|  GADGETUP,
 
/* Flags */
  WINDOWCLOSE
| WINDOWDEPTH
| ACTIVATE
| RMBTRAP
| REPORTMOUSE,
 
  NULL,               /* First gadget */
  NULL,               /* Checkmark */
 (UBYTE *)  "Gadget Editor: Version 1.0",  /* Window title */
  NULL,               /* No custom streen */
  NULL,               /* Not a super bitmap window */
  0, 0, 640, 200,     /* Not used, but set up anyway */
  WBENCHSCREEN
};
 
/*=========================================================================
  Main entry to the test routine from CLI
========================================================================*/
main(argc,argv)
  int argc;
  char **argv;
{
    /*---------------- local vars ---------------*/
    struct IntuiMessage *mes;          /* Message pointer */
    ULONG class;                       /* Message class */
    USHORT code;                       /* Message code */
    SHORT mx, my;                      /* Mouse X and Y */
    APTR Iadr, adr;                    /* IAddress from Intuimessage */
    ULONG flgs;                        /* temp IDCMP flags */
    SHORT i;
    char *clarg;
 
 
    /*--------------- Check for command line args ----------------RRL-*/
     for(i=1; i<=argc; i++)
        { clarg = argv[i];
          if (isdigit(*clarg)) N_Bitplanes = atoi(clarg);
         }
 
      if (N_Bitplanes == 0) N_Bitplanes = 2;
      else if (N_Bitplanes > 4)
          { printf("Too many bit planes for HiRes!\n");
            exit(0);
           }
           else {
                  newscr.Depth = N_Bitplanes;
                   newscr.ViewModes = HIRES;
                   nw.Width = 640;
                   nw.MaxWidth = 640;
                   nw.Type = CUSTOMSCREEN;
                 }
 
    /*----------- Open up graphics library and Intuition ------------*/
    if ((GfxBase = (struct GfxBase *)
       OpenLibrary("graphics.library", NULL)) == NULL) {
         printf("no graphics library on disk\n");
         CloseThings();
         exit(100);
    }
    mask |= GRAPHICS;
 
    if ((IntuitionBase = (struct IntuitionBase *)
      OpenLibrary("intuition.library", NULL)) == NULL) {
        printf("No intuition library\n");
        CloseThings();
        exit(100);
    }
    mask |= INTUITION;
 
    if (N_Bitplanes != 2) /* the user has set a different screen resolution */
      {            /* so, try opening up the new screen       RRL           */
       if((scr = (struct Screen *)OpenScreen(&newscr)) == NULL)
         { printf("Can't open the custom screen!!\n");
           CloseThings();
           exit (3);
          }
        mask |= SCREEN;
       }
 
    /* Set window pointer to the custom screen, if needed */
 
    if (N_Bitplanes != 2) nw.Screen = scr ;
 
 
    /*----------- open window ----------*/
    if ((w = OpenWindow(&nw)) == NULL) {
        printf("Cannot open window\n");
        CloseThings();
    }
    mask |= WINDOW;
 
    /* ------------- init stuff here -------------*/
 
    rp = w->RPort;
    MakeMyMenus();                /* Init the Menus */
    init_all_req();               /* Initialize all requesters */
 
    /* --------------- main loop -----------------*/
    for (;;)
    {
        if ((mes = GetMsg(w->UserPort)) == NULL) {
            Wait(1L << w->UserPort->mp_SigBit);
            continue;
        }
        class = mes->Class;
        code = mes->Code;
        mx = mes->MouseX;
        my = mes->MouseY;
        Iadr = mes->IAddress;
        ReplyMsg(mes);
        switch (class) {
 
           case CLOSEWINDOW:   CloseThings();   exit(NULL);  break;
 
           case GADGETUP:   /* do nothing for now  - except note that*/
                            /* the gadget was selected.              */
                            SelectedGadget = (struct Gadget *)Iadr;
                            break;
 
           case MENUPICK:  if (MENUNUM(code) != MENUNULL)
                             Do_menu((long)MENUNUM(code), (long)ITEMNUM(code));
                           break;
 
           case GADGETDOWN: /* do nothing for now  - except note that*/
                            /* the gadget was selected.              */
                            SelectedGadget = (struct Gadget *)Iadr;
                            break;
 
           case MOUSEMOVE:  proc_mouse(adr, mx, my); break;
 
           case MOUSEBUTTONS: proc_button(code, mx, my); break;
        }
    }
}
 
/*========================================================================
  draw_box - Draws a box, given Left, Top, Width, Height, and a RastPort
==$$$======================================================================*/
draw_box(rport, left, top, width, height)
struct RastPort *rport;
SHORT left, top, width, height;
{
    long flags;
    long lf = (long)left;
    long tp = (long)top;
    long wd = (long)width;
    long hg = (long)height;
/*
    flags = (long)testgad.UserData;
    if ((flags & GAD_BORDER_TYPE) == GAD_BORDER_TYPE) {
*/
       SetDrMd(rport, COMPLEMENT);
       SetAPen(rport, 1L);
       SetBPen(rport, 0L);
       Move(rport, lf, tp);
       Draw(rport, lf+wd, tp);
       Draw(rport, lf+wd, tp+hg);
       Draw(rport, lf,  tp+hg);
       Draw(rport, lf, tp);
 
}
 
/*========================================================================
  $$$ CloseThings - Function that closes everything properly if any failures
   occur before main loop entry,
========================================================================*/
CloseThings()
{
    int i;
 
    if (mask & INTUITION) CloseLibrary(IntuitionBase);
    if (mask & GRAPHICS)  CloseLibrary(GfxBase);
    if (mask & WINDOW)    CloseWindow(w);
    if (mask & SCREEN)    CloseScreen(scr);
    if (mask & MENU)      ClearMenuStrip(w);
 
    /* All allocation is now done by AllocRemember          */
    /* so, this call is all that is needed to free the      */
    /* gadgets and link nodes allocated by AllocRemember RRL*/
 
    FreeRemember(rememberBase,(LONG)TRUE);
}