dataobj.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/dataobj.h
  3. // Purpose: common data object classes
  4. // Author: Vadim Zeitlin, Robert Roebling
  5. // Modified by:
  6. // Created: 26.05.99
  7. // Copyright: (c) wxWidgets Team
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_DATAOBJ_H_BASE_
  11. #define _WX_DATAOBJ_H_BASE_
  12. // ----------------------------------------------------------------------------
  13. // headers
  14. // ----------------------------------------------------------------------------
  15. #include "wx/defs.h"
  16. #if wxUSE_DATAOBJ
  17. #include "wx/string.h"
  18. #include "wx/bitmap.h"
  19. #include "wx/list.h"
  20. #include "wx/arrstr.h"
  21. // ============================================================================
  22. /*
  23. Generic data transfer related classes. The class hierarchy is as follows:
  24. - wxDataObject-
  25. / \
  26. / \
  27. wxDataObjectSimple wxDataObjectComposite
  28. / | \
  29. / | \
  30. wxTextDataObject | wxBitmapDataObject
  31. |
  32. wxCustomDataObject
  33. */
  34. // ============================================================================
  35. // ----------------------------------------------------------------------------
  36. // wxDataFormat class is declared in platform-specific headers: it represents
  37. // a format for data which may be either one of the standard ones (text,
  38. // bitmap, ...) or a custom one which is then identified by a unique string.
  39. // ----------------------------------------------------------------------------
  40. /* the class interface looks like this (pseudo code):
  41. class wxDataFormat
  42. {
  43. public:
  44. typedef <integral type> NativeFormat;
  45. wxDataFormat(NativeFormat format = wxDF_INVALID);
  46. wxDataFormat(const wxString& format);
  47. wxDataFormat& operator=(NativeFormat format);
  48. wxDataFormat& operator=(const wxDataFormat& format);
  49. bool operator==(NativeFormat format) const;
  50. bool operator!=(NativeFormat format) const;
  51. void SetType(NativeFormat format);
  52. NativeFormat GetType() const;
  53. wxString GetId() const;
  54. void SetId(const wxString& format);
  55. };
  56. */
  57. #if defined(__WXMSW__)
  58. #include "wx/msw/ole/dataform.h"
  59. #elif defined(__WXMOTIF__)
  60. #include "wx/motif/dataform.h"
  61. #elif defined(__WXGTK20__)
  62. #include "wx/gtk/dataform.h"
  63. #elif defined(__WXGTK__)
  64. #include "wx/gtk1/dataform.h"
  65. #elif defined(__WXX11__)
  66. #include "wx/x11/dataform.h"
  67. #elif defined(__WXMAC__)
  68. #include "wx/osx/dataform.h"
  69. #elif defined(__WXCOCOA__)
  70. #include "wx/cocoa/dataform.h"
  71. #elif defined(__WXPM__)
  72. #include "wx/os2/dataform.h"
  73. #endif
  74. // the value for default argument to some functions (corresponds to
  75. // wxDF_INVALID)
  76. extern WXDLLIMPEXP_CORE const wxDataFormat& wxFormatInvalid;
  77. // ----------------------------------------------------------------------------
  78. // wxDataObject represents a piece of data which knows which formats it
  79. // supports and knows how to render itself in each of them - GetDataHere(),
  80. // and how to restore data from the buffer (SetData()).
  81. //
  82. // Although this class may be used directly (i.e. custom classes may be
  83. // derived from it), in many cases it might be simpler to use either
  84. // wxDataObjectSimple or wxDataObjectComposite classes.
  85. //
  86. // A data object may be "read only", i.e. support only GetData() functions or
  87. // "read-write", i.e. support both GetData() and SetData() (in principle, it
  88. // might be "write only" too, but this is rare). Moreover, it doesn't have to
  89. // support the same formats in Get() and Set() directions: for example, a data
  90. // object containing JPEG image might accept BMPs in GetData() because JPEG
  91. // image may be easily transformed into BMP but not in SetData(). Accordingly,
  92. // all methods dealing with formats take an additional "direction" argument
  93. // which is either SET or GET and which tells the function if the format needs
  94. // to be supported by SetData() or GetDataHere().
  95. // ----------------------------------------------------------------------------
  96. class WXDLLIMPEXP_CORE wxDataObjectBase
  97. {
  98. public:
  99. enum Direction
  100. {
  101. Get = 0x01, // format is supported by GetDataHere()
  102. Set = 0x02, // format is supported by SetData()
  103. Both = 0x03 // format is supported by both (unused currently)
  104. };
  105. // this class is polymorphic, hence it needs a virtual dtor
  106. virtual ~wxDataObjectBase();
  107. // get the best suited format for rendering our data
  108. virtual wxDataFormat GetPreferredFormat(Direction dir = Get) const = 0;
  109. // get the number of formats we support
  110. virtual size_t GetFormatCount(Direction dir = Get) const = 0;
  111. // return all formats in the provided array (of size GetFormatCount())
  112. virtual void GetAllFormats(wxDataFormat *formats,
  113. Direction dir = Get) const = 0;
  114. // get the (total) size of data for the given format
  115. virtual size_t GetDataSize(const wxDataFormat& format) const = 0;
  116. // copy raw data (in the specified format) to the provided buffer, return
  117. // true if data copied successfully, false otherwise
  118. virtual bool GetDataHere(const wxDataFormat& format, void *buf) const = 0;
  119. // get data from the buffer of specified length (in the given format),
  120. // return true if the data was read successfully, false otherwise
  121. virtual bool SetData(const wxDataFormat& WXUNUSED(format),
  122. size_t WXUNUSED(len), const void * WXUNUSED(buf))
  123. {
  124. return false;
  125. }
  126. // returns true if this format is supported
  127. bool IsSupported(const wxDataFormat& format, Direction dir = Get) const;
  128. };
  129. // ----------------------------------------------------------------------------
  130. // include the platform-specific declarations of wxDataObject
  131. // ----------------------------------------------------------------------------
  132. #if defined(__WXMSW__)
  133. #include "wx/msw/ole/dataobj.h"
  134. #elif defined(__WXMOTIF__)
  135. #include "wx/motif/dataobj.h"
  136. #elif defined(__WXX11__)
  137. #include "wx/x11/dataobj.h"
  138. #elif defined(__WXGTK20__)
  139. #include "wx/gtk/dataobj.h"
  140. #elif defined(__WXGTK__)
  141. #include "wx/gtk1/dataobj.h"
  142. #elif defined(__WXMAC__)
  143. #include "wx/osx/dataobj.h"
  144. #elif defined(__WXCOCOA__)
  145. #include "wx/cocoa/dataobj.h"
  146. #elif defined(__WXPM__)
  147. #include "wx/os2/dataobj.h"
  148. #endif
  149. // ----------------------------------------------------------------------------
  150. // wxDataObjectSimple is a wxDataObject which only supports one format (in
  151. // both Get and Set directions, but you may return false from GetDataHere() or
  152. // SetData() if one of them is not supported). This is the simplest possible
  153. // wxDataObject implementation.
  154. //
  155. // This is still an "abstract base class" (although it doesn't have any pure
  156. // virtual functions), to use it you should derive from it and implement
  157. // GetDataSize(), GetDataHere() and SetData() functions because the base class
  158. // versions don't do anything - they just return "not implemented".
  159. //
  160. // This class should be used when you provide data in only one format (no
  161. // conversion to/from other formats), either a standard or a custom one.
  162. // Otherwise, you should use wxDataObjectComposite or wxDataObject directly.
  163. // ----------------------------------------------------------------------------
  164. class WXDLLIMPEXP_CORE wxDataObjectSimple : public wxDataObject
  165. {
  166. public:
  167. // ctor takes the format we support, but it can also be set later with
  168. // SetFormat()
  169. wxDataObjectSimple(const wxDataFormat& format = wxFormatInvalid)
  170. : m_format(format)
  171. {
  172. }
  173. // get/set the format we support
  174. const wxDataFormat& GetFormat() const { return m_format; }
  175. void SetFormat(const wxDataFormat& format) { m_format = format; }
  176. // virtual functions to override in derived class (the base class versions
  177. // just return "not implemented")
  178. // -----------------------------------------------------------------------
  179. // get the size of our data
  180. virtual size_t GetDataSize() const
  181. { return 0; }
  182. // copy our data to the buffer
  183. virtual bool GetDataHere(void *WXUNUSED(buf)) const
  184. { return false; }
  185. // copy data from buffer to our data
  186. virtual bool SetData(size_t WXUNUSED(len), const void *WXUNUSED(buf))
  187. { return false; }
  188. // implement base class pure virtuals
  189. // ----------------------------------
  190. virtual wxDataFormat GetPreferredFormat(wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
  191. { return m_format; }
  192. virtual size_t GetFormatCount(wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
  193. { return 1; }
  194. virtual void GetAllFormats(wxDataFormat *formats,
  195. wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
  196. { *formats = m_format; }
  197. virtual size_t GetDataSize(const wxDataFormat& WXUNUSED(format)) const
  198. { return GetDataSize(); }
  199. virtual bool GetDataHere(const wxDataFormat& WXUNUSED(format),
  200. void *buf) const
  201. { return GetDataHere(buf); }
  202. virtual bool SetData(const wxDataFormat& WXUNUSED(format),
  203. size_t len, const void *buf)
  204. { return SetData(len, buf); }
  205. private:
  206. // the one and only format we support
  207. wxDataFormat m_format;
  208. wxDECLARE_NO_COPY_CLASS(wxDataObjectSimple);
  209. };
  210. // ----------------------------------------------------------------------------
  211. // wxDataObjectComposite is the simplest way to implement wxDataObject
  212. // supporting multiple formats. It contains several wxDataObjectSimple and
  213. // supports all formats supported by any of them.
  214. //
  215. // This class shouldn't be (normally) derived from, but may be used directly.
  216. // If you need more flexibility than what it provides, you should probably use
  217. // wxDataObject directly.
  218. // ----------------------------------------------------------------------------
  219. WX_DECLARE_EXPORTED_LIST(wxDataObjectSimple, wxSimpleDataObjectList);
  220. class WXDLLIMPEXP_CORE wxDataObjectComposite : public wxDataObject
  221. {
  222. public:
  223. // ctor
  224. wxDataObjectComposite();
  225. virtual ~wxDataObjectComposite();
  226. // add data object (it will be deleted by wxDataObjectComposite, hence it
  227. // must be allocated on the heap) whose format will become the preferred
  228. // one if preferred == true
  229. void Add(wxDataObjectSimple *dataObject, bool preferred = false);
  230. // Report the format passed to the SetData method. This should be the
  231. // format of the data object within the composite that received data from
  232. // the clipboard or the DnD operation. You can use this method to find
  233. // out what kind of data object was received.
  234. wxDataFormat GetReceivedFormat() const;
  235. // Returns the pointer to the object which supports this format or NULL.
  236. // The returned pointer is owned by wxDataObjectComposite and must
  237. // therefore not be destroyed by the caller.
  238. wxDataObjectSimple *GetObject(const wxDataFormat& format,
  239. wxDataObjectBase::Direction dir = Get) const;
  240. // implement base class pure virtuals
  241. // ----------------------------------
  242. virtual wxDataFormat GetPreferredFormat(wxDataObjectBase::Direction dir = Get) const;
  243. virtual size_t GetFormatCount(wxDataObjectBase::Direction dir = Get) const;
  244. virtual void GetAllFormats(wxDataFormat *formats, wxDataObjectBase::Direction dir = Get) const;
  245. virtual size_t GetDataSize(const wxDataFormat& format) const;
  246. virtual bool GetDataHere(const wxDataFormat& format, void *buf) const;
  247. virtual bool SetData(const wxDataFormat& format, size_t len, const void *buf);
  248. #if defined(__WXMSW__)
  249. virtual const void* GetSizeFromBuffer( const void* buffer, size_t* size,
  250. const wxDataFormat& format );
  251. virtual void* SetSizeInBuffer( void* buffer, size_t size,
  252. const wxDataFormat& format );
  253. virtual size_t GetBufferOffset( const wxDataFormat& format );
  254. #endif
  255. private:
  256. // the list of all (simple) data objects whose formats we support
  257. wxSimpleDataObjectList m_dataObjects;
  258. // the index of the preferred one (0 initially, so by default the first
  259. // one is the preferred)
  260. size_t m_preferred;
  261. wxDataFormat m_receivedFormat;
  262. wxDECLARE_NO_COPY_CLASS(wxDataObjectComposite);
  263. };
  264. // ============================================================================
  265. // Standard implementations of wxDataObjectSimple which can be used directly
  266. // (i.e. without having to derive from them) for standard data type transfers.
  267. //
  268. // Note that although all of them can work with provided data, you can also
  269. // override their virtual GetXXX() functions to only provide data on demand.
  270. // ============================================================================
  271. // ----------------------------------------------------------------------------
  272. // wxTextDataObject contains text data
  273. // ----------------------------------------------------------------------------
  274. #if wxUSE_UNICODE
  275. #if defined(__WXGTK20__)
  276. #define wxNEEDS_UTF8_FOR_TEXT_DATAOBJ
  277. #elif defined(__WXMAC__)
  278. #define wxNEEDS_UTF16_FOR_TEXT_DATAOBJ
  279. #endif
  280. #endif // wxUSE_UNICODE
  281. class WXDLLIMPEXP_CORE wxHTMLDataObject : public wxDataObjectSimple
  282. {
  283. public:
  284. // ctor: you can specify the text here or in SetText(), or override
  285. // GetText()
  286. wxHTMLDataObject(const wxString& html = wxEmptyString)
  287. : wxDataObjectSimple(wxDF_HTML),
  288. m_html(html)
  289. {
  290. }
  291. // virtual functions which you may override if you want to provide text on
  292. // demand only - otherwise, the trivial default versions will be used
  293. virtual size_t GetLength() const { return m_html.Len() + 1; }
  294. virtual wxString GetHTML() const { return m_html; }
  295. virtual void SetHTML(const wxString& html) { m_html = html; }
  296. virtual size_t GetDataSize() const;
  297. virtual bool GetDataHere(void *buf) const;
  298. virtual bool SetData(size_t len, const void *buf);
  299. // Must provide overloads to avoid hiding them (and warnings about it)
  300. virtual size_t GetDataSize(const wxDataFormat&) const
  301. {
  302. return GetDataSize();
  303. }
  304. virtual bool GetDataHere(const wxDataFormat&, void *buf) const
  305. {
  306. return GetDataHere(buf);
  307. }
  308. virtual bool SetData(const wxDataFormat&, size_t len, const void *buf)
  309. {
  310. return SetData(len, buf);
  311. }
  312. private:
  313. wxString m_html;
  314. };
  315. class WXDLLIMPEXP_CORE wxTextDataObject : public wxDataObjectSimple
  316. {
  317. public:
  318. // ctor: you can specify the text here or in SetText(), or override
  319. // GetText()
  320. wxTextDataObject(const wxString& text = wxEmptyString)
  321. : wxDataObjectSimple(
  322. #if wxUSE_UNICODE
  323. wxDF_UNICODETEXT
  324. #else
  325. wxDF_TEXT
  326. #endif
  327. ),
  328. m_text(text)
  329. {
  330. }
  331. // virtual functions which you may override if you want to provide text on
  332. // demand only - otherwise, the trivial default versions will be used
  333. virtual size_t GetTextLength() const { return m_text.Len() + 1; }
  334. virtual wxString GetText() const { return m_text; }
  335. virtual void SetText(const wxString& text) { m_text = text; }
  336. // implement base class pure virtuals
  337. // ----------------------------------
  338. // some platforms have 2 and not 1 format for text data
  339. #if defined(wxNEEDS_UTF8_FOR_TEXT_DATAOBJ) || defined(wxNEEDS_UTF16_FOR_TEXT_DATAOBJ)
  340. virtual size_t GetFormatCount(Direction WXUNUSED(dir) = Get) const { return 2; }
  341. virtual void GetAllFormats(wxDataFormat *formats,
  342. wxDataObjectBase::Direction WXUNUSED(dir) = Get) const;
  343. virtual size_t GetDataSize() const { return GetDataSize(GetPreferredFormat()); }
  344. virtual bool GetDataHere(void *buf) const { return GetDataHere(GetPreferredFormat(), buf); }
  345. virtual bool SetData(size_t len, const void *buf) { return SetData(GetPreferredFormat(), len, buf); }
  346. size_t GetDataSize(const wxDataFormat& format) const;
  347. bool GetDataHere(const wxDataFormat& format, void *pBuf) const;
  348. bool SetData(const wxDataFormat& format, size_t nLen, const void* pBuf);
  349. #else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ
  350. virtual size_t GetDataSize() const;
  351. virtual bool GetDataHere(void *buf) const;
  352. virtual bool SetData(size_t len, const void *buf);
  353. // Must provide overloads to avoid hiding them (and warnings about it)
  354. virtual size_t GetDataSize(const wxDataFormat&) const
  355. {
  356. return GetDataSize();
  357. }
  358. virtual bool GetDataHere(const wxDataFormat&, void *buf) const
  359. {
  360. return GetDataHere(buf);
  361. }
  362. virtual bool SetData(const wxDataFormat&, size_t len, const void *buf)
  363. {
  364. return SetData(len, buf);
  365. }
  366. #endif // different wxTextDataObject implementations
  367. private:
  368. wxString m_text;
  369. wxDECLARE_NO_COPY_CLASS(wxTextDataObject);
  370. };
  371. // ----------------------------------------------------------------------------
  372. // wxBitmapDataObject contains a bitmap
  373. // ----------------------------------------------------------------------------
  374. class WXDLLIMPEXP_CORE wxBitmapDataObjectBase : public wxDataObjectSimple
  375. {
  376. public:
  377. // ctor: you can specify the bitmap here or in SetBitmap(), or override
  378. // GetBitmap()
  379. wxBitmapDataObjectBase(const wxBitmap& bitmap = wxNullBitmap)
  380. : wxDataObjectSimple(wxDF_BITMAP), m_bitmap(bitmap)
  381. {
  382. }
  383. // virtual functions which you may override if you want to provide data on
  384. // demand only - otherwise, the trivial default versions will be used
  385. virtual wxBitmap GetBitmap() const { return m_bitmap; }
  386. virtual void SetBitmap(const wxBitmap& bitmap) { m_bitmap = bitmap; }
  387. protected:
  388. wxBitmap m_bitmap;
  389. wxDECLARE_NO_COPY_CLASS(wxBitmapDataObjectBase);
  390. };
  391. // ----------------------------------------------------------------------------
  392. // wxFileDataObject contains a list of filenames
  393. //
  394. // NB: notice that this is a "write only" object, it can only be filled with
  395. // data from drag and drop operation.
  396. // ----------------------------------------------------------------------------
  397. class WXDLLIMPEXP_CORE wxFileDataObjectBase : public wxDataObjectSimple
  398. {
  399. public:
  400. // ctor: use AddFile() later to fill the array
  401. wxFileDataObjectBase() : wxDataObjectSimple(wxDF_FILENAME) { }
  402. // get a reference to our array
  403. const wxArrayString& GetFilenames() const { return m_filenames; }
  404. protected:
  405. wxArrayString m_filenames;
  406. wxDECLARE_NO_COPY_CLASS(wxFileDataObjectBase);
  407. };
  408. // ----------------------------------------------------------------------------
  409. // wxCustomDataObject contains arbitrary untyped user data.
  410. //
  411. // It is understood that this data can be copied bitwise.
  412. // ----------------------------------------------------------------------------
  413. class WXDLLIMPEXP_CORE wxCustomDataObject : public wxDataObjectSimple
  414. {
  415. public:
  416. // if you don't specify the format in the ctor, you can still use
  417. // SetFormat() later
  418. wxCustomDataObject(const wxDataFormat& format = wxFormatInvalid);
  419. // the dtor calls Free()
  420. virtual ~wxCustomDataObject();
  421. // you can call SetData() to set m_data: it will make a copy of the data
  422. // you pass - or you can use TakeData() which won't copy anything, but
  423. // will take ownership of data (i.e. will call Free() on it later)
  424. void TakeData(size_t size, void *data);
  425. // this function is called to allocate "size" bytes of memory from
  426. // SetData(). The default version uses operator new[].
  427. virtual void *Alloc(size_t size);
  428. // this function is called when the data is freed, you may override it to
  429. // anything you want (or may be nothing at all). The default version calls
  430. // operator delete[] on m_data
  431. virtual void Free();
  432. // get data: you may override these functions if you wish to provide data
  433. // only when it's requested
  434. virtual size_t GetSize() const { return m_size; }
  435. virtual void *GetData() const { return m_data; }
  436. // implement base class pure virtuals
  437. // ----------------------------------
  438. virtual size_t GetDataSize() const;
  439. virtual bool GetDataHere(void *buf) const;
  440. virtual bool SetData(size_t size, const void *buf);
  441. // Must provide overloads to avoid hiding them (and warnings about it)
  442. virtual size_t GetDataSize(const wxDataFormat&) const
  443. {
  444. return GetDataSize();
  445. }
  446. virtual bool GetDataHere(const wxDataFormat&, void *buf) const
  447. {
  448. return GetDataHere(buf);
  449. }
  450. virtual bool SetData(const wxDataFormat&, size_t len, const void *buf)
  451. {
  452. return SetData(len, buf);
  453. }
  454. private:
  455. size_t m_size;
  456. void *m_data;
  457. wxDECLARE_NO_COPY_CLASS(wxCustomDataObject);
  458. };
  459. // ----------------------------------------------------------------------------
  460. // include platform-specific declarations of wxXXXBase classes
  461. // ----------------------------------------------------------------------------
  462. #if defined(__WXMSW__)
  463. #include "wx/msw/ole/dataobj2.h"
  464. // wxURLDataObject defined in msw/ole/dataobj2.h
  465. #elif defined(__WXGTK20__)
  466. #include "wx/gtk/dataobj2.h"
  467. // wxURLDataObject defined in msw/ole/dataobj2.h
  468. #else
  469. #if defined(__WXGTK__)
  470. #include "wx/gtk1/dataobj2.h"
  471. #elif defined(__WXX11__)
  472. #include "wx/x11/dataobj2.h"
  473. #elif defined(__WXMOTIF__)
  474. #include "wx/motif/dataobj2.h"
  475. #elif defined(__WXMAC__)
  476. #include "wx/osx/dataobj2.h"
  477. #elif defined(__WXCOCOA__)
  478. #include "wx/cocoa/dataobj2.h"
  479. #elif defined(__WXPM__)
  480. #include "wx/os2/dataobj2.h"
  481. #endif
  482. // wxURLDataObject is simply wxTextDataObject with a different name
  483. class WXDLLIMPEXP_CORE wxURLDataObject : public wxTextDataObject
  484. {
  485. public:
  486. wxURLDataObject(const wxString& url = wxEmptyString)
  487. : wxTextDataObject(url)
  488. {
  489. }
  490. wxString GetURL() const { return GetText(); }
  491. void SetURL(const wxString& url) { SetText(url); }
  492. };
  493. #endif
  494. #endif // wxUSE_DATAOBJ
  495. #endif // _WX_DATAOBJ_H_BASE_