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