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

  1. /*------------------------------------------------------------*/
  2. /* filename -       tldfield.cpp                              */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  TLongDoubleField 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_TLongDoubleField
  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 TLongDoubleField::name = "TLongDoubleField";
  65.  
  66. TLongDoubleField::TLongDoubleField( const TRect& bounds, int aMaxLen ) :
  67.     TField(bounds, aMaxLen),
  68.     value (0),
  69.     maxValue (0),
  70.     minValue (0),
  71.     formatString (NULL)
  72.     {
  73.     fieldType = TLongDoubleFieldType;
  74.     }
  75.  
  76. TLongDoubleField::~TLongDoubleField()
  77.     {
  78.     if (formatString != NULL)
  79.         delete formatString;
  80.     }
  81.  
  82. void TLongDoubleField::convertData( void )
  83.     {
  84.     value = atof(data);
  85.     char buffer[SizeLongDouble];
  86.     sprintf(buffer, (formatString == NULL) ? "%.2f" : formatString , value);
  87.     strnset( data, '\0', maxLen );
  88.     strncpy(data, buffer, maxLen);
  89.     data[maxLen] = EOS;
  90.     drawView();
  91.     dataChanged = False;
  92.     }
  93.  
  94. ushort TLongDoubleField::dataSize()
  95. {
  96.     return sizeof(value);
  97. }
  98.  
  99. ushort TLongDoubleField::filterCharCode( ushort charCode) //Virtual
  100. {
  101.     char buffer[SizeLongDouble];
  102.  
  103.     ushort retChar = 0;
  104.  
  105.     Boolean test = False;
  106.  
  107.     switch (charCode)
  108.         {
  109.         case '0':
  110.         case '1':
  111.         case '2':
  112.         case '3':
  113.         case '4':
  114.         case '5':
  115.         case '6':
  116.         case '7':
  117.         case '8':
  118.         case '9':
  119.         case '.':
  120.             retChar = charCode;
  121.             break;
  122.  
  123.         case '-':
  124.             if (    (selStart == 0) &&
  125.                     (selEnd == strlen(data)) )
  126.                 {
  127.                 if ((tfOptions & tfValidateMin) != 0 )
  128.                     if ( value > minValue )
  129.                         test = True;
  130.                     else
  131.                         test = False;
  132.                 else
  133.                     test = True;
  134.  
  135.                 if (test)
  136.                     {
  137.                     if (dataChanged)
  138.                         convertData();
  139.                     value--;
  140.                     sprintf(buffer, (formatString == NULL) ? "%.2f" : formatString , value);
  141.                     strnset( data, '\0', maxLen );
  142.                     strncpy( data, buffer, maxLen );
  143.                     data[maxLen] = EOS;
  144.                     drawView();
  145.                     }
  146.                 else
  147.                     if ((tfOptions & tfBeepError) != 0)
  148.                         beep();
  149.                 retChar = 1;
  150.                 }
  151.             else
  152.                 if (curPos == 0)
  153.                     retChar = charCode;
  154.                 else
  155.                     if ( (tfOptions & tfBeepError) != 0)
  156.                         beep();
  157.             break;
  158.  
  159.         case '+':
  160.             if (    (selStart == 0) &&
  161.                     (selEnd == strlen(data)) )
  162.                 {
  163.                 if ((tfOptions & tfValidateMax) != 0 )
  164.                     if ( value < maxValue )
  165.                         test = True;
  166.                     else
  167.                         test = False;
  168.                 else
  169.                     test = True;
  170.  
  171.                 if (test)
  172.                     {
  173.                     if (dataChanged)
  174.                         convertData();
  175.                     value++;
  176.                     sprintf(buffer, (formatString == NULL) ? "%.2f" : formatString , value);
  177.                     strnset( data, '\0', maxLen );
  178.                     strncpy( data, buffer, maxLen );
  179.                     data[maxLen] = EOS;
  180.                     drawView();
  181.                     }
  182.                 else
  183.                     if ((tfOptions & tfBeepError) != 0)
  184.                         beep();
  185.                 retChar = 1;
  186.                 }
  187.             else
  188.                 if ( (tfOptions & tfBeepError) != 0)
  189.                     beep();
  190.             break;
  191.  
  192.         case 0:
  193.             break;
  194.  
  195.         default:
  196.             if ((tfOptions & tfBeepError) != 0)
  197.                 beep();
  198.  
  199.         };
  200.  
  201.  
  202.     return retChar;
  203. }
  204.  
  205. void TLongDoubleField::getData( void *rec )
  206. {
  207.     convertData();
  208.     memcpy( rec, &value, sizeof(value) );
  209. }
  210.  
  211. Boolean TLongDoubleField::isValid( ushort command )
  212. {
  213.     Boolean status = True;
  214.     char buf[160];
  215.  
  216.     switch (command) {
  217.         case cmArriving:
  218.             status = True;
  219.             break;
  220.  
  221.         case cmLeaving:
  222.             if (     ((tfOptions & tfNotEmpty) != 0) &&
  223.                     ( strlen(data) == 0 ) ) {
  224.                 status = False;
  225.                 if ((tfOptions & tfBeepError) != 0)
  226.                     beep();
  227.                 messageBox("This field must contain a number!",
  228.                     mfWarning | mfOKButton );
  229.             }
  230.             else
  231.                 if (     ((tfOptions & tfValidateMax) != 0 ) &&
  232.                         ( value > maxValue ) ) {
  233.                     status = False;
  234.                     if ((tfOptions & tfBeepError) != 0)
  235.                         beep();
  236.                     ostrstream os( buf, sizeof( buf ) );
  237.                     os << "The number in this field must be less than or equal to "
  238.                         << maxValue << "!" << ends;
  239.                     messageBox(    buf, mfWarning | mfOKButton);
  240.                 }
  241.                 else
  242.                     if (     ((tfOptions & tfValidateMin) != 0 ) &&
  243.                             ( value < minValue ) ) {
  244.                         status = False;
  245.                         if ((tfOptions & tfBeepError) != 0)
  246.                             beep();
  247.                         ostrstream os( buf, sizeof( buf ) );
  248.                         os << "The number in this field must be greater than or equal to "
  249.                             << minValue << "!" << ends;
  250.                         messageBox(    buf, mfWarning | mfOKButton);
  251.                     };
  252.                 break;
  253.     };
  254.  
  255.     return status;
  256. }
  257.  
  258. ushort TLongDoubleField::processKeyCode( ushort keyCode ) //Virtual
  259.     {
  260.     ushort retKeyCode = keyCode;     // return a 1 to selectAll( true ) the field in the handleEvent routine
  261.  
  262.     char buffer[SizeLongDouble];
  263.  
  264.     if ( keyCode == kbPgDn )
  265.         if (    (selStart == 0) &&
  266.                 (selEnd == strlen(data)) )
  267.             {
  268.             if (dataChanged)
  269.                 convertData();
  270.             value += 10;
  271.             if (     ((tfOptions & tfValidateMax) != 0 ) &&
  272.                     ( value > maxValue ) )
  273.                 {
  274.                 value -= 10;
  275.                 if ((tfOptions & tfBeepError) != 0)
  276.                     beep();
  277.                 };
  278.             retKeyCode = 1;
  279.             sprintf(buffer, (formatString == NULL) ? "%.2f" : formatString , value);
  280.             strnset( data, '\0', maxLen );
  281.             strncpy( data, buffer, maxLen );
  282.             data[maxLen] = EOS;
  283.             drawView();
  284.             };
  285.  
  286.     if ( keyCode == kbPgUp )
  287.         if (    (selStart == 0) &&
  288.                 (selEnd == strlen(data)) )
  289.             {
  290.             if (dataChanged)
  291.                 convertData();
  292.             value -= 10;
  293.             if (     ((tfOptions & tfValidateMin) != 0 ) &&
  294.                     ( value < minValue ) )
  295.                 {
  296.                 value += 10;
  297.                 if ((tfOptions & tfBeepError) != 0)
  298.                     beep();
  299.                 };
  300.             retKeyCode = 1;
  301.             sprintf(buffer, (formatString == NULL) ? "%.2f" : formatString , value);
  302.             strnset( data, '\0', maxLen );
  303.             strncpy( data, buffer, maxLen );
  304.             data[maxLen] = EOS;
  305.             drawView();
  306.             };
  307.  
  308.     return retKeyCode;
  309.     }
  310.  
  311. void TLongDoubleField::setData( void *rec )
  312. {
  313.     char buffer[SizeLongDouble];
  314.     memcpy( &value, rec, sizeof(value) );
  315.     sprintf(buffer, (formatString == NULL) ? "%.2f" : formatString , value);
  316.     strnset( data, '\0', maxLen );
  317.     strncpy(data, buffer, maxLen);
  318.     data[maxLen] = EOS;
  319.     selectAll( True );
  320. }
  321.  
  322. void TLongDoubleField::setFormat( char *newFormat )
  323. {
  324.     if (formatString != NULL)
  325.         delete formatString;
  326.  
  327.     formatString = newStr( newFormat );
  328.  
  329.     char buffer[SizeLongDouble];
  330.     sprintf(buffer, (formatString == NULL) ? "%.2f" : formatString , value);
  331.     strnset( data, '\0', maxLen );
  332.     strncpy( data, buffer, maxLen );
  333.     data[maxLen] = EOS;
  334.     drawView();
  335. }
  336.  
  337. void TLongDoubleField::setMax( long double newMax )
  338. {
  339.     maxValue = newMax;
  340.     setTFOptions( tfValidateMax, True );
  341.  
  342.     if (minValue > maxValue)
  343.         minValue = maxValue;
  344. }
  345.  
  346. void TLongDoubleField::setMin( long double newMin )
  347. {
  348.     minValue = newMin;
  349.     setTFOptions( tfValidateMin, True );
  350.  
  351.     if (maxValue < minValue)
  352.         maxValue = minValue;
  353. }
  354.