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 NK_ASSERT(ctx);
13 return nk_tooltip_begin_offset(ctx, width, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
14}
15
16NK_API nk_bool
17nk_tooltip_begin_offset(struct nk_context *ctx, float width, enum nk_tooltip_pos position, struct nk_vec2 offset)
18{
19 int x,y,w,h;
20 struct nk_window *win;
21 const struct nk_input *in;
22 struct nk_rect bounds;
23 int ret;
24
25 NK_ASSERT(ctx);
26 NK_ASSERT(ctx->current);
27 NK_ASSERT(ctx->current->layout);
28 if (!ctx || !ctx->current || !ctx->current->layout)
29 return 0;
30
31 /* make sure that no nonblocking popup is currently active */
32 win = ctx->current;
33 in = &ctx->input;
34 if (win->popup.win && ((int)win->popup.type & (int)NK_PANEL_SET_NONBLOCK))
35 return 0;
36
37 w = nk_iceilf(width);
38 h = NK_MAX(win->layout->row.min_height, ctx->style.font->height+2*ctx->style.window.padding.y);
39
40 /* Default origin is top left, plus user offset */
41 x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x + offset.x;
42 y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y + offset.y;
43
44 /* Adjust origin based on enum */
45 switch (position) {
46 case NK_TOP_LEFT:
47 /* no change */
48 break;
49 case NK_TOP_CENTER:
50 x -= w/2;
51 break;
52 case NK_TOP_RIGHT:
53 x -= w;
54 break;
55
56 case NK_MIDDLE_LEFT:
57 y -= h/2;
58 break;
59 case NK_MIDDLE_CENTER:
60 x -= w/2;
61 y -= h/2;
62 break;
63 case NK_MIDDLE_RIGHT:
64 x -= w;
65 y -= h/2;
66 break;
67
68 case NK_BOTTOM_LEFT:
69 y -= h;
70 break;
71 case NK_BOTTOM_CENTER:
72 x -= w/2;
73 y -= h;
74 break;
75 case NK_BOTTOM_RIGHT:
76 x -= w;
77 y -= h;
78 break;
79 default:
80 NK_ASSERT(0 && "Invalid tooltip position");
81 }
82
83 bounds.x = (float)x;
84 bounds.y = (float)y;
85 bounds.w = (float)w;
86 bounds.h = (float)nk_iceilf(nk_null_rect.h);
87
88 ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
89 "__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);
90 if (ret) win->layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
91 win->popup.type = NK_PANEL_TOOLTIP;
92 ctx->current->layout->type = NK_PANEL_TOOLTIP;
93 return ret;
94}
95
96NK_API void
97nk_tooltip_end(struct nk_context *ctx)
98{
99 NK_ASSERT(ctx);
100 NK_ASSERT(ctx->current);
101 if (!ctx || !ctx->current) return;
102 ctx->current->seq--;
103 nk_popup_close(ctx);
104 nk_popup_end(ctx);
105}
106
107NK_API void
108nk_tooltip_offset(struct nk_context *ctx, const char *text, enum nk_tooltip_pos position, struct nk_vec2 offset)
109{
110 const struct nk_style *style;
111 struct nk_vec2 padding;
112
113 int text_len;
114 float text_width;
115 float text_height;
116
117 NK_ASSERT(ctx);
118 NK_ASSERT(ctx->current);
119 NK_ASSERT(ctx->current->layout);
120 NK_ASSERT(text);
121 if (!ctx || !ctx->current || !ctx->current->layout || !text)
122 return;
123
124 /* fetch configuration data */
125 style = &ctx->style;
126 padding = style->window.padding;
127
128 /* calculate size of the text and tooltip */
129 text_len = nk_strlen(text);
130 text_width = style->font->width(style->font->userdata,
131 style->font->height, text, text_len);
132 text_width += (4 * padding.x);
133 text_height = (style->font->height + 2 * padding.y);
134
135 /* execute tooltip and fill with text */
136 if (nk_tooltip_begin_offset(ctx, (float)text_width, position, offset)) {
137 nk_layout_row_dynamic(ctx, (float)text_height, 1);
138 nk_text(ctx, text, text_len, NK_TEXT_LEFT);
139 nk_tooltip_end(ctx);
140 }
141}
142
143NK_API void
144nk_tooltip(struct nk_context *ctx, const char *text)
145{
146 NK_ASSERT(ctx);
147 nk_tooltip_offset(ctx, text, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
148}
149#ifdef NK_INCLUDE_STANDARD_VARARGS
150NK_API void
151nk_tooltipf_offset(struct nk_context *ctx, enum nk_tooltip_pos position, struct nk_vec2 offset, const char *fmt, ...)
152{
153 va_list args;
154 va_start(args, fmt);
155 nk_tooltipfv_offset(ctx, position, offset, fmt, args);
156 va_end(args);
157}
158NK_API void
159nk_tooltipf(struct nk_context *ctx, const char *fmt, ...)
160{
161 va_list args;
162 va_start(args, fmt);
163 nk_tooltipfv(ctx, fmt, args);
164 va_end(args);
165}
166NK_API void
167nk_tooltipfv_offset(struct nk_context *ctx, enum nk_tooltip_pos position, struct nk_vec2 offset, const char *fmt, va_list args)
168{
169 char buf[256];
170 nk_strfmt(buf, NK_LEN(buf), fmt, args);
171 nk_tooltip_offset(ctx, buf, position, offset);
172}
173NK_API void
174nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
175{
176 char buf[256];
177 nk_strfmt(buf, NK_LEN(buf), fmt, args);
178 nk_tooltip(ctx, buf);
179}
180#endif
181
182
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:5537
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:4050
float height
!< user provided font handle
Definition nuklear.h:4049