home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / c / actlib11 / tvtools / inint.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-19  |  2.4 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_TInputInt
  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. // TInputInt
  18.  
  19. const char * const TInputInt::name = "TInputInt";
  20.  
  21. void TInputInt::write( opstream& os )
  22. {
  23.   TInputLine::write( os );
  24.   os << min;
  25.   os << max;
  26. }
  27.  
  28. void *TInputInt::read( ipstream& is )
  29. {
  30.   TInputLine::read( is );
  31.   is >> min;
  32.   is >> max;
  33.   return this;
  34. }
  35.  
  36. TStreamable *TInputInt::build()
  37. {
  38.   return new TInputInt( streamableInit );
  39. }
  40.  
  41.  
  42. TStreamableClass RInputInt( TInputInt::name,
  43.                              TInputInt::build,
  44.                              __DELTA(TInputInt)
  45.                            );
  46.  
  47. TInputInt::TInputInt( const TRect& bounds,
  48.                       int aMaxLen,
  49.                       int aMin,
  50.               int aMax
  51.                     )
  52.           :TInputRegExp( bounds, aMaxLen, "-0-9" )
  53. {
  54.   min = aMin;
  55.   max = aMax;
  56. }
  57.  
  58.  
  59. TInputInt::TInputInt( int x,
  60.                       int y,
  61.                       int aMaxLen,
  62.                       int aMin,
  63.               int aMax
  64.                     )
  65.           :TInputRegExp( x, y, aMaxLen, "-0-9" )
  66. {
  67.   min = aMin;
  68.   max = aMax;
  69. }
  70.  
  71.  
  72. ushort TInputInt::dataSize()
  73. {
  74.   return sizeof( int );
  75. }
  76.  
  77. void TInputInt::getData( void *rec )
  78. {
  79.   *(int *)rec = atoi(data);
  80. }
  81.  
  82. void TInputInt::setData( void *rec )
  83. {
  84.   itoa(*(int *)rec, data, 10);
  85.   selectAll(True);
  86. }
  87.  
  88. Boolean TInputInt::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.                         int value = atoi( data );
  98.                         if ( (value < min) || (value > max) || (! value && *data) )
  99.                            {
  100.                              select();
  101.                              messageBox( mfError | mfOKButton,
  102.                                          "\03Number must be between %d and %d.",
  103.                                          min, max
  104.                                        );
  105.                              selectAll( True );
  106.                              return False;
  107.                            }
  108.                         break;
  109.         }
  110.  
  111.   return TInputRegExp::valid( command );
  112. }
  113.