/*
 *  Source machine generated by GadToolsBox V2.0
 *  which is (c) Copyright 1991-1993 Jaba Development
 *
 *  GUI Designed by : Chris Conger
 */

#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <string.h>
#include <clib/diskfont_protos.h>

#include "plotgui.h"
#include "cp.h"

extern UBYTE *version_string;


struct Screen         *Scr = NULL;
APTR                   VisualInfo = NULL;
struct Window         *PlotWindowWnd = NULL;
struct Window         *delwinWnd = NULL;
struct Gadget         *PlotWindowGList = NULL;
struct Gadget         *delwinGList = NULL;
struct Menu           *PlotWindowMenus = NULL;
struct IntuiMessage    PlotWindowMsg;
struct IntuiMessage    delwinMsg;
struct Gadget         *PlotWindowGadgets[6];
struct Gadget         *delwinGadgets[3];
UWORD                  SWIDTH = STDSCREENWIDTH;
UWORD                  SHEIGHT = STDSCREENHEIGHT;
UWORD                  delwinWidth;
UWORD                  delwinHeight;
UBYTE                 *PlotWindowWdt = (UBYTE *)"ChrisP";
UBYTE                 *delwinWdt = (UBYTE *)"Kill Trace";
struct Window         *PreSlideWnd = NULL;
struct Gadget         *PreSlideGList = NULL;
struct IntuiMessage    PreSlideMsg;
struct Gadget         *PreSlideGadgets[1];
UBYTE                 *PreSlideWdt = (UBYTE *)"Precision for Identify Feature";

WORD GADHEIGHT;
WORD GADWIDTH;


struct NewMenu PlotWindowNewMenu[] = {
     NM_TITLE, (STRPTR)"Project", NULL, 0, NULL, NULL,
     NM_ITEM, (STRPTR)"Open File", (STRPTR)"O", 0, 0L, (APTR)newfile,
     NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
     NM_ITEM, (STRPTR)"Add File", (STRPTR)"A", 0, 0L, (APTR)addClicked,
     NM_ITEM, (STRPTR)"Kill Set", (STRPTR)"K", 0, 0L, (APTR)deleteClicked,
     NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
     NM_ITEM, (STRPTR)"Print", (STRPTR)"P", 0, 0L, (APTR)PlotDump,
     NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
     NM_ITEM, (STRPTR)"About", NULL, 0, 0L, (APTR)PlotWindowItem0,
     NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
     NM_ITEM, (STRPTR)"Die", (STRPTR)"Q", 0, 0L, (APTR)PlotWindowItem1,
     NM_TITLE, (STRPTR)"Display", NULL, 0, NULL, NULL,
     NM_ITEM, (STRPTR)"Full View", (STRPTR)"F", 0L, 0L, (APTR)fullClicked,
     NM_ITEM, (STRPTR)"UnZoom", (STRPTR)"U", 0L, 0L, (APTR)UnZoom,
     NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
     NM_ITEM, (STRPTR)"Cross Hair", (STRPTR)"H", 0L, 0L, (APTR)CrossHair,
     NM_ITEM, (STRPTR)"Significant Digits", (STRPTR)"D", 0L, 0L, (APTR)PreOpenRequest,
     NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
     NM_ITEM, (STRPTR)"Grid", (STRPTR)"G", NULL, 0L, (APTR)gridClicked,
     NM_ITEM, (STRPTR)"Symbols", (STRPTR)"S", NULL, 0L, (APTR)PlotWindowItem6,
     NM_ITEM, (STRPTR)"Control Panel", (STRPTR)"C", NULL, 0L, (APTR)PlotWindowItem7,
     NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
     NM_ITEM, (STRPTR)"Screen  Depth", NULL, 0, NULL, NULL,
     NM_SUB, (STRPTR)"1", (STRPTR)"1", NULL, 0L, (APTR)NewDepth1,
     NM_SUB, (STRPTR)"2", (STRPTR)"2", NULL, 0L, (APTR)NewDepth2,
     NM_SUB, (STRPTR)"3", (STRPTR)"3", NULL, 0L, (APTR)NewDepth3,
     NM_SUB, (STRPTR)"4", (STRPTR)"4", NULL, 0L, (APTR)NewDepth4,
     NM_TITLE, (STRPTR)"Axis", NULL, 0, NULL, NULL,
     NM_ITEM, (STRPTR)"XAxis", NULL, 0, NULL, NULL,
     NM_SUB, (STRPTR)"Lin", NULL, 0, 0L, (APTR)PlotWindowItem2,
     NM_SUB, (STRPTR)"Log", NULL, 0, 0L, (APTR)PlotWindowItem3,
     NM_ITEM, (STRPTR)"YAxis", NULL, 0, NULL, NULL,
     NM_SUB, (STRPTR)"Lin", NULL, 0, 0L, (APTR)PlotWindowItem4,
     NM_SUB, (STRPTR)"Log", NULL, 0, 0L, (APTR)PlotWindowItem5,
     NM_END, NULL, NULL, 0, 0L, NULL };


UWORD PlotWindowGTypes[] = {
     BUTTON_KIND,
     BUTTON_KIND,
     BUTTON_KIND,
     BUTTON_KIND,
     BUTTON_KIND,
     BUTTON_KIND
};

UWORD delwinGTypes[] = {
     LISTVIEW_KIND,
     BUTTON_KIND,
     BUTTON_KIND
};

UWORD PreSlideGTypes[] = {
	SLIDER_KIND
};

struct NewGadget PlotWindowNGad[] = {
     NULL, NULL, NULL, NULL, (UBYTE *)"_Add", NULL, GD_add, PLACETEXT_IN, NULL, (APTR)addClicked,
     NULL, NULL, NULL, NULL, (UBYTE *)"_Kill", NULL, GD_delete, PLACETEXT_IN, NULL, (APTR)deleteClicked,
     NULL, NULL, NULL, NULL, (UBYTE *)"_Full", NULL, GD_full, PLACETEXT_IN, NULL, (APTR)fullClicked,
     NULL, NULL, NULL, NULL, (UBYTE *)"_Zoom", NULL, GD_zoom, PLACETEXT_IN, NULL, (APTR)zoomClicked,
     NULL, NULL, NULL, NULL, (UBYTE *)"_Hair", NULL, GD_grid, PLACETEXT_IN, NULL, (APTR)CrossHair,
     NULL, NULL, NULL, NULL, (UBYTE *)"_Quit", NULL, GD_quit, PLACETEXT_IN, NULL, (APTR)quitClicked
};

struct NewGadget delwinNGad[] = {
     NULL, NULL, NULL, NULL, NULL, NULL, GD_dellist, NULL, NULL, (APTR)dellistClicked,
     NULL, NULL, NULL, NULL, (UBYTE *)"_Kill", NULL, GD_delset, PLACETEXT_IN, NULL, (APTR)delsetClicked,
     NULL, NULL, NULL, NULL, (UBYTE *)"_Redraw", NULL, GD_canlist, PLACETEXT_IN, NULL, (APTR)canlistClicked
};

struct NewGadget PreSlideNGad[] = {
	NULL, NULL, NULL, NULL, NULL, NULL, GD_pre, PLACETEXT_BELOW, NULL, (APTR)preClicked
};


ULONG PlotWindowGTags[] = {
     (GT_Underscore), '_', (TAG_DONE),
     (GT_Underscore), '_', (TAG_DONE),
     (GT_Underscore), '_', (TAG_DONE),
     (GT_Underscore), '_', (TAG_DONE),
     (GT_Underscore), '_', (TAG_DONE),
     (GT_Underscore), '_', (TAG_DONE)
};

ULONG delwinGTags[] = {
     (GTLV_ShowSelected), NULL, (TAG_DONE),
     (GT_Underscore), '_', (TAG_DONE),
     (GT_Underscore), '_', (TAG_DONE)
};

