-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunitformhistorysnapshots.pas
431 lines (360 loc) · 11.2 KB
/
unitformhistorysnapshots.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
unit UnitFormHistorySnapshots;
// Copyright 2022-2025 Zoran Vučenović
// SPDX-License-Identifier: Apache-2.0
{$mode ObjFPC}{$H+}
{$i zxinc.inc}
interface
uses
Classes, SysUtils, Types, UnitSnapshotFiles, UnitHistorySnapshots,
CommonFunctionsLCL, UnitControlScreenBitmap,
UnitSpectrumColoursBGRA, UnitFrameHistorySnapshotOptions, Forms, Controls,
Graphics, Dialogs, ExtCtrls, StdCtrls, LMessages, ButtonPanel;
type
TFormHistorySnapshots = class(TForm)
Bevel1: TBevel;
Bevel2: TBevel;
ButtonPanel1: TButtonPanel;
CheckBox2: TCheckBox;
Label5: TLabel;
Panel1: TPanel;
Panel12: TPanel;
Panel13: TPanel;
Panel15: TPanel;
Panel2: TPanel;
Panel6: TPanel;
Panel7: TPanel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
strict private
HistorySnapshots: TSnapshotHistoryQueue;
ScrollBox: TScrollBox;
BgraColours: TBGRAColours;
PrevCtrl: TControl;
FTimer: TFlashTimer;
FrameHistorySnapshotOptions: TFrameHistorySnapshotOptions;
procedure CheckBoxHistoryEnabledOnChange(Sender: TObject);
function GetSelectedSnapshotNegOffs: Integer;
procedure AfterShow(Data: PtrInt);
procedure UpdateCheckEnabled;
procedure FillSnapshots;
procedure TimerOnTimer(Sender: TObject);
procedure ActiveCtrlChg(Sender: TObject; LastControl: TControl);
public
class function ShowFormHistorySnapshots(
AHistorySnapshots: TSnapshotHistoryQueue;
var ASnapshotHistoryOptions: TSnapshotHistoryOptions;
const ABGRAColours: TBGRAColours;
out AHistoryEnabled: Boolean): Boolean;
end;
implementation
{$R *.lfm}
type
TSnapshotControl = class(TCustomControl)
private
Lab: TLabel;
CtrlScrBmp: TControlScreenBitmap;
InternalNegativeSnapNo: Integer;
AB: Array of Byte;
LastSelected: Boolean;
procedure UserInputEvent(Sender: TObject; Msg: Cardinal);
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Paint; override;
end;
{ TSnapshotControl }
procedure TSnapshotControl.UserInputEvent(Sender: TObject; Msg: Cardinal);
begin
case Msg of
LM_LBUTTONDOWN, LM_RBUTTONDOWN:
if MouseInClient and CanSetFocus then
SetFocus;
otherwise
end;
end;
constructor TSnapshotControl.Create(AOwner: TComponent);
var
F: TFont;
FD: TFontData;
begin
inherited Create(AOwner);
LastSelected := False;
AutoSize := False;
Color := clWhite;
SetLength(AB, 0);
CtrlScrBmp := nil;
Lab := TLabel.Create(nil);
Lab.Alignment := TAlignment.taRightJustify;
F := Lab.Font;
FD := GetFontData(F.Handle);
F.BeginUpdate;
try
F.Name := FD.Name;
F.Style := F.Style + [fsBold];
F.Height := (FD.Height * 4) div 3;
F.Color := clNavy;
finally
F.EndUpdate;
end;
Lab.Font := F;
end;
destructor TSnapshotControl.Destroy;
begin
Application.RemoveAllHandlersOfObject(Self);
Lab.Free;
CtrlScrBmp.Free;
SetLength(AB, 0);
inherited Destroy;
end;
procedure TSnapshotControl.Paint;
var
R: TRect;
procedure DrawRect(); inline;
begin
Canvas.Polyline([R.TopLeft, Point(R.Right, R.Top), R.BottomRight, Point(R.Left, R.Bottom), R.TopLeft]);
end;
begin
inherited Paint;
if LastSelected then begin
Canvas.Pen.Color := clNavy;
R := ClientRect;
R.Inflate(-2, -2);
DrawRect();
R.Inflate(-1, -1);
DrawRect();
end;
end;
{ TFormHistorySnapshots }
procedure TFormHistorySnapshots.FormCreate(Sender: TObject);
begin
BorderIcons := BorderIcons - [TBorderIcon.biMaximize, TBorderIcon.biMinimize];
PrevCtrl := nil;
ScrollBox := nil;
HistorySnapshots := nil;
Panel1.Enabled := False;
FTimer := nil;
Constraints.MinHeight := Height;
Constraints.MaxHeight := Height;
FrameHistorySnapshotOptions := TFrameHistorySnapshotOptions.Create(Panel2);
FrameHistorySnapshotOptions.Parent := Panel2;
Panel2.AutoSize := True;
FrameHistorySnapshotOptions.CheckBoxAutoCreateSnapshots.OnChange := @CheckBoxHistoryEnabledOnChange;
end;
procedure TFormHistorySnapshots.FormDestroy(Sender: TObject);
begin
if Assigned(FTimer) then
FTimer.Enabled := False;
Screen.RemoveAllHandlersOfObject(Self);
ScrollBox.Free;
FTimer.Free;
end;
procedure TFormHistorySnapshots.FormShow(Sender: TObject);
begin
//FillSnapshots;
AfterShow(2);
end;
procedure TFormHistorySnapshots.CheckBoxHistoryEnabledOnChange(Sender: TObject);
begin
UpdateCheckEnabled;
end;
function TFormHistorySnapshots.GetSelectedSnapshotNegOffs: Integer;
begin
Result := 1;
if PrevCtrl is TSnapshotControl then
Result := TSnapshotControl(PrevCtrl).InternalNegativeSnapNo;
end;
procedure TFormHistorySnapshots.UpdateCheckEnabled;
begin
CheckBox2.Enabled := Assigned(HistorySnapshots)
and (HistorySnapshots.Count > 0)
and (PrevCtrl is TSnapshotControl)
;
if CheckBox2.State <> TCheckBoxState.cbUnchecked then begin
if not CheckBox2.Enabled then begin
CheckBox2.State := TCheckBoxState.cbGrayed;
end else begin
CheckBox2.State := TCheckBoxState.cbChecked;
end;
end;
end;
procedure TFormHistorySnapshots.FillSnapshots;
var
N, J: Integer;
SnapshotControl: TSnapshotControl;
C, CC, CC2: TCustomControl;
St: TStream;
Sz: TSize;
S: String;
begin
if Assigned(HistorySnapshots) xor Assigned(ScrollBox) then begin
FreeAndNil(FTimer);
PrevCtrl := nil;
Screen.RemoveHandlerActiveControlChanged(@ActiveCtrlChg);
J := 0;
if Assigned(HistorySnapshots) then begin
ScrollBox := TScrollBox.Create(nil);
ScrollBox.BorderStyle := bsNone;
C := nil;
N := 0;
while N < HistorySnapshots.Count do begin
if HistorySnapshots.GetStream(-N, St) then begin
SnapshotControl := TSnapshotControl.Create(ScrollBox);
SnapshotControl.Anchors := [];
SnapshotControl.AnchorParallel(akLeft, 2, ScrollBox);
if C = nil then
SnapshotControl.AnchorParallel(akTop, 2, ScrollBox)
else
SnapshotControl.AnchorToNeighbour(akTop, 4, C);
C := SnapshotControl;
SetLength(SnapshotControl.AB, 6912);
if TSnapshotInternalSwan.GetScreenMem(St, SnapshotControl.AB) then begin
CC2 := TCustomControl.Create(SnapshotControl);
CC2.Caption := '';
CC2.BorderStyle := bsSingle;
SnapshotControl.CtrlScrBmp := TControlScreenBitmap.CreateNewControl(
CC2, BGRAColours, @SnapshotControl.AB[0]
);
SnapshotControl.InternalNegativeSnapNo := -N;
if J = 0 then begin
TCommonFunctionsLCL.CalculateTextSize(SnapshotControl.Lab.Font, 'w.' + IntToStr(HistorySnapshots.Count), Sz);
FTimer := TFlashTimer.Create(nil);
FTimer.OnTimer := @TimerOnTimer;
end;
Inc(J);
SnapshotControl.CtrlScrBmp.Timer := FTimer;
SnapshotControl.Lab.Caption := J.ToString + '.';
SnapshotControl.Lab.Constraints.MinWidth := Sz.cx;
CC := TCustomControl.Create(SnapshotControl);
CC.Anchors := [];
CC.AnchorParallel(akLeft, 0, SnapshotControl);
CC.AnchorParallel(akTop, 0, SnapshotControl);
CC.BorderSpacing.Around := 5;
SnapshotControl.Lab.Anchors := [];
SnapshotControl.Lab.AnchorVerticalCenterTo(CC);
SnapshotControl.Lab.AnchorParallel(akLeft, 2, CC);
CC2.Anchors := [];
CC2.AnchorVerticalCenterTo(CC);
CC2.AnchorToNeighbour(akLeft, 0, SnapshotControl.Lab);
CC2.BorderSpacing.Around := 3;
SnapshotControl.CtrlScrBmp.Anchors := [];
SnapshotControl.CtrlScrBmp.AnchorParallel(akTop, 0, CC2);
SnapshotControl.CtrlScrBmp.AnchorParallel(akLeft, 0, CC2);
SnapshotControl.CtrlScrBmp.Parent := CC2;
SnapshotControl.Lab.Parent := CC;
CC2.AutoSize := True;
CC2.Parent := CC;
CC.AutoSize := True;
CC.Parent := SnapshotControl;
SnapshotControl.AutoSize := True;
SnapshotControl.TabStop := True;
SnapshotControl.Parent := ScrollBox;
Application.AddOnUserInputHandler(@(SnapshotControl.UserInputEvent));
end;
end;
Inc(N);
end;
end;
if J <= 0 then
FreeAndNil(ScrollBox);
if Assigned(ScrollBox) then begin
if J = 1 then
S := ''
else
S := 's, most recent on top';
Label5.Caption := Format(
'Currently saved in memory (%d snapshot%s):', [J, S]);
ScrollBox.Anchors := [];
ScrollBox.AnchorParallel(akTop, 0, Panel13);
ScrollBox.AnchorParallel(akLeft, 0, Panel13);
ScrollBox.AnchorParallel(akRight, 0, Panel13);
ScrollBox.AnchorParallel(akBottom, 0, Panel13);
ScrollBox.Parent := Panel13;
Screen.AddHandlerActiveControlChanged(@ActiveCtrlChg);
FTimer.Enabled := True;
end;
end;
if ScrollBox = nil then
Label5.Caption := 'No snapshots saved.';
end;
procedure TFormHistorySnapshots.TimerOnTimer(Sender: TObject);
begin
FTimer.FlashState := not FTimer.FlashState;
ScrollBox.Invalidate;
end;
procedure TFormHistorySnapshots.ActiveCtrlChg(Sender: TObject;
LastControl: TControl);
var
C: TControl;
SC: TSnapshotControl;
begin
if ScrollBox = nil then
Exit;
SC := nil;
C := LastControl;
while Assigned(C) do begin
if C is TSnapshotControl then begin
SC := TSnapshotControl(C);
Break;
end;
C := C.Parent;
end;
if Assigned(SC) then begin
if PrevCtrl is TSnapshotControl then
TSnapshotControl(PrevCtrl).LastSelected := False;
SC.LastSelected := True;
PrevCtrl := SC;
ScrollBox.ScrollInView(SC);
ScrollBox.Invalidate;
end;
UpdateCheckEnabled;
end;
procedure TFormHistorySnapshots.AfterShow(Data: PtrInt);
begin
AutoSize := False;
CommonFunctionsLCL.TCommonFunctionsLCL.AdjustFormPos(Self);
if Data > 0 then begin
AutoSize := True;
Application.QueueAsyncCall(@AfterShow, Data - 1)
end else begin
Constraints.MinHeight := 0;
Constraints.MaxHeight := 0;
FillSnapshots;
Panel1.Enabled := True;
end;
end;
class function TFormHistorySnapshots.ShowFormHistorySnapshots(
AHistorySnapshots: TSnapshotHistoryQueue;
var ASnapshotHistoryOptions: TSnapshotHistoryOptions;
const ABGRAColours: TBGRAColours; out AHistoryEnabled: Boolean): Boolean;
var
F: TFormHistorySnapshots;
N: Integer;
begin
Result := False;
AHistoryEnabled := Assigned(AHistorySnapshots);
F := TFormHistorySnapshots.Create(nil);
try
F.HistorySnapshots := AHistorySnapshots;
F.FrameHistorySnapshotOptions.HistoryEnabled := AHistoryEnabled;
F.FrameHistorySnapshotOptions.UpdateValuesFromHistoryOptions(
ASnapshotHistoryOptions);
F.BgraColours := ABGRAColours;
F.UpdateCheckEnabled;
if F.ShowModal = mrOK then begin
F.FrameHistorySnapshotOptions.UpdateSnapshotHistoryOptionsFromValues(
ASnapshotHistoryOptions);
AHistoryEnabled := F.FrameHistorySnapshotOptions.HistoryEnabled;
if F.CheckBox2.Enabled and F.CheckBox2.Checked then begin
N := F.GetSelectedSnapshotNegOffs;
if N <= 0 then
F.HistorySnapshots.LoadSnapshot(N, False);
end;
Result := True;
end;
finally
F.Free;
end;
end;
end.