📄 tksquare.c
字号:
/* * tkSquare.c -- * * This module implements "square" widgets. A "square" is * a widget that displays a single square that can be moved * around and resized. This file is intended as an example * of how to build a widget; it isn't included in the * normal wish, but it is included in "tktest". * * Copyright (c) 1991-1994 The Regents of the University of California. * Copyright (c) 1994-1997 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: @(#) tkSquare.c 1.19 97/07/31 09:13:13 */#include "tkPort.h"#include "tk.h"/* * A data structure of the following type is kept for each square * widget managed by this file: */typedef struct { Tk_Window tkwin; /* Window that embodies the square. NULL * means window has been deleted but * widget record hasn't been cleaned up yet. */ Display *display; /* X's token for the window's display. */ Tcl_Interp *interp; /* Interpreter associated with widget. */ Tcl_Command widgetCmd; /* Token for square's widget command. */ int x, y; /* Position of square's upper-left corner * within widget. */ int size; /* Width and height of square. */ /* * Information used when displaying widget: */ int borderWidth; /* Width of 3-D border around whole widget. */ Tk_3DBorder bgBorder; /* Used for drawing background. */ Tk_3DBorder fgBorder; /* For drawing square. */ int relief; /* Indicates whether window as a whole is * raised, sunken, or flat. */ GC gc; /* Graphics context for copying from * off-screen pixmap onto screen. */ int doubleBuffer; /* Non-zero means double-buffer redisplay * with pixmap; zero means draw straight * onto the display. */ int updatePending; /* Non-zero means a call to SquareDisplay * has already been scheduled. */} Square;/* * Information used for argv parsing. */static Tk_ConfigSpec configSpecs[] = { {TK_CONFIG_BORDER, "-background", "background", "Background", "#d9d9d9", Tk_Offset(Square, bgBorder), TK_CONFIG_COLOR_ONLY}, {TK_CONFIG_BORDER, "-background", "background", "Background", "white", Tk_Offset(Square, bgBorder), TK_CONFIG_MONO_ONLY}, {TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *) NULL, (char *) NULL, 0, 0}, {TK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL, (char *) NULL, 0, 0}, {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth", "2", Tk_Offset(Square, borderWidth), 0}, {TK_CONFIG_INT, "-dbl", "doubleBuffer", "DoubleBuffer", "1", Tk_Offset(Square, doubleBuffer), 0}, {TK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL, (char *) NULL, 0, 0}, {TK_CONFIG_BORDER, "-foreground", "foreground", "Foreground", "#b03060", Tk_Offset(Square, fgBorder), TK_CONFIG_COLOR_ONLY}, {TK_CONFIG_BORDER, "-foreground", "foreground", "Foreground", "black", Tk_Offset(Square, fgBorder), TK_CONFIG_MONO_ONLY}, {TK_CONFIG_RELIEF, "-relief", "relief", "Relief", "raised", Tk_Offset(Square, relief), 0}, {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL, (char *) NULL, 0, 0}};/* * Forward declarations for procedures defined later in this file: */int SquareCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc, char **argv));static void SquareCmdDeletedProc _ANSI_ARGS_(( ClientData clientData));static int SquareConfigure _ANSI_ARGS_((Tcl_Interp *interp, Square *squarePtr, int argc, char **argv, int flags));static void SquareDestroy _ANSI_ARGS_((char *memPtr));static void SquareDisplay _ANSI_ARGS_((ClientData clientData));static void KeepInWindow _ANSI_ARGS_((Square *squarePtr));static void SquareEventProc _ANSI_ARGS_((ClientData clientData, XEvent *eventPtr));static int SquareWidgetCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp *, int argc, char **argv));/* *-------------------------------------------------------------- * * SquareCmd -- * * This procedure is invoked to process the "square" Tcl * command. It creates a new "square" widget. * * Results: * A standard Tcl result. * * Side effects: * A new widget is created and configured. * *-------------------------------------------------------------- */intSquareCmd(clientData, interp, argc, argv) ClientData clientData; /* Main window associated with * interpreter. */ Tcl_Interp *interp; /* Current interpreter. */ int argc; /* Number of arguments. */ char **argv; /* Argument strings. */{ Tk_Window main = (Tk_Window) clientData; Square *squarePtr; Tk_Window tkwin; if (argc < 2) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " pathName ?options?\"", (char *) NULL); return TCL_ERROR; } tkwin = Tk_CreateWindowFromPath(interp, main, argv[1], (char *) NULL); if (tkwin == NULL) { return TCL_ERROR; } Tk_SetClass(tkwin, "Square"); /* * Allocate and initialize the widget record. */ squarePtr = (Square *) ckalloc(sizeof(Square)); squarePtr->tkwin = tkwin; squarePtr->display = Tk_Display(tkwin); squarePtr->interp = interp; squarePtr->widgetCmd = Tcl_CreateCommand(interp, Tk_PathName(squarePtr->tkwin), SquareWidgetCmd, (ClientData) squarePtr, SquareCmdDeletedProc); squarePtr->x = 0; squarePtr->y = 0; squarePtr->size = 20; squarePtr->borderWidth = 0; squarePtr->bgBorder = NULL; squarePtr->fgBorder = NULL; squarePtr->relief = TK_RELIEF_FLAT; squarePtr->gc = None; squarePtr->doubleBuffer = 1; squarePtr->updatePending = 0; Tk_CreateEventHandler(squarePtr->tkwin, ExposureMask|StructureNotifyMask, SquareEventProc, (ClientData) squarePtr); if (SquareConfigure(interp, squarePtr, argc-2, argv+2, 0) != TCL_OK) { Tk_DestroyWindow(squarePtr->tkwin); return TCL_ERROR; } interp->result = Tk_PathName(squarePtr->tkwin); return TCL_OK;}/* *-------------------------------------------------------------- * * SquareWidgetCmd -- * * This procedure is invoked to process the Tcl command * that corresponds to a widget managed by this module. * See the user documentation for details on what it does. * * Results: * A standard Tcl result. * * Side effects: * See the user documentation. * *-------------------------------------------------------------- */static intSquareWidgetCmd(clientData, interp, argc, argv) ClientData clientData; /* Information about square widget. */ Tcl_Interp *interp; /* Current interpreter. */ int argc; /* Number of arguments. */ char **argv; /* Argument strings. */{ Square *squarePtr = (Square *) clientData; int result = TCL_OK; size_t length; char c; if (argc < 2) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " option ?arg arg ...?\"", (char *) NULL); return TCL_ERROR; } Tcl_Preserve((ClientData) squarePtr); c = argv[1][0]; length = strlen(argv[1]); if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0) && (length >= 2)) { if (argc != 3) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " cget option\"", (char *) NULL); goto error; } result = Tk_ConfigureValue(interp, squarePtr->tkwin, configSpecs, (char *) squarePtr, argv[2], 0); } else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0) && (length >= 2)) { if (argc == 2) { result = Tk_ConfigureInfo(interp, squarePtr->tkwin, configSpecs, (char *) squarePtr, (char *) NULL, 0); } else if (argc == 3) { result = Tk_ConfigureInfo(interp, squarePtr->tkwin, configSpecs, (char *) squarePtr, argv[2], 0); } else { result = SquareConfigure(interp, squarePtr, argc-2, argv+2, TK_CONFIG_ARGV_ONLY); } } else if ((c == 'p') && (strncmp(argv[1], "position", length) == 0)) { if ((argc != 2) && (argc != 4)) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " position ?x y?\"", (char *) NULL); goto error; } if (argc == 4) { if ((Tk_GetPixels(interp, squarePtr->tkwin, argv[2], &squarePtr->x) != TCL_OK) || (Tk_GetPixels(interp, squarePtr->tkwin, argv[3], &squarePtr->y) != TCL_OK)) { goto error; } KeepInWindow(squarePtr); } sprintf(interp->result, "%d %d", squarePtr->x, squarePtr->y); } else if ((c == 's') && (strncmp(argv[1], "size", length) == 0)) { if ((argc != 2) && (argc != 3)) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " size ?amount?\"", (char *) NULL); goto error; } if (argc == 3) { int i; if (Tk_GetPixels(interp, squarePtr->tkwin, argv[2], &i) != TCL_OK) { goto error; } if ((i <= 0) || (i > 100)) { Tcl_AppendResult(interp, "bad size \"", argv[2], "\"", (char *) NULL); goto error; } squarePtr->size = i; KeepInWindow(squarePtr); } sprintf(interp->result, "%d", squarePtr->size); } else { Tcl_AppendResult(interp, "bad option \"", argv[1], "\": must be cget, configure, position, or size", (char *) NULL); goto error; } if (!squarePtr->updatePending) { Tcl_DoWhenIdle(SquareDisplay, (ClientData) squarePtr); squarePtr->updatePending = 1; } Tcl_Release((ClientData) squarePtr); return result; error: Tcl_Release((ClientData) squarePtr); return TCL_ERROR;}/* *---------------------------------------------------------------------- *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -