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()
61 static uno::Reference
< beans::XPropertySet
> lcl_TextProperties( uno::Reference
< table::XCell
>& xIf
) throw ( uno::RuntimeException
)
63 uno::Reference
< text::XTextRange
> xTxtRange( xIf
, uno::UNO_QUERY_THROW
);
64 uno::Reference
< text::XSimpleText
> xTxt( xTxtRange
->getText(), uno::UNO_QUERY_THROW
) ;
65 uno::Reference
< beans::XPropertySet
> xProps( xTxt
->createTextCursor(), uno::UNO_QUERY_THROW
);
69 ScVbaFont::setSuperscript( const uno::Any
& aValue
) throw ( uno::RuntimeException
, std::exception
)
71 // #FIXEME create some sort of generic get/set code where
72 // you can pass a functor
73 // get/set - Super/sub script code is exactly the same
74 // except for the call applied at each cell position
75 uno::Reference
< table::XCell
> xCell( mxFont
, uno::UNO_QUERY
);
76 uno::Reference
< table::XCellRange
> xCellRange( mxFont
, uno::UNO_QUERY
);
79 uno::Reference
< table::XColumnRowRange
> xColumnRowRange(xCellRange
, uno::UNO_QUERY_THROW
);
80 sal_Int32 nCols
= xColumnRowRange
->getColumns()->getCount();
81 sal_Int32 nRows
= xColumnRowRange
->getRows()->getCount();
82 for ( sal_Int32 col
= 0; col
< nCols
; ++col
)
84 for ( sal_Int32 row
= 0; row
< nRows
; ++row
)
86 uno::Reference
< beans::XPropertySet
> xProps( xCellRange
->getCellByPosition( col
, row
) , uno::UNO_QUERY_THROW
);
87 ScVbaFont
aFont( getParent(), mxContext
, mPalette
, xProps
);
88 aFont
.setSuperscript( aValue
);
94 xCell
.set( xCellRange
->getCellByPosition( 0,0 ) );
96 uno::Reference
< beans::XPropertySet
> xProps
= lcl_TextProperties( xCell
);
99 sal_Int16 nValue
= NORMAL
;
100 sal_Int8 nValue2
= NORMALHEIGHT
;
104 nValue
= SUPERSCRIPT
;
105 nValue2
= SUPERSCRIPTHEIGHT
;
107 xProps
->setPropertyValue("CharEscapement", uno::Any(nValue
) );
108 xProps
->setPropertyValue("CharEscapementHeight", uno::Any(nValue2
) );
112 ScVbaFont::getSuperscript() throw ( uno::RuntimeException
, std::exception
)
114 uno::Reference
< table::XCell
> xCell( mxFont
, uno::UNO_QUERY
);
115 uno::Reference
< table::XCellRange
> xCellRange( mxFont
, uno::UNO_QUERY
);
118 uno::Reference
< table::XColumnRowRange
> xColumnRowRange(xCellRange
, uno::UNO_QUERY_THROW
);
119 sal_Int32 nCols
= xColumnRowRange
->getColumns()->getCount();
120 sal_Int32 nRows
= xColumnRowRange
->getRows()->getCount();
122 for ( sal_Int32 col
= 0; col
< nCols
; ++col
)
124 for ( sal_Int32 row
= 0; row
< nRows
; ++row
)
126 uno::Reference
< beans::XPropertySet
> xProps( xCellRange
->getCellByPosition( col
, row
), uno::UNO_QUERY_THROW
);
127 ScVbaFont
aFont( getParent(), mxContext
, mPalette
, xProps
);
129 aRes
= aFont
.getSuperscript();
130 else if ( aRes
!= aFont
.getSuperscript() )
137 xCell
.set( xCellRange
->getCellByPosition( 0,0 ) );
138 uno::Reference
< beans::XPropertySet
> xProps
= lcl_TextProperties( xCell
);
140 xProps
->getPropertyValue("CharEscapement") >>= nValue
;
141 return uno::makeAny( ( nValue
== SUPERSCRIPT
) );
145 ScVbaFont::setSubscript( const uno::Any
& aValue
) throw ( uno::RuntimeException
, std::exception
)
147 uno::Reference
< table::XCell
> xCell( mxFont
, uno::UNO_QUERY
);
148 uno::Reference
< table::XCellRange
> xCellRange( mxFont
, uno::UNO_QUERY
);
151 uno::Reference
< table::XColumnRowRange
> xColumnRowRange(xCellRange
, uno::UNO_QUERY_THROW
);
152 sal_Int32 nCols
= xColumnRowRange
->getColumns()->getCount();
153 sal_Int32 nRows
= xColumnRowRange
->getRows()->getCount();
154 for ( sal_Int32 col
= 0; col
< nCols
; ++col
)
156 for ( sal_Int32 row
= 0; row
< nRows
; ++row
)
158 uno::Reference
< beans::XPropertySet
> xProps( xCellRange
->getCellByPosition( col
, row
) , uno::UNO_QUERY_THROW
);
159 ScVbaFont
aFont( getParent(), mxContext
, mPalette
, xProps
);
160 aFont
.setSubscript( aValue
);
166 xCell
.set( xCellRange
->getCellByPosition( 0,0 ) );
167 uno::Reference
< beans::XPropertySet
> xProps
= lcl_TextProperties( xCell
);
171 sal_Int16 nValue
= NORMAL
;
172 sal_Int8 nValue2
= NORMALHEIGHT
;
177 nValue2
= SUBSCRIPTHEIGHT
;
180 xProps
->setPropertyValue("CharEscapementHeight", uno::Any(nValue2
) );
181 xProps
->setPropertyValue("CharEscapement", uno::Any(nValue
) );
186 ScVbaFont::getSubscript() throw ( uno::RuntimeException
, std::exception
)
188 uno::Reference
< table::XCell
> xCell( mxFont
, uno::UNO_QUERY
);
189 uno::Reference
< table::XCellRange
> xCellRange( mxFont
, uno::UNO_QUERY
);
192 uno::Reference
< table::XColumnRowRange
> xColumnRowRange(xCellRange
, uno::UNO_QUERY_THROW
);
193 sal_Int32 nCols
= xColumnRowRange
->getColumns()->getCount();
194 sal_Int32 nRows
= xColumnRowRange
->getRows()->getCount();
196 for ( sal_Int32 col
= 0; col
< nCols
; ++col
)
198 for ( sal_Int32 row
= 0; row
< nRows
; ++row
)
200 uno::Reference
< beans::XPropertySet
> xProps( xCellRange
->getCellByPosition( col
, row
), uno::UNO_QUERY_THROW
);
201 ScVbaFont
aFont( getParent(), mxContext
, mPalette
, xProps
);
203 aRes
= aFont
.getSubscript();
204 else if ( aRes
!= aFont
.getSubscript() )
211 xCell
.set( xCellRange
->getCellByPosition( 0,0 ) );
212 uno::Reference
< beans::XPropertySet
> xProps
= lcl_TextProperties( xCell
);
214 short nValue
= NORMAL
;
215 xProps
->getPropertyValue("CharEscapement") >>= nValue
;
216 return uno::makeAny( ( nValue
== SUBSCRIPT
) );
220 ScVbaFont::getSize() throw ( uno::RuntimeException
, std::exception
)
223 if ( GetDataSet()->GetItemState( ATTR_FONT_HEIGHT
, true, NULL
) == SfxItemState::DONTCARE
)
225 return ScVbaFont_BASE::getSize();
229 ScVbaFont::setColorIndex( const uno::Any
& _colorindex
) throw( uno::RuntimeException
, std::exception
)
231 sal_Int32 nIndex
= 0;
232 _colorindex
>>= nIndex
;
233 // #FIXME xlColorIndexAutomatic & xlColorIndexNone are not really
234 // handled properly here
236 if ( !nIndex
|| ( nIndex
== excel::XlColorIndex::xlColorIndexAutomatic
) )
238 nIndex
= 1; // check defualt ( assume black )
239 ScVbaFont_BASE::setColorIndex( uno::makeAny( nIndex
) );
242 ScVbaFont_BASE::setColorIndex( _colorindex
);
246 ScVbaFont::getColorIndex() throw ( uno::RuntimeException
, std::exception
)
249 if ( GetDataSet()->GetItemState( ATTR_FONT_COLOR
, true, NULL
) == SfxItemState::DONTCARE
)
251 return ScVbaFont_BASE::getColorIndex();
255 ScVbaFont::setStandardFontSize( const uno::Any
& /*aValue*/ ) throw( uno::RuntimeException
, std::exception
)
258 //mxFont->setPropertyValue("CharSize", ( uno::Any )fValue );
259 throw uno::RuntimeException(
260 "setStandardFontSize not supported" );
264 ScVbaFont::getStandardFontSize() throw ( uno::RuntimeException
, std::exception
)
267 throw uno::RuntimeException( "getStandardFontSize not supported" );
268 // return uno::Any();
272 ScVbaFont::setStandardFont( const uno::Any
& /*aValue*/ ) throw( uno::RuntimeException
, std::exception
)
275 throw uno::RuntimeException("setStandardFont not supported" );
279 ScVbaFont::getStandardFont() throw ( uno::RuntimeException
, std::exception
)
282 throw uno::RuntimeException("getStandardFont not supported");
283 // return uno::Any();
287 ScVbaFont::setFontStyle( const uno::Any
& aValue
) throw( uno::RuntimeException
, std::exception
)
290 bool bItalic
= false;
295 std::vector
< OUString
> aTokens
;
296 sal_Int32 nIndex
= 0;
299 OUString aToken
= aStyles
.getToken( 0, ' ', nIndex
);
300 aTokens
.push_back( aToken
);
301 }while( nIndex
>= 0 );
303 std::vector
< OUString
>::iterator it
;
304 for( it
= aTokens
.begin(); it
!= aTokens
.end(); ++it
)
306 if( (*it
).equalsIgnoreAsciiCase("Bold") )
309 if( (*it
).equalsIgnoreAsciiCase("Italic") )
313 setBold( uno::makeAny( bBold
) );
314 setItalic( uno::makeAny( bItalic
) );
318 ScVbaFont::getFontStyle() throw ( uno::RuntimeException
, std::exception
)
320 OUStringBuffer aStyles
;
322 getBold() >>= bValue
;
324 aStyles
.appendAscii("Bold");
326 getItalic() >>= bValue
;
329 if( !aStyles
.isEmpty() )
330 aStyles
.appendAscii(" ");
331 aStyles
.appendAscii("Italic");
333 return uno::makeAny( aStyles
.makeStringAndClear() );
337 ScVbaFont::getBold() throw ( uno::RuntimeException
, std::exception
)
340 if ( GetDataSet()->GetItemState( ATTR_FONT_WEIGHT
, true, NULL
) == SfxItemState::DONTCARE
)
342 return ScVbaFont_BASE::getBold();
346 ScVbaFont::setUnderline( const uno::Any
& aValue
) throw ( uno::RuntimeException
, std::exception
)
349 sal_Int32 nValue
= excel::XlUnderlineStyle::xlUnderlineStyleNone
;
353 // NOTE:: #TODO #FIMXE
354 // xlUnderlineStyleDoubleAccounting & xlUnderlineStyleSingleAccounting
355 // don't seem to be supported in Openoffice.
356 // The import filter converts them to single or double underlines as appropriate
357 // So, here at the moment we are similarly silently converting
358 // xlUnderlineStyleSingleAccounting to xlUnderlineStyleSingle.
360 case excel::XlUnderlineStyle::xlUnderlineStyleNone
:
361 nValue
= awt::FontUnderline::NONE
;
363 case excel::XlUnderlineStyle::xlUnderlineStyleSingle
:
364 case excel::XlUnderlineStyle::xlUnderlineStyleSingleAccounting
:
365 nValue
= awt::FontUnderline::SINGLE
;
367 case excel::XlUnderlineStyle::xlUnderlineStyleDouble
:
368 case excel::XlUnderlineStyle::xlUnderlineStyleDoubleAccounting
:
369 nValue
= awt::FontUnderline::DOUBLE
;
372 throw uno::RuntimeException("Unknown value for Underline" );
375 mxFont
->setPropertyValue("CharUnderline", uno::Any(nValue
) );
380 ScVbaFont::getUnderline() throw ( uno::RuntimeException
, std::exception
)
383 if ( GetDataSet()->GetItemState( ATTR_FONT_UNDERLINE
, true, NULL
) == SfxItemState::DONTCARE
)
386 sal_Int32 nValue
= awt::FontUnderline::NONE
;
387 mxFont
->getPropertyValue("CharUnderline") >>= nValue
;
390 case awt::FontUnderline::DOUBLE
:
391 nValue
= excel::XlUnderlineStyle::xlUnderlineStyleDouble
;
393 case awt::FontUnderline::SINGLE
:
394 nValue
= excel::XlUnderlineStyle::xlUnderlineStyleSingle
;
396 case awt::FontUnderline::NONE
:
397 nValue
= excel::XlUnderlineStyle::xlUnderlineStyleNone
;
400 throw uno::RuntimeException("Unknown value retrieved for Underline" );
403 return uno::makeAny( nValue
);
407 ScVbaFont::getStrikethrough() throw ( uno::RuntimeException
, std::exception
)
410 if ( GetDataSet()->GetItemState( ATTR_FONT_CROSSEDOUT
, true, NULL
) == SfxItemState::DONTCARE
)
412 return ScVbaFont_BASE::getStrikethrough();
416 ScVbaFont::getShadow() throw (uno::RuntimeException
, std::exception
)
419 if ( GetDataSet()->GetItemState( ATTR_FONT_SHADOWED
, true, NULL
) == SfxItemState::DONTCARE
)
421 return ScVbaFont_BASE::getShadow();
425 ScVbaFont::getItalic() throw ( uno::RuntimeException
, std::exception
)
428 if ( GetDataSet()->GetItemState( ATTR_FONT_POSTURE
, true, NULL
) == SfxItemState::DONTCARE
)
431 return ScVbaFont_BASE::getItalic();
435 ScVbaFont::getName() throw ( uno::RuntimeException
, std::exception
)
438 if ( GetDataSet()->GetItemState( ATTR_FONT
, true, NULL
) == SfxItemState::DONTCARE
)
440 return ScVbaFont_BASE::getName();
443 ScVbaFont::getColor() throw (uno::RuntimeException
, std::exception
)
445 // #TODO #FIXME - behave like getXXX above ( wrt. GetDataSet )
447 aAny
= OORGBToXLRGB( mxFont
->getPropertyValue("CharColor") );
452 ScVbaFont::setOutlineFont( const uno::Any
& aValue
) throw ( uno::RuntimeException
, std::exception
)
454 mxFont
->setPropertyValue("CharContoured", aValue
);
458 ScVbaFont::getOutlineFont() throw (uno::RuntimeException
, std::exception
)
461 if ( GetDataSet()->GetItemState( ATTR_FONT_CONTOUR
, true, NULL
) == SfxItemState::DONTCARE
)
463 return mxFont
->getPropertyValue("CharContoured");
467 ScVbaFont::getServiceImplName()
469 return OUString("ScVbaFont");
472 uno::Sequence
< OUString
>
473 ScVbaFont::getServiceNames()
475 static uno::Sequence
< OUString
> aServiceNames
;
476 if ( aServiceNames
.getLength() == 0 )
478 aServiceNames
.realloc( 1 );
479 aServiceNames
[ 0 ] = "ooo.vba.excel.Font";
481 return aServiceNames
;
484 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */