1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <com/sun/star/text/XTextContent.hpp>
23 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <cppuhelper/weakref.hxx>
26 #include <sal/types.h>
27 #include <svl/poolitem.hxx>
28 #include <unotools/weakref.hxx>
37 class SwContentControl
;
38 class SwTextContentControl
;
40 class SwXContentControl
;
42 enum class SwContentControlType
53 /// SfxPoolItem subclass that wraps an SwContentControl.
54 class SW_DLLPUBLIC SwFormatContentControl final
: public SfxPoolItem
56 std::shared_ptr
<SwContentControl
> m_pContentControl
;
57 SwTextContentControl
* m_pTextAttr
;
60 SwTextContentControl
* GetTextAttr() { return m_pTextAttr
; }
61 void SetTextAttr(SwTextContentControl
* pTextAttr
);
63 /// This method must be called when the hint is actually copied.
64 void DoCopy(SwTextNode
& rTargetTextNode
);
66 explicit SwFormatContentControl(sal_uInt16 nWhich
);
68 explicit SwFormatContentControl(const std::shared_ptr
<SwContentControl
>& pContentControl
,
70 ~SwFormatContentControl() override
;
73 bool operator==(const SfxPoolItem
&) const override
;
74 SwFormatContentControl
* Clone(SfxItemPool
* pPool
= nullptr) const override
;
77 * Notify clients registered at m_pContentControl that this content control is being
80 void NotifyChangeTextNode(SwTextNode
* pTextNode
);
81 SwTextNode
* GetTextNode() const;
82 static SwFormatContentControl
* CreatePoolDefault(sal_uInt16 nWhich
);
83 const std::shared_ptr
<SwContentControl
>& GetContentControl() const { return m_pContentControl
; }
85 void dumpAsXml(xmlTextWriterPtr pWriter
) const override
;
88 /// Represents one list item in a content control dropdown list.
89 class SW_DLLPUBLIC SwContentControlListItem
92 /// This may be empty, ToString() falls back to m_aValue.
93 OUString m_aDisplayText
;
94 /// This must not be empty.
97 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
99 const OUString
& ToString() const;
101 bool operator==(const SwContentControlListItem
& rOther
) const;
103 static void ItemsToAny(const std::vector
<SwContentControlListItem
>& rItems
,
104 css::uno::Any
& rVal
);
106 static std::vector
<SwContentControlListItem
> ItemsFromAny(const css::uno::Any
& rVal
);
109 /// Stores the properties of a content control.
110 class SW_DLLPUBLIC SwContentControl final
: public sw::BroadcastingModify
112 unotools::WeakReference
<SwXContentControl
> m_wXContentControl
;
114 SwFormatContentControl
* m_pFormat
;
116 /// Can be nullptr if not in a document for undo purposes.
117 SwTextNode
* m_pTextNode
;
119 /// Current content is placeholder text.
120 bool m_bShowingPlaceHolder
= false;
122 /// Display the content control as a checkbox.
123 bool m_bCheckbox
= false;
125 /// If m_bCheckbox is true, is the checkbox checked?
126 bool m_bChecked
= false;
128 /// If m_bCheckbox is true, the value of a checked checkbox.
129 OUString m_aCheckedState
;
131 /// If m_bCheckbox is true, the value of an unchecked checkbox.
132 OUString m_aUncheckedState
;
134 std::vector
<SwContentControlListItem
> m_aListItems
;
136 bool m_bPicture
= false;
138 bool m_bDate
= false;
140 /// If m_bDate is true, the date format in a syntax accepted by SvNumberFormatter::PutEntry().
141 OUString m_aDateFormat
;
143 /// If m_bDate is true, the date's BCP 47 language tag.
144 OUString m_aDateLanguage
;
146 /// Date in YYYY-MM-DDT00:00:00Z format.
147 OUString m_aCurrentDate
;
149 /// Plain text, i.e. not rich text.
150 bool m_bPlainText
= false;
152 /// Same as drop-down, but free-form input is also accepted.
153 bool m_bComboBox
= false;
155 /// Same as combo box, but free-form input is not accepted.
156 bool m_bDropDown
= false;
158 /// The placeholder's doc part: just remembered.
159 OUString m_aPlaceholderDocPart
;
161 /// The data bindings's prefix mappings: just remembered.
162 OUString m_aDataBindingPrefixMappings
;
164 /// The data bindings's XPath: just remembered.
165 OUString m_aDataBindingXpath
;
167 /// The data bindings's store item ID: just remembered.
168 OUString m_aDataBindingStoreItemID
;
170 /// The color: just remembered.
173 /// The appearance: just remembered.
174 OUString m_aAppearance
;
176 /// The alias: just remembered.
179 /// The tag: just remembered.
182 /// The id: just remembered.
185 /// The tabIndex: just remembered.
186 sal_uInt32 m_nTabIndex
= 0;
188 /// The control and content locks: mostly just remembered.
191 /// Stores a list item index, in case the doc model is not yet updated.
192 // i.e. temporarily store the selected item until the text is inserted by GotoContentControl.
193 std::optional
<size_t> m_oSelectedListItem
;
195 /// Stores a date timestamp, in case the doc model is not yet updated.
196 // i.e. temporarily store the date until the text is inserted by GotoContentControl.
197 std::optional
<double> m_oSelectedDate
;
200 * E.g. checkbox is read-only by default, but we still update contents on interaction
201 * internally. This flag is true for the duration of that interaction.
203 bool m_bReadWrite
= false;
206 SwTextContentControl
* GetTextAttr() const;
208 SwTextNode
* GetTextNode() const { return m_pTextNode
; }
210 SwFormatContentControl
* GetFormatContentControl() const { return m_pFormat
; }
212 void SetFormatContentControl(SwFormatContentControl
* pFormat
) { m_pFormat
= pFormat
; };
214 void NotifyChangeTextNode(SwTextNode
* pTextNode
);
216 const unotools::WeakReference
<SwXContentControl
>& GetXContentControl() const
218 return m_wXContentControl
;
221 void SetXContentControl(const rtl::Reference
<SwXContentControl
>& xContentControl
);
223 virtual void SwClientNotify(const SwModify
&, const SfxHint
&) override
;
225 explicit SwContentControl(SwFormatContentControl
* pFormat
);
227 virtual ~SwContentControl() override
;
229 void SetShowingPlaceHolder(bool bShowingPlaceHolder
)
231 m_bShowingPlaceHolder
= bShowingPlaceHolder
;
234 bool GetShowingPlaceHolder() const { return m_bShowingPlaceHolder
; }
236 void SetCheckbox(bool bCheckbox
) { m_bCheckbox
= bCheckbox
; }
238 bool GetCheckbox() const { return m_bCheckbox
; }
240 void SetChecked(bool bChecked
) { m_bChecked
= bChecked
; }
242 bool GetChecked() const { return m_bChecked
; }
244 void SetCheckedState(const OUString
& rCheckedState
) { m_aCheckedState
= rCheckedState
; }
246 const OUString
& GetCheckedState() const { return m_aCheckedState
; }
248 void SetUncheckedState(const OUString
& rUncheckedState
) { m_aUncheckedState
= rUncheckedState
; }
250 const OUString
& GetUncheckedState() const { return m_aUncheckedState
; }
252 const std::vector
<SwContentControlListItem
>& GetListItems() const { return m_aListItems
; }
254 void SetListItems(const std::vector
<SwContentControlListItem
>& rListItems
)
256 m_aListItems
= rListItems
;
259 bool AddListItem(size_t nZIndex
, const OUString
& rDisplayText
, const OUString
& rValue
);
260 void DeleteListItem(size_t nZIndex
);
261 void ClearListItems();
263 void SetPicture(bool bPicture
) { m_bPicture
= bPicture
; }
265 bool GetPicture() const { return m_bPicture
; }
267 void SetDate(bool bDate
) { m_bDate
= bDate
; }
269 bool GetDate() const { return m_bDate
; }
271 void SetDateFormat(const OUString
& rDateFormat
) { m_aDateFormat
= rDateFormat
; }
273 const OUString
& GetDateFormat() const { return m_aDateFormat
; }
275 void SetDateLanguage(const OUString
& rDateLanguage
) { m_aDateLanguage
= rDateLanguage
; }
277 const OUString
& GetDateLanguage() const { return m_aDateLanguage
; }
279 void SetCurrentDate(const OUString
& rCurrentDate
) { m_aCurrentDate
= rCurrentDate
; }
281 const OUString
& GetCurrentDate() const { return m_aCurrentDate
; }
283 /// Formats fCurrentDate and sets it.
284 void SetCurrentDateValue(double fCurrentDate
);
286 /// Parses m_aCurrentDate and returns it.
287 double GetCurrentDateValue() const;
289 /// Formats m_oSelectedDate, taking m_aDateFormat and m_aDateLanguage into account.
290 OUString
GetDateString() const;
292 void SetPlainText(bool bPlainText
) { m_bPlainText
= bPlainText
; }
294 bool GetPlainText() const { return m_bPlainText
; }
296 void SetComboBox(bool bComboBox
) { m_bComboBox
= bComboBox
; }
298 bool GetComboBox() const { return m_bComboBox
; }
300 void SetDropDown(bool bDropDown
) { m_bDropDown
= bDropDown
; }
302 bool GetDropDown() const { return m_bDropDown
; }
304 void SetPlaceholderDocPart(const OUString
& rPlaceholderDocPart
)
306 m_aPlaceholderDocPart
= rPlaceholderDocPart
;
309 const OUString
& GetPlaceholderDocPart() const { return m_aPlaceholderDocPart
; }
311 void SetSelectedListItem(std::optional
<size_t> oSelectedListItem
)
313 m_oSelectedListItem
= oSelectedListItem
;
316 const std::optional
<size_t>& GetSelectedListItem() const { return m_oSelectedListItem
; }
318 /// Get a copy of selected list item's index,
319 /// potentially even if the selection is already written out to text (i.e. validated).
320 std::optional
<size_t> GetSelectedListItem(bool bCheckDocModel
) const;
322 void SetSelectedDate(std::optional
<double> oSelectedDate
) { m_oSelectedDate
= oSelectedDate
; }
324 const std::optional
<double>& GetSelectedDate() const { return m_oSelectedDate
; }
326 /// Should this character (during key input) interact with the content control?
327 bool IsInteractingCharacter(sal_Unicode cCh
);
329 /// Given rKeyCode as a keyboard event, should a popup be opened for this content control?
330 bool ShouldOpenPopup(const vcl::KeyCode
& rKeyCode
);
332 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
334 void SetDataBindingPrefixMappings(const OUString
& rDataBindingPrefixMappings
)
336 m_aDataBindingPrefixMappings
= rDataBindingPrefixMappings
;
339 const OUString
& GetDataBindingPrefixMappings() const { return m_aDataBindingPrefixMappings
; }
341 void SetDataBindingXpath(const OUString
& rDataBindingXpath
)
343 m_aDataBindingXpath
= rDataBindingXpath
;
346 const OUString
& GetDataBindingXpath() const { return m_aDataBindingXpath
; }
348 void SetDataBindingStoreItemID(const OUString
& rDataBindingStoreItemID
)
350 m_aDataBindingStoreItemID
= rDataBindingStoreItemID
;
353 const OUString
& GetDataBindingStoreItemID() const { return m_aDataBindingStoreItemID
; }
355 void SetColor(const OUString
& rColor
) { m_aColor
= rColor
; }
357 const OUString
& GetColor() const { return m_aColor
; }
359 void SetAppearance(const OUString
& rAppearance
) { m_aAppearance
= rAppearance
; }
361 const OUString
& GetAppearance() const { return m_aAppearance
; }
363 void SetAlias(const OUString
& rAlias
) { m_aAlias
= rAlias
; }
365 const OUString
& GetAlias() const { return m_aAlias
; }
367 void SetTag(const OUString
& rTag
) { m_aTag
= rTag
; }
369 const OUString
& GetTag() const { return m_aTag
; }
371 void SetId(sal_Int32 nId
) { m_nId
= nId
; }
373 sal_Int32
GetId() const { return m_nId
; }
375 void SetTabIndex(sal_uInt32 nTabIndex
) { m_nTabIndex
= nTabIndex
; }
377 sal_uInt32
GetTabIndex() const { return m_nTabIndex
; }
379 // At the design level, define how the control should be locked. No effect at implementation lvl
380 void SetLock(bool bLockContent
, bool bLockControl
);
381 void SetLock(const OUString
& rLock
) { m_aLock
= rLock
; }
383 // At the design level, get how the control is locked. Does not reflect actual implementation.
384 std::optional
<bool> GetLock(bool bControl
) const;
385 const OUString
& GetLock() const { return m_aLock
; }
387 void SetReadWrite(bool bReadWrite
) { m_bReadWrite
= bReadWrite
; }
389 // At the implementation level, define whether the user can directly modify the contents.
390 bool GetReadWrite() const { return m_bReadWrite
; }
392 SwContentControlType
GetType() const;
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */