LCOV - code coverage report
Current view: top level - sc/source/ui/unoobj - defltuno.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 135 172 78.5 %
Date: 2014-11-03 Functions: 14 23 60.9 %
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 <editeng/memberids.hrc>
      21             : #include <svl/smplhint.hxx>
      22             : #include <svl/itemprop.hxx>
      23             : #include <svx/unomid.hxx>
      24             : #include <vcl/svapp.hxx>
      25             : #include <i18nlangtag/languagetag.hxx>
      26             : 
      27             : #include <com/sun/star/beans/PropertyAttribute.hpp>
      28             : 
      29             : #include "scitems.hxx"
      30             : #include "defltuno.hxx"
      31             : #include "miscuno.hxx"
      32             : #include "docsh.hxx"
      33             : #include "docpool.hxx"
      34             : #include "unonames.hxx"
      35             : #include "docoptio.hxx"
      36             : 
      37             : #include <limits>
      38             : 
      39             : using namespace ::com::sun::star;
      40             : 
      41         372 : static const SfxItemPropertyMapEntry* lcl_GetDocDefaultsMap()
      42             : {
      43             :     static const SfxItemPropertyMapEntry aDocDefaultsMap_Impl[] =
      44             :     {
      45          50 :         {OUString(SC_UNONAME_CFCHARS),  ATTR_FONT,          cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_CHAR_SET },
      46          50 :         {OUString(SC_UNO_CJK_CFCHARS),  ATTR_CJK_FONT,      cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_CHAR_SET },
      47          50 :         {OUString(SC_UNO_CTL_CFCHARS),  ATTR_CTL_FONT,      cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_CHAR_SET },
      48          50 :         {OUString(SC_UNONAME_CFFAMIL),  ATTR_FONT,          cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_FAMILY },
      49          50 :         {OUString(SC_UNO_CJK_CFFAMIL),  ATTR_CJK_FONT,      cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_FAMILY },
      50          50 :         {OUString(SC_UNO_CTL_CFFAMIL),  ATTR_CTL_FONT,      cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_FAMILY },
      51          50 :         {OUString(SC_UNONAME_CFNAME),   ATTR_FONT,          cppu::UnoType<OUString>::get(),    0, MID_FONT_FAMILY_NAME },
      52          50 :         {OUString(SC_UNO_CJK_CFNAME),   ATTR_CJK_FONT,      cppu::UnoType<OUString>::get(),    0, MID_FONT_FAMILY_NAME },
      53          50 :         {OUString(SC_UNO_CTL_CFNAME),   ATTR_CTL_FONT,      cppu::UnoType<OUString>::get(),    0, MID_FONT_FAMILY_NAME },
      54          50 :         {OUString(SC_UNONAME_CFPITCH),  ATTR_FONT,          cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_PITCH },
      55          50 :         {OUString(SC_UNO_CJK_CFPITCH),  ATTR_CJK_FONT,      cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_PITCH },
      56          50 :         {OUString(SC_UNO_CTL_CFPITCH),  ATTR_CTL_FONT,      cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_PITCH },
      57          50 :         {OUString(SC_UNONAME_CFSTYLE),  ATTR_FONT,          cppu::UnoType<OUString>::get(),    0, MID_FONT_STYLE_NAME },
      58          50 :         {OUString(SC_UNO_CJK_CFSTYLE),  ATTR_CJK_FONT,      cppu::UnoType<OUString>::get(),    0, MID_FONT_STYLE_NAME },
      59          50 :         {OUString(SC_UNO_CTL_CFSTYLE),  ATTR_CTL_FONT,      cppu::UnoType<OUString>::get(),    0, MID_FONT_STYLE_NAME },
      60          50 :         {OUString(SC_UNONAME_CLOCAL),   ATTR_FONT_LANGUAGE, cppu::UnoType<lang::Locale>::get(),     0, MID_LANG_LOCALE },
      61          50 :         {OUString(SC_UNO_CJK_CLOCAL),   ATTR_CJK_FONT_LANGUAGE, cppu::UnoType<lang::Locale>::get(), 0, MID_LANG_LOCALE },
      62          50 :         {OUString(SC_UNO_CTL_CLOCAL),   ATTR_CTL_FONT_LANGUAGE, cppu::UnoType<lang::Locale>::get(), 0, MID_LANG_LOCALE },
      63          50 :         {OUString(SC_UNO_STANDARDDEC),              0,      cppu::UnoType<sal_Int16>::get(),        0, 0 },
      64          50 :         {OUString(SC_UNO_TABSTOPDIS),               0,      cppu::UnoType<sal_Int32>::get(),        0, 0 },
      65             :         { OUString(), 0, css::uno::Type(), 0, 0 }
      66        1422 :     };
      67         372 :     return aDocDefaultsMap_Impl;
      68             : }
      69             : 
      70             : using sc::HMMToTwips;
      71             : using sc::TwipsToHMM;
      72             : using sc::TwipsToEvenHMM;
      73             : 
      74           0 : SC_SIMPLE_SERVICE_INFO( ScDocDefaultsObj, "ScDocDefaultsObj", "com.sun.star.sheet.Defaults" )
      75             : 
      76         372 : ScDocDefaultsObj::ScDocDefaultsObj(ScDocShell* pDocSh) :
      77             :     pDocShell( pDocSh ),
      78         372 :     aPropertyMap(lcl_GetDocDefaultsMap())
      79             : {
      80         372 :     pDocShell->GetDocument().AddUnoObject(*this);
      81         372 : }
      82             : 
      83        1116 : ScDocDefaultsObj::~ScDocDefaultsObj()
      84             : {
      85         372 :     SolarMutexGuard g;
      86             : 
      87         372 :     if (pDocShell)
      88         370 :         pDocShell->GetDocument().RemoveUnoObject(*this);
      89         744 : }
      90             : 
      91         142 : void ScDocDefaultsObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
      92             : {
      93         142 :     const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>(&rHint);
      94         142 :     if ( pSimpleHint && pSimpleHint->GetId() == SFX_HINT_DYING )
      95             :     {
      96           2 :         pDocShell = NULL;       // document gone
      97             :     }
      98         142 : }
      99             : 
     100        3990 : void ScDocDefaultsObj::ItemsChanged()
     101             : {
     102        3990 :     if (pDocShell)
     103             :     {
     104             :         //! if not in XML import, adjust row heights
     105             : 
     106        3990 :         pDocShell->PostPaint(ScRange(0, 0, 0, MAXCOL, MAXROW, MAXTAB), PAINT_GRID);
     107             :     }
     108        3990 : }
     109             : 
     110             : // XPropertySet
     111             : 
     112         418 : uno::Reference<beans::XPropertySetInfo> SAL_CALL ScDocDefaultsObj::getPropertySetInfo()
     113             :                                                         throw(uno::RuntimeException, std::exception)
     114             : {
     115         418 :     SolarMutexGuard aGuard;
     116             :     static uno::Reference<beans::XPropertySetInfo> aRef = new SfxItemPropertySetInfo(
     117         418 :                                                                         aPropertyMap );
     118         418 :     return aRef;
     119             : }
     120             : 
     121        5076 : void SAL_CALL ScDocDefaultsObj::setPropertyValue(
     122             :                         const OUString& aPropertyName, const uno::Any& aValue )
     123             :                 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
     124             :                         lang::IllegalArgumentException, lang::WrappedTargetException,
     125             :                         uno::RuntimeException, std::exception)
     126             : {
     127        5076 :     SolarMutexGuard aGuard;
     128             : 
     129        5076 :     if ( !pDocShell )
     130           0 :         throw uno::RuntimeException();
     131             : 
     132        5076 :     const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
     133        5076 :     if ( !pEntry )
     134           0 :         throw beans::UnknownPropertyException();
     135        5076 :     if(!pEntry->nWID)
     136             :     {
     137         288 :         if(aPropertyName.equalsAscii(SC_UNO_STANDARDDEC) )
     138             :         {
     139          22 :             ScDocument& rDoc = pDocShell->GetDocument();
     140          22 :             ScDocOptions aDocOpt(rDoc.GetDocOptions());
     141          22 :             sal_Int16 nValue = 0;
     142          22 :             if (aValue >>= nValue)
     143             :             {
     144          22 :                 aDocOpt.SetStdPrecision(static_cast<sal_uInt16> (nValue));
     145          22 :                 rDoc.SetDocOptions(aDocOpt);
     146          22 :             }
     147             :         }
     148         266 :         else if (aPropertyName.equalsAscii(SC_UNO_TABSTOPDIS) )
     149             :         {
     150         266 :             ScDocument& rDoc = pDocShell->GetDocument();
     151         266 :             ScDocOptions aDocOpt(rDoc.GetDocOptions());
     152         266 :             sal_Int32 nValue = 0;
     153         266 :             if (aValue >>= nValue)
     154             :             {
     155         266 :                 aDocOpt.SetTabDistance(static_cast<sal_uInt16>(HMMToTwips(nValue)));
     156         266 :                 rDoc.SetDocOptions(aDocOpt);
     157         266 :             }
     158             :         }
     159             :     }
     160        9310 :     else if ( pEntry->nWID == ATTR_FONT_LANGUAGE ||
     161        8778 :               pEntry->nWID == ATTR_CJK_FONT_LANGUAGE ||
     162        4256 :               pEntry->nWID == ATTR_CTL_FONT_LANGUAGE )
     163             :     {
     164             :         //  for getPropertyValue the PoolDefaults are sufficient,
     165             :         //  but setPropertyValue has to be handled differently
     166             : 
     167         798 :         lang::Locale aLocale;
     168         798 :         if ( aValue >>= aLocale )
     169             :         {
     170             :             LanguageType eNew;
     171         798 :             if (!aLocale.Language.isEmpty() || !aLocale.Country.isEmpty())
     172         778 :                 eNew = LanguageTag::convertToLanguageType( aLocale, false);
     173             :             else
     174          20 :                 eNew = LANGUAGE_NONE;
     175             : 
     176         798 :             ScDocument& rDoc = pDocShell->GetDocument();
     177             :             LanguageType eLatin, eCjk, eCtl;
     178         798 :             rDoc.GetLanguage( eLatin, eCjk, eCtl );
     179             : 
     180         798 :             if ( pEntry->nWID == ATTR_CJK_FONT_LANGUAGE )
     181         266 :                 eCjk = eNew;
     182         532 :             else if ( pEntry->nWID == ATTR_CTL_FONT_LANGUAGE )
     183         266 :                 eCtl = eNew;
     184             :             else
     185         266 :                 eLatin = eNew;
     186             : 
     187         798 :             rDoc.SetLanguage( eLatin, eCjk, eCtl );
     188         798 :         }
     189             :     }
     190             :     else
     191             :     {
     192        3990 :         ScDocumentPool* pPool = pDocShell->GetDocument().GetPool();
     193        3990 :         SfxPoolItem* pNewItem = pPool->GetDefaultItem(pEntry->nWID).Clone();
     194             : 
     195        3990 :         if( !pNewItem->PutValue( aValue, pEntry->nMemberId ) )
     196           0 :             throw lang::IllegalArgumentException();
     197             : 
     198        3990 :         pPool->SetPoolDefaultItem( *pNewItem );
     199        3990 :         delete pNewItem;    // copied in SetPoolDefaultItem
     200             : 
     201        3990 :         ItemsChanged();
     202        5076 :     }
     203        5076 : }
     204             : 
     205         960 : uno::Any SAL_CALL ScDocDefaultsObj::getPropertyValue( const OUString& aPropertyName )
     206             :                 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
     207             :                         uno::RuntimeException, std::exception)
     208             : {
     209             :     //  use pool default if set
     210             : 
     211         960 :     SolarMutexGuard aGuard;
     212             : 
     213         960 :     if ( !pDocShell )
     214           0 :         throw uno::RuntimeException();
     215             : 
     216         960 :     uno::Any aRet;
     217         960 :     const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
     218         960 :     if ( !pEntry )
     219           0 :         throw beans::UnknownPropertyException();
     220             : 
     221         960 :     if (!pEntry->nWID)
     222             :     {
     223          96 :         if(aPropertyName.equalsAscii(SC_UNO_STANDARDDEC) )
     224             :         {
     225          48 :             ScDocument& rDoc = pDocShell->GetDocument();
     226          48 :             const ScDocOptions& aDocOpt = rDoc.GetDocOptions();
     227          48 :             sal_uInt16 nPrec = aDocOpt.GetStdPrecision();
     228             :             // the max value of unsigned 16-bit integer is used as the flag
     229             :             // value for unlimited precision, c.f.
     230             :             // SvNumberFormatter::UNLIMITED_PRECISION.
     231          48 :             if (nPrec <= ::std::numeric_limits<sal_Int16>::max())
     232           0 :                 aRet <<= static_cast<sal_Int16> (nPrec);
     233             :         }
     234          48 :         else if (aPropertyName.equalsAscii(SC_UNO_TABSTOPDIS) )
     235             :         {
     236          48 :             ScDocument& rDoc = pDocShell->GetDocument();
     237          48 :             const ScDocOptions& aDocOpt = rDoc.GetDocOptions();
     238          48 :             sal_Int32 nValue (TwipsToEvenHMM(aDocOpt.GetTabDistance()));
     239          48 :             aRet <<= nValue;
     240             :         }
     241             :     }
     242             :     else
     243             :     {
     244         864 :         ScDocumentPool* pPool = pDocShell->GetDocument().GetPool();
     245         864 :         const SfxPoolItem& rItem = pPool->GetDefaultItem( pEntry->nWID );
     246         864 :         rItem.QueryValue( aRet, pEntry->nMemberId );
     247             :     }
     248         960 :     return aRet;
     249             : }
     250             : 
     251           0 : SC_IMPL_DUMMY_PROPERTY_LISTENER( ScDocDefaultsObj )
     252             : 
     253             : // XPropertyState
     254             : 
     255         960 : beans::PropertyState SAL_CALL ScDocDefaultsObj::getPropertyState( const OUString& aPropertyName )
     256             :                                 throw(beans::UnknownPropertyException, uno::RuntimeException, std::exception)
     257             : {
     258         960 :     SolarMutexGuard aGuard;
     259             : 
     260         960 :     if ( !pDocShell )
     261           0 :         throw uno::RuntimeException();
     262             : 
     263         960 :     const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
     264         960 :     if ( !pEntry )
     265           0 :         throw beans::UnknownPropertyException();
     266             : 
     267         960 :     beans::PropertyState eRet = beans::PropertyState_DEFAULT_VALUE;
     268             : 
     269         960 :     sal_uInt16 nWID = pEntry->nWID;
     270         960 :     if ( nWID == ATTR_FONT || nWID == ATTR_CJK_FONT || nWID == ATTR_CTL_FONT || !nWID )
     271             :     {
     272             :         //  static default for font is system-dependent,
     273             :         //  so font default is always treated as "direct value".
     274             : 
     275         816 :         eRet = beans::PropertyState_DIRECT_VALUE;
     276             :     }
     277             :     else
     278             :     {
     279             :         //  check if pool default is set
     280             : 
     281         144 :         ScDocumentPool* pPool = pDocShell->GetDocument().GetPool();
     282         144 :         if ( pPool->GetPoolDefaultItem( nWID ) != NULL )
     283         144 :             eRet = beans::PropertyState_DIRECT_VALUE;
     284             :     }
     285             : 
     286         960 :     return eRet;
     287             : }
     288             : 
     289          48 : uno::Sequence<beans::PropertyState> SAL_CALL ScDocDefaultsObj::getPropertyStates(
     290             :                             const uno::Sequence<OUString>& aPropertyNames )
     291             :                     throw(beans::UnknownPropertyException, uno::RuntimeException, std::exception)
     292             : {
     293             :     //  the simple way: call getPropertyState
     294             : 
     295          48 :     SolarMutexGuard aGuard;
     296          48 :     const OUString* pNames = aPropertyNames.getConstArray();
     297          48 :     uno::Sequence<beans::PropertyState> aRet(aPropertyNames.getLength());
     298          48 :     beans::PropertyState* pStates = aRet.getArray();
     299        1008 :     for(sal_Int32 i = 0; i < aPropertyNames.getLength(); i++)
     300         960 :         pStates[i] = getPropertyState(pNames[i]);
     301          48 :     return aRet;
     302             : }
     303             : 
     304           0 : void SAL_CALL ScDocDefaultsObj::setPropertyToDefault( const OUString& aPropertyName )
     305             :                             throw(beans::UnknownPropertyException, uno::RuntimeException, std::exception)
     306             : {
     307           0 :     SolarMutexGuard aGuard;
     308             : 
     309           0 :     if ( !pDocShell )
     310           0 :         throw uno::RuntimeException();
     311             : 
     312           0 :     const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
     313           0 :     if ( !pEntry )
     314           0 :         throw beans::UnknownPropertyException();
     315             : 
     316           0 :     if (pEntry->nWID)
     317             :     {
     318           0 :         ScDocumentPool* pPool = pDocShell->GetDocument().GetPool();
     319           0 :         pPool->ResetPoolDefaultItem( pEntry->nWID );
     320             : 
     321           0 :         ItemsChanged();
     322           0 :     }
     323           0 : }
     324             : 
     325           0 : uno::Any SAL_CALL ScDocDefaultsObj::getPropertyDefault( const OUString& aPropertyName )
     326             :                             throw(beans::UnknownPropertyException, lang::WrappedTargetException,
     327             :                                     uno::RuntimeException, std::exception)
     328             : {
     329             :     //  always use static default
     330             : 
     331           0 :     SolarMutexGuard aGuard;
     332             : 
     333           0 :     if ( !pDocShell )
     334           0 :         throw uno::RuntimeException();
     335             : 
     336           0 :     const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
     337           0 :     if ( !pEntry )
     338           0 :         throw beans::UnknownPropertyException();
     339             : 
     340           0 :     uno::Any aRet;
     341           0 :     if (pEntry->nWID)
     342             :     {
     343           0 :         ScDocumentPool* pPool = pDocShell->GetDocument().GetPool();
     344           0 :         const SfxPoolItem* pItem = pPool->GetItem2Default( pEntry->nWID );
     345           0 :         if (pItem)
     346           0 :             pItem->QueryValue( aRet, pEntry->nMemberId );
     347             :     }
     348           0 :     return aRet;
     349         228 : }
     350             : 
     351             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10