fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / vba / vbafont.cxx
blob7d3cde1091886c54f85178dc6f5eeaf241276e0e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
39 ScVbaFont::ScVbaFont(
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 ),
46 mPalette( dPalette ),
47 mpRangeObj( pRangeObj )
51 SfxItemSet*
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 );
66 return xProps;
68 void SAL_CALL
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 );
77 if ( !xCell.is() )
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 );
91 return;
94 xCell.set( xCellRange->getCellByPosition( 0,0 ) );
96 uno::Reference< beans::XPropertySet > xProps = lcl_TextProperties( xCell );
97 bool bValue = false;
98 aValue >>= bValue;
99 sal_Int16 nValue = NORMAL;
100 sal_Int8 nValue2 = NORMALHEIGHT;
102 if( bValue )
104 nValue = SUPERSCRIPT;
105 nValue2 = SUPERSCRIPTHEIGHT;
107 xProps->setPropertyValue("CharEscapement", uno::Any(nValue) );
108 xProps->setPropertyValue("CharEscapementHeight", uno::Any(nValue2) );
111 uno::Any SAL_CALL
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 );
116 if ( !xCell.is() )
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();
121 uno::Any aRes;
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 );
128 if ( !col && !row )
129 aRes = aFont.getSuperscript();
130 else if ( aRes != aFont.getSuperscript() )
131 return aNULL();
134 return aRes;
137 xCell.set( xCellRange->getCellByPosition( 0,0 ) );
138 uno::Reference< beans::XPropertySet > xProps = lcl_TextProperties( xCell );
139 short nValue = 0;
140 xProps->getPropertyValue("CharEscapement") >>= nValue;
141 return uno::makeAny( ( nValue == SUPERSCRIPT ) );
144 void SAL_CALL
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 );
149 if ( !xCell.is() )
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 );
163 return;
166 xCell.set( xCellRange->getCellByPosition( 0,0 ) );
167 uno::Reference< beans::XPropertySet > xProps = lcl_TextProperties( xCell );
169 bool bValue = false;
170 aValue >>= bValue;
171 sal_Int16 nValue = NORMAL;
172 sal_Int8 nValue2 = NORMALHEIGHT;
174 if( bValue )
176 nValue= SUBSCRIPT;
177 nValue2 = SUBSCRIPTHEIGHT;
180 xProps->setPropertyValue("CharEscapementHeight", uno::Any(nValue2) );
181 xProps->setPropertyValue("CharEscapement", uno::Any(nValue) );
185 uno::Any SAL_CALL
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 );
190 if ( !xCell.is() )
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();
195 uno::Any aRes;
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 );
202 if ( !col && !row )
203 aRes = aFont.getSubscript();
204 else if ( aRes != aFont.getSubscript() )
205 return aNULL();
208 return aRes;
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 ) );
219 uno::Any SAL_CALL
220 ScVbaFont::getSize() throw ( uno::RuntimeException, std::exception )
222 if ( GetDataSet() )
223 if ( GetDataSet()->GetItemState( ATTR_FONT_HEIGHT, true, NULL) == SfxItemState::DONTCARE )
224 return aNULL();
225 return ScVbaFont_BASE::getSize();
228 void SAL_CALL
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 ) );
241 else
242 ScVbaFont_BASE::setColorIndex( _colorindex );
245 uno::Any SAL_CALL
246 ScVbaFont::getColorIndex() throw ( uno::RuntimeException, std::exception )
248 if ( GetDataSet() )
249 if ( GetDataSet()->GetItemState( ATTR_FONT_COLOR, true, NULL) == SfxItemState::DONTCARE )
250 return aNULL();
251 return ScVbaFont_BASE::getColorIndex();
254 void SAL_CALL
255 ScVbaFont::setStandardFontSize( const uno::Any& /*aValue*/ ) throw( uno::RuntimeException, std::exception )
257 //XXX #TODO# #FIXME#
258 //mxFont->setPropertyValue("CharSize", ( uno::Any )fValue );
259 throw uno::RuntimeException(
260 "setStandardFontSize not supported" );
263 uno::Any SAL_CALL
264 ScVbaFont::getStandardFontSize() throw ( uno::RuntimeException, std::exception )
266 //XXX #TODO# #FIXME#
267 throw uno::RuntimeException( "getStandardFontSize not supported" );
268 // return uno::Any();
271 void SAL_CALL
272 ScVbaFont::setStandardFont( const uno::Any& /*aValue*/ ) throw( uno::RuntimeException, std::exception )
274 //XXX #TODO# #FIXME#
275 throw uno::RuntimeException("setStandardFont not supported" );
278 uno::Any SAL_CALL
279 ScVbaFont::getStandardFont() throw ( uno::RuntimeException, std::exception )
281 //XXX #TODO# #FIXME#
282 throw uno::RuntimeException("getStandardFont not supported");
283 // return uno::Any();
286 void SAL_CALL
287 ScVbaFont::setFontStyle( const uno::Any& aValue ) throw( uno::RuntimeException, std::exception )
289 bool bBold = false;
290 bool bItalic = false;
292 OUString aStyles;
293 aValue >>= aStyles;
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") )
307 bBold = true;
309 if( (*it).equalsIgnoreAsciiCase("Italic") )
310 bItalic = true;
313 setBold( uno::makeAny( bBold ) );
314 setItalic( uno::makeAny( bItalic ) );
317 uno::Any SAL_CALL
318 ScVbaFont::getFontStyle() throw ( uno::RuntimeException, std::exception )
320 OUStringBuffer aStyles;
321 bool bValue = false;
322 getBold() >>= bValue;
323 if( bValue )
324 aStyles.appendAscii("Bold");
326 getItalic() >>= bValue;
327 if( bValue )
329 if( !aStyles.isEmpty() )
330 aStyles.appendAscii(" ");
331 aStyles.appendAscii("Italic");
333 return uno::makeAny( aStyles.makeStringAndClear() );
336 uno::Any SAL_CALL
337 ScVbaFont::getBold() throw ( uno::RuntimeException, std::exception )
339 if ( GetDataSet() )
340 if ( GetDataSet()->GetItemState( ATTR_FONT_WEIGHT, true, NULL) == SfxItemState::DONTCARE )
341 return aNULL();
342 return ScVbaFont_BASE::getBold();
345 void SAL_CALL
346 ScVbaFont::setUnderline( const uno::Any& aValue ) throw ( uno::RuntimeException, std::exception )
348 // default
349 sal_Int32 nValue = excel::XlUnderlineStyle::xlUnderlineStyleNone;
350 aValue >>= nValue;
351 switch ( nValue )
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;
362 break;
363 case excel::XlUnderlineStyle::xlUnderlineStyleSingle:
364 case excel::XlUnderlineStyle::xlUnderlineStyleSingleAccounting:
365 nValue = awt::FontUnderline::SINGLE;
366 break;
367 case excel::XlUnderlineStyle::xlUnderlineStyleDouble:
368 case excel::XlUnderlineStyle::xlUnderlineStyleDoubleAccounting:
369 nValue = awt::FontUnderline::DOUBLE;
370 break;
371 default:
372 throw uno::RuntimeException("Unknown value for Underline" );
375 mxFont->setPropertyValue("CharUnderline", uno::Any(nValue) );
379 uno::Any SAL_CALL
380 ScVbaFont::getUnderline() throw ( uno::RuntimeException, std::exception )
382 if ( GetDataSet() )
383 if ( GetDataSet()->GetItemState( ATTR_FONT_UNDERLINE, true, NULL) == SfxItemState::DONTCARE )
384 return aNULL();
386 sal_Int32 nValue = awt::FontUnderline::NONE;
387 mxFont->getPropertyValue("CharUnderline") >>= nValue;
388 switch ( nValue )
390 case awt::FontUnderline::DOUBLE:
391 nValue = excel::XlUnderlineStyle::xlUnderlineStyleDouble;
392 break;
393 case awt::FontUnderline::SINGLE:
394 nValue = excel::XlUnderlineStyle::xlUnderlineStyleSingle;
395 break;
396 case awt::FontUnderline::NONE:
397 nValue = excel::XlUnderlineStyle::xlUnderlineStyleNone;
398 break;
399 default:
400 throw uno::RuntimeException("Unknown value retrieved for Underline" );
403 return uno::makeAny( nValue );
406 uno::Any SAL_CALL
407 ScVbaFont::getStrikethrough() throw ( uno::RuntimeException, std::exception )
409 if ( GetDataSet() )
410 if ( GetDataSet()->GetItemState( ATTR_FONT_CROSSEDOUT, true, NULL) == SfxItemState::DONTCARE )
411 return aNULL();
412 return ScVbaFont_BASE::getStrikethrough();
415 uno::Any SAL_CALL
416 ScVbaFont::getShadow() throw (uno::RuntimeException, std::exception)
418 if ( GetDataSet() )
419 if ( GetDataSet()->GetItemState( ATTR_FONT_SHADOWED, true, NULL) == SfxItemState::DONTCARE )
420 return aNULL();
421 return ScVbaFont_BASE::getShadow();
424 uno::Any SAL_CALL
425 ScVbaFont::getItalic() throw ( uno::RuntimeException, std::exception )
427 if ( GetDataSet() )
428 if ( GetDataSet()->GetItemState( ATTR_FONT_POSTURE, true, NULL) == SfxItemState::DONTCARE )
429 return aNULL();
431 return ScVbaFont_BASE::getItalic();
434 uno::Any SAL_CALL
435 ScVbaFont::getName() throw ( uno::RuntimeException, std::exception )
437 if ( GetDataSet() )
438 if ( GetDataSet()->GetItemState( ATTR_FONT, true, NULL) == SfxItemState::DONTCARE )
439 return aNULL();
440 return ScVbaFont_BASE::getName();
442 uno::Any
443 ScVbaFont::getColor() throw (uno::RuntimeException, std::exception)
445 // #TODO #FIXME - behave like getXXX above ( wrt. GetDataSet )
446 uno::Any aAny;
447 aAny = OORGBToXLRGB( mxFont->getPropertyValue("CharColor") );
448 return aAny;
451 void SAL_CALL
452 ScVbaFont::setOutlineFont( const uno::Any& aValue ) throw ( uno::RuntimeException, std::exception )
454 mxFont->setPropertyValue("CharContoured", aValue );
457 uno::Any SAL_CALL
458 ScVbaFont::getOutlineFont() throw (uno::RuntimeException, std::exception)
460 if ( GetDataSet() )
461 if ( GetDataSet()->GetItemState( ATTR_FONT_CONTOUR, true, NULL) == SfxItemState::DONTCARE )
462 return aNULL();
463 return mxFont->getPropertyValue("CharContoured");
466 OUString
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: */