Skip to main content

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