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

Generated by: LCOV version 1.10