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

/* Scrollbars management in control */

#include "osscrolls.inl"
#include "osscroll.inl"
#include "oscontrol.inl"
#include <core/event.h>
#include <core/heap.h>
#include <sewer/cassert.h>
#include <sewer/ptr.h>

struct _osscrolls_t
{
    OSControl *control;
    Listener *OnScroll;
    OSScroll *hscroll;
    OSScroll *vscroll;
    bool_t hvisible;
    bool_t vvisible;
    uint32_t view_width;
    uint32_t view_height;
    uint32_t content_width;
    uint32_t content_height;
    uint32_t line_width;
    uint32_t line_height;
};

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

OSScrolls *_osscrolls_create(OSControl *control, const bool_t horizontal, const bool_t vertical)
{
    OSScrolls *scroll = heap_new0(OSScrolls);
    cassert_no_null(control);
    cassert(horizontal == TRUE || vertical == TRUE);
    scroll->control = control;

    if (horizontal == TRUE)
        scroll->hscroll = _osscroll_horizontal(control);

    if (vertical == TRUE)
        scroll->vscroll = _osscroll_vertical(control);

    scroll->hvisible = TRUE;
    scroll->vvisible = TRUE;
    return scroll;
}

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

void _osscrolls_destroy(OSScrolls **scroll)
{
    cassert_no_null(scroll);
    cassert_no_null(*scroll);

    if ((*scroll)->hscroll != NULL)
        _osscroll_destroy(&(*scroll)->hscroll, (*scroll)->control);

    if ((*scroll)->vscroll != NULL)
        _osscroll_destroy(&(*scroll)->vscroll, (*scroll)->control);

    listener_destroy(&(*scroll)->OnScroll);
    heap_delete(scroll, OSScrolls);
}

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

void _osscrolls_OnScroll(OSScrolls *scroll, Listener *listener)
{
    cassert_no_null(scroll);
    listener_update(&scroll->OnScroll, listener);
}

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

void _osscrolls_visible_area(OSScrolls *scroll, uint32_t *x, uint32_t *y, uint32_t *width, uint32_t *height, uint32_t *total_width, uint32_t *total_height)
{
    cassert_no_null(scroll);
    if (scroll->hscroll != NULL)
    {
        uint32_t pos = _osscroll_pos(scroll->hscroll);
        ptr_assign(x, pos);
        ptr_assign(total_width, scroll->content_width);
    }
    else
    {
        ptr_assign(x, 0);
        ptr_assign(total_width, scroll->view_width);
    }

    if (scroll->vscroll != NULL)
    {
        uint32_t pos = _osscroll_pos(scroll->vscroll);
        ptr_assign(y, pos);
        ptr_assign(total_height, scroll->content_height);
    }
    else
    {
        ptr_assign(y, 0);
        ptr_assign(total_height, scroll->view_height);
    }

    ptr_assign(width, scroll->view_width);
    ptr_assign(height, scroll->view_height);
}

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

uint32_t _osscrolls_x_pos(const OSScrolls *scroll)
{
    cassert_no_null(scroll);
    if (scroll->hscroll != NULL)
        return _osscroll_pos(scroll->hscroll);
    return 0;
}

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

uint32_t _osscrolls_y_pos(const OSScrolls *scroll)
{
    cassert_no_null(scroll);
    if (scroll->vscroll != NULL)
        return _osscroll_pos(scroll->vscroll);
    return 0;
}

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

uint32_t _osscrolls_bar_width(const OSScrolls *scroll, const bool_t check_if_visible)
{
    if (check_if_visible == TRUE)
    {
        cassert_no_null(scroll);
        if (scroll->vscroll != NULL)
        {
            if (scroll->vvisible == TRUE && scroll->content_height > scroll->view_height)
                return _osscroll_bar_width(scroll->vscroll);
        }

        return 0;
    }
    else
    {
        return _osscroll_bar_width(scroll->vscroll);
    }
}

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

uint32_t _osscrolls_bar_height(const OSScrolls *scroll, const bool_t check_if_visible)
{
    if (check_if_visible == TRUE)
    {
        cassert_no_null(scroll);
        if (scroll->hscroll != NULL)
        {
            if (scroll->hvisible == TRUE && scroll->content_width > scroll->view_width)
                return _osscroll_bar_height(scroll->hscroll);
        }

        return 0;
    }
    else
    {
        return _osscroll_bar_height(scroll->hscroll);
    }
}

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

bool_t _osscrolls_event(OSScrolls *scroll, const gui_orient_t orient, const gui_scroll_t event, const bool_t update_children)
{
    OSScroll *sbar = NULL;
    uint32_t step = 0;
    uint32_t page = 0;
    uint32_t max = 0;

    cassert_no_null(scroll);
    cassert_no_null(scroll->control);

    switch (orient)
    {
    case ekGUI_HORIZONTAL:
        sbar = scroll->hscroll;
        step = scroll->line_width;
        page = scroll->view_width;
        max = scroll->content_width - scroll->view_width;
        break;
    case ekGUI_VERTICAL:
        sbar = scroll->vscroll;
        step = scroll->line_height;
        page = scroll->view_height;
        max = scroll->content_height - scroll->view_height;
        break;
        cassert_default();
    }

    if (sbar != NULL)
    {
        uint32_t curpos = _osscroll_pos(sbar);
        uint32_t pos = curpos;

        switch (event)
        {
        case ekGUI_SCROLL_BEGIN:
            pos = 0;
            break;

        case ekGUI_SCROLL_END:
            pos = max;
            break;

        case ekGUI_SCROLL_STEP_LEFT:
            if (pos > step)
                pos -= step;
            else
                pos = 0;
            break;

        case ekGUI_SCROLL_STEP_RIGHT:
            pos += step;
            if (pos > max)
                pos = max;
            break;

        case ekGUI_SCROLL_PAGE_LEFT:
            if (pos > page)
                pos -= page;
            else
                pos = 0;
            break;

        case ekGUI_SCROLL_PAGE_RIGHT:
            pos += page;
            if (pos > max)
                pos = max;
            break;

        case ekGUI_SCROLL_THUMB:
            pos = _osscroll_trackpos(sbar);
            break;

            cassert_default();
        }

        if (scroll->OnScroll != NULL)
        {
            EvScroll p;
            real32_t r = (real32_t)pos;
            gui_type_t type = _oscontrol_type(scroll->control);
            p.orient = orient;
            p.scroll = event;
            p.cpos = (real32_t)curpos;

            if (type == ekGUI_TYPE_PANEL)
            {
                listener_event(scroll->OnScroll, ekGUI_EVENT_SCROLL, cast(scroll->control, OSPanel), &p, &r, OSPanel, EvScroll, real32_t);
            }
            else
            {
                cassert(type == ekGUI_TYPE_CUSTOMVIEW);
                listener_event(scroll->OnScroll, ekGUI_EVENT_SCROLL, cast(scroll->control, OSView), &p, &r, OSView, EvScroll, real32_t);
            }

            pos = (uint32_t)r;
        }

        if (curpos != pos)
        {
            _osscroll_set_pos(sbar, pos);

            if (update_children == TRUE)
            {
                int32_t incr_x = 0;
                int32_t incr_y = 0;

                switch (orient)
                {
                case ekGUI_HORIZONTAL:
                    incr_x = (int32_t)curpos - (int32_t)pos;
                    break;
                case ekGUI_VERTICAL:
                    incr_y = (int32_t)curpos - (int32_t)pos;
                    break;
                    cassert_default();
                }

                _osscroll_control_scroll(scroll->control, incr_x, incr_y);
            }

            return TRUE;
        }
    }

    return FALSE;
}

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

void _osscrolls_set(OSScrolls *scroll, const uint32_t x, const uint32_t y, const bool_t update_children)
{
    uint32_t px = x;
    uint32_t py = y;
    int32_t incr_x = 0;
    int32_t incr_y = 0;

    cassert_no_null(scroll);

    if (px != UINT32_MAX)
    {
        uint32_t max = scroll->content_width - scroll->view_width;
        max += _osscrolls_bar_width(scroll, TRUE);
        if (px > max)
            px = max;
    }

    if (py != UINT32_MAX)
    {
        if (py > scroll->content_height - scroll->view_height)
            py = scroll->content_height - scroll->view_height;
    }

    if (px != UINT32_MAX && scroll->hscroll != NULL)
    {
        uint32_t pos = _osscroll_pos(scroll->hscroll);
        if (pos != px)
        {
            _osscroll_set_pos(scroll->hscroll, px);
            incr_x = (int32_t)pos - (int32_t)px;
        }
    }

    if (py != UINT32_MAX && scroll->vscroll != NULL)
    {
        uint32_t pos = _osscroll_pos(scroll->vscroll);
        if (pos != py)
        {
            _osscroll_set_pos(scroll->vscroll, py);
            incr_y = (int32_t)pos - (int32_t)py;
        }
    }

    if (update_children == TRUE)
    {
        if (incr_x != 0 || incr_y != 0)
            _osscroll_control_scroll(scroll->control, incr_x, incr_y);
    }
}

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

static bool_t i_limits(OSScroll *scroll, const bool_t visible, const uint32_t visible_size, const uint32_t total_size)
{
    if (visible == TRUE && visible_size > 0 && visible_size < total_size)
    {
        uint32_t max = total_size;
        uint32_t pos = _osscroll_pos(scroll);
        uint32_t page = visible_size;
        uint32_t max_pos = total_size - visible_size;

        if (pos > max_pos)
            pos = max_pos;

        _osscroll_config(scroll, pos, max, page);
        return TRUE;
    }
    /* Scrollbar is not necessary */
    else
    {
        return FALSE;
    }
}

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

static void i_update_bars(OSScrolls *scroll)
{
    bool_t with_hscroll = FALSE;
    bool_t with_vscroll = FALSE;
    uint32_t bw = 0;
    uint32_t bh = 0;

    cassert_no_null(scroll);

    if (scroll->hscroll != NULL)
    {
        with_hscroll = i_limits(scroll->hscroll, scroll->hvisible, scroll->view_width, scroll->content_width);
        bh = _osscroll_bar_height(scroll->hscroll);
    }

    if (scroll->vscroll != NULL)
    {
        with_vscroll = i_limits(scroll->vscroll, scroll->vvisible, scroll->view_height, scroll->content_height);
        bw = _osscroll_bar_width(scroll->vscroll);
    }

    if (with_hscroll == TRUE)
    {
        uint32_t x = 0;
        uint32_t y = scroll->view_height - bh;
        uint32_t width = scroll->view_width;
        uint32_t height = bh;

        if (with_vscroll == TRUE)
        {
            width -= bw;
            i_limits(scroll->hscroll, scroll->hvisible, width, scroll->content_width);
        }

        _osscroll_frame(scroll->hscroll, x, y, width, height);
        _osscroll_visible(scroll->hscroll, TRUE);
    }
    else
    {
        if (scroll->hscroll != NULL)
            _osscroll_visible(scroll->hscroll, FALSE);
    }

    if (with_vscroll == TRUE)
    {
        uint32_t x = scroll->view_width - bw;
        uint32_t y = 0;
        uint32_t width = bw;
        uint32_t height = scroll->view_height;
        _osscroll_frame(scroll->vscroll, x, y, width, height);
        _osscroll_visible(scroll->vscroll, TRUE);
    }
    else
    {
        if (scroll->vscroll != NULL)
            _osscroll_visible(scroll->vscroll, FALSE);
    }
}

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

void _osscrolls_content_size(OSScrolls *scroll, const uint32_t width, const uint32_t height, const uint32_t line_width, const uint32_t line_height)
{
    cassert_no_null(scroll);
    scroll->content_width = width;
    scroll->content_height = height;
    scroll->line_width = line_width;
    scroll->line_height = line_height;
    i_update_bars(scroll);
}

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

void _osscrolls_control_size(OSScrolls *scroll, const uint32_t width, const uint32_t height)
{
    cassert_no_null(scroll);
    scroll->view_width = width;
    scroll->view_height = height;
    i_update_bars(scroll);
}

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

void _osscrolls_visible(OSScrolls *scroll, const bool_t horizontal, const bool_t vertical)
{
    cassert_no_null(scroll);
    scroll->hvisible = horizontal;
    scroll->vvisible = vertical;
    i_update_bars(scroll);
}