merge the formfield patch from ooo-build
[ooovba.git] / extensions / test / ole / OleClient / funcs.cxx
blobda5bb6989a8e4b7d07a6ec4ee25e54ed5f478f35
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: funcs.cxx,v $
10 * $Revision: 1.5 $
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_extensions.hxx"
34 #include <atlbase.h>
35 #include <stdio.h>
36 #include "cppuhelper/bootstrap.hxx"
37 #include "rtl/process.h"
38 #include "typelib/typedescription.hxx"
40 #include "com/sun/star/bridge/ModelDependent.hpp"
41 #include "com/sun/star/bridge/XBridgeSupplier2.hpp"
42 #include "com/sun/star/uno/TypeClass.hpp"
43 #include "com/sun/star/script/XInvocation.hpp"
44 #include "com/sun/star/lang/XMultiServiceFactory.hpp"
45 #include "com/sun/star/uno/XComponentContext.hpp"
46 #include <com/sun/star/bridge/oleautomation/NamedArgument.hpp>
47 #include "rtl/ustring.hxx"
49 using namespace com::sun::star::bridge;
50 using namespace com::sun::star::bridge::ModelDependent;
51 using namespace com::sun::star::uno;
52 using namespace com::sun::star::lang;
53 using namespace com::sun::star::script;
54 using namespace com::sun::star::bridge::oleautomation;
55 using namespace rtl;
56 using namespace cppu;
58 template< class T >
59 bool equalSequences(const Sequence<T>& seqIn, const Sequence<Any> & returned);
62 Reference< XMultiServiceFactory > objectFactory;//
65 Reference<XMultiServiceFactory> getMultiServiceFactory()
67 static Reference< XMultiServiceFactory > factory;
68 if( ! objectFactory.is() )
70 Reference<XComponentContext> context = defaultBootstrap_InitialComponentContext();
71 factory = Reference<XMultiServiceFactory>( context->getServiceManager(), UNO_QUERY);
74 return factory;
77 Reference<XInvocation> getComObject( OUString progId)
79 HRESULT hr= S_OK;
80 Reference< XInvocation > ret;
81 // Reference<XMultiServiceFactory> fac;
82 if( ! objectFactory.is())
83 { Reference<XMultiServiceFactory> mgr= getMultiServiceFactory();
84 Reference<XInterface> xInt= mgr->createInstance(
85 OUString(L"com.sun.star.bridge.oleautomation.Factory"));
86 objectFactory= Reference<XMultiServiceFactory>::query( xInt);
89 if( objectFactory.is())
91 Reference<XInterface> xIntAx= objectFactory->createInstance( progId.getStr());
92 if( xIntAx.is() )
94 Reference< XInvocation > xInv( xIntAx, UNO_QUERY);
95 ret= xInv;
98 return ret;
101 Reference<XInvocation> convertComObject( IUnknown* pUnk)
103 Reference< XMultiServiceFactory > mgr= getMultiServiceFactory();
104 Reference< XInterface > xIntSupplier= mgr->createInstance(OUString(L"com.sun.star.bridge.OleBridgeSupplier2"));
105 Reference< XBridgeSupplier2 > xSuppl( xIntSupplier, UNO_QUERY);
107 Any any;
108 CComVariant var( pUnk);
109 any <<= ( sal_uInt32)&var;
110 sal_uInt8 arId[16];
111 rtl_getGlobalProcessId( arId);
112 Any target= xSuppl->createBridge( any, Sequence<sal_Int8>( (sal_Int8*)arId, 16), OLE, UNO );
114 Reference<XInvocation> ret;
115 target>>= ret;
116 return ret;
120 Parameter values contains the expected return values. The value at index 0
121 correspond to parameter 0 (left - most). For parameters which are not out or
122 in/out the value must be a void any.
124 The number of items in value must be the
125 same as the number of provided parameter during the call on the method.
127 The parameter outArgs, indices correspond to the sequences which are
128 arguments to XInvocation::invoke
130 bool checkOutArgs(const Sequence<Any> & outArgs,
131 const Sequence<sal_Int16> & indices, const Sequence<Any> & values)
133 if (values.getLength() != outArgs.getLength())
134 return false;
135 //iterate over all parameters. i represents the parameter index
136 for (int i = 0; i < values.getLength(); i++)
138 if (values[i].getValueType() == getVoidCppuType())
139 continue;
140 //out parameter
141 //Based on the parameter index find the correspondent out value
142 int indexOutSeq = -1;
143 for (int iIndices = indices.getLength() - 1; iIndices >= 0; iIndices --)
145 if (indices[iIndices] == i)
147 indexOutSeq = iIndices;
148 break;
151 if (indexOutSeq == -1)
152 return false;
154 Any value;
155 Any out;
156 values[i] >>= value;
157 outArgs[indexOutSeq] >>=out;
158 NamedArgument naVal;
159 NamedArgument naOut;
160 value >>= naVal;
161 out >>= naOut;
162 if (values[i].getValueType() == getCppuType((NamedArgument *) 0))
164 NamedArgument inNamed;
165 values[i] >>= inNamed;
166 value <<= inNamed.Value;
168 if (value != outArgs[indexOutSeq])
169 return false;
171 return true;
174 /* The returned sequence always contains Any elements
176 bool equalSequences(const Any& orig, const Any& returned)
178 if (orig.getValueTypeClass() != TypeClass_SEQUENCE)
180 OSL_ASSERT(0);
181 return false;
183 TypeDescription td(orig.getValueTypeRef());
184 typelib_IndirectTypeDescription * indirect_td = (typelib_IndirectTypeDescription *) td.get();
186 switch (indirect_td->pType->eTypeClass)
188 case TypeClass_CHAR:
190 Sequence<sal_Unicode> seq;
191 orig >>= seq;
192 Sequence<Any> seq2;
193 returned >>= seq2;
194 return equalSequences(seq, seq2);
196 case TypeClass_BOOLEAN:
198 Sequence<sal_Bool> seq;
199 orig >>= seq;
200 Sequence<Any> seq2;
201 returned >>= seq2;
202 return equalSequences(seq, seq2);
204 case TypeClass_BYTE:
206 Sequence<sal_Int8> seq;
207 orig >>= seq;
208 Sequence<Any> seq2;
209 returned >>= seq2;
210 return equalSequences(seq, seq2);
212 case TypeClass_SHORT:
214 Sequence<sal_Int16> seq;
215 orig >>= seq;
216 Sequence<Any> seq2;
217 returned >>= seq2;
218 return equalSequences(seq, seq2);
220 case TypeClass_LONG:
222 Sequence<sal_Int32> seq;
223 orig >>= seq;
224 Sequence<Any> seq2;
225 returned >>= seq2;
226 return equalSequences(seq, seq2);
228 case TypeClass_FLOAT:
230 Sequence<float> seq;
231 orig >>= seq;
232 Sequence<Any> seq2;
233 returned >>= seq2;
234 return equalSequences(seq, seq2);
236 case TypeClass_DOUBLE:
238 Sequence<double> seq;
239 orig >>= seq;
240 Sequence<Any> seq2;
241 returned >>= seq2;
242 return equalSequences(seq, seq2);
244 case TypeClass_STRING:
246 Sequence<OUString> seq;
247 orig >>= seq;
248 Sequence<Any> seq2;
249 returned >>= seq2;
250 return equalSequences(seq, seq2);
252 case TypeClass_ANY:
254 Sequence<Any> seq;
255 orig >>= seq;
256 Sequence<Any> seq2;
257 returned >>= seq2;
258 return equalSequences(seq, seq2);
260 case TypeClass_SEQUENCE:
262 //Sequence<sal_Unicode> seq;
263 //orig >>= seq;
264 //Sequence<Any> seq2;
265 //returned >>= seq2;
266 //return equalSequences(seq, seq2);
267 break;
269 case TypeClass_INTERFACE:
271 Sequence<Reference<XInvocation> > seq;
272 orig >>= seq;
273 Sequence<Any> seq2;
274 returned >>= seq2;
275 return equalSequences(seq, seq2);
277 default:
278 return false;
280 return false;
283 template< class T >
284 bool equalSequences(const Sequence<T>& seqIn, const Sequence<Any> & seqOut)
286 if (seqIn.getLength() != seqOut.getLength())
287 return false;
288 int len = seqIn.getLength();
289 for (int i = 0; i < len; i++)
291 Any anyIn;
292 anyIn <<= seqIn[i];
293 Any anyOut = seqOut[i];
294 if (anyIn != anyOut)
295 return false;
298 return true;
301 void printSequence( Sequence<Any>& val)
304 // typelib_TypeDescription* desc;
305 // val.getValueTypeDescription( &desc);
306 // typelib_typedescription_release( desc);
308 USES_CONVERSION;
309 char buff[1024];
310 buff[0]=0;
311 char tmpBuf[1024];
312 tmpBuf[0]=0;
313 sal_Int32 i;
315 for( i=0; i< val.getLength(); i++)
317 Any& elem= val[i];
318 switch ( elem.getValueTypeClass())
320 case TypeClass_BYTE:
321 sprintf( tmpBuf, "sal_Int8 %d \n", *(sal_Int8*)elem.getValue());
322 break;
323 case TypeClass_SHORT:
324 sprintf( tmpBuf, "sal_Int16 %d \n", *(sal_Int16*)elem.getValue());
325 break;
326 case TypeClass_LONG:
327 sprintf( tmpBuf, "sal_Int32 %d \n", *(sal_Int32*)elem.getValue());
328 break;
329 case TypeClass_DOUBLE:
330 sprintf( tmpBuf, "double %f \n", *(double*)elem.getValue());
331 break;
332 case TypeClass_FLOAT:
333 sprintf( tmpBuf, "float %f \n", *(float*)elem.getValue());
334 break;
335 case TypeClass_STRING:
336 sprintf( tmpBuf, "%S \n", (*(OUString*)elem.getValue()).getStr());
337 break;
338 case TypeClass_INTERFACE:
340 // we assume that the interface is XInvocation of a AxTestControls.Basic component.
341 Reference<XInvocation> inv;
342 elem>>= inv;
343 if( inv.is())
345 Any prpVal= inv->getValue( OUString( L"prpString"));
346 sprintf( tmpBuf, "Property prpString: %S \n", (*(OUString*)prpVal.getValue()).getStr());
348 break;
350 default:break;
352 strcat( buff, tmpBuf);
356 MessageBox( NULL, A2T(buff), _T("clientTest: printing Sequence elements"), MB_OK);