1. wxPostEvent added and documented
[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& operator=(NativeFormat format)
27 { m_format = format; return *this; }
28
29 // defautl copy ctor/assignment operators ok
30
31 // comparison (must have both versions)
32 bool operator==(wxDataFormatId format) const
33 { return m_format == (NativeFormat)format; }
34 bool operator!=(wxDataFormatId format) const
35 { return m_format != (NativeFormat)format; }
36 bool operator==(const wxDataFormat& format) const
37 { return m_format == format.m_format; }
38 bool operator!=(const wxDataFormat& format) const
39 { return m_format != format.m_format; }
40
41 // explicit and implicit conversions to NativeFormat which is one of
42 // standard data types (implicit conversion is useful for preserving the
43 // compatibility with old code)
44 NativeFormat GetFormatId() const { return m_format; }
45 operator NativeFormat() const { return m_format; }
46
47 // this only works with standard ids
48 void SetId(wxDataFormatId format) { m_format = format; }
49
50 // string ids are used for custom types - this SetId() must be used for
51 // application-specific formats
52 wxString GetId() const;
53 void SetId(const wxChar *format);
54
55 private:
56 // returns TRUE if the format is one of those defined in wxDataFormatId
57 bool IsStandard() const { return m_format > 0 && m_format < wxDF_MAX; }
58
59 NativeFormat m_format;
60 };
61
62 // ----------------------------------------------------------------------------
63 // forward declarations
64 // ----------------------------------------------------------------------------
65 struct IDataObject;
66
67 // ----------------------------------------------------------------------------
68 // wxDataObject is a "smart" and polymorphic piece of data.
69 //
70 // TODO it's currently "read-only" from COM point of view, i.e. we don't
71 // support SetData. We don't support all advise functions neither (but
72 // it's easy to do if we really want them)
73 // ----------------------------------------------------------------------------
74
75 class WXDLLEXPORT wxDataObject
76 {
77 public:
78 // ctor & dtor
79 wxDataObject();
80 virtual ~wxDataObject();
81
82 // pure virtuals to override
83 // get the best suited format for our data
84 virtual wxDataFormat GetPreferredFormat() const = 0;
85 // get the number of formats we support
86 virtual size_t GetFormatCount() const
87 { return 1; }
88 // return all formats in the provided array (of size GetFormatCount())
89 virtual void GetAllFormats(wxDataFormat *formats) const
90 { formats[0] = GetPreferredFormat(); }
91 // get the (total) size of data for the given format
92 virtual size_t GetDataSize(const wxDataFormat& format) const = 0;
93 // copy raw data (in the specified format) to provided pointer
94 virtual void GetDataHere(const wxDataFormat& format, void *pBuf) const = 0;
95
96 // accessors
97 // retrieve IDataObject interface (for other OLE related classes)
98 IDataObject *GetInterface() const { return m_pIDataObject; }
99 // a simpler name which makes more sense for data objects supporting
100 // only one format
101 wxDataFormat GetFormat() const { return GetPreferredFormat(); }
102
103 // old interface
104 // decide if we support this format (can be either standard or custom
105 // format) -- now uses GetAllFormats()
106 virtual bool IsSupportedFormat(const wxDataFormat& format) const;
107
108 #ifdef __WXDEBUG__
109 // function to return symbolic name of clipboard format (for debug messages)
110 static const char *GetFormatName(wxDataFormat format);
111 #endif // Debug
112
113 private:
114 IDataObject *m_pIDataObject; // pointer to the COM interface
115 };
116
117 // ----------------------------------------------------------------------------
118 // wxTextDataObject is a specialization of wxDataObject for text data
119 // ----------------------------------------------------------------------------
120
121 class WXDLLEXPORT wxTextDataObject : public wxDataObject
122 {
123 public:
124 // ctors
125 wxTextDataObject() { }
126 wxTextDataObject(const wxString& strText) : m_strText(strText) { }
127 void Init(const wxString& strText) { m_strText = strText; }
128
129 // implement base class pure virtuals
130 virtual wxDataFormat GetPreferredFormat() const
131 { return wxDF_TEXT; }
132 virtual bool IsSupportedFormat(const wxDataFormat& format) const
133 { return format == wxDF_TEXT || format == wxDF_LOCALE; }
134 virtual size_t GetDataSize(const wxDataFormat& format) const
135 { return m_strText.Len() + 1; } // +1 for trailing '\0'of course
136 virtual void GetDataHere(const wxDataFormat& format, void *pBuf) const
137 { memcpy(pBuf, m_strText.c_str(), GetDataSize(format)); }
138
139 // additional helpers
140 void SetText(const wxString& strText) { m_strText = strText; }
141 wxString GetText() const { return m_strText; }
142
143 private:
144 wxString m_strText;
145 };
146
147 // ----------------------------------------------------------------------------
148 // TODO: wx{Bitmap|Metafile|...}DataObject
149 // ----------------------------------------------------------------------------
150
151 // ----------------------------------------------------------------------------
152 // wxBitmapDataObject is a specialization of wxDataObject for bitmap data
153 // ----------------------------------------------------------------------------
154
155 // TODO: implement OLE side of things. At present, it's just for clipboard
156 // use.
157 #include "wx/bitmap.h"
158
159 class WXDLLEXPORT wxBitmapDataObject : public wxDataObject
160 {
161 public:
162 // ctors
163 wxBitmapDataObject() { }
164 wxBitmapDataObject(const wxBitmap& bitmap): m_bitmap(bitmap) { }
165
166 // set/get our bitmap
167 void SetBitmap(const wxBitmap& bitmap) { m_bitmap = bitmap; }
168 const wxBitmap GetBitmap() const { return m_bitmap; }
169
170 // implement base class pure virtuals
171 virtual wxDataFormat GetPreferredFormat() const
172 { return wxDF_BITMAP; }
173 virtual bool IsSupportedFormat(const wxDataFormat& format) const
174 { return format == wxDF_BITMAP; }
175 virtual size_t GetDataSize(const wxDataFormat& format) const;
176 virtual void GetDataHere(const wxDataFormat& format, void *pBuf) const;
177
178 private:
179 wxBitmap m_bitmap;
180 };
181
182 // ----------------------------------------------------------------------------
183 // wxMetaFileDataObject: see metafile.h is a specialization of wxDataObject for bitmap data
184 // ----------------------------------------------------------------------------
185
186 // TODO: wxFileDataObject.
187
188 #endif //_WX_OLEDATAOBJ_H
189