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
20 #include <drawingfragment.hxx>
21
22 #include <basegfx/matrix/b2dhommatrix.hxx>
23 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/container/XIndexContainer.hpp>
26 #include <com/sun/star/container/XNameReplace.hpp>
27 #include <com/sun/star/document/XEventsSupplier.hpp>
28 #include <com/sun/star/drawing/XControlShape.hpp>
29 #include <com/sun/star/drawing/XDrawPage.hpp>
30 #include <com/sun/star/drawing/XShapes.hpp>
31 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
32 #include <com/sun/star/script/XEventAttacherManager.hpp>
33 #include <rtl/strbuf.hxx>
34 #include <svx/svdobj.hxx>
35 #include <drwlayer.hxx>
36 #include <userdat.hxx>
37 #include <oox/core/filterbase.hxx>
38 #include <oox/drawingml/connectorshapecontext.hxx>
39 #include <oox/drawingml/graphicshapecontext.hxx>
40 #include <oox/helper/attributelist.hxx>
41 #include <oox/helper/propertyset.hxx>
42 #include <oox/token/namespaces.hxx>
43 #include <oox/token/properties.hxx>
44 #include <oox/token/tokens.hxx>
45 #include <oox/vml/vmlshape.hxx>
46 #include <oox/vml/vmlshapecontainer.hxx>
47 #include <osl/diagnose.h>
48 #include <formulaparser.hxx>
49 #include <stylesbuffer.hxx>
50 #include <themebuffer.hxx>
51 #include <unitconverter.hxx>
52 #include <worksheetbuffer.hxx>
53 namespace oox::xls {
54
55 using namespace ::com::sun::star::beans;
56 using namespace ::com::sun::star::container;
57 using namespace ::com::sun::star::document;
58 using namespace ::com::sun::star::drawing;
59 using namespace ::com::sun::star::script;
60 using namespace ::com::sun::star::uno;
61 using namespace ::oox::core;
62 using namespace ::oox::drawingml;
63 using namespace ::oox::ole;
64
65 using ::com::sun::star::awt::Size;
66 using ::com::sun::star::awt::Point;
67 using ::com::sun::star::awt::Rectangle;
68 using ::com::sun::star::awt::XControlModel;
69 // no using's for ::oox::vml, that may clash with ::oox::drawingml types
70
ShapeMacroAttacher(const OUString & rMacroName,const Reference<XShape> & rxShape)71 ShapeMacroAttacher::ShapeMacroAttacher( const OUString& rMacroName, const Reference< XShape >& rxShape ) :
72 VbaMacroAttacherBase( rMacroName ),
73 mxShape( rxShape )
74 {
75 }
76
attachMacro(const OUString & rMacroUrl)77 void ShapeMacroAttacher::attachMacro( const OUString& rMacroUrl )
78 {
79 try
80 {
81 Reference< XEventsSupplier > xSupplier( mxShape, UNO_QUERY_THROW );
82 Reference< XNameReplace > xEvents( xSupplier->getEvents(), UNO_SET_THROW );
83 Sequence< PropertyValue > aEventProps( 2 );
84 aEventProps[ 0 ].Name = "EventType";
85 aEventProps[ 0 ].Value <<= OUString( "Script" );
86 aEventProps[ 1 ].Name = "Script";
87 aEventProps[ 1 ].Value <<= rMacroUrl;
88 xEvents->replaceByName( "OnClick", Any( aEventProps ) );
89 }
90 catch( Exception& )
91 {
92 }
93 }
94
Shape(const WorksheetHelper & rHelper,const AttributeList & rAttribs,const char * pcServiceName)95 Shape::Shape( const WorksheetHelper& rHelper, const AttributeList& rAttribs, const char* pcServiceName ) :
96 ::oox::drawingml::Shape( pcServiceName ),
97 WorksheetHelper( rHelper )
98 {
99 OUString aMacro = rAttribs.getXString( XML_macro, OUString() );
100 if( !aMacro.isEmpty() )
101 maMacroName = getFormulaParser().importMacroName( aMacro );
102 }
103
finalizeXShape(XmlFilterBase & rFilter,const Reference<XShapes> & rxShapes)104 void Shape::finalizeXShape( XmlFilterBase& rFilter, const Reference< XShapes >& rxShapes )
105 {
106 OUString sURL;
107 getShapeProperties().getProperty( PROP_URL ) >>= sURL;
108 getWorksheets().convertSheetNameRef( sURL );
109 if( !maMacroName.isEmpty() && mxShape.is() )
110 {
111 VbaMacroAttacherRef xAttacher = std::make_shared<ShapeMacroAttacher>( maMacroName, mxShape );
112 getBaseFilter().getVbaProject().registerMacroAttacher( xAttacher );
113 }
114 ::oox::drawingml::Shape::finalizeXShape( rFilter, rxShapes );
115 if ( !sURL.isEmpty() )
116 {
117 SdrObject* pObj = SdrObject::getSdrObjectFromXShape( mxShape );
118 if ( pObj )
119 {
120 if ( ScMacroInfo* pInfo = ScDrawLayer::GetMacroInfo( pObj, true ) )
121 pInfo->SetHlink( sURL );
122 }
123 }
124 }
125
GroupShapeContext(const FragmentHandler2 & rParent,const WorksheetHelper & rHelper,const ShapePtr & rxParentShape,const ShapePtr & rxShape)126 GroupShapeContext::GroupShapeContext( const FragmentHandler2& rParent,
127 const WorksheetHelper& rHelper, const ShapePtr& rxParentShape, const ShapePtr& rxShape ) :
128 ShapeGroupContext( rParent, rxParentShape, rxShape ),
129 WorksheetHelper( rHelper )
130 {
131 }
132
createShapeContext(FragmentHandler2 & rParent,const WorksheetHelper & rHelper,sal_Int32 nElement,const AttributeList & rAttribs,const ShapePtr & rxParentShape,ShapePtr * pxShape)133 /*static*/ ContextHandlerRef GroupShapeContext::createShapeContext( FragmentHandler2& rParent,
134 const WorksheetHelper& rHelper, sal_Int32 nElement, const AttributeList& rAttribs,
135 const ShapePtr& rxParentShape, ShapePtr* pxShape )
136 {
137 switch( nElement )
138 {
139 case XDR_TOKEN( sp ):
140 {
141 ShapePtr xShape = std::make_shared<Shape>( rHelper, rAttribs, "com.sun.star.drawing.CustomShape" );
142 if( pxShape ) *pxShape = xShape;
143 return new ShapeContext( rParent, rxParentShape, xShape );
144 }
145 case XDR_TOKEN( cxnSp ):
146 {
147 ShapePtr xShape = std::make_shared<Shape>( rHelper, rAttribs, "com.sun.star.drawing.ConnectorShape" );
148 if( pxShape ) *pxShape = xShape;
149 return new ConnectorShapeContext( rParent, rxParentShape, xShape );
150 }
151 case XDR_TOKEN( pic ):
152 {
153 ShapePtr xShape = std::make_shared<Shape>( rHelper, rAttribs, "com.sun.star.drawing.GraphicObjectShape" );
154 if( pxShape ) *pxShape = xShape;
155 return new GraphicShapeContext( rParent, rxParentShape, xShape );
156 }
157 case XDR_TOKEN( graphicFrame ):
158 {
159 ShapePtr xShape = std::make_shared<Shape>( rHelper, rAttribs, "com.sun.star.drawing.GraphicObjectShape" );
160 if( pxShape ) *pxShape = xShape;
161 return new GraphicalObjectFrameContext( rParent, rxParentShape, xShape, rHelper.getSheetType() != WorksheetType::Chart );
162 }
163 case XDR_TOKEN( grpSp ):
164 {
165 ShapePtr xShape = std::make_shared<Shape>( rHelper, rAttribs, "com.sun.star.drawing.GroupShape" );
166 if( pxShape ) *pxShape = xShape;
167 return new GroupShapeContext( rParent, rHelper, rxParentShape, xShape );
168 }
169 }
170 return nullptr;
171 }
172
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)173 ContextHandlerRef GroupShapeContext::onCreateContext(
174 sal_Int32 nElement, const AttributeList& rAttribs )
175 {
176 ContextHandlerRef xContext = createShapeContext( *this, *this, nElement, rAttribs, mpGroupShapePtr );
177 return xContext ? xContext : ShapeGroupContext::onCreateContext( nElement, rAttribs );
178 }
179
DrawingFragment(const WorksheetHelper & rHelper,const OUString & rFragmentPath)180 DrawingFragment::DrawingFragment( const WorksheetHelper& rHelper, const OUString& rFragmentPath ) :
181 WorksheetFragmentBase( rHelper, rFragmentPath ),
182 mxDrawPage( rHelper.getDrawPage() )
183 {
184 OSL_ENSURE( mxDrawPage.is(), "DrawingFragment::DrawingFragment - missing drawing page" );
185 }
186
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)187 ContextHandlerRef DrawingFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
188 {
189 switch( getCurrentElement() )
190 {
191 case XML_ROOT_CONTEXT:
192 if( nElement == XDR_TOKEN( wsDr ) ) return this;
193 break;
194
195 case XDR_TOKEN( wsDr ):
196 switch( nElement )
197 {
198 case XDR_TOKEN( absoluteAnchor ):
199 case XDR_TOKEN( oneCellAnchor ):
200 case XDR_TOKEN( twoCellAnchor ):
201 mxAnchor.reset( new ShapeAnchor( *this ) );
202 mxAnchor->importAnchor( nElement, rAttribs );
203 return this;
204 }
205 break;
206
207 case XDR_TOKEN( absoluteAnchor ):
208 case XDR_TOKEN( oneCellAnchor ):
209 case XDR_TOKEN( twoCellAnchor ):
210 {
211 switch( nElement )
212 {
213 case XDR_TOKEN( from ):
214 case XDR_TOKEN( to ): return this;
215
216 case XDR_TOKEN( pos ): if( mxAnchor ) mxAnchor->importPos( rAttribs ); break;
217 case XDR_TOKEN( ext ): if( mxAnchor ) mxAnchor->importExt( rAttribs ); break;
218 case XDR_TOKEN( clientData ): if( mxAnchor ) mxAnchor->importClientData( rAttribs ); break;
219
220 default: return GroupShapeContext::createShapeContext( *this, *this, nElement, rAttribs, ShapePtr(), &mxShape );
221 }
222 }
223 break;
224
225 case XDR_TOKEN( from ):
226 case XDR_TOKEN( to ):
227 switch( nElement )
228 {
229 case XDR_TOKEN( col ):
230 case XDR_TOKEN( row ):
231 case XDR_TOKEN( colOff ):
232 case XDR_TOKEN( rowOff ): return this; // collect index in onCharacters()
233 }
234 break;
235 }
236 return nullptr;
237 }
238
onCharacters(const OUString & rChars)239 void DrawingFragment::onCharacters( const OUString& rChars )
240 {
241 switch( getCurrentElement() )
242 {
243 case XDR_TOKEN( col ):
244 case XDR_TOKEN( row ):
245 case XDR_TOKEN( colOff ):
246 case XDR_TOKEN( rowOff ):
247 if( mxAnchor ) mxAnchor->setCellPos( getCurrentElement(), getParentElement(), rChars );
248 break;
249 }
250 }
251
onEndElement()252 void DrawingFragment::onEndElement()
253 {
254 switch( getCurrentElement() )
255 {
256 case XDR_TOKEN( absoluteAnchor ):
257 case XDR_TOKEN( oneCellAnchor ):
258 case XDR_TOKEN( twoCellAnchor ):
259 if( mxDrawPage.is() && mxShape && mxAnchor )
260 {
261 EmuRectangle aShapeRectEmu = mxAnchor->calcAnchorRectEmu( getDrawPageSize() );
262 const bool bIsShapeVisible = mxAnchor->isAnchorValid();
263 if( (aShapeRectEmu.X >= 0) && (aShapeRectEmu.Y >= 0) && (aShapeRectEmu.Width >= 0) && (aShapeRectEmu.Height >= 0) )
264 {
265 const sal_Int32 aRotation = mxShape->getRotation();
266 if ((aRotation >= 45 * PER_DEGREE && aRotation < 135 * PER_DEGREE)
267 || (aRotation >= 225 * PER_DEGREE && aRotation < 315 * PER_DEGREE))
268 {
269 // When rotating any shape in MSO Excel within the range of degrees given above,
270 // Excel changes the cells in which the shape is anchored. The new position of
271 // the anchors are always calculated using a 90 degrees rotation anticlockwise.
272 // There is an important result of this operation: the top left point of the shape changes,
273 // it will be another vertex.
274 // The anchor position is given in the xml file, it is in the xdr:from and xdr:to elements.
275 // Let's see what happens in time order:
276 // We create a shape in Excel, the anchor position is in a given cell, then the rotation happens
277 // as mentioned above, and excel recalculates the cells in which the anchors are positioned.
278 // This new cell is exported into the xml elements xdr:from and xdr:to, when Excel exports the document!
279 // Thus, if we have a 90 degrees rotation and an already rotated point from which we base
280 // our calculations here in LO, the result is an incorrect 180 degrees rotation.
281 // Now, we need to create the bounding rectangle of the shape with this in mind.
282 // (Important to mention that at this point we don't talk about rotations at all, this bounding
283 // rectangle contains the original not-rotated shape. Rotation happens later in the code.)
284 // We get the new (x, y) coords, then swap width with height.
285 // To get the new coords we reflect the rectangle in the line y = x. (This will return the
286 // correct vertex, which is the actual top left one.)
287 // Another fact that appears to be true in Excel is that there are only 2 of possible anchor
288 // positions for a shape that is only rotated (and not resized for example).
289 // The first position happens in the set of degrees {[45, 135) U [225, 315)} and the second
290 // set is all the other angles. The two sets partition the circle (of all rotations: 360 degrees).
291 sal_Int64 nHalfWidth = aShapeRectEmu.Width / 2;
292 sal_Int64 nHalfHeight = aShapeRectEmu.Height / 2;
293 aShapeRectEmu.X = aShapeRectEmu.X + nHalfWidth - nHalfHeight;
294 aShapeRectEmu.Y = aShapeRectEmu.Y + nHalfHeight - nHalfWidth;
295 std::swap(aShapeRectEmu.Width, aShapeRectEmu.Height);
296 }
297
298 // TODO: DrawingML implementation expects 32-bit coordinates for EMU rectangles (change that to EmuRectangle)
299 // tdf#135918: Negative X,Y position has to be allowed to avoid shape displacement on rotation.
300 // The negative values can exist because of previous lines where the anchor rectangle must be mirrored in some ranges.
301 Rectangle aShapeRectEmu32(
302 getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.X, SAL_MIN_INT32, SAL_MAX_INT32 ),
303 getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.Y, SAL_MIN_INT32, SAL_MAX_INT32 ),
304 getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.Width, 0, SAL_MAX_INT32 ),
305 getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.Height, 0, SAL_MAX_INT32 ) );
306
307 // Make sure to set the position and size *before* calling addShape().
308 mxShape->setPosition(Point(aShapeRectEmu32.X, aShapeRectEmu32.Y));
309 mxShape->setSize(Size(aShapeRectEmu32.Width, aShapeRectEmu32.Height));
310
311 basegfx::B2DHomMatrix aTransformation;
312 if ( !bIsShapeVisible)
313 mxShape->setHidden(true);
314
315 mxShape->addShape( getOoxFilter(), &getTheme(), mxDrawPage, aTransformation, mxShape->getFillProperties() );
316
317 /* Collect all shape positions in the WorksheetHelper base
318 class. But first, scale EMUs to 1/100 mm. */
319 Rectangle aShapeRectHmm(
320 convertEmuToHmm(aShapeRectEmu32.X > 0 ? aShapeRectEmu32.X : 0), convertEmuToHmm(aShapeRectEmu32.Y > 0 ? aShapeRectEmu32.Y : 0),
321 convertEmuToHmm(aShapeRectEmu32.Width ), convertEmuToHmm(aShapeRectEmu32.Height ) );
322 extendShapeBoundingBox( aShapeRectHmm );
323 // set cell Anchoring
324 if ( mxAnchor->getEditAs() != ShapeAnchor::ANCHOR_ABSOLUTE )
325 {
326 SdrObject* pObj = SdrObject::getSdrObjectFromXShape( mxShape->getXShape() );
327 if ( pObj )
328 {
329 bool bResizeWithCell = mxAnchor->getEditAs() == ShapeAnchor::ANCHOR_TWOCELL;
330 ScDrawLayer::SetCellAnchoredFromPosition( *pObj, getScDocument(), getSheetIndex(), bResizeWithCell );
331 }
332 }
333 }
334 }
335 mxShape.reset();
336 mxAnchor.reset();
337 break;
338 }
339 }
340
341 // VML
342
343 namespace {
344
345 class VmlFindNoteFunc
346 {
347 public:
348 explicit VmlFindNoteFunc( const ScAddress& rPos );
349 bool operator()( const ::oox::vml::ShapeBase& rShape ) const;
350
351 private:
352 sal_Int32 mnCol;
353 sal_Int32 mnRow;
354 };
355
VmlFindNoteFunc(const ScAddress & rPos)356 VmlFindNoteFunc::VmlFindNoteFunc( const ScAddress& rPos ) :
357 mnCol( rPos.Col() ),
358 mnRow( rPos.Row() )
359 {
360 }
361
operator ()(const::oox::vml::ShapeBase & rShape) const362 bool VmlFindNoteFunc::operator()( const ::oox::vml::ShapeBase& rShape ) const
363 {
364 const ::oox::vml::ClientData* pClientData = rShape.getClientData();
365 return pClientData && (pClientData->mnCol == mnCol) && (pClientData->mnRow == mnRow);
366 }
367
368 } // namespace
369
VmlControlMacroAttacher(const OUString & rMacroName,const Reference<XIndexContainer> & rxCtrlFormIC,sal_Int32 nCtrlIndex,sal_Int32 nCtrlType,sal_Int32 nDropStyle)370 VmlControlMacroAttacher::VmlControlMacroAttacher( const OUString& rMacroName,
371 const Reference< XIndexContainer >& rxCtrlFormIC, sal_Int32 nCtrlIndex, sal_Int32 nCtrlType, sal_Int32 nDropStyle ) :
372 VbaMacroAttacherBase( rMacroName ),
373 mxCtrlFormIC( rxCtrlFormIC ),
374 mnCtrlIndex( nCtrlIndex ),
375 mnCtrlType( nCtrlType ),
376 mnDropStyle( nDropStyle )
377 {
378 }
379
attachMacro(const OUString & rMacroUrl)380 void VmlControlMacroAttacher::attachMacro( const OUString& rMacroUrl )
381 {
382 ScriptEventDescriptor aEventDesc;
383 aEventDesc.ScriptType = "Script";
384 aEventDesc.ScriptCode = rMacroUrl;
385
386 // editable drop downs are treated like edit boxes
387 bool bEditDropDown = (mnCtrlType == XML_Drop) && (mnDropStyle == XML_ComboEdit);
388 sal_Int32 nCtrlType = bEditDropDown ? XML_Edit : mnCtrlType;
389
390 switch( nCtrlType )
391 {
392 case XML_Button:
393 case XML_Checkbox:
394 case XML_Radio:
395 aEventDesc.ListenerType = "XActionListener";
396 aEventDesc.EventMethod = "actionPerformed";
397 break;
398 case XML_Label:
399 case XML_GBox:
400 case XML_Dialog:
401 aEventDesc.ListenerType = "XMouseListener";
402 aEventDesc.EventMethod = "mouseReleased";
403 break;
404 case XML_Edit:
405 aEventDesc.ListenerType = "XTextListener";
406 aEventDesc.EventMethod = "textChanged";
407 break;
408 case XML_Spin:
409 case XML_Scroll:
410 aEventDesc.ListenerType = "XAdjustmentListener";
411 aEventDesc.EventMethod = "adjustmentValueChanged";
412 break;
413 case XML_List:
414 case XML_Drop:
415 aEventDesc.ListenerType = "XChangeListener";
416 aEventDesc.EventMethod = "changed";
417 break;
418 default:
419 OSL_ENSURE( false, "VmlControlMacroAttacher::attachMacro - unexpected object type" );
420 return;
421 }
422
423 try
424 {
425 Reference< XEventAttacherManager > xEventMgr( mxCtrlFormIC, UNO_QUERY_THROW );
426 xEventMgr->registerScriptEvent( mnCtrlIndex, aEventDesc );
427 }
428 catch( Exception& )
429 {
430 }
431 }
432
VmlDrawing(const WorksheetHelper & rHelper)433 VmlDrawing::VmlDrawing( const WorksheetHelper& rHelper ) :
434 ::oox::vml::Drawing( rHelper.getOoxFilter(), rHelper.getDrawPage(), ::oox::vml::VMLDRAWING_EXCEL ),
435 WorksheetHelper( rHelper ),
436 maControlConv( rHelper.getBaseFilter().getModel(), rHelper.getBaseFilter().getGraphicHelper() )
437 {
438 // default font for legacy listboxes and dropdowns: Tahoma, 8pt
439 maListBoxFont.moName = "Tahoma";
440 maListBoxFont.moColor = "auto";
441 maListBoxFont.monSize = 160;
442 }
443
getNoteShape(const ScAddress & rPos) const444 const ::oox::vml::ShapeBase* VmlDrawing::getNoteShape( const ScAddress& rPos ) const
445 {
446 return getShapes().findShape( VmlFindNoteFunc( rPos ) );
447 }
448
isShapeSupported(const::oox::vml::ShapeBase & rShape) const449 bool VmlDrawing::isShapeSupported( const ::oox::vml::ShapeBase& rShape ) const
450 {
451 const ::oox::vml::ClientData* pClientData = rShape.getClientData();
452 return !pClientData || (pClientData->mnObjType != XML_Note);
453 }
454
getShapeBaseName(const::oox::vml::ShapeBase & rShape) const455 OUString VmlDrawing::getShapeBaseName( const ::oox::vml::ShapeBase& rShape ) const
456 {
457 if( const ::oox::vml::ClientData* pClientData = rShape.getClientData() )
458 {
459 switch( pClientData->mnObjType )
460 {
461 case XML_Button: return "Button";
462 case XML_Checkbox: return "Check Box";
463 case XML_Dialog: return "Dialog Frame";
464 case XML_Drop: return "Drop Down";
465 case XML_Edit: return "Edit Box";
466 case XML_GBox: return "Group Box";
467 case XML_Label: return "Label";
468 case XML_List: return "List Box";
469 case XML_Note: return "Comment";
470 case XML_Pict: return (pClientData->mbDde || getOleObjectInfo( rShape.getShapeId() )) ? OUString( "Object" ) : OUString( "Picture" );
471 case XML_Radio: return "Option Button";
472 case XML_Scroll: return "Scroll Bar";
473 case XML_Spin: return "Spinner";
474 }
475 }
476 return ::oox::vml::Drawing::getShapeBaseName( rShape );
477 }
478
convertClientAnchor(Rectangle & orShapeRect,const OUString & rShapeAnchor) const479 bool VmlDrawing::convertClientAnchor( Rectangle& orShapeRect, const OUString& rShapeAnchor ) const
480 {
481 if( rShapeAnchor.isEmpty() )
482 return false;
483 ShapeAnchor aAnchor( *this );
484 aAnchor.importVmlAnchor( rShapeAnchor );
485 orShapeRect = aAnchor.calcAnchorRectHmm( getDrawPageSize() );
486 return (orShapeRect.Width >= 0) && (orShapeRect.Height >= 0);
487 }
488
createAndInsertClientXShape(const::oox::vml::ShapeBase & rShape,const Reference<XShapes> & rxShapes,const Rectangle & rShapeRect) const489 Reference< XShape > VmlDrawing::createAndInsertClientXShape( const ::oox::vml::ShapeBase& rShape,
490 const Reference< XShapes >& rxShapes, const Rectangle& rShapeRect ) const
491 {
492 // simulate the legacy drawing controls with OLE form controls
493 OUString aShapeName = rShape.getShapeName();
494 const ::oox::vml::ClientData* pClientData = rShape.getClientData();
495 if( !aShapeName.isEmpty() && pClientData )
496 {
497 Rectangle aShapeRect = rShapeRect;
498 const ::oox::vml::TextBox* pTextBox = rShape.getTextBox();
499 EmbeddedControl aControl( aShapeName );
500 switch( pClientData->mnObjType )
501 {
502 case XML_Button:
503 {
504 AxCommandButtonModel& rAxModel = aControl.createModel< AxCommandButtonModel >();
505 convertControlText( rAxModel.maFontData, rAxModel.mnTextColor, rAxModel.maCaption, pTextBox, pClientData->mnTextHAlign );
506 rAxModel.mnFlags = AX_FLAGS_ENABLED | AX_FLAGS_OPAQUE | AX_FLAGS_WORDWRAP;
507 rAxModel.mnVerticalAlign = pClientData->mnTextVAlign;
508 }
509 break;
510
511 case XML_Label:
512 {
513 AxLabelModel& rAxModel = aControl.createModel< AxLabelModel >();
514 convertControlText( rAxModel.maFontData, rAxModel.mnTextColor, rAxModel.maCaption, pTextBox, pClientData->mnTextHAlign );
515 rAxModel.mnFlags = AX_FLAGS_ENABLED | AX_FLAGS_WORDWRAP;
516 rAxModel.mnBorderStyle = AX_BORDERSTYLE_NONE;
517 rAxModel.mnSpecialEffect = AX_SPECIALEFFECT_FLAT;
518 rAxModel.mnVerticalAlign = pClientData->mnTextVAlign;
519 }
520 break;
521
522 case XML_Edit:
523 {
524 bool bNumeric = (pClientData->mnVTEdit == ::oox::vml::VML_CLIENTDATA_INTEGER) || (pClientData->mnVTEdit == ::oox::vml::VML_CLIENTDATA_NUMBER);
525 AxMorphDataModelBase& rAxModel = bNumeric ?
526 static_cast< AxMorphDataModelBase& >( aControl.createModel< AxNumericFieldModel >() ) :
527 static_cast< AxMorphDataModelBase& >( aControl.createModel< AxTextBoxModel >() );
528 convertControlText( rAxModel.maFontData, rAxModel.mnTextColor, rAxModel.maValue, pTextBox, pClientData->mnTextHAlign );
529 setFlag( rAxModel.mnFlags, AX_FLAGS_MULTILINE, pClientData->mbMultiLine );
530 setFlag( rAxModel.mnScrollBars, AX_SCROLLBAR_VERTICAL, pClientData->mbVScroll );
531 if( pClientData->mbSecretEdit )
532 rAxModel.mnPasswordChar = '*';
533 }
534 break;
535
536 case XML_GBox:
537 {
538 AxFrameModel& rAxModel = aControl.createModel< AxFrameModel >();
539 convertControlText( rAxModel.maFontData, rAxModel.mnTextColor, rAxModel.maCaption, pTextBox, pClientData->mnTextHAlign );
540 rAxModel.mnBorderStyle = pClientData->mbNo3D ? AX_BORDERSTYLE_SINGLE : AX_BORDERSTYLE_NONE;
541 rAxModel.mnSpecialEffect = pClientData->mbNo3D ? AX_SPECIALEFFECT_FLAT : AX_SPECIALEFFECT_BUMPED;
542
543 /* Move top border of groupbox up by half font height, because
544 Excel specifies Y position of the groupbox border line
545 instead the top border of the caption text. */
546 if( const ::oox::vml::TextFontModel* pFontModel = pTextBox ? pTextBox->getFirstFont() : nullptr )
547 {
548 sal_Int32 nFontHeightHmm = getUnitConverter().scaleToMm100( pFontModel->monSize.get( 160 ), Unit::Twip );
549 sal_Int32 nYDiff = ::std::min< sal_Int32 >( nFontHeightHmm / 2, aShapeRect.Y );
550 aShapeRect.Y -= nYDiff;
551 aShapeRect.Height += nYDiff;
552 }
553 }
554 break;
555
556 case XML_Checkbox:
557 {
558 AxCheckBoxModel& rAxModel = aControl.createModel< AxCheckBoxModel >();
559 convertControlText( rAxModel.maFontData, rAxModel.mnTextColor, rAxModel.maCaption, pTextBox, pClientData->mnTextHAlign );
560 convertControlBackground( rAxModel, rShape );
561 rAxModel.maValue = OUString::number( pClientData->mnChecked );
562 rAxModel.mnSpecialEffect = pClientData->mbNo3D ? AX_SPECIALEFFECT_FLAT : AX_SPECIALEFFECT_SUNKEN;
563 rAxModel.mnVerticalAlign = pClientData->mnTextVAlign;
564 bool bTriState = (pClientData->mnChecked != ::oox::vml::VML_CLIENTDATA_UNCHECKED) && (pClientData->mnChecked != ::oox::vml::VML_CLIENTDATA_CHECKED);
565 rAxModel.mnMultiSelect = bTriState ? AX_SELECTION_MULTI : AX_SELECTION_SINGLE;
566 }
567 break;
568
569 case XML_Radio:
570 {
571 AxOptionButtonModel& rAxModel = aControl.createModel< AxOptionButtonModel >();
572
573 // unique name to prevent autoGrouping with ActiveX controls and which a GroupBox may override - see vmldrawing.cxx.
574 rAxModel.maGroupName = "autoGroup_formControl";
575 convertControlText( rAxModel.maFontData, rAxModel.mnTextColor, rAxModel.maCaption, pTextBox, pClientData->mnTextHAlign );
576 convertControlBackground( rAxModel, rShape );
577 rAxModel.maValue = OUString::number( pClientData->mnChecked );
578 rAxModel.mnSpecialEffect = pClientData->mbNo3D ? AX_SPECIALEFFECT_FLAT : AX_SPECIALEFFECT_SUNKEN;
579 rAxModel.mnVerticalAlign = pClientData->mnTextVAlign;
580 }
581 break;
582
583 case XML_List:
584 {
585 AxListBoxModel& rAxModel = aControl.createModel< AxListBoxModel >();
586 convertControlFontData( rAxModel.maFontData, rAxModel.mnTextColor, maListBoxFont );
587 rAxModel.mnBorderStyle = pClientData->mbNo3D2 ? AX_BORDERSTYLE_SINGLE : AX_BORDERSTYLE_NONE;
588 rAxModel.mnSpecialEffect = pClientData->mbNo3D2 ? AX_SPECIALEFFECT_FLAT : AX_SPECIALEFFECT_SUNKEN;
589 switch( pClientData->mnSelType )
590 {
591 case XML_Single: rAxModel.mnMultiSelect = AX_SELECTION_SINGLE; break;
592 case XML_Multi: rAxModel.mnMultiSelect = AX_SELECTION_MULTI; break;
593 case XML_Extend: rAxModel.mnMultiSelect = AX_SELECTION_EXTENDED; break;
594 }
595 }
596 break;
597
598 case XML_Drop:
599 {
600 AxComboBoxModel& rAxModel = aControl.createModel< AxComboBoxModel >();
601 convertControlFontData( rAxModel.maFontData, rAxModel.mnTextColor, maListBoxFont );
602 rAxModel.mnDisplayStyle = AX_DISPLAYSTYLE_DROPDOWN;
603 rAxModel.mnShowDropButton = AX_SHOWDROPBUTTON_ALWAYS;
604 rAxModel.mnBorderStyle = pClientData->mbNo3D2 ? AX_BORDERSTYLE_SINGLE : AX_BORDERSTYLE_NONE;
605 rAxModel.mnSpecialEffect = pClientData->mbNo3D2 ? AX_SPECIALEFFECT_FLAT : AX_SPECIALEFFECT_SUNKEN;
606 rAxModel.mnListRows = pClientData->mnDropLines;
607 }
608 break;
609
610 case XML_Spin:
611 {
612 AxSpinButtonModel& rAxModel = aControl.createModel< AxSpinButtonModel >();
613 rAxModel.mnMin = pClientData->mnMin;
614 rAxModel.mnMax = pClientData->mnMax;
615 rAxModel.mnPosition = pClientData->mnVal;
616 rAxModel.mnSmallChange = pClientData->mnInc;
617 }
618 break;
619
620 case XML_Scroll:
621 {
622 AxScrollBarModel& rAxModel = aControl.createModel< AxScrollBarModel >();
623 rAxModel.mnMin = pClientData->mnMin;
624 rAxModel.mnMax = pClientData->mnMax;
625 rAxModel.mnPosition = pClientData->mnVal;
626 rAxModel.mnSmallChange = pClientData->mnInc;
627 rAxModel.mnLargeChange = pClientData->mnPage;
628 }
629 break;
630
631 case XML_Dialog:
632 {
633 // fake with a group box
634 AxFrameModel& rAxModel = aControl.createModel< AxFrameModel >();
635 convertControlText( rAxModel.maFontData, rAxModel.mnTextColor, rAxModel.maCaption, pTextBox, XML_Left );
636 rAxModel.mnBorderStyle = AX_BORDERSTYLE_SINGLE;
637 rAxModel.mnSpecialEffect = AX_SPECIALEFFECT_FLAT;
638 }
639 break;
640 }
641
642 if( ControlModelBase* pAxModel = aControl.getModel() )
643 {
644 // create the control shape
645 pAxModel->maSize.first = aShapeRect.Width;
646 pAxModel->maSize.second = aShapeRect.Height;
647 sal_Int32 nCtrlIndex = -1;
648 Reference< XShape > xShape = createAndInsertXControlShape( aControl, rxShapes, aShapeRect, nCtrlIndex );
649
650 // control shape macro
651 if( xShape.is() && (nCtrlIndex >= 0) && !pClientData->maFmlaMacro.isEmpty() )
652 {
653 OUString aMacroName = getFormulaParser().importMacroName( pClientData->maFmlaMacro );
654 if( !aMacroName.isEmpty() )
655 {
656 Reference< XIndexContainer > xFormIC = getControlForm().getXForm();
657 VbaMacroAttacherRef xAttacher = std::make_shared<VmlControlMacroAttacher>( aMacroName, xFormIC, nCtrlIndex, pClientData->mnObjType, pClientData->mnDropStyle );
658 getBaseFilter().getVbaProject().registerMacroAttacher( xAttacher );
659 }
660 }
661
662 return xShape;
663 }
664 }
665
666 return Reference< XShape >();
667 }
668
notifyXShapeInserted(const Reference<XShape> & rxShape,const Rectangle & rShapeRect,const::oox::vml::ShapeBase & rShape,bool bGroupChild)669 void VmlDrawing::notifyXShapeInserted( const Reference< XShape >& rxShape,
670 const Rectangle& rShapeRect, const ::oox::vml::ShapeBase& rShape, bool bGroupChild )
671 {
672 // collect all shape positions in the WorksheetHelper base class (but not children of group shapes)
673 if( !bGroupChild )
674 extendShapeBoundingBox( rShapeRect );
675
676 // convert settings from VML client data
677 const ::oox::vml::ClientData* pClientData = rShape.getClientData();
678 if(!pClientData)
679 return;
680
681 // specific settings for embedded form controls
682 try
683 {
684 Reference< XControlShape > xCtrlShape( rxShape, UNO_QUERY_THROW );
685 Reference< XControlModel > xCtrlModel( xCtrlShape->getControl(), UNO_SET_THROW );
686 PropertySet aPropSet( xCtrlModel );
687
688 // printable
689 aPropSet.setProperty( PROP_Printable, pClientData->mbPrintObject );
690
691 // control source links
692 if( !pClientData->maFmlaLink.isEmpty() || !pClientData->maFmlaRange.isEmpty() )
693 maControlConv.bindToSources( xCtrlModel, pClientData->maFmlaLink, pClientData->maFmlaRange, getSheetIndex() );
694 }
695 catch( Exception& )
696 {
697 }
698 }
699
700 // private --------------------------------------------------------------------
701
convertControlTextColor(const OUString & rTextColor) const702 sal_uInt32 VmlDrawing::convertControlTextColor( const OUString& rTextColor ) const
703 {
704 // color attribute not present or 'auto' - use passed default color
705 if( rTextColor.isEmpty() || rTextColor.equalsIgnoreAsciiCase( "auto" ) )
706 return AX_SYSCOLOR_WINDOWTEXT;
707
708 if( rTextColor[ 0 ] == '#' )
709 {
710 // RGB colors in the format '#RRGGBB'
711 if( rTextColor.getLength() == 7 )
712 return OleHelper::encodeOleColor( rTextColor.copy( 1 ).toUInt32( 16 ) );
713
714 // RGB colors in the format '#RGB'
715 if( rTextColor.getLength() == 4 )
716 {
717 sal_Int32 nR = rTextColor.copy( 1, 1 ).toUInt32( 16 ) * 0x11;
718 sal_Int32 nG = rTextColor.copy( 2, 1 ).toUInt32( 16 ) * 0x11;
719 sal_Int32 nB = rTextColor.copy( 3, 1 ).toUInt32( 16 ) * 0x11;
720 return OleHelper::encodeOleColor( (nR << 16) | (nG << 8) | nB );
721 }
722
723 OSL_ENSURE( false, OStringBuffer( "VmlDrawing::convertControlTextColor - invalid color name '" ).
724 append( OUStringToOString( rTextColor, RTL_TEXTENCODING_ASCII_US ) ).append( '\'' ).getStr() );
725 return AX_SYSCOLOR_WINDOWTEXT;
726 }
727
728 const GraphicHelper& rGraphicHelper = getBaseFilter().getGraphicHelper();
729
730 /* Predefined color names or system color names (resolve to RGB to detect
731 valid color name). */
732 sal_Int32 nColorToken = AttributeConversion::decodeToken( rTextColor );
733 ::Color nRgbValue = Color::getVmlPresetColor( nColorToken, API_RGB_TRANSPARENT );
734 if( nRgbValue == API_RGB_TRANSPARENT )
735 nRgbValue = rGraphicHelper.getSystemColor( nColorToken );
736 if( nRgbValue != API_RGB_TRANSPARENT )
737 return OleHelper::encodeOleColor( nRgbValue );
738
739 // try palette color
740 return OleHelper::encodeOleColor( rGraphicHelper.getPaletteColor( rTextColor.toInt32() ) );
741 }
742
convertControlFontData(AxFontData & rAxFontData,sal_uInt32 & rnOleTextColor,const::oox::vml::TextFontModel & rFontModel) const743 void VmlDrawing::convertControlFontData( AxFontData& rAxFontData, sal_uInt32& rnOleTextColor, const ::oox::vml::TextFontModel& rFontModel ) const
744 {
745 if( rFontModel.moName.has() )
746 rAxFontData.maFontName = rFontModel.moName.get();
747
748 // font height: convert from twips to points, then to internal representation of AX controls
749 rAxFontData.setHeightPoints( static_cast< sal_Int16 >( (rFontModel.monSize.get( 200 ) + 10) / 20 ) );
750
751 // font effects
752 rAxFontData.mnFontEffects = AxFontFlags::NONE;
753 setFlag( rAxFontData.mnFontEffects, AxFontFlags::Bold, rFontModel.mobBold.get( false ) );
754 setFlag( rAxFontData.mnFontEffects, AxFontFlags::Italic, rFontModel.mobItalic.get( false ) );
755 setFlag( rAxFontData.mnFontEffects, AxFontFlags::Strikeout, rFontModel.mobStrikeout.get( false ) );
756 sal_Int32 nUnderline = rFontModel.monUnderline.get( XML_none );
757 setFlag( rAxFontData.mnFontEffects, AxFontFlags::Underline, nUnderline != XML_none );
758 rAxFontData.mbDblUnderline = nUnderline == XML_double;
759
760 // font color
761 rnOleTextColor = convertControlTextColor( rFontModel.moColor.get( OUString() ) );
762 }
763
convertControlText(AxFontData & rAxFontData,sal_uInt32 & rnOleTextColor,OUString & rCaption,const::oox::vml::TextBox * pTextBox,sal_Int32 nTextHAlign) const764 void VmlDrawing::convertControlText( AxFontData& rAxFontData, sal_uInt32& rnOleTextColor,
765 OUString& rCaption, const ::oox::vml::TextBox* pTextBox, sal_Int32 nTextHAlign ) const
766 {
767 if( pTextBox )
768 {
769 rCaption = pTextBox->getText();
770 if( const ::oox::vml::TextFontModel* pFontModel = pTextBox->getFirstFont() )
771 convertControlFontData( rAxFontData, rnOleTextColor, *pFontModel );
772 }
773
774 switch( nTextHAlign )
775 {
776 case XML_Left: rAxFontData.mnHorAlign = AxHorizontalAlign::Left; break;
777 case XML_Center: rAxFontData.mnHorAlign = AxHorizontalAlign::Center; break;
778 case XML_Right: rAxFontData.mnHorAlign = AxHorizontalAlign::Right; break;
779 default: rAxFontData.mnHorAlign = AxHorizontalAlign::Left;
780 }
781 }
782
convertControlBackground(AxMorphDataModelBase & rAxModel,const::oox::vml::ShapeBase & rShape) const783 void VmlDrawing::convertControlBackground( AxMorphDataModelBase& rAxModel, const ::oox::vml::ShapeBase& rShape ) const
784 {
785 const ::oox::vml::FillModel& rFillModel = rShape.getTypeModel().maFillModel;
786 bool bHasFill = rFillModel.moFilled.get( true );
787 setFlag( rAxModel.mnFlags, AX_FLAGS_OPAQUE, bHasFill );
788 if( bHasFill )
789 {
790 const GraphicHelper& rGraphicHelper = getBaseFilter().getGraphicHelper();
791 ::Color nSysWindowColor = rGraphicHelper.getSystemColor( XML_window, API_RGB_WHITE );
792 ::oox::drawingml::Color aColor = ::oox::vml::ConversionHelper::decodeColor( rGraphicHelper, rFillModel.moColor, rFillModel.moOpacity, nSysWindowColor );
793 ::Color nRgbValue = aColor.getColor( rGraphicHelper );
794 rAxModel.mnBackColor = OleHelper::encodeOleColor( nRgbValue );
795 }
796 }
797
VmlDrawingFragment(const WorksheetHelper & rHelper,const OUString & rFragmentPath)798 VmlDrawingFragment::VmlDrawingFragment( const WorksheetHelper& rHelper, const OUString& rFragmentPath ) :
799 ::oox::vml::DrawingFragment( rHelper.getOoxFilter(), rFragmentPath, rHelper.getVmlDrawing() ),
800 WorksheetHelper( rHelper )
801 {
802 }
803
finalizeImport()804 void VmlDrawingFragment::finalizeImport()
805 {
806 ::oox::vml::DrawingFragment::finalizeImport();
807 getVmlDrawing().convertAndInsert();
808 }
809
810 } // namespace oox
811
812 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
813