home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual 13 / CDA13.ISO / cdactual / demobin / share / program / C / ACTLIB12.ZIP / TVTOOLS.ZIP / TVSRC.ZIP / INREGEXP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-17  |  3.3 KB  |  133 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #define Uses_TEvent
  4. #define Uses_TStreamable
  5. #define Uses_MsgBox
  6. #define Uses_TInputRegExp
  7. #include "tvtools.h"
  8. __link( RInputLine )
  9.  
  10.  
  11. #include <ctype.h>
  12. #include <stdlib.h>
  13. #include <strstream.h>
  14.  
  15. #include "strings.h"
  16. #include "tools.h"
  17.  
  18.  
  19. // TInputRegExp
  20.  
  21. const char * const TInputRegExp::name = "TInputRegExp";
  22.  
  23. char *TInputRegExp::invMsg = " \n\03Invalid entry !";
  24.  
  25. TInputRegExp::TInputRegExp( const TRect& bounds,
  26.                             int aMaxLen,
  27.                             const char *aSet,
  28.                             const char *aRegexp,
  29.                             casetype aNewcase
  30.                           )
  31.              :TInputLine( bounds, aMaxLen )
  32. {
  33.   init( aSet, aRegexp, aNewcase );
  34. }
  35.  
  36.  
  37. TInputRegExp::TInputRegExp( int x, int y,
  38.                             int aMaxLen,
  39.                             const char *aSet,
  40.                             const char *aRegexp,
  41.                             casetype aNewcase
  42.                           )
  43.              :TInput1Line( x, y, aMaxLen )
  44. {
  45.   init( aSet, aRegexp, aNewcase );
  46. }
  47.  
  48.  
  49. void TInputRegExp::init( const char *aSet,
  50.                          const char *aRegexp,
  51.                          casetype aNewcase
  52.                        )
  53. {
  54.   if ( aSet ) set = strdup( aSet );
  55.               else set = 0;
  56.   if ( aRegexp ) regexp = strdup( aRegexp );
  57.             else regexp = 0;
  58.   newcase = aNewcase;
  59.   if ( newcase ) strcase( set, newcase );
  60. }
  61.  
  62.  
  63. TInputRegExp::~TInputRegExp()
  64. {
  65.   if ( set ) free( set );
  66.   if ( regexp ) free( regexp );
  67. }
  68.  
  69.  
  70. void TInputRegExp::setState( ushort aState, Boolean enable )
  71. {
  72.   static Boolean inUse = False;
  73.  
  74.   if ( (aState == sfFocused) && ! enable && ! inUse )
  75.      {
  76.        inUse = True;  // messageBox(),... in valid() could also call setState()
  77.        Boolean check = valid( cmReleasedFocus );
  78.        inUse = False;
  79.        if ( ! check ) return;
  80.      }
  81.  
  82.   TInputLine::setState( aState, enable );
  83. }
  84.  
  85.  
  86.  
  87. void TInputRegExp::handleEvent( TEvent& event )
  88. {
  89.   if ( (state & sfSelected) && (event.what == evKeyDown) )
  90.      {
  91.        uchar &key = event.keyDown.charScan.charCode;
  92.  
  93.        if ( key >= 0x20 )    // replace:   if ( isprint(key) )
  94.           {
  95.             if ( newcase ) key = chcase( key, newcase );
  96.  
  97.             if ( set && *set && ! matchset(key, set) )
  98.                {
  99.                  buzzer();
  100.                  clearEvent( event );
  101.                  return;
  102.                }
  103.            }
  104.      }
  105.  
  106.   TInputLine::handleEvent( event );
  107. }
  108.  
  109.  
  110. Boolean TInputRegExp::valid( ushort command )
  111. {
  112.   switch( command )
  113.         {
  114.           case cmReleasedFocus: return True;
  115.  
  116.           case cmQuit :
  117.           case cmClose:
  118.           case cmOK   : if ( regexp && *regexp )
  119.                            {
  120.                              if ( recursexp(data, regexp) != strlen(data) )
  121.                                 {
  122.                                   select();
  123.                                   if ( invMsg && *invMsg ) messageBox( invMsg, mfError | mfOKButton );
  124.                                   return False;
  125.                                 }
  126.                            }
  127.                         break;
  128.         }
  129.  
  130.   return TInputLine::valid(command);
  131. }
  132.  
  133.