1 /*
2  * tkTextTag.c --
3  *
4  *	This module implements the "tag" subcommand of the widget command for
5  *	text widgets, plus most of the other high-level functions related to
6  *	tags.
7  *
8  * Copyright (c) 1992-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 of
12  * this file, and for a DISCLAIMER OF ALL WARRANTIES.
13  */
14 
15 #include "default.h"
16 #include "tkInt.h"
17 #include "tkText.h"
18 
19 /*
20  * The 'TkWrapMode' enum in tkText.h is used to define a type for the -wrap
21  * option of tags in a Text widget. These values are used as indices into the
22  * string table below. Tags are allowed an empty wrap value, but the widget as
23  * a whole is not.
24  */
25 
26 static const char *wrapStrings[] = {
27     "char", "none", "word", "", NULL
28 };
29 
30 /*
31  * The 'TkTextTabStyle' enum in tkText.h is used to define a type for the
32  * -tabstyle option of the Text widget. These values are used as indices into
33  * the string table below. Tags are allowed an empty wrap value, but the
34  * widget as a whole is not.
35  */
36 
37 static const char *tabStyleStrings[] = {
38     "tabular", "wordprocessor", "", NULL
39 };
40 
41 static const Tk_OptionSpec tagOptionSpecs[] = {
42     {TK_OPTION_BORDER, "-background", NULL, NULL,
43 	NULL, -1, Tk_Offset(TkTextTag, border), TK_OPTION_NULL_OK, 0, 0},
44     {TK_OPTION_BITMAP, "-bgstipple", NULL, NULL,
45 	NULL, -1, Tk_Offset(TkTextTag, bgStipple), TK_OPTION_NULL_OK, 0, 0},
46     {TK_OPTION_PIXELS, "-borderwidth", NULL, NULL,
47 	NULL, Tk_Offset(TkTextTag, borderWidthPtr), Tk_Offset(TkTextTag, borderWidth),
48 	TK_OPTION_NULL_OK|TK_OPTION_DONT_SET_DEFAULT, 0, 0},
49     {TK_OPTION_STRING, "-elide", NULL, NULL,
50 	NULL, -1, Tk_Offset(TkTextTag, elideString),
51 	TK_OPTION_NULL_OK|TK_OPTION_DONT_SET_DEFAULT, 0, 0},
52     {TK_OPTION_BITMAP, "-fgstipple", NULL, NULL,
53 	NULL, -1, Tk_Offset(TkTextTag, fgStipple), TK_OPTION_NULL_OK, 0, 0},
54     {TK_OPTION_FONT, "-font", NULL, NULL,
55 	NULL, -1, Tk_Offset(TkTextTag, tkfont), TK_OPTION_NULL_OK, 0, 0},
56     {TK_OPTION_COLOR, "-foreground", NULL, NULL,
57 	NULL, -1, Tk_Offset(TkTextTag, fgColor), TK_OPTION_NULL_OK, 0, 0},
58     {TK_OPTION_STRING, "-justify", NULL, NULL,
59 	NULL, -1, Tk_Offset(TkTextTag, justifyString), TK_OPTION_NULL_OK, 0,0},
60     {TK_OPTION_STRING, "-lmargin1", NULL, NULL,
61 	NULL, -1, Tk_Offset(TkTextTag, lMargin1String), TK_OPTION_NULL_OK,0,0},
62     {TK_OPTION_STRING, "-lmargin2", NULL, NULL,
63 	NULL, -1, Tk_Offset(TkTextTag, lMargin2String), TK_OPTION_NULL_OK,0,0},
64     {TK_OPTION_STRING, "-offset", NULL, NULL,
65 	NULL, -1, Tk_Offset(TkTextTag, offsetString), TK_OPTION_NULL_OK, 0, 0},
66     {TK_OPTION_STRING, "-overstrike", NULL, NULL,
67 	NULL, -1, Tk_Offset(TkTextTag, overstrikeString),
68 	TK_OPTION_NULL_OK, 0, 0},
69     {TK_OPTION_STRING, "-relief", NULL, NULL,
70 	NULL, -1, Tk_Offset(TkTextTag, reliefString), TK_OPTION_NULL_OK, 0, 0},
71     {TK_OPTION_STRING, "-rmargin", NULL, NULL,
72 	NULL, -1, Tk_Offset(TkTextTag, rMarginString), TK_OPTION_NULL_OK, 0,0},
73     {TK_OPTION_STRING, "-spacing1", NULL, NULL,
74 	NULL, -1, Tk_Offset(TkTextTag, spacing1String), TK_OPTION_NULL_OK,0,0},
75     {TK_OPTION_STRING, "-spacing2", NULL, NULL,
76 	NULL, -1, Tk_Offset(TkTextTag, spacing2String), TK_OPTION_NULL_OK,0,0},
77     {TK_OPTION_STRING, "-spacing3", NULL, NULL,
78 	NULL, -1, Tk_Offset(TkTextTag, spacing3String), TK_OPTION_NULL_OK,0,0},
79     {TK_OPTION_STRING, "-tabs", NULL, NULL,
80 	NULL, Tk_Offset(TkTextTag, tabStringPtr), -1, TK_OPTION_NULL_OK, 0, 0},
81     {TK_OPTION_STRING_TABLE, "-tabstyle", NULL, NULL,
82 	NULL, -1, Tk_Offset(TkTextTag, tabStyle),
83 	TK_OPTION_NULL_OK, (ClientData) tabStyleStrings, 0},
84     {TK_OPTION_STRING, "-underline", NULL, NULL,
85 	NULL, -1, Tk_Offset(TkTextTag, underlineString),
86 	TK_OPTION_NULL_OK, 0, 0},
87     {TK_OPTION_STRING_TABLE, "-wrap", NULL, NULL,
88 	NULL, -1, Tk_Offset(TkTextTag, wrapMode),
89 	TK_OPTION_NULL_OK, (ClientData) wrapStrings, 0},
90     {TK_OPTION_END}
91 };
92 
93 /*
94  * Forward declarations for functions defined later in this file:
95  */
96 
97 static void		ChangeTagPriority(TkText *textPtr, TkTextTag *tagPtr,
98 			    int prio);
99 static TkTextTag *	FindTag(Tcl_Interp *interp, TkText *textPtr,
100 			    Tcl_Obj *tagName);
101 static void		SortTags(int numTags, TkTextTag **tagArrayPtr);
102 static int		TagSortProc(CONST VOID *first, CONST VOID *second);
103 static void             TagBindEvent(TkText *textPtr, XEvent *eventPtr,
104 			    int numTags, TkTextTag **tagArrayPtr);
105 
106 /*
107  *--------------------------------------------------------------
108  *
109  * TkTextTagCmd --
110  *
111  *	This function is invoked to process the "tag" options of the widget
112  *	command for text widgets. See the user documentation for details on
113  *	what it does.
114  *
115  * Results:
116  *	A standard Tcl result.
117  *
118  * Side effects:
119  *	See the user documentation.
120  *
121  *--------------------------------------------------------------
122  */
123 
124 int
TkTextTagCmd(register TkText * textPtr,Tcl_Interp * interp,int objc,Tcl_Obj * CONST objv[])125 TkTextTagCmd(
126     register TkText *textPtr,	/* Information about text widget. */
127     Tcl_Interp *interp,		/* Current interpreter. */
128     int objc,			/* Number of arguments. */
129     Tcl_Obj *CONST objv[])	/* Argument objects. Someone else has already
130 				 * parsed this command enough to know that
131 				 * objv[1] is "tag". */
132 {
133     static CONST char *tagOptionStrings[] = {
134 	"add", "bind", "cget", "configure", "delete", "lower", "names",
135 	"nextrange", "prevrange", "raise", "ranges", "remove", NULL
136     };
137     enum tagOptions {
138 	TAG_ADD, TAG_BIND, TAG_CGET, TAG_CONFIGURE, TAG_DELETE, TAG_LOWER,
139 	TAG_NAMES, TAG_NEXTRANGE, TAG_PREVRANGE, TAG_RAISE, TAG_RANGES,
140 	TAG_REMOVE
141     };
142     int optionIndex, i;
143     register TkTextTag *tagPtr;
144     TkTextIndex index1, index2;
145 
146     if (objc < 3) {
147 	Tcl_WrongNumArgs(interp, 2, objv, "option ?arg arg ...?");
148 	return TCL_ERROR;
149     }
150 
151     if (Tcl_GetIndexFromObj(interp, objv[2], tagOptionStrings,
152 	    "tag option", 0, &optionIndex) != TCL_OK) {
153 	return TCL_ERROR;
154     }
155 
156     switch ((enum tagOptions)optionIndex) {
157     case TAG_ADD:
158     case TAG_REMOVE: {
159 	int addTag;
160 
161 	if (((enum tagOptions)optionIndex) == TAG_ADD) {
162 	    addTag = 1;
163 	} else {
164 	    addTag = 0;
165 	}
166 	if (objc < 5) {
167 	    Tcl_WrongNumArgs(interp, 3, objv,
168 		    "tagName index1 ?index2 index1 index2 ...?");
169 	    return TCL_ERROR;
170 	}
171 	tagPtr = TkTextCreateTag(textPtr, Tcl_GetString(objv[3]), NULL);
172 	if (tagPtr->elide) {
173 		/*
174 		* Indices are potentially obsolete after adding or removing
175 		* elided character ranges, especially indices having "display"
176 		* or "any" submodifier, therefore increase the epoch.
177 		*/
178 		textPtr->sharedTextPtr->stateEpoch++;
179 	}
180 	for (i = 4; i < objc; i += 2) {
181 	    if (TkTextGetObjIndex(interp, textPtr, objv[i],
182 		    &index1) != TCL_OK) {
183 		return TCL_ERROR;
184 	    }
185 	    if (objc > (i+1)) {
186 		if (TkTextGetObjIndex(interp, textPtr, objv[i+1],
187 			&index2) != TCL_OK) {
188 		    return TCL_ERROR;
189 		}
190 		if (TkTextIndexCmp(&index1, &index2) >= 0) {
191 		    return TCL_OK;
192 		}
193 	    } else {
194 		index2 = index1;
195 		TkTextIndexForwChars(NULL,&index2, 1, &index2, COUNT_INDICES);
196 	    }
197 
198 	    if (tagPtr->affectsDisplay) {
199 		TkTextRedrawTag(textPtr->sharedTextPtr, NULL, &index1, &index2,
200 			tagPtr, !addTag);
201 	    } else {
202 		/*
203 		 * Still need to trigger enter/leave events on tags that have
204 		 * changed.
205 		 */
206 
207 		TkTextEventuallyRepick(textPtr);
208 	    }
209 	    if (TkBTreeTag(&index1, &index2, tagPtr, addTag)) {
210 		/*
211 		 * If the tag is "sel", and we actually adjusted something
212 		 * then grab the selection if we're supposed to export it and
213 		 * don't already have it.
214 		 *
215 		 * Also, invalidate partially-completed selection retrievals.
216 		 * We only need to check whether the tag is "sel" for this
217 		 * textPtr (not for other peer widget's "sel" tags) because we
218 		 * cannot reach this code path with a different widget's "sel"
219 		 * tag.
220 		 */
221 
222 		if (tagPtr == textPtr->selTagPtr) {
223 		    /*
224 		     * Send an event that the selection changed.  This is
225 		     * equivalent to:
226 		     *	   event generate $textWidget <<Selection>>
227 		     */
228 
229 		    TkTextSelectionEvent(textPtr);
230 
231 		    if (addTag && textPtr->exportSelection
232 			    && !(textPtr->flags & GOT_SELECTION)) {
233 			Tk_OwnSelection(textPtr->tkwin, XA_PRIMARY,
234 				TkTextLostSelection, (ClientData) textPtr);
235 			textPtr->flags |= GOT_SELECTION;
236 		    }
237 		    textPtr->abortSelections = 1;
238 		}
239 	    }
240 	}
241 	break;
242     }
243     case TAG_BIND:
244 	if ((objc < 4) || (objc > 6)) {
245 	    Tcl_WrongNumArgs(interp, 3, objv, "tagName ?sequence? ?command?");
246 	    return TCL_ERROR;
247 	}
248 	tagPtr = TkTextCreateTag(textPtr, Tcl_GetString(objv[3]), NULL);
249 
250 	/*
251 	 * Make a binding table if the widget doesn't already have one.
252 	 */
253 
254 	if (textPtr->sharedTextPtr->bindingTable == NULL) {
255 	    textPtr->sharedTextPtr->bindingTable =
256 		    Tk_CreateBindingTable(interp);
257 	}
258 
259 	if (objc == 6) {
260 	    int append = 0;
261 	    unsigned long mask;
262 	    char *fifth = Tcl_GetString(objv[5]);
263 
264 	    if (fifth[0] == 0) {
265 		return Tk_DeleteBinding(interp,
266 			textPtr->sharedTextPtr->bindingTable,
267 			(ClientData) tagPtr->name, Tcl_GetString(objv[4]));
268 	    }
269 	    if (fifth[0] == '+') {
270 		fifth++;
271 		append = 1;
272 	    }
273 	    mask = Tk_CreateBinding(interp,
274 		    textPtr->sharedTextPtr->bindingTable,
275 		    (ClientData) tagPtr->name, Tcl_GetString(objv[4]), fifth,
276 		    append);
277 	    if (mask == 0) {
278 		return TCL_ERROR;
279 	    }
280 	    if (mask & (unsigned) ~(ButtonMotionMask|Button1MotionMask
281 		    |Button2MotionMask|Button3MotionMask|Button4MotionMask
282 		    |Button5MotionMask|ButtonPressMask|ButtonReleaseMask
283 		    |EnterWindowMask|LeaveWindowMask|KeyPressMask
284 		    |KeyReleaseMask|PointerMotionMask|VirtualEventMask)) {
285 		Tk_DeleteBinding(interp, textPtr->sharedTextPtr->bindingTable,
286 			(ClientData) tagPtr->name, Tcl_GetString(objv[4]));
287 		Tcl_ResetResult(interp);
288 		Tcl_AppendResult(interp, "requested illegal events; ",
289 			"only key, button, motion, enter, leave, and virtual ",
290 			"events may be used", NULL);
291 		return TCL_ERROR;
292 	    }
293 	} else if (objc == 5) {
294 	    CONST char *command;
295 
296 	    command = Tk_GetBinding(interp,
297 		    textPtr->sharedTextPtr->bindingTable,
298 		    (ClientData) tagPtr->name, Tcl_GetString(objv[4]));
299 	    if (command == NULL) {
300 		CONST char *string = Tcl_GetStringResult(interp);
301 
302 		/*
303 		 * Ignore missing binding errors. This is a special hack that
304 		 * relies on the error message returned by FindSequence in
305 		 * tkBind.c.
306 		 */
307 
308 		if (string[0] != '\0') {
309 		    return TCL_ERROR;
310 		}
311 		Tcl_ResetResult(interp);
312 	    } else {
313 		Tcl_SetResult(interp, (char *) command, TCL_STATIC);
314 	    }
315 	} else {
316 	    Tk_GetAllBindings(interp, textPtr->sharedTextPtr->bindingTable,
317 		    (ClientData) tagPtr->name);
318 	}
319 	break;
320     case TAG_CGET:
321 	if (objc != 5) {
322 	    Tcl_WrongNumArgs(interp, 1, objv, "tag cget tagName option");
323 	    return TCL_ERROR;
324 	} else {
325 	    Tcl_Obj *objPtr;
326 
327 	    tagPtr = FindTag(interp, textPtr, objv[3]);
328 	    if (tagPtr == NULL) {
329 		return TCL_ERROR;
330 	    }
331 	    objPtr = Tk_GetOptionValue(interp, (char *) tagPtr,
332 		    tagPtr->optionTable, objv[4], textPtr->tkwin);
333 	    if (objPtr == NULL) {
334 		return TCL_ERROR;
335 	    }
336 	    Tcl_SetObjResult(interp, objPtr);
337 	    return TCL_OK;
338 	}
339 	break;
340     case TAG_CONFIGURE: {
341 	int newTag;
342 
343 	if (objc < 4) {
344 	    Tcl_WrongNumArgs(interp, 3, objv,
345 		    "tagName ?option? ?value? ?option value ...?");
346 	    return TCL_ERROR;
347 	}
348 	tagPtr = TkTextCreateTag(textPtr, Tcl_GetString(objv[3]), &newTag);
349 	if (objc <= 5) {
350 	    Tcl_Obj *objPtr = Tk_GetOptionInfo(interp, (char *) tagPtr,
351 		    tagPtr->optionTable,
352 		    (objc == 5) ? objv[4] : NULL, textPtr->tkwin);
353 
354 	    if (objPtr == NULL) {
355 		return TCL_ERROR;
356 	    }
357 	    Tcl_SetObjResult(interp, objPtr);
358 	    return TCL_OK;
359 	} else {
360 	    int result = TCL_OK;
361 
362 	    if (Tk_SetOptions(interp, (char*)tagPtr, tagPtr->optionTable,
363 		    objc-4, objv+4, textPtr->tkwin, NULL, NULL) != TCL_OK) {
364 		return TCL_ERROR;
365 	    }
366 
367 	    /*
368 	     * Some of the configuration options, like -underline and
369 	     * -justify, require additional translation (this is needed
370 	     * because we need to distinguish a particular value of an option
371 	     * from "unspecified").
372 	     */
373 
374 	    if (tagPtr->borderWidth < 0) {
375 		tagPtr->borderWidth = 0;
376 	    }
377 	    if (tagPtr->reliefString != NULL) {
378 		if (Tk_GetRelief(interp, tagPtr->reliefString,
379 			&tagPtr->relief) != TCL_OK) {
380 		    return TCL_ERROR;
381 		}
382 	    }
383 	    if (tagPtr->justifyString != NULL) {
384 		if (Tk_GetJustify(interp, tagPtr->justifyString,
385 			&tagPtr->justify) != TCL_OK) {
386 		    return TCL_ERROR;
387 		}
388 	    }
389 	    if (tagPtr->lMargin1String != NULL) {
390 		if (Tk_GetPixels(interp, textPtr->tkwin,
391 			tagPtr->lMargin1String, &tagPtr->lMargin1) != TCL_OK) {
392 		    return TCL_ERROR;
393 		}
394 	    }
395 	    if (tagPtr->lMargin2String != NULL) {
396 		if (Tk_GetPixels(interp, textPtr->tkwin,
397 			tagPtr->lMargin2String, &tagPtr->lMargin2) != TCL_OK) {
398 		    return TCL_ERROR;
399 		}
400 	    }
401 	    if (tagPtr->offsetString != NULL) {
402 		if (Tk_GetPixels(interp, textPtr->tkwin, tagPtr->offsetString,
403 			&tagPtr->offset) != TCL_OK) {
404 		    return TCL_ERROR;
405 		}
406 	    }
407 	    if (tagPtr->overstrikeString != NULL) {
408 		if (Tcl_GetBoolean(interp, tagPtr->overstrikeString,
409 			&tagPtr->overstrike) != TCL_OK) {
410 		    return TCL_ERROR;
411 		}
412 	    }
413 	    if (tagPtr->rMarginString != NULL) {
414 		if (Tk_GetPixels(interp, textPtr->tkwin,
415 			tagPtr->rMarginString, &tagPtr->rMargin) != TCL_OK) {
416 		    return TCL_ERROR;
417 		}
418 	    }
419 	    if (tagPtr->spacing1String != NULL) {
420 		if (Tk_GetPixels(interp, textPtr->tkwin,
421 			tagPtr->spacing1String, &tagPtr->spacing1) != TCL_OK) {
422 		    return TCL_ERROR;
423 		}
424 		if (tagPtr->spacing1 < 0) {
425 		    tagPtr->spacing1 = 0;
426 		}
427 	    }
428 	    if (tagPtr->spacing2String != NULL) {
429 		if (Tk_GetPixels(interp, textPtr->tkwin,
430 			tagPtr->spacing2String, &tagPtr->spacing2) != TCL_OK) {
431 		    return TCL_ERROR;
432 		}
433 		if (tagPtr->spacing2 < 0) {
434 		    tagPtr->spacing2 = 0;
435 		}
436 	    }
437 	    if (tagPtr->spacing3String != NULL) {
438 		if (Tk_GetPixels(interp, textPtr->tkwin,
439 			tagPtr->spacing3String, &tagPtr->spacing3) != TCL_OK) {
440 		    return TCL_ERROR;
441 		}
442 		if (tagPtr->spacing3 < 0) {
443 		    tagPtr->spacing3 = 0;
444 		}
445 	    }
446 	    if (tagPtr->tabArrayPtr != NULL) {
447 		ckfree((char *) tagPtr->tabArrayPtr);
448 		tagPtr->tabArrayPtr = NULL;
449 	    }
450 	    if (tagPtr->tabStringPtr != NULL) {
451 		tagPtr->tabArrayPtr =
452 			TkTextGetTabs(interp, textPtr, tagPtr->tabStringPtr);
453 		if (tagPtr->tabArrayPtr == NULL) {
454 		    return TCL_ERROR;
455 		}
456 	    }
457 	    if (tagPtr->underlineString != NULL) {
458 		if (Tcl_GetBoolean(interp, tagPtr->underlineString,
459 			&tagPtr->underline) != TCL_OK) {
460 		    return TCL_ERROR;
461 		}
462 	    }
463 	    if (tagPtr->elideString != NULL) {
464 		if (Tcl_GetBoolean(interp, tagPtr->elideString,
465 			&tagPtr->elide) != TCL_OK) {
466 		    return TCL_ERROR;
467 		}
468 	        /* Indices are potentially obsolete after changing -elide,
469 	         * especially those computed with "display" or "any"
470                  * submodifier, therefore increase the epoch.
471                  */
472 	        textPtr->sharedTextPtr->stateEpoch++;
473 	    }
474 
475 	    /*
476 	     * If the "sel" tag was changed, be sure to mirror information
477 	     * from the tag back into the text widget record. NOTE: we don't
478 	     * have to free up information in the widget record before
479 	     * overwriting it, because it was mirrored in the tag and hence
480 	     * freed when the tag field was overwritten.
481 	     */
482 
483 	    if (tagPtr == textPtr->selTagPtr) {
484 		textPtr->selBorder = tagPtr->border;
485 		textPtr->selBorderWidth = tagPtr->borderWidth;
486 		textPtr->selBorderWidthPtr = tagPtr->borderWidthPtr;
487 		textPtr->selFgColorPtr = tagPtr->fgColor;
488 	    }
489 
490 	    tagPtr->affectsDisplay = 0;
491 	    tagPtr->affectsDisplayGeometry = 0;
492 	    if ((tagPtr->elideString != NULL)
493 		    || (tagPtr->tkfont != None)
494 		    || (tagPtr->justifyString != NULL)
495 		    || (tagPtr->lMargin1String != NULL)
496 		    || (tagPtr->lMargin2String != NULL)
497 		    || (tagPtr->offsetString != NULL)
498 		    || (tagPtr->rMarginString != NULL)
499 		    || (tagPtr->spacing1String != NULL)
500 		    || (tagPtr->spacing2String != NULL)
501 		    || (tagPtr->spacing3String != NULL)
502 		    || (tagPtr->tabStringPtr != NULL)
503 		    || (tagPtr->tabStyle != TK_TEXT_TABSTYLE_NONE)
504 		    || (tagPtr->wrapMode != TEXT_WRAPMODE_NULL)) {
505 		tagPtr->affectsDisplay = 1;
506 		tagPtr->affectsDisplayGeometry = 1;
507 	    }
508 	    if ((tagPtr->border != NULL)
509 		    || (tagPtr->reliefString != NULL)
510 		    || (tagPtr->bgStipple != None)
511 		    || (tagPtr->fgColor != NULL)
512 		    || (tagPtr->fgStipple != None)
513 		    || (tagPtr->overstrikeString != NULL)
514 		    || (tagPtr->underlineString != NULL)) {
515 		tagPtr->affectsDisplay = 1;
516 	    }
517 	    if (!newTag) {
518 		/*
519 		 * This line is not necessary if this is a new tag, since it
520 		 * can't possibly have been applied to anything yet.
521 		 */
522 
523 		/*
524 		 * VMD: If this is the 'sel' tag, then we don't need to call
525 		 * this for all peers, unless we actually want to synchronize
526 		 * sel-style changes across the peers.
527 		 */
528 
529 		TkTextRedrawTag(textPtr->sharedTextPtr, NULL,
530 			NULL, NULL, tagPtr, 1);
531 	    }
532 	    return result;
533 	}
534 	break;
535     }
536     case TAG_DELETE: {
537 	Tcl_HashEntry *hPtr;
538 
539 	if (objc < 4) {
540 	    Tcl_WrongNumArgs(interp, 3, objv, "tagName ?tagName ...?");
541 	    return TCL_ERROR;
542 	}
543 	for (i = 3; i < objc; i++) {
544 	    hPtr = Tcl_FindHashEntry(&textPtr->sharedTextPtr->tagTable,
545 		    Tcl_GetString(objv[i]));
546 	    if (hPtr == NULL) {
547 		/*
548 		 * Either this tag doesn't exist or it's the 'sel' tag (which
549 		 * is not in the hash table). Either way we don't want to
550 		 * delete it.
551 		 */
552 
553 		continue;
554 	    }
555 	    tagPtr = (TkTextTag *) Tcl_GetHashValue(hPtr);
556 	    if (tagPtr == textPtr->selTagPtr) {
557 		continue;
558 	    }
559 	    if (tagPtr->affectsDisplay) {
560 		TkTextRedrawTag(textPtr->sharedTextPtr, NULL,
561 			NULL, NULL, tagPtr, 1);
562 	    }
563 	    TkTextDeleteTag(textPtr, tagPtr);
564 	    Tcl_DeleteHashEntry(hPtr);
565 	}
566 	break;
567     }
568     case TAG_LOWER: {
569 	TkTextTag *tagPtr2;
570 	int prio;
571 
572 	if ((objc != 4) && (objc != 5)) {
573 	    Tcl_WrongNumArgs(interp, 3, objv, "tagName ?belowThis?");
574 	    return TCL_ERROR;
575 	}
576 	tagPtr = FindTag(interp, textPtr, objv[3]);
577 	if (tagPtr == NULL) {
578 	    return TCL_ERROR;
579 	}
580 	if (objc == 5) {
581 	    tagPtr2 = FindTag(interp, textPtr, objv[4]);
582 	    if (tagPtr2 == NULL) {
583 		return TCL_ERROR;
584 	    }
585 	    if (tagPtr->priority < tagPtr2->priority) {
586 		prio = tagPtr2->priority - 1;
587 	    } else {
588 		prio = tagPtr2->priority;
589 	    }
590 	} else {
591 	    prio = 0;
592 	}
593 	ChangeTagPriority(textPtr, tagPtr, prio);
594 
595 	/*
596 	 * If this is the 'sel' tag, then we don't actually need to call this
597 	 * for all peers.
598 	 */
599 
600 	TkTextRedrawTag(textPtr->sharedTextPtr, NULL, NULL, NULL, tagPtr, 1);
601 	break;
602     }
603     case TAG_NAMES: {
604 	TkTextTag **arrayPtr;
605 	int arraySize;
606 	Tcl_Obj *listObj;
607 
608 	if ((objc != 3) && (objc != 4)) {
609 	    Tcl_WrongNumArgs(interp, 3, objv, "?index?");
610 	    return TCL_ERROR;
611 	}
612 	if (objc == 3) {
613 	    Tcl_HashSearch search;
614 	    Tcl_HashEntry *hPtr;
615 
616 	    arrayPtr = (TkTextTag **) ckalloc((unsigned)
617 		    (textPtr->sharedTextPtr->numTags * sizeof(TkTextTag *)));
618 	    for (i=0, hPtr = Tcl_FirstHashEntry(
619 		    &textPtr->sharedTextPtr->tagTable, &search);
620 		    hPtr != NULL; i++, hPtr = Tcl_NextHashEntry(&search)) {
621 		arrayPtr[i] = (TkTextTag *) Tcl_GetHashValue(hPtr);
622 	    }
623 
624 	    /*
625 	     * The 'sel' tag is not in the hash table.
626 	     */
627 
628 	    arrayPtr[i] = textPtr->selTagPtr;
629 	    arraySize = ++i;
630 	} else {
631 	    if (TkTextGetObjIndex(interp, textPtr, objv[3],
632 		    &index1) != TCL_OK) {
633 		return TCL_ERROR;
634 	    }
635 	    arrayPtr = TkBTreeGetTags(&index1, textPtr, &arraySize);
636 	    if (arrayPtr == NULL) {
637 		return TCL_OK;
638 	    }
639 	}
640 
641 	SortTags(arraySize, arrayPtr);
642 	listObj = Tcl_NewListObj(0, NULL);
643 
644 	for (i = 0; i < arraySize; i++) {
645 	    tagPtr = arrayPtr[i];
646 	    Tcl_ListObjAppendElement(interp, listObj,
647 		    Tcl_NewStringObj(tagPtr->name,-1));
648 	}
649 	Tcl_SetObjResult(interp, listObj);
650 	ckfree((char *) arrayPtr);
651 	break;
652     }
653     case TAG_NEXTRANGE: {
654 	TkTextIndex last;
655 	TkTextSearch tSearch;
656 	char position[TK_POS_CHARS];
657 
658 	if ((objc != 5) && (objc != 6)) {
659 	    Tcl_WrongNumArgs(interp, 3, objv, "tagName index1 ?index2?");
660 	    return TCL_ERROR;
661 	}
662 	tagPtr = FindTag(NULL, textPtr, objv[3]);
663 	if (tagPtr == NULL) {
664 	    return TCL_OK;
665 	}
666 	if (TkTextGetObjIndex(interp, textPtr, objv[4], &index1) != TCL_OK) {
667 	    return TCL_ERROR;
668 	}
669 	TkTextMakeByteIndex(textPtr->sharedTextPtr->tree, textPtr,
670 		TkBTreeNumLines(textPtr->sharedTextPtr->tree, textPtr),
671 		0, &last);
672 	if (objc == 5) {
673 	    index2 = last;
674 	} else if (TkTextGetObjIndex(interp, textPtr, objv[5],
675 		&index2) != TCL_OK) {
676 	    return TCL_ERROR;
677 	}
678 
679 	/*
680 	 * The search below is a bit tricky. Rather than use the B-tree
681 	 * facilities to stop the search at index2, let it search up until the
682 	 * end of the file but check for a position past index2 ourselves.
683 	 * The reason for doing it this way is that we only care whether the
684 	 * *start* of the range is before index2; once we find the start, we
685 	 * don't want TkBTreeNextTag to abort the search because the end of
686 	 * the range is after index2.
687 	 */
688 
689 	TkBTreeStartSearch(&index1, &last, tagPtr, &tSearch);
690 	if (TkBTreeCharTagged(&index1, tagPtr)) {
691 	    TkTextSegment *segPtr;
692 	    int offset;
693 
694 	    /*
695 	     * The first character is tagged. See if there is an on-toggle
696 	     * just before the character. If not, then skip to the end of this
697 	     * tagged range.
698 	     */
699 
700 	    for (segPtr = index1.linePtr->segPtr, offset = index1.byteIndex;
701 		    offset >= 0;
702 		    offset -= segPtr->size, segPtr = segPtr->nextPtr) {
703 		if ((offset == 0) && (segPtr->typePtr == &tkTextToggleOnType)
704 			&& (segPtr->body.toggle.tagPtr == tagPtr)) {
705 		    goto gotStart;
706 		}
707 	    }
708 	    if (!TkBTreeNextTag(&tSearch)) {
709 		return TCL_OK;
710 	    }
711 	}
712 
713 	/*
714 	 * Find the start of the tagged range.
715 	 */
716 
717 	if (!TkBTreeNextTag(&tSearch)) {
718 	    return TCL_OK;
719 	}
720 
721     gotStart:
722 	if (TkTextIndexCmp(&tSearch.curIndex, &index2) >= 0) {
723 	    return TCL_OK;
724 	}
725 	TkTextPrintIndex(textPtr, &tSearch.curIndex, position);
726 	Tcl_AppendElement(interp, position);
727 	TkBTreeNextTag(&tSearch);
728 	TkTextPrintIndex(textPtr, &tSearch.curIndex, position);
729 	Tcl_AppendElement(interp, position);
730 	break;
731     }
732     case TAG_PREVRANGE: {
733 	TkTextIndex last;
734 	TkTextSearch tSearch;
735 	char position1[TK_POS_CHARS];
736 	char position2[TK_POS_CHARS];
737 
738 	if ((objc != 5) && (objc != 6)) {
739 	    Tcl_WrongNumArgs(interp, 3, objv, "tagName index1 ?index2?");
740 	    return TCL_ERROR;
741 	}
742 	tagPtr = FindTag(NULL, textPtr, objv[3]);
743 	if (tagPtr == NULL) {
744 	    return TCL_OK;
745 	}
746 	if (TkTextGetObjIndex(interp, textPtr, objv[4], &index1) != TCL_OK) {
747 	    return TCL_ERROR;
748 	}
749 	if (objc == 5) {
750 	    TkTextMakeByteIndex(textPtr->sharedTextPtr->tree, textPtr, 0, 0,
751 		    &index2);
752 	} else if (TkTextGetObjIndex(interp, textPtr, objv[5],
753 		&index2) != TCL_OK) {
754 	    return TCL_ERROR;
755 	}
756 
757 	/*
758 	 * The search below is a bit weird. The previous toggle can be either
759 	 * an on or off toggle. If it is an on toggle, then we need to turn
760 	 * around and search forward for the end toggle. Otherwise we keep
761 	 * searching backwards.
762 	 */
763 
764 	TkBTreeStartSearchBack(&index1, &index2, tagPtr, &tSearch);
765 
766 	if (!TkBTreePrevTag(&tSearch)) {
767 	    /*
768 	     * Special case, there may be a tag off toggle at index1, and a
769 	     * tag on toggle before the start of a partial peer widget. In
770 	     * this case we missed it.
771 	     */
772 
773 	    if (textPtr->start != NULL && (textPtr->start == index2.linePtr)
774 		    && (index2.byteIndex == 0)
775 		    && TkBTreeCharTagged(&index2, tagPtr)
776 		    && (TkTextIndexCmp(&index2, &index1) < 0)) {
777 		/*
778 		 * The first character is tagged, so just add the range from
779 		 * the first char to the start of the range.
780 		 */
781 
782 		TkTextPrintIndex(textPtr, &index2, position1);
783 		TkTextPrintIndex(textPtr, &index1, position2);
784 		Tcl_AppendElement(interp, position1);
785 		Tcl_AppendElement(interp, position2);
786 	    }
787 	    return TCL_OK;
788 	}
789 
790 	if (tSearch.segPtr->typePtr == &tkTextToggleOnType) {
791 	    TkTextPrintIndex(textPtr, &tSearch.curIndex, position1);
792 	    if (textPtr->start != NULL) {
793 		/*
794 		 * Make sure the first index is not before the first allowed
795 		 * text index in this widget.
796 		 */
797 
798 		TkTextIndex firstIndex;
799 
800 		firstIndex.linePtr = textPtr->start;
801 		firstIndex.byteIndex = 0;
802 		firstIndex.textPtr = NULL;
803 		if (TkTextIndexCmp(&tSearch.curIndex, &firstIndex) < 0) {
804 		    if (TkTextIndexCmp(&firstIndex, &index1) >= 0) {
805 			/*
806 			 * But now the new first index is actually too far
807 			 * along in the text, so nothing is returned.
808 			 */
809 
810 			return TCL_OK;
811 		    }
812 		    TkTextPrintIndex(textPtr, &firstIndex, position1);
813 		}
814 	    }
815 	    TkTextMakeByteIndex(textPtr->sharedTextPtr->tree, textPtr,
816 		    TkBTreeNumLines(textPtr->sharedTextPtr->tree, textPtr),
817 		    0, &last);
818 	    TkBTreeStartSearch(&tSearch.curIndex, &last, tagPtr, &tSearch);
819 	    TkBTreeNextTag(&tSearch);
820 	    TkTextPrintIndex(textPtr, &tSearch.curIndex, position2);
821 	} else {
822 	    TkTextPrintIndex(textPtr, &tSearch.curIndex, position2);
823 	    TkBTreePrevTag(&tSearch);
824 	    TkTextPrintIndex(textPtr, &tSearch.curIndex, position1);
825 	    if (TkTextIndexCmp(&tSearch.curIndex, &index2) < 0) {
826 		if (textPtr->start != NULL && index2.linePtr == textPtr->start
827 			&& index2.byteIndex == 0) {
828 		    /* It's ok */
829 		    TkTextPrintIndex(textPtr, &index2, position1);
830 		} else {
831 		    return TCL_OK;
832 		}
833 	    }
834 	}
835 	Tcl_AppendElement(interp, position1);
836 	Tcl_AppendElement(interp, position2);
837 	break;
838     }
839     case TAG_RAISE: {
840 	TkTextTag *tagPtr2;
841 	int prio;
842 
843 	if ((objc != 4) && (objc != 5)) {
844 	    Tcl_WrongNumArgs(interp, 3, objv, "tagName ?aboveThis?");
845 	    return TCL_ERROR;
846 	}
847 	tagPtr = FindTag(interp, textPtr, objv[3]);
848 	if (tagPtr == NULL) {
849 	    return TCL_ERROR;
850 	}
851 	if (objc == 5) {
852 	    tagPtr2 = FindTag(interp, textPtr, objv[4]);
853 	    if (tagPtr2 == NULL) {
854 		return TCL_ERROR;
855 	    }
856 	    if (tagPtr->priority <= tagPtr2->priority) {
857 		prio = tagPtr2->priority;
858 	    } else {
859 		prio = tagPtr2->priority + 1;
860 	    }
861 	} else {
862 	    prio = textPtr->sharedTextPtr->numTags-1;
863 	}
864 	ChangeTagPriority(textPtr, tagPtr, prio);
865 
866 	/*
867 	 * If this is the 'sel' tag, then we don't actually need to call this
868 	 * for all peers.
869 	 */
870 
871 	TkTextRedrawTag(textPtr->sharedTextPtr, NULL, NULL, NULL, tagPtr, 1);
872 	break;
873     }
874     case TAG_RANGES: {
875 	TkTextIndex first, last;
876 	TkTextSearch tSearch;
877 	Tcl_Obj *listObj = Tcl_NewListObj(0, NULL);
878 	int count = 0;
879 
880 	if (objc != 4) {
881 	    Tcl_WrongNumArgs(interp, 3, objv, "tagName");
882 	    return TCL_ERROR;
883 	}
884 	tagPtr = FindTag(NULL, textPtr, objv[3]);
885 	if (tagPtr == NULL) {
886 	    return TCL_OK;
887 	}
888 	TkTextMakeByteIndex(textPtr->sharedTextPtr->tree, textPtr, 0, 0,
889 		&first);
890 	TkTextMakeByteIndex(textPtr->sharedTextPtr->tree, textPtr,
891 		TkBTreeNumLines(textPtr->sharedTextPtr->tree, textPtr),
892 		0, &last);
893 	TkBTreeStartSearch(&first, &last, tagPtr, &tSearch);
894 	if (TkBTreeCharTagged(&first, tagPtr)) {
895 	    Tcl_ListObjAppendElement(interp, listObj,
896 		    TkTextNewIndexObj(textPtr, &first));
897 	    count++;
898 	}
899 	while (TkBTreeNextTag(&tSearch)) {
900 	    Tcl_ListObjAppendElement(interp, listObj,
901 		    TkTextNewIndexObj(textPtr, &tSearch.curIndex));
902 	    count++;
903 	}
904 	if (count % 2 == 1) {
905 	    /*
906 	     * If a text widget uses '-end', it won't necessarily run to the
907 	     * end of the B-tree, and therefore the tag range might not be
908 	     * closed. In this case we add the end of the range.
909 	     */
910 
911 	    Tcl_ListObjAppendElement(interp, listObj,
912 		    TkTextNewIndexObj(textPtr, &last));
913 	}
914 	Tcl_SetObjResult(interp, listObj);
915 	break;
916     }
917     }
918     return TCL_OK;
919 }
920 
921 /*
922  *----------------------------------------------------------------------
923  *
924  * TkTextCreateTag --
925  *
926  *	Find the record describing a tag within a given text widget, creating
927  *	a new record if one doesn't already exist.
928  *
929  * Results:
930  *	The return value is a pointer to the TkTextTag record for tagName.
931  *
932  * Side effects:
933  *	A new tag record is created if there isn't one already defined for
934  *	tagName.
935  *
936  *----------------------------------------------------------------------
937  */
938 
939 TkTextTag *
TkTextCreateTag(TkText * textPtr,CONST char * tagName,int * newTag)940 TkTextCreateTag(
941     TkText *textPtr,		/* Widget in which tag is being used. */
942     CONST char *tagName,	/* Name of desired tag. */
943     int *newTag)		/* If non-NULL, then return 1 if new, or 0 if
944 				 * already exists. */
945 {
946     register TkTextTag *tagPtr;
947     Tcl_HashEntry *hPtr = NULL;
948     int isNew;
949     CONST char *name;
950 
951     if (!strcmp(tagName, "sel")) {
952         if (textPtr->selTagPtr != NULL) {
953 	    if (newTag != NULL) {
954 	        *newTag = 0;
955 	    }
956             return textPtr->selTagPtr;
957         }
958 	if (newTag != NULL) {
959 	    *newTag = 1;
960         }
961 	name = "sel";
962     } else {
963 	hPtr = Tcl_CreateHashEntry(&textPtr->sharedTextPtr->tagTable,
964 		tagName, &isNew);
965 	if (newTag != NULL) {
966 	    *newTag = isNew;
967 	}
968 	if (!isNew) {
969 	    return (TkTextTag *) Tcl_GetHashValue(hPtr);
970 	}
971 	name = Tcl_GetHashKey(&textPtr->sharedTextPtr->tagTable, hPtr);
972     }
973 
974     /*
975      * No existing entry. Create a new one, initialize it, and add a pointer
976      * to it to the hash table entry.
977      */
978 
979     tagPtr = (TkTextTag *) ckalloc(sizeof(TkTextTag));
980     tagPtr->name = name;
981     tagPtr->textPtr = NULL;
982     tagPtr->toggleCount = 0;
983     tagPtr->tagRootPtr = NULL;
984     tagPtr->priority = textPtr->sharedTextPtr->numTags;
985     tagPtr->border = NULL;
986     tagPtr->borderWidth = 0;
987     tagPtr->borderWidthPtr = NULL;
988     tagPtr->reliefString = NULL;
989     tagPtr->relief = TK_RELIEF_FLAT;
990     tagPtr->bgStipple = None;
991     tagPtr->fgColor = NULL;
992     tagPtr->tkfont = NULL;
993     tagPtr->fgStipple = None;
994     tagPtr->justifyString = NULL;
995     tagPtr->justify = TK_JUSTIFY_LEFT;
996     tagPtr->lMargin1String = NULL;
997     tagPtr->lMargin1 = 0;
998     tagPtr->lMargin2String = NULL;
999     tagPtr->lMargin2 = 0;
1000     tagPtr->offsetString = NULL;
1001     tagPtr->offset = 0;
1002     tagPtr->overstrikeString = NULL;
1003     tagPtr->overstrike = 0;
1004     tagPtr->rMarginString = NULL;
1005     tagPtr->rMargin = 0;
1006     tagPtr->spacing1String = NULL;
1007     tagPtr->spacing1 = 0;
1008     tagPtr->spacing2String = NULL;
1009     tagPtr->spacing2 = 0;
1010     tagPtr->spacing3String = NULL;
1011     tagPtr->spacing3 = 0;
1012     tagPtr->tabStringPtr = NULL;
1013     tagPtr->tabArrayPtr = NULL;
1014     tagPtr->tabStyle = TK_TEXT_TABSTYLE_NONE;
1015     tagPtr->underlineString = NULL;
1016     tagPtr->underline = 0;
1017     tagPtr->elideString = NULL;
1018     tagPtr->elide = 0;
1019     tagPtr->wrapMode = TEXT_WRAPMODE_NULL;
1020     tagPtr->affectsDisplay = 0;
1021     tagPtr->affectsDisplayGeometry = 0;
1022     textPtr->sharedTextPtr->numTags++;
1023     if (!strcmp(tagName, "sel")) {
1024 	tagPtr->textPtr = textPtr;
1025 	textPtr->refCount++;
1026     } else {
1027 	Tcl_SetHashValue(hPtr, tagPtr);
1028     }
1029     tagPtr->optionTable =
1030 	    Tk_CreateOptionTable(textPtr->interp, tagOptionSpecs);
1031     return tagPtr;
1032 }
1033 
1034 /*
1035  *----------------------------------------------------------------------
1036  *
1037  * FindTag --
1038  *
1039  *	See if tag is defined for a given widget.
1040  *
1041  * Results:
1042  *	If tagName is defined in textPtr, a pointer to its TkTextTag structure
1043  *	is returned. Otherwise NULL is returned and an error message is
1044  *	recorded in the interp's result unless interp is NULL.
1045  *
1046  * Side effects:
1047  *	None.
1048  *
1049  *----------------------------------------------------------------------
1050  */
1051 
1052 static TkTextTag *
FindTag(Tcl_Interp * interp,TkText * textPtr,Tcl_Obj * tagName)1053 FindTag(
1054     Tcl_Interp *interp,		/* Interpreter to use for error message; if
1055 				 * NULL, then don't record an error
1056 				 * message. */
1057     TkText *textPtr,		/* Widget in which tag is being used. */
1058     Tcl_Obj *tagName)	        /* Name of desired tag. */
1059 {
1060     Tcl_HashEntry *hPtr;
1061     int len;
1062     CONST char *str;
1063 
1064     str = Tcl_GetStringFromObj(tagName, &len);
1065     if (len == 3 && !strcmp(str,"sel")) {
1066         return textPtr->selTagPtr;
1067     }
1068     hPtr = Tcl_FindHashEntry(&textPtr->sharedTextPtr->tagTable,
1069 	    Tcl_GetString(tagName));
1070     if (hPtr != NULL) {
1071 	return (TkTextTag *) Tcl_GetHashValue(hPtr);
1072     }
1073     if (interp != NULL) {
1074 	Tcl_AppendResult(interp, "tag \"", Tcl_GetString(tagName),
1075 		"\" isn't defined in text widget", NULL);
1076     }
1077     return NULL;
1078 }
1079 
1080 /*
1081  *----------------------------------------------------------------------
1082  *
1083  * TkTextDeleteTag --
1084  *
1085  *	This function is called to carry out most actions associated with the
1086  *	'tag delete' sub-command. It will remove all evidence of the tag from
1087  *	the B-tree, and then call TkTextFreeTag to clean up the tag structure
1088  *	itself.
1089  *
1090  *	The only actions this doesn't carry out it to check if the deletion of
1091  *	the tag requires something to be re-displayed, and to remove the tag
1092  *	from the tagTable (hash table) if that is necessary (i.e. if it's not
1093  *	the 'sel' tag). It is expected that the caller carry out both of these
1094  *	actions.
1095  *
1096  * Results:
1097  *	None.
1098  *
1099  * Side effects:
1100  *	Memory and other resources are freed, the B-tree is manipulated.
1101  *
1102  *----------------------------------------------------------------------
1103  */
1104 
1105 void
TkTextDeleteTag(TkText * textPtr,register TkTextTag * tagPtr)1106 TkTextDeleteTag(
1107     TkText *textPtr,		/* Info about overall widget. */
1108     register TkTextTag *tagPtr)	/* Tag being deleted. */
1109 {
1110     TkTextIndex first, last;
1111 
1112     TkTextMakeByteIndex(textPtr->sharedTextPtr->tree, textPtr, 0, 0, &first);
1113     TkTextMakeByteIndex(textPtr->sharedTextPtr->tree, textPtr,
1114 	    TkBTreeNumLines(textPtr->sharedTextPtr->tree, textPtr), 0, &last),
1115     TkBTreeTag(&first, &last, tagPtr, 0);
1116 
1117     if (tagPtr == textPtr->selTagPtr) {
1118 	/*
1119 	 * Send an event that the selection changed. This is equivalent to:
1120 	 *	event generate $textWidget <<Selection>>
1121 	 */
1122 
1123 	TkTextSelectionEvent(textPtr);
1124     } else {
1125 	/*
1126 	 * Since all peer widgets have an independent "sel" tag, we
1127 	 * don't want removal of one sel tag to remove bindings which
1128 	 * are still valid in other peer widgets.
1129 	 */
1130 
1131 	if (textPtr->sharedTextPtr->bindingTable != NULL) {
1132 	    Tk_DeleteAllBindings(textPtr->sharedTextPtr->bindingTable,
1133 		    (ClientData) tagPtr->name);
1134 	}
1135     }
1136 
1137     /*
1138      * Update the tag priorities to reflect the deletion of this tag.
1139      */
1140 
1141     ChangeTagPriority(textPtr, tagPtr, textPtr->sharedTextPtr->numTags-1);
1142     textPtr->sharedTextPtr->numTags -= 1;
1143     TkTextFreeTag(textPtr, tagPtr);
1144 }
1145 
1146 /*
1147  *----------------------------------------------------------------------
1148  *
1149  * TkTextFreeTag --
1150  *
1151  *	This function is called when a tag is deleted to free up the memory
1152  *	and other resources associated with the tag.
1153  *
1154  * Results:
1155  *	None.
1156  *
1157  * Side effects:
1158  *	Memory and other resources are freed.
1159  *
1160  *----------------------------------------------------------------------
1161  */
1162 
1163 void
TkTextFreeTag(TkText * textPtr,register TkTextTag * tagPtr)1164 TkTextFreeTag(
1165     TkText *textPtr,		/* Info about overall widget. */
1166     register TkTextTag *tagPtr)	/* Tag being deleted. */
1167 {
1168     int i;
1169 
1170     /*
1171      * Let Tk do most of the hard work for us.
1172      */
1173 
1174     Tk_FreeConfigOptions((char *) tagPtr, tagPtr->optionTable,
1175 	    textPtr->tkwin);
1176 
1177     /*
1178      * This associated information is managed by us.
1179      */
1180 
1181     if (tagPtr->tabArrayPtr != NULL) {
1182 	ckfree((char *) tagPtr->tabArrayPtr);
1183     }
1184 
1185     /*
1186      * Make sure this tag isn't referenced from the 'current' tag array.
1187      */
1188 
1189     for (i = 0; i < textPtr->numCurTags; i++) {
1190 	if (textPtr->curTagArrayPtr[i] == tagPtr) {
1191 	    for (; i < textPtr->numCurTags-1; i++) {
1192 		textPtr->curTagArrayPtr[i] = textPtr->curTagArrayPtr[i+1];
1193 	    }
1194 	    textPtr->curTagArrayPtr[textPtr->numCurTags-1] = NULL;
1195 	    textPtr->numCurTags--;
1196 	    break;
1197 	}
1198     }
1199 
1200     /*
1201      * If this tag is widget-specific (peer widgets) then clean up the
1202      * refCount it holds.
1203      */
1204 
1205     if (tagPtr->textPtr != NULL) {
1206 	if (textPtr != tagPtr->textPtr) {
1207 	    Tcl_Panic("Tag being deleted from wrong widget");
1208 	}
1209 	textPtr->refCount--;
1210 	if (textPtr->refCount == 0) {
1211 	    ckfree((char *) textPtr);
1212 	}
1213 	tagPtr->textPtr = NULL;
1214     }
1215 
1216     /*
1217      * Finally free the tag's memory.
1218      */
1219 
1220     ckfree((char *) tagPtr);
1221 }
1222 
1223 /*
1224  *----------------------------------------------------------------------
1225  *
1226  * SortTags --
1227  *
1228  *	This function sorts an array of tag pointers in increasing order of
1229  *	priority, optimizing for the common case where the array is small.
1230  *
1231  * Results:
1232  *	None.
1233  *
1234  * Side effects:
1235  *	None.
1236  *
1237  *----------------------------------------------------------------------
1238  */
1239 
1240 static void
SortTags(int numTags,TkTextTag ** tagArrayPtr)1241 SortTags(
1242     int numTags,		/* Number of tag pointers at *tagArrayPtr. */
1243     TkTextTag **tagArrayPtr)	/* Pointer to array of pointers. */
1244 {
1245     int i, j, prio;
1246     register TkTextTag **tagPtrPtr;
1247     TkTextTag **maxPtrPtr, *tmp;
1248 
1249     if (numTags < 2) {
1250 	return;
1251     }
1252     if (numTags < 20) {
1253 	for (i = numTags-1; i > 0; i--, tagArrayPtr++) {
1254 	    maxPtrPtr = tagPtrPtr = tagArrayPtr;
1255 	    prio = tagPtrPtr[0]->priority;
1256 	    for (j = i, tagPtrPtr++; j > 0; j--, tagPtrPtr++) {
1257 		if (tagPtrPtr[0]->priority < prio) {
1258 		    prio = tagPtrPtr[0]->priority;
1259 		    maxPtrPtr = tagPtrPtr;
1260 		}
1261 	    }
1262 	    tmp = *maxPtrPtr;
1263 	    *maxPtrPtr = *tagArrayPtr;
1264 	    *tagArrayPtr = tmp;
1265 	}
1266     } else {
1267 	qsort(tagArrayPtr,(unsigned)numTags,sizeof(TkTextTag *),TagSortProc);
1268     }
1269 }
1270 
1271 /*
1272  *----------------------------------------------------------------------
1273  *
1274  * TagSortProc --
1275  *
1276  *	This function is called by qsort() when sorting an array of tags in
1277  *	priority order.
1278  *
1279  * Results:
1280  *	The return value is -1 if the first argument should be before the
1281  *	second element (i.e. it has lower priority), 0 if it's equivalent
1282  *	(this should never happen!), and 1 if it should be after the second
1283  *	element.
1284  *
1285  * Side effects:
1286  *	None.
1287  *
1288  *----------------------------------------------------------------------
1289  */
1290 
1291 static int
TagSortProc(CONST void * first,CONST void * second)1292 TagSortProc(
1293     CONST void *first,
1294     CONST void *second)		/* Elements to be compared. */
1295 {
1296     TkTextTag *tagPtr1, *tagPtr2;
1297 
1298     tagPtr1 = * (TkTextTag **) first;
1299     tagPtr2 = * (TkTextTag **) second;
1300     return tagPtr1->priority - tagPtr2->priority;
1301 }
1302 
1303 /*
1304  *----------------------------------------------------------------------
1305  *
1306  * ChangeTagPriority --
1307  *
1308  *	This function changes the priority of a tag by modifying its priority
1309  *	and the priorities of other tags that are affected by the change.
1310  *
1311  * Results:
1312  *	None.
1313  *
1314  * Side effects:
1315  *	Priorities may be changed for some or all of the tags in textPtr. The
1316  *	tags will be arranged so that there is exactly one tag at each
1317  *	priority level between 0 and textPtr->sharedTextPtr->numTags-1, with
1318  *	tagPtr at priority "prio".
1319  *
1320  *----------------------------------------------------------------------
1321  */
1322 
1323 static void
ChangeTagPriority(TkText * textPtr,TkTextTag * tagPtr,int prio)1324 ChangeTagPriority(
1325     TkText *textPtr,		/* Information about text widget. */
1326     TkTextTag *tagPtr,		/* Tag whose priority is to be changed. */
1327     int prio)			/* New priority for tag. */
1328 {
1329     int low, high, delta;
1330     register TkTextTag *tagPtr2;
1331     Tcl_HashEntry *hPtr;
1332     Tcl_HashSearch search;
1333 
1334     if (prio < 0) {
1335 	prio = 0;
1336     }
1337     if (prio >= textPtr->sharedTextPtr->numTags) {
1338 	prio = textPtr->sharedTextPtr->numTags-1;
1339     }
1340     if (prio == tagPtr->priority) {
1341 	return;
1342     }
1343     if (prio < tagPtr->priority) {
1344 	low = prio;
1345 	high = tagPtr->priority-1;
1346 	delta = 1;
1347     } else {
1348 	low = tagPtr->priority+1;
1349 	high = prio;
1350 	delta = -1;
1351     }
1352 
1353     /*
1354      * Adjust first the 'sel' tag, then all others from the hash table
1355      */
1356 
1357     if ((textPtr->selTagPtr->priority >= low)
1358 	    && (textPtr->selTagPtr->priority <= high)) {
1359 	textPtr->selTagPtr->priority += delta;
1360     }
1361     for (hPtr = Tcl_FirstHashEntry(&textPtr->sharedTextPtr->tagTable, &search);
1362 	    hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
1363 	tagPtr2 = (TkTextTag *) Tcl_GetHashValue(hPtr);
1364 	if ((tagPtr2->priority >= low) && (tagPtr2->priority <= high)) {
1365 	    tagPtr2->priority += delta;
1366 	}
1367     }
1368     tagPtr->priority = prio;
1369 }
1370 
1371 /*
1372  *--------------------------------------------------------------
1373  *
1374  * TkTextBindProc --
1375  *
1376  *	This function is invoked by the Tk dispatcher to handle events
1377  *	associated with bindings on items.
1378  *
1379  * Results:
1380  *	None.
1381  *
1382  * Side effects:
1383  *	Depends on the command invoked as part of the binding (if there was
1384  *	any).
1385  *
1386  *--------------------------------------------------------------
1387  */
1388 
1389 void
TkTextBindProc(ClientData clientData,XEvent * eventPtr)1390 TkTextBindProc(
1391     ClientData clientData,	/* Pointer to canvas structure. */
1392     XEvent *eventPtr)		/* Pointer to X event that just happened. */
1393 {
1394     TkText *textPtr = (TkText *) clientData;
1395     int repick  = 0;
1396 
1397 # define AnyButtonMask \
1398 	(Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask)
1399 
1400     textPtr->refCount++;
1401 
1402     /*
1403      * This code simulates grabs for mouse buttons by keeping track of whether
1404      * a button is pressed and refusing to pick a new current character while
1405      * a button is pressed.
1406      */
1407 
1408     if (eventPtr->type == ButtonPress) {
1409 	textPtr->flags |= BUTTON_DOWN;
1410     } else if (eventPtr->type == ButtonRelease) {
1411 	int mask;
1412 
1413 	switch (eventPtr->xbutton.button) {
1414 	case Button1:
1415 	    mask = Button1Mask;
1416 	    break;
1417 	case Button2:
1418 	    mask = Button2Mask;
1419 	    break;
1420 	case Button3:
1421 	    mask = Button3Mask;
1422 	    break;
1423 	case Button4:
1424 	    mask = Button4Mask;
1425 	    break;
1426 	case Button5:
1427 	    mask = Button5Mask;
1428 	    break;
1429 	default:
1430 	    mask = 0;
1431 	    break;
1432 	}
1433 	if ((eventPtr->xbutton.state & AnyButtonMask) == (unsigned) mask) {
1434 	    textPtr->flags &= ~BUTTON_DOWN;
1435 	    repick = 1;
1436 	}
1437     } else if ((eventPtr->type == EnterNotify)
1438 	    || (eventPtr->type == LeaveNotify)) {
1439 	if (eventPtr->xcrossing.state & AnyButtonMask)  {
1440 	    textPtr->flags |= BUTTON_DOWN;
1441 	} else {
1442 	    textPtr->flags &= ~BUTTON_DOWN;
1443 	}
1444 	TkTextPickCurrent(textPtr, eventPtr);
1445 	goto done;
1446     } else if (eventPtr->type == MotionNotify) {
1447 	if (eventPtr->xmotion.state & AnyButtonMask)  {
1448 	    textPtr->flags |= BUTTON_DOWN;
1449 	} else {
1450 	    textPtr->flags &= ~BUTTON_DOWN;
1451 	}
1452 	TkTextPickCurrent(textPtr, eventPtr);
1453     }
1454     if ((textPtr->numCurTags > 0)
1455 	    && (textPtr->sharedTextPtr->bindingTable != NULL)
1456 	    && (textPtr->tkwin != NULL) && !(textPtr->flags & DESTROYED)) {
1457 	TagBindEvent(textPtr, eventPtr, textPtr->numCurTags,
1458 		textPtr->curTagArrayPtr);
1459     }
1460     if (repick) {
1461 	unsigned int oldState;
1462 
1463 	oldState = eventPtr->xbutton.state;
1464 	eventPtr->xbutton.state &= ~(Button1Mask|Button2Mask
1465 		|Button3Mask|Button4Mask|Button5Mask);
1466 	if (!(textPtr->flags & DESTROYED)) {
1467 	    TkTextPickCurrent(textPtr, eventPtr);
1468 	}
1469 	eventPtr->xbutton.state = oldState;
1470     }
1471 
1472   done:
1473     if (--textPtr->refCount == 0) {
1474 	ckfree((char *) textPtr);
1475     }
1476 }
1477 
1478 /*
1479  *--------------------------------------------------------------
1480  *
1481  * TkTextPickCurrent --
1482  *
1483  *	Find the character containing the coordinates in an event and place
1484  *	the "current" mark on that character. If the "current" mark has moved
1485  *	then generate a fake leave event on the old current character and a
1486  *	fake enter event on the new current character.
1487  *
1488  * Results:
1489  *	None.
1490  *
1491  * Side effects:
1492  *	The current mark for textPtr may change. If it does, then the commands
1493  *	associated with character entry and leave could do just about
1494  *	anything. For example, the text widget might be deleted. It is up to
1495  *	the caller to protect itself by incrementing the refCount of the text
1496  *	widget.
1497  *
1498  *--------------------------------------------------------------
1499  */
1500 
1501 void
TkTextPickCurrent(register TkText * textPtr,XEvent * eventPtr)1502 TkTextPickCurrent(
1503     register TkText *textPtr,	/* Text widget in which to select current
1504 				 * character. */
1505     XEvent *eventPtr)		/* Event describing location of mouse cursor.
1506 				 * Must be EnterWindow, LeaveWindow,
1507 				 * ButtonRelease, or MotionNotify. */
1508 {
1509     TkTextIndex index;
1510     TkTextTag **oldArrayPtr, **newArrayPtr;
1511     TkTextTag **copyArrayPtr = NULL;
1512 				/* Initialization needed to prevent compiler
1513 				 * warning. */
1514     int numOldTags, numNewTags, i, j, size, nearby;
1515     XEvent event;
1516 
1517     /*
1518      * If a button is down, then don't do anything at all; we'll be called
1519      * again when all buttons are up, and we can repick then. This implements
1520      * a form of mouse grabbing.
1521      */
1522 
1523     if (textPtr->flags & BUTTON_DOWN) {
1524 	if (((eventPtr->type == EnterNotify)
1525 		|| (eventPtr->type == LeaveNotify))
1526 		&& ((eventPtr->xcrossing.mode == NotifyGrab)
1527 		|| (eventPtr->xcrossing.mode == NotifyUngrab))) {
1528 	    /*
1529 	     * Special case: the window is being entered or left because of a
1530 	     * grab or ungrab. In this case, repick after all. Furthermore,
1531 	     * clear BUTTON_DOWN to release the simulated grab.
1532 	     */
1533 
1534 	    textPtr->flags &= ~BUTTON_DOWN;
1535 	} else {
1536 	    return;
1537 	}
1538     }
1539 
1540     /*
1541      * Save information about this event in the widget in case we have to
1542      * synthesize more enter and leave events later (e.g. because a character
1543      * was deleted, causing a new character to be underneath the mouse
1544      * cursor). Also translate MotionNotify events into EnterNotify events,
1545      * since that's what gets reported to event handlers when the current
1546      * character changes.
1547      */
1548 
1549     if (eventPtr != &textPtr->pickEvent) {
1550 	if ((eventPtr->type == MotionNotify)
1551 		|| (eventPtr->type == ButtonRelease)) {
1552 	    textPtr->pickEvent.xcrossing.type = EnterNotify;
1553 	    textPtr->pickEvent.xcrossing.serial = eventPtr->xmotion.serial;
1554 	    textPtr->pickEvent.xcrossing.send_event
1555 		    = eventPtr->xmotion.send_event;
1556 	    textPtr->pickEvent.xcrossing.display = eventPtr->xmotion.display;
1557 	    textPtr->pickEvent.xcrossing.window = eventPtr->xmotion.window;
1558 	    textPtr->pickEvent.xcrossing.root = eventPtr->xmotion.root;
1559 	    textPtr->pickEvent.xcrossing.subwindow = None;
1560 	    textPtr->pickEvent.xcrossing.time = eventPtr->xmotion.time;
1561 	    textPtr->pickEvent.xcrossing.x = eventPtr->xmotion.x;
1562 	    textPtr->pickEvent.xcrossing.y = eventPtr->xmotion.y;
1563 	    textPtr->pickEvent.xcrossing.x_root = eventPtr->xmotion.x_root;
1564 	    textPtr->pickEvent.xcrossing.y_root = eventPtr->xmotion.y_root;
1565 	    textPtr->pickEvent.xcrossing.mode = NotifyNormal;
1566 	    textPtr->pickEvent.xcrossing.detail = NotifyNonlinear;
1567 	    textPtr->pickEvent.xcrossing.same_screen
1568 		    = eventPtr->xmotion.same_screen;
1569 	    textPtr->pickEvent.xcrossing.focus = False;
1570 	    textPtr->pickEvent.xcrossing.state = eventPtr->xmotion.state;
1571 	} else  {
1572 	    textPtr->pickEvent = *eventPtr;
1573 	}
1574     }
1575 
1576     /*
1577      * Find the new current character, then find and sort all of the tags
1578      * associated with it.
1579      */
1580 
1581     if (textPtr->pickEvent.type != LeaveNotify) {
1582 	TkTextPixelIndex(textPtr, textPtr->pickEvent.xcrossing.x,
1583 		textPtr->pickEvent.xcrossing.y, &index, &nearby);
1584 	if (nearby) {
1585 	    newArrayPtr = NULL;
1586 	    numNewTags = 0;
1587 	} else {
1588 	    newArrayPtr = TkBTreeGetTags(&index, textPtr, &numNewTags);
1589 	    SortTags(numNewTags, newArrayPtr);
1590 	}
1591     } else {
1592 	newArrayPtr = NULL;
1593 	numNewTags = 0;
1594     }
1595 
1596     /*
1597      * Resort the tags associated with the previous marked character (the
1598      * priorities might have changed), then make a copy of the new tags, and
1599      * compare the old tags to the copy, nullifying any tags that are present
1600      * in both groups (i.e. the tags that haven't changed).
1601      */
1602 
1603     SortTags(textPtr->numCurTags, textPtr->curTagArrayPtr);
1604     if (numNewTags > 0) {
1605 	size = numNewTags * sizeof(TkTextTag *);
1606 	copyArrayPtr = (TkTextTag **) ckalloc((unsigned) size);
1607 	memcpy(copyArrayPtr, newArrayPtr, (size_t) size);
1608 	for (i = 0; i < textPtr->numCurTags; i++) {
1609 	    for (j = 0; j < numNewTags; j++) {
1610 		if (textPtr->curTagArrayPtr[i] == copyArrayPtr[j]) {
1611 		    textPtr->curTagArrayPtr[i] = NULL;
1612 		    copyArrayPtr[j] = NULL;
1613 		    break;
1614 		}
1615 	    }
1616 	}
1617     }
1618 
1619     /*
1620      * Invoke the binding system with a LeaveNotify event for all of the tags
1621      * that have gone away. We have to be careful here, because it's possible
1622      * that the binding could do something (like calling tkwait) that
1623      * eventually modifies textPtr->curTagArrayPtr. To avoid problems in
1624      * situations like this, update curTagArrayPtr to its new value before
1625      * invoking any bindings, and don't use it any more here.
1626      */
1627 
1628     numOldTags = textPtr->numCurTags;
1629     textPtr->numCurTags = numNewTags;
1630     oldArrayPtr = textPtr->curTagArrayPtr;
1631     textPtr->curTagArrayPtr = newArrayPtr;
1632     if (numOldTags != 0) {
1633 	if ((textPtr->sharedTextPtr->bindingTable != NULL)
1634 		&& (textPtr->tkwin != NULL)
1635 		&& !(textPtr->flags & DESTROYED)) {
1636 	    event = textPtr->pickEvent;
1637 	    event.type = LeaveNotify;
1638 
1639 	    /*
1640 	     * Always use a detail of NotifyAncestor. Besides being
1641 	     * consistent, this avoids problems where the binding code will
1642 	     * discard NotifyInferior events.
1643 	     */
1644 
1645 	    event.xcrossing.detail = NotifyAncestor;
1646 	    TagBindEvent(textPtr, &event, numOldTags, oldArrayPtr);
1647 	}
1648 	ckfree((char *) oldArrayPtr);
1649     }
1650 
1651     /*
1652      * Reset the "current" mark (be careful to recompute its location, since
1653      * it might have changed during an event binding). Then invoke the binding
1654      * system with an EnterNotify event for all of the tags that have just
1655      * appeared.
1656      */
1657 
1658     TkTextPixelIndex(textPtr, textPtr->pickEvent.xcrossing.x,
1659 	    textPtr->pickEvent.xcrossing.y, &index, &nearby);
1660     TkTextSetMark(textPtr, "current", &index);
1661     if (numNewTags != 0) {
1662 	if ((textPtr->sharedTextPtr->bindingTable != NULL)
1663 		&& (textPtr->tkwin != NULL)
1664 		&& !(textPtr->flags & DESTROYED) && !nearby) {
1665 	    event = textPtr->pickEvent;
1666 	    event.type = EnterNotify;
1667 	    event.xcrossing.detail = NotifyAncestor;
1668 	    TagBindEvent(textPtr, &event, numNewTags, copyArrayPtr);
1669 	}
1670 	ckfree((char *) copyArrayPtr);
1671     }
1672 }
1673 
1674 /*
1675  *--------------------------------------------------------------
1676  *
1677  * TagBindEvent --
1678  *
1679  *	Trigger given events for all tags that match the relevant bindings.
1680  *	To handle the "sel" tag correctly in all peer widgets, we must use the
1681  *	name of the tags as the binding table element.
1682  *
1683  * Results:
1684  *	None.
1685  *
1686  * Side effects:
1687  *	Almost anything can be triggered by tag bindings, including deletion
1688  *	of the text widget.
1689  *
1690  *--------------------------------------------------------------
1691  */
1692 
1693 static void
TagBindEvent(TkText * textPtr,XEvent * eventPtr,int numTags,TkTextTag ** tagArrayPtr)1694 TagBindEvent(
1695     TkText *textPtr,		/* Text widget to fire bindings in. */
1696     XEvent *eventPtr,		/* What actually happened. */
1697     int numTags,		/* Number of relevant tags. */
1698     TkTextTag **tagArrayPtr)	/* Array of relevant tags. */
1699 {
1700     #define NUM_BIND_TAGS 10
1701     CONST char *nameArray[NUM_BIND_TAGS];
1702     CONST char **nameArrPtr;
1703     int i;
1704 
1705     /*
1706      * Try to avoid allocation unless there are lots of tags.
1707      */
1708 
1709     if (numTags > NUM_BIND_TAGS) {
1710 	nameArrPtr = (CONST char **) ckalloc(numTags * sizeof(CONST char *));
1711     } else {
1712 	nameArrPtr = nameArray;
1713     }
1714 
1715     /*
1716      * We use tag names as keys in the hash table. We do this instead of using
1717      * the actual tagPtr objects because we want one "sel" tag binding for all
1718      * peer widgets, despite the fact that each has its own tagPtr object.
1719      */
1720 
1721     for (i = 0; i < numTags; i++) {
1722 	TkTextTag *tagPtr = tagArrayPtr[i];
1723 	if (tagPtr != NULL) {
1724 	    nameArrPtr[i] = tagPtr->name;
1725 	} else {
1726 	    /*
1727 	     * Tag has been deleted elsewhere, and therefore nulled out in
1728 	     * this array. Tk_BindEvent is clever enough to cope with NULLs
1729 	     * being thrown at it.
1730 	     */
1731 
1732 	    nameArrPtr[i] = NULL;
1733 	}
1734     }
1735     Tk_BindEvent(textPtr->sharedTextPtr->bindingTable, eventPtr,
1736 	    textPtr->tkwin, numTags, (ClientData *) nameArrPtr);
1737 
1738     if (numTags > NUM_BIND_TAGS) {
1739 	ckfree((char *) nameArrPtr);
1740     }
1741 }
1742 
1743 /*
1744  * Local Variables:
1745  * mode: c
1746  * c-basic-offset: 4
1747  * fill-column: 78
1748  * End:
1749  */
1750