-
Notifications
You must be signed in to change notification settings - Fork 1
/
unitforminputpokes.pas
323 lines (270 loc) · 8.14 KB
/
unitforminputpokes.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
unit UnitFormInputPokes;
// Copyright 2022-2024 Zoran Vučenović
// SPDX-License-Identifier: Apache-2.0
{$mode ObjFPC}{$H+}
{$i zxinc.inc}
interface
uses
Classes, SysUtils, CommonFunctionsLCL, UnitCommon, Forms, Controls, Graphics,
Dialogs, Grids, LCLType, ExtCtrls, ButtonPanel;
type
TPokeEntry = record
Adr: Word;
Value: Byte;
end;
TPokesArray = Array of TPokeEntry;
TFormInputPokes = class(TForm)
ButtonPanel1: TButtonPanel;
Panel1: TPanel;
Panel2: TPanel;
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
strict private
type
TGridNums = class(TStringGrid)
protected
function EditorCanAcceptKey(const Ch: TUTF8Char): Boolean; override;
function ValidateEntry(const ACol, ARow: Integer;
const OldValue: string; var NewValue: string): Boolean; override;
public
MinAddress: Word;
MaxAddress: Word;
constructor Create(AOwner: TComponent); override;
function CheckValue(const ACol: Integer; const AValue: String; out N: Int32
): Boolean;
procedure DefaultDrawCell(aCol, aRow: Integer; var aRect: TRect;
aState: TGridDrawState); override;
procedure EditingDone; override;
end;
strict private
FPokes: TPokesArray;
GridNums: TGridNums;
procedure GridOnButtonClick(Sender: TObject; aCol, aRow: Integer);
procedure AfterShow(Data: PtrInt);
public
class function ShowInputPokesDialog(const AMinAddress, AMaxAddress: Word; out APokes: TPokesArray): Boolean;
end;
implementation
{$R *.lfm}
{ TFormInputPokes.TGridNums }
function TFormInputPokes.TGridNums.EditorCanAcceptKey(const Ch: TUTF8Char
): Boolean;
begin
if Length(Ch) = 1 then
case Ch[1] of
'0'..'9', AnsiChar(VK_BACK):
Exit(inherited EditorCanAcceptKey(Ch));
otherwise
end;
Result := False;
end;
function TFormInputPokes.TGridNums.ValidateEntry(const ACol, ARow: Integer;
const OldValue: string; var NewValue: string): Boolean;
var
N: Int32;
begin
Result := inherited ValidateEntry(ACol, ARow, OldValue, NewValue)
and CheckValue(ACol, Trim(NewValue), N);
if not Result then
EditordoResetValue;
end;
function TFormInputPokes.TGridNums.CheckValue(const ACol: Integer;
const AValue: String; out N: Int32): Boolean;
var
MinVal, MaxVal: Integer;
begin
if AValue = '' then
Exit(True);
if ACol = FixedCols then begin
MinVal := MinAddress;
MaxVal := MaxAddress;
end else begin
MinVal := Byte.MinValue;
MaxVal := Byte.MaxValue;
end;
Result := TCommonFunctions.TryStrToIntDecimal(AValue, N)
and (N >= MinVal) and (N <= MaxVal);
end;
procedure TFormInputPokes.TGridNums.DefaultDrawCell(aCol, aRow: Integer;
var aRect: TRect; aState: TGridDrawState);
var
TS: TTextStyle;
begin
if (ACol = FixedCols + 2) and (aRow >= FixedRows) then begin
inherited DefaultDrawCell(aCol, aRow, aRect, aState);
TS := Canvas.TextStyle;
TS.Layout := TTextLayout.tlCenter;
TS.Alignment := TAlignment.taCenter;
TS.Opaque := False;
TS.Wordbreak := False;
Canvas.TextRect(ARect, ARect.Left, ARect.Top, 'delete', TS);
end else begin
if aCol < FixedCols then begin
TS := Canvas.TextStyle;
TS.Alignment := TAlignment.taRightJustify;
Canvas.TextStyle := TS;
end;
inherited DefaultDrawCell(aCol, aRow, aRect, aState);
end;
end;
procedure TFormInputPokes.TGridNums.EditingDone;
begin
inherited EditingDone;
if Row = RowCount - 1 then
if not IsEmptyRow(Row) then
RowCount := RowCount + 1;
end;
constructor TFormInputPokes.TGridNums.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ValidateOnSetSelection := True;
Options := Options + [goAlwaysShowEditor, goAutoAddRows, goColSizing, goEditing, goTabs, goFixedRowNumbering];
FixedCols := 1;
FixedRows := 1;
Flat := True;
TitleStyle := tsNative;
RowCount := FixedRows + 1;
end;
{ TFormInputPokes }
procedure TFormInputPokes.FormCreate(Sender: TObject);
begin
GridNums := TGridNums.Create(Panel2);
GridNums.AnchorParallel(akTop, 0, Panel2);
GridNums.AnchorParallel(akLeft, 0, Panel2);
GridNums.AnchorParallel(akRight, 0, Panel2);
GridNums.AnchorParallel(akBottom, 0, Panel2);
GridNums.Parent := Panel2;
GridNums.OnButtonClick := @GridOnButtonClick;
AfterShow(-1);
end;
procedure TFormInputPokes.FormShow(Sender: TObject);
begin
AfterShow(1);
end;
procedure TFormInputPokes.GridOnButtonClick(Sender: TObject; aCol,
aRow: Integer);
begin
if (aCol = GridNums.FixedCols + 2) and (aRow >= GridNums.FixedRows) then begin
if aRow < GridNums.RowCount - 1 then
GridNums.DeleteRow(aRow)
else begin
GridNums.Cells[GridNums.FixedCols, aRow] := '';
GridNums.Cells[GridNums.FixedCols + 1, aRow] := '';
end;
end;
end;
procedure TFormInputPokes.AfterShow(Data: PtrInt);
var
I: Integer;
begin
CommonFunctionsLCL.TCommonFunctionsLCL.AdjustFormPos(Self);
if Data >= 0 then begin
for I := GridNums.FixedCols to GridNums.FixedCols + 1 do
GridNums.AutoSizeColumn(I);
if Data > 0 then
Application.QueueAsyncCall(@AfterShow, Data - 1)
else
if GridNums.CanSetFocus then
GridNums.SetFocus;
end;
end;
procedure TFormInputPokes.FormCloseQuery(Sender: TObject; var CanClose: Boolean
);
procedure SelectCellWithError(const ACol, ARow: Integer; const Msg: String);
begin
CanClose := False;
GridNums.Col := ACol;
GridNums.Row := ARow;
MessageDlg(Msg, mtError, [mbClose], 0);
if GridNums.CanSetFocus then
GridNums.SetFocus;
end;
var
I, J, JJ: Integer;
N: Int32;
Skip: Boolean;
PokeEntry: TPokeEntry;
S1, S2: String;
ColAdr, ColVal: Integer;
UN: UInt32 absolute N;
begin
if ModalResult = mrOK then begin
SetLength(FPokes, GridNums.RowCount - GridNums.FixedRows);
//
ColAdr := GridNums.FixedCols;
ColVal := ColAdr + 1;
J := 0;
for I := GridNums.FixedRows to GridNums.RowCount - 1 do begin
Skip := False;
S1 := Trim(GridNums.Cells[ColAdr, I]);
S2 := Trim(GridNums.Cells[ColVal, I]);
if S1 = '' then begin
if S2 = '' then
Skip := True
else begin
SelectCellWithError(ColAdr, I, 'You have to enter address for the poke.');
Break;
end;
end else
if S2 = '' then begin
SelectCellWithError(ColVal, I, 'You have to enter value.');
Break;
end;
if not Skip then begin
if not GridNums.CheckValue(ColAdr, S1, N) then begin
SelectCellWithError(ColAdr, I, 'Invalid address.');
Break;
end;
PokeEntry.Adr := N;
if not GridNums.CheckValue(ColVal, S2, N) then begin
SelectCellWithError(ColVal, I, 'Invalid Value');
Break;
end;
PokeEntry.Value := UN;
for JJ := 0 to J - 1 do begin
if FPokes[JJ].Adr = PokeEntry.Adr then begin
SelectCellWithError(ColAdr, I, Format(
'Multiple pokes to address %d entered.%sYou cannot enter multiple pokes to same memory address.',
[PokeEntry.Adr, LineEnding]));
Break;
end;
end;
if not CanClose then
Break;
FPokes[J] := PokeEntry;
Inc(J);
end;
end;
SetLength(FPokes, J);
end else
SetLength(FPokes, 0);
end;
class function TFormInputPokes.ShowInputPokesDialog(const AMinAddress,
AMaxAddress: Word; out APokes: TPokesArray): Boolean;
var
F: TFormInputPokes;
C: TGridColumn;
begin
Result := False;
F := TFormInputPokes.Create(nil);
try
F.GridNums.MinAddress := AMinAddress;
F.GridNums.MaxAddress := AMaxAddress;
C := F.GridNums.Columns.Add();
C.Title.Caption := Format('address (%d-%d)', [AMinAddress, AMaxAddress]);
C := F.GridNums.Columns.Add();
C.Title.Caption := Format('value (%d-%d)', [Byte.MinValue, Byte.MaxValue]);
C := F.GridNums.Columns.Add();
C.ButtonStyle := cbsButtonColumn;
C.Title.Caption := ' ';
if (F.ShowModal = mrOK) and (Length(F.FPokes) > 0) then begin
APokes := F.FPokes;
Result := True;
end else
APokes := nil;
finally
F.Free;
end;
end;
end.