2#include "nuklear_internal.h"
11 struct
nk_rect anchor, enum nk_popup_fit fit, float known_h,
23 if (!ctx)
return body;
24 d = ctx->display_bounds;
25 if (d.w <= 0 || d.h <= 0)
return body;
36 need_h = (known_h > 0.0f) ? known_h : req_h;
37 overlap = (anchor.y + anchor.h) - body.y;
42 if (fit == NK_POPUP_FIT_FLIP) {
43 if (body.x + body.w > d_right)
44 body.x = anchor.x + anchor.w - body.w;
45 }
else if (fit == NK_POPUP_FIT_TOOLTIP) {
46 if (body.x + body.w > d_right) {
47 float flipped = anchor.x - body.w;
52 if (body.x + body.w > d_right)
53 body.x = d_right - body.w;
57 space_below = d_bottom - body.y;
58 space_above = anchor.y - d.y;
59 if (space_below < 0.0f) space_below = 0.0f;
60 if (space_above < 0.0f) space_above = 0.0f;
62 if (fit == NK_POPUP_FIT_FLIP) {
69 if (stay_up && space_above > 0.0f)
71 else if (known_h > 0.0f && known_h > space_below) {
72 if (known_h <= space_above)
74 else if (space_above > space_below)
79 if (use_h > space_above) use_h = space_above;
80 if (known_h < use_h) use_h = known_h;
81 if (use_h < 1.0f) use_h = 1.0f;
82 body.y = anchor.y - use_h;
83 if (overlap > 0.0f) body.y += overlap;
85 }
else if (known_h > space_below) {
86 body.h = (space_below > 1.0f) ? space_below : 1.0f;
88 }
else if (fit == NK_POPUP_FIT_SLIDE) {
93 if (known_h > 0.0f && known_h > space_below) {
94 body.y = d_bottom - known_h;
97 if (body.y + known_h > d_bottom)
98 body.h = d_bottom - body.y;
99 else body.h = known_h;
100 if (body.h < 1.0f) body.h = 1.0f;
104 if (body.y + need_h > d_bottom) {
105 float flipped = anchor.y - need_h;
109 if (body.y + need_h > d_bottom)
110 body.y = d_bottom - need_h;
117nk_popup_begin(
struct nk_context *ctx,
enum nk_popup_type type,
118 const char *title, nk_flags flags,
struct nk_rect rect)
130 NK_ASSERT(ctx->current);
131 NK_ASSERT(ctx->current->layout);
132 if (!ctx || !ctx->current || !ctx->current->layout)
137 NK_ASSERT(!((
int)panel->type & (
int)NK_PANEL_SET_POPUP) &&
"popups are not allowed to have popups");
139 title_len = (int)nk_strlen(title);
140 title_hash = nk_murmur_hash(title, (
int)title_len, NK_PANEL_POPUP);
142 popup = win->popup.win;
144 popup = (
struct nk_window*)nk_create_window(ctx);
146 win->popup.win = popup;
147 win->popup.active = 0;
148 win->popup.type = NK_PANEL_POPUP;
152 if (win->popup.name != title_hash) {
153 if (!win->popup.active) {
154 nk_zero(popup,
sizeof(*popup));
155 win->popup.name = title_hash;
156 win->popup.active = 1;
157 win->popup.type = NK_PANEL_POPUP;
162 ctx->current = popup;
163 rect.x += win->layout->clip.x;
164 rect.y += win->layout->clip.y;
168 popup->bounds = rect;
169 popup->seq = ctx->seq;
170 popup->layout = (
struct nk_panel*)nk_create_panel(ctx);
171 popup->flags = flags;
172 popup->flags |= NK_WINDOW_BORDER;
173 if (type == NK_POPUP_DYNAMIC)
174 popup->flags |= NK_WINDOW_DYNAMIC;
176 popup->buffer = win->buffer;
177 nk_start_popup(ctx, win);
179 nk_push_scissor(&popup->buffer, nk_null_rect);
181 if (nk_panel_begin(ctx, title, NK_PANEL_POPUP)) {
186 root->flags |= NK_WINDOW_ROM;
187 root->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
190 win->popup.active = 1;
191 popup->layout->offset_x = &popup->scrollbar.x;
192 popup->layout->offset_y = &popup->scrollbar.y;
193 popup->layout->parent = win->layout;
200 root->flags |= NK_WINDOW_REMOVE_ROM;
203 win->popup.buf.active = 0;
204 win->popup.active = 0;
207 nk_free_panel(ctx, popup->layout);
215 enum nk_panel_type panel_type)
220 int is_active = nk_true;
223 NK_ASSERT(ctx->current);
224 NK_ASSERT(ctx->current->layout);
225 if (!ctx || !ctx->current || !ctx->current->layout)
231 NK_ASSERT(!((
int)panel->type & (
int)NK_PANEL_SET_POPUP));
233 popup = win->popup.win;
236 popup = (
struct nk_window*)nk_create_window(ctx);
238 win->popup.win = popup;
239 win->popup.type = panel_type;
240 nk_command_buffer_init(&popup->buffer, &ctx->memory, NK_CLIPPING_ON);
243 int pressed, in_body, in_header;
244#ifdef NK_BUTTON_TRIGGER_ON_RELEASE
245 pressed = nk_input_is_mouse_released(&ctx->input, NK_BUTTON_LEFT);
247 pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
249 in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
250 in_header = nk_input_is_mouse_hovering_rect(&ctx->input, header);
251 if (pressed && (!in_body || in_header))
252 is_active = nk_false;
254 win->popup.header = header;
258 struct nk_panel *root = win->layout;
260 root->flags |= NK_WINDOW_REMOVE_ROM;
263 win->popup.buf.active = 0;
268 popup->bounds = body;
270 popup->layout = (
struct nk_panel*)nk_create_panel(ctx);
271 popup->flags = flags;
272 popup->flags |= NK_WINDOW_BORDER;
273 popup->flags |= NK_WINDOW_DYNAMIC;
274 popup->seq = ctx->seq;
275 win->popup.active = 1;
276 NK_ASSERT(popup->layout);
278 nk_start_popup(ctx, win);
279 popup->buffer = win->buffer;
280 nk_push_scissor(&popup->buffer, nk_null_rect);
281 ctx->current = popup;
283 nk_panel_begin(ctx, 0, panel_type);
284 win->buffer = popup->buffer;
285 popup->layout->parent = win->layout;
286 popup->layout->offset_x = &popup->scrollbar.x;
287 popup->layout->offset_y = &popup->scrollbar.y;
293 root->flags |= NK_WINDOW_ROM;
303 if (!ctx || !ctx->current)
return;
305 popup = ctx->current;
306 NK_ASSERT(popup->parent);
307 NK_ASSERT((
int)popup->layout->type & (
int)NK_PANEL_SET_POPUP);
308 popup->flags |= NK_WINDOW_HIDDEN;
317 NK_ASSERT(ctx->current);
318 NK_ASSERT(ctx->current->layout);
319 if (!ctx || !ctx->current || !ctx->current->layout)
322 popup = ctx->current;
323 if (!popup->parent)
return;
325 if (popup->flags & NK_WINDOW_HIDDEN) {
329 root->flags |= NK_WINDOW_REMOVE_ROM;
332 win->popup.active = 0;
334 nk_push_scissor(&popup->buffer, nk_null_rect);
337 win->buffer = popup->buffer;
338 nk_finish_popup(ctx, win);
340 nk_push_scissor(&win->buffer, win->layout->clip);
343nk_popup_get_scroll(
const struct nk_context *ctx, nk_uint *offset_x, nk_uint *offset_y)
348 NK_ASSERT(ctx->current);
349 NK_ASSERT(ctx->current->layout);
350 if (!ctx || !ctx->current || !ctx->current->layout)
353 popup = ctx->current;
355 *offset_x = popup->scrollbar.x;
357 *offset_y = popup->scrollbar.y;
360nk_popup_set_scroll(
struct nk_context *ctx, nk_uint offset_x, nk_uint offset_y)
365 NK_ASSERT(ctx->current);
366 NK_ASSERT(ctx->current->layout);
367 if (!ctx || !ctx->current || !ctx->current->layout)
370 popup = ctx->current;
371 popup->scrollbar.x = offset_x;
372 popup->scrollbar.y = offset_y;
nk_size allocated
!< growing factor for dynamic memory management