Skip to main content

pdfium_render/pdf/document/page/field/
radio.rs

1//! Defines the [PdfFormRadioButtonField] struct, exposing functionality related to a single
2//! form field of type [PdfFormFieldType::RadioButton].
3
4use crate::bindgen::{FPDF_ANNOTATION, FPDF_FORMHANDLE};
5use crate::error::PdfiumError;
6use crate::pdf::document::page::field::private::internal::{
7    PdfFormFieldFlags, PdfFormFieldPrivate,
8};
9use crate::pdfium::PdfiumLibraryBindingsAccessor;
10use std::marker::PhantomData;
11
12#[cfg(doc)]
13use {
14    crate::pdf::document::form::PdfForm,
15    crate::pdf::document::page::annotation::PdfPageAnnotationType,
16    crate::pdf::document::page::field::{PdfFormField, PdfFormFieldType},
17};
18
19/// A single [PdfFormField] of type [PdfFormFieldType::RadioButton]. The form field object defines
20/// an interactive radio button widget that can be toggled by the user.
21///
22/// Form fields in Pdfium are wrapped inside page annotations of type [PdfPageAnnotationType::Widget]
23/// or [PdfPageAnnotationType::XfaWidget]. User-specified values can be retrieved directly from
24/// each form field object by unwrapping the form field from the annotation, or in bulk from the
25/// [PdfForm::field_values()] function.
26pub struct PdfFormRadioButtonField<'a> {
27    form_handle: FPDF_FORMHANDLE,
28    annotation_handle: FPDF_ANNOTATION,
29    lifetime: PhantomData<&'a FPDF_ANNOTATION>,
30}
31
32impl<'a> PdfFormRadioButtonField<'a> {
33    #[inline]
34    pub(crate) fn from_pdfium(
35        form_handle: FPDF_FORMHANDLE,
36        annotation_handle: FPDF_ANNOTATION,
37    ) -> Self {
38        PdfFormRadioButtonField {
39            form_handle,
40            annotation_handle,
41            lifetime: PhantomData,
42        }
43    }
44
45    /// Returns the index of this [PdfFormRadioButtonField] in its control group.
46    ///
47    /// Control groups are used to group related interactive fields together. Checkboxes and
48    /// radio buttons can be grouped such that only a single button can be selected within
49    /// the control group. Each field within the group has a unique group index.
50    #[inline]
51    pub fn index_in_group(&self) -> u32 {
52        self.index_in_group_impl()
53    }
54
55    /// Returns the value set for the control group containing this [PdfFormRadioButtonField].
56    ///
57    /// Control groups are used to group related interactive fields together. Checkboxes and
58    /// radio buttons can be grouped such that only a single button can be selected within
59    /// the control group. In this case, a single value can be shared by the group, indicating
60    /// the value of the currently selected field within the group.
61    #[inline]
62    pub fn group_value(&self) -> Option<String> {
63        self.value_impl()
64    }
65
66    /// Returns `true` if this [PdfFormRadioButtonField] object has its radio button selected.
67    #[inline]
68    pub fn is_checked(&self) -> Result<bool, PdfiumError> {
69        // The PDF Reference manual, version 1.7, states that a selected radio button can indicate
70        // its selected appearance by setting a custom appearance stream; this appearance stream
71        // value will then become the group value. Pdfium's FPDFAnnot_IsChecked()
72        // function doesn't check for this; so if FPDFAnnot_IsChecked() comes back with
73        // anything other than Ok(true), we also check the appearance stream.
74
75        match self.is_checked_impl() {
76            Ok(true) => Ok(true),
77            Ok(false) => Ok(self.group_value() == self.appearance_stream_impl()),
78            Err(err) => match self.group_value() {
79                None => Err(err),
80                group_value => Ok(group_value == self.appearance_stream_impl()),
81            },
82        }
83    }
84
85    /// Selects the radio button of this [PdfFormRadioButtonField] object.
86    #[inline]
87    pub fn set_checked(&mut self) -> Result<(), PdfiumError> {
88        match self.appearance_stream_impl() {
89            Some(appearance_stream) => self.set_value_impl(appearance_stream.as_str()),
90            None => Err(PdfiumError::FormFieldAppearanceStreamUndefined),
91        }
92    }
93
94    /// Returns `true` if exactly one radio button in the control group containing this
95    /// [PdfFormRadioButtonField] must be selected at all times. If so, then toggling the
96    /// currently selected radio button is not possible. If `false`, then toggling the
97    /// currently selected radio button will deselect it, leaving no radio button in the
98    /// group selected.
99    #[inline]
100    pub fn is_group_selection_required(&self) -> bool {
101        !self
102            .get_flags_impl()
103            .contains(PdfFormFieldFlags::ButtonNoToggleToOff)
104    }
105
106    #[cfg(any(
107        feature = "pdfium_future",
108        feature = "pdfium_8125",
109        feature = "pdfium_7881",
110        feature = "pdfium_7763",
111        feature = "pdfium_7543",
112        feature = "pdfium_7350"
113    ))]
114    /// Controls whether or not the control group containing this [PdfFormRadioButtonField]
115    /// requires exactly one radio button to be selected at all times.
116    #[inline]
117    pub fn set_is_group_selection_required(
118        &mut self,
119        is_group_selection_required: bool,
120    ) -> Result<(), PdfiumError> {
121        self.update_one_flag_impl(
122            PdfFormFieldFlags::ButtonNoToggleToOff,
123            !is_group_selection_required,
124        )
125    }
126
127    /// Returns `true` if all radio buttons in the same control group as this
128    /// [PdfFormRadioButtonField] use the same value for the checked state; if so, if one
129    /// is checked, then all will be checked, and so all radio buttons will turn on and
130    /// off in unison.
131    ///
132    /// This flag was added in PDF version 1.5.
133    #[inline]
134    pub fn is_group_in_unison(&self) -> bool {
135        self.get_flags_impl()
136            .contains(PdfFormFieldFlags::ButtonIsRadiosInUnison)
137    }
138
139    #[cfg(any(
140        feature = "pdfium_future",
141        feature = "pdfium_8125",
142        feature = "pdfium_7881",
143        feature = "pdfium_7763",
144        feature = "pdfium_7543",
145        feature = "pdfium_7350"
146    ))]
147    /// Controls whether or not all radio buttons in the same control group as this
148    /// [PdfFormRadioButtonField] use the same value for the checked state; if so, if one
149    /// is checked, then all will be checked, and so all radio buttons will turn on and
150    /// off in unison.
151    ///
152    /// This flag was added in PDF version 1.5.
153    #[inline]
154    pub fn set_is_group_in_unison(&mut self, is_group_in_unison: bool) -> Result<(), PdfiumError> {
155        self.update_one_flag_impl(
156            PdfFormFieldFlags::ButtonIsRadiosInUnison,
157            is_group_in_unison,
158        )
159    }
160}
161
162impl<'a> PdfFormFieldPrivate<'a> for PdfFormRadioButtonField<'a> {
163    #[inline]
164    fn form_handle(&self) -> FPDF_FORMHANDLE {
165        self.form_handle
166    }
167
168    #[inline]
169    fn annotation_handle(&self) -> FPDF_ANNOTATION {
170        self.annotation_handle
171    }
172}
173
174impl<'a> PdfiumLibraryBindingsAccessor<'a> for PdfFormRadioButtonField<'a> {}
175
176#[cfg(feature = "thread_safe")]
177unsafe impl<'a> Send for PdfFormRadioButtonField<'a> {}
178
179#[cfg(feature = "thread_safe")]
180unsafe impl<'a> Sync for PdfFormRadioButtonField<'a> {}