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 <svl/zforlist.hxx>
28 #include <svl/zformat.hxx>
29
30 #include <svx/svdmodel.hxx>
31
32 #include <calbck.hxx>
33 #include <calc.hxx>
34 #include <usrfld.hxx>
35 #include <doc.hxx>
36 #include <IDocumentUndoRedo.hxx>
37 #include <editsh.hxx>
38 #include <dpage.hxx>
39 #include <unofldmid.h>
40
41
42 using namespace ::com::sun::star;
43 using ::rtl::OUString;
44
45 /*--------------------------------------------------------------------
46 Beschreibung: Benutzerfelder
47 --------------------------------------------------------------------*/
48
SwUserField(SwUserFieldType * pTyp,sal_uInt16 nSub,sal_uInt32 nFmt)49 SwUserField::SwUserField(SwUserFieldType* pTyp, sal_uInt16 nSub, sal_uInt32 nFmt)
50 : SwValueField(pTyp, nFmt),
51 nSubType(nSub)
52 {
53 }
54
Expand() const55 String SwUserField::Expand() const
56 {
57 String sStr;
58 if(!(nSubType & nsSwExtendedSubType::SUB_INVISIBLE))
59 sStr = ((SwUserFieldType*)GetTyp())->Expand(GetFormat(), nSubType, GetLanguage());
60
61 return sStr;
62 }
63
Copy() const64 SwField* SwUserField::Copy() const
65 {
66 SwField* pTmp = new SwUserField((SwUserFieldType*)GetTyp(), nSubType, GetFormat());
67 pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
68 return pTmp;
69 }
70
GetFieldName() const71 String SwUserField::GetFieldName() const
72 {
73 String aStr(SwFieldType::GetTypeStr(TYP_USERFLD));
74 aStr += ' ';
75 aStr += GetTyp()->GetName();
76 aStr.AppendAscii(" = ");
77 aStr += static_cast<SwUserFieldType*>(GetTyp())->GetContent();
78 return aStr;
79 }
80
GetValue() const81 double SwUserField::GetValue() const
82 {
83 return ((SwUserFieldType*)GetTyp())->GetValue();
84 }
85
SetValue(const double & rVal)86 void SwUserField::SetValue( const double& rVal )
87 {
88 ((SwUserFieldType*)GetTyp())->SetValue(rVal);
89 }
90
91 /*--------------------------------------------------------------------
92 Beschreibung: Name
93 --------------------------------------------------------------------*/
94
GetPar1() const95 const String& SwUserField::GetPar1() const
96 {
97 return ((SwUserFieldType*)GetTyp())->GetName();
98 }
99
100 /*--------------------------------------------------------------------
101 Beschreibung: Content
102 --------------------------------------------------------------------*/
103
GetPar2() const104 String SwUserField::GetPar2() const
105 {
106 return ((SwUserFieldType*)GetTyp())->GetContent(GetFormat());
107 }
108
SetPar2(const String & rStr)109 void SwUserField::SetPar2(const String& rStr)
110 {
111 ((SwUserFieldType*)GetTyp())->SetContent(rStr, GetFormat());
112 }
113
GetSubType() const114 sal_uInt16 SwUserField::GetSubType() const
115 {
116 return ((SwUserFieldType*)GetTyp())->GetType() | nSubType;
117 }
118
SetSubType(sal_uInt16 nSub)119 void SwUserField::SetSubType(sal_uInt16 nSub)
120 {
121 ((SwUserFieldType*)GetTyp())->SetType(nSub & 0x00ff);
122 nSubType = nSub & 0xff00;
123 }
124
125 /*-----------------09.03.98 08:04-------------------
126
127 --------------------------------------------------*/
QueryValue(uno::Any & rAny,sal_uInt16 nWhichId) const128 sal_Bool SwUserField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
129 {
130 switch( nWhichId )
131 {
132 case FIELD_PROP_BOOL2:
133 {
134 sal_Bool bTmp = 0 != (nSubType & nsSwExtendedSubType::SUB_CMD);
135 rAny.setValue(&bTmp, ::getBooleanCppuType());
136 }
137 break;
138 case FIELD_PROP_BOOL1:
139 {
140 sal_Bool bTmp = 0 == (nSubType & nsSwExtendedSubType::SUB_INVISIBLE);
141 rAny.setValue(&bTmp, ::getBooleanCppuType());
142 }
143 break;
144 case FIELD_PROP_FORMAT:
145 rAny <<= (sal_Int32)GetFormat();
146 break;
147 default:
148 return SwField::QueryValue(rAny, nWhichId);
149 }
150 return sal_True;
151 }
152 /*-----------------09.03.98 08:04-------------------
153
154 --------------------------------------------------*/
PutValue(const uno::Any & rAny,sal_uInt16 nWhichId)155 sal_Bool SwUserField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
156 {
157 switch( nWhichId )
158 {
159 case FIELD_PROP_BOOL1:
160 if(*(sal_Bool*) rAny.getValue())
161 nSubType &= (~nsSwExtendedSubType::SUB_INVISIBLE);
162 else
163 nSubType |= nsSwExtendedSubType::SUB_INVISIBLE;
164 break;
165 case FIELD_PROP_BOOL2:
166 if(*(sal_Bool*) rAny.getValue())
167 nSubType |= nsSwExtendedSubType::SUB_CMD;
168 else
169 nSubType &= (~nsSwExtendedSubType::SUB_CMD);
170 break;
171 case FIELD_PROP_FORMAT:
172 {
173 sal_Int32 nTmp = 0;
174 rAny >>= nTmp;
175 SetFormat(nTmp);
176 }
177 break;
178 default:
179 return SwField::PutValue(rAny, nWhichId);
180 }
181 return sal_True;
182 }
183
184 /*--------------------------------------------------------------------
185 Beschreibung: Benutzerfeldtypen
186 --------------------------------------------------------------------*/
187
SwUserFieldType(SwDoc * pDocPtr,const String & aNam)188 SwUserFieldType::SwUserFieldType( SwDoc* pDocPtr, const String& aNam )
189 : SwValueFieldType( pDocPtr, RES_USERFLD ),
190 nValue( 0 ),
191 nType(nsSwGetSetExpType::GSE_STRING)
192 {
193 bValidValue = bDeleted = sal_False;
194 aName = aNam;
195
196 if (nType & nsSwGetSetExpType::GSE_STRING)
197 EnableFormat(sal_False); // Numberformatter nicht einsetzen
198 }
199
Expand(sal_uInt32 nFmt,sal_uInt16 nSubType,sal_uInt16 nLng)200 String SwUserFieldType::Expand(sal_uInt32 nFmt, sal_uInt16 nSubType, sal_uInt16 nLng)
201 {
202 String aStr(aContent);
203 if((nType & nsSwGetSetExpType::GSE_EXPR) && !(nSubType & nsSwExtendedSubType::SUB_CMD))
204 {
205 EnableFormat(sal_True);
206 aStr = ExpandValue(nValue, nFmt, nLng);
207 }
208 else
209 EnableFormat(sal_False); // Numberformatter nicht einsetzen
210
211 return aStr;
212 }
213
Copy() const214 SwFieldType* SwUserFieldType::Copy() const
215 {
216 SwUserFieldType *pTmp = new SwUserFieldType( GetDoc(), aName );
217 pTmp->aContent = aContent;
218 pTmp->nType = nType;
219 pTmp->bValidValue = bValidValue;
220 pTmp->nValue = nValue;
221 pTmp->bDeleted = bDeleted;
222
223 return pTmp;
224 }
225
GetName() const226 const String& SwUserFieldType::GetName() const
227 {
228 return aName;
229 }
230
Modify(const SfxPoolItem * pOld,const SfxPoolItem * pNew)231 void SwUserFieldType::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew )
232 {
233 if( !pOld && !pNew )
234 ChgValid( sal_False );
235
236 NotifyClients( pOld, pNew );
237
238 // update Input Fields as there might be Input Fields depending on this User Field
239 if ( !IsModifyLocked() )
240 {
241 LockModify();
242 GetDoc()->GetSysFldType( RES_INPUTFLD )->UpdateFlds();
243 UnlockModify();
244 }
245 }
246
GetValue(SwCalc & rCalc)247 double SwUserFieldType::GetValue( SwCalc& rCalc )
248 {
249 if(bValidValue)
250 return nValue;
251
252 if(!rCalc.Push( this ))
253 {
254 rCalc.SetCalcError( CALC_SYNTAX );
255 return 0;
256 }
257 nValue = rCalc.Calculate( aContent ).GetDouble();
258 rCalc.Pop( this );
259
260 if( !rCalc.IsCalcError() )
261 bValidValue = sal_True;
262 else
263 nValue = 0;
264
265 return nValue;
266 }
267
GetContent(sal_uInt32 nFmt)268 String SwUserFieldType::GetContent( sal_uInt32 nFmt )
269 {
270 if (nFmt && nFmt != SAL_MAX_UINT32)
271 {
272 String sFormattedValue;
273 Color* pCol = 0;
274
275 SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
276
277 pFormatter->GetOutputString(GetValue(), nFmt, sFormattedValue, &pCol);
278 return sFormattedValue;
279 }
280 else
281 return aContent;
282 }
283
SetContent(const String & rStr,sal_uInt32 nFmt)284 void SwUserFieldType::SetContent( const String& rStr, sal_uInt32 nFmt )
285 {
286 if( aContent != rStr )
287 {
288 aContent = rStr;
289
290 if (nFmt && nFmt != SAL_MAX_UINT32)
291 {
292 double fValue;
293
294 SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
295
296 if (pFormatter->IsNumberFormat(rStr, nFmt, fValue))
297 {
298 SetValue(fValue);
299 aContent.Erase();
300 DoubleToString(aContent, fValue, nFmt);
301 }
302 }
303
304 sal_Bool bModified = GetDoc()->IsModified();
305 GetDoc()->SetModified();
306 if( !bModified ) // Bug 57028
307 {
308 GetDoc()->GetIDocumentUndoRedo().SetUndoNoResetModified();
309 }
310 }
311 }
312
313 /*-----------------04.03.98 17:05-------------------
314
315 --------------------------------------------------*/
QueryValue(uno::Any & rAny,sal_uInt16 nWhichId) const316 sal_Bool SwUserFieldType::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
317 {
318 switch( nWhichId )
319 {
320 case FIELD_PROP_DOUBLE:
321 rAny <<= (double) nValue;
322 break;
323 case FIELD_PROP_PAR2:
324 rAny <<= rtl::OUString(aContent);
325 break;
326 case FIELD_PROP_BOOL1:
327 {
328 sal_Bool bExpression = 0 != (nsSwGetSetExpType::GSE_EXPR&nType);
329 rAny.setValue(&bExpression, ::getBooleanCppuType());
330 }
331 break;
332 default:
333 DBG_ERROR("illegal property");
334 }
335 return sal_True;
336 }
337 /*-----------------04.03.98 17:05-------------------
338
339 --------------------------------------------------*/
PutValue(const uno::Any & rAny,sal_uInt16 nWhichId)340 sal_Bool SwUserFieldType::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
341 {
342 switch( nWhichId )
343 {
344 case FIELD_PROP_DOUBLE:
345 {
346 double fVal = 0;
347 rAny >>= fVal;
348 nValue = fVal;
349
350 // Folgende Zeile ist eigentlich falsch, da die Sprache unbekannt ist
351 // (haengt am Feld) und aContent daher auch eigentlich ans Feld gehoeren
352 // muesste. Jedes Feld kann eine andere Sprache, aber den gleichen Inhalt
353 // haben, nur die Formatierung ist unterschiedlich.
354 DoubleToString(aContent, nValue, (sal_uInt16)LANGUAGE_SYSTEM);
355 }
356 break;
357 case FIELD_PROP_PAR2:
358 ::GetString( rAny, aContent );
359 break;
360 case FIELD_PROP_BOOL1:
361 if(*(sal_Bool*)rAny.getValue())
362 {
363 nType |= nsSwGetSetExpType::GSE_EXPR;
364 nType &= ~nsSwGetSetExpType::GSE_STRING;
365 }
366 else
367 {
368 nType &= ~nsSwGetSetExpType::GSE_EXPR;
369 nType |= nsSwGetSetExpType::GSE_STRING;
370 }
371 break;
372 default:
373 DBG_ERROR("illegal property");
374 }
375 return sal_True;
376 }
377
378
379
380