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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
/*
 * NAppGUI Cross-platform C SDK
 * 2015-2025 Francisco Garcia Collado
 * MIT Licence
 * https://nappgui.com/en/legal/license.html
 *
 * File: osbutton.m
 *
 */

/* Operating System native button */

#include "osbutton_osx.inl"
#include "oscontrol_osx.inl"
#include "ospanel_osx.inl"
#include "oswindow_osx.inl"
#include "osgui_osx.inl"
#include "osglobals.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 <sewer/bmem.h>
#include <sewer/cassert.h>
#include <sewer/ptr.h>
#include <sewer/unicode.h>

#if !defined(__MACOS__)
#error This file is only for OSX
#endif

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

@interface OSXButtonCell : NSButtonCell

{
  @public
    uint32_t flags;
    gui_size_t size;
    real32_t text_width;
    Image *image;
}
@end

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

@interface OSXButton : NSButton

{
  @public
    uint32_t flags;
    uint32_t vpadding;
    OSTextAttr attrs;
    NSString *keyEquivalent;
    Listener *OnClick;
}
@end

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

@implementation OSXButton

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

/* From Mountain Lion to HighSierra render focus issue */
#if (defined MAC_OS_X_VERSION_10_7 && MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_7) || (!defined MAC_OS_X_VERSION_10_7) || (defined(MAC_OS_X_VERSION_10_14) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_14)
#else
- (void)drawRect:(NSRect)rect
{
    [super drawRect:rect];
    if ([[self window] firstResponder] == self)
    {
        NSSetFocusRingStyle(NSFocusRingOnly);
        if (button_get_type(self->flags) == ekBUTTON_FLAT || button_get_type(self->flags) == ekBUTTON_FLATGLE)
        {
            rect.origin.x += 2;
            rect.origin.y += 2;
            rect.size.width -= 4;
            rect.size.height -= 4;
        }
        NSRectFill(rect);
    }
}
#endif

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

static gui_state_t i_get_state(const OSXButton *button)
{
#if defined(MAC_OS_X_VERSION_10_13) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_13
    NSControlStateValue state = 0;
    cassert_no_null(button);
    state = [button state];
    if (state == NSControlStateValueOn)
    {
        return ekGUI_ON;
    }
    else if (state == NSControlStateValueOff)
    {
        return ekGUI_OFF;
    }
    else
    {
        cassert(state == NSControlStateValueMixed);
        return ekGUI_MIXED;
    }
#else
    NSInteger state = 0;
    cassert_no_null(button);
    state = [button state];
    if (state == NSOnState)
    {
        return ekGUI_ON;
    }
    else if (state == NSOffState)
    {
        return ekGUI_OFF;
    }
    else
    {
        cassert(state == NSMixedState);
        return ekGUI_MIXED;
    }
#endif
}

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

static void i_OnClick(OSXButton *button)
{
    if ([button isEnabled] == YES)
    {
        gui_state_t state = i_get_state(button);
        if (button_get_type(button->flags) == ekBUTTON_FLATGLE)
        {
            switch (state)
            {
            case ekGUI_ON:
                [[button cell] setShowsBorderOnlyWhileMouseInside:NO];
                break;
            case ekGUI_OFF:
                [[button cell] setShowsBorderOnlyWhileMouseInside:YES];
                break;
                cassert_default();
            }
        }
        else if (button_get_type(button->flags) == ekBUTTON_CHECK3)
        {
            if (state == ekGUI_MIXED)
            {
                state = ekGUI_ON;
                osbutton_state(cast(button, OSButton), state);
            }
        }

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

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

- (IBAction)onClickButton:(id)sender
{
    cassert_no_null(sender);
    cassert(sender == self);
    i_OnClick(self);
    _oswindow_release_transient_focus(cast(self, OSControl));
}

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

- (void)mouseDown:(NSEvent *)theEvent
{
    if (_oswindow_mouse_down(cast(self, OSControl)) == TRUE)
        [super mouseDown:theEvent];
}

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

#if defined(MAC_OS_X_VERSION_10_8) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_8

- (IBAction)onClickRadio00:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio01:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio02:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio03:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio04:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio05:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio06:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio07:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio08:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio09:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio10:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio11:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio12:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio13:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio14:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio15:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio16:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio17:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio18:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio19:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio20:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio21:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio22:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio23:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio24:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio25:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio26:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio27:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio28:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio29:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio30:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio31:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio32:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio33:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio34:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio35:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio36:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio37:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio38:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio39:(id)sender
{
    [self onClickButton:sender];
}

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

- (IBAction)onClickRadio40:(id)sender
{
    [self onClickButton:sender];
}

#endif

@end

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

@implementation OSXButtonCell

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

- (NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView
{
    if (button_get_type(self->flags) == ekBUTTON_PUSH)
    {
        if (size == ekGUI_SIZE_MINI)
        {
            NSImage *nsimage = [self image];
            if (nsimage != nil)
            {
                NSBitmapImageRep *image_rep = cast([[cast(nsimage, NSImage) representations] objectAtIndex:0], NSBitmapImageRep);
                NSInteger width = [image_rep pixelsWide];
                frame.origin.x -= (CGFloat)(width - 4);
                frame.size.width += (CGFloat)(width - 4);
            }
        }
    }

    return [super drawTitle:title withFrame:frame inView:controlView];
}

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

- (void)drawImage:(NSImage *)nsimage withFrame:(NSRect)frame inView:(NSView *)controlView
{
    if (nsimage != nil)
    {
        if (button_get_type(self->flags) == ekBUTTON_PUSH)
        {
            NSBitmapImageRep *image_rep = nil;
            NSInteger width, height;
            NSInteger offset_x = 0, offset_y = 0;
            NSRect rframe = [controlView frame];
            image_rep = cast([[nsimage representations] objectAtIndex:0], NSBitmapImageRep);
            cassert_no_null(image_rep);
            width = [image_rep pixelsWide];
            height = [image_rep pixelsHigh];
            offset_x = ((NSInteger)rframe.size.width - (NSInteger)self->text_width) / 2;
            offset_y = ((NSInteger)rframe.size.height - height) / 2;

            if (self->size == ekGUI_SIZE_REGULAR)
                offset_y -= 1;
            else if (self->size == ekGUI_SIZE_SMALL)
                offset_y -= 1;

            frame.size.width = (CGFloat)width;
            frame.size.height = (CGFloat)height;
            frame.origin.x = (CGFloat)offset_x;
            frame.origin.y = (CGFloat)offset_y;
            [super drawImage:nsimage withFrame:frame inView:controlView];
        }
        else
        {
            [super drawImage:nsimage withFrame:frame inView:controlView];
        }
    }
}

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

static NSRect i_pushbutton_cell_frame(NSRect rect, const gui_size_t size)
{
    switch (size)
    {
    case ekGUI_SIZE_MINI:
        rect.origin.y -= 0.f;
        break;
    case ekGUI_SIZE_REGULAR:
        rect.origin.x -= 5.f;
        rect.origin.y -= 1.f;
        rect.size.width += 10.f;
        rect.size.height += 4.f;
        break;
    case ekGUI_SIZE_SMALL:
        rect.origin.x -= 5.f;
        rect.origin.y += 1.f;
        rect.size.width += 10.f;
        break;
    default:
        break;
    }

    return rect;
}

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

#if defined(MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7

- (void)drawFocusRingMaskWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    if (button_get_type(self->flags) == ekBUTTON_PUSH)
        cellFrame = i_pushbutton_cell_frame(cellFrame, self->size);
    [super drawFocusRingMaskWithFrame:cellFrame inView:controlView];
}

#endif

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

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    /*NSFrameRect(cellFrame);*/
    if (button_get_type(self->flags) == ekBUTTON_PUSH)
    {
        cellFrame = i_pushbutton_cell_frame(cellFrame, self->size);
    }
    else if (button_get_type(self->flags) == ekBUTTON_FLAT || button_get_type(self->flags) == ekBUTTON_FLATGLE)
    {
        cellFrame.origin.x -= 1.f;
        cellFrame.origin.y -= 1.f;
        cellFrame.size.width += 2.f;
        cellFrame.size.height += 2.f;
    }

    [super drawWithFrame:cellFrame inView:controlView];
}

@end

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

#if defined(MAC_OS_X_VERSION_10_8) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_8

static void i_set_button_action(OSXButton *button, NSUInteger radio_index)
{
    cassert(radio_index < 40);
    radio_index = radio_index % 40;
    switch (radio_index)
    {
    case 0:
        [button setAction:@selector(onClickRadio00:)];
        break;
    case 1:
        [button setAction:@selector(onClickRadio01:)];
        break;
    case 2:
        [button setAction:@selector(onClickRadio02:)];
        break;
    case 3:
        [button setAction:@selector(onClickRadio03:)];
        break;
    case 4:
        [button setAction:@selector(onClickRadio04:)];
        break;
    case 5:
        [button setAction:@selector(onClickRadio05:)];
        break;
    case 6:
        [button setAction:@selector(onClickRadio06:)];
        break;
    case 7:
        [button setAction:@selector(onClickRadio07:)];
        break;
    case 8:
        [button setAction:@selector(onClickRadio08:)];
        break;
    case 9:
        [button setAction:@selector(onClickRadio09:)];
        break;
    case 10:
        [button setAction:@selector(onClickRadio10:)];
        break;
    case 11:
        [button setAction:@selector(onClickRadio11:)];
        break;
    case 12:
        [button setAction:@selector(onClickRadio12:)];
        break;
    case 13:
        [button setAction:@selector(onClickRadio13:)];
        break;
    case 14:
        [button setAction:@selector(onClickRadio14:)];
        break;
    case 15:
        [button setAction:@selector(onClickRadio15:)];
        break;
    case 16:
        [button setAction:@selector(onClickRadio16:)];
        break;
    case 17:
        [button setAction:@selector(onClickRadio17:)];
        break;
    case 18:
        [button setAction:@selector(onClickRadio18:)];
        break;
    case 19:
        [button setAction:@selector(onClickRadio19:)];
        break;
    case 20:
        [button setAction:@selector(onClickRadio20:)];
        break;
    case 21:
        [button setAction:@selector(onClickRadio21:)];
        break;
    case 22:
        [button setAction:@selector(onClickRadio22:)];
        break;
    case 23:
        [button setAction:@selector(onClickRadio23:)];
        break;
    case 24:
        [button setAction:@selector(onClickRadio24:)];
        break;
    case 25:
        [button setAction:@selector(onClickRadio25:)];
        break;
    case 26:
        [button setAction:@selector(onClickRadio26:)];
        break;
    case 27:
        [button setAction:@selector(onClickRadio27:)];
        break;
    case 28:
        [button setAction:@selector(onClickRadio28:)];
        break;
    case 29:
        [button setAction:@selector(onClickRadio29:)];
        break;
    case 30:
        [button setAction:@selector(onClickRadio30:)];
        break;
    case 31:
        [button setAction:@selector(onClickRadio31:)];
        break;
    case 32:
        [button setAction:@selector(onClickRadio32:)];
        break;
    case 33:
        [button setAction:@selector(onClickRadio33:)];
        break;
    case 34:
        [button setAction:@selector(onClickRadio34:)];
        break;
    case 35:
        [button setAction:@selector(onClickRadio35:)];
        break;
    case 36:
        [button setAction:@selector(onClickRadio36:)];
        break;
    case 37:
        [button setAction:@selector(onClickRadio37:)];
        break;
    case 38:
        [button setAction:@selector(onClickRadio38:)];
        break;
    case 39:
        [button setAction:@selector(onClickRadio39:)];
        break;
        cassert_default();
    }
}

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

static void i_recompute_button_action(OSXButton *button, NSView *parent_view)
{
    cassert_no_null(button);
    cassert_no_null(parent_view);
    /* Use of NSMatrix is informally deprecated. We expect to add the formal deprecation macros in
     * a subsequent release, but its use is discouraged in the mean time.
     * The primary use of NSMatrix is for radio button groups, so recall that for applications linked
     * on 10.8 or later, radio buttons that share the same parent view and ACTION will operate as a group.
     *
     * This avoid the automatic radiobutton grouping in OSX 10.8 and later.
     * It's assing a different ACTION selector for each radio button in superview
     */
    if (button_get_type(button->flags) == ekBUTTON_RADIO)
    {
        NSUInteger radio_index = 0;
        NSArray *children = [parent_view subviews];
        NSUInteger i, count;
        count = [children count];
        for (i = 0; i < count; ++i)
        {
            NSView *child = cast([children objectAtIndex:i], NSView);
            if ([child isKindOfClass:[OSXButton class]])
            {
                OSXButton *child_button = cast(child, OSXButton);
                if (button_get_type(child_button->flags) == ekBUTTON_RADIO)
                {
                    i_set_button_action(child_button, radio_index);
                    radio_index += 1;
                }
            }
        }
    }
}

#else

#define i_recompute_button_action(button, parent_view) ((void)button, (void)parent_view)


#endif

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

#if defined(MAC_OS_X_VERSION_10_14) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_14
#define ROUNDED_BEZEL NSBezelStyleRounded

#define REGULAR_SQUARE_BEZEL NSBezelStyleRegularSquare

#define PUSH_IN_BUTTON NSButtonTypeMomentaryPushIn

#define ON_OFF_BUTTON NSButtonTypeOnOff

#define SWITCH_BUTTON NSButtonTypeSwitch

#define RADIO_BUTTON NSButtonTypeRadio

#else
#define ROUNDED_BEZEL NSRoundedBezelStyle

#define REGULAR_SQUARE_BEZEL NSRegularSquareBezelStyle

#define PUSH_IN_BUTTON NSMomentaryPushInButton

#define ON_OFF_BUTTON NSOnOffButton

#define SWITCH_BUTTON NSSwitchButton

#define RADIO_BUTTON NSRadioButton

#endif

#if defined(MAC_OS_X_VERSION_10_12) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
#define KEY_MODIFIER_OPTION NSEventModifierFlagCommand

#else
#define KEY_MODIFIER_OPTION NSCommandKeyMask

#endif

static void i_set_button_type(OSXButton *button, OSXButtonCell *cell, const uint32_t flags)
{
    switch (button_get_type(flags))
    {
    case ekBUTTON_PUSH:
        [cell setBezelStyle:ROUNDED_BEZEL];
        [cell setBordered:YES];
        [cell setShowsBorderOnlyWhileMouseInside:NO];
        [cell setImagePosition:NSNoImage];
        [button setButtonType:PUSH_IN_BUTTON];
        break;

    case ekBUTTON_CHECK2:
    case ekBUTTON_CHECK3:
        [cell setBezelStyle:REGULAR_SQUARE_BEZEL];
        [cell setBordered:NO];
        /* macos 10.15 Catalina hides the check image */
        /* [cell setImagePosition:NSImageLeft]; */
        [cell setShowsBorderOnlyWhileMouseInside:NO];
        [cell setImageScaling:NSImageScaleNone];
        [button setButtonType:SWITCH_BUTTON];
        [button setAllowsMixedState:button_get_type(flags) == ekBUTTON_CHECK3 ? YES : NO];
        break;

    case ekBUTTON_RADIO:
        [cell setBezelStyle:REGULAR_SQUARE_BEZEL];
        [cell setBordered:NO];
        [cell setShowsBorderOnlyWhileMouseInside:NO];
        [cell setImageScaling:NSImageScaleNone];
        [button setButtonType:RADIO_BUTTON];
        break;

    case ekBUTTON_FLAT:
        [cell setBezelStyle:REGULAR_SQUARE_BEZEL];
        [cell setBordered:YES];
        [cell setShowsBorderOnlyWhileMouseInside:YES];
        [cell setImageScaling:NSImageScaleNone];
        [button setButtonType:PUSH_IN_BUTTON];
        break;

    case ekBUTTON_FLATGLE:
        [cell setBezelStyle:REGULAR_SQUARE_BEZEL];
        [cell setBordered:YES];
        [cell setShowsBorderOnlyWhileMouseInside:YES];
        [cell setImageScaling:NSImageScaleNone];
        [button setButtonType:PUSH_IN_BUTTON];
        break;

        cassert_default();
    }
}

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

OSButton *osbutton_create(const uint32_t flags)
{
    OSXButton *button = nil;
    OSXButtonCell *cell = nil;
    heap_auditor_add("OSXButton");
    heap_auditor_add("OSXButtonCell");
    button = [[OSXButton alloc] initWithFrame:NSZeroRect];
    button->flags = flags;
    button->vpadding = UINT32_MAX;
    button->OnClick = NULL;
    _oscontrol_init(button);
    cell = [[OSXButtonCell alloc] init];
    cell->flags = button->flags;
    cell->image = NULL;
    [button setCell:cell];
    [button setTarget:button];
    [button setAction:@selector(onClickButton:)];
    button->keyEquivalent = nil;
    i_set_button_type(button, cell, flags);

    if (_osbutton_text_allowed(flags) == TRUE)
    {
        _oscontrol_init_textattr(&button->attrs);
        _oscontrol_set_align(button, &button->attrs, ekCENTER);
        _oscontrol_set_font(button, &button->attrs, button->attrs.font);
        cell->size = _osgui_size_font(font_size(button->attrs.font));
        _oscontrol_set_text(button, &button->attrs, "");
        _oscontrol_size_from_font(cell, button->attrs.font);
    }
    else
    {
        bmem_zero(&button->attrs, OSTextAttr);
        cell->size = ENUM_MAX(gui_size_t);
    }

    return cast(button, OSButton);
}

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

void osbutton_destroy(OSButton **button)
{
    OSXButton *buttonp = nil;
    OSXButtonCell *cell = nil;
    cassert_no_null(button);
    buttonp = *dcast(button, OSXButton);
    cassert_no_null(buttonp);
    listener_destroy(&buttonp->OnClick);
    _oscontrol_remove_textattr(&buttonp->attrs);
    cell = [buttonp cell];

    if (buttonp->keyEquivalent != nil)
        [buttonp->keyEquivalent release];

    ptr_destopt(image_destroy, &cell->image, Image);
    [cell release];
    [buttonp release];
    *button = NULL;
    heap_auditor_delete("OSXButton");
    heap_auditor_delete("OSXButtonCell");
}

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

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

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

void osbutton_text(OSButton *button, const char_t *text)
{
    OSXButton *lbutton = cast(button, OSXButton);
    char_t tbuff[256];
    cassert_no_null(lbutton);
    cassert(_osbutton_text_allowed(lbutton->flags) == TRUE);
    lbutton->attrs.mark = _osgui_key_equivalent_text(text, tbuff, sizeof(tbuff));
    _oscontrol_set_text(lbutton, &lbutton->attrs, tbuff);

    if (lbutton->keyEquivalent != nil)
    {
        [lbutton->keyEquivalent release];
        lbutton->keyEquivalent = nil;
    }

    if (lbutton->attrs.mark != UINT32_MAX)
    {
        const char_t *pos = unicode_move(tbuff, lbutton->attrs.mark, ekUTF8);
        uint32_t cp = unicode_to_u32(pos, ekUTF8);
        unichar c = (unichar)unicode_tolower(cp);
        lbutton->keyEquivalent = [[NSString alloc] initWithCharacters:&c length:1];
        [lbutton setKeyEquivalent:lbutton->keyEquivalent];
        [lbutton setKeyEquivalentModifierMask:KEY_MODIFIER_OPTION];
    }
}

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

void osbutton_tooltip(OSButton *button, const char_t *text)
{
    cassert_no_null(button);
    _oscontrol_tooltip_set(cast(button, OSXButton), text);
}

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

void osbutton_font(OSButton *button, const Font *font)
{
    OSXButton *lbutton = cast(button, OSXButton);
    OSXButtonCell *cell = [lbutton cell];
    cassert_no_null(lbutton);
    cassert(_osbutton_text_allowed(lbutton->flags) == TRUE);
    _oscontrol_set_font(lbutton, &lbutton->attrs, font);
    _oscontrol_size_from_font([lbutton cell], lbutton -> attrs.font);
    cell->size = _osgui_size_font(font_size(lbutton->attrs.font));
}

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

void osbutton_align(OSButton *button, const align_t align)
{
    OSXButton *lbutton = cast(button, OSXButton);
    cassert_no_null(lbutton);
    cassert(_osbutton_text_allowed(lbutton->flags) == TRUE);
    _oscontrol_set_align(lbutton, &lbutton->attrs, align);
}

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

void osbutton_image(OSButton *button, const Image *image)
{
    OSXButton *lbutton = nil;
    OSXButtonCell *cell = nil;
    lbutton = cast(button, OSXButton);
    cassert_no_null(lbutton);
    cassert(_osbutton_image_allowed(lbutton->flags) == TRUE);
    cell = [lbutton cell];
    if (button_get_type(cell->flags) == ekBUTTON_PUSH)
    {
        ptr_destopt(image_destroy, &cell->image, Image);
        if (image != NULL)
        {
            cell->image = image_copy(image);
            [cell setImagePosition:NSImageLeft];
            _oscontrol_cell_set_image(cell, image);
        }
        else
        {
            [cell setImagePosition:NSNoImage];
            _oscontrol_cell_set_image(cell, NULL);
        }
    }
    else if (button_get_type(cell->flags) == ekBUTTON_FLAT || button_get_type(cell->flags) == ekBUTTON_FLATGLE)
    {
        _oscontrol_cell_set_image(cell, image);
    }
    else
    {
        cassert_msg(FALSE, "Button doesn't accept images.");
    }
}

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

void osbutton_state(OSButton *button, const gui_state_t state)
{
#if defined(MAC_OS_X_VERSION_10_13) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_13
    NSControlStateValue nsstate = NSControlStateValueOn;
    cassert_no_null(button);
    switch (state)
    {
    case ekGUI_ON:
        nsstate = NSControlStateValueOn;
        break;
    case ekGUI_OFF:
        nsstate = NSControlStateValueOff;
        break;
    case ekGUI_MIXED:
        nsstate = NSControlStateValueMixed;
        break;
        cassert_default();
    }
#else
    NSInteger nsstate = NSOnState;
    switch (state)
    {
    case ekGUI_ON:
        nsstate = NSOnState;
        break;
    case ekGUI_OFF:
        nsstate = NSOffState;
        break;
    case ekGUI_MIXED:
        nsstate = NSMixedState;
        break;
        cassert_default();
    }
#endif
    [cast(button, OSXButton) setState:nsstate];
}

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

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

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

void osbutton_vpadding(OSButton *button, const real32_t padding)
{
    OSXButton *lbutton = cast(button, OSXButton);
    cassert_no_null(lbutton);
    if (button_get_type(lbutton->flags) == ekBUTTON_PUSH)
    {
        if (padding >= 0)
            lbutton->vpadding = (uint32_t)padding;
        else
            lbutton->vpadding = UINT32_MAX;
    }
}

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

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

    switch (button_get_type(lbutton->flags))
    {
    case ekBUTTON_PUSH:
    {
        char_t tbuff[256];
        real32_t margin_width, margin_height;
        OSXButtonCell *cell = [lbutton cell];
        real32_t xscale = font_xscale(lbutton->attrs.font);
        uint32_t imgwidth = 0;
        _osgui_key_equivalent_text(text, tbuff, sizeof(tbuff));
        font_extents(lbutton->attrs.font, tbuff, -1.f, width, height);
        font_extents(lbutton->attrs.font, xscale >= 1 ? "OO" : "OOOO", -1.f, &margin_width, &margin_height);
        *width += margin_width + 2;
        cell->text_width = *width;

        if (cell->image != NULL)
            imgwidth = image_width(cell->image);

        switch (cell->size)
        {
        case ekGUI_SIZE_REGULAR:
            if (imgwidth > 0)
                *width += (real32_t)imgwidth;
            *height = 22.f;
            break;
        case ekGUI_SIZE_SMALL:
            if (imgwidth > 0)
                *width += (real32_t)imgwidth;
            *height = 20.f;
            break;
        case ekGUI_SIZE_MINI:
            if (imgwidth > 0)
                *width += (real32_t)imgwidth;
            else
                *width += 2.f;
            *height = 16.f;
            break;
        }

        if (lbutton->vpadding != UINT32_MAX)
        {
            *height += (real32_t)lbutton->vpadding;
            [[lbutton cell] setBezelStyle:REGULAR_SQUARE_BEZEL];
        }

        break;
    }

    case ekBUTTON_CHECK2:
    case ekBUTTON_CHECK3:
    case ekBUTTON_RADIO:
    {
        OSXButtonCell *cell = [lbutton cell];
#if (defined MAC_OS_X_VERSION_10_7 && MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_7) || (defined(MAC_OS_X_VERSION_10_14) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_14)
        static const real32_t i_CHECK_TEXT_SEP = 6.f;
#else
        /* Inexplicable crash in HighSierra and lowers */
        static const real32_t i_CHECK_TEXT_SEP = 8.f;
#endif
        font_extents(lbutton->attrs.font, text, -1.f, width, height);
        switch (cell->size)
        {
        case ekGUI_SIZE_REGULAR:
            *width += (real32_t)_osglobals_check_width() + i_CHECK_TEXT_SEP;
            *height = 18.f;
            break;
        case ekGUI_SIZE_SMALL:
            *width += (real32_t)_osglobals_check_width() + i_CHECK_TEXT_SEP;
            *height = 16.f;
            break;
        case ekGUI_SIZE_MINI:
            *width += (real32_t)_osglobals_check_width() + i_CHECK_TEXT_SEP;
            *height = 12.f;
            break;
        }
        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);
        if (refwidth <= 16.f)
            *width += 4.f;
        if (refheight <= 16.f)
            *height += 4.f;
        break;

        cassert_default();
    }
}

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

void osbutton_attach(OSButton *button, OSPanel *panel)
{
    _ospanel_attach_control(panel, cast(button, NSView));
    i_recompute_button_action(cast(button, OSXButton), cast(panel, NSView));
}

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

void osbutton_detach(OSButton *button, OSPanel *panel)
{
    _ospanel_detach_control(panel, cast(button, NSView));
    i_recompute_button_action(cast(button, OSXButton), cast(panel, NSView));
}

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

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

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

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

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

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

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

void osbutton_origin(const OSButton *button, real32_t *x, real32_t *y)
{
    _oscontrol_get_origin(cast(button, NSView), 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, NSView), x, y, width, height);
}

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

BOOL _osbutton_is(NSView *view)
{
    return [view isKindOfClass:[OSXButton class]];
}

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

BOOL _osbutton_OnIntro(NSResponder *resp)
{
    if (resp != nil && [resp isKindOfClass:[OSXButton class]])
    {
        OSXButton *button = cast(resp, OSXButton);
        if (button_get_type(button->flags) == ekBUTTON_PUSH)
        {
            i_OnClick(button);
            return YES;
        }
    }

    return NO;
}

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

void _osbutton_set_default(OSButton *button, const bool_t is_default)
{
    OSXButton *lbutton = cast(button, OSXButton);
    cassert_no_null(lbutton);
    if (is_default == TRUE)
    {
        [lbutton setKeyEquivalent:@"\r"];
    }
    else
    {
        if (lbutton->keyEquivalent != nil)
        {
            [lbutton setKeyEquivalent:lbutton->keyEquivalent];
            [lbutton setKeyEquivalentModifierMask:KEY_MODIFIER_OPTION];
        }
        else
        {
            [lbutton setKeyEquivalent:@""];
        }
    }
}