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 .
19 #include <com/sun/star/beans/XProperty.hpp>
20 #include <com/sun/star/awt/FontWeight.hpp>
21 #include <com/sun/star/awt/FontUnderline.hpp>
22 #include <com/sun/star/awt/FontStrikeout.hpp>
23 #include <com/sun/star/awt/FontSlant.hpp>
24 #include <com/sun/star/text/XSimpleText.hpp>
25 #include <com/sun/star/table/XCellRange.hpp>
26 #include <com/sun/star/table/XCell.hpp>
27 #include <com/sun/star/table/XColumnRowRange.hpp>
28 #include <ooo/vba/excel/XlColorIndex.hpp>
29 #include <ooo/vba/excel/XlUnderlineStyle.hpp>
30 #include <svl/itemset.hxx>
31 #include "excelvbahelper.hxx"
32 #include "vbafont.hxx"
33 #include "scitems.hxx"
34 #include "cellsuno.hxx"
36 using namespace ::ooo::vba
;
37 using namespace ::com::sun::star
;
40 const uno::Reference
< XHelperInterface
>& xParent
,
41 const uno::Reference
< uno::XComponentContext
>& xContext
,
42 const ScVbaPalette
& dPalette
,
43 const uno::Reference
< beans::XPropertySet
>& xPropertySet
,
44 ScCellRangeObj
* pRangeObj
, bool bFormControl
) throw ( uno::RuntimeException
) :
45 ScVbaFont_BASE( xParent
, xContext
, dPalette
.getPalette(), xPropertySet
, bFormControl
),
47 mpRangeObj( pRangeObj
)
52 ScVbaFont::GetDataSet()
54 return mpRangeObj
? excel::ScVbaCellRangeAccess::GetDataSet( mpRangeObj
) : 0;
57 ScVbaFont::~ScVbaFont()
62 static uno::Reference
< beans::XPropertySet
> lcl_TextProperties( uno::Reference
< table::XCell
>& xIf
) throw ( uno::RuntimeException
)
64 uno::Reference
< text::XTextRange
> xTxtRange( xIf
, uno::UNO_QUERY_THROW
);
65 uno::Reference
< text::XSimpleText
> xTxt( xTxtRange
->getText(), uno::UNO_QUERY_THROW
) ;
66 uno::Reference
< beans::XPropertySet
> xProps( xTxt
->createTextCursor(), uno::UNO_QUERY_THROW
);
70 ScVbaFont::setSuperscript( const uno::Any
& aValue
) throw ( uno::RuntimeException
)
72 // #FIXEME create some sort of generic get/set code where
73 // you can pass a functor
74 // get/set - Super/sub script code is exactly the same
75 // except for the call applied at each cell position
76 uno::Reference
< table::XCell
> xCell( mxFont
, uno::UNO_QUERY
);
77 uno::Reference
< table::XCellRange
> xCellRange( mxFont
, uno::UNO_QUERY
);
80 uno::Reference
< table::XColumnRowRange
> xColumnRowRange(xCellRange
, uno::UNO_QUERY_THROW
);
81 sal_Int32 nCols
= xColumnRowRange
->getColumns()->getCount();
82 sal_Int32 nRows
= xColumnRowRange
->getRows()->getCount();
83 for ( sal_Int32 col
= 0; col
< nCols
; ++col
)
85 for ( sal_Int32 row
= 0; row
< nRows
; ++row
)
87 uno::Reference
< beans::XPropertySet
> xProps( xCellRange
->getCellByPosition( col
, row
) , uno::UNO_QUERY_THROW
);
88 ScVbaFont
aFont( getParent(), mxContext
, mPalette
, xProps
);
89 aFont
.setSuperscript( aValue
);
95 xCell
.set( xCellRange
->getCellByPosition( 0,0 ) );
97 uno::Reference
< beans::XPropertySet
> xProps
= lcl_TextProperties( xCell
);
98 sal_Bool bValue
= false;
100 sal_Int16 nValue
= NORMAL
;
101 sal_Int8 nValue2
= NORMALHEIGHT
;
105 nValue
= SUPERSCRIPT
;
106 nValue2
= SUPERSCRIPTHEIGHT
;
108 xProps
->setPropertyValue("CharEscapement", ( uno::Any
)nValue
);
109 xProps
->setPropertyValue("CharEscapementHeight", ( uno::Any
)nValue2
);
113 ScVbaFont::getSuperscript() throw ( uno::RuntimeException
)
115 uno::Reference
< table::XCell
> xCell( mxFont
, uno::UNO_QUERY
);
116 uno::Reference
< table::XCellRange
> xCellRange( mxFont
, uno::UNO_QUERY
);
119 uno::Reference
< table::XColumnRowRange
> xColumnRowRange(xCellRange
, uno::UNO_QUERY_THROW
);
120 sal_Int32 nCols
= xColumnRowRange
->getColumns()->getCount();
121 sal_Int32 nRows
= xColumnRowRange
->getRows()->getCount();
123 for ( sal_Int32 col
= 0; col
< nCols
; ++col
)
125 for ( sal_Int32 row
= 0; row
< nRows
; ++row
)
127 uno::Reference
< beans::XPropertySet
> xProps( xCellRange
->getCellByPosition( col
, row
), uno::UNO_QUERY_THROW
);
128 ScVbaFont
aFont( getParent(), mxContext
, mPalette
, xProps
);
130 aRes
= aFont
.getSuperscript();
131 else if ( aRes
!= aFont
.getSuperscript() )
138 xCell
.set( xCellRange
->getCellByPosition( 0,0 ) );
139 uno::Reference
< beans::XPropertySet
> xProps
= lcl_TextProperties( xCell
);
141 xProps
->getPropertyValue("CharEscapement") >>= nValue
;
142 return uno::makeAny( ( nValue
== SUPERSCRIPT
) );
146 ScVbaFont::setSubscript( const uno::Any
& aValue
) throw ( uno::RuntimeException
)
148 uno::Reference
< table::XCell
> xCell( mxFont
, uno::UNO_QUERY
);
149 uno::Reference
< table::XCellRange
> xCellRange( mxFont
, uno::UNO_QUERY
);
152 uno::Reference
< table::XColumnRowRange
> xColumnRowRange(xCellRange
, uno::UNO_QUERY_THROW
);
153 sal_Int32 nCols
= xColumnRowRange
->getColumns()->getCount();
154 sal_Int32 nRows
= xColumnRowRange
->getRows()->getCount();
155 for ( sal_Int32 col
= 0; col
< nCols
; ++col
)
157 for ( sal_Int32 row
= 0; row
< nRows
; ++row
)
159 uno::Reference
< beans::XPropertySet
> xProps( xCellRange
->getCellByPosition( col
, row
) , uno::UNO_QUERY_THROW
);
160 ScVbaFont
aFont( getParent(), mxContext
, mPalette
, xProps
);
161 aFont
.setSubscript( aValue
);
167 xCell
.set( xCellRange
->getCellByPosition( 0,0 ) );
168 uno::Reference
< beans::XPropertySet
> xProps
= lcl_TextProperties( xCell
);
170 sal_Bool bValue
= false;
172 sal_Int16 nValue
= NORMAL
;
173 sal_Int8 nValue2
= NORMALHEIGHT
;
178 nValue2
= SUBSCRIPTHEIGHT
;
181 xProps
->setPropertyValue("CharEscapementHeight", ( uno::Any
)nValue2
);
182 xProps
->setPropertyValue("CharEscapement", ( uno::Any
)nValue
);
187 ScVbaFont::getSubscript() throw ( uno::RuntimeException
)
189 uno::Reference
< table::XCell
> xCell( mxFont
, uno::UNO_QUERY
);
190 uno::Reference
< table::XCellRange
> xCellRange( mxFont
, uno::UNO_QUERY
);
193 uno::Reference
< table::XColumnRowRange
> xColumnRowRange(xCellRange
, uno::UNO_QUERY_THROW
);
194 sal_Int32 nCols
= xColumnRowRange
->getColumns()->getCount();
195 sal_Int32 nRows
= xColumnRowRange
->getRows()->getCount();
197 for ( sal_Int32 col
= 0; col
< nCols
; ++col
)
199 for ( sal_Int32 row
= 0; row
< nRows
; ++row
)
201 uno::Reference
< beans::XPropertySet
> xProps( xCellRange
->getCellByPosition( col
, row
), uno::UNO_QUERY_THROW
);
202 ScVbaFont
aFont( getParent(), mxContext
, mPalette
, xProps
);
204 aRes
= aFont
.getSubscript();
205 else if ( aRes
!= aFont
.getSubscript() )
212 xCell
.set( xCellRange
->getCellByPosition( 0,0 ) );
213 uno::Reference
< beans::XPropertySet
> xProps
= lcl_TextProperties( xCell
);
215 short nValue
= NORMAL
;
216 xProps
->getPropertyValue("CharEscapement") >>= nValue
;
217 return uno::makeAny( ( nValue
== SUBSCRIPT
) );
221 ScVbaFont::getSize() throw ( uno::RuntimeException
)
224 if ( GetDataSet()->GetItemState( ATTR_FONT_HEIGHT
, sal_True
, NULL
) == SFX_ITEM_DONTCARE
)
226 return ScVbaFont_BASE::getSize();
230 ScVbaFont::setColorIndex( const uno::Any
& _colorindex
) throw( uno::RuntimeException
)
232 sal_Int32 nIndex
= 0;
233 _colorindex
>>= nIndex
;
234 // #FIXME xlColorIndexAutomatic & xlColorIndexNone are not really
235 // handled properly here
237 if ( !nIndex
|| ( nIndex
== excel::XlColorIndex::xlColorIndexAutomatic
) )
239 nIndex
= 1; // check defualt ( assume black )
240 ScVbaFont_BASE::setColorIndex( uno::makeAny( nIndex
) );
243 ScVbaFont_BASE::setColorIndex( _colorindex
);
248 ScVbaFont::getColorIndex() throw ( uno::RuntimeException
)
251 if ( GetDataSet()->GetItemState( ATTR_FONT_COLOR
, sal_True
, NULL
) == SFX_ITEM_DONTCARE
)
253 return ScVbaFont_BASE::getColorIndex();
256 //////////////////////////////////////////////////////////////////////////////////////////
258 ScVbaFont::setStandardFontSize( const uno::Any
& /*aValue*/ ) throw( uno::RuntimeException
)
261 //mxFont->setPropertyValue("CharSize", ( uno::Any )fValue );
262 throw uno::RuntimeException(
263 OUString("setStandardFontSize not supported"), uno::Reference
< uno::XInterface
>() );
268 ScVbaFont::getStandardFontSize() throw ( uno::RuntimeException
)
271 throw uno::RuntimeException(
272 OUString("getStandardFontSize not supported"), uno::Reference
< uno::XInterface
>() );
273 // return uno::Any();
278 ScVbaFont::setStandardFont( const uno::Any
& /*aValue*/ ) throw( uno::RuntimeException
)
281 throw uno::RuntimeException(
282 OUString("setStandardFont not supported"), uno::Reference
< uno::XInterface
>() );
287 ScVbaFont::getStandardFont() throw ( uno::RuntimeException
)
290 throw uno::RuntimeException(
291 OUString("getStandardFont not supported"), uno::Reference
< uno::XInterface
>() );
292 // return uno::Any();
296 ScVbaFont::setFontStyle( const uno::Any
& aValue
) throw( uno::RuntimeException
)
298 sal_Bool bBold
= false;
299 sal_Bool bItalic
= false;
304 std::vector
< OUString
> aTokens
;
305 sal_Int32 nIndex
= 0;
308 OUString aToken
= aStyles
.getToken( 0, ' ', nIndex
);
309 aTokens
.push_back( aToken
);
310 }while( nIndex
>= 0 );
312 std::vector
< OUString
>::iterator it
;
313 for( it
= aTokens
.begin(); it
!= aTokens
.end(); ++it
)
315 if( (*it
).equalsIgnoreAsciiCase("Bold") )
318 if( (*it
).equalsIgnoreAsciiCase("Italic") )
322 setBold( uno::makeAny( bBold
) );
323 setItalic( uno::makeAny( bItalic
) );
328 ScVbaFont::getFontStyle() throw ( uno::RuntimeException
)
330 OUStringBuffer aStyles
;
331 sal_Bool bValue
= false;
332 getBold() >>= bValue
;
334 aStyles
.appendAscii("Bold");
336 getItalic() >>= bValue
;
339 if( !aStyles
.isEmpty() )
340 aStyles
.appendAscii(" ");
341 aStyles
.appendAscii("Italic");
343 return uno::makeAny( aStyles
.makeStringAndClear() );
347 ScVbaFont::getBold() throw ( uno::RuntimeException
)
350 if ( GetDataSet()->GetItemState( ATTR_FONT_WEIGHT
, sal_True
, NULL
) == SFX_ITEM_DONTCARE
)
352 return ScVbaFont_BASE::getBold();
356 ScVbaFont::setUnderline( const uno::Any
& aValue
) throw ( uno::RuntimeException
)
359 sal_Int32 nValue
= excel::XlUnderlineStyle::xlUnderlineStyleNone
;
363 // NOTE:: #TODO #FIMXE
364 // xlUnderlineStyleDoubleAccounting & xlUnderlineStyleSingleAccounting
365 // don't seem to be supported in Openoffice.
366 // The import filter converts them to single or double underlines as appropriate
367 // So, here at the moment we are similarly silently converting
368 // xlUnderlineStyleSingleAccounting to xlUnderlineStyleSingle.
370 case excel::XlUnderlineStyle::xlUnderlineStyleNone
:
371 nValue
= awt::FontUnderline::NONE
;
373 case excel::XlUnderlineStyle::xlUnderlineStyleSingle
:
374 case excel::XlUnderlineStyle::xlUnderlineStyleSingleAccounting
:
375 nValue
= awt::FontUnderline::SINGLE
;
377 case excel::XlUnderlineStyle::xlUnderlineStyleDouble
:
378 case excel::XlUnderlineStyle::xlUnderlineStyleDoubleAccounting
:
379 nValue
= awt::FontUnderline::DOUBLE
;
382 throw uno::RuntimeException("Unknown value for Underline", uno::Reference
< uno::XInterface
>() );
385 mxFont
->setPropertyValue("CharUnderline", ( uno::Any
)nValue
);
390 ScVbaFont::getUnderline() throw ( uno::RuntimeException
)
393 if ( GetDataSet()->GetItemState( ATTR_FONT_UNDERLINE
, sal_True
, NULL
) == SFX_ITEM_DONTCARE
)
396 sal_Int32 nValue
= awt::FontUnderline::NONE
;
397 mxFont
->getPropertyValue("CharUnderline") >>= nValue
;
400 case awt::FontUnderline::DOUBLE
:
401 nValue
= excel::XlUnderlineStyle::xlUnderlineStyleDouble
;
403 case awt::FontUnderline::SINGLE
:
404 nValue
= excel::XlUnderlineStyle::xlUnderlineStyleSingle
;
406 case awt::FontUnderline::NONE
:
407 nValue
= excel::XlUnderlineStyle::xlUnderlineStyleNone
;
410 throw uno::RuntimeException("Unknown value retrieved for Underline", uno::Reference
< uno::XInterface
>() );
413 return uno::makeAny( nValue
);
417 ScVbaFont::getStrikethrough() throw ( uno::RuntimeException
)
420 if ( GetDataSet()->GetItemState( ATTR_FONT_CROSSEDOUT
, sal_True
, NULL
) == SFX_ITEM_DONTCARE
)
422 return ScVbaFont_BASE::getStrikethrough();
426 ScVbaFont::getShadow() throw (uno::RuntimeException
)
429 if ( GetDataSet()->GetItemState( ATTR_FONT_SHADOWED
, sal_True
, NULL
) == SFX_ITEM_DONTCARE
)
431 return ScVbaFont_BASE::getShadow();
435 ScVbaFont::getItalic() throw ( uno::RuntimeException
)
438 if ( GetDataSet()->GetItemState( ATTR_FONT_POSTURE
, sal_True
, NULL
) == SFX_ITEM_DONTCARE
)
441 return ScVbaFont_BASE::getItalic();
445 ScVbaFont::getName() throw ( uno::RuntimeException
)
448 if ( GetDataSet()->GetItemState( ATTR_FONT
, sal_True
, NULL
) == SFX_ITEM_DONTCARE
)
450 return ScVbaFont_BASE::getName();
453 ScVbaFont::getColor() throw (uno::RuntimeException
)
455 // #TODO #FIXME - behave like getXXX above ( wrt. GetDataSet )
457 aAny
= OORGBToXLRGB( mxFont
->getPropertyValue("CharColor") );
462 ScVbaFont::setOutlineFont( const uno::Any
& aValue
) throw ( uno::RuntimeException
)
464 mxFont
->setPropertyValue("CharContoured", aValue
);
468 ScVbaFont::getOutlineFont() throw (uno::RuntimeException
)
471 if ( GetDataSet()->GetItemState( ATTR_FONT_CONTOUR
, sal_True
, NULL
) == SFX_ITEM_DONTCARE
)
473 return mxFont
->getPropertyValue("CharContoured");
477 ScVbaFont::getServiceImplName()
479 return OUString("ScVbaFont");
482 uno::Sequence
< OUString
>
483 ScVbaFont::getServiceNames()
485 static uno::Sequence
< OUString
> aServiceNames
;
486 if ( aServiceNames
.getLength() == 0 )
488 aServiceNames
.realloc( 1 );
489 aServiceNames
[ 0 ] = "ooo.vba.excel.Font";
491 return aServiceNames
;
494 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */