Compile fix for wxDataFormat cast,
[wxWidgets.git] / src / common / dobjcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/dobjcmn.cpp
3 // Purpose: implementation of data object methods common to all platforms
4 // Author: Vadim Zeitlin, Robert Roebling
5 // Modified by:
6 // Created: 19.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows Team
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "dataobjbase.h"
22 #endif
23
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #ifndef WX_PRECOMP
31 #include "wx/app.h"
32 #include "wx/debug.h"
33 #endif // WX_PRECOMP
34
35 #include "wx/dataobj.h"
36
37 // ----------------------------------------------------------------------------
38 // lists
39 // ----------------------------------------------------------------------------
40
41 #include "wx/listimpl.cpp"
42
43 WX_DEFINE_LIST(wxSimpleDataObjectList);
44
45 // ----------------------------------------------------------------------------
46 // globals
47 // ----------------------------------------------------------------------------
48
49 static wxDataFormat dataFormatInvalid;
50 const wxDataFormat& wxFormatInvalid = dataFormatInvalid;
51
52 // ============================================================================
53 // implementation
54 // ============================================================================
55
56 // ----------------------------------------------------------------------------
57 // wxDataObjectBase
58 // ----------------------------------------------------------------------------
59
60 wxDataObjectBase::~wxDataObjectBase()
61 {
62 }
63
64 // ----------------------------------------------------------------------------
65 // wxDataObjectComposite
66 // ----------------------------------------------------------------------------
67
68 wxDataObjectSimple *
69 wxDataObjectComposite::GetObject(const wxDataFormat& format) const
70 {
71 wxSimpleDataObjectList::Node *node = m_dataObjects.GetFirst();
72 while ( node )
73 {
74 wxDataObjectSimple *dataObj = node->GetData();
75
76 if ( dataObj->GetFormat() == format )
77 {
78 return dataObj;
79 }
80
81 node = node->GetNext();
82 }
83
84 return (wxDataObjectSimple *)NULL;
85 }
86
87 void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred)
88 {
89 if ( preferred )
90 m_preferred = m_dataObjects.GetCount();
91
92 m_dataObjects.Append( dataObject );
93 }
94
95 wxDataFormat
96 wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const
97 {
98 wxSimpleDataObjectList::Node *node = m_dataObjects.Item( m_preferred );
99
100 #ifdef __WXGTK__
101 wxCHECK_MSG( node, wxDataFormat(wxDF_INVALID), wxT("no preferred format") );
102 #else
103 wxCHECK_MSG( node, wxDataFormat((unsigned short) wxDF_INVALID), wxT("no preferred format") );
104 #endif
105
106 wxDataObjectSimple* dataObj = node->GetData();
107
108 return dataObj->GetFormat();
109 }
110
111 size_t wxDataObjectComposite::GetFormatCount(Direction WXUNUSED(dir)) const
112 {
113 // TODO what about the Get/Set only formats?
114 return m_dataObjects.GetCount();
115 }
116
117 void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats,
118 Direction WXUNUSED(dir)) const
119 {
120 size_t n = 0;
121 wxSimpleDataObjectList::Node *node;
122 for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() )
123 {
124 // TODO if ( !outputOnlyToo ) && this one counts ...
125 formats[n++] = node->GetData()->GetFormat();
126 }
127 }
128
129 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat& format) const
130 {
131 wxDataObjectSimple *dataObj = GetObject(format);
132
133 wxCHECK_MSG( dataObj, 0,
134 wxT("unsupported format in wxDataObjectComposite"));
135
136 return dataObj->GetDataSize();
137 }
138
139 bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format,
140 void *buf) const
141 {
142 wxDataObjectSimple *dataObj = GetObject(format);
143
144 wxCHECK_MSG( dataObj, FALSE,
145 wxT("unsupported format in wxDataObjectComposite"));
146
147 return dataObj->GetDataHere(buf);
148 }
149
150 bool wxDataObjectComposite::SetData(const wxDataFormat& format,
151 size_t len,
152 const void *buf)
153 {
154 wxDataObjectSimple *dataObj = GetObject(format);
155
156 wxCHECK_MSG( dataObj, FALSE,
157 wxT("unsupported format in wxDataObjectComposite"));
158
159 return dataObj->SetData(len, buf);
160 }
161
162 // ----------------------------------------------------------------------------
163 // wxTextDataObject
164 // ----------------------------------------------------------------------------
165
166 size_t wxTextDataObject::GetDataSize() const
167 {
168 return GetTextLength();
169 }
170
171 bool wxTextDataObject::GetDataHere(void *buf) const
172 {
173 strcpy((char *)buf, GetText().mb_str());
174
175 return TRUE;
176 }
177
178 bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
179 {
180 SetText(wxString((const char *)buf));
181
182 return TRUE;
183 }
184
185 // ----------------------------------------------------------------------------
186 // wxFileDataObjectBase
187 // ----------------------------------------------------------------------------
188
189 // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
190 // be moved to gtk/dataobj.cpp
191 #if 0
192
193 wxString wxFileDataObjectBase::GetFilenames() const
194 {
195 wxString str;
196 size_t count = m_filenames.GetCount();
197 for ( size_t n = 0; n < count; n++ )
198 {
199 str << m_filenames[n] << wxT('\0');
200 }
201
202 return str;
203 }
204
205 void wxFileDataObjectBase::SetFilenames(const wxChar* filenames)
206 {
207 m_filenames.Empty();
208
209 wxString current;
210 for ( const wxChar *pc = filenames; ; pc++ )
211 {
212 if ( *pc )
213 {
214 current += *pc;
215 }
216 else
217 {
218 if ( !current )
219 {
220 // 2 consecutive NULs - this is the end of the string
221 break;
222 }
223
224 m_filenames.Add(current);
225 current.Empty();
226 }
227 }
228 }
229
230 #endif // 0
231
232 // ----------------------------------------------------------------------------
233 // wxCustomDataObject
234 // ----------------------------------------------------------------------------
235
236 wxCustomDataObject::~wxCustomDataObject()
237 {
238 Free();
239 }
240
241 void wxCustomDataObject::TakeData(size_t size, void *data)
242 {
243 Free();
244
245 m_size = size;
246 m_data = data;
247 }
248
249 void *wxCustomDataObject::Alloc(size_t size)
250 {
251 return (void *)new char[size];
252 }
253
254 void wxCustomDataObject::Free()
255 {
256 delete [] m_data;
257 m_size = 0;
258 m_data = (void *)NULL;
259 }
260
261 size_t wxCustomDataObject::GetDataSize() const
262 {
263 return GetSize();
264 }
265
266 bool wxCustomDataObject::GetDataHere(void *buf) const
267 {
268 void *data = GetData();
269 if ( !data )
270 return FALSE;
271
272 memcpy(buf, data, GetSize());
273
274 return TRUE;
275 }
276
277 bool wxCustomDataObject::SetData(size_t size, const void *buf)
278 {
279 Free();
280
281 m_data = Alloc(size);
282 if ( !m_data )
283 return FALSE;
284
285 memcpy(m_data, buf, m_size = size);
286
287 return TRUE;
288 }
289