Debian Codegraph @

pTk/mTk/additions/imgPmap.c

raw
1 /*
2 * tkImgPmap.c --
3 *
4 * This file implements images of type "pixmap" for Tk.
5 *
6 * Copyright (c) 1996, Expert Interface Technologies
7 *
8 * See the file "license.terms" for information on usage and redistribution
9 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 *
11 */
12
13 #include <imgInt.h>
14 #include <ctype.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <imgPmap.h>
18
19 #if defined(__WIN32__) && !defined (__GNUC__)
20 #define strncasecmp strnicmp
21 #endif
22
23 #ifndef MAC_TCL
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #endif
27
28 #ifndef TCL_STUB_MAGIC
29 EXTERN void panic _ANSI_ARGS_(TCL_VARARGS(char *,format));
30 #endif
31
32 #define UCHAR(c) ((unsigned char) (c))
33
34 /*
35 * Prototypes for procedures used only locally in this file:
36 */
37
38 static int ImgXpmCreate _ANSI_ARGS_((Tcl_Interp *interp,
39 char *name, int argc, Tcl_Obj *objv[],
40 Tk_ImageType *typePtr, Tk_ImageMaster master,
41 ClientData *clientDataPtr));
42 static ClientData ImgXpmGet _ANSI_ARGS_((Tk_Window tkwin,
43 ClientData clientData));
44 static void ImgXpmDisplay _ANSI_ARGS_((ClientData clientData,
45 Display *display, Drawable drawable,
46 int imageX, int imageY, int width, int height,
47 int drawableX, int drawableY));
48 static void ImgXpmFree _ANSI_ARGS_((ClientData clientData,
49 Display *display));
50 static void ImgXpmDelete _ANSI_ARGS_((ClientData clientData));
51 static int ImgXpmCmd _ANSI_ARGS_((ClientData clientData,
52 Tcl_Interp *interp, int argc, char **argv));
53 static void ImgXpmCmdDeletedProc _ANSI_ARGS_((
54 ClientData clientData));
55 static void ImgXpmConfigureInstance _ANSI_ARGS_((
56 PixmapInstance *instancePtr));
57 static int ImgXpmConfigureMaster _ANSI_ARGS_((
58 PixmapMaster *masterPtr, int argc, char **argv,
59 int flags));
60 static int ImgXpmGetData _ANSI_ARGS_((Tcl_Interp *interp,
61 PixmapMaster *masterPtr));
62 static char ** ImgXpmGetDataFromFile _ANSI_ARGS_((Tcl_Interp * interp,
63 char * string, int * numLines_return));
64 static char ** ImgXpmGetDataFromString _ANSI_ARGS_((Tcl_Interp*interp,
65 char * string, int * numLines_return));
66 static void ImgXpmGetPixmapFromData _ANSI_ARGS_((
67 Tcl_Interp * interp,
68 PixmapMaster *masterPtr,
69 PixmapInstance *instancePtr));
70 static char * GetType _ANSI_ARGS_((char * colorDefn,
71 int * type_ret));
72 static char * GetColor _ANSI_ARGS_((char * colorDefn,
73 char * colorName, int * type_ret));
74
75 /*
76 * Information used for parsing configuration specs:
77 */
78
79 static Tk_ConfigSpec configSpecs[] = {
80 {TK_CONFIG_STRING, "-data", (char *) NULL, (char *) NULL,
81 (char *) NULL, Tk_Offset(PixmapMaster, dataString), TK_CONFIG_NULL_OK},
82 {TK_CONFIG_STRING, "-file", (char *) NULL, (char *) NULL,
83 (char *) NULL, Tk_Offset(PixmapMaster, fileString), TK_CONFIG_NULL_OK},
84 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
85 (char *) NULL, 0, 0}
86 };
87
88 Tk_ImageType imgPixmapImageType = {
89 "pixmap", /* name */
90 (Tk_ImageCreateProc *) ImgXpmCreate,/* createProc */
91 ImgXpmGet, /* getProc */
92 ImgXpmDisplay, /* displayProc */
93 ImgXpmFree, /* freeProc */
94 ImgXpmDelete, /* deleteProc */
95 #ifdef TK_CONFIG_OBJS
96 (Tk_ImagePostscriptProc *) NULL, /* postscriptProc */
97 #endif
98 (Tk_ImageType *) NULL /* nextPtr */
99 };
100
101 /*
102 *----------------------------------------------------------------------
103 *
104 * ImgXpmCreate --
105 *
106 * This procedure is called by the Tk image code to create "pixmap"
107 * images.
108 *
109 * Results:
110 * A standard Tcl result.
111 *
112 * Side effects:
113 * The data structure for a new image is allocated.
114 *
115 *----------------------------------------------------------------------
116 */
117 static int
118 ImgXpmCreate(interp, name, argc, objv, typePtr, master, clientDataPtr)
119 Tcl_Interp *interp; /* Interpreter for application containing
120 * image. */
121 char *name; /* Name to use for image. */
122 int argc; /* Number of arguments. */
123 Tcl_Obj *objv[]; /* Argument strings for options (doesn't
124 * include image name or type). */
125 Tk_ImageType *typePtr; /* Pointer to our type record (not used). */
126 Tk_ImageMaster master; /* Token for image, to be used by us in
127 * later callbacks. */
128 ClientData *clientDataPtr; /* Store manager's token for image here;
129 * it will be returned in later callbacks. */
130 {
131 PixmapMaster *masterPtr;
132 int i;
133 #if 0
134 char *argvbuf[10];
135 char **args = argvbuf;
136
137 /*
138 * Convert the objc/objv arguments into string equivalent.
139 */
140 if (argc > 10) {
141 args = (char **) ckalloc(argc * sizeof(char *));
142 }
143 for (i = 0; i < argc; i++) {
144 args[i] = ImgGetStringFromObj(objv[i], NULL);
145 }
146 #endif
147
148 masterPtr = (PixmapMaster *) ckalloc(sizeof(PixmapMaster));
149 masterPtr->tkMaster = master;
150 masterPtr->interp = interp;
151 masterPtr->imageCmd = Tcl_CreateCommand(interp, name, ImgXpmCmd,
152 (ClientData) masterPtr, ImgXpmCmdDeletedProc);
153
154 masterPtr->fileString = NULL;
155 masterPtr->dataString = NULL;
156 masterPtr->data = NULL;
157 masterPtr->isDataAlloced = 0;
158 masterPtr->instancePtr = NULL;
159
160 if (ImgXpmConfigureMaster(masterPtr, argc, objv, 0) != TCL_OK) {
161 ImgXpmDelete((ClientData) masterPtr);
162 #if 0
163 if (args != argvbuf) {
164 ckfree((char *) args);
165 }
166 #endif
167 return TCL_ERROR;
168 }
169 *clientDataPtr = (ClientData) masterPtr;
170 #if 0
171 if (args != argvbuf) {
172 ckfree((char *) args);
173 }
174 #endif
175 return TCL_OK;
176 }
177
178 /*
179 *----------------------------------------------------------------------
180 *
181 * ImgXpmConfigureMaster --
182 *
183 * This procedure is called when a pixmap image is created or
184 * reconfigured. It process configuration options and resets
185 * any instances of the image.
186 *
187 * Results:
188 * A standard Tcl return value. If TCL_ERROR is returned then
189 * an error message is left in masterPtr->interp->result.
190 *
191 * Side effects:
192 * Existing instances of the image will be redisplayed to match
193 * the new configuration options.
194 *
195 * If any error occurs, the state of *masterPtr is restored to
196 * previous state.
197 *
198 *----------------------------------------------------------------------
199 */
200 static int
201 ImgXpmConfigureMaster(masterPtr, argc, argv, flags)
202 PixmapMaster *masterPtr; /* Pointer to data structure describing
203 * overall pixmap image to (reconfigure). */
204 int argc; /* Number of entries in argv. */
205 char **argv; /* Pairs of configuration options for image. */
206 int flags; /* Flags to pass to Tk_ConfigureWidget,
207 * such as TK_CONFIG_ARGV_ONLY. */
208 {
209 PixmapInstance *instancePtr;
210 char * oldData, * oldFile;
211
212 oldData = masterPtr->dataString;
213 oldFile = masterPtr->fileString;
214
215 if (Tk_ConfigureWidget(masterPtr->interp, Tk_MainWindow(masterPtr->interp),
216 configSpecs, argc, argv, (char *) masterPtr, flags)
217 != TCL_OK) {
218 return TCL_ERROR;
219 }
220
221 if (masterPtr->dataString != NULL ||
222 masterPtr->fileString != NULL) {
223 if (ImgXpmGetData(masterPtr->interp, masterPtr) != TCL_OK) {
224 goto error;
225 }
226 } else {
227 Tcl_AppendResult(masterPtr->interp,
228 "must specify one of -data or -file", NULL);
229 goto error;
230 }
231
232 /*
233 * Cycle through all of the instances of this image, regenerating
234 * the information for each instance. Then force the image to be
235 * redisplayed everywhere that it is used.
236 */
237 for (instancePtr = masterPtr->instancePtr; instancePtr != NULL;
238 instancePtr = instancePtr->nextPtr) {
239 ImgXpmConfigureInstance(instancePtr);
240 }
241
242 if (masterPtr->data) {
243 Tk_ImageChanged(masterPtr->tkMaster, 0, 0,
244 masterPtr->size[0], masterPtr->size[1],
245 masterPtr->size[0], masterPtr->size[1]);
246 } else {
247 Tk_ImageChanged(masterPtr->tkMaster, 0, 0, 0, 0, 0, 0);
248 }
249
250 return TCL_OK;
251
252 error:
253 /* Restore it to the original (possible valid) mode */
254 if (masterPtr->dataString && masterPtr->dataString != oldData) {
255 ckfree(masterPtr->dataString);
256 }
257 if (masterPtr->fileString && masterPtr->fileString != oldFile) {
258 ckfree(masterPtr->fileString);
259 }
260 masterPtr->dataString = oldData;
261 masterPtr->fileString = oldFile;
262 return TCL_ERROR;
263 }
264
265 /*
266 *----------------------------------------------------------------------
267 *
268 * ImgXpmGetData --
269 *
270 * Given a file name or ASCII string, this procedure parses the
271 * file or string contents to produce binary data for a pixmap.
272 *
273 * Results:
274 * If the pixmap description was parsed successfully then the data
275 * is read into an array of strings. This array will later be used
276 * to create X Pixmaps for each instance.
277 *
278 * Side effects:
279 * The masterPtr->data array is allocated when successful. Contents of
280 * *masterPtr is changed only when successful.
281 *----------------------------------------------------------------------
282 */
283 static int
284 ImgXpmGetData(interp, masterPtr)
285 Tcl_Interp *interp; /* For reporting errors. */
286 PixmapMaster *masterPtr;
287 {
288 char ** data = NULL;
289 int isAllocated = 0; /* do we need to free "data"? */
290 int listArgc;
291 char ** listArgv = NULL;
292 int numLines;
293 int size[2];
294 int cpp;
295 int ncolors;
296 int code = TCL_OK;
297
298 if (masterPtr->fileString != NULL) {
299 if (Tcl_IsSafe(interp)) {
300 Tcl_AppendResult(interp, "can't get image from a file in a",
301 " safe interpreter", (char *) NULL);
302 return TCL_ERROR;
303 }
304 data = ImgXpmGetDataFromFile(interp, masterPtr->fileString, &numLines);
305 isAllocated = 1;
306 }
307 else if (masterPtr->dataString != NULL) {
308 data = ImgXpmGetDataFromString(interp,masterPtr->dataString,&numLines);
309 isAllocated = 1;
310 }
311 else {
312 /* Should have been enforced by ImgXpmConfigureMaster() */
313 panic("ImgXpmGetData(): -data and -file are all NULL");
314 }
315
316 if (data == NULL) {
317 /* nothing has been allocated yet. Don't need to goto done */
318 return TCL_ERROR;
319 }
320
321 /* Parse the first line of the data and get info about this pixmap */
322 if (Tcl_SplitList(interp, data[0], &listArgc, &listArgv) != TCL_OK) {
323 code = TCL_ERROR; goto done;
324 }
325
326 if (listArgc < 4) { /* file format error */
327 code = TCL_ERROR; goto done;
328 }
329
330 if (Tcl_GetInt(interp, listArgv[0], &size[0]) != TCL_OK) {
331 code = TCL_ERROR; goto done;
332 }
333 if (Tcl_GetInt(interp, listArgv[1], &size[1]) != TCL_OK) {
334 code = TCL_ERROR; goto done;
335 }
336 if (Tcl_GetInt(interp, listArgv[2], &ncolors) != TCL_OK) {
337 code = TCL_ERROR; goto done;
338 }
339 if (Tcl_GetInt(interp, listArgv[3], &cpp) != TCL_OK) {
340 code = TCL_ERROR; goto done;
341 }
342
343 if (isAllocated) {
344 if (numLines != size[1] + ncolors + 1) {
345 /* the number of lines read from the file/data
346 * is not the same as specified in the data
347 */
348 code = TCL_ERROR; goto done;
349 }
350 }
351
352 done:
353 if (code == TCL_OK) {
354 if (masterPtr->isDataAlloced && masterPtr->data) {
355 ckfree((char*)masterPtr->data);
356 }
357 masterPtr->isDataAlloced = isAllocated;
358 masterPtr->data = data;
359 masterPtr->size[0] = size[0];
360 masterPtr->size[1] = size[1];
361 masterPtr->cpp = cpp;
362 masterPtr->ncolors = ncolors;
363 } else {
364 if (isAllocated && data) {
365 ckfree((char*)data);
366 }
367
368 Tcl_ResetResult(interp);
369 Tcl_AppendResult(interp, "File format error", NULL);
370 }
371
372 if (listArgv) {
373 ckfree((char*)listArgv);
374 }
375
376 return code;
377 }
378
379 static char ** ImgXpmGetDataFromString(interp, string, numLines_return)
380 Tcl_Interp * interp;
381 char * string;
382 int * numLines_return;
383 {
384 int quoted;
385 char * p, * list;
386 int numLines;
387 char ** data;
388
389 /* skip the leading blanks (leading blanks are not defined in the
390 * the XPM definition, but skipping them shouldn't hurt. Also, the ability
391 * to skip the leading blanks is good for using in-line XPM data in TCL
392 * scripts
393 */
394 while (isspace(UCHAR(*string))) {
395 ++ string;
396 }
397
398 /* parse the header */
399 if (strncmp("/* XPM", string, 6) != 0) {
400 goto error;
401 }
402
403 /* strip the comments */
404 for (quoted = 0, p=string; *p;) {
405 if (!quoted) {
406 if (*p == '"') {
407 quoted = 1;
408 ++ p;
409 continue;
410 }
411
412 if (*p == '/' && *(p+1) == '*') {
413 *p++ = ' ';
414 *p++ = ' ';
415 while (1) {
416 if (*p == 0) {
417 break;
418 }
419 if (*p == '*' && *(p+1) == '/') {
420 *p++ = ' ';
421 *p++ = ' ';
422 break;
423 }
424 *p++ = ' ';
425 }
426 continue;
427 }
428 ++ p;
429 } else {
430 if (*p == '"') {
431 quoted = 0;
432 }
433 ++ p;
434 }
435 }
436
437 /* Search for the opening brace */
438 for (p=string; *p;) {
439 if (*p != '{') {
440 ++ p;
441 } else {
442 ++p;
443 break;
444 }
445 }
446
447 /* Change the buffer in to a proper TCL list */
448 quoted = 0;
449 list = p;
450
451 while (*p) {
452 if (!quoted) {
453 if (*p == '"') {
454 quoted = 1;
455 ++ p;
456 continue;
457 }
458
459 if (isspace(UCHAR(*p))) {
460 *p = ' ';
461 }
462 else if (*p == ',') {
463 *p = ' ';
464 }
465 else if (*p == '}') {
466 *p = 0;
467 break;
468 }
469 ++p;
470 }
471 else {
472 if (*p == '"') {
473 quoted = 0;
474 }
475 ++ p;
476 }
477 }
478
479 /* The following code depends on the fact that Tcl_SplitList
480 * strips away double quoates inside a list: ie:
481 * if string == "\"1\" \"2\"" then
482 * list[0] = "1"
483 * list[1] = "2"
484 * and NOT
485 *
486 * list[0] = "\"1\""
487 * list[1] = "\"2\""
488 */
489 if (Tcl_SplitList(interp, list, &numLines, &data) != TCL_OK) {
490 goto error;
491 } else {
492 if (numLines == 0) {
493 /* error: empty data? */
494 if (data != NULL) {
495 ckfree((char*)data);
496 goto error;
497 }
498 }
499 * numLines_return = numLines;
500 return data;
501 }
502
503 error:
504 Tcl_AppendResult(interp, "File format error", NULL);
505 return (char**) NULL;
506 }
507
508 static char ** ImgXpmGetDataFromFile(interp, fileName, numLines_return)
509 Tcl_Interp * interp;
510 char * fileName;
511 int * numLines_return;
512 {
513 Tcl_Channel chan;
514 int size;
515 char ** data = (char **) NULL;
516 char *cmdBuffer = NULL;
517
518 chan = ImgOpenFileChannel(interp, fileName, 0);
519 if (!chan) {
520 return (char **) NULL;
521 }
522
523 size = Tcl_Seek(chan, 0, SEEK_END);
524 if (size > 0) {
525 Tcl_Seek(chan, 0, SEEK_SET);
526 cmdBuffer = (char *) ckalloc(size+1);
527 size = Tcl_Read(chan, cmdBuffer, size);
528 }
529 if (Tcl_Close(interp, chan) != TCL_OK) {
530 goto error;
531 }
532 if (size < 0) {
533 Tcl_AppendResult(interp, fileName, ": ",
534 Tcl_PosixError(interp), (char *)NULL);
535 goto error;
536 }
537 cmdBuffer[size] = 0;
538
539 data = ImgXpmGetDataFromString(interp, cmdBuffer, numLines_return);
540 error:
541 if (cmdBuffer) {
542 ckfree(cmdBuffer);
543 }
544 return data;
545 }
546
547
548 static char *
549 GetType(colorDefn, type_ret)
550 char * colorDefn;
551 int * type_ret;
552 {
553 char * p = colorDefn;
554
555 /* skip white spaces */
556 while (*p && isspace(UCHAR(*p))) {
557 p ++;
558 }
559
560 /* parse the type */
561 if (p[0] != '\0' && p[0] == 'm' &&
562 p[1] != '\0' && isspace(UCHAR(p[1]))) {
563 *type_ret = XPM_MONO;
564 p += 2;
565 }
566 else if (p[0] != '\0' && p[0] == 'g' &&
567 p[1] != '\0' && p[1] == '4' &&
568 p[2] != '\0' && isspace(UCHAR(p[2]))) {
569 *type_ret = XPM_GRAY_4;
570 p += 3;
571 }
572 else if (p[0] != '\0' && p[0] == 'g' &&
573 p[1] != '\0' && isspace(UCHAR(p[1]))) {
574 *type_ret = XPM_GRAY;
575 p += 2;
576 }
577 else if (p[0] != '\0' && p[0] == 'c' &&
578 p[1] != '\0' && isspace(UCHAR(p[1]))) {
579 *type_ret = XPM_COLOR;
580 p += 2;
581 }
582 else if (p[0] != '\0' && p[0] == 's' &&
583 p[1] != '\0' && isspace(UCHAR(p[1]))) {
584 *type_ret = XPM_SYMBOLIC;
585 p += 2;
586 }
587 else {
588 *type_ret = XPM_UNKNOWN;
589 return NULL;
590 }
591
592 return p;
593 }
594
595 /*
596 * colorName is guaranteed to be big enough
597 */
598 static char *
599 GetColor(colorDefn, colorName, type_ret)
600 char * colorDefn;
601 char * colorName; /* if found, name is copied to this array */
602 int * type_ret;
603 {
604 int type;
605 char * p;
606
607 if (!colorDefn) {
608 return NULL;
609 }
610
611 if ((colorDefn = GetType(colorDefn, &type)) == NULL) {
612 /* unknown type */
613 return NULL;
614 }
615 else {
616 *type_ret = type;
617 }
618
619 /* skip white spaces */
620 while (*colorDefn && isspace(UCHAR(*colorDefn))) {
621 colorDefn ++;
622 }
623
624 p = colorName;
625
626 while (1) {
627 int dummy;
628
629 while (*colorDefn && !isspace(UCHAR(*colorDefn))) {
630 *p++ = *colorDefn++;
631 }
632
633 if (!*colorDefn) {
634 break;
635 }
636
637 if (GetType(colorDefn, &dummy) == NULL) {
638 /* the next string should also be considered as a part of a color
639 * name */
640
641 while (*colorDefn && isspace(UCHAR(*colorDefn))) {
642 *p++ = *colorDefn++;
643 }
644 } else {
645 break;
646 }
647 if (!*colorDefn) {
648 break;
649 }
650 }
651
652 /* Mark the end of the colorName */
653 *p = '\0';
654
655 return colorDefn;
656 }
657
658 /*----------------------------------------------------------------------
659 * ImgXpmGetPixmapFromData --
660 *
661 * Creates a pixmap for an image instance.
662 *----------------------------------------------------------------------
663 */
664 static void
665 ImgXpmGetPixmapFromData(interp, masterPtr, instancePtr)
666 Tcl_Interp * interp;
667 PixmapMaster *masterPtr;
668 PixmapInstance *instancePtr;
669 {
670 XImage * image = NULL, * mask = NULL;
671 int depth, i, j, k, lOffset, isTransp = 0, isMono;
672 ColorStruct * colors;
673
674 depth = Tk_Depth(instancePtr->tkwin);
675
676 switch ((Tk_Visual(instancePtr->tkwin))->class) {
677 case StaticGray:
678 case GrayScale:
679 isMono = 1;
680 break;
681 default:
682 isMono = 0;
683 }
684
685 ImgXpmAllocTmpBuffer(masterPtr, instancePtr, &image, &mask);
686
687 /*
688 * Parse the colors
689 */
690 lOffset = 1;
691 colors = (ColorStruct*)ckalloc(sizeof(ColorStruct)*masterPtr->ncolors);
692
693 /*
694 * Initialize the color structures
695 */
696 for (i=0; i<masterPtr->ncolors; i++) {
697 colors[i].colorPtr = NULL;
698 if (masterPtr->cpp == 1) {
699 colors[i].c = 0;
700 } else {
701 colors[i].cstring = (char*)ckalloc(masterPtr->cpp);
702 colors[i].cstring[0] = 0;
703 }
704 }
705
706 for (i=0; i<masterPtr->ncolors; i++) {
707 char * colorDefn; /* the color definition line */
708 char * colorName; /* temp place to hold the color name
709 * defined for one type of visual */
710 char * useName; /* the color name used for this
711 * color. If there are many names
712 * defined, choose the name that is
713 * "best" for the target visual
714 */
715 int found;
716
717 colorDefn = masterPtr->data[i+lOffset]+masterPtr->cpp;
718 colorName = (char*)ckalloc(strlen(colorDefn));
719 useName = (char*)ckalloc(strlen(colorDefn));
720 found = 0;
721
722 while (colorDefn && *colorDefn) {
723 int type;
724
725 if ((colorDefn=GetColor(colorDefn, colorName, &type)) == NULL) {
726 break;
727 }
728 if (colorName[0] == '\0') {
729 continue;
730 }
731
732 switch (type) {
733 case XPM_MONO:
734 if (isMono && depth == 1) {
735 strcpy(useName, colorName);
736 found = 1; goto gotcolor;
737 }
738 break;
739 case XPM_GRAY_4:
740 if (isMono && depth == 4) {
741 strcpy(useName, colorName);
742 found = 1; goto gotcolor;
743 }
744 break;
745 case XPM_GRAY:
746 if (isMono && depth > 4) {
747 strcpy(useName, colorName);
748 found = 1; goto gotcolor;
749 }
750 break;
751 case XPM_COLOR:
752 if (!isMono) {
753 strcpy(useName, colorName);
754 found = 1; goto gotcolor;
755 }
756 break;
757 }
758 if (type != XPM_SYMBOLIC && type != XPM_UNKNOWN) {
759 if (!found) { /* use this color as default */
760 strcpy(useName, colorName);
761 found = 1;
762 }
763 }
764 }
765
766 gotcolor:
767 if (masterPtr->cpp == 1) {
768 colors[i].c = masterPtr->data[i+lOffset][0];
769 } else {
770 strncpy(colors[i].cstring, masterPtr->data[i+lOffset],
771 (size_t)masterPtr->cpp);
772 }
773
774 if (found) {
775 if (strncasecmp(useName, "none", 5) != 0) {
776 colors[i].colorPtr = Tk_GetColor(interp,
777 instancePtr->tkwin, Tk_GetUid(useName));
778 if (colors[i].colorPtr == NULL) {
779 colors[i].colorPtr = Tk_GetColor(interp,
780 instancePtr->tkwin, Tk_GetUid("black"));
781 }
782 }
783 } else {
784 colors[i].colorPtr = Tk_GetColor(interp,
785 instancePtr->tkwin, Tk_GetUid("black"));
786 }
787
788 ckfree(colorName);
789 ckfree(useName);
790 }
791
792 lOffset += masterPtr->ncolors;
793
794 /*
795 * Parse the main body of the image
796 */
797 for (i=0; i<masterPtr->size[1]; i++) {
798 char * p = masterPtr->data[i+lOffset];
799
800 for (j=0; j<masterPtr->size[0]; j++) {
801 if (masterPtr->cpp == 1) {
802 for (k=0; k<masterPtr->ncolors; k++) {
803 if (*p == colors[k].c) {
804 ImgXpmSetPixel(instancePtr, image, mask, j, i,
805 colors[k].colorPtr, &isTransp);
806 break;
807 }
808 }
809 if (*p) {
810 p++;
811 }
812 } else {
813 for (k=0; k<masterPtr->ncolors; k++) {
814 if (strncmp(p, colors[k].cstring,
815 (size_t)masterPtr->cpp) == 0) {
816 ImgXpmSetPixel(instancePtr, image, mask, j, i,
817 colors[k].colorPtr, &isTransp);
818 break;
819 }
820 }
821 for (k=0; *p && k<masterPtr->cpp; k++) {
822 p++;
823 }
824 }
825 }
826 }
827
828 instancePtr->colors = colors;
829
830 ImgXpmRealizePixmap(masterPtr, instancePtr, image, mask, isTransp);
831 ImgXpmFreeTmpBuffer(masterPtr, instancePtr, image, mask);
832 }
833
834 /*
835 *----------------------------------------------------------------------
836 *
837 * ImgXpmConfigureInstance --
838 *
839 * This procedure is called to create displaying information for
840 * a pixmap image instance based on the configuration information
841 * in the master. It is invoked both when new instances are
842 * created and when the master is reconfigured.
843 *
844 * Results:
845 * None.
846 *
847 * Side effects:
848 * Generates errors via Tk_BackgroundError if there are problems
849 * in setting up the instance.
850 *
851 *----------------------------------------------------------------------
852 */
853 static void
854 ImgXpmConfigureInstance(instancePtr)
855 PixmapInstance *instancePtr; /* Instance to reconfigure. */
856 {
857 PixmapMaster *masterPtr = instancePtr->masterPtr;
858
859 if (instancePtr->pixmap != None) {
860 Tk_FreePixmap(Tk_Display(instancePtr->tkwin), instancePtr->pixmap);
861 }
862 ImgXpmFreeInstanceData(instancePtr, 0);
863
864 if (instancePtr->colors != NULL) {
865 int i;
866 for (i=0; i<masterPtr->ncolors; i++) {
867 if (instancePtr->colors[i].colorPtr != NULL) {
868 Tk_FreeColor(instancePtr->colors[i].colorPtr);
869 }
870 if (masterPtr->cpp != 1) {
871 ckfree(instancePtr->colors[i].cstring);
872 }
873 }
874 ckfree((char*)instancePtr->colors);
875 }
876
877 if (Tk_WindowId(instancePtr->tkwin) == None) {
878 Tk_MakeWindowExist(instancePtr->tkwin);
879 }
880
881 /*
882 * Assumption: masterPtr->data is always non NULL (enfored by
883 * ImgXpmConfigureMaster()). Also, the data must be in a valid
884 * format (partially enforced by ImgXpmConfigureMaster(), see comments
885 * inside that function).
886 */
887 ImgXpmGetPixmapFromData(masterPtr->interp, masterPtr, instancePtr);
888 }
889
890 /*
891 *--------------------------------------------------------------
892 *
893 * ImgXpmCmd --
894 *
895 * This procedure is invoked to process the Tcl command
896 * that corresponds to an image managed by this module.
897 * See the user documentation for details on what it does.
898 *
899 * Results:
900 * A standard Tcl result.
901 *
902 * Side effects:
903 * See the user documentation.
904 *
905 *--------------------------------------------------------------
906 */
907
908 static int
909 ImgXpmCmd(clientData, interp, argc, argv)
910 ClientData clientData; /* Information about button widget. */
911 Tcl_Interp *interp; /* Current interpreter. */
912 int argc; /* Number of arguments. */
913 char **argv; /* Argument strings. */
914 {
915 PixmapMaster *masterPtr = (PixmapMaster *) clientData;
916 int c, code;
917 size_t length;
918
919 if (argc < 2) {
920 Tcl_AppendResult(interp, "wrong # args: should be \"",
921 argv[0], " option ?arg arg ...?\"",
922 (char *) NULL);
923 return TCL_ERROR;
924 }
925 c = argv[1][0];
926 length = strlen(argv[1]);
927
928 if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0)
929 && (length >= 2)) {
930 if (argc != 3) {
931 Tcl_AppendResult(interp, "wrong # args: should be \"",
932 argv[0], " cget option\"",
933 (char *) NULL);
934 return TCL_ERROR;
935 }
936 return Tk_ConfigureValue(interp, Tk_MainWindow(interp), configSpecs,
937 (char *) masterPtr, argv[2], 0);
938 } else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
939 && (length >= 2)) {
940 if (argc == 2) {
941 code = Tk_ConfigureInfo(interp, Tk_MainWindow(interp),
942 configSpecs, (char *) masterPtr, (char *) NULL, 0);
943 } else if (argc == 3) {
944 code = Tk_ConfigureInfo(interp, Tk_MainWindow(interp),
945 configSpecs, (char *) masterPtr, argv[2], 0);
946 } else {
947 code = ImgXpmConfigureMaster(masterPtr, argc-2, argv+2,
948 TK_CONFIG_ARGV_ONLY);
949 }
950 return code;
951 } else if ((c == 'r') && (strncmp(argv[1], "refcount", length) == 0)) {
952 /*
953 * The "refcount" command is for debugging only
954 */
955 PixmapInstance *instancePtr;
956 int count = 0;
957 char buff[30];
958
959 if (argc != 1) {
960 Tcl_AppendResult(interp, "wrong # args: should be \"",
961 argv[0], "\"", (char *) NULL);
962 return TCL_ERROR;
963 }
964 for (instancePtr=masterPtr->instancePtr; instancePtr;
965 instancePtr = instancePtr->nextPtr) {
966 count += instancePtr->refCount;
967 }
968 sprintf(buff, "%d", count);
969 Tcl_AppendResult(interp, buff, (char *) NULL);
970 return TCL_OK;
971 } else {
972 Tcl_AppendResult(interp, "bad option \"", argv[1],
973 "\": must be cget, configure or refcount", (char *) NULL);
974 return TCL_ERROR;
975 }
976 }
977
978 /*
979 *----------------------------------------------------------------------
980 *
981 * ImgXpmGet --
982 *
983 * This procedure is called for each use of a pixmap image in a
984 * widget.
985 *
986 * Results:
987 * The return value is a token for the instance, which is passed
988 * back to us in calls to ImgXpmDisplay and ImgXpmFre.
989 *
990 * Side effects:
991 * A data structure is set up for the instance (or, an existing
992 * instance is re-used for the new one).
993 *
994 *----------------------------------------------------------------------
995 */
996
997 static ClientData
998 ImgXpmGet(tkwin, masterData)
999 Tk_Window tkwin; /* Window in which the instance will be
1000 * used. */
1001 ClientData masterData; /* Pointer to our master structure for the
1002 * image. */
1003 {
1004 PixmapMaster *masterPtr = (PixmapMaster *) masterData;
1005 PixmapInstance *instancePtr;
1006
1007 /*
1008 * See if there is already an instance for this window. If so
1009 * then just re-use it.
1010 */
1011
1012 for (instancePtr = masterPtr->instancePtr; instancePtr != NULL;
1013 instancePtr = instancePtr->nextPtr) {
1014 if (instancePtr->tkwin == tkwin) {
1015 instancePtr->refCount++;
1016 return (ClientData) instancePtr;
1017 }
1018 }
1019
1020 /*
1021 * The image isn't already in use in this window. Make a new
1022 * instance of the image.
1023 */
1024 instancePtr = (PixmapInstance *) ckalloc(sizeof(PixmapInstance));
1025 instancePtr->refCount = 1;
1026 instancePtr->masterPtr = masterPtr;
1027 instancePtr->tkwin = tkwin;
1028 instancePtr->pixmap = None;
1029 instancePtr->nextPtr = masterPtr->instancePtr;
1030 instancePtr->colors = NULL;
1031 masterPtr->instancePtr = instancePtr;
1032
1033 ImgInitPixmapInstance(masterPtr, instancePtr);
1034 ImgXpmConfigureInstance(instancePtr);
1035
1036 /*
1037 * If this is the first instance, must set the size of the image.
1038 */
1039 if (instancePtr->nextPtr == NULL) {
1040 if (masterPtr->data) {
1041 Tk_ImageChanged(masterPtr->tkMaster, 0, 0,
1042 masterPtr->size[0], masterPtr->size[1],
1043 masterPtr->size[0], masterPtr->size[1]);
1044 } else {
1045 Tk_ImageChanged(masterPtr->tkMaster, 0, 0, 0, 0, 0, 0);
1046 }
1047 }
1048
1049 return (ClientData) instancePtr;
1050 }
1051
1052 /*
1053 *----------------------------------------------------------------------
1054 *
1055 * ImgXpmDisplay --
1056 *
1057 * This procedure is invoked to draw a pixmap image.
1058 *
1059 * Results:
1060 * None.
1061 *
1062 * Side effects:
1063 * A portion of the image gets rendered in a pixmap or window.
1064 *
1065 *----------------------------------------------------------------------
1066 */
1067
1068 static void
1069 ImgXpmDisplay(clientData, display, drawable, imageX, imageY, width,
1070 height, drawableX, drawableY)
1071 ClientData clientData; /* Pointer to PixmapInstance structure for
1072 * for instance to be displayed. */
1073 Display *display; /* Display on which to draw image. */
1074 Drawable drawable; /* Pixmap or window in which to draw image. */
1075 int imageX, imageY; /* Upper-left corner of region within image
1076 * to draw. */
1077 int width, height; /* Dimensions of region within image to draw.*/
1078 int drawableX, drawableY; /* Coordinates within drawable that
1079 * correspond to imageX and imageY. */
1080 {
1081 ImgpXpmDisplay(clientData, display, drawable, imageX, imageY, width,
1082 height, drawableX, drawableY);
1083 }
1084
1085 /*
1086 *----------------------------------------------------------------------
1087 *
1088 * ImgXpmFree --
1089 *
1090 * This procedure is called when a widget ceases to use a
1091 * particular instance of an image.
1092 *
1093 * Results:
1094 * None.
1095 *
1096 * Side effects:
1097 * Internal data structures get cleaned up.
1098 *
1099 *----------------------------------------------------------------------
1100 */
1101
1102 static void
1103 ImgXpmFree(clientData, display)
1104 ClientData clientData; /* Pointer to PixmapInstance structure for
1105 * for instance to be displayed. */
1106 Display *display; /* Display containing window that used image.*/
1107 {
1108 PixmapInstance *instancePtr = (PixmapInstance *) clientData;
1109 PixmapInstance *prevPtr;
1110
1111 instancePtr->refCount--;
1112 if (instancePtr->refCount > 0) {
1113 return;
1114 }
1115
1116 /*
1117 * There are no more uses of the image within this widget. Free
1118 * the instance structure.
1119 */
1120 if (instancePtr->pixmap != None) {
1121 Tk_FreePixmap(display, instancePtr->pixmap);
1122 }
1123 ImgXpmFreeInstanceData(instancePtr, 1);
1124
1125 if (instancePtr->colors != NULL) {
1126 int i;
1127 for (i=0; i<instancePtr->masterPtr->ncolors; i++) {
1128 if (instancePtr->colors[i].colorPtr != NULL) {
1129 Tk_FreeColor(instancePtr->colors[i].colorPtr);
1130 }
1131 if (instancePtr->masterPtr->cpp != 1) {
1132 ckfree(instancePtr->colors[i].cstring);
1133 }
1134 }
1135 ckfree((char*)instancePtr->colors);
1136 }
1137
1138 if (instancePtr->masterPtr->instancePtr == instancePtr) {
1139 instancePtr->masterPtr->instancePtr = instancePtr->nextPtr;
1140 } else {
1141 for (prevPtr = instancePtr->masterPtr->instancePtr;
1142 prevPtr->nextPtr != instancePtr; prevPtr = prevPtr->nextPtr) {
1143 /* Empty loop body */
1144 }
1145 prevPtr->nextPtr = instancePtr->nextPtr;
1146 }
1147 ckfree((char *) instancePtr);
1148 }
1149
1150 /*
1151 *----------------------------------------------------------------------
1152 *
1153 * ImgXpmDelete --
1154 *
1155 * This procedure is called by the image code to delete the
1156 * master structure for an image.
1157 *
1158 * Results:
1159 * None.
1160 *
1161 * Side effects:
1162 * Resources associated with the image get freed.
1163 *
1164 *----------------------------------------------------------------------
1165 */
1166
1167 static void
1168 ImgXpmDelete(masterData)
1169 ClientData masterData; /* Pointer to PixmapMaster structure for
1170 * image. Must not have any more instances. */
1171 {
1172 PixmapMaster *masterPtr = (PixmapMaster *) masterData;
1173
1174 if (masterPtr->instancePtr != NULL) {
1175 panic("tried to delete pixmap image when instances still exist");
1176 }
1177 masterPtr->tkMaster = NULL;
1178 if (masterPtr->imageCmd != NULL) {
1179 Tcl_DeleteCommand(masterPtr->interp,
1180 Tcl_GetCommandName(masterPtr->interp, masterPtr->imageCmd));
1181 }
1182 if (masterPtr->isDataAlloced && masterPtr->data != NULL) {
1183 ckfree((char*)masterPtr->data);
1184 masterPtr->data = NULL;
1185 }
1186
1187 Tk_FreeOptions(configSpecs, (char *) masterPtr, (Display *) NULL, 0);
1188 ckfree((char *) masterPtr);
1189 }
1190
1191 /*
1192 *----------------------------------------------------------------------
1193 *
1194 * ImgXpmCmdDeletedProc --
1195 *
1196 * This procedure is invoked when the image command for an image
1197 * is deleted. It deletes the image.
1198 *
1199 * Results:
1200 * None.
1201 *
1202 * Side effects:
1203 * The image is deleted.
1204 *
1205 *----------------------------------------------------------------------
1206 */
1207 static void
1208 ImgXpmCmdDeletedProc(clientData)
1209 ClientData clientData; /* Pointer to PixmapMaster structure for
1210 * image. */
1211 {
1212 PixmapMaster *masterPtr = (PixmapMaster *) clientData;
1213
1214 masterPtr->imageCmd = NULL;
1215 if (masterPtr->tkMaster != NULL) {
1216 Tk_DeleteImage(masterPtr->interp, Tk_NameOfImage(masterPtr->tkMaster));
1217 }
1218 }