summaryrefslogtreecommitdiff
path: root/svx/source/annotation/TextAPI.cxx
blob: d3891f7f5958dbf4ae15bb83cb520a5b4cbc3a91 (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
/* -*- 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 .
 */

#include <com/sun/star/beans/PropertyAttribute.hpp>
#include <com/sun/star/lang/Locale.hpp>
#include <com/sun/star/text/XTextField.hpp>

#include <svx/annotation/TextAPI.hxx>
#include <editeng/eeitem.hxx>
#include <editeng/editeng.hxx>
#include <editeng/outlobj.hxx>
#include <editeng/unoforou.hxx>
#include <editeng/unoprnms.hxx>
#include <editeng/unoipset.hxx>
#include <svx/svdpool.hxx>
#include <svx/svdundo.hxx>
#include <svx/svdmodel.hxx>
#include <svx/svdobj.hxx>
#include <svx/svdoutl.hxx>

namespace com::sun::star::container
{
class XNameContainer;
}

using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::text;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::container;

namespace sdr::annotation
{
namespace
{
class UndoTextAPIChanged : public SdrUndoAction
{
public:
    UndoTextAPIChanged(SdrModel& rModel, TextApiObject* pTextObj);

    virtual void Undo() override;
    virtual void Redo() override;

protected:
    std::optional<OutlinerParaObject> mpOldText;
    std::optional<OutlinerParaObject> mpNewText;
    rtl::Reference<TextApiObject> mxTextObj;
};
}

UndoTextAPIChanged::UndoTextAPIChanged(SdrModel& rModel, TextApiObject* pTextObj)
    : SdrUndoAction(rModel)
    , mpOldText(pTextObj->CreateText())
    , mxTextObj(pTextObj)
{
}

void UndoTextAPIChanged::Undo()
{
    if (!mpNewText)
        mpNewText = mxTextObj->CreateText();

    mxTextObj->SetText(*mpOldText);
}

void UndoTextAPIChanged::Redo()
{
    if (mpNewText)
    {
        mxTextObj->SetText(*mpNewText);
    }
}

namespace
{
struct OutlinerHolder
{
    SdrModel* mpModel = nullptr;
    std::unique_ptr<Outliner> mpOutliner;
    std::unique_ptr<SvxOutlinerForwarder> mpTextForwarder;
};
}

class TextAPIEditSource : public SvxEditSource
{
    // refcounted
    std::shared_ptr<OutlinerHolder> mpHolder;

    virtual std::unique_ptr<SvxEditSource> Clone() const override;
    virtual SvxTextForwarder* GetTextForwarder() override;
    virtual void UpdateData() override;
    explicit TextAPIEditSource(const TextAPIEditSource& rSource);

public:
    explicit TextAPIEditSource(SdrModel* pModel);

    void SetText(OutlinerParaObject const& rText);
    std::optional<OutlinerParaObject> CreateText();
    OUString GetText() const;
    SdrModel* getModel() { return mpHolder->mpModel; }
};

static const SvxItemPropertySet* ImplGetSdTextPortionPropertyMap()
{
    static const SfxItemPropertyMapEntry aSdTextPortionPropertyEntries[] = {
        SVX_UNOEDIT_CHAR_PROPERTIES,
        SVX_UNOEDIT_FONT_PROPERTIES,
        SVX_UNOEDIT_OUTLINER_PROPERTIES,
        SVX_UNOEDIT_PARA_PROPERTIES,
        { u"TextField"_ustr, EE_FEATURE_FIELD, cppu::UnoType<XTextField>::get(),
          PropertyAttribute::READONLY, 0 },
        { u"TextPortionType"_ustr, WID_PORTIONTYPE, ::cppu::UnoType<OUString>::get(),
          PropertyAttribute::READONLY, 0 },
        { u"TextUserDefinedAttributes"_ustr, EE_CHAR_XMLATTRIBS,
          cppu::UnoType<XNameContainer>::get(), 0, 0 },
        { u"ParaUserDefinedAttributes"_ustr, EE_PARA_XMLATTRIBS,
          cppu::UnoType<XNameContainer>::get(), 0, 0 },
    };
    static SvxItemPropertySet aSdTextPortionPropertyMap(aSdTextPortionPropertyEntries,
                                                        SdrObject::GetGlobalDrawObjectItemPool());

    return &aSdTextPortionPropertyMap;
}

TextApiObject::TextApiObject(std::unique_ptr<TextAPIEditSource> pEditSource)
    : SvxUnoText(pEditSource.get(), ImplGetSdTextPortionPropertyMap(), Reference<XText>())
    , mpSource(std::move(pEditSource))
{
}

TextApiObject::~TextApiObject() noexcept { dispose(); }

rtl::Reference<TextApiObject> TextApiObject::create(SdrModel* pModel)
{
    rtl::Reference<TextApiObject> xRet(
        new TextApiObject(std::make_unique<TextAPIEditSource>(pModel)));
    return xRet;
}

void TextApiObject::dispose()
{
    if (mpSource)
        mpSource.reset();
}

std::optional<OutlinerParaObject> TextApiObject::CreateText() { return mpSource->CreateText(); }

void TextApiObject::SetText(OutlinerParaObject const& rText)
{
    SdrModel* pModel = mpSource->getModel();
    if (pModel && pModel->IsUndoEnabled())
        pModel->AddUndo(std::make_unique<UndoTextAPIChanged>(*pModel, this));

    mpSource->SetText(rText);
    maSelection.start.nPara = EE_PARA_MAX;
}

OUString TextApiObject::GetText() const { return mpSource->GetText(); }

TextApiObject* TextApiObject::getImplementation(const css::uno::Reference<css::text::XText>& xText)
{
    TextApiObject* pImpl = dynamic_cast<TextApiObject*>(xText.get());

    if (!pImpl)
        pImpl = dynamic_cast<TextApiObject*>(comphelper::getFromUnoTunnel<SvxUnoTextBase>(xText));

    return pImpl;
}

TextAPIEditSource::TextAPIEditSource(const TextAPIEditSource& rSource)
    : SvxEditSource(*this)
    , mpHolder(rSource.mpHolder) // shallow copy; uses internal refcounting
{
}

std::unique_ptr<SvxEditSource> TextAPIEditSource::Clone() const
{
    return std::unique_ptr<SvxEditSource>(new TextAPIEditSource(*this));
}

void TextAPIEditSource::UpdateData()
{
    // data is kept in outliner all the time
}

TextAPIEditSource::TextAPIEditSource(SdrModel* pModel)
    : mpHolder(std::make_shared<OutlinerHolder>())
{
    mpHolder->mpModel = pModel;
}

SvxTextForwarder* TextAPIEditSource::GetTextForwarder()
{
    if (!mpHolder->mpModel)
        return nullptr; // mpModel == 0 can be used to flag this as disposed

    if (!mpHolder->mpOutliner)
    {
        //init draw model first
        SfxItemPool* pPool = &mpHolder->mpModel->GetItemPool();
        mpHolder->mpOutliner.reset(new SdrOutliner(pPool, OutlinerMode::TextObject));
        // TODO - we need a way to register this to the current module
        // SdDrawDocument::SetCalcFieldValueHdl(mpHolder->mpOutliner.get());
    }

    if (!mpHolder->mpTextForwarder)
        mpHolder->mpTextForwarder.reset(new SvxOutlinerForwarder(*mpHolder->mpOutliner, false));

    return mpHolder->mpTextForwarder.get();
}

void TextAPIEditSource::SetText(OutlinerParaObject const& rText)
{
    if (mpHolder->mpModel)
    {
        if (!mpHolder->mpOutliner)
        {
            //init draw model first
            SfxItemPool* pPool = &mpHolder->mpModel->GetItemPool();
            mpHolder->mpOutliner.reset(new SdrOutliner(pPool, OutlinerMode::TextObject));
            // TODO - we need a way to register this to the current module
            //SdDrawDocument::SetCalcFieldValueHdl(mpHolder->mpOutliner.get());
        }

        mpHolder->mpOutliner->SetText(rText);
    }
}

std::optional<OutlinerParaObject> TextAPIEditSource::CreateText()
{
    if (mpHolder->mpModel && mpHolder->mpOutliner)
        return mpHolder->mpOutliner->CreateParaObject();
    else
        return std::nullopt;
}

OUString TextAPIEditSource::GetText() const
{
    if (mpHolder->mpModel && mpHolder->mpOutliner)
        return mpHolder->mpOutliner->GetEditEngine().GetText();
    else
        return OUString();
}

} // namespace sdr::annotation

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