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

/* the externs in scale.c */
extern struct Library *AladBase;
extern struct IntuitionBase *IntuitionBase;
extern double scalex,scaley,scalez;
extern int radial,scalefrom,deforms;

/* some statics for a possible user cancel */

static float tscalex,tscaley,tscalez;
static int tradial,tscalefrom;
static int perform;

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

int getscdef(void);

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

static void handlescaldef(APTR object);

/* this is the entrance to the user window routines */
int getscdef()
{
 /* copy the current values for possible cancel */
 tscalex = scalex;
 tscaley = scaley;
 tscalez = scalez;
 tradial = radial;
 tscalefrom = scalefrom;

 /* set up gadget buffers */
 sprintf(sdsdscalexgdSIBuff,"%-7.4f",scalex);
 sprintf(sdsdscaleygdSIBuff,"%-7.4f",scaley);
 sprintf(sdsdscalezgdSIBuff,"%-7.4f",scalez);

 /* set the gadget flags */
 sdradial.Flags = radial ? SELE : NSELE;
 sdscalefrom.Flags = scalefrom ? SELE : NSELE; /* if scalfrom, use atp */
 sddeforms.Flags = deforms ? SELE : NSELE;

 /* set the window's screen pointer
    getactivescreen returns the currently active aladdin screen
 */
 sdNewWindowStructure1.Screen = getactivescreen();
 /* open the window */
 defwin = OpenWindow(&sdNewWindowStructure1);         /* 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 == ACTIVEWINDOW) ActivateGadget(&sdscalexgd,defwin,0);
   if ((class==GADGETUP) || (class==GADGETDOWN)) {
    handlescaldef(object);
   }
  }
 }while(defdone);
 CloseWindow(defwin);
 return(perform);
}

/* this is our event handler */
static void handlescaldef(object)
APTR object;
{
  if (object == (APTR)&sdcancel) {
   scalex = tscalex;
   scaley = tscaley;
   scalez = tscalez;
   radial = tradial;
   scalefrom = tscalefrom;
   defdone = 0;
   return;
  }
  if (object == (APTR)&sdaccept) { defdone = 0; return; }
  if (object == (APTR)&sdperform) {defdone = 0; perform = 1; return; }
  if (object == (APTR)&sdscalexgd) {
   scalex = atof(sdsdscalexgdSIBuff);
   sprintf(sdsdscalexgdSIBuff,"%-7.4f",scalex);
   RefreshGadgets(&sdGadgetList1,defwin,0);
   ActivateGadget(&sdscaleygd,defwin,0);
   return; }
  if (object == (APTR)&sdscaleygd) {
   scaley = atof(sdsdscaleygdSIBuff);
   sprintf(sdsdscaleygdSIBuff,"%-7.4f",scaley);
   RefreshGadgets(&sdGadgetList1,defwin,0);
   ActivateGadget(&sdscalezgd,defwin,0);
   return; }
  if (object == (APTR)&sdscalezgd) {
   scalez = atof(sdsdscalezgdSIBuff);
   sprintf(sdsdscalezgdSIBuff,"%-7.4f",scalez);
   RefreshGadgets(&sdGadgetList1,defwin,0);
   ActivateGadget(&sdscalexgd,defwin,0);
   return; }
  if (object == (APTR)&sdradial) { radial = radial ? 0 : 1; }
  if (object == (APTR)&sdabout) {
   if(system("Aladdin4dhelp:infowin scale.hlp")) { /* 0 is success */
    letemknow("Can't find infowin tool");
   }
   return;
  }
  if (object == (APTR)&sdscalefrom) { scalefrom = scalefrom ? 0 : 1; }
  if (object == (APTR)&sddeforms) { deforms = deforms ? 0 : 1; }
}
