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::bindings::PdfiumLibraryBindings;
6use crate::error::PdfiumError;
7use crate::pdf::document::page::field::private::internal::PdfFormFieldPrivate;
8
9#[cfg(doc)]
10use {
11 crate::pdf::document::form::PdfForm,
12 crate::pdf::document::page::annotation::PdfPageAnnotationType,
13 crate::pdf::document::page::field::{PdfFormField, PdfFormFieldType},
14};
15
16/// A single [PdfFormField] of type [PdfFormFieldType::RadioButton]. The form field object defines
17/// an interactive radio button widget that can be toggled by the user.
18///
19/// Form fields in Pdfium are wrapped inside page annotations of type [PdfPageAnnotationType::Widget]
20/// or [PdfPageAnnotationType::XfaWidget]. User-specified values can be retrieved directly from
21/// each form field object by unwrapping the form field from the annotation, or in bulk from the
22/// [PdfForm::field_values()] function.
23pub struct PdfFormRadioButtonField<'a> {
24 form_handle: FPDF_FORMHANDLE,
25 annotation_handle: FPDF_ANNOTATION,
26 bindings: &'a dyn PdfiumLibraryBindings,
27}
28
29impl<'a> PdfFormRadioButtonField<'a> {
30 #[inline]
31 pub(crate) fn from_pdfium(
32 form_handle: FPDF_FORMHANDLE,
33 annotation_handle: FPDF_ANNOTATION,
34 bindings: &'a dyn PdfiumLibraryBindings,
35 ) -> Self {
36 PdfFormRadioButtonField {
37 form_handle,
38 annotation_handle,
39 bindings,
40 }
41 }
42
43 /// Returns the [PdfiumLibraryBindings] used by this [PdfFormRadioButtonField] object.
44 #[inline]
45 pub fn bindings(&self) -> &'a dyn PdfiumLibraryBindings {
46 self.bindings
47 }
48
49 /// Returns the index of this [PdfFormRadioButtonField] in its control group.
50 ///
51 /// Control groups are used to group related interactive fields together. Checkboxes and
52 /// radio buttons can be grouped such that only a single button can be selected within
53 /// the control group. Each field within the group has a unique group index.
54 #[inline]
55 pub fn index_in_group(&self) -> u32 {
56 self.index_in_group_impl()
57 }
58
59 /// Returns the value set for the control group containing this [PdfFormRadioButtonField].
60 ///
61 /// Control groups are used to group related interactive fields together. Checkboxes and
62 /// radio buttons can be grouped such that only a single button can be selected within
63 /// the control group. In this case, a single value can be shared by the group, indicating
64 /// the value of the currently selected field within the group.
65 #[inline]
66 pub fn group_value(&self) -> Option<String> {
67 self.value_impl()
68 }
69
70 /// Returns `true` if this [PdfFormRadioButtonField] object has its radio button selected.
71 #[inline]
72 pub fn is_checked(&self) -> Result<bool, PdfiumError> {
73 // The PDF Reference manual, version 1.7, states that a selected radio button can indicate
74 // its selected appearance by setting a custom appearance stream; this appearance stream
75 // value will then become the group value. Pdfium's FPDFAnnot_IsChecked()
76 // function doesn't check for this; so if FPDFAnnot_IsChecked() comes back with
77 // anything other than Ok(true), we also check the appearance stream.
78
79 match self.is_checked_impl() {
80 Ok(true) => Ok(true),
81 Ok(false) => Ok(self.group_value() == self.appearance_stream_impl()),
82 Err(err) => match self.group_value() {
83 None => Err(err),
84 group_value => Ok(group_value == self.appearance_stream_impl()),
85 },
86 }
87 }
88
89 /// Selects the radio button of this [PdfFormRadioButtonField] object.
90 #[inline]
91 pub fn set_checked(&mut self) -> Result<(), PdfiumError> {
92 match self.appearance_stream_impl() {
93 Some(appearance_stream) => self.set_value_impl(appearance_stream.as_str()),
94 None => Err(PdfiumError::FormFieldAppearanceStreamUndefined),
95 }
96 }
97}
98
99impl<'a> PdfFormFieldPrivate<'a> for PdfFormRadioButtonField<'a> {
100 #[inline]
101 fn form_handle(&self) -> &FPDF_FORMHANDLE {
102 &self.form_handle
103 }
104
105 #[inline]
106 fn annotation_handle(&self) -> &FPDF_ANNOTATION {
107 &self.annotation_handle
108 }
109
110 #[inline]
111 fn bindings(&self) -> &dyn PdfiumLibraryBindings {
112 self.bindings
113 }
114}