Skip to main content

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

1//! Defines the [PdfFormComboBoxField] struct, exposing functionality related to a single
2//! form field of type [PdfFormFieldType::ComboBox].
3
4use std::marker::PhantomData;
5
6use crate::bindgen::{FPDF_ANNOTATION, FPDF_FORMHANDLE};
7use crate::pdf::document::page::field::options::PdfFormFieldOptions;
8use crate::pdf::document::page::field::private::internal::{
9    PdfFormFieldFlags, PdfFormFieldPrivate,
10};
11use crate::pdfium::PdfiumLibraryBindingsAccessor;
12
13#[cfg(any(
14    feature = "pdfium_future",
15    feature = "pdfium_8125",
16    feature = "pdfium_7881",
17    feature = "pdfium_7763",
18    feature = "pdfium_7543",
19    feature = "pdfium_7350"
20))]
21use crate::error::PdfiumError;
22
23#[cfg(doc)]
24use {
25    crate::pdf::document::form::PdfForm,
26    crate::pdf::document::page::annotation::PdfPageAnnotationType,
27    crate::pdf::document::page::field::{PdfFormField, PdfFormFieldType},
28};
29
30/// A single [PdfFormField] of type [PdfFormFieldType::ComboBox]. The form field object defines
31/// an interactive drop-down list widget that allows the user to either select a value
32/// from a list of options or type a value into a text field.
33///
34/// Form fields in Pdfium are wrapped inside page annotations of type [PdfPageAnnotationType::Widget]
35/// or [PdfPageAnnotationType::XfaWidget]. User-specified values can be retrieved directly from
36/// each form field object by unwrapping the form field from the annotation, or in bulk from the
37/// [PdfForm::field_values()] function.
38pub struct PdfFormComboBoxField<'a> {
39    form_handle: FPDF_FORMHANDLE,
40    annotation_handle: FPDF_ANNOTATION,
41    options: PdfFormFieldOptions<'a>,
42    lifetime: PhantomData<&'a FPDF_ANNOTATION>,
43}
44
45impl<'a> PdfFormComboBoxField<'a> {
46    #[inline]
47    pub(crate) fn from_pdfium(
48        form_handle: FPDF_FORMHANDLE,
49        annotation_handle: FPDF_ANNOTATION,
50    ) -> Self {
51        PdfFormComboBoxField {
52            form_handle,
53            annotation_handle,
54            options: PdfFormFieldOptions::from_pdfium(form_handle, annotation_handle),
55            lifetime: PhantomData,
56        }
57    }
58
59    /// Returns the collection of selectable options in this [PdfFormComboBoxField].
60    pub fn options(&self) -> &PdfFormFieldOptions<'_> {
61        &self.options
62    }
63
64    /// Returns the displayed label for the currently selected option in this [PdfFormComboBoxField] object, if any.
65    #[inline]
66    pub fn value(&self) -> Option<String> {
67        self.options()
68            .iter()
69            .find(|option| option.is_set())
70            .and_then(|option| option.label().cloned())
71    }
72
73    /// Returns `true` if this [PdfFormComboBoxField] also includes an editable text box.
74    /// If `false`, this combo box field only includes a drop-down list.
75    #[inline]
76    pub fn has_editable_text_box(&self) -> bool {
77        self.get_flags_impl()
78            .contains(PdfFormFieldFlags::ChoiceEdit)
79    }
80
81    #[cfg(any(
82        feature = "pdfium_future",
83        feature = "pdfium_8125",
84        feature = "pdfium_7881",
85        feature = "pdfium_7763",
86        feature = "pdfium_7543",
87        feature = "pdfium_7350"
88    ))]
89    /// Controls whether or not this [PdfFormComboBoxField] includes an editable text box
90    /// in addition to a drop-down list.
91    #[inline]
92    pub fn set_has_editable_text_box(
93        &mut self,
94        has_editable_text_box: bool,
95    ) -> Result<(), PdfiumError> {
96        self.update_one_flag_impl(PdfFormFieldFlags::ChoiceEdit, has_editable_text_box)
97    }
98
99    /// Returns `true` if the option items of this [PdfFormComboBoxField] should be sorted
100    /// alphabetically.
101    ///
102    /// This flag is intended for use by form authoring tools, not by PDF viewer applications.
103    #[inline]
104    pub fn is_sorted(&self) -> bool {
105        self.get_flags_impl()
106            .contains(PdfFormFieldFlags::ChoiceSort)
107    }
108
109    #[cfg(any(
110        feature = "pdfium_future",
111        feature = "pdfium_8125",
112        feature = "pdfium_7881",
113        feature = "pdfium_7763",
114        feature = "pdfium_7543",
115        feature = "pdfium_7350"
116    ))]
117    /// Controls whether or not the option items of this [PdfFormComboBoxField] should be
118    /// sorted alphabetically.
119    ///
120    /// This flag is intended for use by form authoring tools, not by PDF viewer applications.
121    #[inline]
122    pub fn set_is_sorted(&mut self, is_sorted: bool) -> Result<(), PdfiumError> {
123        self.update_one_flag_impl(PdfFormFieldFlags::ChoiceSort, is_sorted)
124    }
125
126    /// Returns `true` if more than one of the option items in this [PdfFormComboBoxField]
127    /// may be selected simultaneously. If `false`, only one item at a time may be selected.
128    ///
129    /// This flag was added in PDF version 1.4.
130    pub fn is_multiselect(&self) -> bool {
131        self.get_flags_impl()
132            .contains(PdfFormFieldFlags::ChoiceMultiSelect)
133    }
134
135    #[cfg(any(
136        feature = "pdfium_future",
137        feature = "pdfium_8125",
138        feature = "pdfium_7881",
139        feature = "pdfium_7763",
140        feature = "pdfium_7543",
141        feature = "pdfium_7350"
142    ))]
143    /// Controls whether more than one of the option items in this [PdfFormComboBoxField]
144    /// may be selected simultaneously.
145    ///
146    /// This flag was added in PDF version 1.4.
147    pub fn set_is_multiselect(&mut self, is_multiselect: bool) -> Result<(), PdfiumError> {
148        self.update_one_flag_impl(PdfFormFieldFlags::ChoiceMultiSelect, is_multiselect)
149    }
150
151    /// Returns `true` if text entered into the editable text box included in this
152    /// [PdfFormComboBoxField] should be spell checked.
153    ///
154    /// This flag is meaningful only if the [PdfFormComboBoxField::has_editable_text_box()]
155    /// flag is also `true`.
156    ///
157    /// This flag was added in PDF version 1.4.
158    pub fn is_spell_checked(&self) -> bool {
159        !self
160            .get_flags_impl()
161            .contains(PdfFormFieldFlags::TextDoNotSpellCheck)
162    }
163
164    #[cfg(any(
165        feature = "pdfium_future",
166        feature = "pdfium_8125",
167        feature = "pdfium_7881",
168        feature = "pdfium_7763",
169        feature = "pdfium_7543",
170        feature = "pdfium_7350"
171    ))]
172    /// Controls whether or not text entered into the editable text box included in this
173    /// [PdfFormComboBoxField] should be spell checked.
174    ///
175    /// This flag was added in PDF version 1.4.
176    pub fn set_is_spell_checked(&mut self, is_spell_checked: bool) -> Result<(), PdfiumError> {
177        self.update_one_flag_impl(PdfFormFieldFlags::TextDoNotSpellCheck, !is_spell_checked)
178    }
179
180    /// Returns `true` if any new value is committed to this [PdfFormComboBoxField]
181    /// as soon as a selection is made with the pointing device. This option enables
182    /// applications to perform an action once a selection is made, without requiring
183    /// the user to exit the field. If `false`, any new value is not committed until the
184    /// user exits the field.
185    ///
186    /// This flag was added in PDF version 1.5.
187    pub fn is_commit_on_selection_change(&self) -> bool {
188        self.get_flags_impl()
189            .contains(PdfFormFieldFlags::ChoiceCommitOnSelectionChange)
190    }
191
192    #[cfg(any(
193        feature = "pdfium_future",
194        feature = "pdfium_8125",
195        feature = "pdfium_7881",
196        feature = "pdfium_7763",
197        feature = "pdfium_7543",
198        feature = "pdfium_7350"
199    ))]
200    /// Controls whether or not any new value is committed to this [PdfFormComboBoxField]
201    /// as soon as a selection is made with the pointing device.
202    ///
203    /// This flag was added in PDF version 1.5.
204    pub fn set_is_commit_on_selection_change(
205        &mut self,
206        is_commit_on_selection_change: bool,
207    ) -> Result<(), PdfiumError> {
208        self.update_one_flag_impl(
209            PdfFormFieldFlags::ChoiceCommitOnSelectionChange,
210            is_commit_on_selection_change,
211        )
212    }
213}
214
215impl<'a> PdfFormFieldPrivate<'a> for PdfFormComboBoxField<'a> {
216    #[inline]
217    fn form_handle(&self) -> FPDF_FORMHANDLE {
218        self.form_handle
219    }
220
221    #[inline]
222    fn annotation_handle(&self) -> FPDF_ANNOTATION {
223        self.annotation_handle
224    }
225}
226
227impl<'a> PdfiumLibraryBindingsAccessor<'a> for PdfFormComboBoxField<'a> {}
228
229#[cfg(feature = "thread_safe")]
230unsafe impl<'a> Send for PdfFormComboBoxField<'a> {}
231
232#[cfg(feature = "thread_safe")]
233unsafe impl<'a> Sync for PdfFormComboBoxField<'a> {}