nappgui-sys 0.2.0

Rust raw bindings to NAppGUI
Documentation
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
/*
 * NAppGUI Cross-platform C SDK
 * 2015-2025 Francisco Garcia Collado
 * MIT Licence
 * https://nappgui.com/en/legal/license.html
 *
 * File: combo.c
 *
 */

/* Combo box */

#include "combo.h"
#include "combo.inl"
#include "component.inl"
#include "gui.inl"
#include <draw2d/font.h>
#include <draw2d/guictx.h>
#include <draw2d/image.h>
#include <geom2d/s2d.h>
#include <geom2d/v2d.h>
#include <core/arrpt.h>
#include <core/event.h>
#include <core/objh.h>
#include <core/strings.h>
#include <sewer/cassert.h>
#include <sewer/ptr.h>

struct _combo_t
{
    GuiComponent component;
    S2Df size;
    bool_t duplicates;
    bool_t is_focused;
    bool_t is_placeholder_active;
    ResId ttipid;
    ResId placid;
    String *placeholder;
    String *text;
    ArrPt(String) *texts;
    ArrPt(Image) *images;
    Font *font;
    Font *placeholder_font;
    uint32_t color;
    uint32_t focus_color;
    uint32_t bg_color;
    uint32_t bg_focus_color;
    uint32_t placeholder_color;
    Listener *OnFilter;
    Listener *OnChange;
    Listener *OnSelect;
};

/*---------------------------------------------------------------------------*/

static void i_update_placeholder(Combo *combo);

/*---------------------------------------------------------------------------*/

void _combo_destroy(Combo **combo)
{
    cassert_no_null(combo);
    cassert_no_null(*combo);
    _component_destroy_imp(&(*combo)->component);
    listener_destroy(&(*combo)->OnFilter);
    listener_destroy(&(*combo)->OnChange);
    listener_destroy(&(*combo)->OnSelect);
    ptr_destopt(str_destroy, &(*combo)->placeholder, String);
    str_destroy(&(*combo)->text);
    arrpt_destroy(&(*combo)->texts, str_destroy, String);
    arrpt_destroy(&(*combo)->images, image_destroy, Image);
    font_destroy(&(*combo)->font);
    ptr_destopt(font_destroy, &(*combo)->placeholder_font, Font);
    obj_delete(combo, Combo);
}

/*---------------------------------------------------------------------------*/

static void i_OnFilter(Combo *combo, Event *event)
{
    cassert_no_null(combo);
    if (combo->OnFilter != NULL)
        listener_pass_event(combo->OnFilter, event, combo, Combo);
}

/*---------------------------------------------------------------------------*/

static void i_OnChange(Combo *combo, Event *event)
{
    const EvText *params = event_params(event, EvText);
    cassert_no_null(combo);
    cassert_no_null(params);
    cassert(event_type(event) == ekGUI_EVENT_TXTCHANGE);
    cassert(combo->component.ositem == event_sender_imp(event, NULL));
    str_upd(&combo->text, params->text);
    if (combo->OnChange != NULL)
        listener_pass_event(combo->OnChange, event, combo, Combo);
}

/*---------------------------------------------------------------------------*/

/*
static void i_OnSelect(Combo *combo, Event *event)
{
    const EvButton *params = event_params(event, EvButton);
    const String *str = NULL;
    cassert_no_null(combo);
    cassert_no_null(event);
    cassert_no_null(params);
    cassert(event_type(event) == ekGUI_EVENT_BUTTON_PUSH);
    cassert(event->sender1 == combo->component.ositem);
    cassert(params->text == NULL);
    str = arrptr_get_c(combo->texts, params->index, String);
    str_upd(&combo->text, tc(str));
    if (combo->OnSelect.object != NULL)
    {
        ((EvButton*)params)->text = tc(str);
        listener_pass_event(&combo->OnSelect, combo, event, Combo);
    }
}*/

/*---------------------------------------------------------------------------*/

static void i_OnFocus(Combo *combo, Event *event)
{
    const bool_t *params = event_params(event, bool_t);
    cassert_no_null(combo);
    cassert_no_null(params);
    if (*params == TRUE)
    {
        if (combo->focus_color != UINT32_MAX || combo->placeholder_color != UINT32_MAX)
            combo->component.context->func_combo_set_text_color(combo->component.ositem, combo->focus_color);
        if (combo->bg_focus_color != UINT32_MAX)
            combo->component.context->func_combo_set_bg_color(combo->component.ositem, combo->bg_focus_color);
    }
    else
    {
        if (combo->focus_color != UINT32_MAX)
            combo->component.context->func_combo_set_text_color(combo->component.ositem, combo->color);
        if (combo->bg_focus_color != UINT32_MAX)
            combo->component.context->func_combo_set_bg_color(combo->component.ositem, combo->bg_color);
    }

    combo->is_focused = *params;
    i_update_placeholder(combo);
}

/*---------------------------------------------------------------------------*/

Combo *combo_create(void)
{
    const GuiCtx *context = guictx_get_current();
    Combo *combo = obj_new0(Combo);
    void *ositem = NULL;
    combo->text = str_c("");
    combo->font = _gui_create_default_font();
    combo->texts = arrpt_create(String);
    combo->images = arrpt_create(Image);
    combo->color = UINT32_MAX;
    combo->focus_color = UINT32_MAX;
    combo->bg_color = UINT32_MAX;
    combo->bg_focus_color = UINT32_MAX;
    combo->placeholder_color = UINT32_MAX;
    ositem = context->func_create[ekGUI_TYPE_COMBOBOX](ekCOMBO_FLAG);
    context->func_combo_set_font(ositem, combo->font);
    _component_init(&combo->component, context, PARAM(type, ekGUI_TYPE_COMBOBOX), &ositem);
    context->func_combo_OnFilter(combo->component.ositem, obj_listener(combo, i_OnFilter, Combo));
    context->func_combo_OnChange(combo->component.ositem, obj_listener(combo, i_OnChange, Combo));
    /* context->func_combo_set_OnSelect(combo->component.ositem, obj_listener(combo, i_OnSelect, Combo)); */
    return combo;
}

/*---------------------------------------------------------------------------*/

void combo_OnFilter(Combo *combo, Listener *listener)
{
    cassert_no_null(combo);
    listener_update(&combo->OnFilter, listener);
}

/*---------------------------------------------------------------------------*/

void combo_OnChange(Combo *combo, Listener *listener)
{
    cassert_no_null(combo);
    listener_update(&combo->OnChange, listener);
}

/*---------------------------------------------------------------------------*/

/*
void combo_OnSelect(Combo *combo, Listener listener);
void combo_OnSelect(Combo *combo, Listener listener)
{
    cassert_no_null(combo);
    listener_destroy(&combo->OnSelect);
    combo->OnSelect = listener;
}*/

/*---------------------------------------------------------------------------*/

/*
static bool_t i_update_selection(GuiComponent *component, ArrPt(String) *texts, const char_t *edit_text)
{
    cassert_no_null(component);
    cassert_no_null(component->context);
    cassert_no_nullf(component->context->func_combo_set_selected);
    arrptr_foreach(text, texts, String)
        if (str_equ_sc(*text, edit_text) == TRUE)
        {
            component->context->func_combo_set_selected(component->ositem, text_i);
            return TRUE;
        }
    arrptr_end();
    return FALSE;
}*/

/*---------------------------------------------------------------------------*/

static void i_update_placeholder(Combo *combo)
{
    cassert_no_null(combo);
    if (combo->placeholder != NULL && combo->is_focused == FALSE && str_equ(combo->text, "") == TRUE)
    {
        if (_gui_effective_alt_font(combo->font, combo->placeholder_font) == TRUE)
            combo->component.context->func_combo_set_font(combo->component.ositem, combo->placeholder_font);
        if (combo->placeholder_color != UINT32_MAX)
            combo->component.context->func_combo_set_text_color(combo->component.ositem, combo->placeholder_color);
        combo->component.context->func_combo_set_text(combo->component.ositem, tc(combo->placeholder));
        combo->is_placeholder_active = TRUE;
    }
    else if (combo->is_placeholder_active == TRUE)
    {
        if (_gui_effective_alt_font(combo->font, combo->placeholder_font) == TRUE)
            combo->component.context->func_combo_set_font(combo->component.ositem, combo->font);

        if (combo->placeholder_color != UINT32_MAX)
        {
            if (combo->is_focused == TRUE && combo->focus_color != UINT32_MAX)
                combo->component.context->func_combo_set_text_color(combo->component.ositem, combo->focus_color);
            else
                combo->component.context->func_combo_set_text_color(combo->component.ositem, combo->color);
        }

        combo->component.context->func_combo_set_text(combo->component.ositem, tc(combo->text));
        combo->is_placeholder_active = FALSE;
    }
}

/*---------------------------------------------------------------------------*/

void combo_text(Combo *combo, const char_t *text)
{
    cassert_no_null(combo);
    str_upd(&combo->text, text);
    combo->component.context->func_combo_set_text(combo->component.ositem, text);
    /*if (i_update_selection(&combo->component, combo->texts, text) == FALSE)
    {
        combo->component.context->func_combo_set_selected(combo->component.ositem, UINT32_MAX);
    }*/

    i_update_placeholder(combo);
}

/*---------------------------------------------------------------------------*/

void combo_align(Combo *combo, const align_t align)
{
    unref(combo);
    unref(align);
    cassert(FALSE);
}

/*---------------------------------------------------------------------------*/

void combo_duplicates(Combo *combo, const bool_t duplicates)
{
    cassert_no_null(combo);
    combo->duplicates = duplicates;
}

/*---------------------------------------------------------------------------*/

void combo_tooltip(Combo *combo, const char_t *text)
{
    const char_t *ltext = NULL;
    cassert_no_null(combo);
    if (text != NULL)
        ltext = _gui_respack_text(text, &combo->ttipid);
    combo->component.context->func_set_tooltip[ekGUI_TYPE_COMBOBOX](combo->component.ositem, ltext);
}

/*---------------------------------------------------------------------------*/

void combo_color(Combo *combo, const color_t color)
{
    cassert_no_null(combo);
    combo->color = color;
    combo->component.context->func_combo_set_text_color(combo->component.ositem, color);
}

/*---------------------------------------------------------------------------*/

static void i_update_focus_listener(Combo *combo)
{
    cassert_no_null(combo);
    if (combo->focus_color != UINT32_MAX || combo->bg_focus_color != UINT32_MAX || combo->placeholder != NULL)
    {
        combo->component.context->func_combo_OnFocus(combo->component.ositem, obj_listener(combo, i_OnFocus, Combo));
    }
    else
    {
        combo->component.context->func_combo_OnFocus(combo->component.ositem, NULL);
    }
}

/*---------------------------------------------------------------------------*/

void combo_color_focus(Combo *combo, const color_t color)
{
    cassert_no_null(combo);
    combo->focus_color = color;
    i_update_focus_listener(combo);
}

/*---------------------------------------------------------------------------*/

void combo_bgcolor(Combo *combo, const color_t color)
{
    cassert_no_null(combo);
    combo->bg_color = color;
    combo->component.context->func_combo_set_bg_color(combo->component.ositem, color);
}

/*---------------------------------------------------------------------------*/

void combo_bgcolor_focus(Combo *combo, const color_t color)
{
    cassert_no_null(combo);
    combo->bg_focus_color = color;
    i_update_focus_listener(combo);
    if (combo->is_focused)
        combo->component.context->func_combo_set_bg_color(combo->component.ositem, color);
}

/*---------------------------------------------------------------------------*/

void combo_phtext(Combo *combo, const char_t *text)
{
    const char_t *ltext = NULL;
    cassert_no_null(combo);
    if (text != NULL)
        ltext = _gui_respack_text(text, &combo->placid);
    str_upd(&combo->placeholder, ltext);
    i_update_focus_listener(combo);
    i_update_placeholder(combo);
}

/*---------------------------------------------------------------------------*/

void combo_phcolor(Combo *combo, const color_t color)
{
    cassert_no_null(combo);
    combo->placeholder_color = color;
    i_update_placeholder(combo);
}

/*---------------------------------------------------------------------------*/

void combo_phstyle(Combo *combo, const uint32_t fstyle)
{
    cassert_no_null(combo);
    ptr_destopt(font_destroy, &combo->placeholder_font, Font);
    if (fstyle != font_style(combo->font))
        combo->placeholder_font = font_with_style(combo->font, fstyle);
    i_update_placeholder(combo);
}

/*---------------------------------------------------------------------------*/

const char_t *combo_get_text(const Combo *combo, const uint32_t index)
{
    cassert_no_null(combo);
    if (index != UINT32_MAX)
    {
        const String *str = arrpt_get(combo->texts, index, String);
        return tc(str);
    }
    else
    {
        return tc(combo->text);
    }
}

/*---------------------------------------------------------------------------*/

static void i_delete_duplicates(GuiComponent *component, ArrPt(String) *texts, ArrPt(Image) *images, const char_t *text)
{
    uint32_t i = 0, num_elems = 0;
    cassert_no_null(component);
    num_elems = arrpt_size(texts, String);
    cassert(num_elems == arrpt_size(images, Image));
    for (i = 0; i < num_elems;)
    {
        const String *str = arrpt_get(texts, i, String);
        if (str_str(text, tc(str)) != NULL)
        {
            arrpt_delete(texts, i, str_destroy, String);
            arrpt_delete(images, i, image_destroy, Image);
            component->context->func_combo_set_elem(component->ositem, ekCTRL_OP_DEL, i, NULL, NULL);
            num_elems--;
        }
        else
        {
            i++;
        }
    }
}

/*---------------------------------------------------------------------------*/

static bool_t i_exists_substring(ArrPt(String) *texts, const char_t *subtext)
{
    arrpt_foreach(text, texts, String)
        if (str_str(tc(text), subtext) != NULL)
            return TRUE;
    arrpt_end()
    return FALSE;
}

/*---------------------------------------------------------------------------*/

uint32_t combo_count(const Combo *combo)
{
    uint32_t num_elems = 0;
    cassert_no_null(combo);
    num_elems = arrpt_size(combo->texts, String);
    cassert(num_elems == arrpt_size(combo->images, Image));
    return num_elems;
}

/*---------------------------------------------------------------------------*/

void combo_add_elem(Combo *combo, const char_t *text, const Image *image)
{
    bool_t exists = FALSE;
    cassert_no_null(combo);
    if (combo->duplicates == FALSE)
    {
        i_delete_duplicates(&combo->component, combo->texts, combo->images, text);
        exists = i_exists_substring(combo->texts, text);
    }

    if (exists == FALSE)
    {
        String *ltext = NULL;
        const Image *limage = NULL;
        Image *cimage;
        ltext = str_c(text);
        limage = _gui_respack_image((ResId)image, NULL);
        cimage = limage ? image_copy(limage) : NULL;
        arrpt_append(combo->texts, ltext, String);
        arrpt_append(combo->images, cimage, Image);
        combo->component.context->func_combo_set_elem(combo->component.ositem, ekCTRL_OP_ADD, UINT32_MAX, text, cimage);
        /* i_update_selection(&combo->component, combo->texts, tc(combo->text)); */
    }
}

/*---------------------------------------------------------------------------*/

void combo_set_elem(Combo *combo, const uint32_t index, const char_t *text, const Image *image)
{
    String **ltext = NULL;
    Image **limage = NULL;
    cassert_no_null(combo);
    cassert(index < arrpt_size(combo->texts, String));
    ltext = arrpt_all(combo->texts, String) + index;
    limage = arrpt_all(combo->images, Image) + index;
    str_destroy(ltext);
    ptr_destopt(image_destroy, limage, Image);
    *ltext = str_c(text);
    *limage = ptr_copyopt(image_copy, image, Image);
    combo->component.context->func_combo_set_elem(combo->component.ositem, ekCTRL_OP_SET, index, text, image);
    /* i_update_selection(&combo->component, combo->texts, tc(combo->text)); */
}

/*---------------------------------------------------------------------------*/

void combo_ins_elem(Combo *combo, const uint32_t index, const char_t *text, const Image *image)
{
    bool_t exists = FALSE;
    cassert_no_null(combo);
    cassert(index <= arrpt_size(combo->texts, String));

    if (combo->duplicates == FALSE)
    {
        i_delete_duplicates(&combo->component, combo->texts, combo->images, text);
        exists = i_exists_substring(combo->texts, text);
    }

    if (exists == FALSE)
    {
        String *ltext = NULL;
        Image *limage = NULL;
        ctrl_op_t op;
        uint32_t lindex;
        ltext = str_c(text);
        limage = ptr_copyopt(image_copy, image, Image);

        if (index < arrpt_size(combo->texts, String))
        {
            op = ekCTRL_OP_INS;
            lindex = index;
            arrpt_insert(combo->texts, index, ltext, String);
            arrpt_insert(combo->images, index, limage, Image);
        }
        else
        {
            op = ekCTRL_OP_ADD;
            lindex = UINT32_MAX;
            arrpt_append(combo->texts, ltext, String);
            arrpt_append(combo->images, limage, Image);
        }

        combo->component.context->func_combo_set_elem(combo->component.ositem, op, lindex, text, image);
        /* i_update_selection(&combo->component, combo->texts, tc(combo->text)); */
    }
}

/*---------------------------------------------------------------------------*/

void combo_del_elem(Combo *combo, const uint32_t index)
{
    cassert_no_null(combo);
    arrpt_delete(combo->texts, index, str_destroy, String);
    arrpt_delete(combo->images, index, image_destroy, Image);
    combo->component.context->func_combo_set_elem(combo->component.ositem, ekCTRL_OP_DEL, index, NULL, NULL);
    /* i_update_selection(&combo->component, combo->texts, tc(combo->text)); */
}

/*---------------------------------------------------------------------------*/

/*
void combo_set_font(Combo *combo, const Font *font);
void combo_set_font(Combo *combo, const Font *font)
{
    cassert_no_null(combo);
    cassert_no_null(combo->component.context);
    cassert_no_nullf(combo->component.context->func_combo_set_font);
    combo->component.context->func_combo_set_font(combo->component.ositem, font, _gui_font_family(font));
}*/

/*---------------------------------------------------------------------------*/

/*
uint32_t combo_get_selected(const Combo *combo);
uint32_t combo_get_selected(const Combo *combo)
{
    cassert_no_null(combo);
    cassert_no_null(combo->component.context);
    cassert_no_nullf(combo->component.context->func_combo_get_selected);
    return combo->component.context->func_combo_get_selected(combo->component.ositem);
}
*/

/*---------------------------------------------------------------------------*/

/*
void combo_set_selected(Combo *combo, const uint32_t index);
void combo_set_selected(Combo *combo, const uint32_t index)
{
    const String *str = NULL;
    cassert_no_null(combo);
    cassert_no_null(combo->component.context);
    cassert_no_nullf(combo->component.context->func_combo_set_selected);
    str = arrptr_get_c(combo->texts, index, String);
    str_upd(&combo->text, str);
    combo->component.context->func_combo_set_selected(combo->component.ositem, index);
}*/

/*---------------------------------------------------------------------------*/

void _combo_natural(Combo *combo, const uint32_t i, real32_t *dim0, real32_t *dim1)
{
    cassert_no_null(combo);
    cassert_no_null(dim0);
    cassert_no_null(dim1);
    if (i == 0)
    {
        combo->component.context->func_combo_bounds(combo->component.ositem, 100.f, &combo->size.width, &combo->size.height);
        *dim0 = combo->size.width;
    }
    else
    {
        cassert(i == 1);
        *dim1 = combo->size.height;
    }
}

/*---------------------------------------------------------------------------*/

void _combo_locale(Combo *combo)
{
    cassert_no_null(combo);
    if (combo->placid != NULL)
    {
        const char_t *text = _gui_respack_text(combo->placid, NULL);
        str_upd(&combo->placeholder, text);
        i_update_placeholder(combo);
    }

    if (combo->ttipid != NULL)
    {
        const char_t *text = _gui_respack_text(combo->ttipid, NULL);
        combo->component.context->func_set_tooltip[ekGUI_TYPE_COMBOBOX](combo->component.ositem, text);
    }
}