📄 tkcanvline.c
字号:
/* * tkCanvLine.c -- * * This file implements line items for canvas widgets. * * Copyright (c) 1991-1994 The Regents of the University of California. * Copyright (c) 1994-1995 Sun Microsystems, Inc. * * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * * SCCS: @(#) tkCanvLine.c 1.46 97/04/25 16:51:02 */#include <stdio.h>#include "tkInt.h"#include "tkPort.h"/* * The structure below defines the record for each line item. */typedef struct LineItem { Tk_Item header; /* Generic stuff that's the same for all * types. MUST BE FIRST IN STRUCTURE. */ Tk_Canvas canvas; /* Canvas containing item. Needed for * parsing arrow shapes. */ int numPoints; /* Number of points in line (always >= 2). */ double *coordPtr; /* Pointer to malloc-ed array containing * x- and y-coords of all points in line. * X-coords are even-valued indices, y-coords * are corresponding odd-valued indices. If * the line has arrowheads then the first * and last points have been adjusted to refer * to the necks of the arrowheads rather than * their tips. The actual endpoints are * stored in the *firstArrowPtr and * *lastArrowPtr, if they exist. */ int width; /* Width of line. */ XColor *fg; /* Foreground color for line. */ Pixmap fillStipple; /* Stipple bitmap for filling line. */ int capStyle; /* Cap style for line. */ int joinStyle; /* Join style for line. */ GC gc; /* Graphics context for filling line. */ GC arrowGC; /* Graphics context for drawing arrowheads. */ Tk_Uid arrow; /* Indicates whether or not to draw arrowheads: * "none", "first", "last", or "both". */ float arrowShapeA; /* Distance from tip of arrowhead to center. */ float arrowShapeB; /* Distance from tip of arrowhead to trailing * point, measured along shaft. */ float arrowShapeC; /* Distance of trailing points from outside * edge of shaft. */ double *firstArrowPtr; /* Points to array of PTS_IN_ARROW points * describing polygon for arrowhead at first * point in line. First point of arrowhead * is tip. Malloc'ed. NULL means no arrowhead * at first point. */ double *lastArrowPtr; /* Points to polygon for arrowhead at last * point in line (PTS_IN_ARROW points, first * of which is tip). Malloc'ed. NULL means * no arrowhead at last point. */ int smooth; /* Non-zero means draw line smoothed (i.e. * with Bezier splines). */ int splineSteps; /* Number of steps in each spline segment. */} LineItem;/* * Number of points in an arrowHead: */#define PTS_IN_ARROW 6/* * Prototypes for procedures defined in this file: */static int ArrowheadPostscript _ANSI_ARGS_((Tcl_Interp *interp, Tk_Canvas canvas, LineItem *linePtr, double *arrowPtr));static void ComputeLineBbox _ANSI_ARGS_((Tk_Canvas canvas, LineItem *linePtr));static int ConfigureLine _ANSI_ARGS_((Tcl_Interp *interp, Tk_Canvas canvas, Tk_Item *itemPtr, int argc, char **argv, int flags));static int ConfigureArrows _ANSI_ARGS_((Tk_Canvas canvas, LineItem *linePtr));static int CreateLine _ANSI_ARGS_((Tcl_Interp *interp, Tk_Canvas canvas, struct Tk_Item *itemPtr, int argc, char **argv));static void DeleteLine _ANSI_ARGS_((Tk_Canvas canvas, Tk_Item *itemPtr, Display *display));static void DisplayLine _ANSI_ARGS_((Tk_Canvas canvas, Tk_Item *itemPtr, Display *display, Drawable dst, int x, int y, int width, int height));static int LineCoords _ANSI_ARGS_((Tcl_Interp *interp, Tk_Canvas canvas, Tk_Item *itemPtr, int argc, char **argv));static int LineToArea _ANSI_ARGS_((Tk_Canvas canvas, Tk_Item *itemPtr, double *rectPtr));static double LineToPoint _ANSI_ARGS_((Tk_Canvas canvas, Tk_Item *itemPtr, double *coordPtr));static int LineToPostscript _ANSI_ARGS_((Tcl_Interp *interp, Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));static int ParseArrowShape _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, Tk_Window tkwin, char *value, char *recordPtr, int offset));static char * PrintArrowShape _ANSI_ARGS_((ClientData clientData, Tk_Window tkwin, char *recordPtr, int offset, Tcl_FreeProc **freeProcPtr));static void ScaleLine _ANSI_ARGS_((Tk_Canvas canvas, Tk_Item *itemPtr, double originX, double originY, double scaleX, double scaleY));static void TranslateLine _ANSI_ARGS_((Tk_Canvas canvas, Tk_Item *itemPtr, double deltaX, double deltaY));/* * Information used for parsing configuration specs. If you change any * of the default strings, be sure to change the corresponding default * values in CreateLine. */static Tk_CustomOption arrowShapeOption = {ParseArrowShape, PrintArrowShape, (ClientData) NULL};static Tk_CustomOption tagsOption = {Tk_CanvasTagsParseProc, Tk_CanvasTagsPrintProc, (ClientData) NULL};static Tk_ConfigSpec configSpecs[] = { {TK_CONFIG_UID, "-arrow", (char *) NULL, (char *) NULL, "none", Tk_Offset(LineItem, arrow), TK_CONFIG_DONT_SET_DEFAULT}, {TK_CONFIG_CUSTOM, "-arrowshape", (char *) NULL, (char *) NULL, "8 10 3", Tk_Offset(LineItem, arrowShapeA), TK_CONFIG_DONT_SET_DEFAULT, &arrowShapeOption}, {TK_CONFIG_CAP_STYLE, "-capstyle", (char *) NULL, (char *) NULL, "butt", Tk_Offset(LineItem, capStyle), TK_CONFIG_DONT_SET_DEFAULT}, {TK_CONFIG_COLOR, "-fill", (char *) NULL, (char *) NULL, "black", Tk_Offset(LineItem, fg), TK_CONFIG_NULL_OK}, {TK_CONFIG_JOIN_STYLE, "-joinstyle", (char *) NULL, (char *) NULL, "round", Tk_Offset(LineItem, joinStyle), TK_CONFIG_DONT_SET_DEFAULT}, {TK_CONFIG_BOOLEAN, "-smooth", (char *) NULL, (char *) NULL, "0", Tk_Offset(LineItem, smooth), TK_CONFIG_DONT_SET_DEFAULT}, {TK_CONFIG_INT, "-splinesteps", (char *) NULL, (char *) NULL, "12", Tk_Offset(LineItem, splineSteps), TK_CONFIG_DONT_SET_DEFAULT}, {TK_CONFIG_BITMAP, "-stipple", (char *) NULL, (char *) NULL, (char *) NULL, Tk_Offset(LineItem, fillStipple), TK_CONFIG_NULL_OK}, {TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL, (char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption}, {TK_CONFIG_PIXELS, "-width", (char *) NULL, (char *) NULL, "1", Tk_Offset(LineItem, width), TK_CONFIG_DONT_SET_DEFAULT}, {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL, (char *) NULL, 0, 0}};/* * The structures below defines the line item type by means * of procedures that can be invoked by generic item code. */Tk_ItemType tkLineType = { "line", /* name */ sizeof(LineItem), /* itemSize */ CreateLine, /* createProc */ configSpecs, /* configSpecs */ ConfigureLine, /* configureProc */ LineCoords, /* coordProc */ DeleteLine, /* deleteProc */ DisplayLine, /* displayProc */ 0, /* alwaysRedraw */ LineToPoint, /* pointProc */ LineToArea, /* areaProc */ LineToPostscript, /* postscriptProc */ ScaleLine, /* scaleProc */ TranslateLine, /* translateProc */ (Tk_ItemIndexProc *) NULL, /* indexProc */ (Tk_ItemCursorProc *) NULL, /* icursorProc */ (Tk_ItemSelectionProc *) NULL, /* selectionProc */ (Tk_ItemInsertProc *) NULL, /* insertProc */ (Tk_ItemDCharsProc *) NULL, /* dTextProc */ (Tk_ItemType *) NULL /* nextPtr */};/* * The Tk_Uid's below refer to uids for the various arrow types: */static Tk_Uid noneUid = NULL;static Tk_Uid firstUid = NULL;static Tk_Uid lastUid = NULL;static Tk_Uid bothUid = NULL;/* * The definition below determines how large are static arrays * used to hold spline points (splines larger than this have to * have their arrays malloc-ed). */#define MAX_STATIC_POINTS 200/* *-------------------------------------------------------------- * * CreateLine -- * * This procedure is invoked to create a new line item in * a canvas. * * Results: * A standard Tcl return value. If an error occurred in * creating the item, then an error message is left in * interp->result; in this case itemPtr is left uninitialized, * so it can be safely freed by the caller. * * Side effects: * A new line item is created. * *-------------------------------------------------------------- */static intCreateLine(interp, canvas, itemPtr, argc, argv) Tcl_Interp *interp; /* Interpreter for error reporting. */ Tk_Canvas canvas; /* Canvas to hold new item. */ Tk_Item *itemPtr; /* Record to hold new item; header * has been initialized by caller. */ int argc; /* Number of arguments in argv. */ char **argv; /* Arguments describing line. */{ LineItem *linePtr = (LineItem *) itemPtr; int i; if (argc < 4) { Tcl_AppendResult(interp, "wrong # args: should be \"", Tk_PathName(Tk_CanvasTkwin(canvas)), " create ", itemPtr->typePtr->name, " x1 y1 x2 y2 ?x3 y3 ...? ?options?\"", (char *) NULL); return TCL_ERROR; } /* * Carry out initialization that is needed to set defaults and to * allow proper cleanup after errors during the the remainder of * this procedure. */ linePtr->canvas = canvas; linePtr->numPoints = 0; linePtr->coordPtr = NULL; linePtr->width = 1; linePtr->fg = None; linePtr->fillStipple = None; linePtr->capStyle = CapButt; linePtr->joinStyle = JoinRound; linePtr->gc = None; linePtr->arrowGC = None; if (noneUid == NULL) { noneUid = Tk_GetUid("none"); firstUid = Tk_GetUid("first"); lastUid = Tk_GetUid("last"); bothUid = Tk_GetUid("both"); } linePtr->arrow = noneUid; linePtr->arrowShapeA = (float)8.0; linePtr->arrowShapeB = (float)10.0; linePtr->arrowShapeC = (float)3.0; linePtr->firstArrowPtr = NULL; linePtr->lastArrowPtr = NULL; linePtr->smooth = 0; linePtr->splineSteps = 12; /* * Count the number of points and then parse them into a point * array. Leading arguments are assumed to be points if they * start with a digit or a minus sign followed by a digit. */ for (i = 4; i < (argc-1); i+=2) { if ((!isdigit(UCHAR(argv[i][0]))) && ((argv[i][0] != '-') || ((argv[i][1] != '.') && !isdigit(UCHAR(argv[i][1]))))) { break; } } if (LineCoords(interp, canvas, itemPtr, i, argv) != TCL_OK) { goto error; } if (ConfigureLine(interp, canvas, itemPtr, argc-i, argv+i, 0) == TCL_OK) { return TCL_OK; } error: DeleteLine(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas))); return TCL_ERROR;}/* *-------------------------------------------------------------- * * LineCoords -- * * This procedure is invoked to process the "coords" widget * command on lines. See the user documentation for details * on what it does. * * Results: * Returns TCL_OK or TCL_ERROR, and sets interp->result. * * Side effects: * The coordinates for the given item may be changed. * *-------------------------------------------------------------- */static intLineCoords(interp, canvas, itemPtr, argc, argv) Tcl_Interp *interp; /* Used for error reporting. */ Tk_Canvas canvas; /* Canvas containing item. */ Tk_Item *itemPtr; /* Item whose coordinates are to be * read or modified. */ int argc; /* Number of coordinates supplied in * argv. */ char **argv; /* Array of coordinates: x1, y1, * x2, y2, ... */{ LineItem *linePtr = (LineItem *) itemPtr; char buffer[TCL_DOUBLE_SPACE]; int i, numPoints; if (argc == 0) { double *coordPtr; int numCoords; numCoords = 2*linePtr->numPoints; if (linePtr->firstArrowPtr != NULL) { coordPtr = linePtr->firstArrowPtr; } else { coordPtr = linePtr->coordPtr; } for (i = 0; i < numCoords; i++, coordPtr++) { if (i == 2) { coordPtr = linePtr->coordPtr+2; } if ((linePtr->lastArrowPtr != NULL) && (i == (numCoords-2))) { coordPtr = linePtr->lastArrowPtr; } Tcl_PrintDouble(interp, *coordPtr, buffer); Tcl_AppendElement(interp, buffer); } } else if (argc < 4) { Tcl_AppendResult(interp, "too few coordinates for line: must have at least 4", (char *) NULL); return TCL_ERROR; } else if (argc & 1) { Tcl_AppendResult(interp, "odd number of coordinates specified for line", (char *) NULL); return TCL_ERROR; } else { numPoints = argc/2; if (linePtr->numPoints != numPoints) { if (linePtr->coordPtr != NULL) { ckfree((char *) linePtr->coordPtr); } linePtr->coordPtr = (double *) ckalloc((unsigned) (sizeof(double) * argc)); linePtr->numPoints = numPoints; } for (i = argc-1; i >= 0; i--) { if (Tk_CanvasGetCoord(interp, canvas, argv[i], &linePtr->coordPtr[i]) != TCL_OK) { return TCL_ERROR; } } /* * Update arrowheads by throwing away any existing arrow-head * information and calling ConfigureArrows to recompute it. */ if (linePtr->firstArrowPtr != NULL) { ckfree((char *) linePtr->firstArrowPtr); linePtr->firstArrowPtr = NULL; } if (linePtr->lastArrowPtr != NULL) { ckfree((char *) linePtr->lastArrowPtr); linePtr->lastArrowPtr = NULL; } if (linePtr->arrow != noneUid) { ConfigureArrows(canvas, linePtr); } ComputeLineBbox(canvas, linePtr); } return TCL_OK;}/* *-------------------------------------------------------------- * * ConfigureLine -- * * This procedure is invoked to configure various aspects * of a line item such as its background color. * * Results: * A standard Tcl result code. If an error occurs, then * an error message is left in interp->result.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -