This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinplong.pas
305 lines (256 loc) · 7.58 KB
/
inplong.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
Unit InpLong;
(*--
TInputLong is a derivitave of TInputline designed to accept LongInt
numeric input. Since both the upper and lower limit of acceptable numeric
input can be set, TInputLong may be used for Integer, Word, or Byte input
as well. Option flag bits allow optional hex input and display. A blank
field may optionally be rejected or interpreted as zero.
Methods
constructor Init(var R : TRect; AMaxLen : Integer;
LowerLim, UpperLim : LongInt; Flgs : Word);
Calls TInputline.Init and saves the desired limits and Flags. Flags may
be a combination of:
ilHex will accept hex input (preceded by '$') as well as decimal.
ilBlankEqZero if set, will interpret a blank field as '0'.
ilDisplayHex if set, will display numeric as hex when possible.
constructor Load(var S : TStream);
procedure Store(var S : TStream);
The usual Load and Store routines. Be sure to call RegisterType(RInputLong)
to register the type.
FUNCTION DataSize : Word; virtual;
PROCEDURE GetData(var Rec); virtual;
PROCEDURE SetData(var Rec); virtual;
The transfer methods. DataSize is Sizeof(LongInt) and Rec should be
the address of a LongInt.
FUNCTION RangeCheck : Boolean; virtual;
Returns True if the entered string evaluates to a number >= LowerLim and
<= UpperLim.
PROCEDURE Error; virtual;
Error is called when RangeCheck fails. It displays a messagebox indicating
the label (if any) of the faulting view, as well as the allowable range.
PROCEDURE HandleEvent(var Event : TEvent); virtual;
HandleEvent filters out characters which are not appropriate to numeric
input. Tab and Shift Tab cause a call to RangeCheck and a call to Error
if RangeCheck returns false. The input must be valid to Tab from the view.
There's no attempt made to stop moving to another view with the mouse.
FUNCTION Valid(Cmd : Word) : Boolean; virtual;
if TInputline.Valid is true and Cmd is neither cmValid or cmCancel, Valid
then calls RangeCheck. If RangeCheck is false, then Error is called and
Valid returns False.
----*)
{$i platform.inc}
{$ifdef PPC_FPC}
{$H-}
{$else}
{$F+,O+,E+,N+}
{$endif}
{$X+,R-,I-,Q-,V-}
{$ifndef OS_UNIX}
{$S-}
{$endif}
Interface
uses objects, drivers, views, dialogs, msgbox, fvconsts;
{flags for TInputLong constructor}
const
ilHex = 1; {will enable hex input with leading '$'}
ilBlankEqZero = 2; {No input (blank) will be interpreted as '0'}
ilDisplayHex = 4; {Number displayed as hex when possible}
Type
TInputLong = Object(TInputLine)
ILOptions : Word;
LLim, ULim : LongInt;
constructor Init(var R : TRect; AMaxLen : Sw_Integer;
LowerLim, UpperLim : LongInt; Flgs : Word);
constructor Load(var S : TStream);
procedure Store(var S : TStream);
FUNCTION DataSize : Sw_Word; virtual;
PROCEDURE GetData(var Rec); virtual;
PROCEDURE SetData(var Rec); virtual;
FUNCTION RangeCheck : Boolean; virtual;
PROCEDURE Error; virtual;
PROCEDURE HandleEvent(var Event : TEvent); virtual;
FUNCTION Valid(Cmd : Word) : Boolean; virtual;
end;
PInputLong = ^TInputLong;
const
RInputLong : TStreamRec = (
ObjType: idInputLong;
VmtLink: Ofs(Typeof(TInputLong)^);
Load : @TInputLong.Load;
Store : @TInputLong.Store);
Implementation
{-----------------TInputLong.Init}
constructor TInputLong.Init(var R : TRect; AMaxLen : Sw_Integer;
LowerLim, UpperLim : LongInt; Flgs : Word);
begin
if not TInputLine.Init(R, AMaxLen) then fail;
ULim := UpperLim;
LLim := LowerLim;
if Flgs and ilDisplayHex <> 0 then Flgs := Flgs or ilHex;
ILOptions := Flgs;
if ILOptions and ilBlankEqZero <> 0 then Data^ := '0';
end;
{-------------------TInputLong.Load}
constructor TInputLong.Load(var S : TStream);
begin
TInputLine.Load(S);
S.Read(ILOptions, Sizeof(ILOptions));
S.Read(LLim, Sizeof(LLim));
S.Read(ULim, Sizeof(ULim));
end;
{-------------------TInputLong.Store}
procedure TInputLong.Store(var S : TStream);
begin
TInputLine.Store(S);
S.Write(ILOptions, Sizeof(ILOptions));
S.Write(LLim, Sizeof(LLim));
S.Write(ULim, Sizeof(ULim));
end;
{-------------------TInputLong.DataSize}
FUNCTION TInputLong.DataSize:Sw_Word;
begin
DataSize := Sizeof(LongInt);
end;
{-------------------TInputLong.GetData}
PROCEDURE TInputLong.GetData(var Rec);
var code : Integer;
begin
Val(Data^, LongInt(Rec), code);
end;
FUNCTION Hex2(B : Byte) : String;
Const
HexArray : array[0..15] of char = '0123456789ABCDEF';
begin
Hex2[0] := #2;
Hex2[1] := HexArray[B shr 4];
Hex2[2] := HexArray[B and $F];
end;
FUNCTION Hex4(W : Word) : String;
begin Hex4 := Hex2(Hi(W))+Hex2(Lo(W)); end;
FUNCTION Hex8(L : LongInt) : String;
begin Hex8 := Hex4(LongRec(L).Hi)+Hex4(LongRec(L).Lo); end;
function FormHexStr(L : LongInt) : String;
var
Minus : boolean;
S : string[20];
begin
Minus := L < 0;
if Minus then L := -L;
S := Hex8(L);
while (Length(S) > 1) and (S[1] = '0') do Delete(S, 1, 1);
S := '$' + S;
if Minus then System.Insert('-', S, 2);
FormHexStr := S;
end;
{-------------------TInputLong.SetData}
PROCEDURE TInputLong.SetData(var Rec);
var
L : LongInt;
S : string;
begin
L := LongInt(Rec);
if L > ULim then L := ULim
else if L < LLim then L := LLim;
if ILOptions and ilDisplayHex <> 0 then
S := FormHexStr(L)
else
Str(L : -1, S);
if Length(S) > MaxLen then S[0] := chr(MaxLen);
Data^ := S;
end;
{-------------------TInputLong.RangeCheck}
FUNCTION TInputLong.RangeCheck : Boolean;
var
L : LongInt;
code : Integer;
begin
if (Data^ = '') and (ILOptions and ilBlankEqZero <> 0) then
Data^ := '0';
Val(Data^, L, code);
RangeCheck := (Code = 0) and (L >= LLim) and (L <= ULim);
end;
{-------------------TInputLong.Error}
PROCEDURE TInputLong.Error;
var
SU, SL : string[40];
PMyLabel : PLabel;
Labl : string;
I : Integer;
function FindIt(P : PView) : boolean;{$ifdef PPC_BP}far;{$endif}
begin
FindIt := (Typeof(P^) = Typeof(TLabel)) and (PLabel(P)^.Link = PView(@Self));
end;
begin
Str(LLim : -1, SL);
Str(ULim : -1, SU);
if ILOptions and ilHex <> 0 then
begin
SL := SL+'('+FormHexStr(LLim)+')';
SU := SU+'('+FormHexStr(ULim)+')';
end;
if Owner <> Nil then
PMyLabel := PLabel(Owner^.FirstThat(@FindIt))
else PMyLabel := Nil;
if PMyLabel <> Nil then PMyLabel^.GetText(Labl)
else Labl := '';
if Labl <> '' then
begin
I := Pos('~', Labl);
while I > 0 do
begin
System.Delete(Labl, I, 1);
I := Pos('~', Labl);
end;
Labl := '"'+Labl+'"';
end;
MessageBox(Labl + ^M^J'Value not within range '+SL+' to '+SU, Nil,
mfError+mfOKButton);
end;
{-------------------TInputLong.HandleEvent}
PROCEDURE TInputLong.HandleEvent(var Event : TEvent);
begin
if (Event.What = evKeyDown) then
begin
case Event.KeyCode of
kbTab, kbShiftTab
: if not RangeCheck then
begin
Error;
SelectAll(True);
ClearEvent(Event);
end;
end;
if Event.CharCode <> #0 then {a character key}
begin
Event.Charcode := Upcase(Event.Charcode);
case Event.Charcode of
'0'..'9', #1..#$1B : ; {acceptable}
'-' : if (LLim >= 0) or (CurPos <> 0) then
ClearEvent(Event);
'$' : if ILOptions and ilHex = 0 then ClearEvent(Event);
'A'..'F' : if Pos('$', Data^) = 0 then ClearEvent(Event);
else ClearEvent(Event);
end;
end;
end;
TInputLine.HandleEvent(Event);
end;
{-------------------TInputLong.Valid}
FUNCTION TInputLong.Valid(Cmd : Word) : Boolean;
var
Rslt : boolean;
begin
Rslt := TInputLine.Valid(Cmd);
if Rslt and (Cmd <> 0) and (Cmd <> cmCancel) then
begin
Rslt := RangeCheck;
if not Rslt then
begin
Error;
Select;
SelectAll(True);
end;
end;
Valid := Rslt;
end;
end.