home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / tfield / tlfield.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-25  |  7.4 KB  |  331 lines

  1. /*------------------------------------------------------------*/
  2. /* filename -       tlfield.cpp                               */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  TLongField member functions               */
  6. /*------------------------------------------------------------*/
  7.  
  8. /*------------------------------------------------------------*/
  9. /*                                                            */
  10. /*    Turbo Vision Extensions -- Version 1.1.1                */
  11. /*                                                            */
  12. /*                                                            */
  13. /*    Portions Copyright (c) 1991 by Borland International    */
  14. /*    All Rights Reserved.                                    */
  15. /*                                                            */
  16. /*    TV Extensions are Copyright (c) 1992 by Michael Bonner  */
  17. /*    These extensions may be freely used by any programmer   */
  18. /*    including royalty free inclusion in any commercial      */
  19. /*    application, but any commercial rights to the source    */
  20. /*    code or object files of the Extensions are reserved.    */
  21. /*                                                            */
  22. /*------------------------------------------------------------*/
  23.  
  24. #define Uses_ipstream
  25. #define Uses_MsgBox
  26. #define Uses_opstream
  27. #define Uses_TDeskTop
  28. #define Uses_TDialog
  29. #define Uses_TDrawBuffer
  30. #define Uses_TEvent
  31. #define Uses_TField
  32. #define Uses_TLongField
  33. #define Uses_TKeys
  34. #define Uses_TRect
  35. #define Uses_TView
  36.  
  37. #include <tv.h>
  38. #include "tfield.h"
  39.  
  40. #if !defined( __CTYPE_H )
  41. #include <ctype.h>
  42. #endif  // __CTYPE_H
  43.  
  44. #if !defined( __STRING_H )
  45. #include <String.h>
  46. #endif  // __STRING_H
  47.  
  48. #if !defined( __DOS_H )
  49. #include <Dos.h>
  50. #endif  // __DOS_H
  51.  
  52. #if !defined( __MEM_H )
  53. #include <Mem.h>
  54. #endif  // __MEM_H
  55.  
  56. #if !defined( __STDLIB_H )
  57. #include <Stdlib.h>
  58. #endif // __STDLIB_H
  59.  
  60. #if !defined( __STRSTREAM_H )
  61. #include <Strstream.h>
  62. #endif // __STRSTREAM_H
  63.  
  64. const char * const near TLongField::name = "TLongField";
  65.  
  66. TLongField::TLongField( const TRect& bounds, int aMaxLen ) :
  67.     TField(bounds, aMaxLen),
  68.     value (0),
  69.     maxValue (0),
  70.     minValue (0)
  71.     {
  72.     fieldType = TLongFieldType;
  73.     }
  74.  
  75. void TLongField::convertData( void )
  76. {
  77.     value = atol(data);
  78.     char buffer[SizeLong];
  79.     ltoa( value, buffer, 10);
  80.     strnset( data, '\0', maxLen );
  81.     strncpy(data, buffer, maxLen);
  82.     data[maxLen] = EOS;
  83.     drawView();
  84.     dataChanged = False;
  85. }
  86.  
  87. ushort TLongField::dataSize()
  88. {
  89.     return sizeof(value);
  90. }
  91.  
  92. ushort TLongField::filterCharCode( ushort charCode) //Virtual
  93. {
  94.     char buffer[SizeLong];
  95.  
  96.     ushort retChar = 0;
  97.  
  98.     Boolean test = False;
  99.  
  100.     switch (charCode)
  101.         {
  102.         case '0':
  103.         case '1':
  104.         case '2':
  105.         case '3':
  106.         case '4':
  107.         case '5':
  108.         case '6':
  109.         case '7':
  110.         case '8':
  111.         case '9':
  112.             retChar = charCode;
  113.             break;
  114.  
  115.         case '-':
  116.             if (    (selStart == 0) &&
  117.                     (selEnd == strlen(data)) )
  118.                 {
  119.                 if ((tfOptions & tfValidateMin) != 0 )
  120.                     if ( value > minValue )
  121.                         test = True;
  122.                     else
  123.                         test = False;
  124.                 else
  125.                     test = True;
  126.  
  127.                 if (test)
  128.                     {
  129.                     if (dataChanged)
  130.                         convertData();
  131.                     value--;
  132.                     ltoa(value, buffer, 10);
  133.                     strnset( data, '\0', maxLen );
  134.                     strncpy( data, buffer, maxLen );
  135.                     data[maxLen] = EOS;
  136.                     drawView();
  137.                     }
  138.                 else
  139.                     if ((tfOptions & tfBeepError) != 0)
  140.                         beep();
  141.                 retChar = 1;
  142.                 }
  143.             else
  144.                 if (curPos == 0)
  145.                     retChar = charCode;
  146.                 else
  147.                     if ( (tfOptions & tfBeepError) != 0)
  148.                         beep();
  149.             break;
  150.  
  151.         case '+':
  152.             if (    (selStart == 0) &&
  153.                     (selEnd == strlen(data)) )
  154.                 {
  155.                 if ((tfOptions & tfValidateMax) != 0 )
  156.                     if ( value < maxValue )
  157.                         test = True;
  158.                     else
  159.                         test = False;
  160.                 else
  161.                     test = True;
  162.  
  163.                 if (test)
  164.                     {
  165.                     if (dataChanged)
  166.                         convertData();
  167.                     value++;
  168.                     ltoa(value, buffer, 10);
  169.                     strnset( data, '\0', maxLen );
  170.                     strncpy( data, buffer, maxLen );
  171.                     data[maxLen] = EOS;
  172.                     drawView();
  173.                     }
  174.                 else
  175.                     if ((tfOptions & tfBeepError) != 0)
  176.                         beep();
  177.                 retChar = 1;
  178.                 }
  179.             else
  180.                 if ( (tfOptions & tfBeepError) != 0)
  181.                     beep();
  182.             break;
  183.  
  184.         case 0:
  185.             break;
  186.  
  187.         default:
  188.             if ((tfOptions & tfBeepError) != 0)
  189.                 beep();
  190.  
  191.         };
  192.  
  193.  
  194.     return retChar;
  195. }
  196.  
  197. void TLongField::getData( void *rec )
  198. {
  199.     convertData();
  200.     memcpy( rec, &value, sizeof(value) );
  201. }
  202.  
  203. Boolean TLongField::isValid( ushort command )
  204. {
  205.     Boolean status = True;
  206.     char buf[160];
  207.  
  208.     switch (command) {
  209.         case cmArriving:
  210.             status = True;
  211.             break;
  212.  
  213.         case cmLeaving:
  214.             if (     ((tfOptions & tfNotEmpty) != 0) &&
  215.                     ( strlen(data) == 0 ) ) {
  216.                 status = False;
  217.                 if ((tfOptions & tfBeepError) != 0)
  218.                     beep();
  219.                 messageBox("This field must contain a number!",
  220.                     mfWarning | mfOKButton );
  221.             }
  222.             else
  223.                 if (     ((tfOptions & tfValidateMax) != 0 ) &&
  224.                         ( value > maxValue ) ) {
  225.                     status = False;
  226.                     if ((tfOptions & tfBeepError) != 0)
  227.                         beep();
  228.                     ostrstream os( buf, sizeof( buf ) );
  229.                     os << "The number in this field must be less than "
  230.                         << (maxValue + 1) << "!" << ends;
  231.                     messageBox(    buf, mfWarning | mfOKButton);
  232.                 }
  233.                 else
  234.                     if (     ((tfOptions & tfValidateMin) != 0 ) &&
  235.                             ( value < minValue ) ) {
  236.                         status = False;
  237.                         if ((tfOptions & tfBeepError) != 0)
  238.                             beep();
  239.                         ostrstream os( buf, sizeof( buf ) );
  240.                         os << "The number in this field must be greater than "
  241.                             << (minValue + 1) << "!" << ends;
  242.                         messageBox(    buf, mfWarning | mfOKButton);
  243.                     };
  244.                 break;
  245.     };
  246.  
  247.     return status;
  248. }
  249.  
  250. ushort TLongField::processKeyCode( ushort keyCode ) //Virtual
  251.     {
  252.     ushort retKeyCode = keyCode;     // return a 1 to selectAll( true ) the field in the handleEvent routine
  253.  
  254.     char buffer[SizeLong];
  255.  
  256.     if ( keyCode == kbPgDn )
  257.         if (    (selStart == 0) &&
  258.                 (selEnd == strlen(data)) )
  259.             {
  260.             if (dataChanged)
  261.                 convertData();
  262.             value += 10;
  263.             if (     ((tfOptions & tfValidateMax) != 0 ) &&
  264.                     ( value > maxValue ) )
  265.                 {
  266.                 value -= 10;
  267.                 if ((tfOptions & tfBeepError) != 0)
  268.                     beep();
  269.                 };
  270.             retKeyCode = 1;
  271.             ltoa(value, buffer, 10);
  272.             strnset( data, '\0', maxLen );
  273.             strncpy( data, buffer, maxLen );
  274.             data[maxLen] = EOS;
  275.             drawView();
  276.             };
  277.  
  278.     if ( keyCode == kbPgUp )
  279.         if (    (selStart == 0) &&
  280.                 (selEnd == strlen(data)) )
  281.             {
  282.             if (dataChanged)
  283.                 convertData();
  284.             value -= 10;
  285.             if (     ((tfOptions & tfValidateMin) != 0 ) &&
  286.                     ( value < minValue ) )
  287.                 {
  288.                 value += 10;
  289.                 if ((tfOptions & tfBeepError) != 0)
  290.                     beep();
  291.                 };
  292.             retKeyCode = 1;
  293.             ltoa(value, buffer, 10);
  294.             strnset( data, '\0', maxLen );
  295.             strncpy( data, buffer, maxLen );
  296.             data[maxLen] = EOS;
  297.             drawView();
  298.             };
  299.  
  300.     return retKeyCode;
  301.     }
  302.  
  303. void TLongField::setData( void *rec )
  304. {
  305.     char buffer[SizeLong];
  306.     memcpy( &value, rec, sizeof(value) );
  307.     ltoa( value, buffer, 10);
  308.     strnset( data, '\0', maxLen );
  309.     strncpy(data, buffer, maxLen);
  310.     data[maxLen] = EOS;
  311.     selectAll( True );
  312. }
  313.  
  314. void TLongField::setMax( long newMax )
  315. {
  316.     maxValue = newMax;
  317.     setTFOptions( tfValidateMax, True );
  318.  
  319.     if (minValue > maxValue)
  320.         minValue = maxValue;
  321. }
  322.  
  323. void TLongField::setMin( long newMin )
  324. {
  325.     minValue = newMin;
  326.     setTFOptions( tfValidateMin, True );
  327.  
  328.     if (maxValue < minValue)
  329.         maxValue = minValue;
  330. }
  331.