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

/* Operating System native button */

#include "osgui_gtk.inl"
#include "osglobals_gtk.inl"
#include "osbutton_gtk.inl"
#include "oscontrol_gtk.inl"
#include "ospanel_gtk.inl"
#include "oswindow_gtk.inl"
#include "../osbutton.h"
#include "../osbutton.inl"
#include "../osgui.inl"
#include <draw2d/font.h>
#include <draw2d/image.h>
#include <core/event.h>
#include <core/heap.h>
#include <core/strings.h>
#include <sewer/bmath.h>
#include <sewer/bstd.h>
#include <sewer/cassert.h>
#include <sewer/ptr.h>

#if !defined(__GTK3__)
#error This file is only for GTK Toolkit
#endif

struct _osbutton_t
{
    OSControl control;
    button_flag_t flags;
    bool_t launch_event;
    bool_t is_default;
    uint32_t vpadding;
    uint32_t hpadding;
    real32_t textwidth;
    real32_t textheight;
    GtkWidget *radio;
    Font *fake_font;
    Font *font;
    String *text;
    String *markup;
    Image *image;
    GtkCssProvider *css_padding;
    GtkCssProvider *css_font;
    Listener *OnClick;
};

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

static const uint32_t i_PUSHBUTTON_EXTRAWIDTH = 2;
static const uint32_t i_CHECKBOX_EXTRAHEIGHT = 2;

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

static gui_state_t i_get_state(const OSButton *button)
{
    cassert_no_null(button);
    switch (button_get_type(button->flags))
    {
    case ekBUTTON_PUSH:
    case ekBUTTON_FLAT:
        return ekGUI_ON;

    case ekBUTTON_RADIO:
    case ekBUTTON_CHECK2:
    {
        gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button->control.widget));
        return active == TRUE ? ekGUI_ON : ekGUI_OFF;
    }

    case ekBUTTON_CHECK3:
        if (gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(button->control.widget)) == TRUE)
        {
            return ekGUI_MIXED;
        }
        else
        {
            gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button->control.widget));
            return active == TRUE ? ekGUI_ON : ekGUI_OFF;
        }

    case ekBUTTON_FLATGLE:
    {
        gboolean active = gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(button->control.widget));
        return active == TRUE ? ekGUI_ON : ekGUI_OFF;
    }

        cassert_default();
    }

    return ekGUI_OFF;
}

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

static void i_OnClick(GtkWidget *widget, OSButton *button)
{
    cassert_no_null(button);
    cassert_unref(widget == button->control.widget, widget);
    if (button_get_type(button->flags) == ekBUTTON_CHECK3)
    {
        if (gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(button->control.widget)) == TRUE)
            gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(button->control.widget), FALSE);
    }

    if (button->launch_event == TRUE && button->OnClick != NULL)
    {
        EvButton params;
        params.index = 0;
        params.state = i_get_state(button);
        params.text = NULL;
        listener_event(button->OnClick, ekGUI_EVENT_BUTTON, button, &params, NULL, OSButton, EvButton, void);
    }

    _oswindow_release_transient_focus(cast(button, OSControl));
}

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

static gboolean i_OnKeyPress(GtkWidget *widget, GdkEventKey *event, OSButton *button)
{
    guint key = 0;
    cassert_no_null(event);
    unref(widget);
    unref(button);

    key = event->keyval;

    /* Avoid the "DUMMY" GTK button navigation using arrows */
    if (key == GDK_KEY_Left || key == GDK_KEY_Right || key == GDK_KEY_Down || key == GDK_KEY_Up)
        return TRUE;

    return FALSE;
}

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

static gboolean i_OnPressed(GtkWidget *widget, GdkEventButton *event, OSButton *button)
{
    unref(widget);
    unref(event);
    if (_oswindow_mouse_down(cast(button, OSControl)) == TRUE)
        return FALSE;
    return TRUE;
}

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

static gboolean i_OnPushDraw(GtkWidget *widget, cairo_t *cr, OSButton *button)
{
    unref(cr);
    unref(button);
    if (button->is_default == TRUE)
        gtk_widget_set_state_flags(widget, GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_FOCUSED, FALSE);
    return FALSE;
}

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

static gboolean i_OnLabelDraw(GtkWidget *widget, cairo_t *cr, OSButton *button)
{
    PangoLayout *layout = NULL;
    real32_t bwidth = 0, bheight = 0;
    cassert_no_null(button);
    cassert(GTK_IS_LABEL(widget) == TRUE);
    cassert(_osbutton_text_allowed(button->flags) == TRUE);

    _oscontrol_widget_get_size(button->control.widget, &bwidth, &bheight);

    /*
     * Prepare the PangoLayout to render the button text.
     * Overwrite the 'fake' font by font we want.
     */
    {
        GdkRGBA color;
        layout = gtk_label_get_layout(GTK_LABEL(widget));
        pango_layout_set_font_description(layout, cast(font_native(button->font), PangoFontDescription));
        _oscontrol_to_gdkrgba(ekSYSCOLOR_LABEL, &color);
        cairo_set_source_rgba(cr, color.red, color.green, color.blue, color.alpha);
        pango_layout_set_markup(layout, tc(button->markup), -1);
    }

    cairo_save(cr);

    /* Positioning to draw the text */
    if (button_get_type(button->flags) == ekBUTTON_PUSH)
    {
        /* In pushbuttons, we have to center the content */
        real32_t cwidth = button->textwidth + i_PUSHBUTTON_EXTRAWIDTH;

        if (button->image != NULL)
        {
            real32_t imgw = image_width(button->image);
            cwidth += imgw + kBUTTON_IMAGE_SEP;
            cairo_translate(cr, imgw + kBUTTON_IMAGE_SEP, 0);
        }

        cairo_translate(cr, (bwidth - cwidth) / 2, ((bheight - button->textheight - 4) / 2));
    }

    /* Font scaling */
    cairo_scale(cr, font_xscale(button->font), 1);

    /* Draw the text */
    pango_cairo_show_layout(cr, layout);

    /* Draw the image. I don't know why, if I render the image first, text is not shown */
    if (button->image != NULL)
    {
        GdkPixbuf *pixbuf = cast(image_native(button->image), GdkPixbuf);
        real32_t imgw = image_width(button->image);
        real32_t imgh = image_height(button->image);
        cassert(button_get_type(button->flags) == ekBUTTON_PUSH);
        gdk_cairo_set_source_pixbuf(cr, pixbuf, -(imgw + kBUTTON_IMAGE_SEP), (button->textheight - imgh) / 2);
        cairo_paint(cr);
    }

    /*
     * All is done. Restore Cairo and the fake font.
     * If Gtk detects the 'real' font will change the button dimensions.
     */
    cairo_restore(cr);
    pango_layout_set_font_description(layout, (PangoFontDescription *)font_native(button->fake_font));

    /* Stop other handlers from being invoked for the event */
    return TRUE;
}

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

static GtkWidget *i_get_gtk_label(GtkWidget *widget)
{
    if (GTK_IS_LABEL(widget))
    {
        return widget;
    }
    else if (GTK_IS_CONTAINER(widget))
    {
        uint32_t i, n = _oscontrol_num_children(widget);
        for (i = 0; i < n; ++i)
        {
            GtkWidget *child = _oscontrol_get_child(widget, i);
            GtkWidget *label = i_get_gtk_label(child);
            if (label != NULL)
                return label;
        }

        return NULL;
    }
    else
    {
        /*
        const gchar *ptype = G_OBJECT_TYPE_NAME(widget);
        printf("TYPE: %s\n", ptype);
        */
        return NULL;
    }
}

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

static const char_t *i_css_obj(const uint32_t flags)
{
    /* Initial padding for button */
    switch (button_get_type(flags))
    {
    case ekBUTTON_PUSH:
    case ekBUTTON_FLAT:
    case ekBUTTON_FLATGLE:
        return _osglobals_css_button();
    case ekBUTTON_RADIO:
        return _osglobals_css_radio();
    case ekBUTTON_CHECK2:
    case ekBUTTON_CHECK3:
        return _osglobals_css_check();
        cassert_default();
    }

    return "";
}

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

OSButton *osbutton_create(const uint32_t flags)
{
    OSButton *button = heap_new0(OSButton);
    GtkWidget *widget = NULL;
    GtkWidget *focus_widget = NULL;
    const char_t *cssobj = NULL;
    button->flags = flags;
    button->textwidth = 0;
    button->textheight = 0;

    switch (button_get_type(flags))
    {
    case ekBUTTON_PUSH:
        widget = gtk_button_new_with_label("");
        gtk_button_set_use_underline(GTK_BUTTON(widget), TRUE);
        g_signal_connect(widget, "draw", G_CALLBACK(i_OnPushDraw), button);
        focus_widget = widget;
        break;

    case ekBUTTON_FLAT:
        widget = cast(gtk_tool_button_new(NULL, NULL), GtkWidget);
        focus_widget = gtk_bin_get_child(GTK_BIN(widget));
        break;

    case ekBUTTON_FLATGLE:
        widget = cast(gtk_toggle_tool_button_new(), GtkWidget);
        focus_widget = gtk_bin_get_child(GTK_BIN(widget));
        break;

    case ekBUTTON_RADIO:
        widget = gtk_radio_button_new_with_label(NULL, "");
        gtk_button_set_use_underline(GTK_BUTTON(widget), TRUE);
        button->radio = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(widget), "");
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button->radio), TRUE);
        focus_widget = widget;
        break;

    case ekBUTTON_CHECK2:
    case ekBUTTON_CHECK3:
        widget = gtk_check_button_new_with_label("");
        gtk_button_set_use_underline(GTK_BUTTON(widget), TRUE);
        focus_widget = widget;
        break;
    }

    cssobj = i_css_obj(button->flags);
    g_signal_connect(G_OBJECT(widget), "clicked", G_CALLBACK(i_OnClick), (gpointer)button);
    _oscontrol_init(&button->control, ekGUI_TYPE_BUTTON, widget, focus_widget, FALSE);

    /*
     * Set CSS padding to 0. Like Windows, the button padding in the difference
     * between button frame and content size.
     */
    button->vpadding = kBUTTON_VPADDING;
    button->hpadding = kBUTTON_HPADDING;
    _oscontrol_update_css_padding(focus_widget, cssobj, 0, 0, &button->css_padding);

    /*
     * We set the button font infinitely small for two reasons.
     * 1) That the button text does not influence its minimum size, allowing to create buttons of any size.
     * 2) Ability to assign text with mnemonics, maintain the GTK shortcuts support.
     */
    if (_osbutton_text_allowed(flags) == TRUE)
    {
        Font *fake_font = font_system(0, 0);
        _oscontrol_update_css_font(button->control.widget, cssobj, fake_font, &button->fake_font, &button->css_font);
        font_destroy(&fake_font);
        /* The real font and text for button */
        button->text = str_c("");
        button->markup = str_c("");
        button->font = _osgui_create_default_font();
    }

    g_signal_connect(G_OBJECT(focus_widget), "button-press-event", G_CALLBACK(i_OnPressed), (gpointer)button);
    g_signal_connect(G_OBJECT(focus_widget), "key-press-event", G_CALLBACK(i_OnKeyPress), (gpointer)button);
    button->launch_event = TRUE;
    return button;
}

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

void osbutton_destroy(OSButton **button)
{
    cassert_no_null(button);
    cassert_no_null(*button);

    if ((*button)->radio != NULL)
    {
        /*
        A floating object was finalized. This means that someone
        called g_object_unref() on an object that had only a floating
        reference; the initial floating reference is not owned by anyone
        and must be removed with g_object_ref_sink().
        */
        g_object_ref_sink((*button)->radio);
        g_object_unref((*button)->radio);
    }

    listener_destroy(&(*button)->OnClick);
    str_destopt(&(*button)->text);
    str_destopt(&(*button)->markup);
    ptr_destopt(image_destroy, &(*button)->image, Image);
    ptr_destopt(font_destroy, &(*button)->fake_font, Font);
    ptr_destopt(font_destroy, &(*button)->font, Font);
    _oscontrol_destroy_css_provider(&(*button)->css_padding);
    _oscontrol_destroy_css_provider(&(*button)->css_font);
    _oscontrol_destroy(*dcast(button, OSControl));
    heap_delete(button, OSButton);
}

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

void osbutton_OnClick(OSButton *button, Listener *listener)
{
    cassert_no_null(button);
    listener_update(&button->OnClick, listener);
}

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

static void i_update_text_extents(OSButton *button)
{
    /* Text measure for future bounds and positioning */
    cassert_no_null(button);
    font_extents(button->font, tc(button->text), -1.f, &button->textwidth, &button->textheight);
    button->textwidth = bmath_ceilf(button->textwidth);
    button->textheight = bmath_ceilf(button->textheight);
}

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

void osbutton_text(OSButton *button, const char_t *text)
{
    /*
     * We need to manage three kind of text strings:
     * 1) 'shortcut': translate incomming 'text' NAppGUI '&' to GTK '_' mnemonics.
     * 2) 'markup': pango markup '<span>' to underline the shortcut.
     * 3) 'plain': Without any markup or mnemonic for text measure.
     */
    char_t shortcut[256], markup[256], plain[256];
    uint32_t pos = UINT32_MAX;
    cassert_no_null(button);
    cassert(_osbutton_text_allowed(button->flags) == TRUE);
    pos = _osgui_underline_gtk_text(text, shortcut, sizeof(shortcut));
    _osgui_underline_markup(shortcut, pos, markup, sizeof(markup));
    _osgui_underline_plain(shortcut, pos, plain, sizeof(plain));
    str_upd(&button->text, plain);
    str_upd(&button->markup, markup);

    /*
     * The button inner label is a fake with '0' font size (invisible).
     * The use it just for GTK automatic manage of shortcuts.
     * The button 'real' text will be rendered in 'i_OnLabelDraw'
     */
    {
        GtkWidget *label = NULL;
        gtk_button_set_label(GTK_BUTTON(button->control.widget), shortcut);
        label = i_get_gtk_label(button->control.widget);
        if (label != NULL)
            g_signal_connect(G_OBJECT(label), "draw", G_CALLBACK(i_OnLabelDraw), button);
    }

    i_update_text_extents(button);
}

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

void osbutton_tooltip(OSButton *button, const char_t *text)
{
    cassert_no_null(button);
    gtk_widget_set_tooltip_text(button->control.widget, cast_const(text, gchar));
}

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

void osbutton_font(OSButton *button, const Font *font)
{
    cassert_no_null(button);
    if (font_equals(font, button->font) == FALSE)
    {
        font_destroy(&button->font);
        button->font = font_copy(font);
        i_update_text_extents(button);
    }
}

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

void osbutton_align(OSButton *button, const align_t align)
{
    cassert_no_null(button);
    cassert(_osbutton_text_allowed(button->flags) == TRUE);
    _oscontrol_set_halign(cast(button, OSControl), align);
}

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

void osbutton_image(OSButton *button, const Image *image)
{
    cassert_no_null(button);
    cassert_no_null(image);
    cassert(_osbutton_image_allowed(button->flags) == TRUE);
    if (button->image != NULL)
        image_destroy(&button->image);
    button->image = image_copy(image);

    switch (button_get_type(button->flags))
    {
    case ekBUTTON_FLAT:
    case ekBUTTON_FLATGLE:
    {
        const char_t *icon_name = _osgui_register_icon(button->image);
        gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(button->control.widget), icon_name);
        break;
    }

    /* In push buttons, image will be rendered in custom 'i_OnLabelDraw()' */
    case ekBUTTON_PUSH:
        break;

        cassert_default();
    }
}

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

void osbutton_state(OSButton *button, const gui_state_t state)
{
    cassert_no_null(button);
    button->launch_event = FALSE;
    switch (button_get_type(button->flags))
    {
    case ekBUTTON_RADIO:
        if (state == ekGUI_ON)
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button->control.widget), TRUE);
        else
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button->radio), TRUE);
        break;

    case ekBUTTON_CHECK2:
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button->control.widget), state == ekGUI_ON ? TRUE : FALSE);
        break;

    case ekBUTTON_CHECK3:
        switch (state)
        {
        case ekGUI_ON:
            gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(button->control.widget), FALSE);
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button->control.widget), TRUE);
            break;
        case ekGUI_OFF:
            gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(button->control.widget), FALSE);
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button->control.widget), FALSE);
            break;
        case ekGUI_MIXED:
            gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(button->control.widget), TRUE);
            break;
            cassert_default();
        }

        break;

    case ekBUTTON_FLATGLE:
        gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(button->control.widget), (state == ekGUI_ON) ? TRUE : FALSE);
        break;
        cassert_default();
    }
    button->launch_event = TRUE;
}

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

gui_state_t osbutton_get_state(const OSButton *button)
{
    return i_get_state(button);
}

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

void osbutton_vpadding(OSButton *button, const real32_t padding)
{
    cassert_no_null(button);
    if (button_get_type(button->flags) == ekBUTTON_PUSH)
        button->vpadding = padding >= 0 ? (uint32_t)padding : kBUTTON_VPADDING;
}

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

#if defined __ASSERTS__

static ___INLINE bool_t i_equal_button_text(const OSButton *button, const char_t *text)
{
    char_t shortcut[256], plain[256];
    uint32_t pos = UINT32_MAX;
    cassert_no_null(button);
    cassert(_osbutton_text_allowed(button->flags) == TRUE);
    pos = _osgui_underline_gtk_text(text, shortcut, sizeof(shortcut));
    _osgui_underline_plain(shortcut, pos, plain, sizeof(plain));
    return str_equ_c(plain, tc(button->text));
}

#endif

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

void osbutton_bounds(const OSButton *button, const char_t *text, const real32_t refwidth, const real32_t refheight, real32_t *width, real32_t *height)
{
    cassert_no_null(button);
    cassert_no_null(width);
    cassert_no_null(height);

    switch (button_get_type(button->flags))
    {
    case ekBUTTON_PUSH:
    {
        cassert_unref(i_equal_button_text(button, text) == TRUE, text);
        cassert(button->vpadding != UINT32_MAX);
        cassert(button->hpadding != UINT32_MAX);

        *width = button->textwidth + i_PUSHBUTTON_EXTRAWIDTH;

        /* Image width  */
        if (refwidth > 0.f)
        {
            *width += refwidth;
            *width += (real32_t)kBUTTON_IMAGE_SEP;
        }

        *width += (real32_t)button->hpadding;

        /* Image height */
        if (refheight > button->textheight)
            *height = refheight;
        else
            *height = button->textheight;

        *height += (real32_t)button->vpadding;
        break;
    }

    case ekBUTTON_CHECK2:
    case ekBUTTON_CHECK3:
    case ekBUTTON_RADIO:
    {
        cassert_unref(i_equal_button_text(button, text) == TRUE, text);
        cassert(button->vpadding != UINT32_MAX);
        cassert(button->hpadding != UINT32_MAX);
        *width = button->textwidth + i_PUSHBUTTON_EXTRAWIDTH;
        *width += (real32_t)_osglobals_check_width();
        *width += kCHECKBOX_IMAGE_SEP;
        *height = (real32_t)_osglobals_check_height();
        if (button->textheight > *height)
            *height = button->textheight;
        *height += i_CHECKBOX_EXTRAHEIGHT;
        break;
    }

    case ekBUTTON_FLAT:
    case ekBUTTON_FLATGLE:
        *width = (real32_t)(uint32_t)((refwidth * 1.5f) + .5f);
        *height = (real32_t)(uint32_t)((refheight * 1.5f) + .5f);
        break;

        cassert_default();
    }
}

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

void osbutton_attach(OSButton *button, OSPanel *panel)
{
    _ospanel_attach_control(panel, cast(button, OSControl));
}

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

void osbutton_detach(OSButton *button, OSPanel *panel)
{
    _ospanel_detach_control(panel, cast(button, OSControl));
}

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

void osbutton_visible(OSButton *button, const bool_t visible)
{
    _oscontrol_set_visible(cast(button, OSControl), visible);
}

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

void osbutton_enabled(OSButton *button, const bool_t enabled)
{
    _oscontrol_set_enabled(cast(button, OSControl), enabled);
}

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

void osbutton_size(const OSButton *button, real32_t *width, real32_t *height)
{
    _oscontrol_get_size(cast_const(button, OSControl), width, height);
}

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

void osbutton_origin(const OSButton *button, real32_t *x, real32_t *y)
{
    _oscontrol_get_origin(cast_const(button, OSControl), x, y);
}

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

void osbutton_frame(OSButton *button, const real32_t x, const real32_t y, const real32_t width, const real32_t height)
{
    _oscontrol_set_frame(cast(button, OSControl), x, y, width, height);
}

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

GtkWidget *_osbutton_focus_widget(OSButton *button)
{
    cassert_no_null(button);
    switch (button_get_type(button->flags))
    {
    case ekBUTTON_PUSH:
    case ekBUTTON_RADIO:
    case ekBUTTON_CHECK2:
    case ekBUTTON_CHECK3:
        return button->control.widget;

    case ekBUTTON_FLAT:
    case ekBUTTON_FLATGLE:
        /* The button inside the toolbutton */
        return gtk_bin_get_child(GTK_BIN(button->control.widget));

        cassert_default();
    }

    return NULL;
}

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

void _osbutton_command(OSButton *button)
{
    cassert_no_null(button->control.widget);
    i_OnClick(button->control.widget, button);
}

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

void _osbutton_set_default(OSButton *button, const bool_t is_default)
{
    cassert_no_null(button);
    if (button_get_type(button->flags) == ekBUTTON_PUSH)
    {
        if (button->is_default != is_default)
        {
            button->is_default = is_default;
            if (is_default == FALSE)
                gtk_widget_unset_state_flags(button->control.widget, GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_FOCUSED);

            gtk_widget_queue_draw(button->control.widget);
        }
    }
}