home *** CD-ROM | disk | FTP | other *** search
/ Point Programming 1 / PPROG1.ISO / c / actlib11 / tvtools / indouble.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-19  |  2.5 KB  |  113 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #define Uses_TStreamable
  4. #define Uses_MsgBox
  5. #define Uses_TInputDouble
  6. #include "tvtools.h"
  7. __link( RInputLine )
  8.  
  9.  
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <values.h>
  13. #include <stdlib.h>
  14. #include <strstream.h>
  15.  
  16.  
  17.  
  18.  
  19. // TInputDouble
  20.  
  21. const char * const TInputDouble::name = "TInputDouble";
  22.  
  23. void TInputDouble::write( opstream& os )
  24. {
  25.   TInputLine::write( os );
  26.   os << min;
  27.   os << max;
  28. }
  29.  
  30. void *TInputDouble::read( ipstream& is )
  31. {
  32.   TInputLine::read( is );
  33.   is >> min;
  34.   is >> max;
  35.   return this;
  36. }
  37.  
  38. TStreamable *TInputDouble::build()
  39. {
  40.   return new TInputDouble( streamableInit );
  41. }
  42.  
  43.  
  44. TStreamableClass RInputDouble( TInputDouble::name,
  45.                                TInputDouble::build,
  46.                                __DELTA(TInputDouble)
  47.                              );
  48.  
  49. TInputDouble::TInputDouble( const TRect& bounds,
  50.                 int aMaxLen,
  51.                 double aMin,
  52.                 double aMax
  53.                           )
  54.              :TInputRegExp( bounds, aMaxLen, "-.0-9" )
  55. {
  56.   min = aMin;
  57.   max = aMax;
  58. }
  59.  
  60. TInputDouble::TInputDouble( int x, int y,
  61.                 int aMaxLen,
  62.                 double aMin,
  63.                 double aMax
  64.                           )
  65.              :TInputRegExp( x, y, aMaxLen, "-.0-9" )
  66. {
  67.   min = aMin;
  68.   max = aMax;
  69. }
  70.  
  71.  
  72. ushort TInputDouble::dataSize()
  73. {
  74.   return sizeof( double );
  75. }
  76.  
  77. void TInputDouble::getData( void *rec )
  78. {
  79.   *(double *)rec = atof( data );
  80. }
  81.  
  82. void TInputDouble::setData( void *rec )
  83. {
  84.   gcvt( *(double *)rec, MAXINT, data );
  85.   selectAll(True);
  86. }
  87.  
  88. Boolean TInputDouble::valid( ushort command )
  89. {
  90.   switch( command )
  91.         {
  92.           case cmReleasedFocus: return True;
  93.  
  94.           case cmQuit :
  95.           case cmClose:
  96.           case cmOK   : if ( ! *data ) strcpy( data, "0" );
  97.                         double value = atof( data );
  98.                         if ( (value < min) || (value > max) || (! value && *data) )
  99.                            {
  100.                              select();
  101.                              messageBox( mfError | mfOKButton,
  102.                                          "\03Number must be between %f and %f.",
  103.                                          min, max
  104.                                        );
  105.                              selectAll( True );
  106.                              return False;
  107.                            }
  108.                         break;
  109.         }
  110.  
  111.   return TInputRegExp::valid( command );
  112. }
  113.