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

/* Operating System native common windows */

#include "oscontrol_osx.inl"
#include "oscomwin.inl"
#include "../oscomwin.h"
#include <core/event.h>
#include <core/strings.h>
#include <sewer/cassert.h>

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

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

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

#include <UniformTypeIdentifiers/UniformTypeIdentifiers.h>

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

static void i_set_ftypes(NSSavePanel *panel, const char_t **ftypes, const uint32_t size)
{
    NSMutableArray< UTType * > *array = [NSMutableArray array];

    if (ftypes != NULL && size > 0)
    {
        uint32_t i;
        for (i = 0; i < size; ++i)
        {
            NSString *ext = [NSString stringWithUTF8String:ftypes[i]];
            UTType *type = [UTType typeWithFilenameExtension:ext];
            if (type != nil)
                [array addObject:type];
        }
    }

    [panel setAllowedContentTypes:array];
}

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

#else

static void i_set_ftypes(NSSavePanel *panel, const char_t **ftypes, const uint32_t size)
{
    if (ftypes != NULL && size > 0)
    {
        uint32_t i;
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:(NSUInteger)size];
        for (i = 0; i < size; ++i)
        {
            NSString *str = [NSString stringWithUTF8String:(const char *)ftypes[i]];
            [array addObject:str];
        }

        [panel setAllowedFileTypes:array];
    }
    else
    {
        [panel setAllowedFileTypes:nil];
    }
}

#endif

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

static NSOpenPanel *i_open_file(const char_t **ftypes, const uint32_t size, const char_t *startdir)
{
    NSOpenPanel *open_panel = [NSOpenPanel openPanel];
    BOOL dirsel = NO;

#if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
    if (startdir != NULL)
    {
        NSString *str = [[NSString alloc] initWithUTF8String:startdir];
        NSURL *url = [[NSURL alloc] initWithString:str];
        [open_panel setDirectoryURL:url];
        [url release];
        [str release];
    }
    else
    {
        [open_panel setDirectoryURL:nil];
    }

#else
    unref(startdir);
#endif

    [open_panel setAllowsMultipleSelection:FALSE];
    if (ftypes != NULL)
    {
        cassert(size > 0);
        if (size == 1 && strcmp(cast_const(ftypes[0], char), "..DIR..") == 0)
            dirsel = YES;
    }

    i_set_ftypes(open_panel, ftypes, size);
    [open_panel setCanChooseFiles:!dirsel];
    [open_panel setCanChooseDirectories:dirsel];
    return open_panel;
}

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

static NSSavePanel *i_save_file(const char_t **ftypes, const uint32_t size)
{
    NSSavePanel *save_panel = [NSSavePanel savePanel];

    /* 10.5
       [save_panel setDirectoryURL:nil];
       cassert(FALSE);
     */

    [save_panel setCanCreateDirectories:YES];
    i_set_ftypes(save_panel, ftypes, size);
    return save_panel;
}

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

static const char_t *i_open_file_selected(NSOpenPanel *open_panel)
{
    NSArray *urls;
    NSURL *url;
    cassert_no_null(open_panel);
    urls = [open_panel URLs];
    cassert([urls count] == 1);
    url = [urls objectAtIndex:0];
    cassert_no_null(url);
    return cast_const([[url path] UTF8String], char_t);
}

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

static const char_t *i_save_file_selected(NSSavePanel *save_panel)
{
    NSURL *url;
    cassert_no_null(save_panel);
    url = [save_panel URL];
    cassert_no_null(url);
    return cast_const([[url path] UTF8String], char_t);
}

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

const char_t *oscomwin_file(OSWindow *parent, const char_t **ftypes, const uint32_t size, const char_t *start_dir, const bool_t foropen)
{
    unref(parent);
    if (foropen == TRUE)
    {
        NSOpenPanel *open_panel = i_open_file(ftypes, size, start_dir);

#if defined(MAC_OS_X_VERSION_10_9) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_9
        {
            NSModalResponse ret = [open_panel runModal];
            if (ret == NSModalResponseOK)
                return i_open_file_selected(open_panel);
            else
                return NULL;
        }
#else
        {
            NSUInteger ret = (NSUInteger)[open_panel runModal];
            if (ret == NSFileHandlingPanelOKButton)
                return i_open_file_selected(open_panel);
            else
                return NULL;
        }
#endif
    }
    else
    {
        NSSavePanel *save_panel = i_save_file(ftypes, size);

#if defined(MAC_OS_X_VERSION_10_9) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_9
        {
            NSModalResponse ret = [save_panel runModal];
            if (ret == NSModalResponseOK)
                return i_save_file_selected(save_panel);
            else
                return NULL;
        }
#else
        {
            NSUInteger ret = (NSUInteger)[save_panel runModal];
            if (ret == NSFileHandlingPanelOKButton)
                return i_save_file_selected(save_panel);
            else
                return NULL;
        }
#endif
    }
}

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

@interface NSColorChoose : NSObject

{
  @public
    Listener *OnChange;
}
@end

@implementation NSColorChoose

- (IBAction)onColorChange:(id)sender
{
    NSColor *color = [sender color];
    color_t c = _oscontrol_from_NSColor(color);
    listener_event(self->OnChange, ekGUI_EVENT_COLOR, NULL, &c, NULL, void, color_t, void);
}

- (void)dealloc
{
    listener_destroy(&self->OnChange);
    [super dealloc];
}

@end

static NSColorChoose *i_COLOR_CHOOSE = nil;

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

void oscomwin_color(OSWindow *parent, const char_t *title, const real32_t x, const real32_t y, const align_t halign, const align_t valign, const color_t current, color_t *colors, const uint32_t n, Listener *OnChange)
{
    NSColorPanel *panel = [NSColorPanel sharedColorPanel];

    if (str_empty_c(title) == FALSE)
        [panel setTitle:[NSString stringWithUTF8String:title]];

    if (i_COLOR_CHOOSE != nil)
    {
        [i_COLOR_CHOOSE release];
        i_COLOR_CHOOSE = nil;
    }

    i_COLOR_CHOOSE = [NSColorChoose alloc];
    i_COLOR_CHOOSE->OnChange = OnChange;
    [panel setTarget:i_COLOR_CHOOSE];
    [panel setAction:@selector(onColorChange:)];

    /*[NSColorPanel setPickerMode:NSColorPanelModeRGB];*/
    [panel setColor:_oscontrol_color(current)];
    [panel setShowsAlpha:YES];

    if ([panel isVisible] == NO)
    {
        NSPoint origin = NSMakePoint((CGFloat)x, (CGFloat)y);
        NSSize size = [panel frame].size;
        CGFloat sh = [[NSScreen mainScreen] frame].size.height;
        if (halign != ekLEFT || valign != ekTOP)
        {
            switch (halign)
            {
            case ekLEFT:
            case ekJUSTIFY:
                break;
            case ekCENTER:
                origin.x -= size.width / 2;
                break;
            case ekRIGHT:
                origin.x -= size.width;
                break;
                cassert_default();
            }

            switch (valign)
            {
            case ekTOP:
            case ekJUSTIFY:
                break;
            case ekCENTER:
                origin.x -= size.height / 2;
                break;
            case ekBOTTOM:
                origin.x -= size.height;
                break;
                cassert_default();
            }
        }

        origin.y = sh - origin.y - size.height;
        [panel setFrameOrigin:origin];
        [panel makeKeyAndOrderFront:(NSWindow *)parent];
    }

    /*
    //    ret = [NSApp runModalForWindow:panel];

    //#if defined (MAC_OS_X_VERSION_10_9) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_9
    //
    //
    ////        NSModalResponse ret = [panel runModalForWindow:nil];
    ////        if (ret == NSModalResponseOK)
    ////            return i_save_file_selected(save_panel);
    ////        else
    ////            return NULL;
    //    }
    //    #else
    //    {
    //        NSUInteger ret = (NSUInteger)[save_panel runModal];
    //        if (ret == NSFileHandlingPanelOKButton)
    //            return i_save_file_selected(save_panel);
    //        else
    //            return NULL;
    //    }
    //    #endif
     */
    unref(parent);
    unref(title);
    unref(x);
    unref(y);
    unref(halign);
    unref(valign);
    unref(current);
    unref(colors);
    unref(n);
}

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

void _oscomwin_destroy_globals(void)
{
    if (i_COLOR_CHOOSE != nil)
    {
        [i_COLOR_CHOOSE release];
        i_COLOR_CHOOSE = nil;
    }
}

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

/*
void oscommon_file(
                    OSWindow *owner_window,
                    const uchar_t **allowed_file_types,
                    const uint32_t num_file_types,
                    const bool_t for_open,
                    Listener *OnAccept_listener)
{
    cassert([(NSObject*)owner_window isKindOfClass:[OSXWindow class]] == YES);
    cassert_no_null(OnAccept_listener);
    unreferenced(OnAccept_listener);
    unreferenced(owner_window);
    if (for_open == TRUE)
    {
        NSOpenPanel *open_panel;
        open_panel = i_open_file_panel(allowed_file_types, num_file_types, NULL);
        [open_panel setLevel:1000];
        [open_panel setFloatingPanel:NO];

            // 10.5
      //  cassert(FALSE);


        [open_panel beginSheetModalForWindow:(NSWindow*)owner_window completionHandler:^(NSInteger result)
        {
            if (result == NSFileHandlingPanelOKButton)
            {
                Event event;
                EvFile params;
                event.type = ekGUI_EVENT_OPEN_FILE;
                event.sender = open_panel;
                event.params = &params;
                event.result = NULL;
                params.pathname = i_open_file_selected(open_panel);
                listener_launch_event(OnAccept_listener, &event);
            }
        }];

    }
    else
    {
        NSSavePanel *save_panel;
        save_panel = i_save_file_panel(allowed_file_types, num_file_types);
        [save_panel setLevel:1000];
        [save_panel setFloatingPanel:NO];

        // 10.5
    //    cassert(FALSE);

        [save_panel beginSheetModalForWindow:(NSWindow*)owner_window completionHandler:^(NSInteger result)
        {
            if (result == NSFileHandlingPanelOKButton)
            {
                Event event;
                EvFile params;
                event.type = ekGUI_EVENT_SAVE_FILE;
                event.sender = save_panel;
                event.params = &params;
                event.result = NULL;
                params.pathname = i_save_file_selected(save_panel);
                listener_launch_event(OnAccept_listener, &event);
            }
        }];*/
/*  }
}*/

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

/*
void oscommon_colour_close(void)
{
    [[NSColorPanel sharedColorPanel] orderOut:nil];
}*/

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

/*
void oscommon_colour_convert_to_hud(void)
{
    cassert(FALSE);
}*/

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

/*
void oscommon_colour_set_size(const real32_t width, const real32_t height)
{
    NSRect frame;
    frame = [NSColorPanel sharedColorPanel].frame;
    frame.origin.y += frame.size.height - (CGFloat)height;
    frame.size.width = (CGFloat)width;
    frame.size.height = (CGFloat)height;
    [[NSColorPanel sharedColorPanel] setFrame:frame display:YES animate:NO];
}*/

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

/*
void oscommon_colour_get_size(real32_t *width, real32_t *height)
{
    NSSize size;
    cassert_no_null(width);
    cassert_no_null(height);
    size = [[NSColorPanel sharedColorPanel] frame].size;
    *width = (real32_t)size.width;
    *height = (real32_t)size.height;
}*/

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

/*
void oscommon_colour_set_origin(const real32_t x, const real32_t y)
{
    NSRect window_frame;
    NSPoint origin;
    window_frame = [[NSColorPanel sharedColorPanel] frame];
    window_frame.origin.x = (CGFloat)x;
    window_frame.origin.y = (CGFloat)y;
    _oscontrol_origin_in_screen_coordinates(&window_frame, &origin.x, &origin.y);
    [[NSColorPanel sharedColorPanel] setFrameOrigin:origin];
}*/

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

/*
void oscommon_colour_get_origin(real32_t *x, real32_t *y)
{
    NSRect frame;
    NSPoint origin;
    cassert_no_null(x);
    cassert_no_null(y);
    frame = [[NSColorPanel sharedColorPanel] frame];
    _oscontrol_origin_in_screen_coordinates(&frame, &origin.x, &origin.y);
    *x = (real32_t)origin.x;
    *y = (real32_t)origin.y;
}*/