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