#include <stdio.h>
#include <stdlib.h>
#include "IntegerGadget.h"
#include "IntegerGadgetClass.h"
#include "StringGadgetClass.h"
#include "minmax.h"
#include <proto/exec.h>
#include <proto/intuition.h>
#include "amigamem.h"



LONG IntegerGadget_Value( IntegerGadget *self )
{
   LONG selection;
   struct StringInfo *stringinfo;

   stringinfo = (StringInfo *) self->g.SpecialInfo;

   Forbid();
   sscanf( stringinfo->Buffer, "%d", &selection );
   Permit();

   return selection;
}

LONG IntegerGadget_SetValue( IntegerGadget *self, LONG selection )
{
   struct StringInfo *stringinfo;

   stringinfo = (StringInfo *) self->g.SpecialInfo;
   Forbid();
   sprintf( stringinfo->Buffer, "%d", selection );
   Permit();

   return selection;
}

#ifdef BUILDER
#include "BuilderMethods.h"
#include "GraphicObject_Builder.h"
#include "IntegerGadget_coder.h"
#include "StringGadget_Builder.h"

IntegerGadget *IntegerGadget_New( IntegerGadget *self )
{
   IntegerGadget *new_gadget;
   struct StringInfo *stringinfo;

   if (new_gadget = (IntegerGadget *) Amalloc( sizeof(IntegerGadget) ))
   {
      stringinfo = (struct StringInfo *) self->g.SpecialInfo;

       IntegerGadget_Init( new_gadget,
          self->Location.x, self->Location.y,
          self->Size.x, stringinfo->MaxChars-1,
          self->Pens,   Title(self) );
       new_gadget->g.Flags      = self->g.Flags;
       GiveItAName(new_gadget);
       SetStringValue( new_gadget, StringValue(self));

   }
   return new_gadget;
}

struct BuilderMethods IntegerGadget_bm;

#endif

BOOL IntegerGadget_elaborated = FALSE;

struct ValuatorClass IntegerGadget_Class;

void IntegerGadgetClass_Init( struct ValuatorClass *class )
{
   StringGadgetClass_Init( class );
   class->isa          = StringGadgetClass();
   class->ClassName    = "IntegerGadget";

   class->Value    = IntegerGadget_Value;
   class->SetValue = IntegerGadget_SetValue;

#ifdef BUILDER
   class->BuilderMethods = &IntegerGadget_bm;
   go_InitBuilderMethods( &IntegerGadget_bm );
   IntegerGadget_bm.New       = IntegerGadget_New;
   IntegerGadget_bm.PropEdit  = StringGadget_PropEdit;
   IntegerGadget_bm.WriteCode = IntegerGadget_WriteCode;
#endif
}


struct ValuatorClass *IntegerGadgetClass( void )
{
   if (! IntegerGadget_elaborated)
   {
      IntegerGadgetClass_Init( &IntegerGadget_Class );
      IntegerGadget_elaborated = TRUE;
   }

   return &IntegerGadget_Class;
}



void IntegerGadget_Init( IntegerGadget *self,
                         PIXELS        LeftEdge,
                         PIXELS        TopEdge,
                         PIXELS        Width,
                         USHORT        nChars,
                         pcg_3DPens    Pens,
                         char         *label )
{
   struct StringInfo *stringinfo;

   StringGadget_Init( self, LeftEdge, TopEdge, Width, nChars,
                        Pens, label );

   self->isa = IntegerGadgetClass();

   stringinfo = (struct StringInfo *) self->g.SpecialInfo;

   self->g.Activation |= LONGINT;
   SetValue( self, 0 );
}

