1 /*
2  * Nuklear - v1.40.8 - public domain
3  * no warrenty implied; use at your own risk.
4  * authored from 2015-2017 by Micha Mettke
5  */
6 /*
7  * ==============================================================
8  *
9  *                              API
10  *
11  * ===============================================================
12  */
13 #ifndef NK_XLIB_H_
14 #define NK_XLIB_H_
15 
16 #include <X11/Xlib.h>
17 
18 typedef struct XFont XFont;
19 #ifdef NK_XLIB_USE_XFT
20 NK_API struct nk_context*   nk_xlib_init(XFont*, Display*, int scrn, Window root, Visual *vis, Colormap cmap, unsigned w, unsigned h);
21 #else
22 NK_API struct nk_context*   nk_xlib_init(XFont*, Display*, int scrn, Window root, unsigned w, unsigned h);
23 #endif
24 NK_API int                  nk_xlib_handle_event(Display*, int scrn, Window, XEvent*);
25 NK_API void                 nk_xlib_render(Drawable screen, struct nk_color clear);
26 NK_API void                 nk_xlib_shutdown(void);
27 NK_API void                 nk_xlib_set_font(XFont*);
28 NK_API void                 nk_xlib_push_font(XFont*);
29 NK_API void                 nk_xlib_paste(nk_handle, struct nk_text_edit*);
30 NK_API void                 nk_xlib_copy(nk_handle, const char*, int len);
31 
32 /* Image */
33 #ifdef NK_XLIB_INCLUDE_STB_IMAGE
34 NK_API struct nk_image nk_xsurf_load_image_from_file(char const *filename);
35 NK_API struct nk_image nk_xsurf_load_image_from_memory(const void *membuf, nk_uint membufSize);
36 #endif
37 
38 /* Font */
39 NK_API XFont*               nk_xfont_create(Display *dpy, const char *name);
40 NK_API void                 nk_xfont_del(Display *dpy, XFont *font);
41 
42 #endif
43 /*
44  * ==============================================================
45  *
46  *                          IMPLEMENTATION
47  *
48  * ===============================================================
49  */
50 #ifdef NK_XLIB_IMPLEMENTATION
51 #include <X11/Xlib.h>
52 #include <X11/Xutil.h>
53 #include <X11/Xresource.h>
54 #include <X11/Xlocale.h>
55 #include <X11/Xatom.h>
56 
57 #ifdef NK_XLIB_USE_XFT
58 #include <X11/Xft/Xft.h>
59 #endif
60 
61 #include <sys/time.h>
62 #include <unistd.h>
63 #include <time.h>
64 
65 
66 #ifdef NK_XLIB_IMPLEMENT_STB_IMAGE
67 #define STB_IMAGE_IMPLEMENTATION
68 #endif
69 
70 #ifdef NK_XLIB_INCLUDE_STB_IMAGE
71 #include "../../example/stb_image.h"
72 #endif
73 
74 
75 #ifndef NK_X11_DOUBLE_CLICK_LO
76 #define NK_X11_DOUBLE_CLICK_LO 20
77 #endif
78 #ifndef NK_X11_DOUBLE_CLICK_HI
79 #define NK_X11_DOUBLE_CLICK_HI 200
80 #endif
81 
82 typedef struct XSurface XSurface;
83 typedef struct XImageWithAlpha XImageWithAlpha;
84 struct XFont {
85     int ascent;
86     int descent;
87     int height;
88 #ifdef NK_XLIB_USE_XFT
89     XftFont * ft;
90 #else
91     XFontSet set;
92     XFontStruct *xfont;
93 #endif
94     struct nk_user_font handle;
95 };
96 struct XSurface {
97     GC gc;
98     Display *dpy;
99     int screen;
100     Window root;
101     Drawable drawable;
102     unsigned int w, h;
103 #ifdef NK_XLIB_USE_XFT
104     XftDraw * ftdraw;
105 #endif
106 };
107 struct XImageWithAlpha {
108     XImage* ximage;
109     GC clipMaskGC;
110     Pixmap clipMask;
111 };
112 static struct  {
113     char *clipboard_data;
114     int clipboard_len;
115     struct nk_text_edit* clipboard_target;
116 
117     Atom xa_clipboard;
118     Atom xa_targets;
119     Atom xa_text;
120     Atom xa_utf8_string;
121 
122     struct nk_context ctx;
123     struct XSurface *surf;
124     Cursor cursor;
125     Display *dpy;
126     Window root;
127 #ifdef NK_XLIB_USE_XFT
128     Visual *vis;
129     Colormap cmap;
130 #endif
131     long last_button_click;
132 } xlib;
133 
134 NK_INTERN long
nk_timestamp(void)135 nk_timestamp(void)
136 {
137     struct timeval tv;
138     if (gettimeofday(&tv, NULL) < 0) return 0;
139     return (long)((long)tv.tv_sec * 1000 + (long)tv.tv_usec/1000);
140 }
141 
142 NK_INTERN unsigned long
nk_color_from_byte(const nk_byte * c)143 nk_color_from_byte(const nk_byte *c)
144 {
145     unsigned long res = 0;
146     res |= (unsigned long)c[0] << 16;
147     res |= (unsigned long)c[1] << 8;
148     res |= (unsigned long)c[2] << 0;
149     return (res);
150 }
151 
152 NK_INTERN XSurface*
nk_xsurf_create(int screen,unsigned int w,unsigned int h)153 nk_xsurf_create(int screen, unsigned int w, unsigned int h)
154 {
155     XSurface *surface = (XSurface*)calloc(1, sizeof(XSurface));
156     surface->w = w;
157     surface->h = h;
158     surface->dpy = xlib.dpy;
159     surface->screen = screen;
160     surface->root = xlib.root;
161     surface->gc = XCreateGC(xlib.dpy, xlib.root, 0, NULL);
162     XSetLineAttributes(xlib.dpy, surface->gc, 1, LineSolid, CapButt, JoinMiter);
163     surface->drawable = XCreatePixmap(xlib.dpy, xlib.root, w, h,
164         (unsigned int)DefaultDepth(xlib.dpy, screen));
165 #ifdef NK_XLIB_USE_XFT
166     surface->ftdraw = XftDrawCreate(xlib.dpy, surface->drawable,
167                                 xlib.vis, xlib.cmap);
168 #endif
169     return surface;
170 }
171 
172 NK_INTERN void
nk_xsurf_resize(XSurface * surf,unsigned int w,unsigned int h)173 nk_xsurf_resize(XSurface *surf, unsigned int w, unsigned int h)
174 {
175     if(!surf) return;
176     if (surf->w == w && surf->h == h) return;
177     surf->w = w; surf->h = h;
178     if(surf->drawable) XFreePixmap(surf->dpy, surf->drawable);
179     surf->drawable = XCreatePixmap(surf->dpy, surf->root, w, h,
180         (unsigned int)DefaultDepth(surf->dpy, surf->screen));
181 #ifdef NK_XLIB_USE_XFT
182 	XftDrawChange(surf->ftdraw, surf->drawable);
183 #endif
184 	return;
185 }
186 
187 NK_INTERN void
nk_xsurf_scissor(XSurface * surf,float x,float y,float w,float h)188 nk_xsurf_scissor(XSurface *surf, float x, float y, float w, float h)
189 {
190     XRectangle clip_rect;
191     clip_rect.x = (short)(x-1);
192     clip_rect.y = (short)(y-1);
193     clip_rect.width = (unsigned short)(w+2);
194     clip_rect.height = (unsigned short)(h+2);
195     XSetClipRectangles(surf->dpy, surf->gc, 0, 0, &clip_rect, 1, Unsorted);
196 
197 #ifdef NK_XLIB_USE_XFT
198 	XftDrawSetClipRectangles(surf->ftdraw, 0, 0, &clip_rect, 1);
199 #endif
200 	return;
201 }
202 
203 NK_INTERN void
nk_xsurf_stroke_line(XSurface * surf,short x0,short y0,short x1,short y1,unsigned int line_thickness,struct nk_color col)204 nk_xsurf_stroke_line(XSurface *surf, short x0, short y0, short x1,
205     short y1, unsigned int line_thickness, struct nk_color col)
206 {
207     unsigned long c = nk_color_from_byte(&col.r);
208     XSetForeground(surf->dpy, surf->gc, c);
209     XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
210     XDrawLine(surf->dpy, surf->drawable, surf->gc, (int)x0, (int)y0, (int)x1, (int)y1);
211     XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
212 }
213 
214 NK_INTERN void
nk_xsurf_stroke_rect(XSurface * surf,short x,short y,unsigned short w,unsigned short h,unsigned short r,unsigned short line_thickness,struct nk_color col)215 nk_xsurf_stroke_rect(XSurface* surf, short x, short y, unsigned short w,
216     unsigned short h, unsigned short r, unsigned short line_thickness, struct nk_color col)
217 {
218     unsigned long c = nk_color_from_byte(&col.r);
219     XSetForeground(surf->dpy, surf->gc, c);
220     XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
221     if (r == 0) {XDrawRectangle(surf->dpy, surf->drawable, surf->gc, x, y, w, h);return;}
222 
223     {short xc = x + r;
224     short yc = y + r;
225     short wc = (short)(w - 2 * r);
226     short hc = (short)(h - 2 * r);
227 
228     XDrawLine(surf->dpy, surf->drawable, surf->gc, xc, y, xc+wc, y);
229     XDrawLine(surf->dpy, surf->drawable, surf->gc, x+w, yc, x+w, yc+hc);
230     XDrawLine(surf->dpy, surf->drawable, surf->gc, xc, y+h, xc+wc, y+h);
231     XDrawLine(surf->dpy, surf->drawable, surf->gc, x, yc, x, yc+hc);
232 
233     XDrawArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, y,
234         (unsigned)r*2, (unsigned)r*2, 0 * 64, 90 * 64);
235     XDrawArc(surf->dpy, surf->drawable, surf->gc, x, y,
236         (unsigned)r*2, (unsigned)r*2, 90 * 64, 90 * 64);
237     XDrawArc(surf->dpy, surf->drawable, surf->gc, x, yc + hc - r,
238         (unsigned)r*2, (unsigned)2*r, 180 * 64, 90 * 64);
239     XDrawArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, yc + hc - r,
240         (unsigned)r*2, (unsigned)2*r, -90 * 64, 90 * 64);}
241     XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
242 }
243 
244 NK_INTERN void
nk_xsurf_fill_rect(XSurface * surf,short x,short y,unsigned short w,unsigned short h,unsigned short r,struct nk_color col)245 nk_xsurf_fill_rect(XSurface* surf, short x, short y, unsigned short w,
246     unsigned short h, unsigned short r, struct nk_color col)
247 {
248     unsigned long c = nk_color_from_byte(&col.r);
249     XSetForeground(surf->dpy, surf->gc, c);
250     if (r == 0) {XFillRectangle(surf->dpy, surf->drawable, surf->gc, x, y, w, h); return;}
251 
252     {short xc = x + r;
253     short yc = y + r;
254     short wc = (short)(w - 2 * r);
255     short hc = (short)(h - 2 * r);
256 
257     XPoint pnts[12];
258     pnts[0].x = x;
259     pnts[0].y = yc;
260     pnts[1].x = xc;
261     pnts[1].y = yc;
262     pnts[2].x = xc;
263     pnts[2].y = y;
264 
265     pnts[3].x = xc + wc;
266     pnts[3].y = y;
267     pnts[4].x = xc + wc;
268     pnts[4].y = yc;
269     pnts[5].x = x + w;
270     pnts[5].y = yc;
271 
272     pnts[6].x = x + w;
273     pnts[6].y = yc + hc;
274     pnts[7].x = xc + wc;
275     pnts[7].y = yc + hc;
276     pnts[8].x = xc + wc;
277     pnts[8].y = y + h;
278 
279     pnts[9].x = xc;
280     pnts[9].y = y + h;
281     pnts[10].x = xc;
282     pnts[10].y = yc + hc;
283     pnts[11].x = x;
284     pnts[11].y = yc + hc;
285 
286     XFillPolygon(surf->dpy, surf->drawable, surf->gc, pnts, 12, Convex, CoordModeOrigin);
287     XFillArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, y,
288         (unsigned)r*2, (unsigned)r*2, 0 * 64, 90 * 64);
289     XFillArc(surf->dpy, surf->drawable, surf->gc, x, y,
290         (unsigned)r*2, (unsigned)r*2, 90 * 64, 90 * 64);
291     XFillArc(surf->dpy, surf->drawable, surf->gc, x, yc + hc - r,
292         (unsigned)r*2, (unsigned)2*r, 180 * 64, 90 * 64);
293     XFillArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, yc + hc - r,
294         (unsigned)r*2, (unsigned)2*r, -90 * 64, 90 * 64);}
295 }
296 
297 NK_INTERN void
nk_xsurf_fill_triangle(XSurface * surf,short x0,short y0,short x1,short y1,short x2,short y2,struct nk_color col)298 nk_xsurf_fill_triangle(XSurface *surf, short x0, short y0, short x1,
299     short y1, short x2, short y2, struct nk_color col)
300 {
301     XPoint pnts[3];
302     unsigned long c = nk_color_from_byte(&col.r);
303     pnts[0].x = (short)x0;
304     pnts[0].y = (short)y0;
305     pnts[1].x = (short)x1;
306     pnts[1].y = (short)y1;
307     pnts[2].x = (short)x2;
308     pnts[2].y = (short)y2;
309     XSetForeground(surf->dpy, surf->gc, c);
310     XFillPolygon(surf->dpy, surf->drawable, surf->gc, pnts, 3, Convex, CoordModeOrigin);
311 }
312 
313 NK_INTERN void
nk_xsurf_stroke_triangle(XSurface * surf,short x0,short y0,short x1,short y1,short x2,short y2,unsigned short line_thickness,struct nk_color col)314 nk_xsurf_stroke_triangle(XSurface *surf, short x0, short y0, short x1,
315     short y1, short x2, short y2, unsigned short line_thickness, struct nk_color col)
316 {
317     unsigned long c = nk_color_from_byte(&col.r);
318     XSetForeground(surf->dpy, surf->gc, c);
319     XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
320     XDrawLine(surf->dpy, surf->drawable, surf->gc, x0, y0, x1, y1);
321     XDrawLine(surf->dpy, surf->drawable, surf->gc, x1, y1, x2, y2);
322     XDrawLine(surf->dpy, surf->drawable, surf->gc, x2, y2, x0, y0);
323     XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
324 }
325 
326 NK_INTERN void
nk_xsurf_fill_polygon(XSurface * surf,const struct nk_vec2i * pnts,int count,struct nk_color col)327 nk_xsurf_fill_polygon(XSurface *surf,  const struct nk_vec2i *pnts, int count,
328     struct nk_color col)
329 {
330     int i = 0;
331     #define MAX_POINTS 128
332     XPoint xpnts[MAX_POINTS];
333     unsigned long c = nk_color_from_byte(&col.r);
334     XSetForeground(surf->dpy, surf->gc, c);
335     for (i = 0; i < count && i < MAX_POINTS; ++i) {
336         xpnts[i].x = pnts[i].x;
337         xpnts[i].y = pnts[i].y;
338     }
339     XFillPolygon(surf->dpy, surf->drawable, surf->gc, xpnts, count, Convex, CoordModeOrigin);
340     #undef MAX_POINTS
341 }
342 
343 NK_INTERN void
nk_xsurf_stroke_polygon(XSurface * surf,const struct nk_vec2i * pnts,int count,unsigned short line_thickness,struct nk_color col)344 nk_xsurf_stroke_polygon(XSurface *surf, const struct nk_vec2i *pnts, int count,
345     unsigned short line_thickness, struct nk_color col)
346 {
347     int i = 0;
348     unsigned long c = nk_color_from_byte(&col.r);
349     XSetForeground(surf->dpy, surf->gc, c);
350     XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
351     for (i = 1; i < count; ++i)
352         XDrawLine(surf->dpy, surf->drawable, surf->gc, pnts[i-1].x, pnts[i-1].y, pnts[i].x, pnts[i].y);
353     XDrawLine(surf->dpy, surf->drawable, surf->gc, pnts[count-1].x, pnts[count-1].y, pnts[0].x, pnts[0].y);
354     XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
355 }
356 
357 NK_INTERN void
nk_xsurf_stroke_polyline(XSurface * surf,const struct nk_vec2i * pnts,int count,unsigned short line_thickness,struct nk_color col)358 nk_xsurf_stroke_polyline(XSurface *surf, const struct nk_vec2i *pnts,
359     int count, unsigned short line_thickness, struct nk_color col)
360 {
361     int i = 0;
362     unsigned long c = nk_color_from_byte(&col.r);
363     XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
364     XSetForeground(surf->dpy, surf->gc, c);
365     for (i = 0; i < count-1; ++i)
366         XDrawLine(surf->dpy, surf->drawable, surf->gc, pnts[i].x, pnts[i].y, pnts[i+1].x, pnts[i+1].y);
367     XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
368 }
369 
370 NK_INTERN void
nk_xsurf_fill_circle(XSurface * surf,short x,short y,unsigned short w,unsigned short h,struct nk_color col)371 nk_xsurf_fill_circle(XSurface *surf, short x, short y, unsigned short w,
372     unsigned short h, struct nk_color col)
373 {
374     unsigned long c = nk_color_from_byte(&col.r);
375     XSetForeground(surf->dpy, surf->gc, c);
376     XFillArc(surf->dpy, surf->drawable, surf->gc, (int)x, (int)y,
377         (unsigned)w, (unsigned)h, 0, 360 * 64);
378 }
379 
380 NK_INTERN void
nk_xsurf_stroke_circle(XSurface * surf,short x,short y,unsigned short w,unsigned short h,unsigned short line_thickness,struct nk_color col)381 nk_xsurf_stroke_circle(XSurface *surf, short x, short y, unsigned short w,
382     unsigned short h, unsigned short line_thickness, struct nk_color col)
383 {
384     unsigned long c = nk_color_from_byte(&col.r);
385     XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
386     XSetForeground(surf->dpy, surf->gc, c);
387     XDrawArc(surf->dpy, surf->drawable, surf->gc, (int)x, (int)y,
388         (unsigned)w, (unsigned)h, 0, 360 * 64);
389     XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
390 }
391 
392 NK_INTERN void
nk_xsurf_stroke_curve(XSurface * surf,struct nk_vec2i p1,struct nk_vec2i p2,struct nk_vec2i p3,struct nk_vec2i p4,unsigned int num_segments,unsigned short line_thickness,struct nk_color col)393 nk_xsurf_stroke_curve(XSurface *surf, struct nk_vec2i p1,
394     struct nk_vec2i p2, struct nk_vec2i p3, struct nk_vec2i p4,
395     unsigned int num_segments, unsigned short line_thickness, struct nk_color col)
396 {
397     unsigned int i_step;
398     float t_step;
399     struct nk_vec2i last = p1;
400 
401     XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
402     num_segments = NK_MAX(num_segments, 1);
403     t_step = 1.0f/(float)num_segments;
404     for (i_step = 1; i_step <= num_segments; ++i_step) {
405         float t = t_step * (float)i_step;
406         float u = 1.0f - t;
407         float w1 = u*u*u;
408         float w2 = 3*u*u*t;
409         float w3 = 3*u*t*t;
410         float w4 = t * t *t;
411         float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
412         float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
413         nk_xsurf_stroke_line(surf, last.x, last.y, (short)x, (short)y, line_thickness,col);
414         last.x = (short)x; last.y = (short)y;
415     }
416     XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
417 }
418 
419 NK_INTERN void
nk_xsurf_draw_text(XSurface * surf,short x,short y,unsigned short w,unsigned short h,const char * text,int len,XFont * font,struct nk_color cbg,struct nk_color cfg)420 nk_xsurf_draw_text(XSurface *surf, short x, short y, unsigned short w, unsigned short h,
421     const char *text, int len, XFont *font, struct nk_color cbg, struct nk_color cfg)
422 {
423 #ifdef NK_XLIB_USE_XFT
424     XRenderColor xrc;
425     XftColor color;
426 #else
427     unsigned long fg = nk_color_from_byte(&cfg.r);
428 #endif
429     unsigned long bg = nk_color_from_byte(&cbg.r);
430     int tx, ty;
431 
432     XSetForeground(surf->dpy, surf->gc, bg);
433     XFillRectangle(surf->dpy, surf->drawable, surf->gc, (int)x, (int)y, (unsigned)w, (unsigned)h);
434     if(!text || !font || !len) return;
435 
436     tx = (int)x;
437     ty = (int)y + font->ascent;
438 #ifdef NK_XLIB_USE_XFT
439     xrc.red = cfg.r * 257;
440     xrc.green = cfg.g * 257;
441     xrc.blue = cfg.b * 257;
442     xrc.alpha = cfg.a * 257;
443     XftColorAllocValue(surf->dpy, xlib.vis, xlib.cmap, &xrc, &color);
444     XftDrawStringUtf8(surf->ftdraw, &color, font->ft, tx, ty, (FcChar8*)text, len);
445 	XftColorFree(surf->dpy, xlib.vis, xlib.cmap, &color);
446 #else
447     XSetForeground(surf->dpy, surf->gc, fg);
448     if(font->set) XmbDrawString(surf->dpy,surf->drawable, font->set, surf->gc, tx, ty, (const char*)text, (int)len);
449     else XDrawString(surf->dpy, surf->drawable, surf->gc, tx, ty, (const char*)text, (int)len);
450 #endif
451 	return;
452 }
453 
454 
455 #ifdef NK_XLIB_INCLUDE_STB_IMAGE
456 NK_INTERN struct nk_image
nk_stbi_image_to_xsurf(unsigned char * data,int width,int height,int channels)457 nk_stbi_image_to_xsurf(unsigned char *data, int width, int height, int channels) {
458     XSurface *surf = xlib.surf;
459     struct nk_image img;
460     int bpl = channels;
461     long i, isize = width*height*channels;
462     XImageWithAlpha *aimage = (XImageWithAlpha*)calloc( 1, sizeof(XImageWithAlpha) );
463     int depth = DefaultDepth(surf->dpy, surf->screen);
464     if (data == NULL) return nk_image_id(0);
465     if (aimage == NULL) return nk_image_id(0);
466 
467     switch (depth){
468         case 24:
469             bpl = 4;
470         break;
471         case 16:
472         case 15:
473             bpl = 2;
474         break;
475         default:
476             bpl = 1;
477         break;
478     }
479 
480     /* rgba to bgra */
481     if (channels >= 3){
482         for (i=0; i < isize; i += channels) {
483             unsigned char red  = data[i+2];
484             unsigned char blue = data[i];
485             data[i]   = red;
486             data[i+2] = blue;
487         }
488     }
489 
490     if (channels == 4){
491         const unsigned alpha_treshold = 127;
492         aimage->clipMask = XCreatePixmap(surf->dpy, surf->drawable, width, height, 1);
493 
494         if( aimage->clipMask ){
495             aimage->clipMaskGC = XCreateGC(surf->dpy, aimage->clipMask, 0, 0);
496             XSetForeground(surf->dpy, aimage->clipMaskGC, BlackPixel(surf->dpy, surf->screen));
497             XFillRectangle(surf->dpy, aimage->clipMask, aimage->clipMaskGC, 0, 0, width, height);
498 
499             XSetForeground(surf->dpy, aimage->clipMaskGC, WhitePixel(surf->dpy, surf->screen));
500             for (i=0; i < isize; i += channels){
501                 unsigned char alpha = data[i+3];
502                 int div = i / channels;
503                 int x = div % width;
504                 int y = div / width;
505                 if( alpha > alpha_treshold )
506                     XDrawPoint(surf->dpy, aimage->clipMask, aimage->clipMaskGC, x, y);
507             }
508         }
509     }
510 
511     aimage->ximage = XCreateImage(surf->dpy,
512            CopyFromParent, depth,
513            ZPixmap, 0,
514            (char*)data,
515            width, height,
516            bpl*8, bpl * width);
517     img = nk_image_ptr( (void*)aimage);
518     img.h = height;
519     img.w = width;
520     return img;
521 }
522 
523 NK_API struct nk_image
nk_xsurf_load_image_from_memory(const void * membuf,nk_uint membufSize)524 nk_xsurf_load_image_from_memory(const void *membuf, nk_uint membufSize)
525 {
526     int x,y,n;
527     unsigned char *data;
528     data = stbi_load_from_memory(membuf, membufSize, &x, &y, &n, 0);
529     return nk_stbi_image_to_xsurf(data, x, y, n);
530 }
531 
532 NK_API struct nk_image
nk_xsurf_load_image_from_file(char const * filename)533 nk_xsurf_load_image_from_file(char const *filename)
534 {
535     int x,y,n;
536     unsigned char *data;
537     data = stbi_load(filename, &x, &y, &n, 0);
538     return nk_stbi_image_to_xsurf(data, x, y, n);
539 }
540 #endif /* NK_XLIB_INCLUDE_STB_IMAGE */
541 
542 NK_INTERN void
nk_xsurf_draw_image(XSurface * surf,short x,short y,unsigned short w,unsigned short h,struct nk_image img,struct nk_color col)543 nk_xsurf_draw_image(XSurface *surf, short x, short y, unsigned short w, unsigned short h,
544     struct nk_image img, struct nk_color col)
545 {
546     XImageWithAlpha *aimage = img.handle.ptr;
547 
548     NK_UNUSED(col);
549 
550     if (aimage){
551         if (aimage->clipMask){
552             XSetClipMask(surf->dpy, surf->gc, aimage->clipMask);
553             XSetClipOrigin(surf->dpy, surf->gc, x, y);
554         }
555         XPutImage(surf->dpy, surf->drawable, surf->gc, aimage->ximage, 0, 0, x, y, w, h);
556         XSetClipMask(surf->dpy, surf->gc, None);
557     }
558 }
559 
560 void
nk_xsurf_image_free(struct nk_image * image)561 nk_xsurf_image_free(struct nk_image* image)
562 {
563     XSurface *surf = xlib.surf;
564     XImageWithAlpha *aimage = image->handle.ptr;
565     if (!aimage) return;
566     XDestroyImage(aimage->ximage);
567     XFreePixmap(surf->dpy, aimage->clipMask);
568     XFreeGC(surf->dpy, aimage->clipMaskGC);
569     free(aimage);
570 }
571 
572 
573 NK_INTERN void
nk_xsurf_clear(XSurface * surf,unsigned long color)574 nk_xsurf_clear(XSurface *surf, unsigned long color)
575 {
576     XSetForeground(surf->dpy, surf->gc, color);
577     XFillRectangle(surf->dpy, surf->drawable, surf->gc, 0, 0, surf->w, surf->h);
578 }
579 
580 NK_INTERN void
nk_xsurf_blit(Drawable target,XSurface * surf,unsigned int w,unsigned int h)581 nk_xsurf_blit(Drawable target, XSurface *surf, unsigned int w, unsigned int h)
582 {
583     XCopyArea(surf->dpy, surf->drawable, target, surf->gc, 0, 0, w, h, 0, 0);
584 }
585 
586 NK_INTERN void
nk_xsurf_del(XSurface * surf)587 nk_xsurf_del(XSurface *surf)
588 {
589 #ifdef NK_XLIB_USE_XFT
590 	XftDrawDestroy(surf->ftdraw);
591 #endif
592     XFreePixmap(surf->dpy, surf->drawable);
593     XFreeGC(surf->dpy, surf->gc);
594     free(surf);
595 }
596 
597 NK_API XFont*
nk_xfont_create(Display * dpy,const char * name)598 nk_xfont_create(Display *dpy, const char *name)
599 {
600 #ifdef NK_XLIB_USE_XFT
601     XFont *font = (XFont*)calloc(1, sizeof(XFont));
602     font->ft = XftFontOpenName(dpy, XDefaultScreen(dpy), name);
603     if (!font->ft) {
604         fprintf(stderr, "missing font: %s\n", name);
605         return font;
606     }
607     font->ascent = font->ft->ascent;
608     font->descent = font->ft->descent;
609     font->height = font->ft->height;
610 #else
611     int n;
612     char *def, **missing;
613     XFont *font = (XFont*)calloc(1, sizeof(XFont));
614     font->set = XCreateFontSet(dpy, name, &missing, &n, &def);
615     if(missing) {
616         while(n--)
617             fprintf(stderr, "missing fontset: %s\n", missing[n]);
618         XFreeStringList(missing);
619     }
620     if(font->set) {
621         XFontStruct **xfonts;
622         char **font_names;
623         XExtentsOfFontSet(font->set);
624         n = XFontsOfFontSet(font->set, &xfonts, &font_names);
625         while(n--) {
626             font->ascent = NK_MAX(font->ascent, (*xfonts)->ascent);
627             font->descent = NK_MAX(font->descent,(*xfonts)->descent);
628             xfonts++;
629         }
630     } else {
631         if(!(font->xfont = XLoadQueryFont(dpy, name))
632         && !(font->xfont = XLoadQueryFont(dpy, "fixed"))) {
633             free(font);
634             return 0;
635         }
636         font->ascent = font->xfont->ascent;
637         font->descent = font->xfont->descent;
638     }
639     font->height = font->ascent + font->descent;
640 #endif
641     return font;
642 }
643 
644 NK_INTERN float
nk_xfont_get_text_width(nk_handle handle,float height,const char * text,int len)645 nk_xfont_get_text_width(nk_handle handle, float height, const char *text, int len)
646 {
647     XFont *font = (XFont*)handle.ptr;
648 
649 #ifdef NK_XLIB_USE_XFT
650     XGlyphInfo g;
651 
652     NK_UNUSED(height);
653 
654 	if(!font || !text)
655 		return 0;
656 
657     XftTextExtentsUtf8(xlib.dpy, font->ft, (FcChar8*)text, len, &g);
658     return g.xOff;
659 #else
660     XRectangle r;
661 
662     NK_UNUSED(height);
663 
664 	if(!font || !text)
665 		return 0;
666 
667     if(font->set) {
668         XmbTextExtents(font->set, (const char*)text, len, NULL, &r);
669         return (float)r.width;
670     } else{
671         int w = XTextWidth(font->xfont, (const char*)text, len);
672         return (float)w;
673     }
674 #endif
675 }
676 
677 NK_API void
nk_xfont_del(Display * dpy,XFont * font)678 nk_xfont_del(Display *dpy, XFont *font)
679 {
680     if(!font) return;
681 #ifdef NK_XLIB_USE_XFT
682 	XftFontClose(dpy, font->ft);
683 #else
684     if(font->set)
685         XFreeFontSet(dpy, font->set);
686     else
687         XFreeFont(dpy, font->xfont);
688 #endif
689     free(font);
690 }
691 
692 NK_API struct nk_context*
nk_xlib_init(XFont * xfont,Display * dpy,int screen,Window root,Visual * vis,Colormap cmap,unsigned int w,unsigned int h)693 nk_xlib_init(XFont *xfont, Display *dpy, int screen, Window root,
694 #ifdef NK_XLIB_USE_XFT
695     Visual *vis, Colormap cmap,
696 #endif
697     unsigned int w, unsigned int h)
698 {
699     struct nk_user_font *font = &xfont->handle;
700     font->userdata = nk_handle_ptr(xfont);
701     font->height = (float)xfont->height;
702     font->width = nk_xfont_get_text_width;
703     xlib.dpy = dpy;
704     xlib.root = root;
705 #ifdef NK_XLIB_USE_XFT
706     xlib.vis = vis;
707     xlib.cmap = cmap;
708 #endif
709 
710     if (!setlocale(LC_ALL,"")) return 0;
711     if (!XSupportsLocale()) return 0;
712     if (!XSetLocaleModifiers("@im=none")) return 0;
713 
714     xlib.xa_clipboard = XInternAtom(dpy, "CLIPBOARD", False);
715     xlib.xa_targets = XInternAtom(dpy, "TARGETS", False);
716     xlib.xa_text = XInternAtom(dpy, "TEXT", False);
717     xlib.xa_utf8_string = XInternAtom(dpy, "UTF8_STRING", False);
718 
719     /* create invisible cursor */
720     {static XColor dummy; char data[1] = {0};
721     Pixmap blank = XCreateBitmapFromData(dpy, root, data, 1, 1);
722     if (blank == None) return 0;
723     xlib.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);
724     XFreePixmap(dpy, blank);}
725     xlib.surf = nk_xsurf_create(screen, w, h);
726     nk_init_default(&xlib.ctx, font);
727     return &xlib.ctx;
728 }
729 
730 NK_API void
nk_xlib_set_font(XFont * xfont)731 nk_xlib_set_font(XFont *xfont)
732 {
733     struct nk_user_font *font = &xfont->handle;
734     font->userdata = nk_handle_ptr(xfont);
735     font->height = (float)xfont->height;
736     font->width = nk_xfont_get_text_width;
737     nk_style_set_font(&xlib.ctx, font);
738 }
739 
740 NK_API void
nk_xlib_push_font(XFont * xfont)741 nk_xlib_push_font(XFont *xfont)
742 {
743     struct nk_user_font *font = &xfont->handle;
744     font->userdata = nk_handle_ptr(xfont);
745     font->height = (float)xfont->height;
746     font->width = nk_xfont_get_text_width;
747     nk_style_push_font(&xlib.ctx, font);
748 }
749 
750 NK_API void
nk_xlib_paste(nk_handle handle,struct nk_text_edit * edit)751 nk_xlib_paste(nk_handle handle, struct nk_text_edit* edit)
752 {
753     NK_UNUSED(handle);
754     /* Paste in X is asynchronous, so can not use a temporary text edit */
755     NK_ASSERT(edit != &xlib.ctx.text_edit && "Paste not supported for temporary editors");
756     xlib.clipboard_target = edit;
757     /* Request the contents of the primary buffer */
758     XConvertSelection(xlib.dpy, XA_PRIMARY, XA_STRING, XA_PRIMARY, xlib.root, CurrentTime);
759 }
760 
761 NK_API void
nk_xlib_copy(nk_handle handle,const char * str,int len)762 nk_xlib_copy(nk_handle handle, const char* str, int len)
763 {
764     NK_UNUSED(handle);
765     free(xlib.clipboard_data);
766     xlib.clipboard_len = 0;
767     xlib.clipboard_data = malloc((size_t)len);
768     if (xlib.clipboard_data) {
769         memcpy(xlib.clipboard_data, str, (size_t)len);
770         xlib.clipboard_len = len;
771         XSetSelectionOwner(xlib.dpy, XA_PRIMARY, xlib.root, CurrentTime);
772         XSetSelectionOwner(xlib.dpy, xlib.xa_clipboard, xlib.root, CurrentTime);
773     }
774 }
775 
776 NK_API int
nk_xlib_handle_event(Display * dpy,int screen,Window win,XEvent * evt)777 nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt)
778 {
779     struct nk_context *ctx = &xlib.ctx;
780 
781     NK_UNUSED(screen);
782 
783     /* optional grabbing behavior */
784     if (ctx->input.mouse.grab) {
785         XDefineCursor(xlib.dpy, xlib.root, xlib.cursor);
786         ctx->input.mouse.grab = 0;
787     } else if (ctx->input.mouse.ungrab) {
788         XWarpPointer(xlib.dpy, None, xlib.root, 0, 0, 0, 0,
789             (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y);
790         XUndefineCursor(xlib.dpy, xlib.root);
791         ctx->input.mouse.ungrab = 0;
792     }
793 
794     if (evt->type == KeyPress || evt->type == KeyRelease)
795     {
796         /* Key handler */
797         int ret, down = (evt->type == KeyPress);
798         KeySym *code = XGetKeyboardMapping(xlib.surf->dpy, (KeyCode)evt->xkey.keycode, 1, &ret);
799         if (*code == XK_Shift_L || *code == XK_Shift_R) nk_input_key(ctx, NK_KEY_SHIFT, down);
800         else if (*code == XK_Control_L || *code == XK_Control_R) nk_input_key(ctx, NK_KEY_CTRL, down);
801         else if (*code == XK_Delete)    nk_input_key(ctx, NK_KEY_DEL, down);
802         else if (*code == XK_Return)    nk_input_key(ctx, NK_KEY_ENTER, down);
803         else if (*code == XK_Tab)       nk_input_key(ctx, NK_KEY_TAB, down);
804         else if (*code == XK_Left)      nk_input_key(ctx, NK_KEY_LEFT, down);
805         else if (*code == XK_Right)     nk_input_key(ctx, NK_KEY_RIGHT, down);
806         else if (*code == XK_Up)        nk_input_key(ctx, NK_KEY_UP, down);
807         else if (*code == XK_Down)      nk_input_key(ctx, NK_KEY_DOWN, down);
808         else if (*code == XK_BackSpace) nk_input_key(ctx, NK_KEY_BACKSPACE, down);
809         else if (*code == XK_Escape)    nk_input_key(ctx, NK_KEY_TEXT_RESET_MODE, down);
810         else if (*code == XK_Page_Up)   nk_input_key(ctx, NK_KEY_SCROLL_UP, down);
811         else if (*code == XK_Page_Down) nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
812         else if (*code == XK_Home) {
813             nk_input_key(ctx, NK_KEY_TEXT_START, down);
814             nk_input_key(ctx, NK_KEY_SCROLL_START, down);
815         } else if (*code == XK_End) {
816             nk_input_key(ctx, NK_KEY_TEXT_END, down);
817             nk_input_key(ctx, NK_KEY_SCROLL_END, down);
818         } else {
819             if (*code == 'c' && (evt->xkey.state & ControlMask))
820                 nk_input_key(ctx, NK_KEY_COPY, down);
821             else if (*code == 'v' && (evt->xkey.state & ControlMask))
822                 nk_input_key(ctx, NK_KEY_PASTE, down);
823             else if (*code == 'x' && (evt->xkey.state & ControlMask))
824                 nk_input_key(ctx, NK_KEY_CUT, down);
825             else if (*code == 'z' && (evt->xkey.state & ControlMask))
826                 nk_input_key(ctx, NK_KEY_TEXT_UNDO, down);
827             else if (*code == 'r' && (evt->xkey.state & ControlMask))
828                 nk_input_key(ctx, NK_KEY_TEXT_REDO, down);
829             else if (*code == XK_Left && (evt->xkey.state & ControlMask))
830                 nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down);
831             else if (*code == XK_Right && (evt->xkey.state & ControlMask))
832                 nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down);
833             else if (*code == 'b' && (evt->xkey.state & ControlMask))
834                 nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down);
835             else if (*code == 'e' && (evt->xkey.state & ControlMask))
836                 nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down);
837             else {
838                 if (*code == 'i')
839                     nk_input_key(ctx, NK_KEY_TEXT_INSERT_MODE, down);
840                 else if (*code == 'r')
841                     nk_input_key(ctx, NK_KEY_TEXT_REPLACE_MODE, down);
842                 if (down) {
843                     char buf[32];
844                     KeySym keysym = 0;
845                     if (XLookupString((XKeyEvent*)evt, buf, 32, &keysym, NULL) != NoSymbol)
846                         nk_input_glyph(ctx, buf);
847                 }
848             }
849         }
850         XFree(code);
851         return 1;
852     } else if (evt->type == ButtonPress || evt->type == ButtonRelease) {
853         /* Button handler */
854         int down = (evt->type == ButtonPress);
855         const int x = evt->xbutton.x, y = evt->xbutton.y;
856         if (evt->xbutton.button == Button1) {
857             if (down) { /* Double-Click Button handler */
858                 long dt = nk_timestamp() - xlib.last_button_click;
859                 if (dt > NK_X11_DOUBLE_CLICK_LO && dt < NK_X11_DOUBLE_CLICK_HI)
860                     nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, nk_true);
861                 xlib.last_button_click = nk_timestamp();
862             } else nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, nk_false);
863             nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
864         } else if (evt->xbutton.button == Button2)
865             nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
866         else if (evt->xbutton.button == Button3)
867             nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
868         else if (evt->xbutton.button == Button4)
869             nk_input_scroll(ctx, nk_vec2(0, 1.0f));
870         else if (evt->xbutton.button == Button5)
871             nk_input_scroll(ctx, nk_vec2(0, -1.0f));
872         else return 0;
873         return 1;
874     } else if (evt->type == MotionNotify) {
875         /* Mouse motion handler */
876         const int x = evt->xmotion.x, y = evt->xmotion.y;
877         nk_input_motion(ctx, x, y);
878         if (ctx->input.mouse.grabbed) {
879             ctx->input.mouse.pos.x = ctx->input.mouse.prev.x;
880             ctx->input.mouse.pos.y = ctx->input.mouse.prev.y;
881             XWarpPointer(xlib.dpy, None, xlib.surf->root, 0, 0, 0, 0, (int)ctx->input.mouse.pos.x, (int)ctx->input.mouse.pos.y);
882         }
883         return 1;
884     } else if (evt->type == Expose || evt->type == ConfigureNotify) {
885         /* Window resize handler */
886         unsigned int width, height;
887         XWindowAttributes attr;
888         XGetWindowAttributes(dpy, win, &attr);
889         width = (unsigned int)attr.width;
890         height = (unsigned int)attr.height;
891         nk_xsurf_resize(xlib.surf, width, height);
892         return 1;
893     } else if (evt->type == KeymapNotify) {
894         XRefreshKeyboardMapping(&evt->xmapping);
895         return 1;
896     } else if (evt->type == SelectionClear) {
897         free(xlib.clipboard_data);
898         xlib.clipboard_data = NULL;
899         xlib.clipboard_len = 0;
900         return 1;
901     } else if (evt->type == SelectionRequest) {
902         XEvent reply;
903         reply.xselection.type = SelectionNotify;
904         reply.xselection.requestor = evt->xselectionrequest.requestor;
905         reply.xselection.selection = evt->xselectionrequest.selection;
906         reply.xselection.target = evt->xselectionrequest.target;
907         reply.xselection.property = None; /* Default refuse */
908         reply.xselection.time = evt->xselectionrequest.time;
909 
910         if (reply.xselection.target == xlib.xa_targets) {
911             Atom target_list[4];
912             target_list[0] = xlib.xa_targets;
913             target_list[1] = xlib.xa_text;
914             target_list[2] = xlib.xa_utf8_string;
915             target_list[3] = XA_STRING;
916 
917             reply.xselection.property = evt->xselectionrequest.property;
918             XChangeProperty(evt->xselection.display,evt->xselectionrequest.requestor,
919                 reply.xselection.property, XA_ATOM, 32, PropModeReplace,
920                 (unsigned char*)&target_list, 4);
921         } else if (xlib.clipboard_data && (reply.xselection.target == xlib.xa_text ||
922             reply.xselection.target == xlib.xa_utf8_string || reply.xselection.target == XA_STRING)) {
923             reply.xselection.property = evt->xselectionrequest.property;
924             XChangeProperty(evt->xselection.display,evt->xselectionrequest.requestor,
925                 reply.xselection.property, reply.xselection.target, 8, PropModeReplace,
926                 (unsigned char*)xlib.clipboard_data, xlib.clipboard_len);
927         }
928         XSendEvent(evt->xselection.display, evt->xselectionrequest.requestor, True, 0, &reply);
929         XFlush(evt->xselection.display);
930         return 1;
931     } else if (evt->type == SelectionNotify && xlib.clipboard_target) {
932         if ((evt->xselection.target != XA_STRING) &&
933             (evt->xselection.target != xlib.xa_utf8_string) &&
934             (evt->xselection.target != xlib.xa_text))
935             return 1;
936 
937         {Atom actual_type;
938         int actual_format;
939         unsigned long pos = 0, len, remain;
940         unsigned char* data = 0;
941         do {
942             XGetWindowProperty(dpy, win, XA_PRIMARY, (int)pos, 1024, False,
943                 AnyPropertyType, &actual_type, &actual_format, &len, &remain, &data);
944             if (len && data)
945                 nk_textedit_text(xlib.clipboard_target, (char*)data, (int)len);
946             if (data != 0) XFree(data);
947             pos += (len * (unsigned long)actual_format) / 32;
948         } while (remain != 0);}
949         return 1;
950     }
951     return 0;
952 }
953 
954 NK_API void
nk_xlib_shutdown(void)955 nk_xlib_shutdown(void)
956 {
957     nk_xsurf_del(xlib.surf);
958     nk_free(&xlib.ctx);
959     XFreeCursor(xlib.dpy, xlib.cursor);
960     memset(&xlib, 0, sizeof(xlib));
961 }
962 
963 NK_API void
nk_xlib_render(Drawable screen,struct nk_color clear)964 nk_xlib_render(Drawable screen, struct nk_color clear)
965 {
966     const struct nk_command *cmd;
967     struct nk_context *ctx = &xlib.ctx;
968     XSurface *surf = xlib.surf;
969 
970     nk_xsurf_clear(xlib.surf, nk_color_from_byte(&clear.r));
971     nk_foreach(cmd, &xlib.ctx)
972     {
973         switch (cmd->type) {
974         case NK_COMMAND_NOP: break;
975         case NK_COMMAND_SCISSOR: {
976             const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd;
977             nk_xsurf_scissor(surf, s->x, s->y, s->w, s->h);
978         } break;
979         case NK_COMMAND_LINE: {
980             const struct nk_command_line *l = (const struct nk_command_line *)cmd;
981             nk_xsurf_stroke_line(surf, l->begin.x, l->begin.y, l->end.x,
982                 l->end.y, l->line_thickness, l->color);
983         } break;
984         case NK_COMMAND_RECT: {
985             const struct nk_command_rect *r = (const struct nk_command_rect *)cmd;
986             nk_xsurf_stroke_rect(surf, r->x, r->y, NK_MAX(r->w -r->line_thickness, 0),
987                 NK_MAX(r->h - r->line_thickness, 0), (unsigned short)r->rounding,
988                 r->line_thickness, r->color);
989         } break;
990         case NK_COMMAND_RECT_FILLED: {
991             const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd;
992             nk_xsurf_fill_rect(surf, r->x, r->y, r->w, r->h,
993                 (unsigned short)r->rounding, r->color);
994         } break;
995         case NK_COMMAND_CIRCLE: {
996             const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
997             nk_xsurf_stroke_circle(surf, c->x, c->y, c->w, c->h, c->line_thickness, c->color);
998         } break;
999         case NK_COMMAND_CIRCLE_FILLED: {
1000             const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
1001             nk_xsurf_fill_circle(surf, c->x, c->y, c->w, c->h, c->color);
1002         } break;
1003         case NK_COMMAND_TRIANGLE: {
1004             const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
1005             nk_xsurf_stroke_triangle(surf, t->a.x, t->a.y, t->b.x, t->b.y,
1006                 t->c.x, t->c.y, t->line_thickness, t->color);
1007         } break;
1008         case NK_COMMAND_TRIANGLE_FILLED: {
1009             const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd;
1010             nk_xsurf_fill_triangle(surf, t->a.x, t->a.y, t->b.x, t->b.y,
1011                 t->c.x, t->c.y, t->color);
1012         } break;
1013         case NK_COMMAND_POLYGON: {
1014             const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd;
1015             nk_xsurf_stroke_polygon(surf, p->points, p->point_count, p->line_thickness,p->color);
1016         } break;
1017         case NK_COMMAND_POLYGON_FILLED: {
1018             const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd;
1019             nk_xsurf_fill_polygon(surf, p->points, p->point_count, p->color);
1020         } break;
1021         case NK_COMMAND_POLYLINE: {
1022             const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
1023             nk_xsurf_stroke_polyline(surf, p->points, p->point_count, p->line_thickness, p->color);
1024         } break;
1025         case NK_COMMAND_TEXT: {
1026             const struct nk_command_text *t = (const struct nk_command_text*)cmd;
1027             nk_xsurf_draw_text(surf, t->x, t->y, t->w, t->h,
1028                 (const char*)t->string, t->length,
1029                 (XFont*)t->font->userdata.ptr,
1030                 t->background, t->foreground);
1031         } break;
1032         case NK_COMMAND_CURVE: {
1033             const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
1034             nk_xsurf_stroke_curve(surf, q->begin, q->ctrl[0], q->ctrl[1],
1035                 q->end, 22, q->line_thickness, q->color);
1036         } break;
1037         case NK_COMMAND_IMAGE: {
1038             const struct nk_command_image *i = (const struct nk_command_image *)cmd;
1039             nk_xsurf_draw_image(surf, i->x, i->y, i->w, i->h, i->img, i->col);
1040         } break;
1041         case NK_COMMAND_RECT_MULTI_COLOR:
1042         case NK_COMMAND_ARC:
1043         case NK_COMMAND_ARC_FILLED:
1044         case NK_COMMAND_CUSTOM:
1045         default: break;
1046         }
1047     }
1048     nk_clear(ctx);
1049     nk_xsurf_blit(screen, surf, surf->w, surf->h);
1050 }
1051 #endif
1052