Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
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"
35 :
36 : using namespace ::ooo::vba;
37 : using namespace ::com::sun::star;
38 :
39 10 : 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 10 : mpRangeObj( pRangeObj )
48 : {
49 10 : }
50 :
51 : SfxItemSet*
52 47 : ScVbaFont::GetDataSet()
53 : {
54 47 : return mpRangeObj ? excel::ScVbaCellRangeAccess::GetDataSet( mpRangeObj ) : 0;
55 : }
56 :
57 20 : ScVbaFont::~ScVbaFont()
58 : {
59 20 : }
60 :
61 0 : static uno::Reference< beans::XPropertySet > lcl_TextProperties( uno::Reference< table::XCell >& xIf ) throw ( uno::RuntimeException )
62 : {
63 0 : uno::Reference< text::XTextRange > xTxtRange( xIf, uno::UNO_QUERY_THROW );
64 0 : uno::Reference< text::XSimpleText > xTxt( xTxtRange->getText(), uno::UNO_QUERY_THROW ) ;
65 0 : uno::Reference< beans::XPropertySet > xProps( xTxt->createTextCursor(), uno::UNO_QUERY_THROW );
66 0 : return xProps;
67 : }
68 : void SAL_CALL
69 0 : ScVbaFont::setSuperscript( const uno::Any& aValue ) throw ( uno::RuntimeException, std::exception )
70 : {
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 0 : uno::Reference< table::XCell> xCell( mxFont, uno::UNO_QUERY );
76 0 : uno::Reference< table::XCellRange > xCellRange( mxFont, uno::UNO_QUERY );
77 0 : if ( !xCell.is() )
78 : {
79 0 : uno::Reference< table::XColumnRowRange > xColumnRowRange(xCellRange, uno::UNO_QUERY_THROW );
80 0 : sal_Int32 nCols = xColumnRowRange->getColumns()->getCount();
81 0 : sal_Int32 nRows = xColumnRowRange->getRows()->getCount();
82 0 : for ( sal_Int32 col = 0; col < nCols; ++col )
83 : {
84 0 : for ( sal_Int32 row = 0; row < nRows; ++row )
85 : {
86 0 : uno::Reference< beans::XPropertySet > xProps( xCellRange->getCellByPosition( col, row ) , uno::UNO_QUERY_THROW );
87 0 : ScVbaFont aFont( getParent(), mxContext, mPalette, xProps );
88 0 : aFont.setSuperscript( aValue );
89 0 : }
90 : }
91 0 : return;
92 :
93 : }
94 0 : xCell.set( xCellRange->getCellByPosition( 0,0 ) );
95 :
96 0 : uno::Reference< beans::XPropertySet > xProps = lcl_TextProperties( xCell );
97 0 : bool bValue = false;
98 0 : aValue >>= bValue;
99 0 : sal_Int16 nValue = NORMAL;
100 0 : sal_Int8 nValue2 = NORMALHEIGHT;
101 :
102 0 : if( bValue )
103 : {
104 0 : nValue = SUPERSCRIPT;
105 0 : nValue2 = SUPERSCRIPTHEIGHT;
106 : }
107 0 : xProps->setPropertyValue("CharEscapement", uno::Any(nValue) );
108 0 : xProps->setPropertyValue("CharEscapementHeight", uno::Any(nValue2) );
109 : }
110 :
111 : uno::Any SAL_CALL
112 0 : ScVbaFont::getSuperscript() throw ( uno::RuntimeException, std::exception )
113 : {
114 0 : uno::Reference< table::XCell> xCell( mxFont, uno::UNO_QUERY );
115 0 : uno::Reference< table::XCellRange > xCellRange( mxFont, uno::UNO_QUERY );
116 0 : if ( !xCell.is() )
117 : {
118 0 : uno::Reference< table::XColumnRowRange > xColumnRowRange(xCellRange, uno::UNO_QUERY_THROW );
119 0 : sal_Int32 nCols = xColumnRowRange->getColumns()->getCount();
120 0 : sal_Int32 nRows = xColumnRowRange->getRows()->getCount();
121 0 : uno::Any aRes;
122 0 : for ( sal_Int32 col = 0; col < nCols; ++col )
123 : {
124 0 : for ( sal_Int32 row = 0; row < nRows; ++row )
125 : {
126 0 : uno::Reference< beans::XPropertySet > xProps( xCellRange->getCellByPosition( col, row ), uno::UNO_QUERY_THROW );
127 0 : ScVbaFont aFont( getParent(), mxContext, mPalette, xProps );
128 0 : if ( !col && !row )
129 0 : aRes = aFont.getSuperscript();
130 0 : else if ( aRes != aFont.getSuperscript() )
131 0 : return aNULL();
132 0 : }
133 : }
134 0 : return aRes;
135 :
136 : }
137 0 : xCell.set( xCellRange->getCellByPosition( 0,0 ) );
138 0 : uno::Reference< beans::XPropertySet > xProps = lcl_TextProperties( xCell );
139 0 : short nValue = 0;
140 0 : xProps->getPropertyValue("CharEscapement") >>= nValue;
141 0 : return uno::makeAny( ( nValue == SUPERSCRIPT ) );
142 : }
143 :
144 : void SAL_CALL
145 0 : ScVbaFont::setSubscript( const uno::Any& aValue ) throw ( uno::RuntimeException, std::exception )
146 : {
147 0 : uno::Reference< table::XCell> xCell( mxFont, uno::UNO_QUERY );
148 0 : uno::Reference< table::XCellRange > xCellRange( mxFont, uno::UNO_QUERY );
149 0 : if ( !xCell.is() )
150 : {
151 0 : uno::Reference< table::XColumnRowRange > xColumnRowRange(xCellRange, uno::UNO_QUERY_THROW );
152 0 : sal_Int32 nCols = xColumnRowRange->getColumns()->getCount();
153 0 : sal_Int32 nRows = xColumnRowRange->getRows()->getCount();
154 0 : for ( sal_Int32 col = 0; col < nCols; ++col )
155 : {
156 0 : for ( sal_Int32 row = 0; row < nRows; ++row )
157 : {
158 0 : uno::Reference< beans::XPropertySet > xProps( xCellRange->getCellByPosition( col, row ) , uno::UNO_QUERY_THROW );
159 0 : ScVbaFont aFont( getParent(), mxContext, mPalette, xProps );
160 0 : aFont.setSubscript( aValue );
161 0 : }
162 : }
163 0 : return;
164 :
165 : }
166 0 : xCell.set( xCellRange->getCellByPosition( 0,0 ) );
167 0 : uno::Reference< beans::XPropertySet > xProps = lcl_TextProperties( xCell );
168 :
169 0 : bool bValue = false;
170 0 : aValue >>= bValue;
171 0 : sal_Int16 nValue = NORMAL;
172 0 : sal_Int8 nValue2 = NORMALHEIGHT;
173 :
174 0 : if( bValue )
175 : {
176 0 : nValue= SUBSCRIPT;
177 0 : nValue2 = SUBSCRIPTHEIGHT;
178 : }
179 :
180 0 : xProps->setPropertyValue("CharEscapementHeight", uno::Any(nValue2) );
181 0 : xProps->setPropertyValue("CharEscapement", uno::Any(nValue) );
182 :
183 : }
184 :
185 : uno::Any SAL_CALL
186 0 : ScVbaFont::getSubscript() throw ( uno::RuntimeException, std::exception )
187 : {
188 0 : uno::Reference< table::XCell> xCell( mxFont, uno::UNO_QUERY );
189 0 : uno::Reference< table::XCellRange > xCellRange( mxFont, uno::UNO_QUERY );
190 0 : if ( !xCell.is() )
191 : {
192 0 : uno::Reference< table::XColumnRowRange > xColumnRowRange(xCellRange, uno::UNO_QUERY_THROW );
193 0 : sal_Int32 nCols = xColumnRowRange->getColumns()->getCount();
194 0 : sal_Int32 nRows = xColumnRowRange->getRows()->getCount();
195 0 : uno::Any aRes;
196 0 : for ( sal_Int32 col = 0; col < nCols; ++col )
197 : {
198 0 : for ( sal_Int32 row = 0; row < nRows; ++row )
199 : {
200 0 : uno::Reference< beans::XPropertySet > xProps( xCellRange->getCellByPosition( col, row ), uno::UNO_QUERY_THROW );
201 0 : ScVbaFont aFont( getParent(), mxContext, mPalette, xProps );
202 0 : if ( !col && !row )
203 0 : aRes = aFont.getSubscript();
204 0 : else if ( aRes != aFont.getSubscript() )
205 0 : return aNULL();
206 0 : }
207 : }
208 0 : return aRes;
209 :
210 : }
211 0 : xCell.set( xCellRange->getCellByPosition( 0,0 ) );
212 0 : uno::Reference< beans::XPropertySet > xProps = lcl_TextProperties( xCell );
213 :
214 0 : short nValue = NORMAL;
215 0 : xProps->getPropertyValue("CharEscapement") >>= nValue;
216 0 : return uno::makeAny( ( nValue == SUBSCRIPT ) );
217 : }
218 :
219 : uno::Any SAL_CALL
220 2 : ScVbaFont::getSize() throw ( uno::RuntimeException, std::exception )
221 : {
222 2 : if ( GetDataSet() )
223 2 : if ( GetDataSet()->GetItemState( ATTR_FONT_HEIGHT, true, NULL) == SfxItemState::DONTCARE )
224 0 : return aNULL();
225 2 : return ScVbaFont_BASE::getSize();
226 : }
227 :
228 : void SAL_CALL
229 1 : ScVbaFont::setColorIndex( const uno::Any& _colorindex ) throw( uno::RuntimeException, std::exception )
230 : {
231 1 : sal_Int32 nIndex = 0;
232 1 : _colorindex >>= nIndex;
233 : // #FIXME xlColorIndexAutomatic & xlColorIndexNone are not really
234 : // handled properly here
235 :
236 1 : if ( !nIndex || ( nIndex == excel::XlColorIndex::xlColorIndexAutomatic ) )
237 : {
238 0 : nIndex = 1; // check defualt ( assume black )
239 0 : ScVbaFont_BASE::setColorIndex( uno::makeAny( nIndex ) );
240 : }
241 : else
242 1 : ScVbaFont_BASE::setColorIndex( _colorindex );
243 1 : }
244 :
245 : uno::Any SAL_CALL
246 2 : ScVbaFont::getColorIndex() throw ( uno::RuntimeException, std::exception )
247 : {
248 2 : if ( GetDataSet() )
249 2 : if ( GetDataSet()->GetItemState( ATTR_FONT_COLOR, true, NULL) == SfxItemState::DONTCARE )
250 0 : return aNULL();
251 2 : return ScVbaFont_BASE::getColorIndex();
252 : }
253 :
254 : void SAL_CALL
255 0 : ScVbaFont::setStandardFontSize( const uno::Any& /*aValue*/ ) throw( uno::RuntimeException, std::exception )
256 : {
257 : //XXX #TODO# #FIXME#
258 : //mxFont->setPropertyValue("CharSize", ( uno::Any )fValue );
259 : throw uno::RuntimeException(
260 0 : "setStandardFontSize not supported" );
261 : }
262 :
263 : uno::Any SAL_CALL
264 0 : ScVbaFont::getStandardFontSize() throw ( uno::RuntimeException, std::exception )
265 : {
266 : //XXX #TODO# #FIXME#
267 0 : throw uno::RuntimeException( "getStandardFontSize not supported" );
268 : // return uno::Any();
269 : }
270 :
271 : void SAL_CALL
272 0 : ScVbaFont::setStandardFont( const uno::Any& /*aValue*/ ) throw( uno::RuntimeException, std::exception )
273 : {
274 : //XXX #TODO# #FIXME#
275 0 : throw uno::RuntimeException("setStandardFont not supported" );
276 : }
277 :
278 : uno::Any SAL_CALL
279 0 : ScVbaFont::getStandardFont() throw ( uno::RuntimeException, std::exception )
280 : {
281 : //XXX #TODO# #FIXME#
282 0 : throw uno::RuntimeException("getStandardFont not supported");
283 : // return uno::Any();
284 : }
285 :
286 : void SAL_CALL
287 0 : ScVbaFont::setFontStyle( const uno::Any& aValue ) throw( uno::RuntimeException, std::exception )
288 : {
289 0 : bool bBold = false;
290 0 : bool bItalic = false;
291 :
292 0 : OUString aStyles;
293 0 : aValue >>= aStyles;
294 :
295 0 : std::vector< OUString > aTokens;
296 0 : sal_Int32 nIndex = 0;
297 0 : do
298 : {
299 0 : OUString aToken = aStyles.getToken( 0, ' ', nIndex );
300 0 : aTokens.push_back( aToken );
301 0 : }while( nIndex >= 0 );
302 :
303 0 : std::vector< OUString >::iterator it;
304 0 : for( it = aTokens.begin(); it != aTokens.end(); ++it )
305 : {
306 0 : if( (*it).equalsIgnoreAsciiCase("Bold") )
307 0 : bBold = true;
308 :
309 0 : if( (*it).equalsIgnoreAsciiCase("Italic") )
310 0 : bItalic = true;
311 : }
312 :
313 0 : setBold( uno::makeAny( bBold ) );
314 0 : setItalic( uno::makeAny( bItalic ) );
315 0 : }
316 :
317 : uno::Any SAL_CALL
318 1 : ScVbaFont::getFontStyle() throw ( uno::RuntimeException, std::exception )
319 : {
320 1 : OUStringBuffer aStyles;
321 1 : bool bValue = false;
322 1 : getBold() >>= bValue;
323 1 : if( bValue )
324 1 : aStyles.appendAscii("Bold");
325 :
326 1 : getItalic() >>= bValue;
327 1 : if( bValue )
328 : {
329 1 : if( !aStyles.isEmpty() )
330 1 : aStyles.appendAscii(" ");
331 1 : aStyles.appendAscii("Italic");
332 : }
333 1 : return uno::makeAny( aStyles.makeStringAndClear() );
334 : }
335 :
336 : uno::Any SAL_CALL
337 10 : ScVbaFont::getBold() throw ( uno::RuntimeException, std::exception )
338 : {
339 10 : if ( GetDataSet() )
340 3 : if ( GetDataSet()->GetItemState( ATTR_FONT_WEIGHT, true, NULL) == SfxItemState::DONTCARE )
341 0 : return aNULL();
342 10 : return ScVbaFont_BASE::getBold();
343 : }
344 :
345 : void SAL_CALL
346 1 : ScVbaFont::setUnderline( const uno::Any& aValue ) throw ( uno::RuntimeException, std::exception )
347 : {
348 : // default
349 1 : sal_Int32 nValue = excel::XlUnderlineStyle::xlUnderlineStyleNone;
350 1 : aValue >>= nValue;
351 1 : switch ( nValue )
352 : {
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.
359 :
360 : case excel::XlUnderlineStyle::xlUnderlineStyleNone:
361 0 : nValue = awt::FontUnderline::NONE;
362 0 : break;
363 : case excel::XlUnderlineStyle::xlUnderlineStyleSingle:
364 : case excel::XlUnderlineStyle::xlUnderlineStyleSingleAccounting:
365 1 : nValue = awt::FontUnderline::SINGLE;
366 1 : break;
367 : case excel::XlUnderlineStyle::xlUnderlineStyleDouble:
368 : case excel::XlUnderlineStyle::xlUnderlineStyleDoubleAccounting:
369 0 : nValue = awt::FontUnderline::DOUBLE;
370 0 : break;
371 : default:
372 0 : throw uno::RuntimeException("Unknown value for Underline" );
373 : }
374 :
375 1 : mxFont->setPropertyValue("CharUnderline", uno::Any(nValue) );
376 :
377 1 : }
378 :
379 : uno::Any SAL_CALL
380 2 : ScVbaFont::getUnderline() throw ( uno::RuntimeException, std::exception )
381 : {
382 2 : if ( GetDataSet() )
383 2 : if ( GetDataSet()->GetItemState( ATTR_FONT_UNDERLINE, true, NULL) == SfxItemState::DONTCARE )
384 0 : return aNULL();
385 :
386 2 : sal_Int32 nValue = awt::FontUnderline::NONE;
387 2 : mxFont->getPropertyValue("CharUnderline") >>= nValue;
388 2 : switch ( nValue )
389 : {
390 : case awt::FontUnderline::DOUBLE:
391 0 : nValue = excel::XlUnderlineStyle::xlUnderlineStyleDouble;
392 0 : break;
393 : case awt::FontUnderline::SINGLE:
394 1 : nValue = excel::XlUnderlineStyle::xlUnderlineStyleSingle;
395 1 : break;
396 : case awt::FontUnderline::NONE:
397 1 : nValue = excel::XlUnderlineStyle::xlUnderlineStyleNone;
398 1 : break;
399 : default:
400 0 : throw uno::RuntimeException("Unknown value retrieved for Underline" );
401 :
402 : }
403 2 : return uno::makeAny( nValue );
404 : }
405 :
406 : uno::Any SAL_CALL
407 2 : ScVbaFont::getStrikethrough() throw ( uno::RuntimeException, std::exception )
408 : {
409 2 : if ( GetDataSet() )
410 2 : if ( GetDataSet()->GetItemState( ATTR_FONT_CROSSEDOUT, true, NULL) == SfxItemState::DONTCARE )
411 0 : return aNULL();
412 2 : return ScVbaFont_BASE::getStrikethrough();
413 : }
414 :
415 : uno::Any SAL_CALL
416 2 : ScVbaFont::getShadow() throw (uno::RuntimeException, std::exception)
417 : {
418 2 : if ( GetDataSet() )
419 2 : if ( GetDataSet()->GetItemState( ATTR_FONT_SHADOWED, true, NULL) == SfxItemState::DONTCARE )
420 0 : return aNULL();
421 2 : return ScVbaFont_BASE::getShadow();
422 : }
423 :
424 : uno::Any SAL_CALL
425 3 : ScVbaFont::getItalic() throw ( uno::RuntimeException, std::exception )
426 : {
427 3 : if ( GetDataSet() )
428 3 : if ( GetDataSet()->GetItemState( ATTR_FONT_POSTURE, true, NULL) == SfxItemState::DONTCARE )
429 0 : return aNULL();
430 :
431 3 : return ScVbaFont_BASE::getItalic();
432 : }
433 :
434 : uno::Any SAL_CALL
435 2 : ScVbaFont::getName() throw ( uno::RuntimeException, std::exception )
436 : {
437 2 : if ( GetDataSet() )
438 2 : if ( GetDataSet()->GetItemState( ATTR_FONT, true, NULL) == SfxItemState::DONTCARE )
439 0 : return aNULL();
440 2 : return ScVbaFont_BASE::getName();
441 : }
442 : uno::Any
443 6 : ScVbaFont::getColor() throw (uno::RuntimeException, std::exception)
444 : {
445 : // #TODO #FIXME - behave like getXXX above ( wrt. GetDataSet )
446 6 : uno::Any aAny;
447 6 : aAny = OORGBToXLRGB( mxFont->getPropertyValue("CharColor") );
448 6 : return aAny;
449 : }
450 :
451 : void SAL_CALL
452 1 : ScVbaFont::setOutlineFont( const uno::Any& aValue ) throw ( uno::RuntimeException, std::exception )
453 : {
454 1 : mxFont->setPropertyValue("CharContoured", aValue );
455 1 : }
456 :
457 : uno::Any SAL_CALL
458 2 : ScVbaFont::getOutlineFont() throw (uno::RuntimeException, std::exception)
459 : {
460 2 : if ( GetDataSet() )
461 2 : if ( GetDataSet()->GetItemState( ATTR_FONT_CONTOUR, true, NULL) == SfxItemState::DONTCARE )
462 0 : return aNULL();
463 2 : return mxFont->getPropertyValue("CharContoured");
464 : }
465 :
466 : OUString
467 0 : ScVbaFont::getServiceImplName()
468 : {
469 0 : return OUString("ScVbaFont");
470 : }
471 :
472 : uno::Sequence< OUString >
473 0 : ScVbaFont::getServiceNames()
474 : {
475 0 : static uno::Sequence< OUString > aServiceNames;
476 0 : if ( aServiceNames.getLength() == 0 )
477 : {
478 0 : aServiceNames.realloc( 1 );
479 0 : aServiceNames[ 0 ] = "ooo.vba.excel.Font";
480 : }
481 0 : return aServiceNames;
482 9 : }
483 :
484 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|