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