nuklear_xlib.h 38 KB

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