1 module ws.x.drawSimple;
2 
3 version(Posix):
4 
5 
6 import
7     std.string,
8     std.algorithm,
9     std.math,
10     std.conv,
11     std.range,
12     x11.X,
13     x11.Xlib,
14     x11.extensions.render,
15     x11.extensions.Xrender,
16     x11.extensions.Xfixes,
17     ws.draw,
18     ws.wm,
19     ws.bindings.xft,
20     ws.gui.point,
21     ws.x.backbuffer,
22     ws.x.font;
23 
24 
25 class Color {
26 
27     ulong pix;
28     XftColor rgb;
29     long[4] rgba;
30 
31     this(Display* dpy, int screen, long[4] values){
32         Colormap cmap = DefaultColormap(dpy, screen);
33         Visual* vis = DefaultVisual(dpy, screen);
34         rgba = values;
35         auto name = "#%02x%02x%02x".format(values[0], values[1], values[2]);
36         if(!XftColorAllocName(dpy, vis, cmap, name.toStringz, &rgb))
37             throw new Exception("Cannot allocate color " ~ name);
38         pix = rgb.pixel;
39     }
40 
41 }
42 
43 
44 class XDrawSimple: DrawEmpty {
45 
46     int[2] size;
47     Display* dpy;
48     int screen;
49     x11.X.Window window;
50     GC gc;
51 
52     Color color;
53     Color[long[4]] colors;
54 
55     this(ws.wm.Window window){
56         this(wm.displayHandle, window.windowHandle);
57     }
58 
59     this(Display* dpy, x11.X.Window window){
60         XWindowAttributes wa;
61         XGetWindowAttributes(dpy, window, &wa);
62         this.dpy = dpy;
63         screen = DefaultScreen(dpy);
64         this.window = window;
65         this.size = [wa.width, wa.height];
66         XGCValues gcValues;
67         gcValues.subwindow_mode = IncludeInferiors;
68         gc = XCreateGC(dpy, window, GCSubwindowMode, &gcValues);
69 	    XSetBackground(dpy,gc,0);
70         XSetLineAttributes(dpy, gc, 1, LineSolid, CapButt, JoinMiter);
71         XSetFillStyle(dpy, gc, FillSolid);
72     }
73 
74     override void resize(int[2] size){
75         this.size = size;
76     }
77 
78     override void destroy(){
79         XFreeGC(dpy, gc);
80     }
81 
82     override void setColor(float[3] color){
83         setColor([color[0], color[1], color[2], 1]);
84     }
85 
86     override void setColor(float[4] color){
87         long[4] values = [
88             (color[0]*255).lround.max(0).min(255),
89             (color[1]*255).lround.max(0).min(255),
90             (color[2]*255).lround.max(0).min(255),
91             (color[3]*255).lround.max(0).min(255)
92         ];
93         if(values !in colors)
94             colors[values] = new Color(dpy, screen, values);
95         this.color = colors[values];
96     }
97 
98     override void rect(int[2] pos, int[2] size){
99         XSetForeground(dpy, gc, color.pix);
100         XFillRectangle(dpy, window, gc, pos.x, pos.y, size.w, size.h);
101     }
102 
103     override void line(int[2] start, int[2] end){
104         XSetForeground(dpy, gc, color.pix);
105         XDrawLine(dpy, window, gc, start.x, start.y, end.x, end.y);
106     }
107 
108     override void clear(){
109         XClearWindow(dpy, window);
110     }
111 
112 }