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