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
//!
//! Safe wrapper for wxTextCtrl.

use crate::event::TextEvents;
use crate::event::{Event, EventType, WxEvtHandler};
use crate::geometry::{Point, Size};
use crate::id::Id;
use crate::window::{WindowHandle, WxWidget};
// Window is used by new_from_composition for backwards compatibility
#[allow(unused_imports)]
use crate::window::Window;
use std::ffi::CString;
use std::os::raw::c_char;
use std::ptr::null_mut;
use wxdragon_sys as ffi;

// --- Text Control Styles ---
widget_style_enum!(
    name: TextCtrlStyle,
    doc: "Style flags for TextCtrl widget.",
    variants: {
        Default: 0, "Default style (single line, editable, left-aligned).",
        MultiLine: ffi::WXD_TE_MULTILINE, "Multi-line text control.",
        Password: ffi::WXD_TE_PASSWORD, "Password entry control (displays characters as asterisks).",
        ReadOnly: ffi::WXD_TE_READONLY, "Read-only text control.",
        Rich: ffi::WXD_TE_RICH, "For rich text content (implies multiline). Use with care, may require specific handling.",
        Rich2: ffi::WXD_TE_RICH2, "For more advanced rich text content (implies multiline). Use with care.",
        AutoUrl: ffi::WXD_TE_AUTO_URL, "Automatically detect and make URLs clickable.",
        ProcessEnter: ffi::WXD_TE_PROCESS_ENTER, "Generate an event when Enter key is pressed.",
        ProcessTab: ffi::WXD_TE_PROCESS_TAB, "Process TAB key in the control instead of using it for navigation.",
        NoHideSel: ffi::WXD_TE_NOHIDESEL, "Always show selection, even when control doesn't have focus (Windows only).",
        Centre: ffi::WXD_TE_CENTRE, "Center-align text.",
        Right: ffi::WXD_TE_RIGHT, "Right-align text.",
        CharWrap: ffi::WXD_TE_CHARWRAP, "Wrap at any position, splitting words if necessary.",
        WordWrap: ffi::WXD_TE_WORDWRAP, "Wrap at word boundaries.",
        NoVScroll: ffi::WXD_TE_NO_VSCROLL, "No vertical scrollbar (multiline only).",
        DontWrap: ffi::WXD_TE_DONTWRAP, "Don't wrap at all, show horizontal scrollbar instead."
    },
    default_variant: Default
);

/// Events emitted by TextCtrl
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextCtrlEvent {
    /// Emitted when the text in the control changes
    TextChanged,
    /// Emitted when the user presses Enter in the control
    TextEnter,
}

/// Event data for a TextCtrl event
#[derive(Debug)]
pub struct TextCtrlEventData {
    event: Event,
}

impl TextCtrlEventData {
    /// Create a new TextCtrlEventData from a generic Event
    pub fn new(event: Event) -> Self {
        Self { event }
    }

    /// Get the ID of the control that generated the event
    pub fn get_id(&self) -> i32 {
        self.event.get_id()
    }

    /// Skip this event (allow it to be processed by the parent window)
    pub fn skip(&self, skip: bool) {
        self.event.skip(skip);
    }

    /// Get the current text in the control
    pub fn get_string(&self) -> Option<String> {
        self.event.get_string()
    }
}

/// Represents a wxTextCtrl widget.
///
/// TextCtrl uses `WindowHandle` internally for safe memory management.
/// When the underlying window is destroyed (by calling `destroy()` or when
/// its parent is destroyed), the handle becomes invalid and all operations
/// become safe no-ops.
///
/// # Example
/// ```ignore
/// let textctrl = TextCtrl::builder(&frame).value("Enter text here").build();
///
/// // TextCtrl is Copy - no clone needed for closures!
/// textctrl.bind_text_changed(move |_| {
///     // Safe: if textctrl was destroyed, this is a no-op
///     let text = textctrl.get_value();
///     println!("Text changed: {}", text);
/// });
///
/// // After parent destruction, textctrl operations are safe no-ops
/// frame.destroy();
/// assert!(!textctrl.is_valid());
/// ```
#[derive(Clone, Copy)]
pub struct TextCtrl {
    /// Safe handle to the underlying wxTextCtrl - automatically invalidated on destroy
    handle: WindowHandle,
}

impl TextCtrl {
    /// Creates a new TextCtrl builder.
    pub fn builder(parent: &dyn WxWidget) -> TextCtrlBuilder<'_> {
        TextCtrlBuilder::new(parent)
    }

    /// Creates a new TextCtrl wrapper from a raw pointer.
    /// # Safety
    /// The pointer must be a valid `wxd_TextCtrl_t` pointer.
    pub(crate) unsafe fn from_ptr(ptr: *mut ffi::wxd_TextCtrl_t) -> Self {
        TextCtrl {
            handle: WindowHandle::new(ptr as *mut ffi::wxd_Window_t),
        }
    }

    /// Creates a new TextCtrl from a raw window pointer.
    /// This is for backwards compatibility with widgets that compose TextCtrl.
    /// The parent_ptr parameter is ignored (kept for API compatibility).
    #[allow(dead_code)]
    pub(crate) fn new_from_composition(_window: Window, _parent_ptr: *mut ffi::wxd_Window_t) -> Self {
        // Use the window's pointer to create a new WindowHandle
        Self {
            handle: WindowHandle::new(_window.as_ptr()),
        }
    }

    /// Internal implementation used by the builder.
    fn new_impl(parent_ptr: *mut ffi::wxd_Window_t, id: Id, value: &str, pos: Point, size: Size, style: i64) -> Self {
        let c_value = CString::new(value).unwrap_or_default();

        let ptr = unsafe {
            ffi::wxd_TextCtrl_Create(
                parent_ptr,
                id,
                c_value.as_ptr(),
                pos.into(),
                size.into(),
                style as ffi::wxd_Style_t,
            )
        };

        if ptr.is_null() {
            panic!("Failed to create TextCtrl widget");
        }

        unsafe { TextCtrl::from_ptr(ptr) }
    }

    /// Helper to get raw textctrl pointer, returns null if widget has been destroyed
    #[inline]
    fn textctrl_ptr(&self) -> *mut ffi::wxd_TextCtrl_t {
        self.handle
            .get_ptr()
            .map(|p| p as *mut ffi::wxd_TextCtrl_t)
            .unwrap_or(null_mut())
    }

    fn read_string_with_retry(mut getter: impl FnMut(*mut c_char, i32) -> i32) -> String {
        let mut buffer: Vec<c_char> = vec![0; 1024];
        let mut len = getter(buffer.as_mut_ptr(), buffer.len() as i32);
        if len < 0 {
            return String::new();
        }
        if len as usize >= buffer.len() {
            buffer = vec![0; len as usize + 1];
            len = getter(buffer.as_mut_ptr(), buffer.len() as i32);
            if len < 0 {
                return String::new();
            }
        }
        let byte_slice = unsafe { std::slice::from_raw_parts(buffer.as_ptr() as *const u8, len as usize) };
        String::from_utf8_lossy(byte_slice).to_string()
    }

    /// Sets the text value of the control.
    /// No-op if the control has been destroyed.
    pub fn set_value(&self, value: &str) {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return;
        }
        let c_value = CString::new(value).unwrap_or_default();
        unsafe { ffi::wxd_TextCtrl_SetValue(ptr, c_value.as_ptr()) };
    }

    /// Gets the current text value of the control.
    /// Returns empty string if the control has been destroyed.
    pub fn get_value(&self) -> String {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return String::new();
        }
        unsafe { Self::read_string_with_retry(|buf, len| ffi::wxd_TextCtrl_GetValue(ptr, buf, len)) }
    }

    /// Appends text to the end of the control.
    /// No-op if the control has been destroyed.
    pub fn append_text(&self, text: &str) {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return;
        }
        let c_text = CString::new(text).unwrap_or_default();
        unsafe { ffi::wxd_TextCtrl_AppendText(ptr, c_text.as_ptr()) };
    }

    /// Clears the text in the control.
    /// No-op if the control has been destroyed.
    pub fn clear(&self) {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe {
            ffi::wxd_TextCtrl_Clear(ptr);
        }
    }

    /// Returns whether the text control has been modified by the user since the last
    /// time MarkDirty() or DiscardEdits() was called.
    /// Returns false if the control has been destroyed.
    pub fn is_modified(&self) -> bool {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_TextCtrl_IsModified(ptr) }
    }

    /// Marks the control as modified or unmodified.
    /// No-op if the control has been destroyed.
    pub fn set_modified(&self, modified: bool) {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TextCtrl_SetModified(ptr, modified) };
    }

    /// Makes the text control editable or read-only, overriding the style setting.
    /// No-op if the control has been destroyed.
    pub fn set_editable(&self, editable: bool) {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TextCtrl_SetEditable(ptr, editable) };
    }

    /// Returns true if the control is editable.
    /// Returns false if the control has been destroyed.
    pub fn is_editable(&self) -> bool {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_TextCtrl_IsEditable(ptr) }
    }

    /// Gets the insertion point of the control.
    /// The insertion point is the position at which the caret is currently positioned.
    /// Returns 0 if the control has been destroyed.
    pub fn get_insertion_point(&self) -> i64 {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return 0;
        }
        unsafe { ffi::wxd_TextCtrl_GetInsertionPoint(ptr) }
    }

    /// Sets the insertion point of the control.
    /// No-op if the control has been destroyed.
    pub fn set_insertion_point(&self, pos: i64) {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TextCtrl_SetInsertionPoint(ptr, pos) };
    }

    /// Sets the maximum number of characters that may be entered in the control.
    ///
    /// If `len` is 0, the maximum length limit is removed.
    /// No-op if the control has been destroyed.
    pub fn set_max_length(&self, len: usize) {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TextCtrl_SetMaxLength(ptr, len as i64) };
    }

    /// Returns the last position in the control.
    /// Returns 0 if the control has been destroyed.
    pub fn get_last_position(&self) -> i64 {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return 0;
        }
        unsafe { ffi::wxd_TextCtrl_GetLastPosition(ptr) }
    }

    /// Returns true if this is a multi-line text control.
    /// Returns false if the control has been destroyed.
    pub fn is_multiline(&self) -> bool {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_TextCtrl_IsMultiLine(ptr) }
    }

    /// Returns true if this is a single-line text control.
    /// Returns false if the control has been destroyed.
    pub fn is_single_line(&self) -> bool {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_TextCtrl_IsSingleLine(ptr) }
    }

    // --- Selection Operations ---

    /// Sets the selection in the text control.
    ///
    /// # Arguments
    /// * `from` - The start position of the selection
    /// * `to` - The end position of the selection
    ///
    /// No-op if the control has been destroyed.
    pub fn set_selection(&self, from: i64, to: i64) {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TextCtrl_SetSelection(ptr, from, to) };
    }

    /// Gets the current selection range.
    ///
    /// Returns a tuple (from, to) representing the selection range.
    /// If there's no selection, both values will be equal to the insertion point.
    /// Returns (0, 0) if the control has been destroyed.
    pub fn get_selection(&self) -> (i64, i64) {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return (0, 0);
        }
        let mut from = 0i64;
        let mut to = 0i64;
        unsafe { ffi::wxd_TextCtrl_GetSelection(ptr, &mut from, &mut to) };
        (from, to)
    }

    /// Selects all text in the control.
    /// No-op if the control has been destroyed.
    pub fn select_all(&self) {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TextCtrl_SelectAll(ptr) };
    }

    /// Gets the currently selected text.
    ///
    /// Returns an empty string if no text is selected or if the control has been destroyed.
    pub fn get_string_selection(&self) -> String {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return String::new();
        }
        unsafe { Self::read_string_with_retry(|buf, len| ffi::wxd_TextCtrl_GetStringSelection(ptr, buf, len)) }
    }

    /// Sets the insertion point to the end of the text control.
    /// No-op if the control has been destroyed.
    pub fn set_insertion_point_end(&self) {
        let ptr = self.textctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TextCtrl_SetInsertionPointEnd(ptr) };
    }

    /// Returns the underlying WindowHandle for this textctrl.
    pub fn window_handle(&self) -> WindowHandle {
        self.handle
    }
}

// Implement TextEvents trait for TextCtrl
impl TextEvents for TextCtrl {}

// Manual WxWidget implementation for TextCtrl (using WindowHandle)
impl WxWidget for TextCtrl {
    fn handle_ptr(&self) -> *mut ffi::wxd_Window_t {
        self.handle.get_ptr().unwrap_or(null_mut())
    }

    fn is_valid(&self) -> bool {
        self.handle.is_valid()
    }
}

// Implement WxEvtHandler for event binding
impl WxEvtHandler for TextCtrl {
    unsafe fn get_event_handler_ptr(&self) -> *mut ffi::wxd_EvtHandler_t {
        self.handle.get_ptr().unwrap_or(null_mut()) as *mut ffi::wxd_EvtHandler_t
    }
}

// Implement common event traits that all Window-based widgets support
impl crate::event::WindowEvents for TextCtrl {}

// Implement scrolling functionality for TextCtrl (useful for multiline text)
impl crate::scrollable::WxScrollable for TextCtrl {}

// Use the widget_builder macro for TextCtrl
widget_builder!(
    name: TextCtrl,
    parent_type: &'a dyn WxWidget,
    style_type: TextCtrlStyle,
    fields: {
        value: String = String::new()
    },
    build_impl: |slf| {
        TextCtrl::new_impl(
            slf.parent.handle_ptr(),
            slf.id,
            &slf.value,
            slf.pos,
            slf.size,
            slf.style.bits()
        )
    }
);

// Implement TextCtrl-specific event handlers using the standard macro
crate::implement_widget_local_event_handlers!(
    TextCtrl,
    TextCtrlEvent,
    TextCtrlEventData,
    TextChanged => text_changed, EventType::TEXT,
    TextEnter => text_enter, EventType::TEXT_ENTER
);

// XRC Support - enables TextCtrl to be created from XRC-managed pointers
#[cfg(feature = "xrc")]
impl crate::xrc::XrcSupport for TextCtrl {
    unsafe fn from_xrc_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
        TextCtrl {
            handle: WindowHandle::new(ptr),
        }
    }
}

// Widget casting support for TextCtrl
impl crate::window::FromWindowWithClassName for TextCtrl {
    fn class_name() -> &'static str {
        "wxTextCtrl"
    }

    unsafe fn from_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
        TextCtrl {
            handle: WindowHandle::new(ptr),
        }
    }
}