update credits
[LibreOffice.git] / editeng / source / uno / unonrule.cxx
blob0aeb0db850feddbb9c217303081821c50698ed33
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 .
20 #include <com/sun/star/text/HoriOrientation.hpp>
21 #include <com/sun/star/awt/XBitmap.hpp>
23 #include <vcl/svapp.hxx>
24 #include <osl/mutex.hxx>
25 #include <vcl/graph.hxx>
26 #include <svtools/grfmgr.hxx>
27 #include <toolkit/unohlp.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>
39 using ::com::sun::star::util::XCloneable;
40 using ::com::sun::star::ucb::XAnyCompare;
43 using namespace ::std;
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::container;
49 const SvxAdjust aUnoToSvxAdjust[] =
51 SVX_ADJUST_LEFT,
52 SVX_ADJUST_RIGHT,
53 SVX_ADJUST_CENTER,
54 SVX_ADJUST_LEFT,
55 SVX_ADJUST_LEFT,
56 SVX_ADJUST_LEFT,
57 SVX_ADJUST_BLOCK
60 const unsigned short aSvxToUnoAdjust[] =
62 text::HoriOrientation::LEFT,
63 text::HoriOrientation::RIGHT,
64 text::HoriOrientation::FULL,
65 text::HoriOrientation::CENTER,
66 text::HoriOrientation::FULL,
67 text::HoriOrientation::LEFT
70 SvxAdjust ConvertUnoAdjust( unsigned short nAdjust )
72 DBG_ASSERT( nAdjust <= 7, "Enum hat sich geaendert! [CL]" );
73 return aUnoToSvxAdjust[nAdjust];
76 unsigned short ConvertUnoAdjust( SvxAdjust eAdjust )
78 DBG_ASSERT( eAdjust <= 6, "Enum hat sich geaendert! [CL]" );
79 return aSvxToUnoAdjust[eAdjust];
82 /******************************************************************
83 * SvxUnoNumberingRules
84 ******************************************************************/
86 UNO3_GETIMPLEMENTATION_IMPL( SvxUnoNumberingRules );
88 SvxUnoNumberingRules::SvxUnoNumberingRules( const SvxNumRule& rRule ) throw()
89 : maRule( rRule )
93 SvxUnoNumberingRules::~SvxUnoNumberingRules() throw()
97 //XIndexReplace
98 void SAL_CALL SvxUnoNumberingRules::replaceByIndex( sal_Int32 Index, const uno::Any& Element )
99 throw( IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException )
101 SolarMutexGuard aGuard;
103 if( Index < 0 || Index >= maRule.GetLevelCount() )
104 throw IndexOutOfBoundsException();
106 Sequence< beans::PropertyValue > aSeq;
108 if( !( Element >>= aSeq) )
109 throw IllegalArgumentException();
110 setNumberingRuleByIndex( aSeq, Index );
113 // XIndexAccess
114 sal_Int32 SAL_CALL SvxUnoNumberingRules::getCount() throw( RuntimeException )
116 SolarMutexGuard aGuard;
118 return maRule.GetLevelCount();
121 Any SAL_CALL SvxUnoNumberingRules::getByIndex( sal_Int32 Index )
122 throw( IndexOutOfBoundsException, WrappedTargetException, RuntimeException )
124 SolarMutexGuard aGuard;
126 if( Index < 0 || Index >= maRule.GetLevelCount() )
127 throw IndexOutOfBoundsException();
129 return Any( getNumberingRuleByIndex(Index) );
132 //XElementAccess
133 Type SAL_CALL SvxUnoNumberingRules::getElementType()
134 throw( RuntimeException )
136 return ::getCppuType(( const Sequence< beans::PropertyValue >*)0);
139 sal_Bool SAL_CALL SvxUnoNumberingRules::hasElements() throw( RuntimeException )
141 return sal_True;
144 // XAnyCompare
145 sal_Int16 SAL_CALL SvxUnoNumberingRules::compare( const Any& rAny1, const Any& rAny2 ) throw(RuntimeException)
147 return SvxUnoNumberingRules::Compare( rAny1, rAny2 );
150 // XCloneable
151 Reference< XCloneable > SAL_CALL SvxUnoNumberingRules::createClone( ) throw (RuntimeException)
153 return new SvxUnoNumberingRules(maRule);
156 // XServiceInfo
157 const char pSvxUnoNumberingRulesService[] = "com.sun.star.text.NumberingRules";
159 OUString SAL_CALL SvxUnoNumberingRules::getImplementationName( ) throw(RuntimeException)
161 return OUString( "SvxUnoNumberingRules" );
164 sal_Bool SAL_CALL SvxUnoNumberingRules::supportsService( const OUString& ServiceName ) throw(RuntimeException)
166 return ServiceName == pSvxUnoNumberingRulesService;
169 Sequence< OUString > SAL_CALL SvxUnoNumberingRules::getSupportedServiceNames( ) throw(RuntimeException)
171 OUString aService( RTL_CONSTASCII_USTRINGPARAM( pSvxUnoNumberingRulesService ) );
172 Sequence< OUString > aSeq( &aService, 1 );
173 return aSeq;
176 Sequence<beans::PropertyValue> SvxUnoNumberingRules::getNumberingRuleByIndex( sal_Int32 nIndex) const throw()
178 // NumberingRule aRule;
179 const SvxNumberFormat& rFmt = maRule.GetLevel((sal_uInt16) nIndex);
180 sal_uInt16 nIdx = 0;
182 const int nProps = 15;
183 beans::PropertyValue* pArray = new beans::PropertyValue[nProps];
185 Any aVal;
187 aVal <<= rFmt.GetNumberingType();
188 beans::PropertyValue aAlignProp( OUString(UNO_NAME_NRULE_NUMBERINGTYPE), -1, aVal, beans::PropertyState_DIRECT_VALUE);
189 pArray[nIdx++] = aAlignProp;
193 SvxAdjust eAdj = rFmt.GetNumAdjust();
194 aVal <<= ConvertUnoAdjust(eAdj);
195 pArray[nIdx++] = beans::PropertyValue( OUString(UNO_NAME_NRULE_ADJUST), -1, aVal, beans::PropertyState_DIRECT_VALUE);
199 aVal <<= OUString(rFmt.GetPrefix());
200 beans::PropertyValue aPrefixProp( OUString(UNO_NAME_NRULE_PREFIX), -1, aVal, beans::PropertyState_DIRECT_VALUE);
201 pArray[nIdx++] = aPrefixProp;
205 aVal <<= OUString(rFmt.GetSuffix());
206 beans::PropertyValue aSuffixProp( OUString(UNO_NAME_NRULE_SUFFIX), -1, aVal, beans::PropertyState_DIRECT_VALUE);
207 pArray[nIdx++] = aSuffixProp;
211 sal_Unicode nCode = rFmt.GetBulletChar();
212 OUString aStr( &nCode, 1 );
213 aVal <<= aStr;
214 beans::PropertyValue aBulletProp( OUString("BulletChar"), -1, aVal, beans::PropertyState_DIRECT_VALUE);
215 pArray[nIdx++] = aBulletProp;
218 if( rFmt.GetBulletFont() )
220 awt::FontDescriptor aDesc;
221 SvxUnoFontDescriptor::ConvertFromFont( *rFmt.GetBulletFont(), aDesc );
222 aVal.setValue(&aDesc, ::getCppuType((const awt::FontDescriptor*)0));
223 pArray[nIdx++] = beans::PropertyValue( OUString(UNO_NAME_NRULE_BULLET_FONT), -1, aVal, beans::PropertyState_DIRECT_VALUE);
227 const SvxBrushItem* pBrush = rFmt.GetBrush();
228 if(pBrush && pBrush->GetGraphicObject())
230 const GraphicObject* pGrafObj = pBrush->GetGraphicObject();
231 OUString aURL( UNO_NAME_GRAPHOBJ_URLPREFIX);
232 aURL += OStringToOUString(pGrafObj->GetUniqueID(),
233 RTL_TEXTENCODING_ASCII_US);
235 aVal <<= aURL;
236 const beans::PropertyValue aGraphicProp( OUString("GraphicURL"), -1, aVal, beans::PropertyState_DIRECT_VALUE);
237 pArray[nIdx++] = aGraphicProp;
242 const Size aSize( rFmt.GetGraphicSize() );
243 const awt::Size aUnoSize( aSize.Width(), aSize.Height() );
244 aVal <<= aUnoSize;
245 const beans::PropertyValue aGraphicSizeProp(OUString("GraphicSize"), -1, aVal, beans::PropertyState_DIRECT_VALUE );
246 pArray[nIdx++] = aGraphicSizeProp;
249 aVal <<= (sal_Int16)rFmt.GetStart();
250 pArray[nIdx++] = beans::PropertyValue(OUString(UNO_NAME_NRULE_START_WITH), -1, aVal, beans::PropertyState_DIRECT_VALUE);
252 aVal <<= (sal_Int32)rFmt.GetAbsLSpace();
253 pArray[nIdx++] = beans::PropertyValue(OUString(UNO_NAME_NRULE_LEFT_MARGIN), -1, aVal, beans::PropertyState_DIRECT_VALUE);
255 aVal <<= (sal_Int32)rFmt.GetFirstLineOffset();
256 pArray[nIdx++] = beans::PropertyValue(OUString(UNO_NAME_NRULE_FIRST_LINE_OFFSET), -1, aVal, beans::PropertyState_DIRECT_VALUE);
258 pArray[nIdx++] = beans::PropertyValue(OUString("SymbolTextDistance"), -1, aVal, beans::PropertyState_DIRECT_VALUE);
260 aVal <<= (sal_Int32)rFmt.GetBulletColor().GetColor();
261 pArray[nIdx++] = beans::PropertyValue(OUString(UNO_NAME_NRULE_BULLET_COLOR), -1, aVal, beans::PropertyState_DIRECT_VALUE);
263 aVal <<= (sal_Int16)rFmt.GetBulletRelSize();
264 pArray[nIdx++] = beans::PropertyValue(OUString(UNO_NAME_NRULE_BULLET_RELSIZE), -1, aVal, beans::PropertyState_DIRECT_VALUE);
266 DBG_ASSERT( nIdx <= nProps, "FixMe: overflow in Array!!! [CL]" );
267 Sequence< beans::PropertyValue> aSeq(pArray, nIdx);
269 delete [] pArray;
270 return aSeq;
273 void SvxUnoNumberingRules::setNumberingRuleByIndex( const Sequence< beans::PropertyValue >& rProperties, sal_Int32 nIndex)
274 throw( RuntimeException, IllegalArgumentException )
276 SvxNumberFormat aFmt(maRule.GetLevel( (sal_uInt16)nIndex ));
277 const beans::PropertyValue* pPropArray = rProperties.getConstArray();
278 for(int i = 0; i < rProperties.getLength(); i++)
280 const beans::PropertyValue& rProp = pPropArray[i];
281 const OUString& rPropName = rProp.Name;
282 const Any& aVal = rProp.Value;
284 if ( rPropName == UNO_NAME_NRULE_NUMBERINGTYPE )
286 sal_Int16 nSet = sal_Int16();
287 aVal >>= nSet;
289 // There is no reason to limit numbering types.
290 if ( nSet>=0 )
292 aFmt.SetNumberingType(nSet);
293 continue;
296 else if ( rPropName == UNO_NAME_NRULE_PREFIX )
298 OUString aPrefix;
299 if( aVal >>= aPrefix )
301 aFmt.SetPrefix(aPrefix);
302 continue;
305 else if ( rPropName == UNO_NAME_NRULE_SUFFIX )
307 OUString aSuffix;
308 if( aVal >>= aSuffix )
310 aFmt.SetSuffix(aSuffix);
311 continue;
314 else if ( rPropName == UNO_NAME_NRULE_BULLETID )
316 sal_Int16 nSet = sal_Int16();
317 if( aVal >>= nSet )
319 if(nSet < 0x100)
321 aFmt.SetBulletChar(nSet);
322 continue;
326 else if ( rPropName == "BulletChar" )
328 OUString aStr;
329 if( aVal >>= aStr )
331 if(!aStr.isEmpty())
333 aFmt.SetBulletChar(aStr[0]);
335 else
337 aFmt.SetBulletChar(0);
339 continue;
342 else if ( rPropName == UNO_NAME_NRULE_ADJUST )
344 sal_Int16 nAdjust = sal_Int16();
345 if( aVal >>= nAdjust )
347 aFmt.SetNumAdjust(ConvertUnoAdjust( (unsigned short)nAdjust ));
348 continue;
351 else if ( rPropName == UNO_NAME_NRULE_BULLET_FONT )
353 awt::FontDescriptor aDesc;
354 if( aVal >>= aDesc )
356 Font aFont;
357 SvxUnoFontDescriptor::ConvertToFont( aDesc, aFont );
358 aFmt.SetBulletFont(&aFont);
359 continue;
362 else if ( rPropName == "Graphic" )
364 Reference< awt::XBitmap > xBmp;
365 if( aVal >>= xBmp )
367 Graphic aGraf( VCLUnoHelper::GetBitmap( xBmp ) );
368 SvxBrushItem aBrushItem(aGraf, GPOS_AREA, SID_ATTR_BRUSH);
369 aFmt.SetGraphicBrush( &aBrushItem );
370 continue;
373 else if ( rPropName == "GraphicURL" )
375 OUString aURL;
376 if( aVal >>= aURL )
378 GraphicObject aGrafObj( GraphicObject::CreateGraphicObjectFromURL( aURL ) );
379 SvxBrushItem aBrushItem( aGrafObj, GPOS_AREA, SID_ATTR_BRUSH );
380 aFmt.SetGraphicBrush( &aBrushItem );
381 continue;
384 else if ( rPropName == "GraphicSize" )
386 awt::Size aUnoSize;
387 if( aVal >>= aUnoSize )
389 aFmt.SetGraphicSize( Size( aUnoSize.Width, aUnoSize.Height ) );
390 continue;
393 else if ( rPropName == UNO_NAME_NRULE_START_WITH )
395 sal_Int16 nStart = sal_Int16();
396 if( aVal >>= nStart )
398 aFmt.SetStart( nStart );
399 continue;
402 else if ( rPropName == UNO_NAME_NRULE_LEFT_MARGIN )
404 sal_Int32 nMargin = 0;
405 if( aVal >>= nMargin )
407 aFmt.SetAbsLSpace((sal_uInt16)nMargin);
408 continue;
411 else if ( rPropName == UNO_NAME_NRULE_FIRST_LINE_OFFSET )
413 sal_Int32 nMargin = 0;
414 if( aVal >>= nMargin )
416 aFmt.SetFirstLineOffset((sal_uInt16)nMargin);
417 continue;
420 else if ( rPropName == "SymbolTextDistance" )
422 sal_Int32 nTextDistance = 0;
423 if( aVal >>= nTextDistance )
425 aFmt.SetCharTextDistance((sal_uInt16)nTextDistance);
426 continue;
429 else if ( rPropName == UNO_NAME_NRULE_BULLET_COLOR )
431 sal_Int32 nColor = 0;
432 if( aVal >>= nColor )
434 aFmt.SetBulletColor( (Color) nColor );
435 continue;
438 else if ( rPropName == UNO_NAME_NRULE_BULLET_RELSIZE )
440 sal_Int16 nSize = sal_Int16();
441 if( aVal >>= nSize )
443 // [Bug 120650] the slide content corrupt when open in Aoo
444 if ((nSize>250)||(nSize<=0))
446 nSize = 100;
449 aFmt.SetBulletRelSize( (short)nSize );
450 continue;
453 else
455 continue;
458 throw IllegalArgumentException();
461 // check that we always have a brush item for bitmap numbering
462 if( aFmt.GetNumberingType() == SVX_NUM_BITMAP )
464 if( NULL == aFmt.GetBrush() )
466 GraphicObject aGrafObj;
467 SvxBrushItem aBrushItem( aGrafObj, GPOS_AREA, SID_ATTR_BRUSH );
468 aFmt.SetGraphicBrush( &aBrushItem );
471 maRule.SetLevel( (sal_uInt16)nIndex, aFmt );
474 ///////////////////////////////////////////////////////////////////////
476 const SvxNumRule& SvxGetNumRule( Reference< XIndexReplace > xRule ) throw( IllegalArgumentException )
478 SvxUnoNumberingRules* pRule = SvxUnoNumberingRules::getImplementation( xRule );
479 if( pRule == NULL )
480 throw IllegalArgumentException();
482 return pRule->getNumRule();
485 com::sun::star::uno::Reference< com::sun::star::container::XIndexReplace > SvxCreateNumRule( const SvxNumRule* pRule ) throw()
487 DBG_ASSERT( pRule, "No default SvxNumRule!" );
488 if( pRule )
490 return new SvxUnoNumberingRules( *pRule );
492 else
494 SvxNumRule aDefaultRule( NUM_BULLET_REL_SIZE|NUM_BULLET_COLOR|NUM_CHAR_TEXT_DISTANCE, SVX_MAX_NUM , sal_False);
495 return new SvxUnoNumberingRules( aDefaultRule );
500 ///////////////////////////////////////////////////////////////////////
502 class SvxUnoNumberingRulesCompare : public ::cppu::WeakAggImplHelper1< XAnyCompare >
504 public:
505 virtual sal_Int16 SAL_CALL compare( const Any& Any1, const Any& Any2 ) throw(RuntimeException);
508 sal_Int16 SAL_CALL SvxUnoNumberingRulesCompare::compare( const Any& Any1, const Any& Any2 ) throw(RuntimeException)
510 return SvxUnoNumberingRules::Compare( Any1, Any2 );
513 sal_Int16 SvxUnoNumberingRules::Compare( const Any& Any1, const Any& Any2 )
515 Reference< XIndexReplace > x1( Any1, UNO_QUERY ), x2( Any2, UNO_QUERY );
516 if( x1.is() && x2.is() )
518 if( x1.get() == x2.get() )
519 return 0;
521 SvxUnoNumberingRules* pRule1 = SvxUnoNumberingRules::getImplementation( x1 );
522 if( pRule1 )
524 SvxUnoNumberingRules* pRule2 = SvxUnoNumberingRules::getImplementation( x2 );
525 if( pRule2 )
527 const SvxNumRule& rRule1 = pRule1->getNumRule();
528 const SvxNumRule& rRule2 = pRule2->getNumRule();
530 const sal_uInt16 nLevelCount1 = rRule1.GetLevelCount();
531 const sal_uInt16 nLevelCount2 = rRule2.GetLevelCount();
533 if( nLevelCount1 == 0 || nLevelCount2 == 0 )
534 return -1;
536 for( sal_uInt16 i = 0; (i < nLevelCount1) && (i < nLevelCount2); i++ )
538 if( rRule1.GetLevel(i) != rRule2.GetLevel(i) )
539 return -1;
541 return 0;
546 return -1;
549 Reference< XAnyCompare > SvxCreateNumRuleCompare() throw()
551 return new SvxUnoNumberingRulesCompare();
554 ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexReplace > SvxCreateNumRule() throw()
556 SvxNumRule aTempRule( 0, 10, false );
557 return SvxCreateNumRule( &aTempRule );
560 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */