home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / vrac / ve2tv103.zip / TINTEGER.CPP < prev    next >
C/C++ Source or Header  |  1994-07-31  |  3KB  |  143 lines

  1. // File    : TINTEGER.CPP
  2. // Author  : Eric Woodruff,  CIS ID: 72134,1150
  3. // Updated : Sun 07/31/94 15:56:28
  4. // Note    : Copyright 1994, Eric Woodruff, All rights reserved
  5. // Compiler: Borland C++ 3.1 to 4.02
  6. //
  7. // This is a simple integer inputline class.
  8. // It is the same as TInputLine, except it accepts only valid numeric input
  9. // and performs a numeric range check.
  10. //
  11.  
  12. #include <ctype.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. #define Uses_MsgBox
  17. #define Uses_TEvent
  18. #define Uses_TInputLine
  19. #define Uses_TKeys
  20. #define Uses_TStreamableClass
  21. #define Uses_ipstream
  22. #define Uses_opstream
  23. #include <tv.h>
  24.  
  25. __link( RView )
  26.  
  27. #define Uses_TInteger
  28. #include <tintfile.h>
  29.  
  30. TStreamableClass RInteger( TInteger::name,
  31.     TInteger::build, __DELTA(TInteger) );
  32.  
  33. const char * const _NEAR TInteger::name = "TInteger";
  34.  
  35. TInteger::TInteger(const TRect &bounds, int aMaxLen, short maxVal,
  36.   short minVal) :
  37.     TInputLine(bounds, aMaxLen),
  38.     max_val(maxVal), min_val(minVal)
  39. {
  40. }
  41.  
  42. void TInteger::handleEvent(TEvent &event)
  43. {
  44.     if(event.what == evKeyDown)
  45.         switch(event.keyDown.keyCode)
  46.         {
  47.             case kbTab:
  48.                 if(!valid(cmOK))
  49.                     clearEvent(event);
  50.                 break;
  51.  
  52.             case kbShiftTab:
  53.                 if(!valid(cmOK))
  54.                     clearEvent(event);
  55.                 break;
  56.  
  57.             case kbBack:
  58.             case kbEnter:
  59.             case kbEsc:
  60.                 break;      // Let TInputLine have these.
  61.  
  62.             default:
  63.                 short key = event.keyDown.charScan.charCode;
  64.  
  65.                 if(key)
  66.                 {
  67.                     if(key == '-')
  68.                     {
  69.                         // Don't accept sign if past first position or
  70.                         // if there is already one there.
  71.                         if((curPos && curPos != selEnd) || *data == '-')
  72.                             clearEvent(event);
  73.  
  74.                         break;
  75.                     }
  76.  
  77.                     // Only accept digits 0-9.
  78.                     if(!isdigit(key))
  79.                         clearEvent(event);
  80.                 }
  81.                 break;
  82.         }
  83.     TInputLine::handleEvent(event);
  84. }
  85.  
  86. ushort TInteger::dataSize()
  87. {
  88.     return sizeof(short);
  89. }
  90.  
  91. void TInteger::getData(void *rec)
  92. {
  93.     *(short *)rec = atoi(data);
  94. }
  95.  
  96. void TInteger::setData(void *rec)
  97. {
  98.     itoa(*(short *)rec, data, 10);
  99.     selectAll(True);
  100. }
  101.  
  102. Boolean TInteger::valid(ushort command)
  103. {
  104.     short value;
  105.  
  106.     if(command != cmCancel && command != cmValid)
  107.     {
  108.         value = atoi(data);     // Get the value.
  109.  
  110.         if(value < min_val || value > max_val)
  111.         {
  112.             messageBox(mfError | mfOKButton,"Number must be between %d and %d!",
  113.                 min_val, max_val);
  114.  
  115.             select();
  116.             return False;
  117.         }
  118.     }
  119.     return TInputLine::valid(command);
  120. }
  121.  
  122. void TInteger::write( opstream& os )
  123. {
  124.     TInputLine::write(os);
  125.     os << min_val << max_val;
  126. }
  127.  
  128. void *TInteger::read( ipstream& is )
  129. {
  130.     TInputLine::read(is);
  131.     is >> min_val >> max_val;
  132.     return this;
  133. }
  134.  
  135. TStreamable *TInteger::build()
  136. {
  137.     return new TInteger( streamableInit );
  138. }
  139.  
  140. TInteger::TInteger( StreamableInit ) : TInputLine( streamableInit )
  141. {
  142. }
  143.