/* 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 "mirdef.h"

/* the externs that are in mirror.c */

extern struct Library *AladBase;
extern struct IntuitionBase *IntuitionBase;
extern long mirrorx,mirrory,mirrorz;
extern long from,deforms;

/* some statics for a possible user cancel */
static long tmirrorx,tmirrory,tmirrorz;
static long tfrom,tdeforms;
static long perform;

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

static void handlemirdef(APTR object);

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

int getmirrordef(void);

/* this is the entrance to the user window routines */
int getmirrordef()
{
 /* copy the current values for possible cancel */
 tmirrorx = mirrorx;
 tmirrory = mirrory;
 tmirrorz = mirrorz;
 tfrom = from;
 tdeforms = deforms;

 /* set the gadget flags */
 mdmirxgd.Flags = mirrorx ? SELE : NSELE;
 mdmirygd.Flags = mirrory ? SELE : NSELE;
 mdmirzgd.Flags = mirrorz ? SELE : NSELE;
 mdfrom.Flags = from ? SELE : NSELE;
 mddeforms.Flags = deforms ? SELE : NSELE;

 /* set the window's screen pointer
    getactivescreen returns the currently active aladdin screen
 */
 mdNewWindowStructure1.Screen =  getactivescreen();
 /* open the window */
 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 boards 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) handlemirdef(object);
  }
 }while(defdone);
 CloseWindow(defwin);
 return(perform);
}

/* this is our event handler */
static void handlemirdef(object)
APTR object;
{
  if (object == (APTR)&mdmirxgd) { mirrorx = mirrorx ? 0 : 1; return; }
  if (object == (APTR)&mdmirygd) { mirrory = mirrory ? 0 : 1; return; }
  if (object == (APTR)&mdmirzgd) { mirrorz = mirrorz ? 0 : 1; return; }
  if (object == (APTR)&mdaccept) { defdone = 0; return; }
  if (object == (APTR)&mdcancel) {
   mirrorx = tmirrorx;
   mirrory = tmirrory;
   mirrorz = tmirrorz;
   from = tfrom;
   deforms = tdeforms;
   defdone = 0;
   return;
  }
  if (object == (APTR)&mdperform) { defdone = 0; perform = 1; return; }
  /* for the about file, just call exactly like this, but change the
     "mirror.hlp" to your help file name.
  */
  if (object == (APTR)&mdabout) {
   if(system("Aladdin4dhelp:infowin mirror.hlp")) { /* 0 is success */
    letemknow("Can't find infowin tool");
   }
   return;
  }
  if (object == (APTR)&mddeforms) { deforms = deforms ? 0 : 1; }
  if (object == (APTR)&mdfrom) { from = from ? 0 : 1; }
}
