📄 tkrectoval.c
字号:
/* * tkRectOval.c -- * * This file implements rectangle and oval items for canvas * widgets. * * Copyright (c) 1991-1994 The Regents of the University of California. * Copyright (c) 1994-1996 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: @(#) tkRectOval.c 1.40 96/05/03 10:52:21 */#include <stdio.h>#include "tk.h"#include "tkInt.h"#include "tkPort.h"/* * The structure below defines the record for each rectangle/oval item. */typedef struct RectOvalItem { Tk_Item header; /* Generic stuff that's the same for all * types. MUST BE FIRST IN STRUCTURE. */ double bbox[4]; /* Coordinates of bounding box for rectangle * or oval (x1, y1, x2, y2). Item includes * x1 and x2 but not y1 and y2. */ int width; /* Width of outline. */ XColor *outlineColor; /* Color for outline. */ XColor *fillColor; /* Color for filling rectangle/oval. */ Pixmap fillStipple; /* Stipple bitmap for filling item. */ GC outlineGC; /* Graphics context for outline. */ GC fillGC; /* Graphics context for filling item. */} RectOvalItem;/* * Information used for parsing configuration specs: */static Tk_CustomOption tagsOption = {Tk_CanvasTagsParseProc, Tk_CanvasTagsPrintProc, (ClientData) NULL};static Tk_ConfigSpec configSpecs[] = { {TK_CONFIG_COLOR, "-fill", (char *) NULL, (char *) NULL, (char *) NULL, Tk_Offset(RectOvalItem, fillColor), TK_CONFIG_NULL_OK}, {TK_CONFIG_COLOR, "-outline", (char *) NULL, (char *) NULL, "black", Tk_Offset(RectOvalItem, outlineColor), TK_CONFIG_NULL_OK}, {TK_CONFIG_BITMAP, "-stipple", (char *) NULL, (char *) NULL, (char *) NULL, Tk_Offset(RectOvalItem, 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(RectOvalItem, width), TK_CONFIG_DONT_SET_DEFAULT}, {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL, (char *) NULL, 0, 0}};/* * Prototypes for procedures defined in this file: */static void ComputeRectOvalBbox _ANSI_ARGS_((Tk_Canvas canvas, RectOvalItem *rectOvalPtr));static int ConfigureRectOval _ANSI_ARGS_((Tcl_Interp *interp, Tk_Canvas canvas, Tk_Item *itemPtr, int argc, char **argv, int flags));static int CreateRectOval _ANSI_ARGS_((Tcl_Interp *interp, Tk_Canvas canvas, struct Tk_Item *itemPtr, int argc, char **argv));static void DeleteRectOval _ANSI_ARGS_((Tk_Canvas canvas, Tk_Item *itemPtr, Display *display));static void DisplayRectOval _ANSI_ARGS_((Tk_Canvas canvas, Tk_Item *itemPtr, Display *display, Drawable dst, int x, int y, int width, int height));static int OvalToArea _ANSI_ARGS_((Tk_Canvas canvas, Tk_Item *itemPtr, double *areaPtr));static double OvalToPoint _ANSI_ARGS_((Tk_Canvas canvas, Tk_Item *itemPtr, double *pointPtr));static int RectOvalCoords _ANSI_ARGS_((Tcl_Interp *interp, Tk_Canvas canvas, Tk_Item *itemPtr, int argc, char **argv));static int RectOvalToPostscript _ANSI_ARGS_((Tcl_Interp *interp, Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));static int RectToArea _ANSI_ARGS_((Tk_Canvas canvas, Tk_Item *itemPtr, double *areaPtr));static double RectToPoint _ANSI_ARGS_((Tk_Canvas canvas, Tk_Item *itemPtr, double *pointPtr));static void ScaleRectOval _ANSI_ARGS_((Tk_Canvas canvas, Tk_Item *itemPtr, double originX, double originY, double scaleX, double scaleY));static void TranslateRectOval _ANSI_ARGS_((Tk_Canvas canvas, Tk_Item *itemPtr, double deltaX, double deltaY));/* * The structures below defines the rectangle and oval item types * by means of procedures that can be invoked by generic item code. */Tk_ItemType tkRectangleType = { "rectangle", /* name */ sizeof(RectOvalItem), /* itemSize */ CreateRectOval, /* createProc */ configSpecs, /* configSpecs */ ConfigureRectOval, /* configureProc */ RectOvalCoords, /* coordProc */ DeleteRectOval, /* deleteProc */ DisplayRectOval, /* displayProc */ 0, /* alwaysRedraw */ RectToPoint, /* pointProc */ RectToArea, /* areaProc */ RectOvalToPostscript, /* postscriptProc */ ScaleRectOval, /* scaleProc */ TranslateRectOval, /* 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 */};Tk_ItemType tkOvalType = { "oval", /* name */ sizeof(RectOvalItem), /* itemSize */ CreateRectOval, /* createProc */ configSpecs, /* configSpecs */ ConfigureRectOval, /* configureProc */ RectOvalCoords, /* coordProc */ DeleteRectOval, /* deleteProc */ DisplayRectOval, /* displayProc */ 0, /* alwaysRedraw */ OvalToPoint, /* pointProc */ OvalToArea, /* areaProc */ RectOvalToPostscript, /* postscriptProc */ ScaleRectOval, /* scaleProc */ TranslateRectOval, /* translateProc */ (Tk_ItemIndexProc *) NULL, /* indexProc */ (Tk_ItemCursorProc *) NULL, /* cursorProc */ (Tk_ItemSelectionProc *) NULL, /* selectionProc */ (Tk_ItemInsertProc *) NULL, /* insertProc */ (Tk_ItemDCharsProc *) NULL, /* dTextProc */ (Tk_ItemType *) NULL /* nextPtr */};/* *-------------------------------------------------------------- * * CreateRectOval -- * * This procedure is invoked to create a new rectangle * or oval 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 rectangle or oval item is created. * *-------------------------------------------------------------- */static intCreateRectOval(interp, canvas, itemPtr, argc, argv) Tcl_Interp *interp; /* 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 rectangle. */{ RectOvalItem *rectOvalPtr = (RectOvalItem *) itemPtr; if (argc < 4) { Tcl_AppendResult(interp, "wrong # args: should be \"", Tk_PathName(Tk_CanvasTkwin(canvas)), " create ", itemPtr->typePtr->name, " x1 y1 x2 y2 ?options?\"", (char *) NULL); return TCL_ERROR; } /* * Carry out initialization that is needed in order to clean * up after errors during the the remainder of this procedure. */ rectOvalPtr->width = 1; rectOvalPtr->outlineColor = NULL; rectOvalPtr->fillColor = NULL; rectOvalPtr->fillStipple = None; rectOvalPtr->outlineGC = None; rectOvalPtr->fillGC = None; /* * Process the arguments to fill in the item record. */ if ((Tk_CanvasGetCoord(interp, canvas, argv[0], &rectOvalPtr->bbox[0]) != TCL_OK) || (Tk_CanvasGetCoord(interp, canvas, argv[1], &rectOvalPtr->bbox[1]) != TCL_OK) || (Tk_CanvasGetCoord(interp, canvas, argv[2], &rectOvalPtr->bbox[2]) != TCL_OK) || (Tk_CanvasGetCoord(interp, canvas, argv[3], &rectOvalPtr->bbox[3]) != TCL_OK)) { return TCL_ERROR; } if (ConfigureRectOval(interp, canvas, itemPtr, argc-4, argv+4, 0) != TCL_OK) { DeleteRectOval(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas))); return TCL_ERROR; } return TCL_OK;}/* *-------------------------------------------------------------- * * RectOvalCoords -- * * This procedure is invoked to process the "coords" widget * command on rectangles and ovals. 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 intRectOvalCoords(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, ... */{ RectOvalItem *rectOvalPtr = (RectOvalItem *) itemPtr; char c0[TCL_DOUBLE_SPACE], c1[TCL_DOUBLE_SPACE]; char c2[TCL_DOUBLE_SPACE], c3[TCL_DOUBLE_SPACE]; if (argc == 0) { Tcl_PrintDouble(interp, rectOvalPtr->bbox[0], c0); Tcl_PrintDouble(interp, rectOvalPtr->bbox[1], c1); Tcl_PrintDouble(interp, rectOvalPtr->bbox[2], c2); Tcl_PrintDouble(interp, rectOvalPtr->bbox[3], c3); Tcl_AppendResult(interp, c0, " ", c1, " ", c2, " ", c3, (char *) NULL); } else if (argc == 4) { if ((Tk_CanvasGetCoord(interp, canvas, argv[0], &rectOvalPtr->bbox[0]) != TCL_OK) || (Tk_CanvasGetCoord(interp, canvas, argv[1], &rectOvalPtr->bbox[1]) != TCL_OK) || (Tk_CanvasGetCoord(interp, canvas, argv[2], &rectOvalPtr->bbox[2]) != TCL_OK) || (Tk_CanvasGetCoord(interp, canvas, argv[3], &rectOvalPtr->bbox[3]) != TCL_OK)) { return TCL_ERROR; } ComputeRectOvalBbox(canvas, rectOvalPtr); } else { sprintf(interp->result, "wrong # coordinates: expected 0 or 4, got %d", argc); return TCL_ERROR; } return TCL_OK;}/* *-------------------------------------------------------------- * * ConfigureRectOval -- * * This procedure is invoked to configure various aspects * of a rectangle or oval item, such as its border and * background colors. * * Results: * A standard Tcl result code. If an error occurs, then * an error message is left in interp->result. * * Side effects: * Configuration information, such as colors and stipple * patterns, may be set for itemPtr. * *-------------------------------------------------------------- */static intConfigureRectOval(interp, canvas, itemPtr, argc, argv, flags) Tcl_Interp *interp; /* Used for error reporting. */ Tk_Canvas canvas; /* Canvas containing itemPtr. */ Tk_Item *itemPtr; /* Rectangle item to reconfigure. */ int argc; /* Number of elements in argv. */ char **argv; /* Arguments describing things to configure. */ int flags; /* Flags to pass to Tk_ConfigureWidget. */{ RectOvalItem *rectOvalPtr = (RectOvalItem *) itemPtr; XGCValues gcValues; GC newGC; unsigned long mask; Tk_Window tkwin; tkwin = Tk_CanvasTkwin(canvas); if (Tk_ConfigureWidget(interp, tkwin, configSpecs, argc, argv, (char *) rectOvalPtr, flags) != TCL_OK) { return TCL_ERROR; } /* * A few of the options require additional processing, such as * graphics contexts. */ if (rectOvalPtr->width < 1) { rectOvalPtr->width = 1; } if (rectOvalPtr->outlineColor == NULL) { newGC = None; } else { gcValues.foreground = rectOvalPtr->outlineColor->pixel; gcValues.cap_style = CapProjecting; gcValues.line_width = rectOvalPtr->width; mask = GCForeground|GCCapStyle|GCLineWidth; newGC = Tk_GetGC(tkwin, mask, &gcValues); } if (rectOvalPtr->outlineGC != None) { Tk_FreeGC(Tk_Display(tkwin), rectOvalPtr->outlineGC); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -