merge the formfield patch from ooo-build
[ooovba.git] / sc / source / ui / vba / vbafont.cxx
blob8a1821e8dee763f27ef4b08719157cfce9e2448b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vbafont.cxx,v $
10 * $Revision: 1.7 $
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 )
53 SfxItemSet*
54 ScVbaFont::GetDataSet()
56 SfxItemSet* pDataSet = excel::ScVbaCellRangeAccess::GetDataSet( mpRangeObj );
57 return pDataSet;
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 );
70 return xProps;
72 void SAL_CALL
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 );
81 if ( !xCell.is() )
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 );
95 return;
98 xCell.set( xCellRange->getCellByPosition( 0,0 ) );
100 uno::Reference< beans::XPropertySet > xProps = lcl_TextProperties( xCell );
101 sal_Bool bValue = sal_False;
102 aValue >>= bValue;
103 sal_Int16 nValue = NORMAL;
104 sal_Int8 nValue2 = NORMALHEIGHT;
106 if( bValue )
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 );
115 uno::Any SAL_CALL
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 );
120 if ( !xCell.is() )
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();
125 uno::Any aRes;
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 );
132 if ( !col && !row )
133 aRes = aFont.getSuperscript();
134 else if ( aRes != aFont.getSuperscript() )
135 return aNULL();
138 return aRes;
141 xCell.set( xCellRange->getCellByPosition( 0,0 ) );
142 uno::Reference< beans::XPropertySet > xProps = lcl_TextProperties( xCell );
143 short nValue = 0;
144 xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharEscapement" ) ) ) >>= nValue;
145 return uno::makeAny( ( nValue == SUPERSCRIPT ) );
148 void SAL_CALL
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 );
153 if ( !xCell.is() )
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 );
167 return;
170 xCell.set( xCellRange->getCellByPosition( 0,0 ) );
171 uno::Reference< beans::XPropertySet > xProps = lcl_TextProperties( xCell );
173 sal_Bool bValue = sal_False;
174 aValue >>= bValue;
175 sal_Int16 nValue = NORMAL;
176 sal_Int8 nValue2 = NORMALHEIGHT;
178 if( bValue )
180 nValue= SUBSCRIPT;
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 );
189 uno::Any SAL_CALL
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 );
194 if ( !xCell.is() )
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();
199 uno::Any aRes;
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 );
206 if ( !col && !row )
207 aRes = aFont.getSubscript();
208 else if ( aRes != aFont.getSubscript() )
209 return aNULL();
212 return aRes;
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 ) );
223 uno::Any SAL_CALL
224 ScVbaFont::getSize() throw ( uno::RuntimeException )
226 if ( GetDataSet() )
227 if ( GetDataSet()->GetItemState( ATTR_FONT_HEIGHT, TRUE, NULL) == SFX_ITEM_DONTCARE )
228 return aNULL();
229 return ScVbaFont_BASE::getSize();
232 void SAL_CALL
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 ) );
245 else
246 ScVbaFont_BASE::setColorIndex( _colorindex );
250 uno::Any SAL_CALL
251 ScVbaFont::getColorIndex() throw ( uno::RuntimeException )
253 if ( GetDataSet() )
254 if ( GetDataSet()->GetItemState( ATTR_FONT_COLOR, TRUE, NULL) == SFX_ITEM_DONTCARE )
255 return aNULL();
256 return ScVbaFont_BASE::getColorIndex();
259 //////////////////////////////////////////////////////////////////////////////////////////
260 void SAL_CALL
261 ScVbaFont::setStandardFontSize( const uno::Any& /*aValue*/ ) throw( uno::RuntimeException )
263 //XXX #TODO# #FIXME#
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 >() );
270 uno::Any SAL_CALL
271 ScVbaFont::getStandardFontSize() throw ( uno::RuntimeException )
273 //XXX #TODO# #FIXME#
274 throw uno::RuntimeException(
275 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("getStandardFontSize not supported") ), uno::Reference< uno::XInterface >() );
276 // return uno::Any();
280 void SAL_CALL
281 ScVbaFont::setStandardFont( const uno::Any& /*aValue*/ ) throw( uno::RuntimeException )
283 //XXX #TODO# #FIXME#
284 throw uno::RuntimeException(
285 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("setStandardFont not supported") ), uno::Reference< uno::XInterface >() );
289 uno::Any SAL_CALL
290 ScVbaFont::getStandardFont() throw ( uno::RuntimeException )
292 //XXX #TODO# #FIXME#
293 throw uno::RuntimeException(
294 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("getStandardFont not supported") ), uno::Reference< uno::XInterface >() );
295 // return uno::Any();
298 void SAL_CALL
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;
305 aValue >>= 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" ) )
319 bBold = sal_True;
321 if( (*it).equalsIgnoreAsciiCaseAscii( "Italic" ) )
322 bItalic = sal_True;
325 setBold( uno::makeAny( bBold ) );
326 setItalic( uno::makeAny( bItalic ) );
330 uno::Any SAL_CALL
331 ScVbaFont::getFontStyle() throw ( uno::RuntimeException )
333 rtl::OUStringBuffer aStyles;
334 sal_Bool bValue = sal_False;
335 getBold() >>= bValue;
336 if( bValue )
337 aStyles.appendAscii("Bold");
339 getItalic() >>= bValue;
340 if( bValue )
342 if( aStyles.getLength() )
343 aStyles.appendAscii(" ");
344 aStyles.appendAscii("Italic");
346 return uno::makeAny( aStyles.makeStringAndClear() );
349 uno::Any SAL_CALL
350 ScVbaFont::getBold() throw ( uno::RuntimeException )
352 if ( GetDataSet() )
353 if ( GetDataSet()->GetItemState( ATTR_FONT_WEIGHT, TRUE, NULL) == SFX_ITEM_DONTCARE )
354 return aNULL();
355 return ScVbaFont_BASE::getBold();
358 void SAL_CALL
359 ScVbaFont::setUnderline( const uno::Any& aValue ) throw ( uno::RuntimeException )
361 // default
362 sal_Int32 nValue = excel::XlUnderlineStyle::xlUnderlineStyleNone;
363 aValue >>= nValue;
364 switch ( nValue )
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;
375 break;
376 case excel::XlUnderlineStyle::xlUnderlineStyleSingle:
377 case excel::XlUnderlineStyle::xlUnderlineStyleSingleAccounting:
378 nValue = awt::FontUnderline::SINGLE;
379 break;
380 case excel::XlUnderlineStyle::xlUnderlineStyleDouble:
381 case excel::XlUnderlineStyle::xlUnderlineStyleDoubleAccounting:
382 nValue = awt::FontUnderline::DOUBLE;
383 break;
384 default:
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 );
392 uno::Any SAL_CALL
393 ScVbaFont::getUnderline() throw ( uno::RuntimeException )
395 if ( GetDataSet() )
396 if ( GetDataSet()->GetItemState( ATTR_FONT_UNDERLINE, TRUE, NULL) == SFX_ITEM_DONTCARE )
397 return aNULL();
399 sal_Int32 nValue = awt::FontUnderline::NONE;
400 mxFont->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharUnderline" ) ) ) >>= nValue;
401 switch ( nValue )
403 case awt::FontUnderline::DOUBLE:
404 nValue = excel::XlUnderlineStyle::xlUnderlineStyleDouble;
405 break;
406 case awt::FontUnderline::SINGLE:
407 nValue = excel::XlUnderlineStyle::xlUnderlineStyleSingle;
408 break;
409 case awt::FontUnderline::NONE:
410 nValue = excel::XlUnderlineStyle::xlUnderlineStyleNone;
411 break;
412 default:
413 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Unknown value retrieved for Underline") ), uno::Reference< uno::XInterface >() );
416 return uno::makeAny( nValue );
419 uno::Any SAL_CALL
420 ScVbaFont::getStrikethrough() throw ( uno::RuntimeException )
422 if ( GetDataSet() )
423 if ( GetDataSet()->GetItemState( ATTR_FONT_CROSSEDOUT, TRUE, NULL) == SFX_ITEM_DONTCARE )
424 return aNULL();
425 return ScVbaFont_BASE::getStrikethrough();
428 uno::Any SAL_CALL
429 ScVbaFont::getShadow() throw (uno::RuntimeException)
431 if ( GetDataSet() )
432 if ( GetDataSet()->GetItemState( ATTR_FONT_SHADOWED, TRUE, NULL) == SFX_ITEM_DONTCARE )
433 return aNULL();
434 return ScVbaFont_BASE::getShadow();
437 uno::Any SAL_CALL
438 ScVbaFont::getItalic() throw ( uno::RuntimeException )
440 if ( GetDataSet() )
441 if ( GetDataSet()->GetItemState( ATTR_FONT_POSTURE, TRUE, NULL) == SFX_ITEM_DONTCARE )
442 return aNULL();
444 return ScVbaFont_BASE::getItalic();
447 uno::Any SAL_CALL
448 ScVbaFont::getName() throw ( uno::RuntimeException )
450 if ( GetDataSet() )
451 if ( GetDataSet()->GetItemState( ATTR_FONT, TRUE, NULL) == SFX_ITEM_DONTCARE )
452 return aNULL();
453 return ScVbaFont_BASE::getName();
455 uno::Any
456 ScVbaFont::getColor() throw (uno::RuntimeException)
458 // #TODO #FIXME - behave like getXXX above ( wrt. GetDataSet )
459 uno::Any aAny;
460 aAny = OORGBToXLRGB( mxFont->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharColor" ) ) ) );
461 return aAny;
464 void SAL_CALL
465 ScVbaFont::setOutlineFont( const uno::Any& aValue ) throw ( uno::RuntimeException )
467 mxFont->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharContoured" ) ), aValue );
470 uno::Any SAL_CALL
471 ScVbaFont::getOutlineFont() throw (uno::RuntimeException)
473 if ( GetDataSet() )
474 if ( GetDataSet()->GetItemState( ATTR_FONT_CONTOUR, TRUE, NULL) == SFX_ITEM_DONTCARE )
475 return aNULL();
476 return mxFont->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharContoured" ) ) );
479 rtl::OUString&
480 ScVbaFont::getServiceImplName()
482 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaFont") );
483 return sImplName;
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;