]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dataobj.h
1. made wxBase compile/link/run again under Unix
[wxWidgets.git] / include / wx / dataobj.h
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 license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_DATAOBJ_H_BASE_
13 #define _WX_DATAOBJ_H_BASE_
14
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"
82 #elif defined(__WXPM__)
83 #include "wx/os2/dataform.h"
84 #endif
85
86 // ----------------------------------------------------------------------------
87 // wxDataObject represents a piece of data which knows which formats it
88 // supports and knows how to render itself in each of them - GetDataHere(),
89 // and how to restore data from the buffer (SetData()).
90 //
91 // Although this class may be used directly (i.e. custom classes may be
92 // derived from it), in many cases it might be simpler to use either
93 // wxDataObjectSimple or wxDataObjectComposite classes.
94 //
95 // A data object may be "read only", i.e. support only GetData() functions or
96 // "read-write", i.e. support both GetData() and SetData() (in principle, it
97 // might be "write only" too, but this is rare). Moreover, it doesn't have to
98 // support the same formats in Get() and Set() directions: for example, a data
99 // object containing JPEG image might accept BMPs in GetData() because JPEG
100 // image may be easily transformed into BMP but not in SetData(). Accordingly,
101 // all methods dealing with formats take an additional "direction" argument
102 // which is either SET or GET and which tells the function if the format needs
103 // to be supported by SetData() or GetDataHere().
104 // ----------------------------------------------------------------------------
105
106 class WXDLLEXPORT wxDataObjectBase
107 {
108 public:
109 enum Direction
110 {
111 Get = 0x01, // format is supported by GetDataHere()
112 Set = 0x02, // format is supported by SetData()
113 Both = 0x03 // format is supported by both (unused currently)
114 };
115
116 // this class is polymorphic, hence it needs a virtual dtor
117 virtual ~wxDataObjectBase();
118
119 // get the best suited format for rendering our data
120 virtual wxDataFormat GetPreferredFormat(Direction dir = Get) const = 0;
121
122 // get the number of formats we support
123 virtual size_t GetFormatCount(Direction dir = Get) const = 0;
124
125 // return all formats in the provided array (of size GetFormatCount())
126 virtual void GetAllFormats(wxDataFormat *formats,
127 Direction dir = Get) const = 0;
128
129 // get the (total) size of data for the given format
130 virtual size_t GetDataSize(const wxDataFormat& format) const = 0;
131
132 // copy raw data (in the specified format) to the provided buffer, return
133 // TRUE if data copied successfully, FALSE otherwise
134 virtual bool GetDataHere(const wxDataFormat& format, void *buf) const = 0;
135
136 // get data from the buffer of specified length (in the given format),
137 // return TRUE if the data was read successfully, FALSE otherwise
138 virtual bool SetData(const wxDataFormat& WXUNUSED(format),
139 size_t WXUNUSED(len), const void * WXUNUSED(buf))
140 {
141 return FALSE;
142 }
143 };
144
145 // ----------------------------------------------------------------------------
146 // include the platform-specific declarations of wxDataObject
147 // ----------------------------------------------------------------------------
148
149 #if defined(__WXMSW__)
150 #include "wx/msw/ole/dataobj.h"
151 #elif defined(__WXMOTIF__)
152 #include "wx/motif/dataobj.h"
153 #elif defined(__WXGTK__)
154 #include "wx/gtk/dataobj.h"
155 #elif defined(__WXQT__)
156 #include "wx/qt/dnd.h"
157 #elif defined(__WXMAC__)
158 #include "wx/mac/dnd.h"
159 #elif defined(__WXPM__)
160 #include "wx/os2/dataobj.h"
161 #elif defined(__WXSTUBS__)
162 #include "wx/stubs/dnd.h"
163 #endif
164
165 // ----------------------------------------------------------------------------
166 // wxDataObjectSimple is a wxDataObject which only supports one format (in
167 // both Get and Set directions, but you may return FALSE from GetDataHere() or
168 // SetData() if one of them is not supported). This is the simplest possible
169 // wxDataObject implementation.
170 //
171 // This is still an "abstract base class" (although it doesn't have any pure
172 // virtual functions), to use it you should derive from it and implement
173 // GetDataSize(), GetDataHere() and SetData() functions because the base class
174 // versions don't do anything - they just return "not implemented".
175 //
176 // This class should be used when you provide data in only one format (no
177 // conversion to/from other formats), either a standard or a custom one.
178 // Otherwise, you should use wxDataObjectComposite or wxDataObject directly.
179 // ----------------------------------------------------------------------------
180
181 class WXDLLEXPORT wxDataObjectSimple : public wxDataObject
182 {
183 public:
184 // ctor takes the format we support, but it can also be set later with
185 // SetFormat()
186 wxDataObjectSimple(const wxDataFormat& format = wxDF_INVALID)
187 : m_format(format)
188 {
189 }
190
191 // get/set the format we support
192 const wxDataFormat& GetFormat() const { return m_format; }
193 void SetFormat(const wxDataFormat& format) { m_format = format; }
194
195 // virtual functions to override in derived class (the base class versions
196 // just return "not implemented")
197 // -----------------------------------------------------------------------
198
199 // get the size of our data
200 virtual size_t GetDataSize() const
201 { return 0; }
202
203 // copy our data to the buffer
204 virtual bool GetDataHere(void *WXUNUSED(buf)) const
205 { return FALSE; }
206
207 // copy data from buffer to our data
208 virtual bool SetData(size_t WXUNUSED(len), const void *WXUNUSED(buf))
209 { return FALSE; }
210
211 // implement base class pure virtuals
212 // ----------------------------------
213 virtual wxDataFormat GetPreferredFormat(wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
214 { return m_format; }
215 virtual size_t GetFormatCount(wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
216 { return 1; }
217 virtual void GetAllFormats(wxDataFormat *formats,
218 wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
219 { *formats = m_format; }
220 virtual size_t GetDataSize(const wxDataFormat& WXUNUSED(format)) const
221 { return GetDataSize(); }
222 virtual bool GetDataHere(const wxDataFormat& WXUNUSED(format),
223 void *buf) const
224 { return GetDataHere(buf); }
225 virtual bool SetData(const wxDataFormat& WXUNUSED(format),
226 size_t len, const void *buf)
227 { return SetData(len, buf); }
228
229 private:
230 // the one and only format we support
231 wxDataFormat m_format;
232 };
233
234 // ----------------------------------------------------------------------------
235 // wxDataObjectComposite is the simplest way to implement wxDataObject
236 // supporting multiple formats. It contains several wxDataObjectSimple and
237 // supports all formats supported by any of them.
238 //
239 // This class shouldn't be (normally) derived from, but may be used directly.
240 // If you need more flexibility than what it provides, you should probably use
241 // wxDataObject directly.
242 // ----------------------------------------------------------------------------
243
244 WX_DECLARE_LIST(wxDataObjectSimple, wxSimpleDataObjectList);
245
246 class WXDLLEXPORT wxDataObjectComposite : public wxDataObject
247 {
248 public:
249 // ctor
250 wxDataObjectComposite() { m_preferred = 0; }
251
252 // add data object (it will be deleted by wxDataObjectComposite, hence it
253 // must be allocated on the heap) whose format will become the preferred
254 // one if preferred == TRUE
255 void Add(wxDataObjectSimple *dataObject, bool preferred = FALSE);
256
257 // implement base class pure virtuals
258 // ----------------------------------
259 virtual wxDataFormat GetPreferredFormat(wxDataObjectBase::Direction dir = Get) const;
260 virtual size_t GetFormatCount(wxDataObjectBase::Direction dir = Get) const;
261 virtual void GetAllFormats(wxDataFormat *formats, wxDataObjectBase::Direction dir = Get) const;
262 virtual size_t GetDataSize(const wxDataFormat& format) const;
263 virtual bool GetDataHere(const wxDataFormat& format, void *buf) const;
264 virtual bool SetData(const wxDataFormat& format, size_t len, const void *buf);
265
266 protected:
267 // returns the pointer to the object which supports this format or NULL
268 wxDataObjectSimple *GetObject(const wxDataFormat& format) const;
269
270 private:
271 // the list of all (simple) data objects whose formats we support
272 wxSimpleDataObjectList m_dataObjects;
273
274 // the index of the preferred one (0 initially, so by default the first
275 // one is the preferred)
276 size_t m_preferred;
277 };
278
279 // ============================================================================
280 // Standard implementations of wxDataObjectSimple which can be used directly
281 // (i.e. without having to derive from them) for standard data type transfers.
282 //
283 // Note that although all of them can work with provided data, you can also
284 // override their virtual GetXXX() functions to only provide data on demand.
285 // ============================================================================
286
287 // ----------------------------------------------------------------------------
288 // wxTextDataObject contains text data
289 // ----------------------------------------------------------------------------
290
291 class WXDLLEXPORT wxTextDataObject : public wxDataObjectSimple
292 {
293 public:
294 // ctor: you can specify the text here or in SetText(), or override
295 // GetText()
296 wxTextDataObject(const wxString& text = wxEmptyString)
297 : wxDataObjectSimple(wxDF_TEXT), m_text(text)
298 {
299 }
300
301 // virtual functions which you may override if you want to provide text on
302 // demand only - otherwise, the trivial default versions will be used
303 virtual size_t GetTextLength() const { return m_text.Len() + 1; }
304 virtual wxString GetText() const { return m_text; }
305 virtual void SetText(const wxString& text) { m_text = text; }
306
307 // implement base class pure virtuals
308 // ----------------------------------
309 virtual size_t GetDataSize() const;
310 virtual bool GetDataHere(void *buf) const;
311 virtual bool SetData(size_t len, const void *buf);
312
313 private:
314 wxString m_text;
315
316 #if defined(__VISAGECPP__)
317 // Virtual function hiding supression
318 size_t GetDataSize(const wxDataFormat& rFormat) const
319 { return(wxDataObjectSimple::GetDataSize(rFormat)); }
320 bool GetDataHere(const wxDataFormat& WXUNUSED(rFormat), void *pBuf) const
321 { return(GetDataHere(pBuf)); }
322 bool SetData(const wxDataFormat& rFormat, size_t nLen, const void* pBuf)
323 { return(wxDataObjectSimple::SetData(rFormat, nLen, pBuf)); }
324 #endif
325 };
326
327 // ----------------------------------------------------------------------------
328 // wxBitmapDataObject contains a bitmap
329 // ----------------------------------------------------------------------------
330
331 class WXDLLEXPORT wxBitmapDataObjectBase : public wxDataObjectSimple
332 {
333 public:
334 // ctor: you can specify the bitmap here or in SetBitmap(), or override
335 // GetBitmap()
336 wxBitmapDataObjectBase(const wxBitmap& bitmap = wxNullBitmap)
337 : wxDataObjectSimple(wxDF_BITMAP), m_bitmap(bitmap)
338 {
339 }
340
341 // virtual functions which you may override if you want to provide data on
342 // demand only - otherwise, the trivial default versions will be used
343 virtual wxBitmap GetBitmap() const { return m_bitmap; }
344 virtual void SetBitmap(const wxBitmap& bitmap) { m_bitmap = bitmap; }
345
346 protected:
347 wxBitmap m_bitmap;
348 };
349
350 // ----------------------------------------------------------------------------
351 // wxFileDataObject contains a list of filenames
352 //
353 // NB: notice that this is a "write only" object, it can only be filled with
354 // data from drag and drop operation.
355 // ----------------------------------------------------------------------------
356
357 class WXDLLEXPORT wxFileDataObjectBase : public wxDataObjectSimple
358 {
359 public:
360 // ctor: use AddFile() later to fill the array
361 wxFileDataObjectBase() : wxDataObjectSimple(wxDF_FILENAME) { }
362
363 // get a reference to our array
364 const wxArrayString& GetFilenames() const { return m_filenames; }
365
366 // the Get() functions do nothing for us
367 virtual size_t GetDataSize() const { return 0; }
368 virtual bool GetDataHere(void *WXUNUSED(buf)) const { return FALSE; }
369
370 protected:
371 wxArrayString m_filenames;
372
373 #if defined(__VISAGECPP__)
374 private:
375 // Virtual function hiding supression
376 size_t GetDataSize(const wxDataFormat& rFormat) const
377 { return(wxDataObjectSimple::GetDataSize(rFormat)); }
378 bool GetDataHere(const wxDataFormat& WXUNUSED(rformat), void* pBuf) const
379 { return(GetDataHere(pBuf)); }
380 #endif
381 };
382
383 // ----------------------------------------------------------------------------
384 // wxCustomDataObject contains arbitrary untyped user data.
385 //
386 // It is understood that this data can be copied bitwise.
387 // ----------------------------------------------------------------------------
388
389 class WXDLLEXPORT wxCustomDataObject : public wxDataObjectSimple
390 {
391 public:
392 // if you don't specify the format in the ctor, you can still use
393 // SetFormat() later
394 wxCustomDataObject(const wxDataFormat& format = wxDF_INVALID);
395
396 // the dtor calls Free()
397 virtual ~wxCustomDataObject();
398
399 // you can call SetData() to set m_data: it will make a copy of the data
400 // you pass - or you can use TakeData() which won't copy anything, but
401 // will take ownership of data (i.e. will call Free() on it later)
402 void TakeData(size_t size, void *data);
403
404 // this function is called to allocate "size" bytes of memory from
405 // SetData(). The default version uses operator new[].
406 virtual void *Alloc(size_t size);
407
408 // this function is called when the data is freed, you may override it to
409 // anything you want (or may be nothing at all). The default version calls
410 // operator delete[] on m_data
411 virtual void Free();
412
413 // get data: you may override these functions if you wish to provide data
414 // only when it's requested
415 virtual size_t GetSize() const { return m_size; }
416 virtual void *GetData() const { return m_data; }
417
418 // implement base class pure virtuals
419 // ----------------------------------
420 virtual size_t GetDataSize() const;
421 virtual bool GetDataHere(void *buf) const;
422 virtual bool SetData(size_t size, const void *buf);
423
424 private:
425 size_t m_size;
426 void *m_data;
427
428 #if defined(__VISAGECPP__)
429 // Virtual function hiding supression
430 size_t GetDataSize(const wxDataFormat& rFormat) const
431 { return(wxDataObjectSimple::GetDataSize(rFormat)); }
432 bool GetDataHere(const wxDataFormat& rFormat, void* pBuf) const
433 { return(wxDataObjectSimple::GetDataHere(rFormat, pBuf)); }
434 bool SetData(const wxDataFormat& rFormat, size_t nLen, const void* pBuf)
435 { return(wxDataObjectSimple::SetData(rFormat, nLen, pBuf)); }
436 #endif
437 };
438
439 // ----------------------------------------------------------------------------
440 // include platform-specific declarations of wxXXXBase classes
441 // ----------------------------------------------------------------------------
442
443 #if defined(__WXMSW__)
444 #include "wx/msw/ole/dataobj2.h"
445 #elif defined(__WXMOTIF__)
446 // #include "wx/motif/dataobj2.h" -- not yet
447 #elif defined(__WXGTK__)
448 #include "wx/gtk/dataobj2.h"
449 #elif defined(__WXPM__)
450 #include "wx/os2/dataobj2.h"
451 #endif
452
453 #endif // _WX_DATAOBJ_H_BASE_