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