home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / vlstde / formatln.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-12  |  7.9 KB  |  353 lines

  1. /****************************************************************************
  2. *****************************************************************************
  3. ***                                                                       ***
  4. ***  TFormatLine - A formatted derivative of TInputLine                   ***
  5. ***                                                                       ***
  6. ***  Formatting Specifiers:                                               ***
  7. ***    U = Uppercase Alpha/Numeric                                        ***
  8. ***    u = Alpha/Numeric                                                  ***
  9. ***    A = Uppercase Alpha                                                ***
  10. ***    a = Alpha                                                          ***
  11. ***    N = Numeric                                                        ***
  12. ***                                                                       ***
  13. ***  Any other character passed in the format string will be treated as   ***
  14. ***  a literal character and will be displayed in the string              ***
  15. ***                                                                       ***
  16. *** Copyright 1991                                                        ***
  17. *** Golden E. Murray                                                      ***
  18. *** Spanish Fork, UT                                                      ***
  19. *** (801) 798-0223                                                        ***
  20. *****************************************************************************
  21. ****************************************************************************/
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include "formatln.hpp"
  25.  
  26. const int CONTROL_Y  = 25;
  27.  
  28. TFormatLine::TFormatLine( const TRect& bounds, const char *aFormat, int aDataLen):
  29.     TInputLine(bounds, strlen(aFormat) + 1), dataLen(aDataLen)
  30. {
  31.     format = new char[maxLen];
  32.     strcpy(format,aFormat);
  33.     //*** put format into data
  34.     for (int n = 0; n < maxLen; n++)
  35.     {
  36.         if (isInputPos(n))
  37.             data[n] = ' ';
  38.         else
  39.             data[n] = format[n];
  40.     }
  41.     data[n] = '\0';
  42.     out      = new char[dataLen];
  43.     memset(out, 0, dataLen);
  44. }
  45.  
  46. TFormatLine::~TFormatLine()
  47. {
  48.     if (format)
  49.         delete format;
  50.     if (out)
  51.         delete out;
  52. }
  53.  
  54. void TFormatLine::getData( void *rec )
  55. {
  56.     //*** Copy the formatted string to the out buffer
  57.     //*** without the formatting characters
  58.     int outPos = 0;
  59.     for (int n = 0; n < maxLen; n++)
  60.     {
  61.         switch (format[n])
  62.         {
  63.             case 'U':
  64.             case 'u':
  65.             case 'A':
  66.             case 'a':
  67.             case 'N':
  68.             case 'n':
  69.                 out[outPos++] = data[n];
  70.                 break;
  71.         }
  72.     }
  73.     out[outPos] = EOS;
  74.  
  75.     //*** Now copy the out buffer into the rec parameter
  76.     memcpy( rec, out, dataLen );
  77. }
  78.  
  79. void TFormatLine::handleEvent( TEvent& event )
  80. {
  81.     TView::handleEvent(event);
  82.     if ( (state & sfSelected) != 0 )
  83.     {
  84.         switch( event.what )
  85.         {
  86.             case evKeyDown:
  87.                 switch( ctrlToArrow(event.keyDown.keyCode) )
  88.                 {
  89.                     case kbLeft:
  90.                         CursorBack();
  91.                         break;
  92.  
  93.                     case kbRight:
  94.                         CursorForward();
  95.                         break;
  96.  
  97.                     case kbHome:
  98.                         curPos = -1;
  99.                         CursorForward();
  100.                         break;
  101.  
  102.                     case kbEnd:
  103.                         CursorEnd();
  104.                         break;
  105.  
  106.                     case kbBack:
  107.                         CursorBack();
  108.                         if( curPos >= 0 )
  109.                         {
  110.                             data[curPos] = ' ';
  111.                             if( firstPos > 0 )
  112.                                 firstPos--;
  113.                         }
  114.                         break;
  115.  
  116.                     case kbDel:
  117.                         atDelete(curPos);
  118.                         break;
  119.  
  120.                     default:
  121.                         if( event.keyDown.charScan.charCode >= ' ' )
  122.                         {
  123.                             deleteSelect();
  124.                             if( !isInputPos(curPos) )
  125.                                 CursorForward();
  126.                             if( !isInputPos(curPos) )
  127.                             {
  128.                                 clearEvent( event );
  129.                                 return;
  130.                             }
  131.                             //*** Check for valid input for position
  132.                             char thisChar = event.keyDown.charScan.charCode;
  133.                             switch (format[curPos])
  134.                             {
  135.                                 case 'A':
  136.                                     if ( !isalpha(thisChar) &&
  137.                                         (thisChar != ' '))
  138.                                         return;
  139.                                 case 'U':    thisChar = toupper(thisChar);
  140.                                     break;
  141.                                 case 'a':
  142.                                     if ( !isalpha(thisChar) &&
  143.                                         (thisChar != ' '))
  144.                                         return;
  145.                                     break;
  146.                                 case 'N':
  147.                                 case 'n': if ( !isdigit(thisChar) ) return;
  148.                             }
  149.                             //*** If we have gotton this far the character is valid
  150.                             data[curPos] = thisChar;
  151.                             int oldPos = curPos;
  152.                             CursorForward();
  153.                             if (oldPos == curPos)
  154.                             {
  155.                                 curPos = maxLen;
  156.                                 CursorBack();
  157.                                 curPos++;
  158.                             }
  159.                         }
  160.                         else if( event.keyDown.charScan.charCode == CONTROL_Y )
  161.                         {
  162.                             strcpy(data,format);
  163.                             CursorForward();
  164.                         }
  165.                         else
  166.                         {
  167.                             TInputLine::handleEvent(event);
  168.                             return;
  169.                         }
  170.                 }
  171.                 selStart = 0;
  172.                 selEnd = 0;
  173.                 if( firstPos > curPos )
  174.                     firstPos = curPos;
  175.                 int i = curPos - size.x + 3;
  176.                 if( firstPos < i )
  177.                     firstPos = i;
  178.                 drawView();
  179.                 clearEvent( event );
  180.                 break;
  181.  
  182.             default:
  183.                 TInputLine::handleEvent(event);
  184.         }
  185.     }
  186. }
  187.  
  188. void TFormatLine::setData( void *rec )
  189. {
  190.     //*** Copy the rec buffer to the data buffer
  191.     //*** expanding it to include the formatting characters
  192.     int recPos = 0;
  193.     int endRecFound = False;
  194.     for (int n = 0; n < maxLen; n++)
  195.     {
  196.         if( isInputPos(n) )
  197.         {
  198.             if (((char *)rec)[recPos] == '\0')
  199.                 endRecFound = True;
  200.             if (endRecFound)
  201.                 data[n] = ' ';
  202.             else
  203.                 data[n] = ((char *)rec)[recPos++];
  204.         }
  205.         else
  206.             data[n] = format[n];
  207.     }
  208.     data[maxLen] = EOS;
  209.     selectAll( True );
  210. }
  211.  
  212. int TFormatLine::CursorBack()
  213. {
  214.     int oldPos = curPos;
  215.     int tempPos = oldPos;
  216.     if( tempPos > 0 )
  217.     {
  218.         int foundPlace = False;
  219.         while( tempPos > 0 && !foundPlace )
  220.         {
  221.             if( isInputPos(--tempPos) )
  222.             {
  223.                 foundPlace = True;
  224.                 curPos = tempPos;
  225.             }
  226.         }
  227.     }
  228.     if (oldPos == curPos)
  229.         return False;
  230.     return True;
  231. }
  232.  
  233. int TFormatLine::CursorForward()
  234. {
  235.     int oldPos = curPos;
  236.     int tempPos = oldPos;
  237.     if( tempPos < maxLen )
  238.     {
  239.         int foundPlace = False;
  240.         while( tempPos < maxLen && !foundPlace )
  241.         {
  242.             if( isInputPos(++tempPos) )
  243.             {
  244.                 foundPlace = True;
  245.                 curPos = tempPos;
  246.             }
  247.         }
  248.     }
  249.     if (oldPos == curPos)
  250.     {
  251.         CursorEnd();
  252.         return False;
  253.     }
  254.     return True;
  255. }
  256.  
  257. int TFormatLine::isInputPos(int Position)
  258. {
  259.     switch( format[Position] )
  260.     {
  261.         case 'U':
  262.         case 'u':
  263.         case 'A':
  264.         case 'a':
  265.         case 'N':
  266.         case 'n':
  267.             return True;
  268.         default:
  269.             return False;
  270.     }
  271. }
  272.  
  273. void TFormatLine::atDelete(int deletePos)
  274. {
  275.     //*** make sure that we are on an input character position ***
  276.     if( !isInputPos(deletePos) )
  277.     {
  278.         CursorForward();
  279.         deletePos = curPos;
  280.     }
  281.     if( !isInputPos(deletePos) )
  282.         return;
  283.  
  284.     //*** Now move the input position characters down the the previous space ***
  285.     int lastPos = deletePos;
  286.     for (int n = lastPos; n < maxLen; n++)
  287.     {
  288.         if ( isInputPos(n) )
  289.         {
  290.             data[lastPos] = data[n];
  291.             lastPos = n;
  292.             data[n] = ' ';
  293.         }
  294.     }
  295. }
  296.  
  297. void TFormatLine::deleteSelect()
  298. {
  299.     if( selStart < selEnd )
  300.     {
  301.         //*** Find out how many format positions are in the select range
  302.         int selected = 0;
  303.         int selFirst = -1;
  304.         for(int n = selStart; n <= selEnd; n++ )
  305.         {
  306.             if (isInputPos(n))
  307.             {
  308.                 if (selFirst < 0)
  309.                     selFirst = n;
  310.                 selected++;
  311.             }
  312.         }
  313.  
  314.         //*** Now delete the number of selected items ***
  315.         for(n = 0; n++ < selected;)
  316.             atDelete(selFirst);
  317.  
  318.         if (selFirst >= 0)
  319.             curPos = selFirst;
  320.     }
  321. }
  322.  
  323. void TFormatLine::selectAll( Boolean enable )
  324. {
  325.     int charFound = False;
  326.     int curPos = -1;
  327.     selEnd = selStart = 0;
  328.     if ( enable )
  329.     {
  330.         //*** Check to see if any of the format positions are not space
  331.         CursorForward();
  332.         while ( CursorForward() && !charFound )
  333.         {
  334.             if ( data[curPos] != ' ' )
  335.                 charFound = True;
  336.         }
  337.         if (charFound)
  338.             selEnd = curPos;
  339.     }
  340.     else
  341.     {
  342.         curPos = selEnd = 0;
  343.     }
  344.     firstPos = max( 0, curPos-size.x+3 );
  345.     drawView();
  346. }
  347.  
  348. void TFormatLine::CursorEnd()
  349. {
  350.     curPos = maxLen;
  351.     while ( CursorBack() && data[curPos] == ' ') {}
  352.     if (data[curPos] != ' ') curPos++;
  353. }