summaryrefslogtreecommitdiffstats
path: root/sw/source/ui/vba/vbalistformat.cxx
blob: efcbf2dbe874ae60194557816e2e70704bdd34a3 (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
318
319
320
321
322
323
324
325
326
327
328
/* -*- 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 "vbalistformat.hxx"
#include <utility>
#include <vbahelper/vbahelper.hxx>
#include <ooo/vba/word/WdListApplyTo.hpp>
#include <ooo/vba/word/WdDefaultListBehavior.hpp>
#include <com/sun/star/awt/FontDescriptor.hpp>
#include <com/sun/star/container/XEnumerationAccess.hpp>
#include <com/sun/star/container/XEnumeration.hpp>
#include <com/sun/star/document/XUndoManagerSupplier.hpp>
#include <com/sun/star/beans/XMultiPropertySet.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/style/TabStop.hpp>
#include <com/sun/star/text/PositionAndSpaceMode.hpp>
#include <com/sun/star/text/XTextTable.hpp>
#include <com/sun/star/util/Color.hpp>
#include <comphelper/sequence.hxx>
#include <comphelper/sequenceashashmap.hxx>
#include <comphelper/scopeguard.hxx>
#include <editeng/numitem.hxx>
#include "vbalisttemplate.hxx"

#include <vector>

using namespace ::ooo::vba;
using namespace ::com::sun::star;

SwVbaListFormat::SwVbaListFormat( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, uno::Reference< text::XTextRange >  xTextRange ) : SwVbaListFormat_BASE( rParent, rContext ), mxTextRange(std::move( xTextRange ))
{
}

SwVbaListFormat::~SwVbaListFormat()
{
}

void SAL_CALL SwVbaListFormat::ApplyListTemplate( const css::uno::Reference< word::XListTemplate >& ListTemplate, const css::uno::Any& ContinuePreviousList, const css::uno::Any& ApplyTo, const css::uno::Any& DefaultListBehavior )
{
    bool bContinuePreviousList = true;
    if( ContinuePreviousList.hasValue() )
        ContinuePreviousList >>= bContinuePreviousList;

    // "applyto" must be current selection
    sal_Int32 bApplyTo = word::WdListApplyTo::wdListApplyToSelection;
    if( ApplyTo.hasValue() )
        ApplyTo >>= bApplyTo;
    if( bApplyTo != word::WdListApplyTo::wdListApplyToSelection )
        throw uno::RuntimeException();

    // default behaviour must be wdWord8ListBehavior
    sal_Int32 nDefaultListBehavior = word::WdDefaultListBehavior::wdWord8ListBehavior;
    if( DefaultListBehavior.hasValue() )
        DefaultListBehavior >>= nDefaultListBehavior;
    if( nDefaultListBehavior != word::WdDefaultListBehavior::wdWord8ListBehavior )
        throw uno::RuntimeException();

    uno::Reference< container::XEnumerationAccess > xEnumAccess( mxTextRange, uno::UNO_QUERY_THROW );
    uno::Reference< container::XEnumeration > xEnum = xEnumAccess->createEnumeration();
    if (!xEnum->hasMoreElements())
        return;

    SwVbaListTemplate& rListTemplate = dynamic_cast<SwVbaListTemplate&>(*ListTemplate);

    bool isFirstElement = true;
    do
    {
        uno::Reference< beans::XPropertySet > xProps( xEnum->nextElement(), uno::UNO_QUERY_THROW );
        if( isFirstElement )
        {
            bool isNumberingRestart = !bContinuePreviousList;
            xProps->setPropertyValue("ParaIsNumberingRestart", uno::Any( isNumberingRestart ) );
            if( isNumberingRestart )
            {
                xProps->setPropertyValue("NumberingStartValue", uno::Any( sal_Int16(1) ) );
            }
            isFirstElement = false;
        }
        else
        {
            xProps->setPropertyValue("ParaIsNumberingRestart", uno::Any( false ) );
        }
        rListTemplate.applyListTemplate( xProps );
    }
    while( xEnum->hasMoreElements() );
}

template <class Ref>
static void addParagraphsToList(const Ref& a,
                                std::vector<css::uno::Reference<css::beans::XPropertySet>>& rList)
{
    if (css::uno::Reference<css::lang::XServiceInfo> xInfo{ a, css::uno::UNO_QUERY })
    {
        if (xInfo->supportsService("com.sun.star.text.Paragraph"))
        {
            rList.emplace_back(xInfo, css::uno::UNO_QUERY_THROW);
        }
        else if (xInfo->supportsService("com.sun.star.text.TextTable"))
        {
            css::uno::Reference<css::text::XTextTable> xTable(xInfo, css::uno::UNO_QUERY_THROW);
            const auto aNames = xTable->getCellNames();
            for (const auto& rName : aNames)
            {
                addParagraphsToList(xTable->getCellByName(rName), rList);
            }
        }
    }
    if (css::uno::Reference<css::container::XEnumerationAccess> xEnumAccess{ a,
                                                                             css::uno::UNO_QUERY })
    {
        auto xEnum = xEnumAccess->createEnumeration();
        while (xEnum->hasMoreElements())
            addParagraphsToList(xEnum->nextElement(), rList);
    }
}

void SAL_CALL SwVbaListFormat::ConvertNumbersToText(  )
{
    css::uno::Reference<css::frame::XModel> xModel(getThisWordDoc(mxContext));
    css::uno::Reference<css::document::XUndoManagerSupplier> xUndoSupplier(
        xModel, css::uno::UNO_QUERY_THROW);
    css::uno::Reference<css::document::XUndoManager> xUndoManager(xUndoSupplier->getUndoManager());
    xUndoManager->enterUndoContext("ConvertNumbersToText");
    xModel->lockControllers();
    comphelper::ScopeGuard g([xModel, xUndoManager]() {
        xModel->unlockControllers();
        xUndoManager->leaveUndoContext();
    });

    std::vector<css::uno::Reference<css::beans::XPropertySet>> aParagraphs;
    addParagraphsToList(mxTextRange, aParagraphs);

    // in reverse order, to get proper label strings
    for (auto iter = aParagraphs.rbegin(); iter != aParagraphs.rend(); ++iter)
    {
        auto& rPropertySet = *iter;
        if (bool bNumber; (rPropertySet->getPropertyValue("NumberingIsNumber") >>= bNumber) && bNumber)
        {
            css::uno::Reference<css::text::XTextRange> xRange(rPropertySet, css::uno::UNO_QUERY_THROW);
            OUString sLabelString;
            rPropertySet->getPropertyValue("ListLabelString") >>= sLabelString;
            // sal_Int16 nAdjust = SAL_MAX_INT16; // TODO?
            sal_Int16 nNumberingType = SAL_MAX_INT16; // css::style::NumberingType
            sal_Int16 nPositionAndSpaceMode = SAL_MAX_INT16;
            sal_Int16 nLabelFollowedBy = SAL_MAX_INT16;
            sal_Int32 nListtabStopPosition = SAL_MAX_INT32;
            sal_Int32 nFirstLineIndent = SAL_MAX_INT32;
            sal_Int32 nIndentAt = SAL_MAX_INT32;
            sal_Int32 nLeftMargin = SAL_MAX_INT32;
            sal_Int32 nSymbolTextDistance = SAL_MAX_INT32;
            sal_Int32 nFirstLineOffset = SAL_MAX_INT32;
            OUString sCharStyleName, sBulletChar;
            css::awt::FontDescriptor aBulletFont;
            bool bHasFont;
            css::util::Color aBulletColor = css::util::Color(COL_AUTO);
            bool bHasColor;

            {
                sal_uInt16 nLevel = SAL_MAX_UINT16;
                rPropertySet->getPropertyValue("NumberingLevel") >>= nLevel;
                css::uno::Reference<css::container::XIndexAccess> xNumberingRules;
                rPropertySet->getPropertyValue("NumberingRules") >>= xNumberingRules;
                comphelper::SequenceAsHashMap aLevelRule(xNumberingRules->getByIndex(nLevel));

                // See offapi/com/sun/star/text/NumberingLevel.idl
                aLevelRule["CharStyleName"] >>= sCharStyleName;
                aLevelRule["NumberingType"] >>= nNumberingType;
                // TODO: aLevelRule["Adjust"] >>= nAdjust; // HoriOrientation::LEFT/RIGHT/CENTER
                aLevelRule["PositionAndSpaceMode"] >>= nPositionAndSpaceMode;

                // for css::text::PositionAndSpaceMode::LABEL_ALIGNMENT
                aLevelRule["LabelFollowedBy"] >>= nLabelFollowedBy;
                aLevelRule["ListtabStopPosition"] >>= nListtabStopPosition;
                aLevelRule["FirstLineIndent"] >>= nFirstLineIndent;
                aLevelRule["IndentAt"] >>= nIndentAt;

                // for css::text::PositionAndSpaceMode::LABEL_WIDTH_AND_POSITION
                aLevelRule["LeftMargin"] >>= nLeftMargin;
                aLevelRule["SymbolTextDistance"] >>= nSymbolTextDistance;
                aLevelRule["FirstLineOffset"] >>= nFirstLineOffset;

                aLevelRule["BulletChar"] >>= sBulletChar;
                bHasFont = (aLevelRule["BulletFont"] >>= aBulletFont);
                bHasColor = (aLevelRule["BulletColor"] >>= aBulletColor);
            }

            if (nNumberingType != css::style::NumberingType::BITMAP) // TODO
            {
                if (nPositionAndSpaceMode
                    == css::text::PositionAndSpaceMode::LABEL_WIDTH_AND_POSITION)
                {
                    nIndentAt = nLeftMargin;
                    nFirstLineIndent = nFirstLineOffset;
                    nListtabStopPosition = nSymbolTextDistance;
                    nLabelFollowedBy = SvxNumberFormat::LabelFollowedBy::LISTTAB;
                }

                switch (nLabelFollowedBy)
                {
                    case SvxNumberFormat::LabelFollowedBy::LISTTAB:
                        sLabelString += "\t";
                        break;
                    case SvxNumberFormat::LabelFollowedBy::SPACE:
                        sLabelString += " ";
                        break;
                    case SvxNumberFormat::LabelFollowedBy::NEWLINE:
                        sLabelString += "\n";
                        break;
                }

                css::uno::Reference<css::text::XTextRange> xNumberText(xRange->getStart());
                xNumberText->setString(sLabelString);
                css::uno::Reference<css::beans::XPropertySet> xNumberProps(
                    xNumberText, css::uno::UNO_QUERY_THROW);
                if (!sCharStyleName.isEmpty())
                    xNumberProps->setPropertyValue("CharStyleName", css::uno::Any(sCharStyleName));

                if (nNumberingType == css::style::NumberingType::CHAR_SPECIAL)
                {
                    css::uno::Reference<css::text::XTextRange> xBulletText(xNumberText->getStart());
                    xBulletText->setString(sBulletChar);

                    std::unordered_map<OUString, css::uno::Any> aNameValues;
                    if (bHasFont)
                    {
                        aNameValues.insert({
                            { "CharFontName", css::uno::Any(aBulletFont.Name) },
                            { "CharFontStyleName", css::uno::Any(aBulletFont.StyleName) },
                            { "CharFontFamily", css::uno::Any(aBulletFont.Family) },
                            { "CharFontCharSet", css::uno::Any(aBulletFont.CharSet) },
                            { "CharWeight", css::uno::Any(aBulletFont.Weight) },
                            { "CharUnderline", css::uno::Any(aBulletFont.Underline) },
                            { "CharStrikeout", css::uno::Any(aBulletFont.Strikeout) },
                            { "CharAutoKerning", css::uno::Any(aBulletFont.Kerning) },
                            { "CharFontPitch", css::uno::Any(aBulletFont.Pitch) },
                            { "CharWordMode", css::uno::Any(aBulletFont.WordLineMode) },
                            { "CharRotation", css::uno::Any(static_cast<sal_Int16>(
                                                  std::round(aBulletFont.Orientation * 10))) },
                            });
                        if (aBulletFont.Height)
                            aNameValues["CharHeight"] <<= aBulletFont.Height;
                    }
                    if (bHasColor)
                    {
                        aNameValues["CharColor"] <<= aBulletColor;
                    }

                    if (css::uno::Reference<css::beans::XMultiPropertySet> xBulletMultiProps{
                            xBulletText, css::uno::UNO_QUERY })
                    {
                        xBulletMultiProps->setPropertyValues(
                            comphelper::mapKeysToSequence(aNameValues),
                            comphelper::mapValuesToSequence(aNameValues));
                    }
                    else
                    {
                        css::uno::Reference<css::beans::XPropertySet> xBulletProps(
                            xBulletText, css::uno::UNO_QUERY_THROW);
                        for (const auto& [rName, rVal] : aNameValues)
                            xBulletProps->setPropertyValue(rName, rVal);
                    }
                }
                else
                {
                    // TODO: css::style::NumberingType::BITMAP
                }

                rPropertySet->setPropertyValue("ParaLeftMargin", css::uno::Any(nIndentAt));
                rPropertySet->setPropertyValue("ParaFirstLineIndent", css::uno::Any(nFirstLineIndent));
                if (nLabelFollowedBy == SvxNumberFormat::LabelFollowedBy::LISTTAB)
                {
                    css::uno::Sequence<css::style::TabStop> stops;
                    rPropertySet->getPropertyValue("ParaTabStops") >>= stops;
                    css::style::TabStop tabStop{};
                    tabStop.Position = nListtabStopPosition;
                    tabStop.Alignment = com::sun::star::style::TabAlign::TabAlign_LEFT;
                    tabStop.FillChar = ' ';
                    rPropertySet->setPropertyValue("ParaTabStops",
                                    css::uno::Any(comphelper::combineSequences({ tabStop }, stops)));
                    // FIXME: What if added tap stop is greater than already defined ones?
                }
            }
            else
            {
                continue; // for now, keep such lists as is
            }

            // In case of higher outline levels, each assignment of empty value just sets level 1
            while (rPropertySet->getPropertyValue("NumberingRules") != css::uno::Any())
            {
                rPropertySet->setPropertyValue("NumberingRules", css::uno::Any());
            }
        }
    }
}

OUString
SwVbaListFormat::getServiceImplName()
{
    return "SwVbaListFormat";
}

uno::Sequence< OUString >
SwVbaListFormat::getServiceNames()
{
    static uno::Sequence< OUString > const aServiceNames
    {
        "ooo.vba.word.ListFormat"
    };
    return aServiceNames;
}

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