use crate::bindgen::{FPDF_ANNOTATION, FPDF_FORMHANDLE};
use crate::error::PdfiumError;
use crate::pdf::document::page::field::private::internal::{
PdfFormFieldFlags, PdfFormFieldPrivate,
};
use crate::pdfium::PdfiumLibraryBindingsAccessor;
use std::marker::PhantomData;
#[cfg(doc)]
use {
crate::pdf::document::form::PdfForm,
crate::pdf::document::page::annotation::PdfPageAnnotationType,
crate::pdf::document::page::field::{PdfFormField, PdfFormFieldType},
};
pub struct PdfFormRadioButtonField<'a> {
form_handle: FPDF_FORMHANDLE,
annotation_handle: FPDF_ANNOTATION,
lifetime: PhantomData<&'a FPDF_ANNOTATION>,
}
impl<'a> PdfFormRadioButtonField<'a> {
#[inline]
pub(crate) fn from_pdfium(
form_handle: FPDF_FORMHANDLE,
annotation_handle: FPDF_ANNOTATION,
) -> Self {
PdfFormRadioButtonField {
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.is_checked_impl() {
Ok(true) => Ok(true),
Ok(false) => Ok(self.group_value() == self.appearance_stream_impl()),
Err(err) => match self.group_value() {
None => Err(err),
group_value => Ok(group_value == self.appearance_stream_impl()),
},
}
}
#[inline]
pub fn set_checked(&mut self) -> Result<(), PdfiumError> {
match self.appearance_stream_impl() {
Some(appearance_stream) => self.set_value_impl(appearance_stream.as_str()),
None => Err(PdfiumError::FormFieldAppearanceStreamUndefined),
}
}
#[inline]
pub fn is_group_selection_required(&self) -> bool {
!self
.get_flags_impl()
.contains(PdfFormFieldFlags::ButtonNoToggleToOff)
}
#[cfg(any(feature = "pdfium_future", feature = "pdfium_7350"))]
#[inline]
pub fn set_is_group_selection_required(
&mut self,
is_group_selection_required: bool,
) -> Result<(), PdfiumError> {
self.update_one_flag_impl(
PdfFormFieldFlags::ButtonNoToggleToOff,
!is_group_selection_required,
)
}
#[inline]
pub fn is_group_in_unison(&self) -> bool {
self.get_flags_impl()
.contains(PdfFormFieldFlags::ButtonIsRadiosInUnison)
}
#[cfg(any(feature = "pdfium_future", feature = "pdfium_7350"))]
#[inline]
pub fn set_is_group_in_unison(&mut self, is_group_in_unison: bool) -> Result<(), PdfiumError> {
self.update_one_flag_impl(
PdfFormFieldFlags::ButtonIsRadiosInUnison,
is_group_in_unison,
)
}
}
impl<'a> PdfFormFieldPrivate<'a> for PdfFormRadioButtonField<'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 PdfFormRadioButtonField<'a> {}
#[cfg(feature = "thread_safe")]
unsafe impl<'a> Send for PdfFormRadioButtonField<'a> {}
#[cfg(feature = "thread_safe")]
unsafe impl<'a> Sync for PdfFormRadioButtonField<'a> {}