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