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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
//! wxTreeListCtrl wrapper
//!
//! The `TreeListCtrl` widget combines the functionality of a tree control with list control columns,
//! providing a powerful way to display hierarchical data with additional information in columns.
//! It supports checkboxes for easy selection/deselection of items.
//!
//! # Examples
//!
//! ```rust,no_run
//! use wxdragon::prelude::*;
//! use wxdragon::widgets::treelistctrl::{TreeListCtrl, TreeListCtrlStyle, CheckboxState, TreeListCtrlEventData};
//! use wxdragon::widgets::list_ctrl::ListColumnFormat;
//!
//! fn create_tree_list(parent: &dyn WxWidget) -> TreeListCtrl {
//!     // Create a tree list control with checkboxes
//!     let tree_list = TreeListCtrl::builder(parent)
//!         .with_style(TreeListCtrlStyle::Default | TreeListCtrlStyle::Checkbox)
//!         .build();
//!
//!     // Add columns
//!     tree_list.append_column("Name", 200, ListColumnFormat::Left);
//!     tree_list.append_column("Size", 100, ListColumnFormat::Right);
//!     tree_list.append_column("Modified", 150, ListColumnFormat::Left);
//!
//!     // Add root item
//!     let root = tree_list.append_item(&tree_list.get_root_item(), "Documents").unwrap();
//!     tree_list.set_item_text(&root, 1, "Folder");
//!     tree_list.set_item_text(&root, 2, "Today");
//!
//!     // Add child items with checkboxes
//!     let doc1 = tree_list.append_item(&root, "Report.pdf").unwrap();
//!     tree_list.set_item_text(&doc1, 1, "1.2 MB");
//!     tree_list.set_item_text(&doc1, 2, "Yesterday");
//!     tree_list.check_item(&doc1, CheckboxState::Checked);
//!
//!     // Set up event handlers using the generated methods
//!     tree_list.on_item_checked(|event: TreeListCtrlEventData| {
//!         if let Some(item) = event.get_item() {
//!             if let Some(is_checked) = event.is_checked() {
//!                 println!("Item {:?} was {}", item, if is_checked { "checked" } else { "unchecked" });
//!             }
//!         }
//!     });
//!
//!     tree_list.on_selection_changed(|event: TreeListCtrlEventData| {
//!         if let Some(item) = event.get_item() {
//!             println!("Selection changed to item {:?}", item);
//!         }
//!     });
//!
//!     tree_list.on_column_sorted(|event: TreeListCtrlEventData| {
//!         if let Some(column) = event.get_column() {
//!             println!("Column {} was clicked for sorting", column);
//!         }
//!     });
//!
//!     tree_list.on_item_checked(|event: TreeListCtrlEventData| {
//!         if let Some(item) = event.get_item() {
//!             if let Some(old_state) = event.get_old_checked_state() {
//!                 if let Some(is_checked) = event.is_checked() {
//!                     println!("Item {:?} changed from {:?} to {}",
//!                         item, old_state, if is_checked { "checked" } else { "unchecked" });
//!                 }
//!             }
//!         }
//!     });
//!
//!     tree_list
//! }
//! ```

use std::ffi::CString;
use std::os::raw::c_char;

use crate::event::{Event, EventType, WxEvtHandler};
use crate::geometry::{Point, Size};
use crate::id::Id;
use crate::widgets::list_ctrl::ListColumnFormat;
use crate::window::{WindowHandle, WxWidget};
// Window is used for backwards compatibility
#[allow(unused_imports)]
use crate::window::Window;
use wxdragon_sys as ffi;

// --- TreeListCtrl Styles ---
widget_style_enum!(
    name: TreeListCtrlStyle,
    doc: "Style flags for TreeListCtrl widget.",
    variants: {
        Default: 0, "Default style.",
        Checkbox: 1, "Show checkboxes next to items.",
        Three_State: 2, "Use 3-state checkboxes (checked, unchecked, undetermined)."
    },
    default_variant: Default
);

/// Checkbox state for tree list items
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CheckboxState {
    Unchecked = 0,
    Checked = 1,
    Undetermined = 2,
}

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

impl From<i32> for CheckboxState {
    fn from(val: i32) -> Self {
        match val {
            0 => CheckboxState::Unchecked,
            1 => CheckboxState::Checked,
            2 => CheckboxState::Undetermined,
            _ => CheckboxState::Unchecked,
        }
    }
}

/// Represents a tree item ID in the TreeListCtrl
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TreeListItem {
    id: i64,
}

impl TreeListItem {
    pub fn new(id: i64) -> Self {
        Self { id }
    }

    pub fn id(&self) -> i64 {
        self.id
    }

    pub fn is_valid(&self) -> bool {
        self.id != 0
    }
}

/// Events emitted by TreeListCtrl
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TreeListCtrlEvent {
    /// Emitted when the selection changes
    SelectionChanged,
    /// Emitted when an item checkbox is checked/unchecked
    ItemChecked,
    /// Emitted when an item is activated (double-clicked)
    ItemActivated,
    /// Emitted when a column header is clicked for sorting
    ColumnSorted,
    /// Emitted when an item is expanding
    ItemExpanding,
    /// Emitted when an item has expanded
    ItemExpanded,
}

/// Event data for TreeListCtrl events
#[derive(Debug)]
pub struct TreeListCtrlEventData {
    event: Event,
}

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

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

    /// Get the TreeListItem of the affected item
    pub fn get_item(&self) -> Option<TreeListItem> {
        if self.event.is_null() {
            return None;
        }
        let id = unsafe { ffi::wxd_TreeListEvent_GetItem(self.event._as_ptr()) };
        if id != 0 { Some(TreeListItem::new(id)) } else { None }
    }

    /// Get the column index for column-related events
    pub fn get_column(&self) -> Option<i32> {
        if self.event.is_null() {
            return None;
        }
        let col = unsafe { ffi::wxd_TreeListEvent_GetColumn(self.event._as_ptr()) };
        if col >= 0 { Some(col) } else { None }
    }

    /// Get the label text for label edit events (fallback to generic event string)
    pub fn get_string(&self) -> Option<String> {
        self.event.get_string()
    }

    /// Get the previous checkbox state for ItemChecked events
    pub fn get_old_checked_state(&self) -> Option<CheckboxState> {
        if self.event.is_null() {
            return None;
        }
        let state = unsafe { ffi::wxd_TreeListEvent_GetOldCheckedState(self.event._as_ptr()) };
        if state >= 0 { Some(CheckboxState::from(state)) } else { None }
    }

    /// Get the checkbox state for ItemChecked events
    pub fn is_checked(&self) -> Option<bool> {
        self.event.is_checked()
    }

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

/// Represents a wxTreeListCtrl widget.
///
/// TreeListCtrl 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.
///
/// TreeListCtrl combines tree functionality with list columns, allowing hierarchical data
/// to be displayed with additional information in columns. It supports checkboxes for
/// easy selection/deselection of items.
#[derive(Clone, Copy)]
pub struct TreeListCtrl {
    /// Safe handle to the underlying wxTreeListCtrl - automatically invalidated on destroy
    handle: WindowHandle,
}

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

    /// Internal implementation used by the builder.
    fn new_impl(parent_ptr: *mut ffi::wxd_Window_t, id: Id, pos: Point, size: Size, style: i64) -> Self {
        let ptr = unsafe { ffi::wxd_TreeListCtrl_Create(parent_ptr, id, pos.into(), size.into(), style as ffi::wxd_Style_t) };

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

        TreeListCtrl {
            handle: WindowHandle::new(ptr as *mut ffi::wxd_Window_t),
        }
    }

    /// Helper to get raw tree list ctrl pointer, returns null if widget has been destroyed
    #[inline]
    fn tree_list_ctrl_ptr(&self) -> *mut ffi::wxd_TreeListCtrl_t {
        self.handle
            .get_ptr()
            .map(|p| p as *mut ffi::wxd_TreeListCtrl_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()
    }

    // --- Column Management ---

    /// Appends a new column to the control.
    /// Returns -1 if the control has been destroyed.
    ///
    /// # Arguments
    /// * `text` - The column header text
    /// * `width` - The column width in pixels
    /// * `align` - The column alignment
    ///
    /// Returns the column index.
    pub fn append_column(&self, text: &str, width: i32, align: ListColumnFormat) -> i32 {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return -1;
        }
        let c_text = CString::new(text).unwrap_or_default();
        unsafe { ffi::wxd_TreeListCtrl_AppendColumn(ptr, c_text.as_ptr(), width, align.as_i32()) }
    }

    /// Gets the number of columns in the control.
    /// Returns 0 if the control has been destroyed.
    pub fn get_column_count(&self) -> i32 {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return 0;
        }
        unsafe { ffi::wxd_TreeListCtrl_GetColumnCount(ptr) }
    }

    /// Sets the width of the specified column.
    /// No-op if the control has been destroyed.
    pub fn set_column_width(&self, col: i32, width: i32) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_SetColumnWidth(ptr, col, width) };
    }

    /// Gets the width of the specified column.
    /// Returns 0 if the control has been destroyed.
    pub fn get_column_width(&self, col: i32) -> i32 {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return 0;
        }
        unsafe { ffi::wxd_TreeListCtrl_GetColumnWidth(ptr, col) }
    }

    /// Deletes the column with the given index.
    /// Returns false if the control has been destroyed.
    pub fn delete_column(&self, col: u32) -> bool {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_TreeListCtrl_DeleteColumn(ptr, col) }
    }

    /// Deletes all columns.
    /// No-op if the control has been destroyed.
    pub fn clear_columns(&self) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_ClearColumns(ptr) };
    }

    /// Gets the width appropriate for showing the given text.
    /// Returns 0 if the control has been destroyed.
    pub fn width_for(&self, text: &str) -> i32 {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return 0;
        }
        let c_text = CString::new(text).unwrap_or_default();
        unsafe { ffi::wxd_TreeListCtrl_WidthFor(ptr, c_text.as_ptr()) }
    }

    // --- Item Management ---

    /// Gets the root item of the tree.
    /// Returns an invalid item if the control has been destroyed.
    pub fn get_root_item(&self) -> TreeListItem {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return TreeListItem::new(0);
        }
        let id = unsafe { ffi::wxd_TreeListCtrl_GetRootItem(ptr) };
        TreeListItem::new(id)
    }

    /// Appends a new item to the tree.
    ///
    /// # Arguments
    /// * `parent` - The parent item
    /// * `text` - The item text
    ///
    /// Returns the new item, or None if the operation failed or control has been destroyed.
    pub fn append_item(&self, parent: &TreeListItem, text: &str) -> Option<TreeListItem> {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return None;
        }
        let c_text = CString::new(text).unwrap_or_default();
        let id = unsafe { ffi::wxd_TreeListCtrl_AppendItem(ptr, parent.id(), c_text.as_ptr()) };
        if id != 0 { Some(TreeListItem::new(id)) } else { None }
    }

    /// Inserts a new item into the tree.
    ///
    /// # Arguments
    /// * `parent` - The parent item
    /// * `previous` - The item after which to insert the new item
    /// * `text` - The item text
    ///
    /// Returns the new item, or None if the operation failed or control has been destroyed.
    pub fn insert_item(&self, parent: &TreeListItem, previous: &TreeListItem, text: &str) -> Option<TreeListItem> {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return None;
        }
        let c_text = CString::new(text).unwrap_or_default();
        let id = unsafe { ffi::wxd_TreeListCtrl_InsertItem(ptr, parent.id(), previous.id(), c_text.as_ptr()) };
        if id != 0 { Some(TreeListItem::new(id)) } else { None }
    }

    /// Prepends a new item to the tree (inserts at the beginning).
    ///
    /// # Arguments
    /// * `parent` - The parent item
    /// * `text` - The item text
    ///
    /// Returns the new item, or None if the operation failed or control has been destroyed.
    pub fn prepend_item(&self, parent: &TreeListItem, text: &str) -> Option<TreeListItem> {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return None;
        }
        let c_text = CString::new(text).unwrap_or_default();
        let id = unsafe { ffi::wxd_TreeListCtrl_PrependItem(ptr, parent.id(), c_text.as_ptr()) };
        if id != 0 { Some(TreeListItem::new(id)) } else { None }
    }

    /// Deletes the specified item.
    /// No-op if the control has been destroyed.
    pub fn delete_item(&self, item: &TreeListItem) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_DeleteItem(ptr, item.id()) };
    }

    /// Deletes all items in the tree.
    /// No-op if the control has been destroyed.
    pub fn delete_all_items(&self) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_DeleteAllItems(ptr) };
    }

    /// Sets the text for the specified item and column.
    /// No-op if the control has been destroyed.
    pub fn set_item_text(&self, item: &TreeListItem, col: i32, text: &str) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        let c_text = CString::new(text).unwrap_or_default();
        unsafe { ffi::wxd_TreeListCtrl_SetItemText(ptr, item.id(), col, c_text.as_ptr()) };
    }

    /// Gets the text for the specified item and column.
    /// Returns empty string if the control has been destroyed.
    pub fn get_item_text(&self, item: &TreeListItem, col: i32) -> String {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return String::new();
        }
        unsafe { Self::read_string_with_retry(|buf, len| ffi::wxd_TreeListCtrl_GetItemText(ptr, item.id(), col, buf, len)) }
    }

    /// Expands the specified item.
    /// No-op if the control has been destroyed.
    pub fn expand(&self, item: &TreeListItem) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_Expand(ptr, item.id()) };
    }

    /// Collapses the specified item.
    /// No-op if the control has been destroyed.
    pub fn collapse(&self, item: &TreeListItem) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_Collapse(ptr, item.id()) };
    }

    /// Checks if the specified item is expanded.
    /// Returns false if the control has been destroyed.
    pub fn is_expanded(&self, item: &TreeListItem) -> bool {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_TreeListCtrl_IsExpanded(ptr, item.id()) }
    }

    // --- Selection Management ---

    /// Gets the currently selected item.
    /// Returns None if no item is selected or control has been destroyed.
    pub fn get_selection(&self) -> Option<TreeListItem> {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return None;
        }
        let id = unsafe { ffi::wxd_TreeListCtrl_GetSelection(ptr) };
        if id != 0 { Some(TreeListItem::new(id)) } else { None }
    }

    /// Selects the specified item.
    /// No-op if the control has been destroyed.
    pub fn select_item(&self, item: &TreeListItem) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_SelectItem(ptr, item.id()) };
    }

    /// Unselects all items.
    /// No-op if the control has been destroyed.
    pub fn unselect_all(&self) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_UnselectAll(ptr) };
    }

    // --- Checkbox Management ---

    /// Checks or unchecks the specified item.
    /// No-op if the control has been destroyed.
    pub fn check_item(&self, item: &TreeListItem, state: CheckboxState) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_CheckItem(ptr, item.id(), state.into()) };
    }

    /// Gets the checkbox state of the specified item.
    /// Returns Unchecked if the control has been destroyed.
    pub fn get_checked_state(&self, item: &TreeListItem) -> CheckboxState {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return CheckboxState::Unchecked;
        }
        let state = unsafe { ffi::wxd_TreeListCtrl_GetCheckedState(ptr, item.id()) };
        CheckboxState::from(state)
    }

    /// Checks if the specified item is checked.
    /// Returns false if the control has been destroyed.
    pub fn is_checked(&self, item: &TreeListItem) -> bool {
        self.get_checked_state(item) == CheckboxState::Checked
    }

    /// Checks all items recursively starting from the specified item.
    /// No-op if the control has been destroyed.
    pub fn check_item_recursively(&self, item: &TreeListItem, state: CheckboxState) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_CheckItemRecursively(ptr, item.id(), state.into()) };
    }

    /// Updates the parent's checkbox state based on children (for 3-state checkboxes).
    /// No-op if the control has been destroyed.
    pub fn update_item_parent_state(&self, item: &TreeListItem) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_UpdateItemParentState(ptr, item.id()) };
    }

    // --- Tree Navigation ---

    /// Gets the parent of the specified item.
    /// Returns None if the control has been destroyed.
    pub fn get_item_parent(&self, item: &TreeListItem) -> Option<TreeListItem> {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return None;
        }
        let id = unsafe { ffi::wxd_TreeListCtrl_GetItemParent(ptr, item.id()) };
        if id != 0 { Some(TreeListItem::new(id)) } else { None }
    }

    /// Gets the first child of the specified item.
    /// Returns None if the control has been destroyed.
    pub fn get_first_child(&self, item: &TreeListItem) -> Option<TreeListItem> {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return None;
        }
        let id = unsafe { ffi::wxd_TreeListCtrl_GetFirstChild(ptr, item.id()) };
        if id != 0 { Some(TreeListItem::new(id)) } else { None }
    }

    /// Gets the next sibling of the specified item.
    /// Returns None if the control has been destroyed.
    pub fn get_next_sibling(&self, item: &TreeListItem) -> Option<TreeListItem> {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return None;
        }
        let id = unsafe { ffi::wxd_TreeListCtrl_GetNextSibling(ptr, item.id()) };
        if id != 0 { Some(TreeListItem::new(id)) } else { None }
    }

    /// Gets the next item in depth-first tree traversal order.
    /// Returns None if the control has been destroyed.
    pub fn get_next_item(&self, item: &TreeListItem) -> Option<TreeListItem> {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return None;
        }
        let id = unsafe { ffi::wxd_TreeListCtrl_GetNextItem(ptr, item.id()) };
        if id != 0 { Some(TreeListItem::new(id)) } else { None }
    }

    /// Gets the first item in the tree (first child of root).
    /// Returns None if the control has been destroyed.
    pub fn get_first_item(&self) -> Option<TreeListItem> {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return None;
        }
        let id = unsafe { ffi::wxd_TreeListCtrl_GetFirstItem(ptr) };
        if id != 0 { Some(TreeListItem::new(id)) } else { None }
    }

    // --- Item Attributes ---

    /// Sets the image for the specified item.
    /// No-op if the control has been destroyed.
    pub fn set_item_image(&self, item: &TreeListItem, closed: i32, opened: i32) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_SetItemImage(ptr, item.id(), closed, opened) };
    }

    // --- Multi-Selection Support ---

    /// Gets all selected items. Returns a vector of selected items.
    /// Returns empty vector if the control has been destroyed.
    pub fn get_selections(&self) -> Vec<TreeListItem> {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return Vec::new();
        }
        const MAX_SELECTIONS: usize = 1000;
        let mut selections: Vec<i64> = vec![0; MAX_SELECTIONS];
        let count = unsafe { ffi::wxd_TreeListCtrl_GetSelections(ptr, selections.as_mut_ptr(), MAX_SELECTIONS as u32) };

        selections.truncate(count as usize);
        selections.into_iter().map(TreeListItem::new).collect()
    }

    /// Selects the specified item.
    /// No-op if the control has been destroyed.
    pub fn select(&self, item: &TreeListItem) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_Select(ptr, item.id()) };
    }

    /// Unselects the specified item.
    /// No-op if the control has been destroyed.
    pub fn unselect(&self, item: &TreeListItem) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_Unselect(ptr, item.id()) };
    }

    /// Checks if the specified item is selected.
    /// Returns false if the control has been destroyed.
    pub fn is_selected(&self, item: &TreeListItem) -> bool {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_TreeListCtrl_IsSelected(ptr, item.id()) }
    }

    /// Selects all items (only valid in multiple selection mode).
    /// No-op if the control has been destroyed.
    pub fn select_all(&self) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_SelectAll(ptr) };
    }

    /// Ensures the specified item is visible.
    /// No-op if the control has been destroyed.
    pub fn ensure_visible(&self, item: &TreeListItem) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_EnsureVisible(ptr, item.id()) };
    }

    // --- Additional Checkbox Methods ---

    /// Unchecks the specified item.
    /// No-op if the control has been destroyed.
    pub fn uncheck_item(&self, item: &TreeListItem) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_UncheckItem(ptr, item.id()) };
    }

    /// Checks if all children of the specified item are in the given state.
    /// Returns false if the control has been destroyed.
    pub fn are_all_children_in_state(&self, item: &TreeListItem, state: CheckboxState) -> bool {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_TreeListCtrl_AreAllChildrenInState(ptr, item.id(), state as i32) }
    }

    // --- Sorting ---

    /// Sets the column to sort by.
    /// No-op if the control has been destroyed.
    pub fn set_sort_column(&self, col: u32, ascending: bool) {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_TreeListCtrl_SetSortColumn(ptr, col, ascending) };
    }

    /// Gets the current sort column and order.
    /// Returns None if the control has been destroyed.
    pub fn get_sort_column(&self) -> Option<(u32, bool)> {
        let ptr = self.tree_list_ctrl_ptr();
        if ptr.is_null() {
            return None;
        }
        let mut col: u32 = 0;
        let mut ascending: bool = true;
        let has_sort = unsafe { ffi::wxd_TreeListCtrl_GetSortColumn(ptr, &mut col, &mut ascending) };
        if has_sort { Some((col, ascending)) } else { None }
    }

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

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

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

// Implement event handlers for TreeListCtrl
crate::implement_widget_local_event_handlers!(
    TreeListCtrl,
    TreeListCtrlEvent,
    TreeListCtrlEventData,
    SelectionChanged => selection_changed, EventType::TREELIST_SELECTION_CHANGED,
    ItemChecked => item_checked, EventType::TREELIST_ITEM_CHECKED,
    ItemActivated => item_activated, EventType::TREELIST_ITEM_ACTIVATED,
    ColumnSorted => column_sorted, EventType::TREELIST_COLUMN_SORTED,
    ItemExpanding => item_expanding, EventType::TREELIST_ITEM_EXPANDING,
    ItemExpanded => item_expanded, EventType::TREELIST_ITEM_EXPANDED
);

// Implement standard window events trait

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

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

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