home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / c / actlib11 / tvtools / inregexp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-19  |  3.1 KB  |  127 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.   set = strdup( aSet );
  34.   regexp = strdup( aRegexp );
  35.   newcase = aNewcase;
  36.   if ( newcase ) strcase( set, newcase );
  37. }
  38.  
  39.  
  40. TInputRegExp::TInputRegExp( int x, int y,
  41.                             int aMaxLen,
  42.                             const char *aSet,
  43.                             const char *aRegexp,
  44.                             casetype aNewcase
  45.                           )
  46.              :TInput1Line( x, y, aMaxLen )
  47. {
  48.   if ( aSet ) set = strdup( aSet );
  49.               else set = 0;
  50.   if ( aRegexp ) regexp = strdup( aRegexp );
  51.             else regexp = 0;
  52.   newcase = aNewcase;
  53.   if ( newcase ) strcase( set, newcase );
  54. }
  55.  
  56.  
  57. TInputRegExp::~TInputRegExp()
  58. {
  59.   if ( set ) free( set );
  60.   if ( regexp ) free( regexp );
  61. }
  62.  
  63.  
  64. void TInputRegExp::setState( ushort aState, Boolean enable )
  65. {
  66.   static Boolean inUse = False;
  67.  
  68.   if ( (aState == sfFocused) && ! enable && ! inUse )
  69.      {
  70.        inUse = True;  // messageBox() in valid() will also call setState()
  71.        Boolean check = valid( cmReleasedFocus );
  72.        inUse = False;
  73.        if ( ! check ) return;
  74.      }
  75.  
  76.   TInputLine::setState( aState, enable );
  77. }
  78.  
  79.  
  80.  
  81. void TInputRegExp::handleEvent( TEvent& event )
  82. {
  83.   if ( (state & sfSelected) && (event.what == evKeyDown) )
  84.      {
  85.        uchar &key = event.keyDown.charScan.charCode;
  86.  
  87.        if ( isprint(key) )
  88.           {
  89.             if ( newcase ) key = chcase( key, newcase );
  90.  
  91.             if ( set && *set && ! matchset(key, set) )
  92.                {
  93.                  buzzer();
  94.                  clearEvent( event );
  95.                  return;
  96.                }
  97.            }
  98.      }
  99.  
  100.   TInputLine::handleEvent( event );
  101. }
  102.  
  103.  
  104. Boolean TInputRegExp::valid( ushort command )
  105. {
  106.   switch( command )
  107.         {
  108.           case cmReleasedFocus: return True;
  109.  
  110.           case cmQuit :
  111.           case cmClose:
  112.           case cmOK   : if ( regexp && *regexp )
  113.                            {
  114.                              if ( recursexp(data, regexp) != strlen(data) )
  115.                                 {
  116.                                   select();
  117.                                   if ( invMsg && *invMsg ) messageBox( invMsg, mfError | mfOKButton );
  118.                                   return False;
  119.                                 }
  120.                            }
  121.                         break;
  122.         }
  123.  
  124.   return TInputLine::valid(command);
  125. }
  126.  
  127.