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

/* Cocoa text label */

#include "oslabel_osx.inl"
#include "oslistener.inl"
#include "oscontrol_osx.inl"
#include "ospanel_osx.inl"
#include "../oslabel.h"
#include <geom2d/t2d.h>
#include <draw2d/color.h>
#include <draw2d/dctx.h>
#include <draw2d/dctxh.h>
#include <draw2d/draw.h>
#include <draw2d/font.h>
#include <core/event.h>
#include <core/heap.h>
#include <core/strings.h>
#include <sewer/cassert.h>

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

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

@interface OSXLabel : NSView

{
  @public
    DCtx *ctx;
    String *text;
    uint32_t flags;
    color_t color;
    color_t bgcolor;
    NSTrackingArea *tracking_area;
    Listener *OnClick;
    Listener *OnMouseEntered;
    Listener *OnMouseExited;
}
@end

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

@implementation OSXLabel

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

- (void)mouseEntered:(NSEvent *)theEvent
{
    cassert_no_null(theEvent);
    if (self->OnMouseEntered != NULL)
    {
        EvMouse params;
        _oslistener_mouse_position_in_view_coordinates(self, [theEvent locationInWindow], &params.x, &params.y);
        params.lx = params.x;
        params.ly = params.y;
        params.button = ENUM_MAX(gui_mouse_t);
        params.count = 0;
        params.modifiers = 0;
        params.tag = 0;
        listener_event(self->OnMouseEntered, ekGUI_EVENT_ENTER, cast(self, OSLabel), &params, NULL, OSLabel, EvMouse, void);
    }
}

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

- (void)mouseExited:(NSEvent *)theEvent
{
    unref(theEvent);
    if (self->OnMouseExited != NULL)
        listener_event(self->OnMouseExited, ekGUI_EVENT_EXIT, cast(self, OSLabel), NULL, NULL, OSLabel, void, void);
}

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

- (void)mouseUp:(NSEvent *)theEvent
{
    unref(theEvent);
    if (self->OnClick != NULL)
    {
        EvText params;
        params.text = NULL;
        listener_event(self->OnClick, ekGUI_EVENT_LABEL, cast(self, OSLabel), &params, NULL, OSLabel, EvText, void);
    }
}

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

- (BOOL)isFlipped
{
    return YES;
}

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

- (void)drawRect:(NSRect)rect
{
    NSGraphicsContext *nscontext;
    unref(rect);
    cassert_no_null(self->ctx);
    nscontext = [NSGraphicsContext currentContext];
    dctx_set_gcontext(self->ctx, nscontext, (uint32_t)rect.size.width, (uint32_t)rect.size.height, 0, 0, 0, FALSE);
    if (self->bgcolor != kCOLOR_DEFAULT)
    {
        draw_fill_color(self->ctx, self->bgcolor);
        draw_rect(self->ctx, ekFILL, 0, 0, (real32_t)rect.size.width, (real32_t)rect.size.height);
    }

    if (self->color != kCOLOR_DEFAULT)
        draw_text_color(self->ctx, color);
    else
        draw_text_color(self->ctx, ekSYSCOLOR_LABEL);

    switch (label_get_type(self->flags))
    {
    case ekLABEL_SINGLE:
        draw_text_single_line(self->ctx, tc(self->text), 0, 0);
        break;
    case ekLABEL_MULTI:
        draw_text(self->ctx, tc(self->text), 0, 0);
        break;
        cassert_default();
    }

    dctx_unset_gcontext(ctx);
}

@end

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

OSLabel *oslabel_create(const uint32_t flags)
{
    OSXLabel *label = nil;
    heap_auditor_add("OSXLabel");
    label = [[OSXLabel alloc] initWithFrame:NSZeroRect];

    /* https://developer.apple.com/documentation/macos-release-notes/appkit-release-notes-for-macos-14#NSView */
#if defined(MAC_OS_VERSION_14_0) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_14
    [label setClipsToBounds:YES];
#endif

    _oscontrol_init(label);
    label->ctx = dctx_create();
    label->flags = flags;
    dctx_set_flipped(label->ctx, (bool_t)[label isFlipped]);
    label->text = str_c("");
    label->color = kCOLOR_DEFAULT;
    label->bgcolor = kCOLOR_DEFAULT;
    draw_text_align(label->ctx, ekLEFT, ekTOP);
    draw_text_width(label->ctx, -1);
    draw_text_halign(label->ctx, ekLEFT);
    label->tracking_area = nil;
    label->OnClick = NULL;
    label->OnMouseEntered = NULL;
    label->OnMouseExited = NULL;
    return cast(label, OSLabel);
}

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

void oslabel_destroy(OSLabel **label)
{
    OSXLabel *llabel = nil;
    cassert_no_null(label);
    llabel = *dcast(label, OSXLabel);
    cassert_no_null(llabel);
    listener_destroy(&llabel->OnClick);
    listener_destroy(&llabel->OnMouseEntered);
    listener_destroy(&llabel->OnMouseExited);
    str_destroy(&llabel->text);
    dctx_destroy(&llabel->ctx);

    if (llabel->tracking_area != nil)
    {
        [llabel removeTrackingArea:llabel->tracking_area];
        [llabel->tracking_area release];
    }

    [llabel release];
    *label = NULL;
    heap_auditor_delete("OSXLabel");
}

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

static bool_t i_is_mouse_sensible(OSXLabel *label)
{
    cassert_no_null(label);
    if (label->OnClick != NULL)
        return TRUE;
    if (label->OnMouseEntered != NULL)
        return TRUE;
    if (label->OnMouseExited != NULL)
        return TRUE;
    return FALSE;
}

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

static void i_update_tracking_area(OSXLabel *label)
{
    bool_t with_area = i_is_mouse_sensible(label);

    if (label->tracking_area != nil && with_area == TRUE)
    {
        NSSize required_size = [label frame].size;
        NSSize current_size = [label->tracking_area rect].size;
        if (NSEqualSizes(required_size, current_size) == NO)
        {
            [label removeTrackingArea:label->tracking_area];
            [label->tracking_area release];
            label->tracking_area = [[NSTrackingArea alloc] initWithRect:NSMakeRect(0.f, 0.f, required_size.width, required_size.height) options:(NSTrackingAreaOptions)(NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:label userInfo:nil];
            [label addTrackingArea:label->tracking_area];
        }

        return;
    }

    if (label->tracking_area != nil)
    {
        [label removeTrackingArea:label->tracking_area];
        [label->tracking_area release];
        label->tracking_area = nil;
    }

    if (with_area == TRUE)
    {
        NSSize size = [label frame].size;
        label->tracking_area = [[NSTrackingArea alloc] initWithRect:NSMakeRect(0.f, 0.f, size.width, size.height) options:(NSTrackingAreaOptions)(NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:label userInfo:nil];
        [label addTrackingArea:label->tracking_area];
    }
}

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

void oslabel_OnClick(OSLabel *label, Listener *listener)
{
    OSXLabel *llabel = cast(label, OSXLabel);
    cassert_no_null(llabel);
    listener_update(&llabel->OnClick, listener);
    i_update_tracking_area(llabel);
}

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

void oslabel_OnEnter(OSLabel *label, Listener *listener)
{
    OSXLabel *llabel = cast(label, OSXLabel);
    cassert_no_null(llabel);
    listener_update(&llabel->OnMouseEntered, listener);
    i_update_tracking_area(llabel);
}

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

void oslabel_OnExit(OSLabel *label, Listener *listener)
{
    OSXLabel *llabel = cast(label, OSXLabel);
    cassert_no_null(llabel);
    listener_update(&llabel->OnMouseExited, listener);
    i_update_tracking_area(llabel);
}

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

void oslabel_text(OSLabel *label, const char_t *text)
{
    OSXLabel *llabel = cast(label, OSXLabel);
    cassert_no_null(llabel);
    str_upd(&llabel->text, text);
    [llabel setNeedsDisplay:YES];
}

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

void oslabel_font(OSLabel *label, const Font *font)
{
    OSXLabel *llabel = cast(label, OSXLabel);
    cassert_no_null(llabel);
    draw_font(llabel->ctx, font);
    [llabel setNeedsDisplay:YES];
}

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

void oslabel_flags(OSLabel *label, const uint32_t flags)
{
    OSXLabel *llabel = cast(label, OSXLabel);
    cassert_no_null(llabel);
    llabel->flags = flags;
}

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

void oslabel_align(OSLabel *label, const align_t align)
{
    OSXLabel *llabel = cast(label, OSXLabel);
    cassert_no_null(llabel);
    draw_text_halign(llabel->ctx, align);
    [llabel setNeedsDisplay:YES];
}

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

void oslabel_ellipsis(OSLabel *label, const ellipsis_t ellipsis)
{
    OSXLabel *llabel = cast(label, OSXLabel);
    cassert_no_null(llabel);
    draw_text_trim(llabel->ctx, ellipsis);
    [llabel setNeedsDisplay:YES];
}

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

void oslabel_color(OSLabel *label, const color_t color)
{
    OSXLabel *llabel = cast(label, OSXLabel);
    cassert_no_null(llabel);
    llabel->color = color;
    [llabel setNeedsDisplay:YES];
}

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

void oslabel_bgcolor(OSLabel *label, const color_t color)
{
    OSXLabel *llabel = cast(label, OSXLabel);
    cassert_no_null(llabel);
    llabel->bgcolor = color;
    [llabel setNeedsDisplay:YES];
}

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

void oslabel_bounds(const OSLabel *label, const char_t *text, const real32_t refwidth, real32_t *width, real32_t *height)
{
    OSXLabel *llabel = cast(label, OSXLabel);
    cassert_no_null(llabel);
    draw_text_extents(llabel->ctx, text, refwidth, width, height);
}

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

void oslabel_attach(OSLabel *label, OSPanel *panel)
{
    _ospanel_attach_control(panel, cast(label, NSView));
}

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

void oslabel_detach(OSLabel *label, OSPanel *panel)
{
    _ospanel_detach_control(panel, cast(label, NSView));
}

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

void oslabel_visible(OSLabel *label, const bool_t visible)
{
    _oscontrol_set_visible(cast(label, NSView), visible);
}

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

void oslabel_enabled(OSLabel *label, const bool_t enabled)
{
    unref(label);
    unref(enabled);
}

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

void oslabel_size(const OSLabel *label, real32_t *width, real32_t *height)
{
    _oscontrol_get_size(cast(label, NSView), width, height);
}

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

void oslabel_origin(const OSLabel *label, real32_t *x, real32_t *y)
{
    _oscontrol_get_origin(cast(label, NSView), x, y);
}

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

void oslabel_frame(OSLabel *label, const real32_t x, const real32_t y, const real32_t width, const real32_t height)
{
    OSXLabel *llabel = cast(label, OSXLabel);
    cassert_no_null(llabel);
    _oscontrol_set_frame(llabel, x, y, width, height);
    draw_text_width(llabel->ctx, width);
    i_update_tracking_area(llabel);
    [llabel setNeedsDisplay:YES];
}

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

BOOL _oslabel_is(NSView *view)
{
    return [view isKindOfClass:[OSXLabel class]];
}