LCOV - code coverage report
Current view: top level - sw/source/core/fields - usrfld.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 61 152 40.1 %
Date: 2014-04-11 Functions: 13 23 56.5 %
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 <svl/zforlist.hxx>
      21             : #include <svl/zformat.hxx>
      22             : 
      23             : #include <svx/svdmodel.hxx>
      24             : 
      25             : #include <calbck.hxx>
      26             : #include <calc.hxx>
      27             : #include <usrfld.hxx>
      28             : #include <doc.hxx>
      29             : #include <IDocumentUndoRedo.hxx>
      30             : #include <editsh.hxx>
      31             : #include <dpage.hxx>
      32             : #include <unofldmid.h>
      33             : 
      34             : using namespace ::com::sun::star;
      35             : 
      36             : // Userfields
      37             : 
      38          13 : SwUserField::SwUserField(SwUserFieldType* pTyp, sal_uInt16 nSub, sal_uInt32 nFmt)
      39             :     : SwValueField(pTyp, nFmt),
      40          13 :     nSubType(nSub)
      41             : {
      42          13 : }
      43             : 
      44           4 : OUString SwUserField::Expand() const
      45             : {
      46           4 :     if(!(nSubType & nsSwExtendedSubType::SUB_INVISIBLE))
      47           4 :         return ((SwUserFieldType*)GetTyp())->Expand(GetFormat(), nSubType, GetLanguage());
      48             : 
      49           0 :     return OUString();
      50             : }
      51             : 
      52          11 : SwField* SwUserField::Copy() const
      53             : {
      54          11 :     SwField* pTmp = new SwUserField((SwUserFieldType*)GetTyp(), nSubType, GetFormat());
      55          11 :     pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
      56          11 :     return pTmp;
      57             : }
      58             : 
      59           1 : OUString SwUserField::GetFieldName() const
      60             : {
      61           2 :     return SwFieldType::GetTypeStr(TYP_USERFLD) +
      62           4 :         " " + GetTyp()->GetName() + " = " +
      63           3 :         static_cast<SwUserFieldType*>(GetTyp())->GetContent();
      64             : }
      65             : 
      66           0 : double SwUserField::GetValue() const
      67             : {
      68           0 :     return ((SwUserFieldType*)GetTyp())->GetValue();
      69             : }
      70             : 
      71           0 : void SwUserField::SetValue( const double& rVal )
      72             : {
      73           0 :     ((SwUserFieldType*)GetTyp())->SetValue(rVal);
      74           0 : }
      75             : 
      76             : /// Get name
      77           0 : OUString SwUserField::GetPar1() const
      78             : {
      79           0 :     return ((const SwUserFieldType*)GetTyp())->GetName();
      80             : }
      81             : 
      82             : /// Get content
      83           0 : OUString SwUserField::GetPar2() const
      84             : {
      85           0 :     return ((SwUserFieldType*)GetTyp())->GetContent(GetFormat());
      86             : }
      87             : 
      88           0 : void SwUserField::SetPar2(const OUString& rStr)
      89             : {
      90           0 :     ((SwUserFieldType*)GetTyp())->SetContent(rStr, GetFormat());
      91           0 : }
      92             : 
      93           2 : sal_uInt16 SwUserField::GetSubType() const
      94             : {
      95           2 :     return ((SwUserFieldType*)GetTyp())->GetType() | nSubType;
      96             : }
      97             : 
      98           0 : void SwUserField::SetSubType(sal_uInt16 nSub)
      99             : {
     100           0 :     ((SwUserFieldType*)GetTyp())->SetType(nSub & 0x00ff);
     101           0 :     nSubType = nSub & 0xff00;
     102           0 : }
     103             : 
     104           0 : bool SwUserField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
     105             : {
     106           0 :     switch( nWhichId )
     107             :     {
     108             :     case FIELD_PROP_BOOL2:
     109             :         {
     110           0 :             sal_Bool bTmp = 0 != (nSubType & nsSwExtendedSubType::SUB_CMD);
     111           0 :             rAny.setValue(&bTmp, ::getBooleanCppuType());
     112             :         }
     113           0 :         break;
     114             :     case FIELD_PROP_BOOL1:
     115             :         {
     116           0 :             sal_Bool bTmp = 0 == (nSubType & nsSwExtendedSubType::SUB_INVISIBLE);
     117           0 :             rAny.setValue(&bTmp, ::getBooleanCppuType());
     118             :         }
     119           0 :         break;
     120             :     case FIELD_PROP_FORMAT:
     121           0 :         rAny <<= (sal_Int32)GetFormat();
     122           0 :         break;
     123             :     default:
     124           0 :         return SwField::QueryValue(rAny, nWhichId);
     125             :     }
     126           0 :     return true;
     127             : }
     128             : 
     129           0 : bool SwUserField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
     130             : {
     131           0 :     switch( nWhichId )
     132             :     {
     133             :     case FIELD_PROP_BOOL1:
     134           0 :         if(*(sal_Bool*) rAny.getValue())
     135           0 :             nSubType &= (~nsSwExtendedSubType::SUB_INVISIBLE);
     136             :         else
     137           0 :             nSubType |= nsSwExtendedSubType::SUB_INVISIBLE;
     138           0 :         break;
     139             :     case FIELD_PROP_BOOL2:
     140           0 :         if(*(sal_Bool*) rAny.getValue())
     141           0 :             nSubType |= nsSwExtendedSubType::SUB_CMD;
     142             :         else
     143           0 :             nSubType &= (~nsSwExtendedSubType::SUB_CMD);
     144           0 :         break;
     145             :     case FIELD_PROP_FORMAT:
     146             :         {
     147           0 :             sal_Int32 nTmp = 0;
     148           0 :             rAny >>= nTmp;
     149           0 :             SetFormat(nTmp);
     150             :         }
     151           0 :         break;
     152             :     default:
     153           0 :         return SwField::PutValue(rAny, nWhichId);
     154             :     }
     155           0 :     return true;
     156             : }
     157             : 
     158          46 : SwUserFieldType::SwUserFieldType( SwDoc* pDocPtr, const OUString& aNam )
     159             :     : SwValueFieldType( pDocPtr, RES_USERFLD ),
     160             :     nValue( 0 ),
     161          46 :     nType(nsSwGetSetExpType::GSE_STRING)
     162             : {
     163          46 :     bValidValue = bDeleted = false;
     164          46 :     aName = aNam;
     165             : 
     166          46 :     if (nType & nsSwGetSetExpType::GSE_STRING)
     167          46 :         EnableFormat(sal_False);    // Do not use a Numberformatter
     168          46 : }
     169             : 
     170           5 : OUString SwUserFieldType::Expand(sal_uInt32 nFmt, sal_uInt16 nSubType, sal_uInt16 nLng)
     171             : {
     172           5 :     if((nType & nsSwGetSetExpType::GSE_EXPR) && !(nSubType & nsSwExtendedSubType::SUB_CMD))
     173             :     {
     174           0 :         EnableFormat(sal_True);
     175           0 :         return ExpandValue(nValue, nFmt, nLng);
     176             :     }
     177             : 
     178           5 :     EnableFormat(sal_False);    // Do not use a Numberformatter
     179           5 :     return aContent;
     180             : }
     181             : 
     182          23 : SwFieldType* SwUserFieldType::Copy() const
     183             : {
     184          23 :     SwUserFieldType *pTmp = new SwUserFieldType( GetDoc(), aName );
     185          23 :     pTmp->aContent      = aContent;
     186          23 :     pTmp->nType         = nType;
     187          23 :     pTmp->bValidValue   = bValidValue;
     188          23 :     pTmp->nValue        = nValue;
     189          23 :     pTmp->bDeleted      = bDeleted;
     190             : 
     191          23 :     return pTmp;
     192             : }
     193             : 
     194         449 : OUString SwUserFieldType::GetName() const
     195             : {
     196         449 :     return aName;
     197             : }
     198             : 
     199          46 : void SwUserFieldType::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew )
     200             : {
     201          46 :     if( !pOld && !pNew )
     202          46 :         ChgValid( false );
     203             : 
     204          46 :     NotifyClients( pOld, pNew );
     205             : 
     206             :     // update input fields that might be connected to the user field
     207          46 :     if ( !IsModifyLocked() )
     208             :     {
     209          46 :         LockModify();
     210          46 :         GetDoc()->GetSysFldType( RES_INPUTFLD )->UpdateFlds();
     211          46 :         UnlockModify();
     212             :     }
     213          46 : }
     214             : 
     215           0 : double SwUserFieldType::GetValue( SwCalc& rCalc )
     216             : {
     217           0 :     if(bValidValue)
     218           0 :         return nValue;
     219             : 
     220           0 :     if(!rCalc.Push( this ))
     221             :     {
     222           0 :         rCalc.SetCalcError( CALC_SYNTAX );
     223           0 :         return 0;
     224             :     }
     225           0 :     nValue = rCalc.Calculate( aContent ).GetDouble();
     226           0 :     rCalc.Pop();
     227             : 
     228           0 :     if( !rCalc.IsCalcError() )
     229           0 :         bValidValue = true;
     230             :     else
     231           0 :         nValue = 0;
     232             : 
     233           0 :     return nValue;
     234             : }
     235             : 
     236           3 : OUString SwUserFieldType::GetContent( sal_uInt32 nFmt )
     237             : {
     238           3 :     if (nFmt && nFmt != SAL_MAX_UINT32)
     239             :     {
     240           0 :         OUString sFormattedValue;
     241           0 :         Color* pCol = 0;
     242             : 
     243           0 :         SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
     244             : 
     245           0 :         pFormatter->GetOutputString(GetValue(), nFmt, sFormattedValue, &pCol);
     246           0 :         return sFormattedValue;
     247             :     }
     248             : 
     249           3 :     return aContent;
     250             : }
     251             : 
     252          23 : void SwUserFieldType::SetContent( const OUString& rStr, sal_uInt32 nFmt )
     253             : {
     254          23 :     if( aContent != rStr )
     255             :     {
     256           0 :         aContent = rStr;
     257             : 
     258           0 :         if (nFmt && nFmt != SAL_MAX_UINT32)
     259             :         {
     260             :             double fValue;
     261             : 
     262           0 :             SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
     263             : 
     264           0 :             if (pFormatter->IsNumberFormat(rStr, nFmt, fValue))
     265             :             {
     266           0 :                 SetValue(fValue);
     267           0 :                 aContent = DoubleToString(fValue, nFmt);
     268             :             }
     269             :         }
     270             : 
     271           0 :         sal_Bool bModified = GetDoc()->IsModified();
     272           0 :         GetDoc()->SetModified();
     273           0 :         if( !bModified )    // Bug 57028
     274             :         {
     275           0 :             GetDoc()->GetIDocumentUndoRedo().SetUndoNoResetModified();
     276             :         }
     277             :     }
     278          23 : }
     279             : 
     280           0 : bool SwUserFieldType::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
     281             : {
     282           0 :     switch( nWhichId )
     283             :     {
     284             :     case FIELD_PROP_DOUBLE:
     285           0 :         rAny <<= (double) nValue;
     286           0 :         break;
     287             :     case FIELD_PROP_PAR2:
     288           0 :         rAny <<= aContent;
     289           0 :         break;
     290             :     case FIELD_PROP_BOOL1:
     291             :         {
     292           0 :             sal_Bool bExpression = 0 != (nsSwGetSetExpType::GSE_EXPR&nType);
     293           0 :             rAny.setValue(&bExpression, ::getBooleanCppuType());
     294             :         }
     295           0 :         break;
     296             :     default:
     297             :         OSL_FAIL("illegal property");
     298             :     }
     299           0 :     return true;
     300             : }
     301             : 
     302          45 : bool SwUserFieldType::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
     303             : {
     304          45 :     switch( nWhichId )
     305             :     {
     306             :     case FIELD_PROP_DOUBLE:
     307             :         {
     308           0 :             double fVal = 0;
     309           0 :             rAny >>= fVal;
     310           0 :             nValue = fVal;
     311             : 
     312             :             // The following line is in fact wrong, since the language is unknown (is part of the
     313             :             // field) and, thus, aContent should also belong to the field. Each field can have a
     314             :             // differnt language, but the same content with just different formatting.
     315           0 :             aContent = DoubleToString(nValue, static_cast<sal_uInt32>(LANGUAGE_SYSTEM));
     316             :         }
     317           0 :         break;
     318             :     case FIELD_PROP_PAR2:
     319          24 :         rAny >>= aContent;
     320          24 :         break;
     321             :     case FIELD_PROP_BOOL1:
     322          21 :         if(*(sal_Bool*)rAny.getValue())
     323             :         {
     324           0 :             nType |= nsSwGetSetExpType::GSE_EXPR;
     325           0 :             nType &= ~nsSwGetSetExpType::GSE_STRING;
     326             :         }
     327             :         else
     328             :         {
     329          21 :             nType &= ~nsSwGetSetExpType::GSE_EXPR;
     330          21 :             nType |= nsSwGetSetExpType::GSE_STRING;
     331             :         }
     332          21 :         break;
     333             :     default:
     334             :         OSL_FAIL("illegal property");
     335             :     }
     336          45 :     return true;
     337             : }
     338             : 
     339             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10