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;
widget_style_enum!(
name: RadioButtonStyle,
doc: "Style flags for RadioButton",
variants: {
Default: 0, "Default style. Represents a standalone radio button or a subsequent button in a group.",
GroupStart: ffi::WXD_RB_GROUP, "Marks this radio button as the first in a new group. Subsequent radio buttons (until the next `GroupStart` or end of dialog) belong to the same group, where only one can be selected."
},
default_variant: Default
);
#[derive(Clone, Copy)]
pub struct RadioButton {
handle: WindowHandle,
}
impl RadioButton {
pub fn builder(parent: &dyn WxWidget) -> RadioButtonBuilder<'_> {
RadioButtonBuilder::new(parent)
}
#[inline]
fn radiobutton_ptr(&self) -> *mut ffi::wxd_RadioButton_t {
self.handle
.get_ptr()
.map(|p| p as *mut ffi::wxd_RadioButton_t)
.unwrap_or(std::ptr::null_mut())
}
pub(crate) unsafe fn from_ptr(ptr: *mut ffi::wxd_RadioButton_t) -> Self {
assert!(!ptr.is_null());
RadioButton {
handle: WindowHandle::new(ptr as *mut ffi::wxd_Window_t),
}
}
pub fn get_value(&self) -> bool {
let ptr = self.radiobutton_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_RadioButton_GetValue(ptr) }
}
pub fn set_value(&self, value: bool) {
let ptr = self.radiobutton_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_RadioButton_SetValue(ptr, value) };
}
}
impl WxWidget for RadioButton {
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 RadioButton {
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 RadioButton {}
widget_builder!(
name: RadioButton,
parent_type: &'a dyn WxWidget,
style_type: RadioButtonStyle,
fields: {
label: String = String::new()
},
build_impl: |slf| {
let label_c = CString::new(slf.label.as_str()).unwrap_or_default();
let ctrl_ptr = unsafe {
ffi::wxd_RadioButton_Create(
slf.parent.handle_ptr(),
slf.id,
label_c.as_ptr(),
slf.pos.into(),
slf.size.into(),
slf.style.bits() as ffi::wxd_Style_t,
)
};
if ctrl_ptr.is_null() {
panic!("Failed to create RadioButton: FFI returned null pointer");
}
unsafe { RadioButton::from_ptr(ctrl_ptr) }
}
);
impl<'a> RadioButtonBuilder<'a> {
pub fn first_in_group(mut self) -> Self {
self.style = RadioButtonStyle::GroupStart;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RadioButtonEvent {
Selected,
}
#[derive(Debug)]
pub struct RadioButtonEventData {
base: CommandEventData,
}
impl RadioButtonEventData {
pub(crate) fn new(event: Event) -> Self {
Self {
base: CommandEventData::new(event),
}
}
pub fn get_id(&self) -> i32 {
self.base.get_id()
}
}
crate::implement_widget_local_event_handlers!(
RadioButton, RadioButtonEvent, RadioButtonEventData,
Selected => selected, EventType::COMMAND_RADIOBUTTON_SELECTED
);
#[cfg(feature = "xrc")]
impl crate::xrc::XrcSupport for RadioButton {
unsafe fn from_xrc_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
RadioButton {
handle: WindowHandle::new(ptr),
}
}
}
impl crate::window::FromWindowWithClassName for RadioButton {
fn class_name() -> &'static str {
"wxRadioButton"
}
unsafe fn from_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
RadioButton {
handle: WindowHandle::new(ptr),
}
}
}