xref: /aoo4110/main/sw/source/core/fields/fldbas.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 #include <float.h>
28 #include <rtl/math.hxx>
29 #include <svl/zforlist.hxx>
30 #include <svl/zformat.hxx>
31 #include <editeng/unolingu.hxx>
32 #include <unofldmid.h>
33 #include <doc.hxx>
34 #include <editsh.hxx>
35 #include <frame.hxx>
36 #include <fldbas.hxx>
37 #include <flddat.hxx>
38 #include <ndtxt.hxx>
39 #include <fmtfld.hxx>
40 #include <txtfld.hxx>
41 #include <pam.hxx>
42 #include <docfld.hxx>
43 #include <swtable.hxx>
44 #include <docufld.hxx>
45 #include <expfld.hxx>
46 #include <shellres.hxx>
47 #include <calc.hxx>
48 #include <comcore.hrc>
49 
50 #include <math.h>
51 #include <float.h>
52 
53 using namespace ::com::sun::star;
54 using namespace nsSwDocInfoSubType;
55 
lcl_GetLanguageOfFormat(sal_uInt16 nLng,sal_uLong nFmt,const SvNumberFormatter & rFormatter)56 sal_uInt16 lcl_GetLanguageOfFormat( sal_uInt16 nLng, sal_uLong nFmt,
57 								const SvNumberFormatter& rFormatter )
58 {
59 	if( nLng == LANGUAGE_NONE )	// wegen Bug #60010
60 		nLng = LANGUAGE_SYSTEM;
61 	else if( nLng == ::GetAppLanguage() )
62 		switch( rFormatter.GetIndexTableOffset( nFmt ))
63 		{
64 		case NF_NUMBER_SYSTEM:
65 		case NF_DATE_SYSTEM_SHORT:
66 		case NF_DATE_SYSTEM_LONG:
67 		case NF_DATETIME_SYSTEM_SHORT_HHMM:
68 			nLng = LANGUAGE_SYSTEM;
69 			break;
70         default: break;
71 		}
72 	return nLng;
73 }
74 
75 /*--------------------------------------------------------------------
76 	Beschreibung: Globals
77  --------------------------------------------------------------------*/
78 // Array der Feldname
79 
80 SvStringsDtor* SwFieldType::pFldNames = 0;
81 
82 DBG_NAME(SwFieldType)
83 
84 	sal_uInt16 __FAR_DATA aTypeTab[] = {
85 	/* RES_DBFLD			*/		TYP_DBFLD,
86 	/* RES_USERFLD			*/		TYP_USERFLD,
87 	/* RES_FILENAMEFLD		*/		TYP_FILENAMEFLD,
88 	/* RES_DBNAMEFLD		*/		TYP_DBNAMEFLD,
89 	/* RES_DATEFLD	   		*/		TYP_DATEFLD,
90 	/* RES_TIMEFLD			*/		TYP_TIMEFLD,
91 	/* RES_PAGENUMBERFLD	*/		TYP_PAGENUMBERFLD,	// dynamisch
92 	/* RES_AUTHORFLD		*/		TYP_AUTHORFLD,
93 	/* RES_CHAPTERFLD		*/		TYP_CHAPTERFLD,
94 	/* RES_DOCSTATFLD		*/		TYP_DOCSTATFLD,
95 	/* RES_GETEXPFLD		*/		TYP_GETFLD,			// dynamisch
96 	/* RES_SETEXPFLD		*/		TYP_SETFLD,			// dynamisch
97 	/* RES_GETREFFLD		*/		TYP_GETREFFLD,
98 	/* RES_HIDDENTXTFLD		*/		TYP_HIDDENTXTFLD,
99 	/* RES_POSTITFLD		*/		TYP_POSTITFLD,
100 	/* RES_FIXDATEFLD		*/		TYP_FIXDATEFLD,
101 	/* RES_FIXTIMEFLD		*/		TYP_FIXTIMEFLD,
102 	/* RES_REGFLD			*/		0,					// alt
103 	/* RES_VARREGFLD		*/		0,					// alt
104 	/* RES_SETREFFLD		*/		TYP_SETREFFLD,
105 	/* RES_INPUTFLD			*/		TYP_INPUTFLD,
106 	/* RES_MACROFLD			*/		TYP_MACROFLD,
107 	/* RES_DDEFLD			*/		TYP_DDEFLD,
108 	/* RES_TABLEFLD			*/		TYP_FORMELFLD,
109 	/* RES_HIDDENPARAFLD	*/		TYP_HIDDENPARAFLD,
110 	/* RES_DOCINFOFLD		*/		TYP_DOCINFOFLD,
111 	/* RES_TEMPLNAMEFLD		*/		TYP_TEMPLNAMEFLD,
112 	/* RES_DBNEXTSETFLD		*/		TYP_DBNEXTSETFLD,
113 	/* RES_DBNUMSETFLD		*/		TYP_DBNUMSETFLD,
114 	/* RES_DBSETNUMBERFLD	*/		TYP_DBSETNUMBERFLD,
115 	/* RES_EXTUSERFLD		*/		TYP_EXTUSERFLD,
116 	/* RES_REFPAGESETFLD	*/		TYP_SETREFPAGEFLD,
117 	/* RES_REFPAGEGETFLD    */		TYP_GETREFPAGEFLD,
118 	/* RES_INTERNETFLD		*/ 		TYP_INTERNETFLD,
119 	/* RES_JUMPEDITFLD		*/		TYP_JUMPEDITFLD,
120 	/* RES_SCRIPTFLD		*/		TYP_SCRIPTFLD,
121 	/* RES_DATETIMEFLD		*/		0,					// dynamisch
122 	/* RES_AUTHORITY		*/		TYP_AUTHORITY,
123 	/* RES_COMBINED_CHARS	*/		TYP_COMBINED_CHARS,
124     /* RES_DROPDOWN         */      TYP_DROPDOWN
125 	};
126 		// ????? TYP_USRINPFLD,
127 
128 
129 
GetTypeStr(sal_uInt16 nTypeId)130 const String& SwFieldType::GetTypeStr(sal_uInt16 nTypeId)
131 {
132 	if( !pFldNames )
133 		_GetFldName();
134 
135 	if( nTypeId < SwFieldType::pFldNames->Count() )
136 		return *SwFieldType::pFldNames->GetObject( nTypeId );
137 	else
138 		return aEmptyStr;
139 }
140 
141 
142 /*---------------------------------------------------
143  Jedes Feld referenziert einen Feldtypen, der fuer
144  jedes Dokument einmalig ist.
145  --------------------------------------------------*/
146 
SwFieldType(sal_uInt16 nWhichId)147 SwFieldType::SwFieldType( sal_uInt16 nWhichId )
148 	: SwModify(0),
149 	nWhich( nWhichId )
150 {
151 	DBG_CTOR( SwFieldType, 0 );
152 }
153 
154 #ifdef DBG_UTIL
155 
~SwFieldType()156 SwFieldType::~SwFieldType()
157 {
158 	DBG_DTOR( SwFieldType, 0 );
159 }
160 
161 #endif
162 
GetName() const163 const String& SwFieldType::GetName() const
164 {
165 	return aEmptyStr;
166 }
167 
QueryValue(uno::Any &,sal_uInt16) const168 sal_Bool SwFieldType::QueryValue( uno::Any&, sal_uInt16 ) const
169 {
170 	return sal_False;
171 }
PutValue(const uno::Any &,sal_uInt16)172 sal_Bool SwFieldType::PutValue( const uno::Any& , sal_uInt16 )
173 {
174 	return sal_False;
175 }
176 
177 /*--------------------------------------------------------------------
178 	Beschreibung:	Basisklasse aller Felder
179 					Felder referenzieren einen Feldtyp
180 					Felder sind n-mal vorhanden, Feldtypen nur einmal
181  --------------------------------------------------------------------*/
182 
SwField(SwFieldType * pTyp,sal_uInt32 nFmt,sal_uInt16 nLng,bool bUseFieldValueCache)183 SwField::SwField(
184     SwFieldType* pTyp,
185     sal_uInt32 nFmt,
186     sal_uInt16 nLng,
187     bool bUseFieldValueCache )
188     : m_Cache()
189     , m_bUseFieldValueCache( bUseFieldValueCache )
190     , nLang( nLng )
191     , bIsAutomaticLanguage( sal_True )
192     , nFormat( nFmt )
193     , pType( pTyp )
194 {
195     ASSERT( pTyp, "SwField: ungueltiger SwFieldType" );
196 }
197 
~SwField()198 SwField::~SwField()
199 {
200 }
201 
202 /*--------------------------------------------------------------------
203 	Beschreibung: Statt Umweg ueber den Typ
204  --------------------------------------------------------------------*/
205 
206 #ifdef DBG_UTIL
Which() const207 sal_uInt16 SwField::Which() const
208 {
209 	ASSERT(pType, "Kein Typ vorhanden");
210 	return pType->Which();
211 }
212 #endif
213 
214 /*--------------------------------------------------------------------
215 	Beschreibung:
216  --------------------------------------------------------------------*/
217 
GetTypeId() const218 sal_uInt16 SwField::GetTypeId() const
219 {
220 
221 	sal_uInt16 nRet;
222 	switch( pType->Which() )
223 	{
224 	case RES_DATETIMEFLD:
225 		if (GetSubType() & FIXEDFLD)
226 			nRet = static_cast<sal_uInt16>(GetSubType() & DATEFLD ? TYP_FIXDATEFLD : TYP_FIXTIMEFLD);
227 		else
228 			nRet = static_cast<sal_uInt16>(GetSubType() & DATEFLD ? TYP_DATEFLD : TYP_TIMEFLD);
229 		break;
230 	case RES_GETEXPFLD:
231 		nRet = static_cast<sal_uInt16>(nsSwGetSetExpType::GSE_FORMULA & GetSubType() ? TYP_FORMELFLD : TYP_GETFLD);
232 		break;
233 
234 	case RES_HIDDENTXTFLD:
235 		nRet = GetSubType();
236 		break;
237 
238 	case RES_SETEXPFLD:
239 		if( nsSwGetSetExpType::GSE_SEQ & GetSubType() )
240 			nRet = TYP_SEQFLD;
241 		else if( ((SwSetExpField*)this)->GetInputFlag() )
242 			nRet = TYP_SETINPFLD;
243 		else
244 			nRet = TYP_SETFLD;
245 		break;
246 
247 	case RES_PAGENUMBERFLD:
248 		nRet = GetSubType();
249 		if( PG_NEXT == nRet )
250 			nRet = TYP_NEXTPAGEFLD;
251 		else if( PG_PREV == nRet )
252 			nRet = TYP_PREVPAGEFLD;
253 		else
254 			nRet = TYP_PAGENUMBERFLD;
255 		break;
256 
257 	default:
258 		nRet = aTypeTab[ pType->Which() ];
259 	}
260 	return nRet;
261 }
262 
263 
264 /*--------------------------------------------------------------------
265 	Beschreibung: liefert den Namen oder den Inhalt
266  --------------------------------------------------------------------*/
267 
GetFieldName() const268 String SwField::GetFieldName() const
269 {
270     sal_uInt16 nTypeId = GetTypeId();
271     if (RES_DATETIMEFLD == GetTyp()->Which())
272     {
273         nTypeId = static_cast<sal_uInt16>(
274             ((GetSubType() & DATEFLD) != 0) ? TYP_DATEFLD : TYP_TIMEFLD);
275     }
276     String sRet = SwFieldType::GetTypeStr( nTypeId );
277     if (IsFixed())
278     {
279         sRet += ' ';
280         sRet += ViewShell::GetShellRes()->aFixedStr;
281     }
282 	return sRet;
283 }
284 
285 /*--------------------------------------------------------------------
286 	Beschreibung: Parameter setzen auslesen
287  --------------------------------------------------------------------*/
288 
GetPar1() const289 const String& SwField::GetPar1() const
290 {
291 	return aEmptyStr;
292 }
293 
GetPar2() const294 String SwField::GetPar2() const
295 {
296 	return aEmptyStr;
297 }
298 
GetFormula() const299 String SwField::GetFormula() const
300 {
301 	return GetPar2();
302 }
303 
SetPar1(const String &)304 void SwField::SetPar1(const String& )
305 {}
306 
SetPar2(const String &)307 void SwField::SetPar2(const String& )
308 {}
309 
GetSubType() const310 sal_uInt16 SwField::GetSubType() const
311 {
312 //	ASSERT(0, "Sorry Not implemented");
313 	return 0;
314 }
315 
SetSubType(sal_uInt16)316 void SwField::SetSubType(sal_uInt16 )
317 {
318 //	ASSERT(0, "Sorry Not implemented");
319 }
320 
QueryValue(uno::Any & rVal,sal_uInt16 nWhichId) const321 sal_Bool  SwField::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
322 {
323     switch( nWhichId )
324     {
325         case FIELD_PROP_BOOL4:
326         {
327             sal_Bool bFixed = !bIsAutomaticLanguage;
328             rVal.setValue(&bFixed, ::getCppuBooleanType());
329         }
330         break;
331         default:
332             DBG_ERROR("illegal property");
333     }
334     return sal_True;
335 }
PutValue(const uno::Any & rVal,sal_uInt16 nWhichId)336 sal_Bool SwField::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
337 {
338     switch( nWhichId )
339     {
340         case FIELD_PROP_BOOL4:
341         {
342             sal_Bool bFixed = sal_False;
343             if(rVal >>= bFixed)
344                 bIsAutomaticLanguage = !bFixed;
345         }
346         break;
347         default:
348             DBG_ERROR("illegal property");
349     }
350     return sal_True;
351 }
352 
353 
354 /*--------------------------------------------------------------------
355 	Beschreibung: 	neuen Typ setzen
356 					(wird fuer das Kopieren zwischen Dokumenten benutzt)
357 					muss immer vom gleichen Typ sein.
358  --------------------------------------------------------------------*/
359 
ChgTyp(SwFieldType * pNewType)360 SwFieldType* SwField::ChgTyp( SwFieldType* pNewType )
361 {
362 	ASSERT( pNewType && pNewType->Which() == pType->Which(),
363 			"kein Typ oder ungleiche Typen" );
364 
365 	SwFieldType* pOld = pType;
366 	pType = pNewType;
367 	return pOld;
368 }
369 
370 	// hat das Feld eine Action auf dem ClickHandler ? (z.B. INetFelder,..)
HasClickHdl() const371 sal_Bool SwField::HasClickHdl() const
372 {
373 	sal_Bool bRet = sal_False;
374 	switch( pType->Which() )
375 	{
376 	case RES_INTERNETFLD:
377 	case RES_JUMPEDITFLD:
378 	case RES_GETREFFLD:
379 	case RES_MACROFLD:
380 	case RES_INPUTFLD:
381     case RES_DROPDOWN :
382 		bRet = sal_True;
383 		break;
384 
385 	case RES_SETEXPFLD:
386 		bRet = ((SwSetExpField*)this)->GetInputFlag();
387 		break;
388 	}
389 	return bRet;
390 }
391 
SetLanguage(sal_uInt16 nLng)392 void SwField::SetLanguage(sal_uInt16 nLng)
393 {
394 	nLang = nLng;
395 }
396 
ChangeFormat(sal_uInt32 n)397 void SwField::ChangeFormat(sal_uInt32 n)
398 {
399 	nFormat = n;
400 }
401 
IsFixed() const402 sal_Bool SwField::IsFixed() const
403 {
404 	sal_Bool bRet = sal_False;
405 	switch( pType->Which() )
406 	{
407 	case RES_FIXDATEFLD:
408 	case RES_FIXTIMEFLD:
409 		bRet = sal_True;
410 		break;
411 
412 	case RES_DATETIMEFLD:
413 		bRet = 0 != (GetSubType() & FIXEDFLD);
414 		break;
415 
416 	case RES_EXTUSERFLD:
417 	case RES_AUTHORFLD:
418 		bRet = 0 != (GetFormat() & AF_FIXED);
419 		break;
420 
421 	case RES_FILENAMEFLD:
422 		bRet = 0 != (GetFormat() & FF_FIXED);
423 		break;
424 
425 	case RES_DOCINFOFLD:
426 		bRet = 0 != (GetSubType() & DI_SUB_FIXED);
427 		break;
428 	}
429 	return bRet;
430 }
431 
ExpandField(bool const bCached) const432 String SwField::ExpandField(bool const bCached) const
433 {
434     if ( m_bUseFieldValueCache )
435     {
436         if (!bCached) // #i85766# do not expand fields in clipboard documents
437         {
438             m_Cache = Expand();
439         }
440         return m_Cache;
441     }
442 
443     return Expand();
444 }
445 
CopyField() const446 SwField * SwField::CopyField() const
447 {
448     SwField *const pNew = Copy();
449     // #i85766# cache expansion of source (for clipboard)
450     // use this->cache, not this->Expand(): only text formatting calls Expand()
451     pNew->m_Cache = m_Cache;
452     pNew->m_bUseFieldValueCache = m_bUseFieldValueCache;
453 
454     return pNew;
455 }
456 
457 /*--------------------------------------------------------------------
458 	Beschreibung: Numerierung expandieren
459  --------------------------------------------------------------------*/
460 
FormatNumber(sal_uInt16 nNum,sal_uInt32 nFormat)461 String FormatNumber(sal_uInt16 nNum, sal_uInt32 nFormat)
462 {
463     if(SVX_NUM_PAGEDESC == nFormat)
464         return  String::CreateFromInt32( nNum );
465     SvxNumberType aNumber;
466 
467 	ASSERT(nFormat != SVX_NUM_NUMBER_NONE, "Falsches Nummern-Format" );
468 
469 	aNumber.SetNumberingType((sal_Int16)nFormat);
470 	return aNumber.GetNumStr(nNum);
471 }
472 
473 /*--------------------------------------------------------------------
474 	Beschreibung: CTOR SwValueFieldType
475  --------------------------------------------------------------------*/
476 
SwValueFieldType(SwDoc * pDocPtr,sal_uInt16 nWhichId)477 SwValueFieldType::SwValueFieldType( SwDoc* pDocPtr, sal_uInt16 nWhichId )
478 	: SwFieldType(nWhichId),
479 	pDoc(pDocPtr),
480 	bUseFormat(sal_True)
481 {
482 }
483 
SwValueFieldType(const SwValueFieldType & rTyp)484 SwValueFieldType::SwValueFieldType( const SwValueFieldType& rTyp )
485 	: SwFieldType(rTyp.Which()),
486 	pDoc(rTyp.GetDoc()),
487 	bUseFormat(rTyp.UseFormat())
488 {
489 }
490 
491 /*--------------------------------------------------------------------
492 	Beschreibung: Wert formatiert als String zurueckgeben
493  --------------------------------------------------------------------*/
494 
ExpandValue(const double & rVal,sal_uInt32 nFmt,sal_uInt16 nLng) const495 String SwValueFieldType::ExpandValue( const double& rVal,
496 										sal_uInt32 nFmt, sal_uInt16 nLng) const
497 {
498 	if (rVal >= DBL_MAX)		// FehlerString fuer Calculator
499 		return ViewShell::GetShellRes()->aCalc_Error;
500 
501 	String sExpand;
502 	SvNumberFormatter* pFormatter = pDoc->GetNumberFormatter();
503 	Color* pCol = 0;
504 
505 	// wegen Bug #60010
506 	sal_uInt16 nFmtLng = ::lcl_GetLanguageOfFormat( nLng, nFmt, *pFormatter );
507 
508 	if( nFmt < SV_COUNTRY_LANGUAGE_OFFSET && LANGUAGE_SYSTEM != nFmtLng )
509 	{
510 		short nType = NUMBERFORMAT_DEFINED;
511 		xub_StrLen nDummy;
512 
513 		const SvNumberformat* pEntry = pFormatter->GetEntry(nFmt);
514 
515 		if (pEntry && nLng != pEntry->GetLanguage())
516 		{
517 			sal_uInt32 nNewFormat = pFormatter->GetFormatForLanguageIfBuiltIn(nFmt,
518 													(LanguageType)nFmtLng);
519 
520 			if (nNewFormat == nFmt)
521 			{
522 				// Warscheinlich benutzerdefiniertes Format
523 				String sFmt(pEntry->GetFormatstring());
524 
525 				pFormatter->PutandConvertEntry(sFmt, nDummy, nType, nFmt,
526 										pEntry->GetLanguage(), nFmtLng );
527 			}
528 			else
529 				nFmt = nNewFormat;
530 		}
531 		ASSERT(pEntry, "Unbekanntes Zahlenformat!");
532 	}
533 
534 	if( pFormatter->IsTextFormat( nFmt ) )
535 	{
536 		String sValue;
537 		DoubleToString(sValue, rVal, nFmtLng);
538 		pFormatter->GetOutputString(sValue, nFmt, sExpand, &pCol);
539 	}
540 	else
541 		pFormatter->GetOutputString(rVal, nFmt, sExpand, &pCol);
542 
543 	return sExpand;
544 }
545 
546 /*--------------------------------------------------------------------
547 	Beschreibung:
548  --------------------------------------------------------------------*/
549 
DoubleToString(String & rValue,const double & rVal,sal_uInt32 nFmt) const550 void SwValueFieldType::DoubleToString( String &rValue, const double &rVal,
551 										sal_uInt32 nFmt) const
552 {
553 	SvNumberFormatter* pFormatter = pDoc->GetNumberFormatter();
554 	const SvNumberformat* pEntry = pFormatter->GetEntry(nFmt);
555 
556 	if (pEntry)
557 		DoubleToString(rValue, rVal, pEntry->GetLanguage());
558 }
559 
560 /*--------------------------------------------------------------------
561 	Beschreibung:
562  --------------------------------------------------------------------*/
563 
DoubleToString(String & rValue,const double & rVal,sal_uInt16 nLng) const564 void SwValueFieldType::DoubleToString( String &rValue, const double &rVal,
565 										sal_uInt16 nLng ) const
566 {
567 	SvNumberFormatter* pFormatter = pDoc->GetNumberFormatter();
568 
569 	// wegen Bug #60010
570 	if( nLng == LANGUAGE_NONE )	// wegen Bug #60010
571 		nLng = LANGUAGE_SYSTEM;
572 
573 	pFormatter->ChangeIntl( nLng );	// Separator in der richtigen Sprache besorgen
574 	rValue = ::rtl::math::doubleToUString( rVal, rtl_math_StringFormat_F, 12,
575 									pFormatter->GetDecSep(), true );
576 }
577 
578 /*--------------------------------------------------------------------
579 	Beschreibung: CTOR SwValueField
580  --------------------------------------------------------------------*/
581 
SwValueField(SwValueFieldType * pFldType,sal_uInt32 nFmt,sal_uInt16 nLng,const double fVal)582 SwValueField::SwValueField( SwValueFieldType* pFldType, sal_uInt32 nFmt,
583 							sal_uInt16 nLng, const double fVal )
584 	: SwField(pFldType, nFmt, nLng),
585 	fValue(fVal)
586 {
587 }
588 
SwValueField(const SwValueField & rFld)589 SwValueField::SwValueField( const SwValueField& rFld )
590 	: SwField(rFld),
591 	fValue(rFld.GetValue())
592 {
593 }
594 
~SwValueField()595 SwValueField::~SwValueField()
596 {
597 }
598 /*--------------------------------------------------------------------
599 	Beschreibung: 	neuen Typ setzen
600 					(wird fuer das Kopieren zwischen Dokumenten benutzt)
601 					muss immer vom gleichen Typ sein.
602  --------------------------------------------------------------------*/
603 
ChgTyp(SwFieldType * pNewType)604 SwFieldType* SwValueField::ChgTyp( SwFieldType* pNewType )
605 {
606 	SwDoc* pNewDoc = ((SwValueFieldType *)pNewType)->GetDoc();
607 	SwDoc* pDoc    = GetDoc();
608 
609 	if( pNewDoc && pDoc && pDoc != pNewDoc)
610 	{
611 		SvNumberFormatter* pFormatter = pNewDoc->GetNumberFormatter();
612 
613 		if( pFormatter && pFormatter->HasMergeFmtTbl() &&
614 			((SwValueFieldType *)GetTyp())->UseFormat() )
615 			SetFormat(pFormatter->GetMergeFmtIndex( GetFormat() ));
616 	}
617 
618 	return SwField::ChgTyp(pNewType);
619 }
620 
621 /*--------------------------------------------------------------------
622 	Beschreibung: Format in Office-Sprache ermitteln
623  --------------------------------------------------------------------*/
624 
GetSystemFormat(SvNumberFormatter * pFormatter,sal_uInt32 nFmt)625 sal_uInt32 SwValueField::GetSystemFormat(SvNumberFormatter* pFormatter, sal_uInt32 nFmt)
626 {
627 	const SvNumberformat* pEntry = pFormatter->GetEntry(nFmt);
628 	sal_uInt16 nLng = SvxLocaleToLanguage( SvtSysLocale().GetLocaleData().getLocale() );
629 
630 	if (pEntry && nLng != pEntry->GetLanguage())
631 	{
632 		sal_uInt32 nNewFormat = pFormatter->GetFormatForLanguageIfBuiltIn(nFmt,
633 														(LanguageType)nLng);
634 
635 		if (nNewFormat == nFmt)
636 		{
637 			// Warscheinlich benutzerdefiniertes Format
638 			short nType = NUMBERFORMAT_DEFINED;
639 			xub_StrLen nDummy;
640 
641 			String sFmt(pEntry->GetFormatstring());
642 
643 			sal_uInt32 nFormat = nFmt;
644 			pFormatter->PutandConvertEntry(sFmt, nDummy, nType,
645 								nFormat, pEntry->GetLanguage(), nLng);
646 			nFmt = nFormat;
647 		}
648 		else
649 			nFmt = nNewFormat;
650 	}
651 
652 	return nFmt;
653 }
654 
655 /*--------------------------------------------------------------------
656 	Beschreibung: Sprache im Format anpassen
657  --------------------------------------------------------------------*/
658 
SetLanguage(sal_uInt16 nLng)659 void SwValueField::SetLanguage( sal_uInt16 nLng )
660 {
661     if( IsAutomaticLanguage() &&
662             ((SwValueFieldType *)GetTyp())->UseFormat() &&
663 		GetFormat() != SAL_MAX_UINT32 )
664 	{
665 		// wegen Bug #60010
666 		SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
667 		sal_uInt16 nFmtLng = ::lcl_GetLanguageOfFormat( nLng, GetFormat(),
668 													*pFormatter );
669 
670 		if( (GetFormat() >= SV_COUNTRY_LANGUAGE_OFFSET ||
671 			 LANGUAGE_SYSTEM != nFmtLng ) &&
672 			!(Which() == RES_USERFLD && (GetSubType()&nsSwExtendedSubType::SUB_CMD) ) )
673 		{
674 			const SvNumberformat* pEntry = pFormatter->GetEntry(GetFormat());
675 
676 			if( pEntry && nFmtLng != pEntry->GetLanguage() )
677 			{
678 				sal_uInt32 nNewFormat = pFormatter->GetFormatForLanguageIfBuiltIn(
679 										GetFormat(), (LanguageType)nFmtLng );
680 
681 				if( nNewFormat == GetFormat() )
682 				{
683 					// Warscheinlich benutzerdefiniertes Format
684 					short nType = NUMBERFORMAT_DEFINED;
685 					xub_StrLen nDummy;
686 					String sFmt( pEntry->GetFormatstring() );
687 					pFormatter->PutandConvertEntry( sFmt, nDummy, nType,
688 													nNewFormat,
689 													pEntry->GetLanguage(),
690 													nFmtLng );
691 				}
692 				SetFormat( nNewFormat );
693 			}
694 			ASSERT(pEntry, "Unbekanntes Zahlenformat!");
695 		}
696 	}
697 
698 	SwField::SetLanguage(nLng);
699 }
700 
701 /*--------------------------------------------------------------------
702 	Beschreibung:
703  --------------------------------------------------------------------*/
704 
GetValue() const705 double SwValueField::GetValue() const
706 {
707 	return fValue;
708 }
709 
SetValue(const double & rVal)710 void SwValueField::SetValue( const double& rVal )
711 {
712 	fValue = rVal;
713 }
714 
715 /*--------------------------------------------------------------------
716 	Beschreibung: SwFormulaField
717  --------------------------------------------------------------------*/
718 
SwFormulaField(SwValueFieldType * pFldType,sal_uInt32 nFmt,const double fVal)719 SwFormulaField::SwFormulaField( SwValueFieldType* pFldType, sal_uInt32 nFmt, const double fVal)
720 	: SwValueField(pFldType, nFmt, LANGUAGE_SYSTEM, fVal)
721 {
722 }
723 
SwFormulaField(const SwFormulaField & rFld)724 SwFormulaField::SwFormulaField( const SwFormulaField& rFld )
725 	: SwValueField((SwValueFieldType *)rFld.GetTyp(), rFld.GetFormat(),
726 					rFld.GetLanguage(), rFld.GetValue())
727 {
728 }
729 
730 /*--------------------------------------------------------------------
731 	Beschreibung:
732  --------------------------------------------------------------------*/
733 
GetFormula() const734 String SwFormulaField::GetFormula() const
735 {
736 	return sFormula;
737 }
738 
739 /*--------------------------------------------------------------------
740 	Beschreibung:
741  --------------------------------------------------------------------*/
742 
SetFormula(const String & rStr)743 void SwFormulaField::SetFormula(const String& rStr)
744 {
745 	sFormula = rStr;
746 
747 	sal_uLong nFmt(GetFormat());
748 
749 	if( nFmt && SAL_MAX_UINT32 != nFmt )
750 	{
751 		xub_StrLen nPos = 0;
752 		double fTmpValue;
753 		if( SwCalc::Str2Double( rStr, nPos, fTmpValue, GetDoc() ) )
754 			SwValueField::SetValue( fTmpValue );
755 	}
756 }
757 
758 /*--------------------------------------------------------------------
759 	Beschreibung:
760  --------------------------------------------------------------------*/
761 
SetExpandedFormula(const String & rStr)762 void SwFormulaField::SetExpandedFormula( const String& rStr )
763 {
764 	sal_uInt32 nFmt(GetFormat());
765 
766 	if (nFmt && nFmt != SAL_MAX_UINT32 && ((SwValueFieldType *)GetTyp())->UseFormat())
767 	{
768 		double fTmpValue;
769 
770 		SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
771 
772 		if (pFormatter->IsNumberFormat(rStr, nFmt, fTmpValue))
773 		{
774 			SwValueField::SetValue(fTmpValue);
775 			sFormula.Erase();
776 
777 			((SwValueFieldType *)GetTyp())->DoubleToString(sFormula, fTmpValue, nFmt);
778 			return;
779 		}
780 	}
781 	sFormula = rStr;
782 }
783 
784 /*--------------------------------------------------------------------
785 	Beschreibung:
786  --------------------------------------------------------------------*/
787 
GetExpandedFormula() const788 String SwFormulaField::GetExpandedFormula() const
789 {
790 	sal_uInt32 nFmt(GetFormat());
791 
792 	if (nFmt && nFmt != SAL_MAX_UINT32 && ((SwValueFieldType *)GetTyp())->UseFormat())
793 	{
794 		String sFormattedValue;
795 		Color* pCol = 0;
796 
797 		SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
798 
799 		if (pFormatter->IsTextFormat(nFmt))
800 		{
801 			String sValue;
802 			((SwValueFieldType *)GetTyp())->DoubleToString(sValue, GetValue(), nFmt);
803 			pFormatter->GetOutputString(sValue, nFmt, sFormattedValue, &pCol);
804 		}
805 		else
806 			pFormatter->GetOutputString(GetValue(), nFmt, sFormattedValue, &pCol);
807 
808 		return sFormattedValue;
809 	}
810 	else
811 		return GetFormula();
812 }
813 
GetDescription() const814 String SwField::GetDescription() const
815 {
816     return SW_RES(STR_FIELD);
817 }
818