-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExtStringGrid.pas
1160 lines (1028 loc) · 26.7 KB
/
ExtStringGrid.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
{ Extension of string grid
Copyright (C) 2020 Red_prig
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
}
unit ExtStringGrid;
{$mode objfpc}{$H+}
interface
Uses
Classes,SysUtils,Controls,Graphics,Dialogs,LCLIntf,
StdCtrls,ComCtrls,Menus,
ExtCtrls,Clipbrd,
LCLType,kgrids,kcontrols;
type
TExtGridColumn=class(TKGridCol)
protected
FName,FCaption:RawByteString;
public
procedure Assign(Source:TPersistent); override;
property Name:RawByteString read FName;
property Caption:RawByteString read FCaption write FCaption;
end;
TExtStringGrid=class(TKCustomGrid)
private
PopupFields:TPopupMenu;
protected
procedure MouseDblClickCell(ACol,ARow:Integer); override;
procedure MouseUp(Button:TMouseButton;Shift:TShiftState;X,Y:Integer); override;
procedure OnClickPopupFields(Sender:TObject);
procedure KeyDown(var Key:Word;Shift:TShiftState); override;
procedure PasteCellRectFromClipboardText(ACol,ARow:LongInt);
function GetFieldValue(Const FName:String;aRow:Integer):RawByteString; virtual;
Procedure SetFieldValue(Const FName:String;aRow:Integer;Const FValue:RawByteString); virtual;
function MeasureCell(ACol,ARow:Integer;const ARect:TRect;AState:TKGridDrawState;Priority:TKGridMeasureCellPriority):TPoint; override;
public
constructor Create(AOwner:TComponent); override;
function AddColumn(Const FName,FCaption:String):TExtGridColumn;
function FindColumn(Const FName:String):Integer;
function GetColumnName(ACol:Integer):String;
procedure DoCopyToClipboard; virtual;
procedure DoPasteFromClipboard; virtual;
procedure DoCutToClipboard; virtual;
procedure DoInsert; virtual;
procedure DoDelete; virtual;
procedure CopyCellRectToClipboardText(const R:TKGridRect);
procedure PasteCellRectFromClipboard(ACol,ARow:LongInt);
procedure AutoSizeCol(ACol:Integer;FixedCells:Boolean); override;
property FieldValue[Const FName:String;aRow:Integer]:RawByteString read GetFieldValue write SetFieldValue;
end;
TOnDbUpdateRow=function(Const FName,FValue:RawByteString;ACol,ARow:Integer;AEditor:TWinControl):Boolean of object;
TOnDbSelectRow=procedure(FirstRow,LastRow:Integer) of object;
TOnDbDeleteRow=function(aRow:Integer):Boolean of object;
TDBStringGrid=class(TExtStringGrid)
protected
FRowInsId:Longint;
FRowInsIsNull:Boolean;
function DrawCell(ACol,ARow:Integer;ARect:TRect;AState:TKGridDrawState):Boolean; override;
function EditorCreate(ACol,ARow:Integer):TWinControl; override;
procedure EditorDataFromGrid(AEditor:TWinControl;ACol,ARow:Integer); override;
procedure EditorDataToGrid(AEditor:TWinControl;ACol,ARow:Integer); override;
Function CanDbSelect:Boolean; inline;
procedure Scroll(CodeHorz,CodeVert,DeltaHorz,DeltaVert:Integer;CallUpdateEditor:Boolean); override;
procedure ChangeBounds(ALeft,ATop,AWidth,AHeight:integer;KeepBase:boolean); override;
procedure ChangeDataSize(ColInsert:Boolean;ColAt,ColCnt:Integer;RowInsert:Boolean;RowAt,RowCnt:Integer); override;
procedure InternalSetSelection(NewSelection:TKGridRect;Flags:TKGridSelectionFlags); override;
public
FOnDbUpdate:TOnDbUpdateRow;
FOnDbSelect:TOnDbSelectRow;
FOnDbInsert:TOnDbUpdateRow;
FOnDbDelete:TOnDbDeleteRow;
Procedure ResetRowInsert; inline;
constructor Create(AOwner:TComponent); override;
procedure DoInsert; override;
procedure DoDelete; override;
end;
TKVirtualGridRow=class(TKGridRow)
procedure ValidateInitialPos; override;
procedure Assign(Source:TPersistent); override;
procedure SetVisible(Value:Boolean); override;
function GetVisible:Boolean; override;
procedure GridChanged; override;
end;
TKVirtualRows=class(TKGridAxisItems)
protected
FRowsV:array[0..1] of TKVirtualGridRow;
FRowCount:Integer;
function GetItem(Index:Integer):TKGridAxisItem; override;
procedure SetItem(Index:Integer;Value:TKGridAxisItem); override;
procedure Update(Item:TCollectionItem); override;
function _get_next_row:TKVirtualGridRow; inline;
function _get_row:TKVirtualGridRow;
public
constructor Create(_Grid:TKCustomGrid;AClass:TCollectionItemClass);
destructor Destroy; override;
function Count:Integer; override;
function Add:TKGridAxisItem; override;
function AddOnly:TKGridAxisItem; override;
procedure Clear; override;
procedure Delete(Index:Integer); override;
procedure DeleteOnly(Index:Integer); override;
procedure Exchange(Index1,Index2:Integer); override;
function Insert(At:Integer):TKGridAxisItem; override;
function InsertOnly(At:Integer):TKGridAxisItem; override;
end;
implementation
///////
procedure TKVirtualGridRow.ValidateInitialPos;
begin
///
end;
procedure TKVirtualGridRow.Assign(Source: TPersistent);
begin
//
end;
procedure TKVirtualGridRow.SetVisible(Value:Boolean);
begin
//
end;
function TKVirtualGridRow.GetVisible:Boolean;
begin
Result:=true;
end;
procedure TKVirtualGridRow.GridChanged;
begin
//////
end;
///////
function TKVirtualRows.Count:Integer;
begin
Result:=FRowCount;
end;
constructor TKVirtualRows.Create(_Grid:TKCustomGrid;AClass:TCollectionItemClass);
begin
FRowsV[0]:=TKVirtualGridRow.Create(self);
FRowsV[1]:=TKVirtualGridRow.Create(self);
AClass:=TKVirtualGridRow;
FRowCount:=_Grid.RowCount;
inherited;
end;
destructor TKVirtualRows.Destroy;
begin
FreeAndNil(FRowsV[0]);
FreeAndNil(FRowsV[1]);
inherited;
end;
function TKVirtualRows._get_next_row:TKVirtualGridRow; inline;
var
Tmp:TKVirtualGridRow;
begin
if PtrUint(FRowsV[0])>PtrUint(FRowsV[1]) then Result:=FRowsV[0] else Result:=FRowsV[1];
Tmp:=FRowsV[0];
FRowsV[0]:=FRowsV[1];
FRowsV[1]:=Tmp;
end;
function TKVirtualRows._get_row:TKVirtualGridRow;
begin
Result:=_get_next_row;
Result.Grid :=nil;
Result.InitialPos :=0;
Result.SortMode :=smNone;
Result.Tag :=nil;
Result.CanResize :=true;
Result.FBackExtent :=cDefaultRowHeightDef;
Result.Extent :=cDefaultRowHeightDef;
Result.MaxExtent :=0;
Result.MinExtent :=cDefaultRowHeightDef;
Result.SortArrowIndex:=0;
Result.Grid :=Grid;
end;
function TKVirtualRows.GetItem(Index:Integer):TKGridAxisItem;
Var
Row:TKVirtualGridRow;
begin
// Write(Index,' ');
Row:=_get_row;
Row.InitialPos:=Index;
Result:=Row;
end;
procedure TKVirtualRows.SetItem(Index:Integer;Value:TKGridAxisItem);
begin
end;
procedure TKVirtualRows.Update(Item:TCollectionItem);
begin
inherited;
end;
function TKVirtualRows.Add:TKGridAxisItem;
begin
Inc(FRowCount);
Result:=Grid.InsertRow(-1);
end;
function TKVirtualRows.AddOnly:TKGridAxisItem;
begin
Inc(FRowCount);
Result:=_get_row;
end;
procedure TKVirtualRows.Clear;
begin
Grid.DeleteRows(0,Grid.RowCount);
FRowCount:=Grid.RowCount;
end;
procedure TKVirtualRows.Delete(Index:Integer);
begin
Grid.DeleteRow(Index);
FRowCount:=Grid.RowCount;
end;
procedure TKVirtualRows.DeleteOnly(Index:Integer);
begin
if (Index>0) and (Index<FRowCount) then Dec(FRowCount);
end;
procedure TKVirtualRows.Exchange(Index1,Index2:Integer);
begin
//
end;
function TKVirtualRows.Insert(At:Integer):TKGridAxisItem;
begin
Inc(FRowCount);
Result:=Grid.InsertRow(At)
end;
function TKVirtualRows.InsertOnly(At:Integer):TKGridAxisItem;
begin
Inc(FRowCount);
Result:=_get_row;
end;
///////////
procedure TExtGridColumn.Assign(Source:TPersistent);
begin
if (Source is TExtGridColumn) then
begin
Grid :=TExtGridColumn(Source).Grid;
Extent :=TExtGridColumn(Source).Extent;
InitialPos :=TExtGridColumn(Source).InitialPos;
SortMode :=TExtGridColumn(Source).SortMode;
FName :=TExtGridColumn(Source).FName;
FCaption :=TExtGridColumn(Source).FCaption;
Tag :=TExtGridColumn(Source).Tag;
AssignPublished(TKGridAxisItem(Source));
end;
end;
//TExtStringGrid
procedure TExtStringGrid.MouseDblClickCell(ACol,ARow:Integer);
function NextSortMode(ASortMode: TKGridSortMode): TKGridSortMode;
begin
case SortStyle of
ssDownUp: if ASortMode = smDown then Result := smUp else Result := smDown;
ssDownUpNone:
case ASortMode of
smDown: Result := smUp;
smUp: Result := smNone;
else
Result := smDown;
end;
ssUpDown: if ASortMode = smUp then Result := smDown else Result := smUp;
else
case ASortMode of
smUp: Result := smDown;
smDown: Result := smNone;
else
Result := smUp;
end;
end;
end;
begin
if ((FGridState = gsColSortWaiting) or (FGridState = gsRowMoveWaiting)) and
(goColSorting in Options) and (ACol = FRows[ARow].SortArrowIndex) then
SortCols(ARow, NextSortMode(Rows[ARow].SortMode))
else if ((FGridState = gsRowSortWaiting) or (FGridState = gsColMoveWaiting)) and
(goRowSorting in Options) and (ARow = FCols[ACol].SortArrowIndex) then
SortRows(ACol, NextSortMode(Cols[ACol].SortMode));
inherited;
end;
procedure TExtStringGrid.MouseUp(Button:TMouseButton;Shift:TShiftState;X,Y:Integer);
Var
i,ACol,ARow:Integer;
Item:TMenuItem;
_Cell:TKGridCell;
begin
case FGridState of
gsColSortWaiting,
gsRowSortWaiting,
gsColMoveWaiting,
gsRowMoveWaiting:FGridState:=gsClickWaiting;
end;
inherited;
if Button=mbRight then
if MouseToCell(X,Y,ACol,ARow) then
if (ARow<FixedRows) and ColValid(ACol) then
begin
if Assigned(Columns) then
if Columns.Count>0 then
begin
PopupFields.Items.Clear;
For i:=0 to Columns.Count-1 do
begin
_Cell:=InternalGetCell(i,ARow);
if _Cell.InheritsFrom(TKGridTextCell) then
begin
Item:=TMenuItem.Create(PopupFields);
Item.Tag:=i;
Item.Caption:=TKGridTextCell(_Cell).Text;
Item.Checked:=Columns[i].Visible;
Item.OnClick:=@OnClickPopupFields;
PopupFields.Items.Add(Item);
end;
end;
PopupFields.PopUp;
end;
end;
end;
procedure TExtStringGrid.OnClickPopupFields(Sender:TObject);
begin
if Sender is TMenuItem then
if Assigned(Columns) then
if Columns.Count>0 then
With TMenuItem(Sender) do
if Columns.Count>Tag then
begin
Columns.Items[Tag].Visible:=not Columns.Items[Tag].Visible;
end;
end;
constructor TExtStringGrid.Create(AOwner:TComponent);
begin
inherited;
FixedCols:=0;
FixedRows:=0;
GridLineWidth:=1;
RowCount:=1;
ColCount:=1;
ColClass:=TExtGridColumn;
RealizeColClass;
FixedRows:=1;
Colors.CellLines:=clSilver;
Colors.FixedThemedCellLines:=cl3DDKShadow;
Colors.FixedCellLines:=cl3DDKShadow;
Options:=Options+[goAlignLastCol,goRowSorting,goColMoving,goColSizing]-[goIndicateHiddenCells,goMouseCanHideCells];
SortStyle:=ssDownUpNone;
//ScrollBars:=ssAutoBoth;
PopupFields:=TPopupMenu.Create(Self);
PopupFields.Parent:=Self;
end;
function TExtStringGrid.AddColumn(Const FName,FCaption:String):TExtGridColumn;
begin
Result:=nil;
if (Columns.Count=1) then
if (Columns[0] is TExtGridColumn) then
if TExtGridColumn(Columns[0]).FName='' then
begin
Result:=TExtGridColumn(Columns[0]);
end;
if (Result=nil) then Result:=TExtGridColumn(Columns.Add);
Result.MinExtent:=20;
Cells[Result.Index,0]:=FCaption;
if (Result is TExtGridColumn) then
begin
TExtGridColumn(Result).FName:=FName;
TExtGridColumn(Result).FCaption:=FCaption;
end;
end;
function TExtStringGrid.FindColumn(Const FName:String):Integer;
var
A:TKGridAxisItem;
Index:Integer;
begin
Result:=-1;
if (FName<>'') then
begin
Index:=FixedCols;
while (Index<ColCount) do
begin
A:=FCols[Index];
if (A<>nil) and (A is TExtGridColumn) and (TExtGridColumn(A).FName=FName) then
begin
Result:=Index;
Exit;
end else
Inc(Index);
end;
end;
end;
function TExtStringGrid.GetColumnName(ACol:Integer):String;
var
A:TKGridAxisItem;
begin
Result:='';
if ColValid(ACol) then
begin
A:=FCols[ACol];
if (A<>nil) and (A is TExtGridColumn) then
begin
Result:=TExtGridColumn(A).FName;
end;
end;
end;
function TExtStringGrid.GetFieldValue(Const FName:String;aRow:Integer):RawByteString;
var
aCol:Integer;
begin
Result:='';
aCol:=FindColumn(FName);
if (aCol<>-1) then
begin
Result:=Cells[aCol,aRow];
end;
end;
Procedure TExtStringGrid.SetFieldValue(Const FName:String;aRow:Integer;Const FValue:RawByteString);
var
aCol:Integer;
begin
aCol:=FindColumn(FName);
if (aCol<>-1) then
begin
Cells[aCol,aRow]:=FValue;
end;
end;
procedure TExtStringGrid.KeyDown(var Key:Word;Shift:TShiftState);
begin
inherited;
if (Shift=[ssModifier]) then
begin
case Key of
VK_C:DoCopyToClipboard;
VK_V:DoPasteFromClipboard;
VK_X:DoCutToClipboard;
VK_A:SelectAll;
end;
end else
if (Shift=[]) then
case Key of
VK_INSERT:DoInsert;
VK_DELETE:DoDelete;
end;
end;
procedure TExtStringGrid.DoCopyToClipboard;
begin
CopyCellRectToClipboardText(Selection);
end;
procedure TExtStringGrid.DoPasteFromClipboard;
begin
end;
procedure TExtStringGrid.DoCutToClipboard;
begin
end;
procedure TExtStringGrid.DoInsert;
begin
end;
procedure TExtStringGrid.DoDelete;
begin
end;
procedure TExtStringGrid.CopyCellRectToClipboardText(const R:TKGridRect);
var
Stream:TMemoryStream;
aRow,aCol:LongInt;
Rect:TKGridRect;
procedure _add_char(Ch:AnsiChar); inline;
begin
Stream.Write(Ch,1);
end;
procedure _add_Quoted(const S:RawByteString); inline;
Const
Quote='"';
var
i,j,count:LongInt;
begin
_add_char(Quote);
count:=length(s);
i:=0;
j:=0;
while (i<count) do
begin
i:=i+1;
if S[i]=Quote then
begin
Stream.Write(S[1+j],i-j);
_add_char(Quote);
j:=i;
end;
end;
if (i<>j) then Stream.Write(S[1+j],i-j);
_add_char(Quote);
end;
procedure AddText(const s:RawByteString); inline;
begin
if (pos(#9, s)>0) or
(pos(#10,s)>0) or
(pos(#13,s)>0)
then
_add_Quoted(s)
else
Stream.Write(PAnsiChar(s)^,Length(s));
end;
function _GetCells(ACol,ARow:LongInt):RawByteString; inline;
var
Data:TKGridCell;
begin
Result:='';
Data:=InternalGetCell(ACol,ARow);
if (Data<>nil) and (Data.InheritsFrom(TKGridTextCell)) then
Result:=TKGridTextCell(Data).Text;
end;
function Min(f,l:Integer):Integer; inline;
begin
if (f>l) then Result:=l else Result:=f;
end;
function Max(f,l:Integer):Integer; inline;
begin
if (f<l) then Result:=l else Result:=f;
end;
begin
if (not Assigned(FCells)) and (not (goVirtualGrid in Options)) then Exit;
Stream:=TMemoryStream.Create;
Rect.Row1:=Min(R.Row1,R.Row2);
Rect.Row2:=Max(R.Row1,R.Row2);
Rect.Col1:=Min(R.Col1,R.Col2);
Rect.Col2:=Max(R.Col1,R.Col2);
for aRow:=Rect.Row1 to Rect.Row2 do
begin
if RowValid(aRow) then
begin
if (aRow<>Rect.Row1) then _add_char(#13);
for aCol:=Rect.Col1 to Rect.Col2 do
if ColValid(aCol) then
begin
if (aCol<>Rect.Col1) then _add_char(#9);
AddText(_GetCells(aCol,aRow));
end;
end;
end;
_add_char(#0);
Clipboard.AddFormat(CF_TEXT,Stream);
FreeAndNil(Stream);
end;
procedure TExtStringGrid.PasteCellRectFromClipboard(ACol,ARow:LongInt);
begin
if Clipboard.HasFormat(CF_TEXT) then
begin
PasteCellRectFromClipboardText(ACol,ARow);
end;
end;
procedure TExtStringGrid.PasteCellRectFromClipboardText(ACol,ARow:LongInt);
var
Stream:TMemoryStream;
Data:RawByteString;
DataLen:SizeUint;
Ch:AnsiChar;
State:Byte;
_Col,_Row:LongInt;
Valid,TrueEscape:Boolean;
Procedure AddChar(Ch:AnsiChar);
begin
if not Valid then Exit;
if DataLen=Length(Data) then
begin
if Length(Data)<8 then SetLength(Data,8) else SetLength(Data,DataLen+(DataLen shr 1));
end;
Inc(DataLen);
Data[DataLen]:=Ch;
end;
Procedure NextCol;
begin
if Valid then
begin
InternalSetCells(_Col,_Row,Copy(Data,1,DataLen));
end;
Inc(_Col);
Valid:=ColValid(_Col) and RowValid(_Row);
DataLen:=0;
State:=0;
TrueEscape:=False;
end;
Procedure NextRow;
begin
if Valid then
begin
InternalSetCells(_Col,_Row,Copy(Data,1,DataLen));
end;
Inc(_Row);
_Col:=ACol;
Valid:=ColValid(_Col) and RowValid(_Row);
DataLen:=0;
State:=0;
TrueEscape:=False;
end;
Procedure FixEscape;
begin
if Valid and (not TrueEscape) then
begin
AddChar('"');
AddChar(#0);
Move(Data[1],Data[2],DataLen-1);
Data[1]:='"';
end;
end;
begin
if not Assigned(FCells) then Exit;
Valid:=ColValid(ACol) and RowValid(ARow);
if not Valid then Exit;
Stream:=TMemoryStream.Create;
if not Clipboard.GetFormat(CF_TEXT,Stream) then
begin
FreeAndNil(Stream);
Exit;
end;
Stream.Position:=0;
_Col:=ACol;
_Row:=ARow;
if (PChar(Stream.Memory)[Stream.Size-1]=#0) then
begin
Stream.Size:=Stream.Size-1; //zero char
end;
SetLength(Data,0);
DataLen:=0;
State:=0;
While (Stream.Read(Ch,1)=1) do
begin
Case State of
0:Case Ch of //initial
#9 :NextCol;
#13:State:=4;
#10:NextRow;
'"':State:=2;
else
begin
AddChar(Ch);
State:=1;
end;
end;
1:Case Ch of //simple string
#9 :NextCol;
#13:State:=4;
#10:NextRow;
else
AddChar(Ch);
end;
2:Case Ch of //escape string
#9,
#13,
#10:begin
AddChar(Ch);
TrueEscape:=True;
end;
'"':State:=3;
else
AddChar(Ch);
end;
3:Case Ch of //escape char
#9 :
begin
FixEscape;
NextCol;
end;
#13:
begin
FixEscape;
State:=4;
end;
#10:
begin
FixEscape;
NextRow;
end;
'"':begin
AddChar(Ch);
State:=2;
TrueEscape:=True;
end;
else
begin
AddChar(Ch);
State:=1;
end;
end;
4:Case Ch of //wait #10
#9 :begin
NextRow;
NextCol;
end;
#13:begin
NextRow;
NextRow;
end;
#10:NextRow;
'"':begin
NextCol;
State:=2;
end;
else
begin
NextRow;
AddChar(Ch);
State:=1;
end;
end;
end;
end;
Case State of
1..4:NextCol;
end;
FreeAndNil(Stream);
end;
procedure TExtStringGrid.AutoSizeCol(ACol:Integer;FixedCells:Boolean);
var
R: TRect;
Dummy, Extent, FirstRow, LastRow, I, MaxExtent: Integer;
Span: TKGridCellSpan;
GridFocused: Boolean;
function Max(a, b: Integer): Integer;inline;
begin
if a > b then
Result := a
else
Result := b;
end;
procedure calc(I:Integer);
begin
Span := InternalGetCellSpan(ACol, I);
if (Span.RowSpan > 0) and (Span.ColSpan > 0) then
begin
InternalGetHExtent(ACol, Span.ColSpan, R.Right, Dummy);
InternalGetVExtent(I, Span.RowSpan, R.Bottom, Dummy);
MaxExtent := Max(MaxExtent, MeasureCell(ACol, I, R, GetDrawState(ACol, I, GridFocused), mpColWidth).X - R.Right + Extent);
end;
end;
begin
if ColValid(ACol) then
begin
GridFocused := HasFocus;
R.Left := 0;
R.Top := 0;
MaxExtent := MinColWidth;
Extent := InternalGetColWidths(ACol);
if FixedCells and (FixedRows<>0) then
begin
for I:=0 to FixedRows-1 do calc(I);
end;
FirstRow:=TopRow;
LastRow:=LastVisibleRow-1;
if FirstRow<=LastRow then
for I:=FirstRow to LastRow do calc(I);
ColWidths[ACol]:=MaxExtent;
end;
end;
type
TMyGridCellPainter=class(TKGridCellPainter)
end;
function TExtStringGrid.MeasureCell(ACol,ARow:Integer;const ARect:TRect;AState:TKGridDrawState;Priority:TKGridMeasureCellPriority):TPoint;
var
_Cell:TKGridCell;
begin
CellPainter.Col := ACol;
CellPainter.Row := ARow;
CellPainter.State := AState;
CellPainter.CellPos := ARect.TopLeft;
CellPainter.Canvas := Canvas;
CellPainter.CellRect := ARect;
TMyGridCellPainter(CellPainter).FPrinting := False;
// prepare cell painter and measure cell data
TMyGridCellPainter(CellPainter).BeginDraw;
try
Result.X := ARect.Right - ARect.Left;
Result.Y := ARect.Bottom - ARect.Top;
if Assigned(OnMeasureCell) then
OnMeasureCell(Self, ACol, ARow, ARect, AState, Priority, Result)
else
begin
_Cell:=InternalGetCell(ACol,ARow);
if Assigned(_Cell) then _Cell.ApplyDrawProperties;
Result:=CellPainter.DefaultMeasure(Priority);
end;
finally
TMyGridCellPainter(CellPainter).EndDraw;
end;
end;
//TDBStringGrid
constructor TDBStringGrid.Create(AOwner:TComponent);
begin
inherited;
FRowInsId:=-1;
FRowInsIsNull:=True;
end;
procedure TDBStringGrid.DoInsert;
begin
if Enabled and (not EditorMode) and Assigned(FOnDbInsert) then
if (FRowInsId=-1) then
begin
FRowInsIsNull:=True;
FRowInsId:=RowCount;
InsertRow(RowCount);
if (goRowSelect in Options) then
begin
SelectRow(FRowInsId);
end else
begin
Selection:=GridRect(Col, FRowInsId, Col, FRowInsId);
end;
end;
end;
procedure TDBStringGrid.DoDelete;
begin
if Enabled and (not EditorMode) then
if (Row>=FixedRows) and (Row=FRowInsId) then
begin
if FRowInsIsNull then
begin
FRowInsId:=-1;
DeleteRow(Row);
end;
end else
if Assigned(FOnDbDelete) then
begin
if FOnDbDelete(Row) then
DeleteRow(Row);
end;
end;
procedure TDBStringGrid.InternalSetSelection(NewSelection:TKGridRect;Flags:TKGridSelectionFlags);
begin
inherited;
if Enabled and (not EditorMode) then
begin
if (FRowInsId<>-1) and (FRowInsId<>Row) and FRowInsIsNull then
begin
DeleteRow(FRowInsId);
FRowInsId:=-1;
end;
end;
end;
Procedure TDBStringGrid.ResetRowInsert; inline;
begin
FRowInsId:=-1;
end;
type
_TKGridCell=class(TKGridCell)
end;
function TDBStringGrid.DrawCell(ACol,ARow:Integer;ARect:TRect;AState:TKGridDrawState):Boolean;
var
_Cell:TKGridCell;
begin
_Cell:=InternalGetCell(ACol,ARow);
Result:=Assigned(_Cell);
if Result then
begin
_Cell.ApplyDrawProperties;
_TKGridCell(_Cell).DrawCell(ACol,ARow,ARect,AState);
end;
end;
function TDBStringGrid.EditorCreate(ACol,ARow:Integer):TWinControl;
var
_Cell:TKGridCell;
begin
Result:=inherited;
if Assigned(Result) then
begin
_Cell:=InternalGetCell(ACol,ARow);
if Assigned(_Cell) then
begin
if FEditedCell=nil then FEditedCell:=TKGridCellClass(_Cell.ClassType).Create(nil);
FEditedCell.Assign(_Cell);
end;
end;
end;
procedure TDBStringGrid.EditorDataFromGrid(AEditor:TWinControl;ACol,ARow:Integer);
var
_Cell:TKGridCell;
AssignText:Boolean;
begin
AssignText:=True;
_Cell:=InternalGetCell(ACol,ARow);
if Assigned(_Cell) then