use super::{ControlBase, ControlHandle};
use crate::win32::base_helper::check_hwnd;
use crate::win32::window_helper as wh;
use crate::{Font, NwgError, RawEventHandler, unbind_raw_event_handler};
use std::cell::RefCell;
use winapi::shared::windef::HBRUSH;
use winapi::um::{
wingdi::DeleteObject,
winuser::{WS_DISABLED, WS_GROUP, WS_TABSTOP, WS_VISIBLE},
};
const NOT_BOUND: &'static str = "RadioButton is not yet bound to a winapi object";
const BAD_HANDLE: &'static str = "INTERNAL ERROR: RadioButton handle is not HWND!";
bitflags! {
pub struct RadioButtonFlags: u32 {
const VISIBLE = WS_VISIBLE;
const DISABLED = WS_DISABLED;
const TAB_STOP = WS_TABSTOP;
const GROUP = WS_GROUP;
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum RadioButtonState {
Checked,
Unchecked,
}
#[derive(Default)]
pub struct RadioButton {
pub handle: ControlHandle,
background_brush: Option<HBRUSH>,
handler0: RefCell<Option<RawEventHandler>>,
}
impl RadioButton {
pub fn builder<'a>() -> RadioButtonBuilder<'a> {
RadioButtonBuilder {
text: "A radio button",
size: (100, 25),
position: (0, 0),
focus: false,
background_color: None,
check_state: RadioButtonState::Unchecked,
flags: None,
ex_flags: 0,
font: None,
parent: None,
}
}
pub fn check_state(&self) -> RadioButtonState {
use winapi::um::winuser::{BM_GETCHECK, BST_CHECKED, BST_UNCHECKED};
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
match wh::send_message(handle, BM_GETCHECK, 0, 0) as usize {
BST_UNCHECKED => RadioButtonState::Unchecked,
BST_CHECKED => RadioButtonState::Checked,
_ => unreachable!(),
}
}
pub fn set_check_state(&self, state: RadioButtonState) {
use winapi::shared::minwindef::WPARAM;
use winapi::um::winuser::{BM_SETCHECK, BST_CHECKED, BST_UNCHECKED};
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
let x = match state {
RadioButtonState::Unchecked => BST_UNCHECKED,
RadioButtonState::Checked => BST_CHECKED,
};
wh::send_message(handle, BM_SETCHECK, x as WPARAM, 0);
}
pub fn font(&self) -> Option<Font> {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
let font_handle = wh::get_window_font(handle);
if font_handle.is_null() {
None
} else {
Some(Font {
handle: font_handle,
})
}
}
pub fn set_font(&self, font: Option<&Font>) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::set_window_font(handle, font.map(|f| f.handle), true);
}
pub fn focus(&self) -> bool {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::get_focus(handle)
}
pub fn set_focus(&self) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::set_focus(handle);
}
pub fn enabled(&self) -> bool {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::get_window_enabled(handle)
}
pub fn set_enabled(&self, v: bool) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::set_window_enabled(handle, v)
}
pub fn visible(&self) -> bool {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::get_window_visibility(handle)
}
pub fn set_visible(&self, v: bool) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::set_window_visibility(handle, v)
}
pub fn size(&self) -> (u32, u32) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::get_window_size(handle)
}
pub fn set_size(&self, x: u32, y: u32) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::set_window_size(handle, x, y, false)
}
pub fn position(&self) -> (i32, i32) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::get_window_position(handle)
}
pub fn set_position(&self, x: i32, y: i32) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::set_window_position(handle, x, y)
}
pub fn text(&self) -> String {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::get_window_text(handle)
}
pub fn set_text<'a>(&self, v: &'a str) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::set_window_text(handle, v)
}
pub fn class_name(&self) -> &'static str {
"BUTTON"
}
pub fn flags(&self) -> u32 {
WS_VISIBLE
}
pub fn forced_flags(&self) -> u32 {
use winapi::um::winuser::{BS_AUTORADIOBUTTON, BS_NOTIFY, WS_CHILD};
BS_NOTIFY | WS_CHILD | BS_AUTORADIOBUTTON
}
fn hook_background_color(&mut self, c: [u8; 3]) {
use crate::bind_raw_event_handler_inner;
use winapi::shared::{basetsd::UINT_PTR, minwindef::LRESULT, windef::HWND};
use winapi::um::wingdi::{CreateSolidBrush, RGB};
use winapi::um::winuser::WM_CTLCOLORSTATIC;
if self.handle.blank() {
panic!("{}", NOT_BOUND);
}
let handle = self.handle.hwnd().expect(BAD_HANDLE);
let parent_handle = ControlHandle::Hwnd(wh::get_window_parent(handle));
let brush = unsafe { CreateSolidBrush(RGB(c[0], c[1], c[2])) };
self.background_brush = Some(brush);
let handler = bind_raw_event_handler_inner(
&parent_handle,
handle as UINT_PTR,
move |_hwnd, msg, _w, l| {
match msg {
WM_CTLCOLORSTATIC => {
let child = l as HWND;
if child == handle {
return Some(brush as LRESULT);
}
}
_ => {}
}
None
},
);
*self.handler0.borrow_mut() = Some(handler.unwrap());
}
}
impl Drop for RadioButton {
fn drop(&mut self) {
let handler = self.handler0.borrow();
if let Some(h) = handler.as_ref() {
drop(unbind_raw_event_handler(h));
}
if let Some(bg) = self.background_brush {
unsafe {
DeleteObject(bg as _);
}
}
self.handle.destroy();
}
}
pub struct RadioButtonBuilder<'a> {
text: &'a str,
size: (i32, i32),
position: (i32, i32),
focus: bool,
background_color: Option<[u8; 3]>,
check_state: RadioButtonState,
flags: Option<RadioButtonFlags>,
ex_flags: u32,
font: Option<&'a Font>,
parent: Option<ControlHandle>,
}
impl<'a> RadioButtonBuilder<'a> {
pub fn flags(mut self, flags: RadioButtonFlags) -> RadioButtonBuilder<'a> {
self.flags = Some(flags);
self
}
pub fn ex_flags(mut self, flags: u32) -> RadioButtonBuilder<'a> {
self.ex_flags = flags;
self
}
pub fn text(mut self, text: &'a str) -> RadioButtonBuilder<'a> {
self.text = text;
self
}
pub fn size(mut self, size: (i32, i32)) -> RadioButtonBuilder<'a> {
self.size = size;
self
}
pub fn position(mut self, pos: (i32, i32)) -> RadioButtonBuilder<'a> {
self.position = pos;
self
}
pub fn focus(mut self, focus: bool) -> RadioButtonBuilder<'a> {
self.focus = focus;
self
}
pub fn check_state(mut self, check: RadioButtonState) -> RadioButtonBuilder<'a> {
self.check_state = check;
self
}
pub fn background_color(mut self, color: Option<[u8; 3]>) -> RadioButtonBuilder<'a> {
self.background_color = color;
self
}
pub fn font(mut self, font: Option<&'a Font>) -> RadioButtonBuilder<'a> {
self.font = font;
self
}
pub fn parent<C: Into<ControlHandle>>(mut self, p: C) -> RadioButtonBuilder<'a> {
self.parent = Some(p.into());
self
}
pub fn build(self, out: &mut RadioButton) -> Result<(), NwgError> {
let flags = self.flags.map(|f| f.bits()).unwrap_or(out.flags());
let parent = match self.parent {
Some(p) => Ok(p),
None => Err(NwgError::no_parent("RadioButton")),
}?;
*out = Default::default();
out.handle = ControlBase::build_hwnd()
.class_name(out.class_name())
.forced_flags(out.forced_flags())
.flags(flags)
.ex_flags(self.ex_flags)
.size(self.size)
.position(self.position)
.text(self.text)
.parent(Some(parent))
.build()?;
if self.font.is_some() {
out.set_font(self.font);
} else {
out.set_font(Font::global_default().as_ref());
}
if self.background_color.is_some() {
out.hook_background_color(self.background_color.unwrap());
}
if self.focus {
out.set_focus();
}
out.set_check_state(self.check_state);
Ok(())
}
}
impl PartialEq for RadioButton {
fn eq(&self, other: &Self) -> bool {
self.handle == other.handle
}
}