-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathNiceGrid.pas
4806 lines (4088 loc) · 120 KB
/
NiceGrid.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
{-------------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is NiceGrid.pas released at April 11st, 2003.
The Original Code is a part of NiceGrid component.
The Initial Developer of the Original Code is Priyatna.
(Website: http://www.priyatna.org/ Email: [email protected])
All Rights Reserved.
Contributors:
- C. S. Phua <[email protected]>
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL and not to allow others to use your version of this file
under the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.
-------------------------------------------------------------------------------}
unit NiceGrid;
interface
uses
Windows, Forms, Controls, Messages, SysUtils, Classes, Graphics, Contnrs,
StdCtrls, ExtCtrls, Clipbrd;
type
PHeaderInfo = ^THeaderInfo;
THeaderInfo = record
Str: string;
Rc: TRect;
end;
THorzAlign = (haLeft, haCenter, haRight);
TVertAlign = (vaTop, vaCenter, vaBottom);
TGutterKind = (gkNone, gkBlank, gkPointer, gkNumber, gkString);
TGridHittest = (gtNone, gtLeftTop, gtLeft, gtTop, gtCell, gtColSizing, gtSmallBox);
TNiceGridOption = (ngoMultiCellSelect, ngoThemed, ngoExcel);
TNiceGridOptions = set of TNiceGridOption;
TNiceGridInplaceEditorType = (ngetEdit, ngetEditInteger, ngetEditFloat, ngetCombo, ngetComboNoEditText);
TNiceGrid = class;
TOnFormatText = procedure (Grid: TNiceGrid; X,Y: Integer; var CellText: string) of object;
TNiceColumn = class(TCollectionItem)
private
FTitle: string;
FFooter: string;
FWidth: Integer;
FFont: TFont;
FColor: TColor;
FHorzAlign: THorzAlign;
FVertAlign: TVertAlign;
FVisible: Boolean;
FStrings: TStrings;
FTag: Integer;
FTag2: Integer;
FCanResize: Boolean;
FHint: string;
FReadOnly: Boolean;
FEditorType: TNiceGridInplaceEditorType;
FOnFormatText: TOnFormatText;
function GetGrid: TNiceGrid;
function IsFontStored: Boolean;
procedure FontChange(Sender: TObject);
procedure SetTitle(Value: string);
procedure SetWidth(Value: Integer);
procedure SetFont(Value: TFont);
procedure SetColor(Value: TColor);
procedure SetHorzAlign(Value: THorzAlign);
procedure SetVertAlign(Value: TVertAlign);
procedure SetVisible(Value: Boolean);
procedure SetStrings(Value: TStrings);
procedure SetFooter(const Value: string);
protected
function GetDisplayName: string; override;
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
published
property Grid: TNiceGrid read GetGrid;
property Title: string read FTitle write SetTitle;
property Footer: string read FFooter write SetFooter;
property Width: Integer read FWidth write SetWidth;
property Font: TFont read FFont write SetFont stored IsFontStored;
property Color: TColor read FColor write SetColor default clWindow;
property HorzAlign: THorzAlign read FHorzAlign write SetHorzAlign default haLeft;
property VertAlign: TVertAlign read FVertAlign write SetVertAlign default vaCenter;
property Visible: Boolean read FVisible write SetVisible default True;
property Tag: Integer read FTag write FTag default 0;
property Tag2: Integer read FTag2 write FTag2 default 0;
property Hint: string read FHint write FHint;
property Strings: TStrings read FStrings write SetStrings;
property CanResize: Boolean read FCanResize write FCanResize default True;
property ReadOnly: Boolean read FReadOnly write FReadOnly default False;
property EditorType: TNiceGridInplaceEditorType read FEditorType write FEditorType;
property OnFormatText: TOnFormatText read FOnFormatText write FOnFormatText;
end;
TNiceColumns = class(TCollection)
private
FGrid: TNiceGrid;
function GetItem(Index: Integer): TNiceColumn;
procedure SetItem(Index: Integer; Value: TNiceColumn);
protected
function GetOwner: TPersistent; override;
procedure Update(Item: TCollectionItem); override;
public
constructor Create(AGrid: TNiceGrid);
property Grid: TNiceGrid read FGrid;
property Items[Index: Integer]: TNiceColumn read GetItem write SetItem; default;
function Add: TNiceColumn;
function AddItem(Item: TNiceColumn; Index: Integer): TNiceColumn;
function Insert(Index: Integer): TNiceColumn;
end;
INiceGridInplaceEditor = interface
['{95DCBCC1-B941-42F9-8CFE-CEE9BA16BB84}']
procedure ShowEdit(X, Y: Integer);
procedure HideEdit;
end;
TNiceInplace = class;
TNiceInplaceEdit = class(TEdit, INiceGridInplaceEditor)
private
m_Mediator: TNiceInplace;
m_Alignment: THorzAlign;
m_nCellX, m_nCellY: Integer;
m_EditorType: TNiceGridInplaceEditorType;
m_bMuteSetOnChange: Boolean;
procedure SetAlignment(Value: THorzAlign);
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure Change; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
procedure WMKillFocus(var Msg: TWMKillFocus); message WM_KILLFOCUS;
procedure CMWantSpecialKey(var Message: TWMKey); message CM_WANTSPECIALKEY;
public
constructor Create(AMediator: TNiceInplace); reintroduce;
procedure ShowEdit(X, Y: Integer);
procedure HideEdit;
end;
TNiceInplaceCombo = class(TComboBox, INiceGridInplaceEditor)
private
m_Mediator: TNiceInplace;
m_CellX, m_CellY: Integer;
protected
procedure Change; override;
procedure KeyPress(var Key: Char); override;
procedure CMWantSpecialKey(var Message: TWMKey); message CM_WANTSPECIALKEY;
procedure WMKillFocus(var Msg: TWMKillFocus); message WM_KILLFOCUS;
procedure WMSetFocus(var Msg: TWMSetFocus); message WM_SETFOCUS;
public
constructor Create(AMediator: TNiceInplace); reintroduce;
procedure ShowEdit(X, Y: Integer);
procedure HideEdit;
property Mediator: TNiceInplace read m_Mediator;
end;
TNiceInplace = class
private
m_Grid: TNiceGrid;
m_Control: TWinControl;
function GetHandle: THandle;
procedure Clear;
public
constructor Create(AGrid: TNiceGrid); reintroduce;
procedure ShowEdit(X, Y: Integer);
procedure HideEdit;
property Grid: TNiceGrid read m_Grid;
property Handle: THandle read GetHandle;
property Control: TWinControl read m_Control;
end;
TMergeCell = class(TObject)
public
Caption: string;
Rc: TRect;
Color: TColor;
Font: TFont;
HorzAlign: THorzAlign;
VertAlign: TVertAlign;
constructor Create;
destructor Destroy; override;
end;
TOnDrawCellEvent = procedure (Sender: TObject; ACanvas: TCanvas; X, Y: Integer;
Rc: TRect; var Handled: Boolean) of object;
TOnDrawHeaderEvent = procedure (Sender: TObject; ACanvas: TCanvas; Rc: TRect;
Str: string; var Handled: Boolean) of object;
TOnFilterChar = procedure (Sender: TObject; Col: Integer; Row: Integer;
Chr: Char; var Allowed: Boolean) of object;
TOnHeaderClick = procedure (Sender: TObject; Col: Integer;
Button: TMouseButton; Shift: TShiftState) of object;
TOnGutterClick = procedure (Sender: TObject; Row: Integer;
Button: TMouseButton; Shift: TShiftState) of object;
TOnCellAssignment = procedure (Sender: TObject; Col, Row: Integer;
var Str: string) of object;
TOnCellChange = procedure (Sender: TObject; Col, Row: Integer; var Str: string)
of object;
TOnCellChanging = procedure (Sender: TObject; Col, Row: Integer;
var CanChange: Boolean) of object;
TOnRowEvent = procedure (Sender: TObject; ARow: Integer) of object;
TOnColRowChanged = procedure (Sender: TObject; Col, Row: Integer) of object;
TOnEditorCreated = procedure (Grid: TNiceGrid; EditorControl: TWinControl) of object;
TOnGetCellColor = function (Grid: TNiceGrid; Col, Row: Integer; var Handled: boolean): TColor of object;
TNiceGridRowsByIndexEnumerator = record
m_NiceGrid: TNiceGrid;
m_nRowIndex: Integer;
constructor Create(ANiceGrid: TNiceGrid);
function GetEnumerator: TNiceGridRowsByIndexEnumerator;
function MoveNext(): Boolean;
function GetCurrent(): Integer;
property Current: Integer read GetCurrent;
end;
TNiceGrid = class(TCustomPanel)
private
ForcedColumn: Integer;
FixedWidth, FixedHeight: Integer;
BodyWidth, BodyHeight: Integer;
AllWidth, AllHeight: Integer;
FooterTop: Integer;
CellBox: TRect;
FHorzOffset: Integer;
FVertOffset: Integer;
FMaxHScroll: Integer;
FMaxVScroll: Integer;
FSmallChange: Integer;
FLargeChange: Integer;
FAutoAddRow: Boolean;
FRowCount: Integer;
FDefRowHeight: Integer;
FDefColWidth: Integer;
FFlat: Boolean;
FHeaderLine: Integer;
FHeaderInfos: TList;
FUpdating: Boolean;
FColor: TColor;
FAlternateColor: TColor;
FGridColor: TColor;
FShowGrid: Boolean;
FHeaderColor: TColor;
FHeaderLightColor: TColor;
FHeaderDarkColor: TColor;
FSelectionColor: TColor;
FHeaderFont: TFont;
FGutterFont: TFont;
FGutterKind: TGutterKind;
FGutterWidth: Integer;
FFitToWidth: Boolean;
FAutoColWidth: Boolean;
FReadOnly: Boolean;
FColumns: TNiceColumns;
ValidationEnabled: Boolean;
FEdit: TNiceInplace;
FCol: Integer;
FRow: Integer;
FCol2, FRow2: Integer; // Selection
FColEdit, FRowEdit: Integer; // Selection
FSelectArea: TRect;
SmallBox: TRect;
SmallBoxArea: TRect;
SmallBoxPos: Byte;
BuffString: string;
IsEditing: Boolean;
SizingCol: Integer;
SizingColX: Integer;
LastHover: Integer;
//Sync: TNiceGridSync;
Mergeds: TList;
FOnDrawCell: TOnDrawCellEvent;
FOnDrawHeader: TOnDrawHeaderEvent;
FOnDrawGutter: TOnDrawHeaderEvent;
FOnDrawFooter: TOnDrawHeaderEvent;
FOnFilterChar: TOnFilterChar;
FOnHeaderClick: TOnHeaderClick;
FOnGutterClick: TOnGutterClick;
FOnCellChange: TOnCellChange;
FOnCellChanging: TOnCellChanging;
FOnColRowChanged: TOnColRowChanged;
FOnInsertRow: TOnRowEvent;
FOnDeleteRow: TOnRowEvent;
FOnCellAssignment: TOnCellAssignment;
FOnEditorCreated: TOnEditorCreated;
FOnFormatText: TOnFormatText;
FOnEditorCreating: TNotifyEvent;
FOnDrawBackground: TOnDrawCellEvent;
FOnGetCellColor: TOnGetCellColor;
FGutterStrings: TStrings;
FShowFooter: Boolean;
FFooterFont: TFont;
FEnabled: Boolean;
FAutoFillRight: Boolean;
FAutoFillDown: Boolean;
FOptions: TNiceGridOptions;
procedure WMUnknown(var Msg: TMessage); message WM_USER + $B902;
procedure WMVScroll(var Msg: TWMVScroll); message WM_VSCROLL;
procedure WMHScroll(var Msg: TWMHScroll); message WM_HSCROLL;
procedure WMMouseWheel(var Msg: TWMMouseWheel); message WM_MOUSEWHEEL;
procedure WMSize(var Msg: TMessage); message WM_SIZE;
procedure WMEraseBkgnd(var Msg: TWMEraseBkGnd); message WM_ERASEBKGND;
procedure WMSetFocus(var Msg: TWMSetFocus); message WM_SETFOCUS;
procedure WMKillFocus(var Msg: TWMKillFocus); message WM_KILLFOCUS;
procedure CMWantSpecialKey(var Message: TWMKey); message CM_WANTSPECIALKEY;
procedure CMFontChanged(var Msg: TMessage); message CM_FONTCHANGED;
function TotalWidth: Integer;
procedure ClearHeaderInfos;
procedure ClearUnused;
procedure RenderGutter;
procedure RenderHeader;
procedure DrawSelection;
procedure SetHorzOffset(Value: Integer);
procedure SetVertOffset(Value: Integer);
function GetColCount: Integer;
procedure SetColCount(Value: Integer);
procedure SetRowCount(Value: Integer);
procedure SetDefColWidth(Value: Integer);
procedure SetDefRowHeight(Value: Integer);
procedure SetFlat(Value: Boolean);
procedure SetColor(Value: TColor);
procedure SetAlternateColor(Value: TColor);
procedure SetGridColor(Value: TColor);
procedure SetShowGrid(Value: Boolean);
procedure SetHeaderLine(Value: Integer);
procedure SetHeaderColor(Value: TColor);
procedure SetHeaderLightColor(Value: TColor);
procedure SetHeaderDarkColor(Value: TColor);
procedure SetHeaderFont(Value: TFont);
procedure SetSelectionColor(Value: TColor);
procedure SetFitToWidth(Value: Boolean);
procedure SetAutoColWidth(Value: Boolean);
procedure SetReadOnly(Value: Boolean);
procedure InternalSetCell(X, Y: Integer; Value: string; FireOnChange: Boolean);
procedure SetCell(X, Y: Integer; Value: string);
function GetColWidths(Index: Integer): Integer;
procedure SetColWidths(Index: Integer; Value: Integer);
procedure SetColumns(Value: TNiceColumns);
procedure SetCol(Value: Integer);
procedure SetRow(Value: Integer);
procedure AdjustSelection(Value: TRect; Force: Boolean);
procedure SetSelectArea(Value: TRect);
procedure SetGutterKind(Value: TGutterKind);
procedure SetGutterWidth(Value: Integer);
procedure SetGutterFont(Value: TFont);
procedure HeaderFontChange(Sender: TObject);
procedure GutterFontChange(Sender: TObject);
function CreateColumn: TNiceColumn;
procedure UpdateColumn(Index: Integer);
procedure UpdateColumns;
procedure UpdateHeader;
function GetCellRect(x, y: Integer): TRect;
function CellRectToClient(R: TRect): TRect;
function GetCellAtPos(X, Y: Integer): TPoint;
function GetColFromX(X: Integer): Integer;
function GetRowFromY(Y: Integer): Integer;
function GetColCoord(I: Integer): Integer;
function GetCell(X, Y: Integer): string;
function SafeGetCell(X, Y: Integer): string;
function GetCellColor(X, Y: Integer): TColor;
procedure DrawText(X, Y: Integer);
function FastDrawText(X, Y: Integer; IsEditing: Boolean): TPoint;
procedure NormalizeVertOffset;
procedure InvalidateCells;
procedure InvalidateRightWard(Left: Integer);
procedure InvalidateDownWard(Top: Integer);
procedure InvalidateHeader;
procedure InvalidateGutter;
function GetFirstVisible: Integer;
function GetLastVisible: Integer;
function GetNextVisible(Index: Integer): Integer;
function GetPrevVisible(Index: Integer): Integer;
procedure ColRowChanged;
procedure SetGutterStrings(const Value: TStrings);
function GetObject(X, Y: Integer): TObject;
procedure SetObject(X, Y: Integer; const Value: TObject);
procedure BuildMergeData;
procedure DrawMergedCell(Index: Integer);
procedure SetShowFooter(const Value: Boolean);
procedure RenderFooter;
procedure SetFooterFont(const Value: TFont);
procedure FooterFontChange(Sender: TObject);
procedure DrawFixCell(Rc: TRect; Str: string; AFont: TFont; AEvent: TOnDrawHeaderEvent);
procedure SetEnabled(const Value: Boolean); reintroduce;
procedure DrawBackground(X, Y: Integer);
procedure SetOptions(const Value: TNiceGridOptions);
function IsThemed: Boolean;
protected
function GetMergedCellsData: TList;
function GetHeaderInfo: TList;
procedure SetScrollBar(AKind, AMax, APos, AMask: Integer); virtual;
procedure ShowHideScrollBar(HorzVisible, VertVisible: Boolean); virtual;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure Recalculate; virtual;
procedure CreateWnd; override;
procedure CreateParams(var Params: TCreateParams); override;
procedure Paint; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure BeginUpdate;
procedure EndUpdate;
procedure Clear;
property Cells[X, Y: Integer]: string read GetCell write SetCell; default;
property Objects[X, Y: Integer]: TObject read GetObject write SetObject;
property ColWidths[Index: Integer]: Integer read GetColWidths write SetColWidths;
procedure EnsureVisible(X, Y: Integer); overload;
procedure CutToClipboard;
procedure CopyToClipboard;
procedure PasteFromClipboard;
function GetHitTestInfo(X, Y: Integer): TGridHitTest;
function HeaderCellsCount: Integer;
function HeaderCells(I: Integer): THeaderInfo;
property Col: Integer read FCol write SetCol;
property Row: Integer read FRow write SetRow;
property SelectArea: TRect read FSelectArea write SetSelectArea;
procedure DeleteRow(ARow: Integer);
procedure InsertRow(ARow: Integer);
function AddRow: Integer;
property HorzOffset: Integer read FHorzOffset write SetHorzOffset;
property VertOffset: Integer read FVertOffset write SetVertOffset;
function MergeCells(const X1, Y1, X2, Y2: Integer; ACaption: string): TMergeCell;
procedure ClearMergeCells;
procedure TryEdit;
procedure EndEdit;
function RowsByIndex: TNiceGridRowsByIndexEnumerator;
function IsEmptyRow(y: Integer): Boolean;
published
property Enabled: Boolean read FEnabled write SetEnabled default True;
property ColCount: Integer read GetColCount write SetColCount;
property RowCount: Integer read FRowCount write SetRowCount default 5;
property AutoAddRow: Boolean read FAutoAddRow write FAutoAddRow default False;
property AutoFillDown: Boolean read FAutoFillDown write FAutoFillDown default False;
property AutoFillRight: Boolean read FAutoFillRight write FAutoFillRight default False;
property DefRowHeight: Integer read FDefRowHeight write SetDefRowHeight default 18;
property DefColWidth: Integer read FDefColWidth write SetDefColWidth default 80;
property Flat: Boolean read FFlat write SetFlat default True;
property Color: TColor read FColor write SetColor default clWindow;
property AlternateColor: TColor read FAlternateColor write SetAlternateColor default clWindow;
property GridColor: TColor read FGridColor write SetGridColor default clBtnFace;
property ShowGrid: Boolean read FShowGrid write SetShowGrid default True;
property HeaderLine: Integer read FHeaderLine write SetHeaderLine default 1;
property HeaderColor: TColor read FHeaderColor write SetHeaderColor default clBtnFace;
property HeaderLightColor: TColor read FHeaderLightColor write SetHeaderLightColor default clBtnHighlight;
property HeaderDarkColor: TColor read FHeaderDarkColor write SetHeaderDarkColor default clBtnShadow;
property HeaderFont: TFont read FHeaderFont write SetHeaderFont;
property FooterFont: TFont read FFooterFont write SetFooterFont;
property SelectionColor: TColor read FSelectionColor write SetSelectionColor default $00CAFFFF;
property FitToWidth: Boolean read FFitToWidth write SetFitToWidth default False;
property AutoColWidth: Boolean read FAutoColWidth write SetAutoColWidth default False;
property ReadOnly: Boolean read FReadOnly write SetReadOnly default False;
property Columns: TNiceColumns read FColumns write SetColumns;
property GutterKind: TGutterKind read FGutterKind write SetGutterKind default gkBlank;
property GutterWidth: Integer read FGutterWidth write SetGutterWidth default 20;
property GutterFont: TFont read FGutterFont write SetGutterFont;
property GutterStrings: TStrings read FGutterStrings write SetGutterStrings;
property ShowFooter: Boolean read FShowFooter write SetShowFooter;
property OnDrawCell: TOnDrawCellEvent read FOnDrawCell write FOnDrawCell;
property OnDrawHeader: TOnDrawHeaderEvent read FOnDrawHeader write FOnDrawHeader;
property OnDrawGutter: TOnDrawHeaderEvent read FOnDrawGutter write FOnDrawGutter;
property OnDrawFooter: TOnDrawHeaderEvent read FOnDrawFooter write FOnDrawFooter;
property OnFilterChar: TOnFilterChar read FOnFilterChar write FOnFilterChar;
property OnHeaderClick: TOnHeaderClick read FOnHeaderClick write FOnHeaderClick;
property OnGutterClick: TOnGutterClick read FOnGutterClick write FOnGutterClick;
property OnCellChange: TOnCellChange read FOnCellChange write FOnCellChange;
property OnCellChanging: TOnCellChanging read FOnCellChanging write FOnCellChanging;
property OnColRowChanged: TOnColRowChanged read FOnColRowChanged write FOnColRowChanged;
property OnInsertRow: TOnRowEvent read FOnInsertRow write FOnInsertRow;
property OnDeleteRow: TOnRowEvent read FOnDeleteRow write FOnDeleteRow;
property OnCellAssignment: TOnCellAssignment read FOnCellAssignment write FOnCellAssignment;
property OnEditorCreated: TOnEditorCreated read FOnEditorCreated write FOnEditorCreated;
property OnFormatText: TOnFormatText read FOnFormatText write FOnFormatText;
property OnEditorCreating: TNotifyEvent read FOnEditorCreating write FOnEditorCreating;
property OnDrawBackground: TOnDrawCellEvent read FOnDrawBackground write FOnDrawBackground;
property OnGetCellColor: TOnGetCellColor read FOnGetCellColor write FOnGetCellColor;
property Options: TNiceGridOptions read FOptions write SetOptions;
property Font;
property Anchors;
property Align;
property BevelKind;
property BorderStyle default bsSingle;
property BevelOuter default bvNone;
property BevelInner;
property TabOrder;
property TabStop default True;
property Tag;
property OnClick;
property OnDblClick;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnKeyPress;
property OnKeyDown;
property OnKeyUp;
property PopupMenu;
end;
function DrawString(Canvas: TCanvas; Str: string; Rc: TRect;
HorzAlign: THorzAlign; VertAlign: TVertAlign; IsEditing: Boolean): TPoint;
procedure DrawStringMulti(Canvas: TCanvas; Str: string; Rc: TRect;
HorzAlign: THorzAlign; VertAlign: TVertAlign);
implementation
{$R NiceCursors.res}
uses
Math, Themes, UxTheme;
const
crPlus = 101;
crSmallCross = 102;
crRight = 103;
crDown = 104;
crLeftTop = 105;
CursorArray: array [TGridHitTest] of TCursor =
//(gtNone, gtLeftTop, gtLeft, gtTop, gtCell, gtColSizing, gtSmallBox);
(crDefault, crLeftTop, crRight, crDown, crPlus, crHSplit, crSmallCross);
MergeID = -2;
function GetWin7Themed: Boolean;
{-----------------------------------------------------------------------------
Procedure: GetWin7Themed
Author: nbi
Date: 19-May-2012
Arguments: None
Result: Boolean
-----------------------------------------------------------------------------}
begin
Result := (Win32MajorVersion>=6) and (ThemeServices.ThemesEnabled);
end;
{ TNiceGrid }
constructor TNiceGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Width := 200;
Height := 200;
inherited Color := clWindow;
BevelOuter := bvNone;
BorderStyle := bsSingle;
TabStop := True;
TabOrder := 0;
ParentColor := False;
ParentBackground := False;
ParentFont := False;
{$IFDEF VER150}
ControlStyle := ControlStyle + [csNeedsBorderPaint];
{$ENDIF}
FFlat := True;
FEnabled := True;
FColor := clWindow;
FAlternateColor := clWindow;
FGridColor := clBtnFace;
FShowGrid := True;
FHeaderColor := clBtnface;
FHeaderLightColor := clBtnHighlight;
FHeaderDarkColor := clBtnShadow;
FHeaderFont := TFont.Create;
FHeaderFont.OnChange := HeaderFontChange;
FSelectionColor := $00CAFFFF;
FFooterFont := TFont.Create;
FFooterFont.OnChange := FooterFontChange;
FDefRowHeight := 18;
FDefColWidth := 80;
FRowCount := 5;
FAutoAddRow := False;
FGutterKind := gkBlank;
FGutterWidth := 20;
FGutterFont := TFont.Create;
FGutterFont.OnChange := GutterFontChange;
FHorzOffset := 0;
FVertOffset := 0;
FMaxHScroll := 0;
FMaxVScroll := 0;
FSmallChange := FDefRowHeight;
FLargeChange := FDefRowHeight * 5;
ForcedColumn := -1;
AllWidth := 200;
AllHeight := 200;
FHeaderLine := 1;
FHeaderInfos := TList.Create;
ValidationEnabled := True;
CellBox := Rect(0, 0, 0, 0);
FCol := 0;
FRow := 0;
FCol2 := 0;
FRow2 := 0;
FSelectArea := Rect(0, 0, 0, 0);
IsEditing := False;
BuffString := '';
SmallBox := Rect(-1, -1, -1, -1);
SmallBoxArea := Rect(-1, -1, -1, -1);
SmallBoxPos := 0;
SizingCol := -1;
SizingColX := -1;
Screen.Cursors[crPlus] := LoadCursor(hinstance, 'CR_PLUS');
Screen.Cursors[crSmallCross] := LoadCursor(hInstance, 'CR_CROSS');
Screen.Cursors[crRight] := LoadCursor(hinstance, 'CR_RIGHT');
Screen.Cursors[crDown] := LoadCursor(hinstance, 'CR_DOWN');
Screen.Cursors[crLeftTop] := LoadCursor(hinstance, 'CR_LEFTTOP');
Cursor := crPlus;
FColumns := TNiceColumns.Create(Self);
FEdit := TNiceInplace.Create(Self);
FGutterStrings := TStringList.Create;
Mergeds := TList.Create;
FOptions := [ngoThemed];
FOnDrawBackground := nil;
FOnEditorCreating := nil;
FOnGetCellColor := nil;
end;
destructor TNiceGrid.Destroy;
begin
ClearMergeCells;
Mergeds.Free;
FGutterStrings.Free;
FEdit.Free;
FColumns.Free;
ClearHeaderInfos;
FHeaderInfos.Free;
FHeaderFont.Free;
FFooterFont.Free;
FGutterFont.Free;
inherited Destroy;
end;
procedure TNiceGrid.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or WS_HSCROLL or WS_VSCROLL;
end;
procedure TNiceGrid.CreateWnd;
begin
inherited CreateWnd;
ShowHideScrollBar(False, False);
Recalculate;
end;
procedure TNiceGrid.SetScrollBar(AKind, AMax, APos, AMask: Integer);
var Info: TScrollInfo;
begin
FillChar(Info, SizeOf(TScrollInfo), 0);
Info.cbSize := SizeOf(TScrollInfo);
Info.nMin := 0;
Info.nMax := AMax;
Info.nPos := APos;
Info.fMask := AMask;
SetScrollInfo(Handle, AKind, Info, TRUE);
// if (AKind = SB_VERT) and Assigned(Sync) then
// begin
// if ((AMask and SIF_RANGE) <> 0)
// then Sync.FMaxVScroll := AMax;
// if ((AMask and SIF_POS) <> 0)
// then Sync.VertOffset := APos;
// end;
end;
procedure TNiceGrid.ShowHideScrollBar(HorzVisible, VertVisible: Boolean);
begin
ShowScrollBar(Handle, SB_HORZ, HorzVisible);
ShowScrollBar(Handle, SB_VERT, VertVisible);
end;
procedure TNiceGrid.WMHScroll(var Msg: TWMVScroll);
var
Old: Integer;
begin
//ForceHideCaret;
Old := FHorzOffset;
case Msg.ScrollCode of
SB_LINELEFT:
FHorzOffset := FHorzOffset - FSmallChange;
SB_LINERIGHT:
FHorzOffset := FHorzOffset + FSmallChange;
SB_PAGELEFT:
FHorzOffset := FHorzOffset - FLargeChange;
SB_PAGERIGHT:
FHorzOffset := FHorzOffset + FLargeChange;
SB_THUMBTRACK:
FHorzOffset := Msg.Pos;
SB_THUMBPOSITION:
FHorzOffset := Msg.Pos;
end;
FHorzOffset := Max(0, Min(FMaxHScroll, FHorzOffset));
if (FHorzOffset <> Old) then
begin
SetScrollBar(SB_HORZ, 0, FHorzOffset, SIF_POS);
InvalidateRightWard(FixedWidth);
end;
EndEdit;
end;
procedure TNiceGrid.WMVScroll(var Msg: TWMHScroll);
var
Old: Integer;
begin
//ForceHideCaret;
Old := FVertOffset;
case Msg.ScrollCode of
SB_LINEUP:
FVertOffset := FVertOffset - FSmallChange;
SB_LINEDOWN:
FVertOffset := FVertOffset + FSmallChange;
SB_PAGEUP:
FVertOffset := FVertOffset - FLargeChange;
SB_PAGEDOWN:
FVertOffset := FVertOffset + FLargeChange;
SB_THUMBTRACK:
FVertOffset := Msg.Pos;
SB_THUMBPOSITION:
FVertOffset := Msg.Pos;
end;
FVertOffset := Max(0, Min(FMaxVScroll, FVertOffset));
NormalizeVertOffset;
if (FVertOffset <> Old) then
begin
SetScrollBar(SB_VERT, 0, FVertOffset, SIF_POS);
InvalidateDownWard(FixedHeight);
end;
EndEdit;
end;
procedure TNiceGrid.SetColCount(Value: Integer);
begin
if (ColCount <> Value) then
begin
FColumns.BeginUpdate;
while (ColCount > Value)
do FColumns.Delete(FColumns.Count-1);
while (ColCount < Value)
do FColumns.Add;
FHorzOffset := 0;
FVertOffset := 0;
FCol := Max(0, Min(FCol, ColCount-1));
FRow := Max(0, Min(FRow, FRowCount-1));
if (FRowCount = 0) or (ColCount = 0) then
begin
FCol := -1;
FRow := -1;
end;
FSelectArea := Rect(FCol, FRow, FCol, FRow);
FColumns.EndUpdate;
ColRowChanged;
end;
end;
procedure TNiceGrid.SetRowCount(Value: Integer);
begin
if (FRowCount <> Value) then
begin
FRowCount := Value;
FCol := Max(0, Min(FCol, ColCount-1));
FRow := Max(0, Min(FRow, FRowCount-1));
if (FRowCount = 0) or (ColCount = 0) then
begin
FCol := -1;
FRow := -1;
end;
FSelectArea := Rect(FCol, FRow, FCol, FRow);
Recalculate;
Invalidate;
ColRowChanged;
end;
end;
procedure TNiceGrid.ClearHeaderInfos;
var
x: Integer;
P: PHeaderInfo;
begin
for x := 0 to FHeaderInfos.Count-1 do
begin
P := PHeaderInfo(FHeaderInfos[x]);
Dispose(P);
end;
FHeaderInfos.Clear;
end;
procedure TNiceGrid.Recalculate;
var
x: Integer;
HVisible, VVisible: Boolean;
VisCount: Integer;
WidthAvail, HeightAvail: Integer;
v: Integer;
LastBodyWidth: Integer;
function GetColAutoWidth(i: Integer): Integer;
var
n: Integer;
t: TStrings;
begin
Result := 0;
t := Columns[i].FStrings;
for n := 0 to t.Count-1
do Result := Max(Result, Canvas.TextWidth(t[n]) + 7);
Result := Max(Result, 20);
end;
begin
BuildMergeData;
VisCount := 0;
for x := 0 to FColumns.Count-1 do
begin
if FColumns[x].FVisible
then Inc(VisCount);
end;
if (VisCount = 0) then
begin
FixedHeight := 0;
FixedWidth := 0;
BodyWidth := 0;
BodyHeight := 0;
ShowHideScrollBar(False, False);
Exit;
end;
if FAutoColWidth then
begin
Canvas.Font.Assign(Font);
for x := 0 to FColumns.Count-1
do FColumns[x].FWidth := Max(FDefColWidth, GetColAutoWidth(x));
end;
FixedWidth := 0;
if (FGutterKind <> gkNone)
then FixedWidth := FGutterWidth;
FixedHeight := FHeaderLine * FDefRowHeight;
BodyHeight := FRowCount * FDefRowHeight;
WidthAvail := ClientWidth - FixedWidth;
HeightAvail := ClientHeight - FixedHeight;
if FShowFooter
then HeightAvail := HeightAvail - FDefRowHeight;
BodyWidth := 0;
for x := 0 to FColumns.Count-1 do
begin
if FColumns[x].FVisible
then BodyWidth := BodyWidth + FColumns[x].FWidth;
end;
if FFitToWidth then
begin
if (BodyWidth < WidthAvail) then
begin
LastBodyWidth := BodyWidth;
x := 0;
while (BodyWidth < WidthAvail) do
begin
if (x > ColCount-1) then
begin
if (BodyWidth = LastBodyWidth)
then Break
else x := 0;
end;
if FColumns[x].FVisible and FColumns[x].FCanResize then
begin
FColumns[x].FWidth := FColumns[x].FWidth + 1;
Inc(BodyWidth);
end;
Inc(x);
end;
end;
if (BodyWidth > WidthAvail) then
begin
LastBodyWidth := BodyWidth;
x := 0;
while (BodyWidth > WidthAvail) do
begin
if (x > ColCount-1) then
begin
if (BodyWidth = LastBodyWidth)
then Break
else x := 0;
end;
if FColumns[x].FVisible and (x <> ForcedColumn) and FColumns[x].FCanResize then
begin
FColumns[x].FWidth := FColumns[x].FWidth - 1;
Dec(BodyWidth);
end;
Inc(x);
end;
end;
ForcedColumn := -1;
end;
if (BodyWidth < WidthAvail)
then FHorzOffset := 0;
if (BodyHeight < HeightAvail)
then FVertOffset := 0;
HVisible := BodyWidth > WidthAvail;
VVisible := BodyHeight > HeightAvail;
ShowHideScrollBar(HVisible, VVisible);
FMaxHScroll := Max(0, BodyWidth - ClientWidth + FixedWidth);
if FShowFooter
then FMaxVScroll := Max(0, BodyHeight - ClientHeight + FixedHeight + FDefRowHeight)
else FMaxVScroll := Max(0, BodyHeight - ClientHeight + FixedHeight);