nspire/bindings/
msg.rs

1use core::mem::transmute;
2use core::slice;
3
4use cstr_core::CStr;
5use ndless_sys::_show_msgbox;
6
7use crate::cstr;
8use crate::prelude::*;
9
10#[repr(u32)]
11#[derive(Debug)]
12pub enum Button {
13	ONE = 1,
14	TWO = 2,
15	THREE = 3,
16}
17
18pub fn msg(title: impl Into<String>, msg: impl Into<String>) {
19	let title = cstr!(title.into());
20	let msg = cstr!(msg.into());
21	unsafe {
22		_show_msgbox(title.as_ptr(), msg.as_ptr(), 0);
23	}
24}
25
26pub fn msg_2b(
27	title: impl Into<String>,
28	msg: impl Into<String>,
29	btn1: impl Into<String>,
30	btn2: impl Into<String>,
31) -> Button {
32	let title = cstr!(title.into());
33	let msg = cstr!(msg.into());
34	let btn1 = cstr!(btn1.into());
35	let btn2 = cstr!(btn2.into());
36	unsafe {
37		transmute(_show_msgbox(
38			title.as_ptr(),
39			msg.as_ptr(),
40			2,
41			btn1.as_ptr(),
42			btn2.as_ptr(),
43		))
44	}
45}
46
47pub fn msg_3b(
48	title: impl Into<String>,
49	msg: impl Into<String>,
50	btn1: impl Into<String>,
51	btn2: impl Into<String>,
52	btn3: impl Into<String>,
53) -> Button {
54	let title = cstr!(title.into());
55	let msg = cstr!(msg.into());
56	let btn1 = cstr!(btn1.into());
57	let btn2 = cstr!(btn2.into());
58	let btn3 = cstr!(btn3.into());
59	unsafe {
60		transmute(_show_msgbox(
61			title.as_ptr(),
62			msg.as_ptr(),
63			3,
64			btn1.as_ptr(),
65			btn2.as_ptr(),
66			btn3.as_ptr(),
67		))
68	}
69}
70
71pub fn msg_numeric(
72	title: impl Into<String>,
73	subtitle: impl Into<String>,
74	msg: impl Into<String>,
75	min: i32,
76	max: i32,
77) -> Option<i32> {
78	let title = cstr!(title.into());
79	let subtitle = cstr!(subtitle.into());
80	let msg = cstr!(msg.into());
81	let mut num = 0i32;
82	match unsafe {
83		ndless_sys::show_1numeric_input(
84			title.as_ptr(),
85			subtitle.as_ptr(),
86			msg.as_ptr(),
87			&mut num,
88			min,
89			max,
90		)
91	} {
92		1 => Some(num),
93		_ => None
94	}
95}
96
97pub fn msg_2numeric(
98	title: impl Into<String>,
99	subtitle: impl Into<String>,
100	msg1: impl Into<String>,
101	min1: i32,
102	max1: i32,
103	msg2: impl Into<String>,
104	min2: i32,
105	max2: i32,
106) -> Option<(i32, i32)> {
107	let title = cstr!(title.into());
108	let subtitle = cstr!(subtitle.into());
109	let msg1 = cstr!(msg1.into());
110	let msg2 = cstr!(msg2.into());
111	let mut num1 = 0i32;
112	let mut num2 = 0i32;
113	match unsafe {
114		ndless_sys::show_2numeric_input(
115			title.as_ptr(),
116			subtitle.as_ptr(),
117			msg1.as_ptr(),
118			&mut num1,
119			min1,
120			max1,
121			msg2.as_ptr(),
122			&mut num2,
123			min2,
124			max2,
125		)
126	} {
127		1 => Some((num1, num2)),
128		_ => None
129	}
130}
131
132pub fn msg_input(title: impl Into<String>, msg: impl Into<String>, default: impl Into<String>) -> Option<String> {
133	let title = cstr!(title.into());
134	let msg = cstr!(msg.into());
135	let default = cstr!(default.into());
136	let mut ptr: *mut cty::c_char = core::ptr::null_mut();
137	match unsafe {
138		ndless_sys::show_msg_user_input(title.as_ptr(), msg.as_ptr(), default.as_ptr(), &mut ptr)
139	} {
140		-1 => None,
141		len => unsafe {
142			Some(CStr::from_bytes_with_nul_unchecked(
143				slice::from_raw_parts(ptr as *const u8, len as usize + 1))
144				.to_string_lossy()
145				.into_owned()
146			)
147		}
148	}
149}