Factor out and simplify COM-safe wait
[LibreOffice.git] / sw / inc / formatcontentcontrol.hxx
blobcffe326d0703ddc8f4349bd77e990a6dc8e76463
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #pragma once
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>
30 #include "calbck.hxx"
31 #include "swdllapi.h"
33 namespace vcl
35 class KeyCode;
37 class SwContentControl;
38 class SwTextContentControl;
39 class SwTextNode;
40 class SwXContentControl;
42 enum class SwContentControlType
44 RICH_TEXT,
45 CHECKBOX,
46 DROP_DOWN_LIST,
47 PICTURE,
48 DATE,
49 PLAIN_TEXT,
50 COMBO_BOX,
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;
59 public:
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,
69 sal_uInt16 nWhich);
70 ~SwFormatContentControl() override;
72 /// SfxPoolItem
73 bool operator==(const SfxPoolItem&) const override;
74 SwFormatContentControl* Clone(SfxItemPool* pPool = nullptr) const override;
76 /**
77 * Notify clients registered at m_pContentControl that this content control is being
78 * (re-)moved.
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
91 public:
92 /// This may be empty, ToString() falls back to m_aValue.
93 OUString m_aDisplayText;
94 /// This must not be empty.
95 OUString m_aValue;
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.
171 OUString m_aColor;
173 /// The appearance: just remembered.
174 OUString m_aAppearance;
176 /// The alias.
177 OUString m_aAlias;
179 /// The tag: just remembered.
180 OUString m_aTag;
182 /// The id: just remembered.
183 sal_Int32 m_nId = 0;
185 /// The tabIndex: just remembered.
186 sal_uInt32 m_nTabIndex = 0;
188 /// The control and content locks: mostly just remembered.
189 OUString m_aLock;
191 /// The multiline property: just remembered.
192 OUString m_aMultiLine;
194 /// Stores a list item index, in case the doc model is not yet updated.
195 // i.e. temporarily store the selected item until the text is inserted by GotoContentControl.
196 std::optional<size_t> m_oSelectedListItem;
198 /// Stores a date timestamp, in case the doc model is not yet updated.
199 // i.e. temporarily store the date until the text is inserted by GotoContentControl.
200 std::optional<double> m_oSelectedDate;
203 * E.g. checkbox is read-only by default, but we still update contents on interaction
204 * internally. This flag is true for the duration of that interaction.
206 bool m_bReadWrite = false;
208 public:
209 SwTextContentControl* GetTextAttr() const;
211 SwTextNode* GetTextNode() const { return m_pTextNode; }
213 SwFormatContentControl* GetFormatContentControl() const { return m_pFormat; }
215 void SetFormatContentControl(SwFormatContentControl* pFormat) { m_pFormat = pFormat; };
217 void NotifyChangeTextNode(SwTextNode* pTextNode);
219 const unotools::WeakReference<SwXContentControl>& GetXContentControl() const
221 return m_wXContentControl;
224 void SetXContentControl(const rtl::Reference<SwXContentControl>& xContentControl);
226 virtual void SwClientNotify(const SwModify&, const SfxHint&) override;
228 explicit SwContentControl(SwFormatContentControl* pFormat);
230 virtual ~SwContentControl() override;
232 void SetShowingPlaceHolder(bool bShowingPlaceHolder)
234 m_bShowingPlaceHolder = bShowingPlaceHolder;
237 bool GetShowingPlaceHolder() const { return m_bShowingPlaceHolder; }
239 void SetCheckbox(bool bCheckbox) { m_bCheckbox = bCheckbox; }
241 bool GetCheckbox() const { return m_bCheckbox; }
243 void SetChecked(bool bChecked) { m_bChecked = bChecked; }
245 bool GetChecked() const { return m_bChecked; }
247 void SetCheckedState(const OUString& rCheckedState) { m_aCheckedState = rCheckedState; }
249 const OUString& GetCheckedState() const { return m_aCheckedState; }
251 void SetUncheckedState(const OUString& rUncheckedState) { m_aUncheckedState = rUncheckedState; }
253 const OUString& GetUncheckedState() const { return m_aUncheckedState; }
255 const std::vector<SwContentControlListItem>& GetListItems() const { return m_aListItems; }
257 void SetListItems(const std::vector<SwContentControlListItem>& rListItems)
259 m_aListItems = rListItems;
262 bool AddListItem(size_t nZIndex, const OUString& rDisplayText, const OUString& rValue);
263 void DeleteListItem(size_t nZIndex);
264 void ClearListItems();
266 void SetPicture(bool bPicture) { m_bPicture = bPicture; }
268 bool GetPicture() const { return m_bPicture; }
270 void SetDate(bool bDate) { m_bDate = bDate; }
272 bool GetDate() const { return m_bDate; }
274 void SetDateFormat(const OUString& rDateFormat) { m_aDateFormat = rDateFormat; }
276 const OUString& GetDateFormat() const { return m_aDateFormat; }
278 void SetDateLanguage(const OUString& rDateLanguage) { m_aDateLanguage = rDateLanguage; }
280 const OUString& GetDateLanguage() const { return m_aDateLanguage; }
282 void SetCurrentDate(const OUString& rCurrentDate) { m_aCurrentDate = rCurrentDate; }
284 const OUString& GetCurrentDate() const { return m_aCurrentDate; }
286 /// Formats fCurrentDate and sets it.
287 void SetCurrentDateValue(double fCurrentDate);
289 /// Parses m_aCurrentDate and returns it.
290 double GetCurrentDateValue() const;
292 /// Formats m_oSelectedDate, taking m_aDateFormat and m_aDateLanguage into account.
293 OUString GetDateString() const;
295 void SetPlainText(bool bPlainText) { m_bPlainText = bPlainText; }
297 bool GetPlainText() const { return m_bPlainText; }
299 void SetComboBox(bool bComboBox) { m_bComboBox = bComboBox; }
301 bool GetComboBox() const { return m_bComboBox; }
303 void SetDropDown(bool bDropDown) { m_bDropDown = bDropDown; }
305 bool GetDropDown() const { return m_bDropDown; }
307 void SetPlaceholderDocPart(const OUString& rPlaceholderDocPart)
309 m_aPlaceholderDocPart = rPlaceholderDocPart;
312 const OUString& GetPlaceholderDocPart() const { return m_aPlaceholderDocPart; }
314 void SetSelectedListItem(std::optional<size_t> oSelectedListItem)
316 m_oSelectedListItem = oSelectedListItem;
319 const std::optional<size_t>& GetSelectedListItem() const { return m_oSelectedListItem; }
321 /// Get a copy of selected list item's index,
322 /// potentially even if the selection is already written out to text (i.e. validated).
323 std::optional<size_t> GetSelectedListItem(bool bCheckDocModel) const;
325 void SetSelectedDate(std::optional<double> oSelectedDate) { m_oSelectedDate = oSelectedDate; }
327 const std::optional<double>& GetSelectedDate() const { return m_oSelectedDate; }
329 /// Should this character (during key input) interact with the content control?
330 bool IsInteractingCharacter(sal_Unicode cCh);
332 /// Given rKeyCode as a keyboard event, should a popup be opened for this content control?
333 bool ShouldOpenPopup(const vcl::KeyCode& rKeyCode);
335 void dumpAsXml(xmlTextWriterPtr pWriter) const;
337 void SetDataBindingPrefixMappings(const OUString& rDataBindingPrefixMappings)
339 m_aDataBindingPrefixMappings = rDataBindingPrefixMappings;
342 const OUString& GetDataBindingPrefixMappings() const { return m_aDataBindingPrefixMappings; }
344 void SetDataBindingXpath(const OUString& rDataBindingXpath)
346 m_aDataBindingXpath = rDataBindingXpath;
349 const OUString& GetDataBindingXpath() const { return m_aDataBindingXpath; }
351 void SetDataBindingStoreItemID(const OUString& rDataBindingStoreItemID)
353 m_aDataBindingStoreItemID = rDataBindingStoreItemID;
356 const OUString& GetDataBindingStoreItemID() const { return m_aDataBindingStoreItemID; }
358 void SetColor(const OUString& rColor) { m_aColor = rColor; }
360 const OUString& GetColor() const { return m_aColor; }
362 void SetAppearance(const OUString& rAppearance) { m_aAppearance = rAppearance; }
364 const OUString& GetAppearance() const { return m_aAppearance; }
366 void SetAlias(const OUString& rAlias) { m_aAlias = rAlias; }
368 const OUString& GetAlias() const { return m_aAlias; }
370 void SetTag(const OUString& rTag) { m_aTag = rTag; }
372 const OUString& GetTag() const { return m_aTag; }
374 void SetId(sal_Int32 nId) { m_nId = nId; }
376 sal_Int32 GetId() const { return m_nId; }
378 void SetTabIndex(sal_uInt32 nTabIndex) { m_nTabIndex = nTabIndex; }
380 sal_uInt32 GetTabIndex() const { return m_nTabIndex; }
382 // At the design level, define how the control should be locked. No effect at implementation lvl
383 void SetLock(bool bLockContent, bool bLockControl);
384 void SetLock(const OUString& rLock) { m_aLock = rLock; }
386 // At the design level, get how the control is locked. Does not reflect actual implementation.
387 std::optional<bool> GetLock(bool bControl) const;
388 const OUString& GetLock() const { return m_aLock; }
390 void SetReadWrite(bool bReadWrite) { m_bReadWrite = bReadWrite; }
392 // At the implementation level, define whether the user can directly modify the contents.
393 bool GetReadWrite() const { return m_bReadWrite; }
395 void SetMultiLine(const OUString& rMultiline) { m_aMultiLine = rMultiline; }
397 const OUString& GetMultiLine() const { return m_aMultiLine; }
399 SwContentControlType GetType() const;
402 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */