home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 2.ddi / TVDEMOS.ZIP / FIELDS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  3.6 KB  |  158 lines

  1. /*-------------------------------------------------------*/
  2. /*                                                       */
  3. /*   Turbo Vision 1.0                                    */
  4. /*   Turbo Vision Forms Demo                             */
  5. /*   Copyright (c) 1991 by Borland International         */
  6. /*                                                       */
  7. /*   Fields.cpp: Support source file for TVFORMS demo    */
  8. /*-------------------------------------------------------*/
  9.  
  10. #define Uses_TStreamableClass
  11. #define Uses_TInputLine
  12. #define Uses_TStreamable
  13. #define Uses_MsgBox
  14. #include <tv.h>
  15. __link( RInputLine )
  16.  
  17. #if !defined( __FIELDS_H )
  18. #include "fields.h"
  19. #endif  // __FIELDS_H
  20.  
  21. #if !defined( __STRING_H )
  22. #include <string.h>
  23. #endif  // __STRING_H
  24.  
  25. #if !defined( __STDLIB_H )
  26. #include <stdlib.h>
  27. #endif  // __STDLIB_H
  28.  
  29. #if !defined( __STRSTREAM_H )
  30. #include <strstream.h>
  31. #endif  // __STRSTREAM_H
  32.  
  33.  
  34. // TKeyInputLine
  35.  
  36. const char *const TKeyInputLine::name = "TKeyInputLine";
  37.  
  38. TStreamable *TKeyInputLine::build()
  39. {
  40.     return new TKeyInputLine( streamableInit );
  41. }
  42.  
  43. TKeyInputLine::TKeyInputLine( const TRect& bounds, int aMaxLen ) :
  44.     TInputLine( bounds, aMaxLen )
  45. {
  46. }
  47.  
  48. TStreamableClass RKeyInputLine( TKeyInputLine::name,
  49.                                   TKeyInputLine::build,
  50.                                   __DELTA(TKeyInputLine)
  51.                                 );
  52.  
  53.  
  54. Boolean TKeyInputLine::valid( ushort command )
  55. {
  56.  
  57.     Boolean ok;
  58.  
  59.     ok = True;
  60.     if ( ( command != cmCancel) && ( command != cmValid ) )
  61.     if ( strlen( data ) == 0 )
  62.             {
  63.             select();
  64.             messageBox("This field cannot be empty.", mfError | mfOKButton);
  65.             ok = False;
  66.             }
  67.     if ( ok )
  68.         return TInputLine::valid(command);
  69.     else
  70.         return False;
  71. }
  72.  
  73. // TNumInputLine
  74.  
  75. const char * const TNumInputLine::name = "TNumInputLine";
  76.  
  77. void TNumInputLine::write( opstream& os )
  78. {
  79.     TInputLine::write( os );
  80.     os << min;
  81.     os << max;
  82.     
  83. }
  84.  
  85. void *TNumInputLine::read( ipstream& is )
  86. {
  87.     TInputLine::read( is );
  88.     is >> min;
  89.     is >> max;
  90.     return this;
  91. }
  92.  
  93. TStreamable *TNumInputLine::build()
  94. {
  95.     return new TNumInputLine( streamableInit );
  96. }
  97.  
  98.  
  99. TStreamableClass RNumInputLine( TNumInputLine::name,
  100.                                   TNumInputLine::build,
  101.                                   __DELTA(TNumInputLine)
  102.                                 );
  103.  
  104. TNumInputLine::TNumInputLine( const TRect& bounds,
  105.                   int aMaxLen,
  106.                   long aMin,
  107.                   long aMax ) :
  108.     TInputLine(bounds, aMaxLen)
  109. {
  110.     min = aMin;
  111.     max = aMax;
  112. }
  113.  
  114.  
  115. ushort TNumInputLine::dataSize()
  116. {
  117.     return sizeof(long);
  118. }
  119.  
  120. void TNumInputLine::getData( void *rec )
  121. {
  122.     *(long *)rec = atol(data);
  123. }
  124.  
  125. void TNumInputLine::setData( void *rec )
  126. {
  127.     ltoa(*(long *)rec, data, 10);
  128.     selectAll(True);
  129. }
  130.  
  131. Boolean TNumInputLine::valid( ushort command )
  132. {
  133.     long value;
  134.     Boolean ok;
  135.     char msg[80];
  136.     ostrstream os(msg, 80);
  137.  
  138.     ok = True;
  139.     if ( (command != cmCancel) && (command != cmValid) )
  140.         {
  141.         if (strlen(data) == 0)
  142.             strcpy(data,"0");
  143.         value = atol(data);
  144.         if ( (value == 0) || (value < min) || (value > max) )
  145.             {
  146.             select();
  147.             os << "Number must be from " << min << " to " << max << "." << ends;
  148.             messageBox(os.str(), mfError + mfOKButton);
  149.             selectAll(True);
  150.             ok = False;
  151.             }
  152.         }
  153.     if (ok)
  154.         return TInputLine::valid(command);
  155.     else
  156.         return False;
  157. }
  158.