forked from rbrito/xpdf-poppler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XPDFCore.cc
1651 lines (1498 loc) · 46.2 KB
/
XPDFCore.cc
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
//========================================================================
//
// XPDFCore.cc
//
// Copyright 2002-2003 Glyph & Cog, LLC
//
// Modified for Debian by Hamish Moffatt, 22 May 2002.
//
//========================================================================
#include <poppler-config.h>
#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif
#include <X11/keysym.h>
#include <X11/cursorfont.h>
#include <string.h>
#include "poppler/goo/gmem.h"
#include "poppler/goo/GooString.h"
#include "poppler/goo/GooList.h"
#include "poppler/Error.h"
#include "GlobalParamsGUI.h"
#include "poppler/PDFDoc.h"
#include "poppler/Link.h"
#include "poppler/FileSpec.h"
#include "poppler/ErrorCodes.h"
#include "poppler/GfxState.h"
#include "CoreOutputDev.h"
#include "poppler/PSOutputDev.h"
#include "poppler/TextOutputDev.h"
#include "poppler/splash/SplashBitmap.h"
#include "poppler/splash/SplashPattern.h"
#include "XPDFApp.h"
#include "XPDFCore.h"
// these macro defns conflict with xpdf's Object class
#ifdef LESSTIF_VERSION
#undef XtDisplay
#undef XtScreen
#undef XtWindow
#undef XtParent
#undef XtIsRealized
#endif
//------------------------------------------------------------------------
// Divide a 16-bit value (in [0, 255*255]) by 255, returning an 8-bit result.
static inline unsigned char div255(int x) {
return (unsigned char)((x + (x >> 8) + 0x80) >> 8);
}
//------------------------------------------------------------------------
GooString *XPDFCore::currentSelection = NULL;
XPDFCore *XPDFCore::currentSelectionOwner = NULL;
Atom XPDFCore::targetsAtom;
//------------------------------------------------------------------------
// XPDFCoreTile
//------------------------------------------------------------------------
class XPDFCoreTile: public PDFCoreTile {
public:
XPDFCoreTile(int xDestA, int yDestA);
virtual ~XPDFCoreTile();
XImage *image;
};
XPDFCoreTile::XPDFCoreTile(int xDestA, int yDestA):
PDFCoreTile(xDestA, yDestA)
{
image = NULL;
}
XPDFCoreTile::~XPDFCoreTile() {
if (image) {
gfree(image->data);
image->data = NULL;
XDestroyImage(image);
}
}
//------------------------------------------------------------------------
// XPDFCore
//------------------------------------------------------------------------
XPDFCore::XPDFCore(Widget shellA, Widget parentWidgetA,
SplashColorPtr paperColorA, unsigned long paperPixelA,
unsigned long mattePixelA, bool fullScreenA, bool reverseVideoA,
bool installCmap, int rgbCubeSizeA):
PDFCore(splashModeRGB8, 4, reverseVideoA, paperColorA, !fullScreenA)
{
GooString *initialZoom;
shell = shellA;
parentWidget = parentWidgetA;
display = XtDisplay(parentWidget);
screenNum = XScreenNumberOfScreen(XtScreen(parentWidget));
targetsAtom = XInternAtom(display, "TARGETS", False);
paperPixel = paperPixelA;
mattePixel = mattePixelA;
fullScreen = fullScreenA;
setupX(installCmap, rgbCubeSizeA);
scrolledWin = NULL;
hScrollBar = NULL;
vScrollBar = NULL;
drawAreaFrame = NULL;
drawArea = NULL;
// get the initial zoom value
if (fullScreen) {
zoom = zoomPage;
} else {
initialZoom = globalParamsGUI->getInitialZoom();
if (!initialZoom->cmp("page")) {
zoom = zoomPage;
} else if (!initialZoom->cmp("width")) {
zoom = zoomWidth;
} else {
zoom = atoi(initialZoom->getCString());
if (zoom <= 0) {
zoom = defZoom;
}
}
delete initialZoom;
}
linkAction = NULL;
panning = false;
updateCbk = NULL;
actionCbk = NULL;
keyPressCbk = NULL;
mouseCbk = NULL;
// optional features default to on
hyperlinksEnabled = true;
selectEnabled = true;
// do X-specific initialization and create the widgets
initWindow();
initPasswordDialog();
}
XPDFCore::~XPDFCore() {
if (currentSelectionOwner == this && currentSelection) {
delete currentSelection;
currentSelection = NULL;
currentSelectionOwner = NULL;
}
if (drawAreaGC) {
XFreeGC(display, drawAreaGC);
}
if (scrolledWin) {
XtDestroyWidget(scrolledWin);
}
if (busyCursor) {
XFreeCursor(display, busyCursor);
}
if (linkCursor) {
XFreeCursor(display, linkCursor);
}
if (selectCursor) {
XFreeCursor(display, selectCursor);
}
}
//------------------------------------------------------------------------
// loadFile / displayPage / displayDest
//------------------------------------------------------------------------
int XPDFCore::loadFile(GooString *fileName, GooString *ownerPassword,
GooString *userPassword) {
int err;
err = PDFCore::loadFile(fileName, ownerPassword, userPassword);
if (err == errNone) {
// save the modification time
modTime = getModTime(doc->getFileName()->getCString());
// update the parent window
if (updateCbk) {
(*updateCbk)(updateCbkData, doc->getFileName(), -1,
doc->getNumPages(), NULL);
}
}
return err;
}
int XPDFCore::loadFile(BaseStream *stream, GooString *ownerPassword,
GooString *userPassword) {
int err;
err = PDFCore::loadFile(stream, ownerPassword, userPassword);
if (err == errNone) {
// no file
modTime = 0;
// update the parent window
if (updateCbk) {
(*updateCbk)(updateCbkData, doc->getFileName(), -1,
doc->getNumPages(), NULL);
}
}
return err;
}
void XPDFCore::loadDoc(PDFDoc *docA) {
PDFCore::loadDoc(docA);
// save the modification time
if (doc->getFileName()) {
modTime = getModTime(doc->getFileName()->getCString());
}
// update the parent window
if (updateCbk) {
(*updateCbk)(updateCbkData, doc->getFileName(), -1,
doc->getNumPages(), NULL);
}
}
void XPDFCore::resizeToPage(int pg) {
Dimension width, height;
double width1, height1;
Dimension topW, topH, topBorder, daW, daH;
Dimension displayW, displayH;
displayW = DisplayWidth(display, screenNum);
displayH = DisplayHeight(display, screenNum);
if (fullScreen) {
width = displayW;
height = displayH;
} else {
if (!doc || pg <= 0 || pg > doc->getNumPages()) {
width1 = 612;
height1 = 792;
} else if (doc->getPageRotate(pg) == 90 ||
doc->getPageRotate(pg) == 270) {
width1 = doc->getPageCropHeight(pg);
height1 = doc->getPageCropWidth(pg);
} else {
width1 = doc->getPageCropWidth(pg);
height1 = doc->getPageCropHeight(pg);
}
if (zoom == zoomPage || zoom == zoomWidth) {
width = (Dimension)(width1 * 0.01 * defZoom + 0.5);
height = (Dimension)(height1 * 0.01 * defZoom + 0.5);
} else {
width = (Dimension)(width1 * 0.01 * zoom + 0.5);
height = (Dimension)(height1 * 0.01 * zoom + 0.5);
}
if (continuousMode) {
height += continuousModePageSpacing;
}
if (width > displayW - 100) {
width = displayW - 100;
}
if (height > displayH - 100) {
height = displayH - 100;
}
}
if (XtIsRealized(shell)) {
XtVaGetValues(shell, XmNwidth, &topW, XmNheight, &topH,
XmNborderWidth, &topBorder, NULL);
XtVaGetValues(drawArea, XmNwidth, &daW, XmNheight, &daH, NULL);
XtVaSetValues(shell, XmNwidth, width + (topW - daW),
XmNheight, height + (topH - daH), NULL);
} else {
XtVaSetValues(drawArea, XmNwidth, width, XmNheight, height, NULL);
}
}
void XPDFCore::update(int topPageA, int scrollXA, int scrollYA,
double zoomA, int rotateA,
bool force, bool addToHist) {
int oldPage;
oldPage = topPage;
PDFCore::update(topPageA, scrollXA, scrollYA, zoomA, rotateA,
force, addToHist);
linkAction = NULL;
if (doc && topPage != oldPage) {
if (updateCbk) {
(*updateCbk)(updateCbkData, NULL, topPage, -1, "");
}
}
}
bool XPDFCore::checkForNewFile() {
time_t newModTime;
if (doc->getFileName()) {
newModTime = getModTime(doc->getFileName()->getCString());
if (newModTime != modTime) {
modTime = newModTime;
return true;
}
}
return false;
}
//------------------------------------------------------------------------
// page/position changes
//------------------------------------------------------------------------
bool XPDFCore::gotoNextPage(int inc, bool top) {
if (!PDFCore::gotoNextPage(inc, top)) {
XBell(display, 0);
return false;
}
return true;
}
bool XPDFCore::gotoPrevPage(int dec, bool top, bool bottom) {
if (!PDFCore::gotoPrevPage(dec, top, bottom)) {
XBell(display, 0);
return false;
}
return true;
}
bool XPDFCore::goForward() {
if (!PDFCore::goForward()) {
XBell(display, 0);
return false;
}
return true;
}
bool XPDFCore::goBackward() {
if (!PDFCore::goBackward()) {
XBell(display, 0);
return false;
}
return true;
}
void XPDFCore::startPan(int wx, int wy) {
panning = true;
panMX = wx;
panMY = wy;
}
void XPDFCore::endPan(int wx, int wy) {
panning = false;
}
//------------------------------------------------------------------------
// selection
//------------------------------------------------------------------------
void XPDFCore::startSelection(int wx, int wy) {
int pg, x, y;
takeFocus();
if (doc && doc->getNumPages() > 0) {
if (selectEnabled) {
if (cvtWindowToDev(wx, wy, &pg, &x, &y)) {
setSelection(pg, x, y, x, y);
setCursor(selectCursor);
dragging = true;
}
}
}
}
void XPDFCore::endSelection(int wx, int wy) {
int pg, x, y;
bool ok;
if (doc && doc->getNumPages() > 0) {
ok = cvtWindowToDev(wx, wy, &pg, &x, &y);
if (dragging) {
dragging = false;
setCursor(None);
if (ok) {
moveSelection(pg, x, y);
}
if (selectULX != selectLRX &&
selectULY != selectLRY) {
copySelection();
}
}
}
}
// X's copy-and-paste mechanism is brain damaged. Xt doesn't help
// any, but doesn't make it too much worse, either. Motif, on the
// other hand, adds significant complexity to the mess. So here we
// blow off the Motif junk and stick to plain old Xt. The next two
// functions (copySelection and convertSelectionCbk) implement the
// magic needed to deal with Xt's mechanism. Note that this requires
// global variables (currentSelection and currentSelectionOwner).
void XPDFCore::copySelection() {
int pg;
double ulx, uly, lrx, lry;
if (getSelection(&pg, &ulx, &uly, &lrx, &lry)) {
//~ for multithreading: need a mutex here
delete currentSelection;
currentSelection = extractText(pg, ulx, uly, lrx, lry);
currentSelectionOwner = this;
XtOwnSelection(drawArea, XA_PRIMARY, XtLastTimestampProcessed(display),
&convertSelectionCbk, NULL, NULL);
}
}
Boolean XPDFCore::convertSelectionCbk(Widget widget, Atom *selection,
Atom *target, Atom *type,
XtPointer *value, unsigned long *length,
int *format) {
Atom *array;
// send back a list of supported conversion targets
if (*target == targetsAtom) {
if (!(array = (Atom *)XtMalloc(sizeof(Atom)))) {
return False;
}
array[0] = XA_STRING;
*value = (XtPointer)array;
*type = XA_ATOM;
*format = 32;
*length = 1;
return True;
// send the selected text
} else if (*target == XA_STRING) {
//~ for multithreading: need a mutex here
*value = XtNewString(currentSelection->getCString());
*length = currentSelection->getLength();
*type = XA_STRING;
*format = 8; // 8-bit elements
return True;
}
return False;
}
//------------------------------------------------------------------------
// hyperlinks
//------------------------------------------------------------------------
void XPDFCore::doAction(LinkAction *action) {
LinkActionKind kind;
LinkDest *dest;
GooString *namedDest;
char *s;
GooString *fileName, *fileName2;
GooString *cmd;
GooString *actionName;
Object movieAnnot, obj1, obj2, obj3;
GooString *msg;
int i;
switch (kind = action->getKind()) {
// GoTo / GoToR action
case actionGoTo:
case actionGoToR:
if (kind == actionGoTo) {
dest = NULL;
namedDest = NULL;
if ((dest = ((LinkGoTo *)action)->getDest())) {
dest = dest->copy();
} else if ((namedDest = ((LinkGoTo *)action)->getNamedDest())) {
namedDest = namedDest->copy();
}
} else {
dest = NULL;
namedDest = NULL;
if ((dest = ((LinkGoToR *)action)->getDest())) {
dest = dest->copy();
} else if ((namedDest = ((LinkGoToR *)action)->getNamedDest())) {
namedDest = namedDest->copy();
}
s = ((LinkGoToR *)action)->getFileName()->getCString();
//~ translate path name for VMS (deal with '/')
if (isAbsolutePath(s)) {
fileName = new GooString(s);
} else {
fileName = appendToPath(grabPath(doc->getFileName()->getCString()), s);
}
if (loadFile(fileName) != errNone) {
delete dest;
delete namedDest;
delete fileName;
return;
}
delete fileName;
}
if (namedDest) {
dest = doc->findDest(namedDest);
delete namedDest;
}
if (dest) {
displayDest(dest, zoom, rotate, true);
delete dest;
} else {
if (kind == actionGoToR) {
displayPage(1, zoom, 0, false, true);
}
}
break;
// Launch action
case actionLaunch:
fileName = ((LinkLaunch *)action)->getFileName();
s = fileName->getCString();
if (!strcmp(s + fileName->getLength() - 4, ".pdf") ||
!strcmp(s + fileName->getLength() - 4, ".PDF")) {
//~ translate path name for VMS (deal with '/')
if (isAbsolutePath(s)) {
fileName = fileName->copy();
} else {
fileName = appendToPath(grabPath(doc->getFileName()->getCString()), s);
}
if (loadFile(fileName) != errNone) {
delete fileName;
return;
}
delete fileName;
displayPage(1, zoom, rotate, false, true);
} else {
fileName = fileName->copy();
if (((LinkLaunch *)action)->getParams()) {
fileName->append(' ');
fileName->append(((LinkLaunch *)action)->getParams());
}
fileName->append(" &");
msg = new GooString("About to execute the command:\n");
msg->append(fileName);
if (doQuestionDialog("Launching external application", msg)) {
system(fileName->getCString());
}
delete fileName;
delete msg;
}
break;
// URI action
case actionURI:
if (!(cmd = globalParamsGUI->getURLCommand())) {
error(errConfig, -1, "No urlCommand defined in config file");
break;
}
runCommand(cmd, ((LinkURI *)action)->getURI());
break;
// Named action
case actionNamed:
actionName = ((LinkNamed *)action)->getName();
if (!actionName->cmp("NextPage")) {
gotoNextPage(1, true);
} else if (!actionName->cmp("PrevPage")) {
gotoPrevPage(1, true, false);
} else if (!actionName->cmp("FirstPage")) {
if (topPage != 1) {
displayPage(1, zoom, rotate, true, true);
}
} else if (!actionName->cmp("LastPage")) {
if (topPage != doc->getNumPages()) {
displayPage(doc->getNumPages(), zoom, rotate, true, true);
}
} else if (!actionName->cmp("GoBack")) {
goBackward();
} else if (!actionName->cmp("GoForward")) {
goForward();
} else if (!actionName->cmp("Quit")) {
if (actionCbk) {
(*actionCbk)(actionCbkData, actionName->getCString());
}
} else {
error(errUnimplemented, -1, "Unknown named action: '%s'", actionName->getCString());
}
break;
// Movie action
case actionMovie:
if (!(cmd = globalParamsGUI->getMovieCommand())) {
error(errConfig, -1, "No movieCommand defined in config file");
break;
}
if (((LinkMovie *)action)->hasAnnotRef()) {
doc->getXRef()->fetch(((LinkMovie *)action)->getAnnotRef()->num,
((LinkMovie *)action)->getAnnotRef()->gen,
&movieAnnot);
} else {
//~ need to use the correct page num here
doc->getCatalog()->getPage(topPage)->getAnnots(&obj1);
if (obj1.isArray()) {
for (i = 0; i < obj1.arrayGetLength(); ++i) {
if (obj1.arrayGet(i, &movieAnnot)->isDict()) {
if (movieAnnot.dictLookup("Subtype", &obj2)->isName("Movie")) {
obj2.free();
break;
}
obj2.free();
}
movieAnnot.free();
}
obj1.free();
}
}
if (movieAnnot.isDict()) {
if (movieAnnot.dictLookup("Movie", &obj1)->isDict()) {
if (obj1.dictLookup("F", &obj2)) {
if (getFileSpecNameForPlatform(&obj2, &obj3)) {
fileName = obj3.getString()->copy();
obj3.free();
if (!isAbsolutePath(fileName->getCString())) {
fileName2 = appendToPath(
grabPath(doc->getFileName()->getCString()),
fileName->getCString());
delete fileName;
fileName = fileName2;
}
runCommand(cmd, fileName);
delete fileName;
}
obj2.free();
}
obj1.free();
}
}
movieAnnot.free();
break;
// unimplemented action type
case actionRendition:
case actionSound:
case actionJavaScript:
error(errUnimplemented, -1, "Unimplemented link action type: '%s'",
((LinkUnknown *)action)->getAction()->getCString());
break;
// unknown action type
case actionUnknown:
error(errUnimplemented, -1, "Unknown link action type: '%s'",
((LinkUnknown *)action)->getAction()->getCString());
break;
}
}
// Run a command, given a <cmdFmt> string with one '%s' in it, and an
// <arg> string to insert in place of the '%s'.
void XPDFCore::runCommand(GooString *cmdFmt, GooString *arg) {
GooString *cmd;
char *s;
if ((s = strstr(cmdFmt->getCString(), "%s"))) {
cmd = mungeURL(arg);
cmd->insert(0, cmdFmt->getCString(),
s - cmdFmt->getCString());
cmd->append(s + 2);
} else {
cmd = cmdFmt->copy();
}
cmd->append(" &");
system(cmd->getCString());
delete cmd;
}
// Escape any characters in a URL which might cause problems when
// calling system().
GooString *XPDFCore::mungeURL(GooString *url) {
static char *allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"-_.~/?:@&=+,#%";
GooString *newURL;
char c;
char buf[4];
int i;
newURL = new GooString();
for (i = 0; i < url->getLength(); ++i) {
c = url->getChar(i);
if (strchr(allowed, c)) {
newURL->append(c);
} else {
sprintf(buf, "%%%02x", c & 0xff);
newURL->append(buf);
}
}
return newURL;
}
//------------------------------------------------------------------------
// find
//------------------------------------------------------------------------
bool XPDFCore::find(char *s, bool caseSensitive,
bool next, bool backward, bool onePageOnly) {
if (!PDFCore::find(s, caseSensitive, next, backward, onePageOnly)) {
XBell(display, 0);
return false;
}
#ifndef NO_TEXT_SELECT
copySelection();
#endif
return true;
}
bool XPDFCore::findU(Unicode *u, int len, bool caseSensitive,
bool next, bool backward, bool onePageOnly) {
if (!PDFCore::findU(u, len, caseSensitive, next, backward, onePageOnly)) {
XBell(display, 0);
return false;
}
#ifndef NO_TEXT_SELECT
copySelection();
#endif
return true;
}
//------------------------------------------------------------------------
// misc access
//------------------------------------------------------------------------
void XPDFCore::setBusyCursor(bool busy) {
setCursor(busy ? busyCursor : None);
}
void XPDFCore::takeFocus() {
XmProcessTraversal(drawArea, XmTRAVERSE_CURRENT);
}
//------------------------------------------------------------------------
// GUI code
//------------------------------------------------------------------------
void XPDFCore::setupX(bool installCmap, int rgbCubeSizeA) {
XVisualInfo visualTempl;
XVisualInfo *visualList;
unsigned long mask;
int nVisuals;
XColor xcolor;
XColor *xcolors;
int r, g, b, n, m;
bool ok;
// for some reason, querying XmNvisual doesn't work (even if done
// after the window is mapped)
visual = DefaultVisual(display, screenNum);
XtVaGetValues(shell, XmNcolormap, &colormap, NULL);
// check for TrueColor visual
//~ this should scan the list, not just look at the first one
visualTempl.visualid = XVisualIDFromVisual(visual);
visualList = XGetVisualInfo(display, VisualIDMask,
&visualTempl, &nVisuals);
if (nVisuals < 1) {
// this shouldn't happen
XFree((XPointer)visualList);
visualList = XGetVisualInfo(display, VisualNoMask, &visualTempl,
&nVisuals);
}
depth = visualList->depth;
if (visualList->c_class == TrueColor) {
trueColor = true;
for (mask = visualList->red_mask, rShift = 0;
mask && !(mask & 1);
mask >>= 1, ++rShift) ;
for (rDiv = 8; mask; mask >>= 1, --rDiv) ;
for (mask = visualList->green_mask, gShift = 0;
mask && !(mask & 1);
mask >>= 1, ++gShift) ;
for (gDiv = 8; mask; mask >>= 1, --gDiv) ;
for (mask = visualList->blue_mask, bShift = 0;
mask && !(mask & 1);
mask >>= 1, ++bShift) ;
for (bDiv = 8; mask; mask >>= 1, --bDiv) ;
} else {
trueColor = false;
}
XFree((XPointer)visualList);
// allocate a color cube
if (!trueColor) {
// set colors in private colormap
if (installCmap) {
for (rgbCubeSize = xMaxRGBCube; rgbCubeSize >= 2; --rgbCubeSize) {
m = rgbCubeSize * rgbCubeSize * rgbCubeSize;
if (XAllocColorCells(display, colormap, False, NULL, 0, colors, m)) {
break;
}
}
if (rgbCubeSize >= 2) {
m = rgbCubeSize * rgbCubeSize * rgbCubeSize;
xcolors = (XColor *)gmallocn(m, sizeof(XColor));
n = 0;
for (r = 0; r < rgbCubeSize; ++r) {
for (g = 0; g < rgbCubeSize; ++g) {
for (b = 0; b < rgbCubeSize; ++b) {
xcolors[n].pixel = colors[n];
xcolors[n].red = (r * 65535) / (rgbCubeSize - 1);
xcolors[n].green = (g * 65535) / (rgbCubeSize - 1);
xcolors[n].blue = (b * 65535) / (rgbCubeSize - 1);
xcolors[n].flags = DoRed | DoGreen | DoBlue;
++n;
}
}
}
XStoreColors(display, colormap, xcolors, m);
gfree(xcolors);
} else {
rgbCubeSize = 1;
colors[0] = BlackPixel(display, screenNum);
colors[1] = WhitePixel(display, screenNum);
}
// allocate colors in shared colormap
} else {
if (rgbCubeSize > xMaxRGBCube) {
rgbCubeSize = xMaxRGBCube;
}
ok = false;
for (rgbCubeSize = rgbCubeSizeA; rgbCubeSize >= 2; --rgbCubeSize) {
ok = true;
n = 0;
for (r = 0; r < rgbCubeSize && ok; ++r) {
for (g = 0; g < rgbCubeSize && ok; ++g) {
for (b = 0; b < rgbCubeSize && ok; ++b) {
if (n == 0) {
colors[n] = BlackPixel(display, screenNum);
++n;
} else {
xcolor.red = (r * 65535) / (rgbCubeSize - 1);
xcolor.green = (g * 65535) / (rgbCubeSize - 1);
xcolor.blue = (b * 65535) / (rgbCubeSize - 1);
if (XAllocColor(display, colormap, &xcolor)) {
colors[n++] = xcolor.pixel;
} else {
ok = false;
}
}
}
}
}
if (ok) {
break;
}
XFreeColors(display, colormap, &colors[1], n-1, 0);
}
if (!ok) {
rgbCubeSize = 1;
colors[0] = BlackPixel(display, screenNum);
colors[1] = WhitePixel(display, screenNum);
}
}
}
}
void XPDFCore::initWindow() {
Arg args[20];
int n;
// create the cursors
busyCursor = XCreateFontCursor(display, XC_watch);
linkCursor = XCreateFontCursor(display, XC_hand2);
selectCursor = XCreateFontCursor(display, XC_cross);
currentCursor = 0;
// create the scrolled window and scrollbars
n = 0;
XtSetArg(args[n], XmNscrollingPolicy, XmAPPLICATION_DEFINED); ++n;
XtSetArg(args[n], XmNvisualPolicy, XmVARIABLE); ++n;
scrolledWin = XmCreateScrolledWindow(parentWidget, "scroll", args, n);
XtManageChild(scrolledWin);
n = 0;
XtSetArg(args[n], XmNorientation, XmHORIZONTAL); ++n;
XtSetArg(args[n], XmNminimum, 0); ++n;
XtSetArg(args[n], XmNmaximum, 1); ++n;
XtSetArg(args[n], XmNsliderSize, 1); ++n;
XtSetArg(args[n], XmNvalue, 0); ++n;
XtSetArg(args[n], XmNincrement, 1); ++n;
XtSetArg(args[n], XmNpageIncrement, 1); ++n;
hScrollBar = XmCreateScrollBar(scrolledWin, "hScrollBar", args, n);
if (!fullScreen) {
XtManageChild(hScrollBar);
}
XtAddCallback(hScrollBar, XmNvalueChangedCallback,
&hScrollChangeCbk, (XtPointer)this);
#ifndef DISABLE_SMOOTH_SCROLL
XtAddCallback(hScrollBar, XmNdragCallback,
&hScrollDragCbk, (XtPointer)this);
#endif
n = 0;
XtSetArg(args[n], XmNorientation, XmVERTICAL); ++n;
XtSetArg(args[n], XmNminimum, 0); ++n;
XtSetArg(args[n], XmNmaximum, 1); ++n;
XtSetArg(args[n], XmNsliderSize, 1); ++n;
XtSetArg(args[n], XmNvalue, 0); ++n;
XtSetArg(args[n], XmNincrement, 1); ++n;
XtSetArg(args[n], XmNpageIncrement, 1); ++n;
vScrollBar = XmCreateScrollBar(scrolledWin, "vScrollBar", args, n);
if (!fullScreen) {
XtManageChild(vScrollBar);
}
XtAddCallback(vScrollBar, XmNvalueChangedCallback,
&vScrollChangeCbk, (XtPointer)this);
#ifndef DISABLE_SMOOTH_SCROLL
XtAddCallback(vScrollBar, XmNdragCallback,
&vScrollDragCbk, (XtPointer)this);
#endif
// create the drawing area
n = 0;
XtSetArg(args[n], XmNshadowType, XmSHADOW_IN); ++n;
XtSetArg(args[n], XmNmarginWidth, 0); ++n;
XtSetArg(args[n], XmNmarginHeight, 0); ++n;
if (fullScreen) {
XtSetArg(args[n], XmNshadowThickness, 0); ++n;
}
drawAreaFrame = XmCreateFrame(scrolledWin, "drawAreaFrame", args, n);
XtManageChild(drawAreaFrame);
n = 0;
XtSetArg(args[n], XmNresizePolicy, XmRESIZE_ANY); ++n;
XtSetArg(args[n], XmNwidth, 700); ++n;
XtSetArg(args[n], XmNheight, 500); ++n;
drawArea = XmCreateDrawingArea(drawAreaFrame, "drawArea", args, n);
XtManageChild(drawArea);
XtAddCallback(drawArea, XmNresizeCallback, &resizeCbk, (XtPointer)this);
XtAddCallback(drawArea, XmNexposeCallback, &redrawCbk, (XtPointer)this);
XtAddCallback(drawArea, XmNinputCallback, &inputCbk, (XtPointer)this);
resizeCbk(drawArea, this, NULL);
// set up mouse motion translations
XtOverrideTranslations(drawArea, XtParseTranslationTable(
"<Btn1Down>:DrawingAreaInput()\n"
"<Btn1Up>:DrawingAreaInput()\n"
"<Btn1Motion>:DrawingAreaInput()\n"
"<Motion>:DrawingAreaInput()"));
// can't create a GC until the window gets mapped
drawAreaGC = NULL;
}
void XPDFCore::hScrollChangeCbk(Widget widget, XtPointer ptr,
XtPointer callData) {
XPDFCore *core = (XPDFCore *)ptr;
XmScrollBarCallbackStruct *data = (XmScrollBarCallbackStruct *)callData;
core->scrollTo(data->value, core->scrollY);
}
void XPDFCore::hScrollDragCbk(Widget widget, XtPointer ptr,
XtPointer callData) {
XPDFCore *core = (XPDFCore *)ptr;
XmScrollBarCallbackStruct *data = (XmScrollBarCallbackStruct *)callData;
core->scrollTo(data->value, core->scrollY);
}
void XPDFCore::vScrollChangeCbk(Widget widget, XtPointer ptr,
XtPointer callData) {
XPDFCore *core = (XPDFCore *)ptr;
XmScrollBarCallbackStruct *data = (XmScrollBarCallbackStruct *)callData;
core->scrollTo(core->scrollX, data->value);
}
void XPDFCore::vScrollDragCbk(Widget widget, XtPointer ptr,
XtPointer callData) {
XPDFCore *core = (XPDFCore *)ptr;
XmScrollBarCallbackStruct *data = (XmScrollBarCallbackStruct *)callData;
core->scrollTo(core->scrollX, data->value);
}
void XPDFCore::resizeCbk(Widget widget, XtPointer ptr, XtPointer callData) {
XPDFCore *core = (XPDFCore *)ptr;
XEvent event;
Widget top;
Window rootWin;
int x1, y1;
unsigned w1, h1, bw1, depth1;
Arg args[2];
int n;
Dimension w, h;
int sx, sy;
// find the top-most widget which has an associated window, and look
// for a pending ConfigureNotify in the event queue -- if there is
// one, and it specifies a different width or height, that means
// we're still resizing, and we want to skip the current event
for (top = core->parentWidget;
XtParent(top) && XtWindow(XtParent(top));
top = XtParent(top)) ;
if (XCheckTypedWindowEvent(core->display, XtWindow(top),