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