android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / ui / vba / vbalisthelper.cxx
blob2de570af0f0c3af14ea74e0739ed7b41ab4f3994
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
20 #include <utility>
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 )
53 Init();
56 void SwVbaListHelper::Init()
58 // set the numbering style name
59 switch( mnGalleryType )
61 case word::WdListGalleryType::wdBulletGallery:
63 msStyleName = "WdBullet";
64 break;
66 case word::WdListGalleryType::wdNumberGallery:
68 msStyleName = "WdNumber";
69 break;
71 case word::WdListGalleryType::wdOutlineNumberGallery:
73 msStyleName = "WdOutlineNumber";
74 break;
76 default:
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 );
92 else
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();
114 break;
116 case word::WdListGalleryType::wdNumberGallery:
118 CreateNumberListTemplate();
119 break;
121 case word::WdListGalleryType::wdOutlineNumberGallery:
123 CreateOutlineNumberListTemplate();
124 break;
126 default:
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 )
145 case 1:
147 aBulletChar = CHAR_CLOSED_DOT;
148 break;
150 case 2:
152 aBulletChar = CHAR_EMPTY_DOT;
153 break;
155 case 3:
157 aBulletChar = CHAR_SQUARE;
158 break;
160 case 4:
162 aBulletChar = CHAR_STAR_SYMBOL;
163 break;
165 case 5:
167 aBulletChar = CHAR_FOUR_DIAMONDS;
168 break;
170 case 6:
172 aBulletChar = CHAR_ARROW;
173 break;
175 case 7:
177 aBulletChar = CHAR_CHECK_MARK;
178 break;
180 default:
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;
199 OUString sSuffix;
200 switch( mnTemplateType )
202 case 1:
204 nNumberingType = style::NumberingType::ARABIC;
205 sSuffix = ".";
206 break;
208 case 2:
210 nNumberingType = style::NumberingType::ARABIC;
211 sSuffix = ")";
212 break;
214 case 3:
216 nNumberingType = style::NumberingType::ROMAN_UPPER;
217 sSuffix = ".";
218 break;
220 case 4:
222 nNumberingType = style::NumberingType::CHARS_UPPER_LETTER;
223 sSuffix = ".";
224 break;
226 case 5:
228 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
229 sSuffix = ")";
230 break;
232 case 6:
234 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
235 sSuffix = ".";
236 break;
238 case 7:
240 nNumberingType = style::NumberingType::ROMAN_LOWER;
241 sSuffix = ".";
242 break;
244 default:
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 )
260 case 1:
262 CreateOutlineNumberForType1();
263 break;
265 case 2:
267 CreateOutlineNumberForType2();
268 break;
270 case 3:
272 CreateOutlineNumberForType3();
273 break;
275 case 4:
277 CreateOutlineNumberForType4();
278 break;
280 case 5:
282 CreateOutlineNumberForType5();
283 break;
285 case 6:
287 CreateOutlineNumberForType6();
288 break;
290 case 7:
292 CreateOutlineNumberForType7();
293 break;
295 default:
297 // we only support 7 types template now
298 throw css::uno::RuntimeException();
303 void SwVbaListHelper::CreateOutlineNumberForType1()
305 sal_Int16 nNumberingType = 0;
306 OUString sPrefix;
307 OUString sSuffix;
308 uno::Sequence< beans::PropertyValue > aPropertyValues;
310 for( sal_Int32 nLevel = 0; nLevel < LIST_LEVEL_COUNT; nLevel++ )
312 mxNumberingRules->getByIndex( nLevel ) >>= aPropertyValues;
313 switch( nLevel )
315 case 0:
316 case 1:
318 nNumberingType = style::NumberingType::ARABIC;
319 sPrefix.clear();
320 sSuffix = ")";
321 break;
323 case 2:
325 nNumberingType = style::NumberingType::ROMAN_LOWER;
326 sPrefix.clear();
327 sSuffix = ")";
328 break;
330 case 3:
332 nNumberingType = style::NumberingType::ARABIC;
333 sPrefix = "(";
334 sSuffix = ")";
335 break;
337 case 4:
339 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
340 sPrefix = "(";
341 sSuffix = ")";
342 break;
344 case 5:
346 nNumberingType = style::NumberingType::ROMAN_LOWER;
347 sPrefix = "(";
348 sSuffix = ")";
349 break;
351 case 6:
353 nNumberingType = style::NumberingType::ARABIC;
354 sPrefix.clear();
355 sSuffix = ".";
356 break;
358 case 7:
360 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
361 sPrefix.clear();
362 sSuffix = ".";
363 break;
365 case 8:
367 nNumberingType = style::NumberingType::ROMAN_LOWER;
368 sPrefix.clear();
369 sSuffix = ".";
370 break;
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 ) );
391 if( nLevel != 0 )
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") ) );
410 switch( nLevel )
412 case 0:
414 aBulletChar = CHAR_FOUR_DIAMONDS;
415 break;
417 case 1:
418 case 5:
420 aBulletChar = CHAR_ARROW;
421 break;
423 case 2:
424 case 6:
426 aBulletChar = CHAR_SQUARE;
427 break;
429 case 3:
430 case 7:
432 aBulletChar = CHAR_CLOSED_DOT;
433 break;
435 case 4:
436 case 8:
438 aBulletChar = CHAR_DIAMOND;
439 break;
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;
450 OUString sPrefix;
451 OUString sSuffix;
452 uno::Sequence< beans::PropertyValue > aPropertyValues;
454 for( sal_Int32 nLevel = 0; nLevel < LIST_LEVEL_COUNT; nLevel++ )
456 mxNumberingRules->getByIndex( nLevel ) >>= aPropertyValues;
457 switch( nLevel )
459 case 0:
461 nNumberingType = style::NumberingType::ROMAN_UPPER;
462 sPrefix.clear();
463 sSuffix = ".";
464 break;
466 case 1:
468 nNumberingType = style::NumberingType::ARABIC;
469 sPrefix.clear();
470 sSuffix = ".";
471 setOrAppendPropertyValue( aPropertyValues, UNO_NAME_PARENT_NUMBERING, uno::Any( sal_Int16(0) ) );
472 break;
474 case 2:
476 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
477 sPrefix = "(";
478 sSuffix = ")";
479 break;
481 case 3:
483 nNumberingType = style::NumberingType::ROMAN_LOWER;
484 sPrefix = "(";
485 sSuffix = ")";
486 break;
488 case 4:
490 nNumberingType = style::NumberingType::ARABIC;
491 sPrefix.clear();
492 sSuffix = ")";
493 break;
495 case 5:
497 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
498 sPrefix.clear();
499 sSuffix = ")";
500 break;
502 case 6:
504 nNumberingType = style::NumberingType::ROMAN_LOWER;
505 sPrefix.clear();
506 sSuffix = ")";
507 break;
509 case 7:
511 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
512 sPrefix.clear();
513 sSuffix = ".";
514 break;
516 case 8:
518 nNumberingType = style::NumberingType::ROMAN_LOWER;
519 sPrefix.clear();
520 sSuffix = ".";
521 break;
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) ) );
540 if( nLevel != 0 )
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;
552 OUString sPrefix;
553 OUString sSuffix;
554 uno::Sequence< beans::PropertyValue > aPropertyValues;
556 for( sal_Int32 nLevel = 0; nLevel < LIST_LEVEL_COUNT; nLevel++ )
558 mxNumberingRules->getByIndex( nLevel ) >>= aPropertyValues;
559 switch( nLevel )
561 case 0:
563 nNumberingType = style::NumberingType::ROMAN_UPPER;
564 sPrefix.clear();
565 sSuffix = ".";
566 break;
568 case 1:
570 nNumberingType = style::NumberingType::CHARS_UPPER_LETTER;
571 sPrefix.clear();
572 sSuffix = ".";
573 break;
575 case 2:
577 nNumberingType = style::NumberingType::ARABIC;
578 sPrefix.clear();
579 sSuffix = ")";
580 break;
582 case 3:
584 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
585 sPrefix.clear();
586 sSuffix = ")";
587 break;
589 case 4:
591 nNumberingType = style::NumberingType::ARABIC;
592 sPrefix = "(";
593 sSuffix = ")";
594 break;
596 case 5:
598 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
599 sPrefix = "(";
600 sSuffix = ")";
601 break;
603 case 6:
605 nNumberingType = style::NumberingType::ROMAN_LOWER;
606 sPrefix = "(";
607 sSuffix = ")";
608 break;
610 case 7:
612 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
613 sPrefix = "(";
614 sSuffix = ".";
615 break;
617 case 8:
619 nNumberingType = style::NumberingType::ROMAN_LOWER;
620 sPrefix = "(";
621 sSuffix = ".";
622 break;
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: */