-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuStringGridHelper.pas
More file actions
1748 lines (1541 loc) · 51.9 KB
/
Copy pathuStringGridHelper.pas
File metadata and controls
1748 lines (1541 loc) · 51.9 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
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 uStringGridHelper;
interface
uses
Vcl.Grids, System.Classes, Vcl.Dialogs, System.SysUtils, Winapi.Windows, Vcl.ExtCtrls,
Vcl.Controls, Vcl.Forms, ComObj, Vcl.Printers, System.Variants, Vcl.Graphics,
System.Generics.Collections, ClipBrd, uProgressHelper, uSmartComparer;
type
TGridHack = class(TStringGrid);
type
TExportType = (etExcel, etPDF, etCSV); //Export Tipleri
type //Hücrelerin stilleri
TCellStyle = record
BackColor: TColor;
FontColor: TColor;
FontBold: Boolean;
FontItalic: Boolean;
FontSize: Integer;
end;
type //Kýsayol Tuþlarý
TKeyConfig = record
Key: Word;
UseCtrl: Boolean;
UseShift: Boolean;
UseAlt: Boolean;
end;
//Klavye kýsayollarýný aktarma
function MakeKeyConfig(AKey: Word; ACtrl: Boolean = False; AShift: Boolean = False; AAlt: Boolean = False): TKeyConfig;
type //Grid silindiðinde haber alacak olan takipçi sýnýf
TGridObserver = class(TComponent)
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
end;
type
TStringGridHelperData = class
private
FCellStyles: array of array of TCellStyle;
FLastSearchText: string;
FLastSearchCol: Integer;
FLastSearchRow: Integer;
FCaseSensitive: Boolean;
FOriginalKeyDown: TKeyEvent;
FOriginalDrawCell: TDrawCellEvent;
FOriginalSelectCell: TSelectCellEvent;
FOriginalMouseDown: TMouseEvent;
FSearching: Boolean;
FCancelled: Boolean;
FExporting: Boolean;
FProgress: TProgressHelper;
FSearchKey: TKeyConfig;
FExcelKey: TKeyConfig;
FPDFKey: TKeyConfig;
FCSVKey: TKeyConfig;
FCancelKey: TKeyConfig;
FDeleteRowKey: TKeyConfig;
FAutoFitKey: TKeyConfig;
FGrid: TStringGrid;
FObserver: TGridObserver;
FHighlightCol: Integer;
FHighlightRow: Integer;
FOriginalHighlightColor: TColor;
FIsHighlighted: Boolean;
FSearchHighlightColor: TColor;
FSortColumn: Integer;
FSortAscending: Boolean;
FHeaderCount: Integer;
FOriginalFixedRows: Integer;
public
constructor Create(AGrid: TStringGrid);
destructor Destroy; override;
end;
TStringGridHelper = class helper for TStringGrid
private
function GetHelperData: TStringGridHelperData;
procedure GridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure GridMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
function CompareText(const S1, S2: string): Boolean;
procedure ExportToCSVFile(const FileName, FileTypeText: string);
procedure ExportToFile(ExportType: TExportType);
function CheckKeyMatch(Key: Word; Shift: TShiftState; Config: TKeyConfig): Boolean;
procedure HandleNavigationKeys(var Key: Word; Shift: TShiftState);
function GetNextVisibleCol(CurrentCol: Integer; Direction: Integer): Integer;
function GetNextVisibleRow(CurrentRow: Integer; Direction: Integer): Integer;
procedure DeleteCurrentRow;
procedure AutoFitColumns;
procedure SortColumn(ACol: Integer; Ascending: Boolean = True);
function GetCellStyle(ACol, ARow: Integer): TCellStyle;
procedure InternalDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
procedure ShiftStyles(StartRow: Integer; Up: Boolean);
function GetCaseSensitive: Boolean;
procedure SetCaseSensitive(const Value: Boolean);
function GetIsSearching: Boolean;
function GetIsExporting: Boolean;
procedure GridSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
procedure ClearHighlight;
procedure HighlightCell(ACol, ARow: Integer);
function GetSearchHighlightColor: TColor;
procedure SetSearchHighlightColor(const Value: TColor);
procedure SortByColumn(ACol: Integer; Ascending: Boolean);
function GetHeaderCount: Integer;
function GetHeaderRowIndex(HeaderIndex: Integer): Integer;
procedure SwapRows(const Data: TStringGridHelperData; Idx1, Idx2: Integer);
public
property CaseSensitive: Boolean read GetCaseSensitive write SetCaseSensitive;
property IsSearching: Boolean read GetIsSearching;
property IsExporting: Boolean read GetIsExporting;
property HeaderCount: Integer read GetHeaderCount;
procedure InitHelper;
procedure FreeHelper;
procedure Search(const ASearchText: string = '');
procedure Reset;
procedure CancelSearch;
procedure ExportToExcel;
procedure ExportToPDF;
procedure ExportToCSV;
procedure DeleteRow;
procedure CloneRow(CurrentRow: Integer);
procedure AutoFit;
procedure SetSearchKey(AKey: Word; ACtrl: Boolean = True; AShift: Boolean = False; AAlt: Boolean = False);
procedure SetExcelKey(AKey: Word; ACtrl: Boolean = True; AShift: Boolean = False; AAlt: Boolean = False);
procedure SetPDFKey(AKey: Word; ACtrl: Boolean = True; AShift: Boolean = False; AAlt: Boolean = False);
procedure SetCSVKey(AKey: Word; ACtrl: Boolean = True; AShift: Boolean = False; AAlt: Boolean = False);
procedure SetCancelKey(AKey: Word; ACtrl: Boolean = False; AShift: Boolean = False; AAlt: Boolean = False);
procedure SetDeleteRowKey(AKey: Word; ACtrl: Boolean = True; AShift: Boolean = False; AAlt: Boolean = False);
procedure SetAutoFitKey(AKey: Word; ACtrl: Boolean = True; AShift: Boolean = False; AAlt: Boolean = False);
procedure SetCellStyle(ACol, ARow: Integer; ABackColor, AFontColor: TColor; ABold: Boolean = False; AItalic: Boolean = False; AFontSize: Integer = 8);
procedure ClearAllStyles;
procedure PrepareStyleArray;
function AddHeader: Integer;
procedure RemoveHeader(HeaderIndex: Integer);
procedure RemoveAllHeaders;
procedure SetHeaderText(HeaderIndex, ACol: Integer; const AText: string); overload;
function GetHeaderText(HeaderIndex, ACol: Integer): string;
procedure SetHeaderStyle(HeaderIndex: Integer; ABackColor, AFontColor: TColor; ABold: Boolean = True; AItalic: Boolean = False; AFontSize: Integer = 9);
function IsHeaderRow(ARow: Integer): Boolean; overload;
function GetHeaderIndex(ARow: Integer): Integer;
end;
implementation
const
//Grid sütun geniþlik ayarlarý
MIN_COL_WIDTH = 30;
MAX_COL_WIDTH = 500;
CELL_PADDING = 20;
//Progress bar güncelleme sýklýðý
PROGRESS_UPDATE_INTERVAL_COL = 10; //Her 10 sütunda bir
PROGRESS_UPDATE_INTERVAL_ROW = 50; //Her 50 satýrda bir
PROGRESS_UPDATE_INTERVAL_SEARCH = 100; //Her 100 satýrda bir (arama için)
//Highlight rengi (default)
DEFAULT_HIGHLIGHT_COLOR = $66B2FF; //Turuncu
var
GridHelperList: TObjectDictionary<TStringGrid, TStringGridHelperData>;
function MakeKeyConfig(AKey: Word; ACtrl: Boolean; AShift: Boolean; AAlt: Boolean): TKeyConfig;
begin
Result.Key := AKey;
Result.UseCtrl := ACtrl;
Result.UseShift := AShift;
Result.UseAlt := AAlt;
end;
{ TStringGridHelperData }
constructor TStringGridHelperData.Create(AGrid: TStringGrid);
begin
inherited Create;
FGrid := AGrid;
FObserver := TGridObserver.Create(nil);
FGrid.FreeNotification(FObserver);
FLastSearchText := '';
FLastSearchCol := -1;
FLastSearchRow := -1;
FCaseSensitive := False;
FSearching := False;
FCancelled := False;
FExporting := False;
FProgress := TProgressHelper.Create;
//Default tuþ atamalarý
FSearchKey := MakeKeyConfig(VK_F3, True); //Ctrl+F3
FExcelKey := MakeKeyConfig(VK_F8, True); //Ctrl+F8
FCSVKey := MakeKeyConfig(VK_F9, True); //Ctrl+F9
FPDFKey := MakeKeyConfig(VK_F10, True); //Ctrl+F10
FCancelKey := MakeKeyConfig(VK_ESCAPE, False); //ESC
FDeleteRowKey := MakeKeyConfig(VK_DELETE, True); //Ctrl+Delete
FAutoFitKey := MakeKeyConfig(VK_F1, True); //Ctrl+F1
//Mevcut event'leri saklýyoruz
FOriginalKeyDown := FGrid.OnKeyDown;
FOriginalDrawCell := FGrid.OnDrawCell;
//Highlight için default bilgiler
FHighlightCol := -1;
FHighlightRow := -1;
FIsHighlighted := False;
FSearchHighlightColor := DEFAULT_HIGHLIGHT_COLOR;
//Sýralama için default bilgiler
FSortColumn := -1;
FSortAscending := True;
//Baþlýklar dýþýnda header için default bilgiler
FHeaderCount := 0;
FOriginalFixedRows := FGrid.FixedRows;
end;
destructor TStringGridHelperData.Destroy;
var
i: Integer;
begin
for i := 0 to High(FCellStyles) do begin
SetLength(FCellStyles[i], 0);
end;
SetLength(FCellStyles, 0);
FObserver.Free;
FProgress.Free;
inherited;
end;
{ TStringGridHelper }
function TStringGridHelper.GetHelperData: TStringGridHelperData;
begin
if not Assigned(GridHelperList) then begin
GridHelperList := TObjectDictionary<TStringGrid, TStringGridHelperData>.Create([doOwnsValues]);
end;
if not GridHelperList.ContainsKey(Self) then begin
GridHelperList.Add(Self, TStringGridHelperData.Create(Self));
end;
Result := GridHelperList[Self];
end;
procedure TStringGridHelper.InitHelper;
var
Data: TStringGridHelperData;
begin
FreeHelper;
if Self = nil then Exit;
try
Data := GetHelperData;
if not Assigned(Self) then
raise Exception.Create('StringGrid nesnesi nil!');
Self.OnKeyDown := GridKeyDown;
Self.OnDrawCell := InternalDrawCell;
Self.OnSelectCell := GridSelectCell;
Self.OnMouseDown := GridMouseDown;
except
on E: Exception do
raise Exception.CreateFmt('InitHelper hatasý: %s', [E.Message]);
end;
end;
procedure TStringGridHelper.FreeHelper;
begin
try
if Assigned(GridHelperList) and GridHelperList.ContainsKey(Self) then begin
Self.OnKeyDown := GetHelperData.FOriginalKeyDown;
Self.OnDrawCell := GetHelperData.FOriginalDrawCell;
Self.OnSelectCell := GetHelperData.FOriginalSelectCell;
Self.OnMouseDown := GetHelperData.FOriginalMouseDown;
GridHelperList.Remove(Self);
end;
except
on E: Exception do begin
OutputDebugString(PChar('FreeHelper hatasý: ' + E.Message));
end;
end;
end;
function TStringGridHelper.GetCaseSensitive: Boolean;
begin
Result := GetHelperData.FCaseSensitive;
end;
procedure TStringGridHelper.SetCaseSensitive(const Value: Boolean);
begin
GetHelperData.FCaseSensitive := Value;
end;
function TStringGridHelper.GetIsSearching: Boolean;
begin
Result := GetHelperData.FSearching;
end;
function TStringGridHelper.GetIsExporting: Boolean;
begin
Result := GetHelperData.FExporting;
end;
procedure TStringGridHelper.ClearHighlight;
var
Data: TStringGridHelperData;
begin
Data := GetHelperData;
if Data.FIsHighlighted and (Data.FHighlightCol >= 0) and (Data.FHighlightRow >= 0) then begin
if (Data.FHighlightCol < Length(Data.FCellStyles)) and
(Data.FHighlightRow < Length(Data.FCellStyles[Data.FHighlightCol])) then begin
Data.FCellStyles[Data.FHighlightCol][Data.FHighlightRow].BackColor := Data.FOriginalHighlightColor;
TGridHack(Self).InvalidateCell(Data.FHighlightCol, Data.FHighlightRow);
end;
Data.FIsHighlighted := False;
Data.FHighlightCol := -1;
Data.FHighlightRow := -1;
end;
end;
procedure TStringGridHelper.HighlightCell(ACol, ARow: Integer);
var
Data: TStringGridHelperData;
begin
try
Data := GetHelperData;
if (ACol < 0) or (ACol >= Self.ColCount) then
raise Exception.CreateFmt('Geçersiz sütun: %d', [ACol]);
if (ARow < 0) or (ARow >= Self.RowCount) then
raise Exception.CreateFmt('Geçersiz satýr: %d', [ARow]);
ClearHighlight;
Data.FOriginalHighlightColor := GetCellStyle(ACol, ARow).BackColor;
Data.FHighlightCol := ACol;
Data.FHighlightRow := ARow;
Data.FIsHighlighted := True;
if (ACol < Length(Data.FCellStyles)) and
(ARow < Length(Data.FCellStyles[ACol])) and
(Data.FCellStyles[ACol][ARow].FontSize > 0) then begin
Data.FCellStyles[ACol][ARow].BackColor := Data.FSearchHighlightColor;
end
else begin
if ACol >= Length(Data.FCellStyles) then begin
SetLength(Data.FCellStyles, Self.ColCount);
end;
if ARow >= Length(Data.FCellStyles[ACol]) then begin
SetLength(Data.FCellStyles[ACol], Self.RowCount);
end;
Data.FCellStyles[ACol][ARow].BackColor := Data.FSearchHighlightColor;
Data.FCellStyles[ACol][ARow].FontColor := Self.Font.Color;
Data.FCellStyles[ACol][ARow].FontBold := fsBold in Self.Font.Style;
Data.FCellStyles[ACol][ARow].FontItalic := fsItalic in Self.Font.Style;
Data.FCellStyles[ACol][ARow].FontSize := Self.Font.Size;
end;
TGridHack(Self).InvalidateCell(ACol, ARow);
except
on E: Exception do
raise Exception.CreateFmt('HighlightCell hatasý [Col:%d, Row:%d], mesaj: %s', [ACol, ARow, E.Message]);
end;
end;
procedure TStringGridHelper.GridSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
var
Data: TStringGridHelperData;
begin
Data := GetHelperData;
if Assigned(Data.FOriginalSelectCell) then begin
Data.FOriginalSelectCell(Sender, ACol, ARow, CanSelect);
end;
if CanSelect and ((ACol <> Data.FHighlightCol) or (ARow <> Data.FHighlightRow)) then begin
ClearHighlight;
end;
end;
function TStringGridHelper.GetSearchHighlightColor: TColor;
begin
Result := GetHelperData.FSearchHighlightColor;
end;
procedure TStringGridHelper.SetSearchHighlightColor(const Value: TColor);
begin
GetHelperData.FSearchHighlightColor := Value;
end;
procedure TStringGridHelper.DeleteCurrentRow;
var
i: Integer;
CurrentRow, MinRow: Integer;
Data: TStringGridHelperData;
begin
try
Data := GetHelperData;
CurrentRow := Self.Row;
MinRow := Self.FixedRows;
if CurrentRow < MinRow then
raise Exception.Create('Sabit satýrlar silinemez!');
if Self.RowCount <= MinRow + 1 then
raise Exception.Create('Son veri satýrý silinemez!');
if MessageDlg('Seçili satýrý silmek istediðinizden emin misiniz?', mtConfirmation, [mbYes, mbNo], 0) <> mrYes then Exit;
ShiftStyles(CurrentRow, True);
for i := CurrentRow to Self.RowCount - 2 do begin
Self.Rows[i].Assign(Self.Rows[i + 1]);
end;
Self.RowCount := Self.RowCount - 1;
if CurrentRow >= Self.RowCount then begin
Self.Row := Self.RowCount - 1;
end;
Reset;
except
on E: Exception do
raise Exception.CreateFmt('Satýr silme hatasý [Row:%d], mesaj: %s', [Self.Row, E.Message]);
end;
end;
procedure TStringGridHelper.DeleteRow;
begin
if IsSearching then
raise Exception.Create('Arama devam ederken satýr silinemez!');
if IsExporting then
raise Exception.Create('Export devam ederken satýr silinemez!');
DeleteCurrentRow;
end;
procedure TStringGridHelper.PrepareStyleArray;
var
i: Integer;
Data: TStringGridHelperData;
begin
Data := GetHelperData;
SetLength(Data.FCellStyles, Self.ColCount);
for i := 0 to Self.ColCount - 1 do begin
SetLength(Data.FCellStyles[i], Self.RowCount);
end;
end;
function TStringGridHelper.CheckKeyMatch(Key: Word; Shift: TShiftState; Config: TKeyConfig): Boolean;
var
CtrlPressed, ShiftPressed, AltPressed: Boolean;
begin
CtrlPressed := ssCtrl in Shift;
ShiftPressed := ssShift in Shift;
AltPressed := ssAlt in Shift;
Result := (Key = Config.Key) and
(CtrlPressed = Config.UseCtrl) and
(ShiftPressed = Config.UseShift) and
(AltPressed = Config.UseAlt);
end;
procedure TStringGridHelper.ClearAllStyles;
var
i: Integer;
Data: TStringGridHelperData;
begin
Data := GetHelperData;
for i := 0 to High(Data.FCellStyles) do begin
SetLength(Data.FCellStyles[i], 0);
end;
SetLength(Data.FCellStyles, 0);
Self.Invalidate;
end;
procedure TStringGridHelper.SetSearchKey(AKey: Word; ACtrl: Boolean; AShift: Boolean; AAlt: Boolean);
var
Data: TStringGridHelperData;
begin
Data := GetHelperData;
Data.FSearchKey := MakeKeyConfig(AKey, ACtrl, AShift, AAlt);
end;
procedure TStringGridHelper.ShiftStyles(StartRow: Integer; Up: Boolean);
var
i, j: Integer;
Data: TStringGridHelperData;
begin
Data := GetHelperData;
if Length(Data.FCellStyles) = 0 then Exit;
if Up then begin
for i := StartRow to Self.RowCount - 2 do begin
for j := 0 to Self.ColCount - 1 do begin
if (j < Length(Data.FCellStyles)) and (Length(Data.FCellStyles[j]) > i + 1) then begin
Data.FCellStyles[j][i] := Data.FCellStyles[j][i + 1]
end
else if (j < Length(Data.FCellStyles)) and (Length(Data.FCellStyles[j]) > i) then begin
Data.FCellStyles[j][i].FontSize := 0;
end;
end;
end;
for j := 0 to High(Data.FCellStyles) do begin
if Length(Data.FCellStyles[j]) >= Self.RowCount then begin
SetLength(Data.FCellStyles[j], Self.RowCount - 1);
end;
end;
end
else begin
for j := 0 to High(Data.FCellStyles) do begin
SetLength(Data.FCellStyles[j], Self.RowCount);
end;
for i := Self.RowCount - 1 downto StartRow + 1 do begin
for j := 0 to Self.ColCount - 1 do begin
Data.FCellStyles[j][i] := Data.FCellStyles[j][i - 1];
end;
end;
end;
end;
procedure TStringGridHelper.GridMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
ACol, ARow: Integer;
Data: TStringGridHelperData;
HeaderRowIndex: Integer;
begin
Data := GetHelperData;
if Assigned(Data.FOriginalMouseDown) then begin
Data.FOriginalMouseDown(Sender, Button, Shift, X, Y);
end;
if (Button = mbLeft) and (ssCtrl in Shift) then begin
Self.MouseToCell(X, Y, ACol, ARow);
if IsHeaderRow(ARow) then Exit;
HeaderRowIndex := Data.FHeaderCount;
if (ARow >= HeaderRowIndex) and (ARow < Self.FixedRows) and (ACol >= Self.FixedCols) then begin
if Data.FSortColumn = ACol then begin
Data.FSortAscending := not Data.FSortAscending;
end
else begin
Data.FSortColumn := ACol;
Data.FSortAscending := True;
end;
SortByColumn(ACol, Data.FSortAscending);
end;
end;
end;
procedure TStringGridHelper.SetExcelKey(AKey: Word; ACtrl: Boolean; AShift: Boolean; AAlt: Boolean);
var
Data: TStringGridHelperData;
begin
Data := GetHelperData;
Data.FExcelKey := MakeKeyConfig(AKey, ACtrl, AShift, AAlt);
end;
procedure TStringGridHelper.SetPDFKey(AKey: Word; ACtrl: Boolean; AShift: Boolean; AAlt: Boolean);
var
Data: TStringGridHelperData;
begin
Data := GetHelperData;
Data.FPDFKey := MakeKeyConfig(AKey, ACtrl, AShift, AAlt);
end;
procedure TStringGridHelper.SetAutoFitKey(AKey: Word; ACtrl, AShift, AAlt: Boolean);
var
Data: TStringGridHelperData;
begin
Data := GetHelperData;
Data.FAutoFitKey := MakeKeyConfig(AKey, ACtrl, AShift, AAlt);
end;
procedure TStringGridHelper.SetCancelKey(AKey: Word; ACtrl: Boolean; AShift: Boolean; AAlt: Boolean);
var
Data: TStringGridHelperData;
begin
Data := GetHelperData;
Data.FCancelKey := MakeKeyConfig(AKey, ACtrl, AShift, AAlt);
end;
procedure TStringGridHelper.SetCellStyle(ACol, ARow: Integer; ABackColor, AFontColor: TColor; ABold, AItalic: Boolean; AFontSize: Integer);
var
Data: TStringGridHelperData;
begin
try
Data := GetHelperData;
if (ACol < 0) or (ACol >= Self.ColCount) then
raise Exception.CreateFmt('Geçersiz sütun indexi: %d (0-%d arasý olmalý)', [ACol, Self.ColCount - 1]);
if (ARow < 0) or (ARow >= Self.RowCount) then
raise Exception.CreateFmt('Geçersiz satýr indexi: %d (0-%d arasý olmalý)', [ARow, Self.RowCount - 1]);
if AFontSize <= 0 then
raise Exception.CreateFmt('Geçersiz font boyutu: %d (pozitif olmalý)', [AFontSize]);
if ACol >= Length(Data.FCellStyles) then begin
SetLength(Data.FCellStyles, Self.ColCount);
end;
if ARow >= Length(Data.FCellStyles[ACol]) then begin
SetLength(Data.FCellStyles[ACol], Self.RowCount);
end;
try
Data.FCellStyles[ACol][ARow].BackColor := ABackColor;
Data.FCellStyles[ACol][ARow].FontColor := AFontColor;
Data.FCellStyles[ACol][ARow].FontBold := ABold;
Data.FCellStyles[ACol][ARow].FontItalic := AItalic;
Data.FCellStyles[ACol][ARow].FontSize := AFontSize;
finally
TGridHack(Self).InvalidateCell(ACol, ARow);
end;
except
on E: Exception do
raise Exception.CreateFmt('SetCellStyle hatasý [Col:%d, Row:%d], mesaj: %s', [ACol, ARow, E.Message]);
end;
end;
procedure TStringGridHelper.SetCSVKey(AKey: Word; ACtrl, AShift, AAlt: Boolean);
var
Data: TStringGridHelperData;
begin
Data := GetHelperData;
Data.FCSVKey := MakeKeyConfig(AKey, ACtrl, AShift, AAlt);
end;
procedure TStringGridHelper.SetDeleteRowKey(AKey: Word; ACtrl, AShift, AAlt: Boolean);
var
Data: TStringGridHelperData;
begin
Data := GetHelperData;
Data.FDeleteRowKey := MakeKeyConfig(AKey, ACtrl, AShift, AAlt);
end;
function TStringGridHelper.CompareText(const S1, S2: string): Boolean;
begin
if CaseSensitive then begin
Result := Pos(S1, S2) > 0;
end
else begin
Result := Pos(AnsiUpperCase(S1), AnsiUpperCase(S2)) > 0;
end;
end;
procedure TStringGridHelper.SwapRows(const Data: TStringGridHelperData; Idx1, Idx2: Integer);
var
TempRow: TStringList;
TempStyles: array of TCellStyle;
k: Integer;
begin
if Idx1 = Idx2 then Exit;
TempRow := TStringList.Create;
try
TempRow.Assign(Self.Rows[Idx1]);
Self.Rows[Idx1].Assign(Self.Rows[Idx2]);
Self.Rows[Idx2].Assign(TempRow);
finally
TempRow.Free;
end;
if Length(Data.FCellStyles) > 0 then begin
SetLength(TempStyles, Self.ColCount);
for k := 0 to Self.ColCount - 1 do begin
if (k < Length(Data.FCellStyles)) and (Idx1 < Length(Data.FCellStyles[k])) then begin
TempStyles[k] := Data.FCellStyles[k][Idx1];
end;
end;
for k := 0 to Self.ColCount - 1 do begin
if (k < Length(Data.FCellStyles)) and (Idx2 < Length(Data.FCellStyles[k])) then begin
Data.FCellStyles[k][Idx1] := Data.FCellStyles[k][Idx2];
end;
end;
for k := 0 to Self.ColCount - 1 do begin
if (k < Length(Data.FCellStyles)) and (Idx2 < Length(Data.FCellStyles[k])) then begin
Data.FCellStyles[k][Idx2] := TempStyles[k];
end;
end;
end;
end;
procedure TStringGridHelper.SortByColumn(ACol: Integer; Ascending: Boolean);
var
Data: TStringGridHelperData;
procedure QuickSort(L, R: Integer);
var
I, J: Integer;
Pivot: string;
begin
I := L;
J := R;
Pivot := Self.Cells[ACol, (L + R) div 2];
repeat
if Ascending then begin
while TSmartComparer.Compare(Self.Cells[ACol, I], Pivot) < 0 do Inc(I);
while TSmartComparer.Compare(Self.Cells[ACol, J], Pivot) > 0 do Dec(J);
end
else begin
while TSmartComparer.Compare(Self.Cells[ACol, I], Pivot) > 0 do Inc(I);
while TSmartComparer.Compare(Self.Cells[ACol, J], Pivot) < 0 do Dec(J);
end;
if I <= J then begin
SwapRows(Data, I, J);
Inc(I);
Dec(J);
end;
until I > J;
if L < J then QuickSort(L, J);
if I < R then QuickSort(I, R);
end;
begin
Data := GetHelperData;
if (ACol < 0) or (ACol >= Self.ColCount) then Exit;
if Self.RowCount <= Self.FixedRows + 1 then Exit;
Screen.Cursor := crHourGlass;
try
QuickSort(Self.FixedRows, Self.RowCount - 1);
Self.Invalidate;
finally
Screen.Cursor := crDefault;
end;
end;
procedure TStringGridHelper.SortColumn(ACol: Integer; Ascending: Boolean);
var
Data: TStringGridHelperData;
begin
Data := GetHelperData;
Data.FSortColumn := ACol;
Data.FSortAscending := Ascending;
SortByColumn(ACol, Ascending);
end;
procedure TStringGridHelper.CloneRow(CurrentRow: Integer);
var
i: Integer;
NewRow: Integer;
Data: TStringGridHelperData;
begin
Data := GetHelperData;
if (CurrentRow < 0) or (CurrentRow >= Self.RowCount) then Exit;
Self.RowCount := Self.RowCount + 1;
NewRow := CurrentRow + 1;
ShiftStyles(CurrentRow, False);
for i := 0 to Self.ColCount - 1 do begin
Self.Cells[i, NewRow] := Self.Cells[i, CurrentRow];
if (i < Length(Data.FCellStyles)) and (CurrentRow < Length(Data.FCellStyles[i])) then begin
Data.FCellStyles[i][NewRow] := Data.FCellStyles[i][CurrentRow];
end;
end;
Self.Invalidate;
end;
function TStringGridHelper.GetCellStyle(ACol, ARow: Integer): TCellStyle;
var
Data: TStringGridHelperData;
begin
try
Data := GetHelperData;
if (ACol < 0) or (ACol >= Self.ColCount) then
raise Exception.CreateFmt('Geçersiz sütun indexi: %d', [ACol]);
if (ARow < 0) or (ARow >= Self.RowCount) then
raise Exception.CreateFmt('Geçersiz satýr indexi: %d', [ARow]);
if (ACol < Length(Data.FCellStyles))
and (ARow < Length(Data.FCellStyles[ACol]))
and (Data.FCellStyles[ACol][ARow].FontSize > 0) then begin
Result := Data.FCellStyles[ACol][ARow];
end
else begin
if (ACol < Self.FixedCols) or (ARow < Self.FixedRows) then begin
Result.BackColor := Self.FixedColor;
Result.FontColor := Self.Font.Color;
end
else begin
Result.BackColor := Self.Color;
Result.FontColor := Self.Font.Color;
end;
Result.FontBold := fsBold in Self.Font.Style;
Result.FontItalic := fsItalic in Self.Font.Style;
Result.FontSize := Self.Font.Size;
end;
except
on E: Exception do
begin
Result.BackColor := clWhite;
Result.FontColor := clBlack;
Result.FontBold := False;
Result.FontItalic := False;
Result.FontSize := 8;
OutputDebugString(PChar(Format('GetCellStyle hatasý [Col:%d, Row:%d], mesaj: %s', [ACol, ARow, E.Message])));
end;
end;
end;
function TStringGridHelper.GetNextVisibleCol(CurrentCol, Direction: Integer): Integer;
var
NewCol, CheckCount, MinCol: Integer;
begin
NewCol := CurrentCol;
CheckCount := 0;
MinCol := Self.FixedCols;
repeat
NewCol := NewCol + Direction;
if NewCol >= Self.ColCount then begin
NewCol := MinCol
end
else if NewCol < MinCol then begin
NewCol := Self.ColCount - 1;
end;
Inc(CheckCount);
if CheckCount > Self.ColCount then begin
Result := CurrentCol;
Exit;
end;
until Self.ColWidths[NewCol] >= 1;
Result := NewCol;
end;
function TStringGridHelper.GetNextVisibleRow(CurrentRow, Direction: Integer): Integer;
var
NewRow, CheckCount, MinRow: Integer;
begin
NewRow := CurrentRow;
CheckCount := 0;
MinRow := Self.FixedRows;
repeat
NewRow := NewRow + Direction;
if NewRow >= Self.RowCount then begin
NewRow := MinRow
end
else if NewRow < MinRow then begin
NewRow := Self.RowCount - 1;
end;
Inc(CheckCount);
if CheckCount > Self.RowCount then begin
Result := CurrentRow;
Exit;
end;
until Self.RowHeights[NewRow] >= 1;
Result := NewRow;
end;
procedure TStringGridHelper.GridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
Data: TStringGridHelperData;
begin
Data := GetHelperData;
HandleNavigationKeys(Key, Shift);
if Key = 0 then Exit;
if Assigned(Data.FOriginalKeyDown) then begin
Data.FOriginalKeyDown(Sender, Key, Shift);
end;
if CheckKeyMatch(Key, Shift, Data.FSearchKey) then begin
if not Data.FSearching then begin
Search;
end;
Key := 0;
end
else if CheckKeyMatch(Key, Shift, Data.FDeleteRowKey) then begin
if not Data.FExporting and not Data.FSearching then begin
DeleteRow;
end;
Key := 0;
end
else if CheckKeyMatch(Key, Shift, Data.FCancelKey) then begin
if Data.FSearching or Data.FExporting then begin
CancelSearch
end
else begin
Reset;
end;
Key := 0;
end
else if CheckKeyMatch(Key, Shift, Data.FAutoFitKey) then begin
if not Data.FExporting and not Data.FSearching then begin
AutoFit;
end;
Key := 0;
end
else if CheckKeyMatch(Key, Shift, Data.FExcelKey) then begin
if not Data.FExporting and not Data.FSearching then begin
ExportToExcel;
end;
Key := 0;
end
else if CheckKeyMatch(Key, Shift, Data.FPDFKey) then begin
if not Data.FExporting and not Data.FSearching then begin
ExportToPDF;
end;
Key := 0;
end
else if CheckKeyMatch(Key, Shift, Data.FCSVKey) then begin
if not Data.FExporting and not Data.FSearching then begin
ExportToCSV;
end;
Key := 0;
end
else if (Key = Ord('C')) and (ssCtrl in Shift) then begin
if (Row < FixedRows) or (Col < FixedCols) then Exit;
Clipboard.AsText := Cells[Col, Row];
Key := 0;
end;
end;
procedure TStringGridHelper.HandleNavigationKeys(var Key: Word; Shift: TShiftState);
var
NewCol, NewRow: Integer;
begin
if (ssCtrl in Shift) or (ssShift in Shift) or (ssAlt in Shift) then Exit;
case Key of
VK_LEFT:
begin
NewCol := GetNextVisibleCol(Self.Col, -1);
if NewCol <> Self.Col then begin
Self.Col := NewCol;
Key := 0;
end;
end;
VK_RIGHT:
begin
NewCol := GetNextVisibleCol(Self.Col, 1);
if NewCol <> Self.Col then begin
Self.Col := NewCol;
Key := 0;
end;
end;
VK_UP:
begin
NewRow := GetNextVisibleRow(Self.Row, -1);
if NewRow <> Self.Row then begin
Self.Row := NewRow;
Key := 0;
end;
end;
VK_DOWN:
begin
NewRow := GetNextVisibleRow(Self.Row, 1);
if NewRow <> Self.Row then begin
Self.Row := NewRow;
Key := 0;
end;
end;
end;
end;
procedure TStringGridHelper.InternalDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
Style: TCellStyle;
CellValue: string;
TextRect: TRect;
IntValue: Int64;
FloatValue, PercentageValue: Double;
DateValue, TimeValue: TDateTime;
CurrencyValue: Currency;
TextWidth, TextHeight: Integer;
TextX, TextY: Integer;
Alignment: TAlignment;
begin
Style := GetCellStyle(ACol, ARow);
Self.Canvas.Brush.Color := Style.BackColor;
Self.Canvas.FillRect(Rect);
Self.Canvas.Font.Color := Style.FontColor;
Self.Canvas.Font.Size := Style.FontSize;
if Style.FontBold then begin
Self.Canvas.Font.Style := Self.Canvas.Font.Style + [fsBold];
end
else begin
Self.Canvas.Font.Style := Self.Canvas.Font.Style - [fsBold];
end;
CellValue := Trim(Self.Cells[ACol, ARow]);