]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ole/automtn.cpp
Fix for the undocumented holes in spiner sizes.
[wxWidgets.git] / src / msw / ole / automtn.cpp
CommitLineData
d980b3e1
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: automtn.cpp
3// Purpose: OLE automation utilities
4// Author: Julian Smart
5// Modified by:
6// Created: 11/6/98
7// RCS-ID: $Id$
8// Copyright: (c) 1998, Julian Smart
65571936 9// Licence: wxWindows licence
d980b3e1
JS
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
d980b3e1
JS
13#pragma implementation "automtn.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#if defined(__BORLANDC__)
20#pragma hdrstop
21#endif
22
f6bcfd97 23#include "wx/defs.h"
d980b3e1 24
457e6c54 25// Watcom C++ gives a linker error if this is compiled in.
f6bcfd97 26// With Borland C++, all samples crash if this is compiled in.
6b12494d 27#if wxUSE_OLE && !(defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) && !defined(__CYGWIN10__)
457e6c54 28
5283098e 29#define _FORCENAMELESSUNION
f6bcfd97 30#include "wx/log.h"
4676948b 31#include "wx/msw/private.h"
ed5317e5 32#include "wx/msw/ole/oleutils.h"
42e69d6b 33#include "wx/msw/ole/automtn.h"
42e69d6b 34
f6bcfd97 35#include <math.h>
4676948b
JS
36
37#ifdef __WXWINCE__
38#include "wx/msw/wince/time.h"
39#else
f6bcfd97 40#include <time.h>
4676948b 41#endif
f6bcfd97 42
7dee726c
RS
43#include <wtypes.h>
44#include <unknwn.h>
4676948b 45
7dee726c
RS
46#include <ole2.h>
47#define _huge
4676948b
JS
48
49#ifndef __WXWINCE__
42e69d6b 50#include <ole2ver.h>
4676948b
JS
51#endif
52
42e69d6b 53#include <oleauto.h>
17b74d79 54
d980b3e1
JS
55// Verifies will fail if the needed buffer size is too large
56#define MAX_TIME_BUFFER_SIZE 128 // matches that in timecore.cpp
57#define MIN_DATE (-657434L) // about year 100
58#define MAX_DATE 2958465L // about year 9999
59
60// Half a second, expressed in days
61#define HALF_SECOND (1.0/172800.0)
62
63// One-based array of days in year at month start
64static int rgMonthDays[13] =
0a0e6a5b 65 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
d980b3e1 66
61bfe4b0
JS
67#if wxUSE_DATETIME
68#include "wx/datetime.h"
69
d980b3e1 70static BOOL OleDateFromTm(WORD wYear, WORD wMonth, WORD wDay,
0a0e6a5b 71 WORD wHour, WORD wMinute, WORD wSecond, DATE& dtDest);
d980b3e1 72static BOOL TmFromOleDate(DATE dtSrc, struct tm& tmDest);
e421922f 73#endif // wxUSE_TIMEDATE
d980b3e1
JS
74
75static void ClearVariant(VARIANTARG *pvarg) ;
76static void ReleaseVariant(VARIANTARG *pvarg) ;
77// static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr);
78
79/*
80 * wxAutomationObject
81 */
82
83wxAutomationObject::wxAutomationObject(WXIDISPATCH* dispatchPtr)
84{
0a0e6a5b 85 m_dispatchPtr = dispatchPtr;
d980b3e1
JS
86}
87
88wxAutomationObject::~wxAutomationObject()
89{
0a0e6a5b
WS
90 if (m_dispatchPtr)
91 {
92 ((IDispatch*)m_dispatchPtr)->Release();
93 m_dispatchPtr = NULL;
94 }
d980b3e1
JS
95}
96
97#define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
98
99// For Put/Get, no named arguments are allowed.
100bool wxAutomationObject::Invoke(const wxString& member, int action,
101 wxVariant& retValue, int noArgs, wxVariant args[], const wxVariant* ptrArgs[]) const
102{
0a0e6a5b
WS
103 if (!m_dispatchPtr)
104 return false;
105
106 // nonConstMember is necessary because the wxString class doesn't have enough consts...
107 wxString nonConstMember(member);
108
109 int ch = nonConstMember.Find('.');
110 if (ch != -1)
111 {
112 // Use dot notation to get the next object
113 wxString member2(nonConstMember.Left((size_t) ch));
114 wxString rest(nonConstMember.Right(nonConstMember.Length() - ch - 1));
115 wxAutomationObject obj;
116 if (!GetObject(obj, member2))
117 return false;
118 return obj.Invoke(rest, action, retValue, noArgs, args, ptrArgs);
119 }
120
121 VARIANTARG vReturn;
122 ClearVariant(& vReturn);
123
124 VARIANTARG* vReturnPtr = & vReturn;
125
126 // Find number of names args
127 int namedArgCount = 0;
128 int i;
129 for (i = 0; i < noArgs; i++)
130 if (!INVOKEARG(i).GetName().IsNull())
12335fa6 131 {
0a0e6a5b
WS
132 namedArgCount ++;
133 }
134
135 int namedArgStringCount = namedArgCount + 1;
136 BSTR* argNames = new BSTR[namedArgStringCount];
137 argNames[0] = wxConvertStringToOle(member);
138
139 // Note that arguments are specified in reverse order
140 // (all totally logical; hey, we're dealing with OLE here.)
141
142 int j = 0;
143 for (i = 0; i < namedArgCount; i++)
144 {
145 if (!INVOKEARG(i).GetName().IsNull())
146 {
147 argNames[(namedArgCount-j)] = wxConvertStringToOle(INVOKEARG(i).GetName());
148 j ++;
149 }
150 }
151
152 // + 1 for the member name, + 1 again in case we're a 'put'
153 DISPID* dispIds = new DISPID[namedArgCount + 2];
154
155 HRESULT hr;
156 DISPPARAMS dispparams;
157 unsigned int uiArgErr;
158 EXCEPINFO excep;
159
160 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
161 // member name as the first name, followed by argument names (if any).
162 hr = ((IDispatch*)m_dispatchPtr)->GetIDsOfNames(IID_NULL, argNames,
163 1 + namedArgCount, LOCALE_SYSTEM_DEFAULT, dispIds);
164 if (FAILED(hr))
165 {
166// ShowException(szMember, hr, NULL, 0);
167 delete[] argNames;
168 delete[] dispIds;
169 return false;
170 }
171
172 // if doing a property put(ref), we need to adjust the first argument to have a
173 // named arg of DISPID_PROPERTYPUT.
174 if (action & (DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYPUTREF))
175 {
176 namedArgCount = 1;
177 dispIds[1] = DISPID_PROPERTYPUT;
178 vReturnPtr = (VARIANTARG*) NULL;
179 }
180
181 // Convert the wxVariants to VARIANTARGs
182 VARIANTARG* oleArgs = new VARIANTARG[noArgs];
183 for (i = 0; i < noArgs; i++)
184 {
185 // Again, reverse args
186 if (!wxConvertVariantToOle(INVOKEARG((noArgs-1) - i), oleArgs[i]))
187 {
188 delete[] argNames;
189 delete[] dispIds;
12335fa6 190 delete[] oleArgs;
0a0e6a5b
WS
191 return false;
192 }
193 }
194
195 dispparams.rgdispidNamedArgs = dispIds + 1;
196 dispparams.rgvarg = oleArgs;
197 dispparams.cArgs = noArgs;
198 dispparams.cNamedArgs = namedArgCount;
199
200 excep.pfnDeferredFillIn = NULL;
201
202 hr = ((IDispatch*)m_dispatchPtr)->Invoke(dispIds[0], IID_NULL, LOCALE_SYSTEM_DEFAULT,
203 action, &dispparams, vReturnPtr, &excep, &uiArgErr);
204
205 for (i = 0; i < namedArgStringCount; i++)
206 {
207 SysFreeString(argNames[i]);
208 }
209 delete[] argNames;
210 delete[] dispIds;
211
212 for (i = 0; i < noArgs; i++)
213 ReleaseVariant(& oleArgs[i]) ;
214 delete[] oleArgs;
215
216 if (FAILED(hr))
217 {
218 // display the exception information if appropriate:
219// ShowException((const char*) member, hr, &excep, uiArgErr);
220
221 // free exception structure information
222 SysFreeString(excep.bstrSource);
223 SysFreeString(excep.bstrDescription);
224 SysFreeString(excep.bstrHelpFile);
225
226 if (vReturnPtr)
227 ReleaseVariant(vReturnPtr);
228 return false;
229 }
230 else
231 {
232 if (vReturnPtr)
233 {
234 // Convert result to wxVariant form
235 wxConvertOleToVariant(vReturn, retValue);
236 // Mustn't release the dispatch pointer
237 if (vReturn.vt == VT_DISPATCH)
238 {
239 vReturn.pdispVal = (IDispatch*) NULL;
240 }
241 ReleaseVariant(& vReturn);
12335fa6 242 }
0a0e6a5b
WS
243 }
244 return true;
d980b3e1
JS
245}
246
247// Invoke a member function
248wxVariant wxAutomationObject::CallMethod(const wxString& member, int noArgs, wxVariant args[])
249{
0a0e6a5b
WS
250 wxVariant retVariant;
251 if (!Invoke(member, DISPATCH_METHOD, retVariant, noArgs, args))
252 {
253 retVariant.MakeNull();
254 }
255 return retVariant;
d980b3e1
JS
256}
257
24f4ad95
JS
258wxVariant wxAutomationObject::CallMethodArray(const wxString& member, int noArgs, const wxVariant **args)
259{
0a0e6a5b
WS
260 wxVariant retVariant;
261 if (!Invoke(member, DISPATCH_METHOD, retVariant, noArgs, NULL, args))
262 {
263 retVariant.MakeNull();
264 }
265 return retVariant;
24f4ad95
JS
266}
267
d980b3e1 268wxVariant wxAutomationObject::CallMethod(const wxString& member,
0a0e6a5b
WS
269 const wxVariant& arg1, const wxVariant& arg2,
270 const wxVariant& arg3, const wxVariant& arg4,
271 const wxVariant& arg5, const wxVariant& arg6)
d980b3e1 272{
0a0e6a5b
WS
273 const wxVariant** args = new const wxVariant*[6];
274 int i = 0;
275 if (!arg1.IsNull())
276 {
277 args[i] = & arg1;
278 i ++;
279 }
280 if (!arg2.IsNull())
281 {
282 args[i] = & arg2;
283 i ++;
284 }
285 if (!arg3.IsNull())
286 {
287 args[i] = & arg3;
288 i ++;
289 }
290 if (!arg4.IsNull())
291 {
292 args[i] = & arg4;
293 i ++;
294 }
295 if (!arg5.IsNull())
296 {
297 args[i] = & arg5;
298 i ++;
299 }
300 if (!arg6.IsNull())
301 {
302 args[i] = & arg6;
303 i ++;
304 }
305 wxVariant retVariant;
306 if (!Invoke(member, DISPATCH_METHOD, retVariant, i, NULL, args))
307 {
308 retVariant.MakeNull();
309 }
310 delete[] args;
311 return retVariant;
d980b3e1
JS
312}
313
314// Get/Set property
24f4ad95
JS
315wxVariant wxAutomationObject::GetPropertyArray(const wxString& property, int noArgs, const wxVariant **args) const
316{
0a0e6a5b
WS
317 wxVariant retVariant;
318 if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, NULL, args))
319 {
320 retVariant.MakeNull();
321 }
322 return retVariant;
24f4ad95 323}
d980b3e1
JS
324wxVariant wxAutomationObject::GetProperty(const wxString& property, int noArgs, wxVariant args[]) const
325{
0a0e6a5b
WS
326 wxVariant retVariant;
327 if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, args))
328 {
329 retVariant.MakeNull();
330 }
331 return retVariant;
d980b3e1
JS
332}
333
334wxVariant wxAutomationObject::GetProperty(const wxString& property,
0a0e6a5b
WS
335 const wxVariant& arg1, const wxVariant& arg2,
336 const wxVariant& arg3, const wxVariant& arg4,
337 const wxVariant& arg5, const wxVariant& arg6)
d980b3e1 338{
0a0e6a5b
WS
339 const wxVariant** args = new const wxVariant*[6];
340 int i = 0;
341 if (!arg1.IsNull())
342 {
343 args[i] = & arg1;
344 i ++;
345 }
346 if (!arg2.IsNull())
347 {
348 args[i] = & arg2;
349 i ++;
350 }
351 if (!arg3.IsNull())
352 {
353 args[i] = & arg3;
354 i ++;
355 }
356 if (!arg4.IsNull())
357 {
358 args[i] = & arg4;
359 i ++;
360 }
361 if (!arg5.IsNull())
362 {
363 args[i] = & arg5;
364 i ++;
365 }
366 if (!arg6.IsNull())
367 {
368 args[i] = & arg6;
369 i ++;
370 }
371 wxVariant retVariant;
372 if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, i, NULL, args))
373 {
374 retVariant.MakeNull();
375 }
376 delete[] args;
377 return retVariant;
d980b3e1
JS
378}
379
380bool wxAutomationObject::PutProperty(const wxString& property, int noArgs, wxVariant args[])
381{
0a0e6a5b
WS
382 wxVariant retVariant;
383 if (!Invoke(property, DISPATCH_PROPERTYPUT, retVariant, noArgs, args))
384 {
385 return false;
386 }
387 return true;
d980b3e1
JS
388}
389
24f4ad95
JS
390bool wxAutomationObject::PutPropertyArray(const wxString& property, int noArgs, const wxVariant **args)
391{
0a0e6a5b
WS
392 wxVariant retVariant;
393 if (!Invoke(property, DISPATCH_PROPERTYPUT, retVariant, noArgs, NULL, args))
394 {
395 return false;
396 }
397 return true;
24f4ad95
JS
398}
399
d980b3e1 400bool wxAutomationObject::PutProperty(const wxString& property,
0a0e6a5b
WS
401 const wxVariant& arg1, const wxVariant& arg2,
402 const wxVariant& arg3, const wxVariant& arg4,
403 const wxVariant& arg5, const wxVariant& arg6)
d980b3e1 404{
0a0e6a5b
WS
405 const wxVariant** args = new const wxVariant*[6];
406 int i = 0;
407 if (!arg1.IsNull())
408 {
409 args[i] = & arg1;
410 i ++;
411 }
412 if (!arg2.IsNull())
413 {
414 args[i] = & arg2;
415 i ++;
416 }
417 if (!arg3.IsNull())
418 {
419 args[i] = & arg3;
420 i ++;
421 }
422 if (!arg4.IsNull())
423 {
424 args[i] = & arg4;
425 i ++;
426 }
427 if (!arg5.IsNull())
428 {
429 args[i] = & arg5;
430 i ++;
431 }
432 if (!arg6.IsNull())
433 {
434 args[i] = & arg6;
435 i ++;
436 }
437 wxVariant retVariant;
438 bool ret = Invoke(property, DISPATCH_PROPERTYPUT, retVariant, i, NULL, args);
439 delete[] args;
440 return ret;
d980b3e1
JS
441}
442
443
444// Uses DISPATCH_PROPERTYGET
445// and returns a dispatch pointer. The calling code should call Release
446// on the pointer, though this could be implicit by constructing an wxAutomationObject
447// with it and letting the destructor call Release.
448WXIDISPATCH* wxAutomationObject::GetDispatchProperty(const wxString& property, int noArgs, wxVariant args[]) const
449{
0a0e6a5b
WS
450 wxVariant retVariant;
451 if (Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, args))
452 {
453 if (retVariant.GetType() == wxT("void*"))
454 {
455 return (WXIDISPATCH*) retVariant.GetVoidPtr();
456 }
457 }
458
459 return (WXIDISPATCH*) NULL;
d980b3e1
JS
460}
461
24f4ad95
JS
462// Uses DISPATCH_PROPERTYGET
463// and returns a dispatch pointer. The calling code should call Release
464// on the pointer, though this could be implicit by constructing an wxAutomationObject
465// with it and letting the destructor call Release.
466WXIDISPATCH* wxAutomationObject::GetDispatchProperty(const wxString& property, int noArgs, const wxVariant **args) const
467{
0a0e6a5b
WS
468 wxVariant retVariant;
469 if (Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, NULL, args))
470 {
471 if (retVariant.GetType() == wxT("void*"))
472 {
473 return (WXIDISPATCH*) retVariant.GetVoidPtr();
474 }
475 }
476
477 return (WXIDISPATCH*) NULL;
24f4ad95
JS
478}
479
480
d980b3e1
JS
481// A way of initialising another wxAutomationObject with a dispatch object
482bool wxAutomationObject::GetObject(wxAutomationObject& obj, const wxString& property, int noArgs, wxVariant args[]) const
483{
0a0e6a5b
WS
484 WXIDISPATCH* dispatch = GetDispatchProperty(property, noArgs, args);
485 if (dispatch)
486 {
487 obj.SetDispatchPtr(dispatch);
488 return true;
489 }
490 else
491 return false;
d980b3e1
JS
492}
493
24f4ad95
JS
494// A way of initialising another wxAutomationObject with a dispatch object
495bool wxAutomationObject::GetObject(wxAutomationObject& obj, const wxString& property, int noArgs, const wxVariant **args) const
496{
0a0e6a5b
WS
497 WXIDISPATCH* dispatch = GetDispatchProperty(property, noArgs, args);
498 if (dispatch)
499 {
500 obj.SetDispatchPtr(dispatch);
501 return true;
502 }
503 else
504 return false;
24f4ad95
JS
505}
506
d980b3e1
JS
507// Get a dispatch pointer from the current object associated
508// with a class id
509bool wxAutomationObject::GetInstance(const wxString& classId) const
510{
0a0e6a5b
WS
511 if (m_dispatchPtr)
512 return false;
513
514 CLSID clsId;
515 IUnknown * pUnk = NULL;
516
517 wxBasicString unicodeName(classId.mb_str());
518
519 if (FAILED(CLSIDFromProgID((BSTR) unicodeName, &clsId)))
520 {
521 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
522 return false;
523 }
524
525 if (FAILED(GetActiveObject(clsId, NULL, &pUnk)))
526 {
527 wxLogWarning(wxT("Cannot find an active object"));
528 return false;
529 }
530
531 if (pUnk->QueryInterface(IID_IDispatch, (LPVOID*) &m_dispatchPtr) != S_OK)
532 {
533 wxLogWarning(wxT("Cannot find IDispatch interface"));
534 return false;
535 }
536
537 return true;
d980b3e1
JS
538}
539
540// Get a dispatch pointer from a new object associated
541// with the given class id
542bool wxAutomationObject::CreateInstance(const wxString& classId) const
543{
0a0e6a5b
WS
544 if (m_dispatchPtr)
545 return false;
546
547 CLSID clsId;
548
549 wxBasicString unicodeName(classId.mb_str());
550
551 if (FAILED(CLSIDFromProgID((BSTR) unicodeName, &clsId)))
552 {
553 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
554 return false;
555 }
556
557 // start a new copy of Excel, grab the IDispatch interface
558 if (FAILED(CoCreateInstance(clsId, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void**)&m_dispatchPtr)))
559 {
560 wxLogWarning(wxT("Cannot start an instance of this class."));
561 return false;
562 }
563
564 return true;
d980b3e1
JS
565}
566
567
ed5317e5 568bool wxConvertVariantToOle(const wxVariant& variant, VARIANTARG& oleVariant)
d980b3e1 569{
0a0e6a5b
WS
570 ClearVariant(&oleVariant);
571 if (variant.IsNull())
572 {
573 oleVariant.vt = VT_NULL;
574 return true;
575 }
d980b3e1
JS
576
577 wxString type(variant.GetType());
578
6b978929
JS
579
580 if (type == wxT("long"))
d980b3e1
JS
581 {
582 oleVariant.vt = VT_I4;
583 oleVariant.lVal = variant.GetLong() ;
584 }
6b978929
JS
585 // cVal not always present
586#ifndef __GNUWIN32__
587 else if (type == wxT("char"))
588 {
0a0e6a5b 589 oleVariant.vt=VT_I1; // Signed Char
6b978929
JS
590 oleVariant.cVal=variant.GetChar();
591 }
592#endif
223d09f6 593 else if (type == wxT("double"))
d980b3e1
JS
594 {
595 oleVariant.vt = VT_R8;
596 oleVariant.dblVal = variant.GetDouble();
597 }
223d09f6 598 else if (type == wxT("bool"))
d980b3e1
JS
599 {
600 oleVariant.vt = VT_BOOL;
7c5dc04f 601 // 'bool' required for VC++ 4 apparently
6b12494d 602#if (defined(__VISUALC__) && (__VISUALC__ <= 1000))
7be1f0d9
JS
603 oleVariant.bool = variant.GetBool();
604#else
d980b3e1 605 oleVariant.boolVal = variant.GetBool();
7be1f0d9 606#endif
d980b3e1 607 }
223d09f6 608 else if (type == wxT("string"))
d980b3e1
JS
609 {
610 wxString str( variant.GetString() );
611 oleVariant.vt = VT_BSTR;
ed5317e5 612 oleVariant.bstrVal = wxConvertStringToOle(str);
d980b3e1 613 }
457e6c54 614// For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
61bfe4b0
JS
615// Now obsolete
616#if 0 // wxUSE_TIMEDATE && !defined(__WATCOMC__)
223d09f6 617 else if (type == wxT("date"))
d980b3e1
JS
618 {
619 wxDate date( variant.GetDate() );
620 oleVariant.vt = VT_DATE;
621
0a0e6a5b
WS
622 if (!OleDateFromTm(date.GetYear(), date.GetMonth(), date.GetDay(),
623 0, 0, 0, oleVariant.date))
624 return false;
d980b3e1 625 }
223d09f6 626 else if (type == wxT("time"))
d980b3e1
JS
627 {
628 wxTime time( variant.GetTime() );
629 oleVariant.vt = VT_DATE;
630
0a0e6a5b
WS
631 if (!OleDateFromTm(time.GetYear(), time.GetMonth(), time.GetDay(),
632 time.GetHour(), time.GetMinute(), time.GetSecond(), oleVariant.date))
633 return false;
d980b3e1 634 }
61bfe4b0
JS
635#endif
636#if wxUSE_DATETIME
637 else if (type == wxT("datetime"))
638 {
639 wxDateTime date( variant.GetDateTime() );
640 oleVariant.vt = VT_DATE;
641
0a0e6a5b
WS
642 if (!OleDateFromTm(date.GetYear(), date.GetMonth(), date.GetDay(),
643 date.GetHour(), date.GetMinute(), date.GetSecond(), oleVariant.date))
644 return false;
61bfe4b0 645 }
457e6c54 646#endif
223d09f6 647 else if (type == wxT("void*"))
d980b3e1
JS
648 {
649 oleVariant.vt = VT_DISPATCH;
650 oleVariant.pdispVal = (IDispatch*) variant.GetVoidPtr();
651 }
223d09f6 652 else if (type == wxT("list") || type == wxT("stringlist"))
d980b3e1
JS
653 {
654 oleVariant.vt = VT_VARIANT | VT_ARRAY;
655
0a0e6a5b
WS
656 SAFEARRAY *psa;
657 SAFEARRAYBOUND saBound;
658 VARIANTARG *pvargBase;
659 VARIANTARG *pvarg;
660 int i, j;
d980b3e1
JS
661
662 int iCount = variant.GetCount();
0a0e6a5b
WS
663
664 saBound.lLbound = 0;
665 saBound.cElements = iCount;
666
667 psa = SafeArrayCreate(VT_VARIANT, 1, &saBound);
668 if (psa == NULL)
669 return false;
670
671 SafeArrayAccessData(psa, (void**)&pvargBase);
672
673 pvarg = pvargBase;
674 for (i = 0; i < iCount; i++)
675 {
676 // copy each string in the list of strings
d980b3e1 677 wxVariant eachVariant(variant[i]);
ed5317e5 678 if (!wxConvertVariantToOle(eachVariant, * pvarg))
d980b3e1 679 {
0a0e6a5b
WS
680 // memory failure: back out and free strings alloc'ed up to
681 // now, and then the array itself.
682 pvarg = pvargBase;
683 for (j = 0; j < i; j++)
684 {
685 SysFreeString(pvarg->bstrVal);
686 pvarg++;
687 }
688 SafeArrayDestroy(psa);
689 return false;
690 }
691 pvarg++;
692 }
693
694 SafeArrayUnaccessData(psa);
d980b3e1
JS
695
696 oleVariant.parray = psa;
697 }
698 else
699 {
700 oleVariant.vt = VT_NULL;
0a0e6a5b 701 return false;
d980b3e1 702 }
0a0e6a5b 703 return true;
d980b3e1
JS
704}
705
706#ifndef VT_TYPEMASK
707#define VT_TYPEMASK 0xfff
708#endif
709
ed5317e5 710bool wxConvertOleToVariant(const VARIANTARG& oleVariant, wxVariant& variant)
d980b3e1 711{
0a0e6a5b
WS
712 switch (oleVariant.vt & VT_TYPEMASK)
713 {
714 case VT_BSTR:
715 {
716 wxString str(wxConvertStringFromOle(oleVariant.bstrVal));
717 variant = str;
718 break;
719 }
720 case VT_DATE:
721 {
61bfe4b0 722#if wxUSE_DATETIME
f6bcfd97 723 struct tm tmTemp;
0a0e6a5b
WS
724 if (!TmFromOleDate(oleVariant.date, tmTemp))
725 return false;
d980b3e1 726
0a0e6a5b 727 wxDateTime date(tmTemp.tm_yday, (wxDateTime::Month) tmTemp.tm_mon, tmTemp.tm_year, tmTemp.tm_hour, tmTemp.tm_min, tmTemp.tm_sec);
d980b3e1 728
0a0e6a5b 729 variant = date;
f6bcfd97
BP
730#endif
731
732 break;
0a0e6a5b
WS
733 }
734 case VT_I4:
735 {
736 variant = (long) oleVariant.lVal;
737 break;
738 }
739 case VT_I2:
740 {
741 variant = (long) oleVariant.iVal;
742 break;
743 }
744
745 case VT_BOOL:
746 {
6b12494d 747#if (defined(_MSC_VER) && (_MSC_VER <= 1000) && !defined(__MWERKS__) ) //GC
25889d3c 748#ifndef HAVE_BOOL // Can't use bool operator if no native bool type
0a0e6a5b 749 variant = (long) (oleVariant.bool != 0);
25889d3c 750#else
0a0e6a5b 751 variant = (bool) (oleVariant.bool != 0);
25889d3c 752#endif
f6bcfd97
BP
753#else
754#ifndef HAVE_BOOL // Can't use bool operator if no native bool type
0a0e6a5b 755 variant = (long) (oleVariant.boolVal != 0);
7be1f0d9 756#else
0a0e6a5b 757 variant = (bool) (oleVariant.boolVal != 0);
f6bcfd97 758#endif
7be1f0d9 759#endif
0a0e6a5b
WS
760 break;
761 }
762 case VT_R8:
763 {
764 variant = oleVariant.dblVal;
765 break;
766 }
767 case VT_ARRAY:
768 {
769 variant.ClearList();
770
771 int cDims, cElements, i;
772 VARIANTARG* pvdata;
773
774 // Iterate the dimensions: number of elements is x*y*z
775 for (cDims = 0, cElements = 1;
776 cDims < oleVariant.parray->cDims; cDims ++)
777 cElements *= oleVariant.parray->rgsabound[cDims].cElements;
778
779 // Get a pointer to the data
780 HRESULT hr = SafeArrayAccessData(oleVariant.parray, (void HUGEP* FAR*) & pvdata);
781 if (hr != NOERROR)
782 return false;
783 // Iterate the data.
784 for (i = 0; i < cElements; i++)
785 {
786 VARIANTARG& oleElement = pvdata[i];
787 wxVariant vElement;
788 if (!wxConvertOleToVariant(oleElement, vElement))
789 return false;
790
791 variant.Append(vElement);
792 }
793 SafeArrayUnaccessData(oleVariant.parray);
794 break;
795 }
796 case VT_DISPATCH:
797 {
798 variant = (void*) oleVariant.pdispVal;
799 break;
800 }
801 case VT_NULL:
802 {
803 variant.MakeNull();
804 break;
805 }
806 case VT_EMPTY:
807 {
808 break; // Ignore Empty Variant, used only during destruction of objects
809 }
810 default:
811 {
812 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type"));
813 return false;
814 }
815 }
816 return true;
d980b3e1
JS
817}
818
ed5317e5 819BSTR wxConvertStringToOle(const wxString& str)
d980b3e1
JS
820{
821/*
0a0e6a5b
WS
822 unsigned int len = strlen((const char*) str);
823 unsigned short* s = new unsigned short[len*2+2];
824 unsigned int i;
825 memset(s, 0, len*2+2);
826 for (i=0; i < len; i++)
827 s[i*2] = str[i];
d980b3e1 828*/
0a0e6a5b
WS
829 wxBasicString bstr(str.mb_str());
830 return bstr.Get();
d980b3e1
JS
831}
832
ed5317e5 833wxString wxConvertStringFromOle(BSTR bStr)
d980b3e1 834{
2b5f62a0
VZ
835#if wxUSE_UNICODE
836 wxString str(bStr);
837#else
0a0e6a5b
WS
838 int len = SysStringLen(bStr) + 1;
839 char *buf = new char[len];
840 (void)wcstombs( buf, bStr, len);
841 wxString str(buf);
842 delete[] buf;
2b5f62a0 843#endif
0a0e6a5b 844 return str;
d980b3e1
JS
845}
846
847// ----------------------------------------------------------------------------
ed5317e5 848// wxBasicString
d980b3e1
JS
849// ----------------------------------------------------------------------------
850
851// ctor takes an ANSI string and transforms it to Unicode
ed5317e5
JS
852wxBasicString::wxBasicString(const char *sz)
853{
854 Init(sz);
855}
856
857// ctor takes an ANSI or Unicode string and transforms it to Unicode
858wxBasicString::wxBasicString(const wxString& str)
d980b3e1 859{
ed5317e5
JS
860#if wxUSE_UNICODE
861 m_wzBuf = new OLECHAR[str.Length() + 1];
862 memcpy(m_wzBuf, str.c_str(), str.Length()*2);
863 m_wzBuf[str.Length()] = L'\0';
864#else
865 Init(str.c_str());
866#endif
867}
868
869// Takes an ANSI string and transforms it to Unicode
870void wxBasicString::Init(const char *sz)
871{
872 // get the size of required buffer
873 UINT lenAnsi = strlen(sz);
874#ifdef __MWERKS__
875 UINT lenWide = lenAnsi * 2 ;
876#else
877 UINT lenWide = mbstowcs(NULL, sz, lenAnsi);
878#endif
0a0e6a5b 879
ed5317e5
JS
880 if ( lenWide > 0 ) {
881 m_wzBuf = new OLECHAR[lenWide + 1];
882 mbstowcs(m_wzBuf, sz, lenAnsi);
883 m_wzBuf[lenWide] = L'\0';
884 }
885 else {
886 m_wzBuf = NULL;
887 }
d980b3e1
JS
888}
889
890// dtor frees memory
ed5317e5 891wxBasicString::~wxBasicString()
d980b3e1
JS
892{
893 delete [] m_wzBuf;
894}
895
896/////////////////////////////////////////////////////////////////////////////
897// COleDateTime class HELPERS - implementation
898
899BOOL OleDateFromTm(WORD wYear, WORD wMonth, WORD wDay,
0a0e6a5b 900 WORD wHour, WORD wMinute, WORD wSecond, DATE& dtDest)
d980b3e1 901{
0a0e6a5b
WS
902 // Validate year and month (ignore day of week and milliseconds)
903 if (wYear > 9999 || wMonth < 1 || wMonth > 12)
904 return FALSE;
d980b3e1 905
0a0e6a5b
WS
906 // Check for leap year and set the number of days in the month
907 BOOL bLeapYear = ((wYear & 3) == 0) &&
908 ((wYear % 100) != 0 || (wYear % 400) == 0);
d980b3e1 909
0a0e6a5b
WS
910 int nDaysInMonth =
911 rgMonthDays[wMonth] - rgMonthDays[wMonth-1] +
912 ((bLeapYear && wDay == 29 && wMonth == 2) ? 1 : 0);
d980b3e1 913
0a0e6a5b
WS
914 // Finish validating the date
915 if (wDay < 1 || wDay > nDaysInMonth ||
916 wHour > 23 || wMinute > 59 ||
917 wSecond > 59)
918 {
919 return FALSE;
920 }
d980b3e1 921
0a0e6a5b
WS
922 // Cache the date in days and time in fractional days
923 long nDate;
924 double dblTime;
d980b3e1 925
0a0e6a5b
WS
926 //It is a valid date; make Jan 1, 1AD be 1
927 nDate = wYear*365L + wYear/4 - wYear/100 + wYear/400 +
928 rgMonthDays[wMonth-1] + wDay;
d980b3e1 929
0a0e6a5b
WS
930 // If leap year and it's before March, subtract 1:
931 if (wMonth <= 2 && bLeapYear)
932 --nDate;
d980b3e1 933
0a0e6a5b
WS
934 // Offset so that 12/30/1899 is 0
935 nDate -= 693959L;
d980b3e1 936
0a0e6a5b
WS
937 dblTime = (((long)wHour * 3600L) + // hrs in seconds
938 ((long)wMinute * 60L) + // mins in seconds
939 ((long)wSecond)) / 86400.;
d980b3e1 940
0a0e6a5b 941 dtDest = (double) nDate + ((nDate >= 0) ? dblTime : -dblTime);
d980b3e1 942
0a0e6a5b 943 return TRUE;
d980b3e1
JS
944}
945
946BOOL TmFromOleDate(DATE dtSrc, struct tm& tmDest)
947{
0a0e6a5b
WS
948 // The legal range does not actually span year 0 to 9999.
949 if (dtSrc > MAX_DATE || dtSrc < MIN_DATE) // about year 100 to about 9999
950 return FALSE;
951
952 long nDaysAbsolute; // Number of days since 1/1/0
953 long nSecsInDay; // Time in seconds since midnight
954 long nMinutesInDay; // Minutes in day
955
956 long n400Years; // Number of 400 year increments since 1/1/0
957 long n400Century; // Century within 400 year block (0,1,2 or 3)
958 long n4Years; // Number of 4 year increments since 1/1/0
959 long n4Day; // Day within 4 year block
960 // (0 is 1/1/yr1, 1460 is 12/31/yr4)
961 long n4Yr; // Year within 4 year block (0,1,2 or 3)
962 BOOL bLeap4 = TRUE; // TRUE if 4 year block includes leap year
963
964 double dblDate = dtSrc; // tempory serial date
965
966 // Round to the second
967 dblDate += ((dtSrc > 0.0) ? HALF_SECOND : -HALF_SECOND);
968
969 nDaysAbsolute = (long)dblDate + 693959L; // Add days from 1/1/0 to 12/30/1899
970
971 dblDate = fabs(dblDate);
972 nSecsInDay = (long)((dblDate - floor(dblDate)) * 86400.);
973
974 // Calculate the day of week (sun=1, mon=2...)
975 // -1 because 1/1/0 is Sat. +1 because we want 1-based
976 tmDest.tm_wday = (int)((nDaysAbsolute - 1) % 7L) + 1;
977
978 // Leap years every 4 yrs except centuries not multiples of 400.
979 n400Years = (long)(nDaysAbsolute / 146097L);
980
981 // Set nDaysAbsolute to day within 400-year block
982 nDaysAbsolute %= 146097L;
983
984 // -1 because first century has extra day
985 n400Century = (long)((nDaysAbsolute - 1) / 36524L);
986
987 // Non-leap century
988 if (n400Century != 0)
989 {
990 // Set nDaysAbsolute to day within century
991 nDaysAbsolute = (nDaysAbsolute - 1) % 36524L;
992
993 // +1 because 1st 4 year increment has 1460 days
994 n4Years = (long)((nDaysAbsolute + 1) / 1461L);
995
996 if (n4Years != 0)
997 n4Day = (long)((nDaysAbsolute + 1) % 1461L);
998 else
999 {
1000 bLeap4 = FALSE;
1001 n4Day = (long)nDaysAbsolute;
1002 }
1003 }
1004 else
1005 {
1006 // Leap century - not special case!
1007 n4Years = (long)(nDaysAbsolute / 1461L);
1008 n4Day = (long)(nDaysAbsolute % 1461L);
1009 }
1010
1011 if (bLeap4)
1012 {
1013 // -1 because first year has 366 days
1014 n4Yr = (n4Day - 1) / 365;
1015
1016 if (n4Yr != 0)
1017 n4Day = (n4Day - 1) % 365;
1018 }
1019 else
1020 {
1021 n4Yr = n4Day / 365;
1022 n4Day %= 365;
1023 }
1024
1025 // n4Day is now 0-based day of year. Save 1-based day of year, year number
1026 tmDest.tm_yday = (int)n4Day + 1;
1027 tmDest.tm_year = n400Years * 400 + n400Century * 100 + n4Years * 4 + n4Yr;
1028
1029 // Handle leap year: before, on, and after Feb. 29.
1030 if (n4Yr == 0 && bLeap4)
1031 {
1032 // Leap Year
1033 if (n4Day == 59)
1034 {
1035 /* Feb. 29 */
1036 tmDest.tm_mon = 2;
1037 tmDest.tm_mday = 29;
1038 goto DoTime;
1039 }
1040
1041 // Pretend it's not a leap year for month/day comp.
1042 if (n4Day >= 60)
1043 --n4Day;
1044 }
1045
1046 // Make n4DaY a 1-based day of non-leap year and compute
1047 // month/day for everything but Feb. 29.
1048 ++n4Day;
1049
1050 // Month number always >= n/32, so save some loop time */
1051 for (tmDest.tm_mon = (n4Day >> 5) + 1;
1052 n4Day > rgMonthDays[tmDest.tm_mon]; tmDest.tm_mon++)
1053 ;
1054
1055 tmDest.tm_mday = (int)(n4Day - rgMonthDays[tmDest.tm_mon-1]);
d980b3e1
JS
1056
1057DoTime:
0a0e6a5b
WS
1058 if (nSecsInDay == 0)
1059 tmDest.tm_hour = tmDest.tm_min = tmDest.tm_sec = 0;
1060 else
1061 {
1062 tmDest.tm_sec = (int)nSecsInDay % 60L;
1063 nMinutesInDay = nSecsInDay / 60L;
1064 tmDest.tm_min = (int)nMinutesInDay % 60;
1065 tmDest.tm_hour = (int)nMinutesInDay / 60;
1066 }
1067
1068 return TRUE;
d980b3e1
JS
1069}
1070
b21624e7
VZ
1071// this function is not used
1072#if 0
d980b3e1
JS
1073void TmConvertToStandardFormat(struct tm& tmSrc)
1074{
0a0e6a5b
WS
1075 // Convert afx internal tm to format expected by runtimes (_tcsftime, etc)
1076 tmSrc.tm_year -= 1900; // year is based on 1900
1077 tmSrc.tm_mon -= 1; // month of year is 0-based
1078 tmSrc.tm_wday -= 1; // day of week is 0-based
1079 tmSrc.tm_yday -= 1; // day of year is 0-based
d980b3e1
JS
1080}
1081
1082double DoubleFromDate(DATE dt)
1083{
0a0e6a5b
WS
1084 // No problem if positive
1085 if (dt >= 0)
1086 return dt;
1087
1088 // If negative, must convert since negative dates not continuous
1089 // (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25)
1090 double temp = ceil(dt);
1091 return temp - (dt - temp);
d980b3e1
JS
1092}
1093
1094DATE DateFromDouble(double dbl)
1095{
0a0e6a5b
WS
1096 // No problem if positive
1097 if (dbl >= 0)
1098 return dbl;
1099
1100 // If negative, must convert since negative dates not continuous
1101 // (examples: -.75 to -1.25, -.50 to -1.50, -.25 to -1.75)
1102 double temp = floor(dbl); // dbl is now whole part
1103 return temp + (temp - dbl);
d980b3e1 1104}
b21624e7 1105#endif // 0
d980b3e1
JS
1106
1107/*
1108 * ClearVariant
1109 *
1110 * Zeros a variant structure without regard to current contents
1111 */
1112static void ClearVariant(VARIANTARG *pvarg)
1113{
0a0e6a5b
WS
1114 pvarg->vt = VT_EMPTY;
1115 pvarg->wReserved1 = 0;
1116 pvarg->wReserved2 = 0;
1117 pvarg->wReserved3 = 0;
1118 pvarg->lVal = 0;
d980b3e1
JS
1119}
1120
1121/*
1122 * ReleaseVariant
1123 *
1124 * Clears a particular variant structure and releases any external objects
1125 * or memory contained in the variant. Supports the data types listed above.
1126 */
1127static void ReleaseVariant(VARIANTARG *pvarg)
1128{
0a0e6a5b
WS
1129 VARTYPE vt;
1130 VARIANTARG _huge *pvargArray;
1131 long lLBound, lUBound, l;
1132
1133 vt = pvarg->vt & 0xfff; // mask off flags
1134
1135 // check if an array. If so, free its contents, then the array itself.
1136 if (V_ISARRAY(pvarg))
1137 {
1138 // variant arrays are all this routine currently knows about. Since a
1139 // variant can contain anything (even other arrays), call ourselves
1140 // recursively.
1141 if (vt == VT_VARIANT)
1142 {
1143 SafeArrayGetLBound(pvarg->parray, 1, &lLBound);
1144 SafeArrayGetUBound(pvarg->parray, 1, &lUBound);
1145
1146 if (lUBound > lLBound)
1147 {
1148 lUBound -= lLBound;
1149
1150 SafeArrayAccessData(pvarg->parray, (void**)&pvargArray);
1151
1152 for (l = 0; l < lUBound; l++)
1153 {
1154 ReleaseVariant(pvargArray);
1155 pvargArray++;
1156 }
1157
1158 SafeArrayUnaccessData(pvarg->parray);
1159 }
1160 }
1161 else
1162 {
1163 wxLogWarning(wxT("ReleaseVariant: Array contains non-variant type"));
1164 }
1165
1166 // Free the array itself.
1167 SafeArrayDestroy(pvarg->parray);
1168 }
1169 else
1170 {
1171 switch (vt)
1172 {
1173 case VT_DISPATCH:
1174 if (pvarg->pdispVal)
1175 pvarg->pdispVal->Release();
1176 break;
1177
1178 case VT_BSTR:
1179 SysFreeString(pvarg->bstrVal);
1180 break;
1181
1182 case VT_I2:
1183 case VT_BOOL:
1184 case VT_R8:
1185 case VT_ERROR: // to avoid erroring on an error return from Excel
1186 // no work for these types
1187 break;
1188
1189 default:
1190 wxLogWarning(wxT("ReleaseVariant: Unknown type"));
1191 break;
1192 }
1193 }
1194
1195 ClearVariant(pvarg);
d980b3e1
JS
1196}
1197
1198#if 0
1199
1200void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr)
1201{
0a0e6a5b
WS
1202 TCHAR szBuf[512];
1203
1204 switch (GetScode(hr))
1205 {
1206 case DISP_E_UNKNOWNNAME:
1207 wsprintf(szBuf, L"%s: Unknown name or named argument.", szMember);
1208 break;
1209
1210 case DISP_E_BADPARAMCOUNT:
1211 wsprintf(szBuf, L"%s: Incorrect number of arguments.", szMember);
1212 break;
1213
1214 case DISP_E_EXCEPTION:
1215 wsprintf(szBuf, L"%s: Error %d: ", szMember, pexcep->wCode);
1216 if (pexcep->bstrDescription != NULL)
1217 lstrcat(szBuf, pexcep->bstrDescription);
1218 else
1219 lstrcat(szBuf, L"<<No Description>>");
1220 break;
1221
1222 case DISP_E_MEMBERNOTFOUND:
1223 wsprintf(szBuf, L"%s: method or property not found.", szMember);
1224 break;
1225
1226 case DISP_E_OVERFLOW:
1227 wsprintf(szBuf, L"%s: Overflow while coercing argument values.", szMember);
1228 break;
1229
1230 case DISP_E_NONAMEDARGS:
1231 wsprintf(szBuf, L"%s: Object implementation does not support named arguments.",
1232 szMember);
1233 break;
1234
1235 case DISP_E_UNKNOWNLCID:
1236 wsprintf(szBuf, L"%s: The locale ID is unknown.", szMember);
1237 break;
1238
1239 case DISP_E_PARAMNOTOPTIONAL:
1240 wsprintf(szBuf, L"%s: Missing a required parameter.", szMember);
1241 break;
1242
1243 case DISP_E_PARAMNOTFOUND:
1244 wsprintf(szBuf, L"%s: Argument not found, argument %d.", szMember, uiArgErr);
1245 break;
1246
1247 case DISP_E_TYPEMISMATCH:
1248 wsprintf(szBuf, L"%s: Type mismatch, argument %d.", szMember, uiArgErr);
1249 break;
1250
1251 default:
1252 wsprintf(szBuf, L"%s: Unknown error occured.", szMember);
1253 break;
1254 }
1255
1256 wxLogWarning(szBuf);
d980b3e1
JS
1257}
1258
1259#endif
1260
6b12494d 1261#endif // wxUSE_OLE && !(defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) && !defined(__CYGWIN10__)
457e6c54 1262