xref: /AOO41X/main/sw/source/core/fields/usrfld.cxx (revision 8809db7a87f97847b57a57f4cd2b0104b2b83182)
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 
49 SwUserField::SwUserField(SwUserFieldType* pTyp, sal_uInt16 nSub, sal_uInt32 nFmt)
50     : SwValueField(pTyp, nFmt),
51     nSubType(nSub)
52 {
53 }
54 
55 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 
64 SwField* SwUserField::Copy() const
65 {
66     SwField* pTmp = new SwUserField((SwUserFieldType*)GetTyp(), nSubType, GetFormat());
67     pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
68     return pTmp;
69 }
70 
71 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 
81 double SwUserField::GetValue() const
82 {
83     return ((SwUserFieldType*)GetTyp())->GetValue();
84 }
85 
86 void SwUserField::SetValue( const double& rVal )
87 {
88     ((SwUserFieldType*)GetTyp())->SetValue(rVal);
89 }
90 
91 /*--------------------------------------------------------------------
92     Beschreibung: Name
93  --------------------------------------------------------------------*/
94 
95 const String& SwUserField::GetPar1() const
96 {
97     return ((SwUserFieldType*)GetTyp())->GetName();
98 }
99 
100 /*--------------------------------------------------------------------
101     Beschreibung: Content
102  --------------------------------------------------------------------*/
103 
104 String SwUserField::GetPar2() const
105 {
106     return ((SwUserFieldType*)GetTyp())->GetContent(GetFormat());
107 }
108 
109 void SwUserField::SetPar2(const String& rStr)
110 {
111     ((SwUserFieldType*)GetTyp())->SetContent(rStr, GetFormat());
112 }
113 
114 sal_uInt16 SwUserField::GetSubType() const
115 {
116     return ((SwUserFieldType*)GetTyp())->GetType() | nSubType;
117 }
118 
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 --------------------------------------------------*/
128 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 --------------------------------------------------*/
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 
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 
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 
214 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 
226 const String& SwUserFieldType::GetName() const
227 {
228     return aName;
229 }
230 
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     // und ggfs. am UserFeld haengende InputFelder updaten!
238     GetDoc()->GetSysFldType( RES_INPUTFLD )->UpdateFlds();
239 }
240 
241 double SwUserFieldType::GetValue( SwCalc& rCalc )
242 {
243     if(bValidValue)
244         return nValue;
245 
246     if(!rCalc.Push( this ))
247     {
248         rCalc.SetCalcError( CALC_SYNTAX );
249         return 0;
250     }
251     nValue = rCalc.Calculate( aContent ).GetDouble();
252     rCalc.Pop( this );
253 
254     if( !rCalc.IsCalcError() )
255         bValidValue = sal_True;
256     else
257         nValue = 0;
258 
259     return nValue;
260 }
261 
262 String SwUserFieldType::GetContent( sal_uInt32 nFmt )
263 {
264     if (nFmt && nFmt != SAL_MAX_UINT32)
265     {
266         String sFormattedValue;
267         Color* pCol = 0;
268 
269         SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
270 
271         pFormatter->GetOutputString(GetValue(), nFmt, sFormattedValue, &pCol);
272         return sFormattedValue;
273     }
274     else
275         return aContent;
276 }
277 
278 void SwUserFieldType::SetContent( const String& rStr, sal_uInt32 nFmt )
279 {
280     if( aContent != rStr )
281     {
282         aContent = rStr;
283 
284         if (nFmt && nFmt != SAL_MAX_UINT32)
285         {
286             double fValue;
287 
288             SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
289 
290             if (pFormatter->IsNumberFormat(rStr, nFmt, fValue))
291             {
292                 SetValue(fValue);
293                 aContent.Erase();
294                 DoubleToString(aContent, fValue, nFmt);
295             }
296         }
297 
298         sal_Bool bModified = GetDoc()->IsModified();
299         GetDoc()->SetModified();
300         if( !bModified )    // Bug 57028
301         {
302             GetDoc()->GetIDocumentUndoRedo().SetUndoNoResetModified();
303         }
304     }
305 }
306 
307 /*-----------------04.03.98 17:05-------------------
308 
309 --------------------------------------------------*/
310 sal_Bool SwUserFieldType::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
311 {
312     switch( nWhichId )
313     {
314     case FIELD_PROP_DOUBLE:
315         rAny <<= (double) nValue;
316         break;
317     case FIELD_PROP_PAR2:
318         rAny <<= rtl::OUString(aContent);
319         break;
320     case FIELD_PROP_BOOL1:
321         {
322             sal_Bool bExpression = 0 != (nsSwGetSetExpType::GSE_EXPR&nType);
323             rAny.setValue(&bExpression, ::getBooleanCppuType());
324         }
325         break;
326     default:
327         DBG_ERROR("illegal property");
328     }
329     return sal_True;
330 }
331 /*-----------------04.03.98 17:05-------------------
332 
333 --------------------------------------------------*/
334 sal_Bool SwUserFieldType::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
335 {
336     switch( nWhichId )
337     {
338     case FIELD_PROP_DOUBLE:
339         {
340             double fVal = 0;
341             rAny >>= fVal;
342             nValue = fVal;
343 
344             // Folgende Zeile ist eigentlich falsch, da die Sprache unbekannt ist
345             // (haengt am Feld) und aContent daher auch eigentlich ans Feld gehoeren
346             // muesste. Jedes Feld kann eine andere Sprache, aber den gleichen Inhalt
347             // haben, nur die Formatierung ist unterschiedlich.
348             DoubleToString(aContent, nValue, (sal_uInt16)LANGUAGE_SYSTEM);
349         }
350         break;
351     case FIELD_PROP_PAR2:
352         ::GetString( rAny, aContent );
353         break;
354     case FIELD_PROP_BOOL1:
355         if(*(sal_Bool*)rAny.getValue())
356         {
357             nType |= nsSwGetSetExpType::GSE_EXPR;
358             nType &= ~nsSwGetSetExpType::GSE_STRING;
359         }
360         else
361         {
362             nType &= ~nsSwGetSetExpType::GSE_EXPR;
363             nType |= nsSwGetSetExpType::GSE_STRING;
364         }
365         break;
366     default:
367         DBG_ERROR("illegal property");
368     }
369     return sal_True;
370 }
371 
372 
373 
374