Nuklear
This is a minimal-state, immediate-mode graphical user interface toolkit written in ANSI C and licensed under public domain. It was designed as a simple embeddable user interface for application and does not have any dependencies, a default render backend or OS window/input handling but instead provides a highly modular, library-based approach, with simple input state for input and draw commands describing primitive shapes as output. So instead of providing a layered library that tries to abstract over a number of platform and render backends, it focuses only on the actual UI.
 
Loading...
Searching...
No Matches
nuklear_tooltip.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ===============================================================
5 *
6 * TOOLTIP
7 *
8 * ===============================================================*/
9NK_API nk_bool
10nk_tooltip_begin(struct nk_context *ctx, float width)
11{
12 int x,y,w,h;
13 struct nk_window *win;
14 const struct nk_input *in;
15 struct nk_rect bounds;
16 int ret;
17
18 NK_ASSERT(ctx);
19 NK_ASSERT(ctx->current);
20 NK_ASSERT(ctx->current->layout);
21 if (!ctx || !ctx->current || !ctx->current->layout)
22 return 0;
23
24 /* make sure that no nonblocking popup is currently active */
25 win = ctx->current;
26 in = &ctx->input;
27 if (win->popup.win && ((int)win->popup.type & (int)NK_PANEL_SET_NONBLOCK))
28 return 0;
29
30 w = nk_iceilf(width);
31 h = nk_iceilf(nk_null_rect.h);
32 x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x;
33 y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y;
34
35 bounds.x = (float)x;
36 bounds.y = (float)y;
37 bounds.w = (float)w;
38 bounds.h = (float)h;
39
40 ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
41 "__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);
42 if (ret) win->layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
43 win->popup.type = NK_PANEL_TOOLTIP;
44 ctx->current->layout->type = NK_PANEL_TOOLTIP;
45 return ret;
46}
47
48NK_API void
49nk_tooltip_end(struct nk_context *ctx)
50{
51 NK_ASSERT(ctx);
52 NK_ASSERT(ctx->current);
53 if (!ctx || !ctx->current) return;
54 ctx->current->seq--;
55 nk_popup_close(ctx);
56 nk_popup_end(ctx);
57}
58NK_API void
59nk_tooltip(struct nk_context *ctx, const char *text)
60{
61 const struct nk_style *style;
62 struct nk_vec2 padding;
63
64 int text_len;
65 float text_width;
66 float text_height;
67
68 NK_ASSERT(ctx);
69 NK_ASSERT(ctx->current);
70 NK_ASSERT(ctx->current->layout);
71 NK_ASSERT(text);
72 if (!ctx || !ctx->current || !ctx->current->layout || !text)
73 return;
74
75 /* fetch configuration data */
76 style = &ctx->style;
77 padding = style->window.padding;
78
79 /* calculate size of the text and tooltip */
80 text_len = nk_strlen(text);
81 text_width = style->font->width(style->font->userdata,
82 style->font->height, text, text_len);
83 text_width += (4 * padding.x);
84 text_height = (style->font->height + 2 * padding.y);
85
86 /* execute tooltip and fill with text */
87 if (nk_tooltip_begin(ctx, (float)text_width)) {
88 nk_layout_row_dynamic(ctx, (float)text_height, 1);
89 nk_text(ctx, text, text_len, NK_TEXT_LEFT);
90 nk_tooltip_end(ctx);
91 }
92}
93#ifdef NK_INCLUDE_STANDARD_VARARGS
94NK_API void
95nk_tooltipf(struct nk_context *ctx, const char *fmt, ...)
96{
97 va_list args;
98 va_start(args, fmt);
99 nk_tooltipfv(ctx, fmt, args);
100 va_end(args);
101}
102NK_API void
103nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
104{
105 char buf[256];
106 nk_strfmt(buf, NK_LEN(buf), fmt, args);
107 nk_tooltip(ctx, buf);
108}
109#endif
110
111
main API and documentation file
@ NK_WINDOW_ROM
sets window widgets into a read only mode and does not allow input changes
Definition nuklear.h:5492
NK_API void nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols)
Sets current row layout to share horizontal space between @cols number of widgets evenly.
nk_text_width_f width
!< max height of the font
Definition nuklear.h:4009
float height
!< user provided font handle
Definition nuklear.h:4008