nuklear_button.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. #include "nuklear.h"
  2. #include "nuklear_internal.h"
  3. /* ==============================================================
  4. *
  5. * BUTTON
  6. *
  7. * ===============================================================*/
  8. NK_LIB void
  9. nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type,
  10. struct nk_rect content, struct nk_color background, struct nk_color foreground,
  11. float border_width, const struct nk_user_font *font)
  12. {
  13. switch (type) {
  14. case NK_SYMBOL_X:
  15. case NK_SYMBOL_UNDERSCORE:
  16. case NK_SYMBOL_PLUS:
  17. case NK_SYMBOL_MINUS: {
  18. /* single character text symbol */
  19. const char *X = (type == NK_SYMBOL_X) ? "x":
  20. (type == NK_SYMBOL_UNDERSCORE) ? "_":
  21. (type == NK_SYMBOL_PLUS) ? "+": "-";
  22. struct nk_text text;
  23. text.padding = nk_vec2(0,0);
  24. text.background = background;
  25. text.text = foreground;
  26. nk_widget_text(out, content, X, 1, &text, NK_TEXT_CENTERED, font);
  27. } break;
  28. case NK_SYMBOL_CIRCLE_SOLID:
  29. case NK_SYMBOL_CIRCLE_OUTLINE:
  30. case NK_SYMBOL_RECT_SOLID:
  31. case NK_SYMBOL_RECT_OUTLINE: {
  32. /* simple empty/filled shapes */
  33. if (type == NK_SYMBOL_RECT_SOLID || type == NK_SYMBOL_RECT_OUTLINE) {
  34. nk_fill_rect(out, content, 0, foreground);
  35. if (type == NK_SYMBOL_RECT_OUTLINE)
  36. nk_fill_rect(out, nk_shrink_rect(content, border_width), 0, background);
  37. } else {
  38. nk_fill_circle(out, content, foreground);
  39. if (type == NK_SYMBOL_CIRCLE_OUTLINE)
  40. nk_fill_circle(out, nk_shrink_rect(content, 1), background);
  41. }
  42. } break;
  43. case NK_SYMBOL_TRIANGLE_UP:
  44. case NK_SYMBOL_TRIANGLE_DOWN:
  45. case NK_SYMBOL_TRIANGLE_LEFT:
  46. case NK_SYMBOL_TRIANGLE_RIGHT: {
  47. enum nk_heading heading;
  48. struct nk_vec2 points[3];
  49. heading = (type == NK_SYMBOL_TRIANGLE_RIGHT) ? NK_RIGHT :
  50. (type == NK_SYMBOL_TRIANGLE_LEFT) ? NK_LEFT:
  51. (type == NK_SYMBOL_TRIANGLE_UP) ? NK_UP: NK_DOWN;
  52. nk_triangle_from_direction(points, content, 0, 0, heading);
  53. nk_fill_triangle(out, points[0].x, points[0].y, points[1].x, points[1].y,
  54. points[2].x, points[2].y, foreground);
  55. } break;
  56. default:
  57. case NK_SYMBOL_NONE:
  58. case NK_SYMBOL_MAX: break;
  59. }
  60. }
  61. NK_LIB nk_bool
  62. nk_button_behavior(nk_flags *state, struct nk_rect r,
  63. const struct nk_input *i, enum nk_button_behavior behavior)
  64. {
  65. int ret = 0;
  66. nk_widget_state_reset(state);
  67. if (!i) return 0;
  68. if (nk_input_is_mouse_hovering_rect(i, r)) {
  69. *state = NK_WIDGET_STATE_HOVERED;
  70. if (nk_input_is_mouse_down(i, NK_BUTTON_LEFT))
  71. *state = NK_WIDGET_STATE_ACTIVE;
  72. if (nk_input_has_mouse_click_in_button_rect(i, NK_BUTTON_LEFT, r)) {
  73. ret = (behavior != NK_BUTTON_DEFAULT) ?
  74. nk_input_is_mouse_down(i, NK_BUTTON_LEFT):
  75. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  76. nk_input_is_mouse_released(i, NK_BUTTON_LEFT);
  77. #else
  78. nk_input_is_mouse_pressed(i, NK_BUTTON_LEFT);
  79. #endif
  80. }
  81. }
  82. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(i, r))
  83. *state |= NK_WIDGET_STATE_ENTERED;
  84. else if (nk_input_is_mouse_prev_hovering_rect(i, r))
  85. *state |= NK_WIDGET_STATE_LEFT;
  86. return ret;
  87. }
  88. NK_LIB const struct nk_style_item*
  89. nk_draw_button(struct nk_command_buffer *out,
  90. const struct nk_rect *bounds, nk_flags state,
  91. const struct nk_style_button *style)
  92. {
  93. const struct nk_style_item *background;
  94. if (state & NK_WIDGET_STATE_HOVER)
  95. background = &style->hover;
  96. else if (state & NK_WIDGET_STATE_ACTIVED)
  97. background = &style->active;
  98. else background = &style->normal;
  99. switch(background->type) {
  100. case NK_STYLE_ITEM_IMAGE:
  101. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  102. break;
  103. case NK_STYLE_ITEM_NINE_SLICE:
  104. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_white);
  105. break;
  106. case NK_STYLE_ITEM_COLOR:
  107. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  108. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  109. break;
  110. }
  111. return background;
  112. }
  113. NK_LIB nk_bool
  114. nk_do_button(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r,
  115. const struct nk_style_button *style, const struct nk_input *in,
  116. enum nk_button_behavior behavior, struct nk_rect *content)
  117. {
  118. struct nk_rect bounds;
  119. NK_ASSERT(style);
  120. NK_ASSERT(state);
  121. NK_ASSERT(out);
  122. if (!out || !style)
  123. return nk_false;
  124. /* calculate button content space */
  125. content->x = r.x + style->padding.x + style->border + style->rounding;
  126. content->y = r.y + style->padding.y + style->border + style->rounding;
  127. content->w = r.w - (2 * style->padding.x + style->border + style->rounding*2);
  128. content->h = r.h - (2 * style->padding.y + style->border + style->rounding*2);
  129. /* execute button behavior */
  130. bounds.x = r.x - style->touch_padding.x;
  131. bounds.y = r.y - style->touch_padding.y;
  132. bounds.w = r.w + 2 * style->touch_padding.x;
  133. bounds.h = r.h + 2 * style->touch_padding.y;
  134. return nk_button_behavior(state, bounds, in, behavior);
  135. }
  136. NK_LIB void
  137. nk_draw_button_text(struct nk_command_buffer *out,
  138. const struct nk_rect *bounds, const struct nk_rect *content, nk_flags state,
  139. const struct nk_style_button *style, const char *txt, int len,
  140. nk_flags text_alignment, const struct nk_user_font *font)
  141. {
  142. struct nk_text text;
  143. const struct nk_style_item *background;
  144. background = nk_draw_button(out, bounds, state, style);
  145. /* select correct colors/images */
  146. if (background->type == NK_STYLE_ITEM_COLOR)
  147. text.background = background->data.color;
  148. else text.background = style->text_background;
  149. if (state & NK_WIDGET_STATE_HOVER)
  150. text.text = style->text_hover;
  151. else if (state & NK_WIDGET_STATE_ACTIVED)
  152. text.text = style->text_active;
  153. else text.text = style->text_normal;
  154. text.padding = nk_vec2(0,0);
  155. nk_widget_text(out, *content, txt, len, &text, text_alignment, font);
  156. }
  157. NK_LIB nk_bool
  158. nk_do_button_text(nk_flags *state,
  159. struct nk_command_buffer *out, struct nk_rect bounds,
  160. const char *string, int len, nk_flags align, enum nk_button_behavior behavior,
  161. const struct nk_style_button *style, const struct nk_input *in,
  162. const struct nk_user_font *font)
  163. {
  164. struct nk_rect content;
  165. int ret = nk_false;
  166. NK_ASSERT(state);
  167. NK_ASSERT(style);
  168. NK_ASSERT(out);
  169. NK_ASSERT(string);
  170. NK_ASSERT(font);
  171. if (!out || !style || !font || !string)
  172. return nk_false;
  173. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  174. if (style->draw_begin) style->draw_begin(out, style->userdata);
  175. nk_draw_button_text(out, &bounds, &content, *state, style, string, len, align, font);
  176. if (style->draw_end) style->draw_end(out, style->userdata);
  177. return ret;
  178. }
  179. NK_LIB void
  180. nk_draw_button_symbol(struct nk_command_buffer *out,
  181. const struct nk_rect *bounds, const struct nk_rect *content,
  182. nk_flags state, const struct nk_style_button *style,
  183. enum nk_symbol_type type, const struct nk_user_font *font)
  184. {
  185. struct nk_color sym, bg;
  186. const struct nk_style_item *background;
  187. /* select correct colors/images */
  188. background = nk_draw_button(out, bounds, state, style);
  189. if (background->type == NK_STYLE_ITEM_COLOR)
  190. bg = background->data.color;
  191. else bg = style->text_background;
  192. if (state & NK_WIDGET_STATE_HOVER)
  193. sym = style->text_hover;
  194. else if (state & NK_WIDGET_STATE_ACTIVED)
  195. sym = style->text_active;
  196. else sym = style->text_normal;
  197. nk_draw_symbol(out, type, *content, bg, sym, 1, font);
  198. }
  199. NK_LIB nk_bool
  200. nk_do_button_symbol(nk_flags *state,
  201. struct nk_command_buffer *out, struct nk_rect bounds,
  202. enum nk_symbol_type symbol, enum nk_button_behavior behavior,
  203. const struct nk_style_button *style, const struct nk_input *in,
  204. const struct nk_user_font *font)
  205. {
  206. int ret;
  207. struct nk_rect content;
  208. NK_ASSERT(state);
  209. NK_ASSERT(style);
  210. NK_ASSERT(font);
  211. NK_ASSERT(out);
  212. if (!out || !style || !font || !state)
  213. return nk_false;
  214. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  215. if (style->draw_begin) style->draw_begin(out, style->userdata);
  216. nk_draw_button_symbol(out, &bounds, &content, *state, style, symbol, font);
  217. if (style->draw_end) style->draw_end(out, style->userdata);
  218. return ret;
  219. }
  220. NK_LIB void
  221. nk_draw_button_image(struct nk_command_buffer *out,
  222. const struct nk_rect *bounds, const struct nk_rect *content,
  223. nk_flags state, const struct nk_style_button *style, const struct nk_image *img)
  224. {
  225. nk_draw_button(out, bounds, state, style);
  226. nk_draw_image(out, *content, img, nk_white);
  227. }
  228. NK_LIB nk_bool
  229. nk_do_button_image(nk_flags *state,
  230. struct nk_command_buffer *out, struct nk_rect bounds,
  231. struct nk_image img, enum nk_button_behavior b,
  232. const struct nk_style_button *style, const struct nk_input *in)
  233. {
  234. int ret;
  235. struct nk_rect content;
  236. NK_ASSERT(state);
  237. NK_ASSERT(style);
  238. NK_ASSERT(out);
  239. if (!out || !style || !state)
  240. return nk_false;
  241. ret = nk_do_button(state, out, bounds, style, in, b, &content);
  242. content.x += style->image_padding.x;
  243. content.y += style->image_padding.y;
  244. content.w -= 2 * style->image_padding.x;
  245. content.h -= 2 * style->image_padding.y;
  246. if (style->draw_begin) style->draw_begin(out, style->userdata);
  247. nk_draw_button_image(out, &bounds, &content, *state, style, &img);
  248. if (style->draw_end) style->draw_end(out, style->userdata);
  249. return ret;
  250. }
  251. NK_LIB void
  252. nk_draw_button_text_symbol(struct nk_command_buffer *out,
  253. const struct nk_rect *bounds, const struct nk_rect *label,
  254. const struct nk_rect *symbol, nk_flags state, const struct nk_style_button *style,
  255. const char *str, int len, enum nk_symbol_type type,
  256. const struct nk_user_font *font)
  257. {
  258. struct nk_color sym;
  259. struct nk_text text;
  260. const struct nk_style_item *background;
  261. /* select correct background colors/images */
  262. background = nk_draw_button(out, bounds, state, style);
  263. if (background->type == NK_STYLE_ITEM_COLOR)
  264. text.background = background->data.color;
  265. else text.background = style->text_background;
  266. /* select correct text colors */
  267. if (state & NK_WIDGET_STATE_HOVER) {
  268. sym = style->text_hover;
  269. text.text = style->text_hover;
  270. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  271. sym = style->text_active;
  272. text.text = style->text_active;
  273. } else {
  274. sym = style->text_normal;
  275. text.text = style->text_normal;
  276. }
  277. text.padding = nk_vec2(0,0);
  278. nk_draw_symbol(out, type, *symbol, style->text_background, sym, 0, font);
  279. nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
  280. }
  281. NK_LIB nk_bool
  282. nk_do_button_text_symbol(nk_flags *state,
  283. struct nk_command_buffer *out, struct nk_rect bounds,
  284. enum nk_symbol_type symbol, const char *str, int len, nk_flags align,
  285. enum nk_button_behavior behavior, const struct nk_style_button *style,
  286. const struct nk_user_font *font, const struct nk_input *in)
  287. {
  288. int ret;
  289. struct nk_rect tri = {0,0,0,0};
  290. struct nk_rect content;
  291. NK_ASSERT(style);
  292. NK_ASSERT(out);
  293. NK_ASSERT(font);
  294. if (!out || !style || !font)
  295. return nk_false;
  296. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  297. tri.y = content.y + (content.h/2) - font->height/2;
  298. tri.w = font->height; tri.h = font->height;
  299. if (align & NK_TEXT_ALIGN_LEFT) {
  300. tri.x = (content.x + content.w) - (2 * style->padding.x + tri.w);
  301. tri.x = NK_MAX(tri.x, 0);
  302. } else tri.x = content.x + 2 * style->padding.x;
  303. /* draw button */
  304. if (style->draw_begin) style->draw_begin(out, style->userdata);
  305. nk_draw_button_text_symbol(out, &bounds, &content, &tri,
  306. *state, style, str, len, symbol, font);
  307. if (style->draw_end) style->draw_end(out, style->userdata);
  308. return ret;
  309. }
  310. NK_LIB void
  311. nk_draw_button_text_image(struct nk_command_buffer *out,
  312. const struct nk_rect *bounds, const struct nk_rect *label,
  313. const struct nk_rect *image, nk_flags state, const struct nk_style_button *style,
  314. const char *str, int len, const struct nk_user_font *font,
  315. const struct nk_image *img)
  316. {
  317. struct nk_text text;
  318. const struct nk_style_item *background;
  319. background = nk_draw_button(out, bounds, state, style);
  320. /* select correct colors */
  321. if (background->type == NK_STYLE_ITEM_COLOR)
  322. text.background = background->data.color;
  323. else text.background = style->text_background;
  324. if (state & NK_WIDGET_STATE_HOVER)
  325. text.text = style->text_hover;
  326. else if (state & NK_WIDGET_STATE_ACTIVED)
  327. text.text = style->text_active;
  328. else text.text = style->text_normal;
  329. text.padding = nk_vec2(0,0);
  330. nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
  331. nk_draw_image(out, *image, img, nk_white);
  332. }
  333. NK_LIB nk_bool
  334. nk_do_button_text_image(nk_flags *state,
  335. struct nk_command_buffer *out, struct nk_rect bounds,
  336. struct nk_image img, const char* str, int len, nk_flags align,
  337. enum nk_button_behavior behavior, const struct nk_style_button *style,
  338. const struct nk_user_font *font, const struct nk_input *in)
  339. {
  340. int ret;
  341. struct nk_rect icon;
  342. struct nk_rect content;
  343. NK_ASSERT(style);
  344. NK_ASSERT(state);
  345. NK_ASSERT(font);
  346. NK_ASSERT(out);
  347. if (!out || !font || !style || !str)
  348. return nk_false;
  349. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  350. icon.y = bounds.y + style->padding.y;
  351. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  352. if (align & NK_TEXT_ALIGN_LEFT) {
  353. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  354. icon.x = NK_MAX(icon.x, 0);
  355. } else icon.x = bounds.x + 2 * style->padding.x;
  356. icon.x += style->image_padding.x;
  357. icon.y += style->image_padding.y;
  358. icon.w -= 2 * style->image_padding.x;
  359. icon.h -= 2 * style->image_padding.y;
  360. if (style->draw_begin) style->draw_begin(out, style->userdata);
  361. nk_draw_button_text_image(out, &bounds, &content, &icon, *state, style, str, len, font, &img);
  362. if (style->draw_end) style->draw_end(out, style->userdata);
  363. return ret;
  364. }
  365. NK_API void
  366. nk_button_set_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
  367. {
  368. NK_ASSERT(ctx);
  369. if (!ctx) return;
  370. ctx->button_behavior = behavior;
  371. }
  372. NK_API nk_bool
  373. nk_button_push_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
  374. {
  375. struct nk_config_stack_button_behavior *button_stack;
  376. struct nk_config_stack_button_behavior_element *element;
  377. NK_ASSERT(ctx);
  378. if (!ctx) return 0;
  379. button_stack = &ctx->stacks.button_behaviors;
  380. NK_ASSERT(button_stack->head < (int)NK_LEN(button_stack->elements));
  381. if (button_stack->head >= (int)NK_LEN(button_stack->elements))
  382. return 0;
  383. element = &button_stack->elements[button_stack->head++];
  384. element->address = &ctx->button_behavior;
  385. element->old_value = ctx->button_behavior;
  386. ctx->button_behavior = behavior;
  387. return 1;
  388. }
  389. NK_API nk_bool
  390. nk_button_pop_behavior(struct nk_context *ctx)
  391. {
  392. struct nk_config_stack_button_behavior *button_stack;
  393. struct nk_config_stack_button_behavior_element *element;
  394. NK_ASSERT(ctx);
  395. if (!ctx) return 0;
  396. button_stack = &ctx->stacks.button_behaviors;
  397. NK_ASSERT(button_stack->head > 0);
  398. if (button_stack->head < 1)
  399. return 0;
  400. element = &button_stack->elements[--button_stack->head];
  401. *element->address = element->old_value;
  402. return 1;
  403. }
  404. NK_API nk_bool
  405. nk_button_text_styled(struct nk_context *ctx,
  406. const struct nk_style_button *style, const char *title, int len)
  407. {
  408. struct nk_window *win;
  409. struct nk_panel *layout;
  410. const struct nk_input *in;
  411. struct nk_rect bounds;
  412. enum nk_widget_layout_states state;
  413. NK_ASSERT(ctx);
  414. NK_ASSERT(style);
  415. NK_ASSERT(ctx->current);
  416. NK_ASSERT(ctx->current->layout);
  417. if (!style || !ctx || !ctx->current || !ctx->current->layout) return 0;
  418. win = ctx->current;
  419. layout = win->layout;
  420. state = nk_widget(&bounds, ctx);
  421. if (!state) return 0;
  422. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  423. return nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
  424. title, len, style->text_alignment, ctx->button_behavior,
  425. style, in, ctx->style.font);
  426. }
  427. NK_API nk_bool
  428. nk_button_text(struct nk_context *ctx, const char *title, int len)
  429. {
  430. NK_ASSERT(ctx);
  431. if (!ctx) return 0;
  432. return nk_button_text_styled(ctx, &ctx->style.button, title, len);
  433. }
  434. NK_API nk_bool nk_button_label_styled(struct nk_context *ctx,
  435. const struct nk_style_button *style, const char *title)
  436. {
  437. return nk_button_text_styled(ctx, style, title, nk_strlen(title));
  438. }
  439. NK_API nk_bool nk_button_label(struct nk_context *ctx, const char *title)
  440. {
  441. return nk_button_text(ctx, title, nk_strlen(title));
  442. }
  443. NK_API nk_bool
  444. nk_button_color(struct nk_context *ctx, struct nk_color color)
  445. {
  446. struct nk_window *win;
  447. struct nk_panel *layout;
  448. const struct nk_input *in;
  449. struct nk_style_button button;
  450. int ret = 0;
  451. struct nk_rect bounds;
  452. struct nk_rect content;
  453. enum nk_widget_layout_states state;
  454. NK_ASSERT(ctx);
  455. NK_ASSERT(ctx->current);
  456. NK_ASSERT(ctx->current->layout);
  457. if (!ctx || !ctx->current || !ctx->current->layout)
  458. return 0;
  459. win = ctx->current;
  460. layout = win->layout;
  461. state = nk_widget(&bounds, ctx);
  462. if (!state) return 0;
  463. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  464. button = ctx->style.button;
  465. button.normal = nk_style_item_color(color);
  466. button.hover = nk_style_item_color(color);
  467. button.active = nk_style_item_color(color);
  468. ret = nk_do_button(&ctx->last_widget_state, &win->buffer, bounds,
  469. &button, in, ctx->button_behavior, &content);
  470. nk_draw_button(&win->buffer, &bounds, ctx->last_widget_state, &button);
  471. return ret;
  472. }
  473. NK_API nk_bool
  474. nk_button_symbol_styled(struct nk_context *ctx,
  475. const struct nk_style_button *style, enum nk_symbol_type symbol)
  476. {
  477. struct nk_window *win;
  478. struct nk_panel *layout;
  479. const struct nk_input *in;
  480. struct nk_rect bounds;
  481. enum nk_widget_layout_states state;
  482. NK_ASSERT(ctx);
  483. NK_ASSERT(ctx->current);
  484. NK_ASSERT(ctx->current->layout);
  485. if (!ctx || !ctx->current || !ctx->current->layout)
  486. return 0;
  487. win = ctx->current;
  488. layout = win->layout;
  489. state = nk_widget(&bounds, ctx);
  490. if (!state) return 0;
  491. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  492. return nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  493. symbol, ctx->button_behavior, style, in, ctx->style.font);
  494. }
  495. NK_API nk_bool
  496. nk_button_symbol(struct nk_context *ctx, enum nk_symbol_type symbol)
  497. {
  498. NK_ASSERT(ctx);
  499. if (!ctx) return 0;
  500. return nk_button_symbol_styled(ctx, &ctx->style.button, symbol);
  501. }
  502. NK_API nk_bool
  503. nk_button_image_styled(struct nk_context *ctx, const struct nk_style_button *style,
  504. struct nk_image img)
  505. {
  506. struct nk_window *win;
  507. struct nk_panel *layout;
  508. const struct nk_input *in;
  509. struct nk_rect bounds;
  510. enum nk_widget_layout_states state;
  511. NK_ASSERT(ctx);
  512. NK_ASSERT(ctx->current);
  513. NK_ASSERT(ctx->current->layout);
  514. if (!ctx || !ctx->current || !ctx->current->layout)
  515. return 0;
  516. win = ctx->current;
  517. layout = win->layout;
  518. state = nk_widget(&bounds, ctx);
  519. if (!state) return 0;
  520. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  521. return nk_do_button_image(&ctx->last_widget_state, &win->buffer, bounds,
  522. img, ctx->button_behavior, style, in);
  523. }
  524. NK_API nk_bool
  525. nk_button_image(struct nk_context *ctx, struct nk_image img)
  526. {
  527. NK_ASSERT(ctx);
  528. if (!ctx) return 0;
  529. return nk_button_image_styled(ctx, &ctx->style.button, img);
  530. }
  531. NK_API nk_bool
  532. nk_button_symbol_text_styled(struct nk_context *ctx,
  533. const struct nk_style_button *style, enum nk_symbol_type symbol,
  534. const char *text, int len, nk_flags align)
  535. {
  536. struct nk_window *win;
  537. struct nk_panel *layout;
  538. const struct nk_input *in;
  539. struct nk_rect bounds;
  540. enum nk_widget_layout_states state;
  541. NK_ASSERT(ctx);
  542. NK_ASSERT(ctx->current);
  543. NK_ASSERT(ctx->current->layout);
  544. if (!ctx || !ctx->current || !ctx->current->layout)
  545. return 0;
  546. win = ctx->current;
  547. layout = win->layout;
  548. state = nk_widget(&bounds, ctx);
  549. if (!state) return 0;
  550. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  551. return nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  552. symbol, text, len, align, ctx->button_behavior,
  553. style, ctx->style.font, in);
  554. }
  555. NK_API nk_bool
  556. nk_button_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
  557. const char* text, int len, nk_flags align)
  558. {
  559. NK_ASSERT(ctx);
  560. if (!ctx) return 0;
  561. return nk_button_symbol_text_styled(ctx, &ctx->style.button, symbol, text, len, align);
  562. }
  563. NK_API nk_bool nk_button_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
  564. const char *label, nk_flags align)
  565. {
  566. return nk_button_symbol_text(ctx, symbol, label, nk_strlen(label), align);
  567. }
  568. NK_API nk_bool nk_button_symbol_label_styled(struct nk_context *ctx,
  569. const struct nk_style_button *style, enum nk_symbol_type symbol,
  570. const char *title, nk_flags align)
  571. {
  572. return nk_button_symbol_text_styled(ctx, style, symbol, title, nk_strlen(title), align);
  573. }
  574. NK_API nk_bool
  575. nk_button_image_text_styled(struct nk_context *ctx,
  576. const struct nk_style_button *style, struct nk_image img, const char *text,
  577. int len, nk_flags align)
  578. {
  579. struct nk_window *win;
  580. struct nk_panel *layout;
  581. const struct nk_input *in;
  582. struct nk_rect bounds;
  583. enum nk_widget_layout_states state;
  584. NK_ASSERT(ctx);
  585. NK_ASSERT(ctx->current);
  586. NK_ASSERT(ctx->current->layout);
  587. if (!ctx || !ctx->current || !ctx->current->layout)
  588. return 0;
  589. win = ctx->current;
  590. layout = win->layout;
  591. state = nk_widget(&bounds, ctx);
  592. if (!state) return 0;
  593. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  594. return nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
  595. bounds, img, text, len, align, ctx->button_behavior,
  596. style, ctx->style.font, in);
  597. }
  598. NK_API nk_bool
  599. nk_button_image_text(struct nk_context *ctx, struct nk_image img,
  600. const char *text, int len, nk_flags align)
  601. {
  602. return nk_button_image_text_styled(ctx, &ctx->style.button,img, text, len, align);
  603. }
  604. NK_API nk_bool nk_button_image_label(struct nk_context *ctx, struct nk_image img,
  605. const char *label, nk_flags align)
  606. {
  607. return nk_button_image_text(ctx, img, label, nk_strlen(label), align);
  608. }
  609. NK_API nk_bool nk_button_image_label_styled(struct nk_context *ctx,
  610. const struct nk_style_button *style, struct nk_image img,
  611. const char *label, nk_flags text_alignment)
  612. {
  613. return nk_button_image_text_styled(ctx, style, img, label, nk_strlen(label), text_alignment);
  614. }