ULONG PreSlideGTags[] = {
	(GTSL_Min), 1, (GTSL_Max), 9, (GTSL_Level), NULL, (GTSL_MaxLevelLen), 3, (GTSL_LevelFormat), (ULONG)"%ld", (PGA_Freedom), LORIENT_HORIZ, (GA_RelVerify), TRUE, (TAG_DONE)
};


struct ColorSpec MonoColors[] = {
      0, 0x02, 0x01, 0x05,
      1, 0x0F, 0x0E, 0x0D,
     ~0, 0x00, 0x00, 0x00 };

struct ColorSpec FourColors[] = {
      0, 0x06, 0x07, 0x09,
      1, 0x0B, 0x0A, 0x0A,
      2, 0x03, 0x01, 0x03,
      3, 0x0F, 0x06, 0x06,
     ~0, 0x00, 0x00, 0x00 };

struct ColorSpec EightColors[] = {
      0, 0x08, 0x07, 0x08,
      1, 0x00, 0x00, 0x00,
      2, 0x0C, 0x0C, 0x0F,
      3, 0x09, 0x07, 0x08,
      4, 0x0A, 0x07, 0x0F,
      5, 0x0F, 0x0E, 0x08,
      6, 0x04, 0x04, 0x07,
      7, 0x0F, 0x0F, 0x0F,
     ~0, 0x00, 0x00, 0x00 };

struct ColorSpec SixteenColors[] = {
      0, 0x08, 0x07, 0x08,
      1, 0x00, 0x00, 0x00,
      2, 0x0F, 0x0F, 0x0F,
      3, 0x07, 0x06, 0x09,
      4, 0x08, 0x0B, 0x0F,
      5, 0x00, 0x0E, 0x08,
      6, 0x0F, 0x04, 0x08,
      7, 0x0F, 0x0D, 0x07,
      8, 0x0B, 0x07, 0x0F,
      9, 0x0F, 0x0A, 0x08,
     10, 0x0F, 0x08, 0x0D,
     11, 0x06, 0x00, 0x0F,
     12, 0x00, 0x0F, 0x0F,
     13, 0x0F, 0x05, 0x00,
     14, 0x05, 0x04, 0x07,
     15, 0x0F, 0x0F, 0x0F,
     ~0, 0x00, 0x00, 0x00 };


int SetupScreen( void )
{
UBYTE PubNum[4];
struct List    *publist;
struct PubScreenNode *psnode;
WORD  s=0,t;
WORD len;
struct ColorSpec *ScreenColors;
UBYTE PubName[32];

UWORD DriPens[] = {
     0,1,2,2,1,3,2,0,2,~0 };

UWORD TwoPens[] = {
     0,1,2,1,2,3,2,0,2,~0 };


     strcpy(PubName,MyName);  /* OpenScreen will fail if MyName has already been used */

     if ( Stricmp(PubName,&version_string[5])==0)	 /** if user didn't supply us a name **/
       {
	    strcpy(PubName,"cP");

	    len = (WORD) strlen( PubName );

	    publist = LockPubScreenList();                /* Lock the public screen list     */

	    for (psnode = (struct PubScreenNode *) publist->lh_Head; psnode->psn_Node.ln_Succ; psnode = (struct PubScreenNode *) psnode->psn_Node.ln_Succ)
     	 {
          	if ( ! Strnicmp( psnode-> psn_Node.ln_Name, PubName, len))
            	  {
	               t = 0;
     	          sscanf( &psnode-> psn_Node.ln_Name [len],"%hd", &t);
          	     s = max(s,t);
	            }
	      }

     	UnlockPubScreenList ();

	     sprintf( PubNum,"%hd", s+1 );
	     strcat ( PubName, PubNum);         /* Add #s of other cP screens to name */
	  }

     if ( depth == 1 ) ScreenColors = &MonoColors[0];
     if ( depth == 2 ) ScreenColors = &FourColors[0];
     if ( depth == 3 ) ScreenColors = &EightColors[0];
     if ( depth == 4 ) ScreenColors = &SixteenColors[0];

     if ( depth == 1 )
       {
          DriPens[0] = ~0;
       }


     if ( ! ( Scr = OpenScreenTags( NULL, SA_Left,     0,
                         SA_Top,        0,
                         SA_Width,      SWIDTH,
                         SA_Height,     SHEIGHT,
                         SA_Depth,      depth,
                         SA_Colors,     ScreenColors,
                         SA_Behind,	BEHIND,
                         SA_Font,       cPFont,
                         SA_Type,       PUBLICSCREEN,
                         SA_PubName,    PubName,
                         SA_DisplayID,  DEFAULT_MONITOR_ID | Disp,
                         SA_Overscan,   OSCAN_TEXT,
                         SA_AutoScroll, TRUE,
                         SA_Pens,       (depth == 2 ? &TwoPens[0] : &DriPens[0]),
                         SA_Title,      &version_string[5],
                         TAG_DONE )))
          return( 1L );

     if ( ! ( VisualInfo = GetVisualInfo( Scr, TAG_DONE )))
          return( 2L );

     PubScreenStatus( Scr, NULL );      /* make screen public */
     if ( DEFS ) SetDefaultPubScreen( PubName );  /* make screen default pub if DEFS True */

     SetRGB4( &Scr-> ViewPort, 17,7, 8,15);  /* set colors for pointerz */
     SetRGB4( &Scr-> ViewPort, 18,15,11,9);
     SetRGB4( &Scr-> ViewPort, 19,15,0,10);

     vp = &(Scr-> ViewPort);

     return( 0L );
}

void CloseDownScreen( void )
{
struct EasyStruct myezreq = {
       sizeof (struct EasyStruct), 0,
       "Close Public Screen",
       "Close All Windows on this Screen",
       "OK"
  };

     if ( VisualInfo ) {
          FreeVisualInfo( VisualInfo );
          VisualInfo = NULL;
     }
     if ( Scr ) {

          PubScreenStatus( Scr, PSNF_PRIVATE );   /* make the screen unavailable */

          while( ! CloseScreen( Scr ))
            {
               Forbid();
               EasyRequestArgs( Scr-> FirstWindow, &myezreq, NULL, NULL );
               Permit();
            }

     }
     Scr = NULL;
}

int HandlePlotWindowIDCMP( void )
{
     struct IntuiMessage *m;
     struct MenuItem          *n;
     int            (*func)();
     BOOL           running = TRUE;

     while( m = GT_GetIMsg( PlotWindowWnd->UserPort )) {

          CopyMem(( char * )m, ( char * )&PlotWindowMsg, (long)sizeof( struct IntuiMessage ));

          GT_ReplyIMsg( m );

          switch ( PlotWindowMsg.Class ) {

               case IDCMP_REFRESHWINDOW:
                    GT_BeginRefresh( PlotWindowWnd );
                    GT_EndRefresh( PlotWindowWnd, TRUE );
                    break;

               case IDCMP_VANILLAKEY:
                    running = PlotWindowVanillaKey();
                    break;

               case IDCMP_RAWKEY:
                    running = PlotWindowRawKey();
                    break;

               case IDCMP_MOUSEBUTTONS:
                    if (PlotWindowMsg.Code == SELECTDOWN)
                      {
               	      running = Measure(PlotWindowMsg.MouseX, PlotWindowMsg.MouseY);
               	  }
                    break;

               case IDCMP_GADGETUP:
                    func = ( void * )(( struct Gadget * )PlotWindowMsg.IAddress )->UserData;
                    running = func();
                    break;

               case IDCMP_MENUPICK:
                    while( PlotWindowMsg.Code != MENUNULL ) {
                         n = ItemAddress( PlotWindowMenus, PlotWindowMsg.Code );
                         func = (void *)(GTMENUITEM_USERDATA( n ));
                         running = func();
                         PlotWindowMsg.Code = n->NextSelect;
                    }
                    break;
          }
     }
     return( running );
}

int OpenPlotWindowWindow( void )
{
     struct NewGadget    ng;
     struct Gadget  *g;
     UWORD          lc, tc;
     WORD   GadTop;
	WORD BORD = 3; /* pixels at edge and bottom of control panel */

     if ( ! ( g = CreateContext( &PlotWindowGList )))
          return( 1L );

     GADWIDTH  = ( Scr-> Width - 2*BORD ) / PlotWindow_CNT;
     GADHEIGHT = cPFont->ta_YSize * 2;

     GadTop = Scr-> Height - Scr-> BarHeight -1  - cPFont->ta_YSize * 2 - BORD;

     for( lc = 0, tc = 0; lc < PlotWindow_CNT; lc++ ) {

          PlotWindowNGad[ lc ] . ng_LeftEdge = GADWIDTH * lc + BORD;
          PlotWindowNGad[ lc ] . ng_Width    = GADWIDTH;
          PlotWindowNGad[ lc ] . ng_TopEdge  = GadTop;
          PlotWindowNGad[ lc ] . ng_Height   = GADHEIGHT;

          CopyMem((char * )&PlotWindowNGad[ lc ], (char * )&ng, (long)sizeof( struct NewGadget ));

          ng.ng_VisualInfo = VisualInfo;
          ng.ng_TextAttr   = cPFont;

          PlotWindowGadgets[ lc ] = g = CreateGadgetA((ULONG)PlotWindowGTypes[ lc ], g, &ng, ( struct TagItem * )&PlotWindowGTags[ tc ] );

          while( PlotWindowGTags[ tc ] ) tc += 2;
          tc++;

          if ( NOT g )
               return( 2L );
     }

     if ( ! ( PlotWindowMenus = CreateMenus( PlotWindowNewMenu, GTMN_FrontPen, 0L, TAG_DONE )))
          return( 3L );

     LayoutMenus( PlotWindowMenus, VisualInfo, GTMN_TextAttr, cPFont, TAG_DONE );


     if ( ! ( PlotWindowWnd = OpenWindowTags( NULL,
                    WA_Left,  0,
                    WA_Top,        Scr-> BarHeight+1,
                    WA_Width, Scr-> Width,
                    WA_Height,     Scr-> Height - (Scr-> BarHeight+1),
                    WA_IDCMP, BUTTONIDCMP|IDCMP_MOUSEMOVE|IDCMP_MOUSEBUTTONS|IDCMP_MENUPICK|IDCMP_RAWKEY|IDCMP_VANILLAKEY,
                    WA_Flags, 	WFLG_SMART_REFRESH|WFLG_BACKDROP|WFLG_BORDERLESS,
                    WA_Activate,   ( BEHIND ? FALSE : TRUE),
                    WA_Gadgets,    ( CPANEL ? PlotWindowGList : NULL),
                    WA_ScreenTitle,     MyName,
                    WA_RptQueue,        0,
                    WA_AutoAdjust, TRUE,
                    WA_PubScreen,  Scr,
                    TAG_DONE )))
     return( 4L );

     SetMenuStrip( PlotWindowWnd, PlotWindowMenus );
     GT_RefreshWindow( PlotWindowWnd, NULL );

     rp = PlotWindowWnd-> RPort;

     PlotWindowWnd-> RPort-> Font = fontPtr;

	BEHIND = FALSE;

     return( 0L );
}

void ClosePlotWindowWindow( void )
{
     if ( PlotWindowMenus      ) {
          ClearMenuStrip( PlotWindowWnd );
          FreeMenus( PlotWindowMenus );
          PlotWindowMenus = NULL;  }

     if ( PlotWindowWnd        ) {
          CloseWindow( PlotWindowWnd );
          PlotWindowWnd = NULL;
     }

     if ( PlotWindowGList      ) {
          FreeGadgets( PlotWindowGList );
          PlotWindowGList = NULL;
     }
}

