home *** CD-ROM | disk | FTP | other *** search
- Unit EditTool;
-
- Interface
-
- Uses
- Crt;
-
- Type
- FilterType = Set of Char;
-
- Var
- filter : FilterType;
-
- Function ReadString ( x, y, max : Byte ) : String;
-
- Procedure BlankXY ( x, y, max : Byte );
-
- Procedure ResetFilter;
-
- Implementation
-
- Const
- Blank : String = ' ';
- BackSpace = #08; { Backspace key }
- Enter = #13; { Return/Enter key }
- CTRL_Y = #25; { Control - Y }
- Ins = #82; { Insert (INS) key }
- Del = #83; { Delete (DEL) key }
- LeftArr = #75; { Left arrow }
- RightArr = #77; { Right arrow }
- HomePos = #71; { Home key }
- EndPos = #79; { End key }
-
- Var
- inpSt : String; { Input string variable }
- ch : Char; { Dummy character variable }
- cp : Byte; { Current pointer inside input field }
- xPos, { Global X position of input field }
- yPos, { Global Y position of input field }
- maxChars : Byte; { Maximum characters to be input }
-
- Procedure BlankLine;
- { This procedure will write a blank line to the screen at the input }
- { field location, for maxChars. }
- Begin
- blank [ 0 ] := Chr ( maxChars );
- GotoXY ( xPos, yPos );
- Write ( blank );
- GotoXY ( xPos, yPos );
- End;
-
- Procedure BlankXY;
- Var
- storeAttr : Word;
- Begin
- storeAttr := TextAttr;
- TextColor ( Black );
- TextBackground ( LightGray );
- xPos := x;
- yPos := y;
- maxChars := max;
- BlankLine;
- TextAttr := storeAttr;
- End;
-
- Procedure DisplayString;
- { This procedure will write the input string to the screen at the }
- { field location, and preserve the cursor position. }
- Var
- xTmp,
- yTmp : Byte;
- Begin
- xTmp := WhereX;
- yTmp := WhereY;
- BlankLine;
- Write ( inpSt );
- GotoXY ( cp + xPos - 1, yTmp );
- End;
-
- Procedure ResetFilter;
- Begin
- filter := [#32..#255];
- End;
-
- Function ProcessChar ( funcKey : Boolean; ch : Char ): Boolean;
- { This function will process the character passed in through CH and }
- { take appropriate actions. If the character determines the end of }
- { input for that line, the function will return TRUE. }
- Var
- endInput : Boolean;
- Begin
- endInput := FALSE;
- Case ch of
- BackSpace : Begin { Destructive backspace }
- Delete ( inpSt, cp-1, 1 ); { -delete char before CP }
- Dec ( cp ); { -decrement the CP }
- If ( cp < 1 ) Then
- cp := 1;
- DisplayString; { -display the new string }
- End;
- Enter : Begin { End of line input }
- endInput := TRUE;
- End;
- CTRL_Y : Begin { Delete entire line }
- BlankLine; { -draw blank line }
- cp := 1; { -reset the CP }
- inpSt := ''; { -reset the input string }
- End;
- Ins : If funcKey Then { Insert a blank space }
- Begin
- Insert ( ' ', inpSt, cp ); { -insert the space char }
- If ( Length ( inpSt ) > maxChars ) Then { dont insert past }
- inpSt [ 0 ] := Chr ( maxChars ); { maxChars! }
- DisplayString; { -display the new string }
- End;
- Del : If funcKey Then { Delete character at CP }
- Begin
- Delete ( inpSt, cp, 1 ); { -delete char at CP }
- DisplayString; { -display the new string }
- End;
- HomePos : If funcKey Then { Move to start of string }
- Begin
- cp := 1; { -reset the CP }
- GotoXY ( cp + xPos -1, yPos ); { -reset the cursor }
- End;
- EndPos : If funcKey Then { Move to the end of the string }
- Begin
- cp := Ord ( inpSt [ 0 ] ); { -set the CP to string length }
- GotoXY ( cp + xPos, yPos ); { -move the cursor }
- End;
- LeftArr : If funcKey Then { Move cursor left one char }
- Begin
- Dec ( cp ); { -decrement the CP }
- If cp < 1 Then { -if at start of line, }
- cp := 1; { stay there }
- GotoXY ( cp + xPos - 1, yPos ); { -move the cursor }
- End;
- RightArr : If funcKey Then { Move cursor right one char }
- Begin
- Inc ( cp ); { -increase the CP }
- If cp > Ord ( inpSt [ 0 ] ) Then { -if at end of string, }
- cp := Ord ( inpSt [ 0 ] ) + 1; { stay there }
- GotoXY ( cp + xPos - 1, yPos ); { -move the cursor }
- End;
- End;
- ProcessChar := EndInput;
- End;
-
- Procedure GetLine;
- Var
- func,
- terminator : Boolean; { loop control variable }
- Begin
- cp := 1; { initialize the CP }
- terminator := FALSE; { initialize the loop control variable }
- Repeat
- ch := Readkey; { get keypress from user }
- func := FALSE; { initialize function key variable }
- If ( ch = #0 ) Then { check for an extended key press...}
- Begin
- ch := ReadKey;
- func := TRUE { An extended key was pressed }
- End
- Else
- If ( ch in filter ) Then { is it a valid character? }
- Begin
- inpSt [ cp ] := ch; { insert character at CP }
- If ( cp > Ord ( inpSt [ 0 ] ) ) Then { appending to end of string? }
- inpSt [ 0 ] := Chr ( Ord ( inpSt [ 0 ] ) + 1 ); { yes,increase length }
- Inc ( cp ); { increase the CP }
- Write ( ch ); { display the character }
- If ( cp > maxChars ) Then { have we past the maximum # of chars? }
- Begin
- Dec ( cp ); { Yes, decrease the CP }
- GotoXY ( cp + xPos - 1, yPos ); { move the cursor }
- End;
- End;
- terminator := ProcessChar ( func, ch ); { process the key press }
- Until terminator;
- End;
-
- Procedure EditLine;
- { This procedure sets up the input field and attributes }
- Var
- storeAttr : Word;
- Begin
- inpSt := '';
- storeAttr := TextAttr;
- TextColor ( Black );
- TextBackground ( White );
- BlankLine;
- GetLine;
- TextAttr := storeAttr;
- End;
-
- Function ReadString;
- Begin
- xPos := x;
- yPos := y;
- maxChars := max;
- EditLine;
- ReadString := inpSt;
- End;
-
- Begin
- ResetFilter;
- End.