1. more drag and drop and clipboard changes:
[wxWidgets.git] / include / wx / msw / ole / dataobj.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: ole/dataobj.h
3 // Purpose: declaration of the wxDataObject class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 10.05.98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_OLEDATAOBJ_H
13 #define _WX_OLEDATAOBJ_H
14
15 // ----------------------------------------------------------------------------
16 // wxDataFormat identifies the single format of data
17 // ----------------------------------------------------------------------------
18
19 class WXDLLEXPORT wxDataFormat
20 {
21 public:
22 // the clipboard formats under Win32 are UINTs
23 typedef unsigned int NativeFormat;
24
25 wxDataFormat(NativeFormat format = wxDF_INVALID) { m_format = format; }
26 wxDataFormat(const wxChar *format) { SetId(format); }
27
28 wxDataFormat& operator=(NativeFormat format)
29 { m_format = format; return *this; }
30 wxDataFormat& operator=(const wxDataFormat& format)
31 { m_format = format.m_format; return *this; }
32
33 // defautl copy ctor/assignment operators ok
34
35 // comparison (must have both versions)
36 bool operator==(wxDataFormatId format) const
37 { return m_format == (NativeFormat)format; }
38 bool operator!=(wxDataFormatId format) const
39 { return m_format != (NativeFormat)format; }
40 bool operator==(const wxDataFormat& format) const
41 { return m_format == format.m_format; }
42 bool operator!=(const wxDataFormat& format) const
43 { return m_format != format.m_format; }
44
45 // explicit and implicit conversions to NativeFormat which is one of
46 // standard data types (implicit conversion is useful for preserving the
47 // compatibility with old code)
48 NativeFormat GetFormatId() const { return m_format; }
49 operator NativeFormat() const { return m_format; }
50
51 // this only works with standard ids
52 void SetId(wxDataFormatId format) { m_format = format; }
53
54 // string ids are used for custom types - this SetId() must be used for
55 // application-specific formats
56 wxString GetId() const;
57 void SetId(const wxChar *format);
58
59 private:
60 // returns TRUE if the format is one of those defined in wxDataFormatId
61 bool IsStandard() const { return m_format > 0 && m_format < wxDF_MAX; }
62
63 NativeFormat m_format;
64 };
65
66 // ----------------------------------------------------------------------------
67 // forward declarations
68 // ----------------------------------------------------------------------------
69
70 struct IDataObject;
71
72 // ----------------------------------------------------------------------------
73 // wxDataObject is a "smart" and polymorphic piece of data.
74 // ----------------------------------------------------------------------------
75
76 class WXDLLEXPORT wxDataObject
77 {
78 public:
79 // ctor & dtor
80 wxDataObject();
81 virtual ~wxDataObject();
82
83 // pure virtuals to override
84 // get the best suited format for rendering our data
85 virtual wxDataFormat GetPreferredFormat() const = 0;
86 // get the number of formats we support: it is understood that if we
87 // can accept data in some format, then we can render data in this
88 // format as well, but the contrary is not necessarily true. For the
89 // default value of the argument, all formats we support should be
90 // returned, but if outputOnlyToo == FALSE, then we should only return
91 // the formats which our SetData() understands
92 virtual size_t GetFormatCount(bool outputOnlyToo = TRUE) const
93 { return 1; }
94 // return all formats in the provided array (of size GetFormatCount())
95 virtual void GetAllFormats(wxDataFormat *formats,
96 bool outputOnlyToo = TRUE) const
97 { formats[0] = GetPreferredFormat(); }
98 // get the (total) size of data for the given format
99 virtual size_t GetDataSize(const wxDataFormat& format) const = 0;
100 // copy raw data (in the specified format) to provided pointer
101 virtual bool GetDataHere(const wxDataFormat& format, void *buf) const = 0;
102 // get data from the buffer (in the given format)
103 virtual bool SetData(const wxDataFormat& format, const void *buf) = 0;
104
105 // accessors
106 // retrieve IDataObject interface (for other OLE related classes)
107 IDataObject *GetInterface() const { return m_pIDataObject; }
108 // a simpler name which makes more sense for data objects supporting
109 // only one format
110 wxDataFormat GetFormat() const { return GetPreferredFormat(); }
111
112 // old interface
113 // decide if we support this format (can be either standard or custom
114 // format) -- now uses GetAllFormats()
115 virtual bool IsSupportedFormat(const wxDataFormat& format) const;
116
117 // implementation only from now on
118 // -------------------------------
119
120 // tell the object that it should be now owned by IDataObject - i.e. when
121 // it is deleted, it should delete us as well
122 void SetAutoDelete();
123
124 #ifdef __WXDEBUG__
125 // function to return symbolic name of clipboard format (for debug messages)
126 static const char *GetFormatName(wxDataFormat format);
127 #endif // Debug
128
129 private:
130 IDataObject *m_pIDataObject; // pointer to the COM interface
131 };
132
133 // ----------------------------------------------------------------------------
134 // wxTextDataObject is a specialization of wxDataObject for text data
135 // ----------------------------------------------------------------------------
136
137 class WXDLLEXPORT wxTextDataObject : public wxDataObject
138 {
139 public:
140 // ctors
141 wxTextDataObject() { }
142 wxTextDataObject(const wxString& strText) : m_strText(strText) { }
143 void Init(const wxString& strText) { m_strText = strText; }
144
145 // implement base class pure virtuals
146 virtual wxDataFormat GetPreferredFormat() const
147 { return wxDF_TEXT; }
148 virtual bool IsSupportedFormat(const wxDataFormat& format) const
149 { return format == wxDF_TEXT || format == wxDF_LOCALE; }
150 virtual size_t GetDataSize(const wxDataFormat& format) const
151 { return m_strText.Len() + 1; } // +1 for trailing '\0'of course
152 virtual bool GetDataHere(const wxDataFormat& format, void *buf) const
153 { memcpy(buf, m_strText.c_str(), GetDataSize(format)); return TRUE; }
154 virtual bool SetData(const wxDataFormat& format, const void *buf)
155 { m_strText = (const wxChar *)buf; return TRUE; }
156
157 // additional helpers
158 void SetText(const wxString& strText) { m_strText = strText; }
159 wxString GetText() const { return m_strText; }
160
161 private:
162 wxString m_strText;
163 };
164
165 // ----------------------------------------------------------------------------
166 // wxBitmapDataObject is a specialization of wxDataObject for bitmap data
167 // ----------------------------------------------------------------------------
168
169 #include "wx/bitmap.h"
170
171 class WXDLLEXPORT wxBitmapDataObject : public wxDataObject
172 {
173 public:
174 // ctors
175 wxBitmapDataObject() { }
176 wxBitmapDataObject(const wxBitmap& bitmap): m_bitmap(bitmap) { }
177
178 // set/get our bitmap
179 void SetBitmap(const wxBitmap& bitmap) { m_bitmap = bitmap; }
180 const wxBitmap GetBitmap() const { return m_bitmap; }
181
182 // implement base class pure virtuals
183 virtual wxDataFormat GetPreferredFormat() const { return wxDF_BITMAP; }
184 virtual size_t GetFormatCount(bool outputOnlyToo = TRUE) const;
185 virtual void GetAllFormats(wxDataFormat *formats,
186 bool outputOnlyToo = TRUE) const;
187 virtual size_t GetDataSize(const wxDataFormat& format) const;
188 virtual bool GetDataHere(const wxDataFormat& format, void *buf) const;
189 virtual bool SetData(const wxDataFormat& format, const void *buf);
190
191 private:
192 wxBitmap m_bitmap;
193 };
194
195 // ----------------------------------------------------------------------------
196 // wxMetaFileDataObject: see metafile.h is a specialization of wxDataObject for bitmap data
197 // ----------------------------------------------------------------------------
198
199 // TODO: wxFileDataObject.
200
201 #endif //_WX_OLEDATAOBJ_H
202