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
//! Common event types for DataView widgets.
//!
//! This module defines event types and data structures shared by
//! DataViewCtrl, DataViewListCtrl, and DataViewTreeCtrl.
use super::item::DataViewItem;
use crate::event::{Event, EventToken, EventType, WxEvtHandler};
use wxdragon_sys as ffi;
/// Events emitted by DataView widgets
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DataViewEventType {
/// Emitted when an item is selected
SelectionChanged,
/// Emitted when an item is activated (e.g., double-clicked)
ItemActivated,
/// Emitted when an item editing begins
ItemEditingStarted,
/// Emitted when an item editing ends successfully
ItemEditingDone,
/// Emitted when an item editing is canceled
///
/// This uses the same underlying wxWidgets event as ItemEditingDone.
/// To check if editing was canceled in your handler, use:
/// ```rust,no_run
/// # use wxdragon::prelude::*;
/// # let data_view: DataViewCtrl = todo!();
/// data_view.on_item_editing_cancelled(|event| {
/// if event.is_edit_cancelled() {
/// // Handle cancellation
/// }
/// });
/// ```
ItemEditingCancelled,
/// Emitted when an item is expanded (tree views only)
ItemExpanded,
/// Emitted when an item is collapsed (tree views only)
ItemCollapsed,
/// Emitted when a column header is clicked
ColumnHeaderClick,
/// Emitted when a column header is right-clicked
ColumnHeaderRightClick,
/// Emitted before item expansion (tree views only)
ItemExpanding,
/// Emitted before item collapse (tree views only)
ItemCollapsing,
/// Emitted when a column is sorted
ColumnSorted,
/// Emitted when a column is reordered
ColumnReordered,
/// Emitted when a context menu is requested on an item
///
/// This event provides the item and column information directly.
/// Use this instead of the generic `on_context_menu` from MenuEvents trait
/// for better DataView-specific context information.
ItemContextMenu,
}
/// Event data for a DataView event
#[derive(Debug)]
pub struct DataViewEvent {
/// The underlying event
pub event: Event,
/// The type of event
pub event_type: DataViewEventType,
}
impl DataViewEvent {
/// Create a new DataViewEvent from a generic Event
pub fn new(event: Event, event_type: DataViewEventType) -> Self {
Self { event, event_type }
}
/// Get the mouse position in window coordinates
pub fn get_position(&self) -> Option<crate::Point> {
if self.event.is_null() {
return None;
}
// Prefer DataViewEventType-specific position, which is provided by wxWidgets for
// certain DataView events (e.g., context menu events). For other events,
// wxWidgets returns wxDefaultPosition (-1, -1).
let p = unsafe { ffi::wxd_DataViewEvent_GetPosition(self.event.0) };
if p.x == -1 && p.y == -1 {
None
} else {
Some(crate::Point { x: p.x, y: p.y })
}
}
/// 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 row that was affected by this event
pub fn get_row(&self) -> Option<i64> {
if self.event.is_null() {
return None;
}
let mut row: i64 = 0;
if unsafe { ffi::wxd_DataViewEvent_GetRow(self.event.0, &mut row) } {
Some(row)
} else {
None
}
}
/// Get the item that was affected by this event (for tree views)
pub fn get_item(&self) -> Option<DataViewItem> {
if self.event.is_null() {
return None;
}
unsafe {
let item_ptr = ffi::wxd_DataViewEvent_GetItem(self.event.0);
if item_ptr.is_null() {
None
} else {
// The C++ function returns a newly-allocated wrapper pointer that Rust takes ownership of
Some(DataViewItem::from(item_ptr))
}
}
}
/// Get the column index involved in this event
pub fn get_column(&self) -> Option<i32> {
if self.event.is_null() {
return None;
}
let mut column: i32 = 0;
if unsafe { ffi::wxd_DataViewEvent_GetColumn(self.event.0, &mut column) } {
Some(column)
} else {
None
}
}
/// Get the model column involved in this event
pub fn get_model_column(&self) -> Option<i32> {
self.event.get_int()
}
/// Get whether editing was cancelled for editing events
pub fn is_edit_cancelled(&self) -> bool {
if self.event.is_null() {
return false;
}
unsafe { ffi::wxd_DataViewEvent_IsEditCancelled(self.event.0) }
}
/// Get the value for editing events
pub fn get_value(&self) -> Option<super::Variant> {
if self.event.is_null() {
return None;
}
let p = unsafe { ffi::wxd_DataViewEvent_GetValue(self.event.0) };
if p.is_null() {
return None;
}
// Wrap the returned pointer in a Variant; Rust takes ownership
Some(super::Variant::from(p))
}
/// Set the value for editing events
pub fn set_value(&self, value: &super::Variant) -> bool {
if self.event.is_null() {
return false;
}
unsafe { ffi::wxd_DataViewEvent_SetValue(self.event.0, value.as_const_ptr()) }
}
/// Returns whether the sort order is ascending for column-sorted events.
///
/// This method is only meaningful for [`DataViewEventType::ColumnSorted`] events
/// (i.e., when handling `wxEVT_DATAVIEW_COLUMN_SORTED`). For all other event types,
/// this method will return `None`.
///
/// # Example
/// Typical usage within an `on_column_sorted` handler:
/// ```no_run
/// # use wxdragon::prelude::*;
/// # let data_view: DataViewCtrl = todo!();
/// data_view.on_column_sorted(|event| {
/// if let Some(ascending) = event.get_sort_order() {
/// if ascending {
/// // Handle ascending sort
/// } else {
/// // Handle descending sort
/// }
/// }
/// });
/// ```
pub fn get_sort_order(&self) -> Option<bool> {
if self.event.is_null() {
return None;
}
let mut ascending = false;
if unsafe { ffi::wxd_DataViewEvent_GetSortOrder(self.event.0, &mut ascending) } {
Some(ascending)
} else {
None
}
}
}
/// Trait for DataView event handling
pub trait DataViewEventHandler: WxEvtHandler {
/// Bind an event handler for DataView events.
/// Returns an EventToken that can be used to unbind the handler later.
fn bind_dataview_event<F>(&self, event: DataViewEventType, mut callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
// Map enum variant to EventType
let event_type = match event {
DataViewEventType::SelectionChanged => EventType::DATAVIEW_SELECTION_CHANGED,
DataViewEventType::ItemActivated => EventType::DATAVIEW_ITEM_ACTIVATED,
DataViewEventType::ItemEditingStarted => EventType::DATAVIEW_ITEM_EDITING_STARTED,
DataViewEventType::ItemEditingDone => EventType::DATAVIEW_ITEM_EDITING_DONE,
DataViewEventType::ItemEditingCancelled => EventType::DATAVIEW_ITEM_EDITING_DONE, // Same underlying event as ItemEditingDone
DataViewEventType::ItemExpanded => EventType::DATAVIEW_ITEM_EXPANDED,
DataViewEventType::ItemCollapsed => EventType::DATAVIEW_ITEM_COLLAPSED,
DataViewEventType::ColumnHeaderClick => EventType::DATAVIEW_COLUMN_HEADER_CLICK,
DataViewEventType::ColumnHeaderRightClick => EventType::DATAVIEW_COLUMN_HEADER_RIGHT_CLICK,
DataViewEventType::ItemExpanding => EventType::DATAVIEW_ITEM_EXPANDING,
DataViewEventType::ItemCollapsing => EventType::DATAVIEW_ITEM_COLLAPSING,
DataViewEventType::ColumnSorted => EventType::DATAVIEW_COLUMN_SORTED,
DataViewEventType::ColumnReordered => EventType::DATAVIEW_COLUMN_REORDERED,
DataViewEventType::ItemContextMenu => EventType::DATAVIEW_ITEM_CONTEXT_MENU,
};
// Create wrapper with special handling for editing cancelled events
let wrapper = move |base_event: Event| {
// For ItemEditingCancelled events, only trigger callback if editing was actually cancelled
if event == DataViewEventType::ItemEditingCancelled {
let data = DataViewEvent::new(base_event, event);
if data.is_edit_cancelled() {
callback(data);
}
} else if event == DataViewEventType::ItemEditingDone {
// For ItemEditingDone events, only trigger callback if editing was NOT cancelled
let data = DataViewEvent::new(base_event, event);
if !data.is_edit_cancelled() {
callback(data);
}
} else {
// For all other events, pass through normally
let data = DataViewEvent::new(base_event, event);
callback(data);
}
};
// Use internal bind method and return the token
WxEvtHandler::bind_internal(self, event_type, wrapper)
}
/// Binds a handler to the selection changed event.
/// Returns an EventToken that can be used to unbind the handler later.
fn on_selection_changed<F>(&self, callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
self.bind_dataview_event(DataViewEventType::SelectionChanged, callback)
}
/// Binds a handler to the item activated event.
/// Returns an EventToken that can be used to unbind the handler later.
fn on_item_activated<F>(&self, callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
self.bind_dataview_event(DataViewEventType::ItemActivated, callback)
}
/// Binds a handler to the item editing started event.
/// Returns an EventToken that can be used to unbind the handler later.
fn on_item_editing_started<F>(&self, callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
self.bind_dataview_event(DataViewEventType::ItemEditingStarted, callback)
}
/// Binds a handler to the item editing done event.
/// Returns an EventToken that can be used to unbind the handler later.
fn on_item_editing_done<F>(&self, callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
self.bind_dataview_event(DataViewEventType::ItemEditingDone, callback)
}
/// Binds a handler to the item editing cancelled event.
/// Returns an EventToken that can be used to unbind the handler later.
fn on_item_editing_cancelled<F>(&self, callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
self.bind_dataview_event(DataViewEventType::ItemEditingCancelled, callback)
}
/// Binds a handler to the column header click event.
/// Returns an EventToken that can be used to unbind the handler later.
fn on_column_header_click<F>(&self, callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
self.bind_dataview_event(DataViewEventType::ColumnHeaderClick, callback)
}
/// Binds a handler to the column header right click event.
/// Returns an EventToken that can be used to unbind the handler later.
fn on_column_header_right_click<F>(&self, callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
self.bind_dataview_event(DataViewEventType::ColumnHeaderRightClick, callback)
}
/// Binds a handler to the column sorted event.
/// Returns an EventToken that can be used to unbind the handler later.
fn on_column_sorted<F>(&self, callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
self.bind_dataview_event(DataViewEventType::ColumnSorted, callback)
}
/// Binds a handler to the column reordered event.
/// Returns an EventToken that can be used to unbind the handler later.
fn on_column_reordered<F>(&self, callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
self.bind_dataview_event(DataViewEventType::ColumnReordered, callback)
}
/// Binds a handler to the item context menu event.
/// Returns an EventToken that can be used to unbind the handler later.
///
/// This event is emitted when a context menu is requested on an item
/// (e.g., via right-click or keyboard). The event provides direct access
/// to the item and column information.
///
/// # Example
///
/// ```no_run
/// use wxdragon::{DataViewCtrl, DataViewEventHandler};
///
/// # let data_view: DataViewCtrl = todo!();
/// data_view.on_item_context_menu(|event| {
/// if let Some(item) = event.get_item() {
/// if let Some(col) = event.get_column() {
/// println!("Context menu requested on item at column {}", col);
/// // Show a popup menu here
/// }
/// }
/// });
/// ```
fn on_item_context_menu<F>(&self, callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
self.bind_dataview_event(DataViewEventType::ItemContextMenu, callback)
}
}
/// Extension trait for TreeView-specific events
pub trait DataViewTreeEventHandler: DataViewEventHandler {
/// Binds a handler to the item expanded event.
/// Returns an EventToken that can be used to unbind the handler later.
fn on_item_expanded<F>(&self, callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
self.bind_dataview_event(DataViewEventType::ItemExpanded, callback)
}
/// Binds a handler to the item collapsed event.
/// Returns an EventToken that can be used to unbind the handler later.
fn on_item_collapsed<F>(&self, callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
self.bind_dataview_event(DataViewEventType::ItemCollapsed, callback)
}
/// Binds a handler to the item expanding event.
/// Returns an EventToken that can be used to unbind the handler later.
fn on_item_expanding<F>(&self, callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
self.bind_dataview_event(DataViewEventType::ItemExpanding, callback)
}
/// Binds a handler to the item collapsing event.
/// Returns an EventToken that can be used to unbind the handler later.
fn on_item_collapsing<F>(&self, callback: F) -> EventToken
where
F: FnMut(DataViewEvent) + 'static,
{
self.bind_dataview_event(DataViewEventType::ItemCollapsing, callback)
}
}