summaryrefslogtreecommitdiffstats
path: root/sw/source/core/unocore/unolinebreak.cxx
blob: 948fc73a3ed62d9ae7628fdd294bc0724d488033 (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
/* -*- 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 <unolinebreak.hxx>

#include <cppuhelper/supportsservice.hxx>
#include <cppuhelper/weakref.hxx>
#include <comphelper/servicehelper.hxx>
#include <sal/log.hxx>
#include <svl/listener.hxx>
#include <svl/itemprop.hxx>

#include <IDocumentContentOperations.hxx>
#include <doc.hxx>
#include <formatlinebreak.hxx>
#include <unotextrange.hxx>
#include <ndtxt.hxx>
#include <textlinebreak.hxx>
#include <unomap.hxx>
#include <unoprnms.hxx>

using namespace com::sun::star;

/// The inner part SwXLineBreak, which is deleted with a locked SolarMutex.
class SwXLineBreak::Impl : public SvtListener
{
public:
    bool m_bIsDescriptor;
    SwFormatLineBreak* m_pFormatLineBreak;
    SwLineBreakClear m_eClear;

    Impl(SwFormatLineBreak* const pLineBreak)
        : m_bIsDescriptor(pLineBreak == nullptr)
        , m_pFormatLineBreak(pLineBreak)
        , m_eClear(SwLineBreakClear::NONE)
    {
        if (m_pFormatLineBreak)
        {
            StartListening(m_pFormatLineBreak->GetNotifier());
        }
    }

    const SwFormatLineBreak* GetLineBreakFormat() const;

    const SwFormatLineBreak& GetLineBreakFormatOrThrow() const;

    void Invalidate();

protected:
    void Notify(const SfxHint& rHint) override;
};

const SwFormatLineBreak* SwXLineBreak::Impl::GetLineBreakFormat() const
{
    return m_pFormatLineBreak;
}

const SwFormatLineBreak& SwXLineBreak::Impl::GetLineBreakFormatOrThrow() const
{
    const SwFormatLineBreak* pLineBreak(GetLineBreakFormat());
    if (!pLineBreak)
    {
        throw uno::RuntimeException("SwXLineBreak: disposed or invalid", nullptr);
    }

    return *pLineBreak;
}

void SwXLineBreak::Impl::Invalidate()
{
    EndListeningAll();
    m_pFormatLineBreak = nullptr;
}

void SwXLineBreak::Impl::Notify(const SfxHint& rHint)
{
    if (rHint.GetId() == SfxHintId::Dying)
    {
        Invalidate();
    }
}

SwXLineBreak::SwXLineBreak(SwFormatLineBreak& rFormat)
    : m_pImpl(new SwXLineBreak::Impl(&rFormat))
{
}

SwXLineBreak::SwXLineBreak()
    : m_pImpl(new SwXLineBreak::Impl(nullptr))
{
}

SwXLineBreak::~SwXLineBreak() {}

rtl::Reference<SwXLineBreak> SwXLineBreak::CreateXLineBreak(SwFormatLineBreak* pLineBreakFormat)
{
    rtl::Reference<SwXLineBreak> xLineBreak;
    if (pLineBreakFormat)
    {
        xLineBreak = pLineBreakFormat->GetXTextContent();
    }
    if (!xLineBreak.is())
    {
        xLineBreak = pLineBreakFormat ? new SwXLineBreak(*pLineBreakFormat) : new SwXLineBreak;
        if (pLineBreakFormat)
        {
            pLineBreakFormat->SetXLineBreak(xLineBreak);
        }
    }
    return xLineBreak;
}

OUString SAL_CALL SwXLineBreak::getImplementationName() { return "SwXLineBreak"; }

sal_Bool SAL_CALL SwXLineBreak::supportsService(const OUString& rServiceName)
{
    return cppu::supportsService(this, rServiceName);
}

uno::Sequence<OUString> SAL_CALL SwXLineBreak::getSupportedServiceNames()
{
    return { "com.sun.star.text.LineBreak" };
}

void SAL_CALL SwXLineBreak::attach(const uno::Reference<text::XTextRange>& xTextRange)
{
    SolarMutexGuard aGuard;
    if (!m_pImpl->m_bIsDescriptor)
    {
        throw uno::RuntimeException();
    }

    auto pRange = dynamic_cast<SwXTextRange*>(xTextRange.get());
    if (!pRange)
    {
        throw lang::IllegalArgumentException();
    }

    SwDoc& rNewDoc = pRange->GetDoc();
    SwUnoInternalPaM aPam(rNewDoc);
    sw::XTextRangeToSwPaM(aPam, xTextRange);
    UnoActionContext aContext(&rNewDoc);
    SwFormatLineBreak aLineBreak(m_pImpl->m_eClear);
    SetAttrMode nInsertFlags = SetAttrMode::DEFAULT;
    rNewDoc.getIDocumentContentOperations().InsertPoolItem(aPam, aLineBreak, nInsertFlags);
    auto pTextAttr
        = static_cast<SwTextLineBreak*>(aPam.GetNode().GetTextNode()->GetTextAttrForCharAt(
            aPam.GetPoint()->GetContentIndex() - 1, RES_TXTATR_LINEBREAK));
    if (pTextAttr)
    {
        m_pImpl->EndListeningAll();
        auto pLineBreak = const_cast<SwFormatLineBreak*>(&pTextAttr->GetLineBreak());
        m_pImpl->m_pFormatLineBreak = pLineBreak;
        m_pImpl->StartListening(pLineBreak->GetNotifier());
    }
    m_pImpl->m_bIsDescriptor = false;
}

uno::Reference<text::XTextRange> SAL_CALL SwXLineBreak::getAnchor()
{
    SolarMutexGuard aGuard;

    return m_pImpl->GetLineBreakFormatOrThrow().GetAnchor();
}

void SAL_CALL SwXLineBreak::dispose()
{
    SAL_WARN("sw.uno", "SwXLineBreak::dispose: not implemented");
}

void SAL_CALL
SwXLineBreak::addEventListener(const uno::Reference<lang::XEventListener>& /*xListener*/)
{
}

void SAL_CALL
SwXLineBreak::removeEventListener(const uno::Reference<lang::XEventListener>& /*xListener*/)
{
}

uno::Reference<beans::XPropertySetInfo> SAL_CALL SwXLineBreak::getPropertySetInfo()
{
    SolarMutexGuard aGuard;

    static uno::Reference<beans::XPropertySetInfo> xRet
        = aSwMapProvider.GetPropertySet(PROPERTY_MAP_LINEBREAK)->getPropertySetInfo();
    return xRet;
}

void SAL_CALL SwXLineBreak::setPropertyValue(const OUString& rPropertyName,
                                             const css::uno::Any& rValue)
{
    SolarMutexGuard aGuard;

    if (rPropertyName != UNO_NAME_CLEAR)
    {
        throw lang::IllegalArgumentException();
    }

    if (m_pImpl->m_bIsDescriptor)
    {
        sal_Int16 eValue{};
        if (rValue >>= eValue)
        {
            m_pImpl->m_eClear = static_cast<SwLineBreakClear>(eValue);
        }
    }
    else
    {
        m_pImpl->m_pFormatLineBreak->PutValue(rValue, 0);
    }
}

uno::Any SAL_CALL SwXLineBreak::getPropertyValue(const OUString& rPropertyName)
{
    SolarMutexGuard aGuard;

    uno::Any aRet;
    if (sw::GetDefaultTextContentValue(aRet, rPropertyName))
    {
        return aRet;
    }

    if (rPropertyName != UNO_NAME_CLEAR)
    {
        beans::UnknownPropertyException aExcept;
        aExcept.Message = rPropertyName;
        throw aExcept;
    }

    if (m_pImpl->m_bIsDescriptor)
    {
        auto eValue = static_cast<sal_Int16>(m_pImpl->m_eClear);
        aRet <<= eValue;
    }
    else
    {
        aRet <<= m_pImpl->m_pFormatLineBreak->GetEnumValue();
    }
    return aRet;
}

void SAL_CALL SwXLineBreak::addPropertyChangeListener(
    const OUString& /*rPropertyName*/,
    const uno::Reference<beans::XPropertyChangeListener>& /*xListener*/)
{
    SAL_WARN("sw.uno", "SwXLineBreak::addPropertyChangeListener: not implemented");
}

void SAL_CALL SwXLineBreak::removePropertyChangeListener(
    const OUString& /*rPropertyName*/,
    const uno::Reference<beans::XPropertyChangeListener>& /*xListener*/)
{
    SAL_WARN("sw.uno", "SwXLineBreak::removePropertyChangeListener: not implemented");
}

void SAL_CALL SwXLineBreak::addVetoableChangeListener(
    const OUString& /*rPropertyName*/,
    const uno::Reference<beans::XVetoableChangeListener>& /*xListener*/)
{
    SAL_WARN("sw.uno", "SwXLineBreak::addVetoableChangeListener: not implemented");
}

void SAL_CALL SwXLineBreak::removeVetoableChangeListener(
    const OUString& /*rPropertyName*/,
    const uno::Reference<beans::XVetoableChangeListener>& /*xListener*/)
{
    SAL_WARN("sw.uno", "SwXLineBreak::removeVetoableChangeListener: not implemented");
}

sal_Int64 SAL_CALL SwXLineBreak::getSomething(const css::uno::Sequence<sal_Int8>& rIdentifier)
{
    return comphelper::getSomethingImpl<SwXLineBreak>(rIdentifier, this);
}

const uno::Sequence<sal_Int8>& SwXLineBreak::getUnoTunnelId()
{
    static const comphelper::UnoIdInit theSwXLineBreakUnoTunnelId;
    return theSwXLineBreakUnoTunnelId.getSeq();
}

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