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