~ubuntu-branches/ubuntu/dapper/groff/dapper

« back to all changes in this revision

Viewing changes to src/xditview/xditview.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2002-03-17 04:11:50 UTC
  • Revision ID: james.westby@ubuntu.com-20020317041150-wkgfawjc3gxlk0o5
Tags: upstream-1.17.2
ImportĀ upstreamĀ versionĀ 1.17.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 1991 Massachusetts Institute of Technology
 
3
 *
 
4
 * Permission to use, copy, modify, distribute, and sell this software and its
 
5
 * documentation for any purpose is hereby granted without fee, provided that
 
6
 * the above copyright notice appear in all copies and that both that
 
7
 * copyright notice and this permission notice appear in supporting
 
8
 * documentation, and that the name of M.I.T. not be used in advertising or
 
9
 * publicity pertaining to distribution of the software without specific,
 
10
 * written prior permission.  M.I.T. makes no representations about the
 
11
 * suitability of this software for any purpose.  It is provided "as is"
 
12
 * without express or implied warranty.
 
13
 *
 
14
 * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 
15
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
 
16
 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
17
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 
18
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 
19
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
20
 *
 
21
 */
 
22
/*
 
23
 * xditview -- 
 
24
 *
 
25
 *   Display ditroff output in an X window
 
26
 */
 
27
 
 
28
#ifndef SABER
 
29
#ifndef lint
 
30
static char rcsid[] = "$XConsortium: xditview.c,v 1.17 89/12/10 17:05:08 rws Exp $";
 
31
#endif /* lint */
 
32
#endif /* SABER */
 
33
 
 
34
#include <X11/Xatom.h>
 
35
#include <X11/Xlib.h>
 
36
#include <X11/Xos.h>
 
37
#include <X11/Intrinsic.h>
 
38
#include <X11/StringDefs.h>
 
39
#include <X11/Shell.h>
 
40
#include <X11/Xaw/Paned.h>
 
41
#include <X11/Xaw/Viewport.h>
 
42
#include <X11/Xaw/Box.h>
 
43
#include <X11/Xaw/Command.h>
 
44
#include <X11/Xaw/Dialog.h>
 
45
#include <X11/Xaw/Label.h>
 
46
#include <X11/Xaw/SimpleMenu.h>
 
47
#include <X11/Xaw/SmeBSB.h>
 
48
 
 
49
#include <signal.h>
 
50
 
 
51
#include "Dvi.h"
 
52
 
 
53
#include "xdit.bm"
 
54
#include "xdit_mask.bm"
 
55
#include "stdio.h"
 
56
 
 
57
extern FILE *popen();
 
58
extern void exit();
 
59
 
 
60
static String fallback_resources[] = {
 
61
#include "GXditview-ad.h"
 
62
    NULL
 
63
};
 
64
 
 
65
static struct app_resources {
 
66
    char *print_command;
 
67
    char *filename;
 
68
} app_resources;
 
69
 
 
70
#define offset(field) XtOffset(struct app_resources *, field)
 
71
 
 
72
/* Application resources. */
 
73
 
 
74
static XtResource resources[] = {
 
75
  {"printCommand", "PrintCommand", XtRString, sizeof(char*),
 
76
       offset(print_command), XtRString, NULL},
 
77
  {"filename", "Filename", XtRString, sizeof(char*),
 
78
       offset(filename), XtRString, NULL},
 
79
};
 
80
 
 
81
#undef offset
 
82
 
 
83
/* Command line options table.  Only resources are entered here...there is a
 
84
   pass over the remaining options after XtParseCommand is let loose. */
 
85
 
 
86
static XrmOptionDescRec options[] = {
 
87
{"-page",           "*dvi.pageNumber",      XrmoptionSepArg,    NULL},
 
88
{"-backingStore",   "*dvi.backingStore",    XrmoptionSepArg,    NULL},
 
89
{"-resolution",     "*dvi.resolution",      XrmoptionSepArg,    NULL},
 
90
{"-printCommand",   ".printCommand",        XrmoptionSepArg,    NULL},
 
91
{"-filename",       ".filename",            XrmoptionSepArg,    NULL},
 
92
{"-noPolyText",     "*dvi.noPolyText",      XrmoptionNoArg,     "TRUE"},
 
93
};
 
94
 
 
95
static char current_print_command[1024];
 
96
 
 
97
static char     current_file_name[1024];
 
98
static FILE     *current_file;
 
99
 
 
100
/*
 
101
 * Report the syntax for calling xditview.
 
102
 */
 
103
 
 
104
static
 
105
Syntax(call)
 
106
        char *call;
 
107
{
 
108
        (void) printf ("Usage: %s [-fg <color>] [-bg <color>]\n", call);
 
109
        (void) printf ("       [-bd <color>] [-bw <pixels>] [-help]\n");
 
110
        (void) printf ("       [-display displayname] [-geometry geom]\n");
 
111
        (void) printf ("       [-page <page-number>] [-backing <backing-store>]\n");
 
112
        (void) printf ("       [-resolution <res>] [-print <command>]\n");
 
113
        (void) printf ("       [-filename <file>] [filename]\n\n");
 
114
        exit(1);
 
115
}
 
116
 
 
117
static void     NewFile (), SetPageNumber ();
 
118
static Widget   toplevel, paned, viewport, dvi;
 
119
static Widget   page;
 
120
static Widget   simpleMenu;
 
121
 
 
122
static void     NextPage(), PreviousPage(), SelectPage(), OpenFile(), Quit();
 
123
static void     Print();
 
124
 
 
125
static struct menuEntry {
 
126
    char    *name;
 
127
    void    (*function)();
 
128
} menuEntries[] = {
 
129
    "nextPage",     NextPage,
 
130
    "previousPage", PreviousPage,
 
131
    "selectPage",   SelectPage,
 
132
    "print",        Print,
 
133
    "openFile",     OpenFile,
 
134
    "quit",         Quit,
 
135
};
 
136
 
 
137
static void     NextPageAction(), PreviousPageAction(), SelectPageAction();
 
138
static void     OpenFileAction(), QuitAction();
 
139
static void     AcceptAction(), CancelAction();
 
140
static void     PrintAction();
 
141
static void     RerasterizeAction();
 
142
 
 
143
XtActionsRec xditview_actions[] = {
 
144
    "NextPage",     NextPageAction,
 
145
    "PreviousPage", PreviousPageAction,
 
146
    "SelectPage",   SelectPageAction,
 
147
    "Print",        PrintAction,
 
148
    "OpenFile",     OpenFileAction,
 
149
    "Rerasterize",  RerasterizeAction,
 
150
    "Quit",         QuitAction,
 
151
    "Accept",       AcceptAction,
 
152
    "Cancel",       CancelAction,
 
153
};
 
154
 
 
155
#define MenuNextPage            0
 
156
#define MenuPreviousPage        1
 
157
#define MenuSelectPage          2
 
158
#define MenuPrint               3
 
159
#define MenuOpenFile            4
 
160
#define MenuQuit                5
 
161
 
 
162
static char     pageLabel[256] = "Page <none>";
 
163
 
 
164
int main(argc, argv)
 
165
    int argc;
 
166
    char **argv;
 
167
{
 
168
    char            *file_name = 0;
 
169
    int             i;
 
170
    static Arg      labelArgs[] = {
 
171
                        {XtNlabel, (XtArgVal) pageLabel},
 
172
    };
 
173
    XtAppContext    xtcontext;
 
174
    Arg             topLevelArgs[2];
 
175
    Widget          entry;
 
176
    Arg             pageNumberArgs[1];
 
177
    int             page_number;
 
178
 
 
179
    toplevel = XtAppInitialize(&xtcontext, "GXditview",
 
180
                            options, XtNumber (options),
 
181
                            &argc, argv, fallback_resources, NULL, 0);
 
182
    if (argc > 2)
 
183
        Syntax(argv[0]);
 
184
 
 
185
    XtGetApplicationResources(toplevel, (XtPointer)&app_resources,
 
186
                              resources, XtNumber(resources),
 
187
                              NULL, (Cardinal) 0);
 
188
    if (app_resources.print_command)
 
189
        strcpy(current_print_command, app_resources.print_command);
 
190
 
 
191
    XtAppAddActions(xtcontext, xditview_actions, XtNumber (xditview_actions));
 
192
 
 
193
    XtSetArg (topLevelArgs[0], XtNiconPixmap,
 
194
              XCreateBitmapFromData (XtDisplay (toplevel),
 
195
                                     XtScreen(toplevel)->root,
 
196
                                     xdit_bits, xdit_width, xdit_height));
 
197
                                    
 
198
    XtSetArg (topLevelArgs[1], XtNiconMask,
 
199
              XCreateBitmapFromData (XtDisplay (toplevel),
 
200
                                     XtScreen(toplevel)->root,
 
201
                                     xdit_mask_bits, 
 
202
                                     xdit_mask_width, xdit_mask_height));
 
203
    XtSetValues (toplevel, topLevelArgs, 2);
 
204
    if (argc > 1)
 
205
        file_name = argv[1];
 
206
 
 
207
    /*
 
208
     * create the menu and insert the entries
 
209
     */
 
210
    simpleMenu = XtCreatePopupShell ("menu", simpleMenuWidgetClass, toplevel,
 
211
                                    NULL, 0);
 
212
    for (i = 0; i < XtNumber (menuEntries); i++) {
 
213
        entry = XtCreateManagedWidget(menuEntries[i].name, 
 
214
                                      smeBSBObjectClass, simpleMenu,
 
215
                                      NULL, (Cardinal) 0);
 
216
        XtAddCallback(entry, XtNcallback, menuEntries[i].function, NULL);
 
217
    }
 
218
 
 
219
    paned = XtCreateManagedWidget("paned", panedWidgetClass, toplevel,
 
220
                                    NULL, (Cardinal) 0);
 
221
    viewport = XtCreateManagedWidget("viewport", viewportWidgetClass, paned,
 
222
                                     NULL, (Cardinal) 0);
 
223
    dvi = XtCreateManagedWidget ("dvi", dviWidgetClass, viewport, NULL, 0);
 
224
    page = XtCreateManagedWidget ("label", labelWidgetClass, paned,
 
225
                                        labelArgs, XtNumber (labelArgs));
 
226
    XtSetArg (pageNumberArgs[0], XtNpageNumber, &page_number);
 
227
    XtGetValues (dvi, pageNumberArgs, 1);
 
228
    if (file_name)
 
229
        NewFile (file_name);
 
230
    /* NewFile modifies current_file_name, so do this here. */
 
231
    if (app_resources.filename)
 
232
        strcpy(current_file_name, app_resources.filename);
 
233
    XtRealizeWidget (toplevel);
 
234
    if (file_name)
 
235
        SetPageNumber (page_number);
 
236
    XtAppMainLoop(xtcontext);
 
237
    return 0;
 
238
}
 
239
 
 
240
static void
 
241
SetPageNumber (number)
 
242
{
 
243
    Arg arg[2];
 
244
    int actual_number, last_page;
 
245
 
 
246
    XtSetArg (arg[0], XtNpageNumber, number);
 
247
    XtSetValues (dvi, arg, 1);
 
248
    XtSetArg (arg[0], XtNpageNumber, &actual_number);
 
249
    XtSetArg (arg[1], XtNlastPageNumber, &last_page);
 
250
    XtGetValues (dvi, arg, 2);
 
251
    if (actual_number == 0)
 
252
        sprintf (pageLabel, "Page <none>");
 
253
    else if (last_page > 0)
 
254
        sprintf (pageLabel, "Page %d of %d", actual_number, last_page);
 
255
    else
 
256
        sprintf (pageLabel, "Page %d", actual_number);
 
257
    XtSetArg (arg[0], XtNlabel, pageLabel);
 
258
    XtSetValues (page, arg, 1);
 
259
}
 
260
 
 
261
static void
 
262
SelectPageNumber (number_string)
 
263
char    *number_string;
 
264
{
 
265
        SetPageNumber (atoi(number_string));
 
266
}
 
267
 
 
268
static int hadFile = 0;
 
269
 
 
270
static void
 
271
NewFile (name)
 
272
char    *name;
 
273
{
 
274
    Arg     arg[2];
 
275
    char    *n;
 
276
    FILE    *new_file;
 
277
    Boolean seek = 0;
 
278
 
 
279
    if (current_file) {
 
280
        if (!strcmp (current_file_name, "-"))
 
281
            ;
 
282
        else if (current_file_name[0] == '|')
 
283
            pclose (current_file);
 
284
        else
 
285
            fclose (current_file);
 
286
    }
 
287
    if (!strcmp (name, "-"))
 
288
        new_file = stdin;
 
289
    else if (name[0] == '|')
 
290
        new_file = popen (name+1, "r");
 
291
    else {
 
292
        new_file = fopen (name, "r");
 
293
        seek = 1;
 
294
    }
 
295
    if (!new_file) {
 
296
        /* XXX display error message */
 
297
        return;
 
298
    }
 
299
    XtSetArg (arg[0], XtNfile, new_file);
 
300
    XtSetArg (arg[1], XtNseek, seek);
 
301
    XtSetValues (dvi, arg, 2);
 
302
    if (hadFile || name[0] != '-' || name[1] != '\0') {
 
303
        XtSetArg (arg[0], XtNtitle, name);
 
304
        if (name[0] != '/' && (n = strrchr (name, '/')))
 
305
            n = n + 1;
 
306
        else
 
307
            n = name;
 
308
        XtSetArg (arg[1], XtNiconName, n);
 
309
        XtSetValues (toplevel, arg, 2);
 
310
    }
 
311
    hadFile = 1;
 
312
    SelectPageNumber ("1");
 
313
    strcpy (current_file_name, name);
 
314
    current_file = new_file;
 
315
}
 
316
 
 
317
static char fileBuf[1024];
 
318
 
 
319
ResetMenuEntry (entry)
 
320
    Widget  entry;
 
321
{
 
322
    Arg arg[1];
 
323
 
 
324
    XtSetArg (arg[0], XtNpopupOnEntry, entry);
 
325
    XtSetValues (XtParent(entry) , arg, (Cardinal) 1);
 
326
}
 
327
 
 
328
/*ARGSUSED*/
 
329
 
 
330
static void
 
331
NextPage (entry, name, data)
 
332
    Widget  entry;
 
333
    caddr_t name, data;
 
334
{
 
335
    NextPageAction();
 
336
    ResetMenuEntry (entry);
 
337
}
 
338
 
 
339
static void
 
340
NextPageAction ()
 
341
{
 
342
    Arg args[1];
 
343
    int number;
 
344
 
 
345
    XtSetArg (args[0], XtNpageNumber, &number);
 
346
    XtGetValues (dvi, args, 1);
 
347
    SetPageNumber (number+1);
 
348
}
 
349
 
 
350
/*ARGSUSED*/
 
351
 
 
352
static void
 
353
PreviousPage (entry, name, data)
 
354
    Widget  entry;
 
355
    caddr_t name, data;
 
356
{
 
357
    PreviousPageAction ();
 
358
    ResetMenuEntry (entry);
 
359
}
 
360
 
 
361
static void
 
362
PreviousPageAction ()
 
363
{
 
364
    Arg args[1];
 
365
    int number;
 
366
 
 
367
    XtSetArg (args[0], XtNpageNumber, &number);
 
368
    XtGetValues (dvi, args, 1);
 
369
    SetPageNumber (number-1);
 
370
}
 
371
 
 
372
/* ARGSUSED */
 
373
 
 
374
static void
 
375
SelectPage (entry, name, data)
 
376
    Widget  entry;
 
377
    caddr_t name, data;
 
378
{
 
379
    SelectPageAction ();
 
380
    ResetMenuEntry (entry);
 
381
}
 
382
 
 
383
static void
 
384
SelectPageAction ()
 
385
{
 
386
    MakePrompt (toplevel, "Page number", SelectPageNumber, "");
 
387
}
 
388
 
 
389
 
 
390
static void
 
391
DoPrint (name)
 
392
    char *name;
 
393
{
 
394
    FILE *print_file;
 
395
#ifdef SIGNALRETURNSINT
 
396
    int (*handler)();
 
397
#else
 
398
    void (*handler)();
 
399
#endif
 
400
    /* Avoid dieing because of an invalid command. */
 
401
    handler = signal(SIGPIPE, SIG_IGN);
 
402
 
 
403
    print_file = popen(name, "w");
 
404
    if (!print_file)
 
405
        /* XXX print error message */
 
406
        return;
 
407
    DviSaveToFile(dvi, print_file);
 
408
    pclose(print_file);
 
409
    signal(SIGPIPE, handler);
 
410
    strcpy(current_print_command, name);
 
411
}
 
412
 
 
413
static void
 
414
RerasterizeAction()
 
415
{
 
416
    Arg args[1];
 
417
    int number;
 
418
 
 
419
    if (current_file_name[0] == 0) {
 
420
        /* XXX display an error message */
 
421
        return;
 
422
    } 
 
423
    XtSetArg (args[0], XtNpageNumber, &number);
 
424
    XtGetValues (dvi, args, 1);
 
425
    NewFile(current_file_name);
 
426
    SetPageNumber (number);
 
427
}
 
428
 
 
429
/* ARGSUSED */
 
430
 
 
431
static void
 
432
Print (entry, name, data)
 
433
    Widget  entry;
 
434
    caddr_t name, data;
 
435
{
 
436
    PrintAction ();
 
437
    ResetMenuEntry (entry);
 
438
}
 
439
 
 
440
static void
 
441
PrintAction ()
 
442
{
 
443
    if (current_print_command[0])
 
444
        strcpy (fileBuf, current_print_command);
 
445
    else
 
446
        fileBuf[0] = '\0';
 
447
    MakePrompt (toplevel, "Print command:", DoPrint, fileBuf);
 
448
}
 
449
 
 
450
 
 
451
/* ARGSUSED */
 
452
 
 
453
static void
 
454
OpenFile (entry, name, data)
 
455
    Widget  entry;
 
456
    caddr_t name, data;
 
457
{
 
458
    OpenFileAction ();
 
459
    ResetMenuEntry (entry);
 
460
}
 
461
 
 
462
static void
 
463
OpenFileAction ()
 
464
{
 
465
    if (current_file_name[0])
 
466
        strcpy (fileBuf, current_file_name);
 
467
    else
 
468
        fileBuf[0] = '\0';
 
469
    MakePrompt (toplevel, "File to open:", NewFile, fileBuf);
 
470
}
 
471
 
 
472
/* ARGSUSED */
 
473
 
 
474
static void
 
475
Quit (entry, closure, data)
 
476
    Widget  entry;
 
477
    caddr_t closure, data;
 
478
{
 
479
    QuitAction ();
 
480
}
 
481
 
 
482
static void
 
483
QuitAction ()
 
484
{
 
485
    exit (0);
 
486
}
 
487
 
 
488
Widget  promptShell, promptDialog;
 
489
void    (*promptfunction)();
 
490
 
 
491
/* ARGSUSED */
 
492
static
 
493
void CancelAction (widget, event, params, num_params)
 
494
    Widget      widget;
 
495
    XEvent      *event;
 
496
    String      *params;
 
497
    Cardinal    *num_params;
 
498
{
 
499
    if (promptShell) {
 
500
        XtSetKeyboardFocus(toplevel, (Widget) None);
 
501
        XtDestroyWidget(promptShell);
 
502
        promptShell = (Widget) 0;
 
503
    }
 
504
}
 
505
 
 
506
static
 
507
void AcceptAction (widget, event, params, num_params)
 
508
    Widget      widget;
 
509
    XEvent      *event;
 
510
    String      *params;
 
511
    Cardinal    *num_params;
 
512
{
 
513
    (*promptfunction)(XawDialogGetValueString(promptDialog));
 
514
    CancelAction (widget, event, params, num_params);
 
515
}
 
516
 
 
517
MakePrompt(centerw, prompt, func, def)
 
518
Widget  centerw;
 
519
char *prompt;
 
520
void (*func)();
 
521
char    *def;
 
522
{
 
523
    static Arg dialogArgs[] = {
 
524
        {XtNlabel, 0},
 
525
        {XtNvalue, 0},
 
526
    };
 
527
    Arg valueArgs[1];
 
528
    Arg centerArgs[2];
 
529
    Position    source_x, source_y;
 
530
    Position    dest_x, dest_y;
 
531
    Dimension center_width, center_height;
 
532
    Dimension prompt_width, prompt_height;
 
533
    Widget  valueWidget;
 
534
    
 
535
    CancelAction ((Widget)NULL, (XEvent *) 0, (String *) 0, (Cardinal *) 0);
 
536
    promptShell = XtCreatePopupShell ("promptShell", transientShellWidgetClass,
 
537
                                      toplevel, NULL, (Cardinal) 0);
 
538
    dialogArgs[0].value = (XtArgVal)prompt;
 
539
    dialogArgs[1].value = (XtArgVal)def;
 
540
    promptDialog = XtCreateManagedWidget( "promptDialog", dialogWidgetClass,
 
541
                    promptShell, dialogArgs, XtNumber (dialogArgs));
 
542
    XawDialogAddButton(promptDialog, "accept", NULL, (caddr_t) 0);
 
543
    XawDialogAddButton(promptDialog, "cancel", NULL, (caddr_t) 0);
 
544
    valueWidget = XtNameToWidget (promptDialog, "value");
 
545
    if (valueWidget) {
 
546
        XtSetArg (valueArgs[0], XtNresizable, TRUE);
 
547
        XtSetValues (valueWidget, valueArgs, 1);
 
548
        /*
 
549
         * as resizable isn't set until just above, the
 
550
         * default value will be displayed incorrectly.
 
551
         * rectify the situation by resetting the values
 
552
         */
 
553
        XtSetValues (promptDialog, dialogArgs, XtNumber (dialogArgs));
 
554
    }
 
555
    XtSetKeyboardFocus (promptDialog, valueWidget);
 
556
    XtSetKeyboardFocus (toplevel, valueWidget);
 
557
    XtRealizeWidget (promptShell);
 
558
    /*
 
559
     * place the widget in the center of the "parent"
 
560
     */
 
561
    XtSetArg (centerArgs[0], XtNwidth, &center_width);
 
562
    XtSetArg (centerArgs[1], XtNheight, &center_height);
 
563
    XtGetValues (centerw, centerArgs, 2);
 
564
    XtSetArg (centerArgs[0], XtNwidth, &prompt_width);
 
565
    XtSetArg (centerArgs[1], XtNheight, &prompt_height);
 
566
    XtGetValues (promptShell, centerArgs, 2);
 
567
    source_x = (center_width - prompt_width) / 2;
 
568
    source_y = (center_height - prompt_height) / 3;
 
569
    XtTranslateCoords (centerw, source_x, source_y, &dest_x, &dest_y);
 
570
    XtSetArg (centerArgs[0], XtNx, dest_x);
 
571
    XtSetArg (centerArgs[1], XtNy, dest_y);
 
572
    XtSetValues (promptShell, centerArgs, 2);
 
573
    XtMapWidget(promptShell);
 
574
    promptfunction = func;
 
575
}
 
576
 
 
577
/* For DviChar.c */
 
578
 
 
579
char *xmalloc(n)
 
580
    int n;
 
581
{
 
582
    return XtMalloc(n);
 
583
}
 
584
 
 
585
/*
 
586
Local Variables:
 
587
c-indent-level: 4
 
588
c-continued-statement-offset: 4
 
589
c-brace-offset: -4
 
590
c-argdecl-indent: 4
 
591
c-label-offset: -4
 
592
c-tab-always-indent: nil
 
593
End:
 
594
*/