use std::marker::PhantomData;
use crate::bindgen::{FPDF_ANNOTATION, FPDF_FORMHANDLE};
use crate::error::PdfiumError;
use crate::pdf::document::page::field::private::internal::PdfFormFieldPrivate;
use crate::pdfium::PdfiumLibraryBindingsAccessor;
#[cfg(doc)]
use {
crate::pdf::document::form::PdfForm,
crate::pdf::document::page::annotation::PdfPageAnnotationType,
crate::pdf::document::page::field::{PdfFormField, PdfFormFieldType},
};
pub struct PdfFormCheckboxField<'a> {
form_handle: FPDF_FORMHANDLE,
annotation_handle: FPDF_ANNOTATION,
lifetime: PhantomData<&'a FPDF_ANNOTATION>,
}
impl<'a> PdfFormCheckboxField<'a> {
#[inline]
pub(crate) fn from_pdfium(
form_handle: FPDF_FORMHANDLE,
annotation_handle: FPDF_ANNOTATION,
) -> Self {
PdfFormCheckboxField {
form_handle,
annotation_handle,
lifetime: PhantomData,
}
}
#[inline]
pub fn index_in_group(&self) -> u32 {
self.index_in_group_impl()
}
#[inline]
pub fn group_value(&self) -> Option<String> {
self.value_impl()
}
#[inline]
pub fn is_checked(&self) -> Result<bool, PdfiumError> {
match self.appearance_stream_impl().as_ref() {
Some(appearance_stream) => {
Ok(appearance_stream == "Yes" || appearance_stream == "/Yes")
}
None => {
match self.is_checked_impl() {
Ok(true) => Ok(true),
Ok(false) => match self.group_value() {
Some(value) => Ok(value == "Yes"),
_ => Ok(false),
},
Err(err) => match self.group_value() {
Some(value) => Ok(value == "Yes"),
_ => Err(err),
},
}
}
}
}
#[inline]
pub fn set_checked(&mut self, is_checked: bool) -> Result<(), PdfiumError> {
match self.appearance_stream_impl() {
Some(_) => {
self.set_string_value("AS", if is_checked { "/Yes" } else { "/Off" })?;
self.set_value_impl(if is_checked { "/Yes" } else { "/Off" })
}
None => {
self.set_value_impl(if is_checked { "Yes" } else { "Off" })
}
}
}
}
impl<'a> PdfFormFieldPrivate<'a> for PdfFormCheckboxField<'a> {
#[inline]
fn form_handle(&self) -> FPDF_FORMHANDLE {
self.form_handle
}
#[inline]
fn annotation_handle(&self) -> FPDF_ANNOTATION {
self.annotation_handle
}
}
impl<'a> PdfiumLibraryBindingsAccessor<'a> for PdfFormCheckboxField<'a> {}
#[cfg(feature = "thread_safe")]
unsafe impl<'a> Send for PdfFormCheckboxField<'a> {}
#[cfg(feature = "thread_safe")]
unsafe impl<'a> Sync for PdfFormCheckboxField<'a> {}