automtn.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: msw/ole/automtn.h
  3. // Purpose: interface of wxAutomationObject
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. Automation object creation flags.
  9. These flags can be used with wxAutomationObject::GetInstance().
  10. @since 2.9.2
  11. */
  12. enum wxAutomationInstanceFlags
  13. {
  14. /**
  15. Only use the existing instance, never create a new one.
  16. This flag can be used to forbid the creation of a new instance if none
  17. is currently running.
  18. */
  19. wxAutomationInstance_UseExistingOnly = 0,
  20. /**
  21. Create a new instance if there are no existing ones.
  22. This flag corresponds to the default behaviour of
  23. wxAutomationObject::GetInstance() and means that if getting an existing
  24. instance failed, we should call wxAutomationObject::CreateInstance() to
  25. create a new one.
  26. */
  27. wxAutomationInstance_CreateIfNeeded = 1,
  28. /**
  29. Do not show an error message if no existing instance is currently
  30. running.
  31. All other errors will still be reported as usual.
  32. */
  33. wxAutomationInstance_SilentIfNone = 2
  34. };
  35. /**
  36. Flags used for conversions between wxVariant and OLE VARIANT.
  37. These flags are used by wxAutomationObject for its wxConvertOleToVariant()
  38. calls. They can be obtained by wxAutomationObject::GetConvertVariantFlags()
  39. and set by wxAutomationObject::SetConvertVariantFlags().
  40. @since 3.0
  41. @header{wx/msw/ole/oleutils.h}
  42. */
  43. enum wxOleConvertVariantFlags
  44. {
  45. /**
  46. Default value.
  47. */
  48. wxOleConvertVariant_Default = 0,
  49. /**
  50. If this flag is used, SAFEARRAYs contained in OLE VARIANTs will be
  51. returned as wxVariants with wxVariantDataSafeArray type instead of
  52. wxVariants with the list type containing the (flattened) SAFEARRAY's
  53. elements.
  54. */
  55. wxOleConvertVariant_ReturnSafeArrays = 1
  56. };
  57. /**
  58. @class wxVariantDataCurrency
  59. This class represents a thin wrapper for Microsoft Windows CURRENCY type.
  60. It is used for converting between wxVariant and OLE VARIANT
  61. with type set to VT_CURRENCY. When wxVariant stores
  62. wxVariantDataCurrency, it returns "currency" as its type.
  63. An example of setting and getting CURRENCY value to and from wxVariant:
  64. @code
  65. CURRENCY cy;
  66. wxVariant variant;
  67. // set wxVariant to currency type
  68. if ( SUCCEEDED(VarCyFromR8(123.45, &cy)) ) // set cy to 123.45
  69. {
  70. variant.SetData(new wxVariantDataCurrency(cy));
  71. // or instead of the line above you could write:
  72. // wxVariantDataCurrency wxCy;
  73. // wxCy.SetValue(cy);
  74. // variant.SetData(wxCy.Clone());
  75. }
  76. // get CURRENCY value from wxVariant
  77. if ( variant.GetType() == "currency" )
  78. {
  79. wxVariantDataCurrency*
  80. wxCy = wxDynamicCastVariantData(variant.GetData(), wxVariantDataCurrency);
  81. cy = wxCy->GetValue();
  82. }
  83. @endcode
  84. @onlyfor{wxmsw}
  85. @since 2.9.5
  86. @library{wxcore}
  87. @category{data}
  88. @see wxAutomationObject, wxVariant, wxVariantData, wxVariantDataErrorCode
  89. @header{wx/msw/ole/oleutils.h}
  90. */
  91. class wxVariantDataCurrency : public wxVariantData
  92. {
  93. public:
  94. /**
  95. Default constructor initializes the object to 0.0.
  96. */
  97. wxVariantDataCurrency();
  98. /**
  99. Constructor from CURRENCY.
  100. */
  101. wxVariantDataCurrency(CURRENCY value);
  102. /**
  103. Returns the stored CURRENCY value.
  104. */
  105. CURRENCY GetValue() const;
  106. /**
  107. Sets the stored value to @a value.
  108. */
  109. void SetValue(CURRENCY value);
  110. /**
  111. Returns true if @a data is of wxVariantDataCurency type
  112. and contains the same CURRENCY value.
  113. */
  114. virtual bool Eq(wxVariantData& data) const;
  115. /**
  116. Fills the provided string with the textual representation of this
  117. object.
  118. The implementation of this method using @c VarBstrFromCy() Windows API
  119. function with LOCALE_USER_DEFAULT.
  120. */
  121. virtual bool Write(wxString& str) const;
  122. /**
  123. Returns a copy of itself.
  124. */
  125. wxVariantData* Clone() const;
  126. /**
  127. Returns "currency".
  128. */
  129. virtual wxString GetType() const;
  130. /**
  131. Converts the value of this object to wxAny.
  132. */
  133. virtual bool GetAsAny(wxAny* any) const;
  134. };
  135. /**
  136. @class wxVariantDataErrorCode
  137. This class represents a thin wrapper for Microsoft Windows SCODE type
  138. (which is the same as HRESULT).
  139. It is used for converting between a wxVariant and OLE VARIANT with type set
  140. to VT_ERROR. When wxVariant stores wxVariantDataErrorCode, it returns
  141. "errorcode" as its type. This class can be used for returning error codes
  142. of automation calls or exchanging values with other applications: e.g.
  143. Microsoft Excel returns VARIANTs with VT_ERROR type for cell values with
  144. errors (one of XlCVError constants, displayed as e.g. "#DIV/0!" or "#REF!"
  145. there) etc. See wxVariantDataCurrency for an example of how to exchange
  146. values between wxVariant and a native type not directly supported by it.
  147. @onlyfor{wxmsw}
  148. @since 2.9.5
  149. @library{wxcore}
  150. @category{data}
  151. @see wxAutomationObject, wxVariant, wxVariantData, wxVariantDataCurrency
  152. @header{wx/msw/ole/oleutils.h}
  153. */
  154. class wxVariantDataErrorCode : public wxVariantData
  155. {
  156. public:
  157. /**
  158. Constructor initializes the object to @a value or S_OK if no value was
  159. passed.
  160. */
  161. wxVariantDataErrorCode(SCODE value = S_OK);
  162. /**
  163. Returns the stored SCODE value.
  164. */
  165. SCODE GetValue() const;
  166. /**
  167. Set the stored value to @a value.
  168. */
  169. void SetValue(SCODE value);
  170. /**
  171. Returns true if @a data is of wxVariantDataErrorCode type
  172. and contains the same SCODE value.
  173. */
  174. virtual bool Eq(wxVariantData& data) const;
  175. /**
  176. Fills the provided string with the textual representation of this
  177. object.
  178. The error code is just a number, so it's output as such.
  179. */
  180. virtual bool Write(wxString& str) const;
  181. /**
  182. Returns a copy of itself.
  183. */
  184. wxVariantData* Clone() const;
  185. /**
  186. Returns "errorcode".
  187. */
  188. virtual wxString GetType() const { return wxS("errorcode"); }
  189. /**
  190. Converts the value of this object to wxAny.
  191. */
  192. virtual bool GetAsAny(wxAny* any) const;
  193. };
  194. /**
  195. @class wxVariantDataSafeArray
  196. This class represents a thin wrapper for Microsoft Windows SAFEARRAY type.
  197. It is used for converting between wxVariant and OLE VARIANT
  198. with type set to VT_ARRAY, which has more than one dimension.
  199. When wxVariant stores wxVariantDataSafeArray, it returns "safearray" as its type.
  200. wxVariantDataSafeArray does NOT manage the SAFEARRAY it points to.
  201. If you want to pass it to a wxAutomationObject as a parameter:
  202. -# Assign a SAFEARRAY pointer to it and store it in a wxVariant.
  203. -# Call the wxAutomationObject method (CallMethod(), SetProperty() or Invoke())
  204. -# wxAutomationObject will destroy the array after the approapriate automation call.
  205. An example of creating a 2-dimensional SAFEARRAY containing VARIANTs
  206. and storing it in a wxVariant
  207. @code
  208. SAFEARRAYBOUND bounds[2]; // 2 dimensions
  209. wxSafeArray<VT_VARIANT> safeArray;
  210. unsigned rowCount = 1000;
  211. unsigned colCount = 20;
  212. bounds[0].lLbound = 0; // elements start at 0
  213. bounds[0].cElements = rowCount;
  214. bounds[1].lLbound = 0; // elements start at 0
  215. bounds[1].cElements = colCount;
  216. if ( !safeArray.Create(bounds, 2) )
  217. return false;
  218. long indices[2];
  219. for ( unsigned row = 0; row < rowCount; row++ )
  220. {
  221. indices[0] = row;
  222. for ( unsigned col = 0; col < colCount; col++ )
  223. {
  224. indices[1] = col;
  225. if ( !safeArray.SetElement(indices, wxString::Format("R%u C%u", row+1, col+1)) )
  226. return false;
  227. }
  228. }
  229. range.PutProperty("Value", wxVariant(new wxVariantDataSafeArray(sa.Detach())));
  230. @endcode
  231. If you you received wxVariantDataSafeArray as a result of wxAutomationObject method call:
  232. (1) Get the data out of the array.
  233. (2) Destroy the array.
  234. @code
  235. wxVariant result;
  236. result = range.GetProperty("Value");
  237. if ( result.GetType() == "safearray" )
  238. {
  239. wxSafeArray<VT_VARIANT> safeArray;
  240. wxVariantDataSafeArray* const
  241. sa = wxStaticCastVariantData(variant.GetData(), wxVariantDataSafeArray);
  242. if ( !safeArray.Attach(sa.GetValue() )
  243. {
  244. if ( !safeArray.HasArray() )
  245. SafeArrayDestroy(sa.GetValue()); // we have to dispose the SAFEARRAY ourselves
  246. return false;
  247. }
  248. // get the data from the SAFEARRAY using wxSafeArray::GetElement()
  249. // SAFEARRAY will be disposed by safeArray's dtor
  250. }
  251. @endcode
  252. @onlyfor{wxmsw}
  253. @since 2.9.5
  254. @library{wxcore}
  255. @category{data}
  256. @see wxAutomationObject, wxVariant, wxVariantData, wxVariantDataErrorCode
  257. @header{wx/msw/ole/oleutils.h}
  258. */
  259. class wxVariantDataSafeArray : public wxVariantData
  260. {
  261. public:
  262. /**
  263. Constructor initializes the object to @a value.
  264. */
  265. explicit wxVariantDataSafeArray(SAFEARRAY* value = NULL);
  266. /**
  267. Returns the stored array.
  268. */
  269. SAFEARRAY* GetValue() const;
  270. /**
  271. Set the stored array.
  272. */
  273. void SetValue(SAFEARRAY* value);
  274. /**
  275. Returns true if @a data is of wxVariantDataSafeArray type
  276. and contains the same SAFEARRAY* value.
  277. */
  278. virtual bool Eq(wxVariantData& data) const;
  279. /**
  280. Fills the provided string with the textual representation of this
  281. object.
  282. Only the address of SAFEARRAY pointer is output.
  283. */
  284. virtual bool Write(wxString& str) const;
  285. /**
  286. Returns a copy of itself.
  287. */
  288. wxVariantData* Clone() const;
  289. /**
  290. Returns "safearray".
  291. */
  292. virtual wxString GetType() const;
  293. /**
  294. Converts the value of this object to wxAny.
  295. */
  296. virtual bool GetAsAny(wxAny* any) const;
  297. };
  298. /**
  299. @class wxAutomationObject
  300. The @b wxAutomationObject class represents an OLE automation object containing
  301. a single data member,
  302. an IDispatch pointer. It contains a number of functions that make it easy to
  303. perform
  304. automation operations, and set and get properties. The class makes heavy use of
  305. the wxVariant class.
  306. The usage of these classes is quite close to OLE automation usage in Visual
  307. Basic. The API is
  308. high-level, and the application can specify multiple properties in a single
  309. string. The following example
  310. gets the current Excel instance, and if it exists, makes the active cell bold.
  311. @code
  312. wxAutomationObject excelObject;
  313. if (excelObject.GetInstance("Excel.Application"))
  314. excelObject.PutProperty("ActiveCell.Font.Bold", @true);
  315. @endcode
  316. Note that this class obviously works under Windows only.
  317. @onlyfor{wxmsw}
  318. @library{wxcore}
  319. @category{data}
  320. @see wxVariant, wxVariantDataCurrency, wxVariantDataErrorCode, wxVariantDataSafeArray
  321. */
  322. class wxAutomationObject : public wxObject
  323. {
  324. public:
  325. /**
  326. Constructor, taking an optional IDispatch pointer which will be released when
  327. the
  328. object is deleted.
  329. */
  330. wxAutomationObject(WXIDISPATCH* dispatchPtr = NULL);
  331. /**
  332. Destructor. If the internal IDispatch pointer is non-null, it will be released.
  333. */
  334. ~wxAutomationObject();
  335. //@{
  336. /**
  337. Calls an automation method for this object. The first form takes a method name,
  338. number of
  339. arguments, and an array of variants. The second form takes a method name and
  340. zero to six
  341. constant references to variants. Since the variant class has constructors for
  342. the basic
  343. data types, and C++ provides temporary objects automatically, both of the
  344. following lines
  345. are syntactically valid:
  346. Note that @a method can contain dot-separated property names, to save the
  347. application
  348. needing to call GetProperty several times using several temporary objects. For
  349. example:
  350. */
  351. wxVariant CallMethod(const wxString& method, int noArgs,
  352. wxVariant args[]) const;
  353. const wxVariant CallMethod(const wxString& method, ... ) const;
  354. //@}
  355. /**
  356. Creates a new object based on the ProgID, returning @true if the object was
  357. successfully created,
  358. or @false if not.
  359. */
  360. bool CreateInstance(const wxString& progId) const;
  361. /**
  362. Checks if the object is in a valid state.
  363. Returns @true if the object was successfully initialized or @false if
  364. it has no valid IDispatch pointer.
  365. @see GetDispatchPtr()
  366. */
  367. bool IsOk() const;
  368. /**
  369. Gets the IDispatch pointer.
  370. Notice that the return value of this function is an untyped pointer but
  371. it can be safely cast to @c IDispatch.
  372. */
  373. void* GetDispatchPtr() const;
  374. /**
  375. Retrieves the current object associated with the specified ProgID, and
  376. attaches the IDispatch pointer to this object.
  377. If attaching to an existing object failed and @a flags includes
  378. wxAutomationInstance_CreateIfNeeded flag, a new object will be created.
  379. Otherwise this function will normally log an error message which may be
  380. undesirable if the object may or may not exist. The
  381. wxAutomationInstance_SilentIfNone flag can be used to prevent the error
  382. from being logged in this case.
  383. Returns @true if a pointer was successfully retrieved, @false
  384. otherwise.
  385. Note that this cannot cope with two instances of a given OLE object being
  386. active simultaneously,
  387. such as two copies of Excel running. Which object is referenced cannot
  388. currently be specified.
  389. @param progId COM ProgID, e.g. "Excel.Application"
  390. @param flags The creation flags (this parameters was added in wxWidgets
  391. 2.9.2)
  392. */
  393. bool GetInstance(const wxString& progId,
  394. int flags = wxAutomationInstance_CreateIfNeeded) const;
  395. /**
  396. Retrieves a property from this object, assumed to be a dispatch pointer, and
  397. initialises @a obj with it.
  398. To avoid having to deal with IDispatch pointers directly, use this function in
  399. preference
  400. to GetProperty() when retrieving objects
  401. from other objects.
  402. Note that an IDispatch pointer is stored as a void* pointer in wxVariant
  403. objects.
  404. @see GetProperty()
  405. */
  406. bool GetObject(wxAutomationObject& obj, const wxString& property,
  407. int noArgs = 0,
  408. wxVariant args[] = NULL) const;
  409. //@{
  410. /**
  411. Gets a property value from this object. The first form takes a property name,
  412. number of
  413. arguments, and an array of variants. The second form takes a property name and
  414. zero to six
  415. constant references to variants. Since the variant class has constructors for
  416. the basic
  417. data types, and C++ provides temporary objects automatically, both of the
  418. following lines
  419. are syntactically valid:
  420. Note that @a property can contain dot-separated property names, to save the
  421. application
  422. needing to call GetProperty several times using several temporary objects.
  423. */
  424. wxVariant GetProperty(const wxString& property, int noArgs,
  425. wxVariant args[]) const;
  426. const wxVariant GetProperty(const wxString& property, ... ) const;
  427. //@}
  428. /**
  429. This function is a low-level implementation that allows access to the IDispatch
  430. Invoke function.
  431. It is not meant to be called directly by the application, but is used by other
  432. convenience functions.
  433. @param member
  434. The member function or property name.
  435. @param action
  436. Bitlist: may contain DISPATCH_PROPERTYPUT, DISPATCH_PROPERTYPUTREF,
  437. DISPATCH_METHOD.
  438. @param retValue
  439. Return value (ignored if there is no return value)
  440. @param noArgs
  441. Number of arguments in args or ptrArgs.
  442. @param args
  443. If non-null, contains an array of variants.
  444. @param ptrArgs
  445. If non-null, contains an array of constant pointers to variants.
  446. @return @true if the operation was successful, @false otherwise.
  447. @remarks Two types of argument array are provided, so that when possible
  448. pointers are used for efficiency.
  449. */
  450. bool Invoke(const wxString& member, int action,
  451. wxVariant& retValue, int noArgs,
  452. wxVariant args[],
  453. const wxVariant* ptrArgs[] = 0) const;
  454. //@{
  455. /**
  456. Puts a property value into this object. The first form takes a property name,
  457. number of
  458. arguments, and an array of variants. The second form takes a property name and
  459. zero to six
  460. constant references to variants. Since the variant class has constructors for
  461. the basic
  462. data types, and C++ provides temporary objects automatically, both of the
  463. following lines
  464. are syntactically valid:
  465. Note that @a property can contain dot-separated property names, to save the
  466. application
  467. needing to call GetProperty several times using several temporary objects.
  468. */
  469. bool PutProperty(const wxString& property, int noArgs,
  470. wxVariant args[]);
  471. const bool PutProperty(const wxString& property, ... );
  472. //@}
  473. /**
  474. Sets the IDispatch pointer. This function does not check if there is already an
  475. IDispatch pointer.
  476. You may need to cast from IDispatch* to WXIDISPATCH* when calling this function.
  477. */
  478. void SetDispatchPtr(WXIDISPATCH* dispatchPtr);
  479. /**
  480. Returns the locale identifier used in automation calls.
  481. The default is LOCALE_SYSTEM_DEFAULT but the objects obtained by
  482. GetObject() inherit the locale identifier from the one that created
  483. them.
  484. @since 2.9.5
  485. */
  486. LCID GetLCID() const;
  487. /**
  488. Sets the locale identifier to be used in automation calls performed by
  489. this object.
  490. The default value is LOCALE_SYSTEM_DEFAULT.
  491. Notice that any automation objects created by this one inherit the same
  492. LCID.
  493. @since 2.9.5
  494. */
  495. void SetLCID(LCID lcid);
  496. /**
  497. Returns the flags used for conversions between wxVariant and OLE
  498. VARIANT, see wxOleConvertVariantFlags.
  499. The default value is wxOleConvertVariant_Default for compatibility but
  500. it can be changed using SetConvertVariantFlags().
  501. Notice that objects obtained by GetObject() inherit the flags from the
  502. one that created them.
  503. @since 3.0
  504. */
  505. long GetConvertVariantFlags() const;
  506. /**
  507. Sets the flags used for conversions between wxVariant and OLE VARIANT,
  508. see wxOleConvertVariantFlags.
  509. The default value is wxOleConvertVariant_Default.
  510. @since 3.0
  511. */
  512. void SetConvertVariantFlags(long flags);
  513. };