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