home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tkisrc04.zip / tk / os2 / tkTextTag.c < prev    next >
C/C++ Source or Header  |  1998-08-07  |  42KB  |  1,379 lines

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