Repository navigation
Expand file tree
/
Copy pathFrameworkDemoPagingCapture.dpr
More file actions
443 lines (417 loc) · 15.7 KB
/
Copy pathFrameworkDemoPagingCapture.dpr
File metadata and controls
443 lines (417 loc) · 15.7 KB
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
431
432
433
434
435
436
437
438
439
440
441
442
443
program FrameworkDemoPagingCapture;
{ Copyright © 2026 Herman Schoenfeld <https://sphere10.com>. MIT license.
Capture the real unified demo, using its normal data source and native controls.
Pass one absolute log filename in a fresh QA directory. No wallet data is used. }
{$IFDEF FPC}{$mode Delphi}{$H+}{$ENDIF}
uses
{$IFDEF UNIX}cthreads,{$ENDIF}
{$IFDEF FPC}
Interfaces, Forms, Controls, ExtCtrls, StdCtrls, Graphics, LMessages, Buttons, Menus,
{$ELSE}
Vcl.Forms, Vcl.Controls, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.Graphics, Vcl.Buttons, Vcl.Menus,
Vcl.Imaging.pngimage,
{$ENDIF}
Classes, SysUtils, Types, Math, Windows, Messages, UVisualGrid, UFRMDemoMain,
UVisualGrid.Paging.Checks;
type
TCaptureGridAccess = class(TVisualGrid)
public
function Total: Integer;
function Pages: Integer;
end;
TCaptureStage = (csAuto, csManual, csSingle);
TDemoPagingCapture = class(TComponent)
private
FForm: TFRMDemoMain;
FGrid: TVisualGrid;
FTimer: TTimer;
FLog: TStringList;
FLogPath, FCaptureDirectory, FLastSignature: String;
FDeadline, FStableSince: UInt64;
FStage: TCaptureStage;
FFinished: Boolean;
FSelectPending: Boolean;
FExceptionHooked: Boolean;
FPreviousException: TExceptionEvent;
procedure Tick(Sender: TObject);
procedure OnException(Sender: TObject; E: Exception);
procedure SelectMode(AStage: TCaptureStage);
procedure CheckAndCapture;
procedure CheckAutomaticRows;
procedure Finish(AExitCode: Integer; const AMessage: String);
function Signature: String;
public
constructor Create(AForm: TFRMDemoMain; const ALogPath: String); reintroduce;
destructor Destroy; override;
end;
procedure Require(ACondition: Boolean; const AMessage: String);
begin
if not ACondition then
raise Exception.Create(AMessage);
end;
function CompilerName: String;
begin
{$IFDEF FPC}
Result := 'fpc';
{$ELSE}
Result := 'delphi';
{$ENDIF}
end;
function StageName(AStage: TCaptureStage): String;
begin
case AStage of
csAuto: Result := 'auto';
csManual: Result := 'manual';
csSingle: Result := 'single';
end;
end;
{$IFDEF FPC}
type
TNativePixel = packed record
Blue, Green, Red: Byte;
end;
TNativePixelRow = array[0..32767] of TNativePixel;
PNativePixelRow = ^TNativePixelRow;
function PaintNativeForm(AForm: TForm): Graphics.TBitmap;
var
LCanvasDC: HDC;
procedure PaintControl(AControl: TWinControl);
var
LSaved, I: Integer;
LOrigin: Types.TPoint;
LWindow: Types.TRect;
begin
if not AControl.HandleAllocated or not Windows.IsWindowVisible(AControl.Handle) then
Exit;
LSaved := Windows.SaveDC(LCanvasDC);
try
// Ask the widget to paint its own native border/scrollbars first.
Windows.GetWindowRect(AControl.Handle, LWindow);
LOrigin := AForm.ScreenToClient(LWindow.TopLeft);
Windows.SetViewportOrgEx(LCanvasDC, LOrigin.X, LOrigin.Y, nil);
Windows.SendMessage(AControl.Handle, WM_PRINT, WPARAM(LCanvasDC), PRF_NONCLIENT);
LOrigin := AForm.ScreenToClient(AControl.ClientToScreen(Types.Point(0, 0)));
Windows.SetViewportOrgEx(LCanvasDC, LOrigin.X, LOrigin.Y, nil);
Windows.IntersectClipRect(LCanvasDC, 0, 0, AControl.ClientWidth, AControl.ClientHeight);
// LCL Win32 PaintTo delegates to WM_PRINT, which can leave custom-control
// clients black on an inactive desktop. Deliver their normal LCL paint
// messages to the supplied DC instead, retaining real cell/label painting.
AControl.Perform(LM_ERASEBKGND, WPARAM(LCanvasDC), 0);
AControl.Perform(LM_PAINT, WPARAM(LCanvasDC), 0);
if not (AControl is TCustomControl) and not (AControl is TCustomForm) then
Windows.SendMessage(AControl.Handle, WM_PRINTCLIENT, WPARAM(LCanvasDC), PRF_CLIENT);
for I := 0 to AControl.ControlCount - 1 do
if AControl.Controls[I] is TWinControl then
PaintControl(TWinControl(AControl.Controls[I]));
finally
Windows.RestoreDC(LCanvasDC, LSaved);
end;
end;
begin
Result := Graphics.TBitmap.Create;
try
Result.PixelFormat := pf24bit;
Result.SetSize(AForm.ClientWidth, AForm.ClientHeight);
// Uncovered client margins use the actual form background. Widget pixels
// still come from their own paint handlers and are verified separately.
Result.Canvas.Brush.Color := AForm.GetRGBColorResolvingParent;
Result.Canvas.FillRect(Types.Rect(0, 0, Result.Width, Result.Height));
LCanvasDC := Result.Canvas.Handle;
PaintControl(AForm);
except
Result.Free;
raise;
end;
end;
function CountNativeInk(ABitmap: Graphics.TBitmap; AForm: TForm; AControl: TWinControl; const ALocalRect: Types.TRect): Integer;
var
LOrigin: Types.TPoint;
LRect: Types.TRect;
LRow: PNativePixelRow;
X, Y: Integer;
begin
Require(ABitmap.Width <= High(TNativePixelRow) + 1, 'Native capture exceeds the pixel-check bound');
LOrigin := AForm.ScreenToClient(AControl.ClientToScreen(ALocalRect.TopLeft));
LRect := Types.Rect(Max(0, LOrigin.X), Max(0, LOrigin.Y),
Min(ABitmap.Width, LOrigin.X + ALocalRect.Right - ALocalRect.Left),
Min(ABitmap.Height, LOrigin.Y + ALocalRect.Bottom - ALocalRect.Top));
Require((LRect.Right > LRect.Left) and (LRect.Bottom > LRect.Top),
'Native capture region is outside the actual grid-tab client');
Result := 0;
for Y := LRect.Top to LRect.Bottom - 1 do begin
LRow := ABitmap.ScanLine[Y];
for X := LRect.Left to LRect.Right - 1 do
if (LRow^[X].Red < 96) and (LRow^[X].Green < 96) and
(LRow^[X].Blue < 96) then
Inc(Result);
end;
end;
{$ENDIF}
function TCaptureGridAccess.Total: Integer;
begin
Result := FTotalDataCount;
end;
function TCaptureGridAccess.Pages: Integer;
begin
Result := FPageCount;
end;
constructor TDemoPagingCapture.Create(AForm: TFRMDemoMain; const ALogPath: String);
var
I: Integer;
begin
inherited Create(AForm);
FForm := AForm;
Require((ExtractFileDrive(ALogPath) <> '') and
SameText(ExpandFileName(ALogPath), ALogPath), 'Pass one absolute log filename');
FLogPath := ALogPath;
FCaptureDirectory := ChangeFileExt(FLogPath, '') + '-captures';
Require(not DirectoryExists(FCaptureDirectory), 'Capture directory must be fresh');
Require(ForceDirectories(FCaptureDirectory), 'Cannot create the capture directory');
FLog := TStringList.Create;
FLog.Add('Real TFRMDemoMain / fMain.TForm1; generated demo source; ' + CompilerName);
Require(Assigned(FForm.GridExample), 'Unified demo did not create its grid example');
for I := 0 to FForm.GridExample.ComponentCount - 1 do
if FForm.GridExample.Components[I] is TVisualGrid then begin
Require(not Assigned(FGrid), 'Grid example unexpectedly owns multiple visual grids');
FGrid := TVisualGrid(FForm.GridExample.Components[I]);
end;
Require(Assigned(FGrid), 'Actual grid example has no TVisualGrid');
FForm.Pages.ActivePage := FForm.GridTab;
FTimer := TTimer.Create(Self);
FTimer.Enabled := False;
FTimer.Interval := 25;
FTimer.OnTimer := Tick;
FPreviousException := Application.OnException;
Application.OnException := OnException;
FExceptionHooked := True;
FDeadline := GetTickCount64 + 30000;
FStage := csAuto;
FSelectPending := True;
FTimer.Enabled := True;
end;
destructor TDemoPagingCapture.Destroy;
begin
if Assigned(FTimer) then
FTimer.Enabled := False;
if FExceptionHooked then
Application.OnException := FPreviousException;
FLog.Free;
inherited Destroy;
end;
procedure TDemoPagingCapture.SelectMode(AStage: TCaptureStage);
var
LIndex: Integer;
begin
FStage := AStage;
FStableSince := 0;
FLastSignature := '';
case FStage of
csAuto: LIndex := 1;
csManual: LIndex := 2;
csSingle: LIndex := 0;
end;
if FStage = csManual then
FGrid.ManualPageSize := 25;
FTimer.Enabled := False;
try
ChooseVisualGridPageMode(FGrid,LIndex);
finally
FTimer.Enabled := True;
end;
end;
function TDemoPagingCapture.Signature: String;
begin
Result := Format('%d/%d/%d/%d/%d/%d/%d', [Ord(FGrid.PageMode),
FGrid.PageSize, FGrid.RowCount, TCaptureGridAccess(FGrid).Pages,
TCaptureGridAccess(FGrid).Total, FGrid.InternalDrawGrid.ClientWidth,
FGrid.InternalDrawGrid.ClientHeight]);
end;
procedure TDemoPagingCapture.CheckAutomaticRows;
var
LFirst, LSecond: TRect;
LPitch, LFullBottom, LFixed: Integer;
begin
Require(FGrid.PageMode = vpmAuto, 'Auto menu did not select automatic paging');
Require((FGrid.RowCount = FGrid.PageSize) and (FGrid.RowCount > 1),
'Auto did not display its full page');
LFixed := FGrid.InternalDrawGrid.FixedRows;
LFirst := FGrid.InternalDrawGrid.CellRect(0, LFixed);
LSecond := FGrid.InternalDrawGrid.CellRect(0, LFixed + 1);
LPitch := LSecond.Top - LFirst.Top;
Require(LPitch > 0, 'Native row pitch is empty');
// Extrapolate native cell geometry because VCL clips the final CellRect.
LFullBottom := LFirst.Bottom + (FGrid.RowCount - 1) * LPitch;
Require((LFullBottom <= FGrid.InternalDrawGrid.ClientHeight) and
(LFullBottom + LPitch > FGrid.InternalDrawGrid.ClientHeight),
'Auto must display exactly the complete rows that fit its native viewport');
Require((GetWindowLongPtr(FGrid.InternalDrawGrid.Handle, GWL_STYLE) and WS_VSCROLL) = 0,
'Auto unexpectedly exposed a vertical scrollbar');
end;
procedure TDemoPagingCapture.CheckAndCapture;
var
LBitmap: {$IFDEF FPC}Graphics.TBitmap{$ELSE}Vcl.Graphics.TBitmap{$ENDIF};
LPng: {$IFDEF FPC}TPortableNetworkGraphic{$ELSE}TPngImage{$ENDIF};
LPath: String;
LEffectivePageSize: Integer;
{$IFDEF FPC}
LMode: TBitBtn;
LCell: Types.TRect;
LDataInk, LModeInk, LInspectorInk: Integer;
{$ENDIF}
begin
Require(TCaptureGridAccess(FGrid).Total = 10000,
'Capture must use all 10000 generated records from the real demo source');
Require(FGrid.PageIndex = 0, 'Mode changes must start at page zero');
case FStage of
csAuto: CheckAutomaticRows;
csManual:
begin
Require((FGrid.PageMode = vpmManual) and (FGrid.PageSize = 25) and
(FGrid.ManualPageSize = 25) and (FGrid.RowCount = 25),
'Manual must display its configured finite page of 25 rows');
Require(TCaptureGridAccess(FGrid).Pages = 400, 'Manual page count is incorrect');
end;
csSingle:
begin
Require((FGrid.PageMode = vpmSingle) and (FGrid.PageSize = 25) and
(FGrid.ManualPageSize = 25) and
(FGrid.RowCount = 10000) and (TCaptureGridAccess(FGrid).Pages = 1),
'Single must expose all 10000 records in one unbounded page');
Require((GetWindowLongPtr(FGrid.InternalDrawGrid.Handle, GWL_STYLE) and WS_VSCROLL) <> 0,
'Single must expose its native vertical scrollbar');
end;
end;
// The demo inspector refreshes explicitly; capture its current mode as well
// as the actual grid instead of leaving the initial Manual values on screen.
FForm.GridExample.bRefresh.Click;
FForm.Update;
LPath := IncludeTrailingPathDelimiter(FCaptureDirectory) +
'unified-grid-' + StageName(FStage) + '-' + CompilerName + '.png';
{$IFDEF FPC}
// The inactive-desktop widget cannot print its main tab strip. Capture the
// real hosted grid form's client instead, without inventing missing chrome.
LBitmap := PaintNativeForm(FForm.GridExample);
FLog.Add('CAPTURE FPC GridExample.ClientRect: actual grid-tab content; main tab strip excluded');
FLog.Add(Format('BACKGROUND FPC declared=%d resolved=%d',
[FForm.GridExample.Color, FForm.GridExample.GetRGBColorResolvingParent]));
{$ELSE}
LBitmap := FForm.GetFormImage;
{$ENDIF}
try
Require((LBitmap.Width > 0) and (LBitmap.Height > 0), 'Native form capture is empty');
{$IFDEF FPC}
// Background alone cannot satisfy these checks: require real text inside
// a fetched data cell, the mode selector and the live property inspector.
LCell := FGrid.InternalDrawGrid.CellRect(1, FGrid.InternalDrawGrid.FixedRows);
InflateRect(LCell, -2, -2);
LDataInk := CountNativeInk(LBitmap, FForm.GridExample, FGrid.InternalDrawGrid, LCell);
LMode := FGrid.FindComponent('PageModeButton') as TBitBtn;
Require(Assigned(LMode) and LMode.Visible, 'Native mode selector is missing');
LModeInk := CountNativeInk(LBitmap, FForm.GridExample, LMode,
Types.Rect(3, 3, LMode.ClientWidth - 3, LMode.ClientHeight - 3));
LInspectorInk := CountNativeInk(LBitmap, FForm.GridExample, FForm.GridExample.TIPropertyGrid1,
Types.Rect(4, 4, FForm.GridExample.TIPropertyGrid1.ClientWidth - 4,
Min(100, FForm.GridExample.TIPropertyGrid1.ClientHeight - 4)));
Require((LDataInk > 8) and (LModeInk > 12) and (LInspectorInk > 50),
Format('Incomplete native control painting: data=%d mode=%d inspector=%d',
[LDataInk, LModeInk, LInspectorInk]));
FLog.Add(Format('PIXELS FPC native name-cell=%d mode-icon=%d inspector=%d',
[LDataInk, LModeInk, LInspectorInk]));
{$ENDIF}
LPng := {$IFDEF FPC}TPortableNetworkGraphic{$ELSE}TPngImage{$ENDIF}.Create;
try
LPng.Assign(LBitmap);
LPng.SaveToFile(LPath);
finally
LPng.Free;
end;
finally
LBitmap.Free;
end;
// Single requests an unbounded page while PageSize retains Manual's setting.
LEffectivePageSize := FGrid.PageSize;
if FGrid.PageMode = vpmSingle then
LEffectivePageSize := -1;
FLog.Add(Format('PASS %s mode=%d effectivePageSize=%d configuredPageSize=%d rows=%d pages=%d total=%d image=%s',
[StageName(FStage), Ord(FGrid.PageMode), LEffectivePageSize, FGrid.PageSize, FGrid.RowCount,
TCaptureGridAccess(FGrid).Pages, TCaptureGridAccess(FGrid).Total, LPath]));
FLog.SaveToFile(FLogPath);
end;
procedure TDemoPagingCapture.Finish(AExitCode: Integer; const AMessage: String);
begin
if FFinished then
Exit;
FFinished := True;
FTimer.Enabled := False;
ExitCode := AExitCode;
FLog.Add(AMessage);
FLog.SaveToFile(FLogPath);
// Main-form Close uses the application's normal termination and owned cleanup.
FForm.Close;
end;
procedure TDemoPagingCapture.OnException(Sender: TObject; E: Exception);
begin
Finish(1, 'FAIL ' + E.ClassName + ': ' + E.Message);
end;
procedure TDemoPagingCapture.Tick(Sender: TObject);
var
LSignature: String;
begin
if FFinished then
Exit;
try
Require(GetTickCount64 < FDeadline, 'Demo capture exceeded its 30-second deadline');
if not FForm.Visible or not FForm.HandleAllocated or FGrid.IsUpdating or
FGrid.RefreshThrottle.Waiting or (TCaptureGridAccess(FGrid).Total <> 10000) then begin
FStableSince := 0;
Exit;
end;
if FSelectPending then begin
FSelectPending := False;
SelectMode(FStage);
Exit;
end;
LSignature := Signature;
if (FStableSince = 0) or (LSignature <> FLastSignature) then begin
FLastSignature := LSignature;
FStableSince := GetTickCount64;
Exit;
end;
if GetTickCount64 - FStableSince < 400 then
Exit;
CheckAndCapture;
case FStage of
csAuto: SelectMode(csManual);
csManual: SelectMode(csSingle);
csSingle: Finish(0, 'PASS real unified demo: Auto, Manual 25 and Single captures');
end;
except
on E: Exception do
Finish(1, 'FAIL ' + E.ClassName + ': ' + E.Message);
end;
end;
begin
ExitCode := 1;
Application.Initialize;
Application.Title := 'Sphere 10 Pascal Framework';
{$IFNDEF FPC}Application.MainFormOnTaskbar := True;{$ENDIF}
try
Require(ParamCount = 1, 'Usage: FrameworkDemoPagingCapture.exe <absolute fresh log filename>');
Application.CreateForm(TFRMDemoMain, DemoMainForm);
TDemoPagingCapture.Create(DemoMainForm, ParamStr(1));
// Keep the production entry point's Application-owned main-form lifetime.
Application.Run;
except
on E: Exception do begin
ExitCode := 1;
if ParamCount = 1 then
with TStringList.Create do
try
Add('FAIL ' + E.ClassName + ': ' + E.Message);
SaveToFile(ParamStr(1));
finally
Free;
end;
end;
end;
end.