File: main.c

package info (click to toggle)
xpaint 2.7.6-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,532 kB
  • ctags: 3,405
  • sloc: ansic: 36,749; makefile: 49; sh: 18
file content (873 lines) | stat: -rw-r--r-- 23,066 bytes parent folder | download
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
/* +-------------------------------------------------------------------+ */
/* | Copyright 1992, 1993, David Koblas (koblas@netcom.com)            | */
/* | Copyright 1995, 1996 Torsten Martinsen (bullestock@dk-online.dk)  | */
/* |                                                                   | */
/* | Permission to use, copy, modify, and to distribute this software  | */
/* | and its documentation for any purpose is hereby granted without   | */
/* | fee, provided that the above copyright notice appear in all       | */
/* | copies and that both that copyright notice and this permission    | */
/* | notice appear in supporting documentation.  There is no           | */
/* | representations about the suitability of this software for        | */
/* | any purpose.  this software is provided "as is" without express   | */
/* | or implied warranty.                                              | */
/* |                                                                   | */
/* +-------------------------------------------------------------------+ */

/* $Id: main.c,v 1.17 2005/03/20 20:15:32 demailly Exp $ */

#ifdef __VMS
#define XtDisplay XTDISPLAY
#define XtScreen XTSCREEN
#endif

#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/StringDefs.h>
#include <ctype.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#ifndef NOSTDHDRS
#include <stdlib.h>
#include <unistd.h>
#endif

#include <xpm.h>

#define DEFINE_GLOBAL
#include "xpaint.h"
#include "Paint.h"
#include "misc.h"
#include "operation.h"
#include "graphic.h"
#include "messages.h"
#include "protocol.h"
#include "image.h"
#include "rw/rwTable.h"

#include "XPaintIcon.xpm"

String *helpText;
int helpNum;
String *msgText;
int msgNum;

char lang[10];
char *routine;

extern void BrushInit(Widget toplevel);

static char *appDefaults[] =
{
#include "XPaint.ad.h"
    NULL
};

typedef struct {
    String visualType;
    String lang;
    String size;
    String winsize;
    String help;
    String rcFile;
    String helpFile;
    String msgFile;
    int zoom;
    Boolean fullpopup;
    Boolean nomenubar;
    Boolean popped;
    Boolean snapshot;
    Boolean clipboard;
    Boolean horizontal;
    Boolean nowarn;
} AppInfo;

static AppInfo appInfo;

static XtResource resources[] =
{
    {"visualType", "VisualType", XtRString, sizeof(String),
   XtOffset(AppInfo *, visualType), XtRImmediate, (XtPointer) "default"},
    {"lang", "Lang", XtRString, sizeof(String),
     XtOffset(AppInfo *, lang), XtRImmediate, (XtPointer) NULL},
    {"size", "Size", XtRString, sizeof(String),
     XtOffset(AppInfo *, size), XtRImmediate, (XtPointer) "640x480"},
    {"winsize", "WinSize", XtRString, sizeof(String),
     XtOffset(AppInfo *, winsize), XtRImmediate, (XtPointer) "0x0"},
    {"rcFile", "RcFile", XtRString, sizeof(String),
     XtOffset(AppInfo *, rcFile), XtRImmediate, (XtPointer) NULL},
    {"help", "Help", XtRString, sizeof(String),
     XtOffset(AppInfo *, help), XtRImmediate, (XtPointer) NULL},
    {"helpFile", "HelpFile", XtRString, sizeof(String),
     XtOffset(AppInfo *, helpFile), XtRImmediate, (XtPointer) NULL},
    {"msgFile", "MsgFile", XtRString, sizeof(String),
     XtOffset(AppInfo *, msgFile), XtRImmediate, (XtPointer) NULL},
    {"zoom", "Zoom", XtRInt, sizeof(int),
     XtOffset(AppInfo *, zoom), XtRImmediate, (XtPointer) 1},
    {"fullpopup", "FullPopup", XtRBoolean, sizeof(Boolean),
     XtOffset(AppInfo *, fullpopup), XtRImmediate, (XtPointer) True},
    {"nomenubar", "NoMenubar", XtRBoolean, sizeof(Boolean),
     XtOffset(AppInfo *, nomenubar), XtRImmediate, (XtPointer) False},
    {"popped", "Popped", XtRBoolean, sizeof(Boolean),
     XtOffset(AppInfo *, popped), XtRImmediate, (XtPointer) False},
    {"snapshot", "Snapshot", XtRBoolean, sizeof(Boolean),
     XtOffset(AppInfo *, snapshot), XtRImmediate, (XtPointer) False},
    {"clipboard", "Clipboard", XtRBoolean, sizeof(Boolean),
     XtOffset(AppInfo *, clipboard), XtRImmediate, (XtPointer) False},
    {"horizontal", "Horizontal", XtRBoolean, sizeof(Boolean),
     XtOffset(AppInfo *, horizontal), XtRImmediate, (XtPointer) False},
    {"nowarn", "NoWarn", XtRBoolean, sizeof(Boolean),
     XtOffset(AppInfo *, nowarn), XtRImmediate, (XtPointer) False},
};

static XrmOptionDescRec options[] =
{
    {"-24", ".visualType", XrmoptionNoArg, (XtPointer) "true24"},
    {"-12", ".visualType", XrmoptionNoArg, (XtPointer) "cmap12"},
    {"-language", ".lang", XrmoptionSepArg, (XtPointer) NULL},
    {"-size", ".size", XrmoptionSepArg, (XtPointer) NULL},
    {"-winsize", ".winsize", XrmoptionSepArg, (XtPointer) "0x0"},
    {"-rcFile", ".rcFile", XrmoptionSepArg, (XtPointer) NULL},
    {"-helpFile", ".helpFile", XrmoptionSepArg, (XtPointer) NULL},
    {"-msgFile", ".msgFile", XrmoptionSepArg, (XtPointer) NULL},
    {"-zoom", ".zoom", XrmoptionSepArg, (XtPointer) "-1"},
    {"-simplepopup", ".fullpopup", XrmoptionNoArg, (XtPointer) "False"},
    {"-fullpopup", ".fullpopup", XrmoptionNoArg, (XtPointer) "True"},
    {"-nomenubar", ".nomenubar", XrmoptionNoArg, (XtPointer) "True"},
    {"-popped", ".popped", XrmoptionNoArg, (XtPointer) "True"},
    {"-snapshot", ".snapshot", XrmoptionNoArg, (XtPointer) "True"},
    {"-clipboard", ".clipboard", XrmoptionNoArg, (XtPointer) "True"},
    {"-horizontal", ".horizontal", XrmoptionNoArg, (XtPointer) "True"},
    {"-nowarn", ".nowarn", XrmoptionNoArg, (XtPointer) "True"},
    {"-help", ".help", XrmoptionNoArg, (XtPointer) "command"},
    {"+help", ".help", XrmoptionSepArg, (XtPointer) NULL},
    {"-8", ".visualType", XrmoptionNoArg, (XtPointer) "cmap8"},
    {"-visual", ".visualType", XrmoptionSepArg, (XtPointer) NULL},
};


/*
**  Public query functions for application defaults
 */

void 
SetDefaultWHZ(int w, int h, int zoom)
{
    char str[40];
    if (w<=0) w = 1;
    if (h<=0) h = 1;
    if (zoom<=0) zoom = -1;
    if (zoom>32) zoom = 32;
    sprintf(str, "%dx%d", w, h);
    appInfo.size = strdup(str);
    appInfo.zoom = zoom;
}

void 
GetDefaultWH(int *w, int *h)
{
    int x, y;
    unsigned int width, height;

    XParseGeometry(appInfo.size, &x, &y, &width, &height);
    *w = width;
    *h = height;
}

void 
GetPaintWH(int *w, int *h)
{
    int x, y;
    unsigned int width, height;

    XParseGeometry(appInfo.winsize, &x, &y, &width, &height);
    *w = width;
    *h = height;
}

void 
GetInitZoom(int *z)
{
    *z = appInfo.zoom;
    if (*z<=0) *z = -1;
    if (*z>32) *z = 32;
}

void 
SetMenuBarHidden(Boolean flag)
{
    appInfo.nomenubar = flag;
}

Boolean
ThereIsNoMenuBar()
{
    return appInfo.nomenubar;
}

void 
SetFullMenuPopup(Boolean flag)
{
    appInfo.fullpopup = flag;
}

Boolean
IsFullPopup()
{
    return appInfo.fullpopup;
}

Boolean
ToolsAreHorizontal()
{
    return appInfo.horizontal;
}

char *
GetDefaultRC(void)
{
    return appInfo.rcFile;
}

/*
**  Create a nice icon image for XPaint...
 */
void 
SetIconImage(Widget w)
{
    static Pixmap icon = None;
    static int iconW = 1, iconH = 1;
    Window iconWindow;
    Screen *screen = XtScreen(w);
    Display *dpy = XtDisplay(w);
    XpmAttributes myattributes;

    /*
    **  Build the XPaint icon
     */
    iconWindow = XCreateSimpleWindow(dpy, RootWindowOfScreen(screen),
				     0, 0,	/* x, y */
				     iconW, iconH, 0,
				     BlackPixelOfScreen(screen),
				     BlackPixelOfScreen(screen));
    if (icon == None) {
 	myattributes.valuemask = XpmCloseness;
 	myattributes.closeness = 65535;
 	if (XpmCreatePixmapFromData(dpy, iconWindow,
				    XPaintIcon_xpm, &icon, NULL, &myattributes)) {
	    fprintf(stderr, msgText[CANNOT_INSTALL_XPM_ICON]);
	    exit(1);
 	}
	GetPixmapWHD(dpy, icon, &iconW, &iconH, NULL);
	XResizeWindow(dpy, iconWindow, iconW, iconH);
    }
    XSetWindowBackgroundPixmap(dpy, iconWindow, icon);

    XtVaSetValues(w, XtNiconWindow, iconWindow, NULL);
}


/*
**  The rest of main
 */
static void 
usage(char *prog, char *msg)
{
    if (msg)
	fprintf(stderr, "%s\n", msg);
    fprintf(stderr, "%s %s (XPaint %s):\n", 
            msgText[USAGE_FOR], prog, XPAINT_VERSION);
    HelpTextOutput(stderr, appInfo.help == NULL ? "command" : appInfo.help);
    exit(1);
}

typedef struct {
    XtWorkProcId id;
    Widget toplevel;
    int nfiles;
    int pos;
    char **files;
} LocalInfo;

static Boolean
workProc(LocalInfo * l)
{
    char *file;
    void *v;

    if ((l == NULL) || (l->id == None))
	return TRUE;

    file = l->files[l->pos];

    StateSetBusy(True);

    if ((v = ReadMagic(file)) != NULL)
	GraphicOpenFile(l->toplevel, file, v);
    else
	Notice(l->toplevel, msgText[UNABLE_TO_OPEN_INPUT_FILE],
	       file, RWGetMsg());

    StateSetBusy(False);

    if (++l->pos == l->nfiles) {
	XtFree((XtPointer) l->files);
	XtFree((XtPointer) l);
	return TRUE;
    } else
	return FALSE;
}

/*
 * You can specify:
 * the visual
 * the visual and the depth
 * the visualID
 * Any other combinations may not generate the results that you expect
 * Also note that the visualID is strongly dependent on the platform
 * being used.  We will let the user specify any visualID that they want.
 */
XVisualInfo
GetFutureVisual(Widget *toplevel, int desiredDepth, int desiredVisual,
		int desiredVisualID, Boolean *valid)
{
    Display *display = XtDisplay(*toplevel);
    int i, visnum = 0;
	
    XVisualInfo rtnval;
	
    int visualsMatched;    
     
    XVisualInfo vTemplate;   
    XVisualInfo visual[50]; /* If a machine has more than 50 visuals then this will fail. Higher end SGI's have 20 so this is probably OK */
    XVisualInfo *visualList = visual;
	

    *valid=FALSE;
      
    vTemplate.screen = DefaultScreen(display);

    if (desiredVisualID >= 0)
        visualList = XGetVisualInfo (display, VisualScreenMask, &vTemplate, &visualsMatched);
    else {
        vTemplate.class = desiredVisual;
	if (desiredDepth > 0) {
	    vTemplate.depth = desiredDepth;
	    visualList = XGetVisualInfo(display,
					VisualClassMask | VisualScreenMask |
					VisualDepthMask, &vTemplate,
					&visualsMatched);
	}
	else {
  	    visualList = XGetVisualInfo(display,
					VisualClassMask | VisualScreenMask,
					&vTemplate, &visualsMatched);
	}
    }

    if (visualsMatched && desiredVisualID >= 0) {
        visnum = -1;
	/* look through the entire list for our visual ID */
	for (i = 0; i < visualsMatched; i++) {
  	    if (XVisualIDFromVisual(visualList[i].visual) == desiredVisualID)
	        visnum = i;
	}
	if (visnum < 0)
	    visualsMatched=0;
    }
	
    if (visualsMatched == 0) {
        fprintf(stderr, msgText[NO_MATCH_FOUND_FOR_VISUAL]);
	exit(1);
    }
	
    rtnval = visualList[visnum];
    *valid = TRUE;
	  
    XFree(visualList);
	
    return rtnval;
}

void
LoadMessages()
{
    FILE *fd;
    int i, j;
    char buf[256];
    static char alert[] = "!! No messages found !!";
    static char failure[] = "Memory allocation failure\n";

    helpNum = 0; 
    fd = fopen(appInfo.helpFile, "r");
    if (!fd) {
        sprintf(buf, "%s/help/Help", SHAREDIR);
	fd = fopen(buf, "r");
    }
    if (fd) {
        while (!feof(fd)) {
            ++helpNum;
            helpText = (String *)realloc(helpText, helpNum*sizeof(String));
            fgets(buf, 250, fd);
            i = strlen(buf)-1;
            if (i>0 && buf[i] == '\n') buf[i] = '\0';
            helpText[helpNum-1] = strdup(buf);
        }
        fclose(fd);
    } else {
        fprintf(stderr, "No help file found !!\n");
    }

    msgNum = 0; 
    fd = fopen(appInfo.msgFile, "r");
    if (!fd) {
        sprintf(buf, "%s/messages/Messages", SHAREDIR);
	fd = fopen(buf, "r");
    }
    if (fd) {
        while (!feof(fd)) {
            fgets(buf, 250, fd);
	    if (*buf == '#') {
	         if (!strncmp(buf+1, "END_MESSAGES", 12)) break;
	         continue;
	    }
            ++msgNum;
            msgText = (String *)realloc(msgText, msgNum*sizeof(String));
	    if (!msgText) {
	        fprintf(stderr, failure);
	        exit(1);
	    }
            i = 0;
            j = 0;
            while (buf[i]) {
	        if (buf[i] == '\n') break;
	        if (buf[i] == '\\' && buf[i+1] == 'n') {
                     ++i;
		     buf[j] = '\n';
		} else
		if (j<i)
		     buf[j] = buf[i];
                ++i;
                ++j;
	    }
	    buf[j] = '\0';
            msgText[msgNum-1] = strdup(buf);
	    if (!msgText[msgNum-1]) {
	        fprintf(stderr, failure);
	        exit(1);
	    }
        }
        fclose(fd);
    } else {
        msgNum = END_MESSAGES;
        msgText = (String *)realloc(msgText, msgNum*sizeof(String));
        for (i=0; i<msgNum; i++) msgText[i] = alert;
	fprintf(stderr, "%s\n\n", msgText[0]);
    }
}

String GetAppDefaultFile()
{
    char *lg;
    char name[256];
    int i;
    FILE *fd = NULL;

    if (!*lang && (lg = getenv("LANG"))) strncpy(lang, lg, 8);
    if (*lang) {
        for (i=0; i<=8 ; i++) lang[i] = tolower(lang[i]);
    }
    else
        strcpy(lang, "en");
    sprintf(name, "%s/XPaint_%s", XAPPLOADDIR, lang);
    fd = fopen(name, "r");
    if (*lang && !fd) {
        lang[2] = '\0';
        sprintf(name, "%s/XPaint_%s", XAPPLOADDIR, lang);
	fd = fopen(name, "r");
    }   
    if (fd) {
        fclose(fd);
	sprintf(name, "XPaint_%s", lang);
	return strdup(name);
    } else
        return strdup("XPaint");
}


static void
checkRCsize()
{
    FILE *fd;
    char buf[256];
    char *ptr;
    int w, h, i;
    if (!strcmp(appInfo.size, "640x480")) {
        if (appInfo.rcFile)
	    strcpy(buf, appInfo.rcFile);
	else
	    strcpy(buf, ".XPaintrc");
	if ((fd = fopen(buf, "r"))) {
	    i = 0;
            while (!feof(fd)) {
                fgets(buf, 250, fd);
		if (*buf && isspace(*buf)) ++i;
		if (!strncasecmp(buf, "defaultsize", 11)) {
		    ptr = buf + 12;
		    while (isspace(*ptr)) ++ptr;
	            if (sscanf(ptr, "%dx%d", &w, &h) == 2) {
	                 SetDefaultWHZ(w, h, appInfo.zoom);
		    }
		    break;
		}
		if (i >= 4) break;
	    }
	    fclose(fd);
	}
    }
}

int
main(int argc, char *argv[])
{
    Display *dpy;
    Widget toplevel;
    XtAppContext app;
    char xpTitle[20];
    int i;
    XEvent event;
    Arg args[5];
    int nargs = 0;
    XrmDatabase rdb;
    Boolean isIcon;

    Widget temp;
    int desiredVisual = -1;
    XVisualInfo vis;
    Boolean valid;
    int depth;
    int desiredDepth;
    int tempargc;
    char **tempargv;
    char phrase[128];
    String app_def_file;
    int desiredVisualID = -1;	 

    InitTypeConverters();
	 
    msgText = NULL;
    helpText = NULL;
    tempargc = argc;
    tempargv = xmalloc(argc * sizeof(char *));
    
    *lang = '\0';
    for (i = 0; i < argc; i++) {
        tempargv[i] = xmalloc(strlen(argv[i])+1);
	if (i < argc-1 && !strncasecmp(argv[i], "-language", strlen(argv[i])))
	   strncpy(lang, argv[i+1], 8);
	strcpy(tempargv[i], argv[i]);
    }

    app_def_file = GetAppDefaultFile();
    if (!app_def_file) {
        fprintf(stderr, "%s : %s\n",
            msgText[CANT_FIND_APP_DEFAULTS_FILE], app_def_file);
        exit(1);
    }

    /*
     * Create a temp widget that we can interrogate.
     * This widget will not be realized. 
     * There should be a better way to do this...
     */
    temp = XtAppInitialize(&Global.appContext, app_def_file,
			   options, XtNumber(options), &tempargc, tempargv,
			   appDefaults, args, nargs);
    XtGetApplicationResources(temp, (XtPointer) & appInfo,
			      resources, XtNumber(resources), NULL, 0);

   /*
    **  Load help and messages files
     */
    LoadMessages();

    rdb = XtDatabase(XtDisplay(temp));
    for (i = 0; i < tempargc; i++)
        free(tempargv[i]);
	 
    if (appInfo.help != NULL)
	usage(argv[0], NULL);

    desiredDepth = 0;
    if (strcmp(appInfo.visualType, "default") != 0) {
        char *cp = appInfo.visualType;
	char *vp = phrase;

	/* this includes the terminating null character */
	for (i = 0; i <= strlen(cp); i++)
	    phrase[i] = tolower(cp[i]);
		 
	if (strncmp(vp, "cmap", 4) == 0) {
	    XrmPutStringResource(&rdb, "Canvas*visual", "PseudoColor");
	    desiredVisual = PseudoColor;
	    vp += 4;
	} else if (strncmp(vp, "directcolor", 11) == 0) {
	    XrmPutStringResource(&rdb, "Canvas*visual", "DirectColor");
	    desiredVisual = DirectColor;
	    desiredDepth = 24;
	    vp += 11;
	} else if (strncmp(vp, "truecolor", 9) == 0) {
	    XrmPutStringResource(&rdb, "Canvas*visual", "TrueColor");
	    desiredVisual = TrueColor;
	    desiredDepth = 24;
	    vp += 9;
	} else if (strncmp(vp, "grayscale", 9) == 0) {
	    XrmPutStringResource(&rdb, "Canvas*visual", "GrayScale");
	    desiredVisual = GrayScale;
	    desiredDepth = 8;
	    vp += 9;
	} else if (strncmp(vp, "pseudocolor", 11) == 0) {
	    XrmPutStringResource(&rdb, "Canvas*visual", "PseudoColor");
	    desiredVisual = PseudoColor;
	    vp += 11;
	} else if (strncmp(vp, "staticgray", 10) == 0) {
	    XrmPutStringResource(&rdb, "Canvas*visual", "StaticGray");
	    desiredVisual = StaticGray;
	    desiredDepth = 8;
	    vp += 10;
	} else if (strncmp(vp, "staticcolor", 11) == 0) {
	    XrmPutStringResource(&rdb, "Canvas*visual", "StaticColor");
	    desiredVisual = StaticColor;
	    desiredDepth = 8;
	    vp += 11;
	} else if (strncmp(vp, "true", 4) == 0) {
	    XrmPutStringResource(&rdb, "Canvas*visual", "TrueColor");
	    desiredVisual = TrueColor;
	    vp += 4;
	} else if (strncmp(vp, "gray", 4) == 0) {
	    XrmPutStringResource(&rdb, "Canvas*visual", "StaticGray");
	    desiredVisual = StaticGray;
	    vp += 4;
	} else if (atoi(vp)>0 || (vp[0]=='0' && vp[1]=='\0')) {
 	    desiredVisualID = atoi(vp);
	    *vp = '\0';
	} else {
	    fprintf(stderr, msgText[BAD_VISUAL_TYPE_SPECIFICATION],
		    argv[0], vp);
	    exit(1);
	}
	if (*vp != '\0') {
	    desiredDepth=atoi(vp);
	    if (desiredDepth > 0) 
	        XrmPutStringResource(&rdb, "Canvas*depth", vp);
	    else
	        desiredDepth = 0;
	}
    }
    else {
        /* go find out what the default visual is and get its parameters */
        Global.vis.depth = DefaultDepth(XtDisplay(temp),
					DefaultScreen(XtDisplay(temp)));
	Global.vis.visual = DefaultVisual(XtDisplay(temp),
					  DefaultScreen(XtDisplay(temp)));
    }

    if (desiredVisual >= 0 || desiredVisualID >= 0) {
        vis = GetFutureVisual(&temp, desiredDepth,
			      desiredVisual, desiredVisualID, &valid);
		 
	if (valid) {
	    depth = vis.depth;
	    Global.vis = vis;
	}
	else {
	    fprintf(stderr, msgText[NO_MATCH_FOUND_FOR_VISUAL]);
	    exit(1);
	}
    }
    else {
        Global.vis.depth = DefaultDepth(XtDisplay(temp),
					DefaultScreen(XtDisplay(temp)));
	Global.vis.visual = DefaultVisual(XtDisplay(temp),
					  DefaultScreen(XtDisplay(temp)));
    }
	 
    sprintf(phrase, "%d", Global.vis.depth);
    XrmPutStringResource(&rdb, "Canvas*depth", phrase);

    /* destroy our temporary widget */
    XtDestroyWidget(temp);

    /*
    **  Create the application context
     */

    toplevel = XtAppInitialize(&Global.appContext, app_def_file,
			       options, XtNumber(options), &argc, argv,
			       appDefaults, args, nargs);

    XtGetApplicationResources(toplevel, (XtPointer) & appInfo,
			      resources, XtNumber(resources), NULL, 0);

    if (argc != 1 && argv[1][0] == '-')
	usage(argv[0], "Invalid option");

    checkRCsize();

    /*
    **  A little initialization
     */
    Global.toplevel = toplevel;
    Global.canvas = None;
    Global.back = None;
    Global.patternshell = None;
    Global.patterninfo = NULL;
    Global.region.image = NULL;
    Global.region.cmap = None;
    Global.region.width = 0;
    Global.region.height = 0;
    Global.region.pix = None;
    Global.region.mask = None;
    Global.nbrushes = 0;
    Global.brushes = NULL;
    Global.join = JoinMiter;
    Global.dashlist[0] = '\0';
    Global.dashnumber = 0;
    Global.dashoffset = 0;
    Global.numregions = 0;
    Global.transparent = 0;
    Global.regiondata = NULL;

    /*
    **
     */
    XtVaGetValues(toplevel, XtNiconic, &isIcon, NULL);
    if (isIcon)
	XrmPutStringResource(&rdb, "Canvas.iconic", "on");

	 
    /*
    **  GRR 960525:  check depth and warn user (use AlertBox() instead?)
     */
    if (!appInfo.nowarn && !appInfo.snapshot) {
	int depth = Global.vis.depth;

	/* XtVaGetValues(toplevel, XtNdepth, &depth, NULL); */
	HelpTextOutput(stderr, "data_loss");
	fprintf(stderr, msgText[YOUR_CANVAS_DEPTH_IS_THAT_MANY_BITS], depth);
	switch (depth) {
	    case 8:
	    case 12:
		fprintf(stderr, msgText[SEPARATOR_STRIP_ONE]);
		fprintf(stderr, msgText[WARNING_ONE]);
		fprintf(stderr, msgText[SEPARATOR_STRIP_ONE]);
		fprintf(stderr, "\n");
		break;
	    case 1:
	    case 2:
	    case 4:
	    case 6:
	    case 15:
	    case 16:
		fprintf(stderr, msgText[SEPARATOR_STRIP_TWO]);
		fprintf(stderr, msgText[WARNING_TWO_A]);
		fprintf(stderr, msgText[WARNING_TWO_B]);
		fprintf(stderr, msgText[SEPARATOR_STRIP_TWO]);
		fprintf(stderr, "\n");
		break;
	    case 24:
	    case 32:
		fprintf(stderr, msgText[WARNING_THREE_A]);
		fprintf(stderr, msgText[WARNING_THREE_B]);
		fprintf(stderr, msgText[WARNING_THREE_C]);
		break;
	    default:
		break;
	}
    }

    /*
    **  Now build and construct the widgets
     */
    Global.timeToDie = False;
    Global.explore = False;
    app = XtWidgetToApplicationContext(toplevel);
    dpy = Global.display = XtDisplay(toplevel);

    /*
    **  Call the initializers
     */

    OperationInit(toplevel);

    /*
    **  A few rogue initializers
     */
    BrushInit(toplevel);
    HelpInit(toplevel);

    SRANDOM(time(0));

    SetIconImage(toplevel);

    /*
    **  GRR 960526:  put version number in title string (and icon name?)
     */
    sprintf(xpTitle, "XPaint %s", XPAINT_VERSION);
    XtVaSetValues(toplevel, XtNtitle, xpTitle, NULL);
 /* XtVaSetValues(toplevel, XtNiconName, xpTitle, XtNtitle, xpTitle, NULL); */

    if (appInfo.snapshot) {
        SnapshotImage(toplevel, NULL, 0);
    }
    /*
    **  Realize it (doesn't hurt)
     */
    XtRealizeWidget(toplevel);

    /*
    **  Now open any file on the command line
     */
    if (argc != 1) {
        if (appInfo.clipboard) {
	   Image * image;
	   for (i = 1; i < argc; i++) {
  	       image = ImageFromFile(argv[i]);
               if (image)
                  ImageToMemory(image);
	   }
	} else {
  	   LocalInfo *l = XtNew(LocalInfo);
	   l->pos = 0;
	   l->toplevel = toplevel;
  	   l->nfiles = argc - 1;
	   l->files = (char **) XtCalloc(argc, sizeof(char *));
           for (i = 1; i < argc; i++)
	       l->files[i - 1] = argv[i];
	   l->id = XtAppAddWorkProc(app, (XtWorkProc) workProc, (XtPointer) l);
	}
    } else if (appInfo.popped) {
	/*
	**  If nothing is coming up, bring up a blank canvas
	 */
	GraphicCreate(toplevel, 0);
    }

    XtUnmanageChild(Global.bar);
    XMapWindow(dpy, XtWindow(Global.bar));
    XtUnmanageChild(Global.back);
    XMapWindow(dpy, XtWindow(Global.back));

    /*
    **  MainLoop
     */
    do {
	XtAppNextEvent(app, &event);
	if (event.type == ButtonPress || event.type == ButtonRelease)
	    Global.currentTime = event.xbutton.time;
	XtDispatchEvent(&event);
    }
    while (!Global.timeToDie);
    return 0;
}