#include "graph_x11.h" extern "C" { #include } extern void checkfree (void *ptr); extern char *checkstrdup (char *ptr); extern char *checkmalloc (unsigned int len); extern void *checkrealloc (void *ptr_in, int size); X11Window::X11Window (Display *display, Window window, int screen_num) : GWindow () { this->display= display; this->window= window; this->screen_num= screen_num; color= NULL; ncolors= 0; context= NULL; ncontexts= 0; dash_list_length[0] = 2; dash_list_length[1] = 2; dash_list_length[2] = 4; dash_list_length[3] = 6; dash[0] = 5; dash[1] = 3; dot[0] = 1; dot[1] = 3; dash_dot[0] = 5; dash_dot[1] = 3; dash_dot[2] = 1; dash_dot[3] = 3; dash_dot_dot[0] = 5; dash_dot_dot[1] = 3; dash_dot_dot[2] = 1; dash_dot_dot[3] = 3; dash_dot_dot[4] = 1; dash_dot_dot[5] = 3; dash_list[0] = dash; dash_list[1] = dot; dash_list[2] = dash_dot; dash_list[3] = dash_dot_dot; } X11Window::~X11Window (void) { checkfree (color); checkfree (context); } GWindow::GGC X11Window::GCreateGC (void) { GC gc_default; context= (ggc *) checkrealloc (context, (ncontexts+ 1)* sizeof (ggc)); /* Create context */ gc_default= DefaultGC (display, screen_num); context [ncontexts] = XCreateGC (display, RootWindow(display, screen_num), 0, NULL); XCopyGC (display, gc_default, ~0, context [ncontexts]); return (ncontexts++); } void X11Window::GClearWindow (void) { XClearWindow (display, window); } void X11Window::GDrawImageString (GWindow::GGC gc, int x, int y, char *string) { XDrawImageString (display, window, context [gc], x, y, string, strlen (string)); } void X11Window::GDrawString (GWindow::GGC gc, int x, int y, char *string) { XDrawString (display, window, context [gc], x, y, string, strlen (string)); } void X11Window::GDrawLines (GWindow::GGC gc, GPoint *a, int len) { XDrawLines (display, window, context [gc], (XPoint *) a, len, CoordModeOrigin); } void X11Window::GFillPolygon (GWindow::GGC gc, GPoint *a, int len) { XFillPolygon (display, window, context [gc], (XPoint *) a, len, Complex, CoordModeOrigin); } void X11Window::GFillArc (GWindow::GGC gc, int x, int y, int w, int h, int start, int end, int close) { int arc_mode; if ( close == 0 ) arc_mode = ArcPieSlice; else arc_mode = ArcChord; XSetArcMode (display, context [gc], arc_mode); XFillArc (display, window, context [gc], x, y, w, h, start, end); } void X11Window::GDrawArc (GWindow::GGC gc, int x, int y, int w, int h, int start, int end) { XDrawArc (display, window, context [gc], x, y, w, h, start, end); } /* color: -1= Foreground, -2= Background, rest: index in colortable */ void X11Window::GSetForeground (GWindow::GGC gc, int color) { switch (color) { case -1: XSetForeground (display, context [gc], BlackPixel(display, screen_num)); break; case -2: XSetForeground (display, context [gc], WhitePixel(display, screen_num)); break; default: XSetForeground (display, context [gc], this->color [color]); break; } } void X11Window::GSetColorTable (GWindow::GColor *colors_in, int ncolors, int background) { XColor xcolor; Colormap cmap; int i; cmap= DefaultColormap (display, screen_num); if (color!= NULL) { XFreeColors (display, cmap, color, ncolors, 0); checkfree (color); } color= (unsigned long *) checkmalloc (ncolors* sizeof (unsigned long)); xcolor.pad= 0; xcolor.flags= DoRed | DoGreen | DoBlue; for (i= 0; i< ncolors; ++i) { xcolor.pixel= 0; xcolor.red= colors_in [i].red<< 8; xcolor.green= colors_in [i].green<< 8; xcolor.blue= colors_in [i].blue<< 8; if (XAllocColor (display, cmap, &xcolor)== 0) { printf ("Unable to allocate color\n"); color [i]= 0; /* default */ } else color [i]= xcolor.pixel; } if (background>= 0) XSetWindowBackground (display, window, color [background]); else XSetWindowBackground (display, window, WhitePixel(display, screen_num)); } void X11Window::GSetLineType (GWindow::GGC gc, int type ) { XGCValues values; long valuemask = (GCLineWidth | GCCapStyle | GCJoinStyle ); int line_style; if ( type == 1 ) line_style = LineSolid; else { line_style = LineOnOffDash; XSetDashes (display, context[gc], 0, (const char*)dash_list[type-2], dash_list_length[type-2] ); } XGetGCValues (display, context[gc], valuemask, &values); XSetLineAttributes(display, context[gc], values.line_width, line_style, values.cap_style, values.join_style); } void X11Window::GSetLineWidth (GWindow::GGC gc, int width ) { XGCValues values; long valuemask = (GCLineStyle | GCCapStyle | GCJoinStyle ); XGetGCValues (display, context[gc], valuemask, &values); XSetLineAttributes(display, context[gc], width, values.line_style, values.cap_style, values.join_style); }