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
use core::{
ffi::c_char,
ptr::{null, null_mut},
};
use crate::{
c_api, cstring, error, error_ref_mut, error_str,
widgets::{NcSelector, NcSelectorBuilder, NcSelectorItem, NcSelectorOptions},
NcChannels, NcInput, NcPlane, NcResult, NcString,
};
impl NcSelector {
pub fn new<'a>(plane: &mut NcPlane, options: &NcSelectorOptions) -> NcResult<&'a mut Self> {
error_ref_mut![
unsafe { c_api::ncselector_create(plane, options) },
"ncselector_create"
]
}
pub fn builder() -> NcSelectorBuilder {
NcSelectorBuilder::new()
}
pub fn offer_input(&mut self, input: impl Into<NcInput>) -> bool {
unsafe { c_api::ncselector_offer_input(self, &input.into()) }
}
pub fn destroy(&mut self) -> NcResult<()> {
unsafe { c_api::ncselector_destroy(self, null_mut()) };
Ok(())
}
pub fn additem(&mut self, item: NcSelectorItem) -> NcResult<i32> {
error![
unsafe { c_api::ncselector_additem(self, &item) },
"Calling selector.additem", -1
]
}
pub fn delitem(&mut self, item: &str) -> NcResult<i32> {
let cs = cstring![item];
error![
unsafe { c_api::ncselector_delitem(self, cs.as_ptr()) },
"Calling selector.delitem", -1
]
}
pub fn selected(&mut self) -> Option<String> {
let res = unsafe { c_api::ncselector_selected(self) };
if res.is_null() {
None
} else {
Some(crate::rstring!(res).to_string())
}
}
pub fn nextitem(&mut self) -> NcResult<String> {
let cstr: *const c_char = unsafe { c_api::ncselector_nextitem(self) };
error_str![cstr, "Calling selector.nextitem"]
}
pub fn previtem(&mut self) -> NcResult<String> {
let cstr: *const c_char = unsafe { c_api::ncselector_previtem(self) };
error_str![cstr, "Calling selector.previtem"]
}
}
impl NcSelectorItem {
pub fn new(option: &NcString, desc: &NcString) -> Self {
Self { option: option.as_ptr(), desc: desc.as_ptr() }
}
pub fn new_empty() -> Self {
Self { option: null(), desc: null() }
}
}
impl NcSelectorOptions {
pub fn new(items: &[NcSelectorItem]) -> Self {
Self {
title: null(),
secondary: null(),
footer: null(),
items: items.as_ptr(),
defidx: 0,
maxdisplay: 0,
opchannels: 0,
descchannels: 0,
titlechannels: 0,
footchannels: 0,
boxchannels: 0,
flags: 0,
}
}
pub fn with_all_options(
title: Option<&NcString>,
secondary: Option<&NcString>,
footer: Option<&NcString>,
items: &[NcSelectorItem],
default: u32,
max_display: u32,
opchannels: impl Into<NcChannels>,
descchannels: impl Into<NcChannels>,
titlechannels: impl Into<NcChannels>,
footchannels: impl Into<NcChannels>,
boxchannels: impl Into<NcChannels>,
) -> Self {
assert![!items.is_empty()]; let title_ptr = if let Some(s) = title { s.as_ptr() } else { null() };
let secondary_ptr = if let Some(s) = secondary { s.as_ptr() } else { null() };
let footer_ptr = if let Some(s) = footer { s.as_ptr() } else { null() };
Self {
title: title_ptr,
secondary: secondary_ptr,
footer: footer_ptr,
items: items.as_ptr(),
defidx: default,
maxdisplay: max_display,
opchannels: opchannels.into().into(),
descchannels: descchannels.into().into(),
titlechannels: titlechannels.into().into(),
footchannels: footchannels.into().into(),
boxchannels: boxchannels.into().into(),
flags: 0x0,
}
}
}