/* this is an example of a tool's user window */

/* some general includes */

#include "exec/types.h"

/* these two statements allow more efficient allocmems, see
   SC readme for 6.0
   DON'T call pragmas/exec_pragmas.h
*/

#define __USE_SYSBASE
#include <proto/exec.h>

#include "stdio.h"
#include "string.h"
#include <exec/nodes.h>
#include <exec/lists.h>
#include <exec/libraries.h>
#include <intuition/intuition.h>
#include <graphics/text.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#include "exec/io.h"
#include "exec/memory.h"
#include "dos.h"
#include "libraries/dosextens.h"
#include "libraries/dos.h"
#include "libraries/filehandler.h"
#include "devices/inputevent.h"
#include "graphics/gfxmacros.h"

#include <graphics/gfx.h>
#include <hardware/blit.h>
#include <graphics/copper.h>
#include <graphics/view.h>
#include <graphics/rastport.h>
#include <graphics/gels.h>
#include <graphics/regions.h>
#include <graphics/clip.h>
#include <exec/exec.h>
#include <graphics/gfxbase.h>
#include <m68881.h>
#include "math.h"
#include "float.h"
#include "limits.h"
#include "proto/dos.h"
#include "stdlib.h"
#include "workbench/workbench.h"
#include "workbench/startup.h"
#include "proto/icon.h"
#include "time.h"

/* for our run time libraries */
#include "alad_pragmas.h"
#include "alad_protos.h"

#include "defines.h"
#include "externs.h"

/* the window structures */
#include "selectsamdef.h"

/* externs in selectsam.c */
extern struct Library *AladBase;
extern struct IntuitionBase *IntuitionBase;
extern long attlists,bmlists,paths,lines,complex,hide,deselect;

/* temporary vars for restore in case of a cancel */
static long tattlists,tbmlists,tpaths,tlines,tcomplex,thide,tdeselect;

static long perform;

static struct Window *defwin;
static APTR object;
static ULONG class;
static UWORD code;
static struct IntuiMessage *mess;
static int defdone;

static void handleselsamdef(APTR object);

#define SELE GADGHIMAGE+GADGIMAGE+SELECTED
#define NSELE GADGHIMAGE+GADGIMAGE

int getselectsamdef(void);

int getselectsamdef()
{
 /* initialize the temporary vars */
 tattlists = attlists;
 tbmlists = bmlists;
 tpaths = paths;
 tlines = lines;
 tcomplex = complex;
 thide = hide;
 tdeselect = deselect;

 /* set gadget flags */
 mdattlist.Flags = attlists ? SELE : NSELE;
 mdbmlist.Flags = bmlists ? SELE : NSELE;
 mdpath.Flags = paths ? SELE : NSELE;
 mdlines.Flags = lines ? SELE : NSELE;
 mdcomplex.Flags = complex ? SELE : NSELE;
 mdhide.Flags = hide ? SELE : NSELE;
 mddeselect.Flags = deselect ? SELE : NSELE;

 /* set the window's screen pointer
    getactivescreen returns the currently active aladdin screen
 */
 mdNewWindowStructure1.Screen =  getactivescreen();
 defwin = OpenWindow(&mdNewWindowStructure1);         /* open the window */
 if ( defwin == NULL ) return(0);                 /* in effect a cancel */
 defdone = 1; /* our loop flag */
 perform = 0; /* possible user request */
 do {
  WaitPort(defwin->UserPort); /* ALWAYS wait, some 040 borads fail if you don't */
  while(mess=(struct IntuiMessage *)GetMsg(defwin->UserPort)) {
   object = mess->IAddress; class = mess->Class;
   ReplyMsg((struct message *)mess);
   if(class==GADGETUP) handleselsamdef(object);
  }
 }while(defdone);
 CloseWindow(defwin);
 /* return user preference */
 return(perform);
}

/* this is our event handler */
static void handleselsamdef(object)
APTR object;
{
 if (object == (APTR)&mdattlist) { attlists = attlists ? 0 : 1; }
 if (object == (APTR)&mdbmlist)  { bmlists = bmlists ? 0 : 1; }
 if (object == (APTR)&mdpath)    { paths = paths ? 0 : 1; }
 if (object == (APTR)&mdaccept)  { defdone = 0; return; }
 if (object == (APTR)&mdcancel)  {
  attlists = tattlists;
  bmlists = tbmlists;
  paths = tpaths;
  lines = tlines;
  complex = tcomplex;
  hide = thide;
  deselect = tdeselect;
  defdone = 0;
  return;
 }
 if (object == (APTR)&mdperform) { defdone = 0; perform = 1; return; }
 if (object == (APTR)&mdabout) {
  if(system("Aladdin4dhelp:infowin selectsame.hlp")) { /* 0 is success */
   letemknow("Can't find infowin tool");
  }
  return;
 }
 if (object == (APTR)&mddeselect) { deselect = deselect ? 0 : 1; }
 if (object == (APTR)&mdlines) { lines = lines ? 0 : 1; }
 if (object == (APTR)&mdcomplex) { complex = complex ? 0 : 1; }
 if (object == (APTR)&mdhide) { hide = hide ? 0 : 1; }
}
