Nuklear
Minimal-state, immediate-mode graphical user interface toolkit written in ANSI C.
Loading...
Searching...
No Matches
nuklear_combo.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ==============================================================
5 *
6 * COMBO
7 *
8 * ===============================================================*/
9/* Outer popup height that fits `count` rows: each row is item_height plus
10 * trailing spacing, plus combo top padding and the border nk_panel_begin
11 * subtracts. Combos always get NK_WINDOW_BORDER from nk_nonblock_begin. */
12NK_INTERN float
13nk_combo_calc_max_height(const struct nk_context *ctx, int count, int item_height)
14{
15 struct nk_vec2 spacing;
16 struct nk_vec2 padding;
17
18 spacing = ctx->style.window.spacing;
19 padding = nk_panel_get_padding(&ctx->style, NK_PANEL_COMBO);
20 return (float)count * (float)item_height
21 + (float)count * spacing.y
22 + padding.y
23 + 2.0f * ctx->style.window.combo_border;
24}
25NK_INTERN nk_bool
26nk_combo_begin(struct nk_context *ctx, struct nk_window *win,
27 struct nk_vec2 size, nk_bool is_clicked, struct nk_rect header)
28{
29 struct nk_window *popup;
30 int is_open = 0;
31 int is_active = 0;
32 struct nk_rect body;
33 nk_hash hash;
34
35 NK_ASSERT(ctx);
36 NK_ASSERT(ctx->current);
37 NK_ASSERT(ctx->current->layout);
38 if (!ctx || !ctx->current || !ctx->current->layout)
39 return 0;
40
41 popup = win->popup.win;
42 body.x = header.x;
43 body.w = size.x;
44 body.y = header.y + header.h-ctx->style.window.combo_border;
45 body.h = size.y;
46
47 hash = win->popup.combo_count++;
48 is_open = (popup) ? nk_true:nk_false;
49 is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_COMBO);
50 if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
51 (!is_open && !is_active && !is_clicked)) return 0;
52 {nk_flags flags = NK_WINDOW_NO_SCROLLBAR;
53 float known_h = (is_active) ? win->popup.last_h : 0;
54 body = nk_fit_popup_rect(ctx, body, header, NK_POPUP_FIT_FLIP, known_h,
55 is_active && win->popup.pinned_up);
56 win->popup.pinned_up = (body.y < header.y);
57 /* scrollbar only if actual content does not fit in the fitted panel
58 * (caller size.y is a max; flipping to last_h must not force a bar) */
59 if (known_h > 0.0f && body.h + 0.5f < known_h)
60 flags = 0;
61 if (!nk_nonblock_begin(ctx, flags, body,
62 (is_clicked && is_open)?nk_rect(0,0,0,0):header, NK_PANEL_COMBO)) return 0;}
63
64 win->popup.type = NK_PANEL_COMBO;
65 win->popup.name = hash;
66 return 1;
67}
68NK_API nk_bool
69nk_combo_begin_text(struct nk_context *ctx, const char *selected, int len,
70 struct nk_vec2 size)
71{
72 const struct nk_input *in;
73 struct nk_window *win;
74 struct nk_style *style;
75
76 enum nk_widget_layout_states s;
77 int is_clicked = nk_false;
78 struct nk_rect header;
79 const struct nk_style_item *background;
80 struct nk_text text;
81
82 NK_ASSERT(ctx);
83 NK_ASSERT(selected);
84 NK_ASSERT(ctx->current);
85 NK_ASSERT(ctx->current->layout);
86 if (!ctx || !ctx->current || !ctx->current->layout || !selected)
87 return 0;
88
89 win = ctx->current;
90 style = &ctx->style;
91 s = nk_widget(&header, ctx);
92 if (s == NK_WIDGET_INVALID)
93 return 0;
94
95 in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
96 if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
97 is_clicked = nk_true;
98
99 /* draw combo box header background and border */
100 if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
101 background = &style->combo.active;
102 text.text = style->combo.label_active;
103 } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
104 background = &style->combo.hover;
105 text.text = style->combo.label_hover;
106 } else {
107 background = &style->combo.normal;
108 text.text = style->combo.label_normal;
109 }
110
111 text.text = nk_rgb_factor(text.text, style->combo.color_factor);
112
113 switch(background->type) {
114 case NK_STYLE_ITEM_IMAGE:
115 text.background = nk_rgba(0, 0, 0, 0);
116 nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
117 break;
118 case NK_STYLE_ITEM_NINE_SLICE:
119 text.background = nk_rgba(0, 0, 0, 0);
120 nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
121 break;
122 case NK_STYLE_ITEM_COLOR:
123 text.background = background->data.color;
124 nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
125 nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
126 break;
127 }
128 {
129 /* print currently selected text item */
130 struct nk_rect label;
131 struct nk_rect button;
132 struct nk_rect content;
133 int draw_button_symbol;
134
135 enum nk_symbol_type sym;
136 if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
137 sym = style->combo.sym_hover;
138 else if (is_clicked)
139 sym = style->combo.sym_active;
140 else
141 sym = style->combo.sym_normal;
142
143 /* represents whether or not the combo's button symbol should be drawn */
144 draw_button_symbol = sym != NK_SYMBOL_NONE;
145
146 /* calculate button */
147 button.w = header.h - 2 * style->combo.button_padding.y;
148 button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
149 button.y = header.y + style->combo.button_padding.y;
150 button.h = button.w;
151
152 content.x = button.x + style->combo.button.padding.x;
153 content.y = button.y + style->combo.button.padding.y;
154 content.w = button.w - 2 * style->combo.button.padding.x;
155 content.h = button.h - 2 * style->combo.button.padding.y;
156
157 /* draw selected label */
158 text.padding = nk_vec2(0,0);
159 label.x = header.x + style->combo.content_padding.x;
160 label.y = header.y + style->combo.content_padding.y;
161 label.h = header.h - 2 * style->combo.content_padding.y;
162 if (draw_button_symbol)
163 label.w = button.x - (style->combo.content_padding.x + style->combo.spacing.x) - label.x;
164 else
165 label.w = header.w - 2 * style->combo.content_padding.x;
166 nk_widget_text(&win->buffer, label, selected, len, &text,
167 NK_TEXT_LEFT, ctx->style.font);
168
169 /* draw open/close button */
170 if (draw_button_symbol)
171 nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
172 &ctx->style.combo.button, sym, style->font);
173 }
174 return nk_combo_begin(ctx, win, size, is_clicked, header);
175}
176NK_API nk_bool
177nk_combo_begin_label(struct nk_context *ctx, const char *selected, struct nk_vec2 size)
178{
179 return nk_combo_begin_text(ctx, selected, nk_strlen(selected), size);
180}
181NK_API nk_bool
182nk_combo_begin_color(struct nk_context *ctx, struct nk_color color, struct nk_vec2 size)
183{
184 struct nk_window *win;
185 struct nk_style *style;
186 const struct nk_input *in;
187
188 struct nk_rect header;
189 int is_clicked = nk_false;
190 enum nk_widget_layout_states s;
191 const struct nk_style_item *background;
192
193 NK_ASSERT(ctx);
194 NK_ASSERT(ctx->current);
195 NK_ASSERT(ctx->current->layout);
196 if (!ctx || !ctx->current || !ctx->current->layout)
197 return 0;
198
199 win = ctx->current;
200 style = &ctx->style;
201 s = nk_widget(&header, ctx);
202 if (s == NK_WIDGET_INVALID)
203 return 0;
204
205 in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
206 if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
207 is_clicked = nk_true;
208
209 /* draw combo box header background and border */
210 if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
211 background = &style->combo.active;
212 else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
213 background = &style->combo.hover;
214 else background = &style->combo.normal;
215
216 switch(background->type) {
217 case NK_STYLE_ITEM_IMAGE:
218 nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
219 break;
220 case NK_STYLE_ITEM_NINE_SLICE:
221 nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
222 break;
223 case NK_STYLE_ITEM_COLOR:
224 nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
225 nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
226 break;
227 }
228 {
229 struct nk_rect content;
230 struct nk_rect button;
231 struct nk_rect bounds;
232 int draw_button_symbol;
233
234 enum nk_symbol_type sym;
235 if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
236 sym = style->combo.sym_hover;
237 else if (is_clicked)
238 sym = style->combo.sym_active;
239 else sym = style->combo.sym_normal;
240
241 /* represents whether or not the combo's button symbol should be drawn */
242 draw_button_symbol = sym != NK_SYMBOL_NONE;
243
244 /* calculate button */
245 button.w = header.h - 2 * style->combo.button_padding.y;
246 button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
247 button.y = header.y + style->combo.button_padding.y;
248 button.h = button.w;
249
250 content.x = button.x + style->combo.button.padding.x;
251 content.y = button.y + style->combo.button.padding.y;
252 content.w = button.w - 2 * style->combo.button.padding.x;
253 content.h = button.h - 2 * style->combo.button.padding.y;
254
255 /* draw color */
256 bounds.h = header.h - 4 * style->combo.content_padding.y;
257 bounds.y = header.y + 2 * style->combo.content_padding.y;
258 bounds.x = header.x + 2 * style->combo.content_padding.x;
259 if (draw_button_symbol)
260 bounds.w = (button.x - (style->combo.content_padding.x + style->combo.spacing.x)) - bounds.x;
261 else
262 bounds.w = header.w - 4 * style->combo.content_padding.x;
263 nk_fill_rect(&win->buffer, bounds, 0, nk_rgb_factor(color, style->combo.color_factor));
264
265 /* draw open/close button */
266 if (draw_button_symbol)
267 nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
268 &ctx->style.combo.button, sym, style->font);
269 }
270 return nk_combo_begin(ctx, win, size, is_clicked, header);
271}
272NK_API nk_bool
273nk_combo_begin_symbol(struct nk_context *ctx, enum nk_symbol_type symbol, struct nk_vec2 size)
274{
275 struct nk_window *win;
276 struct nk_style *style;
277 const struct nk_input *in;
278
279 struct nk_rect header;
280 int is_clicked = nk_false;
281 enum nk_widget_layout_states s;
282 const struct nk_style_item *background;
283 struct nk_color sym_background;
284 struct nk_color symbol_color;
285
286 NK_ASSERT(ctx);
287 NK_ASSERT(ctx->current);
288 NK_ASSERT(ctx->current->layout);
289 if (!ctx || !ctx->current || !ctx->current->layout)
290 return 0;
291
292 win = ctx->current;
293 style = &ctx->style;
294 s = nk_widget(&header, ctx);
295 if (s == NK_WIDGET_INVALID)
296 return 0;
297
298 in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
299 if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
300 is_clicked = nk_true;
301
302 /* draw combo box header background and border */
303 if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
304 background = &style->combo.active;
305 symbol_color = style->combo.symbol_active;
306 } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
307 background = &style->combo.hover;
308 symbol_color = style->combo.symbol_hover;
309 } else {
310 background = &style->combo.normal;
311 symbol_color = style->combo.symbol_hover;
312 }
313
314 symbol_color = nk_rgb_factor(symbol_color, style->combo.color_factor);
315
316 switch(background->type) {
317 case NK_STYLE_ITEM_IMAGE:
318 sym_background = nk_rgba(0, 0, 0, 0);
319 nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
320 break;
321 case NK_STYLE_ITEM_NINE_SLICE:
322 sym_background = nk_rgba(0, 0, 0, 0);
323 nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
324 break;
325 case NK_STYLE_ITEM_COLOR:
326 sym_background = background->data.color;
327 nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
328 nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
329 break;
330 }
331 {
332 struct nk_rect bounds = {0,0,0,0};
333 struct nk_rect content;
334 struct nk_rect button;
335
336 enum nk_symbol_type sym;
337 if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
338 sym = style->combo.sym_hover;
339 else if (is_clicked)
340 sym = style->combo.sym_active;
341 else sym = style->combo.sym_normal;
342
343 /* calculate button */
344 button.w = header.h - 2 * style->combo.button_padding.y;
345 button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
346 button.y = header.y + style->combo.button_padding.y;
347 button.h = button.w;
348
349 content.x = button.x + style->combo.button.padding.x;
350 content.y = button.y + style->combo.button.padding.y;
351 content.w = button.w - 2 * style->combo.button.padding.x;
352 content.h = button.h - 2 * style->combo.button.padding.y;
353
354 /* draw symbol */
355 bounds.h = header.h - 2 * style->combo.content_padding.y;
356 bounds.y = header.y + style->combo.content_padding.y;
357 bounds.x = header.x + style->combo.content_padding.x;
358 bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
359 nk_draw_symbol(&win->buffer, symbol, bounds, sym_background, symbol_color,
360 style->combo.border, style->font);
361
362 /* draw open/close button */
363 nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
364 &ctx->style.combo.button, sym, style->font);
365 }
366 return nk_combo_begin(ctx, win, size, is_clicked, header);
367}
368NK_API nk_bool
369nk_combo_begin_symbol_text(struct nk_context *ctx, const char *selected, int len,
370 enum nk_symbol_type symbol, struct nk_vec2 size)
371{
372 struct nk_window *win;
373 struct nk_style *style;
374 struct nk_input *in;
375
376 struct nk_rect header;
377 int is_clicked = nk_false;
378 enum nk_widget_layout_states s;
379 const struct nk_style_item *background;
380 struct nk_color symbol_color;
381 struct nk_text text;
382
383 NK_ASSERT(ctx);
384 NK_ASSERT(ctx->current);
385 NK_ASSERT(ctx->current->layout);
386 if (!ctx || !ctx->current || !ctx->current->layout)
387 return 0;
388
389 win = ctx->current;
390 style = &ctx->style;
391 s = nk_widget(&header, ctx);
392 if (!s) return 0;
393
394 in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
395 if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
396 is_clicked = nk_true;
397
398 /* draw combo box header background and border */
399 if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
400 background = &style->combo.active;
401 symbol_color = style->combo.symbol_active;
402 text.text = style->combo.label_active;
403 } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
404 background = &style->combo.hover;
405 symbol_color = style->combo.symbol_hover;
406 text.text = style->combo.label_hover;
407 } else {
408 background = &style->combo.normal;
409 symbol_color = style->combo.symbol_normal;
410 text.text = style->combo.label_normal;
411 }
412
413 text.text = nk_rgb_factor(text.text, style->combo.color_factor);
414 symbol_color = nk_rgb_factor(symbol_color, style->combo.color_factor);
415
416 switch(background->type) {
417 case NK_STYLE_ITEM_IMAGE:
418 text.background = nk_rgba(0, 0, 0, 0);
419 nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
420 break;
421 case NK_STYLE_ITEM_NINE_SLICE:
422 text.background = nk_rgba(0, 0, 0, 0);
423 nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
424 break;
425 case NK_STYLE_ITEM_COLOR:
426 text.background = background->data.color;
427 nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
428 nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
429 break;
430 }
431 {
432 struct nk_rect content;
433 struct nk_rect button;
434 struct nk_rect label;
435 struct nk_rect image;
436
437 enum nk_symbol_type sym;
438 if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
439 sym = style->combo.sym_hover;
440 else if (is_clicked)
441 sym = style->combo.sym_active;
442 else sym = style->combo.sym_normal;
443
444 /* calculate button */
445 button.w = header.h - 2 * style->combo.button_padding.y;
446 button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
447 button.y = header.y + style->combo.button_padding.y;
448 button.h = button.w;
449
450 content.x = button.x + style->combo.button.padding.x;
451 content.y = button.y + style->combo.button.padding.y;
452 content.w = button.w - 2 * style->combo.button.padding.x;
453 content.h = button.h - 2 * style->combo.button.padding.y;
454 nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
455 &ctx->style.combo.button, sym, style->font);
456
457 /* draw symbol */
458 image.x = header.x + style->combo.content_padding.x;
459 image.y = header.y + style->combo.content_padding.y;
460 image.h = header.h - 2 * style->combo.content_padding.y;
461 image.w = image.h;
462 nk_draw_symbol(&win->buffer, symbol, image, text.background, symbol_color,
463 style->combo.border, style->font);
464
465 /* draw label */
466 text.padding = nk_vec2(0,0);
467 label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
468 label.y = header.y + style->combo.content_padding.y;
469 label.w = (button.x - style->combo.content_padding.x) - label.x;
470 label.h = header.h - 2 * style->combo.content_padding.y;
471 nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
472 }
473 return nk_combo_begin(ctx, win, size, is_clicked, header);
474}
475NK_API nk_bool
476nk_combo_begin_image(struct nk_context *ctx, struct nk_image img, struct nk_vec2 size)
477{
478 struct nk_window *win;
479 struct nk_style *style;
480 const struct nk_input *in;
481
482 struct nk_rect header;
483 int is_clicked = nk_false;
484 enum nk_widget_layout_states s;
485 const struct nk_style_item *background;
486
487 NK_ASSERT(ctx);
488 NK_ASSERT(ctx->current);
489 NK_ASSERT(ctx->current->layout);
490 if (!ctx || !ctx->current || !ctx->current->layout)
491 return 0;
492
493 win = ctx->current;
494 style = &ctx->style;
495 s = nk_widget(&header, ctx);
496 if (s == NK_WIDGET_INVALID)
497 return 0;
498
499 in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
500 if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
501 is_clicked = nk_true;
502
503 /* draw combo box header background and border */
504 if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
505 background = &style->combo.active;
506 else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
507 background = &style->combo.hover;
508 else background = &style->combo.normal;
509
510 switch (background->type) {
511 case NK_STYLE_ITEM_IMAGE:
512 nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
513 break;
514 case NK_STYLE_ITEM_NINE_SLICE:
515 nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
516 break;
517 case NK_STYLE_ITEM_COLOR:
518 nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
519 nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
520 break;
521 }
522 {
523 struct nk_rect bounds = {0,0,0,0};
524 struct nk_rect content;
525 struct nk_rect button;
526 int draw_button_symbol;
527
528 enum nk_symbol_type sym;
529 if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
530 sym = style->combo.sym_hover;
531 else if (is_clicked)
532 sym = style->combo.sym_active;
533 else sym = style->combo.sym_normal;
534
535 /* represents whether or not the combo's button symbol should be drawn */
536 draw_button_symbol = sym != NK_SYMBOL_NONE;
537
538 /* calculate button */
539 button.w = header.h - 2 * style->combo.button_padding.y;
540 button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
541 button.y = header.y + style->combo.button_padding.y;
542 button.h = button.w;
543
544 content.x = button.x + style->combo.button.padding.x;
545 content.y = button.y + style->combo.button.padding.y;
546 content.w = button.w - 2 * style->combo.button.padding.x;
547 content.h = button.h - 2 * style->combo.button.padding.y;
548
549 /* draw image */
550 bounds.h = header.h - 2 * style->combo.content_padding.y;
551 bounds.y = header.y + style->combo.content_padding.y;
552 bounds.x = header.x + style->combo.content_padding.x;
553 if (draw_button_symbol)
554 bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
555 else
556 bounds.w = header.w - 2 * style->combo.content_padding.x;
557 nk_draw_image(&win->buffer, bounds, &img, nk_rgb_factor(nk_white, style->combo.color_factor));
558
559 /* draw open/close button */
560 if (draw_button_symbol)
561 nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
562 &ctx->style.combo.button, sym, style->font);
563 }
564 return nk_combo_begin(ctx, win, size, is_clicked, header);
565}
566NK_API nk_bool
567nk_combo_begin_image_text(struct nk_context *ctx, const char *selected, int len,
568 struct nk_image img, struct nk_vec2 size)
569{
570 struct nk_window *win;
571 struct nk_style *style;
572 struct nk_input *in;
573
574 struct nk_rect header;
575 int is_clicked = nk_false;
576 enum nk_widget_layout_states s;
577 const struct nk_style_item *background;
578 struct nk_text text;
579
580 NK_ASSERT(ctx);
581 NK_ASSERT(ctx->current);
582 NK_ASSERT(ctx->current->layout);
583 if (!ctx || !ctx->current || !ctx->current->layout)
584 return 0;
585
586 win = ctx->current;
587 style = &ctx->style;
588 s = nk_widget(&header, ctx);
589 if (!s) return 0;
590
591 in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
592 if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
593 is_clicked = nk_true;
594
595 /* draw combo box header background and border */
596 if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
597 background = &style->combo.active;
598 text.text = style->combo.label_active;
599 } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
600 background = &style->combo.hover;
601 text.text = style->combo.label_hover;
602 } else {
603 background = &style->combo.normal;
604 text.text = style->combo.label_normal;
605 }
606
607 text.text = nk_rgb_factor(text.text, style->combo.color_factor);
608
609 switch(background->type) {
610 case NK_STYLE_ITEM_IMAGE:
611 text.background = nk_rgba(0, 0, 0, 0);
612 nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
613 break;
614 case NK_STYLE_ITEM_NINE_SLICE:
615 text.background = nk_rgba(0, 0, 0, 0);
616 nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
617 break;
618 case NK_STYLE_ITEM_COLOR:
619 text.background = background->data.color;
620 nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
621 nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
622 break;
623 }
624 {
625 struct nk_rect content;
626 struct nk_rect button;
627 struct nk_rect label;
628 struct nk_rect image;
629 int draw_button_symbol;
630
631 enum nk_symbol_type sym;
632 if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
633 sym = style->combo.sym_hover;
634 else if (is_clicked)
635 sym = style->combo.sym_active;
636 else sym = style->combo.sym_normal;
637
638 /* represents whether or not the combo's button symbol should be drawn */
639 draw_button_symbol = sym != NK_SYMBOL_NONE;
640
641 /* calculate button */
642 button.w = header.h - 2 * style->combo.button_padding.y;
643 button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
644 button.y = header.y + style->combo.button_padding.y;
645 button.h = button.w;
646
647 content.x = button.x + style->combo.button.padding.x;
648 content.y = button.y + style->combo.button.padding.y;
649 content.w = button.w - 2 * style->combo.button.padding.x;
650 content.h = button.h - 2 * style->combo.button.padding.y;
651 if (draw_button_symbol)
652 nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
653 &ctx->style.combo.button, sym, style->font);
654
655 /* draw image */
656 image.x = header.x + style->combo.content_padding.x;
657 image.y = header.y + style->combo.content_padding.y;
658 image.h = header.h - 2 * style->combo.content_padding.y;
659 image.w = image.h;
660 nk_draw_image(&win->buffer, image, &img, nk_rgb_factor(nk_white, style->combo.color_factor));
661
662 /* draw label */
663 text.padding = nk_vec2(0,0);
664 label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
665 label.y = header.y + style->combo.content_padding.y;
666 label.h = header.h - 2 * style->combo.content_padding.y;
667 if (draw_button_symbol)
668 label.w = (button.x - style->combo.content_padding.x) - label.x;
669 else
670 label.w = (header.x + header.w - style->combo.content_padding.x) - label.x;
671 nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
672 }
673 return nk_combo_begin(ctx, win, size, is_clicked, header);
674}
675NK_API nk_bool
676nk_combo_begin_symbol_label(struct nk_context *ctx,
677 const char *selected, enum nk_symbol_type type, struct nk_vec2 size)
678{
679 return nk_combo_begin_symbol_text(ctx, selected, nk_strlen(selected), type, size);
680}
681NK_API nk_bool
682nk_combo_begin_image_label(struct nk_context *ctx,
683 const char *selected, struct nk_image img, struct nk_vec2 size)
684{
685 return nk_combo_begin_image_text(ctx, selected, nk_strlen(selected), img, size);
686}
687NK_API nk_bool
688nk_combo_item_text(struct nk_context *ctx, const char *text, int len,nk_flags align)
689{
690 return nk_contextual_item_text(ctx, text, len, align);
691}
692NK_API nk_bool
693nk_combo_item_label(struct nk_context *ctx, const char *label, nk_flags align)
694{
695 return nk_contextual_item_label(ctx, label, align);
696}
697NK_API nk_bool
698nk_combo_item_image_text(struct nk_context *ctx, struct nk_image img, const char *text,
699 int len, nk_flags alignment)
700{
701 return nk_contextual_item_image_text(ctx, img, text, len, alignment);
702}
703NK_API nk_bool
704nk_combo_item_image_label(struct nk_context *ctx, struct nk_image img,
705 const char *text, nk_flags alignment)
706{
707 return nk_contextual_item_image_label(ctx, img, text, alignment);
708}
709NK_API nk_bool
710nk_combo_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
711 const char *text, int len, nk_flags alignment)
712{
713 return nk_contextual_item_symbol_text(ctx, sym, text, len, alignment);
714}
715NK_API nk_bool
716nk_combo_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
717 const char *label, nk_flags alignment)
718{
719 return nk_contextual_item_symbol_label(ctx, sym, label, alignment);
720}
721NK_API void nk_combo_end(struct nk_context *ctx)
722{
723 nk_contextual_end(ctx);
724}
725NK_API void nk_combo_close(struct nk_context *ctx)
726{
727 nk_contextual_close(ctx);
728}
729NK_API int
730nk_combo(struct nk_context *ctx, const char *const *items, int count,
731 int selected, int item_height, struct nk_vec2 size)
732{
733 int i = 0;
734 float max_height;
735
736 NK_ASSERT(ctx);
737 NK_ASSERT(items);
738 NK_ASSERT(ctx->current);
739 if (!ctx || !items ||!count)
740 return selected;
741
742 max_height = nk_combo_calc_max_height(ctx, count, item_height);
743 size.y = NK_MIN(size.y, max_height);
744 if (nk_combo_begin_label(ctx, items[selected], size)) {
745 nk_layout_row_dynamic(ctx, (float)item_height, 1);
746 for (i = 0; i < count; ++i) {
747 if (nk_combo_item_label(ctx, items[i], NK_TEXT_LEFT))
748 selected = i;
749 }
750 nk_combo_end(ctx);
751 }
752 return selected;
753}
754NK_API int
755nk_combo_separator(struct nk_context *ctx, const char *items_separated_by_separator,
756 int separator, int selected, int count, int item_height, struct nk_vec2 size)
757{
758 int i;
759 float max_height;
760 const char *current_item;
761 const char *iter;
762 int length = 0;
763
764 NK_ASSERT(ctx);
765 NK_ASSERT(items_separated_by_separator);
766 if (!ctx || !items_separated_by_separator)
767 return selected;
768
769 max_height = nk_combo_calc_max_height(ctx, count, item_height);
770 size.y = NK_MIN(size.y, max_height);
771
772 /* find selected item */
773 current_item = items_separated_by_separator;
774 for (i = 0; i < count; ++i) {
775 iter = current_item;
776 while (*iter && *iter != separator) iter++;
777 length = (int)(iter - current_item);
778 if (i == selected) break;
779 current_item = iter + 1;
780 }
781
782 if (nk_combo_begin_text(ctx, current_item, length, size)) {
783 current_item = items_separated_by_separator;
784 nk_layout_row_dynamic(ctx, (float)item_height, 1);
785 for (i = 0; i < count; ++i) {
786 iter = current_item;
787 while (*iter && *iter != separator) iter++;
788 length = (int)(iter - current_item);
789 if (nk_combo_item_text(ctx, current_item, length, NK_TEXT_LEFT))
790 selected = i;
791 current_item = current_item + length + 1;
792 }
793 nk_combo_end(ctx);
794 }
795 return selected;
796}
797NK_API int
798nk_combo_string(struct nk_context *ctx, const char *items_separated_by_zeros,
799 int selected, int count, int item_height, struct nk_vec2 size)
800{
801 return nk_combo_separator(ctx, items_separated_by_zeros, '\0', selected, count, item_height, size);
802}
803NK_API int
804nk_combo_callback(struct nk_context *ctx, void(*item_getter)(void*, int, const char**),
805 void *userdata, int selected, int count, int item_height, struct nk_vec2 size)
806{
807 int i;
808 float max_height;
809 const char *item;
810
811 NK_ASSERT(ctx);
812 NK_ASSERT(item_getter);
813 if (!ctx || !item_getter)
814 return selected;
815
816 max_height = nk_combo_calc_max_height(ctx, count, item_height);
817 size.y = NK_MIN(size.y, max_height);
818
819 item_getter(userdata, selected, &item);
820 if (nk_combo_begin_label(ctx, item, size)) {
821 nk_layout_row_dynamic(ctx, (float)item_height, 1);
822 for (i = 0; i < count; ++i) {
823 item_getter(userdata, i, &item);
824 if (nk_combo_item_label(ctx, item, NK_TEXT_LEFT))
825 selected = i;
826 }
827 nk_combo_end(ctx);
828 } return selected;
829}
830NK_API nk_bool
831nk_combobox(struct nk_context *ctx, const char *const *items, int count,
832 int *selected, int item_height, struct nk_vec2 size)
833{
834 int tmp = *selected;
835 *selected = nk_combo(ctx, items, count, *selected, item_height, size);
836 return tmp != *selected;
837}
838NK_API nk_bool
839nk_combobox_string(struct nk_context *ctx, const char *items_separated_by_zeros,
840 int *selected, int count, int item_height, struct nk_vec2 size)
841{
842 int tmp = *selected;
843 *selected = nk_combo_string(ctx, items_separated_by_zeros, *selected, count, item_height, size);
844 return tmp != *selected;
845}
846NK_API nk_bool
847nk_combobox_separator(struct nk_context *ctx, const char *items_separated_by_separator,
848 int separator, int *selected, int count, int item_height, struct nk_vec2 size)
849{
850 int tmp = *selected;
851 *selected = nk_combo_separator(ctx, items_separated_by_separator, separator,
852 *selected, count, item_height, size);
853 return tmp != *selected;
854}
855NK_API nk_bool
856nk_combobox_callback(struct nk_context *ctx,
857 void(*item_getter)(void* data, int id, const char **out_text),
858 void *userdata, int *selected, int count, int item_height, struct nk_vec2 size)
859{
860 int tmp = *selected;
861 *selected = nk_combo_callback(ctx, item_getter, userdata, *selected, count, item_height, size);
862 return tmp != *selected;
863}
float last_h
last frame's content height; used to flip without a gap
Definition nuklear.h:5642
nk_bool pinned_up
keep drop-up once chosen so expand/collapse does not jump
Definition nuklear.h:5643