use crate::event::event_data::CommandEventData;
use crate::event::{Event, EventType, WxEvtHandler};
use crate::geometry::{Point, Size};
use crate::id::Id;
use crate::window::{WindowHandle, WxWidget};
use std::ffi::CString;
use wxdragon_sys as ffi;
#[derive(Clone, Copy)]
pub struct CheckBox {
handle: WindowHandle,
}
impl CheckBox {
pub fn builder(parent: &dyn WxWidget) -> CheckBoxBuilder<'_> {
CheckBoxBuilder::new(parent)
}
fn new_impl(parent_ptr: *mut ffi::wxd_Window_t, id: Id, label: &str, pos: Point, size: Size, style: i64) -> Self {
let label_c = CString::new(label).unwrap_or_default();
let ctrl_ptr = unsafe {
ffi::wxd_CheckBox_Create(
parent_ptr,
id,
label_c.as_ptr(),
pos.into(),
size.into(),
style as ffi::wxd_Style_t,
)
};
assert!(!ctrl_ptr.is_null(), "wxd_CheckBox_Create returned null");
CheckBox {
handle: WindowHandle::new(ctrl_ptr as *mut ffi::wxd_Window_t),
}
}
#[inline]
fn widget_ptr(&self) -> *mut ffi::wxd_CheckBox_t {
self.handle
.get_ptr()
.map(|p| p as *mut ffi::wxd_CheckBox_t)
.unwrap_or(std::ptr::null_mut())
}
pub fn is_checked(&self) -> bool {
let ptr = self.widget_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_CheckBox_IsChecked(ptr) }
}
pub fn set_value(&self, value: bool) {
let ptr = self.widget_ptr();
if ptr.is_null() {
return;
}
unsafe {
ffi::wxd_CheckBox_SetValue(ptr, value);
}
}
pub fn get_value(&self) -> bool {
self.is_checked()
}
pub fn window_handle(&self) -> WindowHandle {
self.handle
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CheckBoxEvent {
Toggled,
}
#[derive(Debug)]
pub struct CheckBoxEventData {
base: CommandEventData,
}
impl CheckBoxEventData {
pub(crate) fn new(event: Event) -> Self {
Self {
base: CommandEventData::new(event),
}
}
pub fn is_checked(&self) -> bool {
self.base.is_checked().unwrap_or(false) }
pub fn get_id(&self) -> i32 {
self.base.get_id()
}
}
crate::implement_widget_local_event_handlers!(
CheckBox, CheckBoxEvent, CheckBoxEventData,
Toggled => toggled, EventType::CHECKBOX
);
widget_style_enum!(
name: CheckBoxStyle,
doc: "Style flags for `CheckBox`.",
variants: {
Default: ffi::WXD_CHK_2STATE, "Default style (2-state, label on the right).",
ThreeState: ffi::WXD_CHK_3STATE, "Three-state checkbox. The third state is \"undetermined\".",
AllowUserThirdState: ffi::WXD_CHK_ALLOW_3RD_STATE_FOR_USER, "Allows the user to set the checkbox to the third state (undetermined). Only applicable if `ThreeState` is also used.",
AlignLeft: 0, "Align label to the right of the checkbox (checkbox on the left). This is usually the default layout.",
AlignRight: ffi::WXD_ALIGN_RIGHT, "Align label to the left of the checkbox (checkbox on the right)."
},
default_variant: Default
);
widget_builder!(
name: CheckBox,
parent_type: &'a dyn WxWidget,
style_type: CheckBoxStyle,
fields: {
label: String = String::new(),
value: bool = false
},
build_impl: |slf| {
let parent_ptr = slf.parent.handle_ptr();
let checkbox = CheckBox::new_impl(
parent_ptr,
slf.id,
&slf.label,
slf.pos,
slf.size,
slf.style.bits(),
);
checkbox.set_value(slf.value);
checkbox
}
);
impl WxWidget for CheckBox {
fn handle_ptr(&self) -> *mut ffi::wxd_Window_t {
self.handle.get_ptr().unwrap_or(std::ptr::null_mut())
}
fn is_valid(&self) -> bool {
self.handle.is_valid()
}
}
impl WxEvtHandler for CheckBox {
unsafe fn get_event_handler_ptr(&self) -> *mut ffi::wxd_EvtHandler_t {
self.handle.get_ptr().unwrap_or(std::ptr::null_mut()) as *mut ffi::wxd_EvtHandler_t
}
}
impl crate::event::WindowEvents for CheckBox {}
#[cfg(feature = "xrc")]
impl crate::xrc::XrcSupport for CheckBox {
unsafe fn from_xrc_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
CheckBox {
handle: WindowHandle::new(ptr),
}
}
}
impl crate::window::FromWindowWithClassName for CheckBox {
fn class_name() -> &'static str {
"wxCheckBox"
}
unsafe fn from_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
CheckBox {
handle: WindowHandle::new(ptr),
}
}
}