1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ole/automtn.h
3 // Purpose: interface of wxAutomationObject
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
9 Automation object creation flags.
11 These flags can be used with wxAutomationObject::GetInstance().
15 enum wxAutomationInstanceFlags
18 Only use the existing instance, never create a new one.
20 This flag can be used to forbid the creation of a new instance if none
23 wxAutomationInstance_UseExistingOnly
= 0,
26 Create a new instance if there are no existing ones.
28 This flag corresponds to the default behaviour of
29 wxAutomationObject::GetInstance() and means that if getting an existing
30 instance failed, we should call wxAutomationObject::CreateInstance() to
33 wxAutomationInstance_CreateIfNeeded
= 1,
36 Do not show an error message if no existing instance is currently
39 All other errors will still be reported as usual.
41 wxAutomationInstance_SilentIfNone
= 2
45 @class wxVariantDataCurrency
47 This class represents a thin wrapper for Microsoft Windows CURRENCY type.
49 It is used for converting between wxVariant and OLE VARIANT
50 with type set to VT_CURRENCY. When wxVariant stores
51 wxVariantDataCurrency, it returns "currency" as its type.
53 An example of setting and getting CURRENCY value to and from wxVariant:
58 // set wxVariant to currency type
59 if ( SUCCEEDED(VarCyFromR8(123.45, &cy)) ) // set cy to 123.45
61 variant.SetData(new wxVariantDataCurrency(cy));
63 // or instead of the line above you could write:
64 // wxVariantDataCurrency wxCy;
66 // variant.SetData(wxCy.Clone());
69 // get CURRENCY value from wxVariant
70 if ( variant.GetType() == "currency" )
72 wxVariantDataCurrency*
73 wxCy = wxDynamicCastVariantData(variant.GetData(), wxVariantDataCurrency);
74 cy = wxCy->GetValue();
84 @see wxAutomationObject, wxVariant, wxVariantData, wxVariantDataErrorCode
86 @header{wx/msw/ole/oleutils.h}
88 class wxVariantDataCurrency
: public wxVariantData
92 Default constructor initializes the object to 0.0.
94 wxVariantDataCurrency();
97 Constructor from CURRENCY.
99 wxVariantDataCurrency(CURRENCY value
);
102 Returns the stored CURRENCY value.
104 CURRENCY
GetValue() const;
107 Sets the stored value to @a value.
109 void SetValue(CURRENCY value
);
112 Returns true if @a data is of wxVariantDataCurency type
113 and contains the same CURRENCY value.
115 virtual bool Eq(wxVariantData
& data
) const;
118 Fills the provided string with the textual representation of this
121 The implementation of this method using @c VarBstrFromCy() Windows API
122 function with LOCALE_USER_DEFAULT.
124 virtual bool Write(wxString
& str
) const;
127 Returns a copy of itself.
129 wxVariantData
* Clone() const;
134 virtual wxString
GetType() const;
137 Converts the value of this object to wxAny.
139 virtual bool GetAsAny(wxAny
* any
) const;
144 @class wxVariantDataErrorCode
146 This class represents a thin wrapper for Microsoft Windows SCODE type
147 (which is the same as HRESULT).
149 It is used for converting between a wxVariant and OLE VARIANT with type set
150 to VT_ERROR. When wxVariant stores wxVariantDataErrorCode, it returns
151 "errorcode" as its type. This class can be used for returning error codes
152 of automation calls or exchanging values with other applications: e.g.
153 Microsoft Excel returns VARIANTs with VT_ERROR type for cell values with
154 errors (one of XlCVError constants, displayed as e.g. "#DIV/0!" or "#REF!"
155 there) etc. See wxVariantDataCurrency for an example of how to exchange
156 values between wxVariant and a native type not directly supported by it.
164 @see wxAutomationObject, wxVariant, wxVariantData, wxVariantDataCurrency
166 @header{wx/msw/ole/oleutils.h}
168 class wxVariantDataErrorCode
: public wxVariantData
172 Constructor initializes the object to @a value or S_OK if no value was
175 wxVariantDataErrorCode(SCODE value
= S_OK
);
178 Returns the stored SCODE value.
180 SCODE
GetValue() const;
183 Set the stored value to @a value.
185 void SetValue(SCODE value
);
188 Returns true if @a data is of wxVariantDataErrorCode type
189 and contains the same SCODE value.
191 virtual bool Eq(wxVariantData
& data
) const;
194 Fills the provided string with the textual representation of this
197 The error code is just a number, so it's output as such.
199 virtual bool Write(wxString
& str
) const;
202 Returns a copy of itself.
204 wxVariantData
* Clone() const;
209 virtual wxString
GetType() const { return wxS("errorcode"); }
212 Converts the value of this object to wxAny.
214 virtual bool GetAsAny(wxAny
* any
) const;
218 @class wxVariantDataSafeArray
220 This class represents a thin wrapper for Microsoft Windows SAFEARRAY type.
222 It is used for converting between wxVariant and OLE VARIANT
223 with type set to VT_ARRAY, which has more than one dimension.
224 When wxVariant stores wxVariantDataSafeArray, it returns "safearray" as its type.
226 wxVariantDataSafeArray does NOT manage the SAFEARRAY it points to.
227 If you want to pass it to a wxAutomationObject as a parameter:
228 -# Assign a SAFEARRAY pointer to it and store it in a wxVariant.
229 -# Call the wxAutomationObject method (CallMethod(), SetProperty() or Invoke())
230 -# wxAutomationObject will destroy the array after the approapriate automation call.
232 An example of creating a 2-dimensional SAFEARRAY containing VARIANTs
233 and storing it in a wxVariant
235 SAFEARRAYBOUND bounds[2]; // 2 dimensions
236 wxSafeArray<VT_VARIANT> safeArray;
237 unsigned rowCount = 1000;
238 unsigned colCount = 20;
240 bounds[0].lLbound = 0; // elements start at 0
241 bounds[0].cElements = rowCount;
242 bounds[1].lLbound = 0; // elements start at 0
243 bounds[1].cElements = colCount;
245 if ( !safeArray.Create(bounds, 2) )
250 for ( unsigned row = 0; row < rowCount; row++ )
253 for ( unsigned col = 0; col < colCount; col++ )
256 if ( !safeArray.SetElement(indices, wxString::Format("R%u C%u", row+1, col+1)) )
260 range.PutProperty("Value", wxVariant(new wxVariantDataSafeArray(sa.Detach())));
263 If you you received wxVariantDataSafeArray as a result of wxAutomationObject method call:
264 (1) Get the data out of the array.
265 (2) Destroy the array.
268 result = range.GetProperty("Value");
269 if ( result.GetType() == "safearray" )
271 wxSafeArray<VT_VARIANT> safeArray;
272 wxVariantDataSafeArray* const
273 sa = wxStaticCastVariantData(variant.GetData(), wxVariantDataSafeArray);
275 if ( !safeArray.Attach(sa.GetValue() )
277 if ( !safeArray.HasArray() )
278 SafeArrayDestroy(sa.GetValue()); // we have to dispose the SAFEARRAY ourselves
282 // get the data from the SAFEARRAY using wxSafeArray::GetElement()
283 // SAFEARRAY will be disposed by safeArray's dtor
293 @see wxAutomationObject, wxVariant, wxVariantData, wxVariantDataErrorCode
295 @header{wx/msw/ole/oleutils.h}
297 class wxVariantDataSafeArray
: public wxVariantData
301 Constructor initializes the object to @a value.
303 explicit wxVariantDataSafeArray(SAFEARRAY
* value
= NULL
);
306 Returns the stored array.
308 SAFEARRAY
* GetValue() const;
311 Set the stored array.
313 void SetValue(SAFEARRAY
* value
);
316 Returns true if @a data is of wxVariantDataSafeArray type
317 and contains the same SAFEARRAY* value.
319 virtual bool Eq(wxVariantData
& data
) const;
322 Fills the provided string with the textual representation of this
325 Only the address of SAFEARRAY pointer is output.
327 virtual bool Write(wxString
& str
) const;
330 Returns a copy of itself.
332 wxVariantData
* Clone() const;
337 virtual wxString
GetType() const;
340 Converts the value of this object to wxAny.
342 virtual bool GetAsAny(wxAny
* any
) const;
346 @class wxAutomationObject
348 The @b wxAutomationObject class represents an OLE automation object containing
349 a single data member,
350 an IDispatch pointer. It contains a number of functions that make it easy to
352 automation operations, and set and get properties. The class makes heavy use of
355 The usage of these classes is quite close to OLE automation usage in Visual
357 high-level, and the application can specify multiple properties in a single
358 string. The following example
359 gets the current Excel instance, and if it exists, makes the active cell bold.
362 wxAutomationObject excelObject;
363 if (excelObject.GetInstance("Excel.Application"))
364 excelObject.PutProperty("ActiveCell.Font.Bold", @true);
367 Note that this class obviously works under Windows only.
374 @see wxVariant, wxVariantDataCurrency, wxVariantDataErrorCode, wxVariantDataSafeArray
376 class wxAutomationObject
: public wxObject
380 Constructor, taking an optional IDispatch pointer which will be released when
384 wxAutomationObject(WXIDISPATCH
* dispatchPtr
= NULL
);
387 Destructor. If the internal IDispatch pointer is non-null, it will be released.
389 ~wxAutomationObject();
393 Calls an automation method for this object. The first form takes a method name,
395 arguments, and an array of variants. The second form takes a method name and
397 constant references to variants. Since the variant class has constructors for
399 data types, and C++ provides temporary objects automatically, both of the
401 are syntactically valid:
403 Note that @a method can contain dot-separated property names, to save the
405 needing to call GetProperty several times using several temporary objects. For
408 wxVariant
CallMethod(const wxString
& method
, int noArgs
,
409 wxVariant args
[]) const;
410 const wxVariant
CallMethod(const wxString
& method
, ... ) const;
414 Creates a new object based on the ProgID, returning @true if the object was
415 successfully created,
418 bool CreateInstance(const wxString
& progId
) const;
421 Checks if the object is in a valid state.
423 Returns @true if the object was successfully initialized or @false if
424 it has no valid IDispatch pointer.
426 @see GetDispatchPtr()
431 Gets the IDispatch pointer.
433 Notice that the return value of this function is an untyped pointer but
434 it can be safely cast to @c IDispatch.
436 void* GetDispatchPtr() const;
439 Retrieves the current object associated with the specified ProgID, and
440 attaches the IDispatch pointer to this object.
442 If attaching to an existing object failed and @a flags includes
443 wxAutomationInstance_CreateIfNeeded flag, a new object will be created.
444 Otherwise this function will normally log an error message which may be
445 undesirable if the object may or may not exist. The
446 wxAutomationInstance_SilentIfNone flag can be used to prevent the error
447 from being logged in this case.
449 Returns @true if a pointer was successfully retrieved, @false
452 Note that this cannot cope with two instances of a given OLE object being
453 active simultaneously,
454 such as two copies of Excel running. Which object is referenced cannot
455 currently be specified.
457 @param progId COM ProgID, e.g. "Excel.Application"
458 @param flags The creation flags (this parameters was added in wxWidgets
461 bool GetInstance(const wxString
& progId
,
462 int flags
= wxAutomationInstance_CreateIfNeeded
) const;
465 Retrieves a property from this object, assumed to be a dispatch pointer, and
466 initialises @a obj with it.
467 To avoid having to deal with IDispatch pointers directly, use this function in
469 to GetProperty() when retrieving objects
471 Note that an IDispatch pointer is stored as a void* pointer in wxVariant
476 bool GetObject(wxAutomationObject
& obj
, const wxString
& property
,
478 wxVariant args
[] = NULL
) const;
482 Gets a property value from this object. The first form takes a property name,
484 arguments, and an array of variants. The second form takes a property name and
486 constant references to variants. Since the variant class has constructors for
488 data types, and C++ provides temporary objects automatically, both of the
490 are syntactically valid:
492 Note that @a property can contain dot-separated property names, to save the
494 needing to call GetProperty several times using several temporary objects.
496 wxVariant
GetProperty(const wxString
& property
, int noArgs
,
497 wxVariant args
[]) const;
498 const wxVariant
GetProperty(const wxString
& property
, ... ) const;
502 This function is a low-level implementation that allows access to the IDispatch
504 It is not meant to be called directly by the application, but is used by other
505 convenience functions.
508 The member function or property name.
510 Bitlist: may contain DISPATCH_PROPERTYPUT, DISPATCH_PROPERTYPUTREF,
513 Return value (ignored if there is no return value)
515 Number of arguments in args or ptrArgs.
517 If non-null, contains an array of variants.
519 If non-null, contains an array of constant pointers to variants.
521 @return @true if the operation was successful, @false otherwise.
523 @remarks Two types of argument array are provided, so that when possible
524 pointers are used for efficiency.
526 bool Invoke(const wxString
& member
, int action
,
527 wxVariant
& retValue
, int noArgs
,
529 const wxVariant
* ptrArgs
[] = 0) const;
533 Puts a property value into this object. The first form takes a property name,
535 arguments, and an array of variants. The second form takes a property name and
537 constant references to variants. Since the variant class has constructors for
539 data types, and C++ provides temporary objects automatically, both of the
541 are syntactically valid:
543 Note that @a property can contain dot-separated property names, to save the
545 needing to call GetProperty several times using several temporary objects.
547 bool PutProperty(const wxString
& property
, int noArgs
,
549 const bool PutProperty(const wxString
& property
, ... );
553 Sets the IDispatch pointer. This function does not check if there is already an
555 You may need to cast from IDispatch* to WXIDISPATCH* when calling this function.
557 void SetDispatchPtr(WXIDISPATCH
* dispatchPtr
);
560 Returns the locale identifier used in automation calls.
562 The default is LOCALE_SYSTEM_DEFAULT but the objects obtained by
563 GetObject() inherit the locale identifier from the one that created
568 LCID
GetLCID() const;
571 Sets the locale identifier to be used in automation calls performed by
574 The default value is LOCALE_SYSTEM_DEFAULT.
576 Notice that any automation objects created by this one inherit the same
581 void SetLCID(LCID lcid
);