Debian Codegraph @

pTk/mTk/generic/tkTextTag.c

raw symbols SCIP
1 /*
2 * tkTextTag.c --
3 *
4 * This module implements the "tag" subcommand of the widget command
5 * for text widgets, plus most of the other high-level functions
6 * related to 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
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * RCS: @(#) $Id: tkTextTag.c,v 1.8 2002/08/05 04:30:40 dgp Exp $
15 */
16
17 #include "default.h"
18 #include "tkPort.h"
19 #include "tkInt.h"
20 #include "tkText.h"
21
22 static Tk_ConfigSpec tagConfigSpecs[] = {
23 {TK_CONFIG_BORDER, "-background", (char *) NULL, (char *) NULL,
24 (char *) NULL, Tk_Offset(TkTextTag, border), TK_CONFIG_NULL_OK},
25 {TK_CONFIG_BITMAP, "-bgstipple", (char *) NULL, (char *) NULL,
26 (char *) NULL, Tk_Offset(TkTextTag, bgStipple), TK_CONFIG_NULL_OK},
27 {TK_CONFIG_STRING, "-borderwidth", (char *) NULL, (char *) NULL,
28 "0", Tk_Offset(TkTextTag, bdString),
29 TK_CONFIG_DONT_SET_DEFAULT|TK_CONFIG_NULL_OK},
30 {TK_CONFIG_LANGARG, "-elide", (char *) NULL, (char *) NULL,
31 "0", Tk_Offset(TkTextTag, elideString),
32 TK_CONFIG_DONT_SET_DEFAULT|TK_CONFIG_NULL_OK},
33 {TK_CONFIG_BITMAP, "-fgstipple", (char *) NULL, (char *) NULL,
34 (char *) NULL, Tk_Offset(TkTextTag, fgStipple), TK_CONFIG_NULL_OK},
35 {TK_CONFIG_FONT, "-font", (char *) NULL, (char *) NULL,
36 (char *) NULL, Tk_Offset(TkTextTag, tkfont), TK_CONFIG_NULL_OK},
37 {TK_CONFIG_COLOR, "-foreground", (char *) NULL, (char *) NULL,
38 (char *) NULL, Tk_Offset(TkTextTag, fgColor), TK_CONFIG_NULL_OK},
39 {TK_CONFIG_STRING, "-justify", (char *) NULL, (char *) NULL,
40 (char *) NULL, Tk_Offset(TkTextTag, justifyString), TK_CONFIG_NULL_OK},
41 {TK_CONFIG_STRING, "-lmargin1", (char *) NULL, (char *) NULL,
42 (char *) NULL, Tk_Offset(TkTextTag, lMargin1String), TK_CONFIG_NULL_OK},
43 {TK_CONFIG_STRING, "-lmargin2", (char *) NULL, (char *) NULL,
44 (char *) NULL, Tk_Offset(TkTextTag, lMargin2String), TK_CONFIG_NULL_OK},
45 {TK_CONFIG_STRING, "-offset", (char *) NULL, (char *) NULL,
46 (char *) NULL, Tk_Offset(TkTextTag, offsetString), TK_CONFIG_NULL_OK},
47 {TK_CONFIG_LANGARG, "-overstrike", (char *) NULL, (char *) NULL,
48 (char *) NULL, Tk_Offset(TkTextTag, overstrikeString),
49 TK_CONFIG_NULL_OK},
50 {TK_CONFIG_STRING, "-relief", (char *) NULL, (char *) NULL,
51 (char *) NULL, Tk_Offset(TkTextTag, reliefString), TK_CONFIG_NULL_OK},
52 {TK_CONFIG_STRING, "-rmargin", (char *) NULL, (char *) NULL,
53 (char *) NULL, Tk_Offset(TkTextTag, rMarginString), TK_CONFIG_NULL_OK},
54 {TK_CONFIG_STRING, "-spacing1", (char *) NULL, (char *) NULL,
55 (char *) NULL, Tk_Offset(TkTextTag, spacing1String), TK_CONFIG_NULL_OK},
56 {TK_CONFIG_STRING, "-spacing2", (char *) NULL, (char *) NULL,
57 (char *) NULL, Tk_Offset(TkTextTag, spacing2String), TK_CONFIG_NULL_OK},
58 {TK_CONFIG_STRING, "-spacing3", (char *) NULL, (char *) NULL,
59 (char *) NULL, Tk_Offset(TkTextTag, spacing3String), TK_CONFIG_NULL_OK},
60 {TK_CONFIG_LANGARG, "-tabs", (char *) NULL, (char *) NULL,
61 (char *) NULL, Tk_Offset(TkTextTag, tabString), TK_CONFIG_NULL_OK},
62 {TK_CONFIG_LANGARG, "-underline", (char *) NULL, (char *) NULL,
63 (char *) NULL, Tk_Offset(TkTextTag, underlineString),
64 TK_CONFIG_NULL_OK},
65 {TK_CONFIG_CUSTOM, "-wrap", (char *) NULL, (char *) NULL,
66 (char *) NULL, Tk_Offset(TkTextTag, wrapMode),
67 TK_CONFIG_NULL_OK, &textWrapModeOption},
68 {TK_CONFIG_LANGARG, "-data", (char *) NULL, (char *) NULL,
69 (char *) NULL, Tk_Offset(TkTextTag, userData),
70 TK_CONFIG_NULL_OK},
71 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
72 (char *) NULL, 0, 0}
73 };
74
75 /*
76 * Forward declarations for procedures defined later in this file:
77 */
78
79 static void ChangeTagPriority _ANSI_ARGS_((TkText *textPtr,
80 TkTextTag *tagPtr, int prio));
81 static TkTextTag * FindTag _ANSI_ARGS_((Tcl_Interp *interp,
82 TkText *textPtr, CONST char *tagName));
83 static void SortTags _ANSI_ARGS_((int numTags,
84 TkTextTag **tagArrayPtr));
85 static int TagSortProc _ANSI_ARGS_((CONST VOID *first,
86 CONST VOID *second));
87
88 /*
89 *--------------------------------------------------------------
90 *
91 * TkTextTagCmd --
92 *
93 * This procedure is invoked to process the "tag" options of
94 * the widget command for text widgets. See the user documentation
95 * for details on what it does.
96 *
97 * Results:
98 * A standard Tcl result.
99 *
100 * Side effects:
101 * See the user documentation.
102 *
103 *--------------------------------------------------------------
104 */
105
106 int
107 TkTextTagCmd(textPtr, interp, argc, argv)
108 register TkText *textPtr; /* Information about text widget. */
109 Tcl_Interp *interp; /* Current interpreter. */
110 int argc; /* Number of arguments. */
111 CONST char **argv; /* Argument strings. Someone else has already
112 * parsed this command enough to know that
113 * argv[1] is "tag". */
114 {
115 int c, i, addTag;
116 size_t length;
117 char *fullOption;
118 register TkTextTag *tagPtr;
119 TkTextIndex first, last, index1, index2;
120
121 if (argc < 3) {
122 Tcl_AppendResult(interp, "wrong # args: should be \"",
123 argv[0], " tag option ?arg arg ...?\"", (char *) NULL);
124 return TCL_ERROR;
125 }
126 c = argv[2][0];
127 length = strlen(argv[2]);
128 if ((c == 'a') && (strncmp(argv[2], "add", length) == 0)) {
129 fullOption = "add";
130 addTag = 1;
131
132 addAndRemove:
133 if (argc < 5) {
134 Tcl_AppendResult(interp, "wrong # args: should be \"",
135 argv[0], " tag ", fullOption,
136 " tagName index1 ?index2 index1 index2 ...?\"",
137 (char *) NULL);
138 return TCL_ERROR;
139 }
140 tagPtr = TkTextCreateTag(textPtr, argv[3]);
141 for (i = 4; i < argc; i += 2) {
142 if (TkTextGetIndex(interp, textPtr, argv[i], &index1) != TCL_OK) {
143 return TCL_ERROR;
144 }
145 if (argc > (i+1)) {
146 if (TkTextGetIndex(interp, textPtr, argv[i+1], &index2)
147 != TCL_OK) {
148 return TCL_ERROR;
149 }
150 if (TkTextIndexCmp(&index1, &index2) >= 0) {
151 return TCL_OK;
152 }
153 } else {
154 index2 = index1;
155 TkTextIndexForwChars(&index2, 1, &index2);
156 }
157
158 if (tagPtr->affectsDisplay) {
159 TkTextRedrawTag(textPtr, &index1, &index2, tagPtr, !addTag);
160 } else {
161 /*
162 * Still need to trigger enter/leave events on tags that
163 * have changed.
164 */
165
166 TkTextEventuallyRepick(textPtr);
167 }
168 TkBTreeTag(&index1, &index2, tagPtr, addTag);
169
170 /*
171 * If the tag is "sel" then grab the selection if we're supposed
172 * to export it and don't already have it. Also, invalidate
173 * partially-completed selection retrievals.
174 */
175
176 if (tagPtr == textPtr->selTagPtr) {
177 XEvent event;
178 /*
179 * Send an event that the selection changed.
180 * This is equivalent to
181 * "event generate $textWidget <<Selection>>"
182 */
183
184 memset((VOID *) &event, 0, sizeof(event));
185 event.xany.type = VirtualEvent;
186 event.xany.serial = NextRequest(Tk_Display(textPtr->tkwin));
187 event.xany.send_event = False;
188 event.xany.window = Tk_WindowId(textPtr->tkwin);
189 event.xany.display = Tk_Display(textPtr->tkwin);
190 ((XVirtualEvent *) &event)->name = Tk_GetUid("Selection");
191 Tk_HandleEvent(&event);
192
193 if (addTag && textPtr->exportSelection
194 && !(textPtr->flags & GOT_SELECTION)) {
195 Tk_OwnSelection(textPtr->tkwin, XA_PRIMARY,
196 TkTextLostSelection, (ClientData) textPtr);
197 textPtr->flags |= GOT_SELECTION;
198 }
199 textPtr->abortSelections = 1;
200 }
201 }
202 } else if ((c == 'b') && (strncmp(argv[2], "bind", length) == 0)) {
203 if ((argc < 4) || (argc > 6)) {
204 Tcl_AppendResult(interp, "wrong # args: should be \"",
205 argv[0], " tag bind tagName ?sequence? ?command?\"",
206 (char *) NULL);
207 return TCL_ERROR;
208 }
209 tagPtr = TkTextCreateTag(textPtr, argv[3]);
210
211 /*
212 * Make a binding table if the widget doesn't already have
213 * one.
214 */
215
216 if (textPtr->bindingTable == NULL) {
217 textPtr->bindingTable = Tk_CreateBindingTable(interp);
218 }
219
220 if (argc == 6) {
221 int append = 0;
222 unsigned long mask;
223
224 if (argv[5][0] == 0) {
225 return Tk_DeleteBinding(interp, textPtr->bindingTable,
226 (ClientData) tagPtr, argv[4]);
227 }
228 #ifndef _LANG
229 if (argv[5][0] == '+') {
230 argv[5]++;
231 append = 1;
232 }
233 #endif
234 mask = Tk_CreateBinding(interp, textPtr->bindingTable,
235 (ClientData) tagPtr, argv[4], objv[5], append);
236 if (mask == 0) {
237 return TCL_ERROR;
238 }
239 if (mask & (unsigned) ~(ButtonMotionMask|Button1MotionMask
240 |Button2MotionMask|Button3MotionMask|Button4MotionMask
241 |Button5MotionMask|ButtonPressMask|ButtonReleaseMask
242 |EnterWindowMask|LeaveWindowMask|KeyPressMask
243 |KeyReleaseMask|PointerMotionMask|VirtualEventMask)) {
244 Tk_DeleteBinding(interp, textPtr->bindingTable,
245 (ClientData) tagPtr, argv[4]);
246 Tcl_ResetResult(interp);
247 Tcl_AppendResult(interp, "requested illegal events; ",
248 "only key, button, motion, enter, leave, and virtual ",
249 "events may be used", (char *) NULL);
250 return TCL_ERROR;
251 }
252 } else if (argc == 5) {
253 Tcl_Obj *command;
254
255 command = Tk_GetBinding(interp, textPtr->bindingTable,
256 (ClientData) tagPtr, argv[4]);
257 if (command == NULL) {
258 CONST char *string = Tcl_GetStringResult(interp);
259
260 /*
261 * Ignore missing binding errors. This is a special hack
262 * that relies on the error message returned by FindSequence
263 * in tkBind.c.
264 */
265
266 if (string[0] != '\0') {
267 return TCL_ERROR;
268 } else {
269 Tcl_ResetResult(interp);
270 }
271 } else {
272 Tcl_SetObjResult(interp, command);
273 }
274 } else {
275 Tk_GetAllBindings(interp, textPtr->bindingTable,
276 (ClientData) tagPtr);
277 }
278 } else if ((c == 'c') && (strncmp(argv[2], "cget", length) == 0)
279 && (length >= 2)) {
280 if (argc != 5) {
281 Tcl_AppendResult(interp, "wrong # args: should be \"",
282 argv[0], " tag cget tagName option\"",
283 (char *) NULL);
284 return TCL_ERROR;
285 }
286 tagPtr = FindTag(interp, textPtr, argv[3]);
287 if (tagPtr == NULL) {
288 return TCL_ERROR;
289 }
290 return Tk_ConfigureValue(interp, textPtr->tkwin, tagConfigSpecs,
291 (char *) tagPtr, argv[4], 0);
292 } else if ((c == 'c') && (strncmp(argv[2], "configure", length) == 0)
293 && (length >= 2)) {
294 if (argc < 4) {
295 Tcl_AppendResult(interp, "wrong # args: should be \"",
296 argv[0], " tag configure tagName ?option? ?value? ",
297 "?option value ...?\"", (char *) NULL);
298 return TCL_ERROR;
299 }
300 tagPtr = TkTextCreateTag(textPtr, argv[3]);
301 if (argc == 4) {
302 return Tk_ConfigureInfo(interp, textPtr->tkwin, tagConfigSpecs,
303 (char *) tagPtr, (char *) NULL, 0);
304 } else if (argc == 5) {
305 return Tk_ConfigureInfo(interp, textPtr->tkwin, tagConfigSpecs,
306 (char *) tagPtr, argv[4], 0);
307 } else {
308 int result;
309
310 result = Tk_ConfigureWidget(interp, textPtr->tkwin, tagConfigSpecs,
311 argc-4, argv+4, (char *) tagPtr, 0);
312 /*
313 * Some of the configuration options, like -underline
314 * and -justify, require additional translation (this is
315 * needed because we need to distinguish a particular value
316 * of an option from "unspecified").
317 */
318
319 if (tagPtr->bdString != NULL) {
320 if (Tk_GetPixels(interp, textPtr->tkwin, tagPtr->bdString,
321 &tagPtr->borderWidth) != TCL_OK) {
322 return TCL_ERROR;
323 }
324 if (tagPtr->borderWidth < 0) {
325 tagPtr->borderWidth = 0;
326 }
327 }
328 if (tagPtr->reliefString != NULL) {
329 if (Tk_GetRelief(interp, tagPtr->reliefString,
330 &tagPtr->relief) != TCL_OK) {
331 return TCL_ERROR;
332 }
333 }
334 if (tagPtr->justifyString != NULL) {
335 if (Tk_GetJustify(interp, tagPtr->justifyString,
336 &tagPtr->justify) != TCL_OK) {
337 return TCL_ERROR;
338 }
339 }
340 if (tagPtr->lMargin1String != NULL) {
341 if (Tk_GetPixels(interp, textPtr->tkwin,
342 tagPtr->lMargin1String, &tagPtr->lMargin1) != TCL_OK) {
343 return TCL_ERROR;
344 }
345 }
346 if (tagPtr->lMargin2String != NULL) {
347 if (Tk_GetPixels(interp, textPtr->tkwin,
348 tagPtr->lMargin2String, &tagPtr->lMargin2) != TCL_OK) {
349 return TCL_ERROR;
350 }
351 }
352 if (tagPtr->offsetString != NULL) {
353 if (Tk_GetPixels(interp, textPtr->tkwin, tagPtr->offsetString,
354 &tagPtr->offset) != TCL_OK) {
355 return TCL_ERROR;
356 }
357 }
358 if (tagPtr->overstrikeString != NULL) {
359 if (Tcl_GetBooleanFromObj(interp, tagPtr->overstrikeString,
360 &tagPtr->overstrike) != TCL_OK) {
361 return TCL_ERROR;
362 }
363 }
364 if (tagPtr->rMarginString != NULL) {
365 if (Tk_GetPixels(interp, textPtr->tkwin,
366 tagPtr->rMarginString, &tagPtr->rMargin) != TCL_OK) {
367 return TCL_ERROR;
368 }
369 }
370 if (tagPtr->spacing1String != NULL) {
371 if (Tk_GetPixels(interp, textPtr->tkwin,
372 tagPtr->spacing1String, &tagPtr->spacing1) != TCL_OK) {
373 return TCL_ERROR;
374 }
375 if (tagPtr->spacing1 < 0) {
376 tagPtr->spacing1 = 0;
377 }
378 }
379 if (tagPtr->spacing2String != NULL) {
380 if (Tk_GetPixels(interp, textPtr->tkwin,
381 tagPtr->spacing2String, &tagPtr->spacing2) != TCL_OK) {
382 return TCL_ERROR;
383 }
384 if (tagPtr->spacing2 < 0) {
385 tagPtr->spacing2 = 0;
386 }
387 }
388 if (tagPtr->spacing3String != NULL) {
389 if (Tk_GetPixels(interp, textPtr->tkwin,
390 tagPtr->spacing3String, &tagPtr->spacing3) != TCL_OK) {
391 return TCL_ERROR;
392 }
393 if (tagPtr->spacing3 < 0) {
394 tagPtr->spacing3 = 0;
395 }
396 }
397 if (tagPtr->tabArrayPtr != NULL) {
398 ckfree((char *) tagPtr->tabArrayPtr);
399 tagPtr->tabArrayPtr = NULL;
400 }
401 if (tagPtr->tabString != NULL) {
402 tagPtr->tabArrayPtr = TkTextGetTabs(interp, textPtr->tkwin,
403 tagPtr->tabString);
404 if (tagPtr->tabArrayPtr == NULL) {
405 return TCL_ERROR;
406 }
407 }
408 if (tagPtr->underlineString != NULL) {
409 if (Tcl_GetBooleanFromObj(interp, tagPtr->underlineString,
410 &tagPtr->underline) != TCL_OK) {
411 return TCL_ERROR;
412 }
413 }
414 if (tagPtr->elideString != NULL) {
415 if (Tcl_GetBooleanFromObj(interp, tagPtr->elideString,
416 &tagPtr->elide) != TCL_OK) {
417 return TCL_ERROR;
418 }
419 }
420
421 /*
422 * If the "sel" tag was changed, be sure to mirror information
423 * from the tag back into the text widget record. NOTE: we
424 * don't have to free up information in the widget record
425 * before overwriting it, because it was mirrored in the tag
426 * and hence freed when the tag field was overwritten.
427 */
428
429 if (tagPtr == textPtr->selTagPtr) {
430 textPtr->selBorder = tagPtr->border;
431 textPtr->selBdString = tagPtr->bdString;
432 textPtr->selFgColorPtr = tagPtr->fgColor;
433 }
434 tagPtr->affectsDisplay = 0;
435 if ((tagPtr->border != NULL)
436 || (tagPtr->bdString != NULL)
437 || (tagPtr->reliefString != NULL)
438 || (tagPtr->bgStipple != None)
439 || (tagPtr->fgColor != NULL) || (tagPtr->tkfont != None)
440 || (tagPtr->fgStipple != None)
441 || (tagPtr->justifyString != NULL)
442 || (tagPtr->lMargin1String != NULL)
443 || (tagPtr->lMargin2String != NULL)
444 || (tagPtr->offsetString != NULL)
445 || (tagPtr->overstrikeString != NULL)
446 || (tagPtr->rMarginString != NULL)
447 || (tagPtr->spacing1String != NULL)
448 || (tagPtr->spacing2String != NULL)
449 || (tagPtr->spacing3String != NULL)
450 || (tagPtr->tabString != NULL)
451 || (tagPtr->underlineString != NULL)
452 || (tagPtr->elideString != NULL)
453 || (tagPtr->wrapMode != TEXT_WRAPMODE_NULL)) {
454 tagPtr->affectsDisplay = 1;
455 }
456 TkTextRedrawTag(textPtr, (TkTextIndex *) NULL,
457 (TkTextIndex *) NULL, tagPtr, 1);
458 return result;
459 }
460 } else if ((c == 'd') && (strncmp(argv[2], "delete", length) == 0)) {
461 Tcl_HashEntry *hPtr;
462
463 if (argc < 4) {
464 Tcl_AppendResult(interp, "wrong # args: should be \"",
465 argv[0], " tag delete tagName tagName ...\"",
466 (char *) NULL);
467 return TCL_ERROR;
468 }
469 for (i = 3; i < argc; i++) {
470 hPtr = Tcl_FindHashEntry(&textPtr->tagTable, argv[i]);
471 if (hPtr == NULL) {
472 continue;
473 }
474 tagPtr = (TkTextTag *) Tcl_GetHashValue(hPtr);
475 if (tagPtr == textPtr->selTagPtr) {
476 continue;
477 }
478 if (tagPtr->affectsDisplay) {
479 TkTextRedrawTag(textPtr, (TkTextIndex *) NULL,
480 (TkTextIndex *) NULL, tagPtr, 1);
481 }
482 TkTextMakeByteIndex(textPtr->tree, 0, 0, &first);
483 TkTextMakeByteIndex(textPtr->tree, TkBTreeNumLines(textPtr->tree),
484 0, &last),
485 TkBTreeTag(&first, &last, tagPtr, 0);
486
487 if (tagPtr == textPtr->selTagPtr) {
488 XEvent event;
489 /*
490 * Send an event that the selection changed.
491 * This is equivalent to
492 * "event generate $textWidget <<Selection>>"
493 */
494
495 memset((VOID *) &event, 0, sizeof(event));
496 event.xany.type = VirtualEvent;
497 event.xany.serial = NextRequest(Tk_Display(textPtr->tkwin));
498 event.xany.send_event = False;
499 event.xany.window = Tk_WindowId(textPtr->tkwin);
500 event.xany.display = Tk_Display(textPtr->tkwin);
501 ((XVirtualEvent *) &event)->name = Tk_GetUid("Selection");
502 Tk_HandleEvent(&event);
503 }
504
505 Tcl_DeleteHashEntry(hPtr);
506 if (textPtr->bindingTable != NULL) {
507 Tk_DeleteAllBindings(textPtr->bindingTable,
508 (ClientData) tagPtr);
509 }
510
511 /*
512 * Update the tag priorities to reflect the deletion of this tag.
513 */
514
515 ChangeTagPriority(textPtr, tagPtr, textPtr->numTags-1);
516 textPtr->numTags -= 1;
517 TkTextFreeTag(textPtr, tagPtr);
518 }
519 } else if ((c == 'l') && (strncmp(argv[2], "lower", length) == 0)) {
520 TkTextTag *tagPtr2;
521 int prio;
522
523 if ((argc != 4) && (argc != 5)) {
524 Tcl_AppendResult(interp, "wrong # args: should be \"",
525 argv[0], " tag lower tagName ?belowThis?\"",
526 (char *) NULL);
527 return TCL_ERROR;
528 }
529 tagPtr = FindTag(interp, textPtr, argv[3]);
530 if (tagPtr == NULL) {
531 return TCL_ERROR;
532 }
533 if (argc == 5) {
534 tagPtr2 = FindTag(interp, textPtr, argv[4]);
535 if (tagPtr2 == NULL) {
536 return TCL_ERROR;
537 }
538 if (tagPtr->priority < tagPtr2->priority) {
539 prio = tagPtr2->priority - 1;
540 } else {
541 prio = tagPtr2->priority;
542 }
543 } else {
544 prio = 0;
545 }
546 ChangeTagPriority(textPtr, tagPtr, prio);
547 TkTextRedrawTag(textPtr, (TkTextIndex *) NULL, (TkTextIndex *) NULL,
548 tagPtr, 1);
549 } else if ((c == 'n') && (strncmp(argv[2], "names", length) == 0)
550 && (length >= 2)) {
551 TkTextTag **arrayPtr;
552 int arraySize;
553
554 if ((argc != 3) && (argc != 4)) {
555 Tcl_AppendResult(interp, "wrong # args: should be \"",
556 argv[0], " tag names ?index?\"",
557 (char *) NULL);
558 return TCL_ERROR;
559 }
560 if (argc == 3) {
561 Tcl_HashSearch search;
562 Tcl_HashEntry *hPtr;
563
564 arrayPtr = (TkTextTag **) ckalloc((unsigned)
565 (textPtr->numTags * sizeof(TkTextTag *)));
566 for (i = 0, hPtr = Tcl_FirstHashEntry(&textPtr->tagTable, &search);
567 hPtr != NULL; i++, hPtr = Tcl_NextHashEntry(&search)) {
568 arrayPtr[i] = (TkTextTag *) Tcl_GetHashValue(hPtr);
569 }
570 arraySize = textPtr->numTags;
571 } else {
572 if (TkTextGetIndex(interp, textPtr, argv[3], &index1)
573 != TCL_OK) {
574 return TCL_ERROR;
575 }
576 arrayPtr = TkBTreeGetTags(&index1, &arraySize);
577 if (arrayPtr == NULL) {
578 return TCL_OK;
579 }
580 }
581 SortTags(arraySize, arrayPtr);
582 for (i = 0; i < arraySize; i++) {
583 tagPtr = arrayPtr[i];
584 Tcl_AppendElement(interp, tagPtr->name);
585 }
586 ckfree((char *) arrayPtr);
587 } else if ((c == 'n') && (strncmp(argv[2], "nextrange", length) == 0)
588 && (length >= 2)) {
589 TkTextSearch tSearch;
590 char position[TK_POS_CHARS];
591
592 if ((argc != 5) && (argc != 6)) {
593 Tcl_AppendResult(interp, "wrong # args: should be \"",
594 argv[0], " tag nextrange tagName index1 ?index2?\"",
595 (char *) NULL);
596 return TCL_ERROR;
597 }
598 tagPtr = FindTag((Tcl_Interp *) NULL, textPtr, argv[3]);
599 if (tagPtr == NULL) {
600 return TCL_OK;
601 }
602 if (TkTextGetIndex(interp, textPtr, argv[4], &index1) != TCL_OK) {
603 return TCL_ERROR;
604 }
605 TkTextMakeByteIndex(textPtr->tree, TkBTreeNumLines(textPtr->tree),
606 0, &last);
607 if (argc == 5) {
608 index2 = last;
609 } else if (TkTextGetIndex(interp, textPtr, argv[5], &index2)
610 != TCL_OK) {
611 return TCL_ERROR;
612 }
613
614 /*
615 * The search below is a bit tricky. Rather than use the B-tree
616 * facilities to stop the search at index2, let it search up
617 * until the end of the file but check for a position past index2
618 * ourselves. The reason for doing it this way is that we only
619 * care whether the *start* of the range is before index2; once
620 * we find the start, we don't want TkBTreeNextTag to abort the
621 * search because the end of the range is after index2.
622 */
623
624 TkBTreeStartSearch(&index1, &last, tagPtr, &tSearch);
625 if (TkBTreeCharTagged(&index1, tagPtr)) {
626 TkTextSegment *segPtr;
627 int offset;
628
629 /*
630 * The first character is tagged. See if there is an
631 * on-toggle just before the character. If not, then
632 * skip to the end of this tagged range.
633 */
634
635 for (segPtr = index1.linePtr->segPtr, offset = index1.byteIndex;
636 offset >= 0;
637 offset -= segPtr->size, segPtr = segPtr->nextPtr) {
638 if ((offset == 0) && (segPtr->typePtr == &tkTextToggleOnType)
639 && (segPtr->body.toggle.tagPtr == tagPtr)) {
640 goto gotStart;
641 }
642 }
643 if (!TkBTreeNextTag(&tSearch)) {
644 return TCL_OK;
645 }
646 }
647
648 /*
649 * Find the start of the tagged range.
650 */
651
652 if (!TkBTreeNextTag(&tSearch)) {
653 return TCL_OK;
654 }
655 gotStart:
656 if (TkTextIndexCmp(&tSearch.curIndex, &index2) >= 0) {
657 return TCL_OK;
658 }
659 TkTextPrintIndex(&tSearch.curIndex, position);
660 Tcl_AppendElement(interp, position);
661 TkBTreeNextTag(&tSearch);
662 TkTextPrintIndex(&tSearch.curIndex, position);
663 Tcl_AppendElement(interp, position);
664 } else if ((c == 'p') && (strncmp(argv[2], "prevrange", length) == 0)
665 && (length >= 2)) {
666 TkTextSearch tSearch;
667 char position1[TK_POS_CHARS];
668 char position2[TK_POS_CHARS];
669
670 if ((argc != 5) && (argc != 6)) {
671 Tcl_AppendResult(interp, "wrong # args: should be \"",
672 argv[0], " tag prevrange tagName index1 ?index2?\"",
673 (char *) NULL);
674 return TCL_ERROR;
675 }
676 tagPtr = FindTag((Tcl_Interp *) NULL, textPtr, argv[3]);
677 if (tagPtr == NULL) {
678 return TCL_OK;
679 }
680 if (TkTextGetIndex(interp, textPtr, argv[4], &index1) != TCL_OK) {
681 return TCL_ERROR;
682 }
683 if (argc == 5) {
684 TkTextMakeByteIndex(textPtr->tree, 0, 0, &index2);
685 } else if (TkTextGetIndex(interp, textPtr, argv[5], &index2)
686 != TCL_OK) {
687 return TCL_ERROR;
688 }
689
690 /*
691 * The search below is a bit weird. The previous toggle can be
692 * either an on or off toggle. If it is an on toggle, then we
693 * need to turn around and search forward for the end toggle.
694 * Otherwise we keep searching backwards.
695 */
696
697 TkBTreeStartSearchBack(&index1, &index2, tagPtr, &tSearch);
698
699 if (!TkBTreePrevTag(&tSearch)) {
700 return TCL_OK;
701 }
702 if (tSearch.segPtr->typePtr == &tkTextToggleOnType) {
703 TkTextPrintIndex(&tSearch.curIndex, position1);
704 TkTextMakeByteIndex(textPtr->tree, TkBTreeNumLines(textPtr->tree),
705 0, &last);
706 TkBTreeStartSearch(&tSearch.curIndex, &last, tagPtr, &tSearch);
707 TkBTreeNextTag(&tSearch);
708 TkTextPrintIndex(&tSearch.curIndex, position2);
709 } else {
710 TkTextPrintIndex(&tSearch.curIndex, position2);
711 TkBTreePrevTag(&tSearch);
712 if (TkTextIndexCmp(&tSearch.curIndex, &index2) < 0) {
713 return TCL_OK;
714 }
715 TkTextPrintIndex(&tSearch.curIndex, position1);
716 }
717 Tcl_AppendElement(interp, position1);
718 Tcl_AppendElement(interp, position2);
719 } else if ((c == 'r') && (strncmp(argv[2], "raise", length) == 0)
720 && (length >= 3)) {
721 TkTextTag *tagPtr2;
722 int prio;
723
724 if ((argc != 4) && (argc != 5)) {
725 Tcl_AppendResult(interp, "wrong # args: should be \"",
726 argv[0], " tag raise tagName ?aboveThis?\"",
727 (char *) NULL);
728 return TCL_ERROR;
729 }
730 tagPtr = FindTag(interp, textPtr, argv[3]);
731 if (tagPtr == NULL) {
732 return TCL_ERROR;
733 }
734 if (argc == 5) {
735 tagPtr2 = FindTag(interp, textPtr, argv[4]);
736 if (tagPtr2 == NULL) {
737 return TCL_ERROR;
738 }
739 if (tagPtr->priority <= tagPtr2->priority) {
740 prio = tagPtr2->priority;
741 } else {
742 prio = tagPtr2->priority + 1;
743 }
744 } else {
745 prio = textPtr->numTags-1;
746 }
747 ChangeTagPriority(textPtr, tagPtr, prio);
748 TkTextRedrawTag(textPtr, (TkTextIndex *) NULL, (TkTextIndex *) NULL,
749 tagPtr, 1);
750 } else if ((c == 'r') && (strncmp(argv[2], "ranges", length) == 0)
751 && (length >= 3)) {
752 TkTextSearch tSearch;
753 char position[TK_POS_CHARS];
754
755 if (argc != 4) {
756 Tcl_AppendResult(interp, "wrong # args: should be \"",
757 argv[0], " tag ranges tagName\"", (char *) NULL);
758 return TCL_ERROR;
759 }
760 tagPtr = FindTag((Tcl_Interp *) NULL, textPtr, argv[3]);
761 if (tagPtr == NULL) {
762 return TCL_OK;
763 }
764 TkTextMakeByteIndex(textPtr->tree, 0, 0, &first);
765 TkTextMakeByteIndex(textPtr->tree, TkBTreeNumLines(textPtr->tree),
766 0, &last);
767 TkBTreeStartSearch(&first, &last, tagPtr, &tSearch);
768 if (TkBTreeCharTagged(&first, tagPtr)) {
769 TkTextPrintIndex(&first, position);
770 Tcl_AppendElement(interp, position);
771 }
772 while (TkBTreeNextTag(&tSearch)) {
773 TkTextPrintIndex(&tSearch.curIndex, position);
774 Tcl_AppendElement(interp, position);
775 }
776 } else if ((c == 'r') && (strncmp(argv[2], "remove", length) == 0)
777 && (length >= 2)) {
778 fullOption = "remove";
779 addTag = 0;
780 goto addAndRemove;
781 } else {
782 Tcl_AppendResult(interp, "bad tag option \"", argv[2],
783 "\": must be add, bind, cget, configure, delete, lower, ",
784 "names, nextrange, raise, ranges, or remove",
785 (char *) NULL);
786 return TCL_ERROR;
787 }
788 return TCL_OK;
789 }
790
791 /*
792 *----------------------------------------------------------------------
793 *
794 * TkTextCreateTag --
795 *
796 * Find the record describing a tag within a given text widget,
797 * creating a new record if one doesn't already exist.
798 *
799 * Results:
800 * The return value is a pointer to the TkTextTag record for tagName.
801 *
802 * Side effects:
803 * A new tag record is created if there isn't one already defined
804 * for tagName.
805 *
806 *----------------------------------------------------------------------
807 */
808
809 TkTextTag *
810 TkTextCreateTag(textPtr, tagName)
811 TkText *textPtr; /* Widget in which tag is being used. */
812 CONST char *tagName; /* Name of desired tag. */
813 {
814 register TkTextTag *tagPtr;
815 Tcl_HashEntry *hPtr;
816 int new;
817
818 hPtr = Tcl_CreateHashEntry(&textPtr->tagTable, tagName, &new);
819 if (!new) {
820 return (TkTextTag *) Tcl_GetHashValue(hPtr);
821 }
822
823 /*
824 * No existing entry. Create a new one, initialize it, and add a
825 * pointer to it to the hash table entry.
826 */
827
828 tagPtr = (TkTextTag *) ckalloc(sizeof(TkTextTag));
829 memset(tagPtr,0,sizeof(TkTextTag));
830 tagPtr->name = Tcl_GetHashKey(&textPtr->tagTable, hPtr);
831 tagPtr->toggleCount = 0;
832 tagPtr->tagRootPtr = NULL;
833 tagPtr->priority = textPtr->numTags;
834 tagPtr->border = NULL;
835 tagPtr->bdString = NULL;
836 tagPtr->borderWidth = 0;
837 tagPtr->reliefString = NULL;
838 tagPtr->relief = TK_RELIEF_FLAT;
839 tagPtr->bgStipple = None;
840 tagPtr->fgColor = NULL;
841 tagPtr->tkfont = NULL;
842 tagPtr->fgStipple = None;
843 tagPtr->justifyString = NULL;
844 tagPtr->justify = TK_JUSTIFY_LEFT;
845 tagPtr->lMargin1String = NULL;
846 tagPtr->lMargin1 = 0;
847 tagPtr->lMargin2String = NULL;
848 tagPtr->lMargin2 = 0;
849 tagPtr->offsetString = NULL;
850 tagPtr->offset = 0;
851 tagPtr->overstrikeString = NULL;
852 tagPtr->overstrike = 0;
853 tagPtr->rMarginString = NULL;
854 tagPtr->rMargin = 0;
855 tagPtr->spacing1String = NULL;
856 tagPtr->spacing1 = 0;
857 tagPtr->spacing2String = NULL;
858 tagPtr->spacing2 = 0;
859 tagPtr->spacing3String = NULL;
860 tagPtr->spacing3 = 0;
861 tagPtr->tabString = NULL;
862 tagPtr->tabArrayPtr = NULL;
863 tagPtr->underlineString = NULL;
864 tagPtr->underline = 0;
865 tagPtr->elideString = NULL;
866 tagPtr->elide = 0;
867 tagPtr->wrapMode = TEXT_WRAPMODE_NULL;
868 tagPtr->userData = NULL;
869 tagPtr->affectsDisplay = 0;
870 textPtr->numTags++;
871 Tcl_SetHashValue(hPtr, tagPtr);
872 return tagPtr;
873 }
874
875 /*
876 *----------------------------------------------------------------------
877 *
878 * FindTag --
879 *
880 * See if tag is defined for a given widget.
881 *
882 * Results:
883 * If tagName is defined in textPtr, a pointer to its TkTextTag
884 * structure is returned. Otherwise NULL is returned and an
885 * error message is recorded in the interp's result unless interp
886 * is NULL.
887 *
888 * Side effects:
889 * None.
890 *
891 *----------------------------------------------------------------------
892 */
893
894 static TkTextTag *
895 FindTag(interp, textPtr, tagName)
896 Tcl_Interp *interp; /* Interpreter to use for error message;
897 * if NULL, then don't record an error
898 * message. */
899 TkText *textPtr; /* Widget in which tag is being used. */
900 CONST char *tagName; /* Name of desired tag. */
901 {
902 Tcl_HashEntry *hPtr;
903
904 hPtr = Tcl_FindHashEntry(&textPtr->tagTable, tagName);
905 if (hPtr != NULL) {
906 return (TkTextTag *) Tcl_GetHashValue(hPtr);
907 }
908 if (interp != NULL) {
909 Tcl_AppendResult(interp, "tag \"", tagName,
910 "\" isn't defined in text widget", (char *) NULL);
911 }
912 return NULL;
913 }
914
915 /*
916 *----------------------------------------------------------------------
917 *
918 * TkTextFreeTag --
919 *
920 * This procedure is called when a tag is deleted to free up the
921 * memory and other resources associated with the tag.
922 *
923 * Results:
924 * None.
925 *
926 * Side effects:
927 * Memory and other resources are freed.
928 *
929 *----------------------------------------------------------------------
930 */
931
932 void
933 TkTextFreeTag(textPtr, tagPtr)
934 TkText *textPtr; /* Info about overall widget. */
935 register TkTextTag *tagPtr; /* Tag being deleted. */
936 {
937 Tk_FreeOptions(tagConfigSpecs,(char *) tagPtr,textPtr->display, 0);
938 /* Now added above these should be redundant */
939 if (tagPtr->border != None) {
940 Tk_Free3DBorder(tagPtr->border);
941 }
942 if (tagPtr->bdString != NULL) {
943 ckfree(tagPtr->bdString);
944 }
945 if (tagPtr->reliefString != NULL) {
946 ckfree(tagPtr->reliefString);
947 }
948 if (tagPtr->bgStipple != None) {
949 Tk_FreeBitmap(textPtr->display, tagPtr->bgStipple);
950 }
951 if (tagPtr->fgColor != None) {
952 Tk_FreeColor(tagPtr->fgColor);
953 }
954 Tk_FreeFont(tagPtr->tkfont);
955 if (tagPtr->fgStipple != None) {
956 Tk_FreeBitmap(textPtr->display, tagPtr->fgStipple);
957 }
958 if (tagPtr->underlineString != NULL) {
959 LangFreeArg(tagPtr->underlineString,TCL_DYNAMIC);
960 }
961 if (tagPtr->elideString != NULL) {
962 LangFreeArg(tagPtr->elideString,TCL_DYNAMIC);
963 }
964 if (tagPtr->justifyString != NULL) {
965 ckfree(tagPtr->justifyString);
966 }
967 if (tagPtr->lMargin1String != NULL) {
968 ckfree(tagPtr->lMargin1String);
969 }
970 if (tagPtr->lMargin2String != NULL) {
971 ckfree(tagPtr->lMargin2String);
972 }
973 if (tagPtr->offsetString != NULL) {
974 ckfree(tagPtr->offsetString);
975 }
976 if (tagPtr->overstrikeString != NULL) {
977 LangFreeArg(tagPtr->overstrikeString, TCL_DYNAMIC);
978 }
979 if (tagPtr->rMarginString != NULL) {
980 ckfree(tagPtr->rMarginString);
981 }
982 if (tagPtr->spacing1String != NULL) {
983 ckfree(tagPtr->spacing1String);
984 }
985 if (tagPtr->spacing2String != NULL) {
986 ckfree(tagPtr->spacing2String);
987 }
988 if (tagPtr->spacing3String != NULL) {
989 ckfree(tagPtr->spacing3String);
990 }
991 if (tagPtr->tabString != NULL) {
992 LangFreeArg(tagPtr->tabString,TCL_DYNAMIC);
993 }
994 if (tagPtr->tabArrayPtr != NULL) {
995 ckfree((char *) tagPtr->tabArrayPtr);
996 }
997 if (tagPtr->userData != NULL) {
998 LangFreeArg(tagPtr->userData,TCL_DYNAMIC);
999 }
1000 ckfree((char *) tagPtr);
1001 }
1002
1003 /*
1004 *----------------------------------------------------------------------
1005 *
1006 * SortTags --
1007 *
1008 * This procedure sorts an array of tag pointers in increasing
1009 * order of priority, optimizing for the common case where the
1010 * array is small.
1011 *
1012 * Results:
1013 * None.
1014 *
1015 * Side effects:
1016 * None.
1017 *
1018 *----------------------------------------------------------------------
1019 */
1020
1021 static void
1022 SortTags(numTags, tagArrayPtr)
1023 int numTags; /* Number of tag pointers at *tagArrayPtr. */
1024 TkTextTag **tagArrayPtr; /* Pointer to array of pointers. */
1025 {
1026 int i, j, prio;
1027 register TkTextTag **tagPtrPtr;
1028 TkTextTag **maxPtrPtr, *tmp;
1029
1030 if (numTags < 2) {
1031 return;
1032 }
1033 if (numTags < 20) {
1034 for (i = numTags-1; i > 0; i--, tagArrayPtr++) {
1035 maxPtrPtr = tagPtrPtr = tagArrayPtr;
1036 prio = tagPtrPtr[0]->priority;
1037 for (j = i, tagPtrPtr++; j > 0; j--, tagPtrPtr++) {
1038 if (tagPtrPtr[0]->priority < prio) {
1039 prio = tagPtrPtr[0]->priority;
1040 maxPtrPtr = tagPtrPtr;
1041 }
1042 }
1043 tmp = *maxPtrPtr;
1044 *maxPtrPtr = *tagArrayPtr;
1045 *tagArrayPtr = tmp;
1046 }
1047 } else {
1048 qsort((VOID *) tagArrayPtr, (unsigned) numTags, sizeof (TkTextTag *),
1049 TagSortProc);
1050 }
1051 }
1052
1053 /*
1054 *----------------------------------------------------------------------
1055 *
1056 * TagSortProc --
1057 *
1058 * This procedure is called by qsort when sorting an array of
1059 * tags in priority order.
1060 *
1061 * Results:
1062 * The return value is -1 if the first argument should be before
1063 * the second element (i.e. it has lower priority), 0 if it's
1064 * equivalent (this should never happen!), and 1 if it should be
1065 * after the second element.
1066 *
1067 * Side effects:
1068 * None.
1069 *
1070 *----------------------------------------------------------------------
1071 */
1072
1073 static int
1074 TagSortProc(first, second)
1075 CONST VOID *first, *second; /* Elements to be compared. */
1076 {
1077 TkTextTag *tagPtr1, *tagPtr2;
1078
1079 tagPtr1 = * (TkTextTag **) first;
1080 tagPtr2 = * (TkTextTag **) second;
1081 return tagPtr1->priority - tagPtr2->priority;
1082 }
1083
1084 /*
1085 *----------------------------------------------------------------------
1086 *
1087 * ChangeTagPriority --
1088 *
1089 * This procedure changes the priority of a tag by modifying
1090 * its priority and the priorities of other tags that are affected
1091 * by the change.
1092 *
1093 * Results:
1094 * None.
1095 *
1096 * Side effects:
1097 * Priorities may be changed for some or all of the tags in
1098 * textPtr. The tags will be arranged so that there is exactly
1099 * one tag at each priority level between 0 and textPtr->numTags-1,
1100 * with tagPtr at priority "prio".
1101 *
1102 *----------------------------------------------------------------------
1103 */
1104
1105 static void
1106 ChangeTagPriority(textPtr, tagPtr, prio)
1107 TkText *textPtr; /* Information about text widget. */
1108 TkTextTag *tagPtr; /* Tag whose priority is to be
1109 * changed. */
1110 int prio; /* New priority for tag. */
1111 {
1112 int low, high, delta;
1113 register TkTextTag *tagPtr2;
1114 Tcl_HashEntry *hPtr;
1115 Tcl_HashSearch search;
1116
1117 if (prio < 0) {
1118 prio = 0;
1119 }
1120 if (prio >= textPtr->numTags) {
1121 prio = textPtr->numTags-1;
1122 }
1123 if (prio == tagPtr->priority) {
1124 return;
1125 } else if (prio < tagPtr->priority) {
1126 low = prio;
1127 high = tagPtr->priority-1;
1128 delta = 1;
1129 } else {
1130 low = tagPtr->priority+1;
1131 high = prio;
1132 delta = -1;
1133 }
1134 for (hPtr = Tcl_FirstHashEntry(&textPtr->tagTable, &search);
1135 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
1136 tagPtr2 = (TkTextTag *) Tcl_GetHashValue(hPtr);
1137 if ((tagPtr2->priority >= low) && (tagPtr2->priority <= high)) {
1138 tagPtr2->priority += delta;
1139 }
1140 }
1141 tagPtr->priority = prio;
1142 }
1143
1144 /*
1145 *--------------------------------------------------------------
1146 *
1147 * TkTextBindProc --
1148 *
1149 * This procedure is invoked by the Tk dispatcher to handle
1150 * events associated with bindings on items.
1151 *
1152 * Results:
1153 * None.
1154 *
1155 * Side effects:
1156 * Depends on the command invoked as part of the binding
1157 * (if there was any).
1158 *
1159 *--------------------------------------------------------------
1160 */
1161
1162 void
1163 TkTextBindProc(clientData, eventPtr)
1164 ClientData clientData; /* Pointer to canvas structure. */
1165 XEvent *eventPtr; /* Pointer to X event that just
1166 * happened. */
1167 {
1168 TkText *textPtr = (TkText *) clientData;
1169 int repick = 0;
1170
1171 # define AnyButtonMask (Button1Mask|Button2Mask|Button3Mask\
1172 |Button4Mask|Button5Mask)
1173
1174 Tcl_Preserve((ClientData) textPtr);
1175
1176 /*
1177 * This code simulates grabs for mouse buttons by keeping track
1178 * of whether a button is pressed and refusing to pick a new current
1179 * character while a button is pressed.
1180 */
1181
1182 if (eventPtr->type == ButtonPress) {
1183 textPtr->flags |= BUTTON_DOWN;
1184 } else if (eventPtr->type == ButtonRelease) {
1185 int mask;
1186
1187 switch (eventPtr->xbutton.button) {
1188 case Button1:
1189 mask = Button1Mask;
1190 break;
1191 case Button2:
1192 mask = Button2Mask;
1193 break;
1194 case Button3:
1195 mask = Button3Mask;
1196 break;
1197 case Button4:
1198 mask = Button4Mask;
1199 break;
1200 case Button5:
1201 mask = Button5Mask;
1202 break;
1203 default:
1204 mask = 0;
1205 break;
1206 }
1207 if ((eventPtr->xbutton.state & AnyButtonMask) == (unsigned) mask) {
1208 textPtr->flags &= ~BUTTON_DOWN;
1209 repick = 1;
1210 }
1211 } else if ((eventPtr->type == EnterNotify)
1212 || (eventPtr->type == LeaveNotify)) {
1213 if (eventPtr->xcrossing.state & AnyButtonMask) {
1214 textPtr->flags |= BUTTON_DOWN;
1215 } else {
1216 textPtr->flags &= ~BUTTON_DOWN;
1217 }
1218 TkTextPickCurrent(textPtr, eventPtr);
1219 goto done;
1220 } else if (eventPtr->type == MotionNotify) {
1221 if (eventPtr->xmotion.state & AnyButtonMask) {
1222 textPtr->flags |= BUTTON_DOWN;
1223 } else {
1224 textPtr->flags &= ~BUTTON_DOWN;
1225 }
1226 TkTextPickCurrent(textPtr, eventPtr);
1227 }
1228 if ((textPtr->numCurTags > 0) && (textPtr->bindingTable != NULL)
1229 && (textPtr->tkwin != NULL)) {
1230 Tk_BindEvent(textPtr->bindingTable, eventPtr, textPtr->tkwin,
1231 textPtr->numCurTags, (ClientData *) textPtr->curTagArrayPtr);
1232 }
1233 if (repick) {
1234 unsigned int oldState;
1235
1236 oldState = eventPtr->xbutton.state;
1237 eventPtr->xbutton.state &= ~(Button1Mask|Button2Mask
1238 |Button3Mask|Button4Mask|Button5Mask);
1239 TkTextPickCurrent(textPtr, eventPtr);
1240 eventPtr->xbutton.state = oldState;
1241 }
1242
1243 done:
1244 Tcl_Release((ClientData) textPtr);
1245 }
1246
1247 /*
1248 *--------------------------------------------------------------
1249 *
1250 * TkTextPickCurrent --
1251 *
1252 * Find the character containing the coordinates in an event
1253 * and place the "current" mark on that character. If the
1254 * "current" mark has moved then generate a fake leave event
1255 * on the old current character and a fake enter event on the new
1256 * current character.
1257 *
1258 * Results:
1259 * None.
1260 *
1261 * Side effects:
1262 * The current mark for textPtr may change. If it does,
1263 * then the commands associated with character entry and leave
1264 * could do just about anything. For example, the text widget
1265 * might be deleted. It is up to the caller to protect itself
1266 * with calls to Tcl_Preserve and Tcl_Release.
1267 *
1268 *--------------------------------------------------------------
1269 */
1270
1271 void
1272 TkTextPickCurrent(textPtr, eventPtr)
1273 register TkText *textPtr; /* Text widget in which to select
1274 * current character. */
1275 XEvent *eventPtr; /* Event describing location of
1276 * mouse cursor. Must be EnterWindow,
1277 * LeaveWindow, ButtonRelease, or
1278 * MotionNotify. */
1279 {
1280 TkTextIndex index;
1281 TkTextTag **oldArrayPtr, **newArrayPtr;
1282 TkTextTag **copyArrayPtr = NULL; /* Initialization needed to prevent
1283 * compiler warning. */
1284
1285 int numOldTags, numNewTags, i, j, size;
1286 XEvent event;
1287
1288 /*
1289 * If a button is down, then don't do anything at all; we'll be
1290 * called again when all buttons are up, and we can repick then.
1291 * This implements a form of mouse grabbing.
1292 */
1293
1294 if (textPtr->flags & BUTTON_DOWN) {
1295 if (((eventPtr->type == EnterNotify) || (eventPtr->type == LeaveNotify))
1296 && ((eventPtr->xcrossing.mode == NotifyGrab)
1297 || (eventPtr->xcrossing.mode == NotifyUngrab))) {
1298 /*
1299 * Special case: the window is being entered or left because
1300 * of a grab or ungrab. In this case, repick after all.
1301 * Furthermore, clear BUTTON_DOWN to release the simulated
1302 * grab.
1303 */
1304
1305 textPtr->flags &= ~BUTTON_DOWN;
1306 } else {
1307 return;
1308 }
1309 }
1310
1311 /*
1312 * Save information about this event in the widget in case we have
1313 * to synthesize more enter and leave events later (e.g. because a
1314 * character was deleted, causing a new character to be underneath
1315 * the mouse cursor). Also translate MotionNotify events into
1316 * EnterNotify events, since that's what gets reported to event
1317 * handlers when the current character changes.
1318 */
1319
1320 if (eventPtr != &textPtr->pickEvent) {
1321 if ((eventPtr->type == MotionNotify)
1322 || (eventPtr->type == ButtonRelease)) {
1323 textPtr->pickEvent.xcrossing.type = EnterNotify;
1324 textPtr->pickEvent.xcrossing.serial = eventPtr->xmotion.serial;
1325 textPtr->pickEvent.xcrossing.send_event
1326 = eventPtr->xmotion.send_event;
1327 textPtr->pickEvent.xcrossing.display = eventPtr->xmotion.display;
1328 textPtr->pickEvent.xcrossing.window = eventPtr->xmotion.window;
1329 textPtr->pickEvent.xcrossing.root = eventPtr->xmotion.root;
1330 textPtr->pickEvent.xcrossing.subwindow = None;
1331 textPtr->pickEvent.xcrossing.time = eventPtr->xmotion.time;
1332 textPtr->pickEvent.xcrossing.x = eventPtr->xmotion.x;
1333 textPtr->pickEvent.xcrossing.y = eventPtr->xmotion.y;
1334 textPtr->pickEvent.xcrossing.x_root = eventPtr->xmotion.x_root;
1335 textPtr->pickEvent.xcrossing.y_root = eventPtr->xmotion.y_root;
1336 textPtr->pickEvent.xcrossing.mode = NotifyNormal;
1337 textPtr->pickEvent.xcrossing.detail = NotifyNonlinear;
1338 textPtr->pickEvent.xcrossing.same_screen
1339 = eventPtr->xmotion.same_screen;
1340 textPtr->pickEvent.xcrossing.focus = False;
1341 textPtr->pickEvent.xcrossing.state = eventPtr->xmotion.state;
1342 } else {
1343 textPtr->pickEvent = *eventPtr;
1344 }
1345 }
1346
1347 /*
1348 * Find the new current character, then find and sort all of the
1349 * tags associated with it.
1350 */
1351
1352 if (textPtr->pickEvent.type != LeaveNotify) {
1353 TkTextPixelIndex(textPtr, textPtr->pickEvent.xcrossing.x,
1354 textPtr->pickEvent.xcrossing.y, &index);
1355 newArrayPtr = TkBTreeGetTags(&index, &numNewTags);
1356 SortTags(numNewTags, newArrayPtr);
1357 } else {
1358 newArrayPtr = NULL;
1359 numNewTags = 0;
1360 }
1361
1362 /*
1363 * Resort the tags associated with the previous marked character
1364 * (the priorities might have changed), then make a copy of the
1365 * new tags, and compare the old tags to the copy, nullifying
1366 * any tags that are present in both groups (i.e. the tags that
1367 * haven't changed).
1368 */
1369
1370 SortTags(textPtr->numCurTags, textPtr->curTagArrayPtr);
1371 if (numNewTags > 0) {
1372 size = numNewTags * sizeof(TkTextTag *);
1373 copyArrayPtr = (TkTextTag **) ckalloc((unsigned) size);
1374 memcpy((VOID *) copyArrayPtr, (VOID *) newArrayPtr, (size_t) size);
1375 for (i = 0; i < textPtr->numCurTags; i++) {
1376 for (j = 0; j < numNewTags; j++) {
1377 if (textPtr->curTagArrayPtr[i] == copyArrayPtr[j]) {
1378 textPtr->curTagArrayPtr[i] = NULL;
1379 copyArrayPtr[j] = NULL;
1380 break;
1381 }
1382 }
1383 }
1384 }
1385
1386 /*
1387 * Invoke the binding system with a LeaveNotify event for all of
1388 * the tags that have gone away. We have to be careful here,
1389 * because it's possible that the binding could do something
1390 * (like calling tkwait) that eventually modifies
1391 * textPtr->curTagArrayPtr. To avoid problems in situations like
1392 * this, update curTagArrayPtr to its new value before invoking
1393 * any bindings, and don't use it any more here.
1394 */
1395
1396 numOldTags = textPtr->numCurTags;
1397 textPtr->numCurTags = numNewTags;
1398 oldArrayPtr = textPtr->curTagArrayPtr;
1399 textPtr->curTagArrayPtr = newArrayPtr;
1400 if (numOldTags != 0) {
1401 if ((textPtr->bindingTable != NULL) && (textPtr->tkwin != NULL)) {
1402 event = textPtr->pickEvent;
1403 event.type = LeaveNotify;
1404
1405 /*
1406 * Always use a detail of NotifyAncestor. Besides being
1407 * consistent, this avoids problems where the binding code
1408 * will discard NotifyInferior events.
1409 */
1410
1411 event.xcrossing.detail = NotifyAncestor;
1412 Tk_BindEvent(textPtr->bindingTable, &event, textPtr->tkwin,
1413 numOldTags, (ClientData *) oldArrayPtr);
1414 }
1415 ckfree((char *) oldArrayPtr);
1416 }
1417
1418 /*
1419 * Reset the "current" mark (be careful to recompute its location,
1420 * since it might have changed during an event binding). Then
1421 * invoke the binding system with an EnterNotify event for all of
1422 * the tags that have just appeared.
1423 */
1424
1425 TkTextPixelIndex(textPtr, textPtr->pickEvent.xcrossing.x,
1426 textPtr->pickEvent.xcrossing.y, &index);
1427 TkTextSetMark(textPtr, "current", &index);
1428 if (numNewTags != 0) {
1429 if ((textPtr->bindingTable != NULL) && (textPtr->tkwin != NULL)) {
1430 event = textPtr->pickEvent;
1431 event.type = EnterNotify;
1432 event.xcrossing.detail = NotifyAncestor;
1433 Tk_BindEvent(textPtr->bindingTable, &event, textPtr->tkwin,
1434 numNewTags, (ClientData *) copyArrayPtr);
1435 }
1436 ckfree((char *) copyArrayPtr);
1437 }
1438 }