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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
|
{
fpGUI - Free Pascal GUI Toolkit
Copyright (C) 2006 - 2014 See the file AUTHORS.txt, included in this
distribution, for details of the copyright.
See the file COPYING.modifiedLGPL, included in this distribution,
for details about redistributing fpGUI.
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.
Description:
Defines a Base Grid control. Usable as the base for any grid type of
component.
}
unit fpg_basegrid;
{$mode objfpc}{$H+}
{.$Define DEBUG}
interface
uses
Classes,
SysUtils,
fpg_base,
fpg_main,
fpg_widget,
fpg_scrollbar,
fpg_menu;
type
TfpgGridDrawState = set of (gdSelected, gdFocused, gdFixed);
TfpgGridHeaderStyle = (ghsButton, ghsThin, ghsFlat);
TfpgFocusChangeNotify = procedure(Sender: TObject; ARow, ACol: Integer) of object;
TfpgHeaderClick = procedure(Sender: TObject; ACol: Integer) of object;
TfpgRowChangeNotify = procedure(Sender: TObject; ARow: Integer) of object;
TfpgCanSelectCellEvent = procedure(Sender: TObject; const ARow, ACol: Integer; var ACanSelect: boolean) of object;
TfpgDrawCellEvent = procedure(Sender: TObject; const ARow, ACol: Integer; const ARect: TfpgRect; const AFlags: TfpgGridDrawState; var ADefaultDrawing: boolean) of object;
// widget options
TfpgGridOption = (go_HideFocusRect, go_AlternativeColor, go_SmoothScroll);
TfpgGridOptions = set of TfpgGridOption;
// Column 2 is special just for testing purposes. Descendant classes will
// override that special behavior anyway.
TfpgBaseGrid = class(TfpgWidget)
private
FColResizing: boolean;
FDragPos: integer; // used for column resizing
FHeaderStyle: TfpgGridHeaderStyle;
FOnDrawCell: TfpgDrawCellEvent;
FOnHeaderClick: TfpgHeaderClick;
FResizedCol: integer; // used for column resizing
FDefaultColWidth: integer;
FDefaultRowHeight: integer;
FFocusCol: Integer;
FFocusRow: Integer;
FHeaderHeight: integer;
FOnCanSelectCell: TfpgCanSelectCellEvent;
FOnFocusChange: TfpgFocusChangeNotify;
FOnRowChange: TfpgRowChangeNotify;
FPrevCol: Integer;
FPrevRow: Integer;
FFirstRow: Integer;
FFirstCol: Integer;
FXOffset: integer; // used for go_SmoothScroll
FFont: TfpgFont;
FHeaderFont: TfpgFont;
FRowSelect: boolean;
FScrollBarStyle: TfpgScrollStyle;
FShowGrid: boolean;
FShowHeader: boolean;
FTemp: integer;
FVScrollBar: TfpgScrollBar;
FHScrollBar: TfpgScrollBar;
FUpdateCount: integer;
FOptions: TfpgGridOptions;
FPopupMenu: TfpgPopupMenu;
FAlternativeBGColor: TfpgColor;
FBorderStyle: TfpgEditBorderStyle;
function GetFontDesc: string;
function GetHeaderFontDesc: string;
function GetTotalColumnWidth: integer;
function GetAdjustedBorderSizes: TRect;
procedure HScrollBarMove(Sender: TObject; position: integer);
procedure SetFontDesc(const AValue: string);
procedure SetHeaderFontDesc(const AValue: string);
procedure SetHeaderStyle(const AValue: TfpgGridHeaderStyle);
procedure SetRowSelect(const AValue: boolean);
procedure SetScrollBarStyle(const AValue: TfpgScrollStyle);
procedure VScrollBarMove(Sender: TObject; position: integer);
procedure SetDefaultColWidth(const AValue: integer);
procedure SetDefaultRowHeight(const AValue: integer);
procedure SetFocusCol(const AValue: Integer);
procedure SetFocusRow(const AValue: Integer);
procedure CheckFocusChange;
procedure SetShowGrid(const AValue: boolean);
procedure SetShowHeader(const AValue: boolean);
function VisibleLines: Integer;
procedure SetFirstRow(const AValue: Integer);
procedure SetAlternativeBGColor(const AValue: TfpgColor);
procedure SetBorderStyle(AValue: TfpgEditBorderStyle);
protected
property UpdateCount: integer read FUpdateCount;
procedure UpdateScrollBars; virtual;
function GetHeaderText(ACol: Integer): string; virtual;
function GetColumnWidth(ACol: Integer): integer; virtual;
procedure SetColumnWidth(ACol: Integer; const AValue: integer); virtual;
function GetBackgroundColor(ARow: integer; ACol: integer): TfpgColor; virtual;
function GetColumnBackgroundColor(ACol: Integer): TfpgColor; virtual;
procedure SetColumnBackgroundColor(ACol: Integer; const AValue: TfpgColor); virtual;
function GetColumnTextColor(ACol: Integer): TfpgColor; virtual;
procedure SetColumnTextColor(ACol: Integer; const AValue: TfpgColor); virtual;
function GetColumnCount: Integer; virtual;
function GetRowCount: Integer; virtual;
function CanSelectCell(const ARow, ACol: Integer): boolean;
function DoDrawCellEvent(ARow, ACol: Integer; ARect: TfpgRect; AFlags: TfpgGridDrawState): boolean; virtual;
procedure DoCanSelectCell(const ARow, ACol: Integer; var ACanSelect: boolean);
procedure DrawCell(ARow, ACol: Integer; ARect: TfpgRect; AFlags: TfpgGridDrawState); virtual;
procedure DrawHeader(ACol: Integer; ARect: TfpgRect; AFlags: integer); virtual;
procedure DrawGrid(ARow, ACol: Integer; ARect: TfpgRect; AFlags: integer); virtual;
procedure HandlePaint; override;
procedure HandleShow; override;
procedure HandleResize(awidth, aheight: TfpgCoord); override;
procedure HandleKeyPress(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); override;
procedure HandleMouseScroll(x, y: integer; shiftstate: TShiftState; delta: smallint); override;
procedure HandleMouseHorizScroll(x, y: integer; shiftstate: TShiftState; delta: smallint); override;
procedure HandleMouseMove(x, y: integer; btnstate: word; shiftstate: TShiftState); override;
procedure HandleLMouseUp(x, y: integer; shiftstate: TShiftState); override;
procedure HandleLMouseDown(x, y: integer; shiftstate: TShiftState); override;
procedure HandleRMouseUp(x, y: integer; shiftstate: TShiftState); override;
procedure FollowFocus; virtual;
procedure PrepareCells (firstrow, lastrow, firstcol, lastcol : integer); virtual;
property AlternateBGColor: TfpgColor read FAlternativeBGColor write SetAlternativeBGColor default clHilite1;
property BorderStyle: TfpgEditBorderStyle read FBorderStyle write SetBorderStyle default ebsDefault;
property DefaultColWidth: integer read FDefaultColWidth write SetDefaultColWidth default 64;
property DefaultRowHeight: integer read FDefaultRowHeight write SetDefaultRowHeight;
property Font: TfpgFont read FFont;
property FontDesc: string read GetFontDesc write SetFontDesc;
property HeaderFont: TfpgFont read FHeaderFont;
property HeaderFontDesc: string read GetHeaderFontDesc write SetHeaderFontDesc;
property FocusCol: Integer read FFocusCol write SetFocusCol default -1;
property FocusRow: Integer read FFocusRow write SetFocusRow default -1;
property HeaderStyle: TfpgGridHeaderStyle read FHeaderStyle write SetHeaderStyle default ghsButton;
property RowSelect: boolean read FRowSelect write SetRowSelect;
property ColumnCount: Integer read GetColumnCount;
property PopupMenu: TfpgPopupMenu read FPopupMenu write FPopupMenu;
property RowCount: Integer read GetRowCount;
property ShowHeader: boolean read FShowHeader write SetShowHeader default True;
property ShowGrid: boolean read FShowGrid write SetShowGrid default True;
property ScrollBarStyle: TfpgScrollStyle read FScrollBarStyle write SetScrollBarStyle default ssAutoBoth;
property HeaderHeight: integer read FHeaderHeight;
property TotalColumnWidth: integer read GetTotalColumnWidth;
// property ColResizing: boolean read FColResizing write FColResizing;
property ColumnWidth[ACol: Integer]: integer read GetColumnWidth write SetColumnWidth;
property ColumnBackgroundColor[ACol: Integer]: TfpgColor read GetColumnBackgroundColor write SetColumnBackgroundColor;
property ColumnTextColor[ACol: Integer]: TfpgColor read GetColumnTextColor write SetColumnTextColor;
property VisibleRows: Integer read VisibleLines;
property TopRow: Integer read FFirstRow write SetFirstRow;
property Options: TfpgGridOptions read FOptions write FOptions default [];
property OnDrawCell: TfpgDrawCellEvent read FOnDrawCell write FOnDrawCell;
property OnFocusChange: TfpgFocusChangeNotify read FOnFocusChange write FOnFocusChange;
property OnHeaderClick: TfpgHeaderClick read FOnHeaderClick write FOnHeaderClick;
property OnRowChange: TfpgRowChangeNotify read FOnRowChange write FOnRowChange;
property OnCanSelectCell: TfpgCanSelectCellEvent read FOnCanSelectCell write FOnCanSelectCell;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure AfterConstruction; override;
procedure Update;
procedure BeginUpdate;
procedure EndUpdate;
procedure MouseToCell(X, Y: Integer; var ACol, ARow: Integer);
function GetClientRect: TfpgRect; override;
function VisibleWidth: integer;
function VisibleHeight: integer;
end;
implementation
{ TfpgBaseGrid }
procedure TfpgBaseGrid.HScrollBarMove(Sender: TObject; position: integer);
begin
if go_SmoothScroll in FOptions then
begin
if FXOffset <> position then
begin
if Position < 0 then
Position := 0;
FXOffset := position;
Repaint;
end;
end
else
begin
if FFirstCol <> position then
begin
if Position < 0 then
Position := 0;
FFirstCol := position;
RePaint;
end;
end;
end;
function TfpgBaseGrid.GetFontDesc: string;
begin
Result := FFont.FontDesc;
end;
function TfpgBaseGrid.GetHeaderFontDesc: string;
begin
Result := FHeaderFont.FontDesc;
end;
function TfpgBaseGrid.GetTotalColumnWidth: integer;
var
i: integer;
begin
Result := 0;
for i := 0 to ColumnCount-1 do
Result := Result + ColumnWidth[i];
end;
// Adjust theme borders based on BorderStyle property
function TfpgBaseGrid.GetAdjustedBorderSizes: TRect;
begin
Result := fpgStyle.GetControlFrameBorders;
case BorderStyle of
ebsNone:
begin
Result.Left := 0;
Result.Right := 0;
Result.Top := 0;
Result.Bottom := 0;
end;
ebsDefault:
begin
// do nothing - the theme values are correct
end;
ebsSingle:
begin
Result.Left := 1;
Result.Right := 1;
Result.Top := 1;
Result.Bottom := 1;
end;
end;
end;
procedure TfpgBaseGrid.SetFontDesc(const AValue: string);
begin
FFont.Free;
FFont := fpgGetFont(AValue);
if DefaultRowHeight < FFont.Height + 2 then
DefaultRowHeight := FFont.Height + 2;
RePaint;
end;
procedure TfpgBaseGrid.SetHeaderFontDesc(const AValue: string);
begin
FHeaderFont.Free;
FHeaderFont := fpgGetFont(AValue);
if FHeaderHeight < FHeaderFont.Height + 2 then
FHeaderHeight := FHeaderFont.Height + 2;
RePaint;
end;
procedure TfpgBaseGrid.SetHeaderStyle(const AValue: TfpgGridHeaderStyle);
begin
if FHeaderStyle = AValue then
exit;
FHeaderStyle := AValue;
Repaint;
end;
procedure TfpgBaseGrid.SetRowSelect(const AValue: boolean);
begin
if FRowSelect = AValue then
Exit; //==>
FRowSelect := AValue;
RePaint;
end;
procedure TfpgBaseGrid.SetScrollBarStyle(const AValue: TfpgScrollStyle);
begin
if FScrollBarStyle = AValue then
Exit; //==>
FScrollBarStyle := AValue;
end;
procedure TfpgBaseGrid.VScrollBarMove(Sender: TObject; position: integer);
begin
if FFirstRow <> position then
begin
FFirstRow := position;
RePaint;
end;
end;
procedure TfpgBaseGrid.SetDefaultColWidth(const AValue: integer);
begin
if FDefaultColWidth = AValue then
Exit; //==>
FDefaultColWidth := AValue;
RePaint;
end;
procedure TfpgBaseGrid.SetDefaultRowHeight(const AValue: integer);
begin
if FDefaultRowHeight = AValue then
Exit; //==>
FDefaultRowHeight := AValue;
RePaint;
end;
function TfpgBaseGrid.GetColumnWidth(ACol: Integer): integer;
begin
Result := 50;
end;
procedure TfpgBaseGrid.SetColumnWidth(ACol: Integer; const AValue: integer);
begin
// GetColumnWidth and SetColumnWidth will be overriden in decendant!
// Column 2 is special just for testing purposes
if (ACol = 2) and (AValue <> FTemp) then
begin
FTemp := AValue;
UpdateScrollBars;
Repaint;
end;
end;
function TfpgBaseGrid.GetBackgroundColor(ARow: integer; ACol: integer): TfpgColor;
begin
if (ARow >= 0) and (ACol >= 0) and (ARow < RowCount) and (ACol < ColumnCount) then
begin
if go_AlternativeColor in Options then
begin
if (ARow mod 2) <> 0 then
Result := AlternateBGColor
else
Result := ColumnBackgroundColor[ACol];
end
else
Result := ColumnBackgroundColor[ACol];
end
else
Result := BackgroundColor;
end;
function TfpgBaseGrid.GetColumnBackgroundColor(ACol: Integer): TfpgColor;
begin
// implemented in descendant
end;
procedure TfpgBaseGrid.SetColumnBackgroundColor(ACol: Integer; const AValue: TfpgColor);
begin
// implemented in descendant
end;
function TfpgBaseGrid.GetColumnTextColor(ACol: Integer): TfpgColor;
begin
// implemented in descendant
end;
procedure TfpgBaseGrid.SetColumnTextColor(ACol: Integer; const AValue: TfpgColor);
begin
// implemented in descendant
end;
function TfpgBaseGrid.GetColumnCount: Integer;
begin
Result := 7;
end;
function TfpgBaseGrid.GetRowCount: Integer;
begin
Result := 24;
end;
function TfpgBaseGrid.CanSelectCell(const ARow, ACol: Integer): boolean;
begin
Result := (ARow >= 0) and (ACol >= 0) and (ARow < RowCount) and (ACol < ColumnCount);
if Result then
DoCanSelectCell(ARow, ACol, Result);
end;
function TfpgBaseGrid.DoDrawCellEvent(ARow, ACol: Integer; ARect: TfpgRect;
AFlags: TfpgGridDrawState): boolean;
begin
Result := True;
if Assigned(OnDrawCell) then
FOnDrawCell(self, ARow, ACol, ARect, AFlags, Result);
end;
procedure TfpgBaseGrid.DoCanSelectCell(const ARow, ACol: Integer; var
ACanSelect: boolean);
begin
if Assigned(OnCanSelectCell) then
FOnCanSelectCell(self, ARow, ACol, ACanSelect);
end;
procedure TfpgBaseGrid.DrawCell(ARow, ACol: Integer; ARect: TfpgRect; AFlags: TfpgGridDrawState);
var
s: string;
begin
s := 'c(' + IntToStr(ARow) + ',' + IntToStr(ACol) + ')';
if (ARow = 5) and (ACol = 2) then
s := 'Here lives Graeme!';
if not Enabled then
Canvas.SetTextColor(clShadow1);
Canvas.DrawText(ARect, s, [txtHCenter, txtVCenter]);
end;
procedure TfpgBaseGrid.DrawHeader(ACol: Integer; ARect: TfpgRect; AFlags: integer);
var
s: string;
r: TfpgRect;
x: integer;
begin
r := ARect;
// Here we can implement a head style check
case FHeaderStyle of
ghsButton:
begin
Canvas.DrawButtonFace(ARect, [btfIsEmbedded]);
InflateRect(r, -2, -2);
end;
ghsThin:
begin
Canvas.DrawBevel(ARect);
end;
ghsFlat:
begin
Canvas.Color:= clGridHeader;
Canvas.FillRectangle(r);
Canvas.Color:= clShadow2;
Canvas.DrawLine(r.Left, r.Bottom, r.Right, r.Bottom); { bottom line }
Canvas.DrawLine(r.Right, r.Bottom, r.Right, r.Top-1); { right line }
end;
end;
Canvas.AddClipRect(r); // text may not overshoot header border
(*
// drawing grid lines
Canvas.SetColor(clGridLines);
Canvas.DrawLine(r.Left, r.Bottom+1, r.Right+1, r.Bottom+1); // horizontal bottom
Canvas.DrawLine(r.Right+1, r.Top, r.Right+1, r.Bottom+1); // vertical right
if (ACol mod 2) = 0 then
Canvas.SetColor(clGridHeader)
else
Canvas.SetColor(clMagenta);
Canvas.FillRectangle(ARect);
*)
Canvas.SetTextColor(clText1);
s := GetHeaderText(ACol);
x := (ARect.Left + (ARect.Width div 2)) - (FHeaderFont.TextWidth(s) div 2);
if not (go_SmoothScroll in FOptions) then
begin
if x < 1 then
x := 1;
end;
fpgStyle.DrawString(Canvas, x, ARect.Top+1, s, Enabled);
end;
procedure TfpgBaseGrid.DrawGrid(ARow, ACol: Integer; ARect: TfpgRect;
AFlags: integer);
begin
// default is inside bottom/right edge or cell
Canvas.SetColor(clGridLines);
if (BorderStyle <> ebsDefault) and (ACol = 0) then
Canvas.DrawLine(ARect.Left, ARect.Top, ARect.Left, ARect.Bottom); // cell left of first column only
Canvas.DrawLine(ARect.Left, ARect.Bottom, ARect.Right, ARect.Bottom); // cell bottom
Canvas.DrawLine(ARect.Right, ARect.Bottom, ARect.Right, ARect.Top-1); // cell right
end;
procedure TfpgBaseGrid.SetFocusCol(const AValue: Integer);
begin
if FFocusCol = AValue then
Exit; //==>
FFocusCol := AValue;
// apply min/max limit
if FFocusCol < 0 then
FFocusCol := 0;
if FFocusCol > ColumnCount-1 then
FFocusCol := ColumnCount-1;
FollowFocus;
Update;
end;
procedure TfpgBaseGrid.SetFocusRow(const AValue: Integer);
begin
if FFocusRow = AValue then
Exit; //==>
FFocusRow := AValue;
// apply min/max limit
if FFocusRow < 0 then
FFocusRow := 0;
if FFocusRow > RowCount-1 then
FFocusRow := RowCount-1;
FollowFocus;
Update;
end;
procedure TfpgBaseGrid.CheckFocusChange;
begin
if ((FPrevCol <> FFocusCol) and not RowSelect) or (FPrevRow <> FFocusRow) then
if Assigned(FOnFocusChange) then
FOnFocusChange(self, FFocusRow, FFocusCol);
if (FPrevRow <> FFocusRow) then
if Assigned(FOnRowChange) then
FOnRowChange(self, FFocusRow);
FPrevCol := FFocusCol;
FPrevRow := FFocusRow;
end;
procedure TfpgBaseGrid.SetShowGrid(const AValue: boolean);
begin
if FShowGrid = AValue then
Exit; //==>
FShowGrid := AValue;
RePaint;
end;
procedure TfpgBaseGrid.SetShowHeader(const AValue: boolean);
begin
if FShowHeader = AValue then
Exit; //==>
FShowHeader := AValue;
UpdateScrollBars;
RePaint;
end;
// Return the fully visible lines only. Partial lines not counted
function TfpgBaseGrid.VisibleLines: Integer;
var
hh: integer;
begin
if FHScrollBar.Visible then
hh := FHScrollbar.Height
else
hh := 0;
if ShowHeader then
hh := hh + FHeaderHeight+1;
Result := (GetClientRect.Height - hh) div FDefaultRowHeight;
end;
function TfpgBaseGrid.VisibleWidth: integer;
var
sw: integer;
begin
if FVScrollBar.Visible then
sw := FVScrollBar.Width
else
sw := 0;
Result := GetClientRect.Width - sw
end;
function TfpgBaseGrid.VisibleHeight: integer;
var
sw: integer;
begin
if FHScrollBar.Visible then
sw := FHScrollBar.Height
else
sw := 0;
Result := GetClientRect.Height - sw;
end;
procedure TfpgBaseGrid.SetFirstRow(const AValue: Integer);
begin
if FFirstRow = AValue then
Exit; //==>
if AValue < ((RowCount - VisibleLines)) then
FFirstRow := AValue
else
FFirstRow := (RowCount - VisibleLines);
UpdateScrollBars;
RePaint;
end;
procedure TfpgBaseGrid.SetAlternativeBGColor(const AValue: TfpgColor);
begin
if FAlternativeBGColor = AValue then exit;
FAlternativeBGColor := AValue;
end;
procedure TfpgBaseGrid.SetBorderStyle(AValue: TfpgEditBorderStyle);
begin
if FBorderStyle = AValue then
Exit;
FBorderStyle := AValue;
Repaint;
end;
procedure TfpgBaseGrid.UpdateScrollBars;
var
HWidth: integer;
VHeight: integer;
vw: integer;
cw: integer;
vl: integer;
i: integer;
x: integer;
hmax: integer;
vmax: integer;
Hfits, showH : boolean;
Vfits, showV : boolean;
crect: TfpgRect;
borders: TRect;
procedure hideScrollbar (sb : TfpgScrollBar);
begin
with sb do
if Visible then
begin
Visible := False;
UpdateWindowPosition;
end;
end;
procedure getVisWidth;
begin
if showV then
vw := HWidth - (FVScrollBar.Width-1)
else
vw := HWidth;
Hfits := vw >= cw;
end;
procedure getVisLines;
var
hh : integer; // header height
begin
hh := 0;
if ShowHeader then
inc (hh, FHeaderHeight+1);
if showH then
inc (hh, FHScrollBar.Height);
vl := (VHeight - hh) div FDefaultRowHeight;
Vfits := vl >= RowCount;
end;
begin
// if we don't want any scrollbars, hide them and exit
if FScrollBarStyle = ssNone then
begin
hideScrollbar(FHScrollBar);
hideScrollbar(FVScrollBar);
exit;
end;
borders := GetAdjustedBorderSizes;
// preliminary width/height calculations
crect := GetClientRect;
VHeight := crect.Height;
HWidth := crect.Width;
cw := 0;
for i := 0 to ColumnCount-1 do
cw := cw + ColumnWidth[i];
showV := False;
showH := False;
getVisWidth;
getVisLines;
// determine whether to show scrollbars for different configurations
case FScrollBarStyle of
ssHorizontal:
begin
hideScrollbar (FVScrollBar);
if not Hfits then
begin
showH := true;
getVisLines;
end;
end;
ssVertical:
begin
hideScrollbar (FHScrollBar);
if not Vfits then
begin
showV := true;
getVisWidth;
end;
end;
ssAutoBoth:
if not Vfits then
begin
showV := true;
getVisWidth;
if not Hfits then
begin
showH := true;
getVisLines;
getVisWidth;
end;
end
else if not Hfits then
begin
showH := true;
getVisLines;
if not Vfits then
begin
showV := true;
getVisWidth;
getVisLines;
end;
end;
end;
// set the scrollbar width/height space
if showV then
Dec(HWidth, FVScrollBar.Width);
if showH then
Dec(VHeight, FHScrollBar.Height);
// show or hide the scrollbars
if showV then
begin
FVScrollBar.Visible := true;
FVScrollBar.Min := 0;
if RowCount > 0 then
FVScrollBar.SliderSize := VisibleLines / RowCount
else
FVScrollBar.SliderSize := 0;
vmax := RowCount - VisibleLines;
if FFirstRow > vmax then
FFirstRow := vmax;
FVScrollBar.Max := vmax;
FVScrollBar.Position := FFirstRow;
FVScrollBar.RepaintSlider;
FVScrollBar.Top := borders.Top;
FVScrollBar.Left := Width - FVScrollBar.Width - borders.Right;
FVScrollBar.Height := VHeight;
end
else
begin
FVScrollBar.Visible := false;
if Vfits then
FFirstRow := 0;
// if vertical doesn't fit and no scrollbar, do not change firstrow
end;
if showH then
begin
FHScrollBar.Visible := true;
FHScrollBar.Min := 0;
if go_SmoothScroll in FOptions then
begin
hmax := cw - vw;
FHScrollBar.Max := hmax;
if FXOffset>hmax then
FXOffset:=hmax;
FHScrollBar.Position := FXOffset;
FHScrollBar.SliderSize := HWidth / TotalColumnWidth;
FHScrollBar.PageSize := 5;
end
else
begin
FHScrollBar.Max := ColumnCount-1;
FHScrollBar.Position := FFirstCol;
FHScrollBar.SliderSize := 1 / ColumnCount;
FHScrollBar.PageSize := 1;
end;
FHScrollBar.RepaintSlider;
FHScrollBar.Top := Height - FHScrollBar.Height - borders.Bottom;
FHScrollBar.Left := borders.Left;
FHScrollBar.Width := HWidth;
end
else
begin
FHScrollBar.Visible := False;
if Hfits then
begin
FFirstCol := 0;
FXOffset := 0;
end;
// if horizontal doesn't fit and no scrollbar, do not change firstcol/xoffset
end;
FVScrollBar.UpdateWindowPosition;
FHScrollBar.UpdateWindowPosition;
end;
function TfpgBaseGrid.GetHeaderText(ACol: Integer): string;
begin
Result := 'Head ' + IntToStr(ACol);
end;
procedure TfpgBaseGrid.HandlePaint;
var
r: TfpgRect;
r2: TfpgRect;
col: Integer;
row: Integer;
clipr: TfpgRect; // clip rectangle
drawstate: TfpgGridDrawState;
cLeft: integer;
rTop: integer;
firstcol, lastcol, firstrow, lastrow : integer;
cWidths: array of integer;
rect: TRect;
begin
Canvas.ClearClipRect;
r.SetRect(0, 0, Width, Height);
case BorderStyle of
ebsNone:
begin
// do nothing
end;
ebsDefault:
begin
fpgStyle.DrawControlFrame(Canvas, r);
end;
ebsSingle:
begin
Canvas.SetColor(clShadow2);
Canvas.DrawRectangle(r);
end;
end;
r := GetClientRect;
clipr := r;
Canvas.SetClipRect(clipr);
Canvas.SetColor(FBackgroundColor);
Canvas.FillRectangle(r);
cLeft := r.Left; // column starting point
rTop := r.Top; // row starting point
if go_SmoothScroll in FOptions then
begin
if FHScrollBar.Visible then
Dec(cLeft, FHScrollBar.Position);
firstcol := 0;
end
else
begin
firstcol := FFirstCol;
end;
// calculate column widths, and first/last columns
if (ColumnCount <= 0) then
begin
firstcol := -1;
lastcol := -2;
end
else
begin
setlength (cWidths, ColumnCount);
r.Left := cLeft;
for col := firstcol to ColumnCount-1 do
begin
cWidths[col] := ColumnWidth[col];
r.Width := cWidths[col];
if (go_SmoothScroll in FOptions) and (r.Left <= clipr.Left) then
begin
firstcol := col;
if col>0 then inc (cLeft, cWidths[col-1]);
end;
lastcol := col;
if r.Right >= clipr.Right then
break;
inc (r.Left, r.Width);
end;
// first/last rows...
if (RowCount <= 0) then
begin
firstrow := -1;
lastrow := -2;
end
else
begin
if ShowHeader then
inc (r.Top, FHeaderHeight);
if r.Top > clipr.Bottom then
begin
firstrow := -1;
lastrow := -2;
end
else
begin
firstrow := FFirstRow;
lastrow := firstrow + (clipr.Bottom - r.Top) div DefaultRowHeight;
if lastrow >= RowCount then
lastrow := RowCount-1;
end;
end;
end;
PrepareCells (firstrow, lastrow, firstcol, lastcol);
r.Left := cLeft;
r.Top := rTop;
if (ColumnCount > 0) and ShowHeader then
begin
// Drawing horizontal headers
r.Height := FHeaderHeight;
Canvas.SetFont(FHeaderFont);
for col := firstcol to lastcol do
begin
r.Width := cWidths[col];
Canvas.SetClipRect(clipr);
Canvas.AddClipRect(r);
DrawHeader(col, r, 0);
inc(r.Left, r.Width);
//if r.Left >= clipr.Right then
// Break; // optimization made obsolete by lastcol
end;
inc(r.Top, r.Height);
end;
if (RowCount > 0) and (ColumnCount > 0) then
begin
// Drawing cells
r.Height := DefaultRowHeight;
Canvas.SetFont(FFont);
for row := firstrow to lastrow do
begin
r.Left := cLeft;
for col := firstcol to lastcol do
begin
drawstate := [];
r.Width := cWidths[col];
Canvas.SetClipRect(clipr);
if (row = FFocusRow) and (RowSelect or (col = FFocusCol)) and not (go_HideFocusRect in FOptions) then
begin
if FFocused then
begin
Canvas.SetColor(clGridSelection);
Canvas.SetTextColor(clGridSelectionText);
end
else
begin
Canvas.SetColor(clGridInactiveSel);
Canvas.SetTextColor(clGridInactiveSelText);
end;
end
else
begin
Canvas.SetColor(GetBackgroundColor(row, col));
Canvas.SetTextColor(ColumnTextColor[col]);
end;
Canvas.AddClipRect(r);
Canvas.FillRectangle(r);
// setup drawstate
if FFocused then
Include(drawstate, gdFocused);
if (row = FFocusRow) and (col = FFocusCol) then
Include(drawstate, gdSelected);
if DoDrawCellEvent(row, col, r, drawstate) then
DrawCell(row, col, r, drawstate);
// drawing grid lines
if FShowGrid then
DrawGrid(row, col, r, 0);
inc(r.Left, r.Width);
//if r.Left >= clipr.Right then
// Break; // optimization made obsolete by lastcol
end;
// Inc(r.Top, FDefaultRowHeight+1);
inc(r.Top, r.Height);
//if r.Top >= clipr.Bottom then
// break; // optimization made obsolete by lastrow
end;
end; // item drawing
Canvas.SetClipRect(clipr);
Canvas.SetColor(FBackgroundColor);
// clearing after the last column
if r.Left <= clipr.Right then
begin
r2.Left := r.Left;
r2.Top := clipr.Top;
r2.SetRight(clipr.Right);
r2.Height := clipr.Height;
Canvas.FillRectangle(r2);
end;
// clearing after the last row
if r.Top <= clipr.Bottom then
begin
r.Left := clipr.Left;
r.Width := clipr.Width;
r.SetBottom(clipr.Bottom);
Canvas.FillRectangle(r);
end;
// The little square in the bottom right corner
if FHScrollBar.Visible and FVScrollBar.Visible then
begin
Canvas.ClearClipRect;
Canvas.SetColor(clButtonFace);
Canvas.FillRectangle(FHScrollBar.Left+FHScrollBar.Width,
FVScrollBar.Top+FVScrollBar.Height,
FVScrollBar.Width,
FHScrollBar.Height);
end;
end;
procedure TfpgBaseGrid.HandleShow;
begin
inherited HandleShow;
if (csLoading in ComponentState) then
Exit;
UpdateScrollBars;
end;
procedure TfpgBaseGrid.HandleResize(awidth, aheight: TfpgCoord);
begin
inherited HandleResize(awidth, aheight);
if (csLoading in ComponentState) then
Exit; //==>
if csUpdating in ComponentState then
Exit; //==>
if HasHandle then
UpdateScrollBars;
end;
procedure TfpgBaseGrid.HandleKeyPress(var keycode: word;
var shiftstate: TShiftState; var consumed: boolean);
var
w: integer;
r: integer;
c: integer;
begin
if consumed then
exit;
fpgApplication.HideHint;
case keycode of
keyRight:
begin
if RowSelect then
begin
w := 0;
FFocusCol := FFirstCol;
while FFocusCol < ColumnCount do
begin
inc(w, ColumnWidth[FFocusCol]+1);
if w + ColumnWidth[FFocusCol+1]+1 > VisibleWidth then
Break;
inc(FFocusCol);
end;
end;
if CanSelectCell(FFocusRow, FFocusCol+1) then
begin
inc(FFocusCol);
FollowFocus;
RePaint;
end;
consumed := True;
end;
keyLeft:
begin
if RowSelect then
FFocusCol := FFirstCol;
if CanSelectCell(FFocusRow, FFocusCol-1) then
begin
dec(FFocusCol);
FollowFocus;
RePaint;
end;
consumed := True;
end;
keyUp:
begin
if CanSelectCell(FFocusRow-1, FFocusCol) then
begin
dec(FFocusRow);
FollowFocus;
RePaint;
end;
consumed := True;
end;
keyDown:
begin
if CanSelectCell(FFocusRow+1, FFocusCol) then
begin
inc(FFocusRow);
FollowFocus;
RePaint;
end;
consumed := True;
end;
keyPageUp:
begin
r := FFocusRow-VisibleLines;
if r < 0 then
r := 0;
if (FFocusRow <> 0) and CanSelectCell(r, FFocusCol) then
begin
FFocusRow := r;
FollowFocus;
RePaint;
end;
consumed := True;
end;
keyPageDown:
begin
r := FFocusRow+VisibleLines;
if r > (RowCount-1) then
r := RowCount-1;
if (FFocusRow <> (RowCount-1)) and CanSelectCell(r, FFocusCol) then
begin
FFocusRow := r;
FollowFocus;
RePaint;
end;
consumed := True;
end;
keyHome:
begin
if FRowSelect then
begin
if (FFocusRow <> 0) and CanSelectCell(0, FFocusCol) then
begin
FFocusRow := 0;
FollowFocus;
RePaint;
end;
end
else if (FFocusCol <> 0) then
begin
{ find first selectable column }
for c := 0 to FFocusCol-1 do
if CanSelectCell(FFocusRow, c) then
begin
FFocusCol := c;
FollowFocus;
RePaint;
break;
end;
end;
consumed := True;
end;
keyEnd:
begin
if FRowSelect then
begin
if (FFocusRow <> (RowCount-1)) and CanSelectCell(RowCount-1, FFocusCol) then
begin
FFocusRow := RowCount-1;
FollowFocus;
RePaint;
end;
end
else if (FFocusCol <> (ColumnCount-1)) then
begin
for c := ColumnCount-1 downto FFocusCol+1 do
if CanSelectCell(FFocusRow, c) then
begin
FFocusCol := c;
FollowFocus;
RePaint;
break;
end;
end;
consumed := True;
end;
end; { case }
if consumed then
CheckFocusChange;
inherited HandleKeyPress(keycode, shiftstate, consumed);
end;
procedure TfpgBaseGrid.HandleMouseScroll(x, y: integer; shiftstate: TShiftState; delta: smallint);
var
lRow: Integer;
begin
inherited HandleMouseScroll(x, y, shiftstate, delta);
lRow := FFirstRow;
// If vertical scrollbar is not visible, but
// horizontal is, Mouse wheel will scroll horizontally. :)
if FHScrollBar.Visible and (not FVScrollBar.Visible) then
begin
HandleMouseHorizScroll(x, y, shiftstate, delta);
Exit;
end;
inc(FFirstRow, delta*3);
// apply limits
if FFirstRow > RowCount - VisibleLines then
FFirstRow := RowCount - VisibleLines;
if FFirstRow < 0 then
FFirstRow := 0;
if lRow <> FFirstRow then
begin
UpdateScrollBars;
RePaint;
end;
end;
procedure TfpgBaseGrid.HandleMouseHorizScroll(x, y: integer; shiftstate: TShiftState; delta: smallint);
var
old_val: Integer;
begin
inherited HandleMouseHorizScroll(x, y, shiftstate, delta);
if go_SmoothScroll in Options then
begin
old_val := FXOffset;
inc(FXOffset, delta*FHScrollBar.ScrollStep);
if (FXOffset<0) then
FXOffset:=0;
// finding the maximum Xoffset is tricky, let updatescrollbars do it.
if (FXOffset=old_val) then
Exit;
end
else
begin
old_val := FFirstCol;
inc(FFirstCol, delta);
if FFirstCol<0 then
FFirstCol:=0
else if FFirstCol > ColumnCount-1 then
FFirstCol:=ColumnCount-1;
if FFirstCol=old_val then
Exit;
end;
UpdateScrollBars;
RePaint;
end;
procedure TfpgBaseGrid.HandleMouseMove(x, y: integer; btnstate: word; shiftstate: TShiftState);
var
hh: integer;
cw: integer;
n: integer;
colresize: boolean;
cLeft: integer;
c: integer;
borders: TRect;
begin
inherited HandleMouseMove(x, y, btnstate, shiftstate);
if (ColumnCount = 0) or (RowCount = 0) then
Exit; //==>
if FColResizing then
begin
if (btnstate and 1) = 0 then
FColResizing := False
else
begin
cw := (ColumnWidth[FResizedCol]+x)-FDragPos;
if cw < 1 then
cw := 1;
SetColumnWidth(FResizedCol, cw);
FDragPos := x;
end;
end
else if ShowHeader then
begin
colresize := False;
hh := FHeaderHeight;
borders := GetAdjustedBorderSizes;
cLeft := borders.Left; // column starting point
if go_SmoothScroll in FOptions then
begin
if FHScrollBar.Visible then
Dec(cLeft, FHScrollBar.Position);
c := 0;
end
else
begin
c := FFirstCol;
end;
if (y <= (borders.Top + hh)) then // we are over the Header row
begin
cw := 0;
for n := c to ColumnCount-1 do
begin
inc(cw, ColumnWidth[n]);
// Resizing is enabled 4 pixel either way of the cell border
if ((x >= (cLeft+cw-4)) and (x <= (cLeft+cw+4))) then
begin
colresize := True;
Break;
end;
{ TODO: We could optimize this slightly by breaking as soon as x is
greater than current column edge we are over. }
end; { if }
end; { if/else }
if colresize then
MouseCursor := mcSizeEW
else
MouseCursor := mcDefault;
end; { if/else }
end;
procedure TfpgBaseGrid.HandleLMouseUp(x, y: integer; shiftstate: TShiftState);
var
lColumn: integer;
hh: integer; { header height }
cLeft: integer; { column left }
c: integer;
n: integer;
cw: integer;
borders: TRect;
begin
inherited HandleLMouseUp(x, y, shiftstate);
if not FColResizing then
begin
if not ShowHeader then
Exit;
if (ColumnCount = 0) then
Exit; //==>
// searching for the appropriate character position
hh := FHeaderHeight;
borders := GetAdjustedBorderSizes;
if (y < (borders.Top+hh)) then // inside Header row
begin
{$IFDEF DEBUG} Writeln('header click...'); {$ENDIF}
cLeft := borders.Left; // column starting point
if go_SmoothScroll in FOptions then
begin
if FHScrollBar.Visible then
Dec(cLeft, FHScrollBar.Position);
c := 0;
end
else
begin
c := FFirstCol;
end;
cw := 0;
for n := c to ColumnCount-1 do
begin
inc(cw, ColumnWidth[n]);
if x < (cLeft+cw+4) then
begin
if Assigned(FOnHeaderClick) then
FOnHeaderClick(self, n);
Break;
end;
end; { for }
end;
end; {if not FColResizing }
{$IFDEF DEBUG}
if FColResizing then
begin
Writeln('Column ', FResizedCol,' width = ', ColumnWidth[FResizedCol]);
end;
{$ENDIF}
FColResizing := False;
MouseCursor := mcDefault;
end;
procedure TfpgBaseGrid.HandleLMouseDown(x, y: integer; shiftstate: TShiftState);
var
hh: integer;
n: Integer;
cw: integer;
nw: integer;
prow: Integer;
pcol: Integer;
c: integer;
cLeft: integer;
borders: TRect;
begin
inherited HandleLMouseDown(x, y, shiftstate);
if (ColumnCount = 0) or (RowCount = 0) then
Exit; //==>
pcol := FFocusCol;
prow := FFocusRow;
borders := GetAdjustedBorderSizes;
// searching for the appropriate character position
if ShowHeader then
hh := FHeaderHeight
else
hh := 0;
if ShowHeader and (y < (borders.Top+hh)) then // inside Header row
begin
{$IFDEF DEBUG} Writeln('header click...'); {$ENDIF}
cLeft := borders.Left; // column starting point
if go_SmoothScroll in FOptions then
begin
if FHScrollBar.Visible then
Dec(cLeft, FHScrollBar.Position);
c := 0;
end
else
begin
c := FFirstCol;
end;
cw := 0;
for n := c to ColumnCount-1 do
begin
inc(cw, ColumnWidth[n]);
if (x >= (cLeft+cw-4)) and (x <= (cLeft+cw+4)) then
begin
{$IFDEF DEBUG} Writeln('column resize...'); {$ENDIF}
FColResizing := True;
FResizedCol := n;
FDragPos := x;
Exit;
end; { if/else }
{ TODO: We could optimize this slightly by breaking as soon as x is
greater than current column edge we are over. }
end; { for }
end
else
begin // Selecting a Cell via mouse
MouseToCell(x, y, FFocusCol, FFocusRow);
end; { if/else }
if not CanSelectCell(FFocusRow, FFocusCol) then
begin
// restore previous values
FFocusRow := prow;
FFocusCol := pcol;
end;
if (prow <> FFocusRow) or (pcol <> FFocusCol) then
begin
FollowFocus;
Repaint;
end;
if FColResizing then
MouseCursor := mcSizeEW;
CheckFocusChange;
end;
procedure TfpgBaseGrid.HandleRMouseUp(x, y: integer; shiftstate: TShiftState);
var
hh: integer;
begin
inherited HandleRMouseUp(x, y, shiftstate);
if Assigned(PopupMenu) then
begin
// popup should not appear if you clicked in header - maybe this behaviour should be user-selectable?
if ShowHeader then
hh := FHeaderHeight+1
else
hh := 0;
if ShowHeader and (y > (fpgStyle.GetControlFrameBorders.Top + hh)) then // not in Header row
begin
PopupMenu.ShowAt(self, x, y);
end;
end;
end;
procedure TfpgBaseGrid.FollowFocus;
var
n: Integer;
w: TfpgCoord;
lmin, lmax: TfpgCoord;
begin
if (RowCount > 0) and (FFocusRow < 0) then
FFocusRow := 0;
if FFocusRow > RowCount-1 then
FFocusRow := RowCount-1;
if (ColumnCount > 0) and (FFocusCol < 0) then
FFocusCol := 0;
if FFocusCol > ColumnCount-1 then
FFocusCol := ColumnCount-1;
if FFirstRow < 0 then
FFirstRow := 0;
if FFirstCol < 0 then
FFirstCol := 0;
if FFocusRow < FFirstRow then
FFirstRow := FFocusRow
else
begin
if (FFirstRow + VisibleLines) <= FFocusRow then
FFirstRow := (FFocusRow - VisibleLines) + 1; // scroll last partial row into view
end; { if/else }
if FFocusCol < FFirstCol then
FFirstCol := FFocusCol
else
begin
w := 0;
for n := FFocusCol downto FFirstCol do
begin
w := w + ColumnWidth[n];
if w > VisibleWidth then
begin
if n = FFocusCol then
FFirstCol := n
else
FFirstCol := n+1;
break;
end;
end; { for }
end; { if/else }
// If smoothscroll, convert FFirstCol to X Offset value
if go_SmoothScroll in FOptions then
begin
w := 0;
for n := 0 to FFocusCol-1 do
w := w + ColumnWidth[n];
lmin := FXOffset;
lmax := FXOffset + VisibleWidth;
if (w > lmax) or (w < lmin) then
FXOffset := w;
end;
CheckFocusChange;
UpdateScrollBars;
end;
procedure TfpgBaseGrid.PrepareCells(firstrow, lastrow, firstcol, lastcol: integer);
begin
// for descendents
end;
constructor TfpgBaseGrid.Create(AOwner: TComponent);
var
borders: TRect;
begin
Updating;
inherited Create(AOwner);
Focusable := True;
Width := 120;
Height := 80;
FFocusCol := -1;
FPrevCol := -1;
FFocusRow := -1;
FPrevRow := -1;
FFirstRow := 0;
FFirstCol := 0;
FShowHeader := True;
FShowGrid := True;
FRowSelect := False;
FScrollBarStyle := ssAutoBoth;
FUpdateCount := 0;
FOptions := [];
FHeaderStyle := ghsButton;
FBorderStyle := ebsDefault;
borders := GetAdjustedBorderSizes;
FFont := fpgGetFont('#Grid');
FHeaderFont := fpgGetFont('#GridHeader');
FTemp := 50; // Just to prove that ColumnWidth does adjust.
FDefaultColWidth := 64;
FDefaultRowHeight := FFont.Height + 2;
FHeaderHeight := FHeaderFont.Height + 2;
FBackgroundColor := clBoxColor;
FAlternativeBGColor := clHilite1;
FColResizing := False;
MinHeight := HeaderHeight + DefaultRowHeight + borders.Top + borders.Bottom;
MinWidth := DefaultColWidth + borders.Left + borders.Right;
FVScrollBar := TfpgScrollBar.Create(self);
FVScrollBar.Orientation := orVertical;
FVScrollBar.Visible := False;
FVScrollBar.OnScroll := @VScrollBarMove;
FHScrollBar := TfpgScrollBar.Create(self);
FHScrollBar.Orientation := orHorizontal;
FHScrollBar.Visible := False;
FHScrollBar.OnScroll := @HScrollBarMove;
FHScrollBar.ScrollStep := 20;
end;
destructor TfpgBaseGrid.Destroy;
begin
FOnRowChange := nil;
FOnFocusChange := nil;
FFont.Free;
FHeaderFont.Free;
inherited Destroy;
end;
procedure TfpgBaseGrid.AfterConstruction;
begin
inherited AfterConstruction;
Updated;
end;
procedure TfpgBaseGrid.Update;
begin
if csUpdating in ComponentState then
Exit;
UpdateScrollBars;
RePaint;
end;
procedure TfpgBaseGrid.BeginUpdate;
begin
Inc(FUpdateCount);
Updating;
end;
procedure TfpgBaseGrid.EndUpdate;
begin
if FUpdateCount > 0 then
begin
Dec(FUpdateCount);
if FUpdateCount = 0 then
begin
Updated;
// RePaint;
Update;
end;
end;
end;
procedure TfpgBaseGrid.MouseToCell(X, Y: Integer; var ACol, ARow: Integer);
var
hh: integer;
cw: integer;
n: Integer;
cLeft: integer;
c: integer;
begin
if ShowHeader then
hh := FHeaderHeight+1
else
hh := 0;
ARow := FFirstRow + ((y - fpgStyle.GetControlFrameBorders.Top - hh) div FDefaultRowHeight);
if ARow > RowCount-1 then
ARow := RowCount-1;
cLeft := fpgStyle.GetControlFrameBorders.Left; // column starting point
if go_SmoothScroll in FOptions then
begin
if FHScrollBar.Visible then
Dec(cLeft, FHScrollBar.Position);
c := 0;
end
else
begin
c := FFirstCol;
end;
cw := 0;
for n := c to ColumnCount-1 do
begin
inc(cw, ColumnWidth[n]);
if cLeft+cw >= x then
begin
ACol := n;
Break;
end;
end;
end;
function TfpgBaseGrid.GetClientRect: TfpgRect;
var
rect: TRect;
begin
Result := inherited GetClientRect;
rect := fpgStyle.GetControlFrameBorders;
case BorderStyle of
// ebsNone: // nothing to do
ebsDefault: InflateRect(Result, -rect.Left, -rect.Top); { assuming borders are even on opposite sides }
ebsSingle: InflateRect(Result, -1, -1);
end;
end;
end.
|