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

/* Windows controls commons */

#include "oscontrol_win.inl"
#include "osgui_win.inl"
#include "osbutton_win.inl"
#include "oscombo_win.inl"
#include "ospanel_win.inl"
#include "ospopup_win.inl"
#include "osweb_win.inl"
#include "ostooltip.inl"
#include "../oscontrol.inl"
#include "../osgui.inl"
#include <draw2d/color.h>
#include <draw2d/font.h>
#include <core/heap.h>
#include <sewer/cassert.h>
#include <sewer/ptr.h>
#include <sewer/unicode.h>

#if !defined(__WINDOWS__)
#error This file is only for Windows
#endif

#include <sewer/nowarn.hxx>
#include <math.h>
#include <sewer/warn.hxx>

typedef struct i_children_t i_Children;

struct i_children_t
{
    uint32_t num_children;
    HWND *children;
    uint32_t children_size;
};

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

static const unicode_t i_SYSTEM_FORMAT = ekUTF16;

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

static void i_init(OSControl *control, const DWORD dwExStyle, const DWORD dwStyle, const LPCWSTR lpClassName, int nWidth, int nHeight, WNDPROC wndProc, HWND parent_window)
{
    HINSTANCE instance = NULL;
    cassert_no_null(control);
    instance = _osgui_instance();
    cassert_no_null(instance);

    control->hwnd = CreateWindowEx(
        dwExStyle,
        lpClassName,
        PARAM(lpWindowName, NULL),
        dwStyle,
        PARAM(x, 0),
        PARAM(y, 0),
        nWidth, nHeight,
        PARAM(hWndParent, parent_window),
        (HMENU)NULL,
        instance,
        PARAM(lpParam, NULL));

    cassert_no_null(control->hwnd);
    control->tooltip_hwnd = NULL;

    if (wndProc != NULL)
        control->def_wnd_proc = (WNDPROC)SetWindowLongPtr(control->hwnd, GWLP_WNDPROC, (LONG_PTR)wndProc);
    else
        control->def_wnd_proc = NULL;

    {
        void *data = cast(SetWindowLongPtr(control->hwnd, GWLP_USERDATA, (LONG_PTR)control), void);
        cassert_unref(data == NULL, data);
    }
}

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

void _oscontrol_init(OSControl *control, const DWORD dwExStyle, const DWORD dwStyle, const LPCWSTR lpClassName, int nWidth, int nHeight, WNDPROC wndProc, HWND parent_window)
{
    i_init(control, dwExStyle, dwStyle | WS_VISIBLE, lpClassName, nWidth, nHeight, wndProc, parent_window);
}

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

void _oscontrol_init_hidden(OSControl *control, const DWORD dwExStyle, const DWORD dwStyle, const LPCWSTR lpClassName, int nWidth, int nHeight, WNDPROC wndProc, HWND parent_window)
{
    i_init(control, dwExStyle, dwStyle & ~WS_VISIBLE, lpClassName, nWidth, nHeight, wndProc, parent_window);
}

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

void _oscontrol_destroy(OSControl *control)
{
    BOOL ret = 0;
    cassert_no_null(control);
    _ostooltip_destroy_optional(&control->tooltip_hwnd, control->hwnd);
    ret = DestroyWindow(control->hwnd);
    cassert_unref(ret != 0, ret);
}

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

char_t *_oscontrol_get_text(const OSControl *control, uint32_t *tsize, uint32_t *nchars)
{
    uint32_t num_chars = 0;
    WCHAR *wtext = NULL;
    WCHAR wtext_static[WCHAR_BUFFER_SIZE];
    WCHAR *wtext_alloc = NULL;
    char_t *control_text = NULL;
    cassert_no_null(control);
    cassert_no_null(tsize);
    /* WM_GETTEXTLENGTH: The return value is the length of the text in characters, not including the terminating null character. */
    num_chars = 1 + (uint32_t)SendMessage(control->hwnd, WM_GETTEXTLENGTH, (WPARAM)0, (LPARAM)0);
    if (num_chars < WCHAR_BUFFER_SIZE)
    {
        wtext = wtext_static;
    }
    else
    {
        wtext_alloc = cast(heap_malloc(num_chars * sizeof(WCHAR), "OSControlGetTextBuf"), WCHAR);
        wtext = wtext_alloc;
    }

    {
        uint32_t num_chars_copied = 0;
        /* WM_GETTEXT: The return value is the number of characters copied, not including the terminating null character.*/
        num_chars_copied = (uint32_t)SendMessage(control->hwnd, WM_GETTEXT, (WPARAM)num_chars, (LPARAM)wtext);
        cassert_unref(num_chars == num_chars_copied + 1, num_chars_copied);
    }

    *tsize = unicode_convers_nbytes(cast_const(wtext, char_t), kWINDOWS_UNICODE, ekUTF8);
    control_text = cast(heap_malloc(*tsize, "OSControlGetText"), char_t);

    {
        uint32_t bytes = unicode_convers(cast_const(wtext, char_t), control_text, kWINDOWS_UNICODE, ekUTF8, *tsize);
        cassert_unref(bytes == *tsize, bytes);
    }

    if (wtext_alloc != NULL)
        heap_free(dcast(&wtext_alloc, byte_t), num_chars * sizeof(WCHAR), "OSControlGetTextBuf");

    ptr_assign(nchars, num_chars - 1);
    return control_text;
}

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

void _oscontrol_set_text(OSControl *control, const char_t *text)
{
    uint32_t num_chars = 0;
    WCHAR *wtext = NULL;
    WCHAR wtext_static[WCHAR_BUFFER_SIZE];
    WCHAR *wtext_alloc = NULL;
    cassert_no_null(control);
    num_chars = 1 + unicode_nchars(text, ekUTF8);
    if (num_chars < WCHAR_BUFFER_SIZE)
    {
        wtext = wtext_static;
    }
    else
    {
        wtext_alloc = cast(heap_malloc(num_chars * sizeof(WCHAR), "OSControlSetText"), WCHAR);
        wtext = wtext_alloc;
    }

    {
        uint32_t bytes = unicode_convers(text, cast(wtext, char_t), ekUTF8, kWINDOWS_UNICODE, num_chars * sizeof(WCHAR));
        cassert_unref(bytes == num_chars * sizeof(WCHAR), bytes);
    }

    {
        BOOL ok = SetWindowText(control->hwnd, wtext);
        cassert_unref(ok != 0, ok);
    }

    if (wtext_alloc != NULL)
        heap_free(dcast(&wtext_alloc, byte_t), num_chars * sizeof(WCHAR), "OSControlSetText");
}

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

void _oscontrol_set_tooltip(OSControl *control, const char_t *text)
{
    cassert_no_null(control);
    _ostooltip_set_text(&control->tooltip_hwnd, control->hwnd, text);
}

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

void _oscontrol_set_font(OSControl *control, const Font *font)
{
    HFONT hfont = (HFONT)font_native(font);
    cassert_no_null(control);
    SendMessage(control->hwnd, WM_SETFONT, (WPARAM)hfont, (LPARAM)FALSE);
}

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

void _oscontrol_update_font(OSControl *control, Font **current_font, const Font *font)
{
    HFONT hfont = (HFONT)font_native(font);
    cassert_no_null(control);
    cassert_no_null(current_font);
    if (font_equals(*current_font, font) == FALSE)
    {
        font_destroy(current_font);
        *current_font = font_copy(font);
    }
    SendMessage(control->hwnd, WM_SETFONT, (WPARAM)hfont, (LPARAM)FALSE);
}

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

void _oscontrol_set_visible(OSControl *control, const bool_t visible)
{
    cassert_no_null(control);
    ShowWindow(control->hwnd, (visible == TRUE) ? SW_SHOW : SW_HIDE);
}

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

void _oscontrol_set_enabled(OSControl *control, const bool_t enabled)
{
    cassert_no_null(control);
    EnableWindow(control->hwnd, (BOOL)enabled);
}

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

void _oscontrol_get_origin(const OSControl *control, real32_t *x, real32_t *y)
{
    cassert_no_null(control);
    cassert_no_null(x);
    cassert_no_null(y);
    *x = (real32_t)control->x;
    *y = (real32_t)control->y;
}

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

void _oscontrol_get_size(const OSControl *control, real32_t *width, real32_t *height)
{
    BOOL ret;
    RECT rect;
    cassert_no_null(control);
    cassert_no_null(width);
    cassert_no_null(height);
    ret = GetWindowRect(control->hwnd, &rect);
    cassert_unref(ret != 0, ret);
    *width = (real32_t)(rect.right - rect.left);
    *height = (real32_t)(rect.bottom - rect.top);
}

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

void _oscontrol_set_frame(OSControl *control, const real32_t x, const real32_t y, const real32_t width, const real32_t height)
{
    BOOL ret = FALSE;
    OSControl *parent = NULL;
    int scroll_x = 0, scroll_y = 0;
    cassert_no_null(control);

    parent = cast(GetWindowLongPtr(GetParent(control->hwnd), GWLP_USERDATA), OSControl);
    if (parent != NULL && parent->type == ekGUI_TYPE_PANEL)
        _ospanel_scroll_pos(cast(parent, OSPanel), &scroll_x, &scroll_y);

    ret = SetWindowPos(control->hwnd, NULL, (int)x - scroll_x, (int)y - scroll_y, (int)width, (int)height, SWP_NOZORDER);
    cassert_unref(ret != 0, ret);
    control->x = (int32_t)x;
    control->y = (int32_t)y;
}

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

void _oscontrol_attach_to_parent(OSControl *control, OSControl *parent_control)
{
    HWND ret;
    cassert_no_null(control);
    cassert_no_null(parent_control);
    ret = SetParent(control->hwnd, parent_control->hwnd);
    cassert_no_null(ret);
#if defined(__ASSERTS__)
    {
        HWND parent;
        parent = GetParent(control->hwnd);
        cassert_no_null(parent);
        cassert(parent == parent_control->hwnd);
    }
#endif
}

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

void _oscontrol_detach_from_parent(OSControl *control, OSControl *parent_control)
{
    HWND ret;
    cassert_no_null(control);
    cassert_no_null(parent_control);
    cassert(GetParent(control->hwnd) == parent_control->hwnd);
    ret = SetParent(control->hwnd, NULL);
    cassert_unref(ret == parent_control->hwnd, ret);
}

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

static BOOL CALLBACK i_children_count(HWND child_hwnd, LPARAM lParam)
{
    i_Children *children_str = cast(lParam, i_Children);
    if (children_str->num_children < children_str->children_size)
        children_str->children[children_str->num_children] = child_hwnd;
    children_str->num_children += 1;
    return TRUE;
}

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

uint32_t _oscontrol_num_children(const OSControl *control)
{
    i_Children children_str;
    children_str.num_children = 0;
    children_str.children = NULL;
    children_str.children_size = 0;
    cassert_no_null(control);
    EnumChildWindows(control->hwnd, i_children_count, (LPARAM)&children_str);
    return children_str.num_children;
}

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

void _oscontrol_draw_focus(HWND hwnd, const INT left_offset, const INT right_offset, const INT top_offset, const INT bottom_offset)
{
    RECT rc;
    PAINTSTRUCT st;
    HDC hdc;
    GetClientRect(hwnd, &rc);
    rc.left += left_offset;
    rc.right -= right_offset;
    rc.top += top_offset;
    rc.bottom -= bottom_offset;
    InvalidateRect(hwnd, NULL, FALSE);
    hdc = BeginPaint(hwnd, &st);
    FrameRect(hdc, &rc, kCHESSBOARD_BRUSH);
    EndPaint(hwnd, &st);
}

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

DWORD _oscontrol_halign(const align_t halign)
{
    switch (halign)
    {
    case ekLEFT:
        return SS_LEFT;
    case ekCENTER:
    case ekJUSTIFY:
        return SS_CENTER;
    case ekRIGHT:
        return SS_RIGHT;
        cassert_default();
    }

    return UINT32_MAX;
}

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

DWORD _oscontrol_ellipsis(const ellipsis_t ellipsis)
{
    switch (ellipsis)
    {
    case ekELLIPNONE:
    case ekELLIPMLINE:
        return 0;
    case ekELLIPBEGIN:
        return SS_ENDELLIPSIS;
    case ekELLIPEND:
        return SS_ENDELLIPSIS;
    case ekELLIPMIDDLE:
        return SS_PATHELLIPSIS;
    }

    return 0;
}

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

COLORREF _oscontrol_colorref(const color_t color)
{
    uint8_t r, g, b;
    color_get_rgb(color, &r, &g, &b);
    return RGB(r, g, b);
}

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

color_t _oscontrol_from_colorref(const COLORREF color)
{
    return color_rgb(GetRValue(color), GetGValue(color), GetBValue(color));
}

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

void _oscontrol_update_brush(const color_t color, HBRUSH *brush, COLORREF *colorref)
{
    COLORREF cf;
    cassert_no_null(brush);
    if (*brush != NULL)
    {
        BOOL ok = DeleteObject(*brush);
        cassert_unref(ok != 0, ok);
        *brush = NULL;
    }

    cf = _oscontrol_colorref(color);
    ptr_assign(colorref, cf);
    if (color != kCOLOR_TRANSPARENT)
        *brush = CreateSolidBrush(cf);
}

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

void _oscontrol_destroy_brush(HBRUSH *brush)
{
    cassert_no_null(brush);
    if (*brush != NULL)
    {
        BOOL ok = DeleteObject(*brush);
        cassert_unref(ok != 0, ok);
        *brush = NULL;
    }
}

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

gui_type_t _oscontrol_type(const OSControl *control)
{
    cassert_no_null(control);
    return control->type;
}

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

OSControl *_oscontrol_parent(const OSControl *control)
{
    HWND parentHWND = NULL;
    OSControl *parent = NULL;
    cassert_no_null(control);
    parentHWND = GetParent(control->hwnd);
    parent = cast(GetWindowLongPtr(parentHWND, GWLP_USERDATA), OSControl);
    return parent;
}

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

void _oscontrol_frame(const OSControl *control, OSFrame *rect)
{
    real32_t width, height;
    cassert_no_null(control);
    cassert_no_null(rect);
    _oscontrol_get_size(control, &width, &height);
    rect->left = control->x;
    rect->top = control->y;
    rect->right = rect->left + (int32_t)width;
    rect->bottom = rect->top + (int32_t)height;
}

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

void _oscontrol_set_can_focus(OSControl *control, const bool_t can_focus)
{
    cassert_no_null(control);
    if (control->type == ekGUI_TYPE_BUTTON)
        _osbutton_set_can_focus(cast(control, OSButton), can_focus);
}

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

OSWidget *_oscontrol_focus_widget(const OSControl *control)
{
    cassert_no_null(control);
    switch (control->type)
    {
    case ekGUI_TYPE_LABEL:
    case ekGUI_TYPE_PROGRESS:
        return NULL;

    case ekGUI_TYPE_BUTTON:
    case ekGUI_TYPE_EDITBOX:
    case ekGUI_TYPE_SLIDER:
    case ekGUI_TYPE_TEXTVIEW:
    case ekGUI_TYPE_UPDOWN:
    case ekGUI_TYPE_CUSTOMVIEW:
        return cast(control->hwnd, OSWidget);

    case ekGUI_TYPE_POPUP:
        return cast(_ospopup_focus_widget(cast(control, OSPopUp)), OSWidget);

    case ekGUI_TYPE_COMBOBOX:
        return cast(_oscombo_focus_widget(cast(control, OSCombo)), OSWidget);

    case ekGUI_TYPE_WEBVIEW:
        return cast(_osweb_focus_widget(cast(control, OSWeb)), OSWidget);

    case ekGUI_TYPE_TREEVIEW:
    case ekGUI_TYPE_BOXVIEW:
    case ekGUI_TYPE_SPLITVIEW:
    case ekGUI_TYPE_PANEL:
    case ekGUI_TYPE_LINE:
    case ekGUI_TYPE_HEADER:
    case ekGUI_TYPE_WINDOW:
    case ekGUI_TYPE_TOOLBAR:
        cassert_default();
    }

    return NULL;
}

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

bool_t _oscontrol_widget_visible(const OSWidget *widget)
{
    cassert_no_null(widget);
    return (bool_t)IsWindowVisible((HWND)widget);
}

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

bool_t _oscontrol_widget_enable(const OSWidget *widget)
{
    cassert_no_null(widget);
    return (bool_t)IsWindowEnabled((HWND)widget);
}