GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / sc / source / ui / vba / vbafont.cxx
blob1f8ddc8e86a1f4045363c3b4ade3237e2cac216c
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()
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 );
67 return xProps;
69 void SAL_CALL
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 );
78 if ( !xCell.is() )
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 );
92 return;
95 xCell.set( xCellRange->getCellByPosition( 0,0 ) );
97 uno::Reference< beans::XPropertySet > xProps = lcl_TextProperties( xCell );
98 sal_Bool bValue = false;
99 aValue >>= bValue;
100 sal_Int16 nValue = NORMAL;
101 sal_Int8 nValue2 = NORMALHEIGHT;
103 if( bValue )
105 nValue = SUPERSCRIPT;
106 nValue2 = SUPERSCRIPTHEIGHT;
108 xProps->setPropertyValue("CharEscapement", ( uno::Any )nValue );
109 xProps->setPropertyValue("CharEscapementHeight", ( uno::Any )nValue2 );
112 uno::Any SAL_CALL
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 );
117 if ( !xCell.is() )
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();
122 uno::Any aRes;
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 );
129 if ( !col && !row )
130 aRes = aFont.getSuperscript();
131 else if ( aRes != aFont.getSuperscript() )
132 return aNULL();
135 return aRes;
138 xCell.set( xCellRange->getCellByPosition( 0,0 ) );
139 uno::Reference< beans::XPropertySet > xProps = lcl_TextProperties( xCell );
140 short nValue = 0;
141 xProps->getPropertyValue("CharEscapement") >>= nValue;
142 return uno::makeAny( ( nValue == SUPERSCRIPT ) );
145 void SAL_CALL
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 );
150 if ( !xCell.is() )
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 );
164 return;
167 xCell.set( xCellRange->getCellByPosition( 0,0 ) );
168 uno::Reference< beans::XPropertySet > xProps = lcl_TextProperties( xCell );
170 sal_Bool bValue = false;
171 aValue >>= bValue;
172 sal_Int16 nValue = NORMAL;
173 sal_Int8 nValue2 = NORMALHEIGHT;
175 if( bValue )
177 nValue= SUBSCRIPT;
178 nValue2 = SUBSCRIPTHEIGHT;
181 xProps->setPropertyValue("CharEscapementHeight", ( uno::Any )nValue2 );
182 xProps->setPropertyValue("CharEscapement", ( uno::Any )nValue );
186 uno::Any SAL_CALL
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 );
191 if ( !xCell.is() )
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();
196 uno::Any aRes;
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 );
203 if ( !col && !row )
204 aRes = aFont.getSubscript();
205 else if ( aRes != aFont.getSubscript() )
206 return aNULL();
209 return aRes;
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 ) );
220 uno::Any SAL_CALL
221 ScVbaFont::getSize() throw ( uno::RuntimeException )
223 if ( GetDataSet() )
224 if ( GetDataSet()->GetItemState( ATTR_FONT_HEIGHT, sal_True, NULL) == SFX_ITEM_DONTCARE )
225 return aNULL();
226 return ScVbaFont_BASE::getSize();
229 void SAL_CALL
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 ) );
242 else
243 ScVbaFont_BASE::setColorIndex( _colorindex );
247 uno::Any SAL_CALL
248 ScVbaFont::getColorIndex() throw ( uno::RuntimeException )
250 if ( GetDataSet() )
251 if ( GetDataSet()->GetItemState( ATTR_FONT_COLOR, sal_True, NULL) == SFX_ITEM_DONTCARE )
252 return aNULL();
253 return ScVbaFont_BASE::getColorIndex();
256 //////////////////////////////////////////////////////////////////////////////////////////
257 void SAL_CALL
258 ScVbaFont::setStandardFontSize( const uno::Any& /*aValue*/ ) throw( uno::RuntimeException )
260 //XXX #TODO# #FIXME#
261 //mxFont->setPropertyValue("CharSize", ( uno::Any )fValue );
262 throw uno::RuntimeException(
263 OUString("setStandardFontSize not supported"), uno::Reference< uno::XInterface >() );
267 uno::Any SAL_CALL
268 ScVbaFont::getStandardFontSize() throw ( uno::RuntimeException )
270 //XXX #TODO# #FIXME#
271 throw uno::RuntimeException(
272 OUString("getStandardFontSize not supported"), uno::Reference< uno::XInterface >() );
273 // return uno::Any();
277 void SAL_CALL
278 ScVbaFont::setStandardFont( const uno::Any& /*aValue*/ ) throw( uno::RuntimeException )
280 //XXX #TODO# #FIXME#
281 throw uno::RuntimeException(
282 OUString("setStandardFont not supported"), uno::Reference< uno::XInterface >() );
286 uno::Any SAL_CALL
287 ScVbaFont::getStandardFont() throw ( uno::RuntimeException )
289 //XXX #TODO# #FIXME#
290 throw uno::RuntimeException(
291 OUString("getStandardFont not supported"), uno::Reference< uno::XInterface >() );
292 // return uno::Any();
295 void SAL_CALL
296 ScVbaFont::setFontStyle( const uno::Any& aValue ) throw( uno::RuntimeException )
298 sal_Bool bBold = false;
299 sal_Bool bItalic = false;
301 OUString aStyles;
302 aValue >>= aStyles;
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") )
316 bBold = sal_True;
318 if( (*it).equalsIgnoreAsciiCase("Italic") )
319 bItalic = sal_True;
322 setBold( uno::makeAny( bBold ) );
323 setItalic( uno::makeAny( bItalic ) );
327 uno::Any SAL_CALL
328 ScVbaFont::getFontStyle() throw ( uno::RuntimeException )
330 OUStringBuffer aStyles;
331 sal_Bool bValue = false;
332 getBold() >>= bValue;
333 if( bValue )
334 aStyles.appendAscii("Bold");
336 getItalic() >>= bValue;
337 if( bValue )
339 if( !aStyles.isEmpty() )
340 aStyles.appendAscii(" ");
341 aStyles.appendAscii("Italic");
343 return uno::makeAny( aStyles.makeStringAndClear() );
346 uno::Any SAL_CALL
347 ScVbaFont::getBold() throw ( uno::RuntimeException )
349 if ( GetDataSet() )
350 if ( GetDataSet()->GetItemState( ATTR_FONT_WEIGHT, sal_True, NULL) == SFX_ITEM_DONTCARE )
351 return aNULL();
352 return ScVbaFont_BASE::getBold();
355 void SAL_CALL
356 ScVbaFont::setUnderline( const uno::Any& aValue ) throw ( uno::RuntimeException )
358 // default
359 sal_Int32 nValue = excel::XlUnderlineStyle::xlUnderlineStyleNone;
360 aValue >>= nValue;
361 switch ( nValue )
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;
372 break;
373 case excel::XlUnderlineStyle::xlUnderlineStyleSingle:
374 case excel::XlUnderlineStyle::xlUnderlineStyleSingleAccounting:
375 nValue = awt::FontUnderline::SINGLE;
376 break;
377 case excel::XlUnderlineStyle::xlUnderlineStyleDouble:
378 case excel::XlUnderlineStyle::xlUnderlineStyleDoubleAccounting:
379 nValue = awt::FontUnderline::DOUBLE;
380 break;
381 default:
382 throw uno::RuntimeException("Unknown value for Underline", uno::Reference< uno::XInterface >() );
385 mxFont->setPropertyValue("CharUnderline", ( uno::Any )nValue );
389 uno::Any SAL_CALL
390 ScVbaFont::getUnderline() throw ( uno::RuntimeException )
392 if ( GetDataSet() )
393 if ( GetDataSet()->GetItemState( ATTR_FONT_UNDERLINE, sal_True, NULL) == SFX_ITEM_DONTCARE )
394 return aNULL();
396 sal_Int32 nValue = awt::FontUnderline::NONE;
397 mxFont->getPropertyValue("CharUnderline") >>= nValue;
398 switch ( nValue )
400 case awt::FontUnderline::DOUBLE:
401 nValue = excel::XlUnderlineStyle::xlUnderlineStyleDouble;
402 break;
403 case awt::FontUnderline::SINGLE:
404 nValue = excel::XlUnderlineStyle::xlUnderlineStyleSingle;
405 break;
406 case awt::FontUnderline::NONE:
407 nValue = excel::XlUnderlineStyle::xlUnderlineStyleNone;
408 break;
409 default:
410 throw uno::RuntimeException("Unknown value retrieved for Underline", uno::Reference< uno::XInterface >() );
413 return uno::makeAny( nValue );
416 uno::Any SAL_CALL
417 ScVbaFont::getStrikethrough() throw ( uno::RuntimeException )
419 if ( GetDataSet() )
420 if ( GetDataSet()->GetItemState( ATTR_FONT_CROSSEDOUT, sal_True, NULL) == SFX_ITEM_DONTCARE )
421 return aNULL();
422 return ScVbaFont_BASE::getStrikethrough();
425 uno::Any SAL_CALL
426 ScVbaFont::getShadow() throw (uno::RuntimeException)
428 if ( GetDataSet() )
429 if ( GetDataSet()->GetItemState( ATTR_FONT_SHADOWED, sal_True, NULL) == SFX_ITEM_DONTCARE )
430 return aNULL();
431 return ScVbaFont_BASE::getShadow();
434 uno::Any SAL_CALL
435 ScVbaFont::getItalic() throw ( uno::RuntimeException )
437 if ( GetDataSet() )
438 if ( GetDataSet()->GetItemState( ATTR_FONT_POSTURE, sal_True, NULL) == SFX_ITEM_DONTCARE )
439 return aNULL();
441 return ScVbaFont_BASE::getItalic();
444 uno::Any SAL_CALL
445 ScVbaFont::getName() throw ( uno::RuntimeException )
447 if ( GetDataSet() )
448 if ( GetDataSet()->GetItemState( ATTR_FONT, sal_True, NULL) == SFX_ITEM_DONTCARE )
449 return aNULL();
450 return ScVbaFont_BASE::getName();
452 uno::Any
453 ScVbaFont::getColor() throw (uno::RuntimeException)
455 // #TODO #FIXME - behave like getXXX above ( wrt. GetDataSet )
456 uno::Any aAny;
457 aAny = OORGBToXLRGB( mxFont->getPropertyValue("CharColor") );
458 return aAny;
461 void SAL_CALL
462 ScVbaFont::setOutlineFont( const uno::Any& aValue ) throw ( uno::RuntimeException )
464 mxFont->setPropertyValue("CharContoured", aValue );
467 uno::Any SAL_CALL
468 ScVbaFont::getOutlineFont() throw (uno::RuntimeException)
470 if ( GetDataSet() )
471 if ( GetDataSet()->GetItemState( ATTR_FONT_CONTOUR, sal_True, NULL) == SFX_ITEM_DONTCARE )
472 return aNULL();
473 return mxFont->getPropertyValue("CharContoured");
476 OUString
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: */