LCOV - code coverage report
Current view: top level - writerfilter/source/dmapper - SdtHelper.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 121 0.0 %
Date: 2014-04-14 Functions: 0 20 0.0 %
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             : 
      10             : #include <com/sun/star/awt/Size.hpp>
      11             : #include <com/sun/star/awt/XControlModel.hpp>
      12             : #include <com/sun/star/drawing/XControlShape.hpp>
      13             : #include <com/sun/star/text/VertOrientation.hpp>
      14             : 
      15             : #include <editeng/unoprnms.hxx>
      16             : #include <vcl/outdev.hxx>
      17             : #include <vcl/svapp.hxx>
      18             : #include <unotools/datetime.hxx>
      19             : 
      20             : #include <DomainMapper_Impl.hxx>
      21             : #include <StyleSheetTable.hxx>
      22             : #include <SdtHelper.hxx>
      23             : 
      24             : namespace writerfilter
      25             : {
      26             : namespace dmapper
      27             : {
      28             : 
      29             : using namespace ::com::sun::star;
      30             : 
      31             : /// w:sdt's w:dropDownList doesn't have width, so guess the size based on the longest string.
      32           0 : awt::Size lcl_getOptimalWidth(StyleSheetTablePtr pStyleSheet, OUString& rDefault, std::vector<OUString>& rItems)
      33             : {
      34           0 :     OUString aLongest = rDefault;
      35           0 :     sal_Int32 nHeight = 0;
      36           0 :     for (size_t i = 0; i < rItems.size(); ++i)
      37           0 :         if (rItems[i].getLength() > aLongest.getLength())
      38           0 :             aLongest = rItems[i];
      39             : 
      40           0 :     MapMode aMap(MAP_100TH_MM);
      41           0 :     OutputDevice* pOut = Application::GetDefaultDevice();
      42           0 :     pOut->Push(PUSH_FONT | PUSH_MAPMODE);
      43             : 
      44           0 :     PropertyMapPtr pDefaultCharProps = pStyleSheet->GetDefaultCharProps();
      45           0 :     Font aFont(pOut->GetFont());
      46           0 :     PropertyMap::iterator aFontName = pDefaultCharProps->find(PROP_CHAR_FONT_NAME);
      47           0 :     if (aFontName != pDefaultCharProps->end())
      48           0 :         aFont.SetName(aFontName->second.getValue().get<OUString>());
      49           0 :     PropertyMap::iterator aHeight = pDefaultCharProps->find(PROP_CHAR_HEIGHT);
      50           0 :     if (aHeight != pDefaultCharProps->end())
      51             :     {
      52           0 :         nHeight = aHeight->second.getValue().get<double>() * 35; // points -> mm100
      53           0 :         aFont.SetSize(Size(0, nHeight));
      54             :     }
      55           0 :     pOut->SetFont(aFont);
      56           0 :     pOut->SetMapMode(aMap);
      57           0 :     sal_Int32 nWidth = pOut->GetTextWidth(aLongest);
      58             : 
      59           0 :     pOut->Pop();
      60             : 
      61             :     // Border: see PDFWriterImpl::drawFieldBorder(), border size is font height / 4,
      62             :     // so additional width / height needed is height / 2.
      63           0 :     sal_Int32 nBorder = nHeight / 2;
      64             : 
      65             :     // Width: space for the text + the square having the dropdown arrow.
      66           0 :     return awt::Size(nWidth + nBorder + nHeight, nHeight + nBorder);
      67             : }
      68             : 
      69           0 : SdtHelper::SdtHelper(DomainMapper_Impl& rDM_Impl)
      70             :     : m_rDM_Impl(rDM_Impl)
      71           0 :     , m_bHasElements(false)
      72             : {
      73           0 : }
      74             : 
      75           0 : SdtHelper::~SdtHelper()
      76             : {
      77           0 : }
      78             : 
      79           0 : void SdtHelper::createDropDownControl()
      80             : {
      81           0 :     OUString aDefaultText = m_aSdtTexts.makeStringAndClear();
      82           0 :     uno::Reference<awt::XControlModel> xControlModel(m_rDM_Impl.GetTextFactory()->createInstance("com.sun.star.form.component.ComboBox"), uno::UNO_QUERY);
      83           0 :     uno::Reference<beans::XPropertySet> xPropertySet(xControlModel, uno::UNO_QUERY);
      84           0 :     xPropertySet->setPropertyValue("DefaultText", uno::makeAny(aDefaultText));
      85           0 :     xPropertySet->setPropertyValue("Dropdown", uno::makeAny(sal_True));
      86           0 :     uno::Sequence<OUString> aItems(m_aDropDownItems.size());
      87           0 :     for (size_t i = 0; i < m_aDropDownItems.size(); ++i)
      88           0 :         aItems[i] = m_aDropDownItems[i];
      89           0 :     xPropertySet->setPropertyValue("StringItemList", uno::makeAny(aItems));
      90             : 
      91           0 :     createControlShape(lcl_getOptimalWidth(m_rDM_Impl.GetStyleSheetTable(), aDefaultText, m_aDropDownItems), xControlModel);
      92           0 :     m_aDropDownItems.clear();
      93           0 : }
      94             : 
      95           0 : void SdtHelper::createDateControl(OUString& rContentText)
      96             : {
      97           0 :     uno::Reference<awt::XControlModel> xControlModel(m_rDM_Impl.GetTextFactory()->createInstance("com.sun.star.form.component.DateField"), uno::UNO_QUERY);
      98           0 :     uno::Reference<beans::XPropertySet> xPropertySet(xControlModel, uno::UNO_QUERY);
      99             : 
     100           0 :     xPropertySet->setPropertyValue("Dropdown", uno::makeAny(sal_True));
     101             : 
     102             :     // See com/sun/star/awt/UnoControlDateFieldModel.idl, DateFormat; sadly there are no constants
     103           0 :     sal_Int16 nDateFormat = 0;
     104           0 :     OUString sDateFormat = m_sDateFormat.makeStringAndClear();
     105           0 :     if (sDateFormat == "M/d/yyyy" || sDateFormat == "M.d.yyyy")
     106             :         // Approximate with MM.dd.yyy
     107           0 :         nDateFormat = 8;
     108             :     else
     109             :         // Set default format, so at least the date picker is created.
     110             :         SAL_WARN("writerfilter", "unhandled w:dateFormat value");
     111           0 :     xPropertySet->setPropertyValue("DateFormat", uno::makeAny(nDateFormat));
     112             : 
     113           0 :     util::Date aDate;
     114           0 :     util::DateTime aDateTime;
     115           0 :     if (utl::ISO8601parseDateTime(m_sDate.makeStringAndClear(), aDateTime))
     116             :     {
     117           0 :         utl::extractDate(aDateTime, aDate);
     118           0 :         xPropertySet->setPropertyValue("Date", uno::makeAny(aDate));
     119           0 :         xPropertySet->setPropertyValue("HelpText", uno::makeAny(OUString("Click here to enter a date")));
     120             :     }
     121             :     else
     122           0 :         xPropertySet->setPropertyValue("HelpText", uno::makeAny(rContentText));
     123             : 
     124             :     // append date format to grab bag
     125           0 :     uno::Sequence<beans::PropertyValue> aGrabBag(4);
     126           0 :     aGrabBag[0].Name = "OriginalDate";
     127           0 :     aGrabBag[0].Value = uno::makeAny(aDate);
     128           0 :     aGrabBag[1].Name = "OriginalContent";
     129           0 :     aGrabBag[1].Value = uno::makeAny(rContentText);
     130           0 :     aGrabBag[2].Name = "DateFormat";
     131           0 :     aGrabBag[2].Value = uno::makeAny(sDateFormat);
     132           0 :     aGrabBag[3].Name = "Locale";
     133           0 :     aGrabBag[3].Value = uno::makeAny(m_sLocale.makeStringAndClear());
     134             : 
     135           0 :     std::vector<OUString> aItems;
     136           0 :     createControlShape(lcl_getOptimalWidth(m_rDM_Impl.GetStyleSheetTable(), rContentText, aItems), xControlModel, aGrabBag);
     137           0 : }
     138             : 
     139           0 : void SdtHelper::createControlShape(awt::Size aSize, uno::Reference<awt::XControlModel> xControlModel)
     140             : {
     141           0 :     createControlShape(aSize, xControlModel, uno::Sequence<beans::PropertyValue>());
     142           0 : }
     143             : 
     144           0 : void SdtHelper::createControlShape(awt::Size aSize, uno::Reference<awt::XControlModel> xControlModel, uno::Sequence<beans::PropertyValue> rGrabBag)
     145             : {
     146           0 :     uno::Reference<drawing::XControlShape> xControlShape(m_rDM_Impl.GetTextFactory()->createInstance("com.sun.star.drawing.ControlShape"), uno::UNO_QUERY);
     147           0 :     xControlShape->setSize(aSize);
     148           0 :     xControlShape->setControl(xControlModel);
     149             : 
     150           0 :     uno::Reference<beans::XPropertySet> xPropertySet(xControlShape, uno::UNO_QUERY);
     151           0 :     xPropertySet->setPropertyValue("VertOrient", uno::makeAny(text::VertOrientation::CENTER));
     152             : 
     153           0 :     if (rGrabBag.hasElements())
     154           0 :         xPropertySet->setPropertyValue(UNO_NAME_MISC_OBJ_INTEROPGRABBAG, uno::makeAny(rGrabBag));
     155             : 
     156           0 :     uno::Reference<text::XTextContent> xTextContent(xControlShape, uno::UNO_QUERY);
     157           0 :     m_rDM_Impl.appendTextContent(xTextContent, uno::Sequence< beans::PropertyValue >());
     158           0 :     m_bHasElements = true;
     159           0 : }
     160             : 
     161           0 : std::vector<OUString>& SdtHelper::getDropDownItems()
     162             : {
     163           0 :     return m_aDropDownItems;
     164             : }
     165             : 
     166           0 : OUStringBuffer& SdtHelper::getSdtTexts()
     167             : {
     168           0 :     return m_aSdtTexts;
     169             : }
     170             : 
     171           0 : OUStringBuffer& SdtHelper::getDate()
     172             : {
     173           0 :     return m_sDate;
     174             : }
     175             : 
     176           0 : OUStringBuffer& SdtHelper::getDateFormat()
     177             : {
     178           0 :     return m_sDateFormat;
     179             : }
     180             : 
     181           0 : OUStringBuffer& SdtHelper::getLocale()
     182             : {
     183           0 :     return m_sLocale;
     184             : }
     185             : 
     186           0 : bool SdtHelper::hasElements()
     187             : {
     188           0 :     return m_bHasElements;
     189             : }
     190             : 
     191           0 : void SdtHelper::appendToInteropGrabBag(const OUString& rName, const css::uno::Any& rValue)
     192             : {
     193           0 :     sal_Int32 nLength = m_aGrabBag.getLength();
     194           0 :     m_aGrabBag.realloc(nLength + 1);
     195           0 :     m_aGrabBag[nLength].Name = rName;
     196           0 :     m_aGrabBag[nLength].Value = rValue;
     197           0 : }
     198             : 
     199           0 : void SdtHelper::appendToInteropGrabBag(com::sun::star::beans::PropertyValue rValue)
     200             : {
     201           0 :     sal_Int32 nLength = m_aGrabBag.getLength();
     202           0 :     m_aGrabBag.realloc(nLength + 1);
     203           0 :     m_aGrabBag[nLength] = rValue;
     204           0 : }
     205             : 
     206           0 : com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue> SdtHelper::getInteropGrabBagAndClear()
     207             : {
     208           0 :     com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue> aRet = m_aGrabBag;
     209           0 :     m_aGrabBag.realloc(0);
     210           0 :     return aRet;
     211             : }
     212             : 
     213           0 : bool SdtHelper::isInteropGrabBagEmpty()
     214             : {
     215           0 :     return m_aGrabBag.getLength() == 0;
     216             : }
     217             : 
     218           0 : sal_Int32 SdtHelper::getInteropGrabBagSize()
     219             : {
     220           0 :     return m_aGrabBag.getLength();
     221             : }
     222             : 
     223           0 : bool SdtHelper::containedInInteropGrabBag(const OUString& rValueName)
     224             : {
     225           0 :     for (sal_Int32 i=0; i < m_aGrabBag.getLength(); ++i)
     226           0 :         if (m_aGrabBag[i].Name == rValueName)
     227           0 :             return true;
     228             : 
     229           0 :     return false;
     230             : }
     231             : 
     232             : } // namespace dmapper
     233             : } // namespace writerfilter
     234             : 
     235             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10