use crate::Bitmap;
use crate::event::WxEvtHandler;
use crate::window::{WindowHandle, WxWidget};
use std::ffi::CString;
use std::marker::PhantomData;
use wxdragon_sys as ffi;
pub mod about_dialog;
pub mod colour_dialog;
pub mod dir_dialog;
pub mod file_dialog;
pub mod font_dialog;
pub mod message_dialog;
pub mod multi_choice_dialog;
pub mod progress_dialog;
pub mod single_choice_dialog;
pub mod text_entry_dialog;
widget_style_enum!(
name: DialogStyle,
doc: "Style flags for Dialog.",
variants: {
DefaultDialogStyle: ffi::WXD_DEFAULT_DIALOG_STYLE, "Default dialog style (includes Caption, SystemMenu, CloseBox).",
Caption: ffi::WXD_CAPTION, "Show a caption on the dialog.",
ResizeBorder: ffi::WXD_RESIZE_BORDER, "Allow the dialog to be resized.",
SystemMenu: ffi::WXD_SYSTEM_MENU, "Show the system menu (on systems that have one).",
CloseBox: ffi::WXD_CLOSE_BOX, "Show a close box on the dialog.",
MaximizeBox: ffi::WXD_MAXIMIZE_BOX, "Show a maximize box on the dialog.",
MinimizeBox: ffi::WXD_MINIMIZE_BOX, "Show a minimize box on the dialog.",
StayOnTop: ffi::WXD_STAY_ON_TOP, "Keep the dialog on top of other windows."
},
default_variant: DefaultDialogStyle
);
#[derive(Clone, Copy)]
pub struct Dialog {
handle: WindowHandle,
_marker: PhantomData<()>,
}
impl Dialog {
pub unsafe fn from_ptr(ptr: *mut ffi::wxd_Dialog_t) -> Self {
Dialog {
handle: WindowHandle::new(ptr as *mut ffi::wxd_Window_t),
_marker: PhantomData,
}
}
pub unsafe fn from_xrc_ptr(ptr: *mut ffi::wxd_Dialog_t) -> Self {
Dialog {
handle: WindowHandle::new(ptr as *mut ffi::wxd_Window_t),
_marker: PhantomData,
}
}
#[inline]
fn dialog_ptr(&self) -> *mut ffi::wxd_Dialog_t {
self.handle
.get_ptr()
.map(|p| p as *mut ffi::wxd_Dialog_t)
.unwrap_or(std::ptr::null_mut())
}
pub fn window_handle(&self) -> WindowHandle {
self.handle
}
pub fn set_icon(&self, bitmap: &Bitmap) {
let ptr = self.dialog_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_Dialog_SetIcon(ptr, bitmap.as_const_ptr()) }
}
pub fn show_modal(&self) -> i32 {
let ptr = self.dialog_ptr();
if ptr.is_null() {
return -1;
}
unsafe { ffi::wxd_Dialog_ShowModal(ptr) }
}
pub fn end_modal(&self, ret_code: i32) {
let ptr = self.dialog_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_Dialog_EndModal(ptr, ret_code) }
}
pub fn set_escape_id(&self, id: i32) {
let ptr = self.dialog_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_Dialog_SetEscapeId(ptr, id) }
}
pub fn get_escape_id(&self) -> i32 {
let ptr = self.dialog_ptr();
if ptr.is_null() {
return crate::id::ID_NONE;
}
unsafe { ffi::wxd_Dialog_GetEscapeId(ptr) }
}
pub fn set_affirmative_id(&self, id: i32) {
let ptr = self.dialog_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_Dialog_SetAffirmativeId(ptr, id) }
}
pub fn get_affirmative_id(&self) -> i32 {
let ptr = self.dialog_ptr();
if ptr.is_null() {
return crate::id::ID_OK;
}
unsafe { ffi::wxd_Dialog_GetAffirmativeId(ptr) }
}
pub fn set_return_code(&self, ret_code: i32) {
let ptr = self.dialog_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_Dialog_SetReturnCode(ptr, ret_code) }
}
pub fn get_return_code(&self) -> i32 {
let ptr = self.dialog_ptr();
if ptr.is_null() {
return 0;
}
unsafe { ffi::wxd_Dialog_GetReturnCode(ptr) }
}
pub fn as_ptr(&self) -> *mut ffi::wxd_Dialog_t {
self.dialog_ptr()
}
pub fn builder<'a>(parent: &'a dyn WxWidget, title: &str) -> DialogBuilder<'a> {
DialogBuilder::new(parent, title)
}
}
impl WxWidget for Dialog {
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 Dialog {
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 Dialog {}
#[cfg(feature = "xrc")]
impl crate::xrc::XrcSupport for Dialog {
unsafe fn from_xrc_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
Dialog {
handle: WindowHandle::new(ptr),
_marker: PhantomData,
}
}
}
pub struct DialogBuilder<'a> {
parent: &'a dyn WxWidget,
title: String,
style: DialogStyle,
x: i32,
y: i32,
width: i32,
height: i32,
}
impl<'a> DialogBuilder<'a> {
pub fn new(parent: &'a dyn WxWidget, title: &str) -> Self {
DialogBuilder {
parent,
title: title.to_string(),
style: DialogStyle::DefaultDialogStyle,
x: -1,
y: -1,
width: -1,
height: -1,
}
}
pub fn with_style(mut self, style: DialogStyle) -> Self {
self.style = style;
self
}
pub fn with_position(mut self, x: i32, y: i32) -> Self {
self.x = x;
self.y = y;
self
}
pub fn with_size(mut self, width: i32, height: i32) -> Self {
self.width = width;
self.height = height;
self
}
pub fn build(self) -> Dialog {
let parent = self.parent.handle_ptr();
let title = CString::new(self.title).unwrap_or_else(|_| CString::new("").unwrap());
let s = self.style.bits() as ffi::wxd_Style_t;
let dialog = unsafe { ffi::wxd_Dialog_Create(parent, title.as_ptr(), s, self.x, self.y, self.width, self.height) };
if dialog.is_null() {
panic!("Failed to create Dialog");
}
unsafe { Dialog::from_ptr(dialog) }
}
}