use crate::color::{Colour, colours};
use crate::event::{Event, EventType, WxEvtHandler};
use crate::geometry::{Point, Size};
use crate::id::Id;
use crate::window::{WindowHandle, WxWidget};
#[allow(unused_imports)]
use crate::window::Window;
use std::default::Default;
use wxdragon_sys as ffi;
widget_style_enum!(
name: ColourPickerCtrlStyle,
doc: "Style flags for the ColourPickerCtrl widget.",
variants: {
Default: ffi::WXD_CLRP_DEFAULT_STYLE, "Default style with no specific options.",
UseTextCtrl: ffi::WXD_CLRP_USE_TEXTCTRL, "Creates a text control to the left of the picker button which can be used by the user to specify a colour.",
ShowLabel: ffi::WXD_CLRP_SHOW_LABEL, "Shows the colour in HTML form (AABBCC) as colour button label.",
ShowAlpha: ffi::WXD_CLRP_SHOW_ALPHA, "Allows selecting opacity in the colour-chooser (effective under wxGTK and wxOSX)."
},
default_variant: Default
);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColourPickerCtrlEvent {
ColourChanged,
}
#[derive(Debug)]
pub struct ColourPickerCtrlEventData {
event: Event,
}
impl ColourPickerCtrlEventData {
pub fn new(event: Event) -> Self {
Self { event }
}
pub fn get_colour(&self) -> Option<Colour> {
if self.event.is_null() {
return None;
}
let c_colour = unsafe { ffi::wxd_ColourPickerEvent_GetColour(self.event.0) };
Some(Colour::from(c_colour))
}
}
#[derive(Clone, Copy)]
pub struct ColourPickerCtrl {
handle: WindowHandle,
}
impl ColourPickerCtrl {
pub fn builder(parent: &dyn WxWidget) -> ColourPickerCtrlBuilder<'_> {
ColourPickerCtrlBuilder::new(parent)
}
#[inline]
fn picker_ptr(&self) -> *mut ffi::wxd_ColourPickerCtrl_t {
self.handle
.get_ptr()
.map(|p| p as *mut ffi::wxd_ColourPickerCtrl_t)
.unwrap_or(std::ptr::null_mut())
}
pub fn get_colour(&self) -> Colour {
let ptr = self.picker_ptr();
if ptr.is_null() {
return colours::BLACK;
}
let c_colour = unsafe { ffi::wxd_ColourPickerCtrl_GetColour(ptr) };
Colour::from(c_colour)
}
pub fn set_colour(&self, colour: Colour) {
let ptr = self.picker_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_ColourPickerCtrl_SetColour(ptr, colour.into()) };
}
pub fn window_handle(&self) -> WindowHandle {
self.handle
}
}
crate::implement_widget_local_event_handlers!(
ColourPickerCtrl,
ColourPickerCtrlEvent,
ColourPickerCtrlEventData,
ColourChanged => colour_changed, EventType::COLOURPICKER_CHANGED
);
impl WxWidget for ColourPickerCtrl {
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 ColourPickerCtrl {
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 ColourPickerCtrl {}
#[cfg(feature = "xrc")]
impl crate::xrc::XrcSupport for ColourPickerCtrl {
unsafe fn from_xrc_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
ColourPickerCtrl {
handle: WindowHandle::new(ptr),
}
}
}
impl crate::window::FromWindowWithClassName for ColourPickerCtrl {
fn class_name() -> &'static str {
"wxColourPickerCtrl"
}
unsafe fn from_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
ColourPickerCtrl {
handle: WindowHandle::new(ptr),
}
}
}
widget_builder!(
name: ColourPickerCtrl,
parent_type: &'a dyn WxWidget,
style_type: ColourPickerCtrlStyle,
fields: {
initial_colour: Colour = colours::BLACK
},
build_impl: |slf| {
let parent_ptr = slf.parent.handle_ptr();
let pos = slf.pos.into();
let size = slf.size.into();
let colour = slf.initial_colour.into();
let ptr = unsafe {
ffi::wxd_ColourPickerCtrl_Create(
parent_ptr,
slf.id,
colour,
pos,
size,
slf.style.bits(),
)
};
if ptr.is_null() {
panic!("Failed to create wxColourPickerCtrl");
}
ColourPickerCtrl {
handle: WindowHandle::new(ptr as *mut ffi::wxd_Window_t),
}
}
);