chiark / gitweb /
c8eb915cac381c706762b10167080273670c997c
[moebius.git] / x11.cc
1 /*
2  * X11 functions
3  */
4
5
6 #include "x11.hh"
7 #include "parameter.hh"
8
9 static Parameter<int> x11size("x11size", "X11 window size", 500, 100, 10, 10000);
10
11 GC X11Output::gc(const char *colour_name) {
12   XGCValues gcvalues;
13   XColor colour;
14   Status st;
15
16   st= XAllocNamedColor(display,cmap,colour_name,&colour,&colour);
17   if (!st) {
18     cerr << "cannot allocate colour " << colour_name << ", quitting\n";
19     exit(1);
20   }
21
22   gcvalues.foreground= gcvalues.background= colour.pixel;
23   return XCreateGC(display,window,GCForeground|GCBackground,&gcvalues);
24 }
25
26 X11Output::X11Output() {
27   display= XOpenDisplay(0);
28   window= XCreateSimpleWindow(display,
29                               DefaultRootWindow(display),
30                               0,0, x11size,x11size, 0,0,0);
31   cmap= DefaultColormap(display,DefaultScreen(display));
32
33   black= gc("black");
34   white= gc("white");
35   blue= gc("blue");
36   red= gc("red");
37
38   XSelectInput(display,window,0);
39   XMapWindow(display,window);
40   XFlush(display);
41 }
42
43 void X11Output::startimage() {
44   XClearWindow(display,window);
45 }
46
47 void X11Output::endimage() {
48   XFlush(display);
49 }
50
51 X11Output::~X11Output() {
52   XCloseDisplay(display);
53 }
54
55 void X11Output::drawcell(const Point* list, int n, Colour colour) {
56   GC fill, draw;
57   
58   XPoint xp[n+1];
59   for (int i=0; i<n; i++) {
60     Onscreen here= Onscreen(list[i]);
61     xp[i].x= (int)((here.x+1.0)*(x11size*0.5));
62     xp[i].y= (int)((-here.y+1.0)*(x11size*0.5));
63   }
64   switch (colour) {
65   case grid:       fill= black;  draw= white;  break;
66   case solidblack: fill= red;    draw= 0;      break;
67   case solidwhite: fill= blue;   draw= 0;      break;
68   default: abort();
69   }
70   XFillPolygon(display,window,fill,xp,n,Nonconvex,CoordModeOrigin);
71   if (draw) {
72     xp[n]= xp[0];
73     XDrawLines(display,window,draw,xp,n+1,CoordModeOrigin);
74   }
75 }