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

1//! Defines the [PdfFormListBoxField] struct, exposing functionality related to a single
2//! form field of type [PdfFormFieldType::ListBox].
3
4use crate::bindgen::{FPDF_ANNOTATION, FPDF_FORMHANDLE};
5use crate::bindings::PdfiumLibraryBindings;
6use crate::pdf::document::page::field::options::PdfFormFieldOptions;
7use crate::pdf::document::page::field::private::internal::{
8    PdfFormFieldFlags, PdfFormFieldPrivate,
9};
10
11#[cfg(any(feature = "pdfium_future", feature = "pdfium_7350"))]
12use crate::error::PdfiumError;
13
14#[cfg(doc)]
15use {
16    crate::pdf::document::form::PdfForm,
17    crate::pdf::document::page::annotation::PdfPageAnnotationType,
18    crate::pdf::document::page::field::{PdfFormField, PdfFormFieldType},
19};
20
21/// A single [PdfFormField] of type [PdfFormFieldType::ListBox]. The form field object defines
22/// an interactive drop-down list widget that allows the user to select a value from
23/// a list of options.
24///
25/// Form fields in Pdfium are wrapped inside page annotations of type [PdfPageAnnotationType::Widget]
26/// or [PdfPageAnnotationType::XfaWidget]. User-specified values can be retrieved directly from
27/// each form field object by unwrapping the form field from the annotation, or in bulk from the
28/// [PdfForm::field_values()] function.
29pub struct PdfFormListBoxField<'a> {
30    form_handle: FPDF_FORMHANDLE,
31    annotation_handle: FPDF_ANNOTATION,
32    options: PdfFormFieldOptions<'a>,
33    bindings: &'a dyn PdfiumLibraryBindings,
34}
35
36impl<'a> PdfFormListBoxField<'a> {
37    #[inline]
38    pub(crate) fn from_pdfium(
39        form_handle: FPDF_FORMHANDLE,
40        annotation_handle: FPDF_ANNOTATION,
41        bindings: &'a dyn PdfiumLibraryBindings,
42    ) -> Self {
43        PdfFormListBoxField {
44            form_handle,
45            annotation_handle,
46            options: PdfFormFieldOptions::from_pdfium(form_handle, annotation_handle, bindings),
47            bindings,
48        }
49    }
50
51    /// Returns the [PdfiumLibraryBindings] used by this [PdfFormListBoxField] object.
52    #[inline]
53    pub fn bindings(&self) -> &'a dyn PdfiumLibraryBindings {
54        self.bindings
55    }
56
57    /// Returns the collection of selectable options in this [PdfFormListBoxField].
58    pub fn options(&self) -> &PdfFormFieldOptions {
59        &self.options
60    }
61
62    /// Returns the displayed label for the currently selected option in this [PdfFormListBoxField] object, if any.
63    #[inline]
64    pub fn value(&self) -> Option<String> {
65        self.options()
66            .iter()
67            .find(|option| option.is_set())
68            .and_then(|option| option.label().cloned())
69    }
70
71    /// Returns `true` if the option items of this [PdfFormListBoxField] should be sorted
72    /// alphabetically.
73    ///
74    /// This flag is intended for use by form authoring tools, not by PDF viewer applications.
75    #[inline]
76    pub fn is_sorted(&self) -> bool {
77        self.get_flags_impl()
78            .contains(PdfFormFieldFlags::ChoiceSort)
79    }
80
81    #[cfg(any(feature = "pdfium_future", feature = "pdfium_7350"))]
82    /// Controls whether or not the option items of this [PdfFormListBoxField] should be
83    /// sorted alphabetically.
84    ///
85    /// This flag is intended for use by form authoring tools, not by PDF viewer applications.
86    #[inline]
87    pub fn set_is_sorted(&mut self, is_sorted: bool) -> Result<(), PdfiumError> {
88        self.update_one_flag_impl(PdfFormFieldFlags::ChoiceSort, is_sorted)
89    }
90
91    /// Returns `true` if more than one of the option items in this [PdfFormListBoxField]
92    /// may be selected simultaneously. If `false`, only one item at a time may be selected.
93    ///
94    /// This flag was added in PDF version 1.4.
95    pub fn is_multiselect(&self) -> bool {
96        self.get_flags_impl()
97            .contains(PdfFormFieldFlags::ChoiceMultiSelect)
98    }
99
100    #[cfg(any(feature = "pdfium_future", feature = "pdfium_7350"))]
101    /// Controls whether more than one of the option items in this [PdfFormListBoxField]
102    /// may be selected simultaneously.
103    ///
104    /// This flag was added in PDF version 1.4.
105    pub fn set_is_multiselect(&mut self, is_multiselect: bool) -> Result<(), PdfiumError> {
106        self.update_one_flag_impl(PdfFormFieldFlags::ChoiceMultiSelect, is_multiselect)
107    }
108
109    /// Returns `true` if any new value is committed to this [PdfFormListBoxField]
110    /// as soon as a selection is made with the pointing device. This option enables
111    /// applications to perform an action once a selection is made, without requiring
112    /// the user to exit the field. If `false`, any new value is not committed until the
113    /// user exits the field.
114    ///
115    /// This flag was added in PDF version 1.5.
116    pub fn is_commit_on_selection_change(&self) -> bool {
117        self.get_flags_impl()
118            .contains(PdfFormFieldFlags::ChoiceCommitOnSelectionChange)
119    }
120
121    #[cfg(any(feature = "pdfium_future", feature = "pdfium_7350"))]
122    /// Controls whether or not any new value is committed to this [PdfFormListBoxField]
123    /// as soon as a selection is made with the pointing device.
124    ///
125    /// This flag was added in PDF version 1.5.
126    pub fn set_is_commit_on_selection_change(
127        &mut self,
128        is_commit_on_selection_change: bool,
129    ) -> Result<(), PdfiumError> {
130        self.update_one_flag_impl(
131            PdfFormFieldFlags::ChoiceCommitOnSelectionChange,
132            is_commit_on_selection_change,
133        )
134    }
135}
136
137impl<'a> PdfFormFieldPrivate<'a> for PdfFormListBoxField<'a> {
138    #[inline]
139    fn form_handle(&self) -> FPDF_FORMHANDLE {
140        self.form_handle
141    }
142
143    #[inline]
144    fn annotation_handle(&self) -> FPDF_ANNOTATION {
145        self.annotation_handle
146    }
147
148    #[inline]
149    fn bindings(&self) -> &dyn PdfiumLibraryBindings {
150        self.bindings
151    }
152}