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 0 : SwUserField::SwUserField(SwUserFieldType* pTyp, sal_uInt16 nSub, sal_uInt32 nFmt)
39 : : SwValueField(pTyp, nFmt),
40 0 : nSubType(nSub)
41 : {
42 0 : }
43 :
44 0 : String SwUserField::Expand() const
45 : {
46 0 : String sStr;
47 0 : if(!(nSubType & nsSwExtendedSubType::SUB_INVISIBLE))
48 0 : sStr = ((SwUserFieldType*)GetTyp())->Expand(GetFormat(), nSubType, GetLanguage());
49 :
50 0 : return sStr;
51 : }
52 :
53 0 : SwField* SwUserField::Copy() const
54 : {
55 0 : SwField* pTmp = new SwUserField((SwUserFieldType*)GetTyp(), nSubType, GetFormat());
56 0 : pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
57 0 : return pTmp;
58 : }
59 :
60 0 : String SwUserField::GetFieldName() const
61 : {
62 0 : String aStr(SwFieldType::GetTypeStr(TYP_USERFLD));
63 0 : aStr += ' ';
64 0 : aStr += GetTyp()->GetName();
65 0 : aStr.AppendAscii(" = ");
66 0 : aStr += static_cast<SwUserFieldType*>(GetTyp())->GetContent();
67 0 : return aStr;
68 : }
69 :
70 0 : double SwUserField::GetValue() const
71 : {
72 0 : return ((SwUserFieldType*)GetTyp())->GetValue();
73 : }
74 :
75 0 : void SwUserField::SetValue( const double& rVal )
76 : {
77 0 : ((SwUserFieldType*)GetTyp())->SetValue(rVal);
78 0 : }
79 :
80 : /// Get name
81 0 : const OUString& SwUserField::GetPar1() const
82 : {
83 0 : return ((const SwUserFieldType*)GetTyp())->GetName();
84 : }
85 :
86 : /// Get content
87 0 : OUString SwUserField::GetPar2() const
88 : {
89 0 : return ((SwUserFieldType*)GetTyp())->GetContent(GetFormat());
90 : }
91 :
92 0 : void SwUserField::SetPar2(const OUString& rStr)
93 : {
94 0 : ((SwUserFieldType*)GetTyp())->SetContent(rStr, GetFormat());
95 0 : }
96 :
97 0 : sal_uInt16 SwUserField::GetSubType() const
98 : {
99 0 : return ((SwUserFieldType*)GetTyp())->GetType() | nSubType;
100 : }
101 :
102 0 : void SwUserField::SetSubType(sal_uInt16 nSub)
103 : {
104 0 : ((SwUserFieldType*)GetTyp())->SetType(nSub & 0x00ff);
105 0 : nSubType = nSub & 0xff00;
106 0 : }
107 :
108 0 : bool SwUserField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
109 : {
110 0 : switch( nWhichId )
111 : {
112 : case FIELD_PROP_BOOL2:
113 : {
114 0 : sal_Bool bTmp = 0 != (nSubType & nsSwExtendedSubType::SUB_CMD);
115 0 : rAny.setValue(&bTmp, ::getBooleanCppuType());
116 : }
117 0 : break;
118 : case FIELD_PROP_BOOL1:
119 : {
120 0 : sal_Bool bTmp = 0 == (nSubType & nsSwExtendedSubType::SUB_INVISIBLE);
121 0 : rAny.setValue(&bTmp, ::getBooleanCppuType());
122 : }
123 0 : break;
124 : case FIELD_PROP_FORMAT:
125 0 : rAny <<= (sal_Int32)GetFormat();
126 0 : break;
127 : default:
128 0 : return SwField::QueryValue(rAny, nWhichId);
129 : }
130 0 : return true;
131 : }
132 :
133 0 : bool SwUserField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
134 : {
135 0 : switch( nWhichId )
136 : {
137 : case FIELD_PROP_BOOL1:
138 0 : if(*(sal_Bool*) rAny.getValue())
139 0 : nSubType &= (~nsSwExtendedSubType::SUB_INVISIBLE);
140 : else
141 0 : nSubType |= nsSwExtendedSubType::SUB_INVISIBLE;
142 0 : break;
143 : case FIELD_PROP_BOOL2:
144 0 : if(*(sal_Bool*) rAny.getValue())
145 0 : nSubType |= nsSwExtendedSubType::SUB_CMD;
146 : else
147 0 : nSubType &= (~nsSwExtendedSubType::SUB_CMD);
148 0 : break;
149 : case FIELD_PROP_FORMAT:
150 : {
151 0 : sal_Int32 nTmp = 0;
152 0 : rAny >>= nTmp;
153 0 : SetFormat(nTmp);
154 : }
155 0 : break;
156 : default:
157 0 : return SwField::PutValue(rAny, nWhichId);
158 : }
159 0 : return true;
160 : }
161 :
162 44 : SwUserFieldType::SwUserFieldType( SwDoc* pDocPtr, const String& aNam )
163 : : SwValueFieldType( pDocPtr, RES_USERFLD ),
164 : nValue( 0 ),
165 44 : nType(nsSwGetSetExpType::GSE_STRING)
166 : {
167 44 : bValidValue = bDeleted = false;
168 44 : aName = aNam;
169 :
170 44 : if (nType & nsSwGetSetExpType::GSE_STRING)
171 44 : EnableFormat(sal_False); // Do not use a Numberformatter
172 44 : }
173 :
174 1 : String SwUserFieldType::Expand(sal_uInt32 nFmt, sal_uInt16 nSubType, sal_uInt16 nLng)
175 : {
176 1 : String aStr(aContent);
177 1 : if((nType & nsSwGetSetExpType::GSE_EXPR) && !(nSubType & nsSwExtendedSubType::SUB_CMD))
178 : {
179 0 : EnableFormat(sal_True);
180 0 : aStr = ExpandValue(nValue, nFmt, nLng);
181 : }
182 : else
183 1 : EnableFormat(sal_False); // Do not use a Numberformatter
184 :
185 1 : return aStr;
186 : }
187 :
188 22 : SwFieldType* SwUserFieldType::Copy() const
189 : {
190 22 : SwUserFieldType *pTmp = new SwUserFieldType( GetDoc(), aName );
191 22 : pTmp->aContent = aContent;
192 22 : pTmp->nType = nType;
193 22 : pTmp->bValidValue = bValidValue;
194 22 : pTmp->nValue = nValue;
195 22 : pTmp->bDeleted = bDeleted;
196 :
197 22 : return pTmp;
198 : }
199 :
200 494 : const OUString& SwUserFieldType::GetName() const
201 : {
202 494 : return aName;
203 : }
204 :
205 1 : void SwUserFieldType::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew )
206 : {
207 1 : if( !pOld && !pNew )
208 1 : ChgValid( sal_False );
209 :
210 1 : NotifyClients( pOld, pNew );
211 : // update input fields that might be connected to the user field
212 1 : GetDoc()->GetSysFldType( RES_INPUTFLD )->UpdateFlds();
213 1 : }
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 2 : String SwUserFieldType::GetContent( sal_uInt32 nFmt )
237 : {
238 2 : 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 : else
249 2 : return aContent;
250 : }
251 :
252 22 : void SwUserFieldType::SetContent( const String& rStr, sal_uInt32 nFmt )
253 : {
254 22 : 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.Erase();
268 0 : DoubleToString(aContent, fValue, nFmt);
269 : }
270 : }
271 :
272 0 : sal_Bool bModified = GetDoc()->IsModified();
273 0 : GetDoc()->SetModified();
274 0 : if( !bModified ) // Bug 57028
275 : {
276 0 : GetDoc()->GetIDocumentUndoRedo().SetUndoNoResetModified();
277 : }
278 : }
279 22 : }
280 :
281 0 : bool SwUserFieldType::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
282 : {
283 0 : switch( nWhichId )
284 : {
285 : case FIELD_PROP_DOUBLE:
286 0 : rAny <<= (double) nValue;
287 0 : break;
288 : case FIELD_PROP_PAR2:
289 0 : rAny <<= OUString(aContent);
290 0 : break;
291 : case FIELD_PROP_BOOL1:
292 : {
293 0 : sal_Bool bExpression = 0 != (nsSwGetSetExpType::GSE_EXPR&nType);
294 0 : rAny.setValue(&bExpression, ::getBooleanCppuType());
295 : }
296 0 : break;
297 : default:
298 : OSL_FAIL("illegal property");
299 : }
300 0 : return true;
301 : }
302 :
303 45 : bool SwUserFieldType::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
304 : {
305 45 : switch( nWhichId )
306 : {
307 : case FIELD_PROP_DOUBLE:
308 : {
309 0 : double fVal = 0;
310 0 : rAny >>= fVal;
311 0 : nValue = fVal;
312 :
313 : // The following line is in fact wrong, since the language is unknown (is part of the
314 : // field) and, thus, aContent should also belong to the field. Each field can have a
315 : // differnt language, but the same content with just different formatting.
316 0 : DoubleToString(aContent, nValue, (sal_uInt16)LANGUAGE_SYSTEM);
317 : }
318 0 : break;
319 : case FIELD_PROP_PAR2:
320 23 : ::GetString( rAny, aContent );
321 23 : break;
322 : case FIELD_PROP_BOOL1:
323 22 : if(*(sal_Bool*)rAny.getValue())
324 : {
325 0 : nType |= nsSwGetSetExpType::GSE_EXPR;
326 0 : nType &= ~nsSwGetSetExpType::GSE_STRING;
327 : }
328 : else
329 : {
330 22 : nType &= ~nsSwGetSetExpType::GSE_EXPR;
331 22 : nType |= nsSwGetSetExpType::GSE_STRING;
332 : }
333 22 : break;
334 : default:
335 : OSL_FAIL("illegal property");
336 : }
337 45 : return true;
338 99 : }
339 :
340 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|