int HandledelwinIDCMP( void )
{
     struct IntuiMessage *m;
     int            (*func)();
     BOOL           running = TRUE;

     while( m = GT_GetIMsg( delwinWnd->UserPort )) {

          CopyMem(( char * )m, ( char * )&delwinMsg, (long)sizeof( struct IntuiMessage ));

          GT_ReplyIMsg( m );

          switch ( delwinMsg.Class ) {

               case IDCMP_REFRESHWINDOW:
                    GT_BeginRefresh( delwinWnd );
                    GT_EndRefresh( delwinWnd, TRUE );
                    break;

               case IDCMP_CLOSEWINDOW:
                    running = delwinCloseWindow();
                    break;

               case IDCMP_VANILLAKEY:
                    running = delwinVanillaKey();
                    break;

               case IDCMP_RAWKEY:
                    running = delwinRawKey();
                    break;

               case IDCMP_GADGETUP:
               case IDCMP_GADGETDOWN:
                    func = ( void * )(( struct Gadget * )delwinMsg.IAddress )->UserData;
                    running = func();
                    break;
          }
     }
     return( running );
}

int OpendelwinWindow( void )
{
     struct NewGadget    ng;
     struct Gadget  *g;
     UWORD          lc, tc;
     UWORD          offy = Scr->WBorTop + Scr->RastPort.TxHeight + 1;
     WORD           xo, yo;
     struct Rectangle rect;
	

	delwinWidth  = fontPtr->tf_XSize * 26;
	delwinHeight = cPFont->ta_YSize * 12;

	if (QueryOverscan(Disp, &rect, OSCAN_TEXT))
	  {

		 xo = -Scr->ViewPort.DxOffset+(rect.MaxX-rect.MinX)/2 - delwinWidth/2;
		 yo = -Scr->ViewPort.DyOffset+(rect.MaxY-rect.MinY)/2 - delwinHeight/2;
    	  }
	else xo = yo = 100;

     if ( ! ( g = CreateContext( &delwinGList )))
          return( 1L );

	delwinNGad[ 0 ] . ng_LeftEdge = Scr->WBorLeft;
     delwinNGad[ 0 ] . ng_Width    = delwinWidth - Scr->WBorLeft - Scr->WBorRight;
     delwinNGad[ 0 ] . ng_TopEdge  = offy;
     delwinNGad[ 0 ] . ng_Height   = delwinHeight-(2*cPFont->ta_YSize) - offy;

	delwinNGad[ 1 ] . ng_LeftEdge = Scr->WBorLeft;
     delwinNGad[ 1 ] . ng_Width    = (delwinWidth - Scr->WBorLeft - Scr->WBorRight)/2;
     delwinNGad[ 1 ] . ng_TopEdge  = delwinHeight-(2*cPFont->ta_YSize) - Scr->WBorBottom ;
     delwinNGad[ 1 ] . ng_Height   = (2*cPFont->ta_YSize);

	delwinNGad[ 2 ] . ng_LeftEdge = (delwinWidth - Scr->WBorLeft - Scr->WBorRight)/2 + Scr->WBorLeft;
     delwinNGad[ 2 ] . ng_Width    = (delwinWidth - Scr->WBorLeft - Scr->WBorRight)/2;
     delwinNGad[ 2 ] . ng_TopEdge  = delwinHeight-(2*cPFont->ta_YSize) - Scr->WBorBottom ;
     delwinNGad[ 2 ] . ng_Height   = (2*cPFont->ta_YSize);


     for( lc = 0, tc = 0; lc < delwin_CNT; lc++ ) {

          CopyMem((char * )&delwinNGad[ lc ], (char * )&ng, (long)sizeof( struct NewGadget ));

          ng.ng_VisualInfo = VisualInfo;
          ng.ng_TextAttr   = cPFont;

          delwinGadgets[ lc ] = g = CreateGadgetA((ULONG)delwinGTypes[ lc ], g, &ng, ( struct TagItem * )&delwinGTags[ tc ] );

          while( delwinGTags[ tc ] ) tc += 2;
          tc++;

          if ( NOT g )
               return( 2L );
     }

     if ( ! ( delwinWnd = OpenWindowTags( NULL,
                    WA_Left, xo,
                    WA_Top, yo,
                    WA_Width, delwinWidth,
                    WA_Height,     delwinHeight,
                    WA_IDCMP, LISTVIEWIDCMP|BUTTONIDCMP|IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY|IDCMP_RAWKEY,
                    WA_Flags, WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE,
                    WA_Gadgets,    delwinGList,
                    WA_Title, delwinWdt,
                    WA_ScreenTitle,     MyName,
                    WA_PubScreen,  Scr,
                    TAG_DONE )))
     return( 4L );

     GT_RefreshWindow( delwinWnd, NULL );


     return( 0L );
}

void ClosedelwinWindow( void )
{
     if ( delwinWnd        ) {
          CloseWindow( delwinWnd );
          delwinWnd = NULL;
     }

     if ( delwinGList      ) {
          FreeGadgets( delwinGList );
          delwinGList = NULL;
     }
}

int HandlePreSlideIDCMP( void )
{
	struct IntuiMessage	*m;
	int			(*func)();
	BOOL			running = TRUE;

	while( m = GT_GetIMsg( PreSlideWnd->UserPort )) {

		CopyMem(( char * )m, ( char * )&PreSlideMsg, (long)sizeof( struct IntuiMessage ));

		GT_ReplyIMsg( m );

		switch ( PreSlideMsg.Class ) {

			case	IDCMP_CLOSEWINDOW:
				running = PreSlideCloseWindow();
				break;

			case	IDCMP_GADGETUP:
			case	IDCMP_GADGETDOWN:
				func = ( void * )(( struct Gadget * )PreSlideMsg.IAddress )->UserData;
				running = func();
				break;
		}
	}
	return( running );
}

int OpenPreSlideWindow( void )
{
	struct NewGadget	ng;
	struct Gadget	*g;
	UWORD          PreSlideWidth = 300;
	UWORD          PreSlideHeight = 50;

	if ( ! ( g = CreateContext( &PreSlideGList )))
		return( 1L );

	CopyMem((char * )&PreSlideNGad[ 0 ], (char * )&ng, (long)sizeof( struct NewGadget ));

	PreSlideWidth += Scr->BarHeight + fontPtr->tf_XSize;
	PreSlideHeight += cPFont->ta_YSize;

	ng.ng_VisualInfo = VisualInfo;
	ng.ng_TextAttr   = cPFont;
	ng.ng_LeftEdge   = 40 + fontPtr->tf_XSize;
	ng.ng_TopEdge    = 20 + Scr->BarHeight;
	ng.ng_Width      = 220;
	ng.ng_Height     = 20;

	PreSlideGTags[ 5 ] = precision+1;

	PreSlideGadgets[ 0 ] = g = CreateGadgetA((ULONG)PreSlideGTypes[ 0 ], g, &ng, ( struct TagItem * )&PreSlideGTags[ 0 ] );

	if ( NOT g )
		return( 2L );

	if ( ! ( PreSlideWnd = OpenWindowTags( NULL,
				WA_Left,	Scr->Width/2 - PreSlideWidth/2,
				WA_Top,	Scr->Height/2 - PreSlideHeight/2,
				WA_Width,	PreSlideWidth,
				WA_Height,	PreSlideHeight + Scr->BarHeight ,
				WA_IDCMP,	SLIDERIDCMP|IDCMP_CLOSEWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE,
                    WA_PubScreen,  Scr,
				WA_Gadgets,	PreSlideGList,
				WA_Title,	PreSlideWdt,
				WA_ScreenTitle,	"Enter Significant Digits",
				TAG_DONE )))
	return( 4L );

	GT_RefreshWindow( PreSlideWnd, NULL );

	return( 0L );
}

void ClosePreSlideWindow( void )
{
	if ( PreSlideWnd        ) {
		CloseWindow( PreSlideWnd );
		PreSlideWnd = NULL;
	}

	if ( PreSlideGList      ) {
		FreeGadgets( PreSlideGList );
		PreSlideGList = NULL;
	}

}

