use crate::ffi;
use crate::signal::SignalHandle;
use crate::widget::AsWidget;
pub const SCROLL_BAR_ALWAYS_ON: i32 = 1;
pub const SCROLL_BAR_ALWAYS_OFF: i32 = 0;
pub const SCROLL_BAR_AS_NEEDED: i32 = 2;
pub struct ScrollArea {
ptr: *mut ffi::QScrollArea,
has_parent: bool,
signal_handles: Vec<SignalHandle>,
}
impl ScrollArea {
pub fn new() -> Builder { Builder::new() }
pub fn set_widget(&self, w: &dyn AsWidget) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QScrollArea_setWidget(self.ptr, w.widget_ptr()); }
}
pub fn set_widget_resizable(&self, resizable: bool) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QScrollArea_setWidgetResizable(self.ptr, resizable); }
}
pub fn set_horizontal_scroll_bar_policy(&self, policy: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QScrollArea_setHorizontalScrollBarPolicy(self.ptr, policy); }
}
pub fn set_vertical_scroll_bar_policy(&self, policy: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QScrollArea_setVerticalScrollBarPolicy(self.ptr, policy); }
}
pub fn ensure_visible(&self, x: i32, y: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QScrollArea_ensureVisible(self.ptr, x, y); }
}
pub fn ensure_widget_visible(&self, w: &dyn AsWidget) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QScrollArea_ensureWidgetVisible(self.ptr, w.widget_ptr()); }
}
#[doc(hidden)]
pub(crate) fn from_raw(ptr: *mut ffi::QScrollArea) -> Self {
debug_assert!(!ptr.is_null());
Self { ptr, has_parent: true, signal_handles: Vec::new() }
}
}
impl AsWidget for ScrollArea {
fn widget_ptr(&self) -> *mut ffi::QWidget {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::toQWidget_QScrollArea(self.ptr) }
}
fn set_has_parent(&mut self) { self.has_parent = true; }
}
impl Drop for ScrollArea {
fn drop(&mut self) {
if self.ptr.is_null() { return; }
if self.has_parent {
unsafe { ffi::QWidget_disconnectAll(self.ptr as *mut _); }
for h in self.signal_handles.drain(..) { unsafe { h.reclaim(); } }
} else {
for h in self.signal_handles.drain(..) { unsafe { h.reclaim(); } }
unsafe { ffi::QScrollArea_delete(self.ptr) };
}
self.ptr = std::ptr::null_mut();
}
}
pub struct Builder {
widget_resizable: Option<bool>,
parent: Option<*mut ffi::QWidget>,
}
impl Builder {
fn new() -> Self {
Self { widget_resizable: None, parent: None }
}
pub fn set_widget_resizable(mut self, resizable: bool) -> Self {
self.widget_resizable = Some(resizable);
self
}
pub fn parent(mut self, parent: &dyn AsWidget) -> Self {
self.parent = Some(parent.widget_ptr());
self
}
pub fn build(self) -> ScrollArea {
let ptr = unsafe {
ffi::QScrollArea_new(self.parent.unwrap_or(std::ptr::null_mut()))
};
debug_assert!(!ptr.is_null());
let sa = ScrollArea {
ptr,
has_parent: self.parent.is_some(),
signal_handles: Vec::new(),
};
if let Some(resizable) = self.widget_resizable {
unsafe { ffi::QScrollArea_setWidgetResizable(ptr, resizable); }
}
sa
}
}