nuklear_xlib.h 35 KB

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