use nappgui_sys::{
align_t, gui_close_t, gui_mouse_t, gui_orient_t, gui_scroll_t, gui_state_t, vkey_t,
};
use crate::{draw_2d::DCtx, util::helper::array_u32};
pub struct EvButton {
pub index: u32,
pub state: gui_state_t,
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,
state: evbutton.state,
text: unsafe { std::ffi::CStr::from_ptr(evbutton.text) }
.to_string_lossy()
.into_owned(),
}
}
}
pub struct EvSlider {
pub pos: f32,
pub incr: f32,
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,
}
}
}
pub struct EvText {
pub text: String,
pub cpos: u32,
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,
len: evtext.len,
}
}
}
pub struct EvTextFilter {
pub apply: bool,
pub text: String,
pub cpos: u32,
}
impl EvTextFilter {
pub(crate) fn from_ptr(ptr: *mut nappgui_sys::EvTextFilter) -> EvTextFilter {
if ptr.is_null() {
panic!("EvTextFilter is null");
}
let evfilter = unsafe { &*ptr };
EvTextFilter {
apply: evfilter.apply != 0,
text: unsafe { std::ffi::CStr::from_ptr(evfilter.text.as_ptr()) }
.to_string_lossy()
.into_owned(),
cpos: evfilter.cpos,
}
}
}
pub struct EvDraw {
pub ctx: DCtx,
pub x: f32,
pub y: f32,
pub width: f32,
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,
}
}
}
pub struct EvMouse {
pub x: f32,
pub y: f32,
pub lx: f32,
pub ly: f32,
pub button: gui_mouse_t,
pub count: u32,
pub modifiers: u32,
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: evmouse.button,
count: evmouse.count,
modifiers: evmouse.modifiers,
tag: evmouse.tag,
}
}
}
pub struct EvWheel {
pub x: f32,
pub y: f32,
pub dx: f32,
pub dy: f32,
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,
}
}
}
pub struct EvKey {
pub key: vkey_t,
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: evkey.key,
modifiers: evkey.modifiers,
}
}
}
pub struct EvPos {
pub x: f32,
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,
}
}
}
pub struct EvSize {
pub width: f32,
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,
}
}
}
pub struct EvWinClose {
pub origin: gui_close_t,
}
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: evclose.origin,
}
}
}
pub struct EvMenu {
pub index: u32,
pub state: gui_state_t,
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: evmenu.state,
text: unsafe {
std::ffi::CStr::from_ptr(evmenu.text)
.to_string_lossy()
.into_owned()
},
}
}
}
pub struct EvScroll {
pub orient: gui_orient_t,
pub scroll: gui_scroll_t,
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: evscroll.orient,
scroll: evscroll.scroll,
cpos: evscroll.cpos,
}
}
}
pub struct EvTbPos {
pub col: u32,
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,
row: evtbpos.row,
}
}
}
pub struct EvTbRow {
pub sel: bool,
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,
}
}
}
pub struct EvTbRect {
pub stcol: u32,
pub edcol: u32,
pub strow: u32,
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,
edcol: evtbrect.edcol,
strow: evtbrect.strow,
edrow: evtbrect.edrow,
}
}
}
pub struct EvTbSel {
pub sel: Vec<u32>,
}
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_u32(evtbsel.sel).unwrap(),
}
}
}
pub struct EvTbCell {
pub text: String,
pub align: align_t,
}
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: evtbcell.align,
}
}
}
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 _))
}
}
};
}
macro_rules! event_result {
($type:ty) => {
impl crate::core::event::NappGUIEventResult 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);
event_result!(EvTextFilter);