use crate::{
draw_2d::{Color, ImageTrait},
gui::{
control::impl_control,
event::{EvText, EvTextFilter},
impl_layout,
},
types::{Align, FontStyle},
util::macros::callback,
};
use nappgui_sys::{
combo_OnChange, combo_OnFilter, combo_add_elem, combo_align, combo_bgcolor,
combo_bgcolor_focus, combo_color, combo_color_focus, combo_count, combo_create, combo_del_elem,
combo_duplicates, combo_get_text, combo_ins_elem, combo_phcolor, combo_phstyle, combo_phtext,
combo_set_elem, combo_text, combo_tooltip,
};
pub trait ComboTrait {
fn as_ptr(&self) -> *mut nappgui_sys::Combo;
callback! {
on_filter(EvText) -> EvTextFilter => combo_OnFilter;
on_change(EvText) -> bool => combo_OnChange;
}
fn text(&self, text: &str) {
let text = std::ffi::CString::new(text).unwrap();
unsafe {
combo_text(self.as_ptr(), text.as_ptr());
}
}
fn align(&self, align: Align) {
unsafe {
combo_align(self.as_ptr(), align as _);
}
}
fn tooltip(&self, text: &str) {
let text = std::ffi::CString::new(text).unwrap();
unsafe {
combo_tooltip(self.as_ptr(), text.as_ptr());
}
}
fn color(&self, color: Color) {
unsafe {
combo_color(self.as_ptr(), color.inner);
}
}
fn color_focus(&self, color: Color) {
unsafe {
combo_color_focus(self.as_ptr(), color.inner);
}
}
fn background_color(&self, color: Color) {
unsafe {
combo_bgcolor(self.as_ptr(), color.inner);
}
}
fn background_color_focus(&self, color: Color) {
unsafe {
combo_bgcolor_focus(self.as_ptr(), color.inner);
}
}
fn placeholder_text(&self, text: &str) {
let text = std::ffi::CString::new(text).unwrap();
unsafe {
combo_phtext(self.as_ptr(), text.as_ptr());
}
}
fn placeholder_color(&self, color: Color) {
unsafe {
combo_phcolor(self.as_ptr(), color.inner);
}
}
fn placeholder_style(&self, style: FontStyle) {
unsafe {
combo_phstyle(self.as_ptr(), style.to_fstyle_t());
}
}
fn get_text(&self, index: u32) -> String {
let text = unsafe { combo_get_text(self.as_ptr(), index) };
let text = unsafe { std::ffi::CStr::from_ptr(text) };
text.to_string_lossy().into_owned()
}
fn count(&self) -> u32 {
unsafe { combo_count(self.as_ptr()) }
}
fn add_element(&self, text: &str) {
let text = std::ffi::CString::new(text).unwrap();
unsafe {
combo_add_elem(self.as_ptr(), text.as_ptr(), std::ptr::null());
}
}
fn add_image_element<T>(&self, text: &str, image: &T)
where
T: ImageTrait,
{
let text = std::ffi::CString::new(text).unwrap();
unsafe {
combo_add_elem(self.as_ptr(), text.as_ptr(), image.as_ptr());
}
}
fn set_element(&self, index: u32, text: &str) {
let text = std::ffi::CString::new(text).unwrap();
unsafe {
combo_set_elem(self.as_ptr(), index, text.as_ptr(), std::ptr::null());
}
}
fn set_image_element<T>(&self, index: u32, text: &str, image: &T)
where
T: ImageTrait,
{
let text = std::ffi::CString::new(text).unwrap();
unsafe {
combo_set_elem(self.as_ptr(), index, text.as_ptr(), image.as_ptr());
}
}
fn insert_element(&self, index: u32, text: &str) {
let text = std::ffi::CString::new(text).unwrap();
unsafe {
combo_ins_elem(self.as_ptr(), index, text.as_ptr(), std::ptr::null());
}
}
fn insert_image_element<T>(&self, index: u32, text: &str, image: &T)
where
T: ImageTrait,
{
let text = std::ffi::CString::new(text).unwrap();
unsafe {
combo_ins_elem(self.as_ptr(), index, text.as_ptr(), image.as_ptr());
}
}
fn delete_element(&self, index: u32) {
unsafe {
combo_del_elem(self.as_ptr(), index);
}
}
fn duplicates(&self, duplicates: bool) {
unsafe {
combo_duplicates(self.as_ptr(), duplicates as _);
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct Combo {
pub(crate) inner: *mut nappgui_sys::Combo,
}
impl ComboTrait for Combo {
fn as_ptr(&self) -> *mut nappgui_sys::Combo {
self.inner
}
}
impl Combo {
pub fn new() -> Self {
let combo = Self {
inner: unsafe { combo_create() },
};
combo
}
}
impl_control!(Combo, guicontrol_combo);
impl_layout!(Combo, ComboTrait, layout_combo);