nuklear_xlib.h 36 KB

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