LCOV - code coverage report
Current view: top level - sc/source/ui/unoobj - defltuno.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 134 172 77.9 %
Date: 2015-06-13 12:38:46 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         227 : static const SfxItemPropertyMapEntry* lcl_GetDocDefaultsMap()
      42             : {
      43             :     static const SfxItemPropertyMapEntry aDocDefaultsMap_Impl[] =
      44             :     {
      45          34 :         {OUString(SC_UNONAME_CFCHARS),  ATTR_FONT,          cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_CHAR_SET },
      46          34 :         {OUString(SC_UNO_CJK_CFCHARS),  ATTR_CJK_FONT,      cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_CHAR_SET },
      47          34 :         {OUString(SC_UNO_CTL_CFCHARS),  ATTR_CTL_FONT,      cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_CHAR_SET },
      48          34 :         {OUString(SC_UNONAME_CFFAMIL),  ATTR_FONT,          cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_FAMILY },
      49          34 :         {OUString(SC_UNO_CJK_CFFAMIL),  ATTR_CJK_FONT,      cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_FAMILY },
      50          34 :         {OUString(SC_UNO_CTL_CFFAMIL),  ATTR_CTL_FONT,      cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_FAMILY },
      51          34 :         {OUString(SC_UNONAME_CFNAME),   ATTR_FONT,          cppu::UnoType<OUString>::get(),    0, MID_FONT_FAMILY_NAME },
      52          34 :         {OUString(SC_UNO_CJK_CFNAME),   ATTR_CJK_FONT,      cppu::UnoType<OUString>::get(),    0, MID_FONT_FAMILY_NAME },
      53          34 :         {OUString(SC_UNO_CTL_CFNAME),   ATTR_CTL_FONT,      cppu::UnoType<OUString>::get(),    0, MID_FONT_FAMILY_NAME },
      54          34 :         {OUString(SC_UNONAME_CFPITCH),  ATTR_FONT,          cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_PITCH },
      55          34 :         {OUString(SC_UNO_CJK_CFPITCH),  ATTR_CJK_FONT,      cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_PITCH },
      56          34 :         {OUString(SC_UNO_CTL_CFPITCH),  ATTR_CTL_FONT,      cppu::UnoType<sal_Int16>::get(),        0, MID_FONT_PITCH },
      57          34 :         {OUString(SC_UNONAME_CFSTYLE),  ATTR_FONT,          cppu::UnoType<OUString>::get(),    0, MID_FONT_STYLE_NAME },
      58          34 :         {OUString(SC_UNO_CJK_CFSTYLE),  ATTR_CJK_FONT,      cppu::UnoType<OUString>::get(),    0, MID_FONT_STYLE_NAME },
      59          34 :         {OUString(SC_UNO_CTL_CFSTYLE),  ATTR_CTL_FONT,      cppu::UnoType<OUString>::get(),    0, MID_FONT_STYLE_NAME },
      60          34 :         {OUString(SC_UNONAME_CLOCAL),   ATTR_FONT_LANGUAGE, cppu::UnoType<lang::Locale>::get(),     0, MID_LANG_LOCALE },
      61          34 :         {OUString(SC_UNO_CJK_CLOCAL),   ATTR_CJK_FONT_LANGUAGE, cppu::UnoType<lang::Locale>::get(), 0, MID_LANG_LOCALE },
      62          34 :         {OUString(SC_UNO_CTL_CLOCAL),   ATTR_CTL_FONT_LANGUAGE, cppu::UnoType<lang::Locale>::get(), 0, MID_LANG_LOCALE },
      63          34 :         {OUString(SC_UNO_STANDARDDEC),              0,      cppu::UnoType<sal_Int16>::get(),        0, 0 },
      64          34 :         {OUString(SC_UNO_TABSTOPDIS),               0,      cppu::UnoType<sal_Int32>::get(),        0, 0 },
      65             :         { OUString(), 0, css::uno::Type(), 0, 0 }
      66         941 :     };
      67         227 :     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         227 : ScDocDefaultsObj::ScDocDefaultsObj(ScDocShell* pDocSh) :
      77             :     pDocShell( pDocSh ),
      78         227 :     aPropertyMap(lcl_GetDocDefaultsMap())
      79             : {
      80         227 :     pDocShell->GetDocument().AddUnoObject(*this);
      81         227 : }
      82             : 
      83         681 : ScDocDefaultsObj::~ScDocDefaultsObj()
      84             : {
      85         227 :     SolarMutexGuard g;
      86             : 
      87         227 :     if (pDocShell)
      88         227 :         pDocShell->GetDocument().RemoveUnoObject(*this);
      89         454 : }
      90             : 
      91           1 : void ScDocDefaultsObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
      92             : {
      93           1 :     const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>(&rHint);
      94           1 :     if ( pSimpleHint && pSimpleHint->GetId() == SFX_HINT_DYING )
      95             :     {
      96           0 :         pDocShell = NULL;       // document gone
      97             :     }
      98           1 : }
      99             : 
     100        2520 : void ScDocDefaultsObj::ItemsChanged()
     101             : {
     102        2520 :     if (pDocShell)
     103             :     {
     104             :         //! if not in XML import, adjust row heights
     105             : 
     106        2520 :         pDocShell->PostPaint(ScRange(0, 0, 0, MAXCOL, MAXROW, MAXTAB), PAINT_GRID);
     107             :     }
     108        2520 : }
     109             : 
     110             : // XPropertySet
     111             : 
     112         255 : uno::Reference<beans::XPropertySetInfo> SAL_CALL ScDocDefaultsObj::getPropertySetInfo()
     113             :                                                         throw(uno::RuntimeException, std::exception)
     114             : {
     115         255 :     SolarMutexGuard aGuard;
     116             :     static uno::Reference<beans::XPropertySetInfo> aRef = new SfxItemPropertySetInfo(
     117         255 :                                                                         aPropertyMap );
     118         255 :     return aRef;
     119             : }
     120             : 
     121        3203 : 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        3203 :     SolarMutexGuard aGuard;
     128             : 
     129        3203 :     if ( !pDocShell )
     130           0 :         throw uno::RuntimeException();
     131             : 
     132        3203 :     const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
     133        3203 :     if ( !pEntry )
     134           0 :         throw beans::UnknownPropertyException();
     135        3203 :     if(!pEntry->nWID)
     136             :     {
     137         179 :         if(aPropertyName ==SC_UNO_STANDARDDEC)
     138             :         {
     139          11 :             ScDocument& rDoc = pDocShell->GetDocument();
     140          11 :             ScDocOptions aDocOpt(rDoc.GetDocOptions());
     141          11 :             sal_Int16 nValue = 0;
     142          11 :             if (aValue >>= nValue)
     143             :             {
     144          11 :                 aDocOpt.SetStdPrecision(static_cast<sal_uInt16> (nValue));
     145          11 :                 rDoc.SetDocOptions(aDocOpt);
     146          11 :             }
     147             :         }
     148         168 :         else if (aPropertyName == SC_UNO_TABSTOPDIS)
     149             :         {
     150         168 :             ScDocument& rDoc = pDocShell->GetDocument();
     151         168 :             ScDocOptions aDocOpt(rDoc.GetDocOptions());
     152         168 :             sal_Int32 nValue = 0;
     153         168 :             if (aValue >>= nValue)
     154             :             {
     155         168 :                 aDocOpt.SetTabDistance(static_cast<sal_uInt16>(HMMToTwips(nValue)));
     156         168 :                 rDoc.SetDocOptions(aDocOpt);
     157         168 :             }
     158             :         }
     159             :     }
     160        5880 :     else if ( pEntry->nWID == ATTR_FONT_LANGUAGE ||
     161        5544 :               pEntry->nWID == ATTR_CJK_FONT_LANGUAGE ||
     162        2688 :               pEntry->nWID == ATTR_CTL_FONT_LANGUAGE )
     163             :     {
     164             :         //  for getPropertyValue the PoolDefaults are sufficient,
     165             :         //  but setPropertyValue has to be handled differently
     166             : 
     167         504 :         lang::Locale aLocale;
     168         504 :         if ( aValue >>= aLocale )
     169             :         {
     170             :             LanguageType eNew;
     171         504 :             if (!aLocale.Language.isEmpty() || !aLocale.Country.isEmpty())
     172         494 :                 eNew = LanguageTag::convertToLanguageType( aLocale, false);
     173             :             else
     174          10 :                 eNew = LANGUAGE_NONE;
     175             : 
     176         504 :             ScDocument& rDoc = pDocShell->GetDocument();
     177             :             LanguageType eLatin, eCjk, eCtl;
     178         504 :             rDoc.GetLanguage( eLatin, eCjk, eCtl );
     179             : 
     180         504 :             if ( pEntry->nWID == ATTR_CJK_FONT_LANGUAGE )
     181         168 :                 eCjk = eNew;
     182         336 :             else if ( pEntry->nWID == ATTR_CTL_FONT_LANGUAGE )
     183         168 :                 eCtl = eNew;
     184             :             else
     185         168 :                 eLatin = eNew;
     186             : 
     187         504 :             rDoc.SetLanguage( eLatin, eCjk, eCtl );
     188         504 :         }
     189             :     }
     190             :     else
     191             :     {
     192        2520 :         ScDocumentPool* pPool = pDocShell->GetDocument().GetPool();
     193        2520 :         SfxPoolItem* pNewItem = pPool->GetDefaultItem(pEntry->nWID).Clone();
     194             : 
     195        2520 :         if( !pNewItem->PutValue( aValue, pEntry->nMemberId ) )
     196           0 :             throw lang::IllegalArgumentException();
     197             : 
     198        2520 :         pPool->SetPoolDefaultItem( *pNewItem );
     199        2520 :         delete pNewItem;    // copied in SetPoolDefaultItem
     200             : 
     201        2520 :         ItemsChanged();
     202        3203 :     }
     203        3203 : }
     204             : 
     205         580 : 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         580 :     SolarMutexGuard aGuard;
     212             : 
     213         580 :     if ( !pDocShell )
     214           0 :         throw uno::RuntimeException();
     215             : 
     216         580 :     uno::Any aRet;
     217         580 :     const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
     218         580 :     if ( !pEntry )
     219           0 :         throw beans::UnknownPropertyException();
     220             : 
     221         580 :     if (!pEntry->nWID)
     222             :     {
     223          58 :         if(aPropertyName == SC_UNO_STANDARDDEC)
     224             :         {
     225          29 :             ScDocument& rDoc = pDocShell->GetDocument();
     226          29 :             const ScDocOptions& aDocOpt = rDoc.GetDocOptions();
     227          29 :             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          29 :             if (nPrec <= ::std::numeric_limits<sal_Int16>::max())
     232           0 :                 aRet <<= static_cast<sal_Int16> (nPrec);
     233             :         }
     234          29 :         else if (aPropertyName == SC_UNO_TABSTOPDIS)
     235             :         {
     236          29 :             ScDocument& rDoc = pDocShell->GetDocument();
     237          29 :             const ScDocOptions& aDocOpt = rDoc.GetDocOptions();
     238          29 :             sal_Int32 nValue (TwipsToEvenHMM(aDocOpt.GetTabDistance()));
     239          29 :             aRet <<= nValue;
     240             :         }
     241             :     }
     242             :     else
     243             :     {
     244         522 :         ScDocumentPool* pPool = pDocShell->GetDocument().GetPool();
     245         522 :         const SfxPoolItem& rItem = pPool->GetDefaultItem( pEntry->nWID );
     246         522 :         rItem.QueryValue( aRet, pEntry->nMemberId );
     247             :     }
     248         580 :     return aRet;
     249             : }
     250             : 
     251           0 : SC_IMPL_DUMMY_PROPERTY_LISTENER( ScDocDefaultsObj )
     252             : 
     253             : // XPropertyState
     254             : 
     255         580 : beans::PropertyState SAL_CALL ScDocDefaultsObj::getPropertyState( const OUString& aPropertyName )
     256             :                                 throw(beans::UnknownPropertyException, uno::RuntimeException, std::exception)
     257             : {
     258         580 :     SolarMutexGuard aGuard;
     259             : 
     260         580 :     if ( !pDocShell )
     261           0 :         throw uno::RuntimeException();
     262             : 
     263         580 :     const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
     264         580 :     if ( !pEntry )
     265           0 :         throw beans::UnknownPropertyException();
     266             : 
     267         580 :     beans::PropertyState eRet = beans::PropertyState_DEFAULT_VALUE;
     268             : 
     269         580 :     sal_uInt16 nWID = pEntry->nWID;
     270         580 :     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         493 :         eRet = beans::PropertyState_DIRECT_VALUE;
     276             :     }
     277             :     else
     278             :     {
     279             :         //  check if pool default is set
     280             : 
     281          87 :         ScDocumentPool* pPool = pDocShell->GetDocument().GetPool();
     282          87 :         if ( pPool->GetPoolDefaultItem( nWID ) != NULL )
     283          87 :             eRet = beans::PropertyState_DIRECT_VALUE;
     284             :     }
     285             : 
     286         580 :     return eRet;
     287             : }
     288             : 
     289          29 : 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          29 :     SolarMutexGuard aGuard;
     296          29 :     const OUString* pNames = aPropertyNames.getConstArray();
     297          29 :     uno::Sequence<beans::PropertyState> aRet(aPropertyNames.getLength());
     298          29 :     beans::PropertyState* pStates = aRet.getArray();
     299         609 :     for(sal_Int32 i = 0; i < aPropertyNames.getLength(); i++)
     300         580 :         pStates[i] = getPropertyState(pNames[i]);
     301          29 :     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         156 : }
     350             : 
     351             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11