Debian Codegraph @

pTk/mTk/generic/tkImage.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * tkImage.c --
3 *
4 * This module implements the image protocol, which allows lots
5 * of different kinds of images to be used in lots of different
6 * widgets.
7 *
8 * Copyright (c) 1994 The Regents of the University of California.
9 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
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 * RCS: @(#) $Id: tkImage.c,v 1.19.2.2 2003/09/17 23:45:05 dgp Exp $
15 */
16
17 #include "tkInt.h"
18 #include "tkPort.h"
19
20 /*
21 * Each call to Tk_GetImage returns a pointer to one of the following
22 * structures, which is used as a token by clients (widgets) that
23 * display images.
24 */
25
26 typedef struct Image {
27 Tk_Window tkwin; /* Window passed to Tk_GetImage (needed to
28 * "re-get" the image later if the manager
29 * changes). */
30 Display *display; /* Display for tkwin. Needed because when
31 * the image is eventually freed tkwin may
32 * not exist anymore. */
33 struct ImageMaster *masterPtr;
34 /* Master for this image (identifiers image
35 * manager, for example). */
36 ClientData instanceData;
37 /* One word argument to pass to image manager
38 * when dealing with this image instance. */
39 Tk_ImageChangedProc *changeProc;
40 /* Code in widget to call when image changes
41 * in a way that affects redisplay. */
42 ClientData widgetClientData;
43 /* Argument to pass to changeProc. */
44 struct Image *nextPtr; /* Next in list of all image instances
45 * associated with the same name. */
46
47 } Image;
48
49 /*
50 * For each image master there is one of the following structures,
51 * which represents a name in the image table and all of the images
52 * instantiated from it. Entries in mainPtr->imageTable point to
53 * these structures.
54 */
55
56 typedef struct ImageMaster {
57 Tk_ImageType *typePtr; /* Information about image type. NULL means
58 * that no image manager owns this image: the
59 * image was deleted. */
60 ClientData masterData; /* One-word argument to pass to image mgr
61 * when dealing with the master, as opposed
62 * to instances. */
63 int width, height; /* Last known dimensions for image. */
64 Tcl_HashTable *tablePtr; /* Pointer to hash table containing image
65 * (the imageTable field in some TkMainInfo
66 * structure). */
67 Tcl_HashEntry *hPtr; /* Hash entry in mainPtr->imageTable for
68 * this structure (used to delete the hash
69 * entry). */
70 Image *instancePtr; /* Pointer to first in list of instances
71 * derived from this name. */
72 int deleted; /* Flag set when image is being deleted. */
73 TkWindow *winPtr; /* Main window of interpreter (used to
74 * detect when the world is falling apart.) */
75 } ImageMaster;
76
77 typedef struct ThreadSpecificData {
78 Tk_ImageType *imageTypeList;/* First in a list of all known image
79 * types. */
80 Tk_ImageType *oldImageTypeList;/* First in a list of all known old-style image
81 * types. */
82 } ThreadSpecificData;
83 static Tcl_ThreadDataKey dataKey;
84
85 /*
86 * Prototypes for local procedures:
87 */
88
89 static void DeleteImage _ANSI_ARGS_((ImageMaster *masterPtr));
90 static void EventuallyDeleteImage _ANSI_ARGS_((ImageMaster *masterPtr,
91 int forgetHashEntryNow));
92
93 /*
94 *----------------------------------------------------------------------
95 *
96 * Tk_CreateOldImageType, Tk_CreateImageType --
97 *
98 * This procedure is invoked by an image manager to tell Tk about
99 * a new kind of image and the procedures that manage the new type.
100 * The procedure is typically invoked during Tcl_AppInit.
101 *
102 * Results:
103 * None.
104 *
105 * Side effects:
106 * The new image type is entered into a table used in the "image
107 * create" command.
108 *
109 *----------------------------------------------------------------------
110 */
111
112 void
113 Tk_CreateOldImageType(typePtr)
114 Tk_ImageType *typePtr; /* Structure describing the type. All of
115 * the fields except "nextPtr" must be filled
116 * in by caller. Must not have been passed
117 * to Tk_CreateImageType previously. */
118 {
119 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
120 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
121
122 typePtr->nextPtr = tsdPtr->oldImageTypeList;
123 tsdPtr->oldImageTypeList = typePtr;
124 }
125
126 void
127 Tk_CreateImageType(typePtr)
128 Tk_ImageType *typePtr; /* Structure describing the type. All of
129 * the fields except "nextPtr" must be filled
130 * in by caller. Must not have been passed
131 * to Tk_CreateImageType previously. */
132 {
133 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
134 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
135
136 typePtr->nextPtr = tsdPtr->imageTypeList;
137 tsdPtr->imageTypeList = typePtr;
138 }
139
140 /*
141 *----------------------------------------------------------------------
142 *
143 * Tk_ImageObjCmd --
144 *
145 * This procedure is invoked to process the "image" Tcl command.
146 * See the user documentation for details on what it does.
147 *
148 * Results:
149 * A standard Tcl result.
150 *
151 * Side effects:
152 * See the user documentation.
153 *
154 *----------------------------------------------------------------------
155 */
156
157 int
158 Tk_ImageObjCmd(clientData, interp, objc, objv)
159 ClientData clientData; /* Main window associated with interpreter. */
160 Tcl_Interp *interp; /* Current interpreter. */
161 int objc; /* Number of arguments. */
162 Tcl_Obj *CONST objv[]; /* Argument strings. */
163 {
164 static CONST char *imageOptions[] = {
165 "create", "delete", "height", "inuse", "names", "type", "types",
166 "width", (char *) NULL
167 };
168 enum options {
169 IMAGE_CREATE, IMAGE_DELETE, IMAGE_HEIGHT, IMAGE_INUSE, IMAGE_NAMES,
170 IMAGE_TYPE, IMAGE_TYPES, IMAGE_WIDTH
171 };
172 TkWindow *winPtr = (TkWindow *) clientData;
173 int i, new, firstOption, index;
174 Tk_ImageType *typePtr;
175 ImageMaster *masterPtr;
176 Image *imagePtr;
177 Tcl_HashEntry *hPtr;
178 Tcl_HashSearch search;
179 char idString[16 + TCL_INTEGER_SPACE], *name;
180 TkDisplay *dispPtr = winPtr->dispPtr;
181 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
182 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
183
184 if (objc < 2) {
185 Tcl_WrongNumArgs(interp, 1, objv, "option ?args?");
186 return TCL_ERROR;
187 }
188
189 if (Tcl_GetIndexFromObj(interp, objv[1], imageOptions, "option", 0,
190 &index) != TCL_OK) {
191 return TCL_ERROR;
192 }
193 switch ((enum options) index) {
194 case IMAGE_CREATE: {
195 char *arg;
196 Tcl_Obj **args;
197 int oldimage = 0;
198 if (objc < 3) {
199 Tcl_WrongNumArgs(interp, 2, objv, "type ?name? ?options?");
200 return TCL_ERROR;
201 }
202
203 /*
204 * Look up the image type.
205 */
206
207 arg = Tcl_GetString(objv[2]);
208 for (typePtr = tsdPtr->imageTypeList; typePtr != NULL;
209 typePtr = typePtr->nextPtr) {
210 if ((*arg == typePtr->name[0])
211 && (strcmp(arg, typePtr->name) == 0)) {
212 break;
213 }
214 }
215 if (typePtr == NULL) {
216 oldimage = 1;
217 for (typePtr = tsdPtr->oldImageTypeList; typePtr != NULL;
218 typePtr = typePtr->nextPtr) {
219 if ((*arg == typePtr->name[0])
220 && (strcmp(arg, typePtr->name) == 0)) {
221 break;
222 }
223 }
224 }
225 if (typePtr == NULL) {
226 Tcl_AppendResult(interp, "image type \"", arg,
227 "\" doesn't exist", (char *) NULL);
228 return TCL_ERROR;
229 }
230
231 /*
232 * Figure out a name to use for the new image.
233 */
234
235 if ((objc == 3) || (*(arg = Tcl_GetString(objv[3])) == '-')) {
236 Tcl_CmdInfo dummy;
237 do {
238 dispPtr->imageId++;
239 sprintf(idString, "image%d", dispPtr->imageId);
240 name = idString;
241 } while (Tcl_GetCommandInfo(interp, name, &dummy) != 0);
242 firstOption = 3;
243 } else {
244 TkWindow *topWin;
245
246 name = arg;
247 firstOption = 4;
248 /*
249 * Need to check if the _command_ that we are about to
250 * create is the name of the current master widget
251 * command (normally "." but could have been renamed)
252 * and fail in that case before a really nasty and
253 * hard to stop crash happens.
254 */
255 topWin = (TkWindow *) TkToplevelWindowForCommand(interp, name);
256 if (topWin != NULL && winPtr->mainPtr->winPtr == topWin) {
257 Tcl_AppendResult(interp, "images may not be named the ",
258 "same as the main window", (char *) NULL);
259 return TCL_ERROR;
260 }
261 }
262
263 /*
264 * Create the data structure for the new image.
265 */
266
267 hPtr = Tcl_CreateHashEntry(&winPtr->mainPtr->imageTable,
268 name, &new);
269 if (new) {
270 masterPtr = (ImageMaster *) ckalloc(sizeof(ImageMaster));
271 masterPtr->typePtr = NULL;
272 masterPtr->masterData = NULL;
273 masterPtr->width = masterPtr->height = 1;
274 masterPtr->tablePtr = &winPtr->mainPtr->imageTable;
275 masterPtr->hPtr = hPtr;
276 masterPtr->instancePtr = NULL;
277 masterPtr->deleted = 0;
278 masterPtr->winPtr = winPtr->mainPtr->winPtr;
279 Tcl_Preserve((ClientData) masterPtr->winPtr);
280 Tcl_SetHashValue(hPtr, masterPtr);
281 } else {
282 /*
283 * An image already exists by this name. Disconnect the
284 * instances from the master.
285 */
286
287 masterPtr = (ImageMaster *) Tcl_GetHashValue(hPtr);
288 if (masterPtr->typePtr != NULL) {
289 for (imagePtr = masterPtr->instancePtr; imagePtr != NULL;
290 imagePtr = imagePtr->nextPtr) {
291 (*masterPtr->typePtr->freeProc)(
292 imagePtr->instanceData, imagePtr->display);
293 (*imagePtr->changeProc)(imagePtr->widgetClientData,
294 0, 0, masterPtr->width, masterPtr->height,
295 masterPtr->width, masterPtr->height);
296 }
297 (*masterPtr->typePtr->deleteProc)(masterPtr->masterData);
298 masterPtr->typePtr = NULL;
299 }
300 }
301
302 /*
303 * Call the image type manager so that it can perform its own
304 * initialization, then re-"get" for any existing instances of
305 * the image.
306 */
307
308 objv += firstOption;
309 objc -= firstOption;
310 args = (Tcl_Obj **) objv;
311 if (oldimage) {
312 int i;
313 args = (Tcl_Obj **) ckalloc((objc+1) * sizeof(char *));
314 for (i = 0; i < objc; i++) {
315 args[i] = (Tcl_Obj *) Tcl_GetString(objv[i]);
316 }
317 args[objc] = NULL;
318 }
319 Tcl_Preserve((ClientData) masterPtr);
320 if ((*typePtr->createProc)(interp, name, objc,
321 args, typePtr, (Tk_ImageMaster) masterPtr,
322 &masterPtr->masterData) != TCL_OK) {
323 EventuallyDeleteImage(masterPtr, 0);
324 Tcl_Release((ClientData) masterPtr);
325 if (oldimage) {
326 ckfree((char *) args);
327 }
328 return TCL_ERROR;
329 }
330 Tcl_Release((ClientData) masterPtr);
331 if (oldimage) {
332 ckfree((char *) args);
333 }
334 masterPtr->typePtr = typePtr;
335 for (imagePtr = masterPtr->instancePtr; imagePtr != NULL;
336 imagePtr = imagePtr->nextPtr) {
337 imagePtr->instanceData = (*typePtr->getProc)(
338 imagePtr->tkwin, masterPtr->masterData);
339 }
340 Tcl_SetObjResult(interp, LangObjectObj( interp, Tcl_GetHashKey(&winPtr->mainPtr->imageTable, hPtr)));
341 break;
342 }
343 case IMAGE_DELETE: {
344 for (i = 2; i < objc; i++) {
345 char *arg = Tcl_GetString(objv[i]);
346 hPtr = Tcl_FindHashEntry(&winPtr->mainPtr->imageTable, arg);
347 if (hPtr == NULL) {
348 Tcl_AppendResult(interp, "image \"", arg,
349 "\" doesn't exist", (char *) NULL);
350 return TCL_ERROR;
351 }
352 DeleteImage((ImageMaster *) Tcl_GetHashValue(hPtr));
353 }
354 break;
355 }
356 case IMAGE_HEIGHT: {
357 char *arg;
358 if (objc != 3) {
359 Tcl_WrongNumArgs(interp, 2, objv, "name");
360 return TCL_ERROR;
361 }
362 arg = Tcl_GetString(objv[2]);
363 hPtr = Tcl_FindHashEntry(&winPtr->mainPtr->imageTable, arg);
364 if (hPtr == NULL) {
365 Tcl_AppendResult(interp, "image \"", arg,
366 "\" doesn't exist", (char *) NULL);
367 return TCL_ERROR;
368 }
369 masterPtr = (ImageMaster *) Tcl_GetHashValue(hPtr);
370 Tcl_SetIntObj(Tcl_GetObjResult(interp), masterPtr->height);
371 break;
372 }
373
374 case IMAGE_INUSE: {
375 int count = 0;
376 char *arg;
377 if (objc != 3) {
378 Tcl_WrongNumArgs(interp, 2, objv, "name");
379 return TCL_ERROR;
380 }
381 arg = Tcl_GetString(objv[2]);
382 hPtr = Tcl_FindHashEntry(&winPtr->mainPtr->imageTable, arg);
383 if (hPtr == NULL) {
384 Tcl_AppendResult(interp, "image \"", arg,
385 "\" doesn't exist", (char *) NULL);
386 return TCL_ERROR;
387 }
388 masterPtr = (ImageMaster *) Tcl_GetHashValue(hPtr);
389 if (masterPtr->typePtr != NULL && masterPtr->instancePtr != NULL) {
390 count = 1;
391 }
392 Tcl_SetBooleanObj(Tcl_GetObjResult(interp), count);
393 break;
394 }
395
396 case IMAGE_NAMES: {
397 if (objc != 2) {
398 Tcl_WrongNumArgs(interp, 2, objv, NULL);
399 return TCL_ERROR;
400 }
401 hPtr = Tcl_FirstHashEntry(&winPtr->mainPtr->imageTable, &search);
402 for ( ; hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
403 Tcl_ListObjAppendElement(interp, Tcl_GetObjResult(interp),
404 LangObjectObj(interp,Tcl_GetHashKey(&winPtr->mainPtr->imageTable, hPtr)));
405 }
406 break;
407 }
408
409 case IMAGE_TYPE: {
410 char *arg;
411 if (objc != 3) {
412 Tcl_WrongNumArgs(interp, 2, objv, "name");
413 return TCL_ERROR;
414 }
415 arg = Tcl_GetString(objv[2]);
416 hPtr = Tcl_FindHashEntry(&winPtr->mainPtr->imageTable, arg);
417 if (hPtr == NULL) {
418 Tcl_AppendResult(interp, "image \"", arg,
419 "\" doesn't exist", (char *) NULL);
420 return TCL_ERROR;
421 }
422 masterPtr = (ImageMaster *) Tcl_GetHashValue(hPtr);
423 if (masterPtr->typePtr != NULL) {
424 Tcl_SetResult(interp, masterPtr->typePtr->name, TCL_STATIC);
425 }
426 break;
427 }
428 case IMAGE_TYPES: {
429 if (objc != 2) {
430 Tcl_WrongNumArgs(interp, 2, objv, NULL);
431 return TCL_ERROR;
432 }
433 for (typePtr = tsdPtr->imageTypeList; typePtr != NULL;
434 typePtr = typePtr->nextPtr) {
435 Tcl_AppendElement(interp, typePtr->name);
436 }
437 for (typePtr = tsdPtr->oldImageTypeList; typePtr != NULL;
438 typePtr = typePtr->nextPtr) {
439 Tcl_AppendElement(interp, typePtr->name);
440 }
441 break;
442 }
443 case IMAGE_WIDTH: {
444 char *arg;
445 if (objc != 3) {
446 Tcl_WrongNumArgs(interp, 2, objv, "name");
447 return TCL_ERROR;
448 }
449 arg = Tcl_GetString(objv[2]);
450 hPtr = Tcl_FindHashEntry(&winPtr->mainPtr->imageTable, arg);
451 if (hPtr == NULL) {
452 Tcl_AppendResult(interp, "image \"", arg,
453 "\" doesn't exist", (char *) NULL);
454 return TCL_ERROR;
455 }
456 masterPtr = (ImageMaster *) Tcl_GetHashValue(hPtr);
457 Tcl_SetIntObj(Tcl_GetObjResult(interp), masterPtr->width);
458 break;
459 }
460 }
461 return TCL_OK;
462 }
463
464 /*
465 *----------------------------------------------------------------------
466 *
467 * Tk_ImageChanged --
468 *
469 * This procedure is called by an image manager whenever something
470 * has happened that requires the image to be redrawn (some of its
471 * pixels have changed, or its size has changed).
472 *
473 * Results:
474 * None.
475 *
476 * Side effects:
477 * Any widgets that display the image are notified so that they
478 * can redisplay themselves as appropriate.
479 *
480 *----------------------------------------------------------------------
481 */
482
483 void
484 Tk_ImageChanged(imageMaster, x, y, width, height, imageWidth,
485 imageHeight)
486 Tk_ImageMaster imageMaster; /* Image that needs redisplay. */
487 int x, y; /* Coordinates of upper-left pixel of
488 * region of image that needs to be
489 * redrawn. */
490 int width, height; /* Dimensions (in pixels) of region of
491 * image to redraw. If either dimension
492 * is zero then the image doesn't need to
493 * be redrawn (perhaps all that happened is
494 * that its size changed). */
495 int imageWidth, imageHeight;/* New dimensions of image. */
496 {
497 ImageMaster *masterPtr = (ImageMaster *) imageMaster;
498 Image *imagePtr;
499
500 masterPtr->width = imageWidth;
501 masterPtr->height = imageHeight;
502 for (imagePtr = masterPtr->instancePtr; imagePtr != NULL;
503 imagePtr = imagePtr->nextPtr) {
504 (*imagePtr->changeProc)(imagePtr->widgetClientData, x, y,
505 width, height, imageWidth, imageHeight);
506 }
507 }
508
509 /*
510 *----------------------------------------------------------------------
511 *
512 * Tk_NameOfImage --
513 *
514 * Given a token for an image master, this procedure returns
515 * the name of the image.
516 *
517 * Results:
518 * The return value is the string name for imageMaster.
519 *
520 * Side effects:
521 * None.
522 *
523 *----------------------------------------------------------------------
524 */
525
526 CONST char *
527 Tk_NameOfImage(imageMaster)
528 Tk_ImageMaster imageMaster; /* Token for image. */
529 {
530 ImageMaster *masterPtr = (ImageMaster *) imageMaster;
531
532 if (masterPtr->hPtr == NULL) {
533 return NULL;
534 }
535 return Tcl_GetHashKey(masterPtr->tablePtr, masterPtr->hPtr);
536 }
537
538 /*
539 *----------------------------------------------------------------------
540 *
541 * Tk_GetImage --
542 *
543 * This procedure is invoked by a widget when it wants to use
544 * a particular image in a particular window.
545 *
546 * Results:
547 * The return value is a token for the image. If there is no image
548 * by the given name, then NULL is returned and an error message is
549 * left in the interp's result.
550 *
551 * Side effects:
552 * Tk records the fact that the widget is using the image, and
553 * it will invoke changeProc later if the widget needs redisplay
554 * (i.e. its size changes or some of its pixels change). The
555 * caller must eventually invoke Tk_FreeImage when it no longer
556 * needs the image.
557 *
558 *----------------------------------------------------------------------
559 */
560
561 Tk_Image
562 Tk_GetImage(interp, tkwin, name, changeProc, clientData)
563 Tcl_Interp *interp; /* Place to leave error message if image
564 * can't be found. */
565 Tk_Window tkwin; /* Token for window in which image will
566 * be used. */
567 CONST char *name; /* Name of desired image. */
568 Tk_ImageChangedProc *changeProc;
569 /* Procedure to invoke when redisplay is
570 * needed because image's pixels or size
571 * changed. */
572 ClientData clientData; /* One-word argument to pass to damageProc. */
573 {
574 Tcl_HashEntry *hPtr;
575 ImageMaster *masterPtr;
576 Image *imagePtr;
577
578 hPtr = Tcl_FindHashEntry(&((TkWindow *) tkwin)->mainPtr->imageTable, name);
579 if (hPtr == NULL) {
580 goto noSuchImage;
581 }
582 masterPtr = (ImageMaster *) Tcl_GetHashValue(hPtr);
583 if (masterPtr->typePtr == NULL) {
584 goto noSuchImage;
585 }
586 imagePtr = (Image *) ckalloc(sizeof(Image));
587 imagePtr->tkwin = tkwin;
588 imagePtr->display = Tk_Display(tkwin);
589 imagePtr->masterPtr = masterPtr;
590 imagePtr->instanceData =
591 (*masterPtr->typePtr->getProc)(tkwin, masterPtr->masterData);
592 imagePtr->changeProc = changeProc;
593 imagePtr->widgetClientData = clientData;
594 imagePtr->nextPtr = masterPtr->instancePtr;
595 masterPtr->instancePtr = imagePtr;
596 return (Tk_Image) imagePtr;
597
598 noSuchImage:
599 Tcl_AppendResult(interp, "image \"", name, "\" doesn't exist",
600 (char *) NULL);
601 return NULL;
602 }
603
604 /*
605 *----------------------------------------------------------------------
606 *
607 * Tk_FreeImage --
608 *
609 * This procedure is invoked by a widget when it no longer needs
610 * an image acquired by a previous call to Tk_GetImage. For each
611 * call to Tk_GetImage there must be exactly one call to Tk_FreeImage.
612 *
613 * Results:
614 * None.
615 *
616 * Side effects:
617 * The association between the image and the widget is removed.
618 *
619 *----------------------------------------------------------------------
620 */
621
622 void
623 Tk_FreeImage(image)
624 Tk_Image image; /* Token for image that is no longer
625 * needed by a widget. */
626 {
627 Image *imagePtr = (Image *) image;
628 ImageMaster *masterPtr = imagePtr->masterPtr;
629 Image *prevPtr;
630
631 /*
632 * Clean up the particular instance.
633 */
634
635 if (masterPtr->typePtr != NULL) {
636 (*masterPtr->typePtr->freeProc)(imagePtr->instanceData,
637 imagePtr->display);
638 }
639 prevPtr = masterPtr->instancePtr;
640 if (prevPtr == imagePtr) {
641 masterPtr->instancePtr = imagePtr->nextPtr;
642 } else {
643 while (prevPtr->nextPtr != imagePtr) {
644 prevPtr = prevPtr->nextPtr;
645 }
646 prevPtr->nextPtr = imagePtr->nextPtr;
647 }
648 ckfree((char *) imagePtr);
649
650 /*
651 * If there are no more instances left for the master, and if the
652 * master image has been deleted, then delete the master too.
653 */
654
655 if ((masterPtr->typePtr == NULL) && (masterPtr->instancePtr == NULL)) {
656 if (masterPtr->hPtr != NULL) {
657 Tcl_DeleteHashEntry(masterPtr->hPtr);
658 }
659 Tcl_Release(masterPtr->winPtr);
660 ckfree((char *) masterPtr);
661 }
662 }
663
664 /*
665 *----------------------------------------------------------------------
666 *
667 * Tk_PostscriptImage --
668 *
669 * This procedure is called by widgets that contain images in order
670 * to redisplay an image on the screen or an off-screen pixmap.
671 *
672 * Results:
673 * None.
674 *
675 * Side effects:
676 * The image's manager is notified, and it redraws the desired
677 * portion of the image before returning.
678 *
679 *----------------------------------------------------------------------
680 */
681
682 int
683 Tk_PostscriptImage(image, interp, tkwin, psinfo, x, y, width, height, prepass)
684 Tk_Image image; /* Token for image to redisplay. */
685 Tcl_Interp *interp;
686 Tk_Window tkwin;
687 Tk_PostscriptInfo psinfo; /* postscript info */
688 int x, y; /* Upper-left pixel of region in image that
689 * needs to be redisplayed. */
690 int width, height; /* Dimensions of region to redraw. */
691 int prepass;
692 {
693 Image *imagePtr = (Image *) image;
694 int result;
695 XImage *ximage;
696 Pixmap pmap;
697 GC newGC;
698 XGCValues gcValues;
699
700 if (imagePtr->masterPtr->typePtr == NULL) {
701 /*
702 * No master for image, so nothing to display on postscript.
703 */
704 return TCL_OK;
705 }
706
707 /*
708 * Check if an image specific postscript-generation function
709 * exists; otherwise go on with generic code.
710 */
711
712 if (imagePtr->masterPtr->typePtr->postscriptProc != NULL) {
713 return (*imagePtr->masterPtr->typePtr->postscriptProc)(
714 imagePtr->masterPtr->masterData, interp, tkwin, psinfo,
715 x, y, width, height, prepass);
716 }
717
718 if (prepass) {
719 return TCL_OK;
720 }
721
722 /*
723 * Create a Pixmap, tell the image to redraw itself there, and then
724 * generate an XImage from the Pixmap. We can then read pixel
725 * values out of the XImage.
726 */
727
728 pmap = Tk_GetPixmap(Tk_Display(tkwin), Tk_WindowId(tkwin),
729 width, height, Tk_Depth(tkwin));
730
731 gcValues.foreground = WhitePixelOfScreen(Tk_Screen(tkwin));
732 newGC = Tk_GetGC(tkwin, GCForeground, &gcValues);
733 if (newGC != None) {
734 XFillRectangle(Tk_Display(tkwin), pmap, newGC,
735 0, 0, (unsigned int)width, (unsigned int)height);
736 Tk_FreeGC(Tk_Display(tkwin), newGC);
737 }
738
739 Tk_RedrawImage(image, x, y, width, height, pmap, 0, 0);
740
741 ximage = XGetImage(Tk_Display(tkwin), pmap, 0, 0,
742 (unsigned int)width, (unsigned int)height, AllPlanes, ZPixmap);
743
744 Tk_FreePixmap(Tk_Display(tkwin), pmap);
745
746 if (ximage == NULL) {
747 /* The XGetImage() function is apparently not
748 * implemented on this system. Just ignore it.
749 */
750 return TCL_OK;
751 }
752 result = TkPostscriptImage(interp, tkwin, psinfo, ximage, x, y,
753 width, height);
754
755 XDestroyImage(ximage);
756 return result;
757 }
758
759 /*
760 *----------------------------------------------------------------------
761 *
762 * Tk_RedrawImage --
763 *
764 * This procedure is called by widgets that contain images in order
765 * to redisplay an image on the screen or an off-screen pixmap.
766 *
767 * Results:
768 * None.
769 *
770 * Side effects:
771 * The image's manager is notified, and it redraws the desired
772 * portion of the image before returning.
773 *
774 *----------------------------------------------------------------------
775 */
776
777 void
778 Tk_RedrawImage(image, imageX, imageY, width, height, drawable,
779 drawableX, drawableY)
780 Tk_Image image; /* Token for image to redisplay. */
781 int imageX, imageY; /* Upper-left pixel of region in image that
782 * needs to be redisplayed. */
783 int width, height; /* Dimensions of region to redraw. */
784 Drawable drawable; /* Drawable in which to display image
785 * (window or pixmap). If this is a pixmap,
786 * it must have the same depth as the window
787 * used in the Tk_GetImage call for the
788 * image. */
789 int drawableX, drawableY; /* Coordinates in drawable that correspond
790 * to imageX and imageY. */
791 {
792 Image *imagePtr = (Image *) image;
793
794 if (imagePtr->masterPtr->typePtr == NULL) {
795 /*
796 * No master for image, so nothing to display.
797 */
798
799 return;
800 }
801
802 /*
803 * Clip the redraw area to the area of the image.
804 */
805
806 if (imageX < 0) {
807 width += imageX;
808 drawableX -= imageX;
809 imageX = 0;
810 }
811 if (imageY < 0) {
812 height += imageY;
813 drawableY -= imageY;
814 imageY = 0;
815 }
816 if ((imageX + width) > imagePtr->masterPtr->width) {
817 width = imagePtr->masterPtr->width - imageX;
818 }
819 if ((imageY + height) > imagePtr->masterPtr->height) {
820 height = imagePtr->masterPtr->height - imageY;
821 }
822 (*imagePtr->masterPtr->typePtr->displayProc)(
823 imagePtr->instanceData, imagePtr->display, drawable,
824 imageX, imageY, width, height, drawableX, drawableY);
825 }
826
827 /*
828 *----------------------------------------------------------------------
829 *
830 * Tk_SizeOfImage --
831 *
832 * This procedure returns the current dimensions of an image.
833 *
834 * Results:
835 * The width and height of the image are returned in *widthPtr
836 * and *heightPtr.
837 *
838 * Side effects:
839 * None.
840 *
841 *----------------------------------------------------------------------
842 */
843
844 void
845 Tk_SizeOfImage(image, widthPtr, heightPtr)
846 Tk_Image image; /* Token for image whose size is wanted. */
847 int *widthPtr; /* Return width of image here. */
848 int *heightPtr; /* Return height of image here. */
849 {
850 Image *imagePtr = (Image *) image;
851
852 *widthPtr = imagePtr->masterPtr->width;
853 *heightPtr = imagePtr->masterPtr->height;
854 }
855
856 /*
857 *----------------------------------------------------------------------
858 *
859 * Tk_DeleteImage --
860 *
861 * Given the name of an image, this procedure destroys the
862 * image.
863 *
864 * Results:
865 * None.
866 *
867 * Side effects:
868 * The image is destroyed; existing instances will display as
869 * blank areas. If no such image exists then the procedure does
870 * nothing.
871 *
872 *----------------------------------------------------------------------
873 */
874
875 void
876 Tk_DeleteImage(interp, name)
877 Tcl_Interp *interp; /* Interpreter in which the image was
878 * created. */
879 CONST char *name; /* Name of image. */
880 {
881 Tcl_HashEntry *hPtr;
882 TkWindow *winPtr;
883
884 winPtr = (TkWindow *) Tk_MainWindow(interp);
885 if (winPtr == NULL) {
886 return;
887 }
888 hPtr = Tcl_FindHashEntry(&winPtr->mainPtr->imageTable, name);
889 if (hPtr == NULL) {
890 return;
891 }
892 DeleteImage((ImageMaster *)Tcl_GetHashValue(hPtr));
893 }
894
895 /*
896 *----------------------------------------------------------------------
897 *
898 * DeleteImage --
899 *
900 * This procedure is responsible for deleting an image.
901 *
902 * Results:
903 * None.
904 *
905 * Side effects:
906 * The connection is dropped between instances of this image and
907 * an image master. Image instances will redisplay themselves
908 * as empty areas, but existing instances will not be deleted.
909 *
910 *----------------------------------------------------------------------
911 */
912
913 static void
914 DeleteImage(masterPtr)
915 ImageMaster *masterPtr; /* Pointer to main data structure for image. */
916 {
917 Image *imagePtr;
918 Tk_ImageType *typePtr;
919
920 typePtr = masterPtr->typePtr;
921 masterPtr->typePtr = NULL;
922 if (typePtr != NULL) {
923 for (imagePtr = masterPtr->instancePtr; imagePtr != NULL;
924 imagePtr = imagePtr->nextPtr) {
925 (*typePtr->freeProc)(imagePtr->instanceData,
926 imagePtr->display);
927 (*imagePtr->changeProc)(imagePtr->widgetClientData, 0, 0,
928 masterPtr->width, masterPtr->height, masterPtr->width,
929 masterPtr->height);
930 }
931 (*typePtr->deleteProc)(masterPtr->masterData);
932 }
933 if (masterPtr->instancePtr == NULL) {
934 if (masterPtr->hPtr != NULL) {
935 Tcl_DeleteHashEntry(masterPtr->hPtr);
936 }
937 Tcl_Release((ClientData) masterPtr->winPtr);
938 ckfree((char *) masterPtr);
939 }
940 }
941
942 /*
943 *----------------------------------------------------------------------
944 *
945 * EventuallyDeleteImage --
946 *
947 * Arrange for an image to be deleted when it is safe to do so.
948 *
949 * Results:
950 * None.
951 *
952 * Side effects:
953 * Image will get freed, though not until it is no longer
954 * Tcl_Preserve()d by anything. May be called multiple times on
955 * the same image without ill effects.
956 *
957 *----------------------------------------------------------------------
958 */
959
960 static void
961 EventuallyDeleteImage(masterPtr, forgetHashEntryNow)
962 ImageMaster *masterPtr; /* Pointer to main data structure for image. */
963 int forgetHashEntryNow;
964 {
965 if (forgetHashEntryNow) {
966 masterPtr->hPtr = NULL;
967 }
968 if (!masterPtr->deleted) {
969 masterPtr->deleted = 1;
970 Tcl_EventuallyFree((ClientData) masterPtr,
971 (Tcl_FreeProc *)DeleteImage);
972 }
973 }
974
975 /*
976 *----------------------------------------------------------------------
977 *
978 * TkDeleteAllImages --
979 *
980 * This procedure is called when an application is deleted. It
981 * calls back all of the managers for all images so that they
982 * can cleanup, then it deletes all of Tk's internal information
983 * about images.
984 *
985 * Results:
986 * None.
987 *
988 * Side effects:
989 * All information for all images gets deleted.
990 *
991 *----------------------------------------------------------------------
992 */
993
994 void
995 TkDeleteAllImages(mainPtr)
996 TkMainInfo *mainPtr; /* Structure describing application that is
997 * going away. */
998 {
999 Tcl_HashSearch search;
1000 Tcl_HashEntry *hPtr;
1001
1002 for (hPtr = Tcl_FirstHashEntry(&mainPtr->imageTable, &search);
1003 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
1004 EventuallyDeleteImage((ImageMaster *) Tcl_GetHashValue(hPtr), 1);
1005 }
1006 Tcl_DeleteHashTable(&mainPtr->imageTable);
1007 }
1008
1009 /*
1010 *----------------------------------------------------------------------
1011 *
1012 * Tk_GetImageMasterData --
1013 *
1014 * Given the name of an image, this procedure returns the type
1015 * of the image and the clientData associated with its master.
1016 *
1017 * Results:
1018 * If there is no image by the given name, then NULL is returned
1019 * and a NULL value is stored at *typePtrPtr. Otherwise the return
1020 * value is the clientData returned by the createProc when the
1021 * image was created and a pointer to the type structure for the
1022 * image is stored at *typePtrPtr.
1023 *
1024 * Side effects:
1025 * None.
1026 *
1027 *----------------------------------------------------------------------
1028 */
1029
1030 ClientData
1031 Tk_GetImageMasterData(interp, name, typePtrPtr)
1032 Tcl_Interp *interp; /* Interpreter in which the image was
1033 * created. */
1034 CONST char *name; /* Name of image. */
1035 Tk_ImageType **typePtrPtr; /* Points to location to fill in with
1036 * pointer to type information for image. */
1037 {
1038 Tcl_HashEntry *hPtr;
1039 TkWindow *winPtr;
1040 ImageMaster *masterPtr;
1041
1042 winPtr = (TkWindow *) Tk_MainWindow(interp);
1043 hPtr = Tcl_FindHashEntry(&winPtr->mainPtr->imageTable, name);
1044 if (hPtr == NULL) {
1045 *typePtrPtr = NULL;
1046 return NULL;
1047 }
1048 masterPtr = (ImageMaster *) Tcl_GetHashValue(hPtr);
1049 *typePtrPtr = masterPtr->typePtr;
1050 return masterPtr->masterData;
1051 }
1052
1053
1054 /*========================================================================
1055 * Tile support - reimplemented avoiding BLTisms
1056 *========================================================================*/
1057
1058 typedef struct Tk_TileChange_
1059 {
1060 struct Tk_TileChange_ *next;
1061 Tk_TileChangedProc *changeProc;
1062 ClientData clientData;
1063 } Tk_TileChange;
1064
1065 struct Tk_Tile_
1066 {
1067 Tk_Image image;
1068 int width;
1069 int height;
1070 Tk_Window tkwin;
1071 Pixmap pixmap;
1072 Tk_TileChange *handlers;
1073 };
1074
1075 static void TileImageChanged _ANSI_ARGS_((ClientData clientData,
1076 int x, int y, int width, int height,
1077 int imgWidth, int imgHeight));
1078
1079 void
1080 TileImageChanged(clientData, x, y, width, height, imgWidth, imgHeight)
1081 ClientData clientData;
1082 int x;
1083 int y;
1084 int width;
1085 int height;
•1086 int imgWidth;
1087 int imgHeight;
1088 {
1089 Tk_Tile tile = (Tk_Tile) clientData;
1090 Tk_TileChange *handler;
1091 if (tile->pixmap == None ||
1092 tile->width != imgWidth || tile->height != imgHeight) {
1093 Tk_Window tkwin = tile->tkwin;
1094 if (tile->pixmap) {
1095 Tk_FreePixmap(Tk_Display(tkwin),tile->pixmap);
1096 tile->width = 0;
1097 tile->height = 0;
1098 tile->pixmap = None;
1099 }
1100 if (imgWidth >= 0 && imgHeight >= 0) {
1101 tile->pixmap = Tk_GetPixmap(Tk_Display(tkwin), Tk_WindowId(tkwin),
1102 imgWidth, imgHeight, Tk_Depth(tkwin));
1103 if (tile->pixmap) {
1104 tile->width = imgWidth;
1105 tile->height = imgHeight;
1106 }
1107 }
1108 }
1109 if (tile->pixmap != None) {
1110 Tk_RedrawImage(tile->image, 0, 0, tile->width, tile->height,
1111 tile->pixmap, 0, 0);
1112 }
1113 for (handler = tile->handlers; handler != NULL; handler = handler->next) {
1114 (*handler->changeProc)(handler->clientData, tile);
1115
1116 }
1117 }
1118
1119 /*
1120 *----------------------------------------------------------------------
1121 *
1122 * Tk_GetTile
1123 *
1124 * Convert the named image into a tile.
1125 *
1126 * Results:
1127 * If the image is valid, a new tile is returned. If the name
1128 * does not represent a proper image, an error message is left in
1129 * interp->result.
1130 *
1131 * Side Effects:
1132 * Memory and X resources are allocated. Bookkeeping is
1133 * maintained on the tile (i.e. width, height, and name).
1134 *
1135 *----------------------------------------------------------------------
1136 */
1137 Tk_Tile
1138 Tk_GetTile(interp, tkwin, imageName)
1139 Tcl_Interp *interp; /* Interpreter to report results back to */
1140 Tk_Window tkwin; /* Window on the same display as tile */
1141 CONST char *imageName; /* Name of image */
1142 {
1143 Tk_Tile tile = (Tk_Tile) ckalloc(sizeof(struct Tk_Tile_));
1144 memset(tile,0,sizeof(*tile));
1145 tile->tkwin = tkwin;
1146 tile->image = Tk_GetImage(interp, tkwin, (char *) imageName,
1147 TileImageChanged, (ClientData) tile);
1148 if (!tile->image) {
1149 Tk_FreeTile(tile);
1150 return NULL;
1151 }
1152 return tile;
1153 }
1154
1155 /*
1156 *----------------------------------------------------------------------
1157 *
1158 * Tk_FreeTile
1159 *
1160 * Release the resources associated with the tile.
1161 *
1162 * Results:
1163 * None.
1164 *
1165 * Side Effects:
1166 * Memory and X resources are freed. Bookkeeping information
1167 * about the tile (i.e. width, height, and name) is discarded.
1168 *
1169 */
1170 void
1171 Tk_FreeTile(tile)
1172 Tk_Tile tile; /* Tile to be deleted */
1173 {
1174 if (tile->image)
1175 Tk_FreeImage(tile->image);
1176 if (tile->pixmap)
1177 Tk_FreePixmap(Tk_Display(tile->tkwin),tile->pixmap);
1178 while (tile->handlers) {
1179 Tk_TileChange *handler = tile->handlers;
1180 tile->handlers = handler->next;
1181 ckfree((char *) handler);
1182 }
1183 ckfree((char *) tile);
1184 }
1185
1186 /*
1187 *----------------------------------------------------------------------
1188 *
1189 * Tk_NameOfTile
1190 *
1191 * Returns the name of the image from which the tile was
1192 * generated.
1193 *
1194 * Results:
1195 * The name of the image is returned. The name is not unique.
1196 * Many tiles may use the same image.
1197 *
1198 *----------------------------------------------------------------------
1199 */
1200 CONST char *
1201 Tk_NameOfTile(tile)
1202 Tk_Tile tile; /* Tile to query */
1203 {
1204 if (tile)
1205 {
1206 Image *imagePtr = (Image *) tile->image;
1207 return Tk_NameOfImage((Tk_ImageMaster)(imagePtr->masterPtr));
1208 }
1209 return NULL;
1210 }
1211
1212 /*
1213 *----------------------------------------------------------------------
1214 *
1215 * Tk_PixmapOfTile
1216 *
1217 * Returns the pixmap of the tile.
1218 *
1219 * Results:
1220 * The X pixmap used as the tile is returned.
1221 *
1222 *----------------------------------------------------------------------
1223 */
1224 Pixmap
1225 Tk_PixmapOfTile(tile)
1226 Tk_Tile tile; /* Tile to query */
1227 {
1228 if (tile) {
1229 if (tile->image && tile->pixmap == None) {
1230 Tk_Window tkwin = tile->tkwin;
1231 int width = 0;
1232 int height = 0;
1233 Tk_SizeOfImage(tile->image,&width,&height);
1234 if (width >= 0 && height >= 0) {
1235 Tk_MakeWindowExist(tkwin);
1236 tile->pixmap = Tk_GetPixmap(Tk_Display(tkwin), Tk_WindowId(tkwin),
1237 width, height, Tk_Depth(tkwin));
1238 if (tile->pixmap != None) {
1239 tile->width = width;
1240 tile->height = height;
1241 Tk_RedrawImage(tile->image, 0, 0, width, height, tile->pixmap, 0, 0);
1242 }
1243 }
1244 }
1245 return tile->pixmap;
1246 }
1247 return None;
1248 }
1249
1250 /*
1251 *----------------------------------------------------------------------
1252 *
1253 * Tk_SizeOfTile
1254 *
1255 * Returns the width and height of the tile.
1256 *
1257 * Results:
1258 * The width and height of the tile are returned.
1259 *
1260 *----------------------------------------------------------------------
1261 */
1262 void
1263 Tk_SizeOfTile(tile, widthPtr, heightPtr)
1264 Tk_Tile tile; /* Tile to query */
1265 int *widthPtr, *heightPtr; /* Returned dimensions of the tile (out) */
1266 {
1267 if (tile) {
1268 if (tile->image && tile->pixmap == None) {
1269 Tk_SizeOfImage(tile->image,widthPtr,heightPtr);
1270 }
1271 else {
1272 *widthPtr = tile->height;
1273 *heightPtr = tile->width;
1274 }
1275 }
1276 else {
1277 *widthPtr = 0;
1278 *heightPtr = 0;
1279 }
1280 }
1281
1282 /*
1283 *----------------------------------------------------------------------
1284 *
1285 * Tk_SetTileChangedProc
1286 *
1287 * Sets the routine to called when an image changes. If
1288 * *changeProc* is NULL, no callback will be performed.
1289 *
1290 * Results:
1291 * None.
1292 *
1293 * Side Effects:
1294 * The designated routine will be called the next time the
1295 * image associated with the tile changes.
1296 *
1297 *----------------------------------------------------------------------
1298 */
1299 void
1300 Tk_SetTileChangedProc(tile, changeProc, clientData)
1301 Tk_Tile tile; /* Tile to query */
1302 Tk_TileChangedProc *changeProc;
1303 ClientData clientData;
1304 {
1305 if (tile) {
1306 Tk_TileChange **link = &tile->handlers;
1307 Tk_TileChange *handler;
1308
1309 while ((handler = *link)) {
1310 if (handler->clientData == clientData) {
1311 break;
1312 }
1313 link = &handler->next;
1314 }
1315 if (changeProc == NULL ) {
1316 if (handler) {
1317 *link = handler->next;
1318 ckfree((char *) handler);
1319 }
1320 }
1321 else {
1322 if (handler == NULL) {
1323 handler = (Tk_TileChange *) ckalloc(sizeof(Tk_TileChange));
1324 memset(handler,0,sizeof(*handler));
1325 handler->next = NULL;
1326 *link = handler;
1327 handler->clientData = clientData;
1328 }
1329 handler->changeProc = changeProc;
1330 }
1331 }
1332 }
1333
1334
1335 /*
1336 *----------------------------------------------------------------------
1337 *
1338 * Tk_SetTSOrigin --
1339 *
1340 * Set the pattern origin of the tile to a common point (i.e. the
1341 * origin (0,0) of the top level window) so that tiles from two
1342 * different widgets will match up. This done by setting the
1343 * GCTileStipOrigin field is set to the translated origin of the
1344 * toplevel window in the hierarchy.
1345 *
1346 * Results:
1347 * None.
1348 *
1349 * Side Effects:
1350 * The GCTileStipOrigin is reset in the GC. This will cause the
1351 * tile origin to change when the GC is used for drawing.
1352 *
1353 *----------------------------------------------------------------------
1354 */
1355 /*ARGSUSED*/
1356 void
1357 Tk_SetTSOrigin(tkwin, gc, x, y)
1358 Tk_Window tkwin;
1359 GC gc;
1360 int x, y;
1361 {
1362 while (!Tk_TopWinHierarchy(tkwin)) {
1363 x -= Tk_X(tkwin) + Tk_Changes(tkwin)->border_width;
1364 y -= Tk_Y(tkwin) + Tk_Changes(tkwin)->border_width;
1365 tkwin = Tk_Parent(tkwin);
1366 }
1367 XSetTSOrigin(Tk_Display(tkwin), gc, x, y);
1368 }
1369
1370