use super::Control;
use callback_helpers::{from_void_ptr, to_heap_ptr};
use std::ffi::{CStr, CString};
use std::mem;
use std::os::raw::c_void;
use libui_ffi::{self, uiButton, uiControl};
define_control! {
rust_type: Button,
sys_type: uiButton
}
impl Button {
pub fn new(text: &str) -> Button {
unsafe {
let c_string = CString::new(text.as_bytes().to_vec()).unwrap();
Button::from_raw(libui_ffi::uiNewButton(c_string.as_ptr()))
}
}
pub fn text(&self) -> String {
unsafe {
CStr::from_ptr(libui_ffi::uiButtonText(self.uiButton))
.to_string_lossy()
.into_owned()
}
}
pub fn text_ref(&self) -> &CStr {
unsafe { CStr::from_ptr(libui_ffi::uiButtonText(self.uiButton)) }
}
pub fn set_text(&mut self, text: &str) {
unsafe {
let c_string = CString::new(text.as_bytes().to_vec()).unwrap();
libui_ffi::uiButtonSetText(self.uiButton, c_string.as_ptr())
}
}
pub fn on_clicked<'ctx, F>(&mut self, callback: F)
where
F: FnMut(&mut Button) + 'static,
{
extern "C" fn c_callback<G>(button: *mut uiButton, data: *mut c_void)
where
G: FnMut(&mut Button),
{
let mut button = Button { uiButton: button };
unsafe {
from_void_ptr::<G>(data)(&mut button);
}
}
unsafe {
libui_ffi::uiButtonOnClicked(self.uiButton, Some(c_callback::<F>), to_heap_ptr(callback));
}
}
}