xref: /aoo42x/main/sw/source/core/fields/usrfld.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 #include <svl/zforlist.hxx>
32 #include <svl/zformat.hxx>
33 
34 #include <svx/svdmodel.hxx>
35 
36 #include <calbck.hxx>
37 #include <calc.hxx>
38 #include <usrfld.hxx>
39 #include <doc.hxx>
40 #include <IDocumentUndoRedo.hxx>
41 #include <editsh.hxx>
42 #include <dpage.hxx>
43 #include <unofldmid.h>
44 
45 
46 using namespace ::com::sun::star;
47 using ::rtl::OUString;
48 
49 /*--------------------------------------------------------------------
50 	Beschreibung: Benutzerfelder
51  --------------------------------------------------------------------*/
52 
53 SwUserField::SwUserField(SwUserFieldType* pTyp, sal_uInt16 nSub, sal_uInt32 nFmt)
54 	: SwValueField(pTyp, nFmt),
55 	nSubType(nSub)
56 {
57 }
58 
59 String SwUserField::Expand() const
60 {
61 	String sStr;
62 	if(!(nSubType & nsSwExtendedSubType::SUB_INVISIBLE))
63 		sStr = ((SwUserFieldType*)GetTyp())->Expand(GetFormat(), nSubType, GetLanguage());
64 
65 	return sStr;
66 }
67 
68 SwField* SwUserField::Copy() const
69 {
70     SwField* pTmp = new SwUserField((SwUserFieldType*)GetTyp(), nSubType, GetFormat());
71     pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
72     return pTmp;
73 }
74 
75 String SwUserField::GetFieldName() const
76 {
77     String aStr(SwFieldType::GetTypeStr(TYP_USERFLD));
78     aStr += ' ';
79     aStr += GetTyp()->GetName();
80     aStr.AppendAscii(" = ");
81     aStr += static_cast<SwUserFieldType*>(GetTyp())->GetContent();
82     return aStr;
83 }
84 
85 double SwUserField::GetValue() const
86 {
87 	return ((SwUserFieldType*)GetTyp())->GetValue();
88 }
89 
90 void SwUserField::SetValue( const double& rVal )
91 {
92 	((SwUserFieldType*)GetTyp())->SetValue(rVal);
93 }
94 
95 /*--------------------------------------------------------------------
96 	Beschreibung: Name
97  --------------------------------------------------------------------*/
98 
99 const String& SwUserField::GetPar1() const
100 {
101 	return ((SwUserFieldType*)GetTyp())->GetName();
102 }
103 
104 /*--------------------------------------------------------------------
105 	Beschreibung: Content
106  --------------------------------------------------------------------*/
107 
108 String SwUserField::GetPar2() const
109 {
110 	return ((SwUserFieldType*)GetTyp())->GetContent(GetFormat());
111 }
112 
113 void SwUserField::SetPar2(const String& rStr)
114 {
115 	((SwUserFieldType*)GetTyp())->SetContent(rStr, GetFormat());
116 }
117 
118 sal_uInt16 SwUserField::GetSubType() const
119 {
120 	return ((SwUserFieldType*)GetTyp())->GetType() | nSubType;
121 }
122 
123 void SwUserField::SetSubType(sal_uInt16 nSub)
124 {
125 	((SwUserFieldType*)GetTyp())->SetType(nSub & 0x00ff);
126 	nSubType = nSub & 0xff00;
127 }
128 
129 /*-----------------09.03.98 08:04-------------------
130 
131 --------------------------------------------------*/
132 sal_Bool SwUserField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
133 {
134     switch( nWhichId )
135 	{
136 	case FIELD_PROP_BOOL2:
137 		{
138 			sal_Bool bTmp = 0 != (nSubType & nsSwExtendedSubType::SUB_CMD);
139 			rAny.setValue(&bTmp, ::getBooleanCppuType());
140 		}
141 		break;
142 	case FIELD_PROP_BOOL1:
143 		{
144 			sal_Bool bTmp = 0 == (nSubType & nsSwExtendedSubType::SUB_INVISIBLE);
145 			rAny.setValue(&bTmp, ::getBooleanCppuType());
146 		}
147 		break;
148 	case FIELD_PROP_FORMAT:
149 		rAny <<= (sal_Int32)GetFormat();
150 		break;
151 	default:
152         return SwField::QueryValue(rAny, nWhichId);
153     }
154 	return sal_True;
155 }
156 /*-----------------09.03.98 08:04-------------------
157 
158 --------------------------------------------------*/
159 sal_Bool SwUserField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
160 {
161     switch( nWhichId )
162 	{
163 	case FIELD_PROP_BOOL1:
164 		if(*(sal_Bool*) rAny.getValue())
165 			nSubType &= (~nsSwExtendedSubType::SUB_INVISIBLE);
166 		else
167 			nSubType |= nsSwExtendedSubType::SUB_INVISIBLE;
168 		break;
169 	case FIELD_PROP_BOOL2:
170 		if(*(sal_Bool*) rAny.getValue())
171 			nSubType |= nsSwExtendedSubType::SUB_CMD;
172 		else
173 			nSubType &= (~nsSwExtendedSubType::SUB_CMD);
174 		break;
175 	case FIELD_PROP_FORMAT:
176 		{
177 			sal_Int32 nTmp = 0;
178 			rAny >>= nTmp;
179 			SetFormat(nTmp);
180 		}
181 		break;
182 	default:
183         return SwField::PutValue(rAny, nWhichId);
184     }
185 	return sal_True;
186 }
187 
188 /*--------------------------------------------------------------------
189 	Beschreibung: Benutzerfeldtypen
190  --------------------------------------------------------------------*/
191 
192 SwUserFieldType::SwUserFieldType( SwDoc* pDocPtr, const String& aNam )
193 	: SwValueFieldType( pDocPtr, RES_USERFLD ),
194 	nValue( 0 ),
195 	nType(nsSwGetSetExpType::GSE_STRING)
196 {
197 	bValidValue = bDeleted = sal_False;
198 	aName = aNam;
199 
200 	if (nType & nsSwGetSetExpType::GSE_STRING)
201 		EnableFormat(sal_False);	// Numberformatter nicht einsetzen
202 }
203 
204 String SwUserFieldType::Expand(sal_uInt32 nFmt, sal_uInt16 nSubType, sal_uInt16 nLng)
205 {
206 	String aStr(aContent);
207 	if((nType & nsSwGetSetExpType::GSE_EXPR) && !(nSubType & nsSwExtendedSubType::SUB_CMD))
208 	{
209 		EnableFormat(sal_True);
210 		aStr = ExpandValue(nValue, nFmt, nLng);
211 	}
212 	else
213 		EnableFormat(sal_False);	// Numberformatter nicht einsetzen
214 
215 	return aStr;
216 }
217 
218 SwFieldType* SwUserFieldType::Copy() const
219 {
220 	SwUserFieldType *pTmp = new SwUserFieldType( GetDoc(), aName );
221 	pTmp->aContent 		= aContent;
222 	pTmp->nType 		= nType;
223 	pTmp->bValidValue 	= bValidValue;
224 	pTmp->nValue 		= nValue;
225 	pTmp->bDeleted 		= bDeleted;
226 
227 	return pTmp;
228 }
229 
230 const String& SwUserFieldType::GetName() const
231 {
232 	return aName;
233 }
234 
235 void SwUserFieldType::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew )
236 {
237 	if( !pOld && !pNew )
238 		ChgValid( sal_False );
239 
240 	NotifyClients( pOld, pNew );
241 	// und ggfs. am UserFeld haengende InputFelder updaten!
242 	GetDoc()->GetSysFldType( RES_INPUTFLD )->UpdateFlds();
243 }
244 
245 double SwUserFieldType::GetValue( SwCalc& rCalc )
246 {
247 	if(bValidValue)
248 		return nValue;
249 
250 	if(!rCalc.Push( this ))
251 	{
252 		rCalc.SetCalcError( CALC_SYNTAX );
253 		return 0;
254 	}
255 	nValue = rCalc.Calculate( aContent ).GetDouble();
256 	rCalc.Pop( this );
257 
258 	if( !rCalc.IsCalcError() )
259 		bValidValue = sal_True;
260 	else
261 		nValue = 0;
262 
263 	return nValue;
264 }
265 
266 String SwUserFieldType::GetContent( sal_uInt32 nFmt )
267 {
268 	if (nFmt && nFmt != SAL_MAX_UINT32)
269 	{
270 		String sFormattedValue;
271 		Color* pCol = 0;
272 
273 		SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
274 
275 		pFormatter->GetOutputString(GetValue(), nFmt, sFormattedValue, &pCol);
276 		return sFormattedValue;
277 	}
278 	else
279 		return aContent;
280 }
281 
282 void SwUserFieldType::SetContent( const String& rStr, sal_uInt32 nFmt )
283 {
284 	if( aContent != rStr )
285 	{
286 		aContent = rStr;
287 
288 		if (nFmt && nFmt != SAL_MAX_UINT32)
289 		{
290 			double fValue;
291 
292 			SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
293 
294 			if (pFormatter->IsNumberFormat(rStr, nFmt, fValue))
295 			{
296 				SetValue(fValue);
297 				aContent.Erase();
298 				DoubleToString(aContent, fValue, nFmt);
299 			}
300 		}
301 
302         sal_Bool bModified = GetDoc()->IsModified();
303 		GetDoc()->SetModified();
304 		if( !bModified )	// Bug 57028
305         {
306             GetDoc()->GetIDocumentUndoRedo().SetUndoNoResetModified();
307         }
308     }
309 }
310 
311 /*-----------------04.03.98 17:05-------------------
312 
313 --------------------------------------------------*/
314 sal_Bool SwUserFieldType::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
315 {
316     switch( nWhichId )
317 	{
318 	case FIELD_PROP_DOUBLE:
319 		rAny <<= (double) nValue;
320 		break;
321 	case FIELD_PROP_PAR2:
322 		rAny <<= rtl::OUString(aContent);
323 		break;
324 	case FIELD_PROP_BOOL1:
325 		{
326 			sal_Bool bExpression = 0 != (nsSwGetSetExpType::GSE_EXPR&nType);
327 			rAny.setValue(&bExpression, ::getBooleanCppuType());
328 		}
329 		break;
330 	default:
331 		DBG_ERROR("illegal property");
332 	}
333 	return sal_True;
334 }
335 /*-----------------04.03.98 17:05-------------------
336 
337 --------------------------------------------------*/
338 sal_Bool SwUserFieldType::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
339 {
340     switch( nWhichId )
341 	{
342 	case FIELD_PROP_DOUBLE:
343 		{
344 			double fVal = 0;
345 			rAny >>= fVal;
346 			nValue = fVal;
347 
348 			// Folgende Zeile ist eigentlich falsch, da die Sprache unbekannt ist
349 			// (haengt am Feld) und aContent daher auch eigentlich ans Feld gehoeren
350 			// muesste. Jedes Feld kann eine andere Sprache, aber den gleichen Inhalt
351 			// haben, nur die Formatierung ist unterschiedlich.
352 			DoubleToString(aContent, nValue, (sal_uInt16)LANGUAGE_SYSTEM);
353 		}
354 		break;
355 	case FIELD_PROP_PAR2:
356 		::GetString( rAny, aContent );
357 		break;
358 	case FIELD_PROP_BOOL1:
359 		if(*(sal_Bool*)rAny.getValue())
360         {
361 			nType |= nsSwGetSetExpType::GSE_EXPR;
362             nType &= ~nsSwGetSetExpType::GSE_STRING;
363         }
364 		else
365         {
366 			nType &= ~nsSwGetSetExpType::GSE_EXPR;
367 			nType |= nsSwGetSetExpType::GSE_STRING;
368         }
369 		break;
370 	default:
371 		DBG_ERROR("illegal property");
372 	}
373 	return sal_True;
374 }
375 
376 
377 
378