home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-12-11 | 3.8 KB | 112 lines | [TEXT/PJMM] |
- unit xExpressionInput;
-
- { This unit defines two subclasses of xStringInput which are used for inputting }
- { mathematical expressions, as defined in the UNIT "expression" }
-
- interface
-
- uses
- xWindow, xInputDecoration, expression;
-
- type
- xExpressionInput = object(xStringInput)
- { Represents an input box to be used to type in a mathematical expression. }
- displayAlertOnError: boolean; { if this is true, then the user is informed when }
- { an error is found in the expression during a call to GetExpression; this }
- { is the default response. }
- procedure SetUp (win: xWindow;
- theLeft, theTop, theWidth, theHeight: integer);
- override;
- { create an expression box and install it in the specified window; the remaining }
- { parameters determine the location and size of the input box--see the commenst }
- { on the definition of this procedure in the calss xStringInput, and also see }
- { the comment on the definiton of procedure Install in the class xWindowDecoration }
- procedure GetExpression (var exp: expression;
- var err: boolean);
- { gets the expression typed in by the user, converted to an object of type }
- { expression (see the definition of this class in file expression.p); if the }
- { contents of the input box do not represent a legal expression, then the }
- { value of err is set to true, and exp is set to NIL--also, the user is informed }
- { of the error unless you set the value of DisplayAlertOnError to false. }
- procedure doKey (ch: char;
- modifiers: longint);
- override;
- { modifies doKey to reject illegal characters. }
- end;
-
- xConstantExpressionInput = object(xExpressionInput)
- { allows the user to input a constant expression (i.e., one involving no }
- { variables. }
- procedure GetValue (var val: extended;
- var err: boolean);
- { Retrieve the value of the expression typed in by the user; if the contents of }
- { the input box do not form a legal constant expression, then the value of Err is }
- { set to true--also, the user is informed of the error unless you set the value }
- { of DisplayAlertOnError to false.}
- end;
-
-
- implementation
-
- procedure xExpressionInput.GetExpression (var exp: expression;
- var err: boolean);
- var
- str: string;
- errPos: integer;
- errMessage: string;
- begin
- new(exp);
- GetContents(str);
- exp.CreateFromString(str, errPos, errMessage);
- if errPos > -1 then begin
- err := true;
- if displayAlertOnError then begin
- TellUser(StringOf('Error trying to read expression: ', errMessage));
- select;
- TESetSelect(errPos, errPos, TE);
- end;
- dispose(exp);
- end
- else
- err := false;
- end;
-
- procedure xExpressionInput.SetUp (win: xWindow;
- theLeft, theTop, theWidth, theHeight: integer);
- begin
- inherited setUp(win, theLeft, theTop, theWidth, theHeight);
- displayAlertOnError := true;
- end;
-
- procedure xExpressionInput.doKey (ch: char;
- modifiers: longint);
- begin
- if not wantsKey then
- EXIT(doKey);
- if ch in ['a'..'z', 'A'..'Z', '_', ' ', '[', ']', '{', '}', '(', ')', ',', '.', '+', '-', '*', '/', '^', 'π', '0'..'9', chr(8), chr(9), chr($1C)..chr($1F)] then
- inherited doKey(ch, modifiers)
- else
- Sysbeep(5);
- end;
-
-
- procedure xConstantExpressionInput.GetValue (var val: extended;
- var err: boolean);
- var
- exp: expression;
- begin
- val := errorVal;
- GetExpression(exp, err);
- if not err then begin
- if exp.isConstant then
- val := exp.value
- else if DisplayAlertOnError then begin
- TellUser('You must enter a constant, or a constant expression.');
- select;
- TESetSelect(0, 32000, TE);
- end;
- exp.kill;
- end;
- end;
-
- end.