📄 x10.c
字号:
/**********Copyright 1990 Regents of the University of California. All rights reserved.Author: 1988 Jeffrey M. Hsu**********//* X10 drivers.*/#include "spice.h"#ifdef HAS_X10#include "ftegraph.h"#include "ftedbgra.h"#include "ftedev.h"#include "fteinput.h"#include "cpdefs.h"#include "ftedefs.h"#include <X/Xlib.h>#include <X/cursors/left_ptr.cursor>#include <X/cursors/left_ptr_mask.cursor>/* forward declarations */static void eventhandler(), instantcolors(), initcolors();int errorhandler();/* X dependent default parameters */#define DEF_FONT "6x10"#define NUMLINESTYLES 8#define NXPLANES 5 /* note: What is this used for? */#define BOXSIZE 30 /* initial size of bounding box for zoomin */typedef struct { Window window; FontInfo *fontinfo; int lastx, lasty; /* used in X_DrawLine */ int lastlinestyle; /* used in X_DrawLine */} X10devdep;#define DEVDEP(g) (*((X10devdep *) (g)->devdep))static Display *display;/* ick, have to have this global version of display for HLP, change HLP XXX */Display *X_display;static XAssocTable *xtable; /* for getting from window id to graphid */static Pattern xlinestyles[NUMLINESTYLES];static int colors[NUMCOLORS];X_Init(){ char buf[512]; char *displayname; if (cp_getvar("display", VT_STRING, buf)) { displayname = buf; } else if (!(displayname = getenv("DISPLAY"))) { internalerror("Can't open X display."); return (1); } if (!(display = XOpenDisplay(displayname))) { sprintf(ErrorMessage, "Can't open %s.", displayname); internalerror(ErrorMessage); return (1); } X_display = display; /* for HLP */#ifdef HAS_XT /* initialize X toolkit */ XtInitialize();#endif /* assume maximum around 100 windows */ xtable = XCreateAssocTable(32); /* note: set up input sources and handlers for Xt */ /* set correct information */ dispdev->numlinestyles = NUMLINESTYLES; initcolors(); /* sets dispdev->numcolors */#define DisplayHeight() (_XlibCurrentDisplay->height)#define DisplayWidth() (_XlibCurrentDisplay->width) dispdev->width = DisplayWidth(); dispdev->height = DisplayHeight(); /* we don't want non-fatal X errors to call exit */ XErrorHandler(errorhandler); return (0);}interrorhandler(display, errorev)Display *display;XErrorEvent *errorev;{ externalerror(XErrDescrip(errorev->error_code)); return;}/* Recover from bad NewViewPort call. */#define RECOVERNEWVIEWPORT() free((char *) graph);\ graph = (GRAPH *) NULL; /* need to do this or else DestroyGraph will free it again *//* NewViewport is responsible for filling in graph->viewport */X_NewViewport(graph)GRAPH *graph;{ OpaqueFrame frame; char geometry[32], defaultgeometry[32]; WindowInfo wininfo; char fontname[64]; Cursor cursor; graph->devdep = tmalloc(sizeof(X10devdep)); if (!cp_getvar("geometry", VT_STRING, geometry)) { *geometry = '\0'; }/* note: tweak this */ (void) sprintf(defaultgeometry, "=%dx%d+%d+%d", 300, 300, 100, 100); frame.bdrwidth = 2; frame.background = WhitePixmap; frame.border = BlackPixmap; DEVDEP(graph).window = XCreate("nutmeg graph", "nutmeg", geometry, defaultgeometry, &frame, 300, 300); if (!DEVDEP(graph).window) { internalerror("can't open graph window"); RECOVERNEWVIEWPORT(); return(1); } XQueryWindow(DEVDEP(graph).window, &wininfo); graph->absolute.xpos = wininfo.x; graph->absolute.ypos = wininfo.y; graph->absolute.width = wininfo.width; graph->absolute.height = wininfo.height;/* note: set up subwindow and event dispatching */ /* set up fonts */ if (!cp_getvar("font", VT_STRING, fontname)) { (void) strcpy(fontname, DEF_FONT); } if (!(DEVDEP(graph).fontinfo = XOpenFont(fontname))) { sprintf(ErrorMessage, "can't open font %s", fontname); internalerror(ErrorMessage); RECOVERNEWVIEWPORT(); return(1); } graph->fontwidth = DEVDEP(graph).fontinfo->width; graph->fontheight = DEVDEP(graph).fontinfo->height; initlinestyles(); instantcolors(graph); /* set up cursor */ cursor = XCreateCursor(left_ptr_width, left_ptr_height, left_ptr_bits, left_ptr_mask_bits, left_ptr_x_hot, left_ptr_y_hot, BlackPixel, WhitePixel, GXcopy); XDefineCursor(DEVDEP(graph).window, cursor); XMapWindow(DEVDEP(graph).window);#define INPUTMASK (ButtonPressed | ExposeWindow | ExposeRegion | KeyPressed) XSelectInput(DEVDEP(graph).window, INPUTMASK);#ifdef HAS_XT XtSetEventHandler(DEVDEP(currentgraph).window, eventhandler, ButtonPressed | ExposeWindow | ExposeRegion | KeyPressed, (char *) DEVDEP(currentgraph).window);#endif /* have to be able to get from window id to graphid if your windowing system doesn't allow for this facility, you might want to try the following hack graph->graphid = DEVDEP(graph).window; */ XMakeAssoc(xtable, DEVDEP(graph).window, (char *) graph); return (0);}staticinitlinestyles(){ xlinestyles[0] = XMakePattern(1, 1, 2); /* Solid. */ xlinestyles[1] = XMakePattern(1, 4, 2); /* Spaced dots. */ if (DisplayPlanes() == 1) { xlinestyles[2] = XMakePattern(1, 2, 2); /* Dots. */ xlinestyles[3] = XMakePattern(13, 5, 2);/* Dot shortdash. */ xlinestyles[4] = XMakePattern(29, 6, 2);/* Dot longdash. */ xlinestyles[5] = XMakePattern(3, 3, 2); /* Shortdash. */ xlinestyles[6] = XMakePattern(7, 5, 2); /* Longdash. */ xlinestyles[7] = XMakePattern(115, 9, 2);/* Short/long dashed.*/ } else { /* Dotted lines are a distraction when we have colors. */ xlinestyles[2] = xlinestyles[3] = xlinestyles[4] = xlinestyles[5] = xlinestyles[6] = xlinestyles[7] = xlinestyles[0]; } return;}static voidinitcolors(){ int numdispplanes = DisplayPlanes(), i; static char *colornames[] = { "#ffffff", /* white */ "#000000", "#ff0000", "#00ff00", "#0000ff", "#ffff00", "#ff00ff", "#00ffff", "#ff7777", "#77ff77", "#7777ff", "#ffff77", "#ff77ff", "#77ffff", "#ffaa33", "#ff33aa", "#aaff33", "#33ffaa", "#aa33ff", "#33aaff", "" } ; Color col; char buf[BSIZE_SP], colorstring[BSIZE_SP]; int xmaxcolors = NUMCOLORS; /* note: can we get rid of this? */ if (numdispplanes == 1) { /* black and white */ colors[0] = WhitePixel; colors[1] = BlackPixel; dispdev->numcolors = 2; return; } else if (numdispplanes < NXPLANES) { xmaxcolors = 1; while (numdispplanes-- > 1) xmaxcolors *= 2; } for (i = 0; i < xmaxcolors; i++) { (void) sprintf(buf, "color%d", i); if (!cp_getvar(buf, VT_STRING, colorstring)) (void) strcpy(colorstring, colornames[i]); if (!XParseColor(colorstring, &col)) { (void) sprintf(ErrorMessage, "can't parse color %s\n", colorstring); internalerror(ErrorMessage); colors[i] = i ? BlackPixel : WhitePixel; continue; } if (!XGetHardwareColor(&col)) { externalerror("can't get hardware color"); dispdev->numcolors = i; return; } colors[i] = col.pixel; } dispdev->numcolors = xmaxcolors; return;}/* instantiate colors, here, just copy global pixel array */static void instantcolors(graph)GRAPH *graph;{ int i; for (i=0; i < dispdev->numcolors; i++) { graph->colors[i] = colors[i]; }}/* This routine closes the X connection. It is not to be called for finishing a graph. */X_Close(){/* note: This is too high level to place here. Find better place for it. *//* maybe create a gr_close *//* FreeGraphs(); */ XDestroyAssocTable(xtable); XCloseDisplay(display);}X_DrawLine(x1, y1, x2, y2)int x1, y1, x2, y2;{ Vertex vertex[2]; /* dumb, huh? */ vertex[0].flags = VertexDrawLastPoint; vertex[1].flags = VertexDrawLastPoint; /* If the last draw line operation on this graph was of the same linestyle as the current operation and left off at the same point as this one starts, just continue the last draw line, else start a new one. */ if ((DEVDEP(currentgraph).lastlinestyle != currentgraph->linestyle) || ((x1 != DEVDEP(currentgraph).lastx) || (y1 != DEVDEP(currentgraph).lasty))) { vertex[0].x = x1; vertex[0].y = currentgraph->absolute.height - y1; vertex[1].x = x2; vertex[1].y = currentgraph->absolute.height - y2; XDrawDashed(DEVDEP(currentgraph).window, vertex, 2, 1, 1, currentgraph->colors[currentgraph->currentcolor], xlinestyles[currentgraph->linestyle], GXcopy, AllPlanes); } else {/* I know, we're not supposed to do this. But it works and it beats buffering points just because XDrawDashed doesn't work like it's supposed to. */ vertex[0].x = x2; vertex[0].y = currentgraph->absolute.height - y2; switch (XAppendVertex(vertex, 1)) { case -1: /* will not fit in buffer */ case 0: /* no Draw in progress */ vertex[0].x = x1; vertex[0].y = currentgraph->absolute.height - y1; vertex[1].x = x2; vertex[1].y = currentgraph->absolute.height - y2; XDrawDashed(DEVDEP(currentgraph).window, vertex, 2, 1, 1, currentgraph->colors[currentgraph->currentcolor], xlinestyles[currentgraph->linestyle], GXcopy, AllPlanes); break; case 1: /* O.K. */ break; default: /* This will never happen. */ externalerror("unrecognized return value form XAppendVertex"); break; } } /* validate cache */ DEVDEP(currentgraph).lastx = x2; DEVDEP(currentgraph).lasty = y2; DEVDEP(currentgraph).lastlinestyle = currentgraph->linestyle;}/*ARGSUSED*/X_Arc(x0, y0, radius, theta1, theta2)int x0, y0, radius;double theta1, theta2;{ Vertex vertices[2]; vertices[0].x = x0 + radius * cos((double) theta1); vertices[0].y = y0 + radius * sin((double) theta1); vertices[1].x = x0 + radius * cos((double) theta2); vertices[1].y = y0 + radius * sin((double) theta2); vertices[0].flags = VertexCurved; vertices[1].flags = VertexCurved | VertexDrawLastPoint; XDrawDashed(DEVDEP(currentgraph).window, vertices, 2, 1, 1, currentgraph->colors[currentgraph->currentcolor], xlinestyles[currentgraph->linestyle], GXcopy, AllPlanes);}/* note: x and y are the LOWER left corner of text */X_Text(text, x, y)char *text;int x, y;{/* X has x and y set to UPPER left corner, so have to add fontheight */ XText(DEVDEP(currentgraph).window, x, currentgraph->absolute.height - (y + currentgraph->fontheight), text, strlen(text), DEVDEP(currentgraph).fontinfo->id, currentgraph->colors[currentgraph->currentcolor], currentgraph->colors[0]); /* note: unlike before, we do not save any text here */}/*ARGSUSED*/X_DefineColor(colorid, red, green, blue)int colorid;double red, green, blue;{ internalerror("X_DefineColor not implemented."); return(0);}/*ARGSUSED*/X_DefineLinestyle(linestyleid, mask)int linestyleid;int mask;{ internalerror("X_DefineLinestyle not implemented.");}X_SetLinestyle(linestyleid)int linestyleid;{ currentgraph->linestyle = linestyleid;}X_SetColor(colorid)int colorid;{ currentgraph->currentcolor = colorid;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -