home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / c / actlib11 / tvtools / stinplin.cpp < prev    next >
C/C++ Source or Header  |  1993-02-26  |  4KB  |  147 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #define Uses_TEvent
  4. #define Uses_TListViewer
  5. #define Uses_TPalette
  6. #define Uses_TInputRegExp
  7. #define Uses_TStaticInputLine
  8. #include "tvtools.h"
  9. #include "strings.h"
  10. #include "tools.h"
  11. #include <ctype.h>
  12.  
  13.  
  14. #define cpStaticInputLine "\x1A\x1A\x1C\x15"
  15.  
  16.  
  17. const char * const TStaticInputLine::name = "TStaticInputLine";
  18.  
  19.  
  20.  
  21. static Boolean matchChars( void *item, void *target )
  22. {
  23.   int len = strlen( (char *)target );
  24.   if ( ! len ) return False;
  25.  
  26.   return strncomp((char *)item, (char *)target, len) == 0 ;
  27. }
  28.  
  29.  
  30. TStaticInputLine::TStaticInputLine( const TRect& bounds,
  31.                                     int aMaxLen,
  32.                                     TGenCollection *aList
  33.                                   ):
  34.           TInputRegExp( bounds, aMaxLen )
  35. {
  36.   list = aList;
  37. }
  38.  
  39.  
  40. TStaticInputLine::TStaticInputLine( int x, int y,
  41.                                     int aMaxLen,
  42.                                     TGenCollection *aList
  43.                                   ):
  44.           TInputRegExp( x, y, aMaxLen )
  45. {
  46.   list = aList;
  47. }
  48.  
  49. void TStaticInputLine::setData( void *rec )
  50. {
  51.   if ( ! list->firstThat(matchChars, (char*)rec) )
  52.      (char*)rec = list->getData(0);
  53.  
  54.   TInputRegExp::setData( rec );
  55. }
  56.  
  57. void TStaticInputLine::handleEvent(TEvent& event)
  58. {
  59.   if ( event.what == evKeyDown )
  60.      {
  61.        if ( isprint(event.keyDown.charScan.charCode) )
  62.           {
  63.             // Add character to buffer string
  64.             char *tempData = strend( testChar );
  65.             *tempData++ = event.keyDown.charScan.charCode;
  66.             *tempData = EOS;
  67.  
  68.             tempData = (char *)list->firstThat(matchChars, testChar);
  69.             if ( tempData )
  70.                {
  71.                  strncpy( data, tempData, maxLen );
  72.                  data[maxLen] = EOS;
  73.                  selectAll( True );
  74.                  drawView();
  75.                }
  76.             else { testChar[strlen(testChar) - 1] = EOS;
  77.                    buzzer();
  78.                  }
  79.             clearEvent( event );
  80.             return;
  81.           }
  82.  
  83.        int off = 0;
  84.  
  85.        switch( event.keyDown.keyCode )
  86.         {
  87.           case kbLeft : off = -1;
  88.           case kbRight: if ( ! off ) off = 1;
  89.           case kbHome : if ( ! off ) off = - list->getCount();
  90.           case kbEnd  : if ( ! off ) off = list->getCount();
  91.  
  92.                         if ( list->getCount() )
  93.                            {
  94.                              ccIndex index = list->indexOf( (char *)list->firstThat(matchChars, data) );
  95.                              index += off;
  96.                          if ( index < 0 ) index = 0;
  97.                           if ( index >= list->getCount() ) index = list->getCount() - 1;
  98.                          strncpy( data, list->getData(index), maxLen );
  99.                              data[maxLen] = EOS;
  100.                            }
  101.  
  102.                         *testChar = EOS;
  103.  
  104.                     selectAll( True );
  105.                     drawView();
  106.                     clearEvent( event );
  107.                         return;
  108.  
  109.             case kbUp   :
  110.        case kbIns  : *testChar = EOS;
  111.                          clearEvent(event);
  112.                          return;
  113.  
  114.             case kbDel     :
  115.        case kbBack    : *testChar = EOS;
  116.                             break;
  117.          }
  118.      }
  119.  
  120.   if ( event.what == evMouseDown ) *testChar = EOS;
  121.  
  122.   TInputRegExp::handleEvent( event );
  123. }
  124.  
  125.  
  126. Boolean TStaticInputLine::valid( ushort command )
  127. {
  128.   if ( command == cmReleasedFocus ) *testChar = EOS;
  129.  
  130.   return TInputRegExp::valid( command );
  131. }
  132.  
  133. void TStaticInputLine::newList( TGenCollection *aList )
  134. {
  135.   if ( list ) destroy( list );
  136.   list = aList;
  137.   drawView();
  138. }
  139.  
  140. /*
  141. TPalette& TStaticInputLine::getPalette() const
  142. {
  143.   static TPalette palette( cpStaticInputLine, sizeof( cpStaticInputLine)-1 );
  144.   return palette;
  145. }
  146. */
  147.