-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixgui_cocoa.c
3878 lines (3120 loc) · 111 KB
/
fixgui_cocoa.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
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
/*
* FixScript GUI v0.8 - https://www.fixscript.org/
* Copyright (c) 2019-2024 Martin Dvorak <jezek2@advel.cz>
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <unistd.h>
#include <time.h>
#include <limits.h>
#include <dlfcn.h>
#include <sys/time.h>
#include <objc/message.h>
#include "fixgui_common.h"
#if defined(__GNUC__) && !defined(__clang__)
static void (*objc_msgSend_GCCFIX)(void) = (void (*)(void))objc_msgSend;
static void (*objc_msgSend_stret_GCCFIX)(void) = (void (*)(void))objc_msgSend_stret;
static void (*objc_msgSendSuper_GCCFIX)(void) = (void (*)(void))objc_msgSendSuper;
#define objc_msgSend objc_msgSend_GCCFIX
#define objc_msgSend_stret objc_msgSend_stret_GCCFIX
#define objc_msgSendSuper objc_msgSendSuper_GCCFIX
#endif
#define ARGS(...) ,##__VA_ARGS__
#define call(obj, name, ret, args, ...) ((ret (*) (id, SEL ARGS args))objc_msgSend)(obj, sel(name), ##__VA_ARGS__)
#define call_stret(obj, name, ret, args, ...) ((ret (*) (id, SEL ARGS args))objc_msgSend_stret)(obj, sel(name), ##__VA_ARGS__)
#define call_super(obj, cls, sel, ret, args, ...) ((ret (*) (struct objc_super *, SEL ARGS args))objc_msgSendSuper)(&(struct objc_super) { obj, (Class)cls }, sel, ##__VA_ARGS__)
#define class(name) objc_getClass(name)
#define sel(name) sel_registerName(name)
#define alloc(name) call((id)class(name), "alloc", id, ())
#define init(obj) call(obj, "init", id, ())
#define retain(obj) call(obj, "retain", void, ())
#define release(obj) call(obj, "release", void, ())
#define string(s) call(alloc("NSString"), "initWithUTF8String:", id, (const char *), s)
#define ivar(obj, name, type) *(type *)(((void *)(obj)) + ivar_getOffset(class_getInstanceVariable(object_getClass(obj), name)))
typedef void (*BlockFunc)(void);
// https://clang.llvm.org/docs/Block-ABI-Apple.html
typedef struct {
void *isa;
int flags;
int reserved;
BlockFunc invoke;
void *desc;
void *data;
void *next;
} Block;
enum {
NSAlertStyleWarning = 0,
NSAlertStyleInformational = 1,
NSAlertStyleCritical = 2
};
enum {
NSWindowStyleMaskTitled = 0x01,
NSWindowStyleMaskClosable = 0x02,
NSWindowStyleMaskMiniaturizable = 0x04,
NSWindowStyleMaskResizable = 0x08
};
enum {
NSViewWidthSizable = 0x02,
NSViewHeightSizable = 0x10
};
enum {
NSBackingStoreBuffered = 2
};
enum {
NSBezelStyleRounded = 1,
NSBezelStyleRegularSquare = 2,
NSBezelStyleDisclosure = 5,
NSBezelStyleShadowlessSquare = 6,
NSBezelStyleCircular = 7,
NSBezelStyleTexturedSquare = 8,
NSBezelStyleHelpButton = 9,
NSBezelStyleSmallSquare = 10,
NSBezelStyleTexturedRounded = 11,
NSBezelStyleRoundRect = 12,
NSBezelStyleRecessed = 13,
NSBezelStyleRoundedDisclosure = 14,
NSBezelStyleInline = 15 // 10.7+
};
enum {
NSControlSizeRegular = 0,
NSControlSizeSmall = 1,
NSControlSizeMini = 2
};
enum {
NSBitmapFormatAlphaFirst = 1 << 0,
NSBitmapFormatThirtyTwoBitLittleEndian = 1 << 9,
NSBitmapFormatThirtyTwoBitBigEndian = 1 << 11
};
enum {
kCGImageAlphaPremultipliedLast = 1,
kCGImageAlphaPremultipliedFirst = 2,
kCGBitmapByteOrder32Little = 2 << 12,
kCGBitmapByteOrder32Big = 4 << 12,
kCGRenderingIntentDefault = 0,
NSCompositeCopy = 1,
NSCompositeSourceOver = 2
};
enum {
NSEventTypeLeftMouseDown = 1,
NSEventTypeLeftMouseUp = 2,
NSEventTypeRightMouseDown = 3,
NSEventTypeRightMouseUp = 4,
NSEventTypeMouseMoved = 5,
NSEventTypeLeftMouseDragged = 6,
NSEventTypeRightMouseDragged = 7,
NSEventTypeMouseEntered = 8,
NSEventTypeMouseExited = 9,
NSEventTypeKeyDown = 10,
NSEventTypeKeyUp = 11,
NSEventTypeFlagsChanged = 12,
NSEventTypeAppKitDefined = 13,
NSEventTypeSystemDefined = 14,
NSEventTypeApplicationDefined = 15,
NSEventTypePeriodic = 16,
NSEventTypeCursorUpdate = 17,
NSEventTypeScrollWheel = 22,
NSEventTypeTabletPoint = 23,
NSEventTypeTabletProximity = 24,
NSEventTypeOtherMouseDown = 25,
NSEventTypeOtherMouseUp = 26,
NSEventTypeOtherMouseDragged = 27
};
enum {
NSEventModifierFlagShift = 1 << 17,
NSEventModifierFlagControl = 1 << 18,
NSEventModifierFlagOption = 1 << 19,
NSEventModifierFlagCommand = 1 << 20
};
enum {
NSTrackingMouseEnteredAndExited = 0x01,
NSTrackingMouseMoved = 0x02,
NSTrackingCursorUpdate = 0x04,
NSTrackingActiveWhenFirstResponder = 0x10,
NSTrackingActiveInKeyWindow = 0x20,
NSTrackingActiveInActiveApp = 0x40,
NSTrackingActiveAlways = 0x80,
NSTrackingAssumeInside = 0x100,
NSTrackingInVisibleRect = 0x200,
NSTrackingEnabledDuringMouseDrag = 0x400
};
#ifdef __LP64__
typedef double CGFloat;
#else
typedef float CGFloat;
#endif
typedef struct {
CGFloat x, y;
} CGPoint;
typedef struct {
CGFloat width, height;
} CGSize;
typedef struct {
CGPoint origin;
CGSize size;
} CGRect;
typedef CGPoint NSPoint;
typedef CGSize NSSize;
typedef CGRect NSRect;
typedef long NSInteger;
typedef unsigned long NSUInteger;
typedef struct {
NSUInteger location;
NSUInteger length;
} NSRange;
typedef void *CGColorSpaceRef;
typedef void *CGContextRef;
typedef void *CGImageRef;
typedef void *CGDataProviderRef;
typedef void (*CGDataProviderReleaseDataCallback)(void *info, const void *data, size_t size);
typedef void *CFStringRef;
struct View {
ViewCommon common;
id obj;
Rect rect;
union {
struct {
int flags;
int close_requested;
Menu *menu;
} window;
struct {
id obj;
} label;
struct {
int flags;
id wrapper;
id obj;
id scroll_view;
struct {
int pos, max;
int always_show;
} scroll[2];
int placed;
int focusable;
int send_leave;
int cursor;
} canvas;
struct {
id obj;
} text_area;
struct {
id obj, data_obj;
int num_rows, num_columns;
char **data;
int dragged_column;
} table;
};
};
struct Menu {
MenuCommon common;
id obj;
int has_app_menu;
};
struct Worker {
WorkerCommon common;
};
typedef struct {
CGDataProviderRef provider;
CGImageRef img;
uint32_t *pixels;
} ImageData;
struct NotifyIcon {
NotifyIconCommon common;
id image;
CGColorSpaceRef space;
ImageData *images;
int num_images;
id item;
Menu *menu;
};
struct SystemFont {
id font;
};
typedef struct Timer {
Heap *heap;
Value instance;
id timer;
struct Timer *next;
} Timer;
enum {
MH_ABOUT,
MH_PREFERENCES,
MH_NUM_HANDLERS
};
extern const double NSAppKitVersionNumber;
#define NSAppKitVersionNumber10_7 1138
#define NSAppKitVersionNumber10_9 1265
#define NSAppKitVersionNumber10_10 1343
extern id NSCalibratedRGBColorSpace;
extern id NSFontAttributeName;
extern id NSForegroundColorAttributeName;
extern id NSBackgroundColorAttributeName;
extern CFStringRef kCGColorSpaceSRGB;
static Block *free_blocks = NULL;
static int main_argc;
static char **main_argv;
static char *exec_path;
void NSRectFill(NSRect rect);
void CGContextSetRGBFillColor(CGContextRef c, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha);
void CGContextFillRect(CGContextRef c, CGRect rect);
void CGContextShowTextAtPoint(CGContextRef c, CGFloat x, CGFloat y, const char *string, size_t length);
void CGContextSelectFont(CGContextRef c, const char *name, CGFloat size, int32_t textEncoding);
void CGContextSaveGState(CGContextRef c);
void CGContextRestoreGState(CGContextRef c);
CGColorSpaceRef CGColorSpaceCreateDeviceRGB();
CGColorSpaceRef CGColorSpaceCreateWithName(CFStringRef name);
void CGColorSpaceRelease(CGColorSpaceRef space);
CGContextRef CGBitmapContextCreate(void *data, size_t width, size_t height, size_t bitsPerComponent, size_t bytesPerRow, CGColorSpaceRef space, uint32_t bitmapInfo);
CGDataProviderRef CGDataProviderCreateWithData(void *info, const void *data, size_t size, CGDataProviderReleaseDataCallback releaseData);
void CGDataProviderRelease(CGDataProviderRef provider);
CGImageRef CGBitmapContextCreateImage(CGContextRef context);
CGImageRef CGImageCreate(size_t width, size_t height, size_t bitsPerComponent, size_t bitsPerPixel, size_t bytesPerRow, CGColorSpaceRef space, uint32_t bitmapInfo, CGDataProviderRef provider, const CGFloat *decode, bool shouldInterpolate, int32_t intent);
void CGImageRelease(CGImageRef image);
void CGContextRelease(CGContextRef c);
static char *app_name;
static id cursors[NUM_CURSORS];
static struct {
Heap *heap;
Value func;
Value data;
} menu_handlers[MH_NUM_HANDLERS];
static id default_menubar;
static Menu *main_menubar;
static int menubar_set;
static Timer *active_timers;
static int create_search_field = 0;
static id preview_panel = NULL;
static id preview_data_source = NULL;
static char *preview_panel_path = NULL;
void fixgui__tls_init();
static Block *get_block(BlockFunc func, void *data)
{
Block *block;
if (free_blocks) {
block = free_blocks;
free_blocks = block->next;
}
else {
block = calloc(1, sizeof(Block));
}
block->isa = class("__NSGlobalBlock__");
block->flags = 1<<28; // global block
block->invoke = func;
block->data = data;
return block;
}
static void release_block(Block *block)
{
block->next = free_blocks;
free_blocks = block;
}
void trigger_delayed_gc(Heap *heap)
{
}
void free_view(View *view)
{
int i;
switch (view->common.type) {
case TYPE_TABLE: {
for (i=0; i<view->table.num_columns*view->table.num_rows; i++) {
free(view->table.data[i]);
}
free(view->table.data);
view->table.num_columns = 0;
view->table.num_rows = 0;
view->table.data = NULL;
release(view->table.data_obj);
break;
}
}
free(view);
}
void free_menu(Menu *menu)
{
free(menu);
}
void free_notify_icon(NotifyIcon *icon)
{
ImageData *idat;
int i;
release(icon->item);
if (icon->num_images > 0) {
for (i=0; i<icon->num_images; i++) {
idat = &icon->images[i];
CGImageRelease(idat->img);
CGDataProviderRelease(idat->provider);
free(idat->pixels);
}
CGColorSpaceRelease(icon->space);
}
free(icon);
}
void view_destroy(View *view)
{
switch (view->common.type) {
case TYPE_WINDOW: {
if (view->window.close_requested) {
view->window.close_requested = 2;
}
else {
call(view->obj, "close", void, ());
}
break;
}
}
}
static void flip_rect(NSRect *rect, CGFloat parent_height)
{
rect->origin.y = parent_height - rect->size.height - rect->origin.y;
}
static void flip_screen_rect(NSRect *rect)
{
NSRect frame;
id screens, screen;
screens = call((id)class("NSScreen"), "screens", id, ());
screen = call(screens, "objectAtIndex:", id, (int), 0);
frame = call_stret(screen, "frame", NSRect, ());
flip_rect(rect, frame.size.height);
}
static void from_nsrect(Rect *rect, NSRect ns, float scale)
{
rect->x1 = roundf(ns.origin.x * scale);
rect->y1 = roundf(ns.origin.y * scale);
rect->x2 = roundf((ns.origin.x + ns.size.width)*scale);
rect->y2 = roundf((ns.origin.y + ns.size.height)*scale);
}
static NSRect to_nsrect(Rect *rect, float scale)
{
NSRect ns;
ns.origin.x = rect->x1 / scale;
ns.origin.y = rect->y1 / scale;
ns.size.width = (rect->x2 - rect->x1) / scale;
ns.size.height = (rect->y2 - rect->y1) / scale;
return ns;
}
void view_get_rect(View *view, Rect *rect)
{
NSRect frame;
float scale;
if (view->common.type == TYPE_WINDOW) {
scale = view_get_scale(view);
frame = call_stret(view->obj, "frame", NSRect, ());
flip_screen_rect(&frame);
from_nsrect(rect, frame, scale);
}
else {
*rect = view->rect;
}
}
static void get_view_metrics(View *view, Rect *rect, int *width, int *height)
{
float scale = view_get_scale(view);
#define METRICS(w, h, left, top, right, bottom) \
{ \
if (width) *width = roundf(w * scale); \
if (height) *height = roundf(h * scale); \
if (rect) { \
rect->x1 -= roundf(left * scale); \
rect->y1 -= roundf(top * scale); \
rect->x2 += roundf(right * scale); \
rect->y2 += roundf(bottom * scale); \
} \
}
#define M1(w, h, left, top, right, bottom) \
{ \
if (control_size == NSControlSizeRegular) METRICS(w, h, left, top, right, bottom); \
}
#define M2(w, h, left, top, right, bottom) \
{ \
if (control_size == NSControlSizeSmall) METRICS(w, h, left, top, right, bottom); \
}
#define M3(w, h, left, top, right, bottom) \
{ \
if (control_size == NSControlSizeMini) METRICS(w, h, left, top, right, bottom); \
}
switch (view->common.type) {
case TYPE_LABEL:
case TYPE_TEXT_FIELD:
case TYPE_TEXT_AREA:
case TYPE_TABLE: {
METRICS(16, 21, 0, 0, 0, 0);
break;
}
case TYPE_BUTTON: {
int bezel_style = call(view->obj, "bezelStyle", int, ());
int control_size;
id cell;
if (call(view->obj, "respondsToSelector:", BOOL, (SEL), sel("controlSize"))) {
control_size = call(view->obj, "controlSize", int, ());
}
else {
cell = call(view->obj, "cell", id, ());
control_size = call(cell, "controlSize", int, ());
}
if (NSAppKitVersionNumber >= NSAppKitVersionNumber10_9) {
switch (bezel_style) {
default:
case NSBezelStyleRounded: M1(70,21, 6,4,6,7); M2(60,18, 5,4,5,6); M3(55,15, 1,0,1,1); break; // Push Button
case NSBezelStyleRegularSquare: M1(60,59, 2,2,2,3); M2(60,59, 2,2,2,3); M3(60,59, 2,2,2,3); break; // Bevel Button
case NSBezelStyleDisclosure: M1(13,13, 0,0,0,0); M2(13,13, 0,0,0,0); M3(13,13, 0,0,0,0); break; // Disclosure Triangle
case NSBezelStyleShadowlessSquare: M1(48,48, 0,0,0,0); M2(48,48, 0,0,0,0); M3(48,48, 0,0,0,0); break; // Square Button
case NSBezelStyleCircular: M1(26,26, 6,3,7,9); M2(20,20, 6,5,6,7); M3(17,17, 6,3,7,10); break; // Round Button
case NSBezelStyleTexturedSquare: M1(70,20, 0,1,0,2); M2(60,18, 0,0,0,1); M3(55,15, 0,1,0,1); break; // Textured Button
case NSBezelStyleHelpButton: M1(21,21, 2,1,2,3); M2(18,18, 2,1,2,3); M3(15,15, 1,2,3,2); break; // Help Button
case NSBezelStyleSmallSquare: M1(30,24, 0,1,0,1); M2(26,21, 0,1,0,1); M3(18,15, 0,1,0,1); break; // Gradient Button
case NSBezelStyleTexturedRounded: M1(70,22, 0,1,0,2); M2(60,18, 0,0,0,1); M3(55,15, 0,1,0,1); break; // Rounded Textured Button
case NSBezelStyleRoundRect: M1(18,18, 0,0,0,1); M2(16,16, 0,0,0,1); M3(14,14, 0,1,0,2); break; // Rounded Rect Button
case NSBezelStyleRecessed: M1(70,18, 0,0,0,1); M2(60,16, 0,0,0,1); M3(55,14, 0,1,0,2); break; // Recessed Button
case NSBezelStyleRoundedDisclosure: M1(21,21, 4,2,4,3); M2(19,18, 3,2,3,3); M3(15,15, 1,0,1,1); break; // Disclosure Button
case NSBezelStyleInline: M1(70,14, 0,0,0,1); M2(60,14, 0,0,0,1); M3(55,14, 0,0,0,1); break; // Inline Button
}
}
else {
switch (bezel_style) {
default:
case NSBezelStyleRounded: M1(84,20, 6,4,6,8); M2(72,17, 5,4,5,7); M3(60,14, 1,0,1,2); break; // Push Button
case NSBezelStyleRegularSquare: M1(60,58, 2,2,2,4); M2(60,58, 2,2,2,4); M3(60,58, 2,2,2,4); break; // Bevel Button
case NSBezelStyleDisclosure: M1(13,13, 0,0,0,0); M2(13,13, 0,0,0,0); M3(13,13, 0,0,0,0); break; // Disclosure Triangle
case NSBezelStyleShadowlessSquare: M1(48,48, 0,0,0,0); M2(48,48, 0,0,0,0); M3(48,48, 0,0,0,0); break; // Square Button
case NSBezelStyleCircular: M1(26,27, 6,4,7,7); M2(20,21, 6,5,6,6); M3(17,18, 6,5,7,7); break; // Round Button
case NSBezelStyleTexturedSquare: M1(84,21, 1,1,1,1); M2(72,16, 0,1,0,1); M3(60,16, 1,1,1,1); break; // Textured Button
case NSBezelStyleHelpButton: M1(19,20, 3,1,3,4); M2(19,20, 3,1,3,4); M3(19,20, 3,1,3,4); break; // Help Button
case NSBezelStyleSmallSquare: M1(30,24, 0,1,0,1); M2(26,21, 0,1,0,1); M3(18,15, 0,1,0,1); break; // Gradient Button
case NSBezelStyleTexturedRounded: M1(84,22, 0,1,0,2); M2(72,18, 0,0,0,0); M3(60,15, 0,0,0,0); break; // Rounded Textured Button
case NSBezelStyleRoundRect: M1(30,17, 0,0,0,2); M2(27,15, 0,0,0,2); M3(25,14, 0,1,0,2); break; // Rounded Rect Button
case NSBezelStyleRecessed: M1(84,17, 0,0,0,2); M2(72,15, 0,0,0,2); M3(60,15, 0,0,0,2); break; // Recessed Button
case NSBezelStyleRoundedDisclosure: M1(21,21, 4,2,4,3); M2(19,18, 3,2,3,3); M3(17,16, 0,0,0,0); break; // Disclosure Button
case NSBezelStyleInline: M1(84,14, 0,0,0,1); M2(72,14, 0,0,0,1); M3(60,14, 0,0,0,1); break; // Inline Button
}
}
break;
}
}
#undef METRICS
#undef M1
#undef M2
#undef M3
}
static void center_label(View *view)
{
NSRect frame;
frame = call_stret(view->obj, "frame", NSRect, ());
frame.origin.x = 0;
frame.origin.y = (frame.size.height - 17) / 2;
frame.size.height = 17;
call(view->label.obj, "setFrame:", void, (NSRect), frame);
}
void view_set_rect(View *view, Rect *rect)
{
NSRect frame, superframe;
id superview;
float scale;
if (view->common.type == TYPE_WINDOW) {
scale = view_get_scale(view);
frame = to_nsrect(rect, scale);
flip_screen_rect(&frame);
call(view->obj, "setFrame:display:", void, (NSRect, BOOL), frame, 0);
}
else {
view->rect = *rect;
if (view->common.parent) {
scale = view_get_scale(view);
superview = call(view->obj, "superview", id, ());
superframe = call_stret(superview, "frame", NSRect, ());
get_view_metrics(view, rect, NULL, NULL);
frame = to_nsrect(rect, scale);
flip_rect(&frame, superframe.size.height);
call(view->obj, "setFrame:", void, (NSRect), frame);
if (view->common.type == TYPE_CANVAS && (view->canvas.flags & CANVAS_SCROLLABLE) != 0) {
NSRect rect;
call(view->obj, "setHasHorizontalScroller:", void, (BOOL), (view->canvas.scroll[0].max > 0 || view->canvas.scroll[0].always_show) != 0);
call(view->obj, "setHasVerticalScroller:", void, (BOOL), (view->canvas.scroll[1].max > 0 || view->canvas.scroll[1].always_show) != 0);
rect.origin.x = 0;
rect.origin.y = 0;
#ifdef __LP64__
rect.size = call(view->obj, "contentSize", NSSize, ());
#else
rect.size = call_stret(view->obj, "contentSize", NSSize, ());
#endif
rect.size.width += view->canvas.scroll[0].max / scale;
rect.size.height += view->canvas.scroll[1].max / scale;
call(view->canvas.wrapper, "setFrame:", void, (NSRect), rect);
call(view->canvas.obj, "setFrame:", void, (NSRect), rect);
if (!view->canvas.placed) {
NSPoint point;
view->canvas.placed = 1;
point.x = view->canvas.scroll[0].pos / scale;
point.y = view->canvas.scroll[1].pos / scale;
call(view->canvas.wrapper, "scrollPoint:", void, (NSPoint), point);
}
}
if (view->common.type == TYPE_LABEL) {
center_label(view);
}
if (view->common.type == TYPE_CANVAS) {
call_view_callback(view, CALLBACK_CANVAS_RESIZE);
}
}
}
}
void view_get_content_rect(View *view, Rect *rect)
{
NSRect frame, parent_frame;
float scale;
id content;
rect->x1 = 0;
rect->y1 = 0;
rect->x2 = 0;
rect->y2 = 0;
if (view->common.type == TYPE_WINDOW) {
scale = view_get_scale(view);
content = call(view->obj, "contentView", id, ());
frame = call_stret(content, "frame", NSRect, ());
parent_frame = call_stret(view->obj, "frame", NSRect, ());
flip_rect(&frame, parent_frame.size.height);
from_nsrect(rect, frame, scale);
}
}
void view_get_inner_rect(View *view, Rect *rect)
{
view_get_content_rect(view, rect);
}
void view_set_visible(View *view, int visible)
{
if (view->common.type == TYPE_WINDOW) {
if (visible) {
call_view_callback(view, CALLBACK_WINDOW_RESIZE);
if (view->window.flags & WIN_MAXIMIZE) {
call(view->obj, "makeKeyAndOrderFront:", void, (id), NULL);
call(view->obj, "zoom:", void, (id), NULL);
}
else if (view->window.flags & WIN_MINIMIZE) {
call(view->obj, "miniaturize:", void, (id), NULL);
}
else {
call(view->obj, "makeKeyAndOrderFront:", void, (id), NULL);
}
}
}
}
int view_add(View *parent, View *view)
{
NSRect super_frame, frame;
Rect rect;
id superview;
float scale;
superview = parent->obj;
if (parent->common.type == TYPE_WINDOW) {
superview = call(superview, "contentView", id, ());
}
scale = view_get_scale(parent);
super_frame = call_stret(superview, "frame", NSRect, ());
rect = view->rect;
get_view_metrics(view, &rect, NULL, NULL);
frame = to_nsrect(&rect, scale);
flip_rect(&frame, super_frame.size.height);
call(view->obj, "setFrame:", void, (NSRect), frame);
if (view->common.type == TYPE_LABEL) {
center_label(view);
}
call(superview, "addSubview:", void, (id), view->obj);
return 1;
}
void view_focus(View *view)
{
View *top = view;
id obj;
while (top->common.parent) {
top = top->common.parent;
}
if (top->common.type != TYPE_WINDOW) return;
if (top == view) return;
obj = view->obj;
if (view->common.type == TYPE_CANVAS) {
obj = view->canvas.obj;
}
if (call(obj, "acceptsFirstResponder", BOOL, ())) {
call(top->obj, "makeFirstResponder:", BOOL, (id), obj);
}
}
int view_has_focus(View *view)
{
View *top = view;
id obj;
while (top->common.parent) {
top = top->common.parent;
}
if (top->common.type != TYPE_WINDOW) return 0;
if (top == view) return 0;
obj = view->obj;
if (view->common.type == TYPE_CANVAS) {
obj = view->canvas.obj;
}
return call(top->obj, "firstResponder", id, ()) == obj;
}
void view_get_sizing(View *view, float *grid_x, float *grid_y, int *form_small, int *form_medium, int *form_large, int *view_small, int *view_medium, int *view_large)
{
float scale = view_get_scale(view);
*grid_x = 5 * scale;
*grid_y = 5 * scale;
*form_small = roundf(10 * scale);
*form_medium = roundf(20 * scale);
*form_large = roundf(30 * scale);
*view_small = roundf(12 * scale);
*view_medium = roundf(24 * scale);
*view_large = roundf(32 * scale);
}
void view_get_default_size(View *view, int *width, int *height)
{
get_view_metrics(view, NULL, width, height);
}
float view_get_scale(View *view)
{
View *top = view;
float scale = 1.0f;
id screen;
if (NSAppKitVersionNumber < NSAppKitVersionNumber10_7) {
return scale;
}
if (top) {
while (top->common.parent) {
top = top->common.parent;
}
}
if (top && top->common.type == TYPE_WINDOW) {
scale = call(top->obj, "backingScaleFactor", CGFloat, ());
}
else {
screen = call((id)class("NSScreen"), "mainScreen", id, ());
scale = call(screen, "backingScaleFactor", CGFloat, ());
}
return scale;
}
void view_set_cursor(View *view, int cursor)
{
if (view->common.type != TYPE_CANVAS) return; // TODO: add support for other views
if (cursor < 0 || cursor >= NUM_CURSORS) {
return;
}
if (view->canvas.cursor == cursor) {
//return;
}
view->canvas.cursor = cursor;
call(cursors[cursor], "set", void, ());
}
int view_get_cursor(View *view)
{
if (view->common.type == TYPE_CANVAS) {
return view->canvas.cursor;
}
return CURSOR_DEFAULT;
}
View *window_create(plat_char *title, int width, int height, int flags)
{
View *view;
NSRect rect;
NSSize size;
id delegate, str;
float scale;
int style;
view = calloc(1, sizeof(View));
if (!view) return NULL;
scale = view_get_scale(NULL);
rect.origin.x = 0;
rect.origin.y = 0;
rect.size.width = width / scale;
rect.size.height = height / scale;
style = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable;
if (flags & WIN_RESIZABLE) {
style |= NSWindowStyleMaskResizable;
}
view->obj = call(alloc("NSWindow"), "initWithContentRect:styleMask:backing:defer:", id, (NSRect, uint32_t, int, BOOL),
rect,
style,
NSBackingStoreBuffered,
0
);
view->window.flags = flags;
delegate = init(alloc("WindowDelegate"));
ivar(delegate, "window_view", void *) = view;
call(view->obj, "setDelegate:", void, (id), delegate);
str = string(title);
call(view->obj, "setTitle:", void, (id), str);
release(str);
size.width = width / scale;
size.height = height / scale;
call(view->obj, "setContentSize:", void, (NSSize), size);
if (flags & WIN_CENTER) {
call(view->obj, "center", void, ());
}
return view;
}
plat_char *window_get_title(View *view)
{
const char *s;
id str;
str = call(view->obj, "title", id, ());
s = call(str, "UTF8String", const char *, ());
return strdup(s);
}
void window_set_title(View *view, plat_char *title)
{
id str;
str = string(title);
call(view->obj, "setTitle:", void, (id), str);
release(str);
}
void window_set_minimum_size(View *view, int width, int height)
{
NSSize size;
float scale;
scale = view_get_scale(view);
size.width = width / scale;
size.height = height / scale;
call(view->obj, "setContentMinSize:", void, (NSSize), size);
}
int window_is_maximized(View *view)
{
return call(view->obj, "isZoomed", BOOL, ());
}
void window_set_status_text(View *view, plat_char *text)
{
}
static void add_menu_item(id menu, char *title, char *key, int mod, SEL sel, id submenu)
{
id str1, str2, item;
if (!title) {
item = call((id)class("NSMenuItem"), "separatorItem", id, ());
call(menu, "addItem:", void, (id), item);
return;
}
str1 = string(title);
str2 = string(key);
item = call(menu, "addItemWithTitle:action:keyEquivalent:", id, (id, SEL, id),
str1, sel, str2
);
if (mod) {
call(item, "setKeyEquivalentModifierMask:", void, (NSUInteger), mod);
}
if (submenu) {
call(item, "setSubmenu:", void, (id), submenu);
}
release(str1);
release(str2);
}
static void insert_app_menu(id menu)
{
char buf[256];
id app, appmenu, str, services, item;
app = call((id)class("NSApplication"), "sharedApplication", id, ());