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