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