]> git.saurik.com Git - wxWidgets.git/blame - include/wx/dataobj.h
Implemented wxDC stack with focus locking and unlocking
[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"
a6abcf2e 29#include "wx/arrstr.h"
e1ee679c
VZ
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
56class wxDataFormat
57{
58public:
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"
83df96d6
JS
85#elif defined(__WXX11__)
86 #include "wx/x11/dataform.h"
e7549107
SC
87#elif defined(__WXMAC__)
88 #include "wx/mac/dataform.h"
4004aba3
DW
89#elif defined(__WXPM__)
90 #include "wx/os2/dataform.h"
e1ee679c
VZ
91#endif
92
0c2b453f
VZ
93// the value for default argument to some functions (corresponds to
94// wxDF_INVALID)
6f4968e2 95extern WXDLLEXPORT const wxDataFormat& wxFormatInvalid;
0c2b453f 96
e1ee679c
VZ
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
117class WXDLLEXPORT wxDataObjectBase
118{
119public:
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
f2491e2d
VZ
149 virtual bool SetData(const wxDataFormat& WXUNUSED(format),
150 size_t WXUNUSED(len), const void * WXUNUSED(buf))
9e2896e5
VZ
151 {
152 return FALSE;
153 }
d9317fd4
VZ
154
155 // returns TRUE if this format is supported
156 bool IsSupported(const wxDataFormat& format, Direction dir = Get) const;
e1ee679c
VZ
157};
158
159// ----------------------------------------------------------------------------
160// include the platform-specific declarations of wxDataObject
161// ----------------------------------------------------------------------------
162
8b53e5a2 163#if defined(__WXMSW__)
3f480da3 164 #include "wx/msw/ole/dataobj.h"
8b53e5a2 165#elif defined(__WXMOTIF__)
3f480da3 166 #include "wx/motif/dataobj.h"
7266b672
JS
167#elif defined(__WXX11__)
168 #include "wx/x11/dataobj.h"
8b53e5a2 169#elif defined(__WXGTK__)
3f480da3 170 #include "wx/gtk/dataobj.h"
8b53e5a2 171#elif defined(__WXMAC__)
e7549107 172 #include "wx/mac/dataobj.h"
1777b9bb 173#elif defined(__WXPM__)
6dddc146 174 #include "wx/os2/dataobj.h"
8b53e5a2
RR
175#endif
176
e1ee679c
VZ
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// ----------------------------------------------------------------------------
3f480da3 192
e1ee679c 193class WXDLLEXPORT wxDataObjectSimple : public wxDataObject
3f480da3 194{
e1ee679c
VZ
195public:
196 // ctor takes the format we support, but it can also be set later with
197 // SetFormat()
1e8335b0 198 wxDataObjectSimple(const wxDataFormat& format = wxFormatInvalid)
e1ee679c
VZ
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; }
3f480da3 214
e1ee679c
VZ
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
e30d5976 220 virtual bool SetData(size_t WXUNUSED(len), const void *WXUNUSED(buf))
e1ee679c
VZ
221 { return FALSE; }
222
223 // implement base class pure virtuals
224 // ----------------------------------
4004aba3 225 virtual wxDataFormat GetPreferredFormat(wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
e1ee679c 226 { return m_format; }
4004aba3 227 virtual size_t GetFormatCount(wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
e1ee679c
VZ
228 { return 1; }
229 virtual void GetAllFormats(wxDataFormat *formats,
4004aba3 230 wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
e1ee679c
VZ
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
241private:
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
f6bcfd97 256WX_DECLARE_EXPORTED_LIST(wxDataObjectSimple, wxSimpleDataObjectList);
e1ee679c
VZ
257
258class WXDLLEXPORT wxDataObjectComposite : public wxDataObject
259{
3f480da3 260public:
e1ee679c 261 // ctor
e6b01b78 262 wxDataObjectComposite();
222ed1d6 263 virtual ~wxDataObjectComposite();
e1ee679c
VZ
264
265 // add data object (it will be deleted by wxDataObjectComposite, hence it
266 // must be allocated on the heap) whose format will become the preferred
267 // one if preferred == TRUE
268 void Add(wxDataObjectSimple *dataObject, bool preferred = FALSE);
3f480da3 269
e1ee679c
VZ
270 // implement base class pure virtuals
271 // ----------------------------------
4004aba3
DW
272 virtual wxDataFormat GetPreferredFormat(wxDataObjectBase::Direction dir = Get) const;
273 virtual size_t GetFormatCount(wxDataObjectBase::Direction dir = Get) const;
274 virtual void GetAllFormats(wxDataFormat *formats, wxDataObjectBase::Direction dir = Get) const;
e1ee679c
VZ
275 virtual size_t GetDataSize(const wxDataFormat& format) const;
276 virtual bool GetDataHere(const wxDataFormat& format, void *buf) const;
277 virtual bool SetData(const wxDataFormat& format, size_t len, const void *buf);
3f480da3 278
e1ee679c
VZ
279protected:
280 // returns the pointer to the object which supports this format or NULL
281 wxDataObjectSimple *GetObject(const wxDataFormat& format) const;
e1b435af
MB
282#if defined(__WXMSW__)
283 virtual const void* GetSizeFromBuffer( const void* buffer, size_t* size,
284 const wxDataFormat& format );
285 virtual void* SetSizeInBuffer( void* buffer, size_t size,
286 const wxDataFormat& format );
287 virtual size_t GetBufferOffset( const wxDataFormat& format );
288#endif
3f480da3 289
e1ee679c
VZ
290private:
291 // the list of all (simple) data objects whose formats we support
292 wxSimpleDataObjectList m_dataObjects;
3f480da3 293
e1ee679c
VZ
294 // the index of the preferred one (0 initially, so by default the first
295 // one is the preferred)
296 size_t m_preferred;
297};
3f480da3 298
e1ee679c
VZ
299// ============================================================================
300// Standard implementations of wxDataObjectSimple which can be used directly
301// (i.e. without having to derive from them) for standard data type transfers.
302//
303// Note that although all of them can work with provided data, you can also
304// override their virtual GetXXX() functions to only provide data on demand.
305// ============================================================================
3f480da3 306
e1ee679c
VZ
307// ----------------------------------------------------------------------------
308// wxTextDataObject contains text data
309// ----------------------------------------------------------------------------
3f480da3 310
e1ee679c
VZ
311class WXDLLEXPORT wxTextDataObject : public wxDataObjectSimple
312{
313public:
314 // ctor: you can specify the text here or in SetText(), or override
315 // GetText()
316 wxTextDataObject(const wxString& text = wxEmptyString)
e1b435af
MB
317 : wxDataObjectSimple(wxUSE_UNICODE?wxDF_UNICODETEXT:wxDF_TEXT),
318 m_text(text)
e1ee679c
VZ
319 {
320 }
3f480da3 321
e1ee679c
VZ
322 // virtual functions which you may override if you want to provide text on
323 // demand only - otherwise, the trivial default versions will be used
324 virtual size_t GetTextLength() const { return m_text.Len() + 1; }
325 virtual wxString GetText() const { return m_text; }
326 virtual void SetText(const wxString& text) { m_text = text; }
327
328 // implement base class pure virtuals
329 // ----------------------------------
330 virtual size_t GetDataSize() const;
331 virtual bool GetDataHere(void *buf) const;
332 virtual bool SetData(size_t len, const void *buf);
3f480da3
VZ
333
334private:
e1ee679c 335 wxString m_text;
6dddc146 336
b02da6b1
VZ
337 // virtual function hiding supression
338 size_t GetDataSize(const wxDataFormat& format) const
339 { return(wxDataObjectSimple::GetDataSize(format)); }
87a1e308
VZ
340 bool GetDataHere(const wxDataFormat& format, void *pBuf) const
341 { return(wxDataObjectSimple::GetDataHere(format, pBuf)); }
b02da6b1
VZ
342 bool SetData(const wxDataFormat& format, size_t nLen, const void* pBuf)
343 { return(wxDataObjectSimple::SetData(format, nLen, pBuf)); }
e1ee679c 344};
3f480da3 345
e1ee679c
VZ
346// ----------------------------------------------------------------------------
347// wxBitmapDataObject contains a bitmap
348// ----------------------------------------------------------------------------
3f480da3 349
e1ee679c
VZ
350class WXDLLEXPORT wxBitmapDataObjectBase : public wxDataObjectSimple
351{
352public:
353 // ctor: you can specify the bitmap here or in SetBitmap(), or override
354 // GetBitmap()
355 wxBitmapDataObjectBase(const wxBitmap& bitmap = wxNullBitmap)
356 : wxDataObjectSimple(wxDF_BITMAP), m_bitmap(bitmap)
357 {
358 }
359
360 // virtual functions which you may override if you want to provide data on
361 // demand only - otherwise, the trivial default versions will be used
362 virtual wxBitmap GetBitmap() const { return m_bitmap; }
363 virtual void SetBitmap(const wxBitmap& bitmap) { m_bitmap = bitmap; }
364
b068c4e8 365protected:
e1ee679c
VZ
366 wxBitmap m_bitmap;
367};
368
369// ----------------------------------------------------------------------------
370// wxFileDataObject contains a list of filenames
9e2896e5
VZ
371//
372// NB: notice that this is a "write only" object, it can only be filled with
373// data from drag and drop operation.
e1ee679c
VZ
374// ----------------------------------------------------------------------------
375
376class WXDLLEXPORT wxFileDataObjectBase : public wxDataObjectSimple
377{
378public:
9e2896e5 379 // ctor: use AddFile() later to fill the array
e1ee679c
VZ
380 wxFileDataObjectBase() : wxDataObjectSimple(wxDF_FILENAME) { }
381
9e2896e5 382 // get a reference to our array
b068c4e8 383 const wxArrayString& GetFilenames() const { return m_filenames; }
e1ee679c 384
9e2896e5
VZ
385 // the Get() functions do nothing for us
386 virtual size_t GetDataSize() const { return 0; }
387 virtual bool GetDataHere(void *WXUNUSED(buf)) const { return FALSE; }
e1ee679c 388
9e2896e5 389protected:
e1ee679c 390 wxArrayString m_filenames;
6dddc146 391
6dddc146
DW
392private:
393 // Virtual function hiding supression
b02da6b1
VZ
394 size_t GetDataSize(const wxDataFormat& format) const
395 { return(wxDataObjectSimple::GetDataSize(format)); }
87a1e308
VZ
396 bool GetDataHere(const wxDataFormat& format, void* pBuf) const
397 { return(wxDataObjectSimple::GetDataHere(format, pBuf)); }
e1ee679c
VZ
398};
399
400// ----------------------------------------------------------------------------
401// wxCustomDataObject contains arbitrary untyped user data.
402//
403// It is understood that this data can be copied bitwise.
404// ----------------------------------------------------------------------------
405
406class WXDLLEXPORT wxCustomDataObject : public wxDataObjectSimple
407{
408public:
409 // if you don't specify the format in the ctor, you can still use
410 // SetFormat() later
0c2b453f 411 wxCustomDataObject(const wxDataFormat& format = wxFormatInvalid);
e1ee679c
VZ
412
413 // the dtor calls Free()
414 virtual ~wxCustomDataObject();
415
416 // you can call SetData() to set m_data: it will make a copy of the data
417 // you pass - or you can use TakeData() which won't copy anything, but
418 // will take ownership of data (i.e. will call Free() on it later)
419 void TakeData(size_t size, void *data);
420
421 // this function is called to allocate "size" bytes of memory from
422 // SetData(). The default version uses operator new[].
423 virtual void *Alloc(size_t size);
424
425 // this function is called when the data is freed, you may override it to
426 // anything you want (or may be nothing at all). The default version calls
427 // operator delete[] on m_data
428 virtual void Free();
429
430 // get data: you may override these functions if you wish to provide data
431 // only when it's requested
432 virtual size_t GetSize() const { return m_size; }
433 virtual void *GetData() const { return m_data; }
434
435 // implement base class pure virtuals
436 // ----------------------------------
437 virtual size_t GetDataSize() const;
438 virtual bool GetDataHere(void *buf) const;
439 virtual bool SetData(size_t size, const void *buf);
440
441private:
442 size_t m_size;
443 void *m_data;
6dddc146 444
b02da6b1
VZ
445 // virtual function hiding supression
446 size_t GetDataSize(const wxDataFormat& format) const
447 { return(wxDataObjectSimple::GetDataSize(format)); }
448 bool GetDataHere(const wxDataFormat& format, void* pBuf) const
449 { return(wxDataObjectSimple::GetDataHere(format, pBuf)); }
450 bool SetData(const wxDataFormat& format, size_t nLen, const void* pBuf)
451 { return(wxDataObjectSimple::SetData(format, nLen, pBuf)); }
22f3361e
VZ
452
453 DECLARE_NO_COPY_CLASS(wxCustomDataObject)
3f480da3
VZ
454};
455
e1ee679c
VZ
456// ----------------------------------------------------------------------------
457// include platform-specific declarations of wxXXXBase classes
458// ----------------------------------------------------------------------------
3f480da3 459
e1ee679c
VZ
460#if defined(__WXMSW__)
461 #include "wx/msw/ole/dataobj2.h"
444ad3a7
VZ
462
463 // wxURLDataObject defined in msw/ole/dataobj2.h
464#else // !__WXMSW__
465 #if defined(__WXGTK__)
466 #include "wx/gtk/dataobj2.h"
9691c806
RR
467 #elif defined(__WXX11__)
468 #include "wx/x11/dataobj2.h"
dd38c875
MB
469 #elif defined(__WXMOTIF__)
470 #include "wx/motif/dataobj2.h"
444ad3a7
VZ
471 #elif defined(__WXMAC__)
472 #include "wx/mac/dataobj2.h"
473 #elif defined(__WXPM__)
474 #include "wx/os2/dataobj2.h"
475 #endif
476
477 // wxURLDataObject is simply wxTextDataObject with a different name
478 class WXDLLEXPORT wxURLDataObject : public wxTextDataObject
479 {
480 public:
481 wxString GetURL() const { return GetText(); }
e6d318c2 482 void SetURL(const wxString& url) { SetText(url); }
444ad3a7
VZ
483 };
484#endif // __WXMSW__/!__WXMSW__
e1ee679c 485
49de4949
DE
486#endif // wxUSE_DATAOBJ
487
e1ee679c 488#endif // _WX_DATAOBJ_H_BASE_