merge the formfield patch from ooo-build
[ooovba.git] / extensions / test / ole / unoTocomCalls / XCallback_Impl / Callback.cpp
blob12e1be11379566fc58135c2e5b43d59955143be5
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: Callback.cpp,v $
10 * $Revision: 1.7 $
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 ************************************************************************/
30 // Callback.cpp : Implementation of CCallback
31 #include "stdafx.h"
32 #include "XCallback_Impl.h"
33 #include "Callback.h"
35 /////////////////////////////////////////////////////////////////////////////
36 // CCallback
39 STDMETHODIMP CCallback::func1()
41 MessageBox( NULL, _T("Callback::func1 called"),_T(""), MB_OK);
42 return S_OK;
45 STDMETHODIMP CCallback::returnInterface(IDispatch **ppdisp)
47 if( ! ppdisp)
48 return E_POINTER;
49 CComPtr<IDispatch> spDisp;
50 spDisp.CoCreateInstance( L"XCallback_Impl.Simple");
51 *ppdisp= spDisp;
52 (*ppdisp)->AddRef();
53 return S_OK;
56 STDMETHODIMP CCallback::outInterface(IDispatch **ppdisp)
58 // return S_OK;
59 if( ! ppdisp)
60 return E_POINTER;
61 CComPtr<IDispatch> spDisp;
62 spDisp.CoCreateInstance( L"XCallback_Impl.Simple");
63 *ppdisp= spDisp;
64 (*ppdisp)->AddRef();
66 // MessageBox( NULL, _T("CCallback::outInterface"), _T(""), MB_OK);
67 return S_OK;
70 STDMETHODIMP CCallback::outValuesMixed(long val, long *pval, BSTR string)
72 USES_CONVERSION;
73 char buff[1024];
74 *pval = val+1;
75 sprintf( buff, "param1: %d, param2 out: %d, param3: %S", val, *pval, string);
76 MessageBox( NULL, A2T(buff), A2T("XCallback_Impl.Callback"), MB_OK);
77 return S_OK;
81 STDMETHODIMP CCallback::outValuesAll(
82 /* [out] */ IDispatch __RPC_FAR *__RPC_FAR *ppdisp,
83 /* [out] */ IDispatch __RPC_FAR *__RPC_FAR *ppSimpleStruct,
84 /* [out] */ long __RPC_FAR *aSimpleEnum,
85 /* [out] */ SAFEARRAY __RPC_FAR * __RPC_FAR *outSeq,
86 /* [out] */ VARIANT __RPC_FAR *varAny,
87 /* [out] */ VARIANT_BOOL __RPC_FAR *aBool,
88 /* [out] */ short __RPC_FAR *aChar,
89 /* [out] */ BSTR __RPC_FAR *aString,
90 /* [out] */ float __RPC_FAR *aFloat,
91 /* [out] */ double __RPC_FAR *aDouble,
92 /* [out] */ unsigned char __RPC_FAR *aByte,
93 /* [out] */ short __RPC_FAR *aShort,
94 /* [out] */ long __RPC_FAR *aLong)
95 //)
97 // if( ! ppdisp || ! ppSimpleStruct || ! aSimpleEnum ||
98 // ! outSeq || !varAny ||! aBool || ! aChar ||
99 // ! aString || ! aFloat || ! aDouble || ! aByte ||
100 // ! aShort || ! aLong || ! aUShort || ! aULong)
101 // return E_POINTER;
103 HRESULT hr=S_OK;
104 hr= outInterface( ppdisp);
105 hr= outStruct( ppSimpleStruct);
106 hr= outEnum( aSimpleEnum);
107 hr= outSeqAny( outSeq);
108 hr= outAny( varAny);
109 hr= outBool( aBool);
110 hr= outChar( aChar);
111 hr= outString( aString);
112 hr= outFloat( aFloat);
113 hr= outDouble( aDouble);
114 hr= outByte( aByte);
115 hr= outShort( aShort);
116 hr= outLong( aLong);
117 return hr;
120 STDMETHODIMP CCallback::outStruct(IDispatch **outStruct)
122 // return S_OK;
123 if( !outStruct)
124 return E_POINTER;
125 HRESULT hr= E_FAIL;
126 // MessageBox( NULL, _T("CCallback::outStruct"), _T(""), MB_OK);
128 CComPtr<IDispatch> _dispMgr;
129 if( SUCCEEDED(hr= _dispMgr.CoCreateInstance(L"com.sun.star.ServiceManager")))
131 CComDispatchDriver manager( _dispMgr);
132 CComVariant param1(L"com.sun.star.reflection.CoreReflection");
133 CComVariant varRet;
134 hr= manager.Invoke1( L"createInstance", &param1, &varRet);
136 CComDispatchDriver reflection( varRet.pdispVal);
137 param1= L"oletest.SimpleStruct";
138 varRet.Clear();
139 hr= reflection.Invoke1( L"forName", &param1, &varRet);
141 CComDispatchDriver classSimpleStruct( varRet.pdispVal);
143 CComPtr<IDispatch> dispStruct;
144 param1.vt= VT_DISPATCH | VT_BYREF;
145 param1.ppdispVal= &dispStruct;
146 if( SUCCEEDED( hr= classSimpleStruct.Invoke1(L"createObject", &param1)))
148 // Set the value
149 CComDispatchDriver simpleStruct( dispStruct);
150 param1=L" this is a property string";
151 hr= simpleStruct.PutPropertyByName(L"message", &param1);
152 *outStruct= dispStruct;
153 (*outStruct)->AddRef();
154 hr= S_OK;
157 return hr;
160 STDMETHODIMP CCallback::outEnum(long *outEnum)
162 if( !outEnum)
163 return E_POINTER;
164 *outEnum= 1;
165 return S_OK;
168 STDMETHODIMP CCallback::outSeqAny(LPSAFEARRAY* outSeq)
170 // _CrtDbgBreak();
171 SAFEARRAY* pArr= SafeArrayCreateVector( VT_VARIANT, 0, 3);
172 CComVariant var[3];
173 var[0]=L" variant 0";
174 var[1]=L" variant 1";
175 var[2]=L"variant 2";
176 for( long i=0; i<3; i++)
178 SafeArrayPutElement( pArr, &i, (void*)&var[i]);
181 *outSeq= pArr;
182 return S_OK;
185 // ATLASSERT //VT_EMPTY
188 STDMETHODIMP CCallback::outAny(VARIANT *outAny)
190 if( ! outAny)
191 return E_POINTER;
192 outAny->vt= VT_BSTR;
193 outAny->bstrVal= SysAllocString( L"This is a string in a VARIANT");
195 return S_OK;
198 STDMETHODIMP CCallback::outBool(VARIANT_BOOL *outBool)
200 if( ! outBool)
201 return E_POINTER;
202 *outBool= VARIANT_TRUE;
203 return S_OK;
206 STDMETHODIMP CCallback::outChar(short *outChar)
208 if( !outChar)
209 return E_POINTER;
210 *outChar= (short)L'A';
211 return S_OK;
214 STDMETHODIMP CCallback::outString(BSTR *outString)
216 if( !outString)
217 return E_POINTER;
218 *outString= SysAllocString(L"This is a BSTR");
219 return S_OK;
222 STDMETHODIMP CCallback::outFloat(float *outFloat)
224 if( !outFloat)
225 return E_POINTER;
226 *outFloat= 3.14f;
227 return S_OK;
230 STDMETHODIMP CCallback::outDouble(double *outDouble)
232 if(!outDouble)
233 return E_POINTER;
235 *outDouble= 3.145;
236 return S_OK;
241 STDMETHODIMP CCallback::outShort(short *outShort)
243 if(!outShort)
244 return E_POINTER;
245 *outShort= -1;
246 return S_OK;
249 STDMETHODIMP CCallback::outLong(long *outLong)
251 if(!outLong)
252 return E_POINTER;
253 *outLong= 0xffffffff;
254 return S_OK;
259 STDMETHODIMP CCallback::outByte(unsigned char* outByte)
261 if(!outByte)
262 return E_POINTER;
263 *outByte= 0xff;
264 return S_OK;
267 STDMETHODIMP CCallback::inoutInterface(IDispatch **ppdisp)
269 if( !ppdisp)
270 return E_POINTER;
271 CComDispatchDriver disp( *ppdisp);
272 CComVariant param1(L"");
273 disp.Invoke1(L"func", &param1);
275 (*ppdisp)->Release();
277 CComPtr<IDispatch> outDisp;
278 outDisp.CoCreateInstance( L"XCallback_Impl.Simple");
279 *ppdisp= outDisp;
280 (*ppdisp)->AddRef();
282 return S_OK;
285 STDMETHODIMP CCallback::inoutStruct(IDispatch **inoutVal)
287 if( !inoutVal)
288 return E_POINTER;
289 HRESULT hr= S_OK;
290 USES_CONVERSION;
291 CComVariant var;
292 CComDispatchDriver disp( *inoutVal);
294 hr= disp.GetPropertyByName(L"message", &var);
295 MessageBox( NULL, W2T(var.bstrVal), _T("XCallback_Impl.Callback"), MB_OK);
297 (*inoutVal)->Release();
299 CComDispatchDriver dispStruct;
300 hr= outStruct( &dispStruct.p);
301 var.Clear();
302 var= L"This struct was created in XCallback_Imp.Callback";
303 hr= dispStruct.PutPropertyByName(L"message", &var);
305 *inoutVal= dispStruct;
306 (*inoutVal)->AddRef();
307 return hr;
310 STDMETHODIMP CCallback::inoutEnum(long *inoutVal)
312 if( !inoutVal)
313 return E_POINTER;
314 *inoutVal= *inoutVal+1;
316 return S_OK;
319 STDMETHODIMP CCallback::inoutSeqAny(LPSAFEARRAY *pArray)
321 if( !pArray)
322 return E_POINTER;
323 HRESULT hr= S_OK;
324 long lbound=0;
325 long ubound=0;
326 hr= SafeArrayGetLBound( *pArray, 1, &lbound);
327 hr= SafeArrayGetUBound( *pArray, 1, &ubound);
328 long count= ubound - lbound + 1;
330 // the Array is supposet to contain variants
331 CComVariant var;
332 for( long i=0; i<count; i++)
334 var.Clear();
335 hr= SafeArrayGetElement( *pArray, &i, (void*)&var);
338 SafeArrayDestroy( *pArray);
340 outSeqAny( pArray);
341 return S_OK;
344 STDMETHODIMP CCallback::inoutAny(VARIANT *inoutVal)
346 if( !inoutVal)
347 return E_POINTER;
348 USES_CONVERSION;
349 if( inoutVal->vt= VT_BSTR)
350 MessageBox( NULL, W2T( inoutVal->bstrVal), _T("XCallback_Impl.Callback"), MB_OK);
352 VariantClear( inoutVal);
353 inoutVal->vt= VT_BSTR;
354 inoutVal->bstrVal=SysAllocString( L" [string] XCallback_Impl.Callback inoutAny");
355 return S_OK;
358 STDMETHODIMP CCallback::inoutBool(VARIANT_BOOL *inoutVal)
360 if( !inoutVal)
361 return E_POINTER;
363 *inoutVal= *inoutVal == VARIANT_TRUE ? VARIANT_FALSE : VARIANT_TRUE;
364 return S_OK;
367 STDMETHODIMP CCallback::inoutChar(short *inoutVal)
369 if( !inoutVal)
370 return E_POINTER;
371 USES_CONVERSION;
372 char buff[256];
373 sprintf( buff, "character value: %C", *inoutVal);
374 MessageBox( NULL, A2T(buff), _T("XCallback_Impl.Callback"), MB_OK);
375 *inoutVal= L'B';
376 return S_OK;
379 STDMETHODIMP CCallback::inoutString(BSTR *inoutVal)
381 if( !inoutVal)
382 return E_POINTER;
383 USES_CONVERSION;
384 MessageBox( NULL, W2T(*inoutVal), _T("XCallback_Impl.Callback"), MB_OK);
385 SysFreeString(*inoutVal);
386 *inoutVal= SysAllocString(L"a string from XCallback_Impl.Callback");
388 return S_OK;
391 STDMETHODIMP CCallback::inoutFloat(float *inoutVal)
393 if( !inoutVal)
394 return E_POINTER;
395 *inoutVal = *inoutVal+1;
396 return S_OK;
399 STDMETHODIMP CCallback::inoutDouble(double *inoutVal)
401 if( !inoutVal)
402 return E_POINTER;
403 *inoutVal= *inoutVal+1;
404 return S_OK;
407 STDMETHODIMP CCallback::inoutByte(unsigned char *inoutVal)
409 if( !inoutVal)
410 return E_POINTER;
411 *inoutVal= 0xff;
412 return S_OK;
415 STDMETHODIMP CCallback::inoutShort(short *inoutVal)
417 if( !inoutVal)
418 return E_POINTER;
419 *inoutVal= -1;
420 return S_OK;
423 STDMETHODIMP CCallback::inoutLong(long* inoutVal)
425 if( !inoutVal)
426 return E_POINTER;
427 *inoutVal= 0xffffffff;
428 return S_OK;
431 STDMETHODIMP CCallback::inoutValuesAll(
432 /* [out][in] */ IDispatch __RPC_FAR *__RPC_FAR *aXSimple,
433 /* [out][in] */ IDispatch __RPC_FAR *__RPC_FAR *aStruct,
434 /* [out][in] */ long __RPC_FAR *aEnum,
435 /* [out][in] */ SAFEARRAY __RPC_FAR * __RPC_FAR *aSeq,
436 /* [out][in] */ VARIANT __RPC_FAR *aAny,
437 /* [out][in] */ VARIANT_BOOL __RPC_FAR *aBool,
438 /* [out][in] */ short __RPC_FAR *aChar,
439 /* [out][in] */ BSTR __RPC_FAR *aString,
440 /* [out][in] */ float __RPC_FAR *aFloat,
441 /* [out][in] */ double __RPC_FAR *aDouble,
442 /* [out][in] */ unsigned char __RPC_FAR *aByte,
443 /* [out][in] */ short __RPC_FAR *aShort,
444 /* [out][in] */ long __RPC_FAR *aLong)
446 inoutInterface( aXSimple);
447 inoutStruct( aStruct);
448 inoutEnum( aEnum);
449 inoutSeqAny( aSeq);
450 inoutAny( aAny);
451 inoutBool( aBool);
452 inoutChar( aChar);
453 inoutString( aString);
454 inoutFloat( aFloat);
455 inoutDouble( aDouble);
456 inoutByte( aByte);
457 inoutShort( aShort);
458 inoutLong( aLong);
460 return S_OK;
464 STDMETHODIMP CCallback::inValues(short aChar, long aLong, BSTR aString)
466 USES_CONVERSION;
467 wchar_t _char= (wchar_t) aChar;
468 char buff[1024];
469 sprintf( buff, "Parameters: char= %C, long= %d, string= %s", _char, aLong, W2A(aString));
470 MessageBox( NULL, A2T(buff), _T("XCallback_Impl.Callback"), MB_OK);
471 return S_OK;
474 STDMETHODIMP CCallback::outSeqByte(LPSAFEARRAY * outVal)
476 // TODO: Add your implementation code here
478 return S_OK;
481 STDMETHODIMP CCallback::inSeqByte( LPSAFEARRAY listeners)
484 return S_OK;
487 STDMETHODIMP CCallback::inSeqXEventListener( LPSAFEARRAY listeners, LPSAFEARRAY events)
489 HRESULT hr= S_OK;
490 long ubound= 0;
491 long lbound= 0;
492 long count= 0;
493 hr= SafeArrayGetUBound( listeners, 1, &ubound);
494 hr= SafeArrayGetLBound( listeners, 1, &lbound);
495 count= ubound - lbound +1;
497 // We assume thate the count of EventObjects in events is the same
498 for( long i = 0; i < count; i++)
500 CComVariant varListener;
501 CComVariant varEvent;
502 hr= SafeArrayGetElement( listeners, &i, &varListener);
503 hr= SafeArrayGetElement( events, &i, &varEvent);
504 if( varListener.vt == VT_DISPATCH && varEvent.vt == VT_DISPATCH)
506 CComDispatchDriver disp( varListener.pdispVal);
507 hr= disp.Invoke1(L"disposing", &varEvent);
512 return S_OK;