-
Notifications
You must be signed in to change notification settings - Fork 16
/
XPDFApp.cc
438 lines (388 loc) · 13.5 KB
/
XPDFApp.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
//========================================================================
//
// XPDFApp.cc
//
// Copyright 2002-2003 Glyph & Cog, LLC
//
//========================================================================
#include <poppler-config.h>
#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif
#include "poppler/goo/GooString.h"
#include "poppler/goo/GooList.h"
#include "poppler/Error.h"
#include "XPDFViewer.h"
#include "XPDFApp.h"
#include "config.h"
// these macro defns conflict with xpdf's Object class
#ifdef LESSTIF_VERSION
#undef XtDisplay
#undef XtScreen
#undef XtWindow
#undef XtParent
#undef XtIsRealized
#endif
//------------------------------------------------------------------------
#define remoteCmdSize 512
//------------------------------------------------------------------------
static String fallbackResources[] = {
"*.zoomComboBox*fontList: -*-helvetica-medium-r-normal--12-*-*-*-*-*-iso8859-1",
"*XmTextField.fontList: -*-courier-medium-r-normal--12-*-*-*-*-*-iso8859-1",
"*.fontList: -*-helvetica-medium-r-normal--12-*-*-*-*-*-iso8859-1",
"*XmTextField.translations: #override\\n"
" Ctrl<Key>a:beginning-of-line()\\n"
" Ctrl<Key>b:backward-character()\\n"
" Ctrl<Key>d:delete-next-character()\\n"
" Ctrl<Key>e:end-of-line()\\n"
" Ctrl<Key>f:forward-character()\\n"
" Ctrl<Key>u:beginning-of-line()delete-to-end-of-line()\\n"
" Ctrl<Key>k:delete-to-end-of-line()\\n",
"*.toolTipEnable: True",
"*.toolTipPostDelay: 1500",
"*.toolTipPostDuration: 0",
"*.TipLabel.foreground: black",
"*.TipLabel.background: LightYellow",
"*.TipShell.borderWidth: 1",
"*.TipShell.borderColor: black",
NULL
};
static XrmOptionDescRec xOpts[] = {
{"-display", ".display", XrmoptionSepArg, NULL},
{"-foreground", "*Foreground", XrmoptionSepArg, NULL},
{"-fg", "*Foreground", XrmoptionSepArg, NULL},
{"-background", "*Background", XrmoptionSepArg, NULL},
{"-bg", "*Background", XrmoptionSepArg, NULL},
{"-geometry", ".geometry", XrmoptionSepArg, NULL},
{"-g", ".geometry", XrmoptionSepArg, NULL},
{"-font", "*.fontList", XrmoptionSepArg, NULL},
{"-fn", "*.fontList", XrmoptionSepArg, NULL},
{"-title", ".title", XrmoptionSepArg, NULL},
{"-cmap", ".installCmap", XrmoptionNoArg, (XPointer)"on"},
{"-rgb", ".rgbCubeSize", XrmoptionSepArg, NULL},
{"-rv", ".reverseVideo", XrmoptionNoArg, (XPointer)"true"},
{"-papercolor", ".paperColor", XrmoptionSepArg, NULL},
{"-mattecolor", ".matteColor", XrmoptionSepArg, NULL},
{"-z", ".initialZoom", XrmoptionSepArg, NULL}
};
#define nXOpts (sizeof(xOpts) / sizeof(XrmOptionDescRec))
struct XPDFAppResources {
String geometry;
String title;
Bool installCmap;
int rgbCubeSize;
Bool reverseVideo;
String paperColor;
String matteColor;
String fullScreenMatteColor;
String initialZoom;
};
static Bool defInstallCmap = False;
static int defRGBCubeSize = defaultRGBCube;
static Bool defReverseVideo = False;
static XtResource xResources[] = {
{ "geometry", "Geometry", XtRString, sizeof(String), XtOffsetOf(XPDFAppResources, geometry), XtRString, NULL },
{ "title", "Title", XtRString, sizeof(String), XtOffsetOf(XPDFAppResources, title), XtRString, NULL },
{ "installCmap", "InstallCmap", XtRBool, sizeof(Bool), XtOffsetOf(XPDFAppResources, installCmap), XtRBool, &defInstallCmap },
{ "rgbCubeSize", "RgbCubeSize", XtRInt, sizeof(int), XtOffsetOf(XPDFAppResources, rgbCubeSize), XtRInt, &defRGBCubeSize },
{ "reverseVideo", "ReverseVideo", XtRBool, sizeof(Bool), XtOffsetOf(XPDFAppResources, reverseVideo), XtRBool, &defReverseVideo },
{ "paperColor", "PaperColor", XtRString, sizeof(String), XtOffsetOf(XPDFAppResources, paperColor), XtRString, NULL },
{ "matteColor", "MatteColor", XtRString, sizeof(String), XtOffsetOf(XPDFAppResources, matteColor), XtRString, (XtPointer)"gray50" },
{ "fullScreenMatteColor", "FullScreenMatteColor", XtRString, sizeof(String), XtOffsetOf(XPDFAppResources, fullScreenMatteColor), XtRString, (XtPointer)"black" },
{ "initialZoom", "InitialZoom", XtRString, sizeof(String), XtOffsetOf(XPDFAppResources, initialZoom), XtRString, NULL }
};
#define nXResources (sizeof(xResources) / sizeof(XtResource))
//------------------------------------------------------------------------
// XPDFApp
//------------------------------------------------------------------------
#if 0 //~ for debugging
static int xErrorHandler(Display *display, XErrorEvent *ev) {
printf("X error:\n");
printf(" resource ID = %08lx\n", ev->resourceid);
printf(" serial = %lu\n", ev->serial);
printf(" error_code = %d\n", ev->error_code);
printf(" request_code = %d\n", ev->request_code);
printf(" minor_code = %d\n", ev->minor_code);
fflush(stdout);
abort();
}
#endif
XPDFApp::XPDFApp(int *argc, char *argv[]) {
appShell = XtAppInitialize(&appContext, xpdfAppName, xOpts, nXOpts,
argc, argv, fallbackResources, NULL, 0);
display = XtDisplay(appShell);
screenNum = XScreenNumberOfScreen(XtScreen(appShell));
XtVaSetValues(XmGetXmDisplay(XtDisplay(appShell)),
XmNenableButtonTab, True, NULL);
// Drag-and-drop appears to be buggy -- I'm seeing weird crashes
// deep in the Motif code when I destroy widgets in the XpdfForms
// code. Xpdf doesn't use it, so just turn it off.
XtVaSetValues(XmGetXmDisplay(XtDisplay(appShell)),
XmNdragInitiatorProtocolStyle, XmDRAG_NONE,
XmNdragReceiverProtocolStyle, XmDRAG_NONE,
NULL);
#if 0 //~ for debugging
XSynchronize(display, True);
XSetErrorHandler(&xErrorHandler);
#endif
fullScreen = false;
remoteAtom = None;
remoteViewer = NULL;
remoteWin = None;
getResources();
viewers = new GooList();
}
void XPDFApp::getResources() {
XPDFAppResources resources;
XColor xcol, xcol2;
Colormap colormap;
XtGetApplicationResources(appShell, &resources, xResources, nXResources,
NULL, 0);
geometry = resources.geometry ? new GooString(resources.geometry)
: NULL;
title = resources.title ? new GooString(resources.title) : NULL;
installCmap = (bool)resources.installCmap;
rgbCubeSize = resources.rgbCubeSize;
reverseVideo = (bool)resources.reverseVideo;
if (reverseVideo) {
paperRGB[0] = paperRGB[1] = paperRGB[2] = 0;
paperPixel = BlackPixel(display, screenNum);
} else {
paperRGB[0] = paperRGB[1] = paperRGB[2] = 0xff;
paperPixel = WhitePixel(display, screenNum);
}
XtVaGetValues(appShell, XmNcolormap, &colormap, NULL);
if (resources.paperColor) {
if (XAllocNamedColor(display, colormap, resources.paperColor,
&xcol, &xcol2)) {
paperRGB[0] = xcol.red >> 8;
paperRGB[1] = xcol.green >> 8;
paperRGB[2] = xcol.blue >> 8;
paperPixel = xcol.pixel;
} else {
error(errInternal, -1, "Couldn't allocate color '%s'", resources.paperColor);
}
}
if (XAllocNamedColor(display, colormap, resources.matteColor,
&xcol, &xcol2)) {
mattePixel = xcol.pixel;
} else {
mattePixel = paperPixel;
}
if (XAllocNamedColor(display, colormap, resources.fullScreenMatteColor,
&xcol, &xcol2)) {
fullScreenMattePixel = xcol.pixel;
} else {
fullScreenMattePixel = paperPixel;
}
initialZoom = resources.initialZoom ? new GooString(resources.initialZoom)
: NULL;
}
XPDFApp::~XPDFApp() {
deleteGooList(viewers, XPDFViewer);
delete geometry;
delete title;
delete initialZoom;
}
XPDFViewer *XPDFApp::open(GooString *fileName, int page,
GooString *ownerPassword, GooString *userPassword) {
XPDFViewer *viewer;
viewer = new XPDFViewer(this, fileName, page, NULL, fullScreen,
ownerPassword, userPassword);
if (!viewer->isOk()) {
delete viewer;
return NULL;
}
if (remoteAtom != None) {
remoteViewer = viewer;
remoteWin = viewer->getWindow();
XtAddEventHandler(remoteWin, PropertyChangeMask, False,
&remoteMsgCbk, this);
XSetSelectionOwner(display, remoteAtom, XtWindow(remoteWin), CurrentTime);
}
viewers->append(viewer);
return viewer;
}
XPDFViewer *XPDFApp::openAtDest(GooString *fileName, GooString *dest,
GooString *ownerPassword,
GooString *userPassword) {
XPDFViewer *viewer;
viewer = new XPDFViewer(this, fileName, 1, dest, fullScreen,
ownerPassword, userPassword);
if (!viewer->isOk()) {
delete viewer;
return NULL;
}
if (remoteAtom != None) {
remoteViewer = viewer;
remoteWin = viewer->getWindow();
XtAddEventHandler(remoteWin, PropertyChangeMask, False,
&remoteMsgCbk, this);
XSetSelectionOwner(display, remoteAtom, XtWindow(remoteWin), CurrentTime);
}
viewers->append(viewer);
return viewer;
}
XPDFViewer *XPDFApp::reopen(XPDFViewer *viewer, PDFDoc *doc, int page,
bool fullScreenA) {
int i;
for (i = 0; i < viewers->getLength(); ++i) {
if (((XPDFViewer *)viewers->get(i)) == viewer) {
viewers->del(i);
delete viewer;
}
}
viewer = new XPDFViewer(this, doc, page, NULL, fullScreenA);
if (!viewer->isOk()) {
delete viewer;
return NULL;
}
if (remoteAtom != None) {
remoteViewer = viewer;
remoteWin = viewer->getWindow();
XtAddEventHandler(remoteWin, PropertyChangeMask, False,
&remoteMsgCbk, this);
XSetSelectionOwner(display, remoteAtom, XtWindow(remoteWin), CurrentTime);
}
viewers->append(viewer);
return viewer;
}
void XPDFApp::close(XPDFViewer *viewer, bool closeLast) {
int i;
if (viewers->getLength() == 1) {
if (viewer != (XPDFViewer *)viewers->get(0)) {
return;
}
if (closeLast) {
quit();
} else {
viewer->clear();
}
} else {
for (i = 0; i < viewers->getLength(); ++i) {
if (((XPDFViewer *)viewers->get(i)) == viewer) {
viewers->del(i);
if (remoteAtom != None && remoteViewer == viewer) {
remoteViewer = (XPDFViewer *)viewers->get(viewers->getLength() - 1);
remoteWin = remoteViewer->getWindow();
XSetSelectionOwner(display, remoteAtom, XtWindow(remoteWin),
CurrentTime);
}
delete viewer;
return;
}
}
}
}
void XPDFApp::quit() {
if (remoteAtom != None) {
XSetSelectionOwner(display, remoteAtom, None, CurrentTime);
}
while (viewers->getLength() > 0) {
delete (XPDFViewer *)viewers->del(0);
}
/* check the need of this conditional compilation */
#if HAVE_XTAPPSETEXITFLAG
XtAppSetExitFlag(appContext);
#else
exit(0);
#endif
}
void XPDFApp::run() {
XtAppMainLoop(appContext);
}
void XPDFApp::setRemoteName(char *remoteName) {
remoteAtom = XInternAtom(display, remoteName, False);
remoteXWin = XGetSelectionOwner(display, remoteAtom);
}
bool XPDFApp::remoteServerRunning() {
return remoteXWin != None;
}
void XPDFApp::remoteExec(char *cmd) {
char cmd2[remoteCmdSize];
int n;
n = strlen(cmd);
if (n > remoteCmdSize - 2) {
n = remoteCmdSize - 2;
}
memcpy(cmd2, cmd, n);
cmd2[n] = '\n';
cmd2[n+1] = '\0';
XChangeProperty(display, remoteXWin, remoteAtom, remoteAtom, 8,
PropModeReplace, (unsigned char *)cmd2, n + 2);
XFlush(display);
}
void XPDFApp::remoteOpen(GooString *fileName, int page, bool raise) {
char cmd[remoteCmdSize];
sprintf(cmd, "openFileAtPage(%.200s,%d)\n",
fileName->getCString(), page);
if (raise) {
strcat(cmd, "raise\n");
}
XChangeProperty(display, remoteXWin, remoteAtom, remoteAtom, 8,
PropModeReplace, (unsigned char *)cmd, strlen(cmd) + 1);
XFlush(display);
}
void XPDFApp::remoteOpenAtDest(GooString *fileName, GooString *dest, bool raise) {
char cmd[remoteCmdSize];
sprintf(cmd, "openFileAtDest(%.200s,%.256s)\n",
fileName->getCString(), dest->getCString());
if (raise) {
strcat(cmd, "raise\n");
}
XChangeProperty(display, remoteXWin, remoteAtom, remoteAtom, 8,
PropModeReplace, (unsigned char *)cmd, strlen(cmd) + 1);
XFlush(display);
}
void XPDFApp::remoteReload(bool raise) {
char cmd[remoteCmdSize];
strcpy(cmd, "reload\n");
if (raise) {
strcat(cmd, "raise\n");
}
XChangeProperty(display, remoteXWin, remoteAtom, remoteAtom, 8,
PropModeReplace, (unsigned char *)cmd, strlen(cmd) + 1);
XFlush(display);
}
void XPDFApp::remoteRaise() {
XChangeProperty(display, remoteXWin, remoteAtom, remoteAtom, 8,
PropModeReplace, (unsigned char *)"raise\n", 7);
XFlush(display);
}
void XPDFApp::remoteQuit() {
XChangeProperty(display, remoteXWin, remoteAtom, remoteAtom, 8,
PropModeReplace, (unsigned char *)"quit\n", 6);
XFlush(display);
}
void XPDFApp::remoteMsgCbk(Widget widget, XtPointer ptr,
XEvent *event, Boolean *cont) {
XPDFApp *app = (XPDFApp *)ptr;
char *cmd, *p0, *p1;
Atom type;
int format;
unsigned long size, remain;
GooString *cmdStr;
if (event->xproperty.atom != app->remoteAtom) {
*cont = True;
return;
}
*cont = False;
if (XGetWindowProperty(app->display, XtWindow(app->remoteWin),
app->remoteAtom, 0, remoteCmdSize/4,
True, app->remoteAtom,
&type, &format, &size, &remain,
(unsigned char **)&cmd) != Success) {
return;
}
if (!cmd) {
return;
}
p0 = cmd;
while (*p0 && (p1 = strchr(p0, '\n'))) {
cmdStr = new GooString(p0, p1 - p0);
app->remoteViewer->execCmd(cmdStr, NULL);
delete cmdStr;
p0 = p1 + 1;
}
XFree((XPointer)cmd);
}