Bump for 3.6-28
[LibreOffice.git] / editeng / source / uno / unonrule.cxx
blobbd171da234eef4fa4e05674fd750b33b534016b9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <com/sun/star/text/HoriOrientation.hpp>
30 #include <com/sun/star/awt/XBitmap.hpp>
32 #include <vcl/svapp.hxx>
33 #include <osl/mutex.hxx>
34 #include <vcl/graph.hxx>
35 #include <svtools/grfmgr.hxx>
36 #include <toolkit/unohlp.hxx>
38 #include <editeng/brshitem.hxx>
39 #include <editeng/unoprnms.hxx>
40 #include <editeng/numitem.hxx>
41 #include <editeng/eeitem.hxx>
42 #include <editeng/unotext.hxx>
43 #include <editeng/unofdesc.hxx>
44 #include <editeng/unonrule.hxx>
45 #include <editeng/editids.hrc>
47 using ::rtl::OUString;
48 using ::com::sun::star::util::XCloneable;
49 using ::com::sun::star::ucb::XAnyCompare;
52 using namespace ::std;
53 using namespace ::com::sun::star;
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::lang;
56 using namespace ::com::sun::star::container;
58 const SvxAdjust aUnoToSvxAdjust[] =
60 SVX_ADJUST_LEFT,
61 SVX_ADJUST_RIGHT,
62 SVX_ADJUST_CENTER,
63 SVX_ADJUST_LEFT,
64 SVX_ADJUST_LEFT,
65 SVX_ADJUST_LEFT,
66 SVX_ADJUST_BLOCK
69 const unsigned short aSvxToUnoAdjust[] =
71 text::HoriOrientation::LEFT,
72 text::HoriOrientation::RIGHT,
73 text::HoriOrientation::FULL,
74 text::HoriOrientation::CENTER,
75 text::HoriOrientation::FULL,
76 text::HoriOrientation::LEFT
79 SvxAdjust ConvertUnoAdjust( unsigned short nAdjust )
81 DBG_ASSERT( nAdjust <= 7, "Enum hat sich geaendert! [CL]" );
82 return aUnoToSvxAdjust[nAdjust];
85 unsigned short ConvertUnoAdjust( SvxAdjust eAdjust )
87 DBG_ASSERT( eAdjust <= 6, "Enum hat sich geaendert! [CL]" );
88 return aSvxToUnoAdjust[eAdjust];
91 /******************************************************************
92 * SvxUnoNumberingRules
93 ******************************************************************/
95 UNO3_GETIMPLEMENTATION_IMPL( SvxUnoNumberingRules );
97 SvxUnoNumberingRules::SvxUnoNumberingRules( const SvxNumRule& rRule ) throw()
98 : maRule( rRule )
102 SvxUnoNumberingRules::~SvxUnoNumberingRules() throw()
106 //XIndexReplace
107 void SAL_CALL SvxUnoNumberingRules::replaceByIndex( sal_Int32 Index, const uno::Any& Element )
108 throw( IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException )
110 SolarMutexGuard aGuard;
112 if( Index < 0 || Index >= maRule.GetLevelCount() )
113 throw IndexOutOfBoundsException();
115 Sequence< beans::PropertyValue > aSeq;
117 if( !( Element >>= aSeq) )
118 throw IllegalArgumentException();
119 setNumberingRuleByIndex( aSeq, Index );
122 // XIndexAccess
123 sal_Int32 SAL_CALL SvxUnoNumberingRules::getCount() throw( RuntimeException )
125 SolarMutexGuard aGuard;
127 return maRule.GetLevelCount();
130 Any SAL_CALL SvxUnoNumberingRules::getByIndex( sal_Int32 Index )
131 throw( IndexOutOfBoundsException, WrappedTargetException, RuntimeException )
133 SolarMutexGuard aGuard;
135 if( Index < 0 || Index >= maRule.GetLevelCount() )
136 throw IndexOutOfBoundsException();
138 return Any( getNumberingRuleByIndex(Index) );
141 //XElementAccess
142 Type SAL_CALL SvxUnoNumberingRules::getElementType()
143 throw( RuntimeException )
145 return ::getCppuType(( const Sequence< beans::PropertyValue >*)0);
148 sal_Bool SAL_CALL SvxUnoNumberingRules::hasElements() throw( RuntimeException )
150 return sal_True;
153 // XAnyCompare
154 sal_Int16 SAL_CALL SvxUnoNumberingRules::compare( const Any& rAny1, const Any& rAny2 ) throw(RuntimeException)
156 return SvxUnoNumberingRules::Compare( rAny1, rAny2 );
159 // XCloneable
160 Reference< XCloneable > SAL_CALL SvxUnoNumberingRules::createClone( ) throw (RuntimeException)
162 return new SvxUnoNumberingRules(maRule);
165 // XServiceInfo
166 const char pSvxUnoNumberingRulesService[] = "com.sun.star.text.NumberingRules";
168 OUString SAL_CALL SvxUnoNumberingRules::getImplementationName( ) throw(RuntimeException)
170 return OUString( RTL_CONSTASCII_USTRINGPARAM( "SvxUnoNumberingRules" ) );
173 sal_Bool SAL_CALL SvxUnoNumberingRules::supportsService( const OUString& ServiceName ) throw(RuntimeException)
175 return ServiceName == pSvxUnoNumberingRulesService;
178 Sequence< OUString > SAL_CALL SvxUnoNumberingRules::getSupportedServiceNames( ) throw(RuntimeException)
180 OUString aService( RTL_CONSTASCII_USTRINGPARAM( pSvxUnoNumberingRulesService ) );
181 Sequence< OUString > aSeq( &aService, 1 );
182 return aSeq;
185 Sequence<beans::PropertyValue> SvxUnoNumberingRules::getNumberingRuleByIndex( sal_Int32 nIndex) const throw()
187 // NumberingRule aRule;
188 const SvxNumberFormat& rFmt = maRule.GetLevel((sal_uInt16) nIndex);
189 sal_uInt16 nIdx = 0;
191 const int nProps = 15;
192 beans::PropertyValue* pArray = new beans::PropertyValue[nProps];
194 Any aVal;
196 aVal <<= rFmt.GetNumberingType();
197 beans::PropertyValue aAlignProp( OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_NAME_NRULE_NUMBERINGTYPE)), -1, aVal, beans::PropertyState_DIRECT_VALUE);
198 pArray[nIdx++] = aAlignProp;
202 SvxAdjust eAdj = rFmt.GetNumAdjust();
203 aVal <<= ConvertUnoAdjust(eAdj);
204 pArray[nIdx++] = beans::PropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_NAME_NRULE_ADJUST)), -1, aVal, beans::PropertyState_DIRECT_VALUE);
208 aVal <<= OUString(rFmt.GetPrefix());
209 beans::PropertyValue aPrefixProp( OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_NAME_NRULE_PREFIX)), -1, aVal, beans::PropertyState_DIRECT_VALUE);
210 pArray[nIdx++] = aPrefixProp;
214 aVal <<= OUString(rFmt.GetSuffix());
215 beans::PropertyValue aSuffixProp( OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_NAME_NRULE_SUFFIX)), -1, aVal, beans::PropertyState_DIRECT_VALUE);
216 pArray[nIdx++] = aSuffixProp;
220 sal_Unicode nCode = rFmt.GetBulletChar();
221 OUString aStr( &nCode, 1 );
222 aVal <<= aStr;
223 beans::PropertyValue aBulletProp( OUString(RTL_CONSTASCII_USTRINGPARAM("BulletChar")), -1, aVal, beans::PropertyState_DIRECT_VALUE);
224 pArray[nIdx++] = aBulletProp;
227 if( rFmt.GetBulletFont() )
229 awt::FontDescriptor aDesc;
230 SvxUnoFontDescriptor::ConvertFromFont( *rFmt.GetBulletFont(), aDesc );
231 aVal.setValue(&aDesc, ::getCppuType((const awt::FontDescriptor*)0));
232 pArray[nIdx++] = beans::PropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_NAME_NRULE_BULLET_FONT)), -1, aVal, beans::PropertyState_DIRECT_VALUE);
236 const SvxBrushItem* pBrush = rFmt.GetBrush();
237 if(pBrush && pBrush->GetGraphicObject())
239 const GraphicObject* pGrafObj = pBrush->GetGraphicObject();
240 OUString aURL( RTL_CONSTASCII_USTRINGPARAM(UNO_NAME_GRAPHOBJ_URLPREFIX));
241 aURL += OStringToOUString(pGrafObj->GetUniqueID(),
242 RTL_TEXTENCODING_ASCII_US);
244 aVal <<= aURL;
245 const beans::PropertyValue aGraphicProp( OUString(RTL_CONSTASCII_USTRINGPARAM("GraphicURL")), -1, aVal, beans::PropertyState_DIRECT_VALUE);
246 pArray[nIdx++] = aGraphicProp;
251 const Size aSize( rFmt.GetGraphicSize() );
252 const awt::Size aUnoSize( aSize.Width(), aSize.Height() );
253 aVal <<= aUnoSize;
254 const beans::PropertyValue aGraphicSizeProp(OUString(RTL_CONSTASCII_USTRINGPARAM("GraphicSize")), -1, aVal, beans::PropertyState_DIRECT_VALUE );
255 pArray[nIdx++] = aGraphicSizeProp;
258 aVal <<= (sal_Int16)rFmt.GetStart();
259 pArray[nIdx++] = beans::PropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_NAME_NRULE_START_WITH)), -1, aVal, beans::PropertyState_DIRECT_VALUE);
261 aVal <<= (sal_Int32)rFmt.GetAbsLSpace();
262 pArray[nIdx++] = beans::PropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_NAME_NRULE_LEFT_MARGIN)), -1, aVal, beans::PropertyState_DIRECT_VALUE);
264 aVal <<= (sal_Int32)rFmt.GetFirstLineOffset();
265 pArray[nIdx++] = beans::PropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_NAME_NRULE_FIRST_LINE_OFFSET)), -1, aVal, beans::PropertyState_DIRECT_VALUE);
267 pArray[nIdx++] = beans::PropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("SymbolTextDistance")), -1, aVal, beans::PropertyState_DIRECT_VALUE);
269 aVal <<= (sal_Int32)rFmt.GetBulletColor().GetColor();
270 pArray[nIdx++] = beans::PropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_NAME_NRULE_BULLET_COLOR)), -1, aVal, beans::PropertyState_DIRECT_VALUE);
272 aVal <<= (sal_Int16)rFmt.GetBulletRelSize();
273 pArray[nIdx++] = beans::PropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_NAME_NRULE_BULLET_RELSIZE)), -1, aVal, beans::PropertyState_DIRECT_VALUE);
275 DBG_ASSERT( nIdx <= nProps, "FixMe: overflow in Array!!! [CL]" );
276 Sequence< beans::PropertyValue> aSeq(pArray, nIdx);
278 delete [] pArray;
279 return aSeq;
282 void SvxUnoNumberingRules::setNumberingRuleByIndex( const Sequence< beans::PropertyValue >& rProperties, sal_Int32 nIndex)
283 throw( RuntimeException, IllegalArgumentException )
285 SvxNumberFormat aFmt(maRule.GetLevel( (sal_uInt16)nIndex ));
286 const beans::PropertyValue* pPropArray = rProperties.getConstArray();
287 for(int i = 0; i < rProperties.getLength(); i++)
289 const beans::PropertyValue& rProp = pPropArray[i];
290 const OUString& rPropName = rProp.Name;
291 const Any& aVal = rProp.Value;
293 if ( rPropName == UNO_NAME_NRULE_NUMBERINGTYPE )
295 sal_Int16 nSet = sal_Int16();
296 aVal >>= nSet;
298 switch(nSet)
300 case SVX_NUM_BITMAP:
301 case SVX_NUM_CHAR_SPECIAL:
302 case SVX_NUM_ROMAN_UPPER:
303 case SVX_NUM_ROMAN_LOWER:
304 case SVX_NUM_CHARS_UPPER_LETTER:
305 case SVX_NUM_CHARS_LOWER_LETTER:
306 case SVX_NUM_ARABIC:
307 case SVX_NUM_NUMBER_NONE:
308 case SVX_NUM_CHARS_UPPER_LETTER_N:
309 case SVX_NUM_CHARS_LOWER_LETTER_N:
310 aFmt.SetNumberingType(nSet);
311 continue;
314 else if ( rPropName == UNO_NAME_NRULE_PREFIX )
316 OUString aPrefix;
317 if( aVal >>= aPrefix )
319 aFmt.SetPrefix(aPrefix);
320 continue;
323 else if ( rPropName == UNO_NAME_NRULE_SUFFIX )
325 OUString aSuffix;
326 if( aVal >>= aSuffix )
328 aFmt.SetSuffix(aSuffix);
329 continue;
332 else if ( rPropName == UNO_NAME_NRULE_BULLETID )
334 sal_Int16 nSet = sal_Int16();
335 if( aVal >>= nSet )
337 if(nSet < 0x100)
339 aFmt.SetBulletChar(nSet);
340 continue;
344 else if ( rPropName == "BulletChar" )
346 OUString aStr;
347 if( aVal >>= aStr )
349 if(!aStr.isEmpty())
351 aFmt.SetBulletChar(aStr[0]);
353 else
355 aFmt.SetBulletChar(0);
357 continue;
360 else if ( rPropName == UNO_NAME_NRULE_ADJUST )
362 sal_Int16 nAdjust = sal_Int16();
363 if( aVal >>= nAdjust )
365 aFmt.SetNumAdjust(ConvertUnoAdjust( (unsigned short)nAdjust ));
366 continue;
369 else if ( rPropName == UNO_NAME_NRULE_BULLET_FONT )
371 awt::FontDescriptor aDesc;
372 if( aVal >>= aDesc )
374 Font aFont;
375 SvxUnoFontDescriptor::ConvertToFont( aDesc, aFont );
376 aFmt.SetBulletFont(&aFont);
377 continue;
380 else if ( rPropName == "Graphic" )
382 Reference< awt::XBitmap > xBmp;
383 if( aVal >>= xBmp )
385 Graphic aGraf( VCLUnoHelper::GetBitmap( xBmp ) );
386 SvxBrushItem aBrushItem(aGraf, GPOS_AREA, SID_ATTR_BRUSH);
387 aFmt.SetGraphicBrush( &aBrushItem );
388 continue;
391 else if ( rPropName == "GraphicURL" )
393 OUString aURL;
394 if( aVal >>= aURL )
396 GraphicObject aGrafObj( GraphicObject::CreateGraphicObjectFromURL( aURL ) );
397 SvxBrushItem aBrushItem( aGrafObj, GPOS_AREA, SID_ATTR_BRUSH );
398 aFmt.SetGraphicBrush( &aBrushItem );
399 continue;
402 else if ( rPropName == "GraphicSize" )
404 awt::Size aUnoSize;
405 if( aVal >>= aUnoSize )
407 aFmt.SetGraphicSize( Size( aUnoSize.Width, aUnoSize.Height ) );
408 continue;
411 else if ( rPropName == UNO_NAME_NRULE_START_WITH )
413 sal_Int16 nStart = sal_Int16();
414 if( aVal >>= nStart )
416 aFmt.SetStart( nStart );
417 continue;
420 else if ( rPropName == UNO_NAME_NRULE_LEFT_MARGIN )
422 sal_Int32 nMargin = 0;
423 if( aVal >>= nMargin )
425 aFmt.SetAbsLSpace((sal_uInt16)nMargin);
426 continue;
429 else if ( rPropName == UNO_NAME_NRULE_FIRST_LINE_OFFSET )
431 sal_Int32 nMargin = 0;
432 if( aVal >>= nMargin )
434 aFmt.SetFirstLineOffset((sal_uInt16)nMargin);
435 continue;
438 else if ( rPropName == "SymbolTextDistance" )
440 sal_Int32 nTextDistance = 0;
441 if( aVal >>= nTextDistance )
443 aFmt.SetCharTextDistance((sal_uInt16)nTextDistance);
444 continue;
447 else if ( rPropName == UNO_NAME_NRULE_BULLET_COLOR )
449 sal_Int32 nColor = 0;
450 if( aVal >>= nColor )
452 aFmt.SetBulletColor( (Color) nColor );
453 continue;
456 else if ( rPropName == UNO_NAME_NRULE_BULLET_RELSIZE )
458 sal_Int16 nSize = sal_Int16();
459 if( aVal >>= nSize )
461 aFmt.SetBulletRelSize( (short)nSize );
462 continue;
465 else
467 continue;
470 throw IllegalArgumentException();
473 // check that we always have a brush item for bitmap numbering
474 if( aFmt.GetNumberingType() == SVX_NUM_BITMAP )
476 if( NULL == aFmt.GetBrush() )
478 GraphicObject aGrafObj;
479 SvxBrushItem aBrushItem( aGrafObj, GPOS_AREA, SID_ATTR_BRUSH );
480 aFmt.SetGraphicBrush( &aBrushItem );
483 maRule.SetLevel( (sal_uInt16)nIndex, aFmt );
486 ///////////////////////////////////////////////////////////////////////
488 const SvxNumRule& SvxGetNumRule( Reference< XIndexReplace > xRule ) throw( IllegalArgumentException )
490 SvxUnoNumberingRules* pRule = SvxUnoNumberingRules::getImplementation( xRule );
491 if( pRule == NULL )
492 throw IllegalArgumentException();
494 return pRule->getNumRule();
497 com::sun::star::uno::Reference< com::sun::star::container::XIndexReplace > SvxCreateNumRule( const SvxNumRule* pRule ) throw()
499 DBG_ASSERT( pRule, "No default SvxNumRule!" );
500 if( pRule )
502 return new SvxUnoNumberingRules( *pRule );
504 else
506 SvxNumRule aDefaultRule( NUM_BULLET_REL_SIZE|NUM_BULLET_COLOR|NUM_CHAR_TEXT_DISTANCE, 10 , sal_False);
507 return new SvxUnoNumberingRules( aDefaultRule );
512 ///////////////////////////////////////////////////////////////////////
514 class SvxUnoNumberingRulesCompare : public ::cppu::WeakAggImplHelper1< XAnyCompare >
516 public:
517 virtual sal_Int16 SAL_CALL compare( const Any& Any1, const Any& Any2 ) throw(RuntimeException);
520 sal_Int16 SAL_CALL SvxUnoNumberingRulesCompare::compare( const Any& Any1, const Any& Any2 ) throw(RuntimeException)
522 return SvxUnoNumberingRules::Compare( Any1, Any2 );
525 sal_Int16 SvxUnoNumberingRules::Compare( const Any& Any1, const Any& Any2 )
527 Reference< XIndexReplace > x1( Any1, UNO_QUERY ), x2( Any2, UNO_QUERY );
528 if( x1.is() && x2.is() )
530 if( x1.get() == x2.get() )
531 return 0;
533 SvxUnoNumberingRules* pRule1 = SvxUnoNumberingRules::getImplementation( x1 );
534 if( pRule1 )
536 SvxUnoNumberingRules* pRule2 = SvxUnoNumberingRules::getImplementation( x2 );
537 if( pRule2 )
539 const SvxNumRule& rRule1 = pRule1->getNumRule();
540 const SvxNumRule& rRule2 = pRule2->getNumRule();
542 const sal_uInt16 nLevelCount1 = rRule1.GetLevelCount();
543 const sal_uInt16 nLevelCount2 = rRule2.GetLevelCount();
545 if( nLevelCount1 == 0 || nLevelCount2 == 0 )
546 return -1;
548 for( sal_uInt16 i = 0; (i < nLevelCount1) && (i < nLevelCount2); i++ )
550 if( rRule1.GetLevel(i) != rRule2.GetLevel(i) )
551 return -1;
553 return 0;
558 return -1;
561 Reference< XAnyCompare > SvxCreateNumRuleCompare() throw()
563 return new SvxUnoNumberingRulesCompare();
566 ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexReplace > SvxCreateNumRule() throw()
568 SvxNumRule aTempRule( 0, 10, false );
569 return SvxCreateNumRule( &aTempRule );
572 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */