Debian Codegraph @

pTk/mTk/generic/tkTextWind.c

raw symbols SCIP
1 /*
2 * tkTextWind.c --
3 *
4 * This file contains code that allows arbitrary windows to be
5 * nested inside text widgets. It also implements the "window"
6 * widget command for texts.
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: tkTextWind.c,v 1.6 2002/08/05 04:30:40 dgp Exp $
15 */
16 #include "tk.h"
17 #include "tkText.h"
18
19 /*
20 * The following structure is the official type record for the
21 * embedded window geometry manager:
22 */
23
24 static void EmbWinRequestProc _ANSI_ARGS_((ClientData clientData,
25 Tk_Window tkwin));
26 static void EmbWinLostSlaveProc _ANSI_ARGS_((ClientData clientData,
27 Tk_Window tkwin));
28
29 static Tk_GeomMgr textGeomType = {
30 "text", /* name */
31 EmbWinRequestProc, /* requestProc */
32 EmbWinLostSlaveProc, /* lostSlaveProc */
33 };
34
35 /*
36 * Definitions for alignment values:
37 */
38
39 #define ALIGN_BOTTOM 0
40 #define ALIGN_CENTER 1
41 #define ALIGN_TOP 2
42 #define ALIGN_BASELINE 3
43
44 /*
45 * Macro that determines the size of an embedded window segment:
46 */
47
48 #define EW_SEG_SIZE ((unsigned) (Tk_Offset(TkTextSegment, body) \
49 + sizeof(TkTextEmbWindow)))
50
51 /*
52 * Prototypes for procedures defined in this file:
53 */
54
55 static int AlignParseProc _ANSI_ARGS_((ClientData clientData,
56 Tcl_Interp *interp, Tk_Window tkwin,
57 Tcl_Obj *value, char *widgRec, int offset));
58 static Tcl_Obj * AlignPrintProc _ANSI_ARGS_((ClientData clientData,
59 Tk_Window tkwin, char *widgRec, int offset,
60 Tcl_FreeProc **freeProcPtr));
61 static TkTextSegment * EmbWinCleanupProc _ANSI_ARGS_((TkTextSegment *segPtr,
62 TkTextLine *linePtr));
63 static void EmbWinCheckProc _ANSI_ARGS_((TkTextSegment *segPtr,
64 TkTextLine *linePtr));
65 static void EmbWinBboxProc _ANSI_ARGS_((TkTextDispChunk *chunkPtr,
66 int index, int y, int lineHeight, int baseline,
67 int *xPtr, int *yPtr, int *widthPtr,
68 int *heightPtr));
69 static int EmbWinConfigure _ANSI_ARGS_((TkText *textPtr,
70 TkTextSegment *ewPtr, int argc, CONST char **argv));
71 static void EmbWinDelayedUnmap _ANSI_ARGS_((
72 ClientData clientData));
73 static int EmbWinDeleteProc _ANSI_ARGS_((TkTextSegment *segPtr,
74 TkTextLine *linePtr, int treeGone));
75 static void EmbWinDisplayProc _ANSI_ARGS_((
76 TkTextDispChunk *chunkPtr, int x, int y,
77 int lineHeight, int baseline, Display *display,
78 Drawable dst, int screenY));
79 static int EmbWinLayoutProc _ANSI_ARGS_((TkText *textPtr,
80 TkTextIndex *indexPtr, TkTextSegment *segPtr,
81 int offset, int maxX, int maxChars,
82 int noCharsYet, TkWrapMode wrapMode,
83 TkTextDispChunk *chunkPtr));
84 static void EmbWinStructureProc _ANSI_ARGS_((ClientData clientData,
85 XEvent *eventPtr));
86 static void EmbWinUndisplayProc _ANSI_ARGS_((TkText *textPtr,
87 TkTextDispChunk *chunkPtr));
88
89 /*
90 * The following structure declares the "embedded window" segment type.
91 */
92
93 static Tk_SegType tkTextEmbWindowType = {
94 "window", /* name */
95 0, /* leftGravity */
96 (Tk_SegSplitProc *) NULL, /* splitProc */
97 EmbWinDeleteProc, /* deleteProc */
98 EmbWinCleanupProc, /* cleanupProc */
99 (Tk_SegLineChangeProc *) NULL, /* lineChangeProc */
100 EmbWinLayoutProc, /* layoutProc */
101 EmbWinCheckProc /* checkProc */
102 };
103
104 /*
105 * Information used for parsing window configuration options:
106 */
107
108 static Tk_CustomOption alignOption = {AlignParseProc, AlignPrintProc,
109 (ClientData) NULL};
110
111 static Tk_ConfigSpec configSpecs[] = {
112 {TK_CONFIG_CUSTOM, "-align", (char *) NULL, (char *) NULL,
113 "center", 0, TK_CONFIG_DONT_SET_DEFAULT, &alignOption},
114 {TK_CONFIG_CALLBACK, "-create", (char *) NULL, (char *) NULL,
115 (char *) NULL, Tk_Offset(TkTextEmbWindow, create),
116 TK_CONFIG_DONT_SET_DEFAULT|TK_CONFIG_NULL_OK},
117 {TK_CONFIG_INT, "-padx", (char *) NULL, (char *) NULL,
118 "0", Tk_Offset(TkTextEmbWindow, padX),
119 TK_CONFIG_DONT_SET_DEFAULT},
120 {TK_CONFIG_INT, "-pady", (char *) NULL, (char *) NULL,
121 "0", Tk_Offset(TkTextEmbWindow, padY),
122 TK_CONFIG_DONT_SET_DEFAULT},
123 {TK_CONFIG_BOOLEAN, "-stretch", (char *) NULL, (char *) NULL,
124 "0", Tk_Offset(TkTextEmbWindow, stretch),
125 TK_CONFIG_DONT_SET_DEFAULT},
126 {TK_CONFIG_WINDOW, "-window", (char *) NULL, (char *) NULL,
127 (char *) NULL, Tk_Offset(TkTextEmbWindow, tkwin),
128 TK_CONFIG_DONT_SET_DEFAULT|TK_CONFIG_NULL_OK},
129 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
130 (char *) NULL, 0, 0}
131 };
132
133 /*
134 *--------------------------------------------------------------
135 *
136 * TkTextWindowCmd --
137 *
138 * This procedure implements the "window" widget command
139 * for text widgets. See the user documentation for details
140 * on what it does.
141 *
142 * Results:
143 * A standard Tcl result or error.
144 *
145 * Side effects:
146 * See the user documentation.
147 *
148 *--------------------------------------------------------------
149 */
150
151 int
152 TkTextWindowCmd(textPtr, interp, argc, argv)
153 register TkText *textPtr; /* Information about text widget. */
154 Tcl_Interp *interp; /* Current interpreter. */
155 int argc; /* Number of arguments. */
156 CONST char **argv; /* Argument strings. Someone else has already
157 * parsed this command enough to know that
158 * argv[1] is "window". */
159 {
160 size_t length;
161 register TkTextSegment *ewPtr;
162
163 if (argc < 3) {
164 Tcl_AppendResult(interp, "wrong # args: should be \"",
165 argv[0], " window option ?arg arg ...?\"", (char *) NULL);
166 return TCL_ERROR;
167 }
168 length = strlen(argv[2]);
169 if ((strncmp(argv[2], "cget", length) == 0) && (length >= 2)) {
170 TkTextIndex index;
171 TkTextSegment *ewPtr;
172
173 if (argc != 5) {
174 Tcl_AppendResult(interp, "wrong # args: should be \"",
175 argv[0], " window cget index option\"",
176 (char *) NULL);
177 return TCL_ERROR;
178 }
179 if (TkTextGetIndex(interp, textPtr, argv[3], &index) != TCL_OK) {
180 return TCL_ERROR;
181 }
182 ewPtr = TkTextIndexToSeg(&index, (int *) NULL);
183 if (ewPtr->typePtr != &tkTextEmbWindowType) {
184 Tcl_AppendResult(interp, "no embedded window at index \"",
185 argv[3], "\"", (char *) NULL);
186 return TCL_ERROR;
187 }
188 return Tk_ConfigureValue(interp, textPtr->tkwin, configSpecs,
189 (char *) &ewPtr->body.ew, argv[4], 0);
190 } else if ((strncmp(argv[2], "configure", length) == 0) && (length >= 2)) {
191 TkTextIndex index;
192 TkTextSegment *ewPtr;
193
194 if (argc < 4) {
195 Tcl_AppendResult(interp, "wrong # args: should be \"",
196 argv[0], " window configure index ?option value ...?\"",
197 (char *) NULL);
198 return TCL_ERROR;
199 }
200 if (TkTextGetIndex(interp, textPtr, argv[3], &index) != TCL_OK) {
201 return TCL_ERROR;
202 }
203 ewPtr = TkTextIndexToSeg(&index, (int *) NULL);
204 if (ewPtr->typePtr != &tkTextEmbWindowType) {
205 Tcl_AppendResult(interp, "no embedded window at index \"",
206 argv[3], "\"", (char *) NULL);
207 return TCL_ERROR;
208 }
209 if (argc == 4) {
210 return Tk_ConfigureInfo(interp, textPtr->tkwin, configSpecs,
211 (char *) &ewPtr->body.ew, (char *) NULL, 0);
212 } else if (argc == 5) {
213 return Tk_ConfigureInfo(interp, textPtr->tkwin, configSpecs,
214 (char *) &ewPtr->body.ew, argv[4], 0);
215 } else {
216 TkTextChanged(textPtr, &index, &index);
217 return EmbWinConfigure(textPtr, ewPtr, argc-4, argv+4);
218 }
219 } else if ((strncmp(argv[2], "create", length) == 0) && (length >= 2)) {
220 TkTextIndex index;
221 int lineIndex;
222
223 /*
224 * Add a new window. Find where to put the new window, and
225 * mark that position for redisplay.
226 */
227
228 if (argc < 4) {
229 Tcl_AppendResult(interp, "wrong # args: should be \"",
230 argv[0], " window create index ?option value ...?\"",
231 (char *) NULL);
232 return TCL_ERROR;
233 }
234 if (TkTextGetIndex(interp, textPtr, argv[3], &index) != TCL_OK) {
235 return TCL_ERROR;
236 }
237
238 /*
239 * Don't allow insertions on the last (dummy) line of the text.
240 */
241
242 lineIndex = TkBTreeLineIndex(index.linePtr);
243 if (lineIndex == TkBTreeNumLines(textPtr->tree)) {
244 lineIndex--;
245 TkTextMakeByteIndex(textPtr->tree, lineIndex, 1000000, &index);
246 }
247
248 /*
249 * Create the new window segment and initialize it.
250 */
251
252 ewPtr = (TkTextSegment *) ckalloc(EW_SEG_SIZE);
253 ewPtr->typePtr = &tkTextEmbWindowType;
254 ewPtr->size = 1;
255 ewPtr->body.ew.textPtr = textPtr;
256 ewPtr->body.ew.linePtr = NULL;
257 ewPtr->body.ew.tkwin = NULL;
258 ewPtr->body.ew.create = NULL;
259 ewPtr->body.ew.align = ALIGN_CENTER;
260 ewPtr->body.ew.padX = ewPtr->body.ew.padY = 0;
261 ewPtr->body.ew.stretch = 0;
262 ewPtr->body.ew.chunkCount = 0;
263 ewPtr->body.ew.displayed = 0;
264
265 /*
266 * Link the segment into the text widget, then configure it (delete
267 * it again if the configuration fails).
268 */
269
270 TkTextChanged(textPtr, &index, &index);
271 TkBTreeLinkSegment(ewPtr, &index);
272 if (EmbWinConfigure(textPtr, ewPtr, argc-4, argv+4) != TCL_OK) {
273 TkTextIndex index2;
274
275 TkTextIndexForwChars(&index, 1, &index2);
276 TkBTreeDeleteChars(&index, &index2);
277 return TCL_ERROR;
278 }
279 } else if (strncmp(argv[2], "names", length) == 0) {
280 Tcl_HashSearch search;
281 Tcl_HashEntry *hPtr;
282
283 if (argc != 3) {
284 Tcl_AppendResult(interp, "wrong # args: should be \"",
285 argv[0], " window names\"", (char *) NULL);
286 return TCL_ERROR;
287 }
288 for (hPtr = Tcl_FirstHashEntry(&textPtr->windowTable, &search);
289 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
290 Tcl_AppendElement(interp,
291 Tcl_GetHashKey(&textPtr->markTable, hPtr));
292 }
293 } else {
294 Tcl_AppendResult(interp, "bad window option \"", argv[2],
295 "\": must be cget, configure, create, or names",
296 (char *) NULL);
297 return TCL_ERROR;
298 }
299 return TCL_OK;
300 }
301
302 /*
303 *--------------------------------------------------------------
304 *
305 * EmbWinConfigure --
306 *
307 * This procedure is called to handle configuration options
308 * for an embedded window, using an argc/argv list.
309 *
310 * Results:
311 * The return value is a standard Tcl result. If TCL_ERROR is
312 * returned, then the interp's result contains an error message..
313 *
314 * Side effects:
315 * Configuration information for the embedded window changes,
316 * such as alignment, stretching, or name of the embedded
317 * window.
318 *
319 *--------------------------------------------------------------
320 */
321
322 static int
323 EmbWinConfigure(textPtr, ewPtr, argc, argv)
324 TkText *textPtr; /* Information about text widget that
325 * contains embedded window. */
326 TkTextSegment *ewPtr; /* Embedded window to be configured. */
327 int argc; /* Number of strings in argv. */
328 CONST char **argv; /* Array of strings describing configuration
329 * options. */
330 {
331 Tk_Window oldWindow;
332 Tcl_HashEntry *hPtr;
333 int new;
334
335 oldWindow = ewPtr->body.ew.tkwin;
336 if (Tk_ConfigureWidget(textPtr->interp, textPtr->tkwin, configSpecs,
337 argc, argv, (char *) &ewPtr->body.ew, TK_CONFIG_ARGV_ONLY)
338 != TCL_OK) {
339 return TCL_ERROR;
340 }
341 if (oldWindow != ewPtr->body.ew.tkwin) {
342 if (oldWindow != NULL) {
343 Tcl_DeleteHashEntry(Tcl_FindHashEntry(&textPtr->windowTable,
344 Tk_PathName(oldWindow)));
345 Tk_DeleteEventHandler(oldWindow, StructureNotifyMask,
346 EmbWinStructureProc, (ClientData) ewPtr);
347 Tk_ManageGeometry(oldWindow, (Tk_GeomMgr *) NULL,
348 (ClientData) NULL);
349 if (textPtr->tkwin != Tk_Parent(oldWindow)) {
350 Tk_UnmaintainGeometry(oldWindow, textPtr->tkwin);
351 } else {
352 Tk_UnmapWindow(oldWindow);
353 }
354 }
355 if (ewPtr->body.ew.tkwin != NULL) {
356 Tk_Window ancestor, parent;
357
358 /*
359 * Make sure that the text is either the parent of the
360 * embedded window or a descendant of that parent. Also,
361 * don't allow a top-level window to be managed inside
362 * a text.
363 */
364
365 parent = Tk_Parent(ewPtr->body.ew.tkwin);
366 for (ancestor = textPtr->tkwin; ;
367 ancestor = Tk_Parent(ancestor)) {
368 if (ancestor == parent) {
369 break;
370 }
371 if (Tk_TopWinHierarchy(ancestor)) {
372 badMaster:
373 Tcl_AppendResult(textPtr->interp, "can't embed ",
374 Tk_PathName(ewPtr->body.ew.tkwin), " in ",
375 Tk_PathName(textPtr->tkwin), (char *) NULL);
376 ewPtr->body.ew.tkwin = NULL;
377 return TCL_ERROR;
378 }
379 }
380 if (Tk_TopWinHierarchy(ewPtr->body.ew.tkwin)
381 || (ewPtr->body.ew.tkwin == textPtr->tkwin)) {
382 goto badMaster;
383 }
384
385 /*
386 * Take over geometry management for the window, plus create
387 * an event handler to find out when it is deleted.
388 */
389
390 Tk_ManageGeometry(ewPtr->body.ew.tkwin, &textGeomType,
391 (ClientData) ewPtr);
392 Tk_CreateEventHandler(ewPtr->body.ew.tkwin, StructureNotifyMask,
393 EmbWinStructureProc, (ClientData) ewPtr);
394
395 /*
396 * Special trick! Must enter into the hash table *after*
397 * calling Tk_ManageGeometry: if the window was already managed
398 * elsewhere in this text, the Tk_ManageGeometry call will cause
399 * the entry to be removed, which could potentially lose the new
400 * entry.
401 */
402
403 hPtr = Tcl_CreateHashEntry(&textPtr->windowTable,
404 Tk_PathName(ewPtr->body.ew.tkwin), &new);
405 Tcl_SetHashValue(hPtr, ewPtr);
406
407 }
408 }
409 return TCL_OK;
410 }
411
412 /*
413 *--------------------------------------------------------------
414 *
415 * AlignParseProc --
416 *
417 * This procedure is invoked by Tk_ConfigureWidget during
418 * option processing to handle "-align" options for embedded
419 * windows.
420 *
421 * Results:
422 * A standard Tcl return value.
423 *
424 * Side effects:
425 * The alignment for the embedded window may change.
426 *
427 *--------------------------------------------------------------
428 */
429
430 /* ARGSUSED */
431 static int
432 AlignParseProc(clientData, interp, tkwin, arg, widgRec, offset)
433 ClientData clientData; /* Not used.*/
434 Tcl_Interp *interp; /* Used for reporting errors. */
435 Tk_Window tkwin; /* Window for text widget. */
436 Tcl_Obj *arg; /* Value of option. */
437 char *widgRec; /* Pointer to TkTextEmbWindow
438 * structure. */
439 int offset; /* Offset into item (ignored). */
440 {
441 char *value = Tcl_GetString(arg);
442 register TkTextEmbWindow *embPtr = (TkTextEmbWindow *) widgRec;
443
444 if (strcmp(value, "baseline") == 0) {
445 embPtr->align = ALIGN_BASELINE;
446 } else if (strcmp(value, "bottom") == 0) {
447 embPtr->align = ALIGN_BOTTOM;
448 } else if (strcmp(value, "center") == 0) {
449 embPtr->align = ALIGN_CENTER;
450 } else if (strcmp(value, "top") == 0) {
451 embPtr->align = ALIGN_TOP;
452 } else {
453 Tcl_AppendResult(interp, "bad alignment \"", value,
454 "\": must be baseline, bottom, center, or top",
455 (char *) NULL);
456 return TCL_ERROR;
457 }
458 return TCL_OK;
459 }
460
461 /*
462 *--------------------------------------------------------------
463 *
464 * AlignPrintProc --
465 *
466 * This procedure is invoked by the Tk configuration code
467 * to produce a printable string for the "-align" configuration
468 * option for embedded windows.
469 *
470 * Results:
471 * The return value is a string describing the embedded
472 * window's current alignment.
473 *
474 * Side effects:
475 * None.
476 *
477 *--------------------------------------------------------------
478 */
479
480 /* ARGSUSED */
481 static Tcl_Obj *
482 AlignPrintProc(clientData, tkwin, widgRec, offset, freeProcPtr)
483 ClientData clientData; /* Ignored. */
484 Tk_Window tkwin; /* Window for text widget. */
485 char *widgRec; /* Pointer to TkTextEmbWindow
486 * structure. */
487 int offset; /* Ignored. */
488 Tcl_FreeProc **freeProcPtr; /* Pointer to variable to fill in with
489 * information about how to reclaim
490 * storage for return string. */
491 {
492 Tcl_Obj * result = NULL;
493 LangSetDefault(&result, "");
494 /* freeProc set correct for Tcl */
495 *freeProcPtr = TCL_STATIC;
496
497 switch (((TkTextEmbWindow *) widgRec)->align) {
498 case ALIGN_BASELINE:
499 LangSetString(&result, "baseline");
500 break;
501 case ALIGN_BOTTOM:
502 LangSetString(&result, "bottom");
503 break;
504 case ALIGN_CENTER:
505 LangSetString(&result, "centre");
506 break;
507 case ALIGN_TOP:
508 LangSetString(&result, "top");
509 break;
510 default:
511 break;
512 }
513 return result;
514 }
515
516 /*
517 *--------------------------------------------------------------
518 *
519 * EmbWinStructureProc --
520 *
521 * This procedure is invoked by the Tk event loop whenever
522 * StructureNotify events occur for a window that's embedded
523 * in a text widget. This procedure's only purpose is to
524 * clean up when windows are deleted.
525 *
526 * Results:
527 * None.
528 *
529 * Side effects:
530 * The window is disassociated from the window segment, and
531 * the portion of the text is redisplayed.
532 *
533 *--------------------------------------------------------------
534 */
535
536 static void
537 EmbWinStructureProc(clientData, eventPtr)
538 ClientData clientData; /* Pointer to record describing window item. */
539 XEvent *eventPtr; /* Describes what just happened. */
540 {
541 register TkTextSegment *ewPtr = (TkTextSegment *) clientData;
542 TkTextIndex index;
543
544 if (eventPtr->type != DestroyNotify) {
545 return;
546 }
547
548 Tcl_DeleteHashEntry(Tcl_FindHashEntry(&ewPtr->body.ew.textPtr->windowTable,
549 Tk_PathName(ewPtr->body.ew.tkwin)));
550 ewPtr->body.ew.tkwin = NULL;
551 index.tree = ewPtr->body.ew.textPtr->tree;
552 index.linePtr = ewPtr->body.ew.linePtr;
553 index.byteIndex = TkTextSegToOffset(ewPtr, ewPtr->body.ew.linePtr);
554 TkTextChanged(ewPtr->body.ew.textPtr, &index, &index);
555 }
556
557 /*
558 *--------------------------------------------------------------
559 *
560 * EmbWinRequestProc --
561 *
562 * This procedure is invoked whenever a window that's associated
563 * with a window canvas item changes its requested dimensions.
564 *
565 * Results:
566 * None.
567 *
568 * Side effects:
569 * The size and location on the screen of the window may change,
570 * depending on the options specified for the window item.
571 *
572 *--------------------------------------------------------------
573 */
574
575 /* ARGSUSED */
576 static void
577 EmbWinRequestProc(clientData, tkwin)
578 ClientData clientData; /* Pointer to record for window item. */
579 Tk_Window tkwin; /* Window that changed its desired
580 * size. */
581 {
582 TkTextSegment *ewPtr = (TkTextSegment *) clientData;
583 TkTextIndex index;
584
585 index.tree = ewPtr->body.ew.textPtr->tree;
586 index.linePtr = ewPtr->body.ew.linePtr;
587 index.byteIndex = TkTextSegToOffset(ewPtr, ewPtr->body.ew.linePtr);
588 TkTextChanged(ewPtr->body.ew.textPtr, &index, &index);
589 }
590
591 /*
592 *--------------------------------------------------------------
593 *
594 * EmbWinLostSlaveProc --
595 *
596 * This procedure is invoked by the Tk geometry manager when
597 * a slave window managed by a text widget is claimed away
598 * by another geometry manager.
599 *
600 * Results:
601 * None.
602 *
603 * Side effects:
604 * The window is disassociated from the window segment, and
605 * the portion of the text is redisplayed.
606 *
607 *--------------------------------------------------------------
608 */
609
610 static void
611 EmbWinLostSlaveProc(clientData, tkwin)
612 ClientData clientData; /* Pointer to record describing window item. */
613 Tk_Window tkwin; /* Window that was claimed away by another
614 * geometry manager. */
615 {
616 register TkTextSegment *ewPtr = (TkTextSegment *) clientData;
617 TkTextIndex index;
618
619 Tk_DeleteEventHandler(ewPtr->body.ew.tkwin, StructureNotifyMask,
620 EmbWinStructureProc, (ClientData) ewPtr);
621 Tcl_CancelIdleCall(EmbWinDelayedUnmap, (ClientData) ewPtr);
622 if (ewPtr->body.ew.textPtr->tkwin != Tk_Parent(tkwin)) {
623 Tk_UnmaintainGeometry(tkwin, ewPtr->body.ew.textPtr->tkwin);
624 } else {
625 Tk_UnmapWindow(tkwin);
626 }
627 Tcl_DeleteHashEntry(Tcl_FindHashEntry(&ewPtr->body.ew.textPtr->windowTable,
628 Tk_PathName(ewPtr->body.ew.tkwin)));
629 ewPtr->body.ew.tkwin = NULL;
630 index.tree = ewPtr->body.ew.textPtr->tree;
631 index.linePtr = ewPtr->body.ew.linePtr;
632 index.byteIndex = TkTextSegToOffset(ewPtr, ewPtr->body.ew.linePtr);
633 TkTextChanged(ewPtr->body.ew.textPtr, &index, &index);
634 }
635
636 /*
637 *--------------------------------------------------------------
638 *
639 * EmbWinDeleteProc --
640 *
641 * This procedure is invoked by the text B-tree code whenever
642 * an embedded window lies in a range of characters being deleted.
643 *
644 * Results:
645 * Returns 0 to indicate that the deletion has been accepted.
646 *
647 * Side effects:
648 * The embedded window is deleted, if it exists, and any resources
649 * associated with it are released.
650 *
651 *--------------------------------------------------------------
652 */
653
654 /* ARGSUSED */
655 static int
656 EmbWinDeleteProc(ewPtr, linePtr, treeGone)
657 TkTextSegment *ewPtr; /* Segment being deleted. */
658 TkTextLine *linePtr; /* Line containing segment. */
659 int treeGone; /* Non-zero means the entire tree is
660 * being deleted, so everything must
661 * get cleaned up. */
662 {
663 Tcl_HashEntry *hPtr;
664
665 if (ewPtr->body.ew.tkwin != NULL) {
666 hPtr = Tcl_FindHashEntry(&ewPtr->body.ew.textPtr->windowTable,
667 Tk_PathName(ewPtr->body.ew.tkwin));
668 if (hPtr != NULL) {
669 /*
670 * (It's possible for there to be no hash table entry for this
671 * window, if an error occurred while creating the window segment
672 * but before the window got added to the table)
673 */
674
675 Tcl_DeleteHashEntry(hPtr);
676 }
677
678 /*
679 * Delete the event handler for the window before destroying
680 * the window, so that EmbWinStructureProc doesn't get called
681 * (we'll already do everything that it would have done, and
682 * it will just get confused).
683 */
684
685 Tk_DeleteEventHandler(ewPtr->body.ew.tkwin, StructureNotifyMask,
686 EmbWinStructureProc, (ClientData) ewPtr);
687 Tk_DestroyWindow(ewPtr->body.ew.tkwin);
688 }
689 Tcl_CancelIdleCall(EmbWinDelayedUnmap, (ClientData) ewPtr);
690 Tk_FreeOptions(configSpecs, (char *) &ewPtr->body.ew,
691 ewPtr->body.ew.textPtr->display, 0);
692 ckfree((char *) ewPtr);
693 return 0;
694 }
695
696 /*
697 *--------------------------------------------------------------
698 *
699 * EmbWinCleanupProc --
700 *
701 * This procedure is invoked by the B-tree code whenever a
702 * segment containing an embedded window is moved from one
703 * line to another.
704 *
705 * Results:
706 * None.
707 *
708 * Side effects:
709 * The linePtr field of the segment gets updated.
710 *
711 *--------------------------------------------------------------
712 */
713
714 static TkTextSegment *
715 EmbWinCleanupProc(ewPtr, linePtr)
716 TkTextSegment *ewPtr; /* Mark segment that's being moved. */
717 TkTextLine *linePtr; /* Line that now contains segment. */
718 {
719 ewPtr->body.ew.linePtr = linePtr;
720 return ewPtr;
721 }
722
723 /*
724 *--------------------------------------------------------------
725 *
726 * EmbWinLayoutProc --
727 *
728 * This procedure is the "layoutProc" for embedded window
729 * segments.
730 *
731 * Results:
732 * 1 is returned to indicate that the segment should be
733 * displayed. The chunkPtr structure is filled in.
734 *
735 * Side effects:
736 * None, except for filling in chunkPtr.
737 *
738 *--------------------------------------------------------------
739 */
740
741 /*ARGSUSED*/
742 static int
743 EmbWinLayoutProc(textPtr, indexPtr, ewPtr, offset, maxX, maxChars,
744 noCharsYet, wrapMode, chunkPtr)
745 TkText *textPtr; /* Text widget being layed out. */
746 TkTextIndex *indexPtr; /* Identifies first character in chunk. */
747 TkTextSegment *ewPtr; /* Segment corresponding to indexPtr. */
748 int offset; /* Offset within segPtr corresponding to
749 * indexPtr (always 0). */
750 int maxX; /* Chunk must not occupy pixels at this
751 * position or higher. */
752 int maxChars; /* Chunk must not include more than this
753 * many characters. */
754 int noCharsYet; /* Non-zero means no characters have been
755 * assigned to this line yet. */
756 TkWrapMode wrapMode; /* Wrap mode to use for line: TEXT_WRAPMODE_CHAR,
757 * TEXT_WRAPMODE_NONE, or TEXT_WRAPMODE_WORD. */
758 register TkTextDispChunk *chunkPtr;
759 /* Structure to fill in with information
760 * about this chunk. The x field has already
761 * been set by the caller. */
762 {
763 int width, height;
764
765 if (offset != 0) {
766 panic("Non-zero offset in EmbWinLayoutProc");
767 }
768
769 if ((ewPtr->body.ew.tkwin == NULL) && (ewPtr->body.ew.create != NULL)) {
770 int code, new;
771 Tcl_Obj *name;
772 Tk_Window ancestor;
773 Tcl_HashEntry *hPtr;
774
775 /*
776 * The window doesn't currently exist. Create it by evaluating
777 * the creation script. The script must return the window's
778 * path name: look up that name to get back to the window
779 * token. Then register ourselves as the geometry manager for
780 * the window.
781 */
782
783 code = LangDoCallback(textPtr->interp, ewPtr->body.ew.create, 1, 0);
784 if (code != TCL_OK) {
785 createError:
786 Tcl_BackgroundError(textPtr->interp);
787 goto gotWindow;
788 }
789 name = Tcl_GetObjResult(textPtr->interp);
790 Tcl_IncrRefCount(name);
791 Tcl_ResetResult(textPtr->interp);
792 ewPtr->body.ew.tkwin = Tk_NameToWindow(textPtr->interp,
793 Tcl_GetString(name), textPtr->tkwin);
794 Tcl_DecrRefCount(name);
795 if (ewPtr->body.ew.tkwin == NULL) {
796 goto createError;
797 }
798 for (ancestor = textPtr->tkwin; ;
799 ancestor = Tk_Parent(ancestor)) {
800 if (ancestor == Tk_Parent(ewPtr->body.ew.tkwin)) {
801 break;
802 }
803 if (Tk_TopWinHierarchy(ancestor)) {
804 badMaster:
805 Tcl_AppendResult(textPtr->interp, "can't embed ",
806 Tk_PathName(ewPtr->body.ew.tkwin), " relative to ",
807 Tk_PathName(textPtr->tkwin), (char *) NULL);
808 Tcl_BackgroundError(textPtr->interp);
809 ewPtr->body.ew.tkwin = NULL;
810 goto gotWindow;
811 }
812 }
813 if (Tk_TopWinHierarchy(ewPtr->body.ew.tkwin)
814 || (textPtr->tkwin == ewPtr->body.ew.tkwin)) {
815 goto badMaster;
816 }
817 Tk_ManageGeometry(ewPtr->body.ew.tkwin, &textGeomType,
818 (ClientData) ewPtr);
819 Tk_CreateEventHandler(ewPtr->body.ew.tkwin, StructureNotifyMask,
820 EmbWinStructureProc, (ClientData) ewPtr);
821
822 /*
823 * Special trick! Must enter into the hash table *after*
824 * calling Tk_ManageGeometry: if the window was already managed
825 * elsewhere in this text, the Tk_ManageGeometry call will cause
826 * the entry to be removed, which could potentially lose the new
827 * entry.
828 */
829
830 hPtr = Tcl_CreateHashEntry(&textPtr->windowTable,
831 Tk_PathName(ewPtr->body.ew.tkwin), &new);
832 Tcl_SetHashValue(hPtr, ewPtr);
833 }
834
835 /*
836 * See if there's room for this window on this line.
837 */
838
839 gotWindow:
840 if (ewPtr->body.ew.tkwin == NULL) {
841 width = 0;
842 height = 0;
843 } else {
844 width = Tk_ReqWidth(ewPtr->body.ew.tkwin) + 2*ewPtr->body.ew.padX;
845 height = Tk_ReqHeight(ewPtr->body.ew.tkwin) + 2*ewPtr->body.ew.padY;
846 }
847 if ((width > (maxX - chunkPtr->x))
848 && !noCharsYet && (textPtr->wrapMode != TEXT_WRAPMODE_NONE)) {
849 return 0;
850 }
851
852 /*
853 * Fill in the chunk structure.
854 */
855
856 chunkPtr->displayProc = EmbWinDisplayProc;
857 chunkPtr->undisplayProc = EmbWinUndisplayProc;
858 chunkPtr->measureProc = (Tk_ChunkMeasureProc *) NULL;
859 chunkPtr->bboxProc = EmbWinBboxProc;
860 chunkPtr->numBytes = 1;
861 if (ewPtr->body.ew.align == ALIGN_BASELINE) {
862 chunkPtr->minAscent = height - ewPtr->body.ew.padY;
863 chunkPtr->minDescent = ewPtr->body.ew.padY;
864 chunkPtr->minHeight = 0;
865 } else {
866 chunkPtr->minAscent = 0;
867 chunkPtr->minDescent = 0;
868 chunkPtr->minHeight = height;
869 }
870 chunkPtr->width = width;
871 chunkPtr->breakIndex = -1;
872 chunkPtr->breakIndex = 1;
873 chunkPtr->clientData = (ClientData) ewPtr;
874 ewPtr->body.ew.chunkCount += 1;
875 return 1;
876 }
877
878 /*
879 *--------------------------------------------------------------
880 *
881 * EmbWinCheckProc --
882 *
883 * This procedure is invoked by the B-tree code to perform
884 * consistency checks on embedded windows.
885 *
886 * Results:
887 * None.
888 *
889 * Side effects:
890 * The procedure panics if it detects anything wrong with
891 * the embedded window.
892 *
893 *--------------------------------------------------------------
894 */
895
896 static void
897 EmbWinCheckProc(ewPtr, linePtr)
898 TkTextSegment *ewPtr; /* Segment to check. */
899 TkTextLine *linePtr; /* Line containing segment. */
900 {
901 if (ewPtr->nextPtr == NULL) {
902 panic("EmbWinCheckProc: embedded window is last segment in line");
903 }
904 if (ewPtr->size != 1) {
905 panic("EmbWinCheckProc: embedded window has size %d", ewPtr->size);
906 }
907 }
908
909 /*
910 *--------------------------------------------------------------
911 *
912 * EmbWinDisplayProc --
913 *
914 * This procedure is invoked by the text displaying code
915 * when it is time to actually draw an embedded window
916 * chunk on the screen.
917 *
918 * Results:
919 * None.
920 *
921 * Side effects:
922 * The embedded window gets moved to the correct location
923 * and mapped onto the screen.
924 *
925 *--------------------------------------------------------------
926 */
927
928 static void
929 EmbWinDisplayProc(chunkPtr, x, y, lineHeight, baseline, display, dst, screenY)
930 TkTextDispChunk *chunkPtr; /* Chunk that is to be drawn. */
931 int x; /* X-position in dst at which to
932 * draw this chunk (differs from
933 * the x-position in the chunk because
934 * of scrolling). */
935 int y; /* Top of rectangular bounding box
936 * for line: tells where to draw this
937 * chunk in dst (x-position is in
938 * the chunk itself). */
939 int lineHeight; /* Total height of line. */
940 int baseline; /* Offset of baseline from y. */
941 Display *display; /* Display to use for drawing. */
942 Drawable dst; /* Pixmap or window in which to draw */
943 int screenY; /* Y-coordinate in text window that
944 * corresponds to y. */
945 {
946 TkTextSegment *ewPtr = (TkTextSegment *) chunkPtr->clientData;
947 int lineX, windowX, windowY, width, height;
948 Tk_Window tkwin;
949
950 tkwin = ewPtr->body.ew.tkwin;
951 if (tkwin == NULL) {
952 return;
953 }
954 if ((x + chunkPtr->width) <= 0) {
955 /*
956 * The window is off-screen; just unmap it.
957 */
958
959 if (ewPtr->body.ew.textPtr->tkwin != Tk_Parent(tkwin)) {
960 Tk_UnmaintainGeometry(tkwin, ewPtr->body.ew.textPtr->tkwin);
961 } else {
962 Tk_UnmapWindow(tkwin);
963 }
964 return;
965 }
966
967 /*
968 * Compute the window's location and size in the text widget, taking
969 * into account the align and stretch values for the window.
970 */
971
972 EmbWinBboxProc(chunkPtr, 0, screenY, lineHeight, baseline, &lineX,
973 &windowY, &width, &height);
974 windowX = lineX - chunkPtr->x + x;
975
976 if (ewPtr->body.ew.textPtr->tkwin == Tk_Parent(tkwin)) {
977 if ((windowX != Tk_X(tkwin)) || (windowY != Tk_Y(tkwin))
978 || (Tk_ReqWidth(tkwin) != Tk_Width(tkwin))
979 || (height != Tk_Height(tkwin))) {
980 Tk_MoveResizeWindow(tkwin, windowX, windowY, width, height);
981 }
982 Tk_MapWindow(tkwin);
983 } else {
984 Tk_MaintainGeometry(tkwin, ewPtr->body.ew.textPtr->tkwin,
985 windowX, windowY, width, height);
986 }
987
988 /*
989 * Mark the window as displayed so that it won't get unmapped.
990 */
991
992 ewPtr->body.ew.displayed = 1;
993 }
994
995 /*
996 *--------------------------------------------------------------
997 *
998 * EmbWinUndisplayProc --
999 *
1000 * This procedure is called when the chunk for an embedded
1001 * window is no longer going to be displayed. It arranges
1002 * for the window associated with the chunk to be unmapped.
1003 *
1004 * Results:
1005 * None.
1006 *
1007 * Side effects:
1008 * The window is scheduled for unmapping.
1009 *
1010 *--------------------------------------------------------------
1011 */
1012
1013 static void
1014 EmbWinUndisplayProc(textPtr, chunkPtr)
1015 TkText *textPtr; /* Overall information about text
1016 * widget. */
1017 TkTextDispChunk *chunkPtr; /* Chunk that is about to be freed. */
1018 {
1019 TkTextSegment *ewPtr = (TkTextSegment *) chunkPtr->clientData;
1020
1021 ewPtr->body.ew.chunkCount--;
1022 if (ewPtr->body.ew.chunkCount == 0) {
1023 /*
1024 * Don't unmap the window immediately, since there's a good chance
1025 * that it will immediately be redisplayed, perhaps even in the
1026 * same place. Instead, schedule the window to be unmapped later;
1027 * the call to EmbWinDelayedUnmap will be cancelled in the likely
1028 * event that the unmap becomes unnecessary.
1029 */
1030
1031 ewPtr->body.ew.displayed = 0;
1032 Tcl_DoWhenIdle(EmbWinDelayedUnmap, (ClientData) ewPtr);
1033 }
1034 }
1035
1036 /*
1037 *--------------------------------------------------------------
1038 *
1039 * EmbWinBboxProc --
1040 *
1041 * This procedure is called to compute the bounding box of
1042 * the area occupied by an embedded window.
1043 *
1044 * Results:
1045 * There is no return value. *xPtr and *yPtr are filled in
1046 * with the coordinates of the upper left corner of the
1047 * window, and *widthPtr and *heightPtr are filled in with
1048 * the dimensions of the window in pixels. Note: not all
1049 * of the returned bbox is necessarily visible on the screen
1050 * (the rightmost part might be off-screen to the right,
1051 * and the bottommost part might be off-screen to the bottom).
1052 *
1053 * Side effects:
1054 * None.
1055 *
1056 *--------------------------------------------------------------
1057 */
1058
1059 static void
1060 EmbWinBboxProc(chunkPtr, index, y, lineHeight, baseline, xPtr, yPtr,
1061 widthPtr, heightPtr)
1062 TkTextDispChunk *chunkPtr; /* Chunk containing desired char. */
1063 int index; /* Index of desired character within
1064 * the chunk. */
1065 int y; /* Topmost pixel in area allocated
1066 * for this line. */
1067 int lineHeight; /* Total height of line. */
1068 int baseline; /* Location of line's baseline, in
1069 * pixels measured down from y. */
1070 int *xPtr, *yPtr; /* Gets filled in with coords of
1071 * character's upper-left pixel. */
1072 int *widthPtr; /* Gets filled in with width of
1073 * character, in pixels. */
1074 int *heightPtr; /* Gets filled in with height of
1075 * character, in pixels. */
1076 {
1077 TkTextSegment *ewPtr = (TkTextSegment *) chunkPtr->clientData;
1078 Tk_Window tkwin;
1079
1080 tkwin = ewPtr->body.ew.tkwin;
1081 if (tkwin != NULL) {
1082 *widthPtr = Tk_ReqWidth(tkwin);
1083 *heightPtr = Tk_ReqHeight(tkwin);
1084 } else {
1085 *widthPtr = 0;
1086 *heightPtr = 0;
1087 }
1088 *xPtr = chunkPtr->x + ewPtr->body.ew.padX;
1089 if (ewPtr->body.ew.stretch) {
1090 if (ewPtr->body.ew.align == ALIGN_BASELINE) {
1091 *heightPtr = baseline - ewPtr->body.ew.padY;
1092 } else {
1093 *heightPtr = lineHeight - 2*ewPtr->body.ew.padY;
1094 }
1095 }
1096 switch (ewPtr->body.ew.align) {
1097 case ALIGN_BOTTOM:
1098 *yPtr = y + (lineHeight - *heightPtr - ewPtr->body.ew.padY);
1099 break;
1100 case ALIGN_CENTER:
1101 *yPtr = y + (lineHeight - *heightPtr)/2;
1102 break;
1103 case ALIGN_TOP:
1104 *yPtr = y + ewPtr->body.ew.padY;
1105 break;
1106 case ALIGN_BASELINE:
1107 *yPtr = y + (baseline - *heightPtr);
1108 break;
1109 }
1110 }
1111
1112 /*
1113 *--------------------------------------------------------------
1114 *
1115 * EmbWinDelayedUnmap --
1116 *
1117 * This procedure is an idle handler that does the actual
1118 * work of unmapping an embedded window. See the comment
1119 * in EmbWinUndisplayProc for details.
1120 *
1121 * Results:
1122 * None.
1123 *
1124 * Side effects:
1125 * The window gets unmapped, unless its chunk reference count
1126 * has become non-zero again.
1127 *
1128 *--------------------------------------------------------------
1129 */
1130
1131 static void
1132 EmbWinDelayedUnmap(clientData)
1133 ClientData clientData; /* Token for the window to
1134 * be unmapped. */
1135 {
1136 TkTextSegment *ewPtr = (TkTextSegment *) clientData;
1137
1138 if (!ewPtr->body.ew.displayed && (ewPtr->body.ew.tkwin != NULL)) {
1139 if (ewPtr->body.ew.textPtr->tkwin != Tk_Parent(ewPtr->body.ew.tkwin)) {
1140 Tk_UnmaintainGeometry(ewPtr->body.ew.tkwin,
1141 ewPtr->body.ew.textPtr->tkwin);
1142 } else {
1143 Tk_UnmapWindow(ewPtr->body.ew.tkwin);
1144 }
1145 }
1146 }
1147
1148 /*
1149 *--------------------------------------------------------------
1150 *
1151 * TkTextWindowIndex --
1152 *
1153 * Given the name of an embedded window within a text widget,
1154 * returns an index corresponding to the window's position
1155 * in the text.
1156 *
1157 * Results:
1158 * The return value is 1 if there is an embedded window by
1159 * the given name in the text widget, 0 otherwise. If the
1160 * window exists, *indexPtr is filled in with its index.
1161 *
1162 * Side effects:
1163 * None.
1164 *
1165 *--------------------------------------------------------------
1166 */
1167
1168 int
1169 TkTextWindowIndex(textPtr, name, indexPtr)
1170 TkText *textPtr; /* Text widget containing window. */
1171 CONST char *name; /* Name of window. */
1172 TkTextIndex *indexPtr; /* Index information gets stored here. */
1173 {
1174 Tcl_HashEntry *hPtr;
1175 TkTextSegment *ewPtr;
1176
1177 hPtr = Tcl_FindHashEntry(&textPtr->windowTable, name);
1178 if (hPtr == NULL) {
1179 return 0;
1180 }
1181 ewPtr = (TkTextSegment *) Tcl_GetHashValue(hPtr);
1182 indexPtr->tree = textPtr->tree;
1183 indexPtr->linePtr = ewPtr->body.ew.linePtr;
1184 indexPtr->byteIndex = TkTextSegToOffset(ewPtr, indexPtr->linePtr);
1185 return 1;
1186 }
1187