use crate::{draw_2d::Font, prelude::Align, util::macros::callback};
use nappgui_sys::{
tableview_OnData, tableview_OnHeaderClick, tableview_OnRowClick, tableview_OnSelect,
tableview_column_freeze, tableview_column_limits, tableview_column_resizable,
tableview_column_width, tableview_create, tableview_deselect, tableview_deselect_all,
tableview_focus_row, tableview_font, tableview_get_focus_row, tableview_grid,
tableview_header_align, tableview_header_clickable, tableview_header_height,
tableview_header_resizable, tableview_header_title, tableview_header_visible,
tableview_hkey_scroll, tableview_multisel, tableview_new_column_text, tableview_row_height,
tableview_scroll_visible, tableview_select, tableview_selected, tableview_size,
tableview_update, S2Df,
};
pub struct TableView {
pub(crate) inner: *mut nappgui_sys::TableView,
}
impl TableView {
pub(crate) fn new(ptr: *mut nappgui_sys::TableView) -> Self {
if ptr.is_null() {
panic!("ptr is null");
}
Self { inner: ptr }
}
pub fn create() -> Self {
let table = unsafe { tableview_create() };
Self::new(table)
}
callback! {
pub on_data(TableView) => tableview_OnData;
pub on_select(TableView) => tableview_OnSelect;
pub on_row_click(TableView) => tableview_OnRowClick;
pub on_header_click(TableView) => tableview_OnHeaderClick;
}
pub fn font(&self, font: &Font) {
unsafe { tableview_font(self.inner, font.inner) }
}
pub fn size(&self, width: f32, height: f32) {
let size = S2Df { width, height };
unsafe { tableview_size(self.inner, size) }
}
pub fn new_column_text(&self) -> u32 {
unsafe { tableview_new_column_text(self.inner) }
}
pub fn column_width(&self, index: u32, width: f32) {
unsafe { tableview_column_width(self.inner, index, width) }
}
pub fn column_limits(&self, index: u32, min: f32, max: f32) {
unsafe { tableview_column_limits(self.inner, index, min, max) }
}
pub fn column_resizable(&self, column_id: u32, resizable: bool) {
unsafe { tableview_column_resizable(self.inner, column_id, resizable as i8) }
}
pub fn column_freeze(&self, freeze: u32) {
unsafe { tableview_column_freeze(self.inner, freeze) }
}
pub fn header_title(&self, index: u32, text: &str) {
let text = std::ffi::CString::new(text).unwrap();
unsafe { tableview_header_title(self.inner, index, text.as_ptr()) }
}
pub fn header_align(&self, index: u32, align: Align) {
unsafe { tableview_header_align(self.inner, index, align) }
}
pub fn header_visible(&self, visible: bool) {
unsafe { tableview_header_visible(self.inner, visible as i8) }
}
pub fn header_clickable(&self, clickable: bool) {
unsafe { tableview_header_clickable(self.inner, clickable as i8) }
}
pub fn header_resizable(&self, resizable: bool) {
unsafe { tableview_header_resizable(self.inner, resizable as i8) }
}
pub fn header_height(&self, height: f32) {
unsafe { tableview_header_height(self.inner, height) }
}
pub fn row_height(&self, height: f32) {
unsafe { tableview_row_height(self.inner, height) }
}
pub fn hkey_scroll(&self, force_column: bool, scoll: f32) {
unsafe { tableview_hkey_scroll(self.inner, force_column as i8, scoll) }
}
pub fn multisel(&self, multisel: bool, preserve: bool) {
unsafe { tableview_multisel(self.inner, multisel as i8, preserve as i8) }
}
pub fn grid(&self, hlines: bool, vlines: bool) {
unsafe { tableview_grid(self.inner, hlines as i8, vlines as i8) }
}
pub fn update(&self) {
unsafe { tableview_update(self.inner) }
}
pub fn select(&self, row: &[u32], n: u32) {
unsafe { tableview_select(self.inner, row.as_ptr(), n) }
}
pub fn deselect(&self, row: &[u32], n: u32) {
unsafe { tableview_deselect(self.inner, row.as_ptr(), n) }
}
pub fn deselect_all(&self) {
unsafe { tableview_deselect_all(self.inner) }
}
pub fn selected(&self) -> Option<Vec<u32>> {
let result = unsafe { tableview_selected(self.inner) };
if result.is_null() {
return None;
}
let result = unsafe { *result };
if result.content.is_null() {
return None;
}
let content = unsafe { *result.content };
let elem = &content.elem;
Some(elem.to_vec())
}
pub fn focus_row(&self, row: u32, align: Align) {
unsafe { tableview_focus_row(self.inner, row, align) }
}
pub fn get_focus_row(&self) -> u32 {
unsafe { tableview_get_focus_row(self.inner) }
}
pub fn scroll_visible(&self, hscroll: bool, vscroll: bool) {
unsafe { tableview_scroll_visible(self.inner, hscroll as i8, vscroll as i8) }
}
}