/* $Id: INDOUBLE.CPP 2.4 1995/07/20 11:24:33 leon Exp $
 */
/*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */

#define Uses_TStreamable

#define Uses_MsgBox

#define Uses_TInputDouble

#include "input.h"

__link( RInputLine )


#include <string.h>

#include <stdio.h>

#include <values.h>

#include <stdlib.h>

#include <strstream.h>





const char * const TInputDouble::name = "TInputDouble";

/**
 ** Writes double to output stream
 **/
void TInputDouble::write( opstream& os )
{
 TInputLine::write( os );
 os << min;
 os << max;
}

/**
 ** Reads double from input stream
 **/
void *TInputDouble::read( ipstream& is )
{
 TInputLine::read( is );
 is >> min;
 is >> max;
 return this;
}

/**
 ** Boulds stream data
 **/
TStreamable *TInputDouble::build()
{
 return new TInputDouble( streamableInit );
}


TStreamableClass RInputDouble( TInputDouble::name,
                               TInputDouble::build,
                               __DELTA(TInputDouble)
                             );

/**
 ** Input of double value with limits /aMin/ and /aMax/
 **/
TInputDouble::TInputDouble( const TRect& bounds,
			    int aMaxLen,
			    double aMin,
			    double aMax
                          )
             :TInputLine( bounds, aMaxLen)
{
 min = aMin;
 max = aMax;
}


/**
 ** Input of double value with limits /aMin/ and /aMax/
 **/
TInputDouble::TInputDouble( int x, int y,
			    int aMaxLen,
			    double aMin,
			    double aMax
                          )
             :TInputLine( TRect(x, y, x + aMaxLen, y+1), aMaxLen)
{
 min = aMin;
 max = aMax;
}


/**
 ** Returns size of data
 **/
ushort TInputDouble::dataSize()
{
 return sizeof( double );
}

/**
 ** Gets data
 **/
void TInputDouble::getData( void *rec )
{
 *(double *)rec = atof( data );
}

/**
 ** Sets data
 **/
void TInputDouble::setData( void *rec )
{
 char buffer[256];
 sprintf( buffer, "%8.3f", *(double *)rec );
// sprintf( buffer, "%G", *(double *)rec );

 strncpy( data, buffer, maxLen );
 data[maxLen] = EOS;
 selectAll(True);
}

/**
 ** Validates data
 **/
Boolean TInputDouble::valid( ushort command )
{
  switch( command )
        {
          case cmReleasedFocus: return True;

          case cmQuit :
          case cmClose:
          case cmOK   : double value = atof( data );
                        if ( (! *data) || (value < min) || (value > max) )
                           {
                             select();
                             messageBox( mfError | mfOKButton,
                                         "\03Number must be between %G and %G.",
                                         min, max
                                       );
                             selectAll( True );
                             return False;
                           }
                        break;
        }

  return TInputLine::valid( command );
}


syntax highlighted by Code2HTML, v. 0.9.1