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