Password (2)

Last updated 1995.03.04


Version
Not specified
Terms
TDialog; TInputLine; TPasswordInput; TPasswordInput:: draw()

To enter a password in a dialog box and to avoid having that password displayed can be achieved with the following code

   #define chrPassword        '*'
 
   TPasswordInput::TPasswordInput( const TRect& bounds, int maxLen ) :
       TInputLine( bounds, maxLen), starData( new char[ maxLen] ) {
       memset( starData, chrPassword, maxLen);     // fill with stars
       *(starData+maxLen-1) = EOS;                 // end of line
   }

   TPasswordInput::~TPasswordInput() {
       delete [] starData;
   }
 
   void TPasswordInput::draw( void) {
       char *origData;
       char *curEnd = starData + strlen( data);   // length of string
 
       *curEnd  = EOS;            // make '*'-string correct length
       origData = data;           // remember pointer;
       data     = starData;       // point to new data (all stars)
 
       TInputLine::draw();        // draw myself
 
       data     = origData;       // restore pointer
       *curEnd  = chrPassword;    // restore original '*'-string
   }