use std::ffi::CString;
use crate::{dialogs::Dialog, font::Font, font_data::FontData, window::WxWidget};
use wxdragon_sys as ffi;
#[derive(Clone, Copy)]
pub struct FontDialog {
dialog_base: Dialog,
}
pub struct FontDialogBuilder<'a, W: WxWidget> {
parent: &'a W,
title: String,
font_data: Option<&'a FontData>,
}
impl FontDialog {
pub fn builder<'a, W: WxWidget>(parent: &'a W) -> FontDialogBuilder<'a, W> {
FontDialogBuilder {
parent,
title: "Choose a font".to_string(),
font_data: None,
}
}
pub(crate) unsafe fn from_ptr(ptr: *mut ffi::wxd_FontDialog_t) -> Self {
FontDialog {
dialog_base: unsafe { Dialog::from_ptr(ptr as *mut ffi::wxd_Dialog_t) },
}
}
fn as_ptr(&self) -> *mut ffi::wxd_FontDialog_t {
self.dialog_base.as_ptr() as *mut ffi::wxd_FontDialog_t
}
pub fn show_modal(&self) -> i32 {
self.dialog_base.show_modal()
}
pub fn get_font(&self) -> Option<Font> {
let font_ptr = unsafe { ffi::wxd_FontDialog_GetFont(self.as_ptr()) };
if font_ptr.is_null() {
None
} else {
Some(unsafe { Font::from_ptr(font_ptr, true) })
}
}
pub fn get_font_data(&self) -> Option<FontData> {
let data_ptr = unsafe { ffi::wxd_FontDialog_GetFontData(self.as_ptr()) };
if data_ptr.is_null() {
None
} else {
Some(FontData { ptr: data_ptr })
}
}
}
impl<'a, W: WxWidget> FontDialogBuilder<'a, W> {
pub fn with_title(mut self, title: &str) -> Self {
self.title = title.to_string();
self
}
pub fn with_font_data(mut self, font_data: &'a FontData) -> Self {
self.font_data = Some(font_data);
self
}
pub fn build(self) -> FontDialog {
let c_title = CString::new(self.title).expect("CString::new failed for title");
let font_data_ptr = self.font_data.map_or(std::ptr::null_mut(), |d| d.as_ptr());
let parent_ptr = self.parent.handle_ptr();
assert!(!parent_ptr.is_null(), "FontDialog requires a valid parent window pointer.");
let ptr = unsafe { ffi::wxd_FontDialog_Create(parent_ptr, c_title.as_ptr(), font_data_ptr) };
if ptr.is_null() {
panic!("Failed to create wxFontDialog");
}
unsafe { FontDialog::from_ptr(ptr) }
}
}
impl WxWidget for FontDialog {
fn handle_ptr(&self) -> *mut ffi::wxd_Window_t {
self.dialog_base.handle_ptr()
}
}