-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloglistfpc.pas
749 lines (657 loc) · 17.3 KB
/
loglistfpc.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
unit loglistfpc;
{ Simple Log view control
Copyright (c) Do-wan Kim
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
2018. using more reliable lock method
}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Graphics, Controls, LCLType, Forms, syncobjs, ExtCtrls,
LMessages;
type
{ TLogStringData }
TLogStringData = class
public
F, B : TColor;
Obj : TObject;
constructor Create;
destructor Destroy; override;
end;
{ TLogStringList }
TLogStringList = class(TStringList)
private
DefaultTCol, DefaultBCol: TColor;
fEvent:TEvent;
FOnChanged: TNotifyEvent;
procedure Enter;
procedure Leave;
protected
function GetCount: Integer; override;
function Get(Index: Integer): string; override;
function GetObject(Index: Integer): TObject; override;
procedure Put(Index: Integer; const S: string); override;
procedure PutObject(Index: Integer; AObject: TObject); override;
procedure InsertItem(Index: Integer; const S: string; O: TObject);
override;
procedure Changed; override;
public
constructor Create;
destructor Destroy; override;
procedure Clear; override;
function GetStrObj(Index, MaxLen:Integer; out obj:TLogStringData):string;
property TCol: TColor read DefaultTCol write DefaultTCol;
property BCol: TColor read DefaultBCol write DefaultBCol;
property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
end;
{ TLogListFPC }
TEventLogDeleteLimit = procedure(const s:string) of object;
TLogListFPC = class(TScrollingWinControl)
private
PrevCount : Integer;
LogData : TLogStringList;
tHeight, tWidth, MaxTextWidth, tItems, FLineSpace : Integer;
FLineLimit, FLastPosY, FLastPosX : Integer;
FEvent : TEvent;
FOnDeleteLimit : TEventLogDeleteLimit;
FTimer : TTimer;
FAddFlag, FSkipLast, FIsViewHorz, FUpdated, FLastHBar : Boolean;
FLastCHeight: Integer;
function GetCount:Integer;
procedure SetItemIndex(Value : Integer);
function GetItemIndex:Integer;
procedure SetHPos(Pos : Integer);
procedure OnTimer(Sender : TObject);
procedure CheckTextLen(const s: string);
procedure SetLineSpace(AValue: Integer);
procedure DataChange(Sender: TObject);
protected
procedure Paint; override;
procedure FontChanged(Sender: TObject); override;
procedure DoOnResize; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure DoEnter; override;
procedure DoExit; override;
procedure WMHScroll(var Message : TLMHScroll); message LM_HScroll;
procedure WMVScroll(var Message : TLMVScroll); message LM_VScroll;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure EraseBackground(DC: HDC); override;
procedure SetItemPos(Pos : Integer);
procedure SetLastPos;
procedure Enter;
procedure Leave;
function AddLog(const s:string; txtColor, BackColor : TColor):Integer; overload;
function AddLog(const s:string; txtColor : TColor):Integer; overload;
function AddLog(const s:string):Integer; overload;
function AddLogLine(const s:string):Integer;
procedure InsertLog(Idx:Integer; const s:string; txtColor, BackColor : TColor); overload;
procedure InsertLog(Idx:Integer; const s:string; txtColor : TColor); overload;
procedure InsertLog(Idx:Integer; const s:string); overload;
procedure UpdateLog(Idx:Integer; const s:string; txtColor, BackColor : TColor); overload;
procedure UpdateLog(Idx:Integer; const s:string; txtColor: TColor); overload;
procedure UpdateLog(Idx:Integer; const s:string); overload;
procedure DeleteLog(Idx:Integer);
procedure Clear;
property Count: Integer read GetCount;
property SkipMoveLast : Boolean read FSkipLast write FSkipLast;
property LineSpace: Integer read FLineSpace write SetLineSpace;
published
property List : TLogStringList read LogData;
property LineLimit : Integer read FLineLimit write FLineLimit;
property OnDeleteLimit : TEventLogDeleteLimit read FOnDeleteLimit write FOnDeleteLimit;
property ItemIndex : Integer read GetItemIndex write SetItemIndex;
property IsViewHorz : Boolean read FIsViewHorz;
end;
implementation
const
_LogMaxCharLen = 1024;
{ TLogListFPC }
function TLogListFPC.GetCount: Integer;
begin
Result:=LogData.Count;
end;
procedure TLogListFPC.SetItemIndex(Value: Integer);
begin
SetItemPos(Value);
end;
function TLogListFPC.GetItemIndex: Integer;
begin
if VertScrollBar.Visible then begin
FLastPosY:=VertScrollBar.Position;
Result:=FLastPosY div tHeight;
end else begin
FLastPosY:=0;
Result:=0;
end;
end;
procedure TLogListFPC.SetHPos(Pos: Integer);
begin
if Assigned(HorzScrollBar) then begin
if Pos<0 then Pos:=0;
HorzScrollBar.Position:=Pos;
FUpdated:=True;
end;
end;
procedure TLogListFPC.OnTimer(Sender: TObject);
begin
if HorzScrollBar.IsScrollBarVisible<>FLastHBar then
DoOnResize;
if FUpdated then
if VertScrollBar.Visible then
VertScrollBar.Range:=Count * tHeight;
if FAddFlag then begin
FAddFlag:=False;
if not FSkipLast then
SetLastPos;
end;
if FUpdated and (FEvent.WaitFor(0)=wrSignaled) then begin
FUpdated:=False;
Invalidate;
end;
end;
procedure TLogListFPC.CheckTextLen(const s: string);
var
txtWidth:Integer;
begin
txtWidth:=Canvas.TextWidth(s);
if MaxTextWidth<txtWidth then begin
MaxTextWidth:=txtWidth;
HorzScrollBar.Range:=MaxTextWidth;
FIsViewHorz:=True;
FUpdated:=True;
end;
end;
procedure TLogListFPC.SetLineSpace(AValue: Integer);
begin
if FLineSpace=AValue then Exit;
FLineSpace:=AValue;
tHeight:=Canvas.TextHeight('hj');
Inc(tHeight,FLineSpace);
if tHeight=0 then tHeight:=1;
tItems:=ClientHeight div tHeight;
FUpdated:=True;
end;
procedure TLogListFPC.DataChange(Sender: TObject);
begin
FUpdated:=True;
end;
procedure TLogListFPC.Paint;
var
cPos, cPointY, ViewHeight : Integer;
cstr : string;
temp : TLogStringData;
SRect : TRect;
begin
if Canvas.TryLock then begin
try
Canvas.Brush.Color:=Color;
SRect:=GetScrolledClientRect;
Canvas.FillRect(SRect);
FLastPosX:=HorzScrollBar.Position;
cPos:=GetItemIndex; // FLastPosY updated
cPointY:=cPos*tHeight;
ViewHeight:=HorzScrollBar.ClientSizeWithoutBar;
while cPos<Count do begin
cstr:=LogData.GetStrObj(cPos,_LogMaxCharLen,temp);
if temp<>nil then begin
Canvas.Brush.Color:=temp.B;
Canvas.Font.Color:=temp.F;
end;
Canvas.TextRect(SRect,0,cPointY,cstr);
(*
if Focused and (FSelectedIndex=cPos) then
Canvas.DrawFocusRect(Rect(cPointX-1,cPointY-1,cPointX+MaxTextWidth+1,cPointY+tHeight-1));
*)
Inc(cPointY,tHeight);
if (cPointY-FLastPosY)>ViewHeight then
Break;
Inc(cPos);
end;
Canvas.Brush.Color:=Color;
Canvas.FillRect(SRect.Left,SRect.Top,SRect.Left+1,SRect.Bottom);
if Focused then
Canvas.Brush.Color:=clHighlight
else Canvas.Brush.Color:=clInactiveBorder;
Canvas.FillRect(SRect.Left,SRect.Top,SRect.Left+1,SRect.Bottom);
finally
Canvas.Unlock;
end;
end;
FLastHBar:=HorzScrollBar.IsScrollBarVisible;
inherited Paint;
end;
procedure TLogListFPC.FontChanged(Sender: TObject);
begin
inherited FontChanged(Sender);
tHeight:=Canvas.TextHeight('hj');
Inc(tHeight,FLineSpace);
if tHeight=0 then tHeight:=1;
tWidth:=Canvas.TextWidth('W');
tItems:=ClientHeight div tHeight;
FUpdated:=True;
OnTimer(Self);
end;
procedure TLogListFPC.DoOnResize;
begin
inherited DoOnResize;
if Assigned(VertScrollBar) then begin
VertScrollBar.Page:=ClientHeight;
// update position
VertScrollBar.Position:=VertScrollBar.Position+(FLastCHeight-ClientHeight);
end;
if Assigned(HorzScrollBar) then
HorzScrollBar.Page:=ClientWidth;
FLastCHeight:=ClientHeight;
FUpdated:=True;
end;
procedure TLogListFPC.KeyDown(var Key: Word; Shift: TShiftState);
begin
if ssCtrl in Shift then begin
case Key of
VK_HOME : begin SetItemPos(0); Key:=0; end;
VK_END : begin SetLastPos; Key:=0; end;
end;
end else begin
case Key of
VK_HOME : begin SetHPos(0); Key:=0; end;
VK_END : begin SetHPos(MaxTextWidth); Key:=0; end;
VK_UP : begin ItemIndex:=ItemIndex-1; Key:=0; end;
VK_DOWN : begin ItemIndex:=ItemIndex+1; Key:=0 end;
VK_PRIOR: begin ItemIndex:=ItemIndex-tItems; Key:=0; end;
VK_NEXT : begin ItemIndex:=ItemIndex+tItems; Key:=0; end;
VK_LEFT : begin HorzScrollBar.Position:=HorzScrollBar.Position-ClientWidth div 8; Key:=0; end;
VK_RIGHT: begin HorzScrollBar.Position:=HorzScrollBar.Position+ClientWidth div 8; Key:=0; end;
end;
end;
FUpdated:=True;
inherited KeyDown(Key, Shift);
end;
procedure TLogListFPC.DoEnter;
begin
inherited DoEnter;
Invalidate;
end;
procedure TLogListFPC.DoExit;
begin
Invalidate;
inherited DoExit;
end;
procedure TLogListFPC.WMHScroll(var Message: TLMHScroll);
begin
inherited;
FUpdated:=True;
end;
procedure TLogListFPC.WMVScroll(var Message: TLMVScroll);
begin
inherited;
FUpdated:=True;
end;
constructor TLogListFPC.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
BorderStyle:=bsSingle;
if Parent=nil then Parent:=TWinControl(AOwner);
FLineSpace:=2;
BorderWidth:=2;
FAddFlag:=False;
FSkipLast:=False;
FIsViewHorz:=False;
FLineLimit:=100;
FOnDeleteLimit:=nil;
PrevCount:=-1;
FLastPosY:=0;
FLastPosX:=0;
LogData := TLogStringList.Create;
LogData.OnChanged:=@DataChange;
FEvent := TEvent.Create(nil,True,True,'LOGLIST'+IntToStr(Random(65535)));
FontChanged(Self);
LogData.DefaultBCol:=Color;
LogData.DefaultTCol:=Font.Color;
FTimer:=TTimer.Create(self);
FTimer.OnTimer:=@OnTimer;
FTimer.Interval:=100;
FUpdated:=True;
FLastHBar:=HorzScrollBar.IsScrollBarVisible;
end;
destructor TLogListFPC.Destroy;
begin
FTimer.OnTimer:=nil;
LogData.Free;
FEvent.SetEvent;
FEvent.Free;
inherited Destroy;
end;
procedure TLogListFPC.EraseBackground(DC: HDC);
begin
;
end;
procedure TLogListFPC.SetItemPos(Pos: Integer);
var
NewPos : Integer;
begin
if Assigned(VertScrollBar) then begin
if Pos<0 then Pos:=0;
NewPos:=Pos*tHeight;
Enter;
try
VertScrollBar.Position:=NewPos;
FUpdated:=True;
finally
Leave;
end;
end;
end;
procedure TLogListFPC.SetLastPos;
begin
if Assigned(VertScrollBar) then begin
Enter;
try
VertScrollBar.Position:=VertScrollBar.Range;
FUpdated:=True;
finally
Leave;
end;
end;
end;
procedure TLogListFPC.Enter;
begin
while FEvent.WaitFor(0)=wrTimeout do
Sleep(0);
FEvent.ResetEvent;
end;
procedure TLogListFPC.Leave;
begin
FEvent.SetEvent;
end;
function TLogListFPC.AddLog(const s: string; txtColor, BackColor: TColor
): Integer;
var
temp:TLogStringData;
begin
if (LogData.Count>0) and (FLineLimit<=LogData.Count) then begin
if Assigned(FOnDeleteLimit) then
FOnDeleteLimit(LogData.Strings[0]);
LogData.Delete(0);
end;
temp:=TLogStringData.Create;
try
temp.F:=txtColor;
temp.B:=BackColor;
CheckTextLen(s);
Result:=LogData.AddObject(s,temp);
FAddFlag:=True;
except
temp.Free;
raise;
end;
end;
function TLogListFPC.AddLog(const s: string; txtColor: TColor): Integer;
begin
Result:=AddLog(s,txtColor,LogData.DefaultBCol);
end;
function TLogListFPC.AddLog(const s: string): Integer;
begin
Result:=AddLog(s,LogData.DefaultTCol,LogData.DefaultBCol);
end;
function ReadLine(const s:string; var next:Integer; len:Integer):string;
var
i:Integer;
ch: char;
begin
i:=next;
Result:='';
while i<=len do begin
ch:=s[i];
if ch in [#10,#13] then begin
Inc(i);
if (i<=len) and (s[i] in [#10,#13]) then
if s[i]<>ch then
Inc(i);
break;
end else
Result:=Result+ch;
Inc(i);
end;
next:=i;
end;
function TLogListFPC.AddLogLine(const s: string): Integer;
var
temp:TLogStringData;
stemp:string;
i,l:Integer;
begin
if (LogData.Count>0) and (FLineLimit<=LogData.Count) then begin
if Assigned(FOnDeleteLimit) then
FOnDeleteLimit(LogData.Strings[0]);
LogData.Delete(0);
end;
i:=1;
l:=Length(s);
repeat
stemp:=ReadLine(s,i,l);
temp:=TLogStringData.Create;
try
temp.F:=LogData.DefaultTCol;
temp.B:=LogData.DefaultBCol;
CheckTextLen(s);
Result:=LogData.AddObject(stemp,temp);
except
temp.Free;
raise;
end;
until i>l;
FAddFlag:=True;
end;
procedure TLogListFPC.InsertLog(Idx: Integer; const s: string; txtColor,
BackColor: TColor);
var
temp:TLogStringData;
begin
if (LogData.Count>0) and (FLineLimit<=LogData.Count) then begin
if Assigned(FOnDeleteLimit) then
FOnDeleteLimit(LogData.Strings[0]);
LogData.Delete(0);
end;
temp:=TLogStringData.Create;
try
temp.F:=txtColor;
temp.B:=BackColor;
CheckTextLen(s);
if Idx>Count then
Idx:=Count;
LogData.Insert(Idx,s);
LogData.Objects[Idx]:=temp;
FAddFlag:=True;
except
temp.Free;
raise;
end;
end;
procedure TLogListFPC.InsertLog(Idx: Integer; const s: string;
txtColor: TColor);
begin
InsertLog(Idx,s,txtColor,LogData.DefaultBCol);
end;
procedure TLogListFPC.InsertLog(Idx: Integer; const s: string);
begin
InsertLog(Idx,s,LogData.DefaultTCol,LogData.DefaultBCol);
end;
procedure TLogListFPC.UpdateLog(Idx: Integer; const s: string; txtColor,
BackColor: TColor);
var
temp:TLogStringData;
begin
LogData.Strings[Idx]:=s;
temp:=TLogStringData.Create;
try
temp.F:=txtColor;
temp.B:=BackColor;
CheckTextLen(s);
LogData.Objects[Idx]:=temp;
FUpdated:=True;
except
temp.Free;
raise;
end;
end;
procedure TLogListFPC.UpdateLog(Idx: Integer; const s: string;
txtColor: TColor);
begin
UpdateLog(Idx,s,txtColor,LogData.DefaultBCol);
end;
procedure TLogListFPC.UpdateLog(Idx: Integer; const s: string);
begin
UpdateLog(Idx,s,LogData.DefaultTCol,LogData.DefaultBCol);
end;
procedure TLogListFPC.DeleteLog(Idx: Integer);
begin
LogData.Delete(Idx);
FUpdated:=True;
end;
procedure TLogListFPC.Clear;
begin
LogData.Clear;
FLastPosY:=0;
FLastPosX:=0;
MaxTextWidth:=0;
FAddFlag:=False;
FIsViewHorz:=False;
if Assigned(HorzScrollBar) then begin
HorzScrollBar.Range:=0;
HorzScrollBar.Position:=0;
end;
if Assigned(VertScrollBar) then
VertScrollBar.Range:=0;
FUpdated:=True;
end;
{ TLogStringList }
procedure TLogStringList.Enter;
begin
while fEvent.WaitFor(0)=wrTimeout do
sleep(0);
fEvent.ResetEvent;
end;
procedure TLogStringList.Leave;
begin
fEvent.SetEvent;
end;
function TLogStringList.GetCount: Integer;
begin
Enter;
try
Result:=inherited GetCount;
finally
Leave;
end;
end;
function TLogStringList.Get(Index: Integer): string;
begin
Enter;
try
Result:=inherited Get(Index);
finally
Leave;
end;
end;
function TLogStringList.GetObject(Index: Integer): TObject;
begin
Enter;
try
Result:=inherited GetObject(Index);
finally
Leave;
end;
end;
procedure TLogStringList.Put(Index: Integer; const S: string);
begin
Enter;
try
inherited Put(Index, S);
finally
Leave;
end;
end;
procedure TLogStringList.PutObject(Index: Integer; AObject: TObject);
begin
Enter;
try
inherited PutObject(Index, AObject);
finally
Leave;
end;
end;
procedure TLogStringList.InsertItem(Index: Integer; const S: string; O: TObject
);
begin
Enter;
try
inherited InsertItem(Index, S, O);
finally
Leave;
end;
end;
procedure TLogStringList.Clear;
begin
Enter;
try
inherited Clear;
finally
Leave;
end;
end;
procedure TLogStringList.Changed;
begin
inherited Changed;
if Assigned(FOnChanged) then
FOnChanged(Self);
end;
constructor TLogStringList.Create;
begin
inherited;
DefaultBCol:=clBtnFace;
DefaultTCol:=clWindowText;
OwnsObjects:=True;
fEvent:=TEvent.Create(nil,True,True,'LOGSTRLST'+IntToStr(Random(65535)));
end;
destructor TLogStringList.Destroy;
begin
fEvent.SetEvent;
fEvent.Free;
inherited Destroy;
end;
function TLogStringList.GetStrObj(Index, MaxLen: Integer; out
obj: TLogStringData): string;
begin
try
Result:=Copy(Get(Index),1,MaxLen);
obj:=TLogStringData(GetObject(Index));
except
obj:=nil;
end;
end;
{ TLogStringData }
constructor TLogStringData.Create;
begin
Obj:=nil;
end;
destructor TLogStringData.Destroy;
begin
inherited Destroy;
end;
end.