use std::ffi::{CStr, CString};
use wxdragon_sys as ffi;
pub struct DataFormat {
format: i32,
}
impl DataFormat {
pub fn new(format: i32) -> Self {
Self { format }
}
pub fn get_format(&self) -> i32 {
self.format
}
pub const TEXT: i32 = 1;
pub const BITMAP: i32 = 2;
pub const FILENAME: i32 = 4; }
pub trait DataObject {
fn as_data_object_ptr(&self) -> *mut ffi::wxd_DataObject_t;
}
pub trait TransferOwnership {
fn transfer_ownership(&mut self);
}
pub struct DataObjectBase {
ptr: *mut ffi::wxd_DataObject_t,
owned: bool,
}
impl DataObjectBase {
pub(crate) fn from_ptr(ptr: *mut ffi::wxd_DataObject_t, owned: bool) -> Self {
Self { ptr, owned }
}
pub(crate) fn as_ptr(&self) -> *mut ffi::wxd_DataObject_t {
self.ptr
}
pub(crate) fn transfer_ownership(&mut self) {
self.owned = false;
}
}
impl TransferOwnership for DataObjectBase {
fn transfer_ownership(&mut self) {
self.owned = false;
}
}
impl DataObject for DataObjectBase {
fn as_data_object_ptr(&self) -> *mut ffi::wxd_DataObject_t {
self.ptr
}
}
impl Drop for DataObjectBase {
fn drop(&mut self) {
if self.owned && !self.ptr.is_null() {
}
}
}
pub struct TextDataObject {
data_object: DataObjectBase,
}
impl TextDataObject {
pub fn new(text: &str) -> Self {
let c_text = CString::new(text).unwrap_or_default();
let ptr = unsafe { ffi::wxd_TextDataObject_Create(c_text.as_ptr()) };
Self {
data_object: DataObjectBase::from_ptr(ptr as *mut ffi::wxd_DataObject_t, true),
}
}
pub fn get_text(&self) -> String {
let ptr = self.data_object.as_ptr() as *mut ffi::wxd_TextDataObject_t;
let len = unsafe { ffi::wxd_TextDataObject_GetText(ptr, std::ptr::null_mut(), 0) };
if len <= 0 {
return String::new();
}
let mut buf: Vec<i8> = vec![0; len as usize + 1];
unsafe { ffi::wxd_TextDataObject_GetText(ptr, buf.as_mut_ptr(), buf.len()) };
unsafe { CStr::from_ptr(buf.as_ptr()).to_string_lossy().to_string() }
}
pub fn set_text(&mut self, text: &str) {
let text_cstring = CString::new(text).unwrap_or_default();
unsafe {
ffi::wxd_TextDataObject_SetText(
self.data_object.as_ptr() as *mut ffi::wxd_TextDataObject_t,
text_cstring.as_ptr(),
);
}
}
pub fn as_data_object(&self) -> &DataObjectBase {
&self.data_object
}
pub fn as_data_object_mut(&mut self) -> &mut DataObjectBase {
&mut self.data_object
}
}
impl DataObject for TextDataObject {
fn as_data_object_ptr(&self) -> *mut ffi::wxd_DataObject_t {
self.data_object.as_ptr()
}
}
impl Drop for TextDataObject {
fn drop(&mut self) {
if !self.data_object.as_ptr().is_null() && self.data_object.owned {
unsafe { ffi::wxd_TextDataObject_Destroy(self.data_object.as_ptr() as *mut ffi::wxd_TextDataObject_t) };
}
}
}
impl TransferOwnership for TextDataObject {
fn transfer_ownership(&mut self) {
self.data_object.transfer_ownership();
}
}
pub struct FileDataObject {
data_object: DataObjectBase,
}
impl FileDataObject {
pub fn new() -> Self {
let ptr = unsafe { ffi::wxd_FileDataObject_Create() };
Self {
data_object: DataObjectBase::from_ptr(ptr as *mut ffi::wxd_DataObject_t, true),
}
}
pub fn add_file(&mut self, file_path: &str) {
let c_path = CString::new(file_path).unwrap_or_default();
unsafe { ffi::wxd_FileDataObject_AddFile(self.data_object.as_ptr() as *mut ffi::wxd_FileDataObject_t, c_path.as_ptr()) };
}
pub fn get_file_count(&self) -> usize {
unsafe { ffi::wxd_FileDataObject_GetFileCount(self.data_object.as_ptr() as *mut ffi::wxd_FileDataObject_t) as usize }
}
pub fn get_file(&self, index: usize) -> String {
let obj = self.data_object.as_ptr() as *mut ffi::wxd_FileDataObject_t;
let mut buffer = vec![0; 1024]; let len = unsafe { ffi::wxd_FileDataObject_GetFile(obj, index as i32, buffer.as_mut_ptr(), buffer.len()) };
if len > 0 {
unsafe { CStr::from_ptr(buffer.as_ptr()).to_string_lossy().to_string() }
} else {
String::new()
}
}
pub fn get_files(&self) -> Vec<String> {
let count = self.get_file_count();
let mut files = Vec::with_capacity(count);
for i in 0..count {
files.push(self.get_file(i));
}
files
}
pub fn get_filenames(&self) -> Vec<String> {
self.get_files()
}
pub fn as_data_object(&self) -> &DataObjectBase {
&self.data_object
}
pub fn as_data_object_mut(&mut self) -> &mut DataObjectBase {
&mut self.data_object
}
}
impl DataObject for FileDataObject {
fn as_data_object_ptr(&self) -> *mut ffi::wxd_DataObject_t {
self.data_object.as_ptr()
}
}
impl Drop for FileDataObject {
fn drop(&mut self) {
if !self.data_object.as_ptr().is_null() && self.data_object.owned {
unsafe { ffi::wxd_FileDataObject_Destroy(self.data_object.as_ptr() as *mut ffi::wxd_FileDataObject_t) };
}
}
}
impl Default for FileDataObject {
fn default() -> Self {
Self::new()
}
}
impl TransferOwnership for FileDataObject {
fn transfer_ownership(&mut self) {
self.data_object.transfer_ownership();
}
}
pub struct BitmapDataObject {
data_object: DataObjectBase,
}
impl BitmapDataObject {
pub fn new(bitmap: &crate::bitmap::Bitmap) -> Self {
let ptr = unsafe { ffi::wxd_BitmapDataObject_Create(bitmap.as_const_ptr()) };
Self {
data_object: DataObjectBase::from_ptr(ptr as *mut ffi::wxd_DataObject_t, true),
}
}
pub fn get_bitmap(&self) -> Option<crate::bitmap::Bitmap> {
let ptr = unsafe { ffi::wxd_BitmapDataObject_GetBitmap(self.data_object.as_ptr() as *mut ffi::wxd_BitmapDataObject_t) };
if ptr.is_null() {
None
} else {
Some(crate::bitmap::Bitmap::from(ptr))
}
}
pub fn as_data_object(&self) -> &DataObjectBase {
&self.data_object
}
pub fn as_data_object_mut(&mut self) -> &mut DataObjectBase {
&mut self.data_object
}
}
impl DataObject for BitmapDataObject {
fn as_data_object_ptr(&self) -> *mut ffi::wxd_DataObject_t {
self.data_object.as_ptr()
}
}
impl Drop for BitmapDataObject {
fn drop(&mut self) {
if !self.data_object.as_ptr().is_null() && self.data_object.owned {
}
}
}
impl TransferOwnership for BitmapDataObject {
fn transfer_ownership(&mut self) {
self.data_object.transfer_ownership();
}
}