merge the formfield patch from ooo-build
[ooovba.git] / sw / source / core / fields / macrofld.cxx
blob92ba9ae5a219c550be68ba808dfcd08d41435b46
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: macrofld.cxx,v $
10 * $Revision: 1.13.214.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
35 #include <hintids.hxx>
36 #include <doc.hxx>
37 #include <docufld.hxx>
38 #ifndef _UNOFLDMID_H
39 #include <unofldmid.h>
40 #endif
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/uri/XUriReferenceFactory.hpp>
43 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
44 #include <comphelper/processfactory.hxx>
46 using namespace ::com::sun::star;
47 using ::rtl::OUString;
48 /*--------------------------------------------------------------------
49 Beschreibung: MacroFeldtypen
50 --------------------------------------------------------------------*/
52 SwMacroFieldType::SwMacroFieldType(SwDoc* pDocument)
53 : SwFieldType( RES_MACROFLD ),
54 pDoc(pDocument)
58 SwFieldType* SwMacroFieldType::Copy() const
60 SwMacroFieldType* pType = new SwMacroFieldType(pDoc);
61 return pType;
64 /*--------------------------------------------------------------------
65 Beschreibung: Das Macrofeld selbst
66 --------------------------------------------------------------------*/
68 SwMacroField::SwMacroField(SwMacroFieldType* pInitType,
69 const String& rLibAndName, const String& rTxt) :
70 SwField(pInitType), aMacro(rLibAndName), aText(rTxt), bIsScriptURL(FALSE)
72 bIsScriptURL = isScriptURL(aMacro);
75 String SwMacroField::Expand() const
76 { // Button malen anstatt von
77 return aText ;
80 SwField* SwMacroField::Copy() const
82 return new SwMacroField((SwMacroFieldType*)GetTyp(), aMacro, aText);
85 String SwMacroField::GetCntnt(BOOL bName) const
87 if(bName)
89 String aStr(GetTyp()->GetName());
90 aStr += ' ';
91 aStr += aMacro;
92 return aStr;
94 return Expand();
97 String SwMacroField::GetLibName() const
99 // if it is a Scripting Framework macro return an empty string
100 if (bIsScriptURL)
102 return String();
105 if (aMacro.Len())
107 USHORT nPos = aMacro.Len();
109 for (USHORT i = 0; i < 3 && nPos > 0; i++)
110 while (aMacro.GetChar(--nPos) != '.' && nPos > 0) ;
112 return aMacro.Copy(0, nPos );
115 DBG_ASSERT(0, "Kein Macroname vorhanden");
116 return aEmptyStr;
119 String SwMacroField::GetMacroName() const
121 if (aMacro.Len())
123 if (bIsScriptURL)
125 return aMacro.Copy( 0 );
127 else
129 USHORT nPos = aMacro.Len();
131 for (USHORT i = 0; i < 3 && nPos > 0; i++)
132 while (aMacro.GetChar(--nPos) != '.' && nPos > 0) ;
134 return aMacro.Copy( ++nPos );
138 DBG_ASSERT(0, "Kein Macroname vorhanden");
139 return aEmptyStr;
142 SvxMacro SwMacroField::GetSvxMacro() const
144 if (bIsScriptURL)
146 return SvxMacro(aMacro, String(), EXTENDED_STYPE);
148 else
150 return SvxMacro(GetMacroName(), GetLibName(), STARBASIC);
154 /*--------------------------------------------------------------------
155 Beschreibung: LibName und MacroName
156 --------------------------------------------------------------------*/
158 void SwMacroField::SetPar1(const String& rStr)
160 aMacro = rStr;
161 bIsScriptURL = isScriptURL(aMacro);
164 const String& SwMacroField::GetPar1() const
166 return aMacro;
169 /*--------------------------------------------------------------------
170 Beschreibung: Macrotext
171 --------------------------------------------------------------------*/
173 void SwMacroField::SetPar2(const String& rStr)
175 aText = rStr;
178 String SwMacroField::GetPar2() const
180 return aText;
183 /*-----------------05.03.98 13:38-------------------
185 --------------------------------------------------*/
186 BOOL SwMacroField::QueryValue( uno::Any& rAny, USHORT nWhichId ) const
188 switch( nWhichId )
190 case FIELD_PROP_PAR1:
191 rAny <<= OUString(GetMacroName());
192 break;
193 case FIELD_PROP_PAR2:
194 rAny <<= OUString(aText);
195 break;
196 case FIELD_PROP_PAR3:
197 rAny <<= OUString(GetLibName());
198 break;
199 case FIELD_PROP_PAR4:
200 rAny <<= bIsScriptURL ? OUString(GetMacroName()): OUString();
201 break;
202 default:
203 DBG_ERROR("illegal property");
205 return TRUE;
207 /*-----------------05.03.98 13:38-------------------
209 --------------------------------------------------*/
210 BOOL SwMacroField::PutValue( const uno::Any& rAny, USHORT nWhichId )
212 String sTmp;
213 switch( nWhichId )
215 case FIELD_PROP_PAR1:
216 CreateMacroString( aMacro, ::GetString(rAny, sTmp), GetLibName());
217 break;
218 case FIELD_PROP_PAR2:
219 ::GetString( rAny, aText );
220 break;
221 case FIELD_PROP_PAR3:
222 CreateMacroString(aMacro, GetMacroName(), ::GetString(rAny, sTmp) );
223 break;
224 case FIELD_PROP_PAR4:
225 ::GetString(rAny, aMacro);
226 bIsScriptURL = isScriptURL(aMacro);
227 break;
228 default:
229 DBG_ERROR("illegal property");
232 return TRUE;
235 // create an internally used macro name from the library and macro name parts
236 void SwMacroField::CreateMacroString(
237 String& rMacro,
238 const String& rMacroName,
239 const String& rLibraryName )
241 // concatenate library and name; use dot only if both strings have content
242 rMacro = rLibraryName;
243 if ( rLibraryName.Len() > 0 && rMacroName.Len() > 0 )
244 rMacro += '.';
245 rMacro += rMacroName;
248 BOOL SwMacroField::isScriptURL( const String& str )
250 uno::Reference< lang::XMultiServiceFactory > xSMgr =
251 ::comphelper::getProcessServiceFactory();
253 uno::Reference< uri::XUriReferenceFactory >
254 xFactory( xSMgr->createInstance(
255 OUString::createFromAscii(
256 "com.sun.star.uri.UriReferenceFactory" ) ), uno::UNO_QUERY );
258 if ( xFactory.is() )
260 uno::Reference< uri::XVndSunStarScriptUrl >
261 xUrl( xFactory->parse( str ), uno::UNO_QUERY );
263 if ( xUrl.is() )
265 return TRUE;
268 return FALSE;