LCOV - code coverage report
Current view: top level - writerfilter/source/dmapper - FormControlHelper.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 24 164 14.6 %
Date: 2014-04-11 Functions: 5 11 45.5 %
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 <math.h>
      21             : 
      22             : #include <com/sun/star/awt/XControlModel.hpp>
      23             : #include <com/sun/star/beans/XPropertySet.hpp>
      24             : #include <com/sun/star/container/XIndexContainer.hpp>
      25             : #include <com/sun/star/container/XNamed.hpp>
      26             : #include <com/sun/star/drawing/XControlShape.hpp>
      27             : #include <com/sun/star/drawing/XDrawPage.hpp>
      28             : #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
      29             : #include <com/sun/star/form/XForm.hpp>
      30             : #include <com/sun/star/form/XFormComponent.hpp>
      31             : #include <com/sun/star/form/XFormsSupplier.hpp>
      32             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      33             : #include <com/sun/star/text/TextContentAnchorType.hpp>
      34             : #include <com/sun/star/text/VertOrientation.hpp>
      35             : #include <com/sun/star/uno/Any.hxx>
      36             : #include <com/sun/star/uno/Type.hxx>
      37             : 
      38             : #include "FormControlHelper.hxx"
      39             : #include <xmloff/odffields.hxx>
      40             : 
      41             : namespace writerfilter {
      42             : namespace dmapper {
      43             : 
      44             : using namespace ::com::sun::star;
      45             : 
      46          16 : struct FormControlHelper::FormControlHelper_Impl
      47             : {
      48             :     FieldId m_eFieldId;
      49             :     awt::Size aSize;
      50             :     uno::Reference<drawing::XDrawPage> rDrawPage;
      51             :     uno::Reference<form::XForm> rForm;
      52             :     uno::Reference<form::XFormComponent> rFormComponent;
      53             :     uno::Reference<lang::XMultiServiceFactory> rServiceFactory;
      54             :     uno::Reference<text::XTextDocument> rTextDocument;
      55             : 
      56             :     uno::Reference<drawing::XDrawPage> getDrawPage();
      57             :     uno::Reference<lang::XMultiServiceFactory> getServiceFactory();
      58             :     uno::Reference<form::XForm> getForm();
      59             :     uno::Reference<container::XIndexContainer> getFormComps();
      60             : };
      61             : 
      62           0 : uno::Reference<drawing::XDrawPage> FormControlHelper::FormControlHelper_Impl::getDrawPage()
      63             : {
      64           0 :     if (! rDrawPage.is())
      65             :     {
      66             :         uno::Reference<drawing::XDrawPageSupplier>
      67           0 :             xDrawPageSupplier(rTextDocument, uno::UNO_QUERY);
      68           0 :         if (xDrawPageSupplier.is())
      69           0 :             rDrawPage = xDrawPageSupplier->getDrawPage();
      70             :     }
      71             : 
      72           0 :     return rDrawPage;
      73             : }
      74             : 
      75           0 : uno::Reference<lang::XMultiServiceFactory> FormControlHelper::FormControlHelper_Impl::getServiceFactory()
      76             : {
      77           0 :     if (! rServiceFactory.is())
      78           0 :         rServiceFactory = uno::Reference<lang::XMultiServiceFactory>(rTextDocument, uno::UNO_QUERY);
      79             : 
      80           0 :     return rServiceFactory;
      81             : }
      82             : 
      83           0 : uno::Reference<form::XForm> FormControlHelper::FormControlHelper_Impl::getForm()
      84             : {
      85           0 :     if (! rForm.is())
      86             :     {
      87           0 :         uno::Reference<form::XFormsSupplier> xFormsSupplier(getDrawPage(), uno::UNO_QUERY);
      88             : 
      89           0 :         if (xFormsSupplier.is())
      90             :         {
      91           0 :             uno::Reference<container::XNameContainer> xFormsNamedContainer(xFormsSupplier->getForms());
      92           0 :             static OUString sDOCXForm("DOCX-Standard");
      93             : 
      94           0 :             OUString sFormName(sDOCXForm);
      95           0 :             sal_uInt16 nUnique = 0;
      96             : 
      97           0 :             while (xFormsNamedContainer->hasByName(sFormName))
      98             :             {
      99           0 :                 ++nUnique;
     100           0 :                 sFormName = sDOCXForm;
     101           0 :                 sFormName += OUString(nUnique);
     102             :             }
     103             : 
     104           0 :             uno::Reference<uno::XInterface> xForm(getServiceFactory()->createInstance("com.sun.star.form.component.Form"));
     105           0 :             if (xForm.is())
     106             :             {
     107             :                 uno::Reference<beans::XPropertySet>
     108           0 :                     xFormProperties(xForm, uno::UNO_QUERY);
     109           0 :                 uno::Any aAny(sFormName);
     110           0 :                 xFormProperties->setPropertyValue("Name", aAny);
     111             :             }
     112             : 
     113           0 :             rForm = uno::Reference<form::XForm>(xForm, uno::UNO_QUERY);
     114             : 
     115           0 :             uno::Reference<container::XIndexContainer> xForms(xFormsNamedContainer, uno::UNO_QUERY);
     116           0 :             uno::Any aAny(xForm);
     117           0 :             xForms->insertByIndex(xForms->getCount(), aAny);
     118           0 :         }
     119             :     }
     120             : 
     121           0 :     return rForm;
     122             : }
     123             : 
     124           0 : uno::Reference<container::XIndexContainer> FormControlHelper::FormControlHelper_Impl::getFormComps()
     125             : {
     126           0 :     uno::Reference<container::XIndexContainer> xIndexContainer(getForm(), uno::UNO_QUERY);
     127             : 
     128           0 :     return xIndexContainer;
     129             : }
     130             : 
     131           8 : FormControlHelper::FormControlHelper(FieldId eFieldId,
     132             :                                      uno::Reference<text::XTextDocument> rTextDocument,
     133             :                                      FFDataHandler::Pointer_t pFFData)
     134           8 :     : m_pFFData(pFFData), m_pImpl(new FormControlHelper_Impl)
     135             : {
     136           8 :     m_pImpl->m_eFieldId = eFieldId;
     137           8 :     m_pImpl->rTextDocument = rTextDocument;
     138           8 : }
     139             : 
     140           8 : FormControlHelper::~FormControlHelper()
     141             : {
     142           8 : }
     143             : 
     144           0 : bool FormControlHelper::createCheckbox(uno::Reference<text::XTextRange> xTextRange,
     145             :                                        const OUString & rControlName)
     146             : {
     147           0 :     if ( !m_pFFData )
     148           0 :         return false;
     149             :     uno::Reference<lang::XMultiServiceFactory>
     150           0 :         xServiceFactory(m_pImpl->getServiceFactory());
     151             : 
     152           0 :     if (! xServiceFactory.is())
     153           0 :         return false;
     154             : 
     155           0 :     uno::Reference<uno::XInterface> xInterface = xServiceFactory->createInstance("com.sun.star.form.component.CheckBox");
     156             : 
     157           0 :     if (!xInterface.is())
     158           0 :         return false;
     159             : 
     160           0 :     m_pImpl->rFormComponent = uno::Reference<form::XFormComponent>(xInterface, uno::UNO_QUERY);
     161           0 :     if (!m_pImpl->rFormComponent.is())
     162           0 :         return false;
     163             : 
     164           0 :     uno::Reference<beans::XPropertySet> xPropSet(xInterface, uno::UNO_QUERY);
     165             : 
     166           0 :     sal_uInt32 nCheckBoxHeight = 16 * m_pFFData->getCheckboxHeight();
     167             : 
     168           0 :     if (m_pFFData->getCheckboxAutoHeight())
     169             :     {
     170           0 :         uno::Reference<beans::XPropertySet> xTextRangeProps(xTextRange, uno::UNO_QUERY);
     171             : 
     172             :         try
     173             :         {
     174           0 :             float fCheckBoxHeight = 0.0;
     175           0 :             xTextRangeProps->getPropertyValue("CharHeight") >>= fCheckBoxHeight;
     176           0 :             nCheckBoxHeight = static_cast<sal_uInt32>(floor(fCheckBoxHeight * 35.3));
     177             :         }
     178           0 :         catch (beans::UnknownPropertyException &)
     179             :         {
     180           0 :         }
     181             :     }
     182             : 
     183           0 :     m_pImpl->aSize.Width = nCheckBoxHeight;
     184           0 :     m_pImpl->aSize.Height = m_pImpl->aSize.Width;
     185             : 
     186           0 :     uno::Any aAny;
     187           0 :     if (!m_pFFData->getStatusText().isEmpty())
     188             :     {
     189           0 :         aAny <<= m_pFFData->getStatusText();
     190             : 
     191           0 :         xPropSet->setPropertyValue("HelpText", aAny);
     192             :     }
     193             : 
     194           0 :     aAny <<= m_pFFData->getCheckboxChecked();
     195           0 :     xPropSet->setPropertyValue("DefaultState", aAny);
     196             : 
     197           0 :     if (!m_pFFData->getHelpText().isEmpty())
     198             :     {
     199           0 :         aAny <<= m_pFFData->getHelpText();
     200           0 :         xPropSet->setPropertyValue("HelpF1Text", aAny);
     201             :     }
     202             : 
     203           0 :     aAny <<= rControlName;
     204           0 :     xPropSet->setPropertyValue("Name", aAny);
     205             : 
     206           0 :     return true;
     207             : }
     208             : 
     209           8 : bool FormControlHelper::processField(uno::Reference<text::XFormField> xFormField)
     210             : {
     211           8 :     bool bRes = true;
     212           8 :     uno::Reference<container::XNameContainer> xNameCont = xFormField->getParameters();
     213          16 :     uno::Reference<container::XNamed> xNamed( xFormField, uno::UNO_QUERY );
     214           8 :     if ( m_pFFData && xNamed.is() && xNameCont.is() )
     215             :     {
     216             : 
     217           8 :         if (m_pImpl->m_eFieldId == FIELD_FORMTEXT )
     218             :         {
     219           1 :             xFormField->setFieldType(ODF_FORMTEXT);
     220           1 :             if (  !m_pFFData->getName().isEmpty() )
     221             :             {
     222           0 :                 xNamed->setName( m_pFFData->getName() );
     223             :             }
     224             :         }
     225           7 :         else if (m_pImpl->m_eFieldId == FIELD_FORMCHECKBOX )
     226             :         {
     227           7 :             xFormField->setFieldType(ODF_FORMCHECKBOX);
     228           7 :             uno::Reference<beans::XPropertySet> xPropSet(xFormField, uno::UNO_QUERY);
     229          14 :             uno::Any aAny;
     230           7 :             aAny <<= m_pFFData->getCheckboxChecked();
     231           7 :             if ( xPropSet.is() )
     232          14 :                 xPropSet->setPropertyValue("Checked", aAny);
     233             :         }
     234           0 :         else if (m_pImpl->m_eFieldId == FIELD_FORMDROPDOWN )
     235             :         {
     236           0 :             xFormField->setFieldType(ODF_FORMDROPDOWN);
     237           0 :             uno::Sequence< OUString > sItems;
     238           0 :             sItems.realloc( m_pFFData->getDropDownEntries().size() );
     239           0 :             ::std::copy( m_pFFData->getDropDownEntries().begin(), m_pFFData->getDropDownEntries().end(), sItems.begin());
     240           0 :             if ( sItems.getLength() )
     241             :             {
     242           0 :                 if ( xNameCont->hasByName(ODF_FORMDROPDOWN_LISTENTRY) )
     243           0 :                     xNameCont->replaceByName(ODF_FORMDROPDOWN_LISTENTRY, uno::makeAny( sItems ) );
     244             :                 else
     245           0 :                     xNameCont->insertByName(ODF_FORMDROPDOWN_LISTENTRY, uno::makeAny( sItems ) );
     246             : 
     247           0 :                 sal_Int32 nResult = m_pFFData->getDropDownResult().toInt32();
     248           0 :                 if ( nResult )
     249             :                 {
     250           0 :                     if ( xNameCont->hasByName(ODF_FORMDROPDOWN_RESULT) )
     251           0 :                         xNameCont->replaceByName(ODF_FORMDROPDOWN_RESULT, uno::makeAny( nResult ) );
     252             :                     else
     253           0 :                         xNameCont->insertByName(ODF_FORMDROPDOWN_RESULT, uno::makeAny( nResult ) );
     254             :                 }
     255           0 :             }
     256             :         }
     257             :     }
     258             :     else
     259           0 :         bRes = false;
     260          16 :     return bRes;
     261             : }
     262             : 
     263           0 : bool FormControlHelper::insertControl(uno::Reference<text::XTextRange> xTextRange)
     264             : {
     265           0 :     bool bCreated = false;
     266           0 :     if ( !m_pFFData )
     267           0 :         return false;
     268           0 :     uno::Reference<container::XNameContainer> xFormCompsByName(m_pImpl->getForm(), uno::UNO_QUERY);
     269           0 :     uno::Reference<container::XIndexContainer> xFormComps(m_pImpl->getFormComps());
     270           0 :     if (! xFormComps.is())
     271           0 :         return false;
     272             : 
     273           0 :     static OUString sControl("Control");
     274             : 
     275           0 :     sal_Int32 nControl = 0;
     276           0 :     bool bDone = false;
     277           0 :     OUString sControlName;
     278             : 
     279           0 :     do
     280             :     {
     281           0 :         OUString sTmp(sControl);
     282           0 :         sTmp += OUString::number(nControl);
     283             : 
     284           0 :         nControl++;
     285           0 :         if (! xFormCompsByName->hasByName(sTmp))
     286             :         {
     287           0 :             sControlName = sTmp;
     288           0 :             bDone = true;
     289           0 :         }
     290             :     }
     291           0 :     while (! bDone);
     292             : 
     293           0 :     switch (m_pImpl->m_eFieldId)
     294             :     {
     295             :     case FIELD_FORMCHECKBOX:
     296           0 :         bCreated = createCheckbox(xTextRange, sControlName);
     297           0 :         break;
     298             :     default:
     299           0 :         break;
     300             :     }
     301             : 
     302           0 :     if (!bCreated)
     303           0 :         return false;
     304             : 
     305           0 :     uno::Any aAny(m_pImpl->rFormComponent);
     306           0 :     xFormComps->insertByIndex(xFormComps->getCount(), aAny);
     307             : 
     308           0 :     if (! m_pImpl->getServiceFactory().is())
     309           0 :         return false;
     310             : 
     311           0 :     uno::Reference<uno::XInterface> xInterface = m_pImpl->getServiceFactory()->createInstance("com.sun.star.drawing.ControlShape");
     312             : 
     313           0 :     if (! xInterface.is())
     314           0 :         return false;
     315             : 
     316           0 :     uno::Reference<drawing::XShape> xShape(xInterface, uno::UNO_QUERY);
     317             : 
     318           0 :     if (! xShape.is())
     319           0 :         return false;
     320             : 
     321           0 :     xShape->setSize(m_pImpl->aSize);
     322             : 
     323           0 :     uno::Reference<beans::XPropertySet> xShapeProps(xShape, uno::UNO_QUERY);
     324             : 
     325           0 :     sal_uInt16 nTmp = text::TextContentAnchorType_AS_CHARACTER;
     326           0 :     aAny <<= nTmp;
     327             : 
     328           0 :     xShapeProps->setPropertyValue("AnchorType", aAny);
     329             : 
     330           0 :     nTmp = text::VertOrientation::CENTER;
     331           0 :     aAny <<= nTmp;
     332           0 :     xShapeProps->setPropertyValue("VertOrient", aAny);
     333             : 
     334           0 :     aAny <<= xTextRange;
     335             : 
     336           0 :     xShapeProps->setPropertyValue("TextRange", aAny);
     337             : 
     338           0 :     uno::Reference<drawing::XControlShape> xControlShape(xShape, uno::UNO_QUERY);
     339           0 :     uno::Reference<awt::XControlModel> xControlModel(m_pImpl->rFormComponent, uno::UNO_QUERY);
     340           0 :     xControlShape->setControl(xControlModel);
     341             : 
     342           0 :     m_pImpl->getDrawPage()->add(xShape);
     343             : 
     344           0 :     return true;
     345             : }
     346             : 
     347             : }}
     348             : 
     349             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10