1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
19 #include "vbalisthelper.hxx"
21 #include <vbahelper/vbahelper.hxx>
22 #include <sal/log.hxx>
23 #include <ooo/vba/word/WdListGalleryType.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
27 #include <com/sun/star/style/NumberingType.hpp>
28 #include <com/sun/star/container/XIndexReplace.hpp>
30 using namespace ::ooo::vba
;
31 using namespace ::com::sun::star
;
33 const sal_Int32 LIST_LEVEL_COUNT
= 9;
35 constexpr OUStringLiteral UNO_NAME_PARENT_NUMBERING
= u
"ParentNumbering";
36 constexpr OUStringLiteral UNO_NAME_PREFIX
= u
"Prefix";
37 constexpr OUStringLiteral UNO_NAME_SUFFIX
= u
"Suffix";
38 constexpr OUStringLiteral UNO_NAME_CHAR_STYLE_NAME
= u
"CharStyleName";
39 constexpr OUStringLiteral UNO_NAME_NUMBERING_TYPE
= u
"NumberingType";
40 constexpr OUStringLiteral UNO_NAME_BULLET_CHAR
= u
"BulletChar";
42 constexpr OUStringLiteral CHAR_CLOSED_DOT
= u
"\u2022";
43 constexpr OUStringLiteral CHAR_EMPTY_DOT
= u
"o";
44 constexpr OUStringLiteral CHAR_SQUARE
= u
"\u2540";
45 constexpr OUStringLiteral CHAR_STAR_SYMBOL
= u
"\u272A";
46 constexpr OUStringLiteral CHAR_FOUR_DIAMONDS
= u
"\u2756";
47 constexpr OUStringLiteral CHAR_DIAMOND
= u
"\u2726";
48 constexpr OUStringLiteral CHAR_ARROW
= u
"\u27A2";
49 constexpr OUStringLiteral CHAR_CHECK_MARK
= u
"\u2713";
51 SwVbaListHelper::SwVbaListHelper( css::uno::Reference
< css::text::XTextDocument
> xTextDoc
, sal_Int32 nGalleryType
, sal_Int32 nTemplateType
) : mxTextDocument(std::move( xTextDoc
)), mnGalleryType( nGalleryType
), mnTemplateType( nTemplateType
)
56 void SwVbaListHelper::Init()
58 // set the numbering style name
59 switch( mnGalleryType
)
61 case word::WdListGalleryType::wdBulletGallery
:
63 msStyleName
= "WdBullet";
66 case word::WdListGalleryType::wdNumberGallery
:
68 msStyleName
= "WdNumber";
71 case word::WdListGalleryType::wdOutlineNumberGallery
:
73 msStyleName
= "WdOutlineNumber";
78 throw uno::RuntimeException();
81 msStyleName
+= OUString::number( mnTemplateType
);
83 // get the numbering style
84 uno::Reference
< style::XStyleFamiliesSupplier
> xStyleSupplier( mxTextDocument
, uno::UNO_QUERY_THROW
);
85 mxStyleFamily
.set( xStyleSupplier
->getStyleFamilies()->getByName("NumberingStyles"), uno::UNO_QUERY_THROW
);
86 SAL_INFO("sw.vba", "numbering style name: " << msStyleName
);
87 if( mxStyleFamily
->hasByName( msStyleName
) )
89 mxStyleProps
.set( mxStyleFamily
->getByName( msStyleName
), uno::UNO_QUERY_THROW
);
90 mxNumberingRules
.set( mxStyleProps
->getPropertyValue("NumberingRules"), uno::UNO_QUERY_THROW
);
94 // create new numbering style
95 uno::Reference
< lang::XMultiServiceFactory
> xDocMSF( mxTextDocument
, uno::UNO_QUERY_THROW
);
96 mxStyleProps
.set( xDocMSF
->createInstance("com.sun.star.style.NumberingStyle"), uno::UNO_QUERY_THROW
);
97 // insert this style into style family, or the property NumberingRules doesn't exist.
98 mxStyleFamily
->insertByName( msStyleName
, uno::Any( mxStyleProps
) );
99 mxStyleProps
->getPropertyValue("NumberingRules") >>= mxNumberingRules
;
101 CreateListTemplate();
103 mxStyleProps
->setPropertyValue("NumberingRules", uno::Any( mxNumberingRules
) );
107 void SwVbaListHelper::CreateListTemplate()
109 switch( mnGalleryType
)
111 case word::WdListGalleryType::wdBulletGallery
:
113 CreateBulletListTemplate();
116 case word::WdListGalleryType::wdNumberGallery
:
118 CreateNumberListTemplate();
121 case word::WdListGalleryType::wdOutlineNumberGallery
:
123 CreateOutlineNumberListTemplate();
128 throw uno::RuntimeException();
133 void SwVbaListHelper::CreateBulletListTemplate()
135 // there is only 1 level for each bullet list in MSWord
136 sal_Int32 nLevel
= 0;
137 uno::Sequence
< beans::PropertyValue
> aPropertyValues
;
138 mxNumberingRules
->getByIndex( nLevel
) >>= aPropertyValues
;
139 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_CHAR_STYLE_NAME
, uno::Any( OUString( "Bullet Symbols" ) ) );
140 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_NUMBERING_TYPE
, uno::Any( sal_Int16(style::NumberingType::CHAR_SPECIAL
) ) );
142 OUString aBulletChar
;
143 switch( mnTemplateType
)
147 aBulletChar
= CHAR_CLOSED_DOT
;
152 aBulletChar
= CHAR_EMPTY_DOT
;
157 aBulletChar
= CHAR_SQUARE
;
162 aBulletChar
= CHAR_STAR_SYMBOL
;
167 aBulletChar
= CHAR_FOUR_DIAMONDS
;
172 aBulletChar
= CHAR_ARROW
;
177 aBulletChar
= CHAR_CHECK_MARK
;
182 // we only support 7 types template now
183 throw css::uno::RuntimeException();
186 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_BULLET_CHAR
, uno::Any( aBulletChar
) );
188 mxNumberingRules
->replaceByIndex( nLevel
, uno::Any( aPropertyValues
) );
191 void SwVbaListHelper::CreateNumberListTemplate()
193 // there is only 1 level for each bullet list in MSWord
194 sal_Int32 nLevel
= 0;
195 uno::Sequence
< beans::PropertyValue
> aPropertyValues
;
196 mxNumberingRules
->getByIndex( nLevel
) >>= aPropertyValues
;
198 sal_Int16 nNumberingType
= 0;
200 switch( mnTemplateType
)
204 nNumberingType
= style::NumberingType::ARABIC
;
210 nNumberingType
= style::NumberingType::ARABIC
;
216 nNumberingType
= style::NumberingType::ROMAN_UPPER
;
222 nNumberingType
= style::NumberingType::CHARS_UPPER_LETTER
;
228 nNumberingType
= style::NumberingType::CHARS_LOWER_LETTER
;
234 nNumberingType
= style::NumberingType::CHARS_LOWER_LETTER
;
240 nNumberingType
= style::NumberingType::ROMAN_LOWER
;
246 // we only support 7 types template now
247 throw css::uno::RuntimeException();
250 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_NUMBERING_TYPE
, uno::Any( nNumberingType
) );
251 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_SUFFIX
, uno::Any( sSuffix
) );
253 mxNumberingRules
->replaceByIndex( nLevel
, uno::Any( aPropertyValues
) );
256 void SwVbaListHelper::CreateOutlineNumberListTemplate()
258 switch( mnTemplateType
)
262 CreateOutlineNumberForType1();
267 CreateOutlineNumberForType2();
272 CreateOutlineNumberForType3();
277 CreateOutlineNumberForType4();
282 CreateOutlineNumberForType5();
287 CreateOutlineNumberForType6();
292 CreateOutlineNumberForType7();
297 // we only support 7 types template now
298 throw css::uno::RuntimeException();
303 void SwVbaListHelper::CreateOutlineNumberForType1()
305 sal_Int16 nNumberingType
= 0;
308 uno::Sequence
< beans::PropertyValue
> aPropertyValues
;
310 for( sal_Int32 nLevel
= 0; nLevel
< LIST_LEVEL_COUNT
; nLevel
++ )
312 mxNumberingRules
->getByIndex( nLevel
) >>= aPropertyValues
;
318 nNumberingType
= style::NumberingType::ARABIC
;
325 nNumberingType
= style::NumberingType::ROMAN_LOWER
;
332 nNumberingType
= style::NumberingType::ARABIC
;
339 nNumberingType
= style::NumberingType::CHARS_LOWER_LETTER
;
346 nNumberingType
= style::NumberingType::ROMAN_LOWER
;
353 nNumberingType
= style::NumberingType::ARABIC
;
360 nNumberingType
= style::NumberingType::CHARS_LOWER_LETTER
;
367 nNumberingType
= style::NumberingType::ROMAN_LOWER
;
373 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_NUMBERING_TYPE
, uno::Any( nNumberingType
) );
374 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_PREFIX
, uno::Any( sPrefix
) );
375 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_SUFFIX
, uno::Any( sSuffix
) );
376 mxNumberingRules
->replaceByIndex( nLevel
, uno::Any( aPropertyValues
) );
380 void SwVbaListHelper::CreateOutlineNumberForType2()
382 sal_Int16 nParentNumbering
= 0;
383 OUString
sSuffix( '.' );
384 uno::Sequence
< beans::PropertyValue
> aPropertyValues
;
386 for( sal_Int32 nLevel
= 0; nLevel
< LIST_LEVEL_COUNT
; nLevel
++ )
388 mxNumberingRules
->getByIndex( nLevel
) >>= aPropertyValues
;
389 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_NUMBERING_TYPE
, uno::Any( sal_Int16(style::NumberingType::ARABIC
) ) );
390 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_SUFFIX
, uno::Any( sSuffix
) );
393 nParentNumbering
= sal_Int16( nLevel
- 1 );
394 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_PARENT_NUMBERING
, uno::Any( nParentNumbering
) );
396 mxNumberingRules
->replaceByIndex( nLevel
, uno::Any( aPropertyValues
) );
400 void SwVbaListHelper::CreateOutlineNumberForType3()
402 OUString aBulletChar
;
403 uno::Sequence
< beans::PropertyValue
> aPropertyValues
;
405 for( sal_Int32 nLevel
= 0; nLevel
< LIST_LEVEL_COUNT
; nLevel
++ )
407 mxNumberingRules
->getByIndex( nLevel
) >>= aPropertyValues
;
408 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_NUMBERING_TYPE
, uno::Any( sal_Int16(style::NumberingType::CHAR_SPECIAL
) ) );
409 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_CHAR_STYLE_NAME
, uno::Any( OUString("Bullet Symbols") ) );
414 aBulletChar
= CHAR_FOUR_DIAMONDS
;
420 aBulletChar
= CHAR_ARROW
;
426 aBulletChar
= CHAR_SQUARE
;
432 aBulletChar
= CHAR_CLOSED_DOT
;
438 aBulletChar
= CHAR_DIAMOND
;
442 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_BULLET_CHAR
, uno::Any( aBulletChar
) );
443 mxNumberingRules
->replaceByIndex( nLevel
, uno::Any( aPropertyValues
) );
447 void SwVbaListHelper::CreateOutlineNumberForType4()
449 sal_Int16 nNumberingType
= 0;
452 uno::Sequence
< beans::PropertyValue
> aPropertyValues
;
454 for( sal_Int32 nLevel
= 0; nLevel
< LIST_LEVEL_COUNT
; nLevel
++ )
456 mxNumberingRules
->getByIndex( nLevel
) >>= aPropertyValues
;
461 nNumberingType
= style::NumberingType::ROMAN_UPPER
;
468 nNumberingType
= style::NumberingType::ARABIC
;
471 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_PARENT_NUMBERING
, uno::Any( sal_Int16(0) ) );
476 nNumberingType
= style::NumberingType::CHARS_LOWER_LETTER
;
483 nNumberingType
= style::NumberingType::ROMAN_LOWER
;
490 nNumberingType
= style::NumberingType::ARABIC
;
497 nNumberingType
= style::NumberingType::CHARS_LOWER_LETTER
;
504 nNumberingType
= style::NumberingType::ROMAN_LOWER
;
511 nNumberingType
= style::NumberingType::CHARS_LOWER_LETTER
;
518 nNumberingType
= style::NumberingType::ROMAN_LOWER
;
524 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_NUMBERING_TYPE
, uno::Any( nNumberingType
) );
525 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_PREFIX
, uno::Any( sPrefix
) );
526 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_SUFFIX
, uno::Any( sSuffix
) );
527 mxNumberingRules
->replaceByIndex( nLevel
, uno::Any( aPropertyValues
) );
531 void SwVbaListHelper::CreateOutlineNumberForType5()
533 sal_Int16 nParentNumbering
= 0;
534 uno::Sequence
< beans::PropertyValue
> aPropertyValues
;
536 for( sal_Int32 nLevel
= 0; nLevel
< LIST_LEVEL_COUNT
; nLevel
++ )
538 mxNumberingRules
->getByIndex( nLevel
) >>= aPropertyValues
;
539 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_NUMBERING_TYPE
, uno::Any( sal_Int16(style::NumberingType::ARABIC
) ) );
542 nParentNumbering
= sal_Int16( nLevel
- 1 );
543 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_PARENT_NUMBERING
, uno::Any( nParentNumbering
) );
545 mxNumberingRules
->replaceByIndex( nLevel
, uno::Any( aPropertyValues
) );
549 void SwVbaListHelper::CreateOutlineNumberForType6()
551 sal_Int16 nNumberingType
= 0;
554 uno::Sequence
< beans::PropertyValue
> aPropertyValues
;
556 for( sal_Int32 nLevel
= 0; nLevel
< LIST_LEVEL_COUNT
; nLevel
++ )
558 mxNumberingRules
->getByIndex( nLevel
) >>= aPropertyValues
;
563 nNumberingType
= style::NumberingType::ROMAN_UPPER
;
570 nNumberingType
= style::NumberingType::CHARS_UPPER_LETTER
;
577 nNumberingType
= style::NumberingType::ARABIC
;
584 nNumberingType
= style::NumberingType::CHARS_LOWER_LETTER
;
591 nNumberingType
= style::NumberingType::ARABIC
;
598 nNumberingType
= style::NumberingType::CHARS_LOWER_LETTER
;
605 nNumberingType
= style::NumberingType::ROMAN_LOWER
;
612 nNumberingType
= style::NumberingType::CHARS_LOWER_LETTER
;
619 nNumberingType
= style::NumberingType::ROMAN_LOWER
;
625 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_NUMBERING_TYPE
, uno::Any( nNumberingType
) );
626 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_PREFIX
, uno::Any( sPrefix
) );
627 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_SUFFIX
, uno::Any( sSuffix
) );
628 mxNumberingRules
->replaceByIndex( nLevel
, uno::Any( aPropertyValues
) );
632 void SwVbaListHelper::CreateOutlineNumberForType7()
634 uno::Sequence
< beans::PropertyValue
> aPropertyValues
;
636 for( sal_Int32 nLevel
= 0; nLevel
< LIST_LEVEL_COUNT
; nLevel
++ )
638 mxNumberingRules
->getByIndex( nLevel
) >>= aPropertyValues
;
639 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_NUMBERING_TYPE
, uno::Any( sal_Int16(style::NumberingType::ARABIC
) ) );
640 setOrAppendPropertyValue( aPropertyValues
, UNO_NAME_PREFIX
, uno::Any( OUString("Chapter ") ) );
641 mxNumberingRules
->replaceByIndex( nLevel
, uno::Any( aPropertyValues
) );
645 uno::Any
SwVbaListHelper::getPropertyValueWithNameAndLevel( sal_Int32 nLevel
, const OUString
& sName
)
647 uno::Sequence
< beans::PropertyValue
> aPropertyValues
;
648 mxNumberingRules
->getByIndex( nLevel
) >>= aPropertyValues
;
649 return getPropertyValue( aPropertyValues
, sName
);
652 void SwVbaListHelper::setPropertyValueWithNameAndLevel( sal_Int32 nLevel
, const OUString
& sName
, const css::uno::Any
& aValue
)
654 uno::Sequence
< beans::PropertyValue
> aPropertyValues
;
655 mxNumberingRules
->getByIndex( nLevel
) >>= aPropertyValues
;
656 setOrAppendPropertyValue( aPropertyValues
, sName
, aValue
);
657 mxNumberingRules
->replaceByIndex( nLevel
, uno::Any( aPropertyValues
) );
658 mxStyleProps
->setPropertyValue("NumberingRules", uno::Any( mxNumberingRules
) );
661 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */