PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/demo/x11/nuklear_xlib.c

https://gitlab.com/oytunistrator/nuklear
C | 609 lines | 557 code | 48 blank | 4 comment | 142 complexity | 7469fcfa9bdba67959fa3ab15a2d6a74 MD5 | raw file
  1. #include <stdio.h>
  2. #include <X11/Xlib.h>
  3. #include <X11/Xutil.h>
  4. #include <X11/Xresource.h>
  5. #include <X11/Xlocale.h>
  6. #define NK_IMPLEMENTATION
  7. #include "nuklear_xlib.h"
  8. #include "../../nuklear.h"
  9. typedef struct XSurface XSurface;
  10. struct XFont {
  11. int ascent;
  12. int descent;
  13. int height;
  14. XFontSet set;
  15. XFontStruct *xfont;
  16. };
  17. struct XSurface {
  18. GC gc;
  19. Display *dpy;
  20. int screen;
  21. Window root;
  22. Drawable drawable;
  23. unsigned int w, h;
  24. };
  25. static struct {
  26. struct nk_context ctx;
  27. struct XSurface *surf;
  28. } xlib;
  29. #ifndef MIN
  30. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  31. #endif
  32. #ifndef MAX
  33. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  34. #endif
  35. static unsigned long
  36. color_from_byte(const nk_byte *c)
  37. {
  38. unsigned long res = 0;
  39. res |= (unsigned long)c[0] << 16;
  40. res |= (unsigned long)c[1] << 8;
  41. res |= (unsigned long)c[2] << 0;
  42. return (res);
  43. }
  44. static XSurface*
  45. nk_xsurf_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h)
  46. {
  47. XSurface *surface = (XSurface*)calloc(1, sizeof(XSurface));
  48. surface->w = w;
  49. surface->h = h;
  50. surface->dpy = dpy;
  51. surface->screen = screen;
  52. surface->root = root;
  53. surface->gc = XCreateGC(dpy, root, 0, NULL);
  54. XSetLineAttributes(dpy, surface->gc, 1, LineSolid, CapButt, JoinMiter);
  55. surface->drawable = XCreatePixmap(dpy, root, w, h, 32);
  56. return surface;
  57. }
  58. static void
  59. nk_xsurf_resize(XSurface *surf, unsigned int w, unsigned int h)
  60. {
  61. if(!surf) return;
  62. if (surf->w == w && surf->h == h) return;
  63. surf->w = w; surf->h = h;
  64. if(surf->drawable) XFreePixmap(surf->dpy, surf->drawable);
  65. surf->drawable = XCreatePixmap(surf->dpy, surf->root, w, h,
  66. (unsigned int)DefaultDepth(surf->dpy, surf->screen));
  67. }
  68. static void
  69. nk_xsurf_scissor(XSurface *surf, float x, float y, float w, float h)
  70. {
  71. XRectangle clip_rect;
  72. clip_rect.x = (short)(x-1);
  73. clip_rect.y = (short)(y-1);
  74. clip_rect.width = (unsigned short)(w+2);
  75. clip_rect.height = (unsigned short)(h+2);
  76. XSetClipRectangles(surf->dpy, surf->gc, 0, 0, &clip_rect, 1, Unsorted);
  77. }
  78. static void
  79. nk_xsurf_stroke_line(XSurface *surf, short x0, short y0, short x1,
  80. short y1, unsigned int line_thickness, struct nk_color col)
  81. {
  82. unsigned long c = color_from_byte(&col.r);
  83. XSetForeground(surf->dpy, surf->gc, c);
  84. XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
  85. XDrawLine(surf->dpy, surf->drawable, surf->gc, (int)x0, (int)y0, (int)x1, (int)y1);
  86. XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
  87. }
  88. static void
  89. nk_xsurf_stroke_rect(XSurface* surf, short x, short y, unsigned short w,
  90. unsigned short h, unsigned short r, unsigned short line_thickness, struct nk_color col)
  91. {
  92. unsigned long c = color_from_byte(&col.r);
  93. XSetForeground(surf->dpy, surf->gc, c);
  94. XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
  95. if (r == 0) {
  96. XFillRectangle(surf->dpy, surf->drawable, surf->gc, x, y, w, h);
  97. } else {
  98. short xc = x + r;
  99. short yc = y + r;
  100. short wc = (short)(w - 2 * r);
  101. short hc = (short)(h - 2 * r);
  102. XDrawLine(surf->dpy, surf->drawable, surf->gc, xc, y, xc+wc, y);
  103. XDrawLine(surf->dpy, surf->drawable, surf->gc, x+w, yc, x+w, yc+wc);
  104. XDrawLine(surf->dpy, surf->drawable, surf->gc, xc, y+h, xc+wc, y+h);
  105. XDrawLine(surf->dpy, surf->drawable, surf->gc, x, yc, yc+hc, x);
  106. XFillArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, y,
  107. (unsigned)r*2, (unsigned)r*2, 0 * 64, 90 * 64);
  108. XFillArc(surf->dpy, surf->drawable, surf->gc, x, y,
  109. (unsigned)r*2, (unsigned)r*2, 90 * 64, 90 * 64);
  110. XFillArc(surf->dpy, surf->drawable, surf->gc, x, yc + hc - r,
  111. (unsigned)r*2, (unsigned)2*r, 180 * 64, 90 * 64);
  112. XFillArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, yc + hc - r,
  113. (unsigned)r*2, (unsigned)2*r, -90 * 64, 90 * 64);
  114. }
  115. XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
  116. }
  117. static void
  118. nk_xsurf_fill_rect(XSurface* surf, short x, short y, unsigned short w,
  119. unsigned short h, unsigned short r, struct nk_color col)
  120. {
  121. unsigned long c = color_from_byte(&col.r);
  122. XSetForeground(surf->dpy, surf->gc, c);
  123. if (r == 0) {
  124. XFillRectangle(surf->dpy, surf->drawable, surf->gc, x, y, w, h);
  125. } else {
  126. short xc = x + r;
  127. short yc = y + r;
  128. short wc = (short)(w - 2 * r);
  129. short hc = (short)(h - 2 * r);
  130. XPoint pnts[12];
  131. pnts[0].x = x;
  132. pnts[0].y = yc;
  133. pnts[1].x = xc;
  134. pnts[1].y = yc;
  135. pnts[2].x = xc;
  136. pnts[2].y = y;
  137. pnts[3].x = xc + wc;
  138. pnts[3].y = y;
  139. pnts[4].x = xc + wc;
  140. pnts[4].y = yc;
  141. pnts[5].x = x + w;
  142. pnts[5].y = yc;
  143. pnts[6].x = x + w;
  144. pnts[6].y = yc + hc;
  145. pnts[7].x = xc + wc;
  146. pnts[7].y = yc + hc;
  147. pnts[8].x = xc + wc;
  148. pnts[8].y = y + h;
  149. pnts[9].x = xc;
  150. pnts[9].y = y + h;
  151. pnts[10].x = xc;
  152. pnts[10].y = yc + hc;
  153. pnts[11].x = x;
  154. pnts[11].y = yc + hc;
  155. XFillPolygon(surf->dpy, surf->drawable, surf->gc, pnts, 12, Convex, CoordModeOrigin);
  156. XFillArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, y,
  157. (unsigned)r*2, (unsigned)r*2, 0 * 64, 90 * 64);
  158. XFillArc(surf->dpy, surf->drawable, surf->gc, x, y,
  159. (unsigned)r*2, (unsigned)r*2, 90 * 64, 90 * 64);
  160. XFillArc(surf->dpy, surf->drawable, surf->gc, x, yc + hc - r,
  161. (unsigned)r*2, (unsigned)2*r, 180 * 64, 90 * 64);
  162. XFillArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, yc + hc - r,
  163. (unsigned)r*2, (unsigned)2*r, -90 * 64, 90 * 64);
  164. }
  165. }
  166. static void
  167. nk_xsurf_fill_triangle(XSurface *surf, short x0, short y0, short x1,
  168. short y1, short x2, short y2, struct nk_color col)
  169. {
  170. XPoint pnts[3];
  171. unsigned long c = color_from_byte(&col.r);
  172. pnts[0].x = (short)x0;
  173. pnts[0].y = (short)y0;
  174. pnts[1].x = (short)x1;
  175. pnts[1].y = (short)y1;
  176. pnts[2].x = (short)x2;
  177. pnts[2].y = (short)y2;
  178. XSetForeground(surf->dpy, surf->gc, c);
  179. XFillPolygon(surf->dpy, surf->drawable, surf->gc, pnts, 3, Convex, CoordModeOrigin);
  180. }
  181. static void
  182. nk_xsurf_stroke_triangle(XSurface *surf, short x0, short y0, short x1,
  183. short y1, short x2, short y2, unsigned short line_thickness, struct nk_color col)
  184. {
  185. XPoint pnts[3];
  186. unsigned long c = color_from_byte(&col.r);
  187. XSetForeground(surf->dpy, surf->gc, c);
  188. XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
  189. XDrawLine(surf->dpy, surf->drawable, surf->gc, x0, y0, x1, y1);
  190. XDrawLine(surf->dpy, surf->drawable, surf->gc, x1, y1, x2, y2);
  191. XDrawLine(surf->dpy, surf->drawable, surf->gc, x2, y2, x0, y0);
  192. XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
  193. }
  194. static void
  195. nk_xsurf_fill_polygon(XSurface *surf, const struct nk_vec2i *pnts, int count,
  196. struct nk_color col)
  197. {
  198. int i = 0;
  199. #define MAX_POINTS 64
  200. XPoint xpnts[MAX_POINTS];
  201. unsigned long c = color_from_byte(&col.r);
  202. XSetForeground(surf->dpy, surf->gc, c);
  203. for (i = 0; i < count && i < MAX_POINTS; ++i) {
  204. xpnts[i].x = pnts[i].x;
  205. xpnts[i].y = pnts[i].y;
  206. }
  207. XFillPolygon(surf->dpy, surf->drawable, surf->gc, xpnts, count, Convex, CoordModeOrigin);
  208. #undef MAX_POINTS
  209. }
  210. static void
  211. nk_xsurf_stroke_polygon(XSurface *surf, const struct nk_vec2i *pnts, int count,
  212. unsigned short line_thickness, struct nk_color col)
  213. {
  214. int i = 0;
  215. unsigned long c = color_from_byte(&col.r);
  216. XSetForeground(surf->dpy, surf->gc, c);
  217. XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
  218. for (i = 1; i < count; ++i)
  219. XDrawLine(surf->dpy, surf->drawable, surf->gc, pnts[i-1].x, pnts[i-1].y, pnts[i].x, pnts[i].y);
  220. XDrawLine(surf->dpy, surf->drawable, surf->gc, pnts[count-1].x, pnts[count-1].y, pnts[0].x, pnts[0].y);
  221. XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
  222. }
  223. static void
  224. nk_xsurf_stroke_polyline(XSurface *surf, const struct nk_vec2i *pnts,
  225. int count, unsigned short line_thickness, struct nk_color col)
  226. {
  227. int i = 0;
  228. unsigned long c = color_from_byte(&col.r);
  229. XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
  230. XSetForeground(surf->dpy, surf->gc, c);
  231. for (i = 0; i < count-1; ++i)
  232. XDrawLine(surf->dpy, surf->drawable, surf->gc, pnts[i].x, pnts[i].y, pnts[i+1].x, pnts[i+1].y);
  233. XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
  234. }
  235. static void
  236. nk_xsurf_fill_circle(XSurface *surf, short x, short y, unsigned short w,
  237. unsigned short h, struct nk_color col)
  238. {
  239. unsigned long c = color_from_byte(&col.r);
  240. XSetForeground(surf->dpy, surf->gc, c);
  241. XFillArc(surf->dpy, surf->drawable, surf->gc, (int)x, (int)y,
  242. (unsigned)w, (unsigned)h, 0, 360 * 64);
  243. }
  244. static void
  245. nk_xsurf_stroke_circle(XSurface *surf, short x, short y, unsigned short w,
  246. unsigned short h, unsigned short line_thickness, struct nk_color col)
  247. {
  248. unsigned long c = color_from_byte(&col.r);
  249. XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
  250. XSetForeground(surf->dpy, surf->gc, c);
  251. XDrawArc(surf->dpy, surf->drawable, surf->gc, (int)x, (int)y,
  252. (unsigned)w, (unsigned)h, 0, 360 * 64);
  253. XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
  254. }
  255. static void
  256. nk_xsurf_stroke_curve(XSurface *surf, struct nk_vec2i p1,
  257. struct nk_vec2i p2, struct nk_vec2i p3, struct nk_vec2i p4,
  258. unsigned int num_segments, unsigned short line_thickness, struct nk_color col)
  259. {
  260. unsigned int i_step;
  261. float t_step;
  262. struct nk_vec2i last = p1;
  263. XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
  264. num_segments = MAX(num_segments, 1);
  265. t_step = 1.0f/(float)num_segments;
  266. for (i_step = 1; i_step <= num_segments; ++i_step) {
  267. float t = t_step * (float)i_step;
  268. float u = 1.0f - t;
  269. float w1 = u*u*u;
  270. float w2 = 3*u*u*t;
  271. float w3 = 3*u*t*t;
  272. float w4 = t * t *t;
  273. float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
  274. float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
  275. nk_xsurf_stroke_line(surf, last.x, last.y, (short)x, (short)y, line_thickness,col);
  276. last.x = (short)x; last.y = (short)y;
  277. }
  278. XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
  279. }
  280. static void
  281. nk_xsurf_draw_text(XSurface *surf, short x, short y, unsigned short w, unsigned short h,
  282. const char *text, int len, XFont *font, struct nk_color cbg, struct nk_color cfg)
  283. {
  284. int tx, ty;
  285. unsigned long bg = color_from_byte(&cbg.r);
  286. unsigned long fg = color_from_byte(&cfg.r);
  287. XSetForeground(surf->dpy, surf->gc, bg);
  288. XFillRectangle(surf->dpy, surf->drawable, surf->gc, (int)x, (int)y, (unsigned)w, (unsigned)h);
  289. if(!text || !font || !len) return;
  290. tx = (int)x;
  291. ty = (int)y + font->ascent;
  292. XSetForeground(surf->dpy, surf->gc, fg);
  293. if(font->set)
  294. XmbDrawString(surf->dpy,surf->drawable,font->set,surf->gc,tx,ty,(const char*)text,(int)len);
  295. else
  296. XDrawString(surf->dpy, surf->drawable, surf->gc, tx, ty, (const char*)text, (int)len);
  297. }
  298. static void
  299. nk_xsurf_clear(XSurface *surf, unsigned long color)
  300. {
  301. XSetForeground(surf->dpy, surf->gc, color);
  302. XFillRectangle(surf->dpy, surf->drawable, surf->gc, 0, 0, surf->w, surf->h);
  303. }
  304. static void
  305. nk_xsurf_blit(Drawable target, XSurface *surf, unsigned int w, unsigned int h)
  306. {
  307. XCopyArea(surf->dpy, surf->drawable, target, surf->gc, 0, 0, w, h, 0, 0);
  308. }
  309. static void
  310. nk_xsurf_del(XSurface *surf)
  311. {
  312. XFreePixmap(surf->dpy, surf->drawable);
  313. XFreeGC(surf->dpy, surf->gc);
  314. free(surf);
  315. }
  316. XFont*
  317. nk_xfont_create(Display *dpy, const char *name)
  318. {
  319. int n;
  320. char *def, **missing;
  321. XFont *font = (XFont*)calloc(1, sizeof(XFont));
  322. font->set = XCreateFontSet(dpy, name, &missing, &n, &def);
  323. if(missing) {
  324. while(n--)
  325. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  326. XFreeStringList(missing);
  327. }
  328. if(font->set) {
  329. XFontStruct **xfonts;
  330. char **font_names;
  331. XExtentsOfFontSet(font->set);
  332. n = XFontsOfFontSet(font->set, &xfonts, &font_names);
  333. while(n--) {
  334. font->ascent = MAX(font->ascent, (*xfonts)->ascent);
  335. font->descent = MAX(font->descent,(*xfonts)->descent);
  336. xfonts++;
  337. }
  338. } else {
  339. if(!(font->xfont = XLoadQueryFont(dpy, name))
  340. && !(font->xfont = XLoadQueryFont(dpy, "fixed"))) {
  341. free(font);
  342. return 0;
  343. }
  344. font->ascent = font->xfont->ascent;
  345. font->descent = font->xfont->descent;
  346. }
  347. font->height = font->ascent + font->descent;
  348. return font;
  349. }
  350. static float
  351. nk_xfont_get_text_width(nk_handle handle, float height, const char *text, int len)
  352. {
  353. XFont *font = (XFont*)handle.ptr;
  354. XRectangle r;
  355. if(!font || !text)
  356. return 0;
  357. if(font->set) {
  358. XmbTextExtents(font->set, (const char*)text, len, NULL, &r);
  359. return (float)r.width;
  360. } else{
  361. int w = XTextWidth(font->xfont, (const char*)text, len);
  362. return (float)w;
  363. }
  364. }
  365. void
  366. nk_xfont_del(Display *dpy, XFont *font)
  367. {
  368. if(!font) return;
  369. if(font->set)
  370. XFreeFontSet(dpy, font->set);
  371. else
  372. XFreeFont(dpy, font->xfont);
  373. free(font);
  374. }
  375. NK_API struct nk_context*
  376. nk_xlib_init(XFont *xfont, Display *dpy, int screen, Window root,
  377. unsigned int w, unsigned int h)
  378. {
  379. struct nk_user_font font;
  380. font.userdata = nk_handle_ptr(xfont);
  381. font.height = (float)xfont->height;
  382. font.width = nk_xfont_get_text_width;
  383. if (!setlocale(LC_ALL,"")) return 0;
  384. if (!XSupportsLocale()) return 0;
  385. if (!XSetLocaleModifiers("@im=none")) return 0;
  386. xlib.surf = nk_xsurf_create(dpy, screen, root, w, h);
  387. nk_init_default(&xlib.ctx, &font);
  388. return &xlib.ctx;
  389. }
  390. NK_API void
  391. nk_xlib_set_font(XFont *xfont)
  392. {
  393. struct nk_user_font font;
  394. font.userdata = nk_handle_ptr(xfont);
  395. font.height = (float)xfont->height;
  396. font.width = nk_xfont_get_text_width;
  397. nk_style_set_font(&xlib.ctx, &font);
  398. }
  399. NK_API void
  400. nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt)
  401. {
  402. struct nk_context *ctx = &xlib.ctx;
  403. if (evt->type == KeyPress || evt->type == KeyRelease)
  404. {
  405. /* Key handler */
  406. int ret, down = (evt->type == KeyPress);
  407. KeySym *code = XGetKeyboardMapping(xlib.surf->dpy, (KeyCode)evt->xkey.keycode, 1, &ret);
  408. if (*code == XK_Shift_L || *code == XK_Shift_R) nk_input_key(ctx, NK_KEY_SHIFT, down);
  409. else if (*code == XK_Delete) nk_input_key(ctx, NK_KEY_DEL, down);
  410. else if (*code == XK_Return) nk_input_key(ctx, NK_KEY_ENTER, down);
  411. else if (*code == XK_Tab) nk_input_key(ctx, NK_KEY_TAB, down);
  412. else if (*code == XK_Left) nk_input_key(ctx, NK_KEY_LEFT, down);
  413. else if (*code == XK_Right) nk_input_key(ctx, NK_KEY_RIGHT, down);
  414. else if (*code == XK_BackSpace) nk_input_key(ctx, NK_KEY_BACKSPACE, down);
  415. else if (*code == XK_Home) nk_input_key(ctx, NK_KEY_TEXT_START, down);
  416. else if (*code == XK_End) nk_input_key(ctx, NK_KEY_TEXT_END, down);
  417. else if (*code == XK_space && !down) nk_input_char(ctx, ' ');
  418. else {
  419. if (*code == 'c' && (evt->xkey.state & ControlMask))
  420. nk_input_key(ctx, NK_KEY_COPY, down);
  421. else if (*code == 'v' && (evt->xkey.state & ControlMask))
  422. nk_input_key(ctx, NK_KEY_PASTE, down);
  423. else if (*code == 'x' && (evt->xkey.state & ControlMask))
  424. nk_input_key(ctx, NK_KEY_CUT, down);
  425. else if (*code == 'z' && (evt->xkey.state & ControlMask))
  426. nk_input_key(ctx, NK_KEY_TEXT_UNDO, down);
  427. else if (*code == 'r' && (evt->xkey.state & ControlMask))
  428. nk_input_key(ctx, NK_KEY_TEXT_REDO, down);
  429. else if (*code == XK_Left && (evt->xkey.state & ControlMask))
  430. nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down);
  431. else if (*code == XK_Right && (evt->xkey.state & ControlMask))
  432. nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down);
  433. else if (*code == 'b' && (evt->xkey.state & ControlMask))
  434. nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down);
  435. else if (*code == 'e' && (evt->xkey.state & ControlMask))
  436. nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down);
  437. else if (!down) {
  438. char buf[32];
  439. KeySym keysym = 0;
  440. if (XLookupString((XKeyEvent*)evt, buf, 32, &keysym, NULL) != NoSymbol)
  441. nk_input_glyph(ctx, buf);
  442. }
  443. }
  444. XFree(code);
  445. } else if (evt->type == ButtonPress || evt->type == ButtonRelease) {
  446. /* Button handler */
  447. int down = (evt->type == ButtonPress);
  448. const int x = evt->xbutton.x, y = evt->xbutton.y;
  449. if (evt->xbutton.button == Button1)
  450. nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
  451. if (evt->xbutton.button == Button2)
  452. nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
  453. else if (evt->xbutton.button == Button3)
  454. nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
  455. else if (evt->xbutton.button == Button4)
  456. nk_input_scroll(ctx, 1.0f);
  457. else if (evt->xbutton.button == Button5)
  458. nk_input_scroll(ctx, -1.0f);
  459. } else if (evt->type == MotionNotify) {
  460. /* Mouse motion handler */
  461. const int x = evt->xmotion.x, y = evt->xmotion.y;
  462. nk_input_motion(ctx, x, y);
  463. } else if (evt->type == Expose || evt->type == ConfigureNotify) {
  464. /* Window resize handler */
  465. unsigned int width, height;
  466. XWindowAttributes attr;
  467. XGetWindowAttributes(dpy, win, &attr);
  468. width = (unsigned int)attr.width;
  469. height = (unsigned int)attr.height;
  470. nk_xsurf_resize(xlib.surf, width, height);
  471. } else if (evt->type == KeymapNotify)
  472. XRefreshKeyboardMapping(&evt->xmapping);
  473. }
  474. NK_API void
  475. nk_xlib_shutdown(void)
  476. {
  477. nk_xsurf_del(xlib.surf);
  478. nk_free(&xlib.ctx);
  479. }
  480. NK_API void
  481. nk_xlib_render(Drawable screen, struct nk_color clear)
  482. {
  483. const struct nk_command *cmd;
  484. struct nk_context *ctx = &xlib.ctx;
  485. XSurface *surf = xlib.surf;
  486. nk_xsurf_clear(xlib.surf, color_from_byte(&clear.r));
  487. nk_foreach(cmd, &xlib.ctx)
  488. {
  489. switch (cmd->type) {
  490. case NK_COMMAND_NOP: break;
  491. case NK_COMMAND_SCISSOR: {
  492. const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd;
  493. nk_xsurf_scissor(surf, s->x, s->y, s->w, s->h);
  494. } break;
  495. case NK_COMMAND_LINE: {
  496. const struct nk_command_line *l = (const struct nk_command_line *)cmd;
  497. nk_xsurf_stroke_line(surf, l->begin.x, l->begin.y, l->end.x,
  498. l->end.y, l->line_thickness, l->color);
  499. } break;
  500. case NK_COMMAND_RECT: {
  501. const struct nk_command_rect *r = (const struct nk_command_rect *)cmd;
  502. nk_xsurf_stroke_rect(surf, r->x, r->y, r->w, r->h,
  503. (unsigned short)r->rounding, r->line_thickness, r->color);
  504. } break;
  505. case NK_COMMAND_RECT_FILLED: {
  506. const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd;
  507. nk_xsurf_fill_rect(surf, r->x, r->y, r->w, r->h,
  508. (unsigned short)r->rounding, r->color);
  509. } break;
  510. case NK_COMMAND_CIRCLE: {
  511. const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
  512. nk_xsurf_stroke_circle(surf, c->x, c->y, c->w, c->h, c->line_thickness, c->color);
  513. } break;
  514. case NK_COMMAND_CIRCLE_FILLED: {
  515. const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
  516. nk_xsurf_fill_circle(surf, c->x, c->y, c->w, c->h, c->color);
  517. } break;
  518. case NK_COMMAND_TRIANGLE: {
  519. const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
  520. nk_xsurf_stroke_triangle(surf, t->a.x, t->a.y, t->b.x, t->b.y,
  521. t->c.x, t->c.y, t->line_thickness, t->color);
  522. } break;
  523. case NK_COMMAND_TRIANGLE_FILLED: {
  524. const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd;
  525. nk_xsurf_fill_triangle(surf, t->a.x, t->a.y, t->b.x, t->b.y,
  526. t->c.x, t->c.y, t->color);
  527. } break;
  528. case NK_COMMAND_POLYGON: {
  529. const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd;
  530. nk_xsurf_stroke_polygon(surf, p->points, p->point_count, p->line_thickness,p->color);
  531. } break;
  532. case NK_COMMAND_POLYGON_FILLED: {
  533. const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd;
  534. nk_xsurf_fill_polygon(surf, p->points, p->point_count, p->color);
  535. } break;
  536. case NK_COMMAND_POLYLINE: {
  537. const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
  538. nk_xsurf_stroke_polyline(surf, p->points, p->point_count, p->line_thickness, p->color);
  539. } break;
  540. case NK_COMMAND_TEXT: {
  541. const struct nk_command_text *t = (const struct nk_command_text*)cmd;
  542. nk_xsurf_draw_text(surf, t->x, t->y, t->w, t->h,
  543. (const char*)t->string, t->length,
  544. (XFont*)t->font->userdata.ptr,
  545. t->background, t->foreground);
  546. } break;
  547. case NK_COMMAND_CURVE: {
  548. const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
  549. nk_xsurf_stroke_curve(surf, q->begin, q->ctrl[0], q->ctrl[1],
  550. q->end, 22, q->line_thickness, q->color);
  551. } break;
  552. case NK_COMMAND_RECT_MULTI_COLOR:
  553. case NK_COMMAND_IMAGE:
  554. case NK_COMMAND_ARC:
  555. case NK_COMMAND_ARC_FILLED:
  556. default: break;
  557. }
  558. }
  559. nk_clear(ctx);
  560. nk_xsurf_blit(screen, surf, surf->w, surf->h);
  561. }