summaryrefslogtreecommitdiffstats
path: root/cervisia/qttableview.cpp
blob: 02f8aea17d6bd1095ebd386971ff22e6f11150d9 (plain)
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
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
/**********************************************************************
** $Id$
**
** Implementation of QtTableView class
**
** Created : 941115
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file contains a class moved out of the TQt GUI Toolkit API. It
** may be used, distributed and modified without limitation.
**
**********************************************************************/

#include "qttableview.h"
#include "tqscrollbar.h"
#include "tqpainter.h"
#include "tqdrawutil.h"
#include <tqapplication.h>
#include <limits.h>

enum ScrollBarDirtyFlags {
    verGeometry	  = 0x01,
    verSteps	  = 0x02,
    verRange	  = 0x04,
    verValue	  = 0x08,
    horGeometry	  = 0x10,
    horSteps	  = 0x20,
    horRange	  = 0x40,
    horValue	  = 0x80,
    verMask	  = 0x0F,
    horMask	  = 0xF0
};


#define HSBEXT horizontalScrollBar()->sizeHint().height()
#define VSBEXT verticalScrollBar()->sizeHint().width()


class TQCornerSquare : public TQWidget		// internal class
{
public:
    TQCornerSquare( TQWidget *, const char* = 0 );
    void paintEvent( TQPaintEvent * );
};

TQCornerSquare::TQCornerSquare( TQWidget *parent, const char *name )
	: TQWidget( parent, name )
{
}

void TQCornerSquare::paintEvent( TQPaintEvent * )
{
}


// NOT REVISED
/*!
  \class QtTableView qttableview.h
  \brief The QtTableView class provides an abstract base for tables.

  \obsolete

  A table view consists of a number of abstract cells organized in rows
  and columns, and a visible part called a view. The cells are identified
  with a row index and a column index. The top-left cell is in row 0,
  column 0.

  The behavior of the widget can be finely tuned using
  setTableFlags(); a typical subclass will consist of little more than a
  call to setTableFlags(), some table content manipulation and an
  implementation of paintCell().  Subclasses that need cells with
  variable width or height must reimplement cellHeight() and/or
  cellWidth(). Use updateTableSize() to tell QtTableView when the
  width or height has changed.

  When you read this documentation, it is important to understand the
  distinctions among the four pixel coordinate systems involved.

  \list 1
  \i The \e cell coordinates.  (0,0) is the top-left corner of a cell.
  Cell coordinates are used by functions such as paintCell().

  \i The \e table coordinates.  (0,0) is the top-left corner of the cell at
  row 0 and column 0. These coordinates are absolute; that is, they are
  independent of what part of the table is visible at the moment. They are
  used by functions such as setXOffset() or maxYOffset().

  \i The \e widget coordinates. (0,0) is the top-left corner of the widget,
  \e including the frame.  They are used by functions such as repaint().

  \i The \e view coordinates.  (0,0) is the top-left corner of the view, \e
  excluding the frame.  This is the least-used coordinate system; it is used by
  functions such as viewWidth().  \endlist

  It is rather unfortunate that we have to use four different
  coordinate systems, but there was no alternative to provide a flexible and
  powerful base class.

  Note: The row,column indices are always given in that order,
  i.e., first the vertical (row), then the horizontal (column). This is
  the opposite order of all pixel operations, which take first the
  horizontal (x) and then the vertical (y).

  <img src=qtablevw-m.png> <img src=qtablevw-w.png>

  \warning the functions setNumRows(), setNumCols(), setCellHeight(),
  setCellWidth(), setTableFlags() and clearTableFlags() may cause
  virtual functions such as cellWidth() and cellHeight() to be called,
  even if autoUpdate() is FALSE.  This may cause errors if relevant
  state variables are not initialized.

  \warning Experience has shown that use of this widget tends to cause
  more bugs than expected and our analysis indicates that the widget's
  very flexibility is the problem.  If TQScrollView or TQListBox can
  easily be made to do the job you need, we recommend subclassing
  those widgets rather than QtTableView. In addition, TQScrollView makes
  it easy to have child widgets inside tables, which QtTableView
  doesn't support at all.

  \sa TQScrollView
  \link guibooks.html#fowler GUI Design Handbook: Table\endlink
*/


/*!
  Constructs a table view.  The \a parent, \a name and \f arguments
  are passed to the TQFrame constructor.

  The \link setTableFlags() table flags\endlink are all cleared (set to 0).
  Set \c Tbl_autoVScrollBar or \c Tbl_autoHScrollBar to get automatic scroll
  bars and \c Tbl_clipCellPainting to get safe clipping.

  The \link setCellHeight() cell height\endlink and \link setCellWidth()
  cell width\endlink are set to 0.

  Frame line shapes (TQFrame::HLink and TQFrame::VLine) are disallowed;
  see TQFrame::setFrameStyle().

  Note that the \a f argument is \e not \link setTableFlags() table
  flags \endlink but rather \link TQWidget::TQWidget() widget
  flags. \endlink

*/

QtTableView::QtTableView( TQWidget *parent, const char *name, WFlags f )
    : TQFrame( parent, name, f )
{
    nRows		 = nCols      = 0;	// zero rows/cols
    xCellOffs		 = yCellOffs  = 0;	// zero offset
    xCellDelta		 = yCellDelta = 0;	// zero cell offset
    xOffs		 = yOffs      = 0;	// zero total pixel offset
    cellH		 = cellW      = 0;	// user defined cell size
    tFlags		 = 0;
    vScrollBar		 = hScrollBar = 0;	// no scroll bars
    cornerSquare	 = 0;
    sbDirty		 = 0;
    eraseInPaint	 = FALSE;
    verSliding		 = FALSE;
    verSnappingOff	 = FALSE;
    horSliding		 = FALSE;
    horSnappingOff	 = FALSE;
    coveringCornerSquare = FALSE;
    inSbUpdate		 = FALSE;
}

/*!
  Destroys the table view.
*/

QtTableView::~QtTableView()
{
    delete vScrollBar;
    delete hScrollBar;
    delete cornerSquare;
}


/*!
  \internal
  Reimplements TQWidget::setBackgroundColor() for binary compatibility.
  \sa setPalette()
*/

void QtTableView::setBackgroundColor( const TQColor &c )
{
    TQWidget::setBackgroundColor( c );
}

/*!\reimp
*/

void QtTableView::setPalette( const TQPalette &p )
{
    TQWidget::setPalette( p );
}

/*!\reimp
*/

void QtTableView::show()
{
    showOrHideScrollBars();
    TQWidget::show();
}


/*!
  \overload void QtTableView::repaint( bool erase )
  Repaints the entire view.
*/

/*!
  Repaints the table view directly by calling paintEvent() directly
  unless updates are disabled.

  Erases the view area \a (x,y,w,h) if \a erase is TRUE. Parameters \a
  (x,y) are in \e widget coordinates.

  If \a w is negative, it is replaced with <code>width() - x</code>.
  If \a h is negative, it is replaced with <code>height() - y</code>.

  Doing a repaint() usually is faster than doing an update(), but
  calling update() many times in a row will generate a single paint
  event.

  At present, QtTableView is the only widget that reimplements \link
  TQWidget::repaint() repaint()\endlink.	 It does this because by
  clearing and then repainting one cell at at time, it can make the
  screen flicker less than it would otherwise.  */

void QtTableView::repaint( int x, int y, int w, int h, bool erase )
{
    if ( !isVisible() || testWState(WState_BlockUpdates) )
	return;
    if ( w < 0 )
	w = width()  - x;
    if ( h < 0 )
	h = height() - y;
    TQRect r( x, y, w, h );
    if ( r.isEmpty() )
	return; // nothing to do
    TQPaintEvent e( r );
    if ( erase && backgroundMode() != NoBackground )
	eraseInPaint = TRUE;			// erase when painting
    paintEvent( &e );
    eraseInPaint = FALSE;
}

/*!
  \overload void QtTableView::repaint( const TQRect &r, bool erase )
  Replaints rectangle \a r. If \a erase is TRUE draws the background
  using the palette's background.
*/


/*!
  \fn int QtTableView::numRows() const
  Returns the number of rows in the table.
  \sa numCols(), setNumRows()
*/

/*!
  Sets the number of rows of the table to \a rows (must be non-negative).
  Does not change topCell().

  The table repaints itself automatically if autoUpdate() is set.

  \sa numCols(), setNumCols(), numRows()
*/

void QtTableView::setNumRows( int rows )
{
    if ( rows < 0 ) {
#if defined(TQT_CHECK_RANGE)
	tqWarning( "QtTableView::setNumRows: (%s) Negative argument %d.",
		 name( "unnamed" ), rows );
#endif
	return;
    }
    if ( nRows == rows )
	return;

    if ( autoUpdate() && isVisible() ) {
	int oldLastVisible = lastRowVisible();
	int oldTopCell = topCell();
	nRows = rows;
	if ( autoUpdate() && isVisible() &&
	     ( oldLastVisible != lastRowVisible() || oldTopCell != topCell() ) )
		repaint( oldTopCell != topCell() );
    } else {
	// Be more careful - if destructing, bad things might happen.
	nRows = rows;
    }
    updateScrollBars( verRange );
    updateFrameSize();
}

/*!
  \fn int QtTableView::numCols() const
  Returns the number of columns in the table.
  \sa numRows(), setNumCols()
*/

/*!
  Sets the number of columns of the table to \a cols (must be non-negative).
  Does not change leftCell().

  The table repaints itself automatically if autoUpdate() is set.

  \sa numCols(), numRows(), setNumRows()
*/

void QtTableView::setNumCols( int cols )
{
    if ( cols < 0 ) {
#if defined(TQT_CHECK_RANGE)
	tqWarning( "QtTableView::setNumCols: (%s) Negative argument %d.",
		 name( "unnamed" ), cols );
#endif
	return;
    }
    if ( nCols == cols )
	return;
    int oldCols = nCols;
    nCols = cols;
    if ( autoUpdate() && isVisible() ) {
	int maxCol = lastColVisible();
	if ( maxCol >= oldCols || maxCol >= nCols )
	    repaint();
    }
    updateScrollBars( horRange );
    updateFrameSize();
}


/*!
  \fn int QtTableView::topCell() const
  Returns the index of the first row in the table that is visible in
  the view.  The index of the first row is 0.
  \sa leftCell(), setTopCell()
*/

/*!
  Scrolls the table so that \a row becomes the top row.
  The index of the very first row is 0.
  \sa setYOffset(), setTopLeftCell(), setLeftCell()
*/

void QtTableView::setTopCell( int row )
{
    setTopLeftCell( row, -1 );
    return;
}

/*!
  \fn int QtTableView::leftCell() const
  Returns the index of the first column in the table that is visible in
  the view.  The index of the very leftmost column is 0.
  \sa topCell(), setLeftCell()
*/

/*!
  Scrolls the table so that \a col becomes the leftmost
  column.  The index of the leftmost column is 0.
  \sa setXOffset(), setTopLeftCell(), setTopCell()
*/

void QtTableView::setLeftCell( int col )
{
    setTopLeftCell( -1, col );
    return;
}

/*!
  Scrolls the table so that the cell at row \a row and colum \a
  col becomes the top-left cell in the view.  The cell at the extreme
  top left of the table is at position (0,0).
  \sa setLeftCell(), setTopCell(), setOffset()
*/

void QtTableView::setTopLeftCell( int row, int col )
{
    int newX = xOffs;
    int newY = yOffs;

    if ( col >= 0 ) {
	if ( cellW ) {
	    newX = col*cellW;
	    if ( newX > maxXOffset() )
		newX = maxXOffset();
	} else {
	    newX = 0;
	    while ( col )
		newX += cellWidth( --col );   // optimize using current! ###
	}
    }
    if ( row >= 0 ) {
	if ( cellH ) {
	    newY = row*cellH;
	    if ( newY > maxYOffset() )
		newY = maxYOffset();
	} else {
	    newY = 0;
	    while ( row )
		newY += cellHeight( --row );   // optimize using current! ###
	}
    }
    setOffset( newX, newY );
}


/*!
  \fn int QtTableView::xOffset() const

  Returns the x coordinate in \e table coordinates of the pixel that is
  currently on the left edge of the view.

  \sa setXOffset(), yOffset(), leftCell() */

/*!
  Scrolls the table so that \a x becomes the leftmost pixel in the view.
  The \a x parameter is in \e table coordinates.

  The interaction with \link setTableFlags() Tbl_snapToHGrid
  \endlink is tricky.

  \sa xOffset(), setYOffset(), setOffset(), setLeftCell()
*/

void QtTableView::setXOffset( int x )
{
    setOffset( x, yOffset() );
}

/*!
  \fn int QtTableView::yOffset() const

  Returns the y coordinate in \e table coordinates of the pixel that is
  currently on the top edge of the view.

  \sa setYOffset(), xOffset(), topCell()
*/


/*!
  Scrolls the table so that \a y becomes the top pixel in the view.
  The \a y parameter is in \e table coordinates.

  The interaction with \link setTableFlags() Tbl_snapToVGrid
  \endlink is tricky.

  \sa yOffset(), setXOffset(), setOffset(), setTopCell()
*/

void QtTableView::setYOffset( int y )
{
    setOffset( xOffset(), y );
}

/*!
  Scrolls the table so that \a (x,y) becomes the top-left pixel
  in the view. Parameters \a (x,y) are in \e table coordinates.

  The interaction with \link setTableFlags() Tbl_snapTo*Grid \endlink
  is tricky.  If \a updateScrBars is TRUE, the scroll bars are
  updated.

  \sa xOffset(), yOffset(), setXOffset(), setYOffset(), setTopLeftCell()
*/

void QtTableView::setOffset( int x, int y, bool updateScrBars )
{
    if ( (!testTableFlags(Tbl_snapToHGrid) || xCellDelta == 0) &&
	 (!testTableFlags(Tbl_snapToVGrid) || yCellDelta == 0) &&
	 (x == xOffs && y == yOffs) )
	return;

    if ( x < 0 )
	x = 0;
    if ( y < 0 )
	y = 0;

    if ( cellW ) {
	if ( x > maxXOffset() )
	    x = maxXOffset();
	xCellOffs = x / cellW;
	if ( !testTableFlags(Tbl_snapToHGrid) ) {
	    xCellDelta	= (short)(x % cellW);
	} else {
	    x		= xCellOffs*cellW;
	    xCellDelta	= 0;
	}
    } else {
	int xn=0, xcd=0, col = 0;
	while ( col < nCols-1 && x >= xn+(xcd=cellWidth(col)) ) {
	    xn += xcd;
	    col++;
	}
	xCellOffs = col;
	if ( testTableFlags(Tbl_snapToHGrid) ) {
	    xCellDelta = 0;
	    x = xn;
	} else {
	    xCellDelta = (short)(x-xn);
	}
    }
    if ( cellH ) {
	if ( y > maxYOffset() )
	    y = maxYOffset();
	yCellOffs = y / cellH;
	if ( !testTableFlags(Tbl_snapToVGrid) ) {
	    yCellDelta	= (short)(y % cellH);
	} else {
	    y		= yCellOffs*cellH;
	    yCellDelta	= 0;
	}
    } else {
	int yn=0, yrd=0, row=0;
	while ( row < nRows-1 && y >= yn+(yrd=cellHeight(row)) ) {
	    yn += yrd;
	    row++;
	}
	yCellOffs = row;
	if ( testTableFlags(Tbl_snapToVGrid) ) {
	    yCellDelta = 0;
	    y = yn;
	} else {
	    yCellDelta = (short)(y-yn);
	}
    }
    int dx = (x - xOffs);
    int dy = (y - yOffs);
    xOffs = x;
    yOffs = y;
    if ( autoUpdate() && isVisible() )
	scroll( dx, dy );
    if ( updateScrBars )
	updateScrollBars( verValue | horValue );
}


/*!
  \overload int QtTableView::cellWidth() const

  Returns the column width in pixels.	Returns 0 if the columns have
  variable widths.

  \sa setCellWidth(), cellHeight()
*/

/*!
  Returns the width of column \a col in pixels.

  This function is virtual and must be reimplemented by subclasses that
  have variable cell widths. Note that if the total table width
  changes, updateTableSize() must be called.

  \sa setCellWidth(), cellHeight(), totalWidth(), updateTableSize()
*/

int QtTableView::cellWidth( int )
{
    return cellW;
}


/*!
  Sets the width in pixels of the table cells to \a cellWidth.

  Setting it to 0 means that the column width is variable.  When
  set to 0 (this is the default) QtTableView calls the virtual function
  cellWidth() to get the width.

  \sa cellWidth(), setCellHeight(), totalWidth(), numCols()
*/

void QtTableView::setCellWidth( int cellWidth )
{
    if ( cellW == cellWidth )
	return;
#if defined(TQT_CHECK_RANGE)
    if ( cellWidth < 0 || cellWidth > SHRT_MAX ) {
	tqWarning( "QtTableView::setCellWidth: (%s) Argument out of range (%d)",
		 name( "unnamed" ), cellWidth );
	return;
    }
#endif
    cellW = (short)cellWidth;

    updateScrollBars( horSteps | horRange );
    if ( autoUpdate() && isVisible() )
	repaint();

}

