nuklear_xlib.h 36 KB

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