wxdragon 0.9.15

Safe Rust bindings for wxWidgets via the wxDragon C wrapper
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Target for drop operations.

use crate::dnd::DragResult;
use crate::prelude::WxWidget;
use std::boxed::Box;
use std::ffi::{CStr, c_void};
use std::os::raw::c_char;
use wxdragon_sys as ffi;

// Type aliases to reduce complexity warnings
type DragCallback = Box<dyn FnMut(i32, i32, DragResult) -> DragResult + 'static>;
type LeaveCallback = Box<dyn FnMut() + 'static>;
type DropCallback = Box<dyn FnMut(i32, i32) -> bool + 'static>;
type DropTextCallback = Box<dyn FnMut(&str, i32, i32) -> bool + 'static>;
type DropFilesCallback = Box<dyn FnMut(Vec<String>, i32, i32) -> bool + 'static>;

/// Callback handlers for a text drop target.
struct TextDropTargetCallbacks {
    on_enter: Option<DragCallback>,
    on_drag_over: Option<DragCallback>,
    on_leave: Option<LeaveCallback>,
    on_drop: Option<DropCallback>,
    on_data: Option<DragCallback>,
    on_drop_text: DropTextCallback,
}

impl Drop for TextDropTargetCallbacks {
    fn drop(&mut self) {
        log::debug!("Dropping TextDropTargetCallbacks");
    }
}

/// A drop target handles text data dropped via drag and drop.
#[derive(Debug)]
pub struct TextDropTarget {
    _obj: *mut ffi::wxd_TextDropTarget_t,
}

/// Builder for TextDropTarget with full callback support
pub struct TextDropTargetBuilder<'a, W: WxWidget> {
    window: &'a W,
    on_enter: Option<DragCallback>,
    on_drag_over: Option<DragCallback>,
    on_leave: Option<LeaveCallback>,
    on_drop: Option<DropCallback>,
    on_data: Option<DragCallback>,
    on_drop_text: Option<DropTextCallback>,
}

impl<'a, W: WxWidget> TextDropTargetBuilder<'a, W> {
    /// Create a new builder for TextDropTarget.
    fn new(window: &'a W) -> Self {
        Self {
            window,
            on_enter: None,
            on_drag_over: None,
            on_leave: None,
            on_drop: None,
            on_data: None,
            on_drop_text: None,
        }
    }

    /// Set the callback for when the cursor enters the drop target.
    pub fn with_on_enter<F>(mut self, callback: F) -> Self
    where
        F: FnMut(i32, i32, DragResult) -> DragResult + 'static,
    {
        self.on_enter = Some(Box::new(callback));
        self
    }

    /// Set the callback for when the cursor is dragged over the drop target.
    pub fn with_on_drag_over<F>(mut self, callback: F) -> Self
    where
        F: FnMut(i32, i32, DragResult) -> DragResult + 'static,
    {
        self.on_drag_over = Some(Box::new(callback));
        self
    }

    /// Set the callback for when the cursor leaves the drop target.
    pub fn with_on_leave<F>(mut self, callback: F) -> Self
    where
        F: FnMut() + 'static,
    {
        self.on_leave = Some(Box::new(callback));
        self
    }

    /// Set the callback for when the user drops data on the drop target.
    pub fn with_on_drop<F>(mut self, callback: F) -> Self
    where
        F: FnMut(i32, i32) -> bool + 'static,
    {
        self.on_drop = Some(Box::new(callback));
        self
    }

    /// Set the callback for when data is available after a drop.
    pub fn with_on_data<F>(mut self, callback: F) -> Self
    where
        F: FnMut(i32, i32, DragResult) -> DragResult + 'static,
    {
        self.on_data = Some(Box::new(callback));
        self
    }

    /// Set the callback for when text is dropped on the drop target.
    /// This callback is required.
    pub fn with_on_drop_text<F>(mut self, callback: F) -> Self
    where
        F: FnMut(&str, i32, i32) -> bool + 'static,
    {
        self.on_drop_text = Some(Box::new(callback));
        self
    }

    /// Create the TextDropTarget with the configured callbacks.
    pub fn build(self) -> TextDropTarget {
        // Ensure we have a text drop callback
        let on_drop_text = self.on_drop_text.expect("on_drop_text callback is required");

        // Create a struct to hold all our callbacks
        let callbacks = TextDropTargetCallbacks {
            on_enter: self.on_enter,
            on_drag_over: self.on_drag_over,
            on_leave: self.on_leave,
            on_drop: self.on_drop,
            on_data: self.on_data,
            on_drop_text,
        };

        // Create boxed data with callbacks
        let data = Box::new(callbacks);
        let data_ptr = Box::into_raw(data);
        let user_data = data_ptr as *mut c_void;

        // Create the drop target with our callback trampolines
        let _obj = unsafe {
            ffi::wxd_TextDropTarget_CreateFull(
                self.window.handle_ptr(),
                Some(text_on_enter_trampoline),
                Some(text_on_drag_over_trampoline),
                Some(text_on_leave_trampoline),
                Some(text_on_drop_trampoline),
                Some(text_on_data_trampoline),
                Some(text_on_drop_text_trampoline),
                user_data,
                Some(free_text_drop_target_userdata),
            )
        };

        // The C++ side now owns the drop target and callback data, so we don't need to keep track of them
        TextDropTarget { _obj }
    }
}

impl TextDropTarget {
    /// Creates a builder for a text drop target.
    pub fn builder<W: WxWidget>(window: &W) -> TextDropTargetBuilder<'_, W> {
        TextDropTargetBuilder::new(window)
    }
}

/// Callback handlers for a file drop target.
struct FileDropTargetCallbacks {
    on_enter: Option<DragCallback>,
    on_drag_over: Option<DragCallback>,
    on_leave: Option<LeaveCallback>,
    on_drop: Option<DropCallback>,
    on_data: Option<DragCallback>,
    on_drop_files: DropFilesCallback,
}

impl Drop for FileDropTargetCallbacks {
    fn drop(&mut self) {
        log::debug!("Dropping FileDropTargetCallbacks");
    }
}

/// A drop target handles file data dropped via drag and drop.
#[derive(Debug)]
pub struct FileDropTarget {
    _obj: *mut ffi::wxd_FileDropTarget_t,
}

/// Builder for FileDropTarget to allow setting optional callbacks.
pub struct FileDropTargetBuilder<'a, W: WxWidget> {
    window: &'a W,
    on_enter: Option<DragCallback>,
    on_drag_over: Option<DragCallback>,
    on_leave: Option<LeaveCallback>,
    on_drop: Option<DropCallback>,
    on_data: Option<DragCallback>,
    on_drop_files: Option<DropFilesCallback>,
}

impl<'a, W: WxWidget> FileDropTargetBuilder<'a, W> {
    /// Create a new builder for FileDropTarget.
    fn new(window: &'a W) -> Self {
        Self {
            window,
            on_enter: None,
            on_drag_over: None,
            on_leave: None,
            on_drop: None,
            on_data: None,
            on_drop_files: None,
        }
    }

    /// Set the callback for when the cursor enters the drop target.
    pub fn with_on_enter<F>(mut self, callback: F) -> Self
    where
        F: FnMut(i32, i32, DragResult) -> DragResult + 'static,
    {
        self.on_enter = Some(Box::new(callback));
        self
    }

    /// Set the callback for when the cursor is dragged over the drop target.
    pub fn with_on_drag_over<F>(mut self, callback: F) -> Self
    where
        F: FnMut(i32, i32, DragResult) -> DragResult + 'static,
    {
        self.on_drag_over = Some(Box::new(callback));
        self
    }

    /// Set the callback for when the cursor leaves the drop target.
    pub fn with_on_leave<F>(mut self, callback: F) -> Self
    where
        F: FnMut() + 'static,
    {
        self.on_leave = Some(Box::new(callback));
        self
    }

    /// Set the callback for when the user drops data on the drop target.
    pub fn with_on_drop<F>(mut self, callback: F) -> Self
    where
        F: FnMut(i32, i32) -> bool + 'static,
    {
        self.on_drop = Some(Box::new(callback));
        self
    }

    /// Set the callback for when data is available after a drop.
    pub fn with_on_data<F>(mut self, callback: F) -> Self
    where
        F: FnMut(i32, i32, DragResult) -> DragResult + 'static,
    {
        self.on_data = Some(Box::new(callback));
        self
    }

    /// Set the callback for when files are dropped on the drop target.
    /// This callback is required.
    pub fn with_on_drop_files<F>(mut self, callback: F) -> Self
    where
        F: FnMut(Vec<String>, i32, i32) -> bool + 'static,
    {
        self.on_drop_files = Some(Box::new(callback));
        self
    }

    /// Create the FileDropTarget with the configured callbacks.
    pub fn build(self) -> FileDropTarget {
        // Ensure we have a files drop callback
        let on_drop_files = self.on_drop_files.expect("on_drop_files callback is required");

        // Create a struct to hold all our callbacks
        let callbacks = FileDropTargetCallbacks {
            on_enter: self.on_enter,
            on_drag_over: self.on_drag_over,
            on_leave: self.on_leave,
            on_drop: self.on_drop,
            on_data: self.on_data,
            on_drop_files,
        };

        // Create boxed data with callbacks
        let data = Box::new(callbacks);
        let data_ptr = Box::into_raw(data);
        let user_data = data_ptr as *mut c_void;

        // Create the drop target with our callback trampolines
        let _obj = unsafe {
            ffi::wxd_FileDropTarget_CreateFull(
                self.window.handle_ptr(),
                Some(file_on_enter_trampoline),
                Some(file_on_drag_over_trampoline),
                Some(file_on_leave_trampoline),
                Some(file_on_drop_trampoline),
                Some(file_on_data_trampoline),
                Some(file_on_drop_files_trampoline),
                user_data,
                Some(free_file_drop_target_userdata),
            )
        };

        // The C++ side now owns the drop target and callback data, so we don't need to keep track of them
        FileDropTarget { _obj }
    }
}

impl FileDropTarget {
    /// Creates a builder for a file drop target.
    pub fn builder<W: WxWidget>(window: &W) -> FileDropTargetBuilder<'_, W> {
        FileDropTargetBuilder::new(window)
    }
}

// --- Callback trampolines for TextDropTarget ---

extern "C" fn text_on_enter_trampoline(
    x: i32,
    y: i32,
    def_result: ffi::wxd_DragResult,
    data_ptr: *mut c_void,
) -> ffi::wxd_DragResult {
    if data_ptr.is_null() {
        return def_result;
    }

    let callbacks = unsafe { &mut *(data_ptr as *mut TextDropTargetCallbacks) };

    if let Some(ref mut callback) = callbacks.on_enter {
        callback(x, y, DragResult::from(def_result)).into()
    } else {
        def_result
    }
}

extern "C" fn text_on_drag_over_trampoline(
    x: i32,
    y: i32,
    def_result: ffi::wxd_DragResult,
    data_ptr: *mut c_void,
) -> ffi::wxd_DragResult {
    if data_ptr.is_null() {
        return def_result;
    }

    let callbacks = unsafe { &mut *(data_ptr as *mut TextDropTargetCallbacks) };

    if let Some(ref mut callback) = callbacks.on_drag_over {
        callback(x, y, DragResult::from(def_result)).into()
    } else {
        def_result
    }
}

extern "C" fn text_on_leave_trampoline(data_ptr: *mut c_void) {
    if data_ptr.is_null() {
        return;
    }

    let callbacks = unsafe { &mut *(data_ptr as *mut TextDropTargetCallbacks) };

    if let Some(ref mut callback) = callbacks.on_leave {
        callback();
    }
}

extern "C" fn text_on_drop_trampoline(x: i32, y: i32, data_ptr: *mut c_void) -> bool {
    if data_ptr.is_null() {
        return false;
    }

    let callbacks = unsafe { &mut *(data_ptr as *mut TextDropTargetCallbacks) };

    if let Some(ref mut callback) = callbacks.on_drop {
        callback(x, y)
    } else {
        true // Default to accepting the drop
    }
}

extern "C" fn text_on_data_trampoline(
    x: i32,
    y: i32,
    def_result: ffi::wxd_DragResult,
    data_ptr: *mut c_void,
) -> ffi::wxd_DragResult {
    if data_ptr.is_null() {
        return def_result;
    }

    let callbacks = unsafe { &mut *(data_ptr as *mut TextDropTargetCallbacks) };

    if let Some(ref mut callback) = callbacks.on_data {
        callback(x, y, DragResult::from(def_result)).into()
    } else {
        def_result
    }
}

extern "C" fn text_on_drop_text_trampoline(text: *const c_char, x: i32, y: i32, data_ptr: *mut c_void) -> bool {
    if text.is_null() || data_ptr.is_null() {
        return false;
    }

    let text_str = unsafe { CStr::from_ptr(text).to_string_lossy().into_owned() };
    let callbacks = unsafe { &mut *(data_ptr as *mut TextDropTargetCallbacks) };

    (callbacks.on_drop_text)(&text_str, x, y)
}

// --- Callback trampolines for FileDropTarget ---

extern "C" fn file_on_enter_trampoline(
    x: i32,
    y: i32,
    def_result: ffi::wxd_DragResult,
    data_ptr: *mut c_void,
) -> ffi::wxd_DragResult {
    if data_ptr.is_null() {
        return def_result;
    }

    let callbacks = unsafe { &mut *(data_ptr as *mut FileDropTargetCallbacks) };

    if let Some(ref mut callback) = callbacks.on_enter {
        callback(x, y, DragResult::from(def_result)).into()
    } else {
        def_result
    }
}

extern "C" fn file_on_drag_over_trampoline(
    x: i32,
    y: i32,
    def_result: ffi::wxd_DragResult,
    data_ptr: *mut c_void,
) -> ffi::wxd_DragResult {
    if data_ptr.is_null() {
        return def_result;
    }

    let callbacks = unsafe { &mut *(data_ptr as *mut FileDropTargetCallbacks) };

    if let Some(ref mut callback) = callbacks.on_drag_over {
        callback(x, y, DragResult::from(def_result)).into()
    } else {
        def_result
    }
}

extern "C" fn file_on_leave_trampoline(data_ptr: *mut c_void) {
    if data_ptr.is_null() {
        return;
    }

    let callbacks = unsafe { &mut *(data_ptr as *mut FileDropTargetCallbacks) };

    if let Some(ref mut callback) = callbacks.on_leave {
        callback();
    }
}

extern "C" fn file_on_drop_trampoline(x: i32, y: i32, data_ptr: *mut c_void) -> bool {
    if data_ptr.is_null() {
        return false;
    }

    let callbacks = unsafe { &mut *(data_ptr as *mut FileDropTargetCallbacks) };

    if let Some(ref mut callback) = callbacks.on_drop {
        callback(x, y)
    } else {
        true // Default to accepting the drop
    }
}

extern "C" fn file_on_data_trampoline(
    x: i32,
    y: i32,
    def_result: ffi::wxd_DragResult,
    data_ptr: *mut c_void,
) -> ffi::wxd_DragResult {
    if data_ptr.is_null() {
        return def_result;
    }

    let callbacks = unsafe { &mut *(data_ptr as *mut FileDropTargetCallbacks) };

    if let Some(ref mut callback) = callbacks.on_data {
        callback(x, y, DragResult::from(def_result)).into()
    } else {
        def_result
    }
}

extern "C" fn file_on_drop_files_trampoline(
    filenames_ptr: *const ffi::wxd_ArrayString_t,
    x: i32,
    y: i32,
    data_ptr: *mut c_void,
) -> bool {
    if filenames_ptr.is_null() || data_ptr.is_null() {
        return false;
    }

    // Extract filenames from wxArrayString
    let mut filenames = Vec::<String>::new();

    let count = unsafe { ffi::wxd_ArrayString_GetCount(filenames_ptr) };
    filenames.reserve(count as usize);

    for i in 0..count {
        let mut buffer = vec![0; 2048]; // Buffer for path
        let len = unsafe { ffi::wxd_ArrayString_GetString(filenames_ptr, i, buffer.as_mut_ptr(), buffer.len()) };

        if len > 0 {
            let s = unsafe { CStr::from_ptr(buffer.as_ptr()).to_string_lossy().to_string() };
            filenames.push(s);
        }
    }

    let callbacks = unsafe { &mut *(data_ptr as *mut FileDropTargetCallbacks) };

    (callbacks.on_drop_files)(filenames, x, y)
}

// --- Rust-side cleanup functions for boxed user data ---

extern "C" fn free_text_drop_target_userdata(ptr: *mut c_void) {
    if ptr.is_null() {
        return;
    }
    let _ = unsafe { Box::from_raw(ptr as *mut TextDropTargetCallbacks) };
}

extern "C" fn free_file_drop_target_userdata(ptr: *mut c_void) {
    if ptr.is_null() {
        return;
    }
    let _ = unsafe { Box::from_raw(ptr as *mut FileDropTargetCallbacks) };
}