summaryrefslogtreecommitdiffstats
path: root/kviewshell/kmultipage.cpp
blob: 3ba3fa842acf6c158cad5fb0307757244bc3ebbf (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
#include <config.h>

#include <kaction.h>
#include <kapplication.h>
#include <kdebug.h>
#include <kfiledialog.h>
#include <kiconloader.h>
#include <kio/job.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kprinter.h>
#include <kstdaction.h>
#include <tqobject.h>
#include <tqlayout.h>
#include <tqpaintdevicemetrics.h>
#include <tqprogressdialog.h>
#include <tqsplitter.h>
#include <tqurl.h>
#include <tqtoolbox.h>
#include <tqvbox.h>

#include "documentWidget.h"
#include "marklist.h"
#include "tableOfContents.h"
#include "kprintDialogPage_pageoptions.h"
#include "kvsprefs.h"
#include "kmultipage.h"
#include "pageNumber.h"
#include "renderedDocumentPagePrinter.h"
#include "searchWidget.h"
#include "textBox.h"
#include "zoomlimits.h"


//#define DEBUG_KMULTIPAGE

KMultiPage::KMultiPage(TQWidget *parentWidget, const char *widgetName, TQObject *tqparent, const char *name)
  : DCOPObject("kmultipage"), KParts::ReadOnlyPart(tqparent, name)
{
  // For reasons which I don't understand, the initialization of the
  // DCOPObject above does not work properly, the name is ignored. It
  // works fine if we repeat the name here. -- Stefan Kebekus
  // This is because of the virtual inheritance. Get rid of it (but it's BC, and this is a lib...) -- DF
  setObjId("kmultipage");

  parentWdg = parentWidget;
  lastCurrentPage = 0;
  timer_id = -1;
  searchInProgress = false;
  
  TQVBox* verticalBox = new TQVBox(parentWidget);
  verticalBox->setFocusPolicy(TQ_StrongFocus);
  setWidget(verticalBox);
  
  splitterWidget = new TQSplitter(verticalBox, widgetName);
  splitterWidget->setOpaqueResize(false);
  splitterWidget->tqsetSizePolicy(TQSizePolicy::MinimumExpanding, TQSizePolicy::MinimumExpanding);
  
  // Create SideBar
  sideBar = new TQToolBox(splitterWidget, "sidebar");
  
  // Create ContentsList
  tableOfContents = new TableOfContents(sideBar);
  sideBar->addItem(tableOfContents, TQIconSet(SmallIcon("contents")), i18n("Contents"));

  connect(tableOfContents, TQT_SIGNAL(gotoPage(const Anchor&)), this, TQT_SLOT(gotoPage(const Anchor&)));
  
  // Create MarkList
  _markList = new MarkList(sideBar, "marklist");
  sideBar->addItem(_markList, TQIconSet(SmallIcon("thumbnail")), i18n("Thumbnails"));

  // Restore state of the sidebar
  sideBar->setCurrentItem(sideBar->item(KVSPrefs::sideBarItem()));

  splitterWidget->setResizeMode(sideBar, TQSplitter::KeepSize);

  connect(_markList, TQT_SIGNAL(selected(const PageNumber&)), this, TQT_SLOT(gotoPage(const PageNumber&)));

  _scrollView = new PageView(splitterWidget, widgetName);

  // Create Search Panel
  searchWidget = new SearchWidget(verticalBox);
  searchWidget->hide();
  connect(searchWidget, TQT_SIGNAL(findNextText()), this, TQT_SLOT(findNextText()));
  connect(searchWidget, TQT_SIGNAL(findPrevText()), this, TQT_SLOT(findPrevText()));

  sideBar->setMinimumWidth(80);
  sideBar->setMaximumWidth(300);

  connect(_scrollView, TQT_SIGNAL(currentPageChanged(const PageNumber&)), this, TQT_SLOT(setCurrentPageNumber(const PageNumber&)));
  connect(_scrollView, TQT_SIGNAL(viewSizeChanged(const TQSize&)), scrollView(), TQT_SLOT(calculateCurrentPageNumber()));
  connect(_scrollView, TQT_SIGNAL(wheelEventReceived(TQWheelEvent *)), this, TQT_SLOT(wheelEvent(TQWheelEvent*)));

  connect(this, TQT_SIGNAL(enableMoveTool(bool)), _scrollView, TQT_SLOT(slotEnableMoveTool(bool)));

  splitterWidget->setCollapsible(sideBar, false);
  splitterWidget->setSizes(KVSPrefs::guiLayout());

  connect(searchWidget, TQT_SIGNAL(searchEnabled(bool)), this, TQT_SIGNAL(searchEnabled(bool)));
  connect(searchWidget, TQT_SIGNAL(stopSearch()), this, TQT_SLOT(stopSearch()));
}


KMultiPage::~KMultiPage()
{
  writeSettings();

  if (timer_id != -1)
    killTimer(timer_id);

  delete pageCache;
}

void KMultiPage::readSettings()
{
}

void KMultiPage::writeSettings()
{
  // Save TOC tqlayout
  tableOfContents->writeSettings();

  KVSPrefs::setGuiLayout(splitterWidget->sizes());
  // Save state of the sidebar
  KVSPrefs::setSideBarItem(sideBar->indexOf(sideBar->currentItem()));
  KVSPrefs::writeConfig();
}

TQString KMultiPage::name_of_current_file()
{
  return m_file;
}

bool KMultiPage::is_file_loaded(const TQString& filename)
{
  return (filename == m_file);
}

void KMultiPage::slotSave_defaultFilename()
{
  slotSave();
}

void KMultiPage::slotSave()
{
  // Try to guess the proper ending...
  TQString formats;
  TQString ending;
  int rindex = m_file.findRev(".");
  if (rindex == -1) {
    ending = TQString();
    formats = TQString();
  } else {
    ending = m_file.mid(rindex); // e.g. ".dvi"
    formats = fileFormats().grep(ending).join("\n");
  }

  TQString fileName = KFileDialog::getSaveFileName(TQString(), formats, 0, i18n("Save File As"));

  if (fileName.isEmpty())
    return;

  // Add the ending to the filename. I hope the user likes it that
  // way.
  if (!ending.isEmpty() && fileName.find(ending) == -1)
    fileName = fileName+ending;

  if (TQFile(fileName).exists()) {
    int r = KMessageBox::warningContinueCancel (0, i18n("The file %1\nexists. Shall I overwrite that file?").tqarg(fileName),
				       i18n("Overwrite File"), i18n("Overwrite"));
    if (r == KMessageBox::Cancel)
      return;
  }

  KIO::Job *job = KIO::file_copy( KURL( m_file ), KURL( fileName ), 0600, true, false, true );
  connect( job, TQT_SIGNAL( result( KIO::Job * ) ), this, TQT_SLOT( slotIOJobFinished ( KIO::Job * ) ) );

  return;
}


void KMultiPage::setFile(bool)
{
  return;
}


bool KMultiPage::closeURL()
{
#ifdef DEBUG_KMULTIPAGE
  kdDebug(1233) << "KMultiPage::closeURL()" << endl;
#endif

  if (renderer.isNull())
    return false;
  
  // Clear navigation history.
  document_history.clear();
  
  // Close the file.
  renderer->setFile(TQString(), KURL());
  renderer->clear();
  
  // Delete Page Widgets.
  widgetList.setAutoDelete(true);
  widgetList.resize(0);
  widgetList.setAutoDelete(false);
  
  // Update ScrollView.
  scrollView()->layoutPages();
  enableActions(false);
  
  // Clear Thumbnail List.
  markList()->clear();
  
  // Clear Table of Contents
  tableOfContents->clear();
  
  // Clear tqStatus Bar
  emit setStatusBarText(TQString());

  return true;
}

void KMultiPage::slotIOJobFinished ( KIO::Job *job )
{
  if ( job->error() )
    job->showErrorDialog( 0L );
}

void KMultiPage::slotShowScrollbars(bool status)
{
  _scrollView->slotShowScrollbars(status);
}

void KMultiPage::slotShowSidebar(bool show)
{
  if (show)
    sideBar->show();
  else
    sideBar->hide();
}

void KMultiPage::slotShowThumbnails(bool show)
{
  markList()->slotShowThumbnails(show);
}

void KMultiPage::slotSetFullPage(bool fullpage)
{
  _scrollView->setFullScreenMode(fullpage);
  if (fullpage)
    slotShowSidebar(false);
}

void KMultiPage::preferencesChanged()
{
  // We need to read the config options otherwise the KVSPrefs-object would
  // not be syncronized between the kviewpart and the kmultipage.
  KVSPrefs::self()->readConfig();

  slotShowThumbnails(KVSPrefs::showThumbnails());

  // if we are in overviewmode and the number of columns or rows has changed
  if (scrollView()->overviewMode() &&
      (scrollView()->getNrColumns() != KVSPrefs::overviewModeColumns() ||
       scrollView()->getNrRows() != KVSPrefs::overviewModeRows()))
  {
    setViewMode(KVSPrefs::EnumViewMode::Overview);
  }

  if (KVSPrefs::changeColors() && KVSPrefs::renderMode() == KVSPrefs::EnumRenderMode::Paper)
    renderer->setAccessibleBackground(true, KVSPrefs::paperColor());
  else
    renderer->setAccessibleBackground(false);

  renderModeChanged();
}

void KMultiPage::setViewMode(int mode)
{
#ifdef DEBUG_KMULTIPAGE
  kdDebug(1233) << "KMultiPage::setViewMode(" << mode << ")" << endl;
#endif
  // Save the current page number because when we are changing the columns
  // and rows in the scrollview the currently shown Page probably out of view.
  PageNumber currentPage = currentPageNumber();

  // Save viewMode for future uses of KViewShell
  switch (mode) 
  {
    case KVSPrefs::EnumViewMode::SinglePage:
      KVSPrefs::setViewMode(KVSPrefs::EnumViewMode::SinglePage);

      // Don't do anything if the view mode is already set
      if ((scrollView()->getNrColumns() == 1) && (scrollView()->getNrRows() == 1) && (scrollView()->isContinuous() == false))
        return;
      
      scrollView()->setNrColumns(1);
      scrollView()->setNrRows(1);
      scrollView()->setContinuousViewMode(false);
      // We scroll the view to the top, so that top and not the bottom
      // of the visible page is shown.
      scrollView()->scrollTop();
      break;
    case KVSPrefs::EnumViewMode::ContinuousFacing:
      KVSPrefs::setViewMode(KVSPrefs::EnumViewMode::ContinuousFacing);

      // Don't do anything if the view mode is already set
      if ((scrollView()->getNrColumns() == 2) && (scrollView()->getNrRows() == 1) && (scrollView()->isContinuous() == true))
        return;

      scrollView()->setNrColumns(2);
      scrollView()->setNrRows(1);
      scrollView()->setContinuousViewMode(true);
      break;
    case KVSPrefs::EnumViewMode::Overview:
      KVSPrefs::setViewMode(KVSPrefs::EnumViewMode::Overview);

      // Don't do anything if the view mode is already set
      if ((scrollView()->getNrColumns() == KVSPrefs::overviewModeColumns()) && (scrollView()->getNrRows() == KVSPrefs::overviewModeRows()) && (scrollView()->isContinuous() == false))
        return;

      scrollView()->setNrColumns(KVSPrefs::overviewModeColumns());
      scrollView()->setNrRows(KVSPrefs::overviewModeRows());
      scrollView()->setContinuousViewMode(false);
      // We scroll the view to the top, so that top and not the bottom
      // of the visible tableau is shown.
      scrollView()->scrollTop();
      break;
    default:  //KVSPrefs::EnumViewMode::Continuous
      KVSPrefs::setViewMode(KVSPrefs::EnumViewMode::Continuous);

      // Don't do anything if the view mode is already set
      if ((scrollView()->getNrColumns() == 1) && (scrollView()->getNrRows() == 1) && (scrollView()->isContinuous() == true))
        return;
      
      scrollView()->setNrColumns(1);
      scrollView()->setNrRows(1);
      scrollView()->setContinuousViewMode(true);
  }
  generateDocumentWidgets(currentPage);
  KVSPrefs::writeConfig();
  emit viewModeChanged();
}

void KMultiPage::initializePageCache()
{
  pageCache = new DocumentPageCache();
}

DocumentWidget* KMultiPage::createDocumentWidget()
{
  DocumentWidget* documentWidget = new DocumentWidget(scrollView()->viewport(), scrollView(), pageCache, "singlePageWidget");
  connect(documentWidget, TQT_SIGNAL(clearSelection()), this, TQT_SLOT(clearSelection()));
  connect(this, TQT_SIGNAL(enableMoveTool(bool)), documentWidget, TQT_SLOT(slotEnableMoveTool(bool)));
  return documentWidget;
}


void KMultiPage::generateDocumentWidgets(const PageNumber& _startPage)
{
  PageNumber startPage = _startPage;
#ifdef DEBUG_KMULTIPAGE
  kdDebug(1233) << "KMultiPage::generateDocumentWidgets(" << startPage << ")" << endl;
#endif

  // Do nothing if no document is loaded.
  if (getRenderer().isNull() || getRenderer()->isEmpty())
    return;

  // This function is only called with an invalid pagenumber, when
  // the file has been loaded or reloaded.
  bool reload = !startPage.isValid();

  if (reload)
  {
    // Find the number of the current page, for later use.
    startPage = currentPageNumber();
  }

  // Make sure that startPage is in the permissible range.
  if (startPage < 1)
    startPage = 1;
  if (startPage > numberOfPages())
    startPage = numberOfPages();

  unsigned int tableauStartPage = startPage;

  // Find out how many widgets are needed, and resize the widgetList accordingly.
  widgetList.setAutoDelete(true);
  TQ_UINT16 oldwidgetListSize = widgetList.size();
  if (numberOfPages() == 0)
    widgetList.resize(0);
  else
  {
    switch (KVSPrefs::viewMode())
    {
      case KVSPrefs::EnumViewMode::SinglePage:
        widgetList.resize(1);
        break;
      case KVSPrefs::EnumViewMode::Overview:
      {
        // Calculate the number of pages shown in overview mode.
        unsigned int visiblePages = KVSPrefs::overviewModeColumns() * KVSPrefs::overviewModeRows();
        // Calculate the number of the first page in the tableau.
        tableauStartPage = startPage - ((startPage - 1) % visiblePages);
        // We cannot have more widgets then pages in the document.
        visiblePages = TQMIN(visiblePages, numberOfPages() - tableauStartPage + 1);
        if (widgetList.size() != visiblePages)
          widgetList.resize(visiblePages);
        break;
      }
      default:
        // In KVS_Continuous and KVS_ContinuousFacing all pages in the document are shown.
        widgetList.resize(numberOfPages());
    }
  }
  bool isWidgetListResized = (widgetList.size() != oldwidgetListSize);
  widgetList.setAutoDelete(false);

  // If the widgetList is empty, there is nothing left to do.
  if (widgetList.size() == 0) {
    scrollView()->addChild(&widgetList);
    return;
  }

  // Allocate DocumentWidget structures so that all entries of
  // widgetList point to a valid DocumentWidget.
  DocumentWidget *documentWidget;
  for(TQ_UINT16 i=0; i<widgetList.size(); i++) {
    documentWidget = widgetList[i];
    if (documentWidget == 0) {
      documentWidget = createDocumentWidget();

      widgetList.insert(i, documentWidget);
      documentWidget->show();

      connect(documentWidget, TQT_SIGNAL(localLink(const TQString &)), this, TQT_SLOT(handleLocalLink(const TQString &)));
      connect(documentWidget, TQT_SIGNAL(setStatusBarText(const TQString&)), this, TQT_SIGNAL(setStatusBarText(const TQString&)) );
    }
  }

  // Set the page numbers for the newly allocated widgets. How this is
  // done depends on the viewMode.
  if (KVSPrefs::viewMode() == KVSPrefs::EnumViewMode::SinglePage) {
    // In KVS_SinglePage mode, any number between 1 and the maximum
    // number of pages is acceptable. If an acceptable value is found,
    // nothing is done, and otherwise '1' is set as a default.
    documentWidget = widgetList[0];
    if (documentWidget != 0) { // Paranoia safety check
      documentWidget->setPageNumber(startPage);
      documentWidget->update();
    } else
      kdError(4300) << "Zero-Pointer in widgetList in KMultiPage::generateDocumentWidgets()" << endl;
  } else {
    // In all other modes, the widgets will be numbered continuously,
    // starting from firstShownPage.
    for(TQ_UINT16 i=0; i<widgetList.size(); i++) {
      documentWidget = widgetList[i];
      if (documentWidget != 0) // Paranoia safety check
      {
        if (KVSPrefs::viewMode() == KVSPrefs::EnumViewMode::Overview)
          documentWidget->setPageNumber(i+tableauStartPage);
        else
          documentWidget->setPageNumber(i+1);
      }
      else
        kdError(4300) << "Zero-Pointer in widgetList in KMultiPage::generateDocumentWidgets()" << endl;
    }
  }

  // Make the changes in the widgetList known to the scrollview. so
  // that the scrollview may update its contents.
  scrollView()->addChild(&widgetList);

  // If the number of widgets has changed, or the viewmode has been changed the widget 
  // that displays the current page may not be visible anymore. Bring it back into focus.
  if (isWidgetListResized || !reload)
    gotoPage(startPage);
}


bool KMultiPage::gotoPage(const PageNumber& page)
{
  return gotoPage(page, 0, true);
}


bool KMultiPage::gotoPage(const PageNumber& page, int y, bool isLink)
{
#ifdef DEBUG_KMULTIPAGE
  kdDebug(1233) << "KMultiPage::gotoPage()" << endl;
#endif

  if (widgetList.size() == 0) {
    kdError(4300) << "KMultiPage::gotoPage(" << page << ", y) called, but widgetList is empty" << endl;
    return false;
  }

  if (!page.isValid())
  {
    kdDebug(1223) << "KMultiPage::gotoPage(" << page << ") invalid pageNumber." << endl;
    return false;
  }

  if (isLink)
    document_history.add(page, y);

  DocumentWidget* pageWidget;

  // If we are in overview viewmode
  if (KVSPrefs::viewMode() == KVSPrefs::EnumViewMode::Overview)
  {
    unsigned int visiblePages = KVSPrefs::overviewModeColumns() * KVSPrefs::overviewModeRows();
    // Pagenumber of the first visibile Page in the current tableau
    unsigned int firstPage = ((DocumentWidget*)widgetList[0])->getPageNumber();
    // Pagenumber of the first page in the new tableau.
    unsigned int tableauStartPage = page + 1 - (page % visiblePages);
    // If these numbers arn't equal "page" is not in the current tableu.
    if (firstPage != tableauStartPage) // widgets need to be updated
    {
      if ((numberOfPages() - tableauStartPage + 1 < visiblePages) || (widgetList.size() < visiblePages))
      {
        // resize widgetList
        // the pages are also set correctly by "generateDocumentWidgets"
        generateDocumentWidgets(tableauStartPage);
      }
      else
      {
        // "page" is not shown in the scrollview, so we have to switch widgets.
        // Here we don't need to resize the widgetList.
        for (unsigned int i = 0; i < widgetList.size(); i++)
        {
          pageWidget = (DocumentWidget*)(widgetList[i]);
          if (pageWidget != 0)
            pageWidget->setPageNumber(tableauStartPage + i);
        }
        scrollView()->layoutPages();
      }
    }
    // move scrollview to "page".
    // Make the widget pageWidget visible in the scrollview. Somehow this
    // doesn't seem to trigger the signal contentsMoved in the
    // TQScrollview, so that we better call setCurrentPage() ourselves.
    pageWidget = (DocumentWidget*)(widgetList[page % visiblePages]);

    scrollView()->moveViewportToWidget(pageWidget, y);

    // Set current page number.
    setCurrentPageNumber(page);

    return true;
  }
  else if (widgetList.size() == 1)
  {
    // If the widget list contains only a single element, then either
    // the document contains only one page, or we are in "single page"
    // view mode. In either case, we set the page number of the single
    // widget to 'page'
    pageWidget = (DocumentWidget*)(widgetList[0]);

    // Paranoia security check
    if (pageWidget == 0) {
      kdError(4300) << "KMultiPage::goto_Page() called with widgetList.size() == 1, but widgetList[0] == 0" << endl;
      return false;
    }

    if (pageCache->sizeOfPageInPixel(currentPageNumber()) == pageCache->sizeOfPageInPixel(page))
    {
      // We are rendering the page before we switch the widget to the new page.
      // To make a smooth transition. We only do this if the size of the current and new page are equal,
      // otherwise we would have to render the page twice, if autozoom is enabled.
      pageCache->getPage(page);
    }

    pageWidget->setPageNumber(page);
    scrollView()->layoutPages();
    scrollView()->moveViewportToWidget(pageWidget, y);
  } else {
    // There are multiple widgets, then we are either in the
    // "Continuous" or in the "Continouous-Facing" view mode. In that
    // case, we find the widget which is supposed to display page
    // 'page' and move the scrollview to make it visible

    // Paranoia security checks
    if (widgetList.size() < page) {
      kdError(4300) << "KMultiPage::goto_Page(page,y ) called with widgetList.size()=" << widgetList.size() << ", and page=" << page << endl;
      return false;
    }
    pageWidget = (DocumentWidget*)(widgetList[page-1]);
    if (pageWidget == 0) {
      kdError(4300) << "KMultiPage::goto_Page() called with widgetList.size() > 1, but widgetList[page] == 0" << endl;
      return false;
    }

    scrollView()->moveViewportToWidget(pageWidget, y);
  }

  if (isLink && y != 0)
    pageWidget->flash(y);

  // Set current page number.
  setCurrentPageNumber(page);
  return true;
}


void KMultiPage::handleLocalLink(const TQString &linkText)
{
#ifdef DEBUG_SPECIAL
  kdDebug(4300) << "hit: local link to " << linkText << endl;
#endif

  if (renderer.isNull()) {
    kdError(4300) << "KMultiPage::handleLocalLink( " << linkText << " ) called, but renderer==0" << endl;
    return;
  }

  TQString locallink;
  if (linkText[0] == '#' )
    locallink = linkText.mid(1); // Drop the '#' at the beginning
  else
    locallink = linkText;

  Anchor anch = renderer->findAnchor(locallink);

  if (anch.isValid())
    gotoPage(anch);
  else {
    if (linkText[0] != '#' ) {
      // We could in principle use KIO::Netaccess::run() here, but
      // it is perhaps not a very good idea to allow a DVI-file to
      // specify arbitrary commands, such as "rm -rvf /". Using
      // the kfmclient seems to be MUCH safer.
      TQUrl DVI_Url(m_file);
      TQUrl Link_Url(DVI_Url, linkText, true);

      TQStringList args;
      args << "openURL";
      args << Link_Url.toString();
      kapp->kdeinitExec("kfmclient", args);
    }
  }
}

void KMultiPage::setCurrentPageNumber(const PageNumber& page)
{
#ifdef DEBUG_KMULTIPAGE
  kdDebug(1233) << "KMultiPage::setCurrentPageNumber()" << endl;
#endif

  if (page != currentPageNumber())
  {
    markList()->setCurrentPageNumber(page);
    emit pageInfo(numberOfPages(), currentPageNumber());
  }
}

PageNumber KMultiPage::currentPageNumber()
{
  return markList()->currentPageNumber();
}

void KMultiPage::doGoBack()
{
  HistoryItem *it = document_history.back();
  if (it != 0)
    gotoPage(it->page, it->ypos, false); // Do not add a history item.
  else
    kdDebug(4300) << "Faulty return -- bad history buffer" << endl;
  return;
}


void KMultiPage::doGoForward()
{
  HistoryItem *it = document_history.forward();
  if (it != 0)
    gotoPage(it->page, it->ypos, false); // Do not add a history item.
  else
    kdDebug(4300) << "Faulty return -- bad history buffer" << endl;
  return;
}


void KMultiPage::renderModeChanged()
{
  pageCache->clear();

  generateDocumentWidgets();
  scrollView()->layoutPages();

  for (TQ_UINT16 i=0; i < widgetList.size(); i++)
  {
    DocumentWidget* documentWidget = widgetList[i];
    if (documentWidget == 0)
      continue;

    documentWidget->update();
  }

  markList()->repaintThumbnails();
}


void KMultiPage::repaintAllVisibleWidgets()
{
#ifdef DEBUG_KMULTIPAGE
  kdDebug(1233) << "KMultiPage::repaintAllVisibleWidgets()" << endl;
#endif

  bool everResized = false;

  // Go through the list of widgets and resize them, if necessary
  for(TQ_UINT16 i=0; i<widgetList.size(); i++)
  {
    DocumentWidget* documentWidget = widgetList[i];
    if (documentWidget == 0)
      continue;

    // Resize, if necessary
    TQSize pageSize = pageCache->sizeOfPageInPixel(documentWidget->getPageNumber());
    if (pageSize != documentWidget->pageSize())
    {
      documentWidget->setPageSize(pageSize);
      everResized = true;
    }
  }

  // If at least one widget was resized, all widgets should be
  // re-aligned. This will automatically update all necessary
  // widgets.
  if (everResized == true)
    scrollView()->layoutPages(true);
}


double KMultiPage::setZoom(double zoom)
{
#ifdef DEBUG_KMULTIPAGE
  kdDebug(1233) << "KMultiPage::setZoom(" << zoom << ")" << endl;
#endif

  if (zoom < ZoomLimits::MinZoom/1000.0)
    zoom = ZoomLimits::MinZoom/1000.0;
  if (zoom > ZoomLimits::MaxZoom/1000.0)
    zoom = ZoomLimits::MaxZoom/1000.0;

  pageCache->setResolution(TQPaintDevice::x11AppDpiX()*zoom);
  emit zoomChanged();
  return zoom;
}


void KMultiPage::print()
{
  // Paranoid safety checks
  if (renderer.isNull())
    return;
  if (renderer->isEmpty())
    return;

  // Allocate the printer structure
  KPrinter *printer = getPrinter();
  if (printer == 0)
    return;

  // initialize the printer using the print dialog
  if ( printer->setup(parentWdg, i18n("Print %1").tqarg(m_file.section('/', -1))) ) {    
    // Now do the printing. 
    TQValueList<int> pageList = printer->pageList();
    if (pageList.isEmpty()) 
      printer->abort();
    else {
      printer->setCreator("kviewshell");
      printer->setDocName(m_file);
      RenderedDocumentPagePrinter rdpp(printer);
      
      // Obtain papersize information that is required to perform
      // the resizing and centering, if this is wanted by the user.
      Length paperWidth, paperHeight;
      TQPaintDeviceMetrics pdm(printer);
      paperWidth.setLength_in_mm(pdm.widthMM());
      paperHeight.setLength_in_mm(pdm.heightMM());
      
      TQValueList<int>::ConstIterator it = pageList.begin();
      while (true) {
	SimplePageSize paper_s(paperWidth, paperHeight);

	// Printing usually takes a while. This is to keep the GUI
	// updated.
	tqApp->processEvents();
	
	TQPainter *paint = rdpp.getPainter();
	if (paint != 0) {
	  // Before drawing the page, we figure out the zoom-value,
	  // taking the "page sizes and placement" options from the
	  // printer dialog into account
	  double factual_zoom = 1.0;
	  
	  // Obtain pagesize information that is required to perform the
	  // resizing and centering, if this is wanted by the user.
	  SimplePageSize page_s = sizeOfPage(*it);
	  
	  paint->save();

	  // Rotate the page, if appropriate. By default, page
	  // rotation is enabled. This is also hardcoded into
	  // KPrintDialogPage_PageOptions.cpp
	  if ((page_s.isPortrait() != paper_s.isPortrait()) && (printer->option( "kde-kviewshell-rotatepage" ) != "false")) {
	    paint->rotate(-90);
	    paint->translate(-printer->resolution()*paperHeight.getLength_in_inch(), 0.0);
	    paper_s = paper_s.rotate90();
	  }

	  double suggested_zoom = page_s.zoomToFitInto(paper_s);
	  
	  // By default, "shrink page" and "expand page" are off. This
	  // is also hardcoded into KPrintDialogPage_PageOptions.cpp
	  if ((suggested_zoom < 1.0) && (printer->option( "kde-kviewshell-shrinkpage" ) == "true")) 
	    factual_zoom = suggested_zoom;
	  if ((suggested_zoom > 1.0) && (printer->option( "kde-kviewshell-expandpage" ) == "true")) 
	    factual_zoom = suggested_zoom;
	  
	  Length delX, delY;
	  // By default, "center page" is on. This is also hardcoded
	  // into KPrintDialogPage_PageOptions.cpp
	  if (printer->option( "kde-kviewshell-centerpage" ) != "false") {
	    delX = (paper_s.width() - page_s.width()*factual_zoom)/2.0;
	    delY = (paper_s.height() - page_s.height()*factual_zoom)/2.0;
	  }
	  
	  // Now draw the page.
	  rdpp.setPageNumber(*it);
	  
	  double resolution = factual_zoom*printer->resolution();
	  
	  paint->translate(resolution*delX.getLength_in_inch(), resolution*delY.getLength_in_inch());
	  renderer->drawPage(resolution, &rdpp);
	  paint->restore();
	}
	++it;
	if ((it == pageList.end()) || (printer->aborted() == true))
	  break;
	
	printer->newPage();
      }
      // At this point the rdpp is destructed. The last page is then
      // printed.
    }
  }
  delete printer;
}


void KMultiPage::setRenderer(DocumentRenderer* _renderer)
{
  renderer = _renderer;

  // Initialize documentPageCache.
  initializePageCache();
  pageCache->setRenderer(renderer);

  _markList->setPageCache(pageCache);

  // Clear widget list.
  widgetList.resize(0);

  // Relay signals.
  connect(renderer, TQT_SIGNAL(setStatusBarText(const TQString&)), this, TQT_SIGNAL(setStatusBarText(const TQString&)));
  connect(pageCache, TQT_SIGNAL(paperSizeChanged()), this, TQT_SLOT(renderModeChanged()));
  connect(pageCache, TQT_SIGNAL(textSelected(bool)), this, TQT_SIGNAL(textSelected(bool)));
  connect(renderer, TQT_SIGNAL(documentIsChanged()), this, TQT_SLOT(renderModeChanged()));
  connect(this, TQT_SIGNAL(zoomChanged()), this, TQT_SLOT(repaintAllVisibleWidgets()));
}


void KMultiPage::updateWidgetSize(const PageNumber& pageNumber)
{
  for(TQ_UINT16 i=0; i<widgetList.size(); i++)
  {
    DocumentWidget* documentWidget = widgetList[i];
    if (documentWidget == 0)
      continue;

    if (documentWidget->getPageNumber() == pageNumber)
    {
      // Resize, if necessary
      TQSize pageSize = pageCache->sizeOfPageInPixel(documentWidget->getPageNumber());
      if (pageSize != documentWidget->pageSize())
      {
        documentWidget->setPageSize(pageSize);
        scrollView()->layoutPages();
      }
      // We have just one widget per page.
      break;
    }
  }

  // Update marklist
  markList()->updateWidgetSize(pageNumber);
}


PageNumber KMultiPage::widestPage() const
{
  Length maxWidth;
  PageNumber pageNumber = 1;

  for (int i = 1; i <= numberOfPages(); i++)
  {
    Length width = pageCache->sizeOfPage(i).width();

    if (width > maxWidth)
    {
      maxWidth = width;
      pageNumber = i;
    }
  }

  return pageNumber;
}

double KMultiPage::zoomForWidthColumns(unsigned int viewportWidth) const
{
  Length maxLeftColumnWidth;
  Length maxRightColumnWidth;
  Length maxWidth;

  PageNumber widestPageLeft;
  PageNumber widestPageRight;

  for (int i = 1; i <= numberOfPages(); i++)
  {
    Length width = pageCache->sizeOfPage(i).width();

    if ( i % 2 == 0) // page is in left column
    {
      if (width > maxLeftColumnWidth)
      {
        maxLeftColumnWidth = width;
        widestPageLeft = i;
      }
    }

    if ( i % 2 == 1) // page is in right column
    {
      if (width > maxRightColumnWidth)
        maxRightColumnWidth = width;
        widestPageRight = i;
    }
  }

  double ratio =  maxLeftColumnWidth / (maxLeftColumnWidth + maxRightColumnWidth);

  // This number is the amount of space the left column should occupy in the viewport.
  unsigned int leftTargetWidth = (unsigned int)(ratio * viewportWidth);

  return pageCache->sizeOfPage(widestPageLeft).zoomForWidth(leftTargetWidth);
}

double KMultiPage::calculateFitToHeightZoomValue()
{
  PageNumber pageNumber = 1;

  // See below, in the documentation of the method "calculatefitToWidthZoomLevel"
  // for an explanation of the complicated calculation we are doing here.
  int columns = scrollView()->getNrColumns();
  int rows = scrollView()->getNrRows();
  int continuousViewmode = scrollView()->isContinuous();
  bool fullScreenMode = scrollView()->fullScreenMode();

  if (columns == 1 && rows == 1 && !continuousViewmode) // single page mode
  {
    pageNumber = currentPageNumber();
    if (!pageNumber.isValid())
      pageNumber = 1;
  }

  int pageDistance = scrollView()->distanceBetweenPages();
  if (columns == 1 && rows == 1 && !continuousViewmode && fullScreenMode)
  {
    // In Single Page Fullscreen Mode we want to fit the page to the
    // window without a margin around it.
    pageDistance = 0;
  }

  int targetViewportHeight = scrollView()->viewportSize(0,0).height();
  int targetPageHeight = (targetViewportHeight - rows*pageDistance)/rows;
  int targetPageWidth  = (int)(targetPageHeight * pageCache->sizeOfPage(pageNumber).aspectRatio() );
  int targetViewportWidth = targetPageWidth * columns + (columns+1)*pageDistance;
  targetViewportHeight = scrollView()->viewportSize(targetViewportWidth, targetViewportHeight).height();
  targetPageHeight = (targetViewportHeight - rows*pageDistance)/rows;

  return pageCache->sizeOfPage(pageNumber).zoomForHeight(targetPageHeight);
}


double KMultiPage::calculateFitToWidthZoomValue()
{
  PageNumber pageNumber = 1;

  int columns = scrollView()->getNrColumns();
  int rows = scrollView()->getNrRows();
  int continuousViewmode = scrollView()->isContinuous();
  bool fullScreenMode = scrollView()->fullScreenMode();

  if (columns == 1 && rows == 1 && !continuousViewmode) // single page mode
  {
    // To calculate the zoom level in single page mode we need the size
    // of the current page. When a new document is opened this function
    // is called while the currentPageNumber is invalid. We use the size
    // of the first page of the document in this case.
    pageNumber = currentPageNumber();
    if (!pageNumber.isValid())
      pageNumber = 1;
  }

  if (columns == 1 && rows == 1 && continuousViewmode) // continuous viewmode
  {
    pageNumber = widestPage();
    if (!pageNumber.isValid())
      pageNumber = 1;
  }

  // rows should be 1 for Single Page Viewmode,
  // the number of Pages in Continuous Viewmode
  // and number of Pages/2 in Continuous-Facing Viewmode
  if (continuousViewmode)
    rows = (int)(ceil(numberOfPages() / (double)columns));

  int pageDistance = scrollView()->distanceBetweenPages();
  if (columns == 1 && rows == 1 && !continuousViewmode && fullScreenMode)
  {
    // In Single Page Fullscreen Mode we want to fit the page to the
    // window without a margin around it.
    pageDistance = 0;
  }
  // There is a slight complication here... if we just take the width
  // of the viewport and scale the contents by a factor x so that it
  // fits the viewport exactly, then, depending on chosen papersize
  // (landscape, etc.), the contents may be higher than the viewport
  // and the TQScrollview may or may not insert a scrollbar at the
  // right. If the scrollbar appears, then the usable width of the
  // viewport becomes smaller, and scaling by x does not really fit
  // the (now smaller page) anymore.

  // Calculate the width and height of the view, disregarding the
  // possible complications with scrollbars, e.g. assuming the maximal
  // space is available.

  // width of the widget excluding possible scrollbars
  int targetViewportWidth  = scrollView()->viewportSize(0,0).width();

  // maximal width of a single page
  int targetPageWidth = (targetViewportWidth - (columns+1) * pageDistance) / columns;

  // maximal height of a single page
  int targetPageHeight = (int)(targetPageWidth/pageCache->sizeOfPage(pageNumber).aspectRatio());
  // FIXME: this is only correct if all pages in the document have the same height
  int targetViewportHeight = rows * targetPageHeight + (rows+1) * pageDistance;

  // Think again, this time use only the area which is really
  // acessible (which, in case that targetWidth targetHeight don't fit
  // the viewport, is really smaller because of the scrollbars).
  targetViewportWidth = scrollView()->viewportSize(targetViewportWidth, targetViewportHeight).width();

  if (columns == 2 && continuousViewmode) // continuous facing
  {
    // TODO Generalize this for more than 2 columns
    return zoomForWidthColumns(targetViewportWidth - (columns+1) * pageDistance);
  }

  // maximal width of a single page (now the scrollbars are taken into account)
  targetPageWidth = (targetViewportWidth - (columns+1) * pageDistance) / columns;

  return pageCache->sizeOfPage(pageNumber).zoomForWidth(targetPageWidth);
}


void KMultiPage::prevPage()
{
  TQ_UINT8 cols = scrollView()->getNrColumns();
  TQ_UINT8 rows = scrollView()->getNrRows();

  PageNumber np = 1;
  if (cols*rows < currentPageNumber())
  {
    np = currentPageNumber() - cols*rows;
  }

  gotoPage(np);
}


void KMultiPage::nextPage()
{
  TQ_UINT8 cols = scrollView()->getNrColumns();
  TQ_UINT8 rows = scrollView()->getNrRows();

  PageNumber np = TQMIN(currentPageNumber() + cols*rows, (TQ_UINT16)numberOfPages());

  gotoPage(np);
}


void KMultiPage::firstPage()
{
  gotoPage(1);
}


void KMultiPage::lastPage()
{
  gotoPage(numberOfPages());
}


void KMultiPage::scroll(TQ_INT32 deltaInPixel)
{
  TQScrollBar* scrollBar = scrollView()->verticalScrollBar();
  if (scrollBar == 0) {
    kdError(4300) << "KMultiPage::scroll called without scrollBar" << endl;
    return;
  }

  if (deltaInPixel < 0) {
    if (scrollBar->value() == scrollBar->minValue()) {
      if ( (currentPageNumber() == 1) || (changePageDelayTimer.isActive()) )
        return;

      if (scrollView()->isContinuous())
        return;

      changePageDelayTimer.stop();
      prevPage();

      scrollView()->setContentsPos(scrollView()->contentsX(), scrollBar->maxValue());
      return;
    }
  }

  if (deltaInPixel > 0) {
    if (scrollBar->value() == scrollBar->maxValue()) {
      if ( (currentPageNumber() == numberOfPages()) || (changePageDelayTimer.isActive()) )
        return;

      if (scrollView()->isContinuous())
        return;

      changePageDelayTimer.stop();
      nextPage();

      scrollView()->setContentsPos(scrollView()->contentsX(), 0);
      return;
    }
  }

  scrollBar->setValue(scrollBar->value() + deltaInPixel);

  if ( (scrollBar->value() == scrollBar->maxValue()) || (scrollBar->value() == scrollBar->minValue()) )
    changePageDelayTimer.start(200,true);
  else
    changePageDelayTimer.stop();
}


void KMultiPage::scrollUp()
{
  TQScrollBar* scrollBar = scrollView()->verticalScrollBar();
  if (scrollBar == 0)
    return;

  scroll(-scrollBar->lineStep());
}


void KMultiPage::scrollDown()
{
  TQScrollBar* scrollBar = scrollView()->verticalScrollBar();
  if (scrollBar == 0)
    return;

  scroll(scrollBar->lineStep());
}

void KMultiPage::scrollLeft()
{
  TQScrollBar* scrollBar = scrollView()->horizontalScrollBar();
  if (scrollBar)
    scrollBar->subtractLine();
}


void KMultiPage::scrollRight()
{
  TQScrollBar* scrollBar = scrollView()->horizontalScrollBar();
  if (scrollBar)
    scrollBar->addLine();
}


void KMultiPage::scrollUpPage()
{
  TQScrollBar* scrollBar = scrollView()->verticalScrollBar();
  if (scrollBar)
    scrollBar->subtractPage();
}


void KMultiPage::scrollDownPage()
{
  TQScrollBar* scrollBar = scrollView()->verticalScrollBar();
  if (scrollBar)
    scrollBar->addPage();
}


void KMultiPage::scrollLeftPage()
{
  TQScrollBar* scrollBar = scrollView()->horizontalScrollBar();
  if (scrollBar)
    scrollBar->subtractPage();
}


void KMultiPage::scrollRightPage()
{
  TQScrollBar* scrollBar = scrollView()->horizontalScrollBar();
  if (scrollBar)
    scrollBar->addPage();
}


void KMultiPage::readDown()
{
  PageView* sv = scrollView();

  if (sv->atBottom())
  {
    if (sv->isContinuous())
      return;

    if (currentPageNumber() == numberOfPages())
      return;

    nextPage();
    sv->setContentsPos(sv->contentsX(), 0);
  }
  else
    sv->readDown();
}


void KMultiPage::readUp()
{
  PageView* sv = scrollView();

  if (sv->atTop())
  {
    if (sv->isContinuous())
      return;

    if (currentPageNumber() == 1)
      return;

    prevPage();
    sv->setContentsPos(sv->contentsX(),  sv->contentsHeight());
  }
  else
    sv->readUp();
}


void KMultiPage::jumpToReference(const TQString& reference)
{
  if (renderer.isNull())
    return;
  
  gotoPage(renderer->parseReference(reference));
}


void KMultiPage::gotoPage(const Anchor &a)
{
  if (!a.page.isValid() || (renderer.isNull()))
    return;

  gotoPage(a.page, (int)(a.distance_from_top.getLength_in_inch()*pageCache->getResolution() + 0.5), true);
}


void KMultiPage::gotoPage(const TextSelection& selection)
{
  if (selection.isEmpty())
  {
    kdError(4300) << "KMultiPage::gotoPage(...) called with empty TextSelection." << endl;
    return;
  }

  RenderedDocumentPage* pageData = pageCache->getPage(selection.getPageNumber());

  if (pageData == 0) {
#ifdef DEBUG_DOCUMENTWIDGET
    kdDebug(4300) << "DocumentWidget::paintEvent: no documentPage generated" << endl;
#endif
    return;
  }

  switch (widgetList.size())
  {
    case 0:
      kdError(4300) << "KMultiPage::select() while widgetList is empty" << endl;
      break;
    case 1:
      ((DocumentWidget*)widgetList[0])->select(selection);
      break;
    default:
      if (widgetList.size() < currentPageNumber())
        kdError(4300) << "KMultiPage::select() while widgetList.size()=" << widgetList.size() << "and currentPageNumber()=" << currentPageNumber() << endl;
      else
        ((DocumentWidget*)widgetList[selection.getPageNumber() - 1])->select(selection);
  }

  unsigned int y = pageData->textBoxList[selection.getSelectedTextStart()].box.top();
  gotoPage(selection.getPageNumber(), y, false);
}


void KMultiPage::doSelectAll()
{
  switch( widgetList.size() ) {
  case 0:
    kdError(4300) << "KMultiPage::doSelectAll() while widgetList is empty" << endl;
    break;
  case 1:
    ((DocumentWidget *)widgetList[0])->selectAll();
    break;
  default:
    if (widgetList.size() < currentPageNumber())
      kdError(4300) << "KMultiPage::doSelectAll() while widgetList.size()=" << widgetList.size() << "and currentPageNumber()=" << currentPageNumber() << endl;
    else
      ((DocumentWidget *)widgetList[currentPageNumber()-1])->selectAll();
  }
}



void  KMultiPage::showFindTextDialog()
{
  if ((renderer.isNull()) || (renderer->supportsTextSearch() == false))
    return;

  searchWidget->show();
  searchWidget->setFocus();
}

void KMultiPage::stopSearch()
{
  if (searchInProgress)
  {
    // stop the search
    searchInProgress = false;
  }
  else
    searchWidget->hide();
}

void KMultiPage::findNextText()
{
#ifdef KDVI_MULTIPAGE_DEBUG
  kdDebug(4300) << "KMultiPage::findNextText() called" << endl;
#endif

  searchInProgress = true;

  // Used to remember if the documentPage we use is from the cache.
  // If not we need to delete it manually to avoid a memory leak.
  bool cachedPage = false;

  TQString searchText = searchWidget->getText();

  if (searchText.isEmpty())
  {
    kdError(4300) << "KMultiPage::findNextText() called when search text was empty" << endl;
    return;
  }

  bool case_sensitive = searchWidget->caseSensitive();

  // Find the page and text position on the page where the search will
  // start. If nothing is selected, we start at the beginning of the
  // current page. Otherwise, start after the selected text.  TODO:
  // Optimize this to get a better 'user feeling'
  TQ_UINT16 startingPage;
  TQ_UINT16 startingTextItem;

  TextSelection userSelection = pageCache->selectedText();
  if (userSelection.isEmpty())
  {
    startingPage     = currentPageNumber();
    startingTextItem = 0;
  }
  else
  {
    startingPage     = userSelection.getPageNumber();
    startingTextItem = userSelection.getSelectedTextEnd()+1;
  }

  TextSelection foundSelection;

  RenderedDocumentPagePixmap* searchPage = 0;

  for(unsigned int i = 0; i < numberOfPages(); i++)
  {
    unsigned int pageNumber = (i + startingPage - 1) % numberOfPages() + 1;

    if (!searchInProgress)
    {
      // Interrupt the search
      setStatusBarText(i18n("Search interrupted"));
      if (!cachedPage)
        delete searchPage;
      return;
    }

    if (i != 0)
    {
      setStatusBarText(i18n("Search page %1 of %2").tqarg(pageNumber).tqarg(numberOfPages()));
      kapp->processEvents();
    }

    // Check if we already have a rendered version of the page in the cache. As we are only interested in the
    // text we don't care about the page size.
    if (pageCache->isPageCached(pageNumber))
    {
      // If the last search page used was created locally, we need to destroy it
      if (!cachedPage)
        delete searchPage;

      searchPage = pageCache->getPage(pageNumber);
      cachedPage = true;
    }
    else
    {
      // If the page is not in the cache we draw a small version of it, since this is faster.

      // We only create a new searchPage if we need to, otherwise reuse the existing one.
      if (!searchPage || cachedPage)
        searchPage = new RenderedDocumentPagePixmap();

      cachedPage = false;

      searchPage->resize(1,1);
      searchPage->setPageNumber(pageNumber);
      renderer->getText(searchPage);
    }

    // If there is no text in the current page, try the next one.
    if (searchPage->textBoxList.size() == 0)
      continue;

    foundSelection = searchPage->find(searchText, startingTextItem, case_sensitive);

    if (foundSelection.isEmpty())
    {
      // In the next page, start search again at the beginning.
      startingTextItem = 0;
      clearSelection();

      if (pageNumber == numberOfPages())
      {
        int answ = KMessageBox::questionYesNo(scrollView(),
                   i18n("<qt>The search string <strong>%1</strong> could not be found by the "
                        "end of the document. Should the search be restarted from the beginning "
                        "of the document?</qt>").tqarg(searchText),
                   i18n("Text Not Found"), KStdGuiItem::cont(), KStdGuiItem::cancel());

        if (answ != KMessageBox::Yes)
        {
          setStatusBarText(TQString());
          searchInProgress = false;
          if (!cachedPage)
            delete searchPage;
          return;
        }
      }
    }
    else
    {
      pageCache->selectText(foundSelection);
      gotoPage(pageCache->selectedText());
      setStatusBarText(TQString());
      searchInProgress = false;
      if (!cachedPage)
        delete searchPage;
      return;
    }
  }

  KMessageBox::sorry(scrollView(), i18n("<qt>The search string <strong>%1</strong> could not be found.</qt>").tqarg(searchText));
  setStatusBarText(TQString());
  searchInProgress = false;
  if (!cachedPage)
    delete searchPage;
}


void KMultiPage::findPrevText()
{
#ifdef KDVI_MULTIPAGE_DEBUG
  kdDebug(4300) << "KMultiPage::findPrevText() called" << endl;
#endif

  searchInProgress = true;

  // Used to remember if the documentPage we use is from the cache.
  // If not we need to delete it manually to avoid a memory leak.
  bool cachedPage = false;

  TQString searchText = searchWidget->getText();

  if (searchText.isEmpty())
  {
    kdError(4300) << "KMultiPage::findPrevText() called when search text was empty" << endl;
    return;
  }

  bool case_sensitive = searchWidget->caseSensitive();

  // Find the page and text position on the page where the search will
  // start. If nothing is selected, we start at the beginning of the
  // current page. Otherwise, start after the selected text.  TODO:
  // Optimize this to get a better 'user feeling'
  unsigned int startingPage;
  int startingTextItem;

  TextSelection userSelection = pageCache->selectedText();
  if (userSelection.isEmpty())
  {
    startingPage     = currentPageNumber();
    startingTextItem = -1;
  }
  else
  {
    startingPage     = userSelection.getPageNumber();
    startingTextItem = userSelection.getSelectedTextStart()-1;
  }

  TextSelection foundSelection;

  RenderedDocumentPagePixmap* searchPage = 0;

  for(unsigned int i = 0; i < numberOfPages(); i++)
  {
    int pageNumber = startingPage - i;
    if (pageNumber <= 0)
      pageNumber += numberOfPages();

    if (!searchInProgress)
    {
      // Interrupt the search
      setStatusBarText(i18n("Search interrupted"));
      if (!cachedPage)
        delete searchPage;
      return;
    }

    if (i != 0)
    {
      setStatusBarText(i18n("Search page %1 of %2").tqarg(pageNumber).tqarg(numberOfPages()));
      kapp->processEvents();
    }

    // Check if we already have a rendered version of the page in the cache. As we are only interested in the
    // text we don't care about the page size.
    if (pageCache->isPageCached(pageNumber))
    {
      // If the last search page used was created locally, we need to destroy it
      if (!cachedPage)
        delete searchPage;

      searchPage = pageCache->getPage(pageNumber);
      cachedPage = true;
    }
    else
    {
      // If the page is not in the cache we draw a small version of it, since this is faster.

      // We only create a new searchPage if we need to, otherwise reuse the existing one.
      if (!searchPage || cachedPage)
        searchPage = new RenderedDocumentPagePixmap();

      cachedPage = false;

      searchPage->resize(1,1);
      searchPage->setPageNumber(pageNumber);
      renderer->getText(searchPage);
    }

    // If there is no text in the current page, try the next one.
    if (searchPage->textBoxList.size() == 0)
      continue;

    foundSelection = searchPage->findRev(searchText, startingTextItem, case_sensitive);

    if (foundSelection.isEmpty())
    {
      // In the next page, start search again at the beginning.
      startingTextItem = -1;
      clearSelection();

      if (pageNumber == 1)
      {
        int answ = KMessageBox::questionYesNo(scrollView(),
                  i18n("<qt>The search string <strong>%1</strong> could not be found by the "
                        "beginning of the document. Should the search be restarted from the end "
                        "of the document?</qt>").tqarg(searchText),
                  i18n("Text Not Found"), KStdGuiItem::cont(), KStdGuiItem::cancel());

        if (answ != KMessageBox::Yes)
        {
          setStatusBarText(TQString());
          searchInProgress = false;
          if (!cachedPage)
            delete searchPage;
          return;
        }
      }
    }
    else
    {
      pageCache->selectText(foundSelection);
      gotoPage(pageCache->selectedText());
      setStatusBarText(TQString());
      searchInProgress = false;
      if (!cachedPage)
        delete searchPage;
      return;
    }
  }

  KMessageBox::sorry(scrollView(), i18n("<qt>The search string <strong>%1</strong> could not be found.</qt>").tqarg(searchText));
  setStatusBarText(TQString());
  searchInProgress = false;
  if (!cachedPage)
    delete searchPage;
}


void KMultiPage::clearSelection()
{
  PageNumber page = pageCache->selectedText().getPageNumber();

  if (!page.isValid())
    return;

  // Clear selection
  pageCache->deselectText();

  // Now we need to update the widget which contained the selection
  switch(widgetList.size())
  {
    case 0:
      kdError(4300) << "KMultiPage::clearSelection() while widgetList is empty" << endl;
      break;
    case 1:
      widgetList[0]->update();
      break;
    default:
      for (unsigned int i = 0; i < widgetList.size(); i++)
      {
        DocumentWidget* pageWidget = (DocumentWidget*)widgetList[i];
        if (pageWidget->getPageNumber() == page)
        {
          pageWidget->update();
          break;
        }
      }
  }
}

void KMultiPage::copyText()
{
  pageCache->selectedText().copyText();
}

void KMultiPage::timerEvent( TQTimerEvent * )
{
#ifdef KMULTIPAGE_DEBUG
  kdDebug(4300) << "Timer Event " << endl;
#endif
  reload();
}


void KMultiPage::reload()
{
#ifdef KMULTIPAGE_DEBUG
  kdDebug(4300) << "Reload file " << m_file << endl;
#endif
  
  if (renderer.isNull()) {
    kdError() << "KMultiPage::reload() called, but no renderer was set" << endl;
    return;
  }
  
  if (renderer->isValidFile(m_file)) {
    pageCache->clear();
    pageCache->deselectText();
    document_history.clear();
    emit setStatusBarText(i18n("Reloading file %1").tqarg(m_file));
    TQ_INT32 pg = currentPageNumber();

    killTimer(timer_id);
    timer_id = -1;
    bool r = renderer->setFile(m_file, m_url);
    
    generateDocumentWidgets();

    // Set Table of Contents
    tableOfContents->setContents(renderer->getBookmarks());

    // Adjust number of widgets in the thumbnail sidebar
    markList()->clear();
    markList()->setNumberOfPages(numberOfPages(), KVSPrefs::showThumbnails());

    setCurrentPageNumber(pg);
    setFile(r);
    emit setStatusBarText(TQString());
  } else {
    if (timer_id == -1)
      timer_id = startTimer(1000);
  }
}


bool KMultiPage::openFile()
{
  if (renderer.isNull()) {
    kdError(4300) << "KMultiPage::openFile() called when no renderer was set" << endl;
    return false;
  }

  pageCache->deselectText();
  document_history.clear();
  pageCache->clear();
  emit setStatusBarText(i18n("Loading file %1").tqarg(m_file));

  bool r = renderer->setFile(m_file, m_url);

  if (r) {
    setCurrentPageNumber(1);
    generateDocumentWidgets();

    // Set number of widgets in the thumbnail sidebar
    markList()->clear();
    markList()->setNumberOfPages(numberOfPages(), KVSPrefs::showThumbnails());
    
    TQString reference = url().ref();
    if (!reference.isEmpty())
      gotoPage(renderer->parseReference(reference));
    
    // Set Table of Contents
    tableOfContents->setContents(renderer->getBookmarks());
  } else
    m_file = TQString();

  
  setFile(r);
  
  // Clear Statusbar
  emit setStatusBarText(TQString());
  return r;
}


bool KMultiPage::openURL(const TQString &filename, const KURL &base_url)
{
  m_file = filename;
  m_url = base_url;

  bool success = openFile();
  if (success)
    setCurrentPageNumber(1);

  return success;
}


void KMultiPage::enableActions(bool fileLoaded)
{
  Q_UNUSED(fileLoaded);
}

void KMultiPage::wheelEvent(TQWheelEvent *e)
{
  TQScrollBar *sb = scrollView()->verticalScrollBar();
  if (sb == 0)
    return;

  // Zoom in/out
  if (e->state() & ControlButton)
  {
    if (e->delta() < 0)
      emit zoomOut();
    else
      emit zoomIn();
    return;
  }

  TQ_INT32 pxl = -(e->delta()*sb->lineStep())/60;
  if (pxl == 0)
  {
    if (e->delta() > 0)
      pxl = -1;
    else
      pxl = 1;
  }

  // Faster scrolling
  if (e->state() & ShiftButton)
    pxl *= 10;

  scroll(pxl);
}


KPrinter *KMultiPage::getPrinter(bool enablePageSizeFeatures)
{
    // Allocate a new KPrinter structure, if necessary
  KPrinter *printer = new KPrinter(true);
  if (printer == 0) {
    kdError(1223) << "KMultiPage::getPrinter(..): Cannot allocate new KPrinter structure" << endl;
    return 0;
  }
  
  // Allocate a new KPrintDialogPage structure and add it to the
  // printer, if the kmultipage implementation requests that
  if (enablePageSizeFeatures == true) {
    KPrintDialogPage_PageOptions *pageOptions = new KPrintDialogPage_PageOptions();
    if (pageOptions == 0) {
      kdError(1223) << "KMultiPage::getPrinter(..): Cannot allocate new KPrintDialogPage_PageOptions structure" << endl;
      delete printer;
      return 0;
    }
    printer->addDialogPage( pageOptions );
  }
  
  // Feed the printer with useful defaults and information.
  printer->setPageSelection( KPrinter::ApplicationSide );
  printer->setCurrentPage( currentPageNumber() );
  printer->setMinMax( 1, numberOfPages() );
  printer->setFullPage( true );
  
  // If pages are marked, give a list of marked pages to the
  // printer. We try to be smart and optimize the list by using ranges
  // ("5-11") wherever possible. The user will be tankful for
  // that. Complicated? Yeah, but that's life.
  TQValueList<int> selectedPageNo = selectedPages();
  if (selectedPageNo.isEmpty() == true)
    printer->setOption( "kde-range", "" );
  else {
    int commaflag = 0;
    TQString range;
    TQValueList<int>::ConstIterator it = selectedPageNo.begin();
    do{
      int val = *it;
      if (commaflag == 1)
	range +=  TQString(", ");
      else
	commaflag = 1;
      int endval = val;
      if (it != selectedPageNo.end()) {
	TQValueList<int>::ConstIterator jt = it;
	jt++;
	do{
	  int val2 = *jt;
	  if (val2 == endval+1)
	    endval++;
	  else
	    break;
	  jt++;
	} while( jt != selectedPageNo.end() );
	it = jt;
      } else
	it++;
      if (endval == val)
	range +=  TQString("%1").tqarg(val);
      else
	range +=  TQString("%1-%2").tqarg(val).tqarg(endval);
    } while (it != selectedPageNo.end() );
    printer->setOption( "kde-range", range );
  }
  
  return printer;  
}

void KMultiPage::doExportText()
{
  // Generate a suggestion for a reasonable file name
  TQString suggestedName = url().filename();
  suggestedName = suggestedName.left(suggestedName.find(".")) + ".txt";

  TQString fileName = KFileDialog::getSaveFileName(suggestedName, i18n("*.txt|Plain Text (Latin 1) (*.txt)"), scrollView(), i18n("Export File As"));

  if (fileName.isEmpty())
    return;

  TQFileInfo finfo(fileName);
  if (finfo.exists())
  {
    int r = KMessageBox::warningContinueCancel (scrollView(),
                i18n("The file %1\nexists. Do you want to overwrite that file?").tqarg(fileName),
                i18n("Overwrite File"), i18n("Overwrite"));

    if (r == KMessageBox::Cancel)
      return;
  }

  TQFile textFile(fileName);
  textFile.open(IO_WriteOnly);
  TQTextStream stream(&textFile);

  TQProgressDialog progress(i18n("Exporting to text..."), i18n("Abort"), renderer->totalPages(),
                           scrollView(), "export_text_progress", true);
  progress.setMinimumDuration(300);

  RenderedDocumentPagePixmap dummyPage;
  dummyPage.resize(1, 1);

  for(unsigned int page = 1; page <= renderer->totalPages(); page++)
  {
    progress.setProgress(page);
    tqApp->processEvents();

    if (progress.wasCancelled())
      break;

    dummyPage.setPageNumber(page);
    // We gracefully ignore any errors (bad file, etc.)
    renderer->getText(&dummyPage);

    for(unsigned int i = 0; i < dummyPage.textBoxList.size(); i++)
    {
      // We try to detect newlines
      if (i > 0)
      {
        // Like all our textalgorithmns this currently assumes left to right text.
        // TODO: make this more generic. But we first would need to guess the corrent
        // orientation.
        if (dummyPage.textBoxList[i].box.top() > dummyPage.textBoxList[i-1].box.bottom() &&
            dummyPage.textBoxList[i].box.x() < dummyPage.textBoxList[i-1].box.x())
        {
          stream << "\n";
        }
      }
      stream << dummyPage.textBoxList[i].text;
    }

    // Send newline after each page.
    stream << "\n";
  }

  // Switch off the progress dialog, etc.
  progress.setProgress(renderer->totalPages());
  return;
}

void KMultiPage::slotEnableMoveTool(bool enable)
{
  emit enableMoveTool(enable);
}

#include "kmultipage.moc"