-
Notifications
You must be signed in to change notification settings - Fork 33
/
HGM.Controls.Chat.pas
1324 lines (1185 loc) · 34 KB
/
HGM.Controls.Chat.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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit HGM.Controls.Chat;
interface
uses
Winapi.Messages, Winapi.Windows, System.SysUtils, System.Classes,
System.Contnrs, System.Types, System.UITypes, Vcl.Controls, Vcl.Forms,
Vcl.Menus, Vcl.Graphics, Vcl.StdCtrls, Vcl.GraphUtil, Vcl.ImgList, Vcl.Themes,
Winapi.ShellAPI, System.Generics.Collections, HGM.Common, Vcl.Dialogs;
type
TChatMessageType = (mtOpponent, mtMe);
TChatItems = class;
ThCustomChat = class;
TChatItem = class abstract
private
FOwner: TChatItems;
FNeedCalc: Boolean;
FCalcedRect: TRect;
FSelected: Boolean;
FImageIndex: Integer;
FCanSelected: Boolean;
FDate: TDateTime;
FText: string;
FColor: TColor;
procedure SetOwner(const Value: TChatItems);
procedure SetSelected(const Value: Boolean);
procedure SetImageIndex(const Value: Integer);
procedure SetDate(const Value: TDateTime);
procedure SetText(const Value: string);
procedure SetColor(const Value: TColor);
public
function DrawRect(Canvas: TCanvas; Rect: TRect): TRect; virtual;
function DrawImage(Canvas: TCanvas; Rect: TRect): TRect; virtual;
function CalcRect(Canvas: TCanvas; Rect: TRect): TRect; virtual;
constructor Create(AOwner: TChatItems); virtual;
destructor Destroy; override;
property Owner: TChatItems read FOwner write SetOwner;
property Selected: Boolean read FSelected write SetSelected;
property ImageIndex: Integer read FImageIndex write SetImageIndex;
property Date: TDateTime read FDate write SetDate;
property NeedCalc: Boolean read FNeedCalc write FNeedCalc;
property CalcedRect: TRect read FCalcedRect write FCalcedRect;
property Text: string read FText write SetText;
property Color: TColor read FColor write SetColor;
end;
TChatMessage = class(TChatItem)
private
FCalcedFromHeight: Integer;
FShowFrom: Boolean;
FFrom: string;
FFromType: TChatMessageType;
FFromColor: TColor;
FFromColorSelect: TColor;
procedure SetCalcedFromHeight(const Value: Integer);
procedure SetFrom(const Value: string);
procedure SetFromColor(const Value: TColor);
procedure SetFromColorSelect(const Value: TColor);
procedure SetFromType(const Value: TChatMessageType);
procedure SetShowFrom(const Value: Boolean); virtual;
function GetFromType: TChatMessageType;
public
constructor Create(AOwner: TChatItems); override;
destructor Destroy; override;
function CalcRect(Canvas: TCanvas; Rect: TRect): TRect; override;
function DrawRect(Canvas: TCanvas; Rect: TRect): TRect; override;
property ShowFrom: Boolean read FShowFrom write SetShowFrom;
property From: string read FFrom write SetFrom;
property FromType: TChatMessageType read GetFromType write SetFromType;
property FromColor: TColor read FFromColor write SetFromColor;
property FromColorSelect: TColor read FFromColorSelect write SetFromColorSelect;
property CalcedFromHeight: Integer read FCalcedFromHeight write SetCalcedFromHeight;
end;
TChatInfo = class(TChatItem)
private
FFillColor: TColor;
public
procedure SetFillColor(const Value: TColor);
function CalcRect(Canvas: TCanvas; Rect: TRect): TRect; override;
function DrawRect(Canvas: TCanvas; Rect: TRect): TRect; override;
constructor Create(AOwner: TChatItems); override;
property FillColor: TColor read FFillColor write SetFillColor;
end;
TChatItems = class(TList<TChatItem>)
private
FOwner: ThCustomChat;
procedure SetOwner(const Value: ThCustomChat);
public
function AddMessage: TChatMessage; overload;
function AddInfo: TChatInfo; overload;
function Add(Value: TChatItem): Integer; overload;
function Add<T: TChatItem>(): T; overload;
function Insert(Index: Integer; Value: TChatItem): Integer;
function SelectCount: Integer;
function GetTop<T: TChatItem>(): T;
procedure Delete(Index: NativeInt);
procedure Clear;
procedure DoChanged(Item: TChatItem);
procedure NeedResize;
constructor Create(AOwner: ThCustomChat);
destructor Destroy; override;
property Owner: ThCustomChat read FOwner write SetOwner;
end;
TOnSelectionEvent = procedure(Sender: TObject; Count: Integer) of object;
ThCustomChat = class(TCustomControl)
private
FOffset: Integer;
FMaxOffset: Integer;
FItems: TChatItems;
FMousePos: TPoint;
FItemUnderMouse: Integer;
FPreviousSelectedItem: Integer;
FSelectionMode: Boolean;
FScrollBarVisible: Boolean;
FMouseIn: Boolean;
FMouseInScroll: Boolean;
FMouseInScrollButton: Boolean;
FScrollPosStart: TPoint;
FScrollLentgh: Integer;
FScrollPos: Integer;
FScrolling: Boolean;
FPaintCounter: Integer;
FImageList: TImageList;
FDrawImages: Boolean;
FDragItem: Boolean;
FOnSelectionStart: TOnSelectionEvent;
FOnSelectionEnd: TOnSelectionEvent;
FOnSelectionChange: TOnSelectionEvent;
FColorMe: TColor;
FColorInfo: TColor;
FColorSelection: TColor;
FColorOpponent: TColor;
FColorScrollActive: TColor;
FColorScrollInactive: TColor;
FColorScrollButton: TColor;
FImageMargin: Integer;
FPaddingSize: Integer;
FBorderWidth: Integer;
FRevertAdding: Boolean;
FWasEventListEnd: Boolean;
FShowingDownButton: Boolean;
FDownButtonRect: TRect;
FCalcOnly: Boolean;
FOnPaint: TNotifyEvent;
FUpdateCount: Integer;
FOnListEnd: TNotifyEvent;
FShowDownButton: Boolean;
function GetItem(Index: Integer): TChatItem;
procedure SetItem(Index: Integer; const Value: TChatItem);
procedure SetItems(const Value: TChatItems);
procedure FOnMouseWheelDown(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
procedure FOnMouseWheelUp(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
procedure FOnMouseMoveEvent(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure FOnClick(Sender: TObject);
procedure FOnMouseEnter(Sender: TObject);
procedure FOnDownButtonNeed;
procedure FOnDownButtonHide;
procedure FOnMouseLeave(Sender: TObject);
procedure FOnMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure FOnMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure SetScrollBarVisible(const Value: Boolean);
procedure CheckOffset;
procedure SetImageList(const Value: TImageList);
procedure NeedRepaint;
procedure SetDrawImages(const Value: Boolean);
procedure SelectionChange(Count: Integer);
procedure SetOnSelectionChange(const Value: TOnSelectionEvent);
procedure SetOnSelectionEnd(const Value: TOnSelectionEvent);
procedure SetOnSelectionStart(const Value: TOnSelectionEvent);
procedure WMSize(var Message: TWMSize); message WM_SIZE;
procedure SetColorInfo(const Value: TColor);
procedure SetColorMe(const Value: TColor);
procedure SetColorOpponent(const Value: TColor);
procedure SetColorSelection(const Value: TColor);
procedure SetColorScrollActive(const Value: TColor);
procedure SetColorScrollButton(const Value: TColor);
procedure SetColorScrollInactive(const Value: TColor);
procedure SetBorderWidth(const Value: Integer);
procedure SetImageMargin(const Value: Integer);
procedure SetPaddingSize(const Value: Integer);
procedure SetRevertAdding(const Value: Boolean);
procedure SetOnPaint(const Value: TNotifyEvent);
procedure SetOnListEnd(const Value: TNotifyEvent);
function NeedDrawDownButton: Boolean;
function GetShowDownButton: Boolean;
procedure SetShowDownButton(const Value: Boolean);
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure Paint; override;
procedure UpdateStyleElements; override;
procedure WMNCPaint(var Message: TMessage); message WM_NCPAINT;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure BeginUpdate;
procedure EndUpdate(Force: Boolean = False);
procedure Reset;
property Item[Index: Integer]: TChatItem read GetItem write SetItem;
property Items: TChatItems read FItems write SetItems;
property DoubleBuffered default True;
property ScrollBarVisible: Boolean read FScrollBarVisible write SetScrollBarVisible default True;
property ImageList: TImageList read FImageList write SetImageList;
property DrawImages: Boolean read FDrawImages write SetDrawImages default False;
property OnSelectionStart: TOnSelectionEvent read FOnSelectionStart write SetOnSelectionStart;
property OnSelectionChange: TOnSelectionEvent read FOnSelectionChange write SetOnSelectionChange;
property OnSelectionEnd: TOnSelectionEvent read FOnSelectionEnd write SetOnSelectionEnd;
property Color default $0020160F;
property ColorInfo: TColor read FColorInfo write SetColorInfo default $003A2C1D;
property ColorOpponent: TColor read FColorOpponent write SetColorOpponent default $00322519;
property ColorMe: TColor read FColorMe write SetColorMe default $0078522B;
property ColorSelection: TColor read FColorSelection write SetColorSelection default $00A5702E;
property ColorScrollInactive: TColor read FColorScrollInactive write SetColorScrollInactive default $00332A24;
property ColorScrollActive: TColor read FColorScrollActive write SetColorScrollActive default $003C342E;
property ColorScrollButton: TColor read FColorScrollButton write SetColorScrollButton default $00605B56;
property BorderWidth: Integer read FBorderWidth write SetBorderWidth default 4;
property PaddingSize: Integer read FPaddingSize write SetPaddingSize default 10;
property ImageMargin: Integer read FImageMargin write SetImageMargin default 36;
property RevertAdding: Boolean read FRevertAdding write SetRevertAdding;
property OnPaint: TNotifyEvent read FOnPaint write SetOnPaint;
property OnListEnd: TNotifyEvent read FOnListEnd write SetOnListEnd;
property ShowDownButton: Boolean read GetShowDownButton write SetShowDownButton;
end;
ThChat = class(ThCustomChat)
public
property Item;
property Items;
published
property Align;
property Color;
property Canvas;
property ScrollBarVisible;
property DoubleBuffered;
property Visible;
property ImageList;
property DrawImages;
//
property OnSelectionStart;
property OnSelectionChange;
property OnSelectionEnd;
property OnPaint;
property OnListEnd;
//
property ColorInfo;
property ColorOpponent;
property ColorMe;
property ColorSelection;
property ColorScrollInactive;
property ColorScrollActive;
property ColorScrollButton;
property BorderWidth;
property PaddingSize;
property ImageMargin;
property ShowDownButton default False;
end;
procedure Register;
implementation
uses
Math, HGM.Common.Utils;
procedure Register;
begin
RegisterComponents(PackageName, [ThChat]);
end;
{ ThCustomChat }
procedure ThCustomChat.BeginUpdate;
begin
Inc(FUpdateCount);
end;
procedure ThCustomChat.CheckOffset;
begin
FOffset := Max(0, Min(FMaxOffset, FOffset));
if not FShowingDownButton then
begin
if FOffset > 300 then
FOnDownButtonNeed;
end
else
begin
if FOffset < 300 then
FOnDownButtonHide;
end;
if FMaxOffset = FOffset then
begin
FWasEventListEnd := True;
if Assigned(FOnListEnd) then
FOnListEnd(Self);
end
else
FWasEventListEnd := False;
end;
constructor ThCustomChat.Create(AOwner: TComponent);
var
i, j: Integer;
begin
inherited Create(AOwner);
ControlStyle := [csAcceptsControls, csCaptureMouse, csOpaque, csClickEvents, csDoubleClicks,
csPannable, csGestures];
Width := 200;
Height := 400;
Color := $0020160F;
UseDockManager := True;
ParentBackground := False;
DoubleBuffered := True;
TabStop := True;
OnMouseWheelDown := FOnMouseWheelDown;
OnMouseWheelUp := FOnMouseWheelUp;
OnMouseMove := FOnMouseMoveEvent;
OnClick := FOnClick;
OnMouseDown := FOnMouseDown;
OnMouseUp := FOnMouseUp;
OnMouseEnter := FOnMouseEnter;
OnMouseLeave := FOnMouseLeave;
FShowDownButton := False;
FRevertAdding := False;
FWasEventListEnd := False;
FShowingDownButton := False;
FCalcOnly := False;
FColorOpponent := $00322519;
FColorMe := $0078522B;
FColorInfo := $003A2C1D;
FColorSelection := $00A5702E;
FColorScrollActive := $003C342E;
FColorScrollInactive := $00332A24;
FColorScrollButton := $00605B56;
FBorderWidth := 4;
FPaddingSize := 10;
FImageMargin := 36;
FItemUnderMouse := -1;
FDragItem := False;
FSelectionMode := False;
FPaintCounter := 0;
FMouseIn := False;
FMouseInScroll := False;
FMouseInScrollButton := False;
FScrollBarVisible := True;
FPreviousSelectedItem := -100;
FItems := TChatItems.Create(Self);
if csDesigning in ComponentState then
begin
for i := 0 to 40 do
begin
with Items.AddMessage do
begin
From := 'UserName';
if Random(40) in [20..30] then
FromType := mtMe;
Text := 'Text body';
for j := 1 to Random(10) do
Text := Text + 'Text body';
Text := DateTimeToStr(Now) + #13#10 + Text;
FromColor := RGB(RandomRange(100, 240), RandomRange(100, 240), RandomRange(100, 240));
end;
if i in [25..30] then
with Items.AddInfo do
begin
Text := DateTimeToStr(Now);
end;
end;
end;
end;
procedure ThCustomChat.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
end;
destructor ThCustomChat.Destroy;
begin
FItems.Clear;
FItems.Free;
inherited;
end;
procedure ThCustomChat.EndUpdate(Force: Boolean);
begin
if Force then
FUpdateCount := 0
else
Dec(FUpdateCount);
if FUpdateCount = 0 then
NeedRepaint;
end;
procedure ThCustomChat.FOnClick(Sender: TObject);
begin
if not Focused then
SetFocus;
if (not FDragItem) and (not FScrolling) and (FSelectionMode) then
if FItemUnderMouse >= 0 then
begin
FItems[FItemUnderMouse].Selected := not FItems[FItemUnderMouse].Selected;
end;
end;
procedure ThCustomChat.FOnDownButtonHide;
begin
FShowingDownButton := False;
NeedRepaint;
end;
procedure ThCustomChat.FOnDownButtonNeed;
begin
FShowingDownButton := True;
NeedRepaint;
end;
procedure ThCustomChat.FOnMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
begin
if FScrollBarVisible then
begin
if FMouseInScroll and not FMouseInScrollButton then
begin
FScrollPos := FScrollLentgh - Y + 20 {20 - ïîëîâèíà ðàçìåð ñêðîëà};
FOffset := Round((FScrollPos / (FScrollLentgh / 100)) / (100 / FMaxOffset));
CheckOffset;
FScrolling := True;
FScrollPosStart := TPoint.Create(X, Y);
NeedRepaint;
Exit;
end;
if FMouseInScrollButton then
begin
FScrolling := True;
FScrollPosStart := TPoint.Create(X, Y);
NeedRepaint;
Exit;
end;
end;
end;
NeedRepaint;
end;
procedure ThCustomChat.FOnMouseEnter(Sender: TObject);
begin
FMouseIn := True;
NeedRepaint;
end;
procedure ThCustomChat.FOnMouseLeave(Sender: TObject);
begin
FMouseIn := False;
NeedRepaint;
end;
procedure ThCustomChat.FOnMouseMoveEvent(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
{if not Focused then
SetFocus; }
FMousePos := TPoint.Create(X, Y);
if FScrolling then
begin
FScrollPos := FScrollPos + (FScrollPosStart.Y - FMousePos.Y);
FOffset := Round((FScrollPos / (FScrollLentgh / 100)) / (100 / FMaxOffset));
CheckOffset;
FScrollPosStart := FMousePos;
end
else
begin
if not FDragItem then
begin
if ssLeft in Shift then
if FItemUnderMouse >= 0 then
begin
FDragItem := True;
FPreviousSelectedItem := -1;
end;
end;
if FDragItem then
begin
if (FItemUnderMouse >= 0) and (FItemUnderMouse <> FPreviousSelectedItem) then
begin
FPreviousSelectedItem := FItemUnderMouse;
FItems[FItemUnderMouse].Selected := not FItems[FItemUnderMouse].Selected;
end
else
NeedRepaint;
end;
end;
NeedRepaint;
end;
procedure ThCustomChat.FOnMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
begin
if FScrollBarVisible then
FScrolling := False;
if FDragItem then
begin
FDragItem := False;
FPreviousSelectedItem := -1;
end;
end;
end;
procedure ThCustomChat.FOnMouseWheelDown(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin
FOffset := FOffset - 50;
CheckOffset;
NeedRepaint;
end;
procedure ThCustomChat.FOnMouseWheelUp(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin
FOffset := FOffset + 50;
CheckOffset;
NeedRepaint;
end;
function ThCustomChat.GetItem(Index: Integer): TChatItem;
begin
Result := FItems[Index];
end;
function ThCustomChat.GetShowDownButton: Boolean;
begin
Result := FShowDownButton;
end;
procedure ThCustomChat.NeedRepaint;
begin
if FUpdateCount <= 0 then
Invalidate;
end;
procedure ThCustomChat.Paint;
const
LimitWidth = 1000;
var
Rect, ItemRect, LastRect, TxtRect, Limit, ImageRect: TRect;
BaseColor: TColor;
i, Radius: Integer;
FStartDraw, FSkip, FNeedImage: Boolean;
lpPaint: TPaintStruct;
function NeedDraw(Item: TRect): Boolean;
begin
if Item.Bottom < ClientRect.Top then
Exit(False);
if Item.Top > ClientRect.Bottom then
Exit(False);
Result := True;
end;
begin
Inc(FPaintCounter);
Rect := ClientRect;
Rect.Inflate(-BorderWidth, -BorderWidth);
Rect.Right := Rect.Right - 25; //scroll
BaseColor := Color;
FStartDraw := False;
FSkip := False;
BeginPaint(Handle, lpPaint);
with Canvas do
begin
if not FCalcOnly then
begin
Brush.Color := BaseColor;
FillRect(ClientRect);
end;
LastRect := TRect.Create(Rect.Left, Rect.Bottom, Rect.Right, Rect.Bottom);
FItemUnderMouse := -1;
if FItems.Count > 0 then
for i := FItems.Count - 1 downto 0 do
begin
Limit := Rect;
Limit.Inflate(-PaddingSize, -PaddingSize);
FNeedImage := False;
if FDrawImages then
if (FItems[i] is TChatMessage) then
begin
FNeedImage := ((FItems[i] as TChatMessage).FromType = mtOpponent) or (ClientRect.Width >
LimitWidth);
Limit.Width := Limit.Width - FImageMargin;
end;
Limit.Width := Min(500, Limit.Width);
TxtRect := FItems[i].CalcRect(Canvas, Limit);
Brush.Color := FColorOpponent;
ItemRect := TxtRect;
ItemRect.Inflate(PaddingSize, PaddingSize);
ItemRect.Location := TPoint.Create(Rect.Left, LastRect.Top - ItemRect.Height);
LastRect := ItemRect;
if (FItems[i] is TChatInfo) then
begin
LastRect.Offset(0, -20);
end
else
LastRect.Offset(0, -10);
if FSkip then
Continue;
Radius := 14;
if (FItems[i] is TChatMessage) then
begin
if (FItems[i] as TChatMessage).FromType = mtMe then
begin
if ClientRect.Width <= LimitWidth then
ItemRect.Location := TPoint.Create(Rect.Right - ItemRect.Width, ItemRect.Top)
else if FNeedImage then
ItemRect.Location := TPoint.Create(ItemRect.Left + FImageMargin, ItemRect.Top);
Brush.Color := FColorMe;
end
else if FNeedImage then
ItemRect.Location := TPoint.Create(ItemRect.Left + FImageMargin, ItemRect.Top);
end
else if (FItems[i] is TChatInfo) then
begin
if ClientRect.Width <= LimitWidth then
ItemRect.Offset(Rect.CenterPoint.X - (ItemRect.Width div 2 + BorderWidth), 0)
else
ItemRect.Offset((LimitWidth div 2) div 2 - (ItemRect.Width div 2 + BorderWidth), 0);
if (FItems[i] as TChatInfo).FillColor = clNone then
Brush.Color := FColorInfo
else
Brush.Color := (FItems[i] as TChatInfo).FillColor;
Radius := ItemRect.Height;
end;
ItemRect.Offset(PaddingSize, FOffset);
if NeedDraw(ItemRect) then
begin
Limit := ItemRect;
Limit.Left := 0;
Limit.Right := ClientRect.Right;
if Limit.Contains(FMousePos) then
begin
FItemUnderMouse := i;
end
else
begin
//Brush.Color := clRed;
end;
FStartDraw := True;
Brush.Style := bsSolid;
if (FItems[i] is TChatMessage) and (FItems[i].Selected) then
Brush.Color := FColorSelection;
if not FCalcOnly then
begin
Pen.Color := ColorDarker(Brush.Color, 5);
RoundRect(ItemRect, Radius, Radius);
Brush.Style := bsClear;
end;
TxtRect.Location := TPoint.Create(ItemRect.Left + PaddingSize, ItemRect.Top + PaddingSize);
if not FCalcOnly then
begin
FItems[i].DrawRect(Canvas, TxtRect);
end;
if FNeedImage then
begin
ImageRect := TRect.Create(0, 0, ImageMargin, ImageMargin);
ImageRect.Location := TPoint.Create(ItemRect.Left - ImageRect.Width - 3, ItemRect.Bottom
- ImageRect.Height + 2);
if not FCalcOnly then
begin
FItems[i].DrawImage(Canvas, ImageRect);
end;
end;
end
else if FStartDraw then
FSkip := True;
end;
FMaxOffset := 0 - LastRect.Top;
if (not FCalcOnly) and (Assigned(FOnPaint)) then
FOnPaint(Self);
if FScrollBarVisible or FCalcOnly then
begin
FMouseInScroll := False;
FMouseInScrollButton := False;
if FMouseIn or (csDesigning in ComponentState) then
begin
Rect := ClientRect;
ItemRect := Rect;
ItemRect.Left := ItemRect.Right - 10;
ItemRect.Right := ItemRect.Right - 4;
ItemRect.Inflate(0, -4);
if ItemRect.Contains(FMousePos) or FScrolling then
begin
FMouseInScroll := True;
Brush.Color := FColorScrollActive;
end
else
Brush.Color := FColorScrollInactive;
if not FCalcOnly then
begin
Pen.Color := Brush.Color;
RoundRect(ItemRect, 6, 6);
end;
LastRect := ItemRect;
LastRect.Height := 40;
FScrollLentgh := ItemRect.Height - LastRect.Height;
//FOffset
//FMaxOffset
FScrollPos := Round((FScrollLentgh / 100) * ((100 / FMaxOffset) * FOffset));
LastRect.Location := TPoint.Create(LastRect.Left, (ItemRect.Bottom - LastRect.Height) - FScrollPos);
if not FCalcOnly then
begin
if LastRect.Contains(FMousePos) then
FMouseInScrollButton := True;
Brush.Color := FColorScrollButton;
Pen.Color := Brush.Color;
RoundRect(LastRect, 6, 6);
end;
end;
end;
if NeedDrawDownButton then
begin
Rect := ClientRect;
FDownButtonRect := TRect.Create(TPoint.Create(Rect.Right - 100, Rect.Bottom - 100), 40, 40);
FillRect(FDownButtonRect);
end;
{TextOut(0, 0, FOffset.ToString);
TextOut(0, 20, FMaxOffset.ToString);
TextOut(0, 40, FPaintCounter.ToString); }
end;
EndPaint(Handle, lpPaint);
end;
function ThCustomChat.NeedDrawDownButton: Boolean;
begin
Result := FShowDownButton and FShowingDownButton;
end;
procedure ThCustomChat.Reset;
begin
FOffset := 0;
CheckOffset;
NeedRepaint;
end;
procedure ThCustomChat.UpdateStyleElements;
begin
Invalidate;
end;
procedure ThCustomChat.WMNCPaint(var Message: TMessage);
begin
NeedRepaint;
end;
procedure ThCustomChat.WMSize(var Message: TWMSize);
begin
FItems.NeedResize;
FCalcOnly := True;
try
Paint;
finally
FCalcOnly := False;
end;
if FScrollLentgh > 0 then
FOffset := Round((FScrollPos / (FScrollLentgh / 100)) / (100 / FMaxOffset))
else
FOffset := 0;
CheckOffset;
NeedRepaint;
inherited;
end;
procedure ThCustomChat.SelectionChange(Count: Integer);
begin
if FSelectionMode and (Count = 0) then
begin
FSelectionMode := False;
FPreviousSelectedItem := -1;
if Assigned(FOnSelectionEnd) then
FOnSelectionEnd(Self, 0);
end
else if FSelectionMode and (Count > 0) then
begin
if Assigned(FOnSelectionChange) then
FOnSelectionChange(Self, Count);
end
else if (not FSelectionMode) and (Count > 0) then
begin
FSelectionMode := True;
if Assigned(FOnSelectionStart) then
FOnSelectionStart(Self, Count);
end;
end;
procedure ThCustomChat.SetBorderWidth(const Value: Integer);
begin
FBorderWidth := Value;
if not (csLoading in ComponentState) then
begin
FItems.NeedResize;
NeedRepaint;
end;
end;
procedure ThCustomChat.SetColorInfo(const Value: TColor);
begin
FColorInfo := Value;
end;
procedure ThCustomChat.SetColorMe(const Value: TColor);
begin
FColorMe := Value;
end;
procedure ThCustomChat.SetColorOpponent(const Value: TColor);
begin
FColorOpponent := Value;
end;
procedure ThCustomChat.SetColorScrollActive(const Value: TColor);
begin
FColorScrollActive := Value;
end;
procedure ThCustomChat.SetColorScrollButton(const Value: TColor);
begin
FColorScrollButton := Value;
end;
procedure ThCustomChat.SetColorScrollInactive(const Value: TColor);
begin
FColorScrollInactive := Value;
end;
procedure ThCustomChat.SetColorSelection(const Value: TColor);
begin
FColorSelection := Value;
end;
procedure ThCustomChat.SetDrawImages(const Value: Boolean);
begin
FDrawImages := Value;
if not (csLoading in ComponentState) then
begin
FItems.NeedResize;
NeedRepaint;
end;
end;
procedure ThCustomChat.SetImageList(const Value: TImageList);
begin
FImageList := Value;
NeedRepaint;
end;
procedure ThCustomChat.SetImageMargin(const Value: Integer);
begin
FImageMargin := Value;
if not (csLoading in ComponentState) then
begin
FItems.NeedResize;
NeedRepaint;
end;
end;
procedure ThCustomChat.SetItem(Index: Integer; const Value: TChatItem);
begin
FItems[Index] := Value;
NeedRepaint;
end;
procedure ThCustomChat.SetItems(const Value: TChatItems);
begin
if Assigned(FItems) then
FItems.Free;
FItems := Value;
end;
procedure ThCustomChat.SetOnListEnd(const Value: TNotifyEvent);
begin
FOnListEnd := Value;
end;
procedure ThCustomChat.SetOnPaint(const Value: TNotifyEvent);
begin
FOnPaint := Value;
end;
procedure ThCustomChat.SetOnSelectionChange(const Value: TOnSelectionEvent);
begin
FOnSelectionChange := Value;
end;
procedure ThCustomChat.SetOnSelectionEnd(const Value: TOnSelectionEvent);
begin
FOnSelectionEnd := Value;
end;
procedure ThCustomChat.SetOnSelectionStart(const Value: TOnSelectionEvent);
begin
FOnSelectionStart := Value;
end;
procedure ThCustomChat.SetPaddingSize(const Value: Integer);
begin
FPaddingSize := Value;
if not (csLoading in ComponentState) then
begin
FItems.NeedResize;
NeedRepaint;
end;
end;
procedure ThCustomChat.SetRevertAdding(const Value: Boolean);
begin
FRevertAdding := Value;
end;
procedure ThCustomChat.SetScrollBarVisible(const Value: Boolean);
begin
FScrollBarVisible := Value;
NeedRepaint;
end;
procedure ThCustomChat.SetShowDownButton(const Value: Boolean);
begin
FShowDownButton := Value;
NeedRepaint;
end;
{ TChatItems }
function TChatItems.Add(Value: TChatItem): Integer;
begin
if not FOwner.RevertAdding then
Result := inherited Add(Value)
else
begin
Insert(0, Value);
Result := 0;
end;
FOwner.NeedRepaint;
end;
function TChatItems.Add<T>(): T;
begin
Result := T.Create(Self);
Add(Result);
FOwner.NeedRepaint;
end;
function TChatItems.AddInfo: TChatInfo;
begin
Result := TChatInfo.Create(Self);
Add(Result);
FOwner.NeedRepaint;
end;
function TChatItems.AddMessage: TChatMessage;
begin
Result := TChatMessage.Create(Self);
Add(Result);
FOwner.NeedRepaint;
end;
procedure TChatItems.Clear;
var
i: Integer;
begin
for i := 0 to Count - 1 do
Items[i].Free;
inherited;
FOwner.Reset;
end;
constructor TChatItems.Create(AOwner: ThCustomChat);
begin