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
use nappgui_sys::{
edit_OnChange, edit_OnFilter, edit_OnFocus, edit_align, edit_autoselect, edit_bgcolor,
edit_bgcolor_focus, edit_color, edit_color_focus, edit_copy, edit_create, edit_cut,
edit_editable, edit_font, edit_get_height, edit_get_text, edit_multiline, edit_passmode,
edit_paste, edit_phcolor, edit_phstyle, edit_phtext, edit_select, edit_text, edit_tooltip,
edit_vpadding,
};
use crate::{draw_2d::font::Font, prelude::Align, util::macros::callback};
/// EditBox are small text boxes with editing capabilities. Like the Label they are of uniform format:
/// The typeface and colors will affect the entire text
pub struct Edit {
pub(crate) inner: *mut nappgui_sys::Edit,
}
impl Edit {
pub(crate) fn new(ptr: *mut nappgui_sys::Edit) -> Self {
if ptr.is_null() {
panic!("ptr is null");
}
Self { inner: ptr }
}
/// Create a text edit control.
pub fn create() -> Self {
let edit = unsafe { edit_create() };
Self::new(edit)
}
/// Create a text editing control that allows multiple lines.
pub fn new_multiline() -> Self {
let edit = unsafe { edit_multiline() };
Self::new(edit)
}
callback! {
/// Set a function to filter the text while editing.
pub on_filter(Edit) => edit_OnFilter;
/// Set a function to detect when the text has changed.
pub on_change(Edit) => edit_OnChange;
/// Sets a handler for keyboard focus.
pub on_focus(Edit) => edit_OnFocus;
}
/// Set the edit control text.
pub fn text(&self, text: &str) {
let text = std::ffi::CString::new(text).unwrap();
unsafe {
edit_text(self.inner, text.as_ptr());
}
}
/// Set the font of the edit control.
pub fn font(&self, font: &Font) {
unsafe {
edit_font(self.inner, font.inner);
}
}
/// Set text alignment.
pub fn align(&self, align: Align) {
unsafe {
edit_align(self.inner, align);
}
}
/// Activate the password mode, which will hide the typed characters.
pub fn password(&self, passmode: bool) {
unsafe {
edit_passmode(self.inner, passmode as i8);
}
}
/// Enable or disable editing in the control.
pub fn editable(&self, editable: bool) {
unsafe {
edit_editable(self.inner, editable as i8);
}
}
/// Activate or deactivate auto-selection of text.
pub fn autoselect(&self, autoselect: bool) {
unsafe {
edit_autoselect(self.inner, autoselect as i8);
}
}
/// Select text.
pub fn select(&self, start: i32, end: i32) {
unsafe {
edit_select(self.inner, start, end);
}
}
/// Assigns a tooltip to the edit control.
pub fn tooltip(&self, text: &str) {
let text = std::ffi::CString::new(text).unwrap();
unsafe {
edit_tooltip(self.inner, text.as_ptr());
}
}
/// Set the text color.
///
/// # Remarks
/// RGB values may not be fully portable. See Colors.
pub fn color(&self, color: u32) {
unsafe {
edit_color(self.inner, color);
}
}
/// Sets the color of the text, when the control has the keyboard focus.
///
/// # Remarks
/// RGB values may not be fully portable. See Colors.
pub fn color_focus(&self, color: u32) {
unsafe {
edit_color_focus(self.inner, color);
}
}
/// Set the background color.
pub fn bgcolor(&self, color: u32) {
unsafe {
edit_bgcolor(self.inner, color);
}
}
/// Sets the background color, when the control has keyboard focus.
pub fn bgcolor_focus(&self, color: u32) {
unsafe {
edit_bgcolor_focus(self.inner, color);
}
}
/// Set an explanatory text for when the control is blank (placeholder).
pub fn phtext(&self, text: &str) {
let text = std::ffi::CString::new(text).unwrap();
unsafe {
edit_phtext(self.inner, text.as_ptr());
}
}
/// Set the color of the placeholder text.
pub fn phcolor(&self, color: u32) {
unsafe {
edit_phcolor(self.inner, color);
}
}
/// Set the font style for the placeholder.
pub fn phstyle(&self, style: u32) {
unsafe {
edit_phstyle(self.inner, style);
}
}
/// Sets the inner vertical margin.
pub fn vpadding(&self, padding: f32) {
unsafe {
edit_vpadding(self.inner, padding);
}
}
/// Get control text.
pub fn get_text(&self) -> String {
let text = unsafe { edit_get_text(self.inner) };
let text = unsafe { std::ffi::CStr::from_ptr(text) };
text.to_string_lossy().into_owned()
}
/// Gets the current height of the control.
pub fn get_height(&self) -> f32 {
unsafe { edit_get_height(self.inner) }
}
/// Copies the selected text to the clipboard.
pub fn copy(&self) {
unsafe {
edit_copy(self.inner);
}
}
/// Cuts the selected text, copying it to the clipboard.
pub fn cut(&self) {
unsafe {
edit_cut(self.inner);
}
}
/// Pastes the text from the clipboard into the caret position.
pub fn paste(&self) {
unsafe {
edit_paste(self.inner);
}
}
}