]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/oleutils.cpp
Select the entire spin control contents when changing its value.
[wxWidgets.git] / src / msw / ole / oleutils.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/ole/oleutils.cpp
3 // Purpose: implementation of OLE helper functions
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 19.02.98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // Declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #if defined(__BORLANDC__)
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_OLE
28
29 #ifndef WX_PRECOMP
30 #include "wx/log.h"
31 #endif
32
33 #ifndef __CYGWIN10__
34
35 #include "wx/msw/private.h"
36
37 #ifdef __WXWINCE__
38 #include <winreg.h>
39 #include <ole2.h>
40
41 #define GUID_DEFINED
42 #define UUID_DEFINED
43 #endif
44
45 // OLE
46 #ifndef __WXWINCE__
47 #include "wx/msw/ole/uuid.h"
48 #endif
49
50 #include "wx/msw/ole/oleutils.h"
51 #include "wx/msw/ole/safearray.h"
52
53 #if defined(__VISUALC__) && (__VISUALC__ > 1000)
54 #include <docobj.h>
55 #endif
56
57 // ============================================================================
58 // Implementation
59 // ============================================================================
60
61 // return true if the iid is in the array
62 WXDLLEXPORT bool IsIidFromList(REFIID riid, const IID *aIids[], size_t nCount)
63 {
64 for ( size_t i = 0; i < nCount; i++ ) {
65 if ( riid == *aIids[i] )
66 return true;
67 }
68
69 return false;
70 }
71
72 WXDLLEXPORT BSTR wxConvertStringToOle(const wxString& str)
73 {
74 return wxBasicString(str).Get();
75 }
76
77 WXDLLEXPORT wxString wxConvertStringFromOle(BSTR bStr)
78 {
79 // NULL BSTR is equivalent to an empty string (this is the convention used
80 // by VB and hence we must follow it)
81 if ( !bStr )
82 return wxString();
83
84 const int len = SysStringLen(bStr);
85
86 #if wxUSE_UNICODE
87 wxString str(bStr, len);
88 #else
89 wxString str;
90 if (len)
91 {
92 wxStringBufferLength buf(str, len); // asserts if len == 0
93 buf.SetLength(WideCharToMultiByte(CP_ACP, 0 /* no flags */,
94 bStr, len /* not necessarily NUL-terminated */,
95 buf, len,
96 NULL, NULL /* no default char */));
97 }
98 #endif
99
100 return str;
101 }
102
103 // ----------------------------------------------------------------------------
104 // wxBasicString
105 // ----------------------------------------------------------------------------
106
107 wxBasicString::wxBasicString(const wxString& str)
108 {
109 m_bstrBuf = SysAllocString(str.wc_str(*wxConvCurrent));
110 }
111
112 wxBasicString::wxBasicString(const wxBasicString& src)
113 {
114 m_bstrBuf = src.Get();
115 }
116
117 wxBasicString& wxBasicString::operator=(const wxBasicString& src)
118 {
119 SysReAllocString(&m_bstrBuf, src);
120 return *this;
121 }
122
123 wxBasicString::~wxBasicString()
124 {
125 SysFreeString(m_bstrBuf);
126 }
127
128
129 // ----------------------------------------------------------------------------
130 // Convert variants
131 // ----------------------------------------------------------------------------
132
133 #if wxUSE_VARIANT
134
135 // ----------------------------------------------------------------------------
136 // wxVariantDataCurrency
137 // ----------------------------------------------------------------------------
138
139
140 #if wxUSE_ANY
141
142 bool wxVariantDataCurrency::GetAsAny(wxAny* any) const
143 {
144 *any = m_value;
145 return true;
146 }
147
148 wxVariantData* wxVariantDataCurrency::VariantDataFactory(const wxAny& any)
149 {
150 return new wxVariantDataCurrency(wxANY_AS(any, CURRENCY));
151 }
152
153 REGISTER_WXANY_CONVERSION(CURRENCY, wxVariantDataCurrency)
154
155 #endif // wxUSE_ANY
156
157 bool wxVariantDataCurrency::Eq(wxVariantData& data) const
158 {
159 wxASSERT_MSG( (data.GetType() == wxS("currency")),
160 "wxVariantDataCurrency::Eq: argument mismatch" );
161
162 wxVariantDataCurrency& otherData = (wxVariantDataCurrency&) data;
163
164 return otherData.m_value.int64 == m_value.int64;
165 }
166
167 #if wxUSE_STD_IOSTREAM
168 bool wxVariantDataCurrency::Write(wxSTD ostream& str) const
169 {
170 wxString s;
171 Write(s);
172 str << s;
173 return true;
174 }
175 #endif
176
177 bool wxVariantDataCurrency::Write(wxString& str) const
178 {
179 BSTR bStr = NULL;
180 if ( SUCCEEDED(VarBstrFromCy(m_value, LOCALE_USER_DEFAULT, 0, &bStr)) )
181 {
182 str = wxConvertStringFromOle(bStr);
183 SysFreeString(bStr);
184 return true;
185 }
186 return false;
187 }
188
189 // ----------------------------------------------------------------------------
190 // wxVariantDataErrorCode
191 // ----------------------------------------------------------------------------
192
193 #if wxUSE_ANY
194
195 bool wxVariantDataErrorCode::GetAsAny(wxAny* any) const
196 {
197 *any = m_value;
198 return true;
199 }
200
201 wxVariantData* wxVariantDataErrorCode::VariantDataFactory(const wxAny& any)
202 {
203 return new wxVariantDataErrorCode(wxANY_AS(any, SCODE));
204 }
205
206 REGISTER_WXANY_CONVERSION(SCODE, wxVariantDataErrorCode)
207
208 #endif // wxUSE_ANY
209
210 bool wxVariantDataErrorCode::Eq(wxVariantData& data) const
211 {
212 wxASSERT_MSG( (data.GetType() == wxS("errorcode")),
213 "wxVariantDataErrorCode::Eq: argument mismatch" );
214
215 wxVariantDataErrorCode& otherData = (wxVariantDataErrorCode&) data;
216
217 return otherData.m_value == m_value;
218 }
219
220 #if wxUSE_STD_IOSTREAM
221 bool wxVariantDataErrorCode::Write(wxSTD ostream& str) const
222 {
223 wxString s;
224 Write(s);
225 str << s;
226 return true;
227 }
228 #endif
229
230 bool wxVariantDataErrorCode::Write(wxString& str) const
231 {
232 str << m_value;
233 return true;
234 }
235
236
237 // ----------------------------------------------------------------------------
238 // wxVariantDataSafeArray
239 // ----------------------------------------------------------------------------
240
241 #if wxUSE_ANY
242
243 bool wxVariantDataSafeArray::GetAsAny(wxAny* any) const
244 {
245 *any = m_value;
246 return true;
247 }
248
249 wxVariantData* wxVariantDataSafeArray::VariantDataFactory(const wxAny& any)
250 {
251 return new wxVariantDataSafeArray(wxANY_AS(any, SAFEARRAY*));
252 }
253
254 REGISTER_WXANY_CONVERSION(SAFEARRAY*, wxVariantDataSafeArray)
255
256 #endif // wxUSE_ANY
257
258 bool wxVariantDataSafeArray::Eq(wxVariantData& data) const
259 {
260 wxASSERT_MSG( (data.GetType() == wxS("safearray")),
261 "wxVariantDataSafeArray::Eq: argument mismatch" );
262
263 wxVariantDataSafeArray& otherData = (wxVariantDataSafeArray&) data;
264
265 return otherData.m_value == m_value;
266 }
267
268 #if wxUSE_STD_IOSTREAM
269 bool wxVariantDataSafeArray::Write(wxSTD ostream& str) const
270 {
271 wxString s;
272 Write(s);
273 str << s;
274 return true;
275 }
276 #endif
277
278 bool wxVariantDataSafeArray::Write(wxString& str) const
279 {
280 str.Printf(wxS("SAFEARRAY: %p"), (void*)m_value);
281 return true;
282 }
283
284 WXDLLEXPORT bool wxConvertVariantToOle(const wxVariant& variant, VARIANTARG& oleVariant)
285 {
286 VariantInit(&oleVariant);
287 if (variant.IsNull())
288 {
289 oleVariant.vt = VT_NULL;
290 return true;
291 }
292
293 wxString type(variant.GetType());
294
295 if (type == wxT("errorcode"))
296 {
297 wxVariantDataErrorCode* const
298 ec = wxStaticCastVariantData(variant.GetData(),
299 wxVariantDataErrorCode);
300 oleVariant.vt = VT_ERROR;
301 oleVariant.scode = ec->GetValue();
302 }
303 else if (type == wxT("currency"))
304 {
305 wxVariantDataCurrency* const
306 c = wxStaticCastVariantData(variant.GetData(),
307 wxVariantDataCurrency);
308 oleVariant.vt = VT_CY;
309 oleVariant.cyVal = c->GetValue();
310 }
311 else if (type == wxT("safearray"))
312 {
313 wxVariantDataSafeArray* const
314 vsa = wxStaticCastVariantData(variant.GetData(),
315 wxVariantDataSafeArray);
316 SAFEARRAY* psa = vsa->GetValue();
317 VARTYPE vt;
318
319 wxCHECK(psa, false);
320 HRESULT hr = SafeArrayGetVartype(psa, &vt);
321 if ( FAILED(hr) )
322 {
323 wxLogApiError(wxS("SafeArrayGetVartype()"), hr);
324 SafeArrayDestroy(psa);
325 return false;
326 }
327 oleVariant.vt = vt | VT_ARRAY;
328 oleVariant.parray = psa;
329 }
330 else if (type == wxT("long"))
331 {
332 oleVariant.vt = VT_I4;
333 oleVariant.lVal = variant.GetLong() ;
334 }
335 // Original VC6 came with SDK too old to contain VARIANT::llVal declaration
336 // and there doesn't seem to be any way to test for it as Microsoft simply
337 // added it to the later version of oaidl.h without changing anything else.
338 // So assume it's not present for VC6, even though it might be if an
339 // updated SDK is used. In this case the user would need to disable this
340 // check himself.
341 #if wxUSE_LONGLONG && !defined(__VISUALC6__)
342 else if (type == wxT("longlong"))
343 {
344 oleVariant.vt = VT_I8;
345 oleVariant.llVal = variant.GetLongLong().GetValue();
346 }
347 #endif
348 else if (type == wxT("char"))
349 {
350 oleVariant.vt=VT_I1; // Signed Char
351 oleVariant.cVal=variant.GetChar();
352 }
353 else if (type == wxT("double"))
354 {
355 oleVariant.vt = VT_R8;
356 oleVariant.dblVal = variant.GetDouble();
357 }
358 else if (type == wxT("bool"))
359 {
360 oleVariant.vt = VT_BOOL;
361 oleVariant.boolVal = variant.GetBool() ? VARIANT_TRUE : VARIANT_FALSE;
362 }
363 else if (type == wxT("string"))
364 {
365 wxString str( variant.GetString() );
366 oleVariant.vt = VT_BSTR;
367 oleVariant.bstrVal = wxConvertStringToOle(str);
368 }
369 #if wxUSE_DATETIME
370 else if (type == wxT("datetime"))
371 {
372 wxDateTime date( variant.GetDateTime() );
373 oleVariant.vt = VT_DATE;
374
375 SYSTEMTIME st;
376 date.GetAsMSWSysTime(&st);
377
378 SystemTimeToVariantTime(&st, &oleVariant.date);
379 }
380 #endif
381 else if (type == wxT("void*"))
382 {
383 oleVariant.vt = VT_DISPATCH;
384 oleVariant.pdispVal = (IDispatch*) variant.GetVoidPtr();
385 }
386 else if (type == wxT("list"))
387 {
388 wxSafeArray<VT_VARIANT> safeArray;
389 if (!safeArray.CreateFromListVariant(variant))
390 return false;
391
392 oleVariant.vt = VT_VARIANT | VT_ARRAY;
393 oleVariant.parray = safeArray.Detach();
394 }
395 else if (type == wxT("arrstring"))
396 {
397 wxSafeArray<VT_BSTR> safeArray;
398
399 if (!safeArray.CreateFromArrayString(variant.GetArrayString()))
400 return false;
401
402 oleVariant.vt = VT_BSTR | VT_ARRAY;
403 oleVariant.parray = safeArray.Detach();
404 }
405 else
406 {
407 oleVariant.vt = VT_NULL;
408 return false;
409 }
410 return true;
411 }
412
413 #ifndef VT_TYPEMASK
414 #define VT_TYPEMASK 0xfff
415 #endif
416
417 WXDLLEXPORT bool
418 wxConvertOleToVariant(const VARIANTARG& oleVariant, wxVariant& variant)
419 {
420 bool ok = true;
421 if ( oleVariant.vt & VT_ARRAY )
422 {
423 // TODO: We currently return arrays as wxVariant of the list type
424 // containing the flattened form of array but we should allow
425 // getting it as wxVariantDataSafeArray instead. Doing this is
426 // simple, we'd just need to do something like this:
427 //
428 // if ( oleVariant.parray && SafeArrayGetDim(oleVariant.parray) > 1 )
429 // {
430 // variant.SetData(new wxVariantDataSafeArray(oleVariant.parray));
431 // }
432 //
433 // but currently we don't do it for compatibility reasons.
434 switch (oleVariant.vt & VT_TYPEMASK)
435 {
436 case VT_I2:
437 ok = wxSafeArray<VT_I2>::ConvertToVariant(oleVariant.parray, variant);
438 break;
439 case VT_I4:
440 ok = wxSafeArray<VT_I4>::ConvertToVariant(oleVariant.parray, variant);
441 break;
442 case VT_R4:
443 ok = wxSafeArray<VT_R4>::ConvertToVariant(oleVariant.parray, variant);
444 break;
445 case VT_R8:
446 ok = wxSafeArray<VT_R8>::ConvertToVariant(oleVariant.parray, variant);
447 break;
448 case VT_VARIANT:
449 ok = wxSafeArray<VT_VARIANT>::ConvertToVariant(oleVariant.parray, variant);
450 break;
451 case VT_BSTR:
452 {
453 wxArrayString strings;
454 if ( wxSafeArray<VT_BSTR>::ConvertToArrayString(oleVariant.parray, strings) )
455 variant = strings;
456 else
457 ok = false;
458 }
459 break;
460 default:
461 ok = false;
462 break;
463 }
464 if ( !ok )
465 {
466 wxLogDebug(wxT("unhandled VT_ARRAY type %x in wxConvertOleToVariant"),
467 oleVariant.vt & VT_TYPEMASK);
468 variant = wxVariant();
469 }
470 }
471 else if ( oleVariant.vt & VT_BYREF )
472 {
473 switch ( oleVariant.vt & VT_TYPEMASK )
474 {
475 case VT_VARIANT:
476 {
477 VARIANTARG& oleReference = *((LPVARIANT)oleVariant.byref);
478 if (!wxConvertOleToVariant(oleReference,variant))
479 return false;
480 break;
481 }
482
483 default:
484 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: [as yet] unhandled reference %X"),
485 oleVariant.vt);
486 return false;
487 }
488 }
489 else // simply type (not array or reference)
490 {
491 switch ( oleVariant.vt & VT_TYPEMASK )
492 {
493 case VT_ERROR:
494 variant.SetData(new wxVariantDataErrorCode(oleVariant.scode));
495 break;
496
497 case VT_CY:
498 variant.SetData(new wxVariantDataCurrency(oleVariant.cyVal));
499 break;
500
501 case VT_BSTR:
502 {
503 wxString str(wxConvertStringFromOle(oleVariant.bstrVal));
504 variant = str;
505 }
506 break;
507
508 case VT_DATE:
509 #if wxUSE_DATETIME
510 {
511 SYSTEMTIME st;
512 VariantTimeToSystemTime(oleVariant.date, &st);
513
514 wxDateTime date;
515 date.SetFromMSWSysTime(st);
516 variant = date;
517 }
518 #endif // wxUSE_DATETIME
519 break;
520
521 // See the comment before the __VISUALC6__ test above.
522 #if wxUSE_LONGLONG && !defined(__VISUALC6__)
523 case VT_I8:
524 variant = wxLongLong(oleVariant.llVal);
525 break;
526 #endif // wxUSE_LONGLONG
527
528 case VT_I4:
529 variant = (long) oleVariant.lVal;
530 break;
531
532 case VT_I2:
533 variant = (long) oleVariant.iVal;
534 break;
535
536 case VT_BOOL:
537 variant = oleVariant.boolVal != 0;
538 break;
539
540 case VT_R4:
541 variant = oleVariant.fltVal;
542 break;
543
544 case VT_R8:
545 variant = oleVariant.dblVal;
546 break;
547
548 case VT_DISPATCH:
549 variant = (void*) oleVariant.pdispVal;
550 break;
551
552 case VT_NULL:
553 variant.MakeNull();
554 break;
555
556 case VT_EMPTY:
557 break; // Ignore Empty Variant, used only during destruction of objects
558
559 default:
560 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type %X -> %X"),
561 oleVariant.vt,oleVariant.vt&VT_TYPEMASK);
562 return false;
563 }
564 }
565
566 return ok;
567 }
568
569 #endif // wxUSE_VARIANT
570
571
572 // ----------------------------------------------------------------------------
573 // Debug support
574 // ----------------------------------------------------------------------------
575
576 #if wxUSE_DATAOBJ
577
578 #if wxDEBUG_LEVEL && (( defined(__VISUALC__) && (__VISUALC__ > 1000) ))
579 static wxString GetIidName(REFIID riid)
580 {
581 // an association between symbolic name and numeric value of an IID
582 struct KNOWN_IID {
583 const IID *pIid;
584 const wxChar *szName;
585 };
586
587 // construct the table containing all known interfaces
588 #define ADD_KNOWN_IID(name) { &IID_I##name, wxT(#name) }
589
590 static const KNOWN_IID aKnownIids[] = {
591 ADD_KNOWN_IID(AdviseSink),
592 ADD_KNOWN_IID(AdviseSink2),
593 ADD_KNOWN_IID(BindCtx),
594 ADD_KNOWN_IID(ClassFactory),
595 #if ( !defined( __VISUALC__) || (__VISUALC__!=1010) )
596 ADD_KNOWN_IID(ContinueCallback),
597 ADD_KNOWN_IID(EnumOleDocumentViews),
598 ADD_KNOWN_IID(OleCommandTarget),
599 ADD_KNOWN_IID(OleDocument),
600 ADD_KNOWN_IID(OleDocumentSite),
601 ADD_KNOWN_IID(OleDocumentView),
602 ADD_KNOWN_IID(Print),
603 #endif
604 ADD_KNOWN_IID(DataAdviseHolder),
605 ADD_KNOWN_IID(DataObject),
606 ADD_KNOWN_IID(Debug),
607 ADD_KNOWN_IID(DebugStream),
608 ADD_KNOWN_IID(DfReserved1),
609 ADD_KNOWN_IID(DfReserved2),
610 ADD_KNOWN_IID(DfReserved3),
611 ADD_KNOWN_IID(Dispatch),
612 ADD_KNOWN_IID(DropSource),
613 ADD_KNOWN_IID(DropTarget),
614 ADD_KNOWN_IID(EnumCallback),
615 ADD_KNOWN_IID(EnumFORMATETC),
616 ADD_KNOWN_IID(EnumGeneric),
617 ADD_KNOWN_IID(EnumHolder),
618 ADD_KNOWN_IID(EnumMoniker),
619 ADD_KNOWN_IID(EnumOLEVERB),
620 ADD_KNOWN_IID(EnumSTATDATA),
621 ADD_KNOWN_IID(EnumSTATSTG),
622 ADD_KNOWN_IID(EnumString),
623 ADD_KNOWN_IID(EnumUnknown),
624 ADD_KNOWN_IID(EnumVARIANT),
625 ADD_KNOWN_IID(ExternalConnection),
626 ADD_KNOWN_IID(InternalMoniker),
627 ADD_KNOWN_IID(LockBytes),
628 ADD_KNOWN_IID(Malloc),
629 ADD_KNOWN_IID(Marshal),
630 ADD_KNOWN_IID(MessageFilter),
631 ADD_KNOWN_IID(Moniker),
632 ADD_KNOWN_IID(OleAdviseHolder),
633 ADD_KNOWN_IID(OleCache),
634 ADD_KNOWN_IID(OleCache2),
635 ADD_KNOWN_IID(OleCacheControl),
636 ADD_KNOWN_IID(OleClientSite),
637 ADD_KNOWN_IID(OleContainer),
638 ADD_KNOWN_IID(OleInPlaceActiveObject),
639 ADD_KNOWN_IID(OleInPlaceFrame),
640 ADD_KNOWN_IID(OleInPlaceObject),
641 ADD_KNOWN_IID(OleInPlaceSite),
642 ADD_KNOWN_IID(OleInPlaceUIWindow),
643 ADD_KNOWN_IID(OleItemContainer),
644 ADD_KNOWN_IID(OleLink),
645 ADD_KNOWN_IID(OleManager),
646 ADD_KNOWN_IID(OleObject),
647 ADD_KNOWN_IID(OlePresObj),
648 ADD_KNOWN_IID(OleWindow),
649 ADD_KNOWN_IID(PSFactory),
650 ADD_KNOWN_IID(ParseDisplayName),
651 ADD_KNOWN_IID(Persist),
652 ADD_KNOWN_IID(PersistFile),
653 ADD_KNOWN_IID(PersistStorage),
654 ADD_KNOWN_IID(PersistStream),
655 ADD_KNOWN_IID(ProxyManager),
656 ADD_KNOWN_IID(RootStorage),
657 ADD_KNOWN_IID(RpcChannel),
658 ADD_KNOWN_IID(RpcProxy),
659 ADD_KNOWN_IID(RpcStub),
660 ADD_KNOWN_IID(RunnableObject),
661 ADD_KNOWN_IID(RunningObjectTable),
662 ADD_KNOWN_IID(StdMarshalInfo),
663 ADD_KNOWN_IID(Storage),
664 ADD_KNOWN_IID(Stream),
665 ADD_KNOWN_IID(StubManager),
666 ADD_KNOWN_IID(Unknown),
667 ADD_KNOWN_IID(ViewObject),
668 ADD_KNOWN_IID(ViewObject2),
669 };
670
671 // don't clobber preprocessor name space
672 #undef ADD_KNOWN_IID
673
674 // try to find the interface in the table
675 for ( size_t ui = 0; ui < WXSIZEOF(aKnownIids); ui++ ) {
676 if ( riid == *aKnownIids[ui].pIid ) {
677 return aKnownIids[ui].szName;
678 }
679 }
680
681 #ifndef __WXWINCE__
682 // unknown IID, just transform to string
683 Uuid uuid(riid);
684 return wxString((const wxChar *)uuid);
685 #else
686 return wxEmptyString;
687 #endif
688 }
689
690 WXDLLEXPORT void wxLogQueryInterface(const wxChar *szInterface, REFIID riid)
691 {
692 wxLogTrace(wxTRACE_OleCalls, wxT("%s::QueryInterface (iid = %s)"),
693 szInterface, GetIidName(riid).c_str());
694 }
695
696 WXDLLEXPORT void wxLogAddRef(const wxChar *szInterface, ULONG cRef)
697 {
698 wxLogTrace(wxTRACE_OleCalls, wxT("After %s::AddRef: m_cRef = %d"), szInterface, cRef + 1);
699 }
700
701 WXDLLEXPORT void wxLogRelease(const wxChar *szInterface, ULONG cRef)
702 {
703 wxLogTrace(wxTRACE_OleCalls, wxT("After %s::Release: m_cRef = %d"), szInterface, cRef - 1);
704 }
705
706 #endif // wxDEBUG_LEVEL
707
708 #endif // wxUSE_DATAOBJ
709
710 #endif // __CYGWIN10__
711
712 #endif // wxUSE_OLE