Debian Codegraph @

pTk/mTk/generic/tkTextImage.c

raw symbols SCIP
1 /*
2 * tkImage.c --
3 *
4 * This file contains code that allows images to be
5 * nested inside text widgets. It also implements the "image"
6 * widget command for texts.
7 *
8 * Copyright (c) 1997 Sun Microsystems, Inc.
9 *
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 *
13 * RCS: @(#) $Id: tkTextImage.c,v 1.5 2002/08/05 04:30:40 dgp Exp $
14 */
15
16 #include "tk.h"
17 #include "tkText.h"
18 #include "tkPort.h"
19
20 /*
21 * Definitions for alignment values:
22 */
23
24 #define ALIGN_BOTTOM 0
25 #define ALIGN_CENTER 1
26 #define ALIGN_TOP 2
27 #define ALIGN_BASELINE 3
28
29 /*
30 * Macro that determines the size of an embedded image segment:
31 */
32
33 #define EI_SEG_SIZE ((unsigned) (Tk_Offset(TkTextSegment, body) \
34 + sizeof(TkTextEmbImage)))
35
36 /*
37 * Prototypes for procedures defined in this file:
38 */
39
40 static int AlignParseProc _ANSI_ARGS_((ClientData clientData,
41 Tcl_Interp *interp, Tk_Window tkwin,
42 Tcl_Obj *value, char *widgRec, int offset));
43 static Tcl_Obj * AlignPrintProc _ANSI_ARGS_((ClientData clientData,
44 Tk_Window tkwin, char *widgRec, int offset,
45 Tcl_FreeProc **freeProcPtr));
46 static TkTextSegment * EmbImageCleanupProc _ANSI_ARGS_((TkTextSegment *segPtr,
47 TkTextLine *linePtr));
48 static void EmbImageCheckProc _ANSI_ARGS_((TkTextSegment *segPtr,
49 TkTextLine *linePtr));
50 static void EmbImageBboxProc _ANSI_ARGS_((TkTextDispChunk *chunkPtr,
51 int index, int y, int lineHeight, int baseline,
52 int *xPtr, int *yPtr, int *widthPtr,
53 int *heightPtr));
54 static int EmbImageConfigure _ANSI_ARGS_((TkText *textPtr,
55 TkTextSegment *eiPtr, int argc, char **argv));
56 static int EmbImageDeleteProc _ANSI_ARGS_((TkTextSegment *segPtr,
57 TkTextLine *linePtr, int treeGone));
58 static void EmbImageDisplayProc _ANSI_ARGS_((
59 TkTextDispChunk *chunkPtr, int x, int y,
60 int lineHeight, int baseline, Display *display,
61 Drawable dst, int screenY));
62 static int EmbImageLayoutProc _ANSI_ARGS_((TkText *textPtr,
63 TkTextIndex *indexPtr, TkTextSegment *segPtr,
64 int offset, int maxX, int maxChars,
65 int noCharsYet, TkWrapMode wrapMode,
66 TkTextDispChunk *chunkPtr));
67 static void EmbImageProc _ANSI_ARGS_((ClientData clientData,
68 int x, int y, int width, int height,
69 int imageWidth, int imageHeight));
70
71 /*
72 * The following structure declares the "embedded image" segment type.
73 */
74
75 static Tk_SegType tkTextEmbImageType = {
76 "image", /* name */
77 0, /* leftGravity */
78 (Tk_SegSplitProc *) NULL, /* splitProc */
79 EmbImageDeleteProc, /* deleteProc */
80 EmbImageCleanupProc, /* cleanupProc */
81 (Tk_SegLineChangeProc *) NULL, /* lineChangeProc */
82 EmbImageLayoutProc, /* layoutProc */
83 EmbImageCheckProc /* checkProc */
84 };
85
86 /*
87 * Information used for parsing image configuration options:
88 */
89
90 static Tk_CustomOption alignOption = {AlignParseProc, AlignPrintProc,
91 (ClientData) NULL};
92
93 static Tk_ConfigSpec configSpecs[] = {
94 {TK_CONFIG_CUSTOM, "-align", (char *) NULL, (char *) NULL,
95 "center", 0, TK_CONFIG_DONT_SET_DEFAULT, &alignOption},
96 {TK_CONFIG_PIXELS, "-padx", (char *) NULL, (char *) NULL,
97 "0", Tk_Offset(TkTextEmbImage, padX),
98 TK_CONFIG_DONT_SET_DEFAULT},
99 {TK_CONFIG_PIXELS, "-pady", (char *) NULL, (char *) NULL,
100 "0", Tk_Offset(TkTextEmbImage, padY),
101 TK_CONFIG_DONT_SET_DEFAULT},
102 {TK_CONFIG_STRING, "-image", (char *) NULL, (char *) NULL,
103 (char *) NULL, Tk_Offset(TkTextEmbImage, imageString),
104 TK_CONFIG_DONT_SET_DEFAULT|TK_CONFIG_NULL_OK},
105 {TK_CONFIG_STRING, "-name", (char *) NULL, (char *) NULL,
106 (char *) NULL, Tk_Offset(TkTextEmbImage, imageName),
107 TK_CONFIG_DONT_SET_DEFAULT|TK_CONFIG_NULL_OK},
108 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
109 (char *) NULL, 0, 0}
110 };
111
112 /*
113 *--------------------------------------------------------------
114 *
115 * TkTextImageCmd --
116 *
117 * This procedure implements the "image" widget command
118 * for text widgets. See the user documentation for details
119 * on what it does.
120 *
121 * Results:
122 * A standard Tcl result or error.
123 *
124 * Side effects:
125 * See the user documentation.
126 *
127 *--------------------------------------------------------------
128 */
129
130 int
131 TkTextImageCmd(textPtr, interp, argc, argv)
132 register TkText *textPtr; /* Information about text widget. */
133 Tcl_Interp *interp; /* Current interpreter. */
134 int argc; /* Number of arguments. */
135 char **argv; /* Argument strings. Someone else has already
136 * parsed this command enough to know that
137 * argv[1] is "image". */
138 {
139 size_t length;
140 register TkTextSegment *eiPtr;
141
142 if (argc < 3) {
143 Tcl_AppendResult(interp, "wrong # args: should be \"",
144 argv[0], " image option ?arg arg ...?\"", (char *) NULL);
145 return TCL_ERROR;
146 }
147 length = strlen(argv[2]);
148 if ((strncmp(argv[2], "cget", length) == 0) && (length >= 2)) {
149 TkTextIndex index;
150 TkTextSegment *eiPtr;
151
152 if (argc != 5) {
153 Tcl_AppendResult(interp, "wrong # args: should be \"",
154 argv[0], " image cget index option\"",
155 (char *) NULL);
156 return TCL_ERROR;
157 }
158 if (TkTextGetIndex(interp, textPtr, argv[3], &index) != TCL_OK) {
159 return TCL_ERROR;
160 }
161 eiPtr = TkTextIndexToSeg(&index, (int *) NULL);
162 if (eiPtr->typePtr != &tkTextEmbImageType) {
163 Tcl_AppendResult(interp, "no embedded image at index \"",
164 argv[3], "\"", (char *) NULL);
165 return TCL_ERROR;
166 }
167 return Tk_ConfigureValue(interp, textPtr->tkwin, configSpecs,
168 (char *) &eiPtr->body.ei, argv[4], 0);
169 } else if ((strncmp(argv[2], "configure", length) == 0) && (length >= 2)) {
170 TkTextIndex index;
171 TkTextSegment *eiPtr;
172
173 if (argc < 4) {
174 Tcl_AppendResult(interp, "wrong # args: should be \"",
175 argv[0], " image configure index ?option value ...?\"",
176 (char *) NULL);
177 return TCL_ERROR;
178 }
179 if (TkTextGetIndex(interp, textPtr, argv[3], &index) != TCL_OK) {
180 return TCL_ERROR;
181 }
182 eiPtr = TkTextIndexToSeg(&index, (int *) NULL);
183 if (eiPtr->typePtr != &tkTextEmbImageType) {
184 Tcl_AppendResult(interp, "no embedded image at index \"",
185 argv[3], "\"", (char *) NULL);
186 return TCL_ERROR;
187 }
188 if (argc == 4) {
189 return Tk_ConfigureInfo(interp, textPtr->tkwin, configSpecs,
190 (char *) &eiPtr->body.ei, (char *) NULL, 0);
191 } else if (argc == 5) {
192 return Tk_ConfigureInfo(interp, textPtr->tkwin, configSpecs,
193 (char *) &eiPtr->body.ei, argv[4], 0);
194 } else {
195 TkTextChanged(textPtr, &index, &index);
196 return EmbImageConfigure(textPtr, eiPtr, argc-4, argv+4);
197 }
198 } else if ((strncmp(argv[2], "create", length) == 0) && (length >= 2)) {
199 TkTextIndex index;
200 int lineIndex;
201
202 /*
203 * Add a new image. Find where to put the new image, and
204 * mark that position for redisplay.
205 */
206
207 if (argc < 4) {
208 Tcl_AppendResult(interp, "wrong # args: should be \"",
209 argv[0], " image create index ?option value ...?\"",
210 (char *) NULL);
211 return TCL_ERROR;
212 }
213 if (TkTextGetIndex(interp, textPtr, argv[3], &index) != TCL_OK) {
214 return TCL_ERROR;
215 }
216
217 /*
218 * Don't allow insertions on the last (dummy) line of the text.
219 */
220
221 lineIndex = TkBTreeLineIndex(index.linePtr);
222 if (lineIndex == TkBTreeNumLines(textPtr->tree)) {
223 lineIndex--;
224 TkTextMakeByteIndex(textPtr->tree, lineIndex, 1000000, &index);
225 }
226
227 /*
228 * Create the new image segment and initialize it.
229 */
230
231 eiPtr = (TkTextSegment *) ckalloc(EI_SEG_SIZE);
232 eiPtr->typePtr = &tkTextEmbImageType;
233 eiPtr->size = 1;
234 eiPtr->body.ei.textPtr = textPtr;
235 eiPtr->body.ei.linePtr = NULL;
236 eiPtr->body.ei.imageName = NULL;
237 eiPtr->body.ei.imageString = NULL;
238 eiPtr->body.ei.name = NULL;
239 eiPtr->body.ei.image = NULL;
240 eiPtr->body.ei.align = ALIGN_CENTER;
241 eiPtr->body.ei.padX = eiPtr->body.ei.padY = 0;
242 eiPtr->body.ei.chunkCount = 0;
243
244 /*
245 * Link the segment into the text widget, then configure it (delete
246 * it again if the configuration fails).
247 */
248
249 TkTextChanged(textPtr, &index, &index);
250 TkBTreeLinkSegment(eiPtr, &index);
251 if (EmbImageConfigure(textPtr, eiPtr, argc-4, argv+4) != TCL_OK) {
252 TkTextIndex index2;
253
254 TkTextIndexForwChars(&index, 1, &index2);
255 TkBTreeDeleteChars(&index, &index2);
256 return TCL_ERROR;
257 }
258 } else if (strncmp(argv[2], "names", length) == 0) {
259 Tcl_HashSearch search;
260 Tcl_HashEntry *hPtr;
261
262 if (argc != 3) {
263 Tcl_AppendResult(interp, "wrong # args: should be \"",
264 argv[0], " image names\"", (char *) NULL);
265 return TCL_ERROR;
266 }
267 for (hPtr = Tcl_FirstHashEntry(&textPtr->imageTable, &search);
268 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
269 Tcl_AppendElement(interp,
270 Tcl_GetHashKey(&textPtr->markTable, hPtr));
271 }
272 } else {
273 Tcl_AppendResult(interp, "bad image option \"", argv[2],
274 "\": must be cget, configure, create, or names",
275 (char *) NULL);
276 return TCL_ERROR;
277 }
278 return TCL_OK;
279 }
280
281 /*
282 *--------------------------------------------------------------
283 *
284 * EmbImageConfigure --
285 *
286 * This procedure is called to handle configuration options
287 * for an embedded image, using an argc/argv list.
288 *
289 * Results:
290 * The return value is a standard Tcl result. If TCL_ERROR is
291 * returned, then the interp's result contains an error message..
292 *
293 * Side effects:
294 * Configuration information for the embedded image changes,
295 * such as alignment, or name of the image.
296 *
297 *--------------------------------------------------------------
298 */
299
300 static int
301 EmbImageConfigure(textPtr, eiPtr, argc, argv)
302 TkText *textPtr; /* Information about text widget that
303 * contains embedded image. */
304 TkTextSegment *eiPtr; /* Embedded image to be configured. */
305 int argc; /* Number of strings in argv. */
306 char **argv; /* Array of strings describing configuration
307 * options. */
308 {
309 Tk_Image image;
310 Tcl_DString newName;
311 Tcl_HashEntry *hPtr;
312 Tcl_HashSearch search;
313 int new;
314 char *name;
315 int count = 0; /* The counter for picking a unique name */
316 int conflict = 0; /* True if we have a name conflict */
317 unsigned int len; /* length of image name */
318
319 if (Tk_ConfigureWidget(textPtr->interp, textPtr->tkwin, configSpecs,
320 argc, argv, (char *) &eiPtr->body.ei,TK_CONFIG_ARGV_ONLY)
321 != TCL_OK) {
322 return TCL_ERROR;
323 }
324
325 /*
326 * Create the image. Save the old image around and don't free it
327 * until after the new one is allocated. This keeps the reference
328 * count from going to zero so the image doesn't have to be recreated
329 * if it hasn't changed.
330 */
331
332 if (eiPtr->body.ei.imageString != NULL) {
333 image = Tk_GetImage(textPtr->interp, textPtr->tkwin, eiPtr->body.ei.imageString,
334 EmbImageProc, (ClientData) eiPtr);
335 if (image == NULL) {
336 return TCL_ERROR;
337 }
338 } else {
339 image = NULL;
340 }
341 if (eiPtr->body.ei.image != NULL) {
342 Tk_FreeImage(eiPtr->body.ei.image);
343 }
344 eiPtr->body.ei.image = image;
345
346 if (eiPtr->body.ei.name != NULL) {
347 return TCL_OK;
348 }
349
350 /*
351 * Find a unique name for this image. Use imageName (or imageString)
352 * if available, otherwise tack on a #nn and use it. If a name is already
353 * associated with this image, delete the name.
354 */
355
356 name = eiPtr->body.ei.imageName;
357 if (name == NULL) {
358 name = eiPtr->body.ei.imageString;
359 }
360 if (name == NULL) {
361 Tcl_AppendResult(textPtr->interp,"Either a \"-name\" ",
362 "or a \"-image\" argument must be provided ",
363 "to the \"image create\" subcommand.",
364 (char *) NULL);
365 return TCL_ERROR;
366 }
367 len = strlen(name);
368 for (hPtr = Tcl_FirstHashEntry(&textPtr->imageTable, &search);
369 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
370 char *haveName = Tcl_GetHashKey(&textPtr->imageTable, hPtr);
371 if (strncmp(name, haveName, len) == 0) {
372 new = 0;
373 sscanf(haveName+len,"#%d",&new);
374 if (new > count) {
375 count = new;
376 }
377 if (len == strlen(haveName)) {
378 conflict = 1;
379 }
380 }
381 }
382
383 Tcl_DStringInit(&newName);
384 Tcl_DStringAppend(&newName,name, -1);
385
386 if (conflict) {
387 char buf[4 + TCL_INTEGER_SPACE];
388 sprintf(buf, "#%d",count+1);
389 Tcl_DStringAppend(&newName,buf, -1);
390 }
391 name = Tcl_DStringValue(&newName);
392 hPtr = Tcl_CreateHashEntry(&textPtr->imageTable, name, &new);
393 Tcl_SetHashValue(hPtr, eiPtr);
394 Tcl_AppendResult(textPtr->interp, name , (char *) NULL);
395 eiPtr->body.ei.name = ckalloc((unsigned) Tcl_DStringLength(&newName)+1);
396 strcpy(eiPtr->body.ei.name,name);
397 Tcl_DStringFree(&newName);
398
399 return TCL_OK;
400 }
401
402 /*
403 *--------------------------------------------------------------
404 *
405 * AlignParseProc --
406 *
407 * This procedure is invoked by Tk_ConfigureWidget during
408 * option processing to handle "-align" options for embedded
409 * images.
410 *
411 * Results:
412 * A standard Tcl return value.
413 *
414 * Side effects:
415 * The alignment for the embedded image may change.
416 *
417 *--------------------------------------------------------------
418 */
419
420 /* ARGSUSED */
421 static int
422 AlignParseProc(clientData, interp, tkwin, valuearg, widgRec, offset)
423 ClientData clientData; /* Not used.*/
424 Tcl_Interp *interp; /* Used for reporting errors. */
425 Tk_Window tkwin; /* Window for text widget. */
426 Tcl_Obj *valuearg; /* Value of option. */
427 char *widgRec; /* Pointer to TkTextEmbWindow
428 * structure. */
429 int offset; /* Offset into item (ignored). */
430 {
431 char *value = Tcl_GetString(valuearg);
432 register TkTextEmbImage *embPtr = (TkTextEmbImage *) widgRec;
433
434 if (strcmp(value, "baseline") == 0) {
435 embPtr->align = ALIGN_BASELINE;
436 } else if (strcmp(value, "bottom") == 0) {
437 embPtr->align = ALIGN_BOTTOM;
438 } else if (strcmp(value, "center") == 0) {
439 embPtr->align = ALIGN_CENTER;
440 } else if (strcmp(value, "top") == 0) {
441 embPtr->align = ALIGN_TOP;
442 } else {
443 Tcl_AppendResult(interp, "bad alignment \"", value,
444 "\": must be baseline, bottom, center, or top",
445 (char *) NULL);
446 return TCL_ERROR;
447 }
448 return TCL_OK;
449 }
450
451 /*
452 *--------------------------------------------------------------
453 *
454 * AlignPrintProc --
455 *
456 * This procedure is invoked by the Tk configuration code
457 * to produce a printable string for the "-align" configuration
458 * option for embedded images.
459 *
460 * Results:
461 * The return value is a string describing the embedded
462 * images's current alignment.
463 *
464 * Side effects:
465 * None.
466 *
467 *--------------------------------------------------------------
468 */
469
470 /* ARGSUSED */
471 Tcl_Obj *
472 AlignPrintProc(clientData, tkwin, widgRec, offset, freeProcPtr)
473 ClientData clientData; /* Ignored. */
474 Tk_Window tkwin; /* Window for text widget. */
475 char *widgRec; /* Pointer to TkTextEmbImage
476 * structure. */
477 int offset; /* Ignored. */
478 Tcl_FreeProc **freeProcPtr; /* Pointer to variable to fill in with
479 * information about how to reclaim
480 * storage for return string. */
481 {
482 Tcl_Obj * result = NULL;
483 *freeProcPtr = (Tcl_FreeProc *) NULL;
484 switch (((TkTextEmbImage *) widgRec)->align) {
485 case ALIGN_BASELINE:
486 LangSetDefault(&result,"baseline");
487 return result;
488 case ALIGN_BOTTOM:
489 LangSetDefault(&result,"bottom");
490 return result;
491 case ALIGN_CENTER:
492 LangSetDefault(&result,"center");
493 return result;
494 case ALIGN_TOP:
495 LangSetDefault(&result,"top");
496 return result;
497 default:
498 LangSetDefault(&result,"");
499 return result;
500 }
501 }
502
503 /*
504 *--------------------------------------------------------------
505 *
506 * EmbImageDeleteProc --
507 *
508 * This procedure is invoked by the text B-tree code whenever
509 * an embedded image lies in a range of characters being deleted.
510 *
511 * Results:
512 * Returns 0 to indicate that the deletion has been accepted.
513 *
514 * Side effects:
515 * The embedded image is deleted, if it exists, and any resources
516 * associated with it are released.
517 *
518 *--------------------------------------------------------------
519 */
520
521 /* ARGSUSED */
522 static int
523 EmbImageDeleteProc(eiPtr, linePtr, treeGone)
524 TkTextSegment *eiPtr; /* Segment being deleted. */
525 TkTextLine *linePtr; /* Line containing segment. */
526 int treeGone; /* Non-zero means the entire tree is
527 * being deleted, so everything must
528 * get cleaned up. */
529 {
530 Tcl_HashEntry *hPtr;
531
532 if (eiPtr->body.ei.image != NULL) {
533 hPtr = Tcl_FindHashEntry(&eiPtr->body.ei.textPtr->imageTable,
534 eiPtr->body.ei.name);
535 if (hPtr != NULL) {
536 /*
537 * (It's possible for there to be no hash table entry for this
538 * image, if an error occurred while creating the image segment
539 * but before the image got added to the table)
540 */
541
542 Tcl_DeleteHashEntry(hPtr);
543 }
544 Tk_FreeImage(eiPtr->body.ei.image);
545 }
546 Tk_FreeOptions(configSpecs, (char *) &eiPtr->body.ei,
547 eiPtr->body.ei.textPtr->display, 0);
548 if (eiPtr->body.ei.name != NULL) {
549 ckfree(eiPtr->body.ei.name);
550 }
551 ckfree((char *) eiPtr);
552 return 0;
553 }
554
555 /*
556 *--------------------------------------------------------------
557 *
558 * EmbImageCleanupProc --
559 *
560 * This procedure is invoked by the B-tree code whenever a
561 * segment containing an embedded image is moved from one
562 * line to another.
563 *
564 * Results:
565 * None.
566 *
567 * Side effects:
568 * The linePtr field of the segment gets updated.
569 *
570 *--------------------------------------------------------------
571 */
572
573 static TkTextSegment *
574 EmbImageCleanupProc(eiPtr, linePtr)
575 TkTextSegment *eiPtr; /* Mark segment that's being moved. */
576 TkTextLine *linePtr; /* Line that now contains segment. */
577 {
578 eiPtr->body.ei.linePtr = linePtr;
579 return eiPtr;
580 }
581
582 /*
583 *--------------------------------------------------------------
584 *
585 * EmbImageLayoutProc --
586 *
587 * This procedure is the "layoutProc" for embedded image
588 * segments.
589 *
590 * Results:
591 * 1 is returned to indicate that the segment should be
592 * displayed. The chunkPtr structure is filled in.
593 *
594 * Side effects:
595 * None, except for filling in chunkPtr.
596 *
597 *--------------------------------------------------------------
598 */
599
600 /*ARGSUSED*/
601 static int
602 EmbImageLayoutProc(textPtr, indexPtr, eiPtr, offset, maxX, maxChars,
603 noCharsYet, wrapMode, chunkPtr)
604 TkText *textPtr; /* Text widget being layed out. */
605 TkTextIndex *indexPtr; /* Identifies first character in chunk. */
606 TkTextSegment *eiPtr; /* Segment corresponding to indexPtr. */
607 int offset; /* Offset within segPtr corresponding to
608 * indexPtr (always 0). */
609 int maxX; /* Chunk must not occupy pixels at this
610 * position or higher. */
611 int maxChars; /* Chunk must not include more than this
612 * many characters. */
613 int noCharsYet; /* Non-zero means no characters have been
614 * assigned to this line yet. */
615 TkWrapMode wrapMode; /* Wrap mode to use for line: TEXT_WRAPMODE_CHAR,
616 * TEXT_WRAPMODE_NONE, or TEXT_WRAPMODE_WORD. */
617 register TkTextDispChunk *chunkPtr;
618 /* Structure to fill in with information
619 * about this chunk. The x field has already
620 * been set by the caller. */
621 {
622 int width, height;
623
624 if (offset != 0) {
625 panic("Non-zero offset in EmbImageLayoutProc");
626 }
627
628 /*
629 * See if there's room for this image on this line.
630 */
631
632 if (eiPtr->body.ei.image == NULL) {
633 width = 0;
634 height = 0;
635 } else {
636 Tk_SizeOfImage(eiPtr->body.ei.image, &width, &height);
637 width += 2*eiPtr->body.ei.padX;
638 height += 2*eiPtr->body.ei.padY;
639 }
640 if ((width > (maxX - chunkPtr->x))
641 && !noCharsYet && (textPtr->wrapMode != TEXT_WRAPMODE_NONE)) {
642 return 0;
643 }
644
645 /*
646 * Fill in the chunk structure.
647 */
648
649 chunkPtr->displayProc = EmbImageDisplayProc;
650 chunkPtr->undisplayProc = (Tk_ChunkUndisplayProc *) NULL;
651 chunkPtr->measureProc = (Tk_ChunkMeasureProc *) NULL;
652 chunkPtr->bboxProc = EmbImageBboxProc;
653 chunkPtr->numBytes = 1;
654 if (eiPtr->body.ei.align == ALIGN_BASELINE) {
655 chunkPtr->minAscent = height - eiPtr->body.ei.padY;
656 chunkPtr->minDescent = eiPtr->body.ei.padY;
657 chunkPtr->minHeight = 0;
658 } else {
659 chunkPtr->minAscent = 0;
660 chunkPtr->minDescent = 0;
661 chunkPtr->minHeight = height;
662 }
663 chunkPtr->width = width;
664 chunkPtr->breakIndex = -1;
665 chunkPtr->breakIndex = 1;
666 chunkPtr->clientData = (ClientData) eiPtr;
667 eiPtr->body.ei.chunkCount += 1;
668 return 1;
669 }
670
671 /*
672 *--------------------------------------------------------------
673 *
674 * EmbImageCheckProc --
675 *
676 * This procedure is invoked by the B-tree code to perform
677 * consistency checks on embedded images.
678 *
679 * Results:
680 * None.
681 *
682 * Side effects:
683 * The procedure panics if it detects anything wrong with
684 * the embedded image.
685 *
686 *--------------------------------------------------------------
687 */
688
689 static void
690 EmbImageCheckProc(eiPtr, linePtr)
691 TkTextSegment *eiPtr; /* Segment to check. */
692 TkTextLine *linePtr; /* Line containing segment. */
693 {
694 if (eiPtr->nextPtr == NULL) {
695 panic("EmbImageCheckProc: embedded image is last segment in line");
696 }
697 if (eiPtr->size != 1) {
698 panic("EmbImageCheckProc: embedded image has size %d", eiPtr->size);
699 }
700 }
701
702 /*
703 *--------------------------------------------------------------
704 *
705 * EmbImageDisplayProc --
706 *
707 * This procedure is invoked by the text displaying code
708 * when it is time to actually draw an embedded image
709 * chunk on the screen.
710 *
711 * Results:
712 * None.
713 *
714 * Side effects:
715 * The embedded image gets moved to the correct location
716 * and drawn onto the display.
717 *
718 *--------------------------------------------------------------
719 */
720
721 static void
722 EmbImageDisplayProc(chunkPtr, x, y, lineHeight, baseline, display, dst, screenY)
723 TkTextDispChunk *chunkPtr; /* Chunk that is to be drawn. */
724 int x; /* X-position in dst at which to
725 * draw this chunk (differs from
726 * the x-position in the chunk because
727 * of scrolling). */
728 int y; /* Top of rectangular bounding box
729 * for line: tells where to draw this
730 * chunk in dst (x-position is in
731 * the chunk itself). */
732 int lineHeight; /* Total height of line. */
733 int baseline; /* Offset of baseline from y. */
734 Display *display; /* Display to use for drawing. */
735 Drawable dst; /* Pixmap or window in which to draw */
736 int screenY; /* Y-coordinate in text window that
737 * corresponds to y. */
738 {
739 TkTextSegment *eiPtr = (TkTextSegment *) chunkPtr->clientData;
740 int lineX, imageX, imageY, width, height;
741 Tk_Image image;
742
743 image = eiPtr->body.ei.image;
744 if (image == NULL) {
745 return;
746 }
747 if ((x + chunkPtr->width) <= 0) {
748 return;
749 }
750
751 /*
752 * Compute the image's location and size in the text widget, taking
753 * into account the align value for the image.
754 */
755
756 EmbImageBboxProc(chunkPtr, 0, y, lineHeight, baseline, &lineX,
757 &imageY, &width, &height);
758 imageX = lineX - chunkPtr->x + x;
759
760 Tk_RedrawImage(image, 0, 0, width, height, dst,
761 imageX, imageY);
762 }
763
764 /*
765 *--------------------------------------------------------------
766 *
767 * EmbImageBboxProc --
768 *
769 * This procedure is called to compute the bounding box of
770 * the area occupied by an embedded image.
771 *
772 * Results:
773 * There is no return value. *xPtr and *yPtr are filled in
774 * with the coordinates of the upper left corner of the
775 * image, and *widthPtr and *heightPtr are filled in with
776 * the dimensions of the image in pixels. Note: not all
777 * of the returned bbox is necessarily visible on the screen
778 * (the rightmost part might be off-screen to the right,
779 * and the bottommost part might be off-screen to the bottom).
780 *
781 * Side effects:
782 * None.
783 *
784 *--------------------------------------------------------------
785 */
786
787 static void
788 EmbImageBboxProc(chunkPtr, index, y, lineHeight, baseline, xPtr, yPtr,
789 widthPtr, heightPtr)
790 TkTextDispChunk *chunkPtr; /* Chunk containing desired char. */
791 int index; /* Index of desired character within
792 * the chunk. */
793 int y; /* Topmost pixel in area allocated
794 * for this line. */
795 int lineHeight; /* Total height of line. */
796 int baseline; /* Location of line's baseline, in
797 * pixels measured down from y. */
798 int *xPtr, *yPtr; /* Gets filled in with coords of
799 * character's upper-left pixel. */
800 int *widthPtr; /* Gets filled in with width of
801 * character, in pixels. */
802 int *heightPtr; /* Gets filled in with height of
803 * character, in pixels. */
804 {
805 TkTextSegment *eiPtr = (TkTextSegment *) chunkPtr->clientData;
806 Tk_Image image;
807
808 image = eiPtr->body.ei.image;
809 if (image != NULL) {
810 Tk_SizeOfImage(image, widthPtr, heightPtr);
811 } else {
812 *widthPtr = 0;
813 *heightPtr = 0;
814 }
815 *xPtr = chunkPtr->x + eiPtr->body.ei.padX;
816 switch (eiPtr->body.ei.align) {
817 case ALIGN_BOTTOM:
818 *yPtr = y + (lineHeight - *heightPtr - eiPtr->body.ei.padY);
819 break;
820 case ALIGN_CENTER:
821 *yPtr = y + (lineHeight - *heightPtr)/2;
822 break;
823 case ALIGN_TOP:
824 *yPtr = y + eiPtr->body.ei.padY;
825 break;
826 case ALIGN_BASELINE:
827 *yPtr = y + (baseline - *heightPtr);
828 break;
829 }
830 }
831
832 /*
833 *--------------------------------------------------------------
834 *
835 * TkTextImageIndex --
836 *
837 * Given the name of an embedded image within a text widget,
838 * returns an index corresponding to the image's position
839 * in the text.
840 *
841 * Results:
842 * The return value is 1 if there is an embedded image by
843 * the given name in the text widget, 0 otherwise. If the
844 * image exists, *indexPtr is filled in with its index.
845 *
846 * Side effects:
847 * None.
848 *
849 *--------------------------------------------------------------
850 */
851
852 int
853 TkTextImageIndex(textPtr, name, indexPtr)
854 TkText *textPtr; /* Text widget containing image. */
855 CONST char *name; /* Name of image. */
856 TkTextIndex *indexPtr; /* Index information gets stored here. */
857 {
858 Tcl_HashEntry *hPtr;
859 TkTextSegment *eiPtr;
860
861 hPtr = Tcl_FindHashEntry(&textPtr->imageTable, name);
862 if (hPtr == NULL) {
863 return 0;
864 }
865 eiPtr = (TkTextSegment *) Tcl_GetHashValue(hPtr);
866 indexPtr->tree = textPtr->tree;
867 indexPtr->linePtr = eiPtr->body.ei.linePtr;
868 indexPtr->byteIndex = TkTextSegToOffset(eiPtr, indexPtr->linePtr);
869 return 1;
870 }
871
872 /*
873 *--------------------------------------------------------------
874 *
875 * EmbImageProc --
876 *
877 * This procedure is called by the image code whenever an
878 * image or its contents changes.
879 *
880 * Results:
881 * None.
882 *
883 * Side effects:
884 * The image will be redisplayed.
885 *
886 *--------------------------------------------------------------
887 */
888
889 static void
890 EmbImageProc(clientData, x, y, width, height, imgWidth, imgHeight)
891 ClientData clientData; /* Pointer to widget record. */
892 int x, y; /* Upper left pixel (within image)
893 * that must be redisplayed. */
894 int width, height; /* Dimensions of area to redisplay
895 * (may be <= 0). */
896 int imgWidth, imgHeight; /* New dimensions of image. */
897
898 {
899 TkTextSegment *eiPtr = (TkTextSegment *) clientData;
900 TkTextIndex index;
901
902 index.tree = eiPtr->body.ei.textPtr->tree;
903 index.linePtr = eiPtr->body.ei.linePtr;
904 index.byteIndex = TkTextSegToOffset(eiPtr, eiPtr->body.ei.linePtr);
905 TkTextChanged(eiPtr->body.ei.textPtr, &index, &index);
906 }
907
908