use std::ffi::CString;
use crate::{
draw_2d::{Color, Font},
gui::{control::impl_control, event::EvMouse, impl_layout},
types::{Align, FontStyle},
util::macros::callback,
};
use nappgui_sys::{
label_OnClick, label_align, label_bgcolor, label_bgcolor_over, label_color, label_color_over,
label_create, label_font, label_multiline, label_size_text, label_style_over, label_text,
};
pub trait LabelTrait {
fn as_ptr(&self) -> *mut nappgui_sys::Label;
callback! {
on_click(EvMouse) => label_OnClick;
}
fn text(&self, text: &str) {
let text = CString::new(text).unwrap();
unsafe {
label_text(self.as_ptr(), text.as_ptr());
}
}
fn size_text(&self, text: &str) {
let text = CString::new(text).unwrap();
unsafe {
label_size_text(self.as_ptr(), text.as_ptr());
}
}
fn font(&self, font: &Font) {
unsafe {
label_font(self.as_ptr(), font.inner);
}
}
fn style_over(&self, style: FontStyle) {
unsafe {
label_style_over(self.as_ptr(), style.to_fstyle_t() as _);
}
}
fn multiline(&self, multiline: bool) {
unsafe { label_multiline(self.as_ptr(), multiline as _) };
}
fn align(&self, align: Align) {
unsafe {
label_align(self.as_ptr(), align as _);
}
}
fn color(&self, color: Color) {
unsafe {
label_color(self.as_ptr(), color.inner);
}
}
fn color_over(&self, color: Color) {
unsafe {
label_color_over(self.as_ptr(), color.inner);
}
}
fn background_color(&self, color: Color) {
unsafe {
label_bgcolor(self.as_ptr(), color.inner);
}
}
fn background_color_over(&self, color: Color) {
unsafe {
label_bgcolor_over(self.as_ptr(), color.inner);
}
}
}
#[repr(transparent)]
#[derive(Clone, Copy, Debug)]
pub struct Label {
pub(crate) inner: *mut nappgui_sys::Label,
}
impl LabelTrait for Label {
fn as_ptr(&self) -> *mut nappgui_sys::Label {
self.inner
}
}
impl Default for Label {
fn default() -> Self {
let label = unsafe { label_create() };
Self { inner: label }
}
}
impl Label {
pub fn new(text: &str) -> Label {
let label = Label::default();
label.text(text);
label
}
}
impl_control!(Label, guicontrol_label);
impl_layout!(Label, LabelTrait, layout_label);