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

/* Operating System edit box */

#include "osedit_win.inl"
#include "osgui_win.inl"
#include "oscontrol_win.inl"
#include "ospanel_win.inl"
#include "oswindow_win.inl"
#include "../osedit.h"
#include "../osedit.inl"
#include "../osgui.inl"
#include <draw2d/font.h>
#include <core/event.h>
#include <core/heap.h>
#include <core/strings.h>
#include <sewer/cassert.h>
#include <sewer/ptr.h>
#include <sewer/unicode.h>

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

struct _osedit_t
{
    OSControl control;
    uint32_t flags;
    Font *font;
    bool_t launch_event;
    bool_t focused;
    COLORREF color;
    COLORREF bgcolor;
    HBRUSH bgbrush;
    RECT border;
    uint32_t vpadding;
    real32_t rpadding;
    INT wpadding;
    Listener *OnFilter;
    Listener *OnChange;
    Listener *OnFocus;
    UINT_PTR timer;
};

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

static LRESULT CALLBACK i_WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    OSEdit *edit = cast(GetWindowLongPtr(hwnd, GWLP_USERDATA), OSEdit);
    LRESULT res;
    cassert_no_null(edit);

    switch (uMsg)
    {
    case WM_ERASEBKGND:
        return 1;

    case WM_NCCALCSIZE:
        return _osgui_nccalcsize(hwnd, wParam, lParam, TRUE, edit->wpadding, &edit->border);

    case WM_NCPAINT:
        return _osgui_ncpaint(hwnd, edit->focused, &edit->border, edit->bgbrush);

    case WM_LBUTTONDOWN:
    case WM_LBUTTONDBLCLK:
        if (_oswindow_mouse_down(cast(edit, OSControl)) == TRUE)
            break;
        return 0;
    }

    res = CallWindowProc(edit->control.def_wnd_proc, hwnd, uMsg, wParam, lParam);

    if (uMsg == WM_LBUTTONDOWN)
    {
        if (BIT_TEST(edit->flags, ekEDIT_AUTOSEL) == TRUE)
            SendMessage(hwnd, EM_SETSEL, 0, -1);
    }

    return res;
}

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

static DWORD i_flags(const edit_flag_t flags)
{
    if (edit_get_type(flags) == ekEDIT_MULTI)
        return ES_MULTILINE | ES_AUTOVSCROLL;
    else
        return ES_AUTOHSCROLL;
}

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

static void i_update_vpadding(OSEdit *edit)
{
    real32_t width, height;
    uint32_t defpadding = 0;

    cassert_no_null(edit);
    font_extents(edit->font, "O", -1.f, &width, &height);

    defpadding = (uint32_t)((.3f * height) + .5f);
    if (defpadding % 2 == 1)
        defpadding += 1;

    if (defpadding < 5)
        defpadding = 5;

    if (edit->vpadding == UINT32_MAX)
    {
        edit->rpadding = (real32_t)defpadding;
        edit->wpadding = 0;
    }
    else
    {
        real32_t leading = font_leading(edit->font);
        uint32_t padding = (uint32_t)(edit->vpadding + leading);

        if (padding % 2 == 1)
            padding += 1;

        if (padding < 4)
            padding = 4;

        if (padding > defpadding)
            edit->wpadding = (padding - defpadding) / 2;
        else
            edit->wpadding = 0;

        edit->rpadding = (real32_t)padding;
    }
}

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

OSEdit *osedit_create(const uint32_t flags)
{
    OSEdit *edit = heap_new0(OSEdit);
    DWORD dwStyle = WS_CHILD | WS_CLIPSIBLINGS | _oscontrol_halign(ekLEFT) | i_flags(flags);
    edit->control.type = ekGUI_TYPE_EDITBOX;
    edit->flags = flags;
    _oscontrol_init(cast(edit, OSControl), PARAM(dwExStyle, WS_EX_NOPARENTNOTIFY /*| WS_EX_CLIENTEDGE*/), dwStyle, L"edit", 0, 0, i_WndProc, kDEFAULT_PARENT_WINDOW);
    edit->font = _osgui_create_default_font();
    edit->launch_event = TRUE;
    edit->focused = FALSE;
    edit->vpadding = UINT32_MAX;
    edit->timer = 0;
    i_update_vpadding(edit);
    _oscontrol_set_font(cast(edit, OSControl), edit->font);
    return edit;
}

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

void osedit_destroy(OSEdit **edit)
{
    cassert_no_null(edit);
    cassert_no_null(*edit);
    font_destroy(&(*edit)->font);
    listener_destroy(&(*edit)->OnFilter);
    listener_destroy(&(*edit)->OnChange);
    listener_destroy(&(*edit)->OnFocus);
    _oscontrol_destroy_brush(&(*edit)->bgbrush);
    _oscontrol_destroy(&(*edit)->control);
    heap_delete(edit, OSEdit);
}

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

void osedit_OnFilter(OSEdit *edit, Listener *listener)
{
    cassert_no_null(edit);
    listener_update(&edit->OnFilter, listener);
}

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

void osedit_OnChange(OSEdit *edit, Listener *listener)
{
    cassert_no_null(edit);
    listener_update(&edit->OnChange, listener);
}

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

void osedit_OnFocus(OSEdit *edit, Listener *listener)
{
    cassert_no_null(edit);
    listener_update(&edit->OnFocus, listener);
}

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

void osedit_text(OSEdit *edit, const char_t *text)
{
    cassert_no_null(edit);
    edit->launch_event = FALSE;
    _oscontrol_set_text(cast(edit, OSControl), text);
    edit->launch_event = TRUE;
}

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

void osedit_font(OSEdit *edit, const Font *font)
{
    cassert_no_null(edit);
    _oscontrol_update_font(cast(edit, OSControl), &edit->font, font);
    i_update_vpadding(edit);
}

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

void osedit_tooltip(OSEdit *edit, const char_t *text)
{
    _oscontrol_set_tooltip(cast(edit, OSControl), text);
}

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

void osedit_align(OSEdit *edit, const align_t align)
{
    DWORD dwStyle = 0;
    cassert_no_null(edit);
    dwStyle = (DWORD)GetWindowLongPtr(edit->control.hwnd, GWL_STYLE);
    dwStyle &= ~SS_LEFT;
    dwStyle &= ~SS_CENTER;
    dwStyle &= ~SS_RIGHT;
    dwStyle |= _oscontrol_halign(align);
    SetWindowLongPtr(edit->control.hwnd, GWL_STYLE, dwStyle);
}

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

void osedit_passmode(OSEdit *edit, const bool_t passmode)
{
    cassert_no_null(edit);
    if (passmode == TRUE)
    {
        wchar_t pchar = L'\x2022';
        SendMessage(edit->control.hwnd, EM_SETPASSWORDCHAR, (WPARAM)pchar, (LPARAM)0);
    }
    else
    {
        SendMessage(edit->control.hwnd, EM_SETPASSWORDCHAR, (WPARAM)0, (LPARAM)0);
    }
}

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

void osedit_editable(OSEdit *edit, const bool_t is_editable)
{
    LRESULT res = 0;
    cassert_no_null(edit);
    res = SendMessage(edit->control.hwnd, EM_SETREADONLY, (WPARAM)!is_editable, (LPARAM)0);
    cassert_unref(res != 0, res);
}

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

void osedit_autoselect(OSEdit *edit, const bool_t autoselect)
{
    cassert_no_null(edit);
    if (autoselect == TRUE)
        BIT_SET(edit->flags, ekEDIT_AUTOSEL);
    else
        BIT_CLEAR(edit->flags, ekEDIT_AUTOSEL);
}

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

void osedit_select(OSEdit *edit, const int32_t start, const int32_t end)
{
    int32_t platform_st, platform_ed;
    cassert_no_null(edit);
    _osgui_select_text(start, end, &platform_st, &platform_ed);
    SendMessage(edit->control.hwnd, EM_SETSEL, (WPARAM)platform_st, (LPARAM)platform_ed);
}

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

void osedit_color(OSEdit *edit, const color_t color)
{
    cassert_no_null(edit);
    edit->color = _oscontrol_colorref(color);
}

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

void osedit_bgcolor(OSEdit *edit, const color_t color)
{
    cassert_no_null(edit);
    _oscontrol_update_brush(color, &edit->bgbrush, &edit->bgcolor);
}

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

void osedit_vpadding(OSEdit *edit, const real32_t padding)
{
    cassert_no_null(edit);
    cassert(padding >= 0);
    edit->vpadding = (padding < 0) ? UINT32_MAX : (uint32_t)padding;
    i_update_vpadding(edit);
}

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

void osedit_bounds(const OSEdit *edit, const real32_t refwidth, const uint32_t lines, real32_t *width, real32_t *height)
{
    cassert_no_null(edit);
    cassert_no_null(width);
    cassert_no_null(height);
    cassert_unref(lines == 1, lines);

    if (edit_get_type(edit->flags) == ekEDIT_SINGLE)
        font_extents(edit->font, "O", -1.f, width, height);
    else
        font_extents(edit->font, "O\nO", -1.f, width, height);

    *width = refwidth;
    *height += edit->rpadding;
}

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

void osedit_clipboard(OSEdit *edit, const clipboard_t clipboard)
{
    cassert_no_null(edit);
    switch (clipboard)
    {
    case ekCLIPBOARD_COPY:
        SendMessage(edit->control.hwnd, WM_COPY, (WPARAM)0, (LPARAM)0);
        break;
    case ekCLIPBOARD_PASTE:
        SendMessage(edit->control.hwnd, WM_PASTE, (WPARAM)0, (LPARAM)0);
        break;
    case ekCLIPBOARD_CUT:
        SendMessage(edit->control.hwnd, WM_CUT, (WPARAM)0, (LPARAM)0);
        break;
        cassert_default();
    }
}

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

void osedit_attach(OSEdit *edit, OSPanel *panel)
{
    _ospanel_attach_control(panel, cast(edit, OSControl));
}

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

void osedit_detach(OSEdit *edit, OSPanel *panel)
{
    _ospanel_detach_control(panel, cast(edit, OSControl));
}

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

void osedit_visible(OSEdit *edit, const bool_t visible)
{
    _oscontrol_set_visible(cast(edit, OSControl), visible);
}

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

void osedit_enabled(OSEdit *edit, const bool_t enabled)
{
    _oscontrol_set_enabled(cast(edit, OSControl), enabled);
}

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

void osedit_size(const OSEdit *edit, real32_t *width, real32_t *height)
{
    _oscontrol_get_size(cast_const(edit, OSControl), width, height);
}

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

void osedit_origin(const OSEdit *edit, real32_t *x, real32_t *y)
{
    _oscontrol_get_origin(cast_const(edit, OSControl), x, y);
}

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

void osedit_frame(OSEdit *edit, const real32_t x, const real32_t y, const real32_t width, const real32_t height)
{
    _oscontrol_set_frame(cast(edit, OSControl), x, y, width, height);
}

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

static uint32_t i_get_cursor_pos(HWND hwnd)
{
    DWORD start;
    SendMessage(hwnd, EM_GETSEL, (WPARAM)&start, (LPARAM)NULL);
    return (uint32_t)start;
}

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

static void i_set_cursor_pos(HWND hwnd, const uint32_t pos)
{
    SendMessage(hwnd, EM_SETSEL, (WPARAM)pos, (LPARAM)pos);
}

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

void _osedit_command(OSEdit *edit, WPARAM wParam)
{
    cassert_no_null(edit);
    if (HIWORD(wParam) == EN_UPDATE)
    {
        if (edit->launch_event == TRUE && IsWindowEnabled(edit->control.hwnd) && edit->OnFilter != NULL)
        {
            char_t *edit_text;
            uint32_t tsize;
            EvText params;
            EvTextFilter result;
            edit_text = _oscontrol_get_text(cast_const(edit, OSControl), &tsize, NULL);
            params.text = cast_const(edit_text, char_t);
            params.cpos = i_get_cursor_pos(edit->control.hwnd);
            params.len = INT32_MAX;
            result.apply = FALSE;
            result.text[0] = '\0';
            result.cpos = UINT32_MAX;
            listener_event(edit->OnFilter, ekGUI_EVENT_TXTFILTER, edit, &params, &result, OSEdit, EvText, EvTextFilter);
            heap_free(dcast(&edit_text, byte_t), tsize, "OSControlGetText");

            if (result.apply == TRUE)
            {
                bool_t prev = edit->launch_event;
                edit->launch_event = FALSE;
                _oscontrol_set_text(&edit->control, result.text);
                edit->launch_event = prev;
            }

            if (result.cpos != UINT32_MAX)
                i_set_cursor_pos(edit->control.hwnd, result.cpos);
            else
                i_set_cursor_pos(edit->control.hwnd, params.cpos);
        }
    }
}

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

COLORREF _osedit_color(const OSEdit *edit)
{
    cassert_no_null(edit);
    return edit->color;
}

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

HBRUSH _osedit_background_color(const OSEdit *edit, COLORREF *color)
{
    cassert_no_null(edit);
    if (edit->bgbrush != NULL)
    {
        *color = edit->bgcolor & 0x00FFFFFF;
        return edit->bgbrush;
    }

    return NULL;
}

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

bool_t _osedit_resign_focus(const OSEdit *edit)
{
    bool_t lost_focus = TRUE;
    cassert_no_null(edit);
    if (edit->OnChange != NULL)
    {
        char_t *edit_text = NULL;
        uint32_t tsize = 0;
        uint32_t nchars = 0;
        EvText params;
        edit_text = _oscontrol_get_text(cast_const(edit, OSControl), &tsize, &nchars);
        params.text = cast_const(edit_text, char_t);
        params.cpos = i_get_cursor_pos(edit->control.hwnd);
        params.len = (int32_t)nchars;
        listener_event(edit->OnChange, ekGUI_EVENT_TXTCHANGE, edit, &params, &lost_focus, OSEdit, EvText, bool_t);
        heap_free(dcast(&edit_text, byte_t), tsize, "OSControlGetText");
    }

    return lost_focus;
}

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

void _osedit_focus(OSEdit *edit, const bool_t focus)
{
    cassert_no_null(edit);
    edit->focused = focus;
    if (edit->OnFocus != NULL)
    {
        bool_t params = focus;
        listener_event(edit->OnFocus, ekGUI_EVENT_FOCUS, edit, &params, NULL, OSEdit, bool_t, void);
    }

    if (focus == TRUE)
    {
        if (BIT_TEST(edit->flags, ekEDIT_AUTOSEL) == TRUE)
            SendMessage(edit->control.hwnd, EM_SETSEL, 0, -1);
    }
}