/*!
  \overload int QtTableView::cellHeight() const

  Returns the row height, in pixels.  Returns 0 if the rows have
  variable heights.

  \sa setCellHeight(), cellWidth()
*/


/*!
  Returns the height of row \a row in pixels.

  This function is virtual and must be reimplemented by subclasses that
  have variable cell heights.  Note that if the total table height
  changes, updateTableSize() must be called.

  \sa setCellHeight(), cellWidth(), totalHeight()
*/

int QtTableView::cellHeight( int )
{
    return cellH;
}

/*!
  Sets the height in pixels of the table cells to \a cellHeight.

  Setting it to 0 means that the row height is variable.  When set
  to 0 (this is the default), QtTableView calls the virtual function
  cellHeight() to get the height.

  \sa cellHeight(), setCellWidth(), totalHeight(), numRows()
*/

void QtTableView::setCellHeight( int cellHeight )
{
    if ( cellH == cellHeight )
	return;
#if defined(TQT_CHECK_RANGE)
    if ( cellHeight < 0 || cellHeight > SHRT_MAX ) {
	tqWarning( "QtTableView::setCellHeight: (%s) Argument out of range (%d)",
		 name( "unnamed" ), cellHeight );
	return;
    }
#endif
    cellH = (short)cellHeight;
    if ( autoUpdate() && isVisible() )
	repaint();
    updateScrollBars( verSteps | verRange );
}


/*!
  Returns the total width of the table in pixels.

  This function is virtual and should be reimplemented by subclasses that
  have variable cell widths and a non-trivial cellWidth() function, or a
  large number of columns in the table.

  The default implementation may be slow for very wide tables.

  \sa cellWidth(), totalHeight() */

int QtTableView::totalWidth()
{
    if ( cellW ) {
	return cellW*nCols;
    } else {
	int tw = 0;
	for( int i = 0 ; i < nCols ; i++ )
	    tw += cellWidth( i );
	return tw;
    }
}

/*!
  Returns the total height of the table in pixels.

  This function is virtual and should be reimplemented by subclasses that
  have variable cell heights and a non-trivial cellHeight() function, or a
  large number of rows in the table.

  The default implementation may be slow for very tall tables.

  \sa cellHeight(), totalWidth()
*/

int QtTableView::totalHeight()
{
    if ( cellH ) {
	return cellH*nRows;
    } else {
	int th = 0;
	for( int i = 0 ; i < nRows ; i++ )
	    th += cellHeight( i );
	return th;
    }
}


/*!
  \fn uint QtTableView::tableFlags() const

  Returns the union of the table flags that are currently set.

  \sa setTableFlags(), clearTableFlags(), testTableFlags()
*/

/*!
  \fn bool QtTableView::testTableFlags( uint f ) const

  Returns TRUE if any of the table flags in \a f are currently set,
  otherwise FALSE.

  \sa setTableFlags(), clearTableFlags(), tableFlags()
*/

/*!
  Sets the table flags to \a f.

  If a flag setting changes the appearance of the table, the table is
  repainted if - and only if - autoUpdate() is TRUE.

  The table flags are mostly single bits, though there are some multibit
  flags for convenience. Here is a complete list:

  <dl compact>
  <dt> Tbl_vScrollBar <dd> - The table has a vertical scroll bar.
  <dt> Tbl_hScrollBar <dd> - The table has a horizontal scroll bar.
  <dt> Tbl_autoVScrollBar <dd> - The table has a vertical scroll bar if
  - and only if - the table is taller than the view.
  <dt> Tbl_autoHScrollBar <dd> The table has a horizontal scroll bar if
  - and only if - the table is wider than the view.
  <dt> Tbl_autoScrollBars <dd> - The union of the previous two flags.
  <dt> Tbl_clipCellPainting <dd> - The table uses TQPainter::setClipRect() to
  make sure that paintCell() will not draw outside the cell
  boundaries.
  <dt> Tbl_cutCellsV <dd> - The table will never show part of a
  cell at the bottom of the table; if there is not space for all of
  a cell, the space is left blank.
  <dt> Tbl_cutCellsH <dd> - The table will never show part of a
  cell at the right side of the table; if there is not space for all of
  a cell, the space is left blank.
  <dt> Tbl_cutCells <dd> - The union of the previous two flags.
  <dt> Tbl_scrollLastHCell <dd> - When the user scrolls horizontally,
  let him/her scroll the last cell left until it is at the left
  edge of the view.  If this flag is not set, the user can only scroll
  to the point where the last cell is completely visible.
  <dt> Tbl_scrollLastVCell <dd> - When the user scrolls vertically, let
  him/her scroll the last cell up until it is at the top edge of
  the view.  If this flag is not set, the user can only scroll to the
  point where the last cell is completely visible.
  <dt> Tbl_scrollLastCell <dd> - The union of the previous two flags.
  <dt> Tbl_smoothHScrolling <dd> - The table scrolls as smoothly as
  possible when the user scrolls horizontally. When this flag is not
  set, scrolling is done one cell at a time.
  <dt> Tbl_smoothVScrolling <dd> - The table scrolls as smoothly as
  possible when scrolling vertically. When this flag is not set,
  scrolling is done one cell at a time.
  <dt> Tbl_smoothScrolling <dd> - The union of the previous two flags.
  <dt> Tbl_snapToHGrid <dd> - Except when the user is actually scrolling,
  the leftmost column shown snaps to the leftmost edge of the view.
  <dt> Tbl_snapToVGrid <dd> - Except when the user is actually
  scrolling, the top row snaps to the top edge of the view.
  <dt> Tbl_snapToGrid <dd> - The union of the previous two flags.
  </dl>

  You can specify more than one flag at a time using bitwise OR.

  Example:
  \code
    setTableFlags( Tbl_smoothScrolling | Tbl_autoScrollBars );
  \endcode

  \warning The cutCells options (\c Tbl_cutCells, \c Tbl_cutCellsH and
  Tbl_cutCellsV) may cause painting problems when scrollbars are
  enabled. Do not combine cutCells and scrollbars.


  \sa clearTableFlags(), testTableFlags(), tableFlags()
*/

void QtTableView::setTableFlags( uint f )
{
    f = (f ^ tFlags) & f;			// clear flags already set
    tFlags |= f;

    bool updateOn = autoUpdate();
    setAutoUpdate( FALSE );

    uint repaintMask = Tbl_cutCellsV | Tbl_cutCellsH;

    if ( f & Tbl_vScrollBar ) {
	setVerScrollBar( TRUE );
    }
    if ( f & Tbl_hScrollBar ) {
	setHorScrollBar( TRUE );
    }
    if ( f & Tbl_autoVScrollBar ) {
	updateScrollBars( verRange );
    }
    if ( f & Tbl_autoHScrollBar ) {
	updateScrollBars( horRange );
    }
    if ( f & Tbl_scrollLastHCell ) {
	updateScrollBars( horRange );
    }
    if ( f & Tbl_scrollLastVCell ) {
	updateScrollBars( verRange );
    }
    if ( f & Tbl_snapToHGrid ) {
	updateScrollBars( horRange );
    }
    if ( f & Tbl_snapToVGrid ) {
	updateScrollBars( verRange );
    }
    if ( f & Tbl_snapToGrid ) {			// Note: checks for 2 flags
	if ( (f & Tbl_snapToHGrid) != 0 && xCellDelta != 0 || //have to scroll?
	     (f & Tbl_snapToVGrid) != 0 && yCellDelta != 0 ) {
	    snapToGrid( (f & Tbl_snapToHGrid) != 0,	// do snapping
			(f & Tbl_snapToVGrid) != 0 );
	    repaintMask |= Tbl_snapToGrid;	// repaint table
	}
    }

    if ( updateOn ) {
	setAutoUpdate( TRUE );
	updateScrollBars();
	if ( isVisible() && (f & repaintMask) )
	    repaint();
    }

}

/*!
  Clears the \link setTableFlags() table flags\endlink that are set
  in \a f.

  Example (clears a single flag):
  \code
    clearTableFlags( Tbl_snapToGrid );
  \endcode

  The default argument clears all flags.

  \sa setTableFlags(), testTableFlags(), tableFlags()
*/

void QtTableView::clearTableFlags( uint f )
{
    f = (f ^ ~tFlags) & f;		// clear flags that are already 0
    tFlags &= ~f;

    bool updateOn = autoUpdate();
    setAutoUpdate( FALSE );

    uint repaintMask = Tbl_cutCellsV | Tbl_cutCellsH;

    if ( f & Tbl_vScrollBar ) {
	setVerScrollBar( FALSE );
    }
    if ( f & Tbl_hScrollBar ) {
	setHorScrollBar( FALSE );
    }
    if ( f & Tbl_scrollLastHCell ) {
	int maxX = maxXOffset();
	if ( xOffs > maxX ) {
	    setOffset( maxX, yOffs );
	    repaintMask |= Tbl_scrollLastHCell;
	}
	updateScrollBars( horRange );
    }
    if ( f & Tbl_scrollLastVCell ) {
	int maxY = maxYOffset();
	if ( yOffs > maxY ) {
	    setOffset( xOffs, maxY );
	    repaintMask |= Tbl_scrollLastVCell;
	}
	updateScrollBars( verRange );
    }
    if ( f & Tbl_smoothScrolling ) {	      // Note: checks for 2 flags
	if ((f & Tbl_smoothHScrolling) != 0 && xCellDelta != 0 ||//must scroll?
	    (f & Tbl_smoothVScrolling) != 0 && yCellDelta != 0 ) {
	    snapToGrid( (f & Tbl_smoothHScrolling) != 0,      // do snapping
			(f & Tbl_smoothVScrolling) != 0 );
	    repaintMask |= Tbl_smoothScrolling;		     // repaint table
	}
    }
    if ( f & Tbl_snapToHGrid ) {
	updateScrollBars( horRange );
    }
    if ( f & Tbl_snapToVGrid ) {
	updateScrollBars( verRange );
    }
    if ( updateOn ) {
	setAutoUpdate( TRUE );
	updateScrollBars();	     // returns immediately if nothing to do
	if ( isVisible() && (f & repaintMask) )
	    repaint();
    }

}


/*!
  \fn bool QtTableView::autoUpdate() const

  Returns TRUE if the view updates itself automatically whenever it
  is changed in some way.

  \sa setAutoUpdate()
*/

/*!
  Sets the auto-update option of the table view to \a enable.

  If \a enable is TRUE (this is the default), the view updates itself
  automatically whenever it has changed in some way (for example, when a
  \link setTableFlags() flag\endlink is changed).

  If \a enable is FALSE, the view does NOT repaint itself or update
  its internal state variables when it is changed.  This can be
  useful to avoid flicker during large changes and is singularly
  useless otherwise. Disable auto-update, do the changes, re-enable
  auto-update and call repaint().

  \warning Do not leave the view in this state for a long time
  (i.e., between events). If, for example, the user interacts with the
  view when auto-update is off, strange things can happen.

  Setting auto-update to TRUE does not repaint the view; you must call
  repaint() to do this.

  \sa autoUpdate(), repaint()
*/

void QtTableView::setAutoUpdate( bool enable )
{
    if ( isUpdatesEnabled() == enable )
	return;
    setUpdatesEnabled( enable );
    if ( enable ) {
	showOrHideScrollBars();
	updateScrollBars();
    }
}


/*!
  Repaints the cell at row \a row, column \a col if it is inside the view.

  If \a erase is TRUE, the relevant part of the view is cleared to the
  background color/pixmap before the contents are repainted.

  \sa isVisible()
*/

void QtTableView::updateCell( int row, int col, bool erase )
{
    int xPos, yPos;
    if ( !colXPos( col, &xPos ) )
	return;
    if ( !rowYPos( row, &yPos ) )
	return;
    TQRect uR = TQRect( xPos, yPos,
		      cellW ? cellW : cellWidth(col),
		      cellH ? cellH : cellHeight(row) );
    repaint( uR.intersect(viewRect()), erase );
}


/*!
  \fn TQRect QtTableView::cellUpdateRect() const

  This function should be called only from the paintCell() function in
  subclasses. It returns the portion of a cell that actually needs to be
  updated in \e cell coordinates. This is useful only for non-trivial
  paintCell().

*/

/*!
  Returns the rectangle that is the actual table, excluding any
  frame, in \e widget coordinates.
*/

TQRect QtTableView::viewRect() const
{
    return TQRect( frameWidth(), frameWidth(), viewWidth(), viewHeight() );
}


/*!
  Returns the index of the last (bottom) row in the view.
  The index of the first row is 0.

  If no rows are visible it returns -1.	 This can happen if the
  view is too small for the first row and Tbl_cutCellsV is set.

  \sa lastColVisible()
*/

int QtTableView::lastRowVisible() const
{
    int cellMaxY;
    int row = findRawRow( maxViewY(), &cellMaxY );
    if ( row == -1 || row >= nRows ) {		// maxViewY() past end?
	row = nRows - 1;			// yes: return last row
    } else {
	if ( testTableFlags(Tbl_cutCellsV) && cellMaxY > maxViewY() ) {
	    if ( row == yCellOffs )		// cut by right margin?
		return -1;			// yes, nothing in the view
	    else
	       row = row - 1;			// cut by margin, one back
	}
    }
    return row;
}

/*!
  Returns the index of the last (right) column in the view.
  The index of the first column is 0.

  If no columns are visible it returns -1.  This can happen if the
  view is too narrow for the first column and Tbl_cutCellsH is set.

  \sa lastRowVisible()
*/

int QtTableView::lastColVisible() const
{
    int cellMaxX;
    int col = findRawCol( maxViewX(), &cellMaxX );
    if ( col == -1 || col >= nCols ) {		// maxViewX() past end?
	col = nCols - 1;			// yes: return last col
    } else {
	if ( testTableFlags(Tbl_cutCellsH) && cellMaxX > maxViewX() ) {
	    if ( col == xCellOffs )		// cut by bottom margin?
		return -1;			// yes, nothing in the view
	    else
	       col = col - 1;			// cell by margin, one back
	}
    }
    return col;
}

/*!
  Returns TRUE if \a row is at least partially visible.
  \sa colIsVisible()
*/

bool QtTableView::rowIsVisible( int row ) const
{
    return rowYPos( row, 0 );
}

/*!
  Returns TRUE if \a col is at least partially visible.
  \sa rowIsVisible()
*/

bool QtTableView::colIsVisible( int col ) const
{
    return colXPos( col, 0 );
}


/*!
  \internal
  Called when both scroll bars are active at the same time. Covers the
  bottom left corner between the two scroll bars with an empty widget.
*/

void QtTableView::coverCornerSquare( bool enable )
{
    coveringCornerSquare = enable;
    if ( !cornerSquare && enable ) {
	cornerSquare = new TQCornerSquare( this );
	TQ_CHECK_PTR( cornerSquare );
	cornerSquare->setGeometry( maxViewX() + frameWidth() + 1,
				   maxViewY() + frameWidth() + 1,
                                   VSBEXT,
                                 HSBEXT);
    }
    if ( autoUpdate() && cornerSquare ) {
	if ( enable )
	    cornerSquare->show();
	else
	    cornerSquare->hide();
    }
}


/*!
  \internal
  Scroll the view to a position such that:

  If \a horizontal is TRUE, the leftmost column shown fits snugly
  with the left edge of the view.

  If \a vertical is TRUE, the top row shown fits snugly with the top
  of the view.

  You can achieve the same effect automatically by setting any of the
  \link setTableFlags() Tbl_snapTo*Grid \endlink table flags.
*/

void QtTableView::snapToGrid( bool horizontal, bool vertical )
{
    int newXCell = -1;
    int newYCell = -1;
    if ( horizontal && xCellDelta != 0 ) {
	int w = cellW ? cellW : cellWidth( xCellOffs );
	if ( xCellDelta >= w/2 )
	    newXCell = xCellOffs + 1;
	else
	    newXCell = xCellOffs;
    }
    if ( vertical && yCellDelta != 0 ) {
	int h = cellH ? cellH : cellHeight( yCellOffs );
	if ( yCellDelta >= h/2 )
	    newYCell = yCellOffs + 1;
	else
	    newYCell = yCellOffs;
    }
    setTopLeftCell( newYCell, newXCell );  //row,column
}

/*!
  \internal
  This internal slot is connected to the horizontal scroll bar's
  TQScrollBar::valueChanged() signal.

  Moves the table horizontally to offset \a val without updating the
  scroll bar.
*/

void QtTableView::horSbValue( int val )
{
    if ( horSliding ) {
	horSliding = FALSE;
	if ( horSnappingOff ) {
	    horSnappingOff = FALSE;
	    tFlags |= Tbl_snapToHGrid;
	}
    }
    setOffset( val, yOffs, FALSE );
}

/*!
  \internal
  This internal slot is connected to the horizontal scroll bar's
  TQScrollBar::sliderMoved() signal.

  Scrolls the table smoothly horizontally even if \c Tbl_snapToHGrid is set.
*/

void QtTableView::horSbSliding( int val )
{
    if ( testTableFlags(Tbl_snapToHGrid) &&
	 testTableFlags(Tbl_smoothHScrolling) ) {
	tFlags &= ~Tbl_snapToHGrid;	// turn off snapping while sliding
	setOffset( val, yOffs, FALSE );
	tFlags |= Tbl_snapToHGrid;	// turn on snapping again
    } else {
	setOffset( val, yOffs, FALSE );
    }
}

/*!
  \internal
  This internal slot is connected to the horizontal scroll bar's
  TQScrollBar::sliderReleased() signal.
*/

void QtTableView::horSbSlidingDone( )
{
    if ( testTableFlags(Tbl_snapToHGrid) &&
	 testTableFlags(Tbl_smoothHScrolling) )
	snapToGrid( TRUE, FALSE );
}

/*!
  \internal
  This internal slot is connected to the vertical scroll bar's
  TQScrollBar::valueChanged() signal.

  Moves the table vertically to offset \a val without updating the
  scroll bar.
*/

void QtTableView::verSbValue( int val )
{
    if ( verSliding ) {
	verSliding = FALSE;
	if ( verSnappingOff ) {
	    verSnappingOff = FALSE;
	    tFlags |= Tbl_snapToVGrid;
	}
    }
    setOffset( xOffs, val, FALSE );
}

/*!
  \internal
  This internal slot is connected to the vertical scroll bar's
  TQScrollBar::sliderMoved() signal.

  Scrolls the table smoothly vertically even if \c Tbl_snapToVGrid is set.
*/

void QtTableView::verSbSliding( int val )
{
    if ( testTableFlags(Tbl_snapToVGrid) &&
	 testTableFlags(Tbl_smoothVScrolling) ) {
	tFlags &= ~Tbl_snapToVGrid;	// turn off snapping while sliding
	setOffset( xOffs, val, FALSE );
	tFlags |= Tbl_snapToVGrid;	// turn on snapping again
    } else {
	setOffset( xOffs, val, FALSE );
    }
}

/*!
  \internal
  This internal slot is connected to the vertical scroll bar's
  TQScrollBar::sliderReleased() signal.
*/

void QtTableView::verSbSlidingDone( )
{
    if ( testTableFlags(Tbl_snapToVGrid) &&
	 testTableFlags(Tbl_smoothVScrolling) )
	snapToGrid( FALSE, TRUE );
}


/*!
  This virtual function is called before painting of table cells
  is started. It can be reimplemented by subclasses that want to
  to set up the painter in a special way and that do not want to
  do so for each cell.
*/

void QtTableView::setupPainter( TQPainter * )
{
}

/*!
  \fn void QtTableView::paintCell( TQPainter *p, int row, int col )

  This pure virtual function is called to paint the single cell at \a
  (row,col) using \a p, which is open when paintCell() is called and
  must remain open.

  The coordinate system is \link TQPainter::translate() translated \endlink
  so that the origin is at the top-left corner of the cell to be
  painted, i.e. \e cell coordinates.  Do not scale or shear the coordinate
  system (or if you do, restore the transformation matrix before you
  return).

  The painter is not clipped by default and for maximum efficiency. For safety,
  call setTableFlags(Tbl_clipCellPainting) to enable clipping.

  \sa paintEvent(), setTableFlags() */


/*!
  Handles paint events, \a e, for the table view.

  Calls paintCell() for the cells that needs to be repainted.
*/

void QtTableView::paintEvent( TQPaintEvent *e )
{
    TQRect updateR = e->rect();			// update rectangle
    if ( sbDirty ) {
	bool e = eraseInPaint;
	updateScrollBars();
	eraseInPaint = e;
    }

    TQPainter paint( this );

    if ( !contentsRect().contains( updateR, TRUE  ) ) {// update frame ?
	drawFrame( &paint );
	if ( updateR.left() < frameWidth() ) 		//###
	    updateR.setLeft( frameWidth() );
	if ( updateR.top() < frameWidth() )
	    updateR.setTop( frameWidth() );
    }

    int maxWX = maxViewX();
    int maxWY = maxViewY();
    if ( updateR.right() > maxWX )
	updateR.setRight( maxWX );
    if ( updateR.bottom() > maxWY )
	updateR.setBottom( maxWY );

    setupPainter( &paint );			// prepare for painting table

    int firstRow = findRow( updateR.y() );
    int firstCol = findCol( updateR.x() );
    int	 xStart, yStart;
    if ( !colXPos( firstCol, &xStart ) || !rowYPos( firstRow, &yStart ) ) {
	paint.eraseRect( updateR ); // erase area outside cells but in view
	return;
    }
    int	  maxX	= updateR.right();
    int	  maxY	= updateR.bottom();
    int	  row	= firstRow;
    int	  col;
    int	  yPos	= yStart;
    int	  xPos = maxX+1; // in case the while() is empty
    int	  nextX;
    int	  nextY;
    TQRect winR = viewRect();
    TQRect cellR;
    TQRect cellUR;
#ifndef TQT_NO_TRANSFORMATIONS
    TQWMatrix matrix;
#endif

    while ( yPos <= maxY && row < nRows ) {
	nextY = yPos + (cellH ? cellH : cellHeight( row ));
	if ( testTableFlags( Tbl_cutCellsV ) && nextY > ( maxWY + 1 ) )
	    break;
	col  = firstCol;
	xPos = xStart;
	while ( xPos <= maxX && col < nCols ) {
	    nextX = xPos + (cellW ? cellW : cellWidth( col ));
	    if ( testTableFlags( Tbl_cutCellsH ) && nextX > ( maxWX + 1 ) )
		break;

	    cellR.setRect( xPos, yPos, cellW ? cellW : cellWidth(col),
				       cellH ? cellH : cellHeight(row) );
	    cellUR = cellR.intersect( updateR );
	    if ( cellUR.isValid() ) {
		cellUpdateR = cellUR;
		cellUpdateR.moveBy( -xPos, -yPos ); // cell coordinates
		if ( eraseInPaint )
		    paint.eraseRect( cellUR );

#ifndef TQT_NO_TRANSFORMATIONS
		matrix.translate( xPos, yPos );
		paint.setWorldMatrix( matrix );
		if ( testTableFlags(Tbl_clipCellPainting) ||
		     frameWidth() > 0 && !winR.contains( cellR ) ) { //##arnt
		    paint.setClipRect( cellUR );
		    paintCell( &paint, row, col );
		    paint.setClipping( FALSE );
		} else {
		    paintCell( &paint, row, col );
		}
		matrix.reset();
		paint.setWorldMatrix( matrix );
#else
		paint.translate( xPos, yPos );
		if ( testTableFlags(Tbl_clipCellPainting) ||
		     frameWidth() > 0 && !winR.contains( cellR ) ) { //##arnt
		    paint.setClipRect( cellUR );
		    paintCell( &paint, row, col );
		    paint.setClipping( FALSE );
		} else {
		    paintCell( &paint, row, col );
		}
		paint.translate( -xPos, -yPos );
#endif
	    }
	    col++;
	    xPos = nextX;
	}
	row++;
	yPos = nextY;
    }

    // while painting we have to erase any areas in the view that
    // are not covered by cells but are covered by the paint event
    // rectangle these must be erased. We know that xPos is the last
    // x pixel updated + 1 and that yPos is the last y pixel updated + 1.

    // Note that this needs to be done regardless whether we do
    // eraseInPaint or not. Reason: a subclass may implement
    // flicker-freeness and encourage the use of repaint(FALSE).
    // The subclass, however, cannot draw all pixels, just those
    // inside the cells. So QtTableView is reponsible for all pixels
    // outside the cells.

    TQRect viewR = viewRect();
    const TQColorGroup g = colorGroup();

    if ( xPos <= maxX ) {
	TQRect r = viewR;
	r.setLeft( xPos );
	r.setBottom( yPos<maxY?yPos:maxY );
	if ( inherits( "TQMultiLineEdit" ) )
	    paint.fillRect( r.intersect( updateR ), g.base() );
	else
	    paint.eraseRect( r.intersect( updateR ) );
    }
    if ( yPos <= maxY ) {
	TQRect r = viewR;
	r.setTop( yPos );
	if ( inherits( "TQMultiLineEdit" ) )
	    paint.fillRect( r.intersect( updateR ), g.base() );
	else
	    paint.eraseRect( r.intersect( updateR ) );
    }
}

/*!\reimp
*/
void QtTableView::resizeEvent( TQResizeEvent * )
{
    updateScrollBars( horValue | verValue | horSteps | horGeometry | horRange |
		      verSteps | verGeometry | verRange );
    showOrHideScrollBars();
    updateFrameSize();
    int maxX = TQMIN( xOffs, maxXOffset() );			// ### can be slow
    int maxY = TQMIN( yOffs, maxYOffset() );
    setOffset( maxX, maxY );
}

void QtTableView::wheelEvent( TQWheelEvent * e )
{
    if( e->orientation() ==Qt::Vertical && vScrollBar && vScrollBar->isVisible() )
        TQApplication::sendEvent( vScrollBar, e );
}

/*!
  Redraws all visible cells in the table view.
*/

void QtTableView::updateView()
{
    repaint( viewRect() );
}

/*!
  Returns a pointer to the vertical scroll bar mainly so you can
  connect() to its signals.  Note that the scroll bar works in pixel
  values; use findRow() to translate to cell numbers.
*/

TQScrollBar *QtTableView::verticalScrollBar() const
{
    QtTableView *that = (QtTableView*)this; // semantic const
    if ( !vScrollBar ) {
	TQScrollBar *sb = new TQScrollBar( Qt::Vertical, that );
#ifndef TQT_NO_CURSOR
	sb->setCursor( arrowCursor );
#endif
        sb->resize( sb->sizeHint() ); // height is irrelevant
	TQ_CHECK_PTR(sb);
	sb->setTracking( FALSE );
	sb->setFocusPolicy( TQ_NoFocus );
	connect( sb, TQT_SIGNAL(valueChanged(int)),
		 TQT_SLOT(verSbValue(int)));
	connect( sb, TQT_SIGNAL(sliderMoved(int)),
		 TQT_SLOT(verSbSliding(int)));
	connect( sb, TQT_SIGNAL(sliderReleased()),
		 TQT_SLOT(verSbSlidingDone()));
	sb->hide();
	that->vScrollBar = sb;
	return sb;
    }
    return vScrollBar;
}

/*!
  Returns a pointer to the horizontal scroll bar mainly so you can
  connect() to its signals. Note that the scroll bar works in pixel
  values; use findCol() to translate to cell numbers.
*/

TQScrollBar *QtTableView::horizontalScrollBar() const
{
    QtTableView *that = (QtTableView*)this; // semantic const
    if ( !hScrollBar ) {
	TQScrollBar *sb = new TQScrollBar( Qt::Horizontal, that );
#ifndef TQT_NO_CURSOR
	sb->setCursor( arrowCursor );
#endif
	sb->resize( sb->sizeHint() ); // width is irrelevant
	sb->setFocusPolicy( TQ_NoFocus );
	TQ_CHECK_PTR(sb);
	sb->setTracking( FALSE );
	connect( sb, TQT_SIGNAL(valueChanged(int)),
		 TQT_SLOT(horSbValue(int)));
	connect( sb, TQT_SIGNAL(sliderMoved(int)),
		 TQT_SLOT(horSbSliding(int)));
	connect( sb, TQT_SIGNAL(sliderReleased()),
		 TQT_SLOT(horSbSlidingDone()));
	sb->hide();
	that->hScrollBar = sb;
	return sb;
    }
    return hScrollBar;
}

/*!
  Enables or disables the horizontal scroll bar, as required by
  setAutoUpdate() and the \link setTableFlags() table flags\endlink.
*/

void QtTableView::setHorScrollBar( bool on, bool update )
{
    if ( on ) {
	tFlags |= Tbl_hScrollBar;
	horizontalScrollBar(); // created
	if ( update )
	    updateScrollBars( horMask | verMask );
	else
	    sbDirty = sbDirty | (horMask | verMask);
	if ( testTableFlags( Tbl_vScrollBar ) )
	    coverCornerSquare( TRUE );
	if ( autoUpdate() )
	    sbDirty = sbDirty | horMask;
    } else {
	tFlags &= ~Tbl_hScrollBar;
	if ( !hScrollBar )
	    return;
	coverCornerSquare( FALSE );
	bool hideScrollBar = autoUpdate() && hScrollBar->isVisible();
	if ( hideScrollBar )
	    hScrollBar->hide();
	if ( update )
	    updateScrollBars( verMask );
	else
	    sbDirty = sbDirty | verMask;
	if ( hideScrollBar && isVisible() )
	    repaint( hScrollBar->x(), hScrollBar->y(),
		     width() - hScrollBar->x(), hScrollBar->height() );
    }
    if ( update )
	updateFrameSize();
}


/*!
  Enables or disables the vertical scroll bar, as required by
  setAutoUpdate() and the \link setTableFlags() table flags\endlink.
*/

void QtTableView::setVerScrollBar( bool on, bool update )
{
    if ( on ) {
	tFlags |= Tbl_vScrollBar;
	verticalScrollBar(); // created
	if ( update )
	    updateScrollBars( verMask | horMask );
	else
	    sbDirty = sbDirty | (horMask | verMask);
	if ( testTableFlags( Tbl_hScrollBar ) )
	    coverCornerSquare( TRUE );
	if ( autoUpdate() )
	    sbDirty = sbDirty | verMask;
    } else {
	tFlags &= ~Tbl_vScrollBar;
	if ( !vScrollBar )
	    return;
	coverCornerSquare( FALSE );
	bool hideScrollBar = autoUpdate() && vScrollBar->isVisible();
	if ( hideScrollBar )
	    vScrollBar->hide();
	if ( update )
	    updateScrollBars( horMask );
	else
	    sbDirty = sbDirty | horMask;
	if ( hideScrollBar && isVisible() )
	    repaint( vScrollBar->x(), vScrollBar->y(),
		     vScrollBar->width(), height() - vScrollBar->y() );
    }
    if ( update )
	updateFrameSize();
}




int QtTableView::findRawRow( int yPos, int *cellMaxY, int *cellMinY,
			    bool goOutsideView ) const
{
    int r = -1;
    if ( nRows == 0 )
	return r;
    if ( goOutsideView || yPos >= minViewY() && yPos <= maxViewY() ) {
	if ( yPos < minViewY() ) {
#if defined(TQT_CHECK_RANGE)
	    tqWarning( "QtTableView::findRawRow: (%s) internal error: "
		     "yPos < minViewY() && goOutsideView "
		     "not supported. (%d,%d)",
		     name( "unnamed" ), yPos, yOffs );
#endif
	    return -1;
	}
	if ( cellH ) {				     // uniform cell height
	    r = (yPos - minViewY() + yCellDelta)/cellH; // cell offs from top
	    if ( cellMaxY )
		*cellMaxY = (r + 1)*cellH + minViewY() - yCellDelta - 1;
	    if ( cellMinY )
		*cellMinY = r*cellH + minViewY() - yCellDelta;
	    r += yCellOffs;			     // absolute cell index
	} else {				     // variable cell height
	    QtTableView *tw = (QtTableView *)this;
	    r	     = yCellOffs;
	    int h    = minViewY() - yCellDelta; //##arnt3
	    int oldH = h;
	    Q_ASSERT( r < nRows );
	    while ( r < nRows ) {
		oldH = h;
		h += tw->cellHeight( r );	     // Start of next cell
		if ( yPos < h )
		    break;
		r++;
	    }
	    if ( cellMaxY )
		*cellMaxY = h - 1;
	    if ( cellMinY )
		*cellMinY = oldH;
	}
    }
    return r;

}


int QtTableView::findRawCol( int xPos, int *cellMaxX, int *cellMinX ,
			    bool goOutsideView ) const
{
    int c = -1;
    if ( nCols == 0 )
	return c;
    if ( goOutsideView || xPos >= minViewX() && xPos <= maxViewX() ) {
	if ( xPos < minViewX() ) {
#if defined(TQT_CHECK_RANGE)
	    tqWarning( "QtTableView::findRawCol: (%s) internal error: "
		     "xPos < minViewX() && goOutsideView "
		     "not supported. (%d,%d)",
		     name( "unnamed" ), xPos, xOffs );
#endif
	    return -1;
	}
	if ( cellW ) {				// uniform cell width
	    c = (xPos - minViewX() + xCellDelta)/cellW; //cell offs from left
	    if ( cellMaxX )
		*cellMaxX = (c + 1)*cellW + minViewX() - xCellDelta - 1;
	    if ( cellMinX )
		*cellMinX = c*cellW + minViewX() - xCellDelta;
	    c += xCellOffs;			// absolute cell index
	} else {				// variable cell width
	    QtTableView *tw = (QtTableView *)this;
	    c	     = xCellOffs;
	    int w    = minViewX() - xCellDelta; //##arnt3
	    int oldW = w;
	    Q_ASSERT( c < nCols );
	    while ( c < nCols ) {
		oldW = w;
		w += tw->cellWidth( c );	// Start of next cell
		if ( xPos < w )
		    break;
		c++;
	    }
	    if ( cellMaxX )
		*cellMaxX = w - 1;
	    if ( cellMinX )
		*cellMinX = oldW;
	}
    }
    return c;
}


/*!
  Returns the index of the row at position \a yPos, where \a yPos is in
  \e widget coordinates.  Returns -1 if \a yPos is outside the valid
  range.

  \sa findCol(), rowYPos()
*/

int QtTableView::findRow( int yPos ) const
{
    int cellMaxY;
    int row = findRawRow( yPos, &cellMaxY );
    if ( testTableFlags(Tbl_cutCellsV) && cellMaxY > maxViewY() )
	row = - 1;				//  cell cut by bottom margin
    if ( row >= nRows )
	row = -1;
    return row;
}


/*!
  Returns the index of the column at position \a xPos, where \a xPos is
  in \e widget coordinates.  Returns -1 if \a xPos is outside the valid
  range.

  \sa findRow(), colXPos()
*/

int QtTableView::findCol( int xPos ) const
{
    int cellMaxX;
    int col = findRawCol( xPos, &cellMaxX );
    if ( testTableFlags(Tbl_cutCellsH) && cellMaxX > maxViewX() )
	col = - 1;				//  cell cut by right margin
    if ( col >= nCols )
	col = -1;
    return col;
}


/*!
  Computes the position in the widget of row \a row.

  Returns TRUE and stores the result in \a *yPos (in \e widget
  coordinates) if the row is visible.  Returns FALSE and does not modify
  \a *yPos if \a row is invisible or invalid.

  \sa colXPos(), findRow()
*/

bool QtTableView::rowYPos( int row, int *yPos ) const
{
    int y;
    if ( row >= yCellOffs ) {
	if ( cellH ) {
	    int lastVisible = lastRowVisible();
	    if ( row > lastVisible || lastVisible == -1 )
		return FALSE;
	    y = (row - yCellOffs)*cellH + minViewY() - yCellDelta;
	} else {
	    //##arnt3
	    y = minViewY() - yCellDelta;	// y of leftmost cell in view
	    int r = yCellOffs;
	    QtTableView *tw = (QtTableView *)this;
	    int maxY = maxViewY();
	    while ( r < row && y <= maxY )
		y += tw->cellHeight( r++ );
	    if ( y > maxY )
		return FALSE;

	}
    } else {
	return FALSE;
    }
    if ( yPos )
	*yPos = y;
    return TRUE;
}


/*!
  Computes the position in the widget of column \a col.

  Returns TRUE and stores the result in \a *xPos (in \e widget
  coordinates) if the column is visible.  Returns FALSE and does not
  modify \a *xPos if \a col is invisible or invalid.

  \sa rowYPos(), findCol()
*/

bool QtTableView::colXPos( int col, int *xPos ) const
{
    int x;
    if ( col >= xCellOffs ) {
	if ( cellW ) {
	    int lastVisible = lastColVisible();
	    if ( col > lastVisible || lastVisible == -1 )
		return FALSE;
	    x = (col - xCellOffs)*cellW + minViewX() - xCellDelta;
	} else {
	    //##arnt3
	    x = minViewX() - xCellDelta;	// x of uppermost cell in view
	    int c = xCellOffs;
	    QtTableView *tw = (QtTableView *)this;
	    int maxX = maxViewX();
	    while ( c < col && x <= maxX )
		x += tw->cellWidth( c++ );
	    if ( x > maxX )
		return FALSE;
	}
    } else {
	return FALSE;
    }
    if ( xPos )
	*xPos = x;
    return TRUE;
}


/*!
  Moves the visible area of the table right by \a xPixels and
  down by \a yPixels pixels.  Both may be negative.

  \warning You might find that TQScrollView offers a higher-level of
	functionality than using QtTableView and this function.

  This function is \e not the same as TQWidget::scroll(); in particular,
  the signs of \a xPixels and \a yPixels have the reverse semantics.

  \sa setXOffset(), setYOffset(), setOffset(), setTopCell(),
  setLeftCell()
*/

void QtTableView::scroll( int xPixels, int yPixels )
{
    TQWidget::scroll( -xPixels, -yPixels, contentsRect() );
}


/*!
  Returns the leftmost pixel of the table view in \e view
  coordinates.	This excludes the frame and any header.

  \sa maxViewY(), viewWidth(), contentsRect()
*/

int QtTableView::minViewX() const
{
    return frameWidth();
}


/*!
  Returns the top pixel of the table view in \e view
  coordinates.	This excludes the frame and any header.

  \sa maxViewX(), viewHeight(), contentsRect()
*/

int QtTableView::minViewY() const
{
    return frameWidth();
}


/*!
  Returns the rightmost pixel of the table view in \e view
  coordinates.	This excludes the frame and any scroll bar, but
  includes blank pixels to the right of the visible table data.

  \sa maxViewY(), viewWidth(), contentsRect()
*/

int QtTableView::maxViewX() const
{
    return width() - 1 - frameWidth()
        - (tFlags & Tbl_vScrollBar ? VSBEXT
           : 0);
}


/*!
  Returns the bottom pixel of the table view in \e view
  coordinates.	This excludes the frame and any scroll bar, but
  includes blank pixels below the visible table data.

  \sa maxViewX(), viewHeight(), contentsRect()
*/

int QtTableView::maxViewY() const
{
    return height() - 1 - frameWidth()
        - (tFlags & Tbl_hScrollBar ? HSBEXT
           : 0);
}


/*!
  Returns the width of the table view, as such, in \e view
  coordinates.  This does not include any header, scroll bar or frame,
  but it does include background pixels to the right of the table data.

  \sa minViewX() maxViewX(), viewHeight(), contentsRect() viewRect()
*/

int QtTableView::viewWidth() const
{
    return maxViewX() - minViewX() + 1;
}


/*!
  Returns the height of the table view, as such, in \e view
  coordinates.  This does not include any header, scroll bar or frame,
  but it does include background pixels below the table data.

  \sa minViewY() maxViewY() viewWidth() contentsRect() viewRect()
*/

int QtTableView::viewHeight() const
{
    return maxViewY() - minViewY() + 1;
}


void QtTableView::doAutoScrollBars()
{
    int viewW = width()	 - frameWidth() - minViewX();
    int viewH = height() - frameWidth() - minViewY();
    bool vScrollOn = testTableFlags(Tbl_vScrollBar);
    bool hScrollOn = testTableFlags(Tbl_hScrollBar);
    int w = 0;
    int h = 0;
    int i;

    if ( testTableFlags(Tbl_autoHScrollBar) ) {
	if ( cellW ) {
	    w = cellW*nCols;
	} else {
	    i = 0;
	    while ( i < nCols && w <= viewW )
		w += cellWidth( i++ );
	}
	if ( w > viewW )
	    hScrollOn = TRUE;
	else
	    hScrollOn = FALSE;
    }

    if ( testTableFlags(Tbl_autoVScrollBar) ) {
	if ( cellH ) {
	    h = cellH*nRows;
	} else {
	    i = 0;
	    while ( i < nRows && h <= viewH )
		h += cellHeight( i++ );
	}

	if ( h > viewH )
	    vScrollOn = TRUE;
	else
	    vScrollOn = FALSE;
    }

    if ( testTableFlags(Tbl_autoHScrollBar) && vScrollOn && !hScrollOn )
	if ( w > viewW - VSBEXT )
	    hScrollOn = TRUE;

    if ( testTableFlags(Tbl_autoVScrollBar) && hScrollOn && !vScrollOn )
	if ( h > viewH - HSBEXT )
	    vScrollOn = TRUE;

    setHorScrollBar( hScrollOn, FALSE );
    setVerScrollBar( vScrollOn, FALSE );
    updateFrameSize();
}


/*!
  \fn void QtTableView::updateScrollBars()

  Updates the scroll bars' contents and presence to match the table's
  state.  Generally, you should not need to call this.

  \sa setTableFlags()
*/

/*!
  Updates the scroll bars' contents and presence to match the table's
  state \c or \a f.

  \sa setTableFlags()
*/

void QtTableView::updateScrollBars( uint f )
{
    sbDirty = sbDirty | f;
    if ( inSbUpdate )
	return;
    inSbUpdate = TRUE;

    if ( testTableFlags(Tbl_autoHScrollBar) && (sbDirty & horRange) ||
	 testTableFlags(Tbl_autoVScrollBar) && (sbDirty & verRange) )
					// if range change and auto
	doAutoScrollBars();		// turn scroll bars on/off if needed

    if ( !autoUpdate() ) {
	inSbUpdate = FALSE;
	return;
    }
    if ( yOffset() > 0 && testTableFlags( Tbl_autoVScrollBar ) &&
	 !testTableFlags( Tbl_vScrollBar ) ) {
	setYOffset( 0 );
    }
    if ( xOffset() > 0 && testTableFlags( Tbl_autoHScrollBar ) &&
	 !testTableFlags( Tbl_hScrollBar ) ) {
	setXOffset( 0 );
    }
    if ( !isVisible() ) {
	inSbUpdate = FALSE;
	return;
    }

    if ( testTableFlags(Tbl_hScrollBar) && (sbDirty & horMask) != 0 ) {
	if ( sbDirty & horGeometry )
	    hScrollBar->setGeometry( 0,height() - HSBEXT,
                                     viewWidth() + frameWidth()*2,
                                   HSBEXT);

	if ( sbDirty & horSteps ) {
	    if ( cellW )
		hScrollBar->setSteps( TQMIN(cellW,viewWidth()/2), viewWidth() );
	    else
		hScrollBar->setSteps( 16, viewWidth() );
	}

	if ( sbDirty & horRange )
	    hScrollBar->setRange( 0, maxXOffset() );

	if ( sbDirty & horValue )
	    hScrollBar->setValue( xOffs );

			// show scrollbar only when it has a sane geometry
	if ( !hScrollBar->isVisible() )
	    hScrollBar->show();
    }

    if ( testTableFlags(Tbl_vScrollBar) && (sbDirty & verMask) != 0 ) {
	if ( sbDirty & verGeometry )
	    vScrollBar->setGeometry( width() - VSBEXT, 0,
                                     VSBEXT,
                                     viewHeight() + frameWidth()*2 );

	if ( sbDirty & verSteps ) {
	    if ( cellH )
		vScrollBar->setSteps( TQMIN(cellH,viewHeight()/2), viewHeight() );
	    else
		vScrollBar->setSteps( 16, viewHeight() );  // fttb! ###
	}

	if ( sbDirty & verRange )
	    vScrollBar->setRange( 0, maxYOffset() );

	if ( sbDirty & verValue )
	    vScrollBar->setValue( yOffs );

			// show scrollbar only when it has a sane geometry
	if ( !vScrollBar->isVisible() )
	    vScrollBar->show();
    }
    if ( coveringCornerSquare &&
	 ( (sbDirty & verGeometry ) || (sbDirty & horGeometry)) )
	cornerSquare->move( maxViewX() + frameWidth() + 1,
			    maxViewY() + frameWidth() + 1 );

    sbDirty = 0;
    inSbUpdate = FALSE;
}


void QtTableView::updateFrameSize()
{
    int rw = width()  - ( testTableFlags(Tbl_vScrollBar) ?
                          VSBEXT : 0 );
    int rh = height() - ( testTableFlags(Tbl_hScrollBar) ?
                          HSBEXT : 0 );
    if ( rw < 0 )
	rw = 0;
    if ( rh < 0 )
	rh = 0;

    if ( autoUpdate() ) {
        int fh = frameRect().height();
	int fw = frameRect().width();
	setFrameRect( TQRect(0,0,rw,rh) );

	if ( rw != fw )
	    update( TQMIN(fw,rw) - frameWidth() - 2, 0, frameWidth()+4, rh );
	if ( rh != fh )
	    update( 0, TQMIN(fh,rh) - frameWidth() - 2, rw, frameWidth()+4 );
    }
}


/*!
  Returns the maximum horizontal offset within the table of the
  view's left edge in \e table coordinates.

  This is used mainly to set the horizontal scroll bar's range.

  \sa maxColOffset(), maxYOffset(), totalWidth()
*/

int QtTableView::maxXOffset()
{
    int tw = totalWidth();
    int maxOffs;
    if ( testTableFlags(Tbl_scrollLastHCell) ) {
	if ( nCols != 1)
	    maxOffs =  tw - ( cellW ? cellW : cellWidth( nCols - 1 ) );
	else
	    maxOffs = tw - viewWidth();
    } else {
	if ( testTableFlags(Tbl_snapToHGrid) ) {
	    if ( cellW ) {
		maxOffs =  tw - (viewWidth()/cellW)*cellW;
	    } else {
		int goal = tw - viewWidth();
		int pos = tw;
		int nextCol = nCols - 1;
		int nextCellWidth = cellWidth( nextCol );
		while( nextCol > 0 && pos > goal + nextCellWidth ) {
		    pos -= nextCellWidth;
		    nextCellWidth = cellWidth( --nextCol );
		}
		if ( goal + nextCellWidth == pos )
		    maxOffs = goal;
		 else if ( goal < pos )
		   maxOffs = pos;
		 else
		   maxOffs = 0;
	    }
	} else {
	    maxOffs = tw - viewWidth();
	}
    }
    return maxOffs > 0 ? maxOffs : 0;
}


/*!
  Returns the maximum vertical offset within the table of the
  view's top edge in \e table coordinates.

  This is used mainly to set the vertical scroll bar's range.

  \sa maxRowOffset(), maxXOffset(), totalHeight()
*/

int QtTableView::maxYOffset()
{
    int th = totalHeight();
    int maxOffs;
    if ( testTableFlags(Tbl_scrollLastVCell) ) {
	if ( nRows != 1)
	    maxOffs =  th - ( cellH ? cellH : cellHeight( nRows - 1 ) );
	else
	    maxOffs = th - viewHeight();
    } else {
	if ( testTableFlags(Tbl_snapToVGrid) ) {
	    if ( cellH ) {
		maxOffs =  th - (viewHeight()/cellH)*cellH;
	    } else {
		int goal = th - viewHeight();
		int pos = th;
		int nextRow = nRows - 1;
		int nextCellHeight = cellHeight( nextRow );
		while( nextRow > 0 && pos > goal + nextCellHeight ) {
		    pos -= nextCellHeight;
		    nextCellHeight = cellHeight( --nextRow );
		}
		if ( goal + nextCellHeight == pos )
		    maxOffs = goal;
		 else if ( goal < pos )
		   maxOffs = pos;
		 else
		   maxOffs = 0;
	    }
	} else {
	    maxOffs = th - viewHeight();
	}
    }
    return maxOffs > 0 ? maxOffs : 0;
}


/*!
  Returns the index of the last column, which may be at the left edge
  of the view.

  Depending on the \link setTableFlags() Tbl_scrollLastHCell\endlink flag,
  this may or may not be the last column.

  \sa maxXOffset(), maxRowOffset()
*/

int QtTableView::maxColOffset()
{
    int mx = maxXOffset();
    if ( cellW )
	return mx/cellW;
    else {
	int xcd=0, col=0;
	while ( col < nCols && mx > (xcd=cellWidth(col)) ) {
	    mx -= xcd;
	    col++;
	}
	return col;
    }
}


/*!
  Returns the index of the last row, which may be at the top edge of
  the view.

  Depending on the \link setTableFlags() Tbl_scrollLastVCell\endlink flag,
  this may or may not be the last row.

  \sa maxYOffset(), maxColOffset()
*/

int QtTableView::maxRowOffset()
{
    int my = maxYOffset();
    if ( cellH )
	return my/cellH;
    else {
	int ycd=0, row=0;
	while ( row < nRows && my > (ycd=cellHeight(row)) ) {
	    my -= ycd;
	    row++;
	}
	return row;
    }
}


void QtTableView::showOrHideScrollBars()
{
    if ( !autoUpdate() )
	return;
    if ( vScrollBar ) {
	if ( testTableFlags(Tbl_vScrollBar) ) {
	    if ( !vScrollBar->isVisible() )
		sbDirty = sbDirty | verMask;
	} else {
	    if ( vScrollBar->isVisible() )
	       vScrollBar->hide();
	}
    }
    if ( hScrollBar ) {
	if ( testTableFlags(Tbl_hScrollBar) ) {
	    if ( !hScrollBar->isVisible() )
		sbDirty = sbDirty | horMask;
	} else {
	    if ( hScrollBar->isVisible() )
		hScrollBar->hide();
	}
    }
    if ( cornerSquare ) {
	if ( testTableFlags(Tbl_hScrollBar) &&
	     testTableFlags(Tbl_vScrollBar) ) {
	    if ( !cornerSquare->isVisible() )
		cornerSquare->show();
	} else {
	    if ( cornerSquare->isVisible() )
		cornerSquare->hide();
	}
    }
}


/*!
  Updates the scroll bars and internal state.

  Call this function when the table view's total size is changed;
  typically because the result of cellHeight() or cellWidth() have changed.

  This function does not repaint the widget.
*/

void QtTableView::updateTableSize()
{
    bool updateOn = autoUpdate();
    setAutoUpdate( FALSE );
    int xofs = xOffset();
    xOffs++; //so that setOffset will not return immediately
    setOffset(xofs,yOffset(),FALSE); //to calculate internal state correctly
    setAutoUpdate(updateOn);

    updateScrollBars( horSteps |  horRange |
		      verSteps |  verRange );
    showOrHideScrollBars();
}


#include "qttableview.moc"