summaryrefslogtreecommitdiffstats
path: root/tdehtml/tdehtml_caret.cpp
blob: 71d30629be9e526e14c382e359f5ae25e59df661 (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
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
/* This file is part of the KDE project
 *
 * Copyright (C) 2003-2004 Leo Savernik <l.savernik@aon.at>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */


#include "tdehtml_caret_p.h"

#include "html/html_documentimpl.h"

namespace tdehtml {

/** Flags representing the type of advance that has been made.
 * @param LeftObject a render object was left and an ascent to its parent has
 *	taken place
 * @param AdvancedToSibling an actual advance to a sibling has taken place
 * @param EnteredObject a render object was entered by descending into it from
 *	its parent object.
 */
enum ObjectAdvanceState {
  LeftObject = 0x01, AdvancedToSibling = 0x02, EnteredObject = 0x04
};

/** All possible states that may occur during render object traversal.
 * @param OutsideDescending outside of the current object, ready to descend
 *	into children
 * @param InsideDescending inside the current object, descending into
 *	children
 * @param InsideAscending inside the current object, ascending to parents
 * @param OutsideAscending outsie the current object, ascending to parents
 */
enum ObjectTraversalState {
  OutsideDescending, InsideDescending, InsideAscending, OutsideAscending
};

/** Traverses the render object tree in a fine granularity.
 * @param obj render object
 * @param trav object traversal state
 * @param toBegin traverse towards beginning
 * @param base base render object which this method must not advance beyond
 *	(0 means document)
 * @param state object advance state (enum ObjectAdvanceState)
 * @return the render object according to the state. May be the same as \c obj
 */
static RenderObject* traverseRenderObjects(RenderObject *obj,
		ObjectTraversalState &trav, bool toBegin, RenderObject *base,
                int &state)
{
  RenderObject *r;
  switch (trav) {
    case OutsideDescending:
      trav = InsideDescending;
      break;
    case InsideDescending:
      r = toBegin ? obj->lastChild() : obj->firstChild();
      if (r) {
        trav = OutsideDescending;
        obj = r;
        state |= EnteredObject;
      } else {
        trav = InsideAscending;
      }
      break;
    case InsideAscending:
      trav = OutsideAscending;
      break;
    case OutsideAscending:
      r = toBegin ? obj->previousSibling() : obj->nextSibling();
      if (r) {
        trav = OutsideDescending;
        state |= AdvancedToSibling;
      } else {
        r = obj->parent();
        if (r == base) r = 0;
        trav = InsideAscending;
        state |= LeftObject;
      }
      obj = r;
      break;
  }/*end switch*/

  return obj;
}

/** Like RenderObject::objectBelow, but confined to stay within \c base.
 * @param obj render object to begin with
 * @param trav object traversal state, will be reset within this function
 * @param base base render object (0: no base)
 */
static inline RenderObject *renderObjectBelow(RenderObject *obj, ObjectTraversalState &trav, RenderObject *base)
{
  trav = InsideDescending;
  int state;		// we don't need the state, so we don't initialize it
  RenderObject *r = obj;
  while (r && trav != OutsideDescending) {
    r = traverseRenderObjects(r, trav, false, base, state);
#if DEBUG_CARETMODE > 3
    kdDebug(6200) << "renderObjectBelow: r " << r << " trav " << trav << endl;
#endif
  }
  trav = InsideDescending;
  return r;
}

/** Like RenderObject::objectAbove, but confined to stay within \c base.
 * @param obj render object to begin with
 * @param trav object traversal state, will be reset within this function
 * @param base base render object (0: no base)
 */
static inline RenderObject *renderObjectAbove(RenderObject *obj, ObjectTraversalState &trav, RenderObject *base)
{
  trav = OutsideAscending;
  int state;		// we don't need the state, so we don't initialize it
  RenderObject *r = obj;
  while (r && trav != InsideAscending) {
    r = traverseRenderObjects(r, trav, true, base, state);
#if DEBUG_CARETMODE > 3
    kdDebug(6200) << "renderObjectAbove: r " << r << " trav " << trav << endl;
#endif
  }
  trav = InsideAscending;
  return r;
}

/** Checks whether the given inline box matches the IndicatedFlows policy
 * @param box inline box to test
 * @return true on match
 */
static inline bool isIndicatedInlineBox(InlineBox *box)
{
  // text boxes are never indicated.
  if (box->isInlineTextBox()) return false;
  RenderStyle *s = box->object()->style();
  return s->borderLeftWidth() || s->borderRightWidth()
  	|| s->borderTopWidth() || s->borderBottomWidth()
	|| s->paddingLeft().value() || s->paddingRight().value()
	|| s->paddingTop().value() || s->paddingBottom().value()
	// ### Can inline elements have top/bottom margins? Couldn't find
	// it in the CSS 2 spec, but Mozilla ignores them, so we do, too.
	|| s->marginLeft().value() || s->marginRight().value();
}

/** Checks whether the given render object matches the IndicatedFlows policy
 * @param r render object to test
 * @return true on match
 */
static inline bool isIndicatedFlow(RenderObject *r)
{
  RenderStyle *s = r->style();
  return s->borderLeftStyle() != BNONE || s->borderRightStyle() != BNONE
  	|| s->borderTopStyle() != BNONE || s->borderBottomStyle() != BNONE
//	|| s->paddingLeft().value() || s->paddingRight().value()
//	|| s->paddingTop().value() || s->paddingBottom().value()
//	|| s->marginLeft().value() || s->marginRight().value()
	|| s->hasClip() || s->hidesOverflow()
	|| s->backgroundColor().isValid() || s->backgroundImage();
}

/** Advances to the next render object, taking into account the current
 * traversal state.
 *
 * @param r render object
 * @param trav object traversal state
 * @param toBegin @p true, advance towards beginning, @p false, advance toward end
 * @param base base render object which this method must not advance beyond
 *	(0 means document)
 * @param state object advance state (enum ObjectAdvanceState) (unchanged
 *	on LeafsOnly)
 * @return a pointer to the render object which we advanced to,
 *	or 0 if the last possible object has been reached.
 */
static RenderObject *advanceObject(RenderObject *r,
		ObjectTraversalState &trav, bool toBegin,
                RenderObject *base, int &state)
{

  ObjectTraversalState origtrav = trav;
  RenderObject *a = traverseRenderObjects(r, trav, toBegin, base, state);

  bool ignoreOutsideDesc = toBegin && origtrav == OutsideAscending;

  // render object and traversal state at which look ahead has been started
  RenderObject *la = 0;
  ObjectTraversalState latrav = trav;
  ObjectTraversalState lasttrav = origtrav;

  while (a) {
#if DEBUG_CARETMODE > 5
kdDebug(6200) << "a " << a << " trav " << trav << endl;
#endif
    if (a->element()) {
#if DEBUG_CARETMODE > 4
kdDebug(6200) << "a " << a << " trav " << trav << " origtrav " << origtrav << " ignoreOD " << ignoreOutsideDesc << endl;
#endif
      if (toBegin) {

        switch (origtrav) {
	  case OutsideDescending:
            if (trav == InsideAscending) return a;
	    if (trav == OutsideDescending) return a;
	    break;
          case InsideDescending:
	    if (trav == OutsideDescending) return a;
	    // fall through
          case InsideAscending:
            if (trav == OutsideAscending) return a;
	    break;
	  case OutsideAscending:
	    if (trav == OutsideAscending) return a;
            if (trav == InsideAscending && lasttrav == InsideDescending) return a;
	    if (trav == OutsideDescending && !ignoreOutsideDesc) return a;
	    // ignore this outside descending position, as it effectively
	    // demarkates the same position, but remember it in case we fall off
	    // the document.
	    la = a; latrav = trav;
	    ignoreOutsideDesc = false;
	    break;
        }/*end switch*/

      } else {

        switch (origtrav) {
	  case OutsideDescending:
            if (trav == InsideAscending) return a;
	    if (trav == OutsideDescending) return a;
	    break;
          case InsideDescending:
//	    if (trav == OutsideDescending) return a;
	    // fall through
          case InsideAscending:
//            if (trav == OutsideAscending) return a;
//	    break;
	  case OutsideAscending:
	    // ### what if origtrav == OA, and immediately afterwards trav
	    // becomes OD? In this case the effective position hasn't changed ->
	    // the caret gets stuck. Otherwise, it apparently cannot happen in
	    // real usage patterns.
	    if (trav == OutsideDescending) return a;
	    if (trav == OutsideAscending) {
	      if (la) return la;
              // starting lookahead here. Remember old object in case we fall off
	      // the document.
	      la = a; latrav = trav;
	    }
	    break;
	}/*end switch*/

      }/*end if*/
    }/*end if*/

    lasttrav = trav;
    a = traverseRenderObjects(a, trav, toBegin, base, state);
  }/*wend*/

  if (la) trav = latrav, a = la;
  return a;

}

/** Check whether the current render object is unsuitable in caret mode handling.
 *
 * Some render objects cannot be handled correctly in caret mode. These objects
 * are therefore considered to be unsuitable. The null object is suitable, as
 * it denotes reaching the end.
 * @param r current render object
 * @param trav current traversal state
 */
static inline bool isUnsuitable(RenderObject *r, ObjectTraversalState trav)
{
  if (!r) return false;
  return r->isTableCol() || r->isTableSection() || r->isTableRow()
  	|| (r->isText() && static_cast<RenderText *>(r)->inlineTextBoxCount() == 0);
      ;
  Q_UNUSED(trav);
}

/** Advances to the next render object, taking into account the current
 * traversal state, but skipping render objects which are not suitable for
 * having placed the caret into them.
 * @param r render object
 * @param trav object traversal state (unchanged on LeafsOnly)
 * @param toBegin @p true, advance towards beginning, @p false, advance toward end
 * @param base base render object which this method must not advance beyond
 *	(0 means document)
 * @param state object advance state (enum ObjectAdvanceState) (unchanged
 *	on LeafsOnly)
 * @return a pointer to the advanced render object or 0 if the last possible
 *	object has been reached.
 */
static inline RenderObject *advanceSuitableObject(RenderObject *r,
		ObjectTraversalState &trav, bool toBegin,
		RenderObject *base, int &state)
{
  do {
    r = advanceObject(r, trav, toBegin, base, state);
#if DEBUG_CARETMODE > 2
    kdDebug(6200) << "after advanceSWP: r " << r << " trav " << trav << " toBegin " << toBegin << endl;
#endif
  } while (isUnsuitable(r, trav));
  return r;
}

/**
  * Returns the next leaf node.
  *
  * Using this function delivers leaf nodes as if the whole DOM tree
  * were a linear chain of its leaf nodes.
  * @param r dom node
  * @param baseElem base element not to search beyond
  * @return next leaf node or 0 if there are no more.
  */
static NodeImpl *nextLeafNode(NodeImpl *r, NodeImpl *baseElem)
{
  NodeImpl *n = r->firstChild();
  if (n) {
    while (n) { r = n; n = n->firstChild(); }
    return const_cast<NodeImpl *>(r);
  }/*end if*/
  n = r->nextSibling();
  if (n) {
    r = n;
    while (n) { r = n; n = n->firstChild(); }
    return const_cast<NodeImpl *>(r);
  }/*end if*/

  n = r->parentNode();
  if (n == baseElem) n = 0;
  while (n) {
    r = n;
    n = r->nextSibling();
    if (n) {
      r = n;
      n = r->firstChild();
      while (n) { r = n; n = n->firstChild(); }
      return const_cast<NodeImpl *>(r);
    }/*end if*/
    n = r->parentNode();
    if (n == baseElem) n = 0;
  }/*wend*/
  return 0;
}

#if 0		// currently not used
/** (Not part of the official DOM)
  * Returns the previous leaf node.
  *
  * Using this function delivers leaf nodes as if the whole DOM tree
  * were a linear chain of its leaf nodes.
  * @param r dom node
  * @param baseElem base element not to search beyond
  * @return previous leaf node or 0 if there are no more.
  */
static NodeImpl *prevLeafNode(NodeImpl *r, NodeImpl *baseElem)
{
  NodeImpl *n = r->firstChild();
  if (n) {
    while (n) { r = n; n = n->firstChild(); }
    return const_cast<NodeImpl *>(r);
  }/*end if*/
  n = r->previousSibling();
  if (n) {
    r = n;
    while (n) { r = n; n = n->firstChild(); }
    return const_cast<NodeImpl *>(r);
  }/*end if*/

  n = r->parentNode();
  if (n == baseElem) n = 0;
  while (n) {
    r = n;
    n = r->previousSibling();
    if (n) {
      r = n;
      n = r->lastChild();
      while (n) { r = n; n = n->lastChild(); }
      return const_cast<NodeImpl *>(r);
    }/*end if*/
    n = r->parentNode();
    if (n == baseElem) n = 0;
  }/*wend*/
  return 0;
}
#endif

/** Maps a DOM Range position to the corresponding caret position.
 *
 * The offset boundary is not checked for validity.
 * @param node DOM node
 * @param offset zero-based offset within node
 * @param r returns render object (may be 0 if DOM node has no render object)
 * @param r_ofs returns the appropriate offset for the found render object r
 * @param outside returns true when offset is applied to the outside of
 *	\c r, or false for the inside.
 * @param outsideEnd return true when the caret position is at the outside end.
 */
void /*KDE_NO_EXPORT*/ mapDOMPosToRenderPos(NodeImpl *node, long offset,
		RenderObject *&r, long &r_ofs, bool &outside, bool &outsideEnd)
{
  if (node->nodeType() == Node::TEXT_NODE) {
    outside = false;
    outsideEnd = false;
    r = node->renderer();
    r_ofs = offset;
  } else if (node->nodeType() == Node::ELEMENT_NODE || node->nodeType() == Node::DOCUMENT_NODE) {

    // Though offset points between two children, attach it to the visually
    // most suitable one (and only there, because the mapping must stay bijective)
    if (node->firstChild()) {
      outside = true;
      NodeImpl *child = offset <= 0 ? node->firstChild()
      				// childNode is expensive
      				: node->childNode((unsigned long)offset);
      // index was child count or out of bounds
      bool atEnd = !child;
#if DEBUG_CARETMODE > 5
      kdDebug(6200) << "mapDTR: child " << child << "@" << (child ? child->nodeName().string() : TQString::null) << " atEnd " << atEnd << endl;
#endif
      if (atEnd) child = node->lastChild();

      r = child->renderer();
      r_ofs = 0;
      outsideEnd = atEnd;

      // Outside text nodes most likely stem from a continuation. Seek
      // the enclosing continued render object and use this one instead.
      if (r && child->nodeType() == Node::TEXT_NODE) {
        r = r->parent();
        RenderObject *o = node->renderer();
	while (o->continuation() && o->continuation() != r)
	  o = o->continuation();
	if (!r || o->continuation() != r) {
	  r = child->renderer();
	}
      }/*end if*/

      // BRs cause troubles. Returns the previous render object instead,
      // giving it the attributes outside, outside end.
      if (r && r->isBR()) {
        r = r->objectAbove();
        outsideEnd = true;
      }/*end if*/

    } else {
      // Element has no children, treat offset to be inside the node.
      outside = false;
      outsideEnd = false;
      r = node->renderer();
      r_ofs = 0;	// only offset 0 possible
    }

  } else {
    r = 0;
    kdWarning() << k_funcinfo << "Mapping from nodes of type " << node->nodeType()
        << " not supported!" << endl;
  }
}

/** Maps a caret position to the corresponding DOM Range position.
 *
 * @param r render object
 * @param r_ofs offset within render object
 * @param outside true when offset is interpreted to be on the outside of
 *	\c r, or false if on the inside.
 * @param outsideEnd true when the caret position is at the outside end.
 * @param node returns DOM node
 * @param offset returns zero-based offset within node
 */
void /*KDE_NO_EXPORT*/ mapRenderPosToDOMPos(RenderObject *r, long r_ofs,
		bool outside, bool outsideEnd, NodeImpl *&node, long &offset)
{
  node = r->element();
  Q_ASSERT(node);
#if DEBUG_CARETMODE > 5
      kdDebug(6200) << "mapRTD: r " << r << "@" << (r ? r->renderName() : TQString::null) << (r && r->element() ? TQString(".node ") + TQString::number((unsigned)r->element(),16) + "@" + r->element()->nodeName().string() : TQString::null) << " outside " << outside << " outsideEnd " << outsideEnd << endl;
#endif
  if (node->nodeType() == Node::ELEMENT_NODE || node->nodeType() == Node::TEXT_NODE) {

    if (outside) {
      NodeImpl *parent = node->parent();

      // If this is part of a continuation, use the actual node as the parent,
      // and the first render child as the node.
      if (r != node->renderer()) {
        RenderObject *o = node->renderer();
	while (o->continuation() && o->continuation() != r)
	  o = o->continuation();
	if (o->continuation() == r) {
	  parent = node;
	  // ### What if the first render child does not map to a child of
	  // the continued node?
	  node = r->firstChild() ? r->firstChild()->element() : node;
	}
      }/*end if*/

      if (!parent) goto inside;

      offset = (long)node->nodeIndex() + outsideEnd;
      node = parent;
#if DEBUG_CARETMODE > 5
   kdDebug(6200) << node << "@" << (node ? node->nodeName().string() : TQString::null) << " offset " << offset << endl;
#endif
    } else {	// !outside
inside:
      offset = r_ofs;
    }

  } else {
    offset = 0;
    kdWarning() << k_funcinfo << "Mapping to nodes of type " << node->nodeType()
        << " not supported!" << endl;
  }
}

/** Make sure the given node is a leaf node. */
static inline void ensureLeafNode(NodeImpl *&node, NodeImpl *base)
{
  if (node && node->hasChildNodes()) node = nextLeafNode(node, base);
}

/** Converts a caret position to its respective object traversal state.
 * @param outside whether the caret is outside the object
 * @param atEnd whether the caret position is at the end
 * @param toBegin \c true when advancing towards the beginning
 * @param trav returns the corresponding traversal state
 */
static inline void mapRenderPosToTraversalState(bool outside, bool atEnd,
		bool toBegin, ObjectTraversalState &trav)
{
  if (!outside) atEnd = !toBegin;
  if (!atEnd ^ toBegin)
    trav = outside ? OutsideDescending : InsideDescending;
  else
    trav = outside ? OutsideAscending : InsideAscending;
}

/** Converts a traversal state to its respective caret position
 * @param trav object traversal state
 * @param toBegin \c true when advancing towards the beginning
 * @param outside whether the caret is outside the object
 * @param atEnd whether the caret position is at the end
 */
static inline void mapTraversalStateToRenderPos(ObjectTraversalState trav,
		bool toBegin, bool &outside, bool &atEnd)
{
  outside = false;
  switch (trav) {
    case OutsideDescending: outside = true; // fall through
    case InsideDescending: atEnd = toBegin; break;
    case OutsideAscending: outside = true; // fall through
    case InsideAscending: atEnd = !toBegin; break;
  }
}

/** Finds the next node that has a renderer.
 *
 * Note that if the initial @p node has a renderer, this will be returned,
 * regardless of the caret advance policy.
 * Otherwise, for the next nodes, only leaf nodes are considered.
 * @param node node to start with, will be updated accordingly
 * @param offset offset of caret within \c node
 * @param base base render object which this method must not advance beyond
 *	(0 means document)
 * @param r_ofs return the caret offset within the returned renderer
 * @param outside returns whether offset is to be interpreted to the outside
  *	(true) or the inside (false) of the render object.
 * @param outsideEnd returns whether the end of the outside position is meant
 * @return renderer or 0 if no following node has a renderer.
 */
static RenderObject* findRenderer(NodeImpl *&node, long offset,
			RenderObject *base, long &r_ofs,
                        bool &outside, bool &outsideEnd)
{
  if (!node) return 0;
  RenderObject *r;
  mapDOMPosToRenderPos(node, offset, r, r_ofs, outside, outsideEnd);
#if DEBUG_CARETMODE > 2
   kdDebug(6200) << "findRenderer: node " << node << " " << (node ? node->nodeName().string() : TQString::null) << " offset " << offset << " r " << r << "[" << (r ? r->renderName() : TQString::null) << "] r_ofs " << r_ofs << " outside " << outside << " outsideEnd " << outsideEnd << endl;
#endif
  if (r) return r;
  NodeImpl *baseElem = base ? base->element() : 0;
  while (!r) {
    node = nextLeafNode(node, baseElem);
    if (!node) break;
    r = node->renderer();
    if (r) r_ofs = offset;
  }
#if DEBUG_CARETMODE > 3
  kdDebug(6200) << "1r " << r << endl;
#endif
  ObjectTraversalState trav;
  int state;		// not used
  mapRenderPosToTraversalState(outside, outsideEnd, false, trav);
  if (r && isUnsuitable(r, trav)) {
    r = advanceSuitableObject(r, trav, false, base, state);
    mapTraversalStateToRenderPos(trav, false, outside, outsideEnd);
    if (r) r_ofs = r->minOffset();
  }
#if DEBUG_CARETMODE > 3
  kdDebug(6200) << "2r " << r << endl;
#endif
  return r;
}

/** returns a suitable base element
 * @param caretNode current node containing caret.
 */
static ElementImpl *determineBaseElement(NodeImpl *caretNode)
{
  // ### for now, only body is delivered for html documents,
  // and 0 for xml documents.

  DocumentImpl *doc = caretNode->getDocument();
  if (!doc) return 0;	// should not happen, but who knows.

  if (doc->isHTMLDocument())
    return static_cast<HTMLDocumentImpl *>(doc)->body();

  return 0;
}

// == class CaretBox implementation

#if DEBUG_CARETMODE > 0
void CaretBox::dump(TQTextStream &ts, const TQString &ind) const
{
  ts << ind << "b@" << _box;

  if (_box) {
    ts << "<" << _box->object() << ":" << _box->object()->renderName() << ">";
  }/*end if*/

  ts << " " << _x << "+" << _y << "+" << _w << "*" << _h;

  ts << " cb@" << cb;
  if (cb) ts << ":" << cb->renderName();

  ts << " " << (_outside ? (outside_end ? "oe" : "o-") : "i-");
//  ts << endl;
}
#endif

// == class CaretBoxLine implementation

#if DEBUG_CARETMODE > 0
#  define DEBUG_ACIB 1
#else
#  define DEBUG_ACIB DEBUG_CARETMODE
#endif
void CaretBoxLine::addConvertedInlineBox(InlineBox *box, SeekBoxParams &sbp) /*KDE_NO_EXPORT*/
{
  // Generate only one outside caret box between two elements. If
  // coalesceOutsideBoxes is true, generating left outside boxes is inhibited.
  bool coalesceOutsideBoxes = false;
  CaretBoxIterator lastCoalescedBox;
  for (; box; box = box->nextOnLine()) {
#if DEBUG_ACIB
kdDebug(6200) << "box " << box << endl;
kdDebug(6200) << "box->object " << box->object() << endl;
kdDebug(6200) << "x " << box->m_x << " y " << box->m_y << " w " << box->m_width << " h " << box->m_height << " baseline " << box->m_baseline << " ifb " << box->isInlineFlowBox() << " itb " << box->isInlineTextBox() << " rlb " << box->isRootInlineBox() << endl;
#endif
    // ### Why the hell can object() ever be 0?!
    if (!box->object()) continue;

    RenderStyle *s = box->object()->style(box->m_firstLine);
    // parent style for outside caret boxes
    RenderStyle *ps = box->parent() && box->parent()->object()
    		? box->parent()->object()->style(box->parent()->m_firstLine)
		: s;

    if (box->isInlineFlowBox()) {
#if DEBUG_ACIB
kdDebug(6200) << "isinlineflowbox " << box << endl;
#endif
      InlineFlowBox *flowBox = static_cast<InlineFlowBox *>(box);
      bool rtl = ps->direction() == RTL;
      const TQFontMetrics &pfm = ps->fontMetrics();

      if (flowBox->includeLeftEdge()) {
        // If this box is to be coalesced with the outside end box of its
        // predecessor, then check if it is the searched box. If it is, we
        // substitute the outside end box.
        if (coalesceOutsideBoxes) {
          if (sbp.equalsBox(flowBox, true, false)) {
            sbp.it = lastCoalescedBox;
            Q_ASSERT(!sbp.found);
            sbp.found = true;
          }
        } else {
          addCreatedFlowBoxEdge(flowBox, pfm, true, rtl);
          sbp.check(preEnd());
        }
      }/*end if*/

      if (flowBox->firstChild()) {
#if DEBUG_ACIB
kdDebug(6200) << "this " << this << " flowBox " << flowBox << " firstChild " << flowBox->firstChild() << endl;
kdDebug(6200) << "== recursive invocation" << endl;
#endif
        addConvertedInlineBox(flowBox->firstChild(), sbp);
#if DEBUG_ACIB
kdDebug(6200) << "== recursive invocation end" << endl;
#endif
}
      else {
        addCreatedFlowBoxInside(flowBox, s->fontMetrics());
        sbp.check(preEnd());
      }

      if (flowBox->includeRightEdge()) {
        addCreatedFlowBoxEdge(flowBox, pfm, false, rtl);
        lastCoalescedBox = preEnd();
        sbp.check(lastCoalescedBox);
        coalesceOutsideBoxes = true;
      }

    } else if (box->isInlineTextBox()) {
#if DEBUG_ACIB
kdDebug(6200) << "isinlinetextbox " << box << (box->object() ? TQString(" contains \"%1\"").arg(TQConstString(static_cast<RenderText *>(box->object())->str->s+box->minOffset(), kMin(box->maxOffset() - box->minOffset(), 15L)).string()) : TQString::null) << endl;
#endif
      caret_boxes.append(new CaretBox(box, false, false));
      sbp.check(preEnd());
      // coalescing has been interrupted
      coalesceOutsideBoxes = false;

    } else {
#if DEBUG_ACIB
kdDebug(6200) << "some replaced or what " << box << endl;
#endif
      // must be an inline-block, inline-table, or any RenderReplaced
      bool rtl = ps->direction() == RTL;
      const TQFontMetrics &pfm = ps->fontMetrics();

      if (coalesceOutsideBoxes) {
        if (sbp.equalsBox(box, true, false)) {
          sbp.it = lastCoalescedBox;
          Q_ASSERT(!sbp.found);
          sbp.found = true;
        }
      } else {
        addCreatedInlineBoxEdge(box, pfm, true, rtl);
        sbp.check(preEnd());
      }

      caret_boxes.append(new CaretBox(box, false, false));
      sbp.check(preEnd());

      addCreatedInlineBoxEdge(box, pfm, false, rtl);
      lastCoalescedBox = preEnd();
      sbp.check(lastCoalescedBox);
      coalesceOutsideBoxes = true;
    }/*end if*/
  }/*next box*/
}
#undef DEBUG_ACIB

void CaretBoxLine::addCreatedFlowBoxInside(InlineFlowBox *flowBox, const TQFontMetrics &fm) /*KDE_NO_EXPORT*/
{

  CaretBox *caretBox = new CaretBox(flowBox, false, false);
  caret_boxes.append(caretBox);

  // afaik an inner flow box can only have the width 0, therefore we don't
  // have to care for rtl or alignment
  // ### can empty inline elements have a width? css 2 spec isn't verbose about it

  caretBox->_y += flowBox->baseline() - fm.ascent();
  caretBox->_h = fm.height();
}

void CaretBoxLine::addCreatedFlowBoxEdge(InlineFlowBox *flowBox, const TQFontMetrics &fm, bool left, bool rtl) /*KDE_NO_EXPORT*/
{
  CaretBox *caretBox = new CaretBox(flowBox, true, !left);
  caret_boxes.append(caretBox);

  if (left ^ rtl) caretBox->_x -= flowBox->paddingLeft() + flowBox->borderLeft() + 1;
  else caretBox->_x += caretBox->_w + flowBox->paddingRight() + flowBox->borderRight();

  caretBox->_y += flowBox->baseline() - fm.ascent();
  caretBox->_h = fm.height();
  caretBox->_w = 1;
}

void CaretBoxLine::addCreatedInlineBoxEdge(InlineBox *box, const TQFontMetrics &fm, bool left, bool rtl) /*KDE_NO_EXPORT*/
{
  CaretBox *caretBox = new CaretBox(box, true, !left);
  caret_boxes.append(caretBox);

  if (left ^ rtl) caretBox->_x--;
  else caretBox->_x += caretBox->_w;

  caretBox->_y += box->baseline() - fm.ascent();
  caretBox->_h = fm.height();
  caretBox->_w = 1;
}

CaretBoxLine *CaretBoxLine::constructCaretBoxLine(CaretBoxLineDeleter *deleter,
	InlineFlowBox *basicFlowBox, InlineBox *seekBox, bool seekOutside,
        bool seekOutsideEnd, CaretBoxIterator &iter, RenderObject *seekObject)
// 	KDE_NO_EXPORT
{
  // Iterate all inline boxes within this inline flow box.
  // Caret boxes will be created for each
  // - outside begin of an inline flow box (except for the basic inline flow box)
  // - outside end of an inline flow box (except for the basic inline flow box)
  // - inside of an empty inline flow box
  // - outside begin of an inline box resembling a replaced element
  // - outside end of an inline box resembling a replaced element
  // - inline text box
  // - inline replaced box

  CaretBoxLine *result = new CaretBoxLine(basicFlowBox);
  deleter->append(result);

  SeekBoxParams sbp(seekBox, seekOutside, seekOutsideEnd, seekObject, iter);

  // iterate recursively, I'm too lazy to do it iteratively
  result->addConvertedInlineBox(basicFlowBox, sbp);

  if (!sbp.found) sbp.it = result->end();

  return result;
}

CaretBoxLine *CaretBoxLine::constructCaretBoxLine(CaretBoxLineDeleter *deleter,
	RenderBox *cb, bool outside, bool outsideEnd, CaretBoxIterator &iter) /*KDE_NO_EXPORT*/
{
  int _x = cb->xPos();
  int _y = cb->yPos();
  int height;
  int width = 1;		// no override is indicated in boxes

  if (outside) {

    RenderStyle *s = cb->element() && cb->element()->parent()
			&& cb->element()->parent()->renderer()
			? cb->element()->parent()->renderer()->style()
			: cb->style();
    bool rtl = s->direction() == RTL;

    const TQFontMetrics &fm = s->fontMetrics();
    height = fm.height();

    if (!outsideEnd) {
        _x--;
    } else {
        _x += cb->width();
    }

    int hl = fm.leading() / 2;
    int baseline = cb->baselinePosition(false);
    if (!cb->isReplaced() || cb->style()->display() == BLOCK) {
        if (!outsideEnd ^ rtl)
            _y -= fm.leading() / 2;
        else
            _y += kMax(cb->height() - fm.ascent() - hl, 0);
    } else {
        _y += baseline - fm.ascent() - hl;
    }

  } else {		// !outside

    RenderStyle *s = cb->style();
    const TQFontMetrics &fm = s->fontMetrics();
    height = fm.height();

    _x += cb->borderLeft() + cb->paddingLeft();
    _y += cb->borderTop() + cb->paddingTop();

    // ### regard direction
    switch (s->textAlign()) {
      case LEFT:
      case TDEHTML_LEFT:
      case TAAUTO:	// ### find out what this does
      case JUSTIFY:
        break;
      case CENTER:
      case TDEHTML_CENTER:
        _x += cb->contentWidth() / 2;
        break;
      case TDEHTML_RIGHT:
      case RIGHT:
        _x += cb->contentWidth();
        break;
    }/*end switch*/
  }/*end if*/

  CaretBoxLine *result = new CaretBoxLine;
  deleter->append(result);
  result->caret_boxes.append(new CaretBox(_x, _y, width, height, cb,
  			outside, outsideEnd));
  iter = result->begin();
  return result;
}

#if DEBUG_CARETMODE > 0
void CaretBoxLine::dump(TQTextStream &ts, const TQString &ind) const
{
  ts << ind << "cbl: baseFlowBox@" << basefb << endl;
  TQString ind2 = ind + "  ";
  for (size_t i = 0; i < caret_boxes.size(); i++) {
    if (i > 0) ts << endl;
    caret_boxes[i]->dump(ts, ind2);
  }
}
#endif

// == caret mode related helper functions

/** seeks the root line box that is the parent of the given inline box.
 * @param b given inline box
 * @param base base render object which not to step over. If \c base's
 *	inline flow box is hit before the root line box, the flow box
 *	is returned instead.
 * @return the root line box or the base flow box.
 */
inline InlineFlowBox *seekBaseFlowBox(InlineBox *b, RenderObject *base = 0)
{
  // Seek root line box or base inline flow box, if \c base is interfering.
  while (b->parent() && b->object() != base) {
    b = b->parent();
  }/*wend*/
  Q_ASSERT(b->isInlineFlowBox());
  return static_cast<InlineFlowBox *>(b);
}

/** determines whether the given element is a block level replaced element.
 */
inline bool isBlockRenderReplaced(RenderObject *r)
{
  return r->isRenderReplaced() && r->style()->display() == BLOCK;
}

/** determines the caret line box that contains the given position.
 *
 * If the node does not map to a render object, the function will snap to
 * the next suitable render object following it.
 *
 * @param node node to begin with
 * @param offset zero-based offset within node.
 * @param cblDeleter deleter for caret box lines
 * @param base base render object which the caret must not be placed beyond.
 * @param r_ofs adjusted offset within render object
 * @param caretBoxIt returns an iterator to the caret box that contains the
 *	given position.
 * @return the determined caret box lineor 0 if either the node is 0 or
 *	there is no inline flow box containing this node. The containing block
 *	will still be set. If it is 0 too, @p node was invalid.
 */
static CaretBoxLine* findCaretBoxLine(DOM::NodeImpl *node, long offset,
		CaretBoxLineDeleter *cblDeleter, RenderObject *base,
                long &r_ofs, CaretBoxIterator &caretBoxIt)
{
  bool outside, outsideEnd;
  RenderObject *r = findRenderer(node, offset, base, r_ofs, outside, outsideEnd);
  if (!r) { return 0; }
#if DEBUG_CARETMODE > 0
  kdDebug(6200) << "=================== findCaretBoxLine" << endl;
  kdDebug(6200) << "node " << node << " offset: " << offset << " r " << r->renderName() << "[" << r << "].node " << r->element()->nodeName().string() << "[" << r->element() << "]" << " r_ofs " << r_ofs << " outside " << outside << " outsideEnd " << outsideEnd << endl;
#endif

  // There are two strategies to find the correct line box. (The third is failsafe)
  // (A) First, if node's renderer is a RenderText, we only traverse its text
  // runs and return the root line box (saves much time for long blocks).
  // This should be the case 99% of the time.
  // (B) Second, we derive the inline flow box directly when the renderer is
  // a RenderBlock, RenderInline, or blocked RenderReplaced.
  // (C) Otherwise, we iterate linearly through all line boxes in order to find
  // the renderer.

  // (A)
  if (r->isText()) do {
    RenderText *t = static_cast<RenderText *>(r);
    int dummy;
    InlineBox *b = t->findInlineTextBox(offset, dummy, true);
    // Actually b should never be 0, but some render texts don't have text
    // boxes, so we insert the last run as an error correction.
    // If there is no last run, we resort to (B)
    if (!b) {
      if (t->m_lines.count() > 0)
        b = t->m_lines[t->m_lines.count() - 1];
      else
        break;
    }/*end if*/
    Q_ASSERT(b);
    outside = false;	// text boxes cannot have outside positions
    InlineFlowBox *baseFlowBox = seekBaseFlowBox(b, base);
#if DEBUG_CARETMODE > 2
  kdDebug(6200) << "text-box b: " << b << " baseFlowBox: " << baseFlowBox << (b && b->object() ? TQString(" contains \"%1\"").arg(TQConstString(static_cast<RenderText *>(b->object())->str->s+b->minOffset(), kMin(b->maxOffset() - b->minOffset(), 15L)).string()) : TQString::null) << endl;
#endif
#if 0
    if (t->containingBlock()->isListItem()) dumpLineBoxes(static_cast<RenderFlow *>(t->containingBlock()));
#endif
#if DEBUG_CARETMODE > 0
  kdDebug(6200) << "=================== end findCaretBoxLine (renderText)" << endl;
#endif
    return CaretBoxLine::constructCaretBoxLine(cblDeleter, baseFlowBox,
        b, outside, outsideEnd, caretBoxIt);
  } while(false);/*end if*/

  // (B)
  bool isrepl = isBlockRenderReplaced(r);
  if (r->isRenderBlock() || r->isRenderInline() || isrepl) {
    RenderFlow *flow = static_cast<RenderFlow *>(r);
    InlineFlowBox *firstLineBox = isrepl ? 0 : flow->firstLineBox();

    // On render blocks, if we are outside, or have a totally empty render
    // block, we simply construct a special caret box line.
    // The latter case happens only when the render block is a leaf object itself.
    if (isrepl || r->isRenderBlock() && (outside || !firstLineBox)
    		|| r->isRenderInline() && !firstLineBox) {
  #if DEBUG_CARETMODE > 0
    kdDebug(6200) << "=================== end findCaretBoxLine (box " << (outside ? (outsideEnd ? "outside end" : "outside begin") : "inside") << ")" << endl;
  #endif
      Q_ASSERT(r->isBox());
      return CaretBoxLine::constructCaretBoxLine(cblDeleter,
      		static_cast<RenderBox *>(r), outside, outsideEnd, caretBoxIt);
    }/*end if*/

  kdDebug(6200) << "firstlinebox " << firstLineBox << endl;
    InlineFlowBox *baseFlowBox = seekBaseFlowBox(firstLineBox, base);
    return CaretBoxLine::constructCaretBoxLine(cblDeleter, baseFlowBox,
        firstLineBox, outside, outsideEnd, caretBoxIt);
  }/*end if*/

  RenderBlock *cb = r->containingBlock();
  //if ( !cb ) return 0L;
  Q_ASSERT(cb);

  // ### which element doesn't have a block as its containing block?
  // Is it still possible after the RenderBlock/RenderInline merge?
  if (!cb->isRenderBlock()) {
    kdWarning() << "containing block is no render block!!! crash imminent" << endl;
  }/*end if*/

  InlineFlowBox *flowBox = cb->firstLineBox();
  // (C)
  // This case strikes when the element is replaced, but neither a
  // RenderBlock nor a RenderInline
  if (!flowBox) {	// ### utter emergency (why is this possible at all?)
//    flowBox = generateDummyFlowBox(arena, cb, r);
//    if (ibox) *ibox = flowBox->firstChild();
//    outside = outside_end = true;

//    kdWarning() << "containing block contains no inline flow boxes!!! crash imminent" << endl;
#if DEBUG_CARETMODE > 0
   kdDebug(6200) << "=================== end findCaretBoxLine (2)" << endl;
#endif
    return CaretBoxLine::constructCaretBoxLine(cblDeleter, cb,
			outside, outsideEnd, caretBoxIt);
  }/*end if*/

  // We iterate the inline flow boxes of the containing block until
  // we find the given node. This has one major flaw: it is linear, and therefore
  // painfully slow for really large blocks.
  for (; flowBox; flowBox = static_cast<InlineFlowBox *>(flowBox->nextLineBox())) {
#if DEBUG_CARETMODE > 0
    kdDebug(6200) << "[scan line]" << endl;
#endif

    // construct a caret line box and stop when the element is contained within
    InlineFlowBox *baseFlowBox = seekBaseFlowBox(flowBox, base);
    CaretBoxLine *cbl = CaretBoxLine::constructCaretBoxLine(cblDeleter,
        baseFlowBox, 0, outside, outsideEnd, caretBoxIt, r);
#if DEBUG_CARETMODE > 5
    kdDebug(6200) << cbl->information() << endl;
#endif
    if (caretBoxIt != cbl->end()) {
#if DEBUG_CARETMODE > 0
   kdDebug(6200) << "=================== end findCaretBoxLine (3)" << endl;
#endif
      return cbl;
    }
  }/*next flowBox*/

  // no inline flow box found, approximate to nearest following node.
  // Danger: this is O(n^2). It's only called to recover from
  // errors, that means, theoretically, never. (Practically, far too often :-( )
  Q_ASSERT(!flowBox);
  CaretBoxLine *cbl = findCaretBoxLine(nextLeafNode(node, base ? base->element() : 0), 0, cblDeleter, base, r_ofs, caretBoxIt);
#if DEBUG_CARETMODE > 0
  kdDebug(6200) << "=================== end findCaretBoxLine" << endl;
#endif
  return cbl;
}

/** finds the innermost table object @p r is contained within, but no
 * farther than @p cb.
 * @param r leaf element to begin with
 * @param cb bottom element where to stop search at least.
 * @return the table object or 0 if none found.
 */
static inline RenderTable *findTableUpTo(RenderObject *r, RenderFlow *cb)
{
  while (r && r != cb && !r->isTable()) r = r->parent();
  return r && r->isTable() ? static_cast<RenderTable *>(r) : 0;
}

/** checks whether @p r is a descendant of @p cb, or r == cb
 */
static inline bool isDescendant(RenderObject *r, RenderObject *cb)
{
  while (r && r != cb) r = r->parent();
  return r;
}

/** checks whether the given block contains at least one editable element.
 *
 * Warning: This function has linear complexity, and therefore is expensive.
 * Use it sparingly, and cache the result.
 * @param part part
 * @param cb block to be searched
 * @param table returns the nested table if there is one directly at the beginning
 *	or at the end.
 * @param fromEnd begin search from end (default: begin from beginning)
 */
static bool containsEditableElement(TDEHTMLPart *part, RenderBlock *cb,
	RenderTable *&table, bool fromEnd = false)
{
  RenderObject *r = cb;
  if (fromEnd)
    while (r->lastChild()) r = r->lastChild();
  else
    while (r->firstChild()) r = r->firstChild();

  RenderTable *tempTable = 0;
  table = 0;
  bool withinCb;
//  int state;		// not used
  ObjectTraversalState trav = InsideDescending;
  do {
    bool modWithinCb = withinCb = isDescendant(r, cb);

    // treat cb extra, it would not be considered otherwise
    if (!modWithinCb) {
      modWithinCb = true;
      r = cb;
    } else
      tempTable = findTableUpTo(r, cb);

#if DEBUG_CARETMODE > 1
    kdDebug(6201) << "cee: r " << (r ? r->renderName() : TQString::null) << "@" << r << " cb " << cb << " withinCb " << withinCb << " modWithinCb " << modWithinCb << " tempTable " << tempTable << endl;
#endif
    if (r && modWithinCb && r->element() && !isUnsuitable(r, trav)
    	&& (part->isCaretMode() || part->isEditable()
		|| r->style()->userInput() == UI_ENABLED)) {
      table = tempTable;
#if DEBUG_CARETMODE > 1
    kdDebug(6201) << "cee: editable" << endl;
#endif
      return true;
    }/*end if*/

//    RenderObject *oldr = r;
//    while (r && r == oldr)
//      r = advanceSuitableObject(r, trav, fromEnd, cb->parent(), state);
    r = fromEnd ? r->objectAbove() : r->objectBelow();
  } while (r && withinCb);
  return false;
}

/** checks whether the given block contains at least one editable child
 * element, beginning with but excluding @p start.
 *
 * Warning: This function has linear complexity, and therefore is expensive.
 * Use it sparingly, and cache the result.
 * @param part part
 * @param cb block to be searched
 * @param table returns the nested table if there is one directly before/after
 *	the start object.
 * @param fromEnd begin search from end (default: begin from beginning)
 * @param start object after which to begin search.
 */
static bool containsEditableChildElement(TDEHTMLPart *part, RenderBlock *cb,
	RenderTable *&table, bool fromEnd, RenderObject *start)
{
  int state = 0;
  ObjectTraversalState trav = OutsideAscending;
// kdDebug(6201) << "start: " << start << endl;
  RenderObject *r = start;
  do {
    r = traverseRenderObjects(r, trav, fromEnd, cb->parent(), state);
  } while(r && !(state & AdvancedToSibling));
// kdDebug(6201) << "r: " << r << endl;
  //advanceObject(start, trav, fromEnd, cb->parent(), state);
//     RenderObject *oldr = r;
//     while (r && r == oldr)
  if (!r) return false;

  if (fromEnd)
    while (r->firstChild()) r = r->firstChild();
  else
    while (r->lastChild()) r = r->lastChild();
// kdDebug(6201) << "child r: " << r << endl;
  if (!r) return false;

  RenderTable *tempTable = 0;
  table = 0;
  bool withinCb = false;
  do {

    bool modWithinCb = withinCb = isDescendant(r, cb);

    // treat cb extra, it would not be considered otherwise
    if (!modWithinCb) {
      modWithinCb = true;
      r = cb;
    } else
      tempTable = findTableUpTo(r, cb);

#if DEBUG_CARETMODE > 1
    kdDebug(6201) << "cece: r " << (r ? r->renderName() : TQString::null) << "@" << r << " cb " << cb << " withinCb " << withinCb << " modWithinCb " << modWithinCb << " tempTable " << tempTable << endl;
#endif
    if (r && withinCb && r->element() && !isUnsuitable(r, trav)
    	&& (part->isCaretMode() || part->isEditable()
		|| r->style()->userInput() == UI_ENABLED)) {
      table = tempTable;
#if DEBUG_CARETMODE > 1
    kdDebug(6201) << "cece: editable" << endl;
#endif
      return true;
    }/*end if*/

    r = fromEnd ? r->objectAbove() : r->objectBelow();
  } while (withinCb);
  return false;
}

// == class LinearDocument implementation

LinearDocument::LinearDocument(TDEHTMLPart *part, NodeImpl *node, long offset,
  		CaretAdvancePolicy advancePolicy, ElementImpl *baseElem)
	: node(node), offset(offset), m_part(part),
	advPol(advancePolicy), base(0)
{
  if (node == 0) return;

  if (baseElem) {
    RenderObject *b = baseElem->renderer();
    if (b && (b->isRenderBlock() || b->isRenderInline()))
      base = b;
  }

  initPreBeginIterator();
  initEndIterator();
}

LinearDocument::~LinearDocument()
{
}

int LinearDocument::count() const
{
  // FIXME: not implemented
  return 1;
}

LinearDocument::Iterator LinearDocument::current()
{
  return LineIterator(this, node, offset);
}

LinearDocument::Iterator LinearDocument::begin()
{
  NodeImpl *n = base ? base->element() : 0;
  if (!base) n = node ? node->getDocument() : 0;
  if (!n) return end();

  n = n->firstChild();
  if (advPol == LeafsOnly)
    while (n->firstChild()) n = n->firstChild();

  if (!n) return end();		// must be empty document or empty base element
  return LineIterator(this, n, n->minOffset());
}

LinearDocument::Iterator LinearDocument::preEnd()
{
  NodeImpl *n = base ? base->element() : 0;
  if (!base) n = node ? node->getDocument() : 0;
  if (!n) return preBegin();

  n = n->lastChild();
  if (advPol == LeafsOnly)
    while (n->lastChild()) n = n->lastChild();

  if (!n) return preBegin();	// must be empty document or empty base element
  return LineIterator(this, n, n->maxOffset());
}

void LinearDocument::initPreBeginIterator()
{
  _preBegin = LineIterator(this, 0, 0);
}

void LinearDocument::initEndIterator()
{
  _end = LineIterator(this, 0, 1);
}

// == class LineIterator implementation

CaretBoxIterator LineIterator::currentBox /*KDE_NO_EXPORT*/;
long LineIterator::currentOffset /*KDE_NO_EXPORT*/;

LineIterator::LineIterator(LinearDocument *l, DOM::NodeImpl *node, long offset)
		: lines(l)
{
//  kdDebug(6200) << "LineIterator: node " << node << " offset " << offset << endl;
  if (!node) { cbl = 0; return; }
  cbl = findCaretBoxLine(node, offset, &lines->cblDeleter,
  		l->baseObject(), currentOffset, currentBox);
  // can happen on partially loaded documents
#if DEBUG_CARETMODE > 0
  if (!cbl) kdDebug(6200) << "no render object found!" << endl;
#endif
  if (!cbl) return;
#if DEBUG_CARETMODE > 1
  kdDebug(6200) << "LineIterator: offset " << offset << " outside " << cbl->isOutside() << endl;
#endif
#if DEBUG_CARETMODE > 3
  kdDebug(6200) << cbl->information() << endl;
#endif
  if (currentBox == cbl->end()) {
#if DEBUG_CARETMODE > 0
    kdDebug(6200) << "LineIterator: findCaretBoxLine failed" << endl;
#endif
    cbl = 0;
  }/*end if*/
}

void LineIterator::nextBlock()
{
  RenderObject *base = lines->baseObject();

  bool cb_outside = cbl->isOutside();
  bool cb_outside_end = cbl->isOutsideEnd();

  {
    RenderObject *r = cbl->enclosingObject();

    ObjectTraversalState trav;
    int state;		// not used
    mapRenderPosToTraversalState(cb_outside, cb_outside_end, false, trav);
#if DEBUG_CARETMODE > 1
    kdDebug(6200) << "nextBlock: before adv r" << r << " " << (r ? r->renderName() : TQString::null) << (r && r->isText() ? " contains \"" + TQString(((RenderText *)r)->str->s, TQMIN(((RenderText *)r)->str->l,15)) + "\"" : TQString::null) << " trav " << trav << " cb_outside " << cb_outside << " cb_outside_end " << cb_outside_end << endl;
#endif
    r = advanceSuitableObject(r, trav, false, base, state);
    if (!r) {
      cbl = 0;
      return;
    }/*end if*/

    mapTraversalStateToRenderPos(trav, false, cb_outside, cb_outside_end);
#if DEBUG_CARETMODE > 1
    kdDebug(6200) << "nextBlock: after r" << r << " trav " << trav << " cb_outside " << cb_outside << " cb_outside_end " << cb_outside_end << endl;
#endif
#if DEBUG_CARETMODE > 0
    kdDebug(6200) << "++: r " << r << "[" << (r?r->renderName():TQString::null) << "]" << endl;
#endif

    RenderBlock *cb;

    // If we hit a block or replaced object, use this as its enclosing object
    bool isrepl = isBlockRenderReplaced(r);
    if (r->isRenderBlock() || isrepl) {
      RenderBox *cb = static_cast<RenderBox *>(r);

      cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter, cb,
			cb_outside, cb_outside_end, currentBox);

#if DEBUG_CARETMODE > 0
      kdDebug(6200) << "r->isFlow is cb. continuation @" << cb->continuation() << endl;
#endif
      return;
    } else {
      cb = r->containingBlock();
      Q_ASSERT(cb->isRenderBlock());
    }/*end if*/
    InlineFlowBox *flowBox = cb->firstLineBox();
#if DEBUG_CARETMODE > 0
    kdDebug(6200) << "++: flowBox " << flowBox << " cb " << cb << "[" << (cb?cb->renderName()+TQString(".node ")+TQString::number((unsigned)cb->element(),16)+(cb->element()?"@"+cb->element()->nodeName().string():TQString::null):TQString::null) << "]" << endl;
#endif
    Q_ASSERT(flowBox);
    if (!flowBox) {	// ### utter emergency (why is this possible at all?)
      cb_outside = cb_outside_end = true;
      cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter, cb,
			cb_outside, cb_outside_end, currentBox);
      return;
    }

    bool seekOutside = false, seekOutsideEnd = false; // silence gcc uninit warning
    CaretBoxIterator it;
    cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter,
  	flowBox, flowBox->firstChild(), seekOutside, seekOutsideEnd, it);
  }
}

