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

Generated by: LCOV version 1.10