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
[] =
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()
102 SvxUnoNumberingRules::~SvxUnoNumberingRules() throw()
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
);
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
) );
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
)
154 sal_Int16 SAL_CALL
SvxUnoNumberingRules::compare( const Any
& rAny1
, const Any
& rAny2
) throw(RuntimeException
)
156 return SvxUnoNumberingRules::Compare( rAny1
, rAny2
);
160 Reference
< XCloneable
> SAL_CALL
SvxUnoNumberingRules::createClone( ) throw (RuntimeException
)
162 return new SvxUnoNumberingRules(maRule
);
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 );
185 Sequence
<beans::PropertyValue
> SvxUnoNumberingRules::getNumberingRuleByIndex( sal_Int32 nIndex
) const throw()
187 // NumberingRule aRule;
188 const SvxNumberFormat
& rFmt
= maRule
.GetLevel((sal_uInt16
) nIndex
);
191 const int nProps
= 15;
192 beans::PropertyValue
* pArray
= new beans::PropertyValue
[nProps
];
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 );
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
);
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() );
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
);
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();
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
:
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
);
314 else if ( rPropName
== UNO_NAME_NRULE_PREFIX
)
317 if( aVal
>>= aPrefix
)
319 aFmt
.SetPrefix(aPrefix
);
323 else if ( rPropName
== UNO_NAME_NRULE_SUFFIX
)
326 if( aVal
>>= aSuffix
)
328 aFmt
.SetSuffix(aSuffix
);
332 else if ( rPropName
== UNO_NAME_NRULE_BULLETID
)
334 sal_Int16 nSet
= sal_Int16();
339 aFmt
.SetBulletChar(nSet
);
344 else if ( rPropName
== "BulletChar" )
351 aFmt
.SetBulletChar(aStr
[0]);
355 aFmt
.SetBulletChar(0);
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
));
369 else if ( rPropName
== UNO_NAME_NRULE_BULLET_FONT
)
371 awt::FontDescriptor aDesc
;
375 SvxUnoFontDescriptor::ConvertToFont( aDesc
, aFont
);
376 aFmt
.SetBulletFont(&aFont
);
380 else if ( rPropName
== "Graphic" )
382 Reference
< awt::XBitmap
> xBmp
;
385 Graphic
aGraf( VCLUnoHelper::GetBitmap( xBmp
) );
386 SvxBrushItem
aBrushItem(aGraf
, GPOS_AREA
, SID_ATTR_BRUSH
);
387 aFmt
.SetGraphicBrush( &aBrushItem
);
391 else if ( rPropName
== "GraphicURL" )
396 GraphicObject
aGrafObj( GraphicObject::CreateGraphicObjectFromURL( aURL
) );
397 SvxBrushItem
aBrushItem( aGrafObj
, GPOS_AREA
, SID_ATTR_BRUSH
);
398 aFmt
.SetGraphicBrush( &aBrushItem
);
402 else if ( rPropName
== "GraphicSize" )
405 if( aVal
>>= aUnoSize
)
407 aFmt
.SetGraphicSize( Size( aUnoSize
.Width
, aUnoSize
.Height
) );
411 else if ( rPropName
== UNO_NAME_NRULE_START_WITH
)
413 sal_Int16 nStart
= sal_Int16();
414 if( aVal
>>= nStart
)
416 aFmt
.SetStart( nStart
);
420 else if ( rPropName
== UNO_NAME_NRULE_LEFT_MARGIN
)
422 sal_Int32 nMargin
= 0;
423 if( aVal
>>= nMargin
)
425 aFmt
.SetAbsLSpace((sal_uInt16
)nMargin
);
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
);
438 else if ( rPropName
== "SymbolTextDistance" )
440 sal_Int32 nTextDistance
= 0;
441 if( aVal
>>= nTextDistance
)
443 aFmt
.SetCharTextDistance((sal_uInt16
)nTextDistance
);
447 else if ( rPropName
== UNO_NAME_NRULE_BULLET_COLOR
)
449 sal_Int32 nColor
= 0;
450 if( aVal
>>= nColor
)
452 aFmt
.SetBulletColor( (Color
) nColor
);
456 else if ( rPropName
== UNO_NAME_NRULE_BULLET_RELSIZE
)
458 sal_Int16 nSize
= sal_Int16();
461 aFmt
.SetBulletRelSize( (short)nSize
);
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
);
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!" );
502 return new SvxUnoNumberingRules( *pRule
);
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
>
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() )
533 SvxUnoNumberingRules
* pRule1
= SvxUnoNumberingRules::getImplementation( x1
);
536 SvxUnoNumberingRules
* pRule2
= SvxUnoNumberingRules::getImplementation( x2
);
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 )
548 for( sal_uInt16 i
= 0; (i
< nLevelCount1
) && (i
< nLevelCount2
); i
++ )
550 if( rRule1
.GetLevel(i
) != rRule2
.GetLevel(i
) )
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: */