summaryrefslogtreecommitdiffstats
path: root/sw/inc/formatcontentcontrol.hxx
blob: 00f981308cfde4c4b2fa965e7e6659f282ef7c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#pragma once

#include <com/sun/star/text/XTextContent.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>

#include <cppuhelper/weakref.hxx>
#include <sal/types.h>
#include <svl/poolitem.hxx>

#include "calbck.hxx"
#include "swdllapi.h"

namespace vcl
{
class KeyCode;
}
class SwContentControl;
class SwTextContentControl;
class SwTextNode;
class SwXContentControl;

enum class SwContentControlType
{
    RICH_TEXT,
    CHECKBOX,
    DROP_DOWN_LIST,
    PICTURE,
    DATE,
};

/// SfxPoolItem subclass that wraps an SwContentControl.
class SW_DLLPUBLIC SwFormatContentControl final : public SfxPoolItem
{
    std::shared_ptr<SwContentControl> m_pContentControl;
    SwTextContentControl* m_pTextAttr;

public:
    SwTextContentControl* GetTextAttr() { return m_pTextAttr; }
    void SetTextAttr(SwTextContentControl* pTextAttr);

    /// This method must be called when the hint is actually copied.
    void DoCopy(SwTextNode& rTargetTextNode);

    explicit SwFormatContentControl(sal_uInt16 nWhich);

    explicit SwFormatContentControl(const std::shared_ptr<SwContentControl>& pContentControl,
                                    sal_uInt16 nWhich);
    ~SwFormatContentControl() override;

    /// SfxPoolItem
    bool operator==(const SfxPoolItem&) const override;
    SwFormatContentControl* Clone(SfxItemPool* pPool = nullptr) const override;

    /**
     * Notify clients registered at m_pContentControl that this content control is being
     * (re-)moved.
     */
    void NotifyChangeTextNode(SwTextNode* pTextNode);
    static SwFormatContentControl* CreatePoolDefault(sal_uInt16 nWhich);
    std::shared_ptr<SwContentControl> GetContentControl() { return m_pContentControl; }
    const std::shared_ptr<SwContentControl>& GetContentControl() const { return m_pContentControl; }

    void dumpAsXml(xmlTextWriterPtr pWriter) const override;
};

/// Represents one list item in a content control dropdown list.
class SW_DLLPUBLIC SwContentControlListItem
{
public:
    /// This may be empty, ToString() falls back to m_aValue.
    OUString m_aDisplayText;
    /// This must not be empty.
    OUString m_aValue;

    void dumpAsXml(xmlTextWriterPtr pWriter) const;

    OUString ToString() const;

    bool operator==(const SwContentControlListItem& rOther) const;

    static void ItemsToAny(const std::vector<SwContentControlListItem>& rItems,
                           css::uno::Any& rVal);

    static std::vector<SwContentControlListItem> ItemsFromAny(const css::uno::Any& rVal);
};

/// Stores the properties of a content control.
class SW_DLLPUBLIC SwContentControl : public sw::BroadcastingModify
{
    css::uno::WeakReference<css::text::XTextContent> m_wXContentControl;

    SwFormatContentControl* m_pFormat;

    /// Can be nullptr if not in a document for undo purposes.
    SwTextNode* m_pTextNode;

    /// Current content is placeholder text.
    bool m_bShowingPlaceHolder = false;

    /// Display the content control as a checkbox.
    bool m_bCheckbox = false;

    /// If m_bCheckbox is true, is the checkbox checked?
    bool m_bChecked = false;

    /// If m_bCheckbox is true, the value of a checked checkbox.
    OUString m_aCheckedState;

    /// If m_bCheckbox is true, the value of an unchecked checkbox.
    OUString m_aUncheckedState;

    std::vector<SwContentControlListItem> m_aListItems;

    bool m_bPicture = false;

    bool m_bDate = false;

    /// If m_bDate is true, the date format in a syntax accepted by SvNumberFormatter::PutEntry().
    OUString m_aDateFormat;

    /// If m_bDate is true, the date's BCP 47 language tag.
    OUString m_aDateLanguage;

    /// Date in YYYY-MM-DDT00:00:00Z format.
    OUString m_aCurrentDate;

    /// The placeholder's doc part: just remembered.
    OUString m_aPlaceholderDocPart;

    /// The data bindings's prefix mappings: just remembered.
    OUString m_aDataBindingPrefixMappings;

    /// The data bindings's XPath: just remembered.
    OUString m_aDataBindingXpath;

    /// The data bindings's store item ID: just remembered.
    OUString m_aDataBindingStoreItemID;

    /// The color: just remembered.
    OUString m_aColor;

    /// Stores a list item index, in case the doc model is not yet updated.
    std::optional<size_t> m_oSelectedListItem;

    /// Stores a date timestamp, in case the doc model is not yet updated.
    std::optional<double> m_oSelectedDate;

    /**
     * E.g. checkbox is read-only by default, but we still update contents on interaction
     * internally. This flag is true for the duration of that interaction.
     */
    bool m_bReadWrite = false;

public:
    SwTextContentControl* GetTextAttr() const;

    SwTextNode* GetTextNode() const { return m_pTextNode; }

    SwFormatContentControl* GetFormatContentControl() const { return m_pFormat; }

    void SetFormatContentControl(SwFormatContentControl* pFormat) { m_pFormat = pFormat; };

    void NotifyChangeTextNode(SwTextNode* pTextNode);

    const css::uno::WeakReference<css::text::XTextContent>& GetXContentControl() const
    {
        return m_wXContentControl;
    }

    void SetXContentControl(const css::uno::Reference<css::text::XTextContent>& xContentCnotrol)
    {
        m_wXContentControl = xContentCnotrol;
    }

    virtual void SwClientNotify(const SwModify&, const SfxHint&) override;

    explicit SwContentControl(SwFormatContentControl* pFormat);

    virtual ~SwContentControl() override;

    void SetShowingPlaceHolder(bool bShowingPlaceHolder)
    {
        m_bShowingPlaceHolder = bShowingPlaceHolder;
    }

    bool GetShowingPlaceHolder() const { return m_bShowingPlaceHolder; }

    void SetCheckbox(bool bCheckbox) { m_bCheckbox = bCheckbox; }

    bool GetCheckbox() const { return m_bCheckbox; }

    void SetChecked(bool bChecked) { m_bChecked = bChecked; }

    bool GetChecked() const { return m_bChecked; }

    void SetCheckedState(const OUString& rCheckedState) { m_aCheckedState = rCheckedState; }

    OUString GetCheckedState() const { return m_aCheckedState; }

    void SetUncheckedState(const OUString& rUncheckedState) { m_aUncheckedState = rUncheckedState; }

    OUString GetUncheckedState() const { return m_aUncheckedState; }

    std::vector<SwContentControlListItem> GetListItems() const { return m_aListItems; }

    bool HasListItems() const { return !m_aListItems.empty(); }

    void SetListItems(const std::vector<SwContentControlListItem>& rListItems)
    {
        m_aListItems = rListItems;
    }

    void SetPicture(bool bPicture) { m_bPicture = bPicture; }

    bool GetPicture() const { return m_bPicture; }

    void SetDate(bool bDate) { m_bDate = bDate; }

    bool GetDate() const { return m_bDate; }

    void SetDateFormat(const OUString& rDateFormat) { m_aDateFormat = rDateFormat; }

    OUString GetDateFormat() const { return m_aDateFormat; }

    void SetDateLanguage(const OUString& rDateLanguage) { m_aDateLanguage = rDateLanguage; }

    OUString GetDateLanguage() const { return m_aDateLanguage; }

    void SetCurrentDate(const OUString& rCurrentDate) { m_aCurrentDate = rCurrentDate; }

    OUString GetCurrentDate() const { return m_aCurrentDate; }

    /// Formats fCurrentDate and sets it.
    void SetCurrentDateValue(double fCurrentDate);

    /// Parses m_aCurrentDate and returns it.
    double GetCurrentDateValue() const;

    /// Formats m_oSelectedDate, taking m_aDateFormat and m_aDateLanguage into account.
    OUString GetDateString() const;

    void SetPlaceholderDocPart(const OUString& rPlaceholderDocPart)
    {
        m_aPlaceholderDocPart = rPlaceholderDocPart;
    }

    OUString GetPlaceholderDocPart() const { return m_aPlaceholderDocPart; }

    void SetSelectedListItem(std::optional<size_t> oSelectedListItem)
    {
        m_oSelectedListItem = oSelectedListItem;
    }

    std::optional<size_t> GetSelectedListItem() const { return m_oSelectedListItem; }

    void SetSelectedDate(std::optional<double> oSelectedDate) { m_oSelectedDate = oSelectedDate; }

    std::optional<double> GetSelectedDate() const { return m_oSelectedDate; }

    /// Should this character (during key input) interact with the content control?
    bool IsInteractingCharacter(sal_Unicode cCh);

    /// Given rKeyCode as a keyboard event, should a popup be opened for this content control?
    bool ShouldOpenPopup(const vcl::KeyCode& rKeyCode);

    virtual void dumpAsXml(xmlTextWriterPtr pWriter) const;

    void SetDataBindingPrefixMappings(const OUString& rDataBindingPrefixMappings)
    {
        m_aDataBindingPrefixMappings = rDataBindingPrefixMappings;
    }

    OUString GetDataBindingPrefixMappings() const { return m_aDataBindingPrefixMappings; }

    void SetDataBindingXpath(const OUString& rDataBindingXpath)
    {
        m_aDataBindingXpath = rDataBindingXpath;
    }

    OUString GetDataBindingXpath() const { return m_aDataBindingXpath; }

    void SetDataBindingStoreItemID(const OUString& rDataBindingStoreItemID)
    {
        m_aDataBindingStoreItemID = rDataBindingStoreItemID;
    }

    OUString GetDataBindingStoreItemID() const { return m_aDataBindingStoreItemID; }

    void SetColor(const OUString& rColor) { m_aColor = rColor; }

    OUString GetColor() const { return m_aColor; }

    void SetReadWrite(bool bReadWrite) { m_bReadWrite = bReadWrite; }

    bool GetReadWrite() const { return m_bReadWrite; }
};

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */