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

Generated by: LCOV version 1.10