home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DP Tool Club 26
/
CD_ASCQ_26_1295.iso
/
vrac
/
tvme30.zip
/
TINTEGER.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1995-08-02
|
3KB
|
143 lines
// File : TINTEGER.CPP
// Author : Eric Woodruff, CIS ID: 72134,1150
// Updated : Wed 08/02/95 17:54:19
// Note : Copyright 1994-95, Eric Woodruff, All rights reserved
// Compiler: Borland C++ 3.1 to 4.xx
//
// This is a simple integer inputline class.
// It is the same as TInputLine, except it accepts only valid numeric input
// and performs a numeric range check.
//
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define Uses_MsgBox
#define Uses_TEvent
#define Uses_TInputLine
#define Uses_TKeys
#define Uses_TStreamableClass
#define Uses_ipstream
#define Uses_opstream
#include <tv.h>
__link( RView )
#define Uses_TInteger
#include <tintfile.h>
TStreamableClass RInteger( TInteger::name,
TInteger::build, __DELTA(TInteger) );
const char * const _NEAR TInteger::name = "TInteger";
TInteger::TInteger(const TRect &bounds, int aMaxLen, short maxVal,
short minVal) :
TInputLine(bounds, aMaxLen),
max_val(maxVal), min_val(minVal)
{
}
void TInteger::handleEvent(TEvent &event)
{
if(event.what == evKeyDown)
switch(event.keyDown.keyCode)
{
case kbTab:
if(!valid(cmOK))
clearEvent(event);
break;
case kbShiftTab:
if(!valid(cmOK))
clearEvent(event);
break;
case kbBack:
case kbEnter:
case kbEsc:
break; // Let TInputLine have these.
default:
short key = event.keyDown.charScan.charCode;
if(key)
{
if(key == '-')
{
// Don't accept sign if past first position or
// if there is already one there.
if((curPos && curPos != selEnd) || *data == '-')
clearEvent(event);
break;
}
// Only accept digits 0-9.
if(!isdigit(key))
clearEvent(event);
}
break;
}
TInputLine::handleEvent(event);
}
ushort TInteger::dataSize()
{
return sizeof(short);
}
void TInteger::getData(void *rec)
{
*(short *)rec = atoi(data);
}
void TInteger::setData(void *rec)
{
itoa(*(short *)rec, data, 10);
selectAll(True);
}
Boolean TInteger::valid(ushort command)
{
short value;
if(command != cmCancel && command != cmValid)
{
value = atoi(data); // Get the value.
if(value < min_val || value > max_val)
{
messageBox(mfError | mfOKButton,"Number must be between %d and %d!",
min_val, max_val);
select();
return False;
}
}
return TInputLine::valid(command);
}
void TInteger::write( opstream& os )
{
TInputLine::write(os);
os << min_val << max_val;
}
void *TInteger::read( ipstream& is )
{
TInputLine::read(is);
is >> min_val >> max_val;
return this;
}
TStreamable *TInteger::build()
{
return new TInteger( streamableInit );
}
TInteger::TInteger( StreamableInit ) : TInputLine( streamableInit )
{
}