home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tk8.0 / generic / tkCanvText.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  38.2 KB  |  1,313 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * tkCanvText.c --
  3.  *
  4.  *    This file implements text items for canvas widgets.
  5.  *
  6.  * Copyright (c) 1991-1994 The Regents of the University of California.
  7.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  *
  12.  * SCCS: @(#) tkCanvText.c 1.67 97/06/17 17:46:45
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include "tkInt.h"
  17. #include "tkCanvas.h"
  18. #include "tkPort.h"
  19.  
  20. /*
  21.  * The structure below defines the record for each text item.
  22.  */
  23.  
  24. typedef struct TextItem  {
  25.     Tk_Item header;        /* Generic stuff that's the same for all
  26.                  * types.  MUST BE FIRST IN STRUCTURE. */
  27.     Tk_CanvasTextInfo *textInfoPtr;
  28.                 /* Pointer to a structure containing
  29.                  * information about the selection and
  30.                  * insertion cursor.  The structure is owned
  31.                  * by (and shared with) the generic canvas
  32.                  * code. */
  33.     /*
  34.      * Fields that are set by widget commands other than "configure".
  35.      */
  36.      
  37.     double x, y;        /* Positioning point for text. */
  38.     int insertPos;        /* Insertion cursor is displayed just to left
  39.                  * of character with this index. */
  40.  
  41.     /*
  42.      * Configuration settings that are updated by Tk_ConfigureWidget.
  43.      */
  44.  
  45.     Tk_Anchor anchor;        /* Where to anchor text relative to (x,y). */
  46.     XColor *color;        /* Color for text. */
  47.     Tk_Font tkfont;        /* Font for drawing text. */
  48.     Tk_Justify justify;        /* Justification mode for text. */
  49.     Pixmap stipple;        /* Stipple bitmap for text, or None. */
  50.     char *text;            /* Text for item (malloc-ed). */
  51.     int width;            /* Width of lines for word-wrap, pixels.
  52.                  * Zero means no word-wrap. */
  53.  
  54.     /*
  55.      * Fields whose values are derived from the current values of the
  56.      * configuration settings above.
  57.      */
  58.  
  59.     int numChars;        /* Number of non-NULL characters in text. */
  60.     Tk_TextLayout textLayout;    /* Cached text layout information. */
  61.     int leftEdge;        /* Pixel location of the left edge of the
  62.                  * text item; where the left border of the
  63.                  * text layout is drawn. */
  64.     int rightEdge;        /* Pixel just to right of right edge of
  65.                  * area of text item.  Used for selecting up
  66.                  * to end of line. */
  67.     GC gc;            /* Graphics context for drawing text. */
  68.     GC selTextGC;        /* Graphics context for selected text. */
  69.     GC cursorOffGC;        /* If not None, this gives a graphics context
  70.                  * to use to draw the insertion cursor when
  71.                  * it's off.  Used if the selection and
  72.                  * insertion cursor colors are the same.  */
  73. } TextItem;
  74.  
  75. /*
  76.  * Information used for parsing configuration specs:
  77.  */
  78.  
  79. static Tk_CustomOption tagsOption = {Tk_CanvasTagsParseProc,
  80.     Tk_CanvasTagsPrintProc, (ClientData) NULL
  81. };
  82.  
  83. static Tk_ConfigSpec configSpecs[] = {
  84.     {TK_CONFIG_ANCHOR, "-anchor", (char *) NULL, (char *) NULL,
  85.     "center", Tk_Offset(TextItem, anchor),
  86.     TK_CONFIG_DONT_SET_DEFAULT},
  87.     {TK_CONFIG_COLOR, "-fill", (char *) NULL, (char *) NULL,
  88.     "black", Tk_Offset(TextItem, color), 0},
  89.     {TK_CONFIG_FONT, "-font", (char *) NULL, (char *) NULL,
  90.     "Helvetica 12 bold", Tk_Offset(TextItem, tkfont), 0},
  91.     {TK_CONFIG_JUSTIFY, "-justify", (char *) NULL, (char *) NULL,
  92.     "left", Tk_Offset(TextItem, justify),
  93.     TK_CONFIG_DONT_SET_DEFAULT},
  94.     {TK_CONFIG_BITMAP, "-stipple", (char *) NULL, (char *) NULL,
  95.     (char *) NULL, Tk_Offset(TextItem, stipple), TK_CONFIG_NULL_OK},
  96.     {TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
  97.     (char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
  98.     {TK_CONFIG_STRING, "-text", (char *) NULL, (char *) NULL,
  99.     "", Tk_Offset(TextItem, text), 0},
  100.     {TK_CONFIG_PIXELS, "-width", (char *) NULL, (char *) NULL,
  101.     "0", Tk_Offset(TextItem, width), TK_CONFIG_DONT_SET_DEFAULT},
  102.     {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
  103.     (char *) NULL, 0, 0}
  104. };
  105.  
  106. /*
  107.  * Prototypes for procedures defined in this file:
  108.  */
  109.  
  110. static void        ComputeTextBbox _ANSI_ARGS_((Tk_Canvas canvas,
  111.                 TextItem *textPtr));
  112. static int        ConfigureText _ANSI_ARGS_((Tcl_Interp *interp,
  113.                 Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
  114.                 char **argv, int flags));
  115. static int        CreateText _ANSI_ARGS_((Tcl_Interp *interp,
  116.                 Tk_Canvas canvas, struct Tk_Item *itemPtr,
  117.                 int argc, char **argv));
  118. static void        DeleteText _ANSI_ARGS_((Tk_Canvas canvas,
  119.                 Tk_Item *itemPtr, Display *display));
  120. static void        DisplayCanvText _ANSI_ARGS_((Tk_Canvas canvas,
  121.                 Tk_Item *itemPtr, Display *display, Drawable dst,
  122.                 int x, int y, int width, int height));
  123. static int        GetSelText _ANSI_ARGS_((Tk_Canvas canvas,
  124.                 Tk_Item *itemPtr, int offset, char *buffer,
  125.                 int maxBytes));
  126. static int        GetTextIndex _ANSI_ARGS_((Tcl_Interp *interp,
  127.                 Tk_Canvas canvas, Tk_Item *itemPtr,
  128.                 char *indexString, int *indexPtr));
  129. static void        ScaleText _ANSI_ARGS_((Tk_Canvas canvas,
  130.                 Tk_Item *itemPtr, double originX, double originY,
  131.                 double scaleX, double scaleY));
  132. static void        SetTextCursor _ANSI_ARGS_((Tk_Canvas canvas,
  133.                 Tk_Item *itemPtr, int index));
  134. static int        TextCoords _ANSI_ARGS_((Tcl_Interp *interp,
  135.                 Tk_Canvas canvas, Tk_Item *itemPtr,
  136.                 int argc, char **argv));
  137. static void        TextDeleteChars _ANSI_ARGS_((Tk_Canvas canvas,
  138.                 Tk_Item *itemPtr, int first, int last));
  139. static void        TextInsert _ANSI_ARGS_((Tk_Canvas canvas,
  140.                 Tk_Item *itemPtr, int beforeThis, char *string));
  141. static int        TextToArea _ANSI_ARGS_((Tk_Canvas canvas,
  142.                 Tk_Item *itemPtr, double *rectPtr));
  143. static double        TextToPoint _ANSI_ARGS_((Tk_Canvas canvas,
  144.                 Tk_Item *itemPtr, double *pointPtr));
  145. static int        TextToPostscript _ANSI_ARGS_((Tcl_Interp *interp,
  146.                 Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));
  147. static void        TranslateText _ANSI_ARGS_((Tk_Canvas canvas,
  148.                 Tk_Item *itemPtr, double deltaX, double deltaY));
  149.  
  150. /*
  151.  * The structures below defines the rectangle and oval item types
  152.  * by means of procedures that can be invoked by generic item code.
  153.  */
  154.  
  155. Tk_ItemType tkTextType = {
  156.     "text",                /* name */
  157.     sizeof(TextItem),            /* itemSize */
  158.     CreateText,                /* createProc */
  159.     configSpecs,            /* configSpecs */
  160.     ConfigureText,            /* configureProc */
  161.     TextCoords,                /* coordProc */
  162.     DeleteText,                /* deleteProc */
  163.     DisplayCanvText,            /* displayProc */
  164.     0,                    /* alwaysRedraw */
  165.     TextToPoint,            /* pointProc */
  166.     TextToArea,                /* areaProc */
  167.     TextToPostscript,            /* postscriptProc */
  168.     ScaleText,                /* scaleProc */
  169.     TranslateText,            /* translateProc */
  170.     GetTextIndex,            /* indexProc */
  171.     SetTextCursor,            /* icursorProc */
  172.     GetSelText,                /* selectionProc */
  173.     TextInsert,                /* insertProc */
  174.     TextDeleteChars,            /* dTextProc */
  175.     (Tk_ItemType *) NULL        /* nextPtr */
  176. };
  177.  
  178. /*
  179.  *--------------------------------------------------------------
  180.  *
  181.  * CreateText --
  182.  *
  183.  *    This procedure is invoked to create a new text item
  184.  *    in a canvas.
  185.  *
  186.  * Results:
  187.  *    A standard Tcl return value.  If an error occurred in
  188.  *    creating the item then an error message is left in
  189.  *    interp->result;  in this case itemPtr is left uninitialized
  190.  *    so it can be safely freed by the caller.
  191.  *
  192.  * Side effects:
  193.  *    A new text item is created.
  194.  *
  195.  *--------------------------------------------------------------
  196.  */
  197.  
  198. static int
  199. CreateText(interp, canvas, itemPtr, argc, argv)
  200.     Tcl_Interp *interp;            /* Interpreter for error reporting. */
  201.     Tk_Canvas canvas;            /* Canvas to hold new item. */
  202.     Tk_Item *itemPtr;            /* Record to hold new item;  header
  203.                      * has been initialized by caller. */
  204.     int argc;                /* Number of arguments in argv. */
  205.     char **argv;            /* Arguments describing rectangle. */
  206. {
  207.     TextItem *textPtr = (TextItem *) itemPtr;
  208.  
  209.     if (argc < 2) {
  210.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  211.         Tk_PathName(Tk_CanvasTkwin(canvas)), " create ",
  212.         itemPtr->typePtr->name, " x y ?options?\"", (char *) NULL);
  213.     return TCL_ERROR;
  214.     }
  215.  
  216.     /*
  217.      * Carry out initialization that is needed in order to clean
  218.      * up after errors during the the remainder of this procedure.
  219.      */
  220.  
  221.     textPtr->textInfoPtr = Tk_CanvasGetTextInfo(canvas);
  222.  
  223.     textPtr->insertPos    = 0;
  224.  
  225.     textPtr->anchor    = TK_ANCHOR_CENTER;
  226.     textPtr->color    = NULL;
  227.     textPtr->tkfont    = NULL;
  228.     textPtr->justify    = TK_JUSTIFY_LEFT;
  229.     textPtr->stipple    = None;
  230.     textPtr->text    = NULL;
  231.     textPtr->width    = 0;
  232.  
  233.     textPtr->numChars    = 0;
  234.     textPtr->textLayout = NULL;
  235.     textPtr->leftEdge    = 0;
  236.     textPtr->rightEdge    = 0;
  237.     textPtr->gc        = None;
  238.     textPtr->selTextGC    = None;
  239.     textPtr->cursorOffGC = None;
  240.  
  241.     /*
  242.      * Process the arguments to fill in the item record.
  243.      */
  244.  
  245.     if ((Tk_CanvasGetCoord(interp, canvas, argv[0], &textPtr->x) != TCL_OK)
  246.         || (Tk_CanvasGetCoord(interp, canvas, argv[1], &textPtr->y)
  247.         != TCL_OK)) {
  248.     return TCL_ERROR;
  249.     }
  250.  
  251.     if (ConfigureText(interp, canvas, itemPtr, argc-2, argv+2, 0) != TCL_OK) {
  252.     DeleteText(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
  253.     return TCL_ERROR;
  254.     }
  255.     return TCL_OK;
  256. }
  257.  
  258. /*
  259.  *--------------------------------------------------------------
  260.  *
  261.  * TextCoords --
  262.  *
  263.  *    This procedure is invoked to process the "coords" widget
  264.  *    command on text items.  See the user documentation for
  265.  *    details on what it does.
  266.  *
  267.  * Results:
  268.  *    Returns TCL_OK or TCL_ERROR, and sets interp->result.
  269.  *
  270.  * Side effects:
  271.  *    The coordinates for the given item may be changed.
  272.  *
  273.  *--------------------------------------------------------------
  274.  */
  275.  
  276. static int
  277. TextCoords(interp, canvas, itemPtr, argc, argv)
  278.     Tcl_Interp *interp;            /* Used for error reporting. */
  279.     Tk_Canvas canvas;            /* Canvas containing item. */
  280.     Tk_Item *itemPtr;            /* Item whose coordinates are to be
  281.                      * read or modified. */
  282.     int argc;                /* Number of coordinates supplied in
  283.                      * argv. */
  284.     char **argv;            /* Array of coordinates: x1, y1,
  285.                      * x2, y2, ... */
  286. {
  287.     TextItem *textPtr = (TextItem *) itemPtr;
  288.     char x[TCL_DOUBLE_SPACE], y[TCL_DOUBLE_SPACE];
  289.  
  290.     if (argc == 0) {
  291.     Tcl_PrintDouble(interp, textPtr->x, x);
  292.     Tcl_PrintDouble(interp, textPtr->y, y);
  293.     Tcl_AppendResult(interp, x, " ", y, (char *) NULL);
  294.     } else if (argc == 2) {
  295.     if ((Tk_CanvasGetCoord(interp, canvas, argv[0], &textPtr->x) != TCL_OK)
  296.         || (Tk_CanvasGetCoord(interp, canvas, argv[1],
  297.             &textPtr->y) != TCL_OK)) {
  298.         return TCL_ERROR;
  299.     }
  300.     ComputeTextBbox(canvas, textPtr);
  301.     } else {
  302.     sprintf(interp->result,
  303.         "wrong # coordinates: expected 0 or 2, got %d", argc);
  304.     return TCL_ERROR;
  305.     }
  306.     return TCL_OK;
  307. }
  308.  
  309. /*
  310.  *--------------------------------------------------------------
  311.  *
  312.  * ConfigureText --
  313.  *
  314.  *    This procedure is invoked to configure various aspects
  315.  *    of a text item, such as its border and background colors.
  316.  *
  317.  * Results:
  318.  *    A standard Tcl result code.  If an error occurs, then
  319.  *    an error message is left in interp->result.
  320.  *
  321.  * Side effects:
  322.  *    Configuration information, such as colors and stipple
  323.  *    patterns, may be set for itemPtr.
  324.  *
  325.  *--------------------------------------------------------------
  326.  */
  327.  
  328. static int
  329. ConfigureText(interp, canvas, itemPtr, argc, argv, flags)
  330.     Tcl_Interp *interp;        /* Interpreter for error reporting. */
  331.     Tk_Canvas canvas;        /* Canvas containing itemPtr. */
  332.     Tk_Item *itemPtr;        /* Rectangle item to reconfigure. */
  333.     int argc;            /* Number of elements in argv.  */
  334.     char **argv;        /* Arguments describing things to configure. */
  335.     int flags;            /* Flags to pass to Tk_ConfigureWidget. */
  336. {
  337.     TextItem *textPtr = (TextItem *) itemPtr;
  338.     XGCValues gcValues;
  339.     GC newGC, newSelGC;
  340.     unsigned long mask;
  341.     Tk_Window tkwin;
  342.     Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
  343.     XColor *selBgColorPtr;
  344.  
  345.     tkwin = Tk_CanvasTkwin(canvas);
  346.     if (Tk_ConfigureWidget(interp, tkwin, configSpecs, argc, argv,
  347.         (char *) textPtr, flags) != TCL_OK) {
  348.     return TCL_ERROR;
  349.     }
  350.  
  351.     /*
  352.      * A few of the options require additional processing, such as
  353.      * graphics contexts.
  354.      */
  355.  
  356.     newGC = newSelGC = None;
  357.     if ((textPtr->color != NULL) && (textPtr->tkfont != NULL)) {
  358.     gcValues.foreground = textPtr->color->pixel;
  359.     gcValues.font = Tk_FontId(textPtr->tkfont);
  360.     mask = GCForeground|GCFont;
  361.     if (textPtr->stipple != None) {
  362.         gcValues.stipple = textPtr->stipple;
  363.         gcValues.fill_style = FillStippled;
  364.         mask |= GCForeground|GCStipple|GCFillStyle;
  365.     }
  366.     newGC = Tk_GetGC(tkwin, mask, &gcValues);
  367.     gcValues.foreground = textInfoPtr->selFgColorPtr->pixel;
  368.     newSelGC = Tk_GetGC(tkwin, mask, &gcValues);
  369.     }
  370.     if (textPtr->gc != None) {
  371.     Tk_FreeGC(Tk_Display(tkwin), textPtr->gc);
  372.     }
  373.     textPtr->gc = newGC;
  374.     if (textPtr->selTextGC != None) {
  375.     Tk_FreeGC(Tk_Display(tkwin), textPtr->selTextGC);
  376.     }
  377.     textPtr->selTextGC = newSelGC;
  378.  
  379.     selBgColorPtr = Tk_3DBorderColor(textInfoPtr->selBorder);
  380.     if (Tk_3DBorderColor(textInfoPtr->insertBorder)->pixel
  381.         == selBgColorPtr->pixel) {
  382.     if (selBgColorPtr->pixel == BlackPixelOfScreen(Tk_Screen(tkwin))) {
  383.         gcValues.foreground = WhitePixelOfScreen(Tk_Screen(tkwin));
  384.     } else {
  385.         gcValues.foreground = BlackPixelOfScreen(Tk_Screen(tkwin));
  386.     }
  387.     newGC = Tk_GetGC(tkwin, GCForeground, &gcValues);
  388.     } else {
  389.     newGC = None;
  390.     }
  391.     if (textPtr->cursorOffGC != None) {
  392.     Tk_FreeGC(Tk_Display(tkwin), textPtr->cursorOffGC);
  393.     }
  394.     textPtr->cursorOffGC = newGC;
  395.  
  396.  
  397.     /*
  398.      * If the text was changed, move the selection and insertion indices
  399.      * to keep them inside the item.
  400.      */
  401.  
  402.     textPtr->numChars = strlen(textPtr->text);
  403.     if (textInfoPtr->selItemPtr == itemPtr) {
  404.     if (textInfoPtr->selectFirst >= textPtr->numChars) {
  405.         textInfoPtr->selItemPtr = NULL;
  406.     } else {
  407.         if (textInfoPtr->selectLast >= textPtr->numChars) {
  408.         textInfoPtr->selectLast = textPtr->numChars-1;
  409.         }
  410.         if ((textInfoPtr->anchorItemPtr == itemPtr)
  411.             && (textInfoPtr->selectAnchor >= textPtr->numChars)) {
  412.         textInfoPtr->selectAnchor = textPtr->numChars-1;
  413.         }
  414.     }
  415.     }
  416.     if (textPtr->insertPos >= textPtr->numChars) {
  417.     textPtr->insertPos = textPtr->numChars;
  418.     }
  419.  
  420.     ComputeTextBbox(canvas, textPtr);
  421.     return TCL_OK;
  422. }
  423.  
  424. /*
  425.  *--------------------------------------------------------------
  426.  *
  427.  * DeleteText --
  428.  *
  429.  *    This procedure is called to clean up the data structure
  430.  *    associated with a text item.
  431.  *
  432.  * Results:
  433.  *    None.
  434.  *
  435.  * Side effects:
  436.  *    Resources associated with itemPtr are released.
  437.  *
  438.  *--------------------------------------------------------------
  439.  */
  440.  
  441. static void
  442. DeleteText(canvas, itemPtr, display)
  443.     Tk_Canvas canvas;            /* Info about overall canvas widget. */
  444.     Tk_Item *itemPtr;            /* Item that is being deleted. */
  445.     Display *display;            /* Display containing window for
  446.                      * canvas. */
  447. {
  448.     TextItem *textPtr = (TextItem *) itemPtr;
  449.  
  450.     if (textPtr->color != NULL) {
  451.     Tk_FreeColor(textPtr->color);
  452.     }
  453.     Tk_FreeFont(textPtr->tkfont);
  454.     if (textPtr->stipple != None) {
  455.     Tk_FreeBitmap(display, textPtr->stipple);
  456.     }
  457.     if (textPtr->text != NULL) {
  458.     ckfree(textPtr->text);
  459.     }
  460.  
  461.     Tk_FreeTextLayout(textPtr->textLayout);
  462.     if (textPtr->gc != None) {
  463.     Tk_FreeGC(display, textPtr->gc);
  464.     }
  465.     if (textPtr->selTextGC != None) {
  466.     Tk_FreeGC(display, textPtr->selTextGC);
  467.     }
  468.     if (textPtr->cursorOffGC != None) {
  469.     Tk_FreeGC(display, textPtr->cursorOffGC);
  470.     }
  471. }
  472.  
  473. /*
  474.  *--------------------------------------------------------------
  475.  *
  476.  * ComputeTextBbox --
  477.  *
  478.  *    This procedure is invoked to compute the bounding box of
  479.  *    all the pixels that may be drawn as part of a text item.
  480.  *    In addition, it recomputes all of the geometry information
  481.  *    used to display a text item or check for mouse hits.
  482.  *
  483.  * Results:
  484.  *    None.
  485.  *
  486.  * Side effects:
  487.  *    The fields x1, y1, x2, and y2 are updated in the header
  488.  *    for itemPtr, and the linePtr structure is regenerated
  489.  *    for itemPtr.
  490.  *
  491.  *--------------------------------------------------------------
  492.  */
  493.  
  494. static void
  495. ComputeTextBbox(canvas, textPtr)
  496.     Tk_Canvas canvas;            /* Canvas that contains item. */
  497.     TextItem *textPtr;            /* Item whose bbos is to be
  498.                      * recomputed. */
  499. {
  500.     Tk_CanvasTextInfo *textInfoPtr;
  501.     int leftX, topY, width, height, fudge;
  502.  
  503.     Tk_FreeTextLayout(textPtr->textLayout);
  504.     textPtr->textLayout = Tk_ComputeTextLayout(textPtr->tkfont,
  505.         textPtr->text, textPtr->numChars, textPtr->width,
  506.         textPtr->justify, 0, &width, &height);
  507.  
  508.     /*
  509.      * Use overall geometry information to compute the top-left corner
  510.      * of the bounding box for the text item.
  511.      */
  512.  
  513.     leftX = (int) (textPtr->x + 0.5);
  514.     topY = (int) (textPtr->y + 0.5);
  515.     switch (textPtr->anchor) {
  516.     case TK_ANCHOR_NW:
  517.     case TK_ANCHOR_N:
  518.     case TK_ANCHOR_NE:
  519.         break;
  520.  
  521.     case TK_ANCHOR_W:
  522.     case TK_ANCHOR_CENTER:
  523.     case TK_ANCHOR_E:
  524.         topY -= height / 2;
  525.         break;
  526.  
  527.     case TK_ANCHOR_SW:
  528.     case TK_ANCHOR_S:
  529.     case TK_ANCHOR_SE:
  530.         topY -= height;
  531.         break;
  532.     }
  533.     switch (textPtr->anchor) {
  534.     case TK_ANCHOR_NW:
  535.     case TK_ANCHOR_W:
  536.     case TK_ANCHOR_SW:
  537.         break;
  538.  
  539.     case TK_ANCHOR_N:
  540.     case TK_ANCHOR_CENTER:
  541.     case TK_ANCHOR_S:
  542.         leftX -= width / 2;
  543.         break;
  544.  
  545.     case TK_ANCHOR_NE:
  546.     case TK_ANCHOR_E:
  547.     case TK_ANCHOR_SE:
  548.         leftX -= width;
  549.         break;
  550.     }
  551.  
  552.     textPtr->leftEdge  = leftX;
  553.     textPtr->rightEdge = leftX + width;
  554.  
  555.     /*
  556.      * Last of all, update the bounding box for the item.  The item's
  557.      * bounding box includes the bounding box of all its lines, plus
  558.      * an extra fudge factor for the cursor border (which could
  559.      * potentially be quite large).
  560.      */
  561.  
  562.     textInfoPtr = textPtr->textInfoPtr;
  563.     fudge = (textInfoPtr->insertWidth + 1) / 2;
  564.     if (textInfoPtr->selBorderWidth > fudge) {
  565.     fudge = textInfoPtr->selBorderWidth;
  566.     }
  567.     textPtr->header.x1 = leftX - fudge;
  568.     textPtr->header.y1 = topY;
  569.     textPtr->header.x2 = leftX + width + fudge;
  570.     textPtr->header.y2 = topY + height;
  571. }
  572.  
  573. /*
  574.  *--------------------------------------------------------------
  575.  *
  576.  * DisplayCanvText --
  577.  *
  578.  *    This procedure is invoked to draw a text item in a given
  579.  *    drawable.
  580.  *
  581.  * Results:
  582.  *    None.
  583.  *
  584.  * Side effects:
  585.  *    ItemPtr is drawn in drawable using the transformation
  586.  *    information in canvas.
  587.  *
  588.  *--------------------------------------------------------------
  589.  */
  590.  
  591. static void
  592. DisplayCanvText(canvas, itemPtr, display, drawable, x, y, width, height)
  593.     Tk_Canvas canvas;            /* Canvas that contains item. */
  594.     Tk_Item *itemPtr;            /* Item to be displayed. */
  595.     Display *display;            /* Display on which to draw item. */
  596.     Drawable drawable;            /* Pixmap or window in which to draw
  597.                      * item. */
  598.     int x, y, width, height;        /* Describes region of canvas that
  599.                      * must be redisplayed (not used). */
  600. {
  601.     TextItem *textPtr;
  602.     Tk_CanvasTextInfo *textInfoPtr;
  603.     int selFirst, selLast;
  604.     short drawableX, drawableY;
  605.  
  606.     textPtr = (TextItem *) itemPtr;
  607.     textInfoPtr = textPtr->textInfoPtr;
  608.  
  609.     if (textPtr->gc == None) {
  610.     return;
  611.     }
  612.  
  613.     /*
  614.      * If we're stippling, then modify the stipple offset in the GC.  Be
  615.      * sure to reset the offset when done, since the GC is supposed to be
  616.      * read-only.
  617.      */
  618.  
  619.     if (textPtr->stipple != None) {
  620.     Tk_CanvasSetStippleOrigin(canvas, textPtr->gc);
  621.     }
  622.  
  623.     selFirst = -1;
  624.     selLast = 0;        /* lint. */
  625.     if (textInfoPtr->selItemPtr == itemPtr) {
  626.     selFirst = textInfoPtr->selectFirst;
  627.     selLast = textInfoPtr->selectLast;
  628.     if (selLast >= textPtr->numChars) {
  629.         selLast = textPtr->numChars - 1;
  630.     }
  631.     if ((selFirst >= 0) && (selFirst <= selLast)) {
  632.         /*
  633.          * Draw a special background under the selection.
  634.          */
  635.  
  636.         int xFirst, yFirst, hFirst;
  637.         int xLast, yLast, wLast;
  638.  
  639.         Tk_CharBbox(textPtr->textLayout, selFirst,
  640.             &xFirst, &yFirst, NULL, &hFirst);
  641.         Tk_CharBbox(textPtr->textLayout, selLast,
  642.             &xLast, &yLast, &wLast, NULL);
  643.  
  644.         /*
  645.          * If the selection spans the end of this line, then display
  646.          * selection background all the way to the end of the line.
  647.          * However, for the last line we only want to display up to the
  648.          * last character, not the end of the line.
  649.          */
  650.  
  651.         x = xFirst;
  652.         height = hFirst;
  653.         for (y = yFirst ; y <= yLast; y += height) {
  654.         if (y == yLast) {
  655.             width = (xLast + wLast) - x;
  656.         } else {        
  657.             width = textPtr->rightEdge - textPtr->leftEdge - x;
  658.         }
  659.         Tk_CanvasDrawableCoords(canvas,
  660.             (double) (textPtr->leftEdge + x
  661.                 - textInfoPtr->selBorderWidth),
  662.             (double) (textPtr->header.y1 + y),
  663.             &drawableX, &drawableY);
  664.         Tk_Fill3DRectangle(Tk_CanvasTkwin(canvas), drawable,
  665.             textInfoPtr->selBorder, drawableX, drawableY,
  666.             width + 2 * textInfoPtr->selBorderWidth,
  667.             height, textInfoPtr->selBorderWidth, TK_RELIEF_RAISED);
  668.         x = 0;
  669.         }
  670.     }
  671.     }
  672.  
  673.     /*
  674.      * If the insertion point should be displayed, then draw a special
  675.      * background for the cursor before drawing the text.  Note:  if
  676.      * we're the cursor item but the cursor is turned off, then redraw
  677.      * background over the area of the cursor.  This guarantees that
  678.      * the selection won't make the cursor invisible on mono displays,
  679.      * where both are drawn in the same color.
  680.      */
  681.  
  682.     if ((textInfoPtr->focusItemPtr == itemPtr) && (textInfoPtr->gotFocus)) {
  683.     if (Tk_CharBbox(textPtr->textLayout, textPtr->insertPos,
  684.         &x, &y, NULL, &height)) {
  685.         Tk_CanvasDrawableCoords(canvas,
  686.             (double) (textPtr->leftEdge + x
  687.                 - (textInfoPtr->insertWidth / 2)),
  688.             (double) (textPtr->header.y1 + y),
  689.             &drawableX, &drawableY);
  690.         if (textInfoPtr->cursorOn) {
  691.         Tk_Fill3DRectangle(Tk_CanvasTkwin(canvas), drawable,
  692.             textInfoPtr->insertBorder,
  693.             drawableX, drawableY,
  694.             textInfoPtr->insertWidth, height,
  695.             textInfoPtr->insertBorderWidth, TK_RELIEF_RAISED);
  696.         } else if (textPtr->cursorOffGC != None) {
  697.         /*
  698.          * Redraw the background over the area of the cursor,
  699.          * even though the cursor is turned off.  This
  700.          * guarantees that the selection won't make the cursor
  701.          * invisible on mono displays, where both may be drawn
  702.          * in the same color.
  703.          */
  704.  
  705.         XFillRectangle(display, drawable, textPtr->cursorOffGC,
  706.             drawableX, drawableY,
  707.             (unsigned) textInfoPtr->insertWidth,
  708.             (unsigned) height);
  709.         }
  710.     }
  711.     }
  712.  
  713.  
  714.     /*
  715.      * Display the text in two pieces: draw the entire text item, then
  716.      * draw the selected text on top of it.  The selected text then
  717.      * will only need to be drawn if it has different attributes (such
  718.      * as foreground color) than regular text.
  719.      */
  720.  
  721.     Tk_CanvasDrawableCoords(canvas, (double) textPtr->leftEdge,
  722.         (double) textPtr->header.y1, &drawableX, &drawableY);
  723.     Tk_DrawTextLayout(display, drawable, textPtr->gc, textPtr->textLayout,
  724.         drawableX, drawableY, 0, -1);
  725.  
  726.     if ((selFirst >= 0) && (textPtr->selTextGC != textPtr->gc)) {
  727.     Tk_DrawTextLayout(display, drawable, textPtr->selTextGC,
  728.         textPtr->textLayout, drawableX, drawableY, selFirst,
  729.         selLast + 1);
  730.     }
  731.  
  732.     if (textPtr->stipple != None) {
  733.     XSetTSOrigin(display, textPtr->gc, 0, 0);
  734.     }
  735. }
  736.  
  737. /*
  738.  *--------------------------------------------------------------
  739.  *
  740.  * TextInsert --
  741.  *
  742.  *    Insert characters into a text item at a given position.
  743.  *
  744.  * Results:
  745.  *    None.
  746.  *
  747.  * Side effects:
  748.  *    The text in the given item is modified.  The cursor and
  749.  *    selection positions are also modified to reflect the
  750.  *    insertion.
  751.  *
  752.  *--------------------------------------------------------------
  753.  */
  754.  
  755. static void
  756. TextInsert(canvas, itemPtr, beforeThis, string)
  757.     Tk_Canvas canvas;        /* Canvas containing text item. */
  758.     Tk_Item *itemPtr;        /* Text item to be modified. */
  759.     int beforeThis;        /* Index of character before which text is
  760.                  * to be inserted. */
  761.     char *string;        /* New characters to be inserted. */
  762. {
  763.     TextItem *textPtr = (TextItem *) itemPtr;
  764.     int length;
  765.     char *new;
  766.     Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
  767.  
  768.     length = strlen(string);
  769.     if (length == 0) {
  770.     return;
  771.     }
  772.     if (beforeThis < 0) {
  773.     beforeThis = 0;
  774.     }
  775.     if (beforeThis > textPtr->numChars) {
  776.     beforeThis = textPtr->numChars;
  777.     }
  778.  
  779.     new = (char *) ckalloc((unsigned) (textPtr->numChars + length + 1));
  780.     strncpy(new, textPtr->text, (size_t) beforeThis);
  781.     strcpy(new+beforeThis, string);
  782.     strcpy(new+beforeThis+length, textPtr->text+beforeThis);
  783.     ckfree(textPtr->text);
  784.     textPtr->text = new;
  785.     textPtr->numChars += length;
  786.  
  787.     /*
  788.      * Inserting characters invalidates indices such as those for the
  789.      * selection and cursor.  Update the indices appropriately.
  790.      */
  791.  
  792.     if (textInfoPtr->selItemPtr == itemPtr) {
  793.     if (textInfoPtr->selectFirst >= beforeThis) {
  794.         textInfoPtr->selectFirst += length;
  795.     }
  796.     if (textInfoPtr->selectLast >= beforeThis) {
  797.         textInfoPtr->selectLast += length;
  798.     }
  799.     if ((textInfoPtr->anchorItemPtr == itemPtr)
  800.         && (textInfoPtr->selectAnchor >= beforeThis)) {
  801.         textInfoPtr->selectAnchor += length;
  802.     }
  803.     }
  804.     if (textPtr->insertPos >= beforeThis) {
  805.     textPtr->insertPos += length;
  806.     }
  807.     ComputeTextBbox(canvas, textPtr);
  808. }
  809.  
  810. /*
  811.  *--------------------------------------------------------------
  812.  *
  813.  * TextDeleteChars --
  814.  *
  815.  *    Delete one or more characters from a text item.
  816.  *
  817.  * Results:
  818.  *    None.
  819.  *
  820.  * Side effects:
  821.  *    Characters between "first" and "last", inclusive, get
  822.  *    deleted from itemPtr, and things like the selection
  823.  *    position get updated.
  824.  *
  825.  *--------------------------------------------------------------
  826.  */
  827.  
  828. static void
  829. TextDeleteChars(canvas, itemPtr, first, last)
  830.     Tk_Canvas canvas;        /* Canvas containing itemPtr. */
  831.     Tk_Item *itemPtr;        /* Item in which to delete characters. */
  832.     int first;            /* Index of first character to delete. */
  833.     int last;            /* Index of last character to delete. */
  834. {
  835.     TextItem *textPtr = (TextItem *) itemPtr;
  836.     int count;
  837.     char *new;
  838.     Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
  839.  
  840.     if (first < 0) {
  841.     first = 0;
  842.     }
  843.     if (last >= textPtr->numChars) {
  844.     last = textPtr->numChars-1;
  845.     }
  846.     if (first > last) {
  847.     return;
  848.     }
  849.     count = last + 1 - first;
  850.  
  851.     new = (char *) ckalloc((unsigned) (textPtr->numChars + 1 - count));
  852.     strncpy(new, textPtr->text, (size_t) first);
  853.     strcpy(new+first, textPtr->text+last+1);
  854.     ckfree(textPtr->text);
  855.     textPtr->text = new;
  856.     textPtr->numChars -= count;
  857.  
  858.     /*
  859.      * Update indexes for the selection and cursor to reflect the
  860.      * renumbering of the remaining characters.
  861.      */
  862.  
  863.     if (textInfoPtr->selItemPtr == itemPtr) {
  864.     if (textInfoPtr->selectFirst > first) {
  865.         textInfoPtr->selectFirst -= count;
  866.         if (textInfoPtr->selectFirst < first) {
  867.         textInfoPtr->selectFirst = first;
  868.         }
  869.     }
  870.     if (textInfoPtr->selectLast >= first) {
  871.         textInfoPtr->selectLast -= count;
  872.         if (textInfoPtr->selectLast < (first-1)) {
  873.         textInfoPtr->selectLast = (first-1);
  874.         }
  875.     }
  876.     if (textInfoPtr->selectFirst > textInfoPtr->selectLast) {
  877.         textInfoPtr->selItemPtr = NULL;
  878.     }
  879.     if ((textInfoPtr->anchorItemPtr == itemPtr)
  880.         && (textInfoPtr->selectAnchor > first)) {
  881.         textInfoPtr->selectAnchor -= count;
  882.         if (textInfoPtr->selectAnchor < first) {
  883.         textInfoPtr->selectAnchor = first;
  884.         }
  885.     }
  886.     }
  887.     if (textPtr->insertPos > first) {
  888.     textPtr->insertPos -= count;
  889.     if (textPtr->insertPos < first) {
  890.         textPtr->insertPos = first;
  891.     }
  892.     }
  893.     ComputeTextBbox(canvas, textPtr);
  894.     return;
  895. }
  896.  
  897. /*
  898.  *--------------------------------------------------------------
  899.  *
  900.  * TextToPoint --
  901.  *
  902.  *    Computes the distance from a given point to a given
  903.  *    text item, in canvas units.
  904.  *
  905.  * Results:
  906.  *    The return value is 0 if the point whose x and y coordinates
  907.  *    are pointPtr[0] and pointPtr[1] is inside the text item.  If
  908.  *    the point isn't inside the text item then the return value
  909.  *    is the distance from the point to the text item.
  910.  *
  911.  * Side effects:
  912.  *    None.
  913.  *
  914.  *--------------------------------------------------------------
  915.  */
  916.  
  917. static double
  918. TextToPoint(canvas, itemPtr, pointPtr)
  919.     Tk_Canvas canvas;        /* Canvas containing itemPtr. */
  920.     Tk_Item *itemPtr;        /* Item to check against point. */
  921.     double *pointPtr;        /* Pointer to x and y coordinates. */
  922. {
  923.     TextItem *textPtr;
  924.  
  925.     textPtr = (TextItem *) itemPtr;
  926.     return (double) Tk_DistanceToTextLayout(textPtr->textLayout,
  927.         (int) pointPtr[0] - textPtr->leftEdge,
  928.         (int) pointPtr[1] - textPtr->header.y1);
  929. }
  930.  
  931. /*
  932.  *--------------------------------------------------------------
  933.  *
  934.  * TextToArea --
  935.  *
  936.  *    This procedure is called to determine whether an item
  937.  *    lies entirely inside, entirely outside, or overlapping
  938.  *    a given rectangle.
  939.  *
  940.  * Results:
  941.  *    -1 is returned if the item is entirely outside the area
  942.  *    given by rectPtr, 0 if it overlaps, and 1 if it is entirely
  943.  *    inside the given area.
  944.  *
  945.  * Side effects:
  946.  *    None.
  947.  *
  948.  *--------------------------------------------------------------
  949.  */
  950.  
  951. static int
  952. TextToArea(canvas, itemPtr, rectPtr)
  953.     Tk_Canvas canvas;        /* Canvas containing itemPtr. */
  954.     Tk_Item *itemPtr;        /* Item to check against rectangle. */
  955.     double *rectPtr;        /* Pointer to array of four coordinates
  956.                  * (x1, y1, x2, y2) describing rectangular
  957.                  * area.  */
  958. {
  959.     TextItem *textPtr;
  960.  
  961.     textPtr = (TextItem *) itemPtr;
  962.     return Tk_IntersectTextLayout(textPtr->textLayout,
  963.         (int) (rectPtr[0] + 0.5) - textPtr->leftEdge,
  964.         (int) (rectPtr[1] + 0.5) - textPtr->header.y1,
  965.         (int) (rectPtr[2] - rectPtr[0] + 0.5),
  966.         (int) (rectPtr[3] - rectPtr[1] + 0.5));
  967. }
  968.  
  969. /*
  970.  *--------------------------------------------------------------
  971.  *
  972.  * ScaleText --
  973.  *
  974.  *    This procedure is invoked to rescale a text item.
  975.  *
  976.  * Results:
  977.  *    None.
  978.  *
  979.  * Side effects:
  980.  *    Scales the position of the text, but not the size
  981.  *    of the font for the text.
  982.  *
  983.  *--------------------------------------------------------------
  984.  */
  985.  
  986.     /* ARGSUSED */
  987. static void
  988. ScaleText(canvas, itemPtr, originX, originY, scaleX, scaleY)
  989.     Tk_Canvas canvas;            /* Canvas containing rectangle. */
  990.     Tk_Item *itemPtr;            /* Rectangle to be scaled. */
  991.     double originX, originY;        /* Origin about which to scale rect. */
  992.     double scaleX;            /* Amount to scale in X direction. */
  993.     double scaleY;            /* Amount to scale in Y direction. */
  994. {
  995.     TextItem *textPtr = (TextItem *) itemPtr;
  996.  
  997.     textPtr->x = originX + scaleX*(textPtr->x - originX);
  998.     textPtr->y = originY + scaleY*(textPtr->y - originY);
  999.     ComputeTextBbox(canvas, textPtr);
  1000.     return;
  1001. }
  1002.  
  1003. /*
  1004.  *--------------------------------------------------------------
  1005.  *
  1006.  * TranslateText --
  1007.  *
  1008.  *    This procedure is called to move a text item by a
  1009.  *    given amount.
  1010.  *
  1011.  * Results:
  1012.  *    None.
  1013.  *
  1014.  * Side effects:
  1015.  *    The position of the text item is offset by (xDelta, yDelta),
  1016.  *    and the bounding box is updated in the generic part of the
  1017.  *    item structure.
  1018.  *
  1019.  *--------------------------------------------------------------
  1020.  */
  1021.  
  1022. static void
  1023. TranslateText(canvas, itemPtr, deltaX, deltaY)
  1024.     Tk_Canvas canvas;            /* Canvas containing item. */
  1025.     Tk_Item *itemPtr;            /* Item that is being moved. */
  1026.     double deltaX, deltaY;        /* Amount by which item is to be
  1027.                      * moved. */
  1028. {
  1029.     TextItem *textPtr = (TextItem *) itemPtr;
  1030.  
  1031.     textPtr->x += deltaX;
  1032.     textPtr->y += deltaY;
  1033.     ComputeTextBbox(canvas, textPtr);
  1034. }
  1035.  
  1036. /*
  1037.  *--------------------------------------------------------------
  1038.  *
  1039.  * GetTextIndex --
  1040.  *
  1041.  *    Parse an index into a text item and return either its value
  1042.  *    or an error.
  1043.  *
  1044.  * Results:
  1045.  *    A standard Tcl result.  If all went well, then *indexPtr is
  1046.  *    filled in with the index (into itemPtr) corresponding to
  1047.  *    string.  Otherwise an error message is left in
  1048.  *    interp->result.
  1049.  *
  1050.  * Side effects:
  1051.  *    None.
  1052.  *
  1053.  *--------------------------------------------------------------
  1054.  */
  1055.  
  1056. static int
  1057. GetTextIndex(interp, canvas, itemPtr, string, indexPtr)
  1058.     Tcl_Interp *interp;        /* Used for error reporting. */
  1059.     Tk_Canvas canvas;        /* Canvas containing item. */
  1060.     Tk_Item *itemPtr;        /* Item for which the index is being
  1061.                  * specified. */
  1062.     char *string;        /* Specification of a particular character
  1063.                  * in itemPtr's text. */
  1064.     int *indexPtr;        /* Where to store converted index. */
  1065. {
  1066.     TextItem *textPtr = (TextItem *) itemPtr;
  1067.     size_t length;
  1068.     int c;
  1069.     TkCanvas *canvasPtr = (TkCanvas *) canvas;
  1070.     Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
  1071.  
  1072.     c = string[0];
  1073.     length = strlen(string);
  1074.  
  1075.     if ((c == 'e') && (strncmp(string, "end", length) == 0)) {
  1076.     *indexPtr = textPtr->numChars;
  1077.     } else if ((c == 'i') && (strncmp(string, "insert", length) == 0)) {
  1078.     *indexPtr = textPtr->insertPos;
  1079.     } else if ((c == 's') && (strncmp(string, "sel.first", length) == 0)
  1080.         && (length >= 5)) {
  1081.     if (textInfoPtr->selItemPtr != itemPtr) {
  1082.         interp->result = "selection isn't in item";
  1083.         return TCL_ERROR;
  1084.     }
  1085.     *indexPtr = textInfoPtr->selectFirst;
  1086.     } else if ((c == 's') && (strncmp(string, "sel.last", length) == 0)
  1087.         && (length >= 5)) {
  1088.     if (textInfoPtr->selItemPtr != itemPtr) {
  1089.         interp->result = "selection isn't in item";
  1090.         return TCL_ERROR;
  1091.     }
  1092.     *indexPtr = textInfoPtr->selectLast;
  1093.     } else if (c == '@') {
  1094.     int x, y;
  1095.     double tmp;
  1096.     char *end, *p;
  1097.  
  1098.     p = string+1;
  1099.     tmp = strtod(p, &end);
  1100.     if ((end == p) || (*end != ',')) {
  1101.         goto badIndex;
  1102.     }
  1103.     x = (int) ((tmp < 0) ? tmp - 0.5 : tmp + 0.5);
  1104.     p = end+1;
  1105.     tmp = strtod(p, &end);
  1106.     if ((end == p) || (*end != 0)) {
  1107.         goto badIndex;
  1108.     }
  1109.     y = (int) ((tmp < 0) ? tmp - 0.5 : tmp + 0.5);
  1110.     *indexPtr = Tk_PointToChar(textPtr->textLayout,
  1111.         x + canvasPtr->scrollX1 - textPtr->leftEdge,
  1112.         y + canvasPtr->scrollY1 - textPtr->header.y1);
  1113.     } else if (Tcl_GetInt(interp, string, indexPtr) == TCL_OK) {
  1114.     if (*indexPtr < 0){
  1115.         *indexPtr = 0;
  1116.     } else if (*indexPtr > textPtr->numChars) {
  1117.         *indexPtr = textPtr->numChars;
  1118.     }
  1119.     } else {
  1120.     /*
  1121.      * Some of the paths here leave messages in interp->result,
  1122.      * so we have to clear it out before storing our own message.
  1123.      */
  1124.  
  1125.     badIndex:
  1126.     Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
  1127.     Tcl_AppendResult(interp, "bad index \"", string, "\"",
  1128.         (char *) NULL);
  1129.     return TCL_ERROR;
  1130.     }
  1131.     return TCL_OK;
  1132. }
  1133.  
  1134. /*
  1135.  *--------------------------------------------------------------
  1136.  *
  1137.  * SetTextCursor --
  1138.  *
  1139.  *    Set the position of the insertion cursor in this item.
  1140.  *
  1141.  * Results:
  1142.  *    None.
  1143.  *
  1144.  * Side effects:
  1145.  *    The cursor position will change.
  1146.  *
  1147.  *--------------------------------------------------------------
  1148.  */
  1149.  
  1150.     /* ARGSUSED */
  1151. static void
  1152. SetTextCursor(canvas, itemPtr, index)
  1153.     Tk_Canvas canvas;            /* Record describing canvas widget. */
  1154.     Tk_Item *itemPtr;            /* Text item in which cursor position
  1155.                      * is to be set. */
  1156.     int index;                /* Index of character just before which
  1157.                      * cursor is to be positioned. */
  1158. {
  1159.     TextItem *textPtr = (TextItem *) itemPtr;
  1160.  
  1161.     if (index < 0) {
  1162.     textPtr->insertPos = 0;
  1163.     } else  if (index > textPtr->numChars) {
  1164.     textPtr->insertPos = textPtr->numChars;
  1165.     } else {
  1166.     textPtr->insertPos = index;
  1167.     }
  1168. }
  1169.  
  1170. /*
  1171.  *--------------------------------------------------------------
  1172.  *
  1173.  * GetSelText --
  1174.  *
  1175.  *    This procedure is invoked to return the selected portion
  1176.  *    of a text item.  It is only called when this item has
  1177.  *    the selection.
  1178.  *
  1179.  * Results:
  1180.  *    The return value is the number of non-NULL bytes stored
  1181.  *    at buffer.  Buffer is filled (or partially filled) with a
  1182.  *    NULL-terminated string containing part or all of the selection,
  1183.  *    as given by offset and maxBytes.
  1184.  *
  1185.  * Side effects:
  1186.  *    None.
  1187.  *
  1188.  *--------------------------------------------------------------
  1189.  */
  1190.  
  1191. static int
  1192. GetSelText(canvas, itemPtr, offset, buffer, maxBytes)
  1193.     Tk_Canvas canvas;            /* Canvas containing selection. */
  1194.     Tk_Item *itemPtr;            /* Text item containing selection. */
  1195.     int offset;                /* Offset within selection of first
  1196.                      * character to be returned. */
  1197.     char *buffer;            /* Location in which to place
  1198.                      * selection. */
  1199.     int maxBytes;            /* Maximum number of bytes to place
  1200.                      * at buffer, not including terminating
  1201.                      * NULL character. */
  1202. {
  1203.     TextItem *textPtr = (TextItem *) itemPtr;
  1204.     int count;
  1205.     Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
  1206.  
  1207.     count = textInfoPtr->selectLast + 1 - textInfoPtr->selectFirst - offset;
  1208.     if (textInfoPtr->selectLast == textPtr->numChars) {
  1209.     count -= 1;
  1210.     }
  1211.     if (count > maxBytes) {
  1212.     count = maxBytes;
  1213.     }
  1214.     if (count <= 0) {
  1215.     return 0;
  1216.     }
  1217.     strncpy(buffer, textPtr->text + textInfoPtr->selectFirst + offset,
  1218.         (size_t) count);
  1219.     buffer[count] = '\0';
  1220.     return count;
  1221. }
  1222.  
  1223. /*
  1224.  *--------------------------------------------------------------
  1225.  *
  1226.  * TextToPostscript --
  1227.  *
  1228.  *    This procedure is called to generate Postscript for
  1229.  *    text items.
  1230.  *
  1231.  * Results:
  1232.  *    The return value is a standard Tcl result.  If an error
  1233.  *    occurs in generating Postscript then an error message is
  1234.  *    left in interp->result, replacing whatever used
  1235.  *    to be there.  If no error occurs, then Postscript for the
  1236.  *    item is appended to the result.
  1237.  *
  1238.  * Side effects:
  1239.  *    None.
  1240.  *
  1241.  *--------------------------------------------------------------
  1242.  */
  1243.  
  1244. static int
  1245. TextToPostscript(interp, canvas, itemPtr, prepass)
  1246.     Tcl_Interp *interp;            /* Leave Postscript or error message
  1247.                      * here. */
  1248.     Tk_Canvas canvas;            /* Information about overall canvas. */
  1249.     Tk_Item *itemPtr;            /* Item for which Postscript is
  1250.                      * wanted. */
  1251.     int prepass;            /* 1 means this is a prepass to
  1252.                      * collect font information;  0 means
  1253.                      * final Postscript is being created. */
  1254. {
  1255.     TextItem *textPtr = (TextItem *) itemPtr;
  1256.     int x, y;
  1257.     Tk_FontMetrics fm;
  1258.     char *justify;
  1259.     char buffer[500];
  1260.  
  1261.     if (textPtr->color == NULL) {
  1262.     return TCL_OK;
  1263.     }
  1264.  
  1265.     if (Tk_CanvasPsFont(interp, canvas, textPtr->tkfont) != TCL_OK) {
  1266.     return TCL_ERROR;
  1267.     }
  1268.     if (prepass != 0) {
  1269.     return TCL_OK;
  1270.     }
  1271.     if (Tk_CanvasPsColor(interp, canvas, textPtr->color) != TCL_OK) {
  1272.     return TCL_ERROR;
  1273.     }
  1274.     if (textPtr->stipple != None) {
  1275.     Tcl_AppendResult(interp, "/StippleText {\n    ",
  1276.         (char *) NULL);
  1277.     Tk_CanvasPsStipple(interp, canvas, textPtr->stipple);
  1278.     Tcl_AppendResult(interp, "} bind def\n", (char *) NULL);
  1279.     }
  1280.  
  1281.     sprintf(buffer, "%.15g %.15g [\n", textPtr->x,
  1282.         Tk_CanvasPsY(canvas, textPtr->y));
  1283.     Tcl_AppendResult(interp, buffer, (char *) NULL);
  1284.  
  1285.     Tk_TextLayoutToPostscript(interp, textPtr->textLayout);
  1286.  
  1287.     x = 0;  y = 0;  justify = NULL;    /* lint. */
  1288.     switch (textPtr->anchor) {
  1289.     case TK_ANCHOR_NW:    x = 0; y = 0;    break;
  1290.     case TK_ANCHOR_N:    x = 1; y = 0;    break;
  1291.     case TK_ANCHOR_NE:    x = 2; y = 0;    break;
  1292.     case TK_ANCHOR_E:    x = 2; y = 1;    break;
  1293.     case TK_ANCHOR_SE:    x = 2; y = 2;    break;
  1294.     case TK_ANCHOR_S:    x = 1; y = 2;    break;
  1295.     case TK_ANCHOR_SW:    x = 0; y = 2;    break;
  1296.     case TK_ANCHOR_W:    x = 0; y = 1;    break;
  1297.     case TK_ANCHOR_CENTER:    x = 1; y = 1;    break;
  1298.     }
  1299.     switch (textPtr->justify) {
  1300.         case TK_JUSTIFY_LEFT:    justify = "0";    break;
  1301.     case TK_JUSTIFY_CENTER: justify = "0.5";break;
  1302.     case TK_JUSTIFY_RIGHT:  justify = "1";    break;
  1303.     }
  1304.  
  1305.     Tk_GetFontMetrics(textPtr->tkfont, &fm);
  1306.     sprintf(buffer, "] %d %g %g %s %s DrawText\n",
  1307.         fm.linespace, x / -2.0, y / 2.0, justify,
  1308.         ((textPtr->stipple == None) ? "false" : "true"));
  1309.     Tcl_AppendResult(interp, buffer, (char *) NULL);
  1310.  
  1311.     return TCL_OK;
  1312. }
  1313.