nuklear_xlib.h 38 KB

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