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
use crate::{
draw_2d::{Color, Font, Image},
util::macros::callback,
};
use nappgui_sys::{
listbox_OnDown, listbox_OnSelect, listbox_add_elem, listbox_check, listbox_checkbox,
listbox_checked, listbox_clear, listbox_color, listbox_count, listbox_create, listbox_del_elem,
listbox_font, listbox_multisel, listbox_select, listbox_selected, listbox_set_elem,
listbox_size, listbox_text, S2Df,
};
/// The ListBox are controls that display a series of elements as a list.
pub struct ListBox {
pub(crate) inner: *mut nappgui_sys::ListBox,
}
impl ListBox {
pub(crate) fn new(ptr: *mut nappgui_sys::ListBox) -> Self {
if ptr.is_null() {
panic!("ptr is null");
}
Self { inner: ptr }
}
/// Create a new list control.
pub fn create() -> Self {
let listbox = unsafe { listbox_create() };
Self::new(listbox)
}
callback! {
/// Sets a handler for a mouse button press.
///
/// # Remarks
/// This event is processed before listbox_OnSelect. In the tag field of Event the number of the
/// element clicked will be received or UINT32_MAX if it corresponds to an empty area of the ListBox.
/// If the event returns FALSE on event_result, the element will be prevented from being selected
/// (TRUE by default). See GUI Events.
pub on_down(ListBox) => listbox_OnDown;
/// Set an event handler for the selection of a new item.
pub on_select(ListBox) => listbox_OnSelect;
}
/// Set the default size of the list.
///
/// # Remarks
/// It corresponds to Natural sizing of control Default 128x128.
pub fn size(&self, width: f32, height: f32) {
let size = S2Df { width, height };
unsafe {
listbox_size(self.inner, size);
}
}
/// Show or hide checkboxes to the left of items.
pub fn checkbox(&self, show: bool) {
unsafe {
listbox_checkbox(self.inner, show as i8);
}
}
/// Enable multiple selection.
pub fn multisel(&self, enable: bool) {
unsafe {
listbox_multisel(self.inner, enable as i8);
}
}
/// Adds a new element.
pub fn add_elem(&self, text: &str, image: &Image) {
let text = std::ffi::CString::new(text).unwrap();
unsafe {
listbox_add_elem(self.inner, text.as_ptr(), image.inner);
}
}
/// Edit a list item.
pub fn set_elem(&self, index: u32, text: &str, image: &Image) {
let text = std::ffi::CString::new(text).unwrap();
unsafe {
listbox_set_elem(self.inner, index, text.as_ptr(), image.inner);
}
}
/// Delete an item from the list.
pub fn del_elem(&self, index: u32) {
unsafe {
listbox_del_elem(self.inner, index);
}
}
/// Sets the font of the list.
pub fn font(&self, font: &Font) {
unsafe {
listbox_font(self.inner, font.inner);
}
}
/// Remove all items from the list.
pub fn clear(&self) {
unsafe {
listbox_clear(self.inner);
}
}
/// Sets the text color of an element.
pub fn color(&self, index: u32, color: &Color) {
unsafe {
listbox_color(self.inner, index, color.inner);
}
}
/// Select an item from the program code.
///
/// # Remarks
/// If multiple selection is not enabled, selecting one item implies de-selecting all the others.
pub fn select(&self, index: u32, select: bool) {
unsafe {
listbox_select(self.inner, index, select as i8);
}
}
/// Check or uncheck the checkbox of the element from the program code.
///
/// # Remarks
/// Checking an item is independent of selecting it. Items can be marked even if checkboxes are not
/// visible. See listbox_checkbox.
pub fn check(&self, index: u32, check: bool) {
unsafe {
listbox_check(self.inner, index, check as i8);
}
}
/// Returns the number of elements in the list.
///
/// # Remarks
/// The number of elements.
pub fn count(&self) -> u32 {
unsafe { listbox_count(self.inner) }
}
/// Returns the text of an element.
pub fn text(&self, index: u32) -> String {
unsafe {
let text = listbox_text(self.inner, index);
std::ffi::CStr::from_ptr(text)
.to_string_lossy()
.into_owned()
}
}
/// Returns whether or not an element is selected.
pub fn selected(&self, index: u32) -> bool {
unsafe { listbox_selected(self.inner, index) != 0 }
}
/// Returns whether an element is checked or not.
///
/// # Remarks
/// Checking an item is independent of selecting it. Items can be marked even if checkboxes are not
/// visible. See listbox_checkbox.
pub fn checked(&self, index: u32) -> bool {
unsafe { listbox_checked(self.inner, index) != 0 }
}
}