void LineIterator::prevBlock()
{
  RenderObject *base = lines->baseObject();

  bool cb_outside = cbl->isOutside();
  bool cb_outside_end = cbl->isOutsideEnd();

  {
    RenderObject *r = cbl->enclosingObject();
    if (r->isAnonymous() && !cb_outside)
      cb_outside = true, cb_outside_end = false;

    ObjectTraversalState trav;
    int state;		// not used
    mapRenderPosToTraversalState(cb_outside, cb_outside_end, true, trav);
#if DEBUG_CARETMODE > 1
    kdDebug(6200) << "prevBlock: before adv r" << r << " " << (r ? r->renderName() : TQString::null) << (r && r->isText() ? " contains \"" + TQString(((RenderText *)r)->str->s, TQMIN(((RenderText *)r)->str->l,15)) + "\"" : TQString::null) << " trav " << trav << " cb_outside " << cb_outside << " cb_outside_end " << cb_outside_end << endl;
#endif
    r = advanceSuitableObject(r, trav, true, base, state);
    if (!r) {
      cbl = 0;
      return;
    }/*end if*/

    mapTraversalStateToRenderPos(trav, true, cb_outside, cb_outside_end);
#if DEBUG_CARETMODE > 1
    kdDebug(6200) << "prevBlock: after r" << r << " trav " << trav << " cb_outside " << cb_outside << " cb_outside_end " << cb_outside_end << endl;
#endif
#if DEBUG_CARETMODE > 0
    kdDebug(6200) << "--: r " << r << "[" << (r?r->renderName():TQString::null) << "]" << endl;
#endif

    RenderBlock *cb;

    // If we hit a block, use this as its enclosing object
    bool isrepl = isBlockRenderReplaced(r);
//    kdDebug(6200) << "isrepl " << isrepl << " isblock " << r->isRenderBlock() << endl;
    if (r->isRenderBlock() || isrepl) {
      RenderBox *cb = static_cast<RenderBox *>(r);

      cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter, cb,
			cb_outside, cb_outside_end, currentBox);

#if DEBUG_CARETMODE > 0
      kdDebug(6200) << "r->isFlow is cb. continuation @" << cb->continuation() << endl;
#endif
      return;
    } else {
      cb = r->containingBlock();
      Q_ASSERT(cb->isRenderBlock());
    }/*end if*/
    InlineFlowBox *flowBox = cb->lastLineBox();
#if DEBUG_CARETMODE > 0
    kdDebug(6200) << "--: flowBox " << flowBox << " cb " << cb << "[" << (cb?cb->renderName()+TQString(".node ")+TQString::number((unsigned)cb->element(),16)+(cb->element()?"@"+cb->element()->nodeName().string():TQString::null):TQString::null) << "]" << endl;
#endif
    Q_ASSERT(flowBox);
    if (!flowBox) {	// ### utter emergency (why is this possible at all?)
      cb_outside = true; cb_outside_end = false;
      cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter, cb,
			cb_outside, cb_outside_end, currentBox);
      return;
    }

    bool seekOutside = false, seekOutsideEnd = false; // silence gcc uninit warning
    CaretBoxIterator it;
    cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter,
  	flowBox, flowBox->firstChild(), seekOutside, seekOutsideEnd, it);
  }
}

void LineIterator::advance(bool toBegin)
{
  InlineFlowBox *flowBox = cbl->baseFlowBox();
  if (flowBox) {
    flowBox = static_cast<InlineFlowBox *>(toBegin ? flowBox->prevLineBox() : flowBox->nextLineBox());
    if (flowBox) {
      bool seekOutside = false, seekOutsideEnd = false; // silence gcc uninit warning
      CaretBoxIterator it;
      cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter,
          flowBox, flowBox->firstChild(), seekOutside, seekOutsideEnd, it);
    }/*end if*/
  }/*end if*/

  // if there are no more lines in this block, move towards block to come
  if (!flowBox) { if (toBegin) prevBlock(); else nextBlock(); }

#if DEBUG_CARETMODE > 3
  if (cbl) kdDebug(6200) << cbl->information() << endl;
#endif
}

// == class EditableCaretBoxIterator implementation

void EditableCaretBoxIterator::advance(bool toBegin)
{
#if DEBUG_CARETMODE > 3
      kdDebug(6200) << "---------------" << k_funcinfo << "toBegin " << toBegin << endl;
#endif
  const CaretBoxIterator preBegin = cbl->preBegin();
  const CaretBoxIterator end = cbl->end();

  CaretBoxIterator lastbox = *this, curbox;
  bool islastuseable = true;	// silence gcc
  bool iscuruseable;
  // Assume adjacency of caret boxes. Will be falsified later if applicable.
  adjacent = true;

#if DEBUG_CARETMODE > 4
//       kdDebug(6200) << "ebit::advance: before: " << (**this)->object() << "@" << (**this)->object()->renderName() << ".node " << (**this)->object()->element() << "[" << ((**this)->object()->element() ? (**this)->object()->element()->nodeName().string() : TQString::null) << "] inline " << (**this)->isInline() << " outside " << (**this)->isOutside() << " outsideEnd " << (**this)->isOutsideEnd() << endl;
#endif

  if (toBegin) CaretBoxIterator::operator --(); else CaretBoxIterator::operator ++();
  bool curAtEnd = *this == preBegin || *this == end;
  curbox = *this;
  bool atEnd = true;
  if (!curAtEnd) {
    iscuruseable = isEditable(curbox, toBegin);
    if (toBegin) CaretBoxIterator::operator --(); else CaretBoxIterator::operator ++();
    atEnd = *this == preBegin || *this == end;
  }
  while (!curAtEnd) {
    bool haslast = lastbox != end && lastbox != preBegin;
    bool hascoming = !atEnd;
    bool iscominguseable = true; // silence gcc

    if (!atEnd) iscominguseable = isEditable(*this, toBegin);
    if (iscuruseable) {
#if DEBUG_CARETMODE > 3
      kdDebug(6200) << "ebit::advance: " << (*curbox)->object() << "@" << (*curbox)->object()->renderName() << ".node " << (*curbox)->object()->element() << "[" << ((*curbox)->object()->element() ? (*curbox)->object()->element()->nodeName().string() : TQString::null) << "] inline " << (*curbox)->isInline() << " outside " << (*curbox)->isOutside() << " outsideEnd " << (*curbox)->isOutsideEnd() << endl;
#endif

      CaretBox *box = *curbox;
      if (box->isOutside()) {
        // if this caret box represents no inline box, it is an outside box
	// which has to be considered unconditionally
        if (!box->isInline()) break;

        if (advpol == VisibleFlows) break;

	// IndicatedFlows and LeafsOnly are treated equally in caret box lines

	InlineBox *ibox = box->inlineBox();
	// get previous inline box
	InlineBox *prev = box->isOutsideEnd() ? ibox : ibox->prevOnLine();
	// get next inline box
	InlineBox *next = box->isOutsideEnd() ? ibox->nextOnLine() : ibox;

	const bool isprevindicated = !prev || isIndicatedInlineBox(prev);
	const bool isnextindicated = !next || isIndicatedInlineBox(next);
	const bool last = haslast && !islastuseable;
	const bool coming = hascoming && !iscominguseable;
	const bool left = !prev || prev->isInlineFlowBox() && isprevindicated
		|| (toBegin && coming || !toBegin && last);
	const bool right = !next || next->isInlineFlowBox() && isnextindicated
		|| (!toBegin && coming || toBegin && last);
        const bool text2indicated = toBegin && next && next->isInlineTextBox()
                && isprevindicated
            || !toBegin && prev && prev->isInlineTextBox() && isnextindicated;
        const bool indicated2text = !toBegin && next && next->isInlineTextBox()
                && prev && isprevindicated
            // ### this code is so broken.
            /*|| toBegin && prev && prev->isInlineTextBox() && isnextindicated*/;
#if DEBUG_CARETMODE > 5
      kdDebug(6200) << "prev " << prev << " haslast " << haslast << " islastuseable " << islastuseable << " left " << left << " next " << next << " hascoming " << hascoming << " iscominguseable " << iscominguseable << " right " << right << " text2indicated " << text2indicated << " indicated2text " << indicated2text << endl;
#endif

	if (left && right && !text2indicated || indicated2text) {
	  adjacent = false;
#if DEBUG_CARETMODE > 4
      kdDebug(6200) << "left && right && !text2indicated || indicated2text" << endl;
#endif
	  break;
	}

      } else {
        // inside boxes are *always* valid
#if DEBUG_CARETMODE > 4
if (box->isInline()) {
        InlineBox *ibox = box->inlineBox();
      kdDebug(6200) << "inside " << (!ibox->isInlineFlowBox() || static_cast<InlineFlowBox *>(ibox)->firstChild() ? "non-empty" : "empty") << (isIndicatedInlineBox(ibox) ? " indicated" : "") << " adjacent=" << adjacent << endl;
    }
#if 0
  RenderStyle *s = ibox->object()->style();
  kdDebug(6200)	<< "bordls " << s->borderLeftStyle()
  		<< " bordl " << (s->borderLeftStyle() != BNONE)
  		<< " bordr " << (s->borderRightStyle() != BNONE)
  		<< " bordt " << (s->borderTopStyle() != BNONE)
      		<< " bordb " << (s->borderBottomStyle() != BNONE)
		<< " padl " << s->paddingLeft().value()
                << " padr " << s->paddingRight().value()
      		<< " padt " << s->paddingTop().value()
                << " padb " << s->paddingBottom().value()
	// ### Can inline elements have top/bottom margins? Couldn't find
	// it in the CSS 2 spec, but Mozilla ignores them, so we do, too.
		<< " marl " << s->marginLeft().value()
                << " marr " << s->marginRight().value()
      		<< endl;
#endif
#endif
        break;
      }/*end if*/

    } else {

      if (!(*curbox)->isOutside()) {
        // cannot be adjacent anymore
	adjacent = false;
      }

    }/*end if*/
    lastbox = curbox;
    islastuseable = iscuruseable;
    curbox = *this;
    iscuruseable = iscominguseable;
    curAtEnd = atEnd;
    if (!atEnd) {
      if (toBegin) CaretBoxIterator::operator --(); else CaretBoxIterator::operator ++();
      atEnd = *this == preBegin || *this == end;
    }/*end if*/
  }/*wend*/

  *static_cast<CaretBoxIterator *>(this) = curbox;
#if DEBUG_CARETMODE > 4
//  kdDebug(6200) << "still valid? " << (*this != preBegin && *this != end) << endl;
#endif
#if DEBUG_CARETMODE > 3
      kdDebug(6200) << "---------------" << k_funcinfo << "end " << endl;
#endif
}

bool EditableCaretBoxIterator::isEditable(const CaretBoxIterator &boxit, bool fromEnd)
{
  Q_ASSERT(boxit != cbl->end() && boxit != cbl->preBegin());
  CaretBox *b = *boxit;
  RenderObject *r = b->object();
#if DEBUG_CARETMODE > 0
//  if (b->isInlineFlowBox()) kdDebug(6200) << "b is inline flow box" << (outside ? " (outside)" : "") << endl;
  kdDebug(6200) << "isEditable r" << r << ": " << (r ? r->renderName() : TQString::null) << (r && r->isText() ? " contains \"" + TQString(((RenderText *)r)->str->s, TQMIN(((RenderText *)r)->str->l,15)) + "\"" : TQString::null) << endl;
#endif
  // Must check caret mode or design mode *after* r->element(), otherwise
  // lines without a backing DOM node get regarded, leading to a crash.
  // ### check should actually be in InlineBoxIterator
  NodeImpl *node = r->element();
  ObjectTraversalState trav;
  mapRenderPosToTraversalState(b->isOutside(), b->isOutsideEnd(), fromEnd, trav);
  if (isUnsuitable(r, trav) || !node) {
    return false;
  }

  // generally exclude replaced elements with no children from navigation
  if (!b->isOutside() && r->isRenderReplaced() && !r->firstChild())
    return false;

  RenderObject *eff_r = r;
  bool globallyNavigable = m_part->isCaretMode() || m_part->isEditable();

  // calculate the parent element's editability if this inline box is outside.
  if (b->isOutside() && !globallyNavigable) {
    NodeImpl *par = node->parent();
    // I wonder whether par can be 0. It shouldn't be possible if the
    // algorithm contained no bugs.
    Q_ASSERT(par);
    if (par) node = par;
    eff_r = node->renderer();
    Q_ASSERT(eff_r);	// this is a hard requirement
  }

  bool result = globallyNavigable || eff_r->style()->userInput() == UI_ENABLED;
#if DEBUG_CARETMODE > 0
  kdDebug(6200) << result << endl;
#endif
  return result;
}

// == class EditableLineIterator implementation

void EditableLineIterator::advance(bool toBegin)
{
  CaretAdvancePolicy advpol = lines->advancePolicy();
  LineIterator lasteditable, lastindicated;
  bool haslasteditable = false;
  bool haslastindicated = false;
  bool uselasteditable = false;

  LineIterator::advance(toBegin);
  while (cbl) {
    if (isEditable(*this)) {
#if DEBUG_CARETMODE > 3
      kdDebug(6200) << "advance: " << cbl->enclosingObject() << "@" << cbl->enclosingObject()->renderName() << ".node " << cbl->enclosingObject()->element() << "[" << (cbl->enclosingObject()->element() ? cbl->enclosingObject()->element()->nodeName().string() : TQString::null) << "]" << endl;
#endif

      bool hasindicated = isIndicatedFlow(cbl->enclosingObject());
      if (hasindicated) {
        haslastindicated = true;
        lastindicated = *this;
      }

      switch (advpol) {
        case IndicatedFlows:
          if (hasindicated) goto wend;
          // fall through
        case LeafsOnly:
          if (cbl->isOutside()) break;
          // fall through
        case VisibleFlows: goto wend;
      }/*end switch*/

      // remember rejected editable element
      lasteditable = *this;
      haslasteditable = true;
#if DEBUG_CARETMODE > 4
      kdDebug(6200) << "remembered lasteditable " << *lasteditable << endl;
#endif
    } else {

      // If this element isn't editable, but the last one was, and it was only
      // rejected because it didn't match the caret advance policy, force it.
      // Otherwise certain combinations of editable and uneditable elements
      // could never be reached with some policies.
      if (haslasteditable) { uselasteditable = true; break; }

    }
    LineIterator::advance(toBegin);
  }/*wend*/
wend:

  if (uselasteditable) *this = haslastindicated ? lastindicated : lasteditable;
  if (!cbl && haslastindicated) *this = lastindicated;
}

// == class EditableCharacterIterator implementation

void EditableCharacterIterator::initFirstChar()
{
  CaretBox *box = *ebit;
  InlineBox *b = box->inlineBox();
  if (_offset == box->maxOffset())
    peekNext();
  else if (b && !box->isOutside() && b->isInlineTextBox())
    _char = static_cast<RenderText *>(b->object())->str->s[_offset].unicode();
  else
    _char = -1;
}

/** returns true when the given caret box is empty, i. e. should not
 * take place in caret movement.
 */
static inline bool isCaretBoxEmpty(CaretBox *box) {
  if (!box->isInline()) return false;
  InlineBox *ibox = box->inlineBox();
  return ibox->isInlineFlowBox()
  		&& !static_cast<InlineFlowBox *>(ibox)->firstChild()
      		&& !isIndicatedInlineBox(ibox);
}

EditableCharacterIterator &EditableCharacterIterator::operator ++()
{
  _offset++;

  CaretBox *box = *ebit;
  InlineBox *b = box->inlineBox();
  long maxofs = box->maxOffset();
#if DEBUG_CARETMODE > 0
  kdDebug(6200) << "box->maxOffset() " << box->maxOffset() << " box->minOffset() " << box->minOffset() << endl;
#endif
  if (_offset == maxofs) {
#if DEBUG_CARETMODE > 2
kdDebug(6200) << "_offset == maxofs: " << _offset << " == " << maxofs << endl;
#endif
    peekNext();
  } else if (_offset > maxofs) {
#if DEBUG_CARETMODE > 2
kdDebug(6200) << "_offset > maxofs: " << _offset << " > " << maxofs /*<< " _peekNext: " << _peekNext*/ << endl;
#endif
    if (/*!_peekNext*/true) {
      ++ebit;
      if (ebit == (*_it)->end()) {	// end of line reached, go to next line
        ++_it;
#if DEBUG_CARETMODE > 3
kdDebug(6200) << "++_it" << endl;
#endif
        if (_it != _it.lines->end()) {
	  ebit = _it;
          box = *ebit;
          b = box->inlineBox();
#if DEBUG_CARETMODE > 3
kdDebug(6200) << "box " << box << " b " << b << " isText " << box->isInlineTextBox() << endl;
#endif

#if DEBUG_CARETMODE > 3
	  RenderObject *_r = box->object();
kdDebug(6200) << "_r " << _r << ":" << _r->element()->nodeName().string() << endl;
#endif
	  _offset = box->minOffset();
#if DEBUG_CARETMODE > 3
kdDebug(6200) << "_offset " << _offset << endl;
#endif
	} else {
          b = 0;
	  _end = true;
	}/*end if*/
        goto readchar;
      }/*end if*/
    }/*end if*/

    bool adjacent = ebit.isAdjacent();
#if 0
    // Jump over element if this one is not a text node.
    if (adjacent && !(*ebit)->isInlineTextBox()) {
      EditableCaretBoxIterator copy = ebit;
      ++ebit;
      if (ebit != (*_it)->end() && (*ebit)->isInlineTextBox()
          /*&& (!(*ebit)->isInlineFlowBox()
              || static_cast<InlineFlowBox *>(*ebit)->)*/)
        adjacent = false;
      else ebit = copy;
    }/*end if*/
#endif
    // Jump over empty elements.
    if (adjacent && !(*ebit)->isInlineTextBox()) {
      bool noemptybox = true;
      while (isCaretBoxEmpty(*ebit)) {
        noemptybox = false;
        EditableCaretBoxIterator copy = ebit;
        ++ebit;
        if (ebit == (*_it)->end()) { ebit = copy; break; }
      }
      if (noemptybox) adjacent = false;
    }/*end if*/
//     _r = (*ebit)->object();
    /*if (!_it.outside) */_offset = (*ebit)->minOffset() + adjacent;
    //_peekNext = 0;
    box = *ebit;
    b = box->inlineBox();
    goto readchar;
  } else {
readchar:
    // get character
    if (b && !box->isOutside() && b->isInlineTextBox() && _offset < b->maxOffset())
      _char = static_cast<RenderText *>(b->object())->str->s[_offset].unicode();
    else
      _char = -1;
  }/*end if*/
#if DEBUG_CARETMODE > 2
kdDebug(6200) << "_offset: " << _offset /*<< " _peekNext: " << _peekNext*/ << " char '" << (char)_char << "'" << endl;
#endif

#if DEBUG_CARETMODE > 0
  if (!_end && ebit != (*_it)->end()) {
    CaretBox *box = *ebit;
    RenderObject *_r = box->object();
    kdDebug(6200) << "echit++(1): box " << box << (box && box->isInlineTextBox() ? TQString(" contains \"%1\"").arg(TQConstString(static_cast<RenderText *>(box->object())->str->s+box->minOffset(), box->maxOffset() - box->minOffset()).string()) : TQString::null) << " _r " << (_r ? _r->element()->nodeName().string() : TQString("<nil>")) << endl;
  }
#endif
  return *this;
}

EditableCharacterIterator &EditableCharacterIterator::operator --()
{
  _offset--;
  //kdDebug(6200) << "--: _offset=" << _offset << endl;

  CaretBox *box = *ebit;
  CaretBox *_peekPrev = 0;
  CaretBox *_peekNext = 0;
  InlineBox *b = box->inlineBox();
  long minofs = box->minOffset();
#if DEBUG_CARETMODE > 0
  kdDebug(6200) << "box->maxOffset() " << box->maxOffset() << " box->minOffset() " << box->minOffset() << endl;
#endif
  if (_offset == minofs) {
#if DEBUG_CARETMODE > 2
kdDebug(6200) << "_offset == minofs: " << _offset << " == " << minofs << endl;
#endif
//     _peekNext = b;
    // get character
    if (b && !box->isOutside() && b->isInlineTextBox())
      _char = static_cast<RenderText *>(b->object())->text()[_offset].unicode();
    else
      _char = -1;

    //peekPrev();
    bool do_prev = false;
    {
      EditableCaretBoxIterator copy;
      _peekPrev = 0;
      do {
        copy = ebit;
        --ebit;
        if (ebit == (*_it)->preBegin()) { ebit = copy; break; }
      } while (isCaretBoxEmpty(*ebit));
      // Jump to end of previous element if it's adjacent, and a text box
      if (ebit.isAdjacent() && ebit != (*_it)->preBegin() && (*ebit)->isInlineTextBox()) {
        _peekPrev = *ebit;
	do_prev = true;
      } else
        ebit = copy;
    }
    if (do_prev) goto prev;
  } else if (_offset < minofs) {
prev:
#if DEBUG_CARETMODE > 2
kdDebug(6200) << "_offset < minofs: " << _offset << " < " << minofs /*<< " _peekNext: " << _peekNext*/ << endl;
#endif
    if (!_peekPrev) {
      _peekNext = *ebit;
      --ebit;
      if (ebit == (*_it)->preBegin()) { // end of line reached, go to previous line
        --_it;
#if DEBUG_CARETMODE > 3
kdDebug(6200) << "--_it" << endl;
#endif
        if (_it != _it.lines->preBegin()) {
// 	  kdDebug(6200) << "begin from end!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
	  ebit = EditableCaretBoxIterator(_it, true);
          box = *ebit;
// 	  RenderObject *r = box->object();
#if DEBUG_CARETMODE > 3
kdDebug(6200) << "box " << box << " b " << box->inlineBox() << " isText " << box->isInlineTextBox() << endl;
#endif
          _offset = box->maxOffset();
// 	  if (!_it.outside) _offset = r->isBR() ? (*ebit)->minOffset() : (*ebit)->maxOffset();
	  _char = -1;
#if DEBUG_CARETMODE > 0
          kdDebug(6200) << "echit--(2): box " << box << " b " << box->inlineBox() << (box->isInlineTextBox() ? TQString(" contains \"%1\"").arg(TQConstString(static_cast<RenderText *>(box->object())->str->s+box->minOffset(), box->maxOffset() - box->minOffset()).string()) : TQString::null) << endl;
#endif
	} else
	  _end = true;
	return *this;
      }/*end if*/
    }/*end if*/

#if DEBUG_CARETMODE > 0
    bool adjacent = ebit.isAdjacent();
    kdDebug(6200) << "adjacent " << adjacent << " _peekNext " << _peekNext << " _peekNext->isInlineTextBox: " << (_peekNext ? _peekNext->isInlineTextBox() : false) << " !((*ebit)->isInlineTextBox): " << (*ebit ? !(*ebit)->isInlineTextBox() : true) << endl;
#endif
#if 0
    // Ignore this box if it isn't a text box, but the previous box was
    if (adjacent && _peekNext && _peekNext->isInlineTextBox()
    	&& !(*ebit)->isInlineTextBox()) {
      EditableCaretBoxIterator copy = ebit;
      --ebit;
      if (ebit == (*_it)->preBegin())
				/*adjacent = false;
      else */
				ebit = copy;
    }/*end if*/
#endif
#if 0
    // Jump over empty elements.
    if (adjacent //&& _peekNext && _peekNext->isInlineTextBox()
        && !(*ebit)->isInlineTextBox()) {
      bool noemptybox = true;
      while (isCaretBoxEmpty(*ebit)) {
        noemptybox = false;
        EditableCaretBoxIterator copy = ebit;
        --ebit;
        if (ebit == (*_it)->preBegin()) { ebit = copy; break; }
        else _peekNext = *copy;
      }
      if (noemptybox) adjacent = false;
    }/*end if*/
#endif
#if DEBUG_CARETMODE > 0
    kdDebug(6200) << "(*ebit)->obj " << (*ebit)->object()->renderName() << "[" << (*ebit)->object() << "]" << " minOffset: " << (*ebit)->minOffset() << " maxOffset: " << (*ebit)->maxOffset() << endl;
#endif
#if DEBUG_CARETMODE > 3
    RenderObject *_r = (*ebit)->object();
kdDebug(6200) << "_r " << _r << ":" << _r->element()->nodeName().string() << endl;
#endif
    _offset = (*ebit)->maxOffset();
//     if (!_it.outside) _offset = (*ebit)->maxOffset()/* - adjacent*/;
#if DEBUG_CARETMODE > 3
kdDebug(6200) << "_offset " << _offset << endl;
#endif
    _peekPrev = 0;
  } else {
#if DEBUG_CARETMODE > 0
kdDebug(6200) << "_offset: " << _offset << " _peekNext: " << _peekNext << endl;
#endif
    // get character
    if (_peekNext && _offset >= box->maxOffset() && _peekNext->isInlineTextBox())
      _char = static_cast<RenderText *>(_peekNext->object())->text()[_peekNext->minOffset()].unicode();
    else if (b && _offset < b->maxOffset() && b->isInlineTextBox())
      _char = static_cast<RenderText *>(b->object())->text()[_offset].unicode();
    else
      _char = -1;
  }/*end if*/

#if DEBUG_CARETMODE > 0
  if (!_end && ebit != (*_it)->preBegin()) {
    CaretBox *box = *ebit;
    kdDebug(6200) << "echit--(1): box " << box << " b " << box->inlineBox() << (box->isInlineTextBox() ? TQString(" contains \"%1\"").arg(TQConstString(static_cast<RenderText *>(box->object())->str->s+box->minOffset(), box->maxOffset() - box->minOffset()).string()) : TQString::null) << endl;
  }
#endif
  return *this;
}

// == class TableRowIterator implementation

TableRowIterator::TableRowIterator(RenderTable *table, bool fromEnd,
  		RenderTableSection::RowStruct *row)
		: sec(table, fromEnd)
{
  // set index
  if (*sec) {
    if (fromEnd) index = (*sec)->grid.size() - 1;
    else index = 0;
  }/*end if*/

  // initialize with given row
  if (row && *sec) {
    while (operator *() != row)
      if (fromEnd) operator --(); else operator ++();
  }/*end if*/
}

TableRowIterator &TableRowIterator::operator ++()
{
  index++;

  if (index >= (int)(*sec)->grid.size()) {
    ++sec;

    if (*sec) index = 0;
  }/*end if*/
  return *this;
}

TableRowIterator &TableRowIterator::operator --()
{
  index--;

  if (index < 0) {
    --sec;

    if (*sec) index = (*sec)->grid.size() - 1;
  }/*end if*/
  return *this;
}

// == class ErgonomicEditableLineIterator implementation

// some decls
static RenderTableCell *findNearestTableCellInRow(TDEHTMLPart *part, int x,
		RenderTableSection::RowStruct *row, bool fromEnd);

/** finds the cell corresponding to absolute x-coordinate @p x in the given
 * table.
 *
 * If there is no direct cell, or the cell is not accessible, the function
 * will return the nearest suitable cell.
 * @param part part containing the document
 * @param x absolute x-coordinate
 * @param it table row iterator, will be adapted accordingly as more rows are
 *	investigated.
 * @param fromEnd @p true to begin search from end and work towards the
 *	beginning
 * @return the cell, or 0 if no editable cell was found.
 */
static inline RenderTableCell *findNearestTableCell(TDEHTMLPart *part, int x,
		TableRowIterator &it, bool fromEnd)
{
  RenderTableCell *result = 0;

  while (*it) {
    result = findNearestTableCellInRow(part, x, *it, fromEnd);
    if (result) break;

    if (fromEnd) --it; else ++it;
  }/*wend*/

  return result;
}

/** finds the nearest editable cell around the given absolute x-coordinate
 *
 * It will dive into nested tables as necessary to provide seamless navigation.
 *
 * If the cell at @p x is not editable, its left neighbor is tried, then its
 * right neighbor, then the left neighbor's left neighbor etc. If no
 * editable cell can be found, 0 is returned.
 * @param part tdehtml part
 * @param x absolute x-coordinate
 * @param row table row to be searched
 * @param fromEnd @p true, begin from end (applies only to nested tables)
 * @return the found cell or 0 if no editable cell was found
 */
static RenderTableCell *findNearestTableCellInRow(TDEHTMLPart *part, int x,
		RenderTableSection::RowStruct *row, bool fromEnd)
{
  // First pass. Find spatially nearest cell.
  int n = (int)row->row->size();
  int i;
  for (i = 0; i < n; i++) {
    RenderTableCell *cell = row->row->at(i);
    if (!cell || (long)cell == -1) continue;

    int absx, absy;
    cell->absolutePosition(absx, absy, false); // ### position: fixed?
#if DEBUG_CARETMODE > 1
    kdDebug(6201) << "i/n " << i << "/" << n << " absx " << absx << " absy " << absy << endl;
#endif

    // I rely on the assumption that all cells are in ascending visual order
    // ### maybe this assumption is wrong for bidi?
#if DEBUG_CARETMODE > 1
    kdDebug(6201) << "x " << x << " < " << (absx + cell->width()) << "?" << endl;
#endif
    if (x < absx + cell->width()) break;
  }/*next i*/
  if (i >= n) i = n - 1;

  // Second pass. Find editable cell, beginning with the currently found,
  // extending to the left, and to the right, alternating.
  for (int cnt = 0; cnt < 2*n; cnt++) {
    int index = i - ((cnt >> 1) + 1)*(cnt & 1) + (cnt >> 1)*!(cnt & 1);
    if (index < 0 || index >= n) continue;

    RenderTableCell *cell = row->row->at(index);
    if (!cell || (long)cell == -1) continue;

#if DEBUG_CARETMODE > 1
    kdDebug(6201) << "index " << index << " cell " << cell << endl;
#endif
    RenderTable *nestedTable;
    if (containsEditableElement(part, cell, nestedTable, fromEnd)) {

      if (nestedTable) {
        TableRowIterator it(nestedTable, fromEnd);
	while (*it) {
// kdDebug(6201) << "=== recursive invocation" << endl;
          cell = findNearestTableCell(part, x, it, fromEnd);
	  if (cell) break;
	  if (fromEnd) --it; else ++it;
	}/*wend*/
      }/*end if*/

      return cell;
    }/*end if*/
  }/*next i*/
  return 0;
}

/** returns the nearest common ancestor of two objects that is a table cell,
 * a table section, or 0 if not inside a common table.
 *
 * If @p r1 and @p r2 belong to the same table, but different sections, @p r1's
 * section is returned.
 */
static RenderObject *commonAncestorTableSectionOrCell(RenderObject *r1,
		RenderObject *r2)
{
  if (!r1 || !r2) return 0;
  RenderTableSection *sec = 0;
  int start_depth=0, end_depth=0;
  // First we find the depths of the two objects in the tree (start_depth, end_depth)
  RenderObject *n = r1;
  while (n->parent()) {
    n = n->parent();
    start_depth++;
  }/*wend*/
  n = r2;
  while( n->parent()) {
    n = n->parent();
    end_depth++;
  }/*wend*/
  // here we climb up the tree with the deeper object, until both objects have equal depth
  while (end_depth > start_depth) {
    r2 = r2->parent();
    end_depth--;
  }/*wend*/
  while (start_depth > end_depth) {
    r1 = r1->parent();
//    if (r1->isTableSection()) sec = static_cast<RenderTableSection *>(r1);
    start_depth--;
  }/*wend*/
  // Climb the tree with both r1 and r2 until they are the same
  while (r1 != r2){
    r1 = r1->parent();
    if (r1->isTableSection()) sec = static_cast<RenderTableSection *>(r1);
    r2 = r2->parent();
  }/*wend*/

  // At this point, we found the most approximate common ancestor. Now climb
  // up until the condition of the function return value is satisfied.
  while (r1 && !r1->isTableCell() && !r1->isTableSection() && !r1->isTable())
    r1 = r1->parent();

  return r1 && r1->isTable() ? sec : r1;
}

/** Finds the row that contains the given cell, directly, or indirectly
 * @param section section to be searched
 * @param cell table cell
 * @param row returns the row
 * @param directCell returns the direct cell that contains @p cell
 * @return the index of the row.
 */
static int findRowInSection(RenderTableSection *section, RenderTableCell *cell,
		RenderTableSection::RowStruct *&row, RenderTableCell *&directCell)
{
  // Seek direct cell
  RenderObject *r = cell;
  while (r != section) {
    if (r->isTableCell()) directCell = static_cast<RenderTableCell *>(r);
    r = r->parent();
  }/*wend*/

  // So, and this is really nasty: As we have no indices, we have to do a
  // linear comparison. Oh, that sucks so much for long tables, you can't
  // imagine.
  int n = section->numRows();
  for (int i = 0; i < n; i++) {
    row = &section->grid[i];

    // check for cell
    int m = row->row->size();
    for (int j = 0; j < m; j++) {
      RenderTableCell *c = row->row->at(j);
      if (c == directCell) return i;
    }/*next j*/

  }/*next i*/
  Q_ASSERT(false);
  return -1;
}

/** finds the table that is the first direct or indirect descendant of @p block.
 * @param leaf object to begin search from.
 * @param block object to search to, or 0 to search up to top.
 * @return the table or 0 if there were none.
 */
static inline RenderTable *findFirstDescendantTable(RenderObject *leaf, RenderBlock *block)
{
  RenderTable *result = 0;
  while (leaf && leaf != block) {
    if (leaf->isTable()) result = static_cast<RenderTable *>(leaf);
    leaf = leaf->parent();
  }/*wend*/
  return result;
}

/** looks for the table cell the given object @p r is contained within.
 * @return the table cell or 0 if not contained in any table.
 */
static inline RenderTableCell *containingTableCell(RenderObject *r)
{
  while (r && !r->isTableCell()) r = r->parent();
  return static_cast<RenderTableCell *>(r);
}

inline void ErgonomicEditableLineIterator::calcAndStoreNewLine(
			RenderBlock *newBlock, bool toBegin)
{
  // take the first/last editable element in the found cell as the new
  // value for the iterator
  CaretBoxIterator it;
  cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter,
  	newBlock, true, toBegin, it);
#if DEBUG_CARETMODE > 3
  kdDebug(6201) << cbl->information() << endl;
#endif
//  if (toBegin) prevBlock(); else nextBlock();

  if (!cbl) {
    return;
  }/*end if*/

  EditableLineIterator::advance(toBegin);
}

void ErgonomicEditableLineIterator::determineTopologicalElement(
		RenderTableCell *oldCell, RenderObject *newObject, bool toBegin)
{
  // When we arrive here, a transition between cells has happened.
  // Now determine the type of the transition. This can be
  // (1) a transition from this cell into a table inside this cell.
  // (2) a transition from this cell into another cell of this table

  TableRowIterator it;

  RenderObject *commonAncestor = commonAncestorTableSectionOrCell(oldCell, newObject);
#if DEBUG_CARETMODE > 1
  kdDebug(6201) << " ancestor " << commonAncestor << endl;
#endif

  // The whole document is treated as a table cell.
  if (!commonAncestor || commonAncestor->isTableCell()) {	// (1)

    RenderTableCell *cell = static_cast<RenderTableCell *>(commonAncestor);
    RenderTable *table = findFirstDescendantTable(newObject, cell);

#if DEBUG_CARETMODE > 0
    kdDebug(6201) << "table cell: " << cell << endl;
#endif

    // if there is no table, we fell out of the previous table, and are now
    // in some table-less block. Therefore, done.
    if (!table) return;

    it = TableRowIterator(table, toBegin);

  } else if (commonAncestor->isTableSection()) {		// (2)

    RenderTableSection *section = static_cast<RenderTableSection *>(commonAncestor);
    RenderTableSection::RowStruct *row;
    int idx = findRowInSection(section, oldCell, row, oldCell);
#if DEBUG_CARETMODE > 1
    kdDebug(6201) << "table section: row idx " << idx << endl;
#endif

    it = TableRowIterator(section, idx);

    // advance rowspan rows
    int rowspan = oldCell->rowSpan();
    while (*it && rowspan--) {
      if (toBegin) --it; else ++it;
    }/*wend*/

  } else {
    kdError(6201) << "Neither common cell nor section! " << commonAncestor->renderName() << endl;
    // will crash on uninitialized table row iterator
  }/*end if*/

  RenderTableCell *cell = findNearestTableCell(lines->m_part, xCoor, it, toBegin);
#if DEBUG_CARETMODE > 1
  kdDebug(6201) << "findNearestTableCell result: " << cell << endl;
#endif

  RenderBlock *newBlock = cell;
  if (!cell) {
    Q_ASSERT(commonAncestor->isTableSection());
    RenderTableSection *section = static_cast<RenderTableSection *>(commonAncestor);
    cell = containingTableCell(section);
#if DEBUG_CARETMODE > 1
    kdDebug(6201) << "containing cell: " << cell << endl;
#endif

    RenderTable *nestedTable;
    bool editableChild = cell && containsEditableChildElement(lines->m_part,
    		cell, nestedTable, toBegin, section->table());

    if (cell && !editableChild) {
#if DEBUG_CARETMODE > 1
      kdDebug(6201) << "========= recursive invocation outer =========" << endl;
#endif
      determineTopologicalElement(cell, cell->section(), toBegin);
#if DEBUG_CARETMODE > 1
      kdDebug(6201) << "========= end recursive invocation outer =========" << endl;
#endif
      return;

    } else if (cell && nestedTable) {
#if DEBUG_CARETMODE > 1
      kdDebug(6201) << "========= recursive invocation inner =========" << endl;
#endif
      determineTopologicalElement(cell, nestedTable, toBegin);
#if DEBUG_CARETMODE > 1
      kdDebug(6201) << "========= end recursive invocation inner =========" << endl;
#endif
      return;

    } else {
#if DEBUG_CARETMODE > 1
      kdDebug(6201) << "newBlock is table: " << section->table() << endl;
#endif
      RenderObject *r = section->table();
      int state;		// not used
      ObjectTraversalState trav = OutsideAscending;
      r = advanceSuitableObject(r, trav, toBegin, lines->baseObject(), state);
      if (!r) { cbl = 0;  return; }
//      if (toBegin) prevBlock(); else nextBlock();
      newBlock = static_cast<RenderBlock *>(!r || r->isRenderBlock() ? r : r->containingBlock());
    }/*end if*/
#if 0
  } else {
    // adapt cell so that prevBlock/nextBlock works as expected
    newBlock = cell;
    // on forward advancing, we must start from the outside end of the
    // previous object
    if (!toBegin) {
      RenderObject *r = newBlock;
      int state;		// not used
      ObjectTraversalState trav = OutsideAscending;
      r = advanceSuitableObject(r, trav, true, lines->advancePolicy(), lines->baseObject(), state);
      newBlock = static_cast<RenderBlock *>(!r || r->isRenderBlock() ? r : r->containingBlock());
    }/*end if*/
#endif
  }/*end if*/

  calcAndStoreNewLine(newBlock, toBegin);
}

ErgonomicEditableLineIterator &ErgonomicEditableLineIterator::operator ++()
{
  RenderTableCell *oldCell = containingTableCell(cbl->enclosingObject());

  EditableLineIterator::operator ++();
  if (*this == lines->end() || *this == lines->preBegin()) return *this;

  RenderTableCell *newCell = containingTableCell(cbl->enclosingObject());

  if (!newCell || newCell == oldCell) return *this;

  determineTopologicalElement(oldCell, newCell, false);

  return *this;
}

ErgonomicEditableLineIterator &ErgonomicEditableLineIterator::operator --()
{
  RenderTableCell *oldCell = containingTableCell(cbl->enclosingObject());

  EditableLineIterator::operator --();
  if (*this == lines->end() || *this == lines->preBegin()) return *this;

  RenderTableCell *newCell = containingTableCell(cbl->enclosingObject());

  if (!newCell || newCell == oldCell) return *this;

  determineTopologicalElement(oldCell, newCell, true);

  return *this;
}

// == Navigational helper functions ==

/** seeks the caret box which contains or is the nearest to @p x
 * @param it line iterator pointing to line to be searched
 * @param cv caret view context
 * @param x returns the cv->origX approximation, relatively positioned to the
 *	containing block.
 * @param absx returns absolute x-coordinate of containing block
 * @param absy returns absolute y-coordinate of containing block
 * @return the most suitable caret box
 */
static CaretBox *nearestCaretBox(LineIterator &it, CaretViewContext *cv,
	int &x, int &absx, int &absy)
{
  // Find containing block
  RenderObject *cb = (*it)->containingBlock();
#if DEBUG_CARETMODE > 4
  kdDebug(6200) << "nearestCB: cb " << cb << "@" << (cb ? cb->renderName() : "") << endl;
#endif

  if (cb) cb->absolutePosition(absx, absy);
  else absx = absy = 0;

  // Otherwise find out in which inline box the caret is to be placed.

  // this horizontal position is to be approximated
  x = cv->origX - absx;
  CaretBox *caretBox = 0; // Inline box containing the caret
//  NodeImpl *lastnode = 0;  // node of previously checked render object.
  int xPos;		   // x-coordinate of current inline box
  int oldXPos = -1;	   // x-coordinate of last inline box
  EditableCaretBoxIterator fbit = it;
#if DEBUG_CARETMODE > 0
/*  if (it.linearDocument()->advancePolicy() != LeafsOnly)
    kdWarning() << "nearestInlineBox is only prepared to handle the LeafsOnly advance policy" << endl;*/
//   kdDebug(6200) << "*fbit = " << *fbit << endl;
#endif
  // Iterate through all children
  for (CaretBox *b; fbit != (*it)->end(); ++fbit) {
    b = *fbit;

#if DEBUG_CARETMODE > 0
//    RenderObject *r = b->object();
//	if (b->isInlineFlowBox()) kdDebug(6200) << "b is inline flow box" << endl;
//	kdDebug(6200) << "approximate r" << r << ": " << (r ? r->renderName() : TQString::null) << (r && r->isText() ? " contains \"" + TQString(((RenderText *)r)->str->s, ((RenderText *)r)->str->l) + "\"" : TQString::null) << endl;
#endif
    xPos = b->xPos();

    // the caret is before this box
    if (x < xPos) {
      // snap to nearest box
      if (oldXPos < 0 || x - (oldXPos + caretBox->width()) > xPos - x) {
	caretBox = b;	// current box is nearer
      }/*end if*/
      break;		// Otherwise, preceding box is implicitly used
    }

    caretBox = b;

    // the caret is within this box
    if (x >= xPos && x < xPos + caretBox->width())
      break;
    oldXPos = xPos;

    // the caret can only be after the last box which is automatically
    // contained in caretBox when we fall out of the loop.
  }/*next fbit*/

  return caretBox;
}

/** moves the given iterator to the beginning of the next word.
 *
 * If the end is reached, the iterator will be positioned there.
 * @param it character iterator to be moved
 */
static void moveItToNextWord(EditableCharacterIterator &it)
{
#if DEBUG_CARETMODE > 0
  kdDebug(6200) << "%%%%%%%%%%%%%%%%%%%%% moveItToNextWord" << endl;
#endif
  EditableCharacterIterator copy;
  while (!it.isEnd() && !(*it).isSpace() && !(*it).isPunct()) {
#if DEBUG_CARETMODE > 2
    kdDebug(6200) << "reading1 '" << (*it).latin1() << "'" << endl;
#endif
    copy = it;
    ++it;
  }

  if (it.isEnd()) {
    it = copy;
    return;
  }/*end if*/

  while (!it.isEnd() && ((*it).isSpace() || (*it).isPunct())) {
#if DEBUG_CARETMODE > 2
    kdDebug(6200) << "reading2 '" << (*it).latin1() << "'" << endl;
#endif
    copy = it;
    ++it;
  }

  if (it.isEnd()) it = copy;
}

/** moves the given iterator to the beginning of the previous word.
 *
 * If the beginning is reached, the iterator will be positioned there.
 * @param it character iterator to be moved
 */
static void moveItToPrevWord(EditableCharacterIterator &it)
{
  if (it.isEnd()) return;

#if DEBUG_CARETMODE > 0
  kdDebug(6200) << "%%%%%%%%%%%%%%%%%%%%% moveItToPrevWord" << endl;
#endif
  EditableCharacterIterator copy;

  // Jump over all space and punctuation characters first
  do {
    copy = it;
    --it;
#if DEBUG_CARETMODE > 2
    if (!it.isEnd()) kdDebug(6200) << "reading1 '" << (*it).latin1() << "'" << endl;
#endif
  } while (!it.isEnd() && ((*it).isSpace() || (*it).isPunct()));

  if (it.isEnd()) {
    it = copy;
    return;
  }/*end if*/

  do {
    copy = it;
    --it;
#if DEBUG_CARETMODE > 0
    if (!it.isEnd()) kdDebug(6200) << "reading2 '" << (*it).latin1() << "' (" << (int)(*it).latin1() << ") box " << it.caretBox() << endl;
#endif
  } while (!it.isEnd() && !(*it).isSpace() && !(*it).isPunct());

  it = copy;
#if DEBUG_CARETMODE > 1
    if (!it.isEnd()) kdDebug(6200) << "effective '" << (*it).latin1() << "' (" << (int)(*it).latin1() << ") box " << it.caretBox() << endl;
#endif
}

/** moves the iterator by one page.
 * @param ld linear document
 * @param it line iterator, will be updated accordingly
 * @param mindist minimum distance in pixel the iterator should be moved
 *	(if possible)
 * @param next @p true, move downward, @p false move upward
 */
static void moveIteratorByPage(LinearDocument &ld,
		ErgonomicEditableLineIterator &it, int mindist, bool next)
{
  // ### This whole routine plainly sucks. Use an inverse strategie for pgup/pgdn.

  if (it == ld.end() || it == ld.preBegin()) return;

  ErgonomicEditableLineIterator copy = it;
#if DEBUG_CARETMODE > 0
  kdDebug(6200) << " mindist: " << mindist << endl;
#endif

  CaretBoxLine *cbl = *copy;
  int absx = 0, absy = 0;

  RenderBlock *lastcb = cbl->containingBlock();
  Q_ASSERT(lastcb->isRenderBlock());
  lastcb->absolutePosition(absx, absy, false);	// ### what about fixed?

  int lastfby = cbl->begin().data()->yPos();
  int lastheight = 0;
  int rescue = 1000;	// ### this is a hack to keep stuck carets from hanging the ua
  do {
    if (next) ++copy; else --copy;
    if (copy == ld.end() || copy == ld.preBegin()) break;

    cbl = *copy;
    RenderBlock *cb = cbl->containingBlock();

    int diff = 0;
    // ### actually flowBox->yPos() should suffice, but this is not ported
    // over yet from WebCore
    int fby = cbl->begin().data()->yPos();
    if (cb != lastcb) {
      if (next) {
        diff = absy + lastfby + lastheight;
        cb->absolutePosition(absx, absy, false);	// ### what about fixed?
        diff = absy - diff + fby;
        lastfby = 0;
      } else {
        diff = absy;
        cb->absolutePosition(absx, absy, false);	// ### what about fixed?
        diff -= absy + fby + lastheight;
	lastfby = fby - lastheight;
      }/*end if*/
#if DEBUG_CARETMODE > 2
      kdDebug(6200) << "absdiff " << diff << endl;
#endif
    } else {
      diff = kAbs(fby - lastfby);
    }/*end if*/
#if DEBUG_CARETMODE > 2
    kdDebug(6200) << "cbl->begin().data()->yPos(): " << fby << " diff " << diff << endl;
#endif

    mindist -= diff;

    lastheight = kAbs(fby - lastfby);
    lastfby = fby;
    lastcb = cb;
    it = copy;
#if DEBUG_CARETMODE > 0
    kdDebug(6200) << " mindist: " << mindist << endl;
#endif
    // trick: actually the distance is always one line short, but we cannot
    // calculate the height of the first line (### WebCore will make it better)
    // Therefore, we simply approximate that excess line by using the last
    // caluculated line height.
  } while (mindist - lastheight > 0 && --rescue);
}


}/*end namespace*/