LCOV - code coverage report
Current view: top level - oox/source/vml - vmldrawing.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 74 95 77.9 %
Date: 2015-06-13 12:38:46 Functions: 20 27 74.1 %
Legend: Lines: hit not hit

          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             : 
      20             : #include "oox/vml/vmldrawing.hxx"
      21             : 
      22             : #include <algorithm>
      23             : #include <com/sun/star/beans/XPropertySet.hpp>
      24             : #include <com/sun/star/drawing/XControlShape.hpp>
      25             : #include <com/sun/star/drawing/XShapes.hpp>
      26             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      27             : #include <com/sun/star/text/HoriOrientation.hpp>
      28             : #include <com/sun/star/text/RelOrientation.hpp>
      29             : #include <com/sun/star/text/VertOrientation.hpp>
      30             : #include <osl/diagnose.h>
      31             : #include <rtl/ustring.hxx>
      32             : #include "oox/core/xmlfilterbase.hxx"
      33             : #include "oox/helper/containerhelper.hxx"
      34             : #include "oox/ole/axcontrol.hxx"
      35             : #include "oox/vml/vmlshape.hxx"
      36             : #include "oox/vml/vmlshapecontainer.hxx"
      37             : 
      38             : namespace oox {
      39             : namespace vml {
      40             : 
      41             : using namespace ::com::sun::star;
      42             : using namespace ::com::sun::star::awt;
      43             : using namespace ::com::sun::star::beans;
      44             : using namespace ::com::sun::star::drawing;
      45             : using namespace ::com::sun::star::lang;
      46             : using namespace ::com::sun::star::text;
      47             : using namespace ::com::sun::star::uno;
      48             : 
      49             : using ::oox::core::XmlFilterBase;
      50             : 
      51             : namespace {
      52             : 
      53             : /** Returns the textual representation of a numeric VML shape identifier. */
      54           0 : OUString lclGetShapeId( sal_Int32 nShapeId )
      55             : {
      56             :     // identifier consists of a literal NUL character, a lowercase 's', and the id
      57           0 :     sal_Unicode aStr[2] = { '\0', 's' };
      58           0 :     return OUString( aStr, 2 ) + OUString::number( nShapeId );
      59             : }
      60             : 
      61             : /** Returns the numeric VML shape identifier from its textual representation. */
      62           1 : sal_Int32 lclGetShapeId( const OUString& rShapeId )
      63             : {
      64             :     // identifier consists of a literal NUL character, a lowercase 's', and the id
      65           1 :     return ((rShapeId.getLength() >= 3) && (rShapeId[ 0 ] == '\0') && (rShapeId[ 1 ] == 's')) ? rShapeId.copy( 2 ).toInt32() : -1;
      66             : }
      67             : 
      68             : } // namespace
      69             : 
      70           4 : OleObjectInfo::OleObjectInfo( bool bDmlShape ) :
      71             :     mbAutoLoad( false ),
      72           4 :     mbDmlShape( bDmlShape )
      73             : {
      74           4 : }
      75             : 
      76           0 : void OleObjectInfo::setShapeId( sal_Int32 nShapeId )
      77             : {
      78           0 :     maShapeId = lclGetShapeId( nShapeId );
      79           0 : }
      80             : 
      81           0 : ControlInfo::ControlInfo()
      82             : {
      83           0 : }
      84             : 
      85           0 : void ControlInfo::setShapeId( sal_Int32 nShapeId )
      86             : {
      87           0 :     maShapeId = lclGetShapeId( nShapeId );
      88           0 : }
      89             : 
      90        1272 : Drawing::Drawing( XmlFilterBase& rFilter, const Reference< XDrawPage >& rxDrawPage, DrawingType eType ) :
      91             :     mrFilter( rFilter ),
      92             :     mxDrawPage( rxDrawPage ),
      93        1272 :     mxShapes( new ShapeContainer( *this ) ),
      94        2544 :     meType( eType )
      95             : {
      96             :     OSL_ENSURE( mxDrawPage.is(), "Drawing::Drawing - missing UNO draw page" );
      97        1272 : }
      98             : 
      99        2275 : Drawing::~Drawing()
     100             : {
     101        2275 : }
     102             : 
     103           1 : ::oox::ole::EmbeddedForm& Drawing::getControlForm() const
     104             : {
     105           1 :     if( !mxCtrlForm.get() )
     106             :         mxCtrlForm.reset( new ::oox::ole::EmbeddedForm(
     107           1 :             mrFilter.getModel(), mxDrawPage, mrFilter.getGraphicHelper() ) );
     108           1 :     return *mxCtrlForm;
     109             : }
     110             : 
     111           4 : void Drawing::registerBlockId( sal_Int32 nBlockId )
     112             : {
     113             :     OSL_ENSURE( nBlockId > 0, "Drawing::registerBlockId - invalid block index" );
     114           4 :     if( nBlockId > 0 )
     115             :     {
     116             :         // lower_bound() returns iterator pointing to element equal to nBlockId, if existing
     117           4 :         BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
     118           4 :         if( (aIt == maBlockIds.end()) || (nBlockId != *aIt) )
     119           4 :             maBlockIds.insert( aIt, nBlockId );
     120             :     }
     121           4 : }
     122             : 
     123           2 : void Drawing::registerOleObject( const OleObjectInfo& rOleObject )
     124             : {
     125             :     OSL_ENSURE( !rOleObject.maShapeId.isEmpty(), "Drawing::registerOleObject - missing OLE object shape id" );
     126             :     OSL_ENSURE( maOleObjects.count( rOleObject.maShapeId ) == 0, "Drawing::registerOleObject - OLE object already registered" );
     127           2 :     maOleObjects.insert( OleObjectInfoMap::value_type( rOleObject.maShapeId, rOleObject ) );
     128           2 : }
     129             : 
     130           0 : void Drawing::registerControl( const ControlInfo& rControl )
     131             : {
     132             :     OSL_ENSURE( !rControl.maShapeId.isEmpty(), "Drawing::registerControl - missing form control shape id" );
     133             :     OSL_ENSURE( !rControl.maName.isEmpty(), "Drawing::registerControl - missing form control name" );
     134             :     OSL_ENSURE( maControls.count( rControl.maShapeId ) == 0, "Drawing::registerControl - form control already registered" );
     135           0 :     maControls.insert( ControlInfoMap::value_type( rControl.maShapeId, rControl ) );
     136           0 : }
     137             : 
     138         369 : void Drawing::finalizeFragmentImport()
     139             : {
     140         369 :     mxShapes->finalizeFragmentImport();
     141         369 : }
     142             : 
     143         296 : void Drawing::convertAndInsert() const
     144             : {
     145         296 :     Reference< XShapes > xShapes( mxDrawPage, UNO_QUERY );
     146         296 :     mxShapes->convertAndInsert( xShapes );
     147         296 : }
     148             : 
     149           1 : sal_Int32 Drawing::getLocalShapeIndex( const OUString& rShapeId ) const
     150             : {
     151           1 :     sal_Int32 nShapeId = lclGetShapeId( rShapeId );
     152           1 :     if( nShapeId <= 0 ) return -1;
     153             : 
     154             :     /*  Shapes in a drawing are counted per registered shape identifier blocks
     155             :         as stored in the o:idmap element. The contents of this element have
     156             :         been stored in our member maBlockIds. Each block represents 1024 shape
     157             :         identifiers, starting with identifier 1 for the block #0. This means,
     158             :         block #0 represents the identifiers 1-1024, block #1 represents the
     159             :         identifiers 1025-2048, and so on. The local shape index has to be
     160             :         calculated according to all blocks registered for this drawing.
     161             : 
     162             :         Example:
     163             :             Registered for this drawing are blocks #1 and #3 (shape identifiers
     164             :             1025-2048 and 3073-4096).
     165             :             Shape identifier 1025 -> local shape index 1.
     166             :             Shape identifier 1026 -> local shape index 2.
     167             :             ...
     168             :             Shape identifier 2048 -> local shape index 1024.
     169             :             Shape identifier 3073 -> local shape index 1025.
     170             :             ...
     171             :             Shape identifier 4096 -> local shape index 2048.
     172             :      */
     173             : 
     174             :     // get block id from shape id and find its index in the list of used blocks
     175           1 :     sal_Int32 nBlockId = (nShapeId - 1) / 1024;
     176           1 :     BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
     177           1 :     sal_Int32 nIndex = static_cast< sal_Int32 >( aIt - maBlockIds.begin() );
     178             : 
     179             :     // block id not found in set -> register it now (value of nIndex remains valid)
     180           1 :     if( (aIt == maBlockIds.end()) || (*aIt != nBlockId) )
     181           0 :         maBlockIds.insert( aIt, nBlockId );
     182             : 
     183             :     // get one-based offset of shape id in its block
     184           1 :     sal_Int32 nBlockOffset = (nShapeId - 1) % 1024 + 1;
     185             : 
     186             :     // calculate the local shape index
     187           1 :     return 1024 * nIndex + nBlockOffset;
     188             : }
     189             : 
     190         344 : const OleObjectInfo* Drawing::getOleObjectInfo( const OUString& rShapeId ) const
     191             : {
     192         344 :     return ContainerHelper::getMapElement( maOleObjects, rShapeId );
     193             : }
     194             : 
     195         342 : const ControlInfo* Drawing::getControlInfo( const OUString& rShapeId ) const
     196             : {
     197         342 :     return ContainerHelper::getMapElement( maControls, rShapeId );
     198             : }
     199             : 
     200         963 : Reference< XShape > Drawing::createAndInsertXShape( const OUString& rService,
     201             :         const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const
     202             : {
     203             :     OSL_ENSURE( !rService.isEmpty(), "Drawing::createAndInsertXShape - missing UNO shape service name" );
     204             :     OSL_ENSURE( rxShapes.is(), "Drawing::createAndInsertXShape - missing XShapes container" );
     205         963 :     Reference< XShape > xShape;
     206         963 :     if( !rService.isEmpty() && rxShapes.is() ) try
     207             :     {
     208         963 :         Reference< XMultiServiceFactory > xModelFactory( mrFilter.getModelFactory(), UNO_SET_THROW );
     209         963 :         xShape.set( xModelFactory->createInstance( rService ), UNO_QUERY_THROW );
     210         963 :         if ( rService != "com.sun.star.text.TextFrame" )
     211             :         {
     212             :             // insert shape into passed shape collection (maybe drawpage or group shape)
     213         888 :             rxShapes->add( xShape );
     214         888 :             xShape->setPosition( awt::Point( rShapeRect.X, rShapeRect.Y ) );
     215             :         }
     216             :         else
     217             :         {
     218          75 :             Reference< XPropertySet > xPropSet( xShape, UNO_QUERY_THROW );
     219          75 :             xPropSet->setPropertyValue( "HoriOrient", makeAny( HoriOrientation::NONE ) );
     220          75 :             xPropSet->setPropertyValue( "VertOrient", makeAny( VertOrientation::NONE ) );
     221          75 :             xPropSet->setPropertyValue( "HoriOrientPosition", makeAny( rShapeRect.X ) );
     222          75 :             xPropSet->setPropertyValue( "VertOrientPosition", makeAny( rShapeRect.Y ) );
     223          75 :             xPropSet->setPropertyValue( "HoriOrientRelation", makeAny( RelOrientation::FRAME ) );
     224          75 :             xPropSet->setPropertyValue( "VertOrientRelation", makeAny( RelOrientation::FRAME ) );
     225             :         }
     226         963 :         xShape->setSize( awt::Size( rShapeRect.Width, rShapeRect.Height ) );
     227             :     }
     228           0 :     catch( Exception& e )
     229             :     {
     230             :         SAL_WARN( "oox", "Drawing::createAndInsertXShape - error during shape object creation: " << e.Message );
     231             :     }
     232             :     OSL_ENSURE( xShape.is(), "Drawing::createAndInsertXShape - cannot instanciate shape object" );
     233         963 :     return xShape;
     234             : }
     235             : 
     236           1 : Reference< XShape > Drawing::createAndInsertXControlShape( const ::oox::ole::EmbeddedControl& rControl,
     237             :         const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect, sal_Int32& rnCtrlIndex ) const
     238             : {
     239           1 :     Reference< XShape > xShape;
     240             :     try
     241             :     {
     242             :         // create control model and insert it into the form of the draw page
     243           1 :         Reference< XControlModel > xCtrlModel( getControlForm().convertAndInsert( rControl, rnCtrlIndex ), UNO_SET_THROW );
     244             : 
     245             :         // create the control shape
     246           1 :         xShape = createAndInsertXShape( "com.sun.star.drawing.ControlShape", rxShapes, rShapeRect );
     247             : 
     248             :         // set the control model at the shape
     249           1 :         Reference< XControlShape >( xShape, UNO_QUERY_THROW )->setControl( xCtrlModel );
     250             :     }
     251           0 :     catch (Exception const& e)
     252             :     {
     253             :         SAL_WARN("oox", "exception inserting Shape: " << e.Message);
     254             :     }
     255           1 :     return xShape;
     256             : }
     257             : 
     258         963 : bool Drawing::isShapeSupported( const ShapeBase& /*rShape*/ ) const
     259             : {
     260         963 :     return true;
     261             : }
     262             : 
     263         542 : OUString Drawing::getShapeBaseName( const ShapeBase& /*rShape*/ ) const
     264             : {
     265         542 :     return OUString();
     266             : }
     267             : 
     268           0 : bool Drawing::convertClientAnchor( awt::Rectangle& /*orShapeRect*/, const OUString& /*rShapeAnchor*/ ) const
     269             : {
     270           0 :     return false;
     271             : }
     272             : 
     273           0 : Reference< XShape > Drawing::createAndInsertClientXShape( const ShapeBase& /*rShape*/,
     274             :         const Reference< XShapes >& /*rxShapes*/, const awt::Rectangle& /*rShapeRect*/ ) const
     275             : {
     276           0 :     return Reference< XShape >();
     277             : }
     278             : 
     279         961 : void Drawing::notifyXShapeInserted( const Reference< XShape >& /*rxShape*/,
     280             :         const awt::Rectangle& /*rShapeRect*/, const ShapeBase& /*rShape*/, bool /*bGroupChild*/ )
     281             : {
     282         961 : }
     283             : 
     284             : } // namespace vml
     285         246 : } // namespave oox
     286             : 
     287             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11