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_7763",
109 feature = "pdfium_7543",
110 feature = "pdfium_7350"
111 ))]
112 /// Controls whether or not the control group containing this [PdfFormRadioButtonField]
113 /// requires exactly one radio button to be selected at all times.
114 #[inline]
115 pub fn set_is_group_selection_required(
116 &mut self,
117 is_group_selection_required: bool,
118 ) -> Result<(), PdfiumError> {
119 self.update_one_flag_impl(
120 PdfFormFieldFlags::ButtonNoToggleToOff,
121 !is_group_selection_required,
122 )
123 }
124
125 /// Returns `true` if all radio buttons in the same control group as this
126 /// [PdfFormRadioButtonField] use the same value for the checked state; if so, if one
127 /// is checked, then all will be checked, and so all radio buttons will turn on and
128 /// off in unison.
129 ///
130 /// This flag was added in PDF version 1.5.
131 #[inline]
132 pub fn is_group_in_unison(&self) -> bool {
133 self.get_flags_impl()
134 .contains(PdfFormFieldFlags::ButtonIsRadiosInUnison)
135 }
136
137 #[cfg(any(
138 feature = "pdfium_future",
139 feature = "pdfium_7763",
140 feature = "pdfium_7543",
141 feature = "pdfium_7350"
142 ))]
143 /// Controls whether or not all radio buttons in the same control group as this
144 /// [PdfFormRadioButtonField] use the same value for the checked state; if so, if one
145 /// is checked, then all will be checked, and so all radio buttons will turn on and
146 /// off in unison.
147 ///
148 /// This flag was added in PDF version 1.5.
149 #[inline]
150 pub fn set_is_group_in_unison(&mut self, is_group_in_unison: bool) -> Result<(), PdfiumError> {
151 self.update_one_flag_impl(
152 PdfFormFieldFlags::ButtonIsRadiosInUnison,
153 is_group_in_unison,
154 )
155 }
156}
157
158impl<'a> PdfFormFieldPrivate<'a> for PdfFormRadioButtonField<'a> {
159 #[inline]
160 fn form_handle(&self) -> FPDF_FORMHANDLE {
161 self.form_handle
162 }
163
164 #[inline]
165 fn annotation_handle(&self) -> FPDF_ANNOTATION {
166 self.annotation_handle
167 }
168}
169
170impl<'a> PdfiumLibraryBindingsAccessor<'a> for PdfFormRadioButtonField<'a> {}
171
172#[cfg(feature = "thread_safe")]
173unsafe impl<'a> Send for PdfFormRadioButtonField<'a> {}
174
175#[cfg(feature = "thread_safe")]
176unsafe impl<'a> Sync for PdfFormRadioButtonField<'a> {}