📄 gcanvas.c
字号:
case G_ATTREVENTCB: WCU->func = (Gcanvascb) attrp[ai].u.func; break; case G_ATTRUSERDATA: widget->udata = attrp[ai].u.u; break; default: Gerr (POS, G_ERRBADATTRID, attrp[ai].id); return -1; } } XtSetValues (widget->w, argp, argn); return 0;}int GCgetwidgetattr (Gwidget_t *widget, int attrn, Gwattr_t *attrp) { XColor *cp; Dimension width, height; int ai, color; for (ai = 0; ai < attrn; ai++) { RESETARGS; switch (attrp[ai].id) { case G_ATTRSIZE: ADD2ARGS (XtNwidth, &width); ADD2ARGS (XtNheight, &height); XtGetValues (widget->w, argp, argn); attrp[ai].u.s.x = width, attrp[ai].u.s.y = height; break; case G_ATTRBORDERWIDTH: ADD2ARGS (XtNborderWidth, &width); XtGetValues (widget->w, argp, argn); attrp[ai].u.i = width; break; case G_ATTRCURSOR: attrp[ai].u.t = ( curcursori == -1 ) ? "default" : cursormap[curcursori].name; break; case G_ATTRCOLOR: color = attrp[ai].u.c.index; if (color < 0 || color > G_MAXCOLORS) { Gerr (POS, G_ERRBADCOLORINDEX, color); return -1; } if (WCU->colors[color].inuse) { cp = &WCU->colors[color].color; attrp[ai].u.c.r = cp->red / 257.0; attrp[ai].u.c.g = cp->green / 257.0; attrp[ai].u.c.b = cp->blue / 257.0; } else { attrp[ai].u.c.r = -1; attrp[ai].u.c.g = -1; attrp[ai].u.c.b = -1; } break; case G_ATTRVIEWPORT: attrp[ai].u.s = WCU->vsize; break; case G_ATTRWINDOW: attrp[ai].u.r = WCU->wrect; break; case G_ATTRWINDOWID: sprintf (&Gbufp[0], "0x%lx", XtWindow (widget->w)); attrp[ai].u.t = &Gbufp[0]; break; case G_ATTREVENTCB: attrp[ai].u.func = WCU->func; break; case G_ATTRUSERDATA: attrp[ai].u.u = widget->udata; break; default: Gerr (POS, G_ERRBADATTRID, attrp[ai].id); return -1; } } return 0;}int GCdestroywidget (Gwidget_t *widget) { XtDestroyWidget (widget->w); return 0;}int GCcanvasclear (Gwidget_t *widget) { XEvent ev; int gotit; XClearWindow (Gdisplay, WINDOW); /* avoid a redraw */ WCU->needredraw = FALSE; XSync (Gdisplay, False); gotit = FALSE; while (XCheckIfEvent ( Gdisplay, &ev, cwepredicate, (XPointer) WINDOW ) == True) gotit = TRUE; if (gotit) adjustclip (widget); return 0;}int GCsetgfxattr (Gwidget_t *widget, Ggattr_t *ap) { setgattr (widget, ap); WCU->defgattr = WCU->gattr; return 0;}int GCgetgfxattr (Gwidget_t *widget, Ggattr_t *ap) { if ((ap->flags & G_GATTRCOLOR)) ap->color = WCU->gattr.color; if ((ap->flags & G_GATTRWIDTH)) ap->width = WCU->gattr.width; if ((ap->flags & G_GATTRMODE)) ap->mode = WCU->gattr.mode; if ((ap->flags & G_GATTRFILL)) ap->fill = WCU->gattr.fill; if ((ap->flags & G_GATTRSTYLE)) ap->style = WCU->gattr.style; return 0;}int GCarrow (Gwidget_t *widget, Gpoint_t gp1, Gpoint_t gp2, Ggattr_t *ap) { PIXpoint_t pp1, pp2, pa, pb, pd; Grect_t gr; double tangent, l; if (gp1.x < gp2.x) gr.o.x = gp1.x, gr.c.x = gp2.x; else gr.o.x = gp2.x, gr.c.x = gp1.x; if (gp1.y < gp2.y) gr.o.y = gp1.y, gr.c.y = gp2.y; else gr.o.y = gp2.y, gr.c.y = gp1.y; if (!ISVISIBLE (gr)) return 1; pp1 = pdrawtopix (widget, gp1), pp2 = pdrawtopix (widget, gp2); pd.x = pp1.x - pp2.x, pd.y = pp1.y - pp2.y; if (pd.x == 0 && pd.y == 0) return 0; tangent = atan2 ((double) pd.y, (double) pd.x); if ((l = sqrt ((double) (pd.x * pd.x + pd.y * pd.y))) > 30) l = 30; pa.x = l * cos (tangent + M_PI / 7) + pp2.x; pa.y = l * sin (tangent + M_PI / 7) + pp2.y; pb.x = l * cos (tangent - M_PI / 7) + pp2.x; pb.y = l * sin (tangent - M_PI / 7) + pp2.y; setgattr (widget, ap); XDrawLine (Gdisplay, WINDOW, GC, pp1.x, pp1.y, pp2.x, pp2.y); XDrawLine (Gdisplay, WINDOW, GC, pa.x, pa.y, pp2.x, pp2.y); XDrawLine (Gdisplay, WINDOW, GC, pb.x, pb.y, pp2.x, pp2.y); return 0;}int GCline (Gwidget_t *widget, Gpoint_t gp1, Gpoint_t gp2, Ggattr_t *ap) { PIXpoint_t pp1, pp2; Grect_t gr; if (gp1.x < gp2.x) gr.o.x = gp1.x, gr.c.x = gp2.x; else gr.o.x = gp2.x, gr.c.x = gp1.x; if (gp1.y < gp2.y) gr.o.y = gp1.y, gr.c.y = gp2.y; else gr.o.y = gp2.y, gr.c.y = gp1.y; if (!ISVISIBLE (gr)) return 1; pp1 = pdrawtopix (widget, gp1), pp2 = pdrawtopix (widget, gp2); setgattr (widget, ap); XDrawLine (Gdisplay, WINDOW, GC, pp1.x, pp1.y, pp2.x, pp2.y); return 0;}int GCbox (Gwidget_t *widget, Grect_t gr, Ggattr_t *ap) { PIXrect_t pr; Gxy_t p; if (gr.o.x > gr.c.x) p.x = gr.o.x, gr.o.x = gr.c.x, gr.c.x = p.x; if (gr.o.y > gr.c.y) p.y = gr.o.y, gr.o.y = gr.c.y, gr.c.y = p.y; if (!ISVISIBLE (gr)) return 1; pr = rdrawtopix (widget, gr); setgattr (widget, ap); if (WCU->gattr.fill) XFillRectangle ( Gdisplay, WINDOW, GC, pr.o.x, pr.o.y, pr.c.x - pr.o.x, pr.c.y - pr.o.y ); else XDrawRectangle ( Gdisplay, WINDOW, GC, pr.o.x, pr.o.y, pr.c.x - pr.o.x, pr.c.y - pr.o.y ); return 0;}int GCpolygon (Gwidget_t *widget, int gpn, Gpoint_t *gpp, Ggattr_t *ap) { Grect_t gr; int n, i; if (gpn == 0) return 0; gr.o = gpp[0], gr.c = gpp[0]; for (i = 1; i < gpn; i++) { gr.o.x = min (gr.o.x, gpp[i].x); gr.o.y = min (gr.o.y, gpp[i].y); gr.c.x = max (gr.c.x, gpp[i].x); gr.c.y = max (gr.c.y, gpp[i].y); } if (!ISVISIBLE (gr)) return 1; if (gpn + 1 > Gppn) { n = (((gpn + 1) + PPINCR - 1) / PPINCR) * PPINCR; Gppp = Marraygrow (Gppp, (long) n * PPSIZE); Gppn = n; } for (i = 0; i < gpn; i++) Gppp[i] = pdrawtopix (widget, gpp[i]); setgattr (widget, ap); if (WCU->gattr.fill) { if (Gppp[gpn - 1].x != Gppp[0].x || Gppp[gpn - 1].y != Gppp[0].y) Gppp[gpn] = Gppp[0], gpn++; XFillPolygon ( Gdisplay, WINDOW, GC, Gppp, gpn, Complex, CoordModeOrigin ); } else XDrawLines (Gdisplay, WINDOW, GC, Gppp, gpn, CoordModeOrigin); return 0;}int GCsplinegon (Gwidget_t *widget, int gpn, Gpoint_t *gpp, Ggattr_t *ap) { PIXpoint_t p0, p1, p2, p3; Grect_t gr; int n, i; if (gpn == 0) return 0; gr.o = gpp[0], gr.c = gpp[0]; for (i = 1; i < gpn; i++) { gr.o.x = min (gr.o.x, gpp[i].x); gr.o.y = min (gr.o.y, gpp[i].y); gr.c.x = max (gr.c.x, gpp[i].x); gr.c.y = max (gr.c.y, gpp[i].y); } if (!ISVISIBLE (gr)) return 1; Gppi = 1; if (Gppi >= Gppn) { n = (((Gppi + 1) + PPINCR - 1) / PPINCR) * PPINCR; Gppp = Marraygrow (Gppp, (long) n * PPSIZE); Gppn = n; } Gppp[0] = p3 = pdrawtopix (widget, gpp[0]); for (i = 1; i < gpn; i += 3) { p0 = p3; p1 = pdrawtopix (widget, gpp[i]); p2 = pdrawtopix (widget, gpp[i + 1]); p3 = pdrawtopix (widget, gpp[i + 2]); bezier (p0, p1, p2, p3); } setgattr (widget, ap); if (WCU->gattr.fill) { if (Gppp[Gppi - 1].x != Gppp[0].x || Gppp[Gppi - 1].y != Gppp[0].y) Gppp[Gppi] = Gppp[0], Gppi++; XFillPolygon ( Gdisplay, WINDOW, GC, Gppp, Gppi, Complex, CoordModeOrigin ); } else XDrawLines (Gdisplay, WINDOW, GC, Gppp, Gppi, CoordModeOrigin); return 0;}static void bezier ( PIXpoint_t p0, PIXpoint_t p1, PIXpoint_t p2, PIXpoint_t p3) { Gpoint_t gp0, gp1, gp2; Gsize_t s; PIXpoint_t p; double t; int n, i, steps; if ((s.x = p3.x - p0.x) < 0) s.x = - s.x; if ((s.y = p3.y - p0.y) < 0) s.y = - s.y; if (s.x > s.y) steps = s.x / 5 + 1; else steps = s.y / 5 + 1; for (i = 0; i <= steps; i++) { t = i / (double) steps; gp0.x = p0.x + t * (p1.x - p0.x); gp0.y = p0.y + t * (p1.y - p0.y); gp1.x = p1.x + t * (p2.x - p1.x); gp1.y = p1.y + t * (p2.y - p1.y); gp2.x = p2.x + t * (p3.x - p2.x); gp2.y = p2.y + t * (p3.y - p2.y); gp0.x = gp0.x + t * (gp1.x - gp0.x); gp0.y = gp0.y + t * (gp1.y - gp0.y); gp1.x = gp1.x + t * (gp2.x - gp1.x); gp1.y = gp1.y + t * (gp2.y - gp1.y); p.x = gp0.x + t * (gp1.x - gp0.x) + 0.5; p.y = gp0.y + t * (gp1.y - gp0.y) + 0.5; if (Gppi >= Gppn) { n = (((Gppi + 1) + PPINCR - 1) / PPINCR) * PPINCR; Gppp = Marraygrow (Gppp, (long) n * PPSIZE); Gppn = n; } Gppp[Gppi++] = p; }}int GCarc ( Gwidget_t *widget, Gpoint_t gc, Gsize_t gs, double ang1, double ang2, Ggattr_t *ap) { PIXpoint_t pc; PIXsize_t ps; Grect_t gr; gr.o.x = gc.x - gs.x, gr.o.y = gc.y - gs.y; gr.c.x = gc.x + gs.x, gr.c.y = gc.y + gs.y; if (!ISVISIBLE (gr)) return 1; pc = pdrawtopix (widget, gc), ps = sdrawtopix (widget, gs); setgattr (widget, ap); if (WCU->gattr.fill) XFillArc ( Gdisplay, WINDOW, GC, pc.x - ps.x, pc.y - ps.y, ps.x * 2, ps.y * 2, (int) (ang1 * 64), (int) (ang2 * 64) ); else XDrawArc ( Gdisplay, WINDOW, GC, pc.x - ps.x, pc.y - ps.y, ps.x * 2, ps.y * 2, (int) (ang1 * 64), (int) (ang2 * 64) ); return 0;}int GCtext ( Gwidget_t *widget, Gtextline_t *tlp, int n, Gpoint_t go, char *fn, double fs, char *justs, Ggattr_t *ap) { Gsize_t gs; PIXpoint_t po; PIXsize_t ps; PIXrect_t pr; Grect_t gr; XFontStruct *font; int dir, asc, des, x, y, w, h, i; XCharStruct txtinfo; x = 0; po = pdrawtopix (widget, go); gs.x = 0, gs.y = fs; ps = sdrawtopix (widget, gs); if (!(font = findfont (fn, ps.y))) { XDrawRectangle (Gdisplay, WINDOW, GC, po.x, po.y, 1, 1); return 0; } setgattr (widget, ap); SETFONT (font); for (w = h = 0, i = 0; i < n; i++) { if (IS8BIT (font)) XTextExtents (font, tlp[i].p, tlp[i].n, &dir, &asc, &des, &txtinfo); else XTextExtents16 ( font, (XChar2b *) tlp[i].p, tlp[i].n / 2, &dir, &asc, &des, &txtinfo ); tlp[i].w = txtinfo.width, tlp[i].h = asc + des; w = max (w, txtinfo.width), h += asc + des; } switch (justs[0]) { case 'l': po.x += w / 2; break; case 'r': po.x -= w / 2; break; } switch (justs[1]) { case 'd': po.y -= h; break; case 'b': po.y -= (h - des); break; case 'c': po.y -= h / 2; break; } pr.o.x = po.x - w / 2, pr.o.y = po.y; pr.c.x = po.x + w / 2, pr.c.y = po.y + h; gr = rpixtodraw (widget, pr); if (!ISVISIBLE (gr)) return 1; for (i = 0; i < n; i++) { switch (tlp[i].j) { case 'l': x = po.x - w / 2; break; case 'n': x = po.x - tlp[i].w / 2; break; case 'r': x = po.x - (tlp[i].w - w / 2); break; } y = po.y + (i + 1) * tlp[i].h - des; if (IS8BIT (font)) XDrawString (Gdisplay, WINDOW, GC, x, y, tlp[i].p, tlp[i].n); else XDrawString16 ( Gdisplay, WINDOW, GC, x, y, (XChar2b *) tlp[i].p, tlp[i].n / 2 ); } return 0;}int GCgettextsize ( Gwidget_t *widget, Gtextline_t *tlp, int n, char *fn, double fs, Gsize_t *gsp) { Gsize_t gs; PIXsize_t ps; XFontStruct *font; int i, dir, asc, des; XCharStruct txtinfo; gs.x = 0, gs.y = fs; ps = sdrawtopix (widget, gs); if (!(font = findfont (fn, ps.y))) { gsp->x = 1, gsp->y = 1; return 0; } SETFONT (font); for (ps.x = ps.y = 0, i = 0; i < n; i++) { if (IS8BIT (font)) XTextExtents (font, tlp[i].p, tlp[i].n, &dir, &asc, &des, &txtinfo); else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -