-
Notifications
You must be signed in to change notification settings - Fork 24
/
xcircuit.c
1710 lines (1448 loc) · 51.3 KB
/
xcircuit.c
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
/*----------------------------------------------------------------------*/
/* xcircuit.c --- An X-windows program for drawing circuit diagrams */
/* Copyright (c) 2002 R. Timothy Edwards */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to: */
/* Free Software Foundation, Inc. */
/* 59 Temple Place, Suite 330 */
/* Boston, MA 02111-1307 USA */
/* */
/* See file ./README for contact information */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* Uses the Xw widget set (see directory Xw) */
/* Xcircuit written by Tim Edwards beginning 8/13/93 */
/*----------------------------------------------------------------------*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <limits.h>
#include <locale.h>
#include <ctype.h>
#ifndef XC_WIN32
#include <unistd.h> /* for unlink() */
#include <X11/Xresource.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#endif
#ifdef TCL_WRAPPER
#include <tk.h>
#else
#ifndef XC_WIN32
#include "Xw/Xw.h"
#include "Xw/Form.h"
#include "Xw/WorkSpace.h"
#include "Xw/PButton.h"
#include "Xw/SText.h"
#include "Xw/Cascade.h"
#include "Xw/PopupMgr.h"
#include "Xw/MenuBtn.h"
#include "Xw/BBoard.h"
#include "Xw/TextEdit.h"
#include "Xw/Toggle.h"
#endif
#endif
#if defined(XC_WIN32) && defined(TCL_WRAPPER)
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/Xatom.h>
#endif
#ifndef P_tmpdir
#define P_tmpdir TMPDIR
#endif
/*----------------------------------------------------------------------*/
/* Local includes */
/*----------------------------------------------------------------------*/
#include "xcircuit.h"
#include "cursors.h"
#include "colordefs.h"
#include "menudep.h"
/*----------------------------------------------------------------------*/
/* Function prototype declarations */
/*----------------------------------------------------------------------*/
#include "prototypes.h"
/*----------------------------------------------------------------------*/
/* Global Variable definitions */
/*----------------------------------------------------------------------*/
char _STR2[250]; /* Specifically for text returned from the popup prompt */
char _STR[150]; /* Generic multipurpose string */
int pressmode; /* Whether we are in a press & hold state */
Display *dpy = NULL; /* Works well to make this globally accessible */
Colormap cmap;
Pixmap STIPPLE[STIPPLES]; /* Polygon fill-style stipple patterns */
Cursor appcursors[NUM_CURSORS];
ApplicationData appdata;
XCWindowData *areawin;
Globaldata xobjs;
int number_colors;
colorindex *colorlist;
short menusize;
xcIntervalId printtime_id;
short beeper;
short fontcount;
int screenDPI;
fontinfo *fonts;
short popups; /* total number of popup widgets on the screen */
/*----------------------------------------------------------------------*/
/* Externally defined variables */
/*----------------------------------------------------------------------*/
extern aliasptr aliastop;
extern char version[];
#ifdef TCL_WRAPPER
extern Tcl_Interp *xcinterp;
#else
extern XtAppContext app;
#endif
#if !defined(HAVE_CAIRO)
extern Pixmap dbuf;
#endif
/* Bad hack for problems with the DECstation. . . don't know why */
#ifdef UniqueContextProblem
#undef XUniqueContext
XContext XUniqueContext()
{
return XrmUniqueQuark();
}
#endif
/* End of bad hack. . . */
/*----------------------------------------------------------------------*/
/* Update the list of colors in the colormap */
/*----------------------------------------------------------------------*/
void addtocolorlist(xcWidget button, int cvalue)
{
number_colors++;
colorlist = (colorindex *)realloc(colorlist, number_colors *
sizeof(colorindex));
colorlist[number_colors - 1].cbutton = button;
colorlist[number_colors - 1].color.pixel = cvalue;
/* Get and store the RGB values of the new color */
if (areawin && (areawin->area == NULL)) {
colorlist[number_colors - 1].color.red = 256 * (cvalue & 0xff);
colorlist[number_colors - 1].color.green = 256 * ((cvalue >> 8) & 0xff);
colorlist[number_colors - 1].color.blue = 256 * ((cvalue >> 16) & 0xff);
}
else
XQueryColors(dpy, cmap, &(colorlist[number_colors - 1].color), 1);
}
/*----------------------------------------------------------------------*/
/* Add a new color button to the color menu */
/* called if new color button needs to be made */
/*----------------------------------------------------------------------*/
#ifdef TCL_WRAPPER
int addnewcolorentry(int ccolor)
{
xcWidget newbutton = (xcWidget)NULL;
int i;
/* check to see if entry is already in the color list */
for (i = NUMBER_OF_COLORS; i < number_colors; i++)
if (colorlist[i].color.pixel == ccolor) break;
/* make new entry in the menu */
if (i == number_colors) {
addtocolorlist(newbutton, ccolor);
sprintf(_STR2,
"xcircuit::newcolorbutton %d %d %d %d",
colorlist[i].color.red, colorlist[i].color.green,
colorlist[i].color.blue, i);
Tcl_Eval(xcinterp, _STR2);
}
return i;
}
#endif /* TCL_WRAPPER */
/*---------------*/
/* Quit xcircuit */
/*---------------*/
void quit(xcWidget w, caddr_t nulldata)
{
int i;
Matrixptr curmatrix, dmatrix;
/* free up CTM Matrix Stack */
if (areawin != NULL) {
curmatrix = areawin->MatStack;
while (curmatrix != NULL) {
dmatrix = curmatrix->nextmatrix;
free(curmatrix);
curmatrix = dmatrix;
}
areawin->MatStack = NULL;
}
/* free the colormap if a new one has been installed */
if (dpy != NULL)
if (cmap != DefaultColormap(dpy, DefaultScreen(dpy)))
XFreeColormap(dpy, cmap);
/* exit ghostscript if background rendering was enabled */
exit_gs();
#ifdef TCL_WRAPPER
/* exit ngspice if the simulator is still running */
exit_spice();
#endif
/* remove any temporary files created for background rendering */
for (i = 0; i < xobjs.pages; i++) {
if (xobjs.pagelist[i]->pageinst != NULL)
if (xobjs.pagelist[i]->background.name != (char *)NULL)
if (*(xobjs.pagelist[i]->background.name) == '@')
unlink(xobjs.pagelist[i]->background.name + 1);
}
/* remove the temporary file and free the filename memory */
/* if w = NULL, quit() was reached from Ctrl-C. Don't */
/* remove the temporary file; instead, notify user of the */
/* filename. */
if (xobjs.tempfile != NULL) {
if (w != NULL) {
#ifndef XC_WIN32
if (unlink(xobjs.tempfile) < 0)
Fprintf(stderr, "Error %d unlinking file \"%s\"\n", errno, xobjs.tempfile);
#else
if (DeleteFile(xobjs.tempfile) != TRUE)
Fprintf(stderr, "Error %d unlinking file \"%s\"\n",
GetLastError(), xobjs.tempfile);
#endif
}
else
Fprintf(stderr, "Ctrl-C exit: reload workspace from \"%s\"\n",
xobjs.tempfile);
free(xobjs.tempfile);
xobjs.tempfile = NULL;
}
#if defined(HAVE_PYTHON)
/* exit by exiting the Python interpreter, if enabled */
exit_interpreter();
#elif defined(TCL_WRAPPER)
/* Let Tcl/Tk handle exit */
return;
#elif defined(XC_WIN32)
PostQuitMessage(0);
#else
exit(0);
#endif
}
/*--------------------------------------------------------------*/
/* Check for changes in an object and all of its descendents */
/*--------------------------------------------------------------*/
u_short getchanges(objectptr thisobj)
{
genericptr *pgen;
objinstptr pinst;
u_short changes = thisobj->changes;
for (pgen = thisobj->plist; pgen < thisobj->plist + thisobj->parts; pgen++) {
if (IS_OBJINST(*pgen)) {
pinst = TOOBJINST(pgen);
changes += getchanges(pinst->thisobject);
}
}
return changes;
}
/*--------------------------------------------------------------*/
/* Check to see if any objects in xcircuit have been modified */
/* without saving. */
/*--------------------------------------------------------------*/
u_short countchanges(char **promptstr)
{
int slen = 1, i; /* , j; (jdk) */
u_short locchanges, changes = 0, words = 1;
objectptr thisobj;
char *fname;
TechPtr ns;
if (promptstr != NULL) slen += strlen(*promptstr);
for (i = 0; i < xobjs.pages; i++) {
if (xobjs.pagelist[i]->pageinst != NULL) {
thisobj = xobjs.pagelist[i]->pageinst->thisobject;
locchanges = getchanges(thisobj);
if (locchanges > 0) {
if (promptstr != NULL) {
slen += strlen(thisobj->name) + 2;
*promptstr = (char *)realloc(*promptstr, slen);
if ((words % 8) == 0) strcat(*promptstr, ",\n");
else if (changes > 0) strcat(*promptstr, ", ");
strcat(*promptstr, thisobj->name);
words++;
}
changes += locchanges;
}
}
}
/* Check all library objects for unsaved changes */
for (ns = xobjs.technologies; ns != NULL; ns = ns->next) {
tech_set_changes(ns);
if ((ns->flags & TECH_CHANGED) != 0) {
changes++;
if (promptstr != NULL) {
fname = ns->filename;
if (fname != NULL) {
slen += strlen(fname) + 2;
*promptstr = (char *)realloc(*promptstr, slen);
if ((words % 8) == 0) strcat(*promptstr, ",\n");
else if (changes > 0) strcat(*promptstr, ", ");
strcat(*promptstr, fname);
words++;
}
}
}
}
return changes;
}
/*----------------------------------------------*/
/* Check for conditions to approve program exit */
/* Return 0 if prompt "Okay" button ends the */
/* program, 1 if the program should exit */
/* immediately. */
/*----------------------------------------------*/
#ifdef TCL_WRAPPER
int quitcheck(xcWidget w, caddr_t clientdata, caddr_t calldata)
{
char *promptstr;
Boolean doprompt = False;
/* enable default interrupt signal handler during this time, so that */
/* a double Control-C will ALWAYS exit. */
signal(SIGINT, SIG_DFL);
promptstr = (char *)malloc(60);
strcpy(promptstr, ".query.title.field configure -text \"Unsaved changes in: ");
/* Check all page objects for unsaved changes */
doprompt = (countchanges(&promptstr) > 0) ? True : False;
/* If any changes have not been saved, generate a prompt */
if (doprompt) {
promptstr = (char *)realloc(promptstr, strlen(promptstr) + 15);
strcat(promptstr, "\nQuit anyway?");
strcat(promptstr, "\"");
Tcl_Eval(xcinterp, promptstr);
Tcl_Eval(xcinterp, ".query.bbar.okay configure -command {quitnocheck}");
Tcl_Eval(xcinterp, "wm deiconify .query");
Tcl_Eval(xcinterp, "raise .query");
free(promptstr);
return 0;
}
else {
free(promptstr);
quit(w, NULL); // preparation for quit
if (calldata != (caddr_t)NULL)
Tcl_Eval(xcinterp, "quitnocheck intr");
else
Tcl_Eval(xcinterp, "quitnocheck"); // actual quit
return 1; // not reached
}
}
#endif
/*--------------------------------------*/
/* A gentle Ctrl-C shutdown */
/*--------------------------------------*/
void dointr(int signum)
{
quitcheck(NULL, NULL, (caddr_t)1);
}
/*--------------*/
/* Null routine */
/*--------------*/
void DoNothing(xcWidget w, caddr_t clientdata, caddr_t calldata)
{
/* Nothing here! */
}
/*-----------------------------------------------------------------------*/
/* Write page scale (overall scale, and X and Y dimensions) into strings */
/*-----------------------------------------------------------------------*/
void writescalevalues(char *scdest, char *xdest, char *ydest)
{
float oscale, psscale;
int width, height;
Pagedata *curpage;
curpage = xobjs.pagelist[areawin->page];
oscale = curpage->outscale;
psscale = getpsscale(oscale, areawin->page);
width = toplevelwidth(curpage->pageinst, NULL);
height = toplevelheight(curpage->pageinst, NULL);
sprintf(scdest, "%6.5f", oscale);
if (curpage->coordstyle == CM) {
sprintf(xdest, "%6.5f", (width * psscale) / IN_CM_CONVERT);
sprintf(ydest, "%6.5f", (height * psscale) / IN_CM_CONVERT);
}
else {
sprintf(xdest, "%6.5f", (width * psscale) / 72.0);
sprintf(ydest, "%6.5f", (height * psscale) / 72.0);
}
}
/*-------------------------------------------------------------------------*/
/* Destroy an interactive text-editing popup box */
/*-------------------------------------------------------------------------*/
#ifdef TCL_WRAPPER
/* Just pop it down. . . */
void destroypopup(xcWidget button, popupstruct *callstruct, caddr_t calldata)
{
Tk_UnmapWindow(callstruct->popup);
popups--;
/* free the allocated structure space */
free(callstruct->buttonptr);
if (callstruct->filter != NULL) free(callstruct->filter);
free(callstruct);
/* in the case of "quitcheck", we want to make sure that the signal */
/* handler is reset to default behavior if the quit command is */
/* canceled from inside the popup prompt window. */
signal(SIGINT, dointr);
}
/*-------------------------------------------------------------------------*/
/* Create a popup window with "OK" and "Cancel" buttons, */
/* and text and label fields. */
/*-------------------------------------------------------------------------*/
void popupprompt(xcWidget button, char *request, char *current, void (*function)(),
buttonsave *datastruct, const char *filter)
{
Tk_Window popup;
popup = Tk_NameToWindow(xcinterp, ".dialog", Tk_MainWindow(xcinterp));
Tk_MapWindow(popup);
}
/*----------------------------------------------------------------------*/
/* Create a popup window for property changes */
/*----------------------------------------------------------------------*/
void outputpopup(xcWidget button, caddr_t clientdata, caddr_t calldata)
{
Tcl_Eval(xcinterp, "wm deiconify .output");
}
/*----------------------------------------------------------------------*/
/* Get the display resolution. This is needed especially for retinal */
/* displays with 4x the DPI of typical displays. Keeping the DPI value */
/* around is useful for determing a sane size for fonts, buttons, */
/* scrollbar widths, etc. */
/* */
/* Note that this routine does not go so far as to handle non-square */
/* pixels. */
/* */
/* The first check is to look for Xft.dpi in the X resource database. */
/* If it is not found, then fall back on the DisplayWidth as reported */
/* by Xlib (to do: Override both of these with an environment variable */
/* such as XCIRCUIT_DPI). */
/*----------------------------------------------------------------------*/
int getscreenDPI()
{
int x, xmm, dpi;
XrmDatabase db;
XrmValue val;
char *resource_manager;
char *type, *var;
XrmInitialize();
resource_manager = XResourceManagerString(dpy);
if (resource_manager != NULL) {
db = XrmGetStringDatabase(resource_manager);
if (db != NULL) {
XrmGetResource(db, "Xft.dpi", "String", &type, &val);
if (val.addr != NULL && !strncmp("String", type, 64))
if (sscanf(val.addr, "%d", &dpi) == 1)
return dpi;
}
}
x = DisplayWidth(dpy, DefaultScreen(dpy));
xmm = DisplayWidthMM(dpy, DefaultScreen(dpy));
dpi = (int)(((float)x + 0.5) / (float)(xmm / 25.4));
return dpi;
}
/*-------------------------------------------------*/
/* Print a string to the message widget. */
/* Note: Widget message must be a global variable. */
/* For formatted strings, format first into _STR */
/*-------------------------------------------------*/
/* XXX it looks like this routine is an orphan */
void clrmessage(caddr_t clientdata)
{
char buf1[50], buf2[50];
/* Don't write over the report of the edit string contents, */
/* if we're in one of the label edit modes */
if (eventmode == TEXT_MODE || eventmode == ETEXT_MODE)
charreport(TOLABEL(EDITPART));
else {
measurestr(xobjs.pagelist[areawin->page]->gridspace, buf1);
measurestr(xobjs.pagelist[areawin->page]->snapspace, buf2);
Wprintf("Grid %.50s : Snap %.50s", buf1, buf2);
}
}
#endif /* TCL_WRAPPER */
/*------------------------------------------------------------------------------*/
/* diagnostic tool for translating the event mode into a string and printing */
/* to stderr (for debugging only). */
/*------------------------------------------------------------------------------*/
void printeventmode() {
Fprintf(stderr, "eventmode is \'");
switch(eventmode) {
case NORMAL_MODE:
Fprintf(stderr, "NORMAL");
break;
case MOVE_MODE:
Fprintf(stderr, "MOVE");
break;
case COPY_MODE:
Fprintf(stderr, "COPY");
break;
case SELAREA_MODE:
Fprintf(stderr, "SELAREA");
break;
case CATALOG_MODE:
Fprintf(stderr, "CATALOG");
break;
case CATTEXT_MODE:
Fprintf(stderr, "CATTEXT");
break;
case CATMOVE_MODE:
Fprintf(stderr, "CATMOVE");
break;
case FONTCAT_MODE:
Fprintf(stderr, "FONTCAT");
break;
case EFONTCAT_MODE:
Fprintf(stderr, "EFONTCAT");
break;
case TEXT_MODE:
Fprintf(stderr, "TEXT");
break;
case ETEXT_MODE:
Fprintf(stderr, "ETEXT");
break;
case WIRE_MODE:
Fprintf(stderr, "WIRE");
break;
case BOX_MODE:
Fprintf(stderr, "BOX");
break;
case EPOLY_MODE:
Fprintf(stderr, "EPOLY");
break;
case ARC_MODE:
Fprintf(stderr, "ARC");
break;
case EARC_MODE:
Fprintf(stderr, "EARC");
break;
case SPLINE_MODE:
Fprintf(stderr, "SPLINE");
break;
case ESPLINE_MODE:
Fprintf(stderr, "ESPLINE");
break;
case EPATH_MODE:
Fprintf(stderr, "EPATH");
break;
case EINST_MODE:
Fprintf(stderr, "EINST");
break;
case ASSOC_MODE:
Fprintf(stderr, "ASSOC");
break;
case RESCALE_MODE:
Fprintf(stderr, "RESCALE");
break;
case PAN_MODE:
Fprintf(stderr, "PAN");
break;
default:
Fprintf(stderr, "(unknown)");
break;
}
Fprintf(stderr, "_MODE\'\n");
}
/*------------------------------------------------------------------------------*/
#ifdef TCL_WRAPPER
/*------------------------------------------------------------------------------*/
/* "docommand" de-iconifies the TCL console. */
/*------------------------------------------------------------------------------*/
void docommand()
{
Tcl_Eval(xcinterp, "catch xcircuit::raiseconsole");
}
/*------------------------------------------------------------------------------*/
/* When all else fails, install your own colormap */
/*------------------------------------------------------------------------------*/
int installowncmap()
{
Colormap newcmap;
Fprintf(stdout, "Installing my own colormap\n");
/* allocate a new colormap */
newcmap = XCopyColormapAndFree(dpy, cmap);
if (newcmap == (Colormap)NULL) return (-1);
cmap = newcmap;
return(1);
}
#endif /* TCL_WRAPPER */
/*------------------------------------------------------------------------------*/
/* Find the nearest color in the colormap to cvexact and return its pixel value */
/*------------------------------------------------------------------------------*/
#if defined(TCL_WRAPPER) || !defined(XC_WIN32)
int findnearcolor(XColor *cvexact)
{
/* find the nearest-matching color in the colormap */
int i, ncolors = DisplayCells(dpy, DefaultScreen(dpy));
XColor *cmcolors;
long rdist, bdist, gdist;
u_long dist, mindist;
int minidx;
cmcolors = (XColor *)malloc(ncolors * sizeof(XColor));
for (i = 0; i < ncolors; i++) {
cmcolors[i].pixel = i;
cmcolors[i].flags = DoRed | DoGreen | DoBlue;
}
XQueryColors(dpy, cmap, cmcolors, ncolors);
mindist = ULONG_MAX;
for (i = 0; i < ncolors; i++) {
rdist = (cmcolors[i].red - cvexact->red);
bdist = (cmcolors[i].blue - cvexact->blue);
gdist = (cmcolors[i].green - cvexact->green);
dist = rdist * rdist + bdist * bdist + gdist * gdist;
if (dist < mindist) {
mindist = dist;
minidx = i;
}
}
free(cmcolors);
/* if we can't get the right color or something reasonably close, */
/* then allocate our own colormap. If we've allocated so many */
/* colors that the new colormap is full, then we give up and use */
/* whatever color was closest, no matter how far away it was. */
/* findnearcolor already used 512 per component, so to match that, */
/* 3 * (512 * 512) = 786432 ~= 750000 */
if (dist > 750000) {
if (installowncmap() > 0) {
if (XAllocColor(dpy, cmap, cvexact) != 0)
minidx = cvexact->pixel;
}
}
return minidx;
}
#endif
/*----------------------------------------------------------------------*/
/* Return the pixel value of a database color in the colorlist vector */
/* Since this is generally called by defined name, e.g., */
/* xc_getlayoutcolor(RATSNESTCOLOR), it does not check for invalid */
/* values of cidx. */
/*----------------------------------------------------------------------*/
int xc_getlayoutcolor(int cidx)
{
return colorlist[cidx].color.pixel;
}
/*----------------------------------------------------------------------*/
/* Obtain red, green and blue values from a color index */
/* the colour values range from 0 to 65535 */
/*----------------------------------------------------------------------*/
void xc_get_color_rgb(unsigned long cidx, unsigned short *red,
unsigned short *green, unsigned short *blue)
{
XColor loccolor = {
.pixel = cidx,
.flags = DoRed | DoGreen | DoBlue
};
if (areawin->area == NULL) {
loccolor.red = cidx & 0xff;
loccolor.green = (cidx >> 8) & 0xff;
loccolor.blue = (cidx >> 16) & 0xff;
}
else
XQueryColors(dpy, cmap, &loccolor, 1);
*red = loccolor.red;
*green = loccolor.green;
*blue = loccolor.blue;
}
/*-------------------------------------------------------------------------*/
/* Hack on the resource database Color allocation */
/* */
/* This overrides the built-in routine. The difference is that if a */
/* color cell cannot be allocated (colormap is full), then the color */
/* map is searched for the closest RGB value to the named color. */
/* */
/* This code depends on Display *dpy being set: Do not install the */
/* converter until after XtInitialize(), when using the Xt (not Tcl) GUI. */
/*-------------------------------------------------------------------------*/
#if defined(TCL_WRAPPER) || !defined(XC_WIN32)
caddr_t CvtStringToPixel(XrmValuePtr args, int *nargs, XrmValuePtr fromVal,
XrmValuePtr toVal)
{
static XColor cvcolor;
XColor cvexact;
if (dpy == NULL) return NULL;
if (*nargs != 0)
Fprintf(stderr, "String to Pixel conversion takes no arguments");
/* Color defaults to black if name is not found */
if (XAllocNamedColor(dpy, cmap, (char *)fromVal->addr, &cvcolor, &cvexact)
== 0) {
if (XLookupColor(dpy, cmap, (char *)fromVal->addr, &cvexact, &cvcolor) == 0)
cvcolor.pixel = BlackPixel(dpy, DefaultScreen(dpy));
else
cvcolor.pixel = findnearcolor(&cvexact);
}
toVal->size = sizeof(u_long);
toVal->addr = (caddr_t) &(cvcolor.pixel);
return NULL;
}
#endif
/*-------------------------------------------------------------------------*/
/* Allocate new color using the CvtStringToPixel routine */
/*-------------------------------------------------------------------------*/
#if defined(XC_WIN32) && !defined(TCL_WRAPPER)
int xc_alloccolor(char *name)
{
return WinNamedColor(name);
}
#else
int xc_alloccolor(char *name)
{
XrmValue fromC, toC;
int zval = 0, pixval;
fromC.size = strlen(name);
fromC.addr = name;
if (areawin && (areawin->area)) {
CvtStringToPixel(NULL, &zval, &fromC, &toC);
pixval = (int)(*((u_long *)toC.addr));
}
else {
int r, g, b;
/* Graphics-free batch mode. Handle basic colors plus RGB */
if (name[0] == '#' && (strlen(name) == 7)) {
sscanf(&name[1], "%2x", &r);
sscanf(&name[3], "%2x", &g);
sscanf(&name[5], "%2x", &b);
pixval = r + g * 256 + b * 65536;
}
else if (name[0] == '#' && (strlen(name) == 13)) {
sscanf(&name[1], "%4x", &r);
sscanf(&name[5], "%4x", &g);
sscanf(&name[9], "%4x", &b);
pixval = (r >> 8) + (g >> 8) * 256 + (b >> 8) * 65536;
}
else if (sscanf(name, "%d", &pixval) != 1) {
if (!strcasecmp(name, "red")) {
r = 255;
g = 0;
b = 0;
}
else if (!strcasecmp(name, "green")) {
r = 0;
g = 255;
b = 0;
}
else if (!strcasecmp(name, "blue")) {
r = 0;
g = 0;
b = 255;
}
else if (!strcasecmp(name, "white")) {
r = 255;
g = 255;
b = 255;
}
else if (!strcasecmp(name, "gray")) {
r = 128;
g = 128;
b = 128;
}
else if (!strcasecmp(name, "yellow")) {
r = 255;
g = 255;
b = 0;
}
else if (!strcasecmp(name, "gray40")) {
r = 102;
g = 102;
b = 102;
}
else if (!strcasecmp(name, "gray60")) {
r = 153;
g = 153;
b = 153;
}
else if (!strcasecmp(name, "gray80")) {
r = 204;
g = 204;
b = 204;
}
else if (!strcasecmp(name, "gray90")) {
r = 229;
g = 229;
b = 229;
}
else if (!strcasecmp(name, "green2")) {
r = 0;
g = 238;
b = 0;
}
else if (!strcasecmp(name, "purple")) {
r = 160;
g = 32;
b = 240;
}
else if (!strcasecmp(name, "steelblue2")) {
r = 92;
g = 172;
b = 238;
}
else if (!strcasecmp(name, "red3")) {
r = 205;
g = 0;
b = 0;
}
else if (!strcasecmp(name, "tan")) {
r = 210;
g = 180;
b = 140;
}
else if (!strcasecmp(name, "brown")) {
r = 165;
g = 42;
b = 42;
}
else if (!strcasecmp(name, "pink")) {
r = 255;
g = 192;
b = 203;
}
else {
// Default to black
r = 0;
g = 0;
b = 0;
}
pixval = r + g * 256 + b * 65536;
}
}
return pixval;
}
#endif
/*----------------------------------------------------------------------*/
/* Check if color within RGB roundoff error exists in xcircuit's color */
/* table. Assume 24-bit color, in which resolution can be no less than */
/* 256 for each color component. Visual acuity is a bit less than 24- */
/* bit color, so assume difference should be no less than 512 per */
/* component for colors to be considered "different". Psychologically, */
/* we should really find the just-noticable-difference for each color */
/* component separately! But that's too complicated for this simple */
/* routine. */
/* */
/* Return the table entry of the color, if it is in xcircuit's color */
/* table, or ERRORCOLOR if not. If it is in the color table, then */
/* return the actual pixel value from the table in the "pixval" pointer */
/*----------------------------------------------------------------------*/
int rgb_querycolor(int red, int green, int blue, int *pixval)
{
int i;
for (i = NUMBER_OF_COLORS; i < number_colors; i++) {
if (abs(colorlist[i].color.red - red) < 512 &&
abs(colorlist[i].color.green - green) < 512 &&
abs(colorlist[i].color.blue - blue) < 512) {
if (pixval)
*pixval = colorlist[i].color.pixel;
return i;
break;
}
}
return ERRORCOLOR;
}
/*-----------------------------------------------------------------------*/