nappgui 0.2.0

Rust 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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
use std::{
    ffi::CString,
    sync::{Arc, LazyLock, Mutex},
};

use crate::{
    draw_2d::DCtx,
    types::{Align, GuiClose, GuiMouse, GuiOrient, GuiScroll, GuiState, KeyCode},
};

/// Parameters of the OnClick event of a button or OnSelect of a popup.
pub struct EvButton {
    /// Button or item index.
    pub index: u32,
    /// State.
    pub state: GuiState,
    /// Text.
    pub text: String,
}

impl EvButton {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvButton) -> EvButton {
        if ptr.is_null() {
            panic!("EvButton is null");
        }
        let evbutton = unsafe { &*ptr };
        EvButton {
            index: evbutton.index as _,
            state: GuiState::try_from(evbutton.state).unwrap(),
            text: unsafe { std::ffi::CStr::from_ptr(evbutton.text) }
                .to_string_lossy()
                .into_owned(),
        }
    }
}

/// Parameters of the OnMoved event of a slider.
pub struct EvSlider {
    /// Normalized slider position (0, 1).
    pub pos: f32,
    /// Increase with respect to the previous position.
    pub incr: f32,
    /// Interval index (only for discrete ranges).
    pub step: u32,
}

impl EvSlider {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvSlider) -> EvSlider {
        if ptr.is_null() {
            panic!("EvSlider is null");
        }
        let evslider = unsafe { &*ptr };
        EvSlider {
            pos: evslider.pos,
            incr: evslider.incr,
            step: evslider.step as _,
        }
    }
}

/// Parameters of the OnChange event of the text boxes.
pub struct EvText {
    /// Text.
    pub text: String,
    /// Cursor position (caret).
    pub cpos: u32,
    /// Number of characters inserted or deleted.
    pub len: i32,
}

impl EvText {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvText) -> EvText {
        if ptr.is_null() {
            panic!("EvText is null");
        }
        let evtext = unsafe { &*ptr };
        EvText {
            text: unsafe { std::ffi::CStr::from_ptr(evtext.text) }
                .to_string_lossy()
                .into_owned(),
            cpos: evtext.cpos as _,
            len: evtext.len,
        }
    }
}

/// OnDraw event parameters.
pub struct EvDraw {
    /// 2D drawing context.
    pub ctx: DCtx,
    /// X coordinate of the drawing area (viewport).
    pub x: f32,
    /// Y coordinate of the drawing area.
    pub y: f32,
    /// Width of the drawing area.
    pub width: f32,
    /// Height of the drawing area.
    pub height: f32,
}

impl EvDraw {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvDraw) -> EvDraw {
        if ptr.is_null() {
            panic!("EvDraw is null");
        }
        let evdraw = unsafe { &*ptr };
        EvDraw {
            ctx: DCtx::new(evdraw.ctx),
            x: evdraw.x,
            y: evdraw.y,
            width: evdraw.width,
            height: evdraw.height,
        }
    }
}

/// Mouse event parameters.
pub struct EvMouse {
    /// X coordinate of the pointer in the drawing area.
    pub x: f32,
    /// Y coordinate of the pointer in the drawing area.
    pub y: f32,
    /// X coordinate of the pointer on the control. Same as x if there are no scroll bars.
    pub lx: f32,
    /// Y coordinate of the pointer on the control. Same as y if there are no scroll bars.
    pub ly: f32,
    /// Active button.
    pub button: GuiMouse,
    /// Number of clicks.
    pub count: u32,
    /// Combination of values mkey_t. todo!
    pub modifiers: u32,
    /// Additional value for controls.
    pub tag: u32,
}

impl EvMouse {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvMouse) -> EvMouse {
        if ptr.is_null() {
            panic!("EvMouse is null");
        }
        let evmouse = unsafe { &*ptr };
        EvMouse {
            x: evmouse.x,
            y: evmouse.y,
            lx: evmouse.lx,
            ly: evmouse.ly,
            button: GuiMouse::try_from(evmouse.button).unwrap(),
            count: evmouse.count,
            modifiers: evmouse.modifiers,
            tag: evmouse.tag,
        }
    }
}

/// OnWheel event parameters.
pub struct EvWheel {
    /// Pointer x coordinate.
    pub x: f32,
    /// Pointer y coordinate.
    pub y: f32,
    /// Increase in x of the wheel or trackpad.
    pub dx: f32,
    /// Increase in y of the wheel or trackpad.
    pub dy: f32,
    /// Increase in z of the wheel or trackpad.
    pub dz: f32,
}

impl EvWheel {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvWheel) -> EvWheel {
        if ptr.is_null() {
            panic!("EvWheel is null");
        }
        let evwheel = unsafe { &*ptr };
        EvWheel {
            x: evwheel.x,
            y: evwheel.y,
            dx: evwheel.dx,
            dy: evwheel.dy,
            dz: evwheel.dz,
        }
    }
}

/// Keyboard event parameters.
pub struct EvKey {
    /// Referenced key.
    pub key: KeyCode,
    /// Combination of values mkey_t. todo!
    pub modifiers: u32,
}

impl EvKey {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvKey) -> EvKey {
        if ptr.is_null() {
            panic!("EvKey is null");
        }
        let evkey = unsafe { &*ptr };
        EvKey {
            key: KeyCode::try_from(evkey.key).unwrap(),
            modifiers: evkey.modifiers,
        }
    }
}

/// Parameters of change of position events.
pub struct EvPos {
    /// X coordinate.
    pub x: f32,
    /// Y coordinate.
    pub y: f32,
}

impl EvPos {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvPos) -> EvPos {
        if ptr.is_null() {
            panic!("EvPos is null");
        }
        let evpos = unsafe { &*ptr };
        EvPos {
            x: evpos.x,
            y: evpos.y,
        }
    }
}

/// Resize event parameters.
pub struct EvSize {
    /// Width (size in x).
    pub width: f32,
    /// Height (size in y).
    pub height: f32,
}

impl EvSize {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvSize) -> EvSize {
        if ptr.is_null() {
            panic!("EvSize is null");
        }
        let evresize = unsafe { &*ptr };
        EvSize {
            width: evresize.width,
            height: evresize.height,
        }
    }
}

/// Window closing Event Parameters.
pub struct EvWinClose {
    /// Origin of the close.
    pub origin: GuiClose,
}

impl EvWinClose {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvWinClose) -> EvWinClose {
        if ptr.is_null() {
            panic!("EvWinClose is null");
        }
        let evclose = unsafe { &*ptr };
        EvWinClose {
            origin: GuiClose::try_from(evclose.origin).unwrap(),
        }
    }
}

/// Menu event parameters.
pub struct EvMenu {
    /// Menu item index.
    pub index: u32,
    /// Pressed item status.
    pub state: GuiState,
    /// Pressed item text.
    pub text: String,
}

impl EvMenu {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvMenu) -> EvMenu {
        if ptr.is_null() {
            panic!("EvMenu is null");
        }
        let evmenu = unsafe { &*ptr };
        EvMenu {
            index: evmenu.index,
            state: GuiState::try_from(evmenu.state).unwrap(),
            text: unsafe {
                std::ffi::CStr::from_ptr(evmenu.text)
                    .to_string_lossy()
                    .into_owned()
            },
        }
    }
}

/// Scroll event parameters.
pub struct EvScroll {
    /// Scroll bar orientation.
    pub orient: GuiOrient,
    /// Scroll type.
    pub scroll: GuiScroll,
    /// Scroll position.
    pub cpos: f32,
}

impl EvScroll {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvScroll) -> EvScroll {
        if ptr.is_null() {
            panic!("EvScroll is null");
        }
        let evscroll = unsafe { &*ptr };
        EvScroll {
            orient: GuiOrient::try_from(evscroll.orient).unwrap(),
            scroll: GuiScroll::try_from(evscroll.scroll).unwrap(),
            cpos: evscroll.cpos,
        }
    }
}

/// Location of a cell in a table.
pub struct EvTbPos {
    /// Column index.
    pub col: u32,
    /// Row index.
    pub row: u32,
}

impl EvTbPos {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvTbPos) -> EvTbPos {
        if ptr.is_null() {
            panic!("EvTbPos is null");
        }
        let evtbpos = unsafe { &*ptr };
        EvTbPos {
            col: evtbpos.col as _,
            row: evtbpos.row as _,
        }
    }
}

/// Location of a row in a table.
pub struct EvTbRow {
    /// Selected or not.
    pub sel: bool,
    /// Row index.
    pub row: u32,
}

impl EvTbRow {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvTbRow) -> EvTbRow {
        if ptr.is_null() {
            panic!("EvTbRow is null");
        }
        let evtbrow = unsafe { &*ptr };
        EvTbRow {
            sel: evtbrow.sel != 0,
            row: evtbrow.row as _,
        }
    }
}

/// Group of cells in a table.
pub struct EvTbRect {
    /// Initial column index.
    pub stcol: u32,
    /// End column index.
    pub edcol: u32,
    /// Initial row index.
    pub strow: u32,
    /// End row index.
    pub edrow: u32,
}

impl EvTbRect {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvTbRect) -> EvTbRect {
        if ptr.is_null() {
            panic!("EvTbRect is null");
        }
        let evtbrect = unsafe { &*ptr };
        EvTbRect {
            stcol: evtbrect.stcol as _,
            edcol: evtbrect.edcol as _,
            strow: evtbrect.strow as _,
            edrow: evtbrect.edrow as _,
        }
    }
}

fn array_usize(array: *mut nappgui_sys::ArrStuint32_t) -> Option<Vec<usize>> {
    if array.is_null() {
        return None;
    }

    let array = unsafe { *array };

    if array.content.is_null() {
        return None;
    }

    let content = unsafe { *array.content };

    let elem = &content.elem;

    Some(elem.iter().map(|&x| x as _).collect())
}

/// Selection in a table.
pub struct EvTbSel {
    /// Row indices.
    pub sel: Vec<usize>,
}

impl EvTbSel {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvTbSel) -> EvTbSel {
        if ptr.is_null() {
            panic!("EvTbSel is null");
        }
        let evtbsel = unsafe { &*ptr };
        EvTbSel {
            sel: array_usize(evtbsel.sel).unwrap(),
        }
    }
}

/// Data from a cell in a table.
pub struct EvTbCell {
    /// Cell text.
    pub text: String,
    /// Text alignment.
    pub align: Align,
}

impl EvTbCell {
    pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvTbCell) -> EvTbCell {
        if ptr.is_null() {
            panic!("EvTbCell is null");
        }
        let evtbcell = unsafe { &*ptr };
        EvTbCell {
            text: unsafe {
                std::ffi::CStr::from_ptr(evtbcell.text)
                    .to_string_lossy()
                    .into_owned()
            },
            align: Align::try_from(evtbcell.align).unwrap(),
        }
    }
}

macro_rules! event_params {
    ($type:ty) => {
        impl crate::core::event::NappGUIEventParams for $type {
            fn type_() -> &'static str {
                stringify!($type)
            }

            fn from_ptr(ptr: *mut std::ffi::c_void) -> Option<Self> {
                if ptr.is_null() {
                    return None;
                }

                Some(Self::from_ptr(ptr as _))
            }
        }
    };
}

event_params!(EvButton);
event_params!(EvSlider);
event_params!(EvText);
event_params!(EvDraw);
event_params!(EvMouse);
event_params!(EvWheel);
event_params!(EvKey);
event_params!(EvPos);
event_params!(EvSize);
event_params!(EvWinClose);
event_params!(EvMenu);
event_params!(EvScroll);
event_params!(EvTbPos);
event_params!(EvTbRow);
event_params!(EvTbRect);
event_params!(EvTbSel);
event_params!(EvTbCell);

macro_rules! event_params_core {
    ($type:ty, $type_name:literal) => {
        impl crate::core::event::NappGUIEventParams for $type {
            fn type_() -> &'static str {
                $type_name
            }

            fn from_ptr(ptr: *mut std::ffi::c_void) -> Option<Self> {
                if ptr.is_null() {
                    return None;
                }

                Some(unsafe { *(ptr as *mut $type) })
            }
        }
    };
}

event_params_core!(bool, "bool_t");

fn take_str_4096(text: &str) -> [i8; 4096] {
    let text = CString::new(text).unwrap();
    let mut cross_text = [0i8; 4096];
    let length = if text.as_bytes().len() < 4096 {
        text.as_bytes().len()
    } else {
        4096
    };
    cross_text[..length].copy_from_slice(
        text.as_bytes()
            .iter()
            .map(|&x| x as i8)
            .collect::<Vec<i8>>()
            .as_slice(),
    );
    cross_text
}

/// Result of the OnFilter event of the text boxes.
pub struct EvTextFilter {
    /// TRUE if the original control text should be changed.
    pub apply: bool,
    /// New control text, which is a revision (filter) of the original text. len <= 4096usize!
    pub text: String,
    /// Cursor position (caret).
    pub cpos: u32,
}

impl crate::core::event::NappGUIEventResult for EvTextFilter {
    type CrossType = nappgui_sys::EvTextFilter;

    fn type_() -> &'static str {
        "EvTextFilter"
    }

    fn to_cross_type(&self) -> Self::CrossType {
        nappgui_sys::EvTextFilter {
            apply: self.apply as _,
            text: take_str_4096(&self.text),
            cpos: self.cpos as _,
        }
    }
}

/// Keep the temp text.
static TEMP_TEXT: LazyLock<Arc<Mutex<Option<CString>>>> =
    LazyLock::new(|| Arc::new(Mutex::new(None)));

impl crate::core::event::NappGUIEventResult for EvTbCell {
    type CrossType = nappgui_sys::EvTbCell;

    fn to_cross_type(&self) -> Self::CrossType {
        let mut text = TEMP_TEXT.lock().unwrap();
        text.replace(CString::new(&*self.text).unwrap());
        let text = text.as_ref().unwrap();
        nappgui_sys::EvTbCell {
            text: text.as_ptr(),
            align: self.align as _,
        }
    }
}

macro_rules! event_results {
    ($type:ty, $cross_type:literal) => {
        impl crate::core::event::NappGUIEventResult for $type {
            type CrossType = $type;

            fn type_() -> &'static str {
                $cross_type
            }

            fn to_cross_type(&self) -> Self::CrossType {
                *self
            }
        }
    };
}

event_results!(bool, "bool_t");
event_results!(f32, "real32_t");
event_results!(u32, "u32_t");

/// The params of table on_data handler.
pub enum EvTbDataParams {
    /// Column index.
    TableNCols,
    /// Row index.
    TableCell(EvTbPos),
}

/// The result of table on_data handler.
pub enum EvTbDataResult {
    /// The text of the cell.
    TableNCols(u32),
    /// The align of the cell.
    TableCell(EvTbCell),
}

impl From<u32> for EvTbDataResult {
    fn from(value: u32) -> Self {
        Self::TableNCols(value)
    }
}

impl From<EvTbCell> for EvTbDataResult {
    fn from(value: EvTbCell) -> Self {
        Self::TableCell(value)
    }
}