use crate::event::WxEvtHandler;
use crate::prelude::*;
use crate::window::{WindowHandle, WxWidget};
#[allow(unused_imports)]
use crate::window::Window;
use std::ffi::CString;
use wxdragon_sys as ffi;
widget_style_enum!(
name: AuiNotebookStyle,
doc: "Style flags for AuiNotebook.",
variants: {
Default: 0x00000001 | 0x00000002 | 0x00000004 | 0x00000010 | 0x00000040 | 0x00000200, "Default AuiNotebook style."
},
default_variant: Default
);
#[derive(Clone, Copy)]
pub struct AuiNotebook {
handle: WindowHandle,
}
impl AuiNotebook {
fn from_ptr(ptr: *mut ffi::wxd_AuiNotebook_t) -> Self {
AuiNotebook {
handle: WindowHandle::new(ptr as *mut ffi::wxd_Window_t),
}
}
pub fn builder<'a>(parent: &'a dyn WxWidget) -> AuiNotebookBuilder<'a> {
AuiNotebookBuilder::new(parent)
}
#[inline]
fn notebook_ptr(&self) -> *mut ffi::wxd_AuiNotebook_t {
self.handle
.get_ptr()
.map(|p| p as *mut ffi::wxd_AuiNotebook_t)
.unwrap_or(std::ptr::null_mut())
}
pub fn add_page(&self, page: &impl WxWidget, caption: &str, select: bool) -> bool {
let ptr = self.notebook_ptr();
if ptr.is_null() {
return false;
}
let caption_c = CString::new(caption).expect("CString::new failed for caption");
unsafe {
ffi::wxd_AuiNotebook_AddPage(ptr, page.handle_ptr(), caption_c.as_ptr(), select, -1)
}
}
pub fn page_count(&self) -> usize {
let ptr = self.notebook_ptr();
if ptr.is_null() {
return 0;
}
unsafe { ffi::wxd_AuiNotebook_GetPageCount(ptr) as usize }
}
pub fn set_selection(&self, new_page: usize) -> usize {
let ptr = self.notebook_ptr();
if ptr.is_null() {
return 0;
}
unsafe { ffi::wxd_AuiNotebook_SetSelection(ptr, new_page) as usize }
}
pub fn get_selection(&self) -> i32 {
let ptr = self.notebook_ptr();
if ptr.is_null() {
return -1;
}
unsafe { ffi::wxd_AuiNotebook_GetSelection(ptr) }
}
pub fn insert_page(&self, page_idx: usize, page: &impl WxWidget, caption: &str, select: bool) -> bool {
let ptr = self.notebook_ptr();
if ptr.is_null() {
return false;
}
let caption_c = CString::new(caption).expect("CString::new failed for caption");
unsafe { ffi::wxd_AuiNotebook_InsertPage(ptr, page_idx, page.handle_ptr(), caption_c.as_ptr(), select, -1) }
}
pub fn delete_page(&self, page_idx: usize) -> bool {
let ptr = self.notebook_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_AuiNotebook_DeletePage(ptr, page_idx) }
}
pub fn remove_page(&self, page_idx: usize) -> bool {
let ptr = self.notebook_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_AuiNotebook_RemovePage(ptr, page_idx) }
}
pub fn delete_all_pages(&self) -> bool {
let ptr = self.notebook_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_AuiNotebook_DeleteAllPages(ptr) }
}
pub fn get_page(&self, page_idx: usize) -> Option<WindowHandle> {
let ptr = self.notebook_ptr();
if ptr.is_null() {
return None;
}
let page_ptr = unsafe { ffi::wxd_AuiNotebook_GetPage(ptr, page_idx) };
if page_ptr.is_null() {
None
} else {
Some(WindowHandle::new(page_ptr))
}
}
pub fn get_page_index(&self, page: &impl WxWidget) -> i32 {
let ptr = self.notebook_ptr();
if ptr.is_null() {
return -1;
}
unsafe { ffi::wxd_AuiNotebook_GetPageIndex(ptr, page.handle_ptr()) }
}
pub fn get_page_text(&self, page_idx: usize) -> String {
let ptr = self.notebook_ptr();
if ptr.is_null() {
return String::new();
}
let len = unsafe { ffi::wxd_AuiNotebook_GetPageText(ptr, page_idx, std::ptr::null_mut(), 0) };
if len < 0 {
return String::new();
}
let mut buf = vec![0u8; len as usize + 1];
unsafe {
ffi::wxd_AuiNotebook_GetPageText(ptr, page_idx, buf.as_mut_ptr() as *mut _, buf.len());
}
if let Some(last) = buf.last()
&& *last == 0
{
buf.pop();
}
String::from_utf8_lossy(&buf).to_string()
}
pub fn set_page_text(&self, page_idx: usize, text: &str) -> bool {
let ptr = self.notebook_ptr();
if ptr.is_null() {
return false;
}
let text_c = CString::new(text).expect("CString::new failed for text");
unsafe { ffi::wxd_AuiNotebook_SetPageText(ptr, page_idx, text_c.as_ptr()) }
}
pub fn window_handle(&self) -> WindowHandle {
self.handle
}
}
impl WxWidget for AuiNotebook {
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()
}
}
impl WxEvtHandler for AuiNotebook {
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
}
}
impl crate::event::WindowEvents for AuiNotebook {}
widget_builder!(
name: AuiNotebook,
parent_type: &'a dyn WxWidget,
style_type: AuiNotebookStyle,
fields: {},
build_impl: |slf| {
let parent_ptr = slf.parent.handle_ptr();
let ptr = unsafe {
ffi::wxd_AuiNotebook_Create(
parent_ptr,
slf.id,
slf.pos.into(),
slf.size.into(),
slf.style.bits(),
)
};
if ptr.is_null() {
panic!("Failed to create AuiNotebook");
}
AuiNotebook::from_ptr(ptr)
}
);