use crate::dialogs::Dialog; use crate::window::WxWidget; use std::ffi::CString;
use wxdragon_sys as ffi;
pub type MessageDialogPtr = *mut ffi::wxd_MessageDialog;
widget_style_enum!(
name: MessageDialogStyle,
doc: "Style flags for MessageDialog.",
variants: {
OK: ffi::WXD_OK, "Default button is 'OK'.",
Cancel: ffi::WXD_CANCEL, "Include 'Cancel' button.",
YesNo: ffi::WXD_YES_NO, "Use 'Yes' and 'No' buttons instead of 'OK' and 'Cancel'.",
Yes: ffi::WXD_YES, "Include 'Yes' button.",
No: ffi::WXD_NO, "Include 'No' button.",
IconNone: ffi::WXD_ICON_NONE, "No icon.",
IconWarning: ffi::WXD_ICON_WARNING, "Same as IconExclamation.",
IconError: ffi::WXD_ICON_ERROR, "Same as IconHand.",
IconQuestion: ffi::WXD_ICON_QUESTION, "Show a question mark icon.",
IconInformation: ffi::WXD_ICON_INFORMATION, "Show an information symbol.",
IconAuthNeeded: ffi::WXD_ICON_AUTH_NEEDED, "Show an authentication needed symbol.",
Centre: ffi::WXD_CENTRE, "Center the dialog on its parent."
},
default_variant: OK
);
#[derive(Clone)]
pub struct MessageDialog {
dialog_base: Dialog, }
impl MessageDialog {
pub unsafe fn from_ptr(ptr: MessageDialogPtr) -> Self {
MessageDialog {
dialog_base: unsafe { Dialog::from_ptr(ptr as *mut ffi::wxd_Dialog_t) },
}
}
pub fn builder<'a>(parent: &'a dyn WxWidget, message: &str, caption: &str) -> MessageDialogBuilder<'a> {
MessageDialogBuilder::new(parent, message, caption)
}
pub fn show_modal(&self) -> i32 {
self.dialog_base.show_modal()
}
pub fn as_ptr(&self) -> MessageDialogPtr {
self.dialog_base.as_ptr() as MessageDialogPtr
}
}
impl WxWidget for MessageDialog {
fn handle_ptr(&self) -> *mut ffi::wxd_Window_t {
self.dialog_base.handle_ptr()
}
}
impl Drop for MessageDialog {
fn drop(&mut self) {
if !self.handle_ptr().is_null() {
unsafe {
ffi::wxd_Window_Destroy(self.handle_ptr());
}
}
}
}
pub struct MessageDialogBuilder<'a> {
parent: &'a dyn WxWidget,
message: String,
caption: String,
style: MessageDialogStyle, }
impl<'a> MessageDialogBuilder<'a> {
pub fn new(parent: &'a dyn WxWidget, message: &str, caption: &str) -> Self {
MessageDialogBuilder {
parent,
message: message.to_string(),
caption: caption.to_string(),
style: MessageDialogStyle::OK, }
}
pub fn with_style(mut self, style: MessageDialogStyle) -> Self {
self.style = style;
self
}
pub fn build(self) -> MessageDialog {
let c_message = CString::new(self.message).expect("CString::new failed for message");
let c_caption = CString::new(self.caption).expect("CString::new failed for caption");
let parent_ptr = self.parent.handle_ptr();
assert!(!parent_ptr.is_null(), "MessageDialog requires a valid parent window pointer.");
let ptr = unsafe {
ffi::wxd_MessageDialog_Create(
parent_ptr,
c_message.as_ptr(),
c_caption.as_ptr(),
self.style.bits() as ffi::wxd_Style_t,
)
};
if ptr.is_null() {
panic!("Failed to create wxMessageDialog");
}
unsafe { MessageDialog::from_ptr(ptr) }
}
}