1 /* grTkCommon.c
2  *
3  *	Functions common to all graphics routines running under Tcl/Tk
4  *
5  * Copyright 2003 Open Circuit Design, Inc., for MultiGiG Ltd.
6  */
7 
8 /* We should be here if this is not set! */
9 #ifdef MAGIC_WRAPPER
10 
11 #include <stdio.h>
12 #include <string.h>
13 #include <X11/Xlib.h>
14 #include <X11/Xutil.h>
15 #include <X11/keysym.h>
16 
17 #include "tcltk/tclmagic.h"
18 #include "utils/magic.h"
19 #include "utils/styles.h"
20 #include "utils/geometry.h"
21 #include "utils/hash.h"
22 #include "utils/malloc.h"
23 #include "utils/utils.h"
24 #include "tiles/tile.h"
25 #include "database/database.h"
26 #include "windows/windows.h"
27 #include "dbwind/dbwtech.h"
28 #include "utils/styles.h"
29 #include "graphics/graphics.h"
30 #include "graphics/graphicsInt.h"
31 #include "dbwind/dbwind.h"
32 #include "graphics/grTkCommon.h"
33 #include "graphics/glyphs.h"
34 
35 /* Variables declared by grClip.c */
36 
37 extern int grCurFill, grCurOutline, grCurColor;
38 extern void grInformDriver();
39 
40 /* Global variables used by both the Tk and TOGL interfaces	*/
41 /* should be defined here.					*/
42 
43 Display *grXdpy;
44 int grXscrn;
45 Tk_Cursor grCursors[MAX_CURSORS];
46 Tk_Font grTkFonts[4];
47 
48 /* Used by Tk interface.  Defined for both Tk and TOGL, but 	*/
49 /* unused by the TOGL interface.				*/
50 
51 bool GrTkInstalledCMap = FALSE;
52 
53 /*---------------------------------------------------------
54  * grTkLoadFont
55  *	This local routine loads the Tk fonts used by Magic.
56  *
57  * Results:
58  *	Success/Failure.
59  *
60  * Side Effects:
61  *	Font information saved in grTkFonts array.
62  *---------------------------------------------------------
63  */
64 
65 bool
grTkLoadFont()66 grTkLoadFont()
67 {
68     Tk_Window tkwind;
69     int i;
70     char *s;
71     char *unable = "Unable to load font";
72 
73     static char *fontnames[4] = {
74       TK_FONT_SMALL,
75       TK_FONT_MEDIUM,
76       TK_FONT_LARGE,
77       TK_FONT_XLARGE };
78     static char *optionnames[4] = {
79       "small",
80       "medium",
81       "large",
82       "xlarge"};
83 
84     tkwind = Tk_MainWindow(magicinterp);
85     for (i = 0; i < 4; i++)
86     {
87     	s = XGetDefault(grXdpy, "magic", optionnames[i]);
88 	if (s) fontnames[i] = s;
89         if ((grTkFonts[i] = Tk_GetFont(magicinterp,
90 			tkwind, fontnames[i])) == NULL)
91         {
92 	    TxError("%s %s\n", unable, fontnames[i]);
93             if ((grTkFonts[i] = Tk_GetFont(magicinterp,
94 			tkwind, TK_DEFAULT_FONT)) == NULL)
95 	    {
96 	        TxError("%s %s\n", unable, TK_DEFAULT_FONT);
97 		return FALSE;
98 	    }
99         }
100     }
101     return TRUE;
102 }
103 
104 /*---------------------------------------------------------
105  * grTkFreeFonts
106  *      This local routine frees the Tk font cache.
107  *
108  * Results:
109  *	None.
110  *
111  * Side Effects:
112  *	Memory deallocation.
113  *---------------------------------------------------------
114  */
115 
116 void
grTkFreeFonts()117 grTkFreeFonts()
118 {
119     int i;
120 
121     for (i = 0; i < 4; i++)
122         Tk_FreeFont(grTkFonts[i]);
123 }
124 
125 /*
126  * ----------------------------------------------------------------------------
127  * grTkFreeCursors:
128  *
129  *	Remove cursors from the Tk cursor cache.
130  *
131  * Results:
132  *	None.
133  *
134  * Side effects:
135  *	Tk forgets about the glyph cursors.
136  * ----------------------------------------------------------------------------
137  */
138 
139 void
grTkFreeCursors(glyphs)140 grTkFreeCursors(glyphs)
141     GrGlyphs *glyphs;
142 {
143     int i;
144     for (i = 0; i < glyphs->gr_num; i++)
145 	Tk_FreeCursor(grXdpy, grCursors[i]);
146 }
147 
148 /*
149  * ----------------------------------------------------------------------------
150  * grTkDefineCursor:
151  *
152  *	Define a new set of cursors.  Use Tk-style cursors (portable)
153  *
154  * Results:
155  *	None.
156  *
157  * Side effects:
158  *	The given matrix is stored in the graphics display, and it can be
159  *	used as the cursor by calling GrSetCursor.
160  * ----------------------------------------------------------------------------
161  */
162 
163 typedef struct {
164     unsigned char source[32];
165     unsigned char mask[32];
166 } CursorCache;
167 
168 void
grTkDefineCursor(glyphs)169 grTkDefineCursor(glyphs)
170     GrGlyphs *glyphs;
171 {
172     char *fgname, *bgname;
173     int glyphnum;
174     Rect oldClip;
175     int red, green, blue;
176     Tk_Window tkwin;
177     bool fg_needs_free;
178 
179     if (glyphs->gr_num <= 0) return;
180 
181     if (glyphs->gr_num > MAX_CURSORS)
182     {
183 	TxError("magic/Tk only has room for %d cursors\n", MAX_CURSORS);
184 	return;
185     }
186 
187     tkwin = Tk_MainWindow(magicinterp);
188 
189     /* expand clipping amount for off-screen access on the X */
190     GrLock(GR_LOCK_SCREEN, FALSE);
191     oldClip = grCurClip;
192     grCurClip = GrScreenRect;
193     grCurClip.r_ytop += 16;
194 
195     /* enter the glyphs */
196     for (glyphnum = 0; glyphnum < glyphs->gr_num; glyphnum++) {
197 	int i, *p, fgstyle;
198 	XColor curcolor;
199 	GrGlyph *g;
200 	int x, y;
201 	CursorCache *glyphcache;
202 
203 	g = glyphs->gr_glyph[glyphnum];
204 	if ((g->gr_xsize != 16) || (g->gr_ysize != 16)) {
205 	    TxError("Tk/OpenGL Cursors must be 16 X 16 pixels.\n");
206 	    return;
207 	}
208 
209         glyphcache = (CursorCache *)mallocMagic(sizeof(CursorCache));
210 	g->gr_cache = (ClientData)glyphcache;
211 	g->gr_free = freeMagic;
212 
213 	/* Find the foreground and background colors of the glyph */
214 
215 	p = &(g->gr_pixels[0]);
216 	fgstyle = STYLE_TRANSPARENT;
217 	fg_needs_free = FALSE;
218 	for (x = 0; x < 256; x++)
219 	{
220 	    if (*p != STYLE_TRANSPARENT)
221 	    {
222 		fgstyle = *p;
223 		GrGetColor(GrStyleTable[*p].color, &red, &green, &blue);
224 		curcolor.red = (unsigned short)(red << 8);
225 		curcolor.green = (unsigned short)(green << 8);
226 		curcolor.blue = (unsigned short)(blue << 8);
227 		curcolor.flags = DoRed | DoGreen | DoBlue;
228 		fgname = (char *)Tk_NameOfColor(Tk_GetColorByValue(tkwin, &curcolor));
229 		break;
230 	    }
231 	    p++;
232 	}
233 	if (x == 256)
234 	    fgname = "black";
235 
236 	for (; x < 256; x++)
237 	{
238 	    if ((*p != STYLE_TRANSPARENT) && (*p != fgstyle))
239 	    {
240 		GrGetColor(GrStyleTable[*p].color, &red, &green, &blue);
241 		curcolor.red = (unsigned short)(red << 8);
242 		curcolor.green = (unsigned short)(green << 8);
243 		curcolor.blue = (unsigned short)(blue << 8);
244 		curcolor.flags = DoRed | DoGreen | DoBlue;
245 		bgname = StrDup((char **)NULL, fgname);
246 		fgname = bgname;
247 		fg_needs_free = TRUE;
248 		bgname = (char *)Tk_NameOfColor(Tk_GetColorByValue(tkwin, &curcolor));
249 		break;
250 	    }
251 	    p++;
252 	}
253 	if (x >= 256)
254 	    bgname = "white";
255 
256 	/* Perform transposition on the glyph matrix since X displays
257 	 * the least significant bit on the left hand side.
258 	 */
259 	p = &(g->gr_pixels[0]);
260 	for (y = 0; y < 32; y++) {
261 	    i = (y & 1) ? (32 - y) : (30 - y);
262 	    glyphcache->source[i] = glyphcache->mask[i] = 0;
263 	    for (x = 0; x < 8; x++)
264 	    {
265 		if (*p == fgstyle)
266 		     glyphcache->source[i] |= (1 << x);
267 		if (*p != STYLE_TRANSPARENT)
268 		     glyphcache->mask[i] |= (1 << x);
269 		p++;
270 	    }
271 	}
272 	grCursors[glyphnum] = Tk_GetCursorFromData(magicinterp,
273 			Tk_MainWindow(magicinterp),
274 			(char *)glyphcache->source, (char *)glyphcache->mask,
275 			16, 16, g->gr_origin.p_x, (15 - g->gr_origin.p_y),
276 			Tk_GetUid(fgname), Tk_GetUid(bgname));
277 
278 	if (fg_needs_free) freeMagic(fgname);
279     }
280 
281     /* Restore clipping */
282     grCurClip = oldClip;
283     GrUnlock(GR_LOCK_SCREEN);
284 }
285 
286 /*
287  *-------------------------------------------------------------------------
288  * GrTkWindowName --
289  *	Get the window name from the indicated MagWindow structure
290  *
291  * Results:
292  *	The Tk path name of the window "mw" (char * string).
293  *
294  * Side Effects:
295  *	None.
296  *-------------------------------------------------------------------------
297  */
298 
299 char *
GrTkWindowName(mw)300 GrTkWindowName(mw)
301     MagWindow *mw;
302 {
303     Tk_Window tkwind;
304     char *tkname;
305 
306     tkwind = (Tk_Window) mw->w_grdata;
307     return Tk_PathName(tkwind);
308 }
309 
310 /*
311  * ----------------------------------------------------------------------------
312  * grtkFreeBackingStore --
313  *	Free up Pixmap memory for a backing store cell.
314  *
315  * Results:
316  *	None.
317  *
318  * Side effects:
319  *	memory Free'd
320  * ----------------------------------------------------------------------------
321  */
322 
323 void
grtkFreeBackingStore(MagWindow * window)324 grtkFreeBackingStore(MagWindow *window)
325 {
326     Pixmap pmap = (Pixmap)window->w_backingStore;
327     if (pmap == (Pixmap)NULL) return;
328     XFreePixmap(grXdpy, pmap);
329     window->w_backingStore = (ClientData)NULL;
330     /* TxPrintf("grtkFreeBackingStore called\n"); */
331 }
332 
333 /*
334  * ----------------------------------------------------------------------------
335  * grtkCreateBackingStore --
336  *	Create Pixmap memory for a backing store cell and copy data
337  *	from the window into it.
338  *
339  * Results:
340  *	None.
341  *
342  * Side effects:
343  *	memory Allocated.
344  *
345  * ----------------------------------------------------------------------------
346  */
347 
348 void
grtkCreateBackingStore(MagWindow * w)349 grtkCreateBackingStore(MagWindow *w)
350 {
351     Pixmap pmap;
352     Tk_Window tkwind = (Tk_Window)w->w_grdata;
353     Window wind;
354     unsigned int width, height;
355 
356     /* ignore all windows other than layout */
357     if (w->w_client != DBWclientID) return;
358 
359     /* deferred */
360     if (tkwind == NULL) return;
361 
362     wind = Tk_WindowId(tkwind);
363     width = w->w_screenArea.r_xtop - w->w_screenArea.r_xbot;
364     height = w->w_screenArea.r_ytop - w->w_screenArea.r_ybot;
365 
366     if (w->w_backingStore != (ClientData)NULL) grtkFreeBackingStore(w);
367 
368     pmap = XCreatePixmap(grXdpy, wind, width, height, Tk_Depth(tkwind));
369     w->w_backingStore = (ClientData)pmap;
370 
371     /* TxPrintf("grtkCreateBackingStore area %d %d %d %d\n",
372 	w->w_screenArea.r_xbot, w->w_screenArea.r_ybot,
373 	w->w_screenArea.r_xtop, w->w_screenArea.r_ytop); */
374 }
375 
376 /*
377  * ----------------------------------------------------------------------------
378  * grtkGetBackingStore --
379  *	Copy data from a backing store Pixmap into the indicated window.
380  *
381  * Results:
382  *	TRUE if backing store was copied successfully, FALSE if not.
383  *
384  * Side effects:
385  *	Data copied into Pixmap memory.
386  *
387  * ----------------------------------------------------------------------------
388  */
389 
390 bool
grtkGetBackingStore(MagWindow * w,Rect * area)391 grtkGetBackingStore(MagWindow *w, Rect *area)
392 {
393     Pixmap pmap;
394     Tk_Window tkwind = (Tk_Window)w->w_grdata;
395     Window wind = Tk_WindowId(tkwind);
396     unsigned int width, height;
397     int ybot;
398     int xoff, yoff;
399     GC gc;
400     XGCValues gcValues;
401     Rect r;
402 
403     pmap = (Pixmap)w->w_backingStore;
404     if (pmap == (Pixmap)NULL)
405 	return FALSE;
406 
407     gcValues.graphics_exposures = FALSE;
408     gc = Tk_GetGC(tkwind, GCGraphicsExposures, &gcValues);
409 
410     /* Make a local copy of area so we don't disturb the	*/
411     /* original.  Expand by one pixel to deal with different	*/
412     /* boundary	conditions between X11 and OpenGL.  The redraw	*/
413     /* mechanism allows for at least this much slop.		*/
414 
415     GEO_EXPAND(area, 1, &r);
416     GeoClip(&r, &(w->w_screenArea));
417 
418     width = r.r_xtop - r.r_xbot;
419     height = r.r_ytop - r.r_ybot;
420     ybot = grXtransY(w, r.r_ytop);
421 
422     xoff = w->w_screenArea.r_xbot - w->w_allArea.r_xbot;
423     yoff = w->w_allArea.r_ytop - w->w_screenArea.r_ytop;
424 
425     XCopyArea(grXdpy, pmap, wind, gc, r.r_xbot - xoff,
426 		ybot - yoff, width, height, r.r_xbot, ybot);
427 
428     /* This is really only necessary for OpenGL, to avoid event */
429     /* timing conflicts between OpenGL calls and XCopyArea.	*/
430     /* It does not make sense for this to come AFTER the	*/
431     /* XCopyArea() command.  Yet that is what works.  So I'm	*/
432     /* sure that I do not entirely understand the situation.	*/
433 
434     (*GrFlushPtr)();
435 
436     /* TxPrintf("grtkGetBackingStore %d %d %d %d\n",
437 		r.r_xbot, r.r_ybot, r.r_xtop, r.r_ytop); */
438     return TRUE;
439 }
440 
441 /*
442  * ----------------------------------------------------------------------------
443  * grtkScrollBackingStore --
444  *	Enable fast scrolling by shifting part of the backing store
445  *	from one position to another, with the amount of shift indicated
446  *	by the X and/or Y value of the indicated point.
447  *
448  * Results:
449  *	TRUE on success, FALSE on failure.
450  *
451  * Side effects:
452  *	Data shifted in Pixmap memory.
453  *
454  * ----------------------------------------------------------------------------
455  */
456 
457 bool
grtkScrollBackingStore(MagWindow * w,Point * shift)458 grtkScrollBackingStore(MagWindow *w, Point *shift)
459 {
460     Pixmap pmap;
461     Tk_Window tkwind = (Tk_Window)w->w_grdata;
462     unsigned int width, height;
463     int xorigin, yorigin, xshift, yshift;
464     GC gc;
465     XGCValues gcValues;
466 
467     pmap = (Pixmap)w->w_backingStore;
468     if (pmap == (Pixmap)NULL)
469     {
470 	TxPrintf("grtkScrollBackingStore %d %d failure\n",
471 		shift->p_x, shift->p_y);
472 	return FALSE;
473     }
474 
475     gcValues.graphics_exposures = FALSE;
476     gc = Tk_GetGC(tkwind, GCGraphicsExposures, &gcValues);
477 
478     width = w->w_screenArea.r_xtop - w->w_screenArea.r_xbot;
479     height = w->w_screenArea.r_ytop - w->w_screenArea.r_ybot;
480     xorigin = 0;
481     yorigin = 0;
482     xshift = shift->p_x;
483     yshift = -shift->p_y;
484 
485     if (xshift > 0)
486 	width -= xshift;
487     else if (xshift < 0)
488     {
489 	width += xshift;
490 	xorigin = -xshift;
491 	xshift = 0;
492     }
493     if (yshift > 0)
494 	height -= yshift;
495     else if (yshift < 0)
496     {
497 	height += yshift;
498 	yorigin = -yshift;
499 	yshift = 0;
500     }
501 
502     XCopyArea(grXdpy, pmap, pmap, gc, xorigin, yorigin, width, height,
503 		xshift, yshift);
504 
505     /* TxPrintf("grtkScrollBackingStore %d %d\n", shift->p_x, shift->p_y); */
506     return TRUE;
507 }
508 
509 /*
510  * ----------------------------------------------------------------------------
511  * grtkPutBackingStore --
512  *	Copy data from the window into backing store.
513  *
514  * Results:
515  *	None.
516  *
517  * Side effects:
518  *	Graphics drawing into the window.
519  * ----------------------------------------------------------------------------
520  */
521 
522 void
grtkPutBackingStore(MagWindow * w,Rect * area)523 grtkPutBackingStore(MagWindow *w, Rect *area)
524 {
525     Pixmap pmap = (Pixmap)w->w_backingStore;
526     Tk_Window tkwind = (Tk_Window)w->w_grdata;
527     Window wind = Tk_WindowId(tkwind);
528     unsigned int width, height;
529     int ybot, xbot, xoff, yoff;
530     GC gc;
531     XGCValues gcValues;
532 
533     if (pmap == (Pixmap)NULL) return;
534 
535     /* Attempting to write backing store into an obscured	*/
536     /* window immediately invalidates everything in backing	*/
537     /* store.  This is extreme, but is much simpler and under	*/
538     /* normal conditions faster than tracking all obscured	*/
539     /* areas separately.					*/
540 
541     if (w->w_flags & WIND_OBSCURED)
542     {
543 	grtkFreeBackingStore(w);
544 	w->w_backingStore = (ClientData)NULL;
545 	return;
546     }
547 
548     width = area->r_xtop - area->r_xbot;
549     height = area->r_ytop - area->r_ybot;
550     ybot = grXtransY(w, area->r_ytop);
551     xbot = area->r_xbot;
552 
553     gcValues.graphics_exposures = FALSE;
554     gc = Tk_GetGC(tkwind, GCGraphicsExposures, &gcValues);
555 
556     xoff = w->w_screenArea.r_xbot - w->w_allArea.r_xbot;
557     yoff = w->w_allArea.r_ytop - w->w_screenArea.r_ytop;
558 
559     /* This area may need to be expanded by a pixel and/or	*/
560     /* expanded by GrPixelCorrect to compensate for the OpenGL	*/
561     /* coordinate system.					*/
562 
563     if (GrPixelCorrect == 0)
564     {
565 	height--;
566 	width--;
567 	xbot++;
568     }
569 
570     XCopyArea(grXdpy, wind, pmap, gc, xbot, ybot, width, height,
571 		xbot - xoff, ybot - yoff);
572 
573     /* TxPrintf("grtkPutBackingStore %d %d %d %d\n",
574 		xbot, area->r_ybot, area->r_xtop, area->r_ytop); */
575 }
576 
577 /*
578  *---------------------------------------------------------
579  * GrTkGetColorByName --
580  *
581  *	Returns a string appropriate for setting a color
582  *	value in a Tk window (referenced to the Tk colormap)
583  *	for the given color of the magic style referenced
584  *	by the short name or long name (as given in the dstyle5
585  *	file).
586  *
587  *	Note that the colormaps grXcmap and Tk_Colormap(tkwind)
588  *	are the same unless we are in 8-bit PseudoColor mode,
589  *	in which case this function maps between the two.
590  *
591  *	Note that when given an invalid short name, GrStyleNames[]
592  *	always returns 0, which is the index for style
593  *	"no_color_at_all".  On the other hand, long names are not
594  *	uniquely defined between "normal" and "pale" styles.
595  *
596  * Results:
597  *	A string allocated by Tcl_Alloc() which will need to
598  *	be free'd by the calling function using Tcl_Free().
599  *
600  * Side Effects:
601  *	None.
602  *
603  *---------------------------------------------------------
604  */
605 
606 char *
GrTkGetColorByName(name)607 GrTkGetColorByName(name)
608     char *name;
609 {
610     Tk_Window tkwind = Tk_MainWindow(magicinterp);
611     int style, red, green, blue;
612     XColor falsecolor;
613     char *colstring;
614 
615     if (strlen(name) == 1)
616 	style = GrStyleNames[name[0] & 0x7f];
617     else if (DBWNumStyles == 0)
618     {
619 	TxError("No style table exists.\n");
620 	return NULL;
621     }
622     else
623     {
624 	for (style = 0; style < TECHBEGINSTYLES + DBWNumStyles; style++)
625 	    if (GrStyleTable[style].longname != NULL)
626 		if (!strcmp(name, GrStyleTable[style].longname))
627 		    break;
628     }
629 
630     if (style >= TECHBEGINSTYLES + DBWNumStyles)
631     {
632 	TxError("Style does not exist or style is not accessible\n");
633 	return NULL;
634     }
635 
636     falsecolor.pixel = GrStyleTable[style].color;
637     if (GrTkInstalledCMap)
638     {
639 	XQueryColor(grXdpy, Tk_Colormap(tkwind), &falsecolor);
640         colstring = Tcl_Alloc(14);
641         sprintf(colstring, "#%04x%04x%04x", falsecolor.red,
642 			falsecolor.green, falsecolor.blue);
643     }
644     else
645     {
646 	/* Note that XColor colors are unsigned short, but GrGetColor	*/
647 	/* expects an int *.						*/
648 
649 	GrGetColor(falsecolor.pixel, &red, &green, &blue);
650 	falsecolor.red = red;
651 	falsecolor.green = green;
652 	falsecolor.blue = blue;
653         colstring = Tcl_Alloc(8);
654         sprintf(colstring, "#%02x%02x%02x", falsecolor.red,
655 			falsecolor.green, falsecolor.blue);
656     }
657     return colstring;
658 }
659 
660 /*
661  *---------------------------------------------------------
662  * _magic_magiccolor --
663  *	Tcl command callback for GrTkGetColorByName
664  *
665  * Results:
666  *	TCL result type
667  *
668  * Side effects:
669  *	None.
670  *---------------------------------------------------------
671  */
672 
673 static int
_magic_magiccolor(ClientData clientData,Tcl_Interp * interp,int argc,char * argv[])674 _magic_magiccolor(ClientData clientData,
675 	Tcl_Interp *interp, int argc, char *argv[])
676 {
677     char *result;
678     char *name;
679 
680     if (argc != 2)
681     {
682 	TxError("Usage: magiccolor name\n");
683 	return TCL_ERROR;
684     }
685     name = argv[1];
686 
687     result = GrTkGetColorByName(name);
688     if (result)
689     {
690 	Tcl_SetResult(interp, result, TCL_DYNAMIC);
691  	return TCL_OK;
692     }
693     else {
694 	TxError("No such color name \"%s\" in style file.\n", name);
695 	return TCL_ERROR;
696     }
697 }
698 
699 /*
700  * The following data structure represents the master for a layer
701  * image:
702  */
703 
704 typedef struct LayerMaster {
705     Tk_ImageMaster tkMaster;	/* Tk's token for image master.  NULL means
706 				 * the image is being deleted. */
707     Tcl_Interp *interp;		/* Interpreter for application that is
708 				 * using image. */
709     Tcl_Command imageCmd;	/* Token for image command (used to delete
710 				 * it when the image goes away).  NULL means
711 				 * the image command has already been
712 				 * deleted. */
713     int width, height;		/* Dimensions of image. */
714     int layerOff;		/* If TRUE layer is displayed in non-edit style */
715     int layerLock;		/* Layer is displayed with a cursor icon */
716     char *layerString;		/* Value of -layer option (malloc'ed). */
717     struct LayerInstance *instancePtr;
718 				/* First in list of all instances associated
719 				 * with this master. */
720 } LayerMaster;
721 
722 /*
723  * The following data structure represents all of the instances of an
724  * image that lie within a particular window:
725  */
726 
727 typedef struct LayerInstance {
728     int refCount;		/* Number of instances that share this
729 				 * data structure. */
730     LayerMaster *masterPtr;	/* Pointer to master for image. */
731     Tk_Window tkwin;		/* Window in which the instances will be
732 				 * displayed. */
733     Pixmap pixmap;		/* The bitmap to display. */
734     GC gc;			/* Graphics context for displaying pixmap */
735     struct LayerInstance *nextPtr;
736 				/* Next in list of all instance structures
737 				 * associated with masterPtr (NULL means
738 				 * end of list). */
739 } LayerInstance;
740 
741 /*
742  * The type record for bitmap images:
743  */
744 
745 static int		ImgLayerCreate _ANSI_ARGS_((Tcl_Interp *interp,
746 			    CONST84 char *name, int argc, Tcl_Obj *const objv[],
747 			    CONST84 Tk_ImageType *typePtr, Tk_ImageMaster master,
748 			    ClientData *clientDataPtr));
749 static ClientData	ImgLayerGet _ANSI_ARGS_((Tk_Window tkwin,
750 			    ClientData clientData));
751 static void		ImgLayerDisplay _ANSI_ARGS_((ClientData clientData,
752 			    Display *display, Drawable drawable,
753 			    int imageX, int imageY, int width, int height,
754 			    int drawableX, int drawableY));
755 static void		ImgLayerFree _ANSI_ARGS_((ClientData clientData,
756 			    Display *display));
757 static void		ImgLayerDelete _ANSI_ARGS_((ClientData clientData));
758 
759 Tk_ImageType tkLayerImageType = {
760     "layer",			/* name */
761     ImgLayerCreate,		/* createProc */
762     ImgLayerGet,		/* getProc */
763     ImgLayerDisplay,		/* displayProc */
764     ImgLayerFree,		/* freeProc */
765     ImgLayerDelete,		/* deleteProc */
766     NULL,			/* postscriptProc */
767     (Tk_ImageType *) NULL	/* nextPtr */
768 };
769 
770 /*
771  * Information used for parsing configuration specs:
772  * Size defaults to a 16x16 area.
773  */
774 
775 static Tk_ConfigSpec configSpecs[] = {
776     {TK_CONFIG_STRING, "-name", (char *) NULL, (char *) NULL,
777 	(char *) NULL, Tk_Offset(LayerMaster, layerString), TK_CONFIG_NULL_OK},
778     {TK_CONFIG_BOOLEAN, "-disabled", (char *) NULL, (char *) NULL,
779 	(char *) "0", Tk_Offset(LayerMaster, layerOff), 0},
780     {TK_CONFIG_INT, "-icon", (char *) NULL, (char *) NULL,
781 	(char *) "-1", Tk_Offset(LayerMaster, layerLock), 0},
782     {TK_CONFIG_INT, "-width", (char *) NULL, (char *) NULL,
783 	(char *) "16", Tk_Offset(LayerMaster, width), 0},
784     {TK_CONFIG_INT, "-height", (char *) NULL, (char *) NULL,
785 	(char *) "16", Tk_Offset(LayerMaster, height), 0},
786     {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
787 	(char *) NULL, 0, 0}
788 };
789 
790 /*
791  * Prototypes for procedures used only locally in this file:
792  */
793 
794 static int		ImgLayerCmd _ANSI_ARGS_((ClientData clientData,
795 			    Tcl_Interp *interp, int argc, Tcl_Obj *CONST objv[]));
796 static void		ImgLayerCmdDeletedProc _ANSI_ARGS_((
797 			    ClientData clientData));
798 static void		ImgLayerConfigureInstance _ANSI_ARGS_((
799 			    LayerInstance *instancePtr));
800 static int		ImgLayerConfigureMaster _ANSI_ARGS_((
801 			    LayerMaster *masterPtr, int argc, Tcl_Obj *CONST objv[],
802 			    int flags));
803 
804 /*
805  *----------------------------------------------------------------------
806  *
807  * ImgLayerCreate --
808  *
809  *	This procedure is called by the Tk image code to create "test"
810  *	images.
811  *
812  * Results:
813  *	A standard Tcl result.
814  *
815  * Side effects:
816  *	The data structure for a new image is allocated.
817  *
818  *----------------------------------------------------------------------
819  */
820 
821 	/* ARGSUSED */
822 static int
ImgLayerCreate(interp,name,argc,argv,typePtr,master,clientDataPtr)823 ImgLayerCreate(interp, name, argc, argv, typePtr, master, clientDataPtr)
824     Tcl_Interp *interp;		/* Interpreter for application containing
825 				 * image. */
826     const char *name;		/* Name to use for image. */
827     int argc;			/* Number of arguments. */
828     Tcl_Obj *CONST argv[];	/* Argument objects for options (doesn't
829 				 * include image name or type). */
830     const Tk_ImageType *typePtr;/* Pointer to our type record (not used). */
831     Tk_ImageMaster master;	/* Token for image, to be used by us in
832 				 * later callbacks. */
833     ClientData *clientDataPtr;	/* Store manager's token for image here;
834 				 * it will be returned in later callbacks. */
835 {
836     LayerMaster *masterPtr;
837 
838     masterPtr = (LayerMaster *) Tcl_Alloc(sizeof(LayerMaster));
839     masterPtr->tkMaster = master;
840     masterPtr->interp = interp;
841     masterPtr->imageCmd = Tcl_CreateObjCommand(interp, name, ImgLayerCmd,
842 	    (ClientData) masterPtr, ImgLayerCmdDeletedProc);
843     masterPtr->width = masterPtr->height = 0;
844     masterPtr->layerOff = 0;
845     masterPtr->layerLock = -1;
846     masterPtr->layerString = NULL;
847     masterPtr->instancePtr = NULL;
848     if (ImgLayerConfigureMaster(masterPtr, argc, argv, 0) != TCL_OK) {
849 	ImgLayerDelete((ClientData) masterPtr);
850 	return TCL_ERROR;
851     }
852     *clientDataPtr = (ClientData) masterPtr;
853     return TCL_OK;
854 }
855 
856 /*
857  *----------------------------------------------------------------------
858  *
859  * ImgLayerConfigureMaster --
860  *
861  *	This procedure is called when a bitmap image is created or
862  *	reconfigured.  It process configuration options and resets
863  *	any instances of the image.
864  *
865  * Results:
866  *	A standard Tcl return value.  If TCL_ERROR is returned then
867  *	an error message is left in the masterPtr->interp's result.
868  *
869  * Side effects:
870  *	Existing instances of the image will be redisplayed to match
871  *	the new configuration options.
872  *
873  *----------------------------------------------------------------------
874  */
875 
876 static int
ImgLayerConfigureMaster(masterPtr,objc,objv,flags)877 ImgLayerConfigureMaster(masterPtr, objc, objv, flags)
878     LayerMaster *masterPtr;	/* Pointer to data structure describing
879 				 * overall pixmap image to (reconfigure). */
880     int objc;			/* Number of entries in objv. */
881     Tcl_Obj *CONST objv[];	/* Pairs of configuration options for image. */
882     int flags;			/* Flags to pass to Tk_ConfigureWidget,
883 				 * such as TK_CONFIG_ARGV_ONLY. */
884 {
885     LayerInstance *instancePtr;
886     int dummy1;
887 
888     char **argv = (char **) Tcl_Alloc((objc+1) * sizeof(char *));
889     for (dummy1 = 0; dummy1 < objc; dummy1++) {
890 	argv[dummy1]=Tcl_GetString(objv[dummy1]);
891     }
892     argv[objc] = NULL;
893 
894     if (Tk_ConfigureWidget(masterPtr->interp, Tk_MainWindow(masterPtr->interp),
895 	    configSpecs, objc, (CONST84 char **)argv, (char *) masterPtr, flags)
896 	    != TCL_OK) {
897 	Tcl_Free((char *) argv);
898 	return TCL_ERROR;
899     }
900     Tcl_Free((char *) argv);
901 
902     /*
903      * Cycle through all of the instances of this image, regenerating
904      * the information for each instance.  Then force the image to be
905      * redisplayed everywhere that it is used.
906      */
907 
908     for (instancePtr = masterPtr->instancePtr; instancePtr != NULL;
909 	    instancePtr = instancePtr->nextPtr) {
910 	ImgLayerConfigureInstance(instancePtr);
911     }
912     Tk_ImageChanged(masterPtr->tkMaster, 0, 0, masterPtr->width,
913 	    masterPtr->height, masterPtr->width, masterPtr->height);
914     return TCL_OK;
915 }
916 
917 /*
918  *----------------------------------------------------------------------
919  * grDrawOffScreenBox --
920  *	Draw a box on an off-screen drawable (convenience function for
921  *	the following procedure ImgLayerConfigureInstance()
922  *
923  * Results:
924  *	None.
925  *
926  * Side effects:
927  *	Draws into an off-screen drawable destination.
928  *
929  *----------------------------------------------------------------------
930  */
931 
932 void
grDrawOffScreenBox(rect)933 grDrawOffScreenBox(rect)
934     Rect *rect;
935 {
936     (*grDrawLinePtr)(rect->r_xbot, rect->r_ytop - 1, rect->r_xtop - 1,
937 			rect->r_ytop - 1);
938     (*grDrawLinePtr)(rect->r_xbot, rect->r_ybot, rect->r_xtop - 1,
939 			rect->r_ybot);
940     (*grDrawLinePtr)(rect->r_xbot, rect->r_ybot, rect->r_xbot,
941 			rect->r_ytop - 1);
942     (*grDrawLinePtr)(rect->r_xtop - 1, rect->r_ybot, rect->r_xtop - 1,
943 			rect->r_ytop - 1);
944 }
945 
946 
947 /*
948  *----------------------------------------------------------------------
949  *
950  * ImgLayerConfigureInstance --
951  *
952  *	This procedure is called to create displaying information for
953  *	a layer image instance based on the configuration information
954  *	in the master.  It is invoked both when new instances are
955  *	created and when the master is reconfigured.
956  *
957  * Results:
958  *	None.
959  *
960  * Side effects:
961  *	Generates errors via Tcl_BackgroundError if there are problems
962  *	in setting up the instance.
963  *
964  *----------------------------------------------------------------------
965  */
966 
967 #define LAYER_NORMAL	0
968 #define LAYER_LABELS	1
969 #define LAYER_SUBCELL	2
970 #define LAYER_LAYOUT	3
971 
972 static void
ImgLayerConfigureInstance(instancePtr)973 ImgLayerConfigureInstance(instancePtr)
974     LayerInstance *instancePtr;	/* Instance to reconfigure. */
975 {
976     LayerMaster *masterPtr = instancePtr->masterPtr;
977     XGCValues gcValues;
978     GC gc;
979     unsigned int gcmask;
980     TileType layer;
981     TileTypeBitMask *mask;
982     int i, special = LAYER_NORMAL;
983     Rect r;
984     Tk_Window tkwind = instancePtr->tkwin;
985     MagWindow *mw, tmpmw;
986 
987     int saveStyle;
988 
989     if (Tk_WindowId(tkwind) == 0)
990 	Tk_MakeWindowExist(tkwind);
991 
992     if (Tk_WindowId(tkwind) == 0)
993     {
994 	Tcl_AddErrorInfo(masterPtr->interp, "No ID exists for window");
995 	goto error;
996     }
997 
998     /*
999      * For each of the options in masterPtr, translate the string
1000      * form into an internal form appropriate for instancePtr.
1001      */
1002 
1003     if (masterPtr->width <= 0 || masterPtr->height <= 0)
1004     {
1005 	Tcl_AddErrorInfo(masterPtr->interp, "Image width or height is negative");
1006 	goto error;
1007     }
1008 
1009     if (instancePtr->pixmap != None) {
1010 	mw = WindSearchData((ClientData)instancePtr->pixmap);
1011 	if (mw != NULL)
1012 	{
1013 	    windUnlink(mw);
1014 	    windReClip();
1015 	    windFree(mw);
1016 	}
1017 	Tk_FreePixmap(grXdpy, instancePtr->pixmap);
1018 	instancePtr->pixmap = None;
1019     }
1020 
1021     if (masterPtr->layerString != NULL) {
1022 	if (!strcmp(masterPtr->layerString, "none"))
1023 	    layer = TT_SPACE;
1024 	else if (!strcmp(masterPtr->layerString, "errors"))
1025 	    layer = TT_ERROR_P;
1026 	else if (!strcmp(masterPtr->layerString, "labels"))
1027 	{
1028 	    layer = TT_SPACE;
1029 	    special = LAYER_LABELS;
1030 	}
1031 	else if (!strcmp(masterPtr->layerString, "subcell"))
1032 	{
1033 	    layer = TT_SPACE;
1034 	    special = LAYER_SUBCELL;
1035 	}
1036 	else
1037 	    layer = DBTechNameType(masterPtr->layerString);
1038 
1039 	if (layer < 0)
1040 	{
1041 	    layer = (*GrWindowIdPtr)(masterPtr->layerString);
1042 
1043 	    if (layer >= 0)
1044 		special = LAYER_LAYOUT;
1045 	    else
1046 	    {
1047 		Tcl_AddErrorInfo(masterPtr->interp, "Unknown layer type");
1048 		goto error;
1049 	    }
1050 	}
1051 
1052 	r.r_xbot = r.r_ybot = 0;
1053 	r.r_xtop = masterPtr->width;
1054 	r.r_ytop = masterPtr->height;
1055 
1056 	gcValues.graphics_exposures = FALSE;
1057 	gcmask = GCGraphicsExposures;
1058 	gc = Tk_GetGC(tkwind, gcmask, &gcValues);
1059 
1060 	if (instancePtr->gc != None)
1061 	    Tk_FreeGC(grXdpy, instancePtr->gc);
1062 	instancePtr->gc = gc;
1063 
1064 	if (special == LAYER_LAYOUT)	/* Off-Screen Rendering */
1065 	{
1066 	    Rect screenRect;
1067 	    Tk_Window pixwind;
1068 
1069 	    mw = WindSearchWid(layer);
1070 	    if (mw == NULL)
1071 	    {
1072 		Tcl_AddErrorInfo(masterPtr->interp, "Unknown window ID\n");
1073 		goto error;
1074 	    }
1075 
1076 	    pixwind = (Tk_Window)mw->w_grdata;
1077 
1078 	    instancePtr->pixmap = Tk_GetPixmap(grXdpy,
1079 			Tk_WindowId(pixwind),
1080 			masterPtr->width, masterPtr->height,
1081 			Tk_Depth(pixwind));
1082 
1083 	    (*GrDeleteWindowPtr)(mw);
1084 	    mw->w_flags |= WIND_OFFSCREEN;
1085 	    mw->w_grdata = (ClientData)instancePtr->pixmap;
1086 
1087 	    screenRect.r_xbot = 0;
1088 	    screenRect.r_ybot = 0;
1089 	    screenRect.r_xtop = masterPtr->width;
1090 	    screenRect.r_ytop = masterPtr->height;
1091 
1092 	    WindReframe(mw, &screenRect, FALSE, FALSE);
1093 	    WindRedisplay(mw);
1094 
1095 	    return;
1096 	}
1097 
1098 	instancePtr->pixmap = Tk_GetPixmap(grXdpy,
1099 		Tk_WindowId(tkwind),
1100 		masterPtr->width, masterPtr->height,
1101 		Tk_Depth(tkwind));
1102 
1103 	tmpmw.w_flags = WIND_OFFSCREEN;
1104 	tmpmw.w_grdata = (ClientData)instancePtr->pixmap;
1105 	tmpmw.w_grdata2 = (ClientData)NULL;
1106 	tmpmw.w_allArea = r;
1107 	tmpmw.w_clipAgainst = NULL;
1108 
1109 	GrLock(&tmpmw, FALSE);
1110 
1111 	/* Save the current state */
1112 	saveStyle = grCurDStyle;
1113 
1114 	/* First fill with background style */
1115 	GrSetStuff(STYLE_ERASEALL);
1116 	grInformDriver();
1117 	(*grFillRectPtr)(&r);
1118 
1119 	for (i = 0; i < DBWNumStyles; i++)
1120 	{
1121 	    mask = DBWStyleToTypes(i);
1122 	    if (TTMaskHasType(mask, layer))
1123 	    {
1124 		GrSetStuff(i + TECHBEGINSTYLES +
1125 			((masterPtr->layerOff == 0) ? 0 : DBWNumStyles));
1126 		grInformDriver();
1127 
1128 		/* Solid areas */
1129 		if ((grCurFill == GR_STSOLID) || (grCurFill == GR_STSTIPPLE))
1130 		    (*grFillRectPtr)(&r);
1131 
1132 		/* Outlines */
1133 		if (grCurOutline != 0)
1134 		    grDrawOffScreenBox(&r);
1135 
1136 		/* Contact crosses */
1137 		if (grCurFill == GR_STCROSS)
1138 		{
1139 		    (*grDrawLinePtr)(r.r_xbot, r.r_ybot, r.r_xtop - 1,
1140 				r.r_ytop - 1);
1141 		    (*grDrawLinePtr)(r.r_xbot, r.r_ytop - 1, r.r_xtop - 1,
1142 				r.r_ybot);
1143 		}
1144 	    }
1145 	}
1146 
1147 	switch(special) {
1148 	    case LAYER_LABELS:
1149 		GrSetStuff(STYLE_LABEL);
1150 		grInformDriver();
1151 		grDrawOffScreenBox(&r);
1152 		break;
1153 	    case LAYER_SUBCELL:
1154 		GrSetStuff(STYLE_BBOX);
1155 		grInformDriver();
1156 		grDrawOffScreenBox(&r);
1157 		break;
1158 	}
1159 	if (masterPtr->layerLock >= 0) {
1160 	    GrSetStuff(STYLE_BLACK);
1161 	    grInformDriver();
1162 	    GrDrawGlyphNum(masterPtr->layerLock, 0, 0);
1163 	}
1164 
1165 	/* Restore the original state */
1166 	GrSetStuff(saveStyle);
1167 	grInformDriver();
1168 
1169 	GrUnlock(&tmpmw);
1170     }
1171     return;
1172 
1173 error:
1174 
1175     /*
1176      * An error occurred: clear the graphics context in the instance to
1177      * make it clear that this instance cannot be displayed.  Then report
1178      * the error.
1179      */
1180 
1181     if (instancePtr->gc != None)
1182 	Tk_FreeGC(grXdpy, instancePtr->gc);
1183     instancePtr->gc = None;
1184 
1185     Tcl_AddErrorInfo(masterPtr->interp, "\n    (while configuring image \"");
1186     Tcl_AddErrorInfo(masterPtr->interp, Tk_NameOfImage(masterPtr->tkMaster));
1187     Tcl_AddErrorInfo(masterPtr->interp, "\")");
1188     Tcl_BackgroundError(masterPtr->interp);
1189 }
1190 
1191 
1192 /*
1193  *--------------------------------------------------------------
1194  *
1195  * ImgLayerCmd --
1196  *
1197  *	This procedure is invoked to process the Tcl command
1198  *	that corresponds to an image managed by this module.
1199  *	See the user documentation for details on what it does.
1200  *
1201  * Results:
1202  *	A standard Tcl result.
1203  *
1204  * Side effects:
1205  *	See the user documentation.
1206  *
1207  *--------------------------------------------------------------
1208  */
1209 
1210 static int
ImgLayerCmd(clientData,interp,objc,objv)1211 ImgLayerCmd(clientData, interp, objc, objv)
1212     ClientData clientData;	/* Information about the image master. */
1213     Tcl_Interp *interp;		/* Current interpreter. */
1214     int objc;			/* Number of arguments. */
1215     Tcl_Obj *CONST objv[];	/* Argument objects. */
1216 {
1217     static char *layerOptions[] = {"cget", "configure", (char *) NULL};
1218     LayerMaster *masterPtr = (LayerMaster *) clientData;
1219     int code, index;
1220 
1221     if (objc < 2) {
1222 	Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg ...?");
1223 	return TCL_ERROR;
1224     }
1225     if (Tcl_GetIndexFromObj(interp, objv[1], (CONST84 char **)layerOptions,
1226 		"option", 0, &index) != TCL_OK) {
1227 	return TCL_ERROR;
1228     }
1229     switch (index) {
1230       case 0: {
1231 	if (objc != 3) {
1232 	    Tcl_WrongNumArgs(interp, 2, objv, "option");
1233 	    return TCL_ERROR;
1234 	}
1235 	return Tk_ConfigureValue(interp, Tk_MainWindow(interp), configSpecs,
1236 		(char *) masterPtr, Tcl_GetString(objv[2]), 0);
1237       }
1238       case 1: {
1239 	if (objc == 2) {
1240 	    code = Tk_ConfigureInfo(interp, Tk_MainWindow(interp),
1241 		    configSpecs, (char *) masterPtr, (char *) NULL, 0);
1242 	} else if (objc == 3) {
1243 	    code = Tk_ConfigureInfo(interp, Tk_MainWindow(interp),
1244 		    configSpecs, (char *) masterPtr,
1245 		    Tcl_GetString(objv[2]), 0);
1246 	} else {
1247 	    code = ImgLayerConfigureMaster(masterPtr, objc-2, objv+2,
1248 		    TK_CONFIG_ARGV_ONLY);
1249 	}
1250 	return code;
1251       }
1252       default: {
1253 	panic("bad const entries to layerOptions in ImgLayerCmd");
1254       }
1255     }
1256     return TCL_OK;
1257 }
1258 
1259 /*
1260  *----------------------------------------------------------------------
1261  *
1262  * ImgLayerGet --
1263  *
1264  *	This procedure is called for each use of a layer image in a
1265  *	widget.
1266  *
1267  * Results:
1268  *	The return value is a token for the instance, which is passed
1269  *	back to us in calls to ImgLayerDisplay and ImgLayerFree.
1270  *
1271  * Side effects:
1272  *	A data structure is set up for the instance (or, an existing
1273  *	instance is re-used for the new one).
1274  *
1275  *----------------------------------------------------------------------
1276  */
1277 
1278 static ClientData
ImgLayerGet(tkwin,masterData)1279 ImgLayerGet(tkwin, masterData)
1280     Tk_Window tkwin;		/* Window in which the instance will be
1281 				 * used. */
1282     ClientData masterData;	/* Pointer to our master structure for the
1283 				 * image. */
1284 {
1285     LayerMaster *masterPtr = (LayerMaster *) masterData;
1286     LayerInstance *instancePtr;
1287 
1288     /*
1289      * See if there is already an instance for this window.  If so
1290      * then just re-use it.
1291      */
1292 
1293     for (instancePtr = masterPtr->instancePtr; instancePtr != NULL;
1294 	    instancePtr = instancePtr->nextPtr) {
1295 	if (instancePtr->tkwin == tkwin) {
1296 	    instancePtr->refCount++;
1297 	    return (ClientData) instancePtr;
1298 	}
1299     }
1300 
1301     /*
1302      * The image isn't already in use in this window.  Make a new
1303      * instance of the image.
1304      */
1305 
1306     instancePtr = (LayerInstance *) Tcl_Alloc(sizeof(LayerInstance));
1307     instancePtr->refCount = 1;
1308     instancePtr->masterPtr = masterPtr;
1309     instancePtr->tkwin = tkwin;
1310     instancePtr->pixmap = None;
1311     instancePtr->gc = None;
1312     instancePtr->nextPtr = masterPtr->instancePtr;
1313     masterPtr->instancePtr = instancePtr;
1314     ImgLayerConfigureInstance(instancePtr);
1315 
1316     /*
1317      * If this is the first instance, must set the size of the image.
1318      */
1319 
1320     if (instancePtr->nextPtr == NULL) {
1321 	Tk_ImageChanged(masterPtr->tkMaster, 0, 0, 0, 0, masterPtr->width,
1322 		masterPtr->height);
1323     }
1324 
1325     return (ClientData) instancePtr;
1326 }
1327 
1328 /*
1329  *----------------------------------------------------------------------
1330  *
1331  * ImgLayerDisplay --
1332  *
1333  *	This procedure is invoked to draw a layer image.
1334  *
1335  * Results:
1336  *	None.
1337  *
1338  * Side effects:
1339  *	A portion of the image gets rendered in a pixmap or window.
1340  *
1341  *----------------------------------------------------------------------
1342  */
1343 
1344 static void
ImgLayerDisplay(clientData,display,drawable,imageX,imageY,width,height,drawableX,drawableY)1345 ImgLayerDisplay(clientData, display, drawable, imageX, imageY, width,
1346 	height, drawableX, drawableY)
1347     ClientData clientData;	/* Pointer to LayerInstance structure for
1348 				 * for instance to be displayed. */
1349     Display *display;		/* Display on which to draw image. */
1350     Drawable drawable;		/* Pixmap or window in which to draw image. */
1351     int imageX, imageY;		/* Upper-left corner of region within image
1352 				 * to draw. */
1353     int width, height;		/* Dimensions of region within image to draw. */
1354     int drawableX, drawableY;	/* Coordinates within drawable that
1355 				 * correspond to imageX and imageY. */
1356 {
1357     LayerInstance *instancePtr = (LayerInstance *) clientData;
1358 
1359     /*
1360      * If there's no GC, then an error occurred during image creation
1361      * and it should not be displayed.
1362      */
1363 
1364     if (instancePtr->gc == None) return;
1365 
1366     XCopyArea(display, instancePtr->pixmap, drawable, instancePtr->gc,
1367 	    imageX, imageY, (unsigned) width, (unsigned) height,
1368 	    drawableX, drawableY);
1369 }
1370 
1371 /*
1372  *----------------------------------------------------------------------
1373  *
1374  * ImgLayerFree --
1375  *
1376  *	This procedure is called when a widget ceases to use a
1377  *	particular instance of an image.
1378  *
1379  * Results:
1380  *	None.
1381  *
1382  * Side effects:
1383  *	Internal data structures get cleaned up.
1384  *
1385  *----------------------------------------------------------------------
1386  */
1387 
1388 static void
ImgLayerFree(clientData,display)1389 ImgLayerFree(clientData, display)
1390     ClientData clientData;	/* Pointer to LayerInstance structure for
1391 				 * for instance to be displayed. */
1392     Display *display;		/* Display containing window that used image. */
1393 {
1394     LayerInstance *instancePtr = (LayerInstance *) clientData;
1395     LayerInstance *prevPtr;
1396 
1397     instancePtr->refCount--;
1398     if (instancePtr->refCount > 0) {
1399 	return;
1400     }
1401 
1402     /*
1403      * There are no more uses of the image within this widget.  Free
1404      * the instance structure.
1405      */
1406 
1407     if (instancePtr->pixmap != None) {
1408 	MagWindow *mw;
1409 	mw = WindSearchData((ClientData)instancePtr->pixmap);
1410 	if (mw != NULL)
1411 	{
1412 	    windUnlink(mw);
1413 	    windReClip();
1414 	    windFree(mw);
1415 	}
1416 	Tk_FreePixmap(display, instancePtr->pixmap);
1417     }
1418     if (instancePtr->masterPtr->instancePtr == instancePtr) {
1419 	instancePtr->masterPtr->instancePtr = instancePtr->nextPtr;
1420     } else {
1421 	for (prevPtr = instancePtr->masterPtr->instancePtr;
1422 		prevPtr->nextPtr != instancePtr; prevPtr = prevPtr->nextPtr) {
1423 	    /* Empty loop body */
1424 	}
1425 	prevPtr->nextPtr = instancePtr->nextPtr;
1426     }
1427     Tcl_Free((char *) instancePtr);
1428 }
1429 
1430 /*
1431  *----------------------------------------------------------------------
1432  *
1433  * ImgLayerDelete --
1434  *
1435  *	This procedure is called by the image code to delete the
1436  *	master structure for an image.
1437  *
1438  * Results:
1439  *	None.
1440  *
1441  * Side effects:
1442  *	Resources associated with the image get freed.
1443  *
1444  *----------------------------------------------------------------------
1445  */
1446 
1447 static void
ImgLayerDelete(masterData)1448 ImgLayerDelete(masterData)
1449     ClientData masterData;	/* Pointer to BitmapMaster structure for
1450 				 * image.  Must not have any more instances. */
1451 {
1452     LayerMaster *masterPtr = (LayerMaster *) masterData;
1453 
1454     if (masterPtr->instancePtr != NULL) {
1455 	panic("tried to delete layer image when instances still exist");
1456     }
1457     masterPtr->tkMaster = NULL;
1458     if (masterPtr->imageCmd != NULL) {
1459 	Tcl_DeleteCommandFromToken(masterPtr->interp, masterPtr->imageCmd);
1460     }
1461     Tk_FreeOptions(configSpecs, (char *) masterPtr, (Display *) NULL, 0);
1462     Tcl_Free((char *) masterPtr);
1463 }
1464 
1465 /*
1466  *----------------------------------------------------------------------
1467  *
1468  * ImgLayerCmdDeletedProc --
1469  *
1470  *	This procedure is invoked when the image command for an image
1471  *	is deleted.  It deletes the image.
1472  *
1473  * Results:
1474  *	None.
1475  *
1476  * Side effects:
1477  *	The image is deleted.
1478  *
1479  *----------------------------------------------------------------------
1480  */
1481 
1482 static void
ImgLayerCmdDeletedProc(clientData)1483 ImgLayerCmdDeletedProc(clientData)
1484     ClientData clientData;	/* Pointer to BitmapMaster structure for
1485 				 * image. */
1486 {
1487     LayerMaster *masterPtr = (LayerMaster *) clientData;
1488 
1489     masterPtr->imageCmd = NULL;
1490     if (masterPtr->tkMaster != NULL) {
1491 	Tk_DeleteImage(masterPtr->interp, Tk_NameOfImage(masterPtr->tkMaster));
1492     }
1493 }
1494 
1495 /*
1496  *---------------------------------------------------------
1497  * RegisterTkCommands --
1498  *	Register the "magiccolor" command with Tcl.
1499  *	Register the layer type with the Tk "image" command
1500  *
1501  * Results:
1502  *	None.
1503  *
1504  * Side Effects:
1505  *	Command registration with the TCL interpreter.
1506  *---------------------------------------------------------
1507  */
1508 
1509 void
RegisterTkCommands(Tcl_Interp * interp)1510 RegisterTkCommands(Tcl_Interp *interp)
1511 {
1512     Tcl_CreateCommand(interp, "magic::magiccolor",
1513 		(Tcl_CmdProc *)_magic_magiccolor, (ClientData)NULL,
1514 		(Tcl_CmdDeleteProc *)NULL);
1515     Tk_CreateImageType(&tkLayerImageType);
1516 }
1517 
1518 #endif   /* MAGIC_WRAPPER */
1519