xref: /AOO41X/main/sw/source/ui/vba/vbafield.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #include "vbafield.hxx"
28*cdf0e10cSrcweir #include "vbarange.hxx"
29*cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
30*cdf0e10cSrcweir #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/view/XSelectionSupplier.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/text/XTextFieldsSupplier.hpp>
33*cdf0e10cSrcweir #include <ooo/vba/word/WdFieldType.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/text/FilenameDisplayFormat.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/util/XRefreshable.hpp>
36*cdf0e10cSrcweir #include <swtypes.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir using namespace ::ooo::vba;
39*cdf0e10cSrcweir using namespace ::com::sun::star;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir // *** SwVbaField ***********************************************
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir SwVbaField::SwVbaField(  const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, const css::uno::Reference< css::text::XTextDocument >& rDocument, const  uno::Reference< css::text::XTextField >& xTextField) throw ( uno::RuntimeException ) : SwVbaField_BASE( rParent, rContext ), mxTextDocument( rDocument )
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir     mxTextField.set( xTextField, uno::UNO_QUERY_THROW );
46*cdf0e10cSrcweir }
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir // XHelperInterface
49*cdf0e10cSrcweir rtl::OUString&
50*cdf0e10cSrcweir SwVbaField::getServiceImplName()
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir     static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaField") );
53*cdf0e10cSrcweir     return sImplName;
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir uno::Sequence<rtl::OUString>
57*cdf0e10cSrcweir SwVbaField::getServiceNames()
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir     static uno::Sequence< rtl::OUString > aServiceNames;
60*cdf0e10cSrcweir     if ( aServiceNames.getLength() == 0 )
61*cdf0e10cSrcweir     {
62*cdf0e10cSrcweir         aServiceNames.realloc( 1 );
63*cdf0e10cSrcweir         aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Field" ) );
64*cdf0e10cSrcweir     }
65*cdf0e10cSrcweir     return aServiceNames;
66*cdf0e10cSrcweir }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir // *** _ReadFieldParams ***********************************************
69*cdf0e10cSrcweir // the codes are copied from ww8par5.cxx
70*cdf0e10cSrcweir class _ReadFieldParams
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir private:
73*cdf0e10cSrcweir     String aData;
74*cdf0e10cSrcweir     xub_StrLen nLen, nFnd, nNext, nSavPtr;
75*cdf0e10cSrcweir     String aFieldName;
76*cdf0e10cSrcweir public:
77*cdf0e10cSrcweir     _ReadFieldParams( const String& rData );
78*cdf0e10cSrcweir     ~_ReadFieldParams();
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     xub_StrLen GoToTokenParam();
81*cdf0e10cSrcweir     long SkipToNextToken();
82*cdf0e10cSrcweir     xub_StrLen GetTokenSttPtr() const   { return nFnd;  }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     xub_StrLen FindNextStringPiece( xub_StrLen _nStart = STRING_NOTFOUND );
85*cdf0e10cSrcweir     bool GetTokenSttFromTo(xub_StrLen* _pFrom, xub_StrLen* _pTo,
86*cdf0e10cSrcweir         xub_StrLen _nMax);
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     String GetResult() const;
89*cdf0e10cSrcweir     String GetFieldName()const { return aFieldName; }
90*cdf0e10cSrcweir };
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir _ReadFieldParams::_ReadFieldParams( const String& _rData )
94*cdf0e10cSrcweir     : aData( _rData ), nLen( _rData.Len() ), nNext( 0 )
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir     /*
97*cdf0e10cSrcweir         erstmal nach einer oeffnenden Klammer oder einer Leerstelle oder einem
98*cdf0e10cSrcweir         Anfuehrungszeichen oder einem Backslash suchen, damit der Feldbefehl
99*cdf0e10cSrcweir         (also INCLUDEPICTURE bzw EINFUeGENGRAFIK bzw ...) ueberlesen wird
100*cdf0e10cSrcweir     */
101*cdf0e10cSrcweir     while( (nLen > nNext) && (aData.GetChar( nNext ) == ' ') )
102*cdf0e10cSrcweir         ++nNext;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     sal_Unicode c;
105*cdf0e10cSrcweir     while(     nLen > nNext
106*cdf0e10cSrcweir             && (c = aData.GetChar( nNext )) != ' '
107*cdf0e10cSrcweir             && c != '"'
108*cdf0e10cSrcweir             && c != '\\'
109*cdf0e10cSrcweir             && c != 132
110*cdf0e10cSrcweir             && c != 0x201c )
111*cdf0e10cSrcweir         ++nNext;
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir     nFnd      = nNext;
114*cdf0e10cSrcweir     nSavPtr   = nNext;
115*cdf0e10cSrcweir     aFieldName = aData.Copy( 0, nFnd );
116*cdf0e10cSrcweir //  cLastChar = aData.GetChar( nSavPtr );
117*cdf0e10cSrcweir }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir _ReadFieldParams::~_ReadFieldParams()
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir //  aData.SetChar( nSavPtr, cLastChar );
123*cdf0e10cSrcweir }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir String _ReadFieldParams::GetResult() const
127*cdf0e10cSrcweir {
128*cdf0e10cSrcweir     return    (STRING_NOTFOUND == nFnd)
129*cdf0e10cSrcweir             ? aEmptyStr
130*cdf0e10cSrcweir             : aData.Copy( nFnd, (nSavPtr - nFnd) );
131*cdf0e10cSrcweir }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir xub_StrLen _ReadFieldParams::GoToTokenParam()
135*cdf0e10cSrcweir {
136*cdf0e10cSrcweir     xub_StrLen nOld = nNext;
137*cdf0e10cSrcweir     if( -2 == SkipToNextToken() )
138*cdf0e10cSrcweir         return GetTokenSttPtr();
139*cdf0e10cSrcweir     nNext = nOld;
140*cdf0e10cSrcweir     return STRING_NOTFOUND;
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir // ret: -2: NOT a '\' parameter but normal Text
144*cdf0e10cSrcweir long _ReadFieldParams::SkipToNextToken()
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir     long nRet = -1;     // Ende
147*cdf0e10cSrcweir     if (
148*cdf0e10cSrcweir          (STRING_NOTFOUND != nNext) && (nLen > nNext) &&
149*cdf0e10cSrcweir          STRING_NOTFOUND != (nFnd = FindNextStringPiece(nNext))
150*cdf0e10cSrcweir        )
151*cdf0e10cSrcweir     {
152*cdf0e10cSrcweir         nSavPtr = nNext;
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir         if ('\\' == aData.GetChar(nFnd) && '\\' != aData.GetChar(nFnd + 1))
155*cdf0e10cSrcweir         {
156*cdf0e10cSrcweir             nRet = aData.GetChar(++nFnd);
157*cdf0e10cSrcweir             nNext = ++nFnd;             // und dahinter setzen
158*cdf0e10cSrcweir         }
159*cdf0e10cSrcweir         else
160*cdf0e10cSrcweir         {
161*cdf0e10cSrcweir             nRet = -2;
162*cdf0e10cSrcweir             if (
163*cdf0e10cSrcweir                  (STRING_NOTFOUND != nSavPtr ) &&
164*cdf0e10cSrcweir                  (
165*cdf0e10cSrcweir                    ('"' == aData.GetChar(nSavPtr - 1)) ||
166*cdf0e10cSrcweir                    (0x201d == aData.GetChar(nSavPtr - 1))
167*cdf0e10cSrcweir                  )
168*cdf0e10cSrcweir                )
169*cdf0e10cSrcweir             {
170*cdf0e10cSrcweir                 --nSavPtr;
171*cdf0e10cSrcweir             }
172*cdf0e10cSrcweir         }
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir     return nRet;
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir // FindNextPara sucht naechsten Backslash-Parameter oder naechste Zeichenkette
178*cdf0e10cSrcweir // bis zum Blank oder naechsten "\" oder zum schliessenden Anfuehrungszeichen
179*cdf0e10cSrcweir // oder zum String-Ende von pStr.
180*cdf0e10cSrcweir //
181*cdf0e10cSrcweir // Ausgabe ppNext (falls ppNext != 0) Suchbeginn fuer naechsten Parameter bzw. 0
182*cdf0e10cSrcweir //
183*cdf0e10cSrcweir // Returnwert: 0 falls String-Ende erreicht,
184*cdf0e10cSrcweir //             ansonsten Anfang des Paramters bzw. der Zeichenkette
185*cdf0e10cSrcweir //
186*cdf0e10cSrcweir xub_StrLen _ReadFieldParams::FindNextStringPiece(const xub_StrLen nStart)
187*cdf0e10cSrcweir {
188*cdf0e10cSrcweir     xub_StrLen  n = ( STRING_NOTFOUND == nStart ) ? nFnd : nStart;  // Anfang
189*cdf0e10cSrcweir     xub_StrLen n2;          // Ende
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     nNext = STRING_NOTFOUND;        // Default fuer nicht gefunden
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir     while( (nLen > n) && (aData.GetChar( n ) == ' ') )
194*cdf0e10cSrcweir         ++n;
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir     if( nLen == n )
197*cdf0e10cSrcweir         return STRING_NOTFOUND;     // String End reached!
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir     if(     (aData.GetChar( n ) == '"')     // Anfuehrungszeichen vor Para?
200*cdf0e10cSrcweir         ||  (aData.GetChar( n ) == 0x201c)
201*cdf0e10cSrcweir         ||  (aData.GetChar( n ) == 132) )
202*cdf0e10cSrcweir     {
203*cdf0e10cSrcweir         n++;                        // Anfuehrungszeichen ueberlesen
204*cdf0e10cSrcweir         n2 = n;                     // ab hier nach Ende suchen
205*cdf0e10cSrcweir         while(     (nLen > n2)
206*cdf0e10cSrcweir                 && (aData.GetChar( n2 ) != '"')
207*cdf0e10cSrcweir                 && (aData.GetChar( n2 ) != 0x201d)
208*cdf0e10cSrcweir                 && (aData.GetChar( n2 ) != 147) )
209*cdf0e10cSrcweir             n2++;                   // Ende d. Paras suchen
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir     else                        // keine Anfuehrungszeichen
212*cdf0e10cSrcweir     {
213*cdf0e10cSrcweir         n2 = n;                     // ab hier nach Ende suchen
214*cdf0e10cSrcweir         while( (nLen > n2) && (aData.GetChar( n2 ) != ' ') ) // Ende d. Paras suchen
215*cdf0e10cSrcweir         {
216*cdf0e10cSrcweir             if( aData.GetChar( n2 ) == '\\' )
217*cdf0e10cSrcweir             {
218*cdf0e10cSrcweir                 if( aData.GetChar( n2+1 ) == '\\' )
219*cdf0e10cSrcweir                     n2 += 2;        // Doppel-Backslash -> OK
220*cdf0e10cSrcweir                 else
221*cdf0e10cSrcweir                 {
222*cdf0e10cSrcweir                     if( n2 > n )
223*cdf0e10cSrcweir                         n2--;
224*cdf0e10cSrcweir                     break;          // einfach-Backslash -> Ende
225*cdf0e10cSrcweir                 }
226*cdf0e10cSrcweir             }
227*cdf0e10cSrcweir             else
228*cdf0e10cSrcweir                 n2++;               // kein Backslash -> OK
229*cdf0e10cSrcweir         }
230*cdf0e10cSrcweir     }
231*cdf0e10cSrcweir     if( nLen > n2 )
232*cdf0e10cSrcweir     {
233*cdf0e10cSrcweir         if(aData.GetChar( n2 ) != ' ') n2++;
234*cdf0e10cSrcweir         nNext = n2;
235*cdf0e10cSrcweir     }
236*cdf0e10cSrcweir     return n;
237*cdf0e10cSrcweir }
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir // read parameters "1-3" or 1-3 with both values between 1 and nMax
242*cdf0e10cSrcweir bool _ReadFieldParams::GetTokenSttFromTo(sal_uInt16* pFrom, sal_uInt16* pTo, sal_uInt16 nMax)
243*cdf0e10cSrcweir {
244*cdf0e10cSrcweir     sal_uInt16 nStart = 0;
245*cdf0e10cSrcweir     sal_uInt16 nEnd   = 0;
246*cdf0e10cSrcweir     xub_StrLen n = GoToTokenParam();
247*cdf0e10cSrcweir     if( STRING_NOTFOUND != n )
248*cdf0e10cSrcweir     {
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir         String sParams( GetResult() );
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir         xub_StrLen nIndex = 0;
253*cdf0e10cSrcweir         String sStart( sParams.GetToken(0, '-', nIndex) );
254*cdf0e10cSrcweir         if( STRING_NOTFOUND != nIndex )
255*cdf0e10cSrcweir         {
256*cdf0e10cSrcweir             nStart = static_cast<sal_uInt16>(sStart.ToInt32());
257*cdf0e10cSrcweir             nEnd   = static_cast<sal_uInt16>(sParams.Copy(nIndex).ToInt32());
258*cdf0e10cSrcweir         }
259*cdf0e10cSrcweir     }
260*cdf0e10cSrcweir     if( pFrom ) *pFrom = nStart;
261*cdf0e10cSrcweir     if( pTo )   *pTo   = nEnd;
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir     return nStart && nEnd && (nMax >= nStart) && (nMax >= nEnd);
264*cdf0e10cSrcweir }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir // *** SwVbaFields ***********************************************
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir uno::Any lcl_createField( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel, const uno::Any& aSource )
269*cdf0e10cSrcweir {
270*cdf0e10cSrcweir     uno::Reference< text::XTextField > xTextField( aSource, uno::UNO_QUERY_THROW );
271*cdf0e10cSrcweir     uno::Reference< text::XTextDocument > xTextDocument( xModel, uno::UNO_QUERY_THROW );
272*cdf0e10cSrcweir     uno::Reference< word::XField > xField( new SwVbaField( xParent, xContext, xTextDocument, xTextField ) );
273*cdf0e10cSrcweir     return uno::makeAny( xField );
274*cdf0e10cSrcweir }
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir typedef ::cppu::WeakImplHelper1< css::container::XEnumeration > FieldEnumeration_BASE;
277*cdf0e10cSrcweir typedef ::cppu::WeakImplHelper2< container::XIndexAccess, container::XEnumerationAccess > FieldCollectionHelper_BASE;
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir class FieldEnumeration : public FieldEnumeration_BASE
280*cdf0e10cSrcweir {
281*cdf0e10cSrcweir     uno::Reference< XHelperInterface > mxParent;
282*cdf0e10cSrcweir     uno::Reference< uno::XComponentContext > mxContext;
283*cdf0e10cSrcweir     uno::Reference< frame::XModel > mxModel;
284*cdf0e10cSrcweir     uno::Reference< container::XEnumeration > mxEnumeration;
285*cdf0e10cSrcweir public:
286*cdf0e10cSrcweir     FieldEnumeration(  const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xModel, const uno::Reference< container::XEnumeration >& xEnumeration ) : mxParent( xParent ), mxContext( xContext ), mxModel( xModel ), mxEnumeration( xEnumeration )
287*cdf0e10cSrcweir     {
288*cdf0e10cSrcweir     }
289*cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
290*cdf0e10cSrcweir     {
291*cdf0e10cSrcweir         return mxEnumeration->hasMoreElements();
292*cdf0e10cSrcweir     }
293*cdf0e10cSrcweir     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
294*cdf0e10cSrcweir     {
295*cdf0e10cSrcweir         if ( !hasMoreElements() )
296*cdf0e10cSrcweir             throw container::NoSuchElementException();
297*cdf0e10cSrcweir         return lcl_createField( mxParent, mxContext, mxModel, mxEnumeration->nextElement() );
298*cdf0e10cSrcweir     }
299*cdf0e10cSrcweir };
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir class FieldCollectionHelper : public FieldCollectionHelper_BASE
302*cdf0e10cSrcweir {
303*cdf0e10cSrcweir     uno::Reference< XHelperInterface > mxParent;
304*cdf0e10cSrcweir     uno::Reference< uno::XComponentContext > mxContext;
305*cdf0e10cSrcweir     uno::Reference< frame::XModel > mxModel;
306*cdf0e10cSrcweir     uno::Reference< container::XEnumerationAccess > mxEnumerationAccess;
307*cdf0e10cSrcweir public:
308*cdf0e10cSrcweir     FieldCollectionHelper( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel ) throw (css::uno::RuntimeException) : mxParent( xParent ), mxContext( xContext ), mxModel( xModel )
309*cdf0e10cSrcweir     {
310*cdf0e10cSrcweir         uno::Reference< text::XTextFieldsSupplier > xSupp( xModel, uno::UNO_QUERY_THROW );
311*cdf0e10cSrcweir         mxEnumerationAccess.set( xSupp->getTextFields(), uno::UNO_QUERY_THROW );
312*cdf0e10cSrcweir     }
313*cdf0e10cSrcweir 	// XElementAccess
314*cdf0e10cSrcweir 	virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException) { return  mxEnumerationAccess->getElementType(); }
315*cdf0e10cSrcweir 	virtual ::sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException) { return mxEnumerationAccess->hasElements(); }
316*cdf0e10cSrcweir 	// XIndexAccess
317*cdf0e10cSrcweir 	virtual ::sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException)
318*cdf0e10cSrcweir     {
319*cdf0e10cSrcweir         uno::Reference< container::XEnumeration > xEnumeration =  mxEnumerationAccess->createEnumeration();
320*cdf0e10cSrcweir         sal_Int32 nCount = 0;
321*cdf0e10cSrcweir         while( xEnumeration->hasMoreElements() )
322*cdf0e10cSrcweir         {
323*cdf0e10cSrcweir             ++nCount;
324*cdf0e10cSrcweir             xEnumeration->nextElement();
325*cdf0e10cSrcweir         }
326*cdf0e10cSrcweir         return nCount;
327*cdf0e10cSrcweir     }
328*cdf0e10cSrcweir 	virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException )
329*cdf0e10cSrcweir 	{
330*cdf0e10cSrcweir         if( Index < 0 || Index >= getCount() )
331*cdf0e10cSrcweir             throw lang::IndexOutOfBoundsException();
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir         uno::Reference< container::XEnumeration > xEnumeration =  mxEnumerationAccess->createEnumeration();
334*cdf0e10cSrcweir         sal_Int32 nCount = 0;
335*cdf0e10cSrcweir         while( xEnumeration->hasMoreElements() )
336*cdf0e10cSrcweir         {
337*cdf0e10cSrcweir             if( nCount == Index )
338*cdf0e10cSrcweir             {
339*cdf0e10cSrcweir                 return xEnumeration->nextElement();
340*cdf0e10cSrcweir             }
341*cdf0e10cSrcweir             ++nCount;
342*cdf0e10cSrcweir         }
343*cdf0e10cSrcweir         throw lang::IndexOutOfBoundsException();
344*cdf0e10cSrcweir     }
345*cdf0e10cSrcweir 	// XEnumerationAccess
346*cdf0e10cSrcweir 	virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration(  ) throw (uno::RuntimeException)
347*cdf0e10cSrcweir     {
348*cdf0e10cSrcweir         uno::Reference< container::XEnumeration > xEnumeration =  mxEnumerationAccess->createEnumeration();
349*cdf0e10cSrcweir         return uno::Reference< container::XEnumeration >( new FieldEnumeration( mxParent, mxContext, mxModel, xEnumeration ) );
350*cdf0e10cSrcweir     }
351*cdf0e10cSrcweir };
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir SwVbaFields::SwVbaFields( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xModel ) : SwVbaFields_BASE( xParent, xContext , uno::Reference< container::XIndexAccess >( new FieldCollectionHelper( xParent, xContext, xModel ) ) ), mxModel( xModel )
354*cdf0e10cSrcweir {
355*cdf0e10cSrcweir     mxMSF.set( mxModel, uno::UNO_QUERY_THROW );
356*cdf0e10cSrcweir }
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir uno::Reference< word::XField > SAL_CALL
359*cdf0e10cSrcweir SwVbaFields::Add( const css::uno::Reference< ::ooo::vba::word::XRange >& Range, const css::uno::Any& Type, const css::uno::Any& Text, const css::uno::Any& /*PreserveFormatting*/ ) throw (css::uno::RuntimeException)
360*cdf0e10cSrcweir {
361*cdf0e10cSrcweir     sal_Int32 nType = word::WdFieldType::wdFieldEmpty;
362*cdf0e10cSrcweir     Type >>= nType;
363*cdf0e10cSrcweir     rtl::OUString sText;
364*cdf0e10cSrcweir     Text >>= sText;
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir     String sFieldName;
367*cdf0e10cSrcweir     if( ( nType == word::WdFieldType::wdFieldEmpty ) && ( sText.getLength() > 0 ) )
368*cdf0e10cSrcweir     {
369*cdf0e10cSrcweir         _ReadFieldParams aReadParam(sText);
370*cdf0e10cSrcweir         sFieldName = aReadParam.GetFieldName();
371*cdf0e10cSrcweir     }
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir     uno::Reference< text::XTextContent > xTextField;
374*cdf0e10cSrcweir     if( nType == word::WdFieldType::wdFieldFileName || sFieldName.EqualsIgnoreCaseAscii("FILENAME") )
375*cdf0e10cSrcweir     {
376*cdf0e10cSrcweir         xTextField.set( Create_Field_FileName( sText ), uno::UNO_QUERY_THROW );
377*cdf0e10cSrcweir     }
378*cdf0e10cSrcweir     else
379*cdf0e10cSrcweir     {
380*cdf0e10cSrcweir         throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference< uno::XInterface >() );
381*cdf0e10cSrcweir     }
382*cdf0e10cSrcweir 
383*cdf0e10cSrcweir     SwVbaRange* pVbaRange = dynamic_cast< SwVbaRange* >( Range.get() );
384*cdf0e10cSrcweir     uno::Reference< text::XTextRange > xTextRange = pVbaRange->getXTextRange();
385*cdf0e10cSrcweir     uno::Reference< text::XText > xText = xTextRange->getText();
386*cdf0e10cSrcweir     xText->insertTextContent( xTextRange, xTextField, true );
387*cdf0e10cSrcweir     return uno::Reference< word::XField >( new SwVbaField( mxParent, mxContext, uno::Reference< text::XTextDocument >( mxModel, uno::UNO_QUERY_THROW ), uno::Reference< text::XTextField >( xTextField, uno::UNO_QUERY_THROW ) ) );
388*cdf0e10cSrcweir }
389*cdf0e10cSrcweir 
390*cdf0e10cSrcweir uno::Reference< text::XTextField > SwVbaFields::Create_Field_FileName( const rtl::OUString _text ) throw (uno::RuntimeException)
391*cdf0e10cSrcweir {
392*cdf0e10cSrcweir     uno::Reference< text::XTextField > xTextField( mxMSF->createInstance( rtl::OUString::createFromAscii("com.sun.star.text.TextField.FileName") ), uno::UNO_QUERY_THROW );
393*cdf0e10cSrcweir     sal_Int16 nFileFormat = text::FilenameDisplayFormat::NAME_AND_EXT;
394*cdf0e10cSrcweir     if( _text.getLength() > 0 )
395*cdf0e10cSrcweir     {
396*cdf0e10cSrcweir         long nRet;
397*cdf0e10cSrcweir         _ReadFieldParams aReadParam( _text );
398*cdf0e10cSrcweir         while (-1 != (nRet = aReadParam.SkipToNextToken()))
399*cdf0e10cSrcweir         {
400*cdf0e10cSrcweir             switch (nRet)
401*cdf0e10cSrcweir             {
402*cdf0e10cSrcweir                 case 'p':
403*cdf0e10cSrcweir                     nFileFormat = text::FilenameDisplayFormat::FULL;
404*cdf0e10cSrcweir                     break;
405*cdf0e10cSrcweir                 case '*':
406*cdf0e10cSrcweir                     //Skip over MERGEFORMAT
407*cdf0e10cSrcweir                     aReadParam.SkipToNextToken();
408*cdf0e10cSrcweir                     break;
409*cdf0e10cSrcweir                 default:
410*cdf0e10cSrcweir                     DebugHelper::exception(SbERR_BAD_ARGUMENT, rtl::OUString());
411*cdf0e10cSrcweir                     break;
412*cdf0e10cSrcweir             }
413*cdf0e10cSrcweir         }
414*cdf0e10cSrcweir     }
415*cdf0e10cSrcweir 
416*cdf0e10cSrcweir     uno::Reference< beans::XPropertySet > xProps( xTextField, uno::UNO_QUERY_THROW );
417*cdf0e10cSrcweir     xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FileFormat") ), uno::makeAny( nFileFormat ) );
418*cdf0e10cSrcweir 
419*cdf0e10cSrcweir     return xTextField;
420*cdf0e10cSrcweir }
421*cdf0e10cSrcweir 
422*cdf0e10cSrcweir uno::Reference< container::XEnumeration > SAL_CALL
423*cdf0e10cSrcweir SwVbaFields::createEnumeration() throw (uno::RuntimeException)
424*cdf0e10cSrcweir {
425*cdf0e10cSrcweir     uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
426*cdf0e10cSrcweir     return xEnumerationAccess->createEnumeration();
427*cdf0e10cSrcweir }
428*cdf0e10cSrcweir 
429*cdf0e10cSrcweir // ScVbaCollectionBaseImpl
430*cdf0e10cSrcweir uno::Any
431*cdf0e10cSrcweir SwVbaFields::createCollectionObject( const uno::Any& aSource )
432*cdf0e10cSrcweir {
433*cdf0e10cSrcweir     return lcl_createField( mxParent, mxContext, mxModel, aSource );
434*cdf0e10cSrcweir }
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir sal_Int32 SAL_CALL SwVbaFields::Update() throw (uno::RuntimeException)
437*cdf0e10cSrcweir {
438*cdf0e10cSrcweir     sal_Int32 nUpdate = 1;
439*cdf0e10cSrcweir     try
440*cdf0e10cSrcweir     {
441*cdf0e10cSrcweir         uno::Reference< text::XTextFieldsSupplier > xSupp( mxModel, uno::UNO_QUERY_THROW );
442*cdf0e10cSrcweir         uno::Reference< util::XRefreshable > xRef( xSupp->getTextFields(), uno::UNO_QUERY_THROW );
443*cdf0e10cSrcweir         xRef->refresh();
444*cdf0e10cSrcweir         nUpdate = 0;
445*cdf0e10cSrcweir     }catch( uno::Exception )
446*cdf0e10cSrcweir     {
447*cdf0e10cSrcweir         nUpdate = 1;
448*cdf0e10cSrcweir     }
449*cdf0e10cSrcweir     return nUpdate;
450*cdf0e10cSrcweir }
451*cdf0e10cSrcweir 
452*cdf0e10cSrcweir // XHelperInterface
453*cdf0e10cSrcweir rtl::OUString&
454*cdf0e10cSrcweir SwVbaFields::getServiceImplName()
455*cdf0e10cSrcweir {
456*cdf0e10cSrcweir     static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaFields") );
457*cdf0e10cSrcweir     return sImplName;
458*cdf0e10cSrcweir }
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir // XEnumerationAccess
461*cdf0e10cSrcweir uno::Type SAL_CALL
462*cdf0e10cSrcweir SwVbaFields::getElementType() throw (uno::RuntimeException)
463*cdf0e10cSrcweir {
464*cdf0e10cSrcweir     return  word::XField::static_type(0);
465*cdf0e10cSrcweir }
466*cdf0e10cSrcweir 
467*cdf0e10cSrcweir uno::Sequence<rtl::OUString>
468*cdf0e10cSrcweir SwVbaFields::getServiceNames()
469*cdf0e10cSrcweir {
470*cdf0e10cSrcweir     static uno::Sequence< rtl::OUString > aServiceNames;
471*cdf0e10cSrcweir     if ( aServiceNames.getLength() == 0 )
472*cdf0e10cSrcweir     {
473*cdf0e10cSrcweir         aServiceNames.realloc( 1 );
474*cdf0e10cSrcweir         aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Fields" ) );
475*cdf0e10cSrcweir     }
476*cdf0e10cSrcweir     return aServiceNames;
477*cdf0e10cSrcweir }
478*cdf0e10cSrcweir 
479