1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ole/automtn.h
3 // Purpose: interface of wxAutomationObject
4 // Author: wxWidgets team
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
10 Automation object creation flags.
12 These flags can be used with wxAutomationObject::GetInstance().
16 enum wxAutomationInstanceFlags
19 Only use the existing instance, never create a new one.
21 This flag can be used to forbid the creation of a new instance if none
24 wxAutomationInstance_UseExistingOnly
= 0,
27 Create a new instance if there are no existing ones.
29 This flag corresponds to the default behaviour of
30 wxAutomationObject::GetInstance() and means that if getting an existing
31 instance failed, we should call wxAutomationObject::CreateInstance() to
34 wxAutomationInstance_CreateIfNeeded
= 1,
37 Do not show an error message if no existing instance is currently
40 All other errors will still be reported as usual.
42 wxAutomationInstance_SilentIfNone
= 2
46 @class wxVariantDataCurrency
48 This class represents a thin wrapper for Microsoft Windows CURRENCY type.
50 It is used for converting between wxVariant and OLE VARIANT
51 with type set to VT_CURRENCY. When wxVariant stores
52 wxVariantDataCurrency, it returns "currency" as its type.
54 An example of setting and getting CURRENCY value to and from wxVariant:
59 // set wxVariant to currency type
60 if ( SUCCEEDED(VarCyFromR8(123.45, &cy)) ) // set cy to 123.45
62 variant.SetData(new wxVariantDataCurrency(cy));
64 // or instead of the line above you could write:
65 // wxVariantDataCurrency wxCy;
67 // variant.SetData(wxCy.Clone());
70 // get CURRENCY value from wxVariant
71 if ( variant.GetType() == "currency" )
73 wxVariantDataCurrency*
74 wxCy = wxDynamicCastVariantData(variant.GetData(), wxVariantDataCurrency);
75 cy = wxCy->GetValue();
85 @see wxAutomationObject, wxVariant, wxVariantData, wxVariantDataErrorCode
87 @header{wx/msw/ole/oleutils.h}
89 class wxVariantDataCurrency
: public wxVariantData
93 Default constructor initializes the object to 0.0.
95 wxVariantDataCurrency();
98 Constructor from CURRENCY.
100 wxVariantDataCurrency(CURRENCY value
);
103 Returns the stored CURRENCY value.
105 CURRENCY
GetValue() const;
108 Sets the stored value to @a value.
110 void SetValue(CURRENCY value
);
113 Returns true if @a data is of wxVariantDataCurency type
114 and contains the same CURRENCY value.
116 virtual bool Eq(wxVariantData
& data
) const;
119 Fills the provided string with the textual representation of this
122 The implementation of this method using @c VarBstrFromCy() Windows API
123 function with LOCALE_USER_DEFAULT.
125 virtual bool Write(wxString
& str
) const;
128 Returns a copy of itself.
130 wxVariantData
* Clone() const;
135 virtual wxString
GetType() const;
138 Converts the value of this object to wxAny.
140 virtual bool GetAsAny(wxAny
* any
) const;
145 @class wxVariantDataErrorCode
147 This class represents a thin wrapper for Microsoft Windows SCODE type
148 (which is the same as HRESULT).
150 It is used for converting between a wxVariant and OLE VARIANT with type set
151 to VT_ERROR. When wxVariant stores wxVariantDataErrorCode, it returns
152 "errorcode" as its type. This class can be used for returning error codes
153 of automation calls or exchanging values with other applications: e.g.
154 Microsoft Excel returns VARIANTs with VT_ERROR type for cell values with
155 errors (one of XlCVError constants, displayed as e.g. "#DIV/0!" or "#REF!"
156 there) etc. See wxVariantDataCurrency for an example of how to exchange
157 values between wxVariant and a native type not directly supported by it.
165 @see wxAutomationObject, wxVariant, wxVariantData, wxVariantDataCurrency
167 @header{wx/msw/ole/oleutils.h}
169 class wxVariantDataErrorCode
: public wxVariantData
173 Constructor initializes the object to @a value or S_OK if no value was
176 wxVariantDataErrorCode(SCODE value
= S_OK
);
179 Returns the stored SCODE value.
181 SCODE
GetValue() const;
184 Set the stored value to @a value.
186 void SetValue(SCODE value
);
189 Returns true if @a data is of wxVariantDataErrorCode type
190 and contains the same SCODE value.
192 virtual bool Eq(wxVariantData
& data
) const;
195 Fills the provided string with the textual representation of this
198 The error code is just a number, so it's output as such.
200 virtual bool Write(wxString
& str
) const;
203 Returns a copy of itself.
205 wxVariantData
* Clone() const;
210 virtual wxString
GetType() const { return wxS("errorcode"); }
213 Converts the value of this object to wxAny.
215 virtual bool GetAsAny(wxAny
* any
) const;
219 @class wxVariantDataSafeArray
221 This class represents a thin wrapper for Microsoft Windows SAFEARRAY type.
223 It is used for converting between wxVariant and OLE VARIANT
224 with type set to VT_ARRAY, which has more than one dimension.
225 When wxVariant stores wxVariantDataSafeArray, it returns "safearray" as its type.
227 wxVariantDataSafeArray does NOT manage the SAFEARRAY it points to.
228 If you want to pass it to a wxAutomationObject as a parameter:
229 -# Assign a SAFEARRAY pointer to it and store it in a wxVariant.
230 -# Call the wxAutomationObject method (CallMethod(), SetProperty() or Invoke())
231 -# wxAutomationObject will destroy the array after the approapriate automation call.
233 An example of creating a 2-dimensional SAFEARRAY containing VARIANTs
234 and storing it in a wxVariant
236 SAFEARRAYBOUND bounds[2]; // 2 dimensions
237 wxSafeArray<VT_VARIANT> safeArray;
238 unsigned rowCount = 1000;
239 unsigned colCount = 20;
241 bounds[0].lLbound = 0; // elements start at 0
242 bounds[0].cElements = rowCount;
243 bounds[1].lLbound = 0; // elements start at 0
244 bounds[1].cElements = colCount;
246 if ( !safeArray.Create(bounds, 2) )
251 for ( unsigned row = 0; row < rowCount; row++ )
254 for ( unsigned col = 0; col < colCount; col++ )
257 if ( !safeArray.SetElement(indices, wxString::Format("R%u C%u", row+1, col+1)) )
261 range.PutProperty("Value", wxVariant(new wxVariantDataSafeArray(sa.Detach())));
264 If you you received wxVariantDataSafeArray as a result of wxAutomationObject method call:
265 (1) Get the data out of the array.
266 (2) Destroy the array.
269 result = range.GetProperty("Value");
270 if ( result.GetType() == "safearray" )
272 wxSafeArray<VT_VARIANT> safeArray;
273 wxVariantDataSafeArray* const
274 sa = wxStaticCastVariantData(variant.GetData(), wxVariantDataSafeArray);
276 if ( !safeArray.Attach(sa.GetValue() )
278 if ( !safeArray.HasArray() )
279 SafeArrayDestroy(sa.GetValue()); // we have to dispose the SAFEARRAY ourselves
283 // get the data from the SAFEARRAY using wxSafeArray::GetElement()
284 // SAFEARRAY will be disposed by safeArray's dtor
294 @see wxAutomationObject, wxVariant, wxVariantData, wxVariantDataErrorCode
296 @header{wx/msw/ole/oleutils.h}
298 class wxVariantDataSafeArray
: public wxVariantData
302 Constructor initializes the object to @a value.
304 explicit wxVariantDataSafeArray(SAFEARRAY
* value
= NULL
);
307 Returns the stored array.
309 SAFEARRAY
* GetValue() const;
312 Set the stored array.
314 void SetValue(SAFEARRAY
* value
);
317 Returns true if @a data is of wxVariantDataSafeArray type
318 and contains the same SAFEARRAY* value.
320 virtual bool Eq(wxVariantData
& data
) const;
323 Fills the provided string with the textual representation of this
326 Only the address of SAFEARRAY pointer is output.
328 virtual bool Write(wxString
& str
) const;
331 Returns a copy of itself.
333 wxVariantData
* Clone() const;
338 virtual wxString
GetType() const;
341 Converts the value of this object to wxAny.
343 virtual bool GetAsAny(wxAny
* any
) const;
347 @class wxAutomationObject
349 The @b wxAutomationObject class represents an OLE automation object containing
350 a single data member,
351 an IDispatch pointer. It contains a number of functions that make it easy to
353 automation operations, and set and get properties. The class makes heavy use of
356 The usage of these classes is quite close to OLE automation usage in Visual
358 high-level, and the application can specify multiple properties in a single
359 string. The following example
360 gets the current Excel instance, and if it exists, makes the active cell bold.
363 wxAutomationObject excelObject;
364 if (excelObject.GetInstance("Excel.Application"))
365 excelObject.PutProperty("ActiveCell.Font.Bold", @true);
368 Note that this class obviously works under Windows only.
375 @see wxVariant, wxVariantDataCurrency, wxVariantDataErrorCode, wxVariantDataSafeArray
377 class wxAutomationObject
: public wxObject
381 Constructor, taking an optional IDispatch pointer which will be released when
385 wxAutomationObject(WXIDISPATCH
* dispatchPtr
= NULL
);
388 Destructor. If the internal IDispatch pointer is non-null, it will be released.
390 ~wxAutomationObject();
394 Calls an automation method for this object. The first form takes a method name,
396 arguments, and an array of variants. The second form takes a method name and
398 constant references to variants. Since the variant class has constructors for
400 data types, and C++ provides temporary objects automatically, both of the
402 are syntactically valid:
404 Note that @a method can contain dot-separated property names, to save the
406 needing to call GetProperty several times using several temporary objects. For
409 wxVariant
CallMethod(const wxString
& method
, int noArgs
,
410 wxVariant args
[]) const;
411 const wxVariant
CallMethod(const wxString
& method
, ... ) const;
415 Creates a new object based on the ProgID, returning @true if the object was
416 successfully created,
419 bool CreateInstance(const wxString
& progId
) const;
422 Checks if the object is in a valid state.
424 Returns @true if the object was successfully initialized or @false if
425 it has no valid IDispatch pointer.
427 @see GetDispatchPtr()
432 Gets the IDispatch pointer.
434 Notice that the return value of this function is an untyped pointer but
435 it can be safely cast to @c IDispatch.
437 void* GetDispatchPtr() const;
440 Retrieves the current object associated with the specified ProgID, and
441 attaches the IDispatch pointer to this object.
443 If attaching to an existing object failed and @a flags includes
444 wxAutomationInstance_CreateIfNeeded flag, a new object will be created.
445 Otherwise this function will normally log an error message which may be
446 undesirable if the object may or may not exist. The
447 wxAutomationInstance_SilentIfNone flag can be used to prevent the error
448 from being logged in this case.
450 Returns @true if a pointer was successfully retrieved, @false
453 Note that this cannot cope with two instances of a given OLE object being
454 active simultaneously,
455 such as two copies of Excel running. Which object is referenced cannot
456 currently be specified.
458 @param progId COM ProgID, e.g. "Excel.Application"
459 @param flags The creation flags (this parameters was added in wxWidgets
462 bool GetInstance(const wxString
& progId
,
463 int flags
= wxAutomationInstance_CreateIfNeeded
) const;
466 Retrieves a property from this object, assumed to be a dispatch pointer, and
467 initialises @a obj with it.
468 To avoid having to deal with IDispatch pointers directly, use this function in
470 to GetProperty() when retrieving objects
472 Note that an IDispatch pointer is stored as a void* pointer in wxVariant
477 bool GetObject(wxAutomationObject
& obj
, const wxString
& property
,
479 wxVariant args
[] = NULL
) const;
483 Gets a property value from this object. The first form takes a property name,
485 arguments, and an array of variants. The second form takes a property name and
487 constant references to variants. Since the variant class has constructors for
489 data types, and C++ provides temporary objects automatically, both of the
491 are syntactically valid:
493 Note that @a property can contain dot-separated property names, to save the
495 needing to call GetProperty several times using several temporary objects.
497 wxVariant
GetProperty(const wxString
& property
, int noArgs
,
498 wxVariant args
[]) const;
499 const wxVariant
GetProperty(const wxString
& property
, ... ) const;
503 This function is a low-level implementation that allows access to the IDispatch
505 It is not meant to be called directly by the application, but is used by other
506 convenience functions.
509 The member function or property name.
511 Bitlist: may contain DISPATCH_PROPERTYPUT, DISPATCH_PROPERTYPUTREF,
514 Return value (ignored if there is no return value)
516 Number of arguments in args or ptrArgs.
518 If non-null, contains an array of variants.
520 If non-null, contains an array of constant pointers to variants.
522 @return @true if the operation was successful, @false otherwise.
524 @remarks Two types of argument array are provided, so that when possible
525 pointers are used for efficiency.
527 bool Invoke(const wxString
& member
, int action
,
528 wxVariant
& retValue
, int noArgs
,
530 const wxVariant
* ptrArgs
[] = 0) const;
534 Puts a property value into this object. The first form takes a property name,
536 arguments, and an array of variants. The second form takes a property name and
538 constant references to variants. Since the variant class has constructors for
540 data types, and C++ provides temporary objects automatically, both of the
542 are syntactically valid:
544 Note that @a property can contain dot-separated property names, to save the
546 needing to call GetProperty several times using several temporary objects.
548 bool PutProperty(const wxString
& property
, int noArgs
,
550 const bool PutProperty(const wxString
& property
, ... );
554 Sets the IDispatch pointer. This function does not check if there is already an
556 You may need to cast from IDispatch* to WXIDISPATCH* when calling this function.
558 void SetDispatchPtr(WXIDISPATCH
* dispatchPtr
);
561 Returns the locale identifier used in automation calls.
563 The default is LOCALE_SYSTEM_DEFAULT but the objects obtained by
564 GetObject() inherit the locale identifier from the one that created
569 LCID
GetLCID() const;
572 Sets the locale identifier to be used in automation calls performed by
575 The default value is LOCALE_SYSTEM_DEFAULT.
577 Notice that any automation objects created by this one inherit the same
582 void SetLCID(LCID lcid
);