Debian Codegraph @

pTk/mTk/tixGeneric/tixImgXpm.c

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