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