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 .
20 #include <com/sun/star/text/HoriOrientation.hpp>
21 #include <com/sun/star/awt/XBitmap.hpp>
22 #include <cppuhelper/supportsservice.hxx>
23 #include <vcl/svapp.hxx>
24 #include <osl/mutex.hxx>
25 #include <vcl/graph.hxx>
26 #include <svtools/grfmgr.hxx>
27 #include <toolkit/helper/vclunohelper.hxx>
29 #include <editeng/brushitem.hxx>
30 #include <editeng/unoprnms.hxx>
31 #include <editeng/numitem.hxx>
32 #include <editeng/eeitem.hxx>
33 #include <editeng/unotext.hxx>
34 #include <editeng/unofdesc.hxx>
35 #include <editeng/unonrule.hxx>
36 #include <editeng/editids.hrc>
37 #include <editeng/numdef.hxx>
38 #include <boost/scoped_array.hpp>
40 using ::com::sun::star::util::XCloneable
;
41 using ::com::sun::star::ucb::XAnyCompare
;
44 using namespace ::std
;
45 using namespace ::com::sun::star
;
46 using namespace ::com::sun::star::uno
;
47 using namespace ::com::sun::star::lang
;
48 using namespace ::com::sun::star::container
;
50 const SvxAdjust aUnoToSvxAdjust
[] =
61 const unsigned short aSvxToUnoAdjust
[] =
63 text::HoriOrientation::LEFT
,
64 text::HoriOrientation::RIGHT
,
65 text::HoriOrientation::FULL
,
66 text::HoriOrientation::CENTER
,
67 text::HoriOrientation::FULL
,
68 text::HoriOrientation::LEFT
71 SvxAdjust
ConvertUnoAdjust( unsigned short nAdjust
)
73 DBG_ASSERT( nAdjust
<= 7, "Enum hat sich geaendert! [CL]" );
74 return aUnoToSvxAdjust
[nAdjust
];
77 unsigned short ConvertUnoAdjust( SvxAdjust eAdjust
)
79 DBG_ASSERT( eAdjust
<= 6, "Enum hat sich geaendert! [CL]" );
80 return aSvxToUnoAdjust
[eAdjust
];
83 UNO3_GETIMPLEMENTATION_IMPL( SvxUnoNumberingRules
);
85 SvxUnoNumberingRules::SvxUnoNumberingRules( const SvxNumRule
& rRule
) throw()
90 SvxUnoNumberingRules::~SvxUnoNumberingRules() throw()
95 void SAL_CALL
SvxUnoNumberingRules::replaceByIndex( sal_Int32 Index
, const uno::Any
& Element
)
96 throw( IllegalArgumentException
, IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
98 SolarMutexGuard aGuard
;
100 if( Index
< 0 || Index
>= maRule
.GetLevelCount() )
101 throw IndexOutOfBoundsException();
103 Sequence
< beans::PropertyValue
> aSeq
;
105 if( !( Element
>>= aSeq
) )
106 throw IllegalArgumentException();
107 setNumberingRuleByIndex( aSeq
, Index
);
111 sal_Int32 SAL_CALL
SvxUnoNumberingRules::getCount() throw( RuntimeException
, std::exception
)
113 SolarMutexGuard aGuard
;
115 return maRule
.GetLevelCount();
118 Any SAL_CALL
SvxUnoNumberingRules::getByIndex( sal_Int32 Index
)
119 throw( IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
121 SolarMutexGuard aGuard
;
123 if( Index
< 0 || Index
>= maRule
.GetLevelCount() )
124 throw IndexOutOfBoundsException();
126 return Any( getNumberingRuleByIndex(Index
) );
130 Type SAL_CALL
SvxUnoNumberingRules::getElementType()
131 throw( RuntimeException
, std::exception
)
133 return cppu::UnoType
<Sequence
< beans::PropertyValue
>>::get();
136 sal_Bool SAL_CALL
SvxUnoNumberingRules::hasElements() throw( RuntimeException
, std::exception
)
142 sal_Int16 SAL_CALL
SvxUnoNumberingRules::compare( const Any
& rAny1
, const Any
& rAny2
) throw(RuntimeException
, std::exception
)
144 return SvxUnoNumberingRules::Compare( rAny1
, rAny2
);
148 Reference
< XCloneable
> SAL_CALL
SvxUnoNumberingRules::createClone( ) throw (RuntimeException
, std::exception
)
150 return new SvxUnoNumberingRules(maRule
);
154 const char pSvxUnoNumberingRulesService
[] = "com.sun.star.text.NumberingRules";
156 OUString SAL_CALL
SvxUnoNumberingRules::getImplementationName( ) throw(RuntimeException
, std::exception
)
158 return OUString( "SvxUnoNumberingRules" );
161 sal_Bool SAL_CALL
SvxUnoNumberingRules::supportsService( const OUString
& ServiceName
) throw(RuntimeException
, std::exception
)
163 return cppu::supportsService(this, ServiceName
);
166 Sequence
< OUString
> SAL_CALL
SvxUnoNumberingRules::getSupportedServiceNames( ) throw(RuntimeException
, std::exception
)
168 OUString
aService( pSvxUnoNumberingRulesService
);
169 Sequence
< OUString
> aSeq( &aService
, 1 );
173 Sequence
<beans::PropertyValue
> SvxUnoNumberingRules::getNumberingRuleByIndex(sal_Int32 nIndex
) const
174 throw (RuntimeException
)
176 // NumberingRule aRule;
177 const SvxNumberFormat
& rFmt
= maRule
.GetLevel((sal_uInt16
) nIndex
);
180 const int nProps
= 15;
181 boost::scoped_array
<beans::PropertyValue
> pArray(new beans::PropertyValue
[nProps
]);
185 aVal
<<= rFmt
.GetNumberingType();
186 beans::PropertyValue
aAlignProp( OUString(UNO_NAME_NRULE_NUMBERINGTYPE
), -1, aVal
, beans::PropertyState_DIRECT_VALUE
);
187 pArray
[nIdx
++] = aAlignProp
;
191 SvxAdjust eAdj
= rFmt
.GetNumAdjust();
192 aVal
<<= ConvertUnoAdjust(eAdj
);
193 pArray
[nIdx
++] = beans::PropertyValue( OUString(UNO_NAME_NRULE_ADJUST
), -1, aVal
, beans::PropertyState_DIRECT_VALUE
);
197 aVal
<<= OUString(rFmt
.GetPrefix());
198 beans::PropertyValue
aPrefixProp( OUString(UNO_NAME_NRULE_PREFIX
), -1, aVal
, beans::PropertyState_DIRECT_VALUE
);
199 pArray
[nIdx
++] = aPrefixProp
;
203 aVal
<<= OUString(rFmt
.GetSuffix());
204 beans::PropertyValue
aSuffixProp( OUString(UNO_NAME_NRULE_SUFFIX
), -1, aVal
, beans::PropertyState_DIRECT_VALUE
);
205 pArray
[nIdx
++] = aSuffixProp
;
208 if(SVX_NUM_CHAR_SPECIAL
== rFmt
.GetNumberingType())
210 sal_Unicode nCode
= rFmt
.GetBulletChar();
211 OUString
aStr( &nCode
, 1 );
213 beans::PropertyValue
aBulletProp( OUString("BulletChar"), -1, aVal
, beans::PropertyState_DIRECT_VALUE
);
214 pArray
[nIdx
++] = aBulletProp
;
217 if( rFmt
.GetBulletFont() )
219 awt::FontDescriptor aDesc
;
220 SvxUnoFontDescriptor::ConvertFromFont( *rFmt
.GetBulletFont(), aDesc
);
221 aVal
.setValue(&aDesc
, ::cppu::UnoType
<awt::FontDescriptor
>::get());
222 pArray
[nIdx
++] = beans::PropertyValue( OUString(UNO_NAME_NRULE_BULLET_FONT
), -1, aVal
, beans::PropertyState_DIRECT_VALUE
);
226 const SvxBrushItem
* pBrush
= rFmt
.GetBrush();
227 if(pBrush
&& pBrush
->GetGraphicObject())
229 const GraphicObject
* pGrafObj
= pBrush
->GetGraphicObject();
230 OUString
aURL( UNO_NAME_GRAPHOBJ_URLPREFIX
);
231 aURL
+= OStringToOUString(pGrafObj
->GetUniqueID(),
232 RTL_TEXTENCODING_ASCII_US
);
235 const beans::PropertyValue
aGraphicProp( OUString("GraphicURL"), -1, aVal
, beans::PropertyState_DIRECT_VALUE
);
236 pArray
[nIdx
++] = aGraphicProp
;
241 const Size
aSize( rFmt
.GetGraphicSize() );
242 const awt::Size
aUnoSize( aSize
.Width(), aSize
.Height() );
244 const beans::PropertyValue
aGraphicSizeProp(OUString("GraphicSize"), -1, aVal
, beans::PropertyState_DIRECT_VALUE
);
245 pArray
[nIdx
++] = aGraphicSizeProp
;
248 aVal
<<= (sal_Int16
)rFmt
.GetStart();
249 pArray
[nIdx
++] = beans::PropertyValue(OUString(UNO_NAME_NRULE_START_WITH
), -1, aVal
, beans::PropertyState_DIRECT_VALUE
);
251 aVal
<<= (sal_Int32
)rFmt
.GetAbsLSpace();
252 pArray
[nIdx
++] = beans::PropertyValue(OUString(UNO_NAME_NRULE_LEFT_MARGIN
), -1, aVal
, beans::PropertyState_DIRECT_VALUE
);
254 aVal
<<= (sal_Int32
)rFmt
.GetFirstLineOffset();
255 pArray
[nIdx
++] = beans::PropertyValue(OUString(UNO_NAME_NRULE_FIRST_LINE_OFFSET
), -1, aVal
, beans::PropertyState_DIRECT_VALUE
);
257 pArray
[nIdx
++] = beans::PropertyValue(OUString("SymbolTextDistance"), -1, aVal
, beans::PropertyState_DIRECT_VALUE
);
259 aVal
<<= (sal_Int32
)rFmt
.GetBulletColor().GetColor();
260 pArray
[nIdx
++] = beans::PropertyValue(OUString(UNO_NAME_NRULE_BULLET_COLOR
), -1, aVal
, beans::PropertyState_DIRECT_VALUE
);
262 aVal
<<= (sal_Int16
)rFmt
.GetBulletRelSize();
263 pArray
[nIdx
++] = beans::PropertyValue(OUString(UNO_NAME_NRULE_BULLET_RELSIZE
), -1, aVal
, beans::PropertyState_DIRECT_VALUE
);
265 DBG_ASSERT( nIdx
<= nProps
, "FixMe: overflow in Array!!! [CL]" );
266 Sequence
< beans::PropertyValue
> aSeq(pArray
.get(), nIdx
);
271 void SvxUnoNumberingRules::setNumberingRuleByIndex(const Sequence
<beans::PropertyValue
>& rProperties
, sal_Int32 nIndex
)
272 throw (RuntimeException
, IllegalArgumentException
, std::exception
)
274 SvxNumberFormat
aFmt(maRule
.GetLevel( (sal_uInt16
)nIndex
));
275 const beans::PropertyValue
* pPropArray
= rProperties
.getConstArray();
276 for(int i
= 0; i
< rProperties
.getLength(); i
++)
278 const beans::PropertyValue
& rProp
= pPropArray
[i
];
279 const OUString
& rPropName
= rProp
.Name
;
280 const Any
& aVal
= rProp
.Value
;
282 if ( rPropName
== UNO_NAME_NRULE_NUMBERINGTYPE
)
284 sal_Int16 nSet
= sal_Int16();
287 // There is no reason to limit numbering types.
290 aFmt
.SetNumberingType(nSet
);
294 else if ( rPropName
== UNO_NAME_NRULE_PREFIX
)
297 if( aVal
>>= aPrefix
)
299 aFmt
.SetPrefix(aPrefix
);
303 else if ( rPropName
== UNO_NAME_NRULE_SUFFIX
)
306 if( aVal
>>= aSuffix
)
308 aFmt
.SetSuffix(aSuffix
);
312 else if ( rPropName
== UNO_NAME_NRULE_BULLETID
)
314 sal_Int16 nSet
= sal_Int16();
319 aFmt
.SetBulletChar(nSet
);
324 else if ( rPropName
== "BulletChar" )
331 aFmt
.SetBulletChar(aStr
[0]);
335 aFmt
.SetBulletChar(0);
340 else if ( rPropName
== UNO_NAME_NRULE_ADJUST
)
342 sal_Int16 nAdjust
= sal_Int16();
343 if( aVal
>>= nAdjust
)
345 aFmt
.SetNumAdjust(ConvertUnoAdjust( (unsigned short)nAdjust
));
349 else if ( rPropName
== UNO_NAME_NRULE_BULLET_FONT
)
351 awt::FontDescriptor aDesc
;
355 SvxUnoFontDescriptor::ConvertToFont( aDesc
, aFont
);
356 aFmt
.SetBulletFont(&aFont
);
360 else if ( rPropName
== "Graphic" )
362 Reference
< awt::XBitmap
> xBmp
;
365 Graphic
aGraf( VCLUnoHelper::GetBitmap( xBmp
) );
366 SvxBrushItem
aBrushItem(aGraf
, GPOS_AREA
, SID_ATTR_BRUSH
);
367 aFmt
.SetGraphicBrush( &aBrushItem
);
371 else if ( rPropName
== "GraphicURL" )
376 GraphicObject
aGrafObj( GraphicObject::CreateGraphicObjectFromURL( aURL
) );
377 SvxBrushItem
aBrushItem( aGrafObj
, GPOS_AREA
, SID_ATTR_BRUSH
);
378 aFmt
.SetGraphicBrush( &aBrushItem
);
382 else if ( rPropName
== "GraphicSize" )
385 if( aVal
>>= aUnoSize
)
387 aFmt
.SetGraphicSize( Size( aUnoSize
.Width
, aUnoSize
.Height
) );
391 else if ( rPropName
== UNO_NAME_NRULE_START_WITH
)
393 sal_Int16 nStart
= sal_Int16();
394 if( aVal
>>= nStart
)
396 aFmt
.SetStart( nStart
);
400 else if ( rPropName
== UNO_NAME_NRULE_LEFT_MARGIN
)
402 sal_Int32 nMargin
= 0;
403 if( aVal
>>= nMargin
)
405 aFmt
.SetAbsLSpace((sal_uInt16
)nMargin
);
409 else if ( rPropName
== UNO_NAME_NRULE_FIRST_LINE_OFFSET
)
411 sal_Int32 nMargin
= 0;
412 if( aVal
>>= nMargin
)
414 aFmt
.SetFirstLineOffset((sal_uInt16
)nMargin
);
418 else if ( rPropName
== "SymbolTextDistance" )
420 sal_Int32 nTextDistance
= 0;
421 if( aVal
>>= nTextDistance
)
423 aFmt
.SetCharTextDistance((sal_uInt16
)nTextDistance
);
427 else if ( rPropName
== UNO_NAME_NRULE_BULLET_COLOR
)
429 sal_Int32 nColor
= 0;
430 if( aVal
>>= nColor
)
432 aFmt
.SetBulletColor( static_cast<Color
>(nColor
) );
436 else if ( rPropName
== UNO_NAME_NRULE_BULLET_RELSIZE
)
438 sal_Int16 nSize
= sal_Int16();
441 // [Bug 120650] the slide content corrupt when open in Aoo
442 if ((nSize
>250)||(nSize
<=0))
447 aFmt
.SetBulletRelSize( (short)nSize
);
456 throw IllegalArgumentException();
459 // check that we always have a brush item for bitmap numbering
460 if( aFmt
.GetNumberingType() == SVX_NUM_BITMAP
)
462 if( NULL
== aFmt
.GetBrush() )
464 GraphicObject aGrafObj
;
465 SvxBrushItem
aBrushItem( aGrafObj
, GPOS_AREA
, SID_ATTR_BRUSH
);
466 aFmt
.SetGraphicBrush( &aBrushItem
);
469 maRule
.SetLevel( (sal_uInt16
)nIndex
, aFmt
);
472 const SvxNumRule
& SvxGetNumRule( Reference
< XIndexReplace
> xRule
) throw( IllegalArgumentException
)
474 SvxUnoNumberingRules
* pRule
= SvxUnoNumberingRules::getImplementation( xRule
);
476 throw IllegalArgumentException();
478 return pRule
->getNumRule();
481 com::sun::star::uno::Reference
< com::sun::star::container::XIndexReplace
> SvxCreateNumRule( const SvxNumRule
* pRule
) throw()
483 DBG_ASSERT( pRule
, "No default SvxNumRule!" );
486 return new SvxUnoNumberingRules( *pRule
);
490 SvxNumRule
aDefaultRule( SvxNumRuleFlags::BULLET_REL_SIZE
| SvxNumRuleFlags::BULLET_COLOR
| SvxNumRuleFlags::CHAR_TEXT_DISTANCE
, SVX_MAX_NUM
, false);
491 return new SvxUnoNumberingRules( aDefaultRule
);
495 class SvxUnoNumberingRulesCompare
: public ::cppu::WeakAggImplHelper1
< XAnyCompare
>
498 virtual sal_Int16 SAL_CALL
compare( const Any
& Any1
, const Any
& Any2
) throw(RuntimeException
, std::exception
) SAL_OVERRIDE
;
501 sal_Int16 SAL_CALL
SvxUnoNumberingRulesCompare::compare( const Any
& Any1
, const Any
& Any2
) throw(RuntimeException
, std::exception
)
503 return SvxUnoNumberingRules::Compare( Any1
, Any2
);
506 sal_Int16
SvxUnoNumberingRules::Compare( const Any
& Any1
, const Any
& Any2
)
508 Reference
< XIndexReplace
> x1( Any1
, UNO_QUERY
), x2( Any2
, UNO_QUERY
);
509 if( x1
.is() && x2
.is() )
511 if( x1
.get() == x2
.get() )
514 SvxUnoNumberingRules
* pRule1
= SvxUnoNumberingRules::getImplementation( x1
);
517 SvxUnoNumberingRules
* pRule2
= SvxUnoNumberingRules::getImplementation( x2
);
520 const SvxNumRule
& rRule1
= pRule1
->getNumRule();
521 const SvxNumRule
& rRule2
= pRule2
->getNumRule();
523 const sal_uInt16 nLevelCount1
= rRule1
.GetLevelCount();
524 const sal_uInt16 nLevelCount2
= rRule2
.GetLevelCount();
526 if( nLevelCount1
== 0 || nLevelCount2
== 0 )
529 for( sal_uInt16 i
= 0; (i
< nLevelCount1
) && (i
< nLevelCount2
); i
++ )
531 if( rRule1
.GetLevel(i
) != rRule2
.GetLevel(i
) )
542 Reference
< XAnyCompare
> SvxCreateNumRuleCompare() throw()
544 return new SvxUnoNumberingRulesCompare();
547 ::com::sun::star::uno::Reference
< ::com::sun::star::container::XIndexReplace
> SvxCreateNumRule() throw()
549 SvxNumRule
aTempRule( SvxNumRuleFlags::NONE
, 10, false );
550 return SvxCreateNumRule( &aTempRule
);
553 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */