Debian Codegraph @

sw/source/ui/vba/vbalisthelper.cxx

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
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>
29
30 using namespace ::ooo::vba;
31 using namespace ::com::sun::star;
32
33 const sal_Int32 LIST_LEVEL_COUNT = 9;
34
35 constexpr OUString UNO_NAME_PARENT_NUMBERING = u"ParentNumbering"_ustr;
36 constexpr OUString UNO_NAME_PREFIX = u"Prefix"_ustr;
37 constexpr OUString UNO_NAME_SUFFIX = u"Suffix"_ustr;
38 constexpr OUString UNO_NAME_CHAR_STYLE_NAME = u"CharStyleName"_ustr;
39 constexpr OUString UNO_NAME_NUMBERING_TYPE = u"NumberingType"_ustr;
40 constexpr OUString UNO_NAME_BULLET_CHAR = u"BulletChar"_ustr;
41
42 constexpr OUString CHAR_CLOSED_DOT = u"\u2022"_ustr;
43 constexpr OUStringLiteral CHAR_EMPTY_DOT = u"o";
44 constexpr OUString CHAR_SQUARE = u"\u2540"_ustr;
45 constexpr OUStringLiteral CHAR_STAR_SYMBOL = u"\u272A";
46 constexpr OUString CHAR_FOUR_DIAMONDS = u"\u2756"_ustr;
47 constexpr OUStringLiteral CHAR_DIAMOND = u"\u2726";
48 constexpr OUString CHAR_ARROW = u"\u27A2"_ustr;
49 constexpr OUStringLiteral CHAR_CHECK_MARK = u"\u2713";
50
51 SwVbaListHelper::SwVbaListHelper( css::uno::Reference< css::text::XTextDocument > xTextDoc, sal_Int32 nGalleryType, sal_Int32 nTemplateType ) : mxTextDocument(std::move( xTextDoc )), mnGalleryType( nGalleryType ), mnTemplateType( nTemplateType )
52 {
53 Init();
54 }
55
56 void SwVbaListHelper::Init()
57 {
58 // set the numbering style name
59 switch( mnGalleryType )
60 {
61 case word::WdListGalleryType::wdBulletGallery:
62 {
63 msStyleName = "WdBullet";
64 break;
65 }
66 case word::WdListGalleryType::wdNumberGallery:
67 {
68 msStyleName = "WdNumber";
69 break;
70 }
71 case word::WdListGalleryType::wdOutlineNumberGallery:
72 {
73 msStyleName = "WdOutlineNumber";
74 break;
75 }
76 default:
77 {
78 throw uno::RuntimeException();
79 }
80 }
81 msStyleName += OUString::number( mnTemplateType );
82
83 // get the numbering style
84 uno::Reference< style::XStyleFamiliesSupplier > xStyleSupplier( mxTextDocument, uno::UNO_QUERY_THROW );
85 mxStyleFamily.set( xStyleSupplier->getStyleFamilies()->getByName(u"NumberingStyles"_ustr), uno::UNO_QUERY_THROW );
86 SAL_INFO("sw.vba", "numbering style name: " << msStyleName );
87 if( mxStyleFamily->hasByName( msStyleName ) )
88 {
89 mxStyleProps.set( mxStyleFamily->getByName( msStyleName ), uno::UNO_QUERY_THROW );
90 mxNumberingRules.set( mxStyleProps->getPropertyValue(u"NumberingRules"_ustr), uno::UNO_QUERY_THROW );
91 }
92 else
93 {
94 // create new numbering style
95 uno::Reference< lang::XMultiServiceFactory > xDocMSF( mxTextDocument, uno::UNO_QUERY_THROW );
96 mxStyleProps.set( xDocMSF->createInstance(u"com.sun.star.style.NumberingStyle"_ustr), 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(u"NumberingRules"_ustr) >>= mxNumberingRules;
100
101 CreateListTemplate();
102
103 mxStyleProps->setPropertyValue(u"NumberingRules"_ustr, uno::Any( mxNumberingRules ) );
104 }
105 }
106
107 void SwVbaListHelper::CreateListTemplate()
108 {
109 switch( mnGalleryType )
110 {
111 case word::WdListGalleryType::wdBulletGallery:
112 {
113 CreateBulletListTemplate();
114 break;
115 }
116 case word::WdListGalleryType::wdNumberGallery:
117 {
118 CreateNumberListTemplate();
119 break;
120 }
121 case word::WdListGalleryType::wdOutlineNumberGallery:
122 {
123 CreateOutlineNumberListTemplate();
124 break;
125 }
126 default:
127 {
128 throw uno::RuntimeException();
129 }
130 }
131 }
132
133 void SwVbaListHelper::CreateBulletListTemplate()
134 {
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( u"Bullet Symbols"_ustr ) );
140 setOrAppendPropertyValue( aPropertyValues, UNO_NAME_NUMBERING_TYPE, uno::Any( sal_Int16(style::NumberingType::CHAR_SPECIAL) ) );
141
142 OUString aBulletChar;
143 switch( mnTemplateType )
144 {
145 case 1:
146 {
147 aBulletChar = CHAR_CLOSED_DOT;
148 break;
149 }
150 case 2:
151 {
152 aBulletChar = CHAR_EMPTY_DOT;
153 break;
154 }
155 case 3:
156 {
157 aBulletChar = CHAR_SQUARE;
158 break;
159 }
160 case 4:
161 {
162 aBulletChar = CHAR_STAR_SYMBOL;
163 break;
164 }
165 case 5:
166 {
167 aBulletChar = CHAR_FOUR_DIAMONDS;
168 break;
169 }
170 case 6:
171 {
172 aBulletChar = CHAR_ARROW;
173 break;
174 }
175 case 7:
176 {
177 aBulletChar = CHAR_CHECK_MARK;
178 break;
179 }
180 default:
181 {
182 // we only support 7 types template now
183 throw css::uno::RuntimeException();
184 }
185 }
186 setOrAppendPropertyValue( aPropertyValues, UNO_NAME_BULLET_CHAR, uno::Any( aBulletChar ) );
187
188 mxNumberingRules->replaceByIndex( nLevel, uno::Any( aPropertyValues ) );
189 }
190
191 void SwVbaListHelper::CreateNumberListTemplate()
192 {
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;
197
198 sal_Int16 nNumberingType = 0;
199 OUString sSuffix;
200 switch( mnTemplateType )
201 {
202 case 1:
203 {
204 nNumberingType = style::NumberingType::ARABIC;
205 sSuffix = ".";
206 break;
207 }
208 case 2:
209 {
210 nNumberingType = style::NumberingType::ARABIC;
211 sSuffix = ")";
212 break;
213 }
214 case 3:
215 {
216 nNumberingType = style::NumberingType::ROMAN_UPPER;
217 sSuffix = ".";
218 break;
219 }
220 case 4:
221 {
222 nNumberingType = style::NumberingType::CHARS_UPPER_LETTER;
223 sSuffix = ".";
224 break;
225 }
226 case 5:
227 {
228 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
229 sSuffix = ")";
230 break;
231 }
232 case 6:
233 {
234 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
235 sSuffix = ".";
236 break;
237 }
238 case 7:
239 {
240 nNumberingType = style::NumberingType::ROMAN_LOWER;
241 sSuffix = ".";
242 break;
243 }
244 default:
245 {
246 // we only support 7 types template now
247 throw css::uno::RuntimeException();
248 }
249 }
250 setOrAppendPropertyValue( aPropertyValues, UNO_NAME_NUMBERING_TYPE, uno::Any( nNumberingType ) );
251 setOrAppendPropertyValue( aPropertyValues, UNO_NAME_SUFFIX, uno::Any( sSuffix ) );
252
253 mxNumberingRules->replaceByIndex( nLevel, uno::Any( aPropertyValues ) );
254 }
255
256 void SwVbaListHelper::CreateOutlineNumberListTemplate()
257 {
258 switch( mnTemplateType )
259 {
260 case 1:
261 {
262 CreateOutlineNumberForType1();
263 break;
264 }
265 case 2:
266 {
267 CreateOutlineNumberForType2();
268 break;
269 }
270 case 3:
271 {
272 CreateOutlineNumberForType3();
273 break;
274 }
275 case 4:
276 {
277 CreateOutlineNumberForType4();
278 break;
279 }
280 case 5:
281 {
282 CreateOutlineNumberForType5();
283 break;
284 }
285 case 6:
286 {
287 CreateOutlineNumberForType6();
288 break;
289 }
290 case 7:
291 {
292 CreateOutlineNumberForType7();
293 break;
294 }
295 default:
296 {
297 // we only support 7 types template now
298 throw css::uno::RuntimeException();
299 }
300 }
301 }
302
303 void SwVbaListHelper::CreateOutlineNumberForType1()
304 {
305 sal_Int16 nNumberingType = 0;
306 OUString sPrefix;
307 OUString sSuffix;
308 uno::Sequence< beans::PropertyValue > aPropertyValues;
309
310 for( sal_Int32 nLevel = 0; nLevel < LIST_LEVEL_COUNT; nLevel++ )
311 {
312 mxNumberingRules->getByIndex( nLevel ) >>= aPropertyValues;
313 switch( nLevel )
314 {
315 case 0:
316 case 1:
317 {
318 nNumberingType = style::NumberingType::ARABIC;
319 sPrefix.clear();
320 sSuffix = ")";
321 break;
322 }
323 case 2:
324 {
325 nNumberingType = style::NumberingType::ROMAN_LOWER;
326 sPrefix.clear();
327 sSuffix = ")";
328 break;
329 }
330 case 3:
331 {
332 nNumberingType = style::NumberingType::ARABIC;
333 sPrefix = "(";
334 sSuffix = ")";
335 break;
336 }
337 case 4:
338 {
339 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
340 sPrefix = "(";
341 sSuffix = ")";
342 break;
343 }
344 case 5:
345 {
346 nNumberingType = style::NumberingType::ROMAN_LOWER;
347 sPrefix = "(";
348 sSuffix = ")";
349 break;
350 }
351 case 6:
352 {
353 nNumberingType = style::NumberingType::ARABIC;
354 sPrefix.clear();
355 sSuffix = ".";
356 break;
357 }
358 case 7:
359 {
360 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
361 sPrefix.clear();
362 sSuffix = ".";
363 break;
364 }
365 case 8:
366 {
367 nNumberingType = style::NumberingType::ROMAN_LOWER;
368 sPrefix.clear();
369 sSuffix = ".";
370 break;
371 }
372 }
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 ) );
377 }
378 }
379
380 void SwVbaListHelper::CreateOutlineNumberForType2()
381 {
382 sal_Int16 nParentNumbering = 0;
383 OUString sSuffix( '.' );
384 uno::Sequence< beans::PropertyValue > aPropertyValues;
385
386 for( sal_Int32 nLevel = 0; nLevel < LIST_LEVEL_COUNT; nLevel++ )
387 {
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 )
392 {
393 nParentNumbering = sal_Int16( nLevel - 1 );
394 setOrAppendPropertyValue( aPropertyValues, UNO_NAME_PARENT_NUMBERING, uno::Any( nParentNumbering ) );
395 }
396 mxNumberingRules->replaceByIndex( nLevel, uno::Any( aPropertyValues ) );
397 }
398 }
399
400 void SwVbaListHelper::CreateOutlineNumberForType3()
401 {
402 OUString aBulletChar;
403 uno::Sequence< beans::PropertyValue > aPropertyValues;
404
405 for( sal_Int32 nLevel = 0; nLevel < LIST_LEVEL_COUNT; nLevel++ )
406 {
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( u"Bullet Symbols"_ustr ) );
410 switch( nLevel )
411 {
412 case 0:
413 {
414 aBulletChar = CHAR_FOUR_DIAMONDS;
415 break;
416 }
417 case 1:
418 case 5:
419 {
420 aBulletChar = CHAR_ARROW;
421 break;
422 }
423 case 2:
424 case 6:
425 {
426 aBulletChar = CHAR_SQUARE;
427 break;
428 }
429 case 3:
430 case 7:
431 {
432 aBulletChar = CHAR_CLOSED_DOT;
433 break;
434 }
435 case 4:
436 case 8:
437 {
438 aBulletChar = CHAR_DIAMOND;
439 break;
440 }
441 }
442 setOrAppendPropertyValue( aPropertyValues, UNO_NAME_BULLET_CHAR, uno::Any( aBulletChar ) );
443 mxNumberingRules->replaceByIndex( nLevel, uno::Any( aPropertyValues ) );
444 }
445 }
446
447 void SwVbaListHelper::CreateOutlineNumberForType4()
448 {
449 sal_Int16 nNumberingType = 0;
450 OUString sPrefix;
451 OUString sSuffix;
452 uno::Sequence< beans::PropertyValue > aPropertyValues;
453
454 for( sal_Int32 nLevel = 0; nLevel < LIST_LEVEL_COUNT; nLevel++ )
455 {
456 mxNumberingRules->getByIndex( nLevel ) >>= aPropertyValues;
457 switch( nLevel )
458 {
459 case 0:
460 {
461 nNumberingType = style::NumberingType::ROMAN_UPPER;
462 sPrefix.clear();
463 sSuffix = ".";
464 break;
465 }
466 case 1:
467 {
468 nNumberingType = style::NumberingType::ARABIC;
469 sPrefix.clear();
470 sSuffix = ".";
471 setOrAppendPropertyValue( aPropertyValues, UNO_NAME_PARENT_NUMBERING, uno::Any( sal_Int16(0) ) );
472 break;
473 }
474 case 2:
475 {
476 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
477 sPrefix = "(";
478 sSuffix = ")";
479 break;
480 }
481 case 3:
482 {
483 nNumberingType = style::NumberingType::ROMAN_LOWER;
484 sPrefix = "(";
485 sSuffix = ")";
486 break;
487 }
488 case 4:
489 {
490 nNumberingType = style::NumberingType::ARABIC;
491 sPrefix.clear();
492 sSuffix = ")";
493 break;
494 }
495 case 5:
496 {
497 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
498 sPrefix.clear();
499 sSuffix = ")";
500 break;
501 }
502 case 6:
503 {
504 nNumberingType = style::NumberingType::ROMAN_LOWER;
505 sPrefix.clear();
506 sSuffix = ")";
507 break;
508 }
509 case 7:
510 {
511 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
512 sPrefix.clear();
513 sSuffix = ".";
514 break;
515 }
516 case 8:
517 {
518 nNumberingType = style::NumberingType::ROMAN_LOWER;
519 sPrefix.clear();
520 sSuffix = ".";
521 break;
522 }
523 }
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 ) );
528 }
529 }
530
531 void SwVbaListHelper::CreateOutlineNumberForType5()
532 {
533 sal_Int16 nParentNumbering = 0;
534 uno::Sequence< beans::PropertyValue > aPropertyValues;
535
536 for( sal_Int32 nLevel = 0; nLevel < LIST_LEVEL_COUNT; nLevel++ )
537 {
538 mxNumberingRules->getByIndex( nLevel ) >>= aPropertyValues;
539 setOrAppendPropertyValue( aPropertyValues, UNO_NAME_NUMBERING_TYPE, uno::Any( sal_Int16(style::NumberingType::ARABIC) ) );
540 if( nLevel != 0 )
541 {
542 nParentNumbering = sal_Int16( nLevel - 1 );
543 setOrAppendPropertyValue( aPropertyValues, UNO_NAME_PARENT_NUMBERING, uno::Any( nParentNumbering ) );
544 }
545 mxNumberingRules->replaceByIndex( nLevel, uno::Any( aPropertyValues ) );
546 }
547 }
548
549 void SwVbaListHelper::CreateOutlineNumberForType6()
550 {
551 sal_Int16 nNumberingType = 0;
552 OUString sPrefix;
553 OUString sSuffix;
554 uno::Sequence< beans::PropertyValue > aPropertyValues;
555
556 for( sal_Int32 nLevel = 0; nLevel < LIST_LEVEL_COUNT; nLevel++ )
557 {
558 mxNumberingRules->getByIndex( nLevel ) >>= aPropertyValues;
559 switch( nLevel )
560 {
561 case 0:
562 {
563 nNumberingType = style::NumberingType::ROMAN_UPPER;
564 sPrefix.clear();
565 sSuffix = ".";
566 break;
567 }
568 case 1:
569 {
570 nNumberingType = style::NumberingType::CHARS_UPPER_LETTER;
571 sPrefix.clear();
572 sSuffix = ".";
573 break;
574 }
575 case 2:
576 {
577 nNumberingType = style::NumberingType::ARABIC;
578 sPrefix.clear();
579 sSuffix = ")";
580 break;
581 }
582 case 3:
583 {
584 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
585 sPrefix.clear();
586 sSuffix = ")";
587 break;
588 }
589 case 4:
590 {
591 nNumberingType = style::NumberingType::ARABIC;
592 sPrefix = "(";
593 sSuffix = ")";
594 break;
595 }
596 case 5:
597 {
598 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
599 sPrefix = "(";
600 sSuffix = ")";
601 break;
602 }
603 case 6:
604 {
605 nNumberingType = style::NumberingType::ROMAN_LOWER;
606 sPrefix = "(";
607 sSuffix = ")";
608 break;
609 }
610 case 7:
611 {
612 nNumberingType = style::NumberingType::CHARS_LOWER_LETTER;
613 sPrefix = "(";
614 sSuffix = ".";
615 break;
616 }
617 case 8:
618 {
619 nNumberingType = style::NumberingType::ROMAN_LOWER;
620 sPrefix = "(";
621 sSuffix = ".";
622 break;
623 }
624 }
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 ) );
629 }
630 }
631
632 void SwVbaListHelper::CreateOutlineNumberForType7()
633 {
634 uno::Sequence< beans::PropertyValue > aPropertyValues;
635
636 for( sal_Int32 nLevel = 0; nLevel < LIST_LEVEL_COUNT; nLevel++ )
637 {
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( u"Chapter "_ustr ) );
641 mxNumberingRules->replaceByIndex( nLevel, uno::Any( aPropertyValues ) );
642 }
643 }
644
645 uno::Any SwVbaListHelper::getPropertyValueWithNameAndLevel( sal_Int32 nLevel, const OUString& sName )
646 {
647 uno::Sequence< beans::PropertyValue > aPropertyValues;
648 mxNumberingRules->getByIndex( nLevel ) >>= aPropertyValues;
649 return getPropertyValue( aPropertyValues, sName );
650 }
651
652 void SwVbaListHelper::setPropertyValueWithNameAndLevel( sal_Int32 nLevel, const OUString& sName, const css::uno::Any& aValue )
653 {
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(u"NumberingRules"_ustr, uno::Any( mxNumberingRules ) );
659 }
660
661 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */