1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vbafont.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include <com/sun/star/beans/XProperty.hpp>
31 #include <com/sun/star/awt/FontWeight.hpp>
32 #include <com/sun/star/awt/FontUnderline.hpp>
33 #include <com/sun/star/awt/FontStrikeout.hpp>
34 #include <com/sun/star/awt/FontSlant.hpp>
35 #include <com/sun/star/text/XSimpleText.hpp>
36 #include <com/sun/star/table/XCellRange.hpp>
37 #include <com/sun/star/table/XCell.hpp>
38 #include <com/sun/star/table/XColumnRowRange.hpp>
39 #include <ooo/vba/excel/XlColorIndex.hpp>
40 #include <ooo/vba/excel/XlUnderlineStyle.hpp>
41 #include <svtools/itemset.hxx>
42 #include "vbafont.hxx"
43 #include "scitems.hxx"
44 #include "cellsuno.hxx"
46 using namespace ::ooo::vba
;
47 using namespace ::com::sun::star
;
49 ScVbaFont::ScVbaFont( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
, const ScVbaPalette
& dPalette
, uno::Reference
< beans::XPropertySet
> xPropertySet
, ScCellRangeObj
* pRangeObj
) throw ( uno::RuntimeException
) : ScVbaFont_BASE( xParent
, xContext
, dPalette
.getPalette(), xPropertySet
), mPalette( dPalette
), mpRangeObj( pRangeObj
)
54 ScVbaFont::GetDataSet()
56 SfxItemSet
* pDataSet
= excel::ScVbaCellRangeAccess::GetDataSet( mpRangeObj
);
60 ScVbaFont::~ScVbaFont()
65 uno::Reference
< beans::XPropertySet
> lcl_TextProperties( uno::Reference
< table::XCell
>& xIf
) throw ( uno::RuntimeException
)
67 uno::Reference
< text::XTextRange
> xTxtRange( xIf
, uno::UNO_QUERY_THROW
);
68 uno::Reference
< text::XSimpleText
> xTxt( xTxtRange
->getText(), uno::UNO_QUERY_THROW
) ;
69 uno::Reference
< beans::XPropertySet
> xProps( xTxt
->createTextCursor(), uno::UNO_QUERY_THROW
);
73 ScVbaFont::setSuperscript( const uno::Any
& aValue
) throw ( uno::RuntimeException
)
75 // #FIXEME create some sort of generic get/set code where
76 // you can pass a functor
77 // get/set - Super/sub script code is exactly the same
78 // except for the call applied at each cell position
79 uno::Reference
< table::XCell
> xCell( mxFont
, uno::UNO_QUERY
);
80 uno::Reference
< table::XCellRange
> xCellRange( mxFont
, uno::UNO_QUERY
);
83 uno::Reference
< table::XColumnRowRange
> xColumnRowRange(xCellRange
, uno::UNO_QUERY_THROW
);
84 sal_Int32 nCols
= xColumnRowRange
->getColumns()->getCount();
85 sal_Int32 nRows
= xColumnRowRange
->getRows()->getCount();
86 for ( sal_Int32 col
= 0; col
< nCols
; ++col
)
88 for ( sal_Int32 row
= 0; row
< nRows
; ++row
)
90 uno::Reference
< beans::XPropertySet
> xProps( xCellRange
->getCellByPosition( col
, row
) , uno::UNO_QUERY_THROW
);
91 ScVbaFont
aFont( getParent(), mxContext
, mPalette
, xProps
);
92 aFont
.setSuperscript( aValue
);
98 xCell
.set( xCellRange
->getCellByPosition( 0,0 ) );
100 uno::Reference
< beans::XPropertySet
> xProps
= lcl_TextProperties( xCell
);
101 sal_Bool bValue
= sal_False
;
103 sal_Int16 nValue
= NORMAL
;
104 sal_Int8 nValue2
= NORMALHEIGHT
;
108 nValue
= SUPERSCRIPT
;
109 nValue2
= SUPERSCRIPTHEIGHT
;
111 xProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharEscapement" ) ), ( uno::Any
)nValue
);
112 xProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharEscapementHeight" ) ), ( uno::Any
)nValue2
);
116 ScVbaFont::getSuperscript() throw ( uno::RuntimeException
)
118 uno::Reference
< table::XCell
> xCell( mxFont
, uno::UNO_QUERY
);
119 uno::Reference
< table::XCellRange
> xCellRange( mxFont
, uno::UNO_QUERY
);
122 uno::Reference
< table::XColumnRowRange
> xColumnRowRange(xCellRange
, uno::UNO_QUERY_THROW
);
123 sal_Int32 nCols
= xColumnRowRange
->getColumns()->getCount();
124 sal_Int32 nRows
= xColumnRowRange
->getRows()->getCount();
126 for ( sal_Int32 col
= 0; col
< nCols
; ++col
)
128 for ( sal_Int32 row
= 0; row
< nRows
; ++row
)
130 uno::Reference
< beans::XPropertySet
> xProps( xCellRange
->getCellByPosition( col
, row
), uno::UNO_QUERY_THROW
);
131 ScVbaFont
aFont( getParent(), mxContext
, mPalette
, xProps
);
133 aRes
= aFont
.getSuperscript();
134 else if ( aRes
!= aFont
.getSuperscript() )
141 xCell
.set( xCellRange
->getCellByPosition( 0,0 ) );
142 uno::Reference
< beans::XPropertySet
> xProps
= lcl_TextProperties( xCell
);
144 xProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharEscapement" ) ) ) >>= nValue
;
145 return uno::makeAny( ( nValue
== SUPERSCRIPT
) );
149 ScVbaFont::setSubscript( const uno::Any
& aValue
) throw ( uno::RuntimeException
)
151 uno::Reference
< table::XCell
> xCell( mxFont
, uno::UNO_QUERY
);
152 uno::Reference
< table::XCellRange
> xCellRange( mxFont
, uno::UNO_QUERY
);
155 uno::Reference
< table::XColumnRowRange
> xColumnRowRange(xCellRange
, uno::UNO_QUERY_THROW
);
156 sal_Int32 nCols
= xColumnRowRange
->getColumns()->getCount();
157 sal_Int32 nRows
= xColumnRowRange
->getRows()->getCount();
158 for ( sal_Int32 col
= 0; col
< nCols
; ++col
)
160 for ( sal_Int32 row
= 0; row
< nRows
; ++row
)
162 uno::Reference
< beans::XPropertySet
> xProps( xCellRange
->getCellByPosition( col
, row
) , uno::UNO_QUERY_THROW
);
163 ScVbaFont
aFont( getParent(), mxContext
, mPalette
, xProps
);
164 aFont
.setSubscript( aValue
);
170 xCell
.set( xCellRange
->getCellByPosition( 0,0 ) );
171 uno::Reference
< beans::XPropertySet
> xProps
= lcl_TextProperties( xCell
);
173 sal_Bool bValue
= sal_False
;
175 sal_Int16 nValue
= NORMAL
;
176 sal_Int8 nValue2
= NORMALHEIGHT
;
181 nValue2
= SUBSCRIPTHEIGHT
;
184 xProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharEscapementHeight" ) ), ( uno::Any
)nValue2
);
185 xProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharEscapement" ) ), ( uno::Any
)nValue
);
190 ScVbaFont::getSubscript() throw ( uno::RuntimeException
)
192 uno::Reference
< table::XCell
> xCell( mxFont
, uno::UNO_QUERY
);
193 uno::Reference
< table::XCellRange
> xCellRange( mxFont
, uno::UNO_QUERY
);
196 uno::Reference
< table::XColumnRowRange
> xColumnRowRange(xCellRange
, uno::UNO_QUERY_THROW
);
197 sal_Int32 nCols
= xColumnRowRange
->getColumns()->getCount();
198 sal_Int32 nRows
= xColumnRowRange
->getRows()->getCount();
200 for ( sal_Int32 col
= 0; col
< nCols
; ++col
)
202 for ( sal_Int32 row
= 0; row
< nRows
; ++row
)
204 uno::Reference
< beans::XPropertySet
> xProps( xCellRange
->getCellByPosition( col
, row
), uno::UNO_QUERY_THROW
);
205 ScVbaFont
aFont( getParent(), mxContext
, mPalette
, xProps
);
207 aRes
= aFont
.getSubscript();
208 else if ( aRes
!= aFont
.getSubscript() )
215 xCell
.set( xCellRange
->getCellByPosition( 0,0 ) );
216 uno::Reference
< beans::XPropertySet
> xProps
= lcl_TextProperties( xCell
);
218 short nValue
= NORMAL
;
219 xProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharEscapement" ) ) ) >>= nValue
;
220 return uno::makeAny( ( nValue
== SUBSCRIPT
) );
224 ScVbaFont::getSize() throw ( uno::RuntimeException
)
227 if ( GetDataSet()->GetItemState( ATTR_FONT_HEIGHT
, TRUE
, NULL
) == SFX_ITEM_DONTCARE
)
229 return ScVbaFont_BASE::getSize();
233 ScVbaFont::setColorIndex( const uno::Any
& _colorindex
) throw( uno::RuntimeException
)
235 sal_Int32 nIndex
= 0;
236 _colorindex
>>= nIndex
;
237 // #FIXME xlColorIndexAutomatic & xlColorIndexNone are not really
238 // handled properly here
240 if ( !nIndex
|| ( nIndex
== excel::XlColorIndex::xlColorIndexAutomatic
) )
242 nIndex
= 1; // check defualt ( assume black )
243 ScVbaFont_BASE::setColorIndex( uno::makeAny( nIndex
) );
246 ScVbaFont_BASE::setColorIndex( _colorindex
);
251 ScVbaFont::getColorIndex() throw ( uno::RuntimeException
)
254 if ( GetDataSet()->GetItemState( ATTR_FONT_COLOR
, TRUE
, NULL
) == SFX_ITEM_DONTCARE
)
256 return ScVbaFont_BASE::getColorIndex();
259 //////////////////////////////////////////////////////////////////////////////////////////
261 ScVbaFont::setStandardFontSize( const uno::Any
& /*aValue*/ ) throw( uno::RuntimeException
)
264 //mxFont->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharSize" ) ), ( uno::Any )fValue );
265 throw uno::RuntimeException(
266 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("setStandardFontSize not supported") ), uno::Reference
< uno::XInterface
>() );
271 ScVbaFont::getStandardFontSize() throw ( uno::RuntimeException
)
274 throw uno::RuntimeException(
275 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("getStandardFontSize not supported") ), uno::Reference
< uno::XInterface
>() );
276 // return uno::Any();
281 ScVbaFont::setStandardFont( const uno::Any
& /*aValue*/ ) throw( uno::RuntimeException
)
284 throw uno::RuntimeException(
285 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("setStandardFont not supported") ), uno::Reference
< uno::XInterface
>() );
290 ScVbaFont::getStandardFont() throw ( uno::RuntimeException
)
293 throw uno::RuntimeException(
294 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("getStandardFont not supported") ), uno::Reference
< uno::XInterface
>() );
295 // return uno::Any();
299 ScVbaFont::setFontStyle( const uno::Any
& aValue
) throw( uno::RuntimeException
)
301 sal_Bool bBold
= sal_False
;
302 sal_Bool bItalic
= sal_False
;
304 rtl::OUString aStyles
;
307 std::vector
< rtl::OUString
> aTokens
;
308 sal_Int32 nIndex
= 0;
311 rtl::OUString aToken
= aStyles
.getToken( 0, ' ', nIndex
);
312 aTokens
.push_back( aToken
);
313 }while( nIndex
>= 0 );
315 std::vector
< rtl::OUString
>::iterator it
;
316 for( it
= aTokens
.begin(); it
!= aTokens
.end(); ++it
)
318 if( (*it
).equalsIgnoreAsciiCaseAscii( "Bold" ) )
321 if( (*it
).equalsIgnoreAsciiCaseAscii( "Italic" ) )
325 setBold( uno::makeAny( bBold
) );
326 setItalic( uno::makeAny( bItalic
) );
331 ScVbaFont::getFontStyle() throw ( uno::RuntimeException
)
333 rtl::OUStringBuffer aStyles
;
334 sal_Bool bValue
= sal_False
;
335 getBold() >>= bValue
;
337 aStyles
.appendAscii("Bold");
339 getItalic() >>= bValue
;
342 if( aStyles
.getLength() )
343 aStyles
.appendAscii(" ");
344 aStyles
.appendAscii("Italic");
346 return uno::makeAny( aStyles
.makeStringAndClear() );
350 ScVbaFont::getBold() throw ( uno::RuntimeException
)
353 if ( GetDataSet()->GetItemState( ATTR_FONT_WEIGHT
, TRUE
, NULL
) == SFX_ITEM_DONTCARE
)
355 return ScVbaFont_BASE::getBold();
359 ScVbaFont::setUnderline( const uno::Any
& aValue
) throw ( uno::RuntimeException
)
362 sal_Int32 nValue
= excel::XlUnderlineStyle::xlUnderlineStyleNone
;
366 // NOTE:: #TODO #FIMXE
367 // xlUnderlineStyleDoubleAccounting & xlUnderlineStyleSingleAccounting
368 // don't seem to be supported in Openoffice.
369 // The import filter converts them to single or double underlines as appropriate
370 // So, here at the moment we are similarly silently converting
371 // xlUnderlineStyleSingleAccounting to xlUnderlineStyleSingle.
373 case excel::XlUnderlineStyle::xlUnderlineStyleNone
:
374 nValue
= awt::FontUnderline::NONE
;
376 case excel::XlUnderlineStyle::xlUnderlineStyleSingle
:
377 case excel::XlUnderlineStyle::xlUnderlineStyleSingleAccounting
:
378 nValue
= awt::FontUnderline::SINGLE
;
380 case excel::XlUnderlineStyle::xlUnderlineStyleDouble
:
381 case excel::XlUnderlineStyle::xlUnderlineStyleDoubleAccounting
:
382 nValue
= awt::FontUnderline::DOUBLE
;
385 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Unknown value for Underline")), uno::Reference
< uno::XInterface
>() );
388 mxFont
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharUnderline" ) ), ( uno::Any
)nValue
);
393 ScVbaFont::getUnderline() throw ( uno::RuntimeException
)
396 if ( GetDataSet()->GetItemState( ATTR_FONT_UNDERLINE
, TRUE
, NULL
) == SFX_ITEM_DONTCARE
)
399 sal_Int32 nValue
= awt::FontUnderline::NONE
;
400 mxFont
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharUnderline" ) ) ) >>= nValue
;
403 case awt::FontUnderline::DOUBLE
:
404 nValue
= excel::XlUnderlineStyle::xlUnderlineStyleDouble
;
406 case awt::FontUnderline::SINGLE
:
407 nValue
= excel::XlUnderlineStyle::xlUnderlineStyleSingle
;
409 case awt::FontUnderline::NONE
:
410 nValue
= excel::XlUnderlineStyle::xlUnderlineStyleNone
;
413 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Unknown value retrieved for Underline") ), uno::Reference
< uno::XInterface
>() );
416 return uno::makeAny( nValue
);
420 ScVbaFont::getStrikethrough() throw ( uno::RuntimeException
)
423 if ( GetDataSet()->GetItemState( ATTR_FONT_CROSSEDOUT
, TRUE
, NULL
) == SFX_ITEM_DONTCARE
)
425 return ScVbaFont_BASE::getStrikethrough();
429 ScVbaFont::getShadow() throw (uno::RuntimeException
)
432 if ( GetDataSet()->GetItemState( ATTR_FONT_SHADOWED
, TRUE
, NULL
) == SFX_ITEM_DONTCARE
)
434 return ScVbaFont_BASE::getShadow();
438 ScVbaFont::getItalic() throw ( uno::RuntimeException
)
441 if ( GetDataSet()->GetItemState( ATTR_FONT_POSTURE
, TRUE
, NULL
) == SFX_ITEM_DONTCARE
)
444 return ScVbaFont_BASE::getItalic();
448 ScVbaFont::getName() throw ( uno::RuntimeException
)
451 if ( GetDataSet()->GetItemState( ATTR_FONT
, TRUE
, NULL
) == SFX_ITEM_DONTCARE
)
453 return ScVbaFont_BASE::getName();
456 ScVbaFont::getColor() throw (uno::RuntimeException
)
458 // #TODO #FIXME - behave like getXXX above ( wrt. GetDataSet )
460 aAny
= OORGBToXLRGB( mxFont
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharColor" ) ) ) );
465 ScVbaFont::setOutlineFont( const uno::Any
& aValue
) throw ( uno::RuntimeException
)
467 mxFont
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharContoured" ) ), aValue
);
471 ScVbaFont::getOutlineFont() throw (uno::RuntimeException
)
474 if ( GetDataSet()->GetItemState( ATTR_FONT_CONTOUR
, TRUE
, NULL
) == SFX_ITEM_DONTCARE
)
476 return mxFont
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharContoured" ) ) );
480 ScVbaFont::getServiceImplName()
482 static rtl::OUString
sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaFont") );
486 uno::Sequence
< rtl::OUString
>
487 ScVbaFont::getServiceNames()
489 static uno::Sequence
< rtl::OUString
> aServiceNames
;
490 if ( aServiceNames
.getLength() == 0 )
492 aServiceNames
.realloc( 1 );
493 aServiceNames
[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Font" ) );
495 return aServiceNames
;