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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
//!
//! Safe wrapper for wxRichTextCtrl.

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 impl_xrc_support for backwards compatibility
#[allow(unused_imports)]
use crate::window::Window;
use std::ffi::CString;
use std::os::raw::c_char;
use wxdragon_sys as ffi;

// --- Rich Text Control Styles ---
widget_style_enum!(
    name: RichTextCtrlStyle,
    doc: "Style flags for RichTextCtrl widget.",
    variants: {
        Default: 0, "Default style.",
        ReadOnly: ffi::WXD_TE_READONLY, "Read-only rich text control.",
        MultiLine: ffi::WXD_TE_MULTILINE, "Multi-line rich text control.",
        NoVScroll: ffi::WXD_TE_NO_VSCROLL, "No vertical scrollbar.",
        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.",
        WordWrap: ffi::WXD_TE_WORDWRAP, "Wrap at word boundaries.",
        CharWrap: ffi::WXD_TE_CHARWRAP, "Wrap at any position, splitting words if necessary.",
        DontWrap: ffi::WXD_TE_DONTWRAP, "Don't wrap at all, show horizontal scrollbar instead."
    },
    default_variant: Default
);

/// File types for loading and saving rich text documents
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RichTextFileType {
    /// Any file type (let wxWidgets determine)
    Any = 0,
    /// Plain text file
    Text = 1,
    /// XML format
    Xml = 2,
    /// HTML format
    Html = 3,
    /// RTF format
    Rtf = 4,
    /// PDF format (if supported)
    Pdf = 5,
}

impl From<RichTextFileType> for i32 {
    fn from(val: RichTextFileType) -> Self {
        val as i32
    }
}

/// Events emitted by RichTextCtrl
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RichTextCtrlEvent {
    /// Text content has changed
    TextChanged,
    /// Enter key was pressed
    TextEnter,
    /// Left mouse click
    LeftClick,
    /// Right mouse click
    RightClick,
    /// Middle mouse click
    MiddleClick,
    /// Left mouse double-click
    LeftDoubleClick,
    /// Return key pressed
    Return,
    /// Character input
    Character,
    /// Delete operation
    Delete,
    /// Content was inserted
    ContentInserted,
    /// Content was deleted
    ContentDeleted,
    /// Style changed
    StyleChanged,
    /// Selection changed
    SelectionChanged,
}

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

impl RichTextCtrlEventData {
    /// Create a new RichTextCtrlEventData 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()
    }

    /// Get the position for position-related events
    pub fn get_position(&self) -> Option<i32> {
        self.event.get_int()
    }
}

/// Represents a wxRichTextCtrl widget.
///
/// RichTextCtrl is a rich text editor that supports formatted text with different fonts,
/// colors, styles, and other formatting options. It provides a comprehensive set of
/// editing and formatting capabilities.
///
/// RichTextCtrl 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 rtc = RichTextCtrl::builder(&frame).build();
///
/// // RichTextCtrl is Copy - no clone needed for closures!
/// rtc.bind_text_changed(move |_| {
///     // Safe: if rtc was destroyed, this is a no-op
///     rtc.append_text("Changed!");
/// });
///
/// // After parent destruction, rtc operations are safe no-ops
/// frame.destroy();
/// assert!(!rtc.is_valid());
/// ```
#[derive(Clone, Copy)]
pub struct RichTextCtrl {
    /// Safe handle to the underlying wxRichTextCtrl - automatically invalidated on destroy
    handle: WindowHandle,
}

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

    /// Creates a new RichTextCtrl from a raw pointer.
    /// This is intended for internal use by other widget wrappers.
    #[allow(dead_code)]
    pub(crate) fn from_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
        Self {
            handle: WindowHandle::new(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 {
        assert!(!parent_ptr.is_null(), "RichTextCtrl requires a parent");
        let c_value = CString::new(value).unwrap_or_default();

        let ptr = unsafe {
            ffi::wxd_RichTextCtrl_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 RichTextCtrl widget");
        }

        // Create a WindowHandle which automatically registers for destroy events
        RichTextCtrl {
            handle: WindowHandle::new(ptr as *mut ffi::wxd_Window_t),
        }
    }

    /// Helper to get raw richtextctrl pointer, returns null if widget has been destroyed
    #[inline]
    fn richtextctrl_ptr(&self) -> *mut ffi::wxd_RichTextCtrl_t {
        self.handle
            .get_ptr()
            .map(|p| p as *mut ffi::wxd_RichTextCtrl_t)
            .unwrap_or(std::ptr::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()
    }

    // --- Text Content Operations ---

    /// 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.richtextctrl_ptr();
        if ptr.is_null() {
            return;
        }
        let c_value = CString::new(value).unwrap_or_default();
        unsafe { ffi::wxd_RichTextCtrl_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.richtextctrl_ptr();
        if ptr.is_null() {
            return String::new();
        }
        unsafe { Self::read_string_with_retry(|buf, len| ffi::wxd_RichTextCtrl_GetValue(ptr, buf, len)) }
    }

    /// Writes text at the current insertion point.
    /// No-op if the control has been destroyed.
    pub fn write_text(&self, text: &str) {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return;
        }
        let c_text = CString::new(text).unwrap_or_default();
        unsafe { ffi::wxd_RichTextCtrl_WriteText(ptr, c_text.as_ptr()) };
    }

    /// 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.richtextctrl_ptr();
        if ptr.is_null() {
            return;
        }
        let c_text = CString::new(text).unwrap_or_default();
        unsafe { ffi::wxd_RichTextCtrl_AppendText(ptr, c_text.as_ptr()) };
    }

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

    /// Returns the length of the text.
    /// Returns 0 if the control has been destroyed.
    pub fn get_length(&self) -> i32 {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return 0;
        }
        unsafe { ffi::wxd_RichTextCtrl_GetLength(ptr) }
    }

    // --- Text Range Operations ---

    /// Gets text in the specified range.
    /// Returns empty string if the control has been destroyed.
    pub fn get_range(&self, from: i64, to: i64) -> String {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return String::new();
        }
        unsafe { Self::read_string_with_retry(|buf, len| ffi::wxd_RichTextCtrl_GetRange(ptr, from, to, buf, len)) }
    }

    /// Sets the selection range.
    /// No-op if the control has been destroyed.
    pub fn set_selection(&self, from: i64, to: i64) {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_RichTextCtrl_SetSelection(ptr, from, to) };
    }

    /// Gets the current selection range.
    /// Returns (0, 0) if the control has been destroyed.
    pub fn get_selection(&self) -> (i64, i64) {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return (0, 0);
        }
        let mut from = 0i64;
        let mut to = 0i64;
        unsafe { ffi::wxd_RichTextCtrl_GetSelection(ptr, &mut from, &mut to) };
        (from, to)
    }

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

    // --- Editing Operations ---

    /// Cuts the selected text to the clipboard.
    /// No-op if the control has been destroyed.
    pub fn cut(&self) {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_RichTextCtrl_Cut(ptr) };
    }

    /// Copies the selected text to the clipboard.
    /// No-op if the control has been destroyed.
    pub fn copy(&self) {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_RichTextCtrl_Copy(ptr) };
    }

    /// Pastes text from the clipboard.
    /// No-op if the control has been destroyed.
    pub fn paste(&self) {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_RichTextCtrl_Paste(ptr) };
    }

    /// Undoes the last operation.
    /// No-op if the control has been destroyed.
    pub fn undo(&self) {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_RichTextCtrl_Undo(ptr) };
    }

    /// Redoes the last undone operation.
    /// No-op if the control has been destroyed.
    pub fn redo(&self) {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_RichTextCtrl_Redo(ptr) };
    }

    /// Returns true if undo is available.
    /// Returns false if the control has been destroyed.
    pub fn can_undo(&self) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_CanUndo(ptr) }
    }

    /// Returns true if redo is available.
    /// Returns false if the control has been destroyed.
    pub fn can_redo(&self) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_CanRedo(ptr) }
    }

    // --- State Operations ---

    /// Makes the text control editable or read-only.
    /// No-op if the control has been destroyed.
    pub fn set_editable(&self, editable: bool) {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_RichTextCtrl_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.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_IsEditable(ptr) }
    }

    /// Returns true if the control has been modified.
    /// Returns false if the control has been destroyed.
    pub fn is_modified(&self) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_IsModified(ptr) }
    }

    /// Marks the control as dirty (modified).
    /// No-op if the control has been destroyed.
    pub fn mark_dirty(&self) {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_RichTextCtrl_MarkDirty(ptr) };
    }

    /// Discards any edits and marks the control as unmodified.
    /// No-op if the control has been destroyed.
    pub fn discard_edits(&self) {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_RichTextCtrl_DiscardEdits(ptr) };
    }

    // --- Position Operations ---

    /// Gets the insertion point of the control.
    /// Returns 0 if the control has been destroyed.
    pub fn get_insertion_point(&self) -> i64 {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return 0;
        }
        unsafe { ffi::wxd_RichTextCtrl_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.richtextctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_RichTextCtrl_SetInsertionPoint(ptr, pos) };
    }

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

    /// 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.richtextctrl_ptr();
        if ptr.is_null() {
            return 0;
        }
        unsafe { ffi::wxd_RichTextCtrl_GetLastPosition(ptr) }
    }

    // --- File Operations ---

    /// Loads a file into the control.
    /// Returns false if the control has been destroyed.
    pub fn load_file(&self, filename: &str, file_type: RichTextFileType) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        let c_filename = CString::new(filename).unwrap_or_default();
        unsafe { ffi::wxd_RichTextCtrl_LoadFile(ptr, c_filename.as_ptr(), file_type.into()) }
    }

    /// Saves the content to a file.
    /// Returns false if the control has been destroyed.
    pub fn save_file(&self, filename: &str, file_type: RichTextFileType) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        let c_filename = CString::new(filename).unwrap_or_default();
        unsafe { ffi::wxd_RichTextCtrl_SaveFile(ptr, c_filename.as_ptr(), file_type.into()) }
    }

    // --- Style Operations ---

    /// Sets style for a range of text.
    /// Returns false if the control has been destroyed.
    pub fn set_style_range(&self, start: i64, end: i64, bold: bool, italic: bool, underline: bool) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_SetStyleRange(ptr, start, end, bold, italic, underline) }
    }

    /// Applies bold formatting to the selection.
    /// Returns false if the control has been destroyed.
    pub fn apply_bold_to_selection(&self) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_ApplyBoldToSelection(ptr) }
    }

    /// Applies italic formatting to the selection.
    /// Returns false if the control has been destroyed.
    pub fn apply_italic_to_selection(&self) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_ApplyItalicToSelection(ptr) }
    }

    /// Applies underline formatting to the selection.
    /// Returns false if the control has been destroyed.
    pub fn apply_underline_to_selection(&self) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_ApplyUnderlineToSelection(ptr) }
    }

    /// Returns true if the selection is bold.
    /// Returns false if the control has been destroyed.
    pub fn is_selection_bold(&self) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_IsSelectionBold(ptr) }
    }

    /// Returns true if the selection is italic.
    /// Returns false if the control has been destroyed.
    pub fn is_selection_italic(&self) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_IsSelectionItalics(ptr) }
    }

    /// Returns true if the selection is underlined.
    /// Returns false if the control has been destroyed.
    pub fn is_selection_underlined(&self) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_IsSelectionUnderlined(ptr) }
    }

    // --- Font Operations ---

    /// Sets the font size for a range of text.
    /// Returns false if the control has been destroyed.
    pub fn set_font_size(&self, start: i64, end: i64, size: i32) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_SetFontSize(ptr, start, end, size) }
    }

    /// Sets the font size for the current selection.
    /// Returns false if the control has been destroyed.
    pub fn set_font_size_selection(&self, size: i32) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_SetFontSizeSelection(ptr, size) }
    }

    // --- Color Operations ---

    /// Sets text color for a range of text.
    /// Returns false if the control has been destroyed.
    pub fn set_text_color(&self, start: i64, end: i64, color: crate::Colour) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_SetTextColor(ptr, start, end, color.into()) }
    }

    /// Sets text color for the selection.
    /// Returns false if the control has been destroyed.
    pub fn set_text_color_selection(&self, color: crate::Colour) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_SetTextColorSelection(ptr, color.into()) }
    }

    /// Sets background color for a range of text.
    /// Returns false if the control has been destroyed.
    pub fn set_background_color(&self, start: i64, end: i64, color: crate::Colour) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_SetBackgroundColor(ptr, start, end, color.into()) }
    }

    /// Sets background color for the selection.
    /// Returns false if the control has been destroyed.
    pub fn set_background_color_selection(&self, color: crate::Colour) -> bool {
        let ptr = self.richtextctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_RichTextCtrl_SetBackgroundColorSelection(ptr, color.into()) }
    }

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

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

// Manual WxWidget implementation for RichTextCtrl (using WindowHandle)
impl WxWidget for RichTextCtrl {
    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()
    }
}

// Implement WxEvtHandler for event binding
impl WxEvtHandler for RichTextCtrl {
    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
    }
}

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

// Implement scrolling functionality for RichTextCtrl
impl crate::scrollable::WxScrollable for RichTextCtrl {}

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

// Implement RichTextCtrl-specific event handlers using the standard macro
crate::implement_widget_local_event_handlers!(
    RichTextCtrl,
    RichTextCtrlEvent,
    RichTextCtrlEventData,
    TextChanged => text_changed, EventType::TEXT,
    TextEnter => text_enter, EventType::TEXT_ENTER,
    LeftClick => left_click, EventType::RICHTEXT_LEFT_CLICK,
    RightClick => right_click, EventType::RICHTEXT_RIGHT_CLICK,
    MiddleClick => middle_click, EventType::RICHTEXT_MIDDLE_CLICK,
    LeftDoubleClick => left_double_click, EventType::RICHTEXT_LEFT_DCLICK,
    Return => return_key, EventType::RICHTEXT_RETURN,
    Character => character, EventType::RICHTEXT_CHARACTER,
    Delete => delete, EventType::RICHTEXT_DELETE,
    ContentInserted => content_inserted, EventType::RICHTEXT_CONTENT_INSERTED,
    ContentDeleted => content_deleted, EventType::RICHTEXT_CONTENT_DELETED,
    StyleChanged => style_changed, EventType::RICHTEXT_STYLE_CHANGED,
    SelectionChanged => selection_changed, EventType::RICHTEXT_SELECTION_CHANGED
);

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

// Enable widget casting for RichTextCtrl
impl crate::window::FromWindowWithClassName for RichTextCtrl {
    fn class_name() -> &'static str {
        "wxRichTextCtrl"
    }

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