📄 tkplot.c
字号:
/* ***************************************************** * * SaVi by Robert Thurman (thurman@geom.umn.edu) and * Patrick Worfolk (worfolk@alum.mit.edu). * * Copyright (c) 1997 by The Geometry Center. * This file is part of SaVi. SaVi is free software; * you can redistribute it and/or modify it only under * the terms given in the file COPYRIGHT which you should * have received along with this file. SaVi may be * obtained from: * http://savi.sourceforge.net/ * http://www.geom.uiuc.edu/locate/SaVi * ***************************************************** * * tkPlot.c * * Routines for implementing the new tk plot widget. * * $Id: tkPlot.c,v 1.5 2004/01/06 03:57:56 lloydwood Exp $ */#include <math.h>#include <stdlib.h>#include <string.h>#include "tkPlot.h"#include "stats_utils.h"/* * Prototypes for procedures defined later in this file: */static int ConfigurePlot _ANSI_ARGS_((Tcl_Interp * interp, TkPlot * plotPtr, int argc, char **argv, int flags));static void PlotBindProc _ANSI_ARGS_((ClientData clientData, XEvent * eventPtr));static void PlotCmdDeletedProc _ANSI_ARGS_((ClientData clientData));static void PlotEventProc _ANSI_ARGS_((ClientData clientData, XEvent * eventPtr));static int PlotWidgetCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp * interp, int argc, char **argv));static int Tk_PlotCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp * interp, int argc, char **argv));static void PlotDisplay(ClientData clientData);static void PlotDestroy(ClientData clientData);static void colormap_init();static unsigned short hex_to_int(char *h);static char *color_names[] = { "#ff0000", /* red */ "#00ff00", /* green */ "#0000ff", /* blue */ "#ee82ee", /* violet */ "#ffa500", /* orange */ "#ffff00", /* yellow */ "#a020f0", /* purple */ "#ffc0cb", /* pink */ "#add8e6", /* light blue */ "#ffd700", /* gold */ "#00ffff", /* cyan */ "#20b257", /* light sea green */ "#c0c0c0", /* grey */ "#ff00ff", /* magenta *//* REMOVE THESE COLORS FOR DSTOOL - worfolk */ "red", "green", "blue", "violet", "orange", "yellow", "purple", "pink", "light blue", "gold", "cyan", "light sea green", "grey", "magenta", "#acf", "white", "black", "#ffff00", "#ff7f00", "#ff3f00", "#ff1f00"};#define NCOLORS (sizeof(color_names)/sizeof(char *))static XColor **colormap = NULL;/* * Create the colors array for all plot widgets */static voidcolormap_init(Tcl_Interp * interp, TkPlot * plotPtr){ unsigned int i; if (!colormap) { colormap = (XColor **) ckalloc(sizeof(XColor *) * NCOLORS); for (i = 0; i < NCOLORS; i++) colormap[i] = Tk_GetColor(interp, plotPtr->tkwin, color_names[i]); }}intTk_PlotColorLookup(const char color[]){ unsigned int i; for (i = 0; i < NCOLORS; i++) { if (strcmp(color, color_names[i]) == 0) return i; } return 0; /* default color is first color in colormap! */}XColor *Tk_PlotGetColor(unsigned int i){ if (!colormap) return (XColor *) NULL; i = i % NCOLORS; return colormap[i];}XColor *Tk_PlotGetRGB(unsigned int i){ static XColor color; char *c; i = i % NCOLORS; if (!colormap) { c = color_names[i] + 2; color.red = hex_to_int(c) * USHRT_MAX / 255; c += 2; color.green = hex_to_int(c) * USHRT_MAX / 255; c += 2; color.blue = hex_to_int(c) * USHRT_MAX / 255; return &color; } else { return colormap[i]; }}static unsigned shorthex_to_int(char *h){ static char hex[] = "0xff"; hex[2] = *(h++); hex[3] = *h; return ((unsigned short) atoi(hex));}intTk_PlotGetNColors(){ return NCOLORS;}/* * Information used for argv parsing. */static Tk_ConfigSpec configSpecs[] = { {TK_CONFIG_BORDER, "-background", "background", "Background", DEF_PLOT_BG_COLOR, Tk_Offset(TkPlot, bgBorder), TK_CONFIG_COLOR_ONLY}, {TK_CONFIG_BORDER, "-background", "background", "Background", DEF_PLOT_BG_MONO, Tk_Offset(TkPlot, 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", DEF_PLOT_BORDER_WIDTH, Tk_Offset(TkPlot, borderWidth), 0},/* {TK_CONFIG_DOUBLE, "-closeenough", "closeEnough", "CloseEnough", DEF_PLOT_CLOSE_ENOUGH, Tk_Offset(TkPlot, closeEnough), 0},*/ {TK_CONFIG_BOOLEAN, "-confine", "confine", "Confine", DEF_PLOT_CONFINE, Tk_Offset(TkPlot, confine), 0}, {TK_CONFIG_ACTIVE_CURSOR, "-cursor", "cursor", "Cursor", DEF_PLOT_CURSOR, Tk_Offset(TkPlot, cursor), TK_CONFIG_NULL_OK}, {TK_CONFIG_PIXELS, "-height", "height", "Height", DEF_PLOT_HEIGHT, Tk_Offset(TkPlot, height), 0}, {TK_CONFIG_COLOR, "-highlightbackground", "highlightBackground", "HighlightBackground", DEF_PLOT_HIGHLIGHT_BG, Tk_Offset(TkPlot, highlightBgColorPtr), 0}, {TK_CONFIG_COLOR, "-highlightcolor", "highlightColor", "HighlightColor", DEF_PLOT_HIGHLIGHT, Tk_Offset(TkPlot, highlightColorPtr), 0}, {TK_CONFIG_PIXELS, "-highlightthickness", "highlightThickness", "HighlightThickness", DEF_PLOT_HIGHLIGHT_WIDTH, Tk_Offset(TkPlot, highlightWidth), 0},/* {TK_CONFIG_BORDER, "-insertbackground", "insertBackground", "Foreground", DEF_PLOT_INSERT_BG, Tk_Offset(TkPlot, textInfo.insertBorder), 0}, {TK_CONFIG_PIXELS, "-insertborderwidth", "insertBorderWidth", "BorderWidth", DEF_PLOT_INSERT_BD_COLOR, Tk_Offset(TkPlot, textInfo.insertBorderWidth), TK_CONFIG_COLOR_ONLY}, {TK_CONFIG_PIXELS, "-insertborderwidth", "insertBorderWidth", "BorderWidth", DEF_PLOT_INSERT_BD_MONO, Tk_Offset(TkPlot, textInfo.insertBorderWidth), TK_CONFIG_MONO_ONLY}, {TK_CONFIG_INT, "-insertofftime", "insertOffTime", "OffTime", DEF_PLOT_INSERT_OFF_TIME, Tk_Offset(TkPlot, insertOffTime), 0}, {TK_CONFIG_INT, "-insertontime", "insertOnTime", "OnTime", DEF_PLOT_INSERT_ON_TIME, Tk_Offset(TkPlot, insertOnTime), 0}, {TK_CONFIG_PIXELS, "-insertwidth", "insertWidth", "InsertWidth", DEF_PLOT_INSERT_WIDTH, Tk_Offset(TkPlot, textInfo.insertWidth), 0},*/ {TK_CONFIG_RELIEF, "-relief", "relief", "Relief", DEF_PLOT_RELIEF, Tk_Offset(TkPlot,relief), 0},/* {TK_CONFIG_STRING, "-scrollregion", "scrollRegion", "ScrollRegion", DEF_PLOT_SCROLL_REGION, Tk_Offset(TkPlot, regionString), TK_CONFIG_NULL_OK}, {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground", DEF_PLOT_SELECT_COLOR, Tk_Offset(TkPlot, textInfo.selBorder), TK_CONFIG_COLOR_ONLY}, {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground", DEF_PLOT_SELECT_MONO, Tk_Offset(TkPlot, textInfo.selBorder), TK_CONFIG_MONO_ONLY}, {TK_CONFIG_PIXELS, "-selectborderwidth", "selectBorderWidth", "BorderWidth", DEF_PLOT_SELECT_BD_COLOR, Tk_Offset(TkPlot, textInfo.selBorderWidth), TK_CONFIG_COLOR_ONLY}, {TK_CONFIG_PIXELS, "-selectborderwidth", "selectBorderWidth", "BorderWidth", DEF_PLOT_SELECT_BD_MONO, Tk_Offset(TkPlot, textInfo.selBorderWidth), TK_CONFIG_MONO_ONLY}, {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background", DEF_PLOT_SELECT_FG_COLOR, Tk_Offset(TkPlot, textInfo.selFgColorPtr), TK_CONFIG_COLOR_ONLY}, {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background", DEF_PLOT_SELECT_FG_MONO, Tk_Offset(TkPlot, textInfo.selFgColorPtr), TK_CONFIG_MONO_ONLY},*/ {TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus", DEF_PLOT_TAKE_FOCUS, Tk_Offset(TkPlot, takeFocus), TK_CONFIG_NULL_OK}, {TK_CONFIG_PIXELS, "-width", "width", "Width", DEF_PLOT_WIDTH, Tk_Offset(TkPlot, width), 0},/* {TK_CONFIG_STRING, "-xscrollcommand", "xScrollCommand", "ScrollCommand", DEF_PLOT_X_SCROLL_CMD, Tk_Offset(TkPlot, xScrollCmd), TK_CONFIG_NULL_OK}, {TK_CONFIG_PIXELS, "-xscrollincrement", "xScrollIncrement", "ScrollIncrement", DEF_PLOT_X_SCROLL_INCREMENT, Tk_Offset(TkPlot, xScrollIncrement), 0}, {TK_CONFIG_STRING, "-yscrollcommand", "yScrollCommand", "ScrollCommand", DEF_PLOT_Y_SCROLL_CMD, Tk_Offset(TkPlot, yScrollCmd), TK_CONFIG_NULL_OK}, {TK_CONFIG_PIXELS, "-yscrollincrement", "yScrollIncrement", "ScrollIncrement", DEF_PLOT_Y_SCROLL_INCREMENT, Tk_Offset(TkPlot, yScrollIncrement), 0},*/ {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL, (char *) NULL, 0, 0}};/* * Plot_Init * * Creates the plot command. * Should be called from TclAppInit() * */intPlot_Init(Tcl_Interp * interpreter){ static char plot_widget_name[] = "plot"; Tcl_CreateCommand(interpreter, plot_widget_name, Tk_PlotCmd, (ClientData) Tk_MainWindow(interpreter), (Tcl_CmdDeleteProc *) NULL); return TCL_OK;}/* * Tk_PlotCmd * * The plot class command procedure. Invoked to process the "plot" * Tcl command. * */static intTk_PlotCmd(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. */{ static char Plot_class[] = "Plot"; Tk_Window tkwin = (Tk_Window) clientData; TkPlot *plotPtr; Tk_Window new; if (argc < 2) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " pathName ?options?\"", (char *) NULL); return TCL_ERROR; } new = Tk_CreateWindowFromPath(interp, tkwin, argv[1], (char *) NULL); if (!new) { return TCL_ERROR; } plotPtr = (TkPlot *) ckalloc(sizeof(TkPlot)); plotPtr->tkwin = new; plotPtr->display = Tk_Display(new); plotPtr->interp = interp; plotPtr->widgetCmd = Tcl_CreateCommand(interp, Tk_PathName(plotPtr->tkwin), PlotWidgetCmd, (ClientData) plotPtr, PlotCmdDeletedProc); plotPtr->borderWidth = 0; plotPtr->bgBorder = NULL; plotPtr->relief = TK_RELIEF_FLAT; plotPtr->highlightWidth = 0; plotPtr->highlightBgColorPtr = NULL; plotPtr->highlightColorPtr = NULL; plotPtr->inset = 0; plotPtr->pixmapGC = None; plotPtr->plotGC = None; plotPtr->width = None; plotPtr->height = None; plotPtr->confine = 0; plotPtr->pixmap = None;/* plotPtr->firstItemPtr = NULL; plotPtr->lastItemPtr = NULL; plotPtr->textInfo.selBorder = NULL; plotPtr->textInfo.selBorderWidth = 0; plotPtr->textInfo.selFgColorPtr = NULL; plotPtr->textInfo.selItemPtr = NULL; plotPtr->textInfo.selectFirst = -1; plotPtr->textInfo.selectLast = -1; plotPtr->textInfo.anchorItemPtr = NULL; plotPtr->textInfo.selectAnchor = 0; plotPtr->textInfo.insertBorder = NULL; plotPtr->textInfo.insertWidth = 0; plotPtr->textInfo.insertBorderWidth = 0; plotPtr->textInfo.focusItemPtr = NULL; plotPtr->textInfo.gotFocus = 0; plotPtr->textInfo.cursorOn = 0; plotPtr->insertOnTime = 0; plotPtr->insertOffTime = 0; plotPtr->insertBlinkHandler = (Tk_TimerToken) NULL;*/ plotPtr->xOrigin = plotPtr->yOrigin = 0; plotPtr->drawableXOrigin = plotPtr->drawableYOrigin = 0;/* plotPtr->bindingTable = NULL; plotPtr->currentItemPtr = NULL; plotPtr->closeEnough = 0.0; plotPtr->pickEvent.type = LeaveNotify; plotPtr->pickEvent.xcrossing.x = 0; plotPtr->pickEvent.xcrossing.y = 0; plotPtr->state = 0; plotPtr->xScrollCmd = NULL; plotPtr->yScrollCmd = NULL; plotPtr->scrollX1 = 0; plotPtr->scrollY1 = 0; plotPtr->scrollX2 = 0; plotPtr->scrollY2 = 0; plotPtr->regionString = NULL; plotPtr->xScrollIncrement = 0; plotPtr->yScrollIncrement = 0;*//* plotPtr->scanX = 0; plotPtr->scanXOrigin = 0; plotPtr->scanY = 0; plotPtr->scanYOrigin = 0; plotPtr->hotPtr = NULL; plotPtr->hotPrevPtr = NULL;*/ plotPtr->cursor = None; plotPtr->takeFocus = NULL; plotPtr->pixelsPerMM = WidthOfScreen(Tk_Screen(new)); plotPtr->pixelsPerMM /= WidthMMOfScreen(Tk_Screen(new)); plotPtr->flags = 0; plotPtr->nextId = 1; /* plotPtr->psInfoPtr = NULL; */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -