summaryrefslogtreecommitdiff
path: root/xditview.c
blob: 7e408dabc48bb2bb39ddd8128697b7070405541b (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
/*

Copyright (c) 1991  X Consortium

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the X Consortium shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from the X Consortium.

*/
/*
 * xditview -- 
 *
 *   Display ditroff output in an X window
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xatom.h>
#include <X11/Shell.h>
#include <X11/Xos.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Panner.h>
#include <X11/Xaw/Porthole.h>
#include <X11/Xaw/Viewport.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Dialog.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/MenuButton.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/Xaw/AsciiText.h>

#include "Dvi.h"

#include "xdit.bm"
#include "xdit_mask.bm"
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

/* Command line options table.  Only resources are entered here...there is a
   pass over the remaining options after XtParseCommand is let loose. */

static XrmOptionDescRec options[] = {
    {"-page",           "*dvi.pageNumber",       XrmoptionSepArg, NULL},
    {"-backingStore",   "*dvi.backingStore",     XrmoptionSepArg, NULL},
    {"-noPolyText",     "*dvi.noPolyText",       XrmoptionNoArg,  "TRUE"},
    {"-resolution",     "*dvi.screenResolution", XrmoptionSepArg, NULL},
};

static char  current_file_name[1024];
static FILE *current_file;

static void MakePrompt(Widget, const char *, void (*)(char *), char *);

/*
 * Report the syntax for calling xditview.
 */

static void
Syntax(const char *call, int exitval)
{
    (void) printf("Usage: %s [-fg <color>] [-bg <color>]\n%s\n", call,
                  "       [-bd <color>] [-bw <pixels>] [-help] [-version]\n"
                  "       [-display displayname] [-geometry geom]\n"
                  "       [-page <page-number>] [-backing <backing-store>]\n"
                  "       [-resolution <screen-resolution>]\n");
    exit(exitval);
}

static void     NewResolution(char *resString);
static void     NewFile(char *name);
static void     DisplayPageNumber(void);
static void     VisitFile(char *name, Boolean resetPage);

static Widget   toplevel, paned, porthole, dvi;
static Widget   popupMenu;
static Widget   menuBar;
static Widget   fileMenuButton, fileMenu;
static Widget   pageNumber;

static void     NextPage(Widget entry, XtPointer name, XtPointer data);
static void     PreviousPage(Widget entry, XtPointer name, XtPointer data);
static void     SetResolution(Widget entry, XtPointer name, XtPointer data);
static void     OpenFile(Widget entry, XtPointer name, XtPointer data);
static void     RevisitFile(Widget entry, XtPointer name, XtPointer data);
static void     Quit(Widget entry, XtPointer closure, XtPointer data);

struct menuEntry {
    char *name;
    void (*function)(Widget entry, XtPointer name, XtPointer data);
};

static struct menuEntry popupMenuEntries[] = {
    {"nextPage",        NextPage},
    {"previousPage",    PreviousPage},
    {"setResolution",   SetResolution},
    {"openFile",        OpenFile},
    {"revisitFile",     RevisitFile},
    {"quit",            Quit}
};

static struct menuEntry fileMenuEntries[] = {
    {"openFile",        OpenFile},
    {"revisitFile",     RevisitFile},
    {"setResolution",   SetResolution},
    {"quit",            Quit}
};

static void NextPageAction(Widget, XEvent *, String *, Cardinal *);
static void PreviousPageAction(Widget, XEvent *, String *, Cardinal *);
static void SetResolutionAction(Widget, XEvent *, String *, Cardinal *);
static void OpenFileAction(Widget, XEvent *, String *, Cardinal *);
static void RevisitFileAction(Widget, XEvent *, String *, Cardinal *);
static void QuitAction(Widget, XEvent *, String *, Cardinal *);
static void AcceptAction(Widget, XEvent *, String *, Cardinal *);
static void CancelAction(Widget, XEvent *, String *, Cardinal *);
static void UpdatePageNumber(Widget, XEvent *, String *, Cardinal *);
static void Noop(Widget, XEvent *, String *, Cardinal *);

static XtActionsRec xditview_actions[] = {
    {"NextPage",        NextPageAction},
    {"PreviousPage",    PreviousPageAction},
    {"SetResolution",   SetResolutionAction},
    {"OpenFile",        OpenFileAction},
    {"Quit",            QuitAction},
    {"Accept",          AcceptAction},
    {"Cancel",          CancelAction},
    {"SetPageNumber",   UpdatePageNumber},
    {"Noop",            Noop}
};

static Atom wm_delete_window;


int
main(int argc, char **argv)
{
    char *          file_name = NULL;
    XtAppContext    xtcontext;
    Arg             topLevelArgs[2];

    XtSetLanguageProc(NULL, (XtLanguageProc) NULL, NULL);

    /* Handle args that don't require opening a display */
    for (int n = 1; n < argc; n++) {
        const char *argn = argv[n];
        /* accept single or double dash for -help & -version */
        if (argn[0] == '-' && argn[1] == '-') {
            argn++;
        }
        if (strcmp(argn, "-help") == 0) {
            Syntax(argv[0], 0);
        }
        if (strcmp(argn, "-version") == 0) {
            puts(PACKAGE_STRING);
            exit(0);
        }
    }

    toplevel = XtAppInitialize(&xtcontext, "Xditview",
                               options, XtNumber(options),
                               &argc, argv, NULL, NULL, 0);
    if (argc > 2) {
        fputs("Unknown argument(s):", stderr);
        for (int n = 1; n < argc; n++) {
            if ((n < (argc -1)) || (argv[n][0] == '-')) {
                fprintf(stderr, " %s", argv[n]);
            }
        }
        fputs("\n\n", stderr);
        Syntax(argv[0], 1);
    }

    XtAppAddActions(xtcontext, xditview_actions, XtNumber(xditview_actions));
    XtOverrideTranslations
        (toplevel, XtParseTranslationTable("<Message>WM_PROTOCOLS: Quit()"));

    XtSetArg(topLevelArgs[0], XtNiconPixmap,
             XCreateBitmapFromData(XtDisplay(toplevel),
                                   XtScreen(toplevel)->root,
                                   (char *) xdit_bits,
                                   xdit_width, xdit_height));

    XtSetArg(topLevelArgs[1], XtNiconMask,
             XCreateBitmapFromData(XtDisplay(toplevel),
                                   XtScreen(toplevel)->root,
                                   (char *) xdit_mask_bits,
                                   xdit_mask_width, xdit_mask_height));
    XtSetValues(toplevel, topLevelArgs, 2);
    if (argc > 1)
        file_name = argv[1];

    /*
     * create the popup menu and insert the entries
     */
    popupMenu = XtCreatePopupShell("popupMenu", simpleMenuWidgetClass, toplevel,
                                   NULL, 0);
    for (Cardinal i = 0; i < XtNumber(popupMenuEntries); i++) {
        Widget entry = XtCreateManagedWidget(popupMenuEntries[i].name,
                                             smeBSBObjectClass, popupMenu,
                                             NULL, (Cardinal) 0);
        XtAddCallback(entry, XtNcallback, popupMenuEntries[i].function, NULL);
    }

    paned = XtCreateManagedWidget("paned", panedWidgetClass, toplevel,
                                  NULL, (Cardinal) 0);
    menuBar = XtCreateManagedWidget("menuBar", boxWidgetClass, paned, NULL, 0);

    fileMenuButton =
        XtCreateManagedWidget("fileMenuButton", menuButtonWidgetClass, menuBar,
                              NULL, (Cardinal) 0);
    fileMenu =
        XtCreatePopupShell("fileMenu", simpleMenuWidgetClass, fileMenuButton,
                           NULL, (Cardinal) 0);
    for (Cardinal i = 0; i < XtNumber(fileMenuEntries); i++) {
        Widget entry = XtCreateManagedWidget(fileMenuEntries[i].name,
                                             smeBSBObjectClass, fileMenu,
                                             NULL, (Cardinal) 0);
        XtAddCallback(entry, XtNcallback, fileMenuEntries[i].function, NULL);
    }

    (void) XtCreateManagedWidget("prevButton", commandWidgetClass,
                                 menuBar, NULL, (Cardinal) 0);

    pageNumber = XtCreateManagedWidget("pageNumber", asciiTextWidgetClass,
                                       menuBar, NULL, (Cardinal) 0);

    (void) XtCreateManagedWidget("nextButton", commandWidgetClass,
                                 menuBar, NULL, (Cardinal) 0);

    porthole = XtCreateManagedWidget("viewport", viewportWidgetClass,
                                     paned, NULL, 0);
    dvi = XtCreateManagedWidget("dvi", dviWidgetClass, porthole, NULL, 0);
    if (file_name)
        VisitFile(file_name, FALSE);
    XtRealizeWidget(toplevel);
    wm_delete_window = XInternAtom(XtDisplay(toplevel), "WM_DELETE_WINDOW",
                                   False);
    (void) XSetWMProtocols(XtDisplay(toplevel), XtWindow(toplevel),
                           &wm_delete_window, 1);
    XtAppMainLoop(xtcontext);

    return 0;
}

static void
DisplayPageNumber(void)
{
    Arg 	arg[2];
    int 	actual_number, last_page;
    XawTextBlock text;
    int 	length;
    char 	value[128];
    char *	cur;

    XtSetArg(arg[0], XtNpageNumber, &actual_number);
    XtSetArg(arg[1], XtNlastPageNumber, &last_page);
    XtGetValues(dvi, arg, 2);
    if (actual_number == 0)
        snprintf(value, sizeof(value), "<none>");
    else if (last_page > 0)
        snprintf(value, sizeof(value), "%d of %d", actual_number, last_page);
    else
        snprintf(value, sizeof(value), "%d", actual_number);
    text.firstPos = 0;
    text.length = strlen(value);
    text.ptr = value;
    text.format = FMT8BIT;
    XtSetArg(arg[0], XtNstring, &cur);
    XtGetValues(XawTextGetSource(pageNumber), arg, 1);
    length = strlen(cur);
    XawTextReplace(pageNumber, 0, length, &text);
}

static void
SetPageNumber(int number)
{
    Arg arg[1];

    XtSetArg(arg[0], XtNpageNumber, number);
    XtSetValues(dvi, arg, 1);
    DisplayPageNumber();
}

static void
UpdatePageNumber(Widget w, XEvent *xev, String *s, Cardinal *c)
{
    char *string;
    Arg arg[1];

    XtSetArg(arg[0], XtNstring, &string);
    XtGetValues(XawTextGetSource(pageNumber), arg, 1);
    SetPageNumber(atoi(string));
}

static void
NewResolution(char *resString)
{
    int res;
    Arg arg[1];

    res = atoi(resString);
    if (res <= 0)
        return;
    XtSetArg(arg[0], XtNscreenResolution, res);
    XtSetValues(dvi, arg, 1);
}

static void
VisitFile(char *name, Boolean resetPage)
{
    Arg arg[3];
    char *n;
    FILE *new_file;
    Boolean seek = 0;
    int i;

    if (current_file) {
        if (!strcmp(current_file_name, "-"))
            ;
        else if (current_file_name[0] == '|')
            pclose(current_file);
        else
            fclose(current_file);
    }
    if (!strcmp(name, "-"))
        new_file = stdin;
    else if (name[0] == '|')
        new_file = popen(name + 1, "r");
    else {
        struct stat stbuf;

        new_file = fopen(name, "r");

        if (!new_file) {
            perror(name);
            return;
        }
        /* Make sure it is a regular file */
        if (fstat(fileno(new_file), &stbuf) != 0) {
            perror(name);
            fclose(new_file);
            return;
        }
        if (!S_ISREG(stbuf.st_mode)) {
            fprintf(stderr, "%s is not a regular file.\n", name);
            fclose(new_file);
            return;
        }

        seek = 1;
    }
    if (!new_file) {
        /* XXX display error message */
        return;
    }
    i = 0;
    XtSetArg(arg[i], XtNfile, new_file);
    i++;
    XtSetArg(arg[i], XtNseek, seek);
    i++;
    if (resetPage) {
        XtSetArg(arg[i], XtNpageNumber, 1);
        i++;
    }
    XtSetValues(dvi, arg, i);
    XtSetArg(arg[0], XtNtitle, name);
    if (name[0] != '/' && (n = strrchr(name, '/')))
        n = n + 1;
    else
        n = name;
    XtSetArg(arg[1], XtNiconName, n);
    XtSetValues(toplevel, arg, 2);
    strncpy(current_file_name, name, sizeof(current_file_name));
    current_file_name[sizeof(current_file_name) - 1] = '\0';
    current_file = new_file;
    DisplayPageNumber();
}

static void
NewFile(char *name)
{
    VisitFile(name, TRUE);
}

static char fileBuf[1024];
static char resolutionBuf[1024];

static void
ResetMenuEntry(Widget entry)
{
    Arg arg[1];

    XtSetArg(arg[0], XtNpopupOnEntry, entry);
    XtSetValues(XtParent(entry), arg, (Cardinal) 1);
}

/*ARGSUSED*/
static void
NextPage(Widget entry, XtPointer name, XtPointer data)
{
    NextPageAction(entry, NULL, NULL, NULL);
    ResetMenuEntry(entry);
}

static void
NextPageAction(Widget w, XEvent *xev, String *s, Cardinal *c)
{
    Arg args[1];
    int number;

    XtSetArg(args[0], XtNpageNumber, &number);
    XtGetValues(dvi, args, 1);
    SetPageNumber(number + 1);
}

/*ARGSUSED*/
static void
PreviousPage(Widget entry, XtPointer name, XtPointer data)
{
    PreviousPageAction(entry, NULL, NULL, NULL);
    ResetMenuEntry(entry);
}

static void
PreviousPageAction(Widget w, XEvent *xev, String *s, Cardinal *c)
{
    Arg args[1];
    int number;

    XtSetArg(args[0], XtNpageNumber, &number);
    XtGetValues(dvi, args, 1);
    SetPageNumber(number - 1);
}

/*ARGSUSED*/
static void
SetResolution(Widget entry, XtPointer name, XtPointer data)
{
    SetResolutionAction(entry, NULL, NULL, NULL);
    ResetMenuEntry(entry);
}

static void
SetResolutionAction(Widget w, XEvent *xev, String *s, Cardinal *c)
{
    Arg args[1];
    int cur;

    XtSetArg(args[0], XtNscreenResolution, &cur);
    XtGetValues(dvi, args, 1);
    snprintf(resolutionBuf, sizeof(resolutionBuf), "%d", cur);
    MakePrompt(toplevel, "Screen resolution:", NewResolution, resolutionBuf);
}

/*ARGSUSED*/
static void
OpenFile(Widget entry, XtPointer name, XtPointer data)
{
    OpenFileAction(entry, NULL, NULL, NULL);
    ResetMenuEntry(entry);
}

static void
OpenFileAction(Widget w, XEvent *xev, String *s, Cardinal *c)
{
    if (current_file_name[0]) {
	strncpy(fileBuf, current_file_name, sizeof(fileBuf));
	fileBuf[sizeof(fileBuf) - 1] = '\0';
    }
    else
        fileBuf[0] = '\0';
    MakePrompt(toplevel, "File to open:", NewFile, fileBuf);
}

/*ARGSUSED*/
static void
RevisitFile(Widget entry, XtPointer name, XtPointer data)
{
    RevisitFileAction(entry, NULL, NULL, NULL);
    ResetMenuEntry(entry);
}

static void
RevisitFileAction(Widget w, XEvent *xev, String *s, Cardinal *c)
{
    if (current_file_name[0])
        VisitFile(current_file_name, FALSE);
}

/*ARGSUSED*/
static void
Quit(Widget entry, XtPointer closure, XtPointer data)
{
    QuitAction(entry, NULL, NULL, NULL);
}

static void
QuitAction(Widget w, XEvent *xev, String *s, Cardinal *c)
{
    exit(0);
}

static Widget promptShell, promptDialog;
static void (*promptfunction)(char *);

/* ARGSUSED */
static void
CancelAction(Widget widget, XEvent *event,
             String *params, Cardinal *num_params)
{
    if (promptShell) {
        XtSetKeyboardFocus(toplevel, (Widget) None);
        XtDestroyWidget(promptShell);
        promptShell = (Widget) 0;
    }
}

/* ARGSUSED */
static void
AcceptAction(Widget widget, XEvent *event,
             String *params, Cardinal *num_params)
{
    (*promptfunction) (XawDialogGetValueString(promptDialog));
    CancelAction(widget, event, params, num_params);
}

static void
Noop(Widget w, XEvent *xev, String *s, Cardinal *c)
{
}

static void
MakePrompt(Widget centerw, const char *prompt, void (*func)(char *), char *def)
{
    static Arg dialogArgs[] = {
        {XtNlabel,(XtArgVal) 0},
        {XtNvalue,(XtArgVal) 0},
    };
    Arg centerArgs[2];
    Position source_x, source_y;
    Position dest_x, dest_y;
    Dimension center_width, center_height;
    Dimension prompt_width, prompt_height;
    Widget valueWidget;

    CancelAction((Widget) NULL, (XEvent *) 0, (String *) 0, (Cardinal *) 0);
    promptShell = XtCreatePopupShell("promptShell", transientShellWidgetClass,
                                     toplevel, NULL, (Cardinal) 0);
    dialogArgs[0].value = (XtArgVal) prompt;
    dialogArgs[1].value = (XtArgVal) def;
    promptDialog = XtCreateManagedWidget("promptDialog", dialogWidgetClass,
                                         promptShell, dialogArgs,
                                         XtNumber(dialogArgs));
    XawDialogAddButton(promptDialog, "accept", NULL, NULL);
    XawDialogAddButton(promptDialog, "cancel", NULL, NULL);
    valueWidget = XtNameToWidget(promptDialog, "value");
    if (valueWidget) {
        Arg valueArgs[1];

        XtSetArg(valueArgs[0], XtNresizable, TRUE);
        XtSetValues(valueWidget, valueArgs, 1);
        /*
         * as resizable isn't set until just above, the
         * default value will be displayed incorrectly.
         * rectify the situation by resetting the values
         */
        XtSetValues(promptDialog, dialogArgs, XtNumber(dialogArgs));
    }
    XtSetKeyboardFocus(promptDialog, valueWidget);
    XtSetKeyboardFocus(toplevel, valueWidget);
    XtRealizeWidget(promptShell);
    /*
     * place the widget in the center of the "parent"
     */
    XtSetArg(centerArgs[0], XtNwidth, &center_width);
    XtSetArg(centerArgs[1], XtNheight, &center_height);
    XtGetValues(centerw, centerArgs, 2);
    XtSetArg(centerArgs[0], XtNwidth, &prompt_width);
    XtSetArg(centerArgs[1], XtNheight, &prompt_height);
    XtGetValues(promptShell, centerArgs, 2);
    source_x = (int) (center_width - prompt_width) / 2;
    source_y = (int) (center_height - prompt_height) / 3;
    XtTranslateCoords(centerw, source_x, source_y, &dest_x, &dest_y);
    XtSetArg(centerArgs[0], XtNx, dest_x);
    XtSetArg(centerArgs[1], XtNy, dest_y);
    XtSetValues(promptShell, centerArgs, 2);
    XtMapWidget(promptShell);
    promptfunction = func;
}