/*
 *  Source machine generated by GadToolsBox V2.0b
 *  which is (c) Copyright 1991-1993 Jaba Development
 *
 *  GUI Designed by : (ray-1 of A((eSS
 */

#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 "Windows.h"

static struct Screen         *Scr = NULL;
static UBYTE                 *PubScreenName = NULL;
static APTR                   VisualInfo = NULL;
static struct Window         *RequesterWnd = NULL;
static struct Gadget         *RequesterGList = NULL;
static struct Gadget         *RequesterGadgets[9];
static UWORD                  RequesterLeft = 71;
static UWORD                  RequesterTop = 27;
static UWORD                  RequesterWidth = 295;
static UWORD                  RequesterHeight = 169;
static UBYTE                 *RequesterWdt = (UBYTE *)"Access File Requester";
static struct TextAttr       *Font, Attr;
static UWORD                  FontX, FontY;
static UWORD                  OffX, OffY;






#define Requester_TNUM 3

static UWORD RequesterGTypes[] = {
   STRING_KIND,
   SCROLLER_KIND,
   STRING_KIND,
   BUTTON_KIND,
   BUTTON_KIND,
   BUTTON_KIND,
   BUTTON_KIND,
   BUTTON_KIND,
   BUTTON_KIND
};

static struct NewGadget RequesterNGad[] = {
   68, 120, 193, 13, NULL, NULL, GD_PATH, 0, NULL, NULL,
   275, 2, 18, 115, NULL, NULL, GD_SLIDER, 0, NULL, NULL,
   6, 135, 254, 13, NULL, NULL, GD_FILENAME, 0, NULL, NULL,
   263, 120, 32, 12, (UBYTE *)"Read", NULL, GD_RE_READ, PLACETEXT_IN, NULL, NULL,
   263, 133, 32, 13, (UBYTE *)"Icons", NULL, GD_Icons, PLACETEXT_IN, NULL, NULL,
   3, 152, 65, 16, (UBYTE *)"Load", NULL, GD_POSITIVE, PLACETEXT_IN, NULL, NULL,
   70, 152, 67, 16, (UBYTE *)"Parent", NULL, GD_PARENT, PLACETEXT_IN, NULL, NULL,
   138, 152, 71, 16, (UBYTE *)"Volumes", NULL, GD_VOLUMES, PLACETEXT_IN, NULL, NULL,
   210, 152, 82, 16, (UBYTE *)"Cancel", NULL, GD_CANCEL, PLACETEXT_IN, NULL, NULL
};

static ULONG RequesterGTags[] = {
   (GTST_MaxChars), 256, (TAG_DONE),
   (GTSC_Arrows), 8, (PGA_Freedom), LORIENT_VERT, (GA_RelVerify), TRUE, (TAG_DONE),
   (GTST_MaxChars), 256, (TAG_DONE),
   (GT_Underscore), '_', (TAG_DONE),
   (GT_Underscore), '_', (TAG_DONE),
   (TAG_DONE),
   (TAG_DONE),
   (TAG_DONE),
   (TAG_DONE)
};

static UWORD ComputeX( UWORD value )
{
   return(( UWORD )((( FontX * value ) + 3 ) / 6 ));
}

static UWORD ComputeY( UWORD value )
{
   return(( UWORD )((( FontY * value ) + 4 ) / 9 ));
}

static void ComputeFont( UWORD width, UWORD height )
{
   Font = &Attr;
   Font->ta_Name = (STRPTR)Scr->RastPort.Font->tf_Message.mn_Node.ln_Name;
   Font->ta_YSize = FontY = Scr->RastPort.Font->tf_YSize;
   FontX = Scr->RastPort.Font->tf_XSize;

   OffX = Scr->WBorLeft;
   OffY = Scr->RastPort.TxHeight + Scr->WBorTop + 1;

   if ( width && height ) {
      if (( ComputeX( width ) + OffX + Scr->WBorRight ) > Scr->Width )
         goto UseTopaz;
      if (( ComputeY( height ) + OffY + Scr->WBorBottom ) > Scr->Height )
         goto UseTopaz;
   }
   return;

UseTopaz:
   Font->ta_Name = (STRPTR)"topaz.font";
   FontX = FontY = Font->ta_YSize = 8;
}

int SetupScreen( void )
{
   if ( ! ( Scr = LockPubScreen( PubScreenName )))
      return( 1L );

   ComputeFont( 0, 0 );

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

   return( 0L );
}

void CloseDownScreen( void )
{
   if ( VisualInfo ) {
      FreeVisualInfo( VisualInfo );
      VisualInfo = NULL;
   }

   if ( Scr        ) {
      UnlockPubScreen( NULL, Scr );
      Scr = NULL;
   }
}

void RequesterRender( void )
{
   

   ComputeFont( RequesterWidth, RequesterHeight );

   DrawBevelBox( RequesterWnd->RPort, OffX + ComputeX( 6 ),
               OffY + ComputeY( 120 ),
               ComputeX( 60 ),
               ComputeY( 12 ),
               GT_VisualInfo, VisualInfo, TAG_DONE );
   DrawBevelBox( RequesterWnd->RPort, OffX + ComputeX( 6 ),
               OffY + ComputeY( 2 ),
               ComputeX( 265 ),
               ComputeY( 116 ),
               GT_VisualInfo, VisualInfo, TAG_DONE );

  /* for ( cnt = 0; cnt < Requester_TNUM; cnt++ ) {
      CopyMem(( char * )&RequesterIText[ cnt ], ( char * )&it, (long)sizeof( struct IntuiText ));
      it.ITextFont = Font;
      it.LeftEdge  = OffX + ComputeX( it.LeftEdge ) - ( IntuiTextLength( &it ) >> 1 );
      it.TopEdge   = OffY + ComputeY( it.TopEdge ) - ( Font->ta_YSize >> 1 );
      PrintIText( RequesterWnd->RPort, &it, 0, 0 );
   }*/
}

int OpenRequesterWindow( void )
{
   struct NewGadget  ng;
   struct Gadget  *g;
   UWORD    lc, tc;
   UWORD    wleft = RequesterLeft, wtop = RequesterTop, ww, wh;

   ComputeFont( RequesterWidth, RequesterHeight );

   ww = ComputeX( RequesterWidth );
   wh = ComputeY( RequesterHeight );

   if (( wleft + ww + OffX + Scr->WBorRight ) > Scr->Width ) wleft = Scr->Width - ww;
   if (( wtop + wh + OffY + Scr->WBorBottom ) > Scr->Height ) wtop = Scr->Height - wh;

   if ( ! ( g = CreateContext( &RequesterGList )))
      return( 1L );
    
   for( lc = 0, tc = 0; lc < Requester_CNT; lc++ ) {

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

      ng.ng_VisualInfo = VisualInfo;
      ng.ng_TextAttr   = Font;
      ng.ng_LeftEdge   = OffX + ComputeX( ng.ng_LeftEdge );
      ng.ng_TopEdge    = OffY + ComputeY( ng.ng_TopEdge );
      ng.ng_Width      = ComputeX( ng.ng_Width );
      ng.ng_Height     = ComputeY( ng.ng_Height);

      RequesterGadgets[ lc ] = g = CreateGadgetA((ULONG)RequesterGTypes[ lc ], g, &ng, ( struct TagItem * )&RequesterGTags[ tc ] );

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

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

   if ( ! ( RequesterWnd = OpenWindowTags( NULL,
            WA_Left, wleft,
            WA_Top,     wtop,
            WA_Width,   ww + OffX + Scr->WBorRight,
            WA_Height,  wh + OffY + Scr->WBorBottom,
            WA_IDCMP,   STRINGIDCMP|SCROLLERIDCMP|ARROWIDCMP|BUTTONIDCMP|IDCMP_CLOSEWINDOW|IDCMP_DISKREMOVED|IDCMP_REFRESHWINDOW,
            WA_Flags,   WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE|WFLG_RMBTRAP,
            WA_Gadgets, RequesterGList,
            WA_Title,   RequesterWdt,
            WA_PubScreen,  Scr,
            TAG_DONE )))
   return( 4L );
    SetAPen(RequesterWnd->RPort,3);
    RectFill(RequesterWnd->RPort,7,12,250,180);
    RefreshGadgets(RequesterGList,RequesterWnd,0);
    GT_RefreshWindow(RequesterWnd,0);
   
   return( 0L );
}

void CloseRequesterWindow( void )
{
   if ( RequesterWnd        ) {
      CloseWindow( RequesterWnd );
      RequesterWnd = NULL;
   }

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

