nuklear_xlib.h 36 KB

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