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