--- a/src/lib.rs +++ b/src/lib.rs @@ -3,3 +3,7 @@ mod menu; pub use menu::*; + +// alkane-toolkit patch: widget bridge (see cpp_src/widgets_bridge.cpp) +mod widgets; +pub use widgets::*; --- a/src/widgets.rs +++ b/src/widgets.rs @@ -0,0 +1,911 @@ +// widgets.rs — alkane-toolkit extension: Rust bindings for the widget bridge +// in cpp_src/widgets_bridge.cpp. Handles are opaque pointers owned by the +// wxWidgets object tree (parents own children; nothing to free in Rust). +// All functions must be called from the main thread while the wx event loop +// is (or will be) running. + +use std::ffi::CString; +use std::os::raw::{c_char, c_int, c_long, c_void}; + +mod notsafe { + use std::os::raw::{c_char, c_int, c_long, c_void}; + + extern "C" { + pub fn wxbr_set_widget_event_fn(f: extern "C" fn(c_int, c_long, c_long)); + pub fn wxbr_main_frame() -> *mut c_void; + pub fn wxbr_panel(parent: *mut c_void) -> *mut c_void; + pub fn wxbr_box_sizer(vertical: c_int) -> *mut c_void; + pub fn wxbr_wrap_sizer(vertical: c_int) -> *mut c_void; + pub fn wxbr_sizer_add(sizer: *mut c_void, win: *mut c_void, proportion: c_int, flags: c_int, border: c_int); + pub fn wxbr_sizer_add_sizer(sizer: *mut c_void, subsizer: *mut c_void, proportion: c_int, flags: c_int, border: c_int); + pub fn wxbr_sizer_add_spacer(sizer: *mut c_void, px: c_int); + pub fn wxbr_sizer_add_stretch(sizer: *mut c_void, proportion: c_int); + pub fn wxbr_set_sizer(win: *mut c_void, sizer: *mut c_void); + pub fn wxbr_layout(win: *mut c_void); + pub fn wxbr_sizer_detach(sizer: *mut c_void, win: *mut c_void); + pub fn wxbr_sizer_show(sizer: *mut c_void, win: *mut c_void, show: c_int); + pub fn wxbr_bind_size(win: *mut c_void); + pub fn wxbr_fit(win: *mut c_void); + pub fn wxbr_set_min_size(win: *mut c_void, w: c_int, h: c_int); + pub fn wxbr_set_tooltip(win: *mut c_void, text: *const c_char); + pub fn wxbr_static_text(parent: *mut c_void, label: *const c_char) -> *mut c_void; + pub fn wxbr_set_label(win: *mut c_void, label: *const c_char); + pub fn wxbr_get_label(win: *mut c_void, buf: *mut c_char, cap: c_int) -> c_int; + pub fn wxbr_set_bold(win: *mut c_void); + pub fn wxbr_set_font_size(win: *mut c_void, pts: c_int); + pub fn wxbr_button(parent: *mut c_void, label: *const c_char, id: c_int) -> *mut c_void; + pub fn wxbr_bind_click(win: *mut c_void, id: c_int); + pub fn wxbr_text_ctrl(parent: *mut c_void, multiline: c_int) -> *mut c_void; + pub fn wxbr_text_ctrl_get(win: *mut c_void, buf: *mut c_char, cap: c_int) -> c_int; + pub fn wxbr_text_ctrl_set(win: *mut c_void, value: *const c_char); + pub fn wxbr_text_ctrl_write(win: *mut c_void, value: *const c_char); + pub fn wxbr_check_box(parent: *mut c_void, label: *const c_char) -> *mut c_void; + pub fn wxbr_check_box_get(win: *mut c_void) -> c_int; + pub fn wxbr_check_box_set(win: *mut c_void, v: c_int); + pub fn wxbr_check_list_box(parent: *mut c_void) -> *mut c_void; + pub fn wxbr_clb_append(win: *mut c_void, label: *const c_char); + pub fn wxbr_clb_clear(win: *mut c_void); + pub fn wxbr_clb_count(win: *mut c_void) -> c_int; + pub fn wxbr_clb_checked(win: *mut c_void, i: c_int) -> c_int; + pub fn wxbr_clb_set_checked(win: *mut c_void, i: c_int, v: c_int); + pub fn wxbr_clb_string(win: *mut c_void, i: c_int, buf: *mut c_char, cap: c_int) -> c_int; + pub fn wxbr_dialog(parent: *mut c_void, title: *const c_char) -> *mut c_void; + pub fn wxbr_dialog_show_modal(win: *mut c_void) -> c_int; + pub fn wxbr_dialog_close(win: *mut c_void, code: c_int); + pub fn wxbr_file_dialog(parent: *mut c_void, title: *const c_char, buf: *mut c_char, cap: c_int) -> c_int; + pub fn wxbr_save_file_dialog(parent: *mut c_void, title: *const c_char, default_name: *const c_char, wildcard: *const c_char, buf: *mut c_char, cap: c_int) -> c_int; + pub fn wxbr_dvlc(parent: *mut c_void) -> *mut c_void; + pub fn wxbr_dvlc_append_text_col(win: *mut c_void, title: *const c_char, width: c_int, reorderable: c_int); + pub fn wxbr_dvlc_append_colored_col(win: *mut c_void, title: *const c_char, width: c_int, reorderable: c_int) -> *mut c_void; + pub fn wxbr_dvlc_append_toggle_col(win: *mut c_void, width: c_int); + pub fn wxbr_dvlc_append_row(win: *mut c_void, nvals: c_int, vals: *const *const c_char); + pub fn wxbr_dvlc_delete_row(win: *mut c_void, row: usize); + pub fn wxbr_dvlc_clear(win: *mut c_void); + pub fn wxbr_dvlc_row_count(win: *mut c_void) -> c_int; + pub fn wxbr_dvlc_set_cell(win: *mut c_void, row: usize, col: usize, value: *const c_char); + pub fn wxbr_dvlc_set_cell_tooltip(win: *mut c_void, row: usize, col: usize, text: *const c_char); + pub fn wxbr_dvlc_set_icon_tooltips(win: *mut c_void, row: usize, col: usize, joined: *const c_char); + pub fn wxbr_dvlc_get_cell(win: *mut c_void, row: usize, col: usize, buf: *mut c_char, cap: c_int) -> c_int; + pub fn wxbr_dvlc_col_count(win: *mut c_void) -> c_int; + pub fn wxbr_dvlc_autosize_col(win: *mut c_void, pos: c_int); + pub fn wxbr_dvlc_set_col_title(win: *mut c_void, col: c_int, title: *const c_char); + pub fn wxbr_dvlc_set_sort_indicator(win: *mut c_void, col: c_int, active: c_int, ascending: c_int); + pub fn wxbr_dvlc_col_at_pos(win: *mut c_void, pos: c_int) -> c_int; + pub fn wxbr_dvlc_selected_row(win: *mut c_void) -> c_long; + pub fn wxbr_dvlc_select_row(win: *mut c_void, row: c_long); + pub fn wxbr_dvlc_get_selections(win: *mut c_void, buf: *mut c_int, cap: c_int) -> c_int; + pub fn wxbr_dvlc_select_rows(win: *mut c_void, rows: *const c_int, n: c_int); + pub fn wxbr_dvlc_set_alt_row_color(win: *mut c_void, r: c_int, g: c_int, b: c_int); + pub fn wxbr_dvlc_set_row_color(win: *mut c_void, row: usize, r: c_int, g: c_int, b: c_int); + pub fn wxbr_dvlc_set_zebra(win: *mut c_void, r: c_int, g: c_int, b: c_int); + pub fn wxbr_dvlc_get_top_row(win: *mut c_void) -> c_int; + pub fn wxbr_dvlc_ensure_visible(win: *mut c_void, row: usize); + pub fn wxbr_apply_zebra_css(r: c_int, g: c_int, b: c_int); + pub fn wxbr_rearrange_list(parent: *mut c_void, n: c_int, order: *const c_int, items: *const *const c_char) -> *mut c_void; + pub fn wxbr_rearrange_get_order(win: *mut c_void, buf: *mut c_int, cap: c_int) -> c_int; + pub fn wxbr_reorder_list(parent: *mut c_void, n: c_int, order: *const c_int, items: *const *const c_char) -> *mut c_void; + pub fn wxbr_reorder_get_order(win: *mut c_void, buf: *mut c_int, cap: c_int) -> c_int; + pub fn wxbr_reorder_set_order(win: *mut c_void, n: c_int, order: *const c_int, items: *const *const c_char); + pub fn wxbr_menu_new() -> *mut c_void; + pub fn wxbr_menu_append(menu: *mut c_void, id: c_int, label: *const c_char); + pub fn wxbr_menu_append_separator(menu: *mut c_void); + pub fn wxbr_menu_append_submenu(menu: *mut c_void, label: *const c_char, submenu: *mut c_void); + pub fn wxbr_popup_menu(win: *mut c_void, menu: *mut c_void); + pub fn wxbr_timer(id: c_int, ms: c_int) -> *mut c_void; + pub fn wxbr_gauge(parent: *mut c_void, range: c_int) -> *mut c_void; + pub fn wxbr_gauge_set_range(win: *mut c_void, range: c_int); + pub fn wxbr_gauge_set_value(win: *mut c_void, value: c_int); + pub fn wxbr_gauge_pulse(win: *mut c_void); + pub fn wxbr_show(win: *mut c_void, shown: c_int); + pub fn wxbr_launch_browser(url: *const c_char) -> c_int; + pub fn wxbr_set_appearance_system(); + pub fn wxbr_is_dark() -> c_int; + pub fn wxbr_system_color(which: c_int, r: *mut u8, g: *mut u8, b: *mut u8); + pub fn wxbr_set_bg(win: *mut c_void, r: c_int, g: c_int, b: c_int); + pub fn wxbr_set_fg(win: *mut c_void, r: c_int, g: c_int, b: c_int); + pub fn wxbr_choice(parent: *mut c_void, id: c_int) -> *mut c_void; + pub fn wxbr_choice_append(win: *mut c_void, label: *const c_char); + pub fn wxbr_choice_clear(win: *mut c_void); + pub fn wxbr_choice_get_selection(win: *mut c_void) -> c_int; + pub fn wxbr_choice_set_selection(win: *mut c_void, i: c_int); + pub fn wxbr_media_button(parent: *mut c_void, id: c_int, kind: c_int, r: c_int, g: c_int, b: c_int) -> *mut c_void; + pub fn wxbr_svg_icon(parent: *mut c_void, svg: *const c_char, w: c_int, h: c_int) -> *mut c_void; + pub fn wxbr_register_shape_svg(sentinel: c_int, svg: *const c_char, vbw: c_int, vbh: c_int); + pub fn wxbr_set_light_pulse(on: c_int); + pub fn wxbr_bind_sys_colour(win: *mut c_void); + pub fn wxbr_destroy(win: *mut c_void); + pub fn wxbr_destroy_children(win: *mut c_void); + pub fn wxbr_call_after(id: c_int); + pub fn wxbr_status_indicator_init(frame: *mut c_void); + pub fn wxbr_status_indicator_set(text: *const c_char, r: c_int, g: c_int, b: c_int); + pub fn wxbr_status_zoom_init(frame: *mut c_void); + pub fn wxbr_table_set_zoom(factor: f64); + pub fn wxbr_table_zoom_reapply(); + } +} + +/// Opaque handle to a wxWidgets window/sizer/control. +#[derive(Clone, Copy, Debug)] +pub struct Widget(pub(crate) *mut c_void); + +/// Sizer flags understood by the bridge (see widgets_bridge.cpp). +pub mod sizer_flags { + pub const EXPAND: i32 = 1; + pub const ALIGN_CENTRE_VERTICAL: i32 = 2; + pub const BORDER_ALL: i32 = 4; + pub const BORDER_TOP: i32 = 8; + pub const BORDER_BOTTOM: i32 = 16; + pub const BORDER_LEFT: i32 = 32; + pub const BORDER_RIGHT: i32 = 64; +} + +/// Widget event kinds delivered to the handler set by `set_event_handler`. +/// Values match WxWidgetEventKind in widgets_bridge.cpp. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum WxEventKind { + ButtonClicked, + ClbToggled, + DvlcValueChanged, + DvlcHeaderClick, + DvlcReordered, + TimerTick, + DvlcActivated, + DvlcSelectionChanged, + DvlcContextMenu, + MenuItem, + ChoiceChanged, + SysColour, + Deferred, + DvlcHeaderRightClick, + /// Window resized; p1 = new client width, p2 = new client height. + Sized, +} + +impl WxEventKind { + fn from_raw(k: i32) -> Option { + Some(match k { + 1 => WxEventKind::ButtonClicked, + 3 => WxEventKind::ClbToggled, + 4 => WxEventKind::DvlcValueChanged, + 5 => WxEventKind::DvlcHeaderClick, + 6 => WxEventKind::DvlcReordered, + 7 => WxEventKind::TimerTick, + 8 => WxEventKind::DvlcActivated, + 9 => WxEventKind::DvlcSelectionChanged, + 10 => WxEventKind::DvlcContextMenu, + 11 => WxEventKind::MenuItem, + 12 => WxEventKind::ChoiceChanged, + 13 => WxEventKind::SysColour, + 14 => WxEventKind::Deferred, + 15 => WxEventKind::DvlcHeaderRightClick, + 16 => WxEventKind::Sized, + _ => return None, + }) + } +} + +/// Apply app-wide zebra striping to tree/list views (GTK CSS; no-op elsewhere). +pub fn apply_zebra_css(r: u8, g: u8, b: u8) { + unsafe { notsafe::wxbr_apply_zebra_css(r as c_int, g as c_int, b as c_int) } +} + +/// Schedule a deferred action: a `Deferred` event carrying `id` is delivered +/// from the event loop after the current event finishes. Use this to rebuild or +/// destroy a control from outside its own event handler. +pub fn call_after(id: i32) { + unsafe { notsafe::wxbr_call_after(id as c_int) } +} + +/// Reconfigure the frame's status bar to two fields and attach a colour-tinted +/// label pinned to the right-hand field (bottom-right corner). Call once after +/// `create_status_bar()`. The main scan status keeps using the left field via +/// the upstream `set_status_text` path. +pub fn status_indicator_init(frame: Widget) { + unsafe { notsafe::wxbr_status_indicator_init(frame.0) } +} + +/// Add the table-zoom slider + percent label to the frame's status bar (round 14 +/// #3). Call after the table exists so the slider can find it. The slider scales +/// the table entirely in C++; no further Rust calls are needed. +pub fn status_zoom_init(frame: Widget) { + unsafe { notsafe::wxbr_status_zoom_init(frame.0) } +} + +/// Set the table zoom factor programmatically (1.0 = 100%); also moves the slider. +pub fn table_set_zoom(factor: f64) { + unsafe { notsafe::wxbr_table_set_zoom(factor) } +} + +/// Re-apply the current zoom after the table's columns are rebuilt, so a non-100% +/// zoom survives a column show/hide/reorder (round 14 #3). +pub fn table_zoom_reapply() { + unsafe { notsafe::wxbr_table_zoom_reapply() } +} + +/// Set the bottom-right status indicator's text and colour (an empty string +/// clears it). Used for scan/apply errors (red) and warnings (amber). +pub fn status_indicator_set(text: &str, r: u8, g: u8, b: u8) { + let c = CString::new(text).unwrap_or_default(); + unsafe { notsafe::wxbr_status_indicator_set(c.as_ptr(), r as c_int, g as c_int, b as c_int) } +} + +/// Register a raw SVG for a table-cell shape sentinel (a control-char code point) +/// so the segment renderer can draw it recoloured to each cell's segment colour. +/// `vbw`/`vbh` are the SVG viewBox dimensions, used to keep the icon's aspect +/// ratio when it is rasterised at the cell height (round 12 #9/#11). +pub fn register_shape_svg(sentinel: u32, svg: &str, vbw: i32, vbh: i32) { + let c = CString::new(svg).unwrap_or_default(); + unsafe { + notsafe::wxbr_register_shape_svg(sentinel as c_int, c.as_ptr(), vbw as c_int, vbh as c_int) + } +} + +/// Start (or stop) the locate-light dot fade animation. Call with `true` while at +/// least one miner's locate light is flashing so the table repaint timer only +/// runs when there is a pulsing dot to animate (round 12 #6). +pub fn set_light_pulse(on: bool) { + unsafe { notsafe::wxbr_set_light_pulse(on as c_int) } +} + +/// Open a URL in the system default browser. Returns true on success. +pub fn launch_browser(url: &str) -> bool { + let c = CString::new(url).unwrap_or_default(); + unsafe { notsafe::wxbr_launch_browser(c.as_ptr()) != 0 } +} + +/// Ask wx to follow the system light/dark appearance (no-op before 3.3). +pub fn set_appearance_system() { + unsafe { notsafe::wxbr_set_appearance_system() } +} + +/// True if the current system appearance is dark. +pub fn is_dark() -> bool { + unsafe { notsafe::wxbr_is_dark() != 0 } +} + +/// A theme colour. `which`: 0=window bg, 1=window text, 2=highlight, +/// 3=button face, 4=grey text. Returns (r, g, b). +pub fn system_color(which: i32) -> (u8, u8, u8) { + let (mut r, mut g, mut b) = (0u8, 0u8, 0u8); + unsafe { notsafe::wxbr_system_color(which, &mut r, &mut g, &mut b) } + (r, g, b) +} + +/// A widget event: kind plus two payload words. +/// ButtonClicked: p1 = button id. +/// ClbToggled: p1 = control handle, p2 = item index. +/// DvlcValueChanged: p1 = row, p2 = column. +/// DvlcHeaderClick: p1 = column. +/// DvlcReordered: (re-read the column order afterwards). +/// TimerTick: p1 = timer id. +#[derive(Debug, Clone, Copy)] +pub struct WxEvent { + pub kind: WxEventKind, + pub p1: i64, + pub p2: i64, +} + +type Handler = fn(WxEvent); + +thread_local! { + static HANDLER: std::cell::RefCell> = const { std::cell::RefCell::new(None) }; +} + +extern "C" fn event_trampoline(kind: c_int, p1: c_long, p2: c_long) { + let Some(kind) = WxEventKind::from_raw(kind) else { return }; + let ev = WxEvent { kind, p1: p1 as i64, p2: p2 as i64 }; + HANDLER.with(|h| { + if let Some(f) = h.borrow().as_ref() { + f(ev); + } + }); +} + +/// Install the single widget-event handler (call once, before run_app). +pub fn set_event_handler(f: Handler) { + HANDLER.with(|h| *h.borrow_mut() = Some(f)); + unsafe { + notsafe::wxbr_set_widget_event_fn(event_trampoline); + } +} + +impl Widget { + pub fn main_frame() -> Widget { + Widget(unsafe { notsafe::wxbr_main_frame() }) + } + + pub fn panel(parent: Widget) -> Widget { + Widget(unsafe { notsafe::wxbr_panel(parent.0) }) + } + + pub fn box_sizer(vertical: bool) -> Widget { + Widget(unsafe { notsafe::wxbr_box_sizer(vertical as c_int) }) + } + + /// A box sizer that wraps overflowing items onto additional lines instead + /// of shrinking them. wxWrapSizer is a wxBoxSizer subclass, so the result + /// accepts the same add/add_sizer/add_spacer/add_stretch/set_sizer_on + /// calls as `box_sizer`'s. + pub fn wrap_sizer(vertical: bool) -> Widget { + Widget(unsafe { notsafe::wxbr_wrap_sizer(vertical as c_int) }) + } + + pub fn add(&self, win: Widget, proportion: i32, flags: i32, border: i32) { + unsafe { notsafe::wxbr_sizer_add(self.0, win.0, proportion, flags, border) } + } + + /// Add a child SIZER (not a window) to this sizer. + pub fn add_sizer(&self, sizer: Widget, proportion: i32, flags: i32, border: i32) { + unsafe { notsafe::wxbr_sizer_add_sizer(self.0, sizer.0, proportion, flags, border) } + } + + pub fn add_spacer(&self, px: i32) { + unsafe { notsafe::wxbr_sizer_add_spacer(self.0, px) } + } + + pub fn add_stretch(&self, proportion: i32) { + unsafe { notsafe::wxbr_sizer_add_stretch(self.0, proportion) } + } + + pub fn set_sizer_on(&self, win: Widget) { + unsafe { notsafe::wxbr_set_sizer(win.0, self.0) } + } + + /// Re-run this window's sizer (self = a window). Call after adding a child + /// window to an already-installed sizer, or after rebuilding the table. + pub fn layout(&self) { + unsafe { notsafe::wxbr_layout(self.0) } + } + + /// Detach a child window from this sizer (self = a sizer) without + /// destroying it. Use before destroying a sizer-managed window. + pub fn detach(&self, win: Widget) { + unsafe { notsafe::wxbr_sizer_detach(self.0, win.0) } + } + + /// Show or hide a child window in this sizer (self = a sizer), reclaiming or + /// restoring its space on the next `layout()`. + pub fn sizer_show(&self, win: Widget, show: bool) { + unsafe { notsafe::wxbr_sizer_show(self.0, win.0, show as c_int) } + } + + /// Emit `WxEventKind::Sized` (p1 = width, p2 = height) whenever this window + /// is resized. + pub fn bind_size(&self) { + unsafe { notsafe::wxbr_bind_size(self.0) } + } + + pub fn fit(&self, win: Widget) { + unsafe { notsafe::wxbr_fit(win.0) } + } + + pub fn set_min_size(&self, w: i32, h: i32) { + unsafe { notsafe::wxbr_set_min_size(self.0, w, h) } + } + + /// Attach a hover tooltip to this widget (round 15 #10). Empty clears it. + pub fn set_tool_tip(&self, text: &str) { + let c = CString::new(text).unwrap_or_default(); + unsafe { notsafe::wxbr_set_tooltip(self.0, c.as_ptr()) } + } + + pub fn static_text(parent: Widget, label: &str) -> Widget { + let c = CString::new(label).unwrap_or_default(); + Widget(unsafe { notsafe::wxbr_static_text(parent.0, c.as_ptr()) }) + } + + pub fn set_label(&self, label: &str) { + let c = CString::new(label).unwrap_or_default(); + unsafe { notsafe::wxbr_set_label(self.0, c.as_ptr()) } + } + + /// Read back a control's current label text (self-test uses this to verify + /// what a KPI actually displays). + pub fn get_label(&self) -> String { + let mut buf = vec![0i8; 4096]; + let n = unsafe { notsafe::wxbr_get_label(self.0, buf.as_mut_ptr(), buf.len() as c_int) }; + if n <= 0 { + return String::new(); + } + let bytes: Vec = buf[..(n - 1) as usize].iter().map(|&b| b as u8).collect(); + String::from_utf8_lossy(&bytes).into_owned() + } + + pub fn set_bold(&self) { + unsafe { notsafe::wxbr_set_bold(self.0) } + } + + pub fn set_font_size(&self, pts: i32) { + unsafe { notsafe::wxbr_set_font_size(self.0, pts) } + } + + pub fn button(parent: Widget, label: &str, id: i32) -> Widget { + let c = CString::new(label).unwrap_or_default(); + Widget(unsafe { notsafe::wxbr_button(parent.0, c.as_ptr(), id) }) + } + + /// Make this window clickable: a left mouse down on it emits the same + /// `ButtonClicked` event a real button would, carrying `id`. Bind each + /// child that should also count as part of the click target (e.g. the + /// static text labels on a KPI card), since a child window swallows its + /// own mouse events rather than letting them propagate to the parent. + pub fn bind_click(&self, id: i32) { + unsafe { notsafe::wxbr_bind_click(self.0, id) } + } + + pub fn text_ctrl(parent: Widget, multiline: bool) -> Widget { + Widget(unsafe { notsafe::wxbr_text_ctrl(parent.0, multiline as c_int) }) + } + + pub fn get_value(&self) -> String { + let mut buf = vec![0i8; 65536]; + let n = unsafe { notsafe::wxbr_text_ctrl_get(self.0, buf.as_mut_ptr(), buf.len() as c_int) }; + let n = n.max(1) as usize; + let bytes: Vec = buf[..n - 1].iter().map(|&b| b as u8).collect(); + String::from_utf8_lossy(&bytes).into_owned() + } + + pub fn set_value(&self, value: &str) { + let c = CString::new(value).unwrap_or_default(); + unsafe { notsafe::wxbr_text_ctrl_set(self.0, c.as_ptr()) } + } + + /// Insert `value` at the text control's cursor (replacing any selection), + /// rather than replacing the whole contents (round 15 #1). + pub fn write_text(&self, value: &str) { + let c = CString::new(value).unwrap_or_default(); + unsafe { notsafe::wxbr_text_ctrl_write(self.0, c.as_ptr()) } + } + + pub fn check_box(parent: Widget, label: &str) -> Widget { + let c = CString::new(label).unwrap_or_default(); + Widget(unsafe { notsafe::wxbr_check_box(parent.0, c.as_ptr()) }) + } + + pub fn is_checked(&self) -> bool { + unsafe { notsafe::wxbr_check_box_get(self.0) != 0 } + } + + pub fn set_checked(&self, v: bool) { + unsafe { notsafe::wxbr_check_box_set(self.0, v as c_int) } + } + + pub fn check_list_box(parent: Widget) -> Widget { + Widget(unsafe { notsafe::wxbr_check_list_box(parent.0) }) + } + + pub fn clb_append(&self, label: &str) { + let c = CString::new(label).unwrap_or_default(); + unsafe { notsafe::wxbr_clb_append(self.0, c.as_ptr()) } + } + + pub fn clb_clear(&self) { + unsafe { notsafe::wxbr_clb_clear(self.0) } + } + + pub fn clb_count(&self) -> usize { + unsafe { notsafe::wxbr_clb_count(self.0).max(0) as usize } + } + + pub fn clb_checked(&self, i: usize) -> bool { + unsafe { notsafe::wxbr_clb_checked(self.0, i as c_int) != 0 } + } + + pub fn clb_set_checked(&self, i: usize, v: bool) { + unsafe { notsafe::wxbr_clb_set_checked(self.0, i as c_int, v as c_int) } + } + + pub fn clb_string(&self, i: usize) -> Option { + let mut buf = vec![0i8; 4096]; + let n = unsafe { notsafe::wxbr_clb_string(self.0, i as c_int, buf.as_mut_ptr(), buf.len() as c_int) }; + if n <= 0 { + return None; + } + let bytes: Vec = buf[..(n - 1) as usize].iter().map(|&b| b as u8).collect(); + Some(String::from_utf8_lossy(&bytes).into_owned()) + } + + pub fn dialog(parent: Widget, title: &str) -> Widget { + let c = CString::new(title).unwrap_or_default(); + Widget(unsafe { notsafe::wxbr_dialog(parent.0, c.as_ptr()) }) + } + + /// Blocks in a nested event loop until the dialog is dismissed. + pub fn show_modal(&self) -> i32 { + unsafe { notsafe::wxbr_dialog_show_modal(self.0) } + } + + pub fn close(&self, code: i32) { + unsafe { notsafe::wxbr_dialog_close(self.0, code) } + } + + /// Show a native "open file, must exist" dialog. Returns the chosen path, + /// or `None` if the dialog was cancelled. + pub fn file_dialog(parent: Widget, title: &str) -> Option { + let c = CString::new(title).unwrap_or_default(); + let mut buf = vec![0i8; 4096]; + let n = unsafe { + notsafe::wxbr_file_dialog(parent.0, c.as_ptr(), buf.as_mut_ptr(), buf.len() as c_int) + }; + if n <= 0 { + return None; + } + let bytes: Vec = buf[..(n - 1) as usize].iter().map(|&b| b as u8).collect(); + Some(String::from_utf8_lossy(&bytes).into_owned()) + } + + /// Save-file dialog (round 15 #14): returns the chosen path, or None if + /// cancelled. `default_name` prefills the filename; `wildcard` is a wx filter + /// string like "CSV files (*.csv)|*.csv". + pub fn save_file_dialog( + parent: Widget, + title: &str, + default_name: &str, + wildcard: &str, + ) -> Option { + let t = CString::new(title).unwrap_or_default(); + let d = CString::new(default_name).unwrap_or_default(); + let w = CString::new(wildcard).unwrap_or_default(); + let mut buf = vec![0i8; 4096]; + let n = unsafe { + notsafe::wxbr_save_file_dialog( + parent.0, + t.as_ptr(), + d.as_ptr(), + w.as_ptr(), + buf.as_mut_ptr(), + buf.len() as c_int, + ) + }; + if n <= 0 { + return None; + } + let bytes: Vec = buf[..(n - 1) as usize].iter().map(|&b| b as u8).collect(); + Some(String::from_utf8_lossy(&bytes).into_owned()) + } + + // ---- data view list ctrl ---- + + pub fn dvlc(parent: Widget) -> Widget { + Widget(unsafe { notsafe::wxbr_dvlc(parent.0) }) + } + + pub fn dvlc_append_text_col(&self, title: &str, width: i32, reorderable: bool) { + let c = CString::new(title).unwrap_or_default(); + unsafe { notsafe::wxbr_dvlc_append_text_col(self.0, c.as_ptr(), width, reorderable as c_int) } + } + + /// Like `dvlc_append_text_col`, but the column renders cell values through + /// the SegmentRenderer, so a value built with `model::encode_segments` is + /// drawn as coloured text runs (a plain string still renders normally). + pub fn dvlc_append_colored_col(&self, title: &str, width: i32, reorderable: bool) { + let c = CString::new(title).unwrap_or_default(); + unsafe { notsafe::wxbr_dvlc_append_colored_col(self.0, c.as_ptr(), width, reorderable as c_int) }; + } + + pub fn dvlc_append_toggle_col(&self, width: i32) { + unsafe { notsafe::wxbr_dvlc_append_toggle_col(self.0, width) } + } + + pub fn dvlc_append_row(&self, values: &[&str]) { + let cvals: Vec = values.iter().map(|v| CString::new(*v).unwrap_or_default()).collect(); + let ptrs: Vec<*const c_char> = cvals.iter().map(|c| c.as_ptr()).collect(); + unsafe { notsafe::wxbr_dvlc_append_row(self.0, ptrs.len() as c_int, ptrs.as_ptr()) } + } + + pub fn dvlc_delete_row(&self, row: usize) { + unsafe { notsafe::wxbr_dvlc_delete_row(self.0, row) } + } + + pub fn dvlc_clear(&self) { + unsafe { notsafe::wxbr_dvlc_clear(self.0) } + } + + pub fn dvlc_row_count(&self) -> usize { + unsafe { notsafe::wxbr_dvlc_row_count(self.0).max(0) as usize } + } + + pub fn dvlc_set_cell(&self, row: usize, col: usize, value: &str) { + let c = CString::new(value).unwrap_or_default(); + unsafe { notsafe::wxbr_dvlc_set_cell(self.0, row, col, c.as_ptr()) } + } + + /// Set the hover tooltip for one cell (empty clears it). GTK only; a no-op + /// on other platforms until they gain per-cell tooltip support. + pub fn dvlc_set_cell_tooltip(&self, row: usize, col: usize, text: &str) { + let c = CString::new(text).unwrap_or_default(); + unsafe { notsafe::wxbr_dvlc_set_cell_tooltip(self.0, row, col, c.as_ptr()) } + } + + /// Set per-icon hover tooltips for a cell (round 15 #15): one tooltip per icon + /// in draw order. The bridge shows the one under the pointer. Passing an empty + /// slice clears them. + pub fn dvlc_set_icon_tooltips(&self, row: usize, col: usize, tips: &[String]) { + let joined = tips.join("\u{1f}"); + let c = CString::new(joined).unwrap_or_default(); + unsafe { notsafe::wxbr_dvlc_set_icon_tooltips(self.0, row, col, c.as_ptr()) } + } + + pub fn dvlc_get_cell(&self, row: usize, col: usize) -> Option { + let mut buf = vec![0i8; 4096]; + let n = unsafe { + notsafe::wxbr_dvlc_get_cell(self.0, row, col, buf.as_mut_ptr(), buf.len() as c_int) + }; + if n <= 0 { + return None; + } + let bytes: Vec = buf[..(n - 1) as usize].iter().map(|&b| b as u8).collect(); + Some(String::from_utf8_lossy(&bytes).into_owned()) + } + + pub fn dvlc_col_count(&self) -> usize { + unsafe { notsafe::wxbr_dvlc_col_count(self.0).max(0) as usize } + } + + /// Auto-size the column at display position `pos` to fit its content. + pub fn dvlc_autosize_col(&self, pos: usize) { + unsafe { notsafe::wxbr_dvlc_autosize_col(self.0, pos as c_int) } + } + + /// Model column index shown at display position `pos` (after drag reorder). + pub fn dvlc_col_at_pos(&self, pos: usize) -> Option { + let v = unsafe { notsafe::wxbr_dvlc_col_at_pos(self.0, pos as c_int) }; + if v < 0 { None } else { Some(v as usize) } + } + + pub fn dvlc_selected_row(&self) -> Option { + let v = unsafe { notsafe::wxbr_dvlc_selected_row(self.0) }; + if v < 0 { None } else { Some(v as usize) } + } + + pub fn dvlc_select_row(&self, row: usize) { + unsafe { notsafe::wxbr_dvlc_select_row(self.0, row as c_long) } + } + + /// The selected row indices (multi-select). + pub fn dvlc_get_selections(&self) -> Vec { + let mut buf = vec![0i32; 8192]; + let n = unsafe { + notsafe::wxbr_dvlc_get_selections(self.0, buf.as_mut_ptr(), buf.len() as c_int) + }; + buf.into_iter().take(n.max(0) as usize).map(|v| v as usize).collect() + } + + /// Replace the selection with the given row indices. + pub fn dvlc_select_rows(&self, rows: &[usize]) { + let v: Vec = rows.iter().map(|&r| r as c_int).collect(); + unsafe { notsafe::wxbr_dvlc_select_rows(self.0, v.as_ptr(), v.len() as c_int) } + } + + pub fn dvlc_set_alt_row_color(&self, r: u8, g: u8, b: u8) { + unsafe { notsafe::wxbr_dvlc_set_alt_row_color(self.0, r as c_int, g as c_int, b as c_int) } + } + + /// Set a row's text colour (e.g. by miner status). Pass `None` to clear it. + pub fn dvlc_set_row_color(&self, row: usize, color: Option<(u8, u8, u8)>) { + let (r, g, b) = match color { + Some((r, g, b)) => (r as c_int, g as c_int, b as c_int), + None => (-1, -1, -1), + }; + unsafe { notsafe::wxbr_dvlc_set_row_color(self.0, row, r, g, b) } + } + + /// Set the alternating-row (zebra) background colour applied to odd rows. + /// Pass `None` to turn zebra striping off. + pub fn dvlc_set_zebra(&self, color: Option<(u8, u8, u8)>) { + let (r, g, b) = match color { + Some((r, g, b)) => (r as c_int, g as c_int, b as c_int), + None => (-1, -1, -1), + }; + unsafe { notsafe::wxbr_dvlc_set_zebra(self.0, r, g, b) } + } + + /// The model-row index of the top (first visible) row, or `None` if empty. + pub fn dvlc_get_top_row(&self) -> Option { + let r = unsafe { notsafe::wxbr_dvlc_get_top_row(self.0) }; + (r >= 0).then_some(r as usize) + } + + /// Scroll so `row` is visible (used to restore scroll position after a refill). + pub fn dvlc_ensure_visible(&self, row: usize) { + unsafe { notsafe::wxbr_dvlc_ensure_visible(self.0, row) } + } + + // ---- rearrangeable check-list ---- + + /// Create a rearrangeable checklist. `order` uses wx encoding: a value `v` + /// >= 0 is item `v` (checked); `!v` (negative) is item `v` unchecked. + pub fn rearrange_list(parent: Widget, order: &[i32], items: &[&str]) -> Widget { + let cs: Vec = items.iter().map(|s| CString::new(*s).unwrap_or_default()).collect(); + let ptrs: Vec<*const c_char> = cs.iter().map(|c| c.as_ptr()).collect(); + Widget(unsafe { + notsafe::wxbr_rearrange_list(parent.0, items.len() as c_int, order.as_ptr(), ptrs.as_ptr()) + }) + } + + /// Read back the current order (same wx encoding as `rearrange_list`). + pub fn rearrange_get_order(&self) -> Vec { + let mut buf = vec![0i32; 4096]; + let n = unsafe { + notsafe::wxbr_rearrange_get_order(self.0, buf.as_mut_ptr(), buf.len() as c_int) + }; + buf.into_iter().take(n.max(0) as usize).collect() + } + + /// A checkable, multi-select, drag-reorderable column list (round 11 #4): + /// like `rearrange_list` but with no up/down buttons -- rows are reordered by + /// dragging, and selected with shift/ctrl-click or a rubber-band drag. Same + /// `[idx | !idx]` order encoding. + pub fn reorder_list(parent: Widget, order: &[i32], items: &[&str]) -> Widget { + let cs: Vec = items.iter().map(|s| CString::new(*s).unwrap_or_default()).collect(); + let ptrs: Vec<*const c_char> = cs.iter().map(|c| c.as_ptr()).collect(); + Widget(unsafe { + notsafe::wxbr_reorder_list(parent.0, items.len() as c_int, order.as_ptr(), ptrs.as_ptr()) + }) + } + + /// Read back the current order from a `reorder_list` (same encoding). + pub fn reorder_get_order(&self) -> Vec { + let mut buf = vec![0i32; 4096]; + let n = unsafe { + notsafe::wxbr_reorder_get_order(self.0, buf.as_mut_ptr(), buf.len() as c_int) + }; + buf.into_iter().take(n.max(0) as usize).collect() + } + + /// Repopulate a `reorder_list` in a new order (same `[idx | !idx]` encoding + /// and same base-index `items` array as `reorder_list`). Used by the column + /// picker's Alphabetical / Categories sort buttons. + pub fn reorder_set_order(&self, order: &[i32], items: &[&str]) { + let cs: Vec = items.iter().map(|s| CString::new(*s).unwrap_or_default()).collect(); + let ptrs: Vec<*const c_char> = cs.iter().map(|c| c.as_ptr()).collect(); + unsafe { + notsafe::wxbr_reorder_set_order(self.0, items.len() as c_int, order.as_ptr(), ptrs.as_ptr()) + } + } + + // ---- popup menu ---- + + pub fn menu_new() -> Widget { + Widget(unsafe { notsafe::wxbr_menu_new() }) + } + + pub fn menu_append(&self, id: i32, label: &str) { + let c = CString::new(label).unwrap_or_default(); + unsafe { notsafe::wxbr_menu_append(self.0, id, c.as_ptr()) } + } + + pub fn menu_append_separator(&self) { + unsafe { notsafe::wxbr_menu_append_separator(self.0) } + } + + /// Append `submenu` under this menu with `label` (round 15 #13). This menu + /// takes ownership of the submenu. + pub fn menu_append_submenu(&self, label: &str, submenu: Widget) { + let c = CString::new(label).unwrap_or_default(); + unsafe { notsafe::wxbr_menu_append_submenu(self.0, c.as_ptr(), submenu.0) } + } + + /// Show `menu` as a popup on this window; blocks until dismissed. + pub fn popup_menu(&self, menu: Widget) { + unsafe { notsafe::wxbr_popup_menu(self.0, menu.0) } + } + + /// Update a column's header text in place (creation index). + pub fn dvlc_set_col_title(&self, col: usize, title: &str) { + let c = CString::new(title).unwrap_or_default(); + unsafe { notsafe::wxbr_dvlc_set_col_title(self.0, col as c_int, c.as_ptr()) } + } + + /// Show or clear the native sort arrow on a column (creation index). + pub fn dvlc_set_sort_indicator(&self, col: usize, active: bool, ascending: bool) { + unsafe { + notsafe::wxbr_dvlc_set_sort_indicator( + self.0, + col as c_int, + active as c_int, + ascending as c_int, + ) + } + } + + pub fn timer(id: i32, ms: i32) -> Widget { + Widget(unsafe { notsafe::wxbr_timer(id, ms) }) + } + + // ---- gauge (progress bar) ---- + + pub fn gauge(parent: Widget, range: i32) -> Widget { + Widget(unsafe { notsafe::wxbr_gauge(parent.0, range) }) + } + + pub fn gauge_set_range(&self, range: i32) { + unsafe { notsafe::wxbr_gauge_set_range(self.0, range) } + } + + pub fn gauge_set_value(&self, value: i32) { + unsafe { notsafe::wxbr_gauge_set_value(self.0, value) } + } + + pub fn gauge_pulse(&self) { + unsafe { notsafe::wxbr_gauge_pulse(self.0) } + } + + /// Show or hide this window. + pub fn show(&self, shown: bool) { + unsafe { notsafe::wxbr_show(self.0, shown as c_int) } + } + + pub fn set_bg(&self, r: u8, g: u8, b: u8) { + unsafe { notsafe::wxbr_set_bg(self.0, r as c_int, g as c_int, b as c_int) } + } + + pub fn set_fg(&self, r: u8, g: u8, b: u8) { + unsafe { notsafe::wxbr_set_fg(self.0, r as c_int, g as c_int, b as c_int) } + } + + // ---- choice (dropdown) ---- + + /// Create a dropdown; selection changes emit `ChoiceChanged` with this id. + pub fn choice(parent: Widget, id: i32) -> Widget { + Widget(unsafe { notsafe::wxbr_choice(parent.0, id) }) + } + + pub fn choice_append(&self, label: &str) { + let c = CString::new(label).unwrap_or_default(); + unsafe { notsafe::wxbr_choice_append(self.0, c.as_ptr()) } + } + + pub fn choice_clear(&self) { + unsafe { notsafe::wxbr_choice_clear(self.0) } + } + + pub fn choice_get_selection(&self) -> i32 { + unsafe { notsafe::wxbr_choice_get_selection(self.0) } + } + + pub fn choice_set_selection(&self, i: i32) { + unsafe { notsafe::wxbr_choice_set_selection(self.0, i) } + } + + // ---- media (icon) button ---- + + /// A coloured icon button: `kind` 0=play, 1=pause, 2=stop. Clicks emit + /// `ButtonClicked` with `id`, like a normal button. + pub fn media_button(parent: Widget, id: i32, kind: i32, color: (u8, u8, u8)) -> Widget { + Widget(unsafe { + notsafe::wxbr_media_button( + parent.0, + id, + kind, + color.0 as c_int, + color.1 as c_int, + color.2 as c_int, + ) + }) + } + + /// Render an SVG string (recolour it first -- its fill must be an explicit + /// colour, not "currentColor") into a static-bitmap icon at `w`x`h` px + /// (round 11 #15). + pub fn svg_icon(parent: Widget, svg: &str, w: i32, h: i32) -> Widget { + let c = CString::new(svg).unwrap_or_default(); + Widget(unsafe { notsafe::wxbr_svg_icon(parent.0, c.as_ptr(), w as c_int, h as c_int) }) + } + + /// Listen for OS light/dark theme changes on this window (emits `SysColour`). + pub fn bind_sys_colour(&self) { + unsafe { notsafe::wxbr_bind_sys_colour(self.0) } + } + + /// Deferred destruction (safe from event handlers); used to rebuild + /// the table when its column set changes. + pub fn destroy(&self) { + unsafe { notsafe::wxbr_destroy(self.0) } + } + + /// Destroy this window's children (to rebuild dynamic panel contents). + pub fn destroy_children(&self) { + unsafe { notsafe::wxbr_destroy_children(self.0) } + } +}