]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/dataobj.cpp
Changed two conflicting defines in defs.h
[wxWidgets.git] / src / gtk1 / dataobj.cpp
CommitLineData
8b53e5a2
RR
1///////////////////////////////////////////////////////////////////////////////
2// Name: dataobj.cpp
3// Purpose: wxDataObject class
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
3f480da3 7// Licence: wxWindows licence
8b53e5a2
RR
8///////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
3f480da3 11 #pragma implementation "dataobj.h"
8b53e5a2
RR
12#endif
13
14#include "wx/dataobj.h"
ab8884ac 15#include "wx/app.h"
0d2a2b60 16#include "wx/debug.h"
e2acb9ae
RR
17#include "wx/mstream.h"
18#include "wx/image.h"
0d2a2b60
RR
19
20#include "gdk/gdk.h"
21
d6086ea6
RR
22
23//-------------------------------------------------------------------------
24// global data
25//-------------------------------------------------------------------------
26
27GdkAtom g_textAtom = 0;
e2acb9ae 28GdkAtom g_pngAtom = 0;
1dd989e1 29GdkAtom g_fileAtom = 0;
d6086ea6 30
0d2a2b60
RR
31//-------------------------------------------------------------------------
32// wxDataFormat
33//-------------------------------------------------------------------------
34
cd5bf2a6 35wxDataFormat::wxDataFormat()
0d2a2b60 36{
e2acb9ae 37 PrepareFormats();
cd5bf2a6 38 m_type = wxDF_INVALID;
1dd989e1 39 m_format = (GdkAtom) 0;
cd5bf2a6
RR
40}
41
3f480da3 42wxDataFormat::wxDataFormat( wxDataFormatId type )
cd5bf2a6 43{
e2acb9ae 44 PrepareFormats();
cd5bf2a6 45 SetType( type );
0d2a2b60
RR
46}
47
b5be07d4 48wxDataFormat::wxDataFormat( const wxChar *id )
8a126fcc 49{
e2acb9ae 50 PrepareFormats();
8a126fcc
RR
51 SetId( id );
52}
53
0d2a2b60
RR
54wxDataFormat::wxDataFormat( const wxString &id )
55{
e2acb9ae 56 PrepareFormats();
cd5bf2a6 57 SetId( id );
0d2a2b60
RR
58}
59
1dd989e1 60wxDataFormat::wxDataFormat( NativeFormat format )
0d2a2b60 61{
e2acb9ae 62 PrepareFormats();
1dd989e1 63 SetId( format );
0d2a2b60
RR
64}
65
3f480da3 66void wxDataFormat::SetType( wxDataFormatId type )
cd5bf2a6
RR
67{
68 m_type = type;
3f480da3 69
cd5bf2a6 70 if (m_type == wxDF_TEXT)
1dd989e1 71 m_format = g_textAtom;
cd5bf2a6
RR
72 else
73 if (m_type == wxDF_BITMAP)
1dd989e1 74 m_format = g_pngAtom;
cd5bf2a6
RR
75 else
76 if (m_type == wxDF_FILENAME)
1dd989e1 77 m_format = g_fileAtom;
cd5bf2a6
RR
78 else
79 {
223d09f6 80 wxFAIL_MSG( wxT("invalid dataformat") );
cd5bf2a6 81 }
cd5bf2a6 82}
3f480da3
VZ
83
84wxDataFormatId wxDataFormat::GetType() const
0d2a2b60
RR
85{
86 return m_type;
87}
88
89wxString wxDataFormat::GetId() const
90{
1dd989e1
RR
91 wxString ret( gdk_atom_name( m_format ) ); // this will convert from ascii to Unicode
92 return ret;
0d2a2b60 93}
3f480da3 94
1dd989e1 95void wxDataFormat::SetId( NativeFormat format )
3f480da3 96{
1dd989e1 97 m_format = format;
3f480da3 98
1dd989e1
RR
99 if (m_format == g_textAtom)
100 m_type = wxDF_TEXT;
101 else
102 if (m_format == g_pngAtom)
103 m_type = wxDF_BITMAP;
104 else
105 if (m_format == g_fileAtom)
106 m_type = wxDF_FILENAME;
107 else
108 m_type = wxDF_PRIVATE;
0d2a2b60 109}
3f480da3 110
1dd989e1 111void wxDataFormat::SetId( const wxChar *id )
0d2a2b60 112{
1dd989e1
RR
113 m_type = wxDF_PRIVATE;
114 wxString tmp( id );
115 m_format = gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE ); // what is the string cast for?
0d2a2b60 116}
3f480da3 117
1dd989e1 118void wxDataFormat::PrepareFormats()
0d2a2b60 119{
1dd989e1
RR
120 if (!g_textAtom)
121 g_textAtom = gdk_atom_intern( "STRING", FALSE );
122 if (!g_pngAtom)
123 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
124 if (!g_fileAtom)
125 g_fileAtom = gdk_atom_intern( "file:ALL", FALSE );
0d2a2b60 126}
8b53e5a2
RR
127
128//-------------------------------------------------------------------------
129// wxDataObject
130//-------------------------------------------------------------------------
131
132IMPLEMENT_ABSTRACT_CLASS( wxDataObject, wxObject )
133
0d2a2b60
RR
134wxDataObject::wxDataObject()
135{
0d2a2b60 136}
3f480da3 137
0d2a2b60
RR
138wxDataObject::~wxDataObject()
139{
0d2a2b60
RR
140}
141
1dd989e1 142bool wxDataObject::IsSupportedFormat(const wxDataFormat& format) const
0d2a2b60 143{
1dd989e1
RR
144 size_t nFormatCount = GetFormatCount();
145 if ( nFormatCount == 1 ) {
146 return format == GetPreferredFormat();
147 }
148 else {
149 wxDataFormat *formats = new wxDataFormat[nFormatCount];
150 GetAllFormats(formats);
151
152 size_t n;
153 for ( n = 0; n < nFormatCount; n++ ) {
154 if ( formats[n] == format )
155 break;
156 }
0d2a2b60 157
1dd989e1 158 delete [] formats;
cd5bf2a6 159
1dd989e1
RR
160 // found?
161 return n < nFormatCount;
162 }
0d2a2b60
RR
163}
164
cd5bf2a6 165
8b53e5a2
RR
166// ----------------------------------------------------------------------------
167// wxTextDataObject
168// ----------------------------------------------------------------------------
169
170IMPLEMENT_DYNAMIC_CLASS( wxTextDataObject, wxDataObject )
171
1dd989e1
RR
172wxTextDataObject::wxTextDataObject()
173{
0d2a2b60
RR
174}
175
1dd989e1
RR
176wxTextDataObject::wxTextDataObject(const wxString& strText)
177 : m_strText(strText)
178{
0d2a2b60
RR
179}
180
1dd989e1
RR
181size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
182{
183 return m_strText.Len() + 1; // +1 for trailing '\0'of course
0d2a2b60
RR
184}
185
1dd989e1
RR
186bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
187{
188 memcpy(buf, m_strText.c_str(), GetDataSize(format));
189 return TRUE;
0d2a2b60
RR
190}
191
1dd989e1
RR
192bool wxTextDataObject::SetData(const wxDataFormat& format, const void *buf)
193{
194 m_strText = (const wxChar *)buf;
195 return TRUE;
0d2a2b60 196}
3f480da3 197
8b53e5a2
RR
198// ----------------------------------------------------------------------------
199// wxFileDataObject
200// ----------------------------------------------------------------------------
201
202IMPLEMENT_DYNAMIC_CLASS( wxFileDataObject, wxDataObject )
203
cd5bf2a6 204wxFileDataObject::wxFileDataObject()
0d2a2b60 205{
0d2a2b60
RR
206}
207
208void wxFileDataObject::AddFile( const wxString &file )
3f480da3
VZ
209{
210 m_files += file;
211 m_files += (wxChar)0;
0d2a2b60 212}
3f480da3 213
0d2a2b60 214wxString wxFileDataObject::GetFiles() const
3f480da3
VZ
215{
216 return m_files;
0d2a2b60 217}
3f480da3 218
1dd989e1 219bool wxFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
0d2a2b60 220{
1dd989e1
RR
221 if (format == wxDF_FILENAME)
222 {
223 memcpy( buf, m_files.mbc_str(), m_files.Len() + 1 );
224 return TRUE;
225 }
226
227 return FALSE;
0d2a2b60 228}
3f480da3 229
1dd989e1 230size_t wxFileDataObject::GetDataSize(const wxDataFormat& format) const
0d2a2b60 231{
1dd989e1
RR
232 if (format != wxDF_FILENAME) return 0;
233
0d2a2b60
RR
234 return m_files.Len() + 1;
235}
3f480da3 236
1dd989e1
RR
237bool wxFileDataObject::SetData(const wxDataFormat& format, const void *buf)
238{
239 if (format != wxDF_FILENAME)
240 return FALSE;
241
242 m_files = (char*)(buf); // this is so ugly, I cannot look at it
243
244 return TRUE;
245}
246
8b53e5a2
RR
247// ----------------------------------------------------------------------------
248// wxBitmapDataObject
249// ----------------------------------------------------------------------------
250
251IMPLEMENT_DYNAMIC_CLASS( wxBitmapDataObject, wxDataObject )
252
0d2a2b60
RR
253wxBitmapDataObject::wxBitmapDataObject()
254{
1dd989e1 255 m_pngData = (void*)NULL;
e2acb9ae 256 m_pngSize = 0;
0d2a2b60
RR
257}
258
259wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
260{
1dd989e1 261 m_pngData = (void*)NULL;
e2acb9ae 262 m_pngSize = 0;
0d2a2b60 263 m_bitmap = bitmap;
e2acb9ae
RR
264 DoConvertToPng();
265}
266
267wxBitmapDataObject::~wxBitmapDataObject()
268{
1dd989e1
RR
269 if (m_pngData)
270 delete[] m_pngData;
0d2a2b60
RR
271}
272
273void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
274{
1dd989e1
RR
275 if (m_pngData)
276 delete[] m_pngData;
277 m_pngData = (void*)NULL;
278 m_pngSize = 0;
279
0d2a2b60 280 m_bitmap = bitmap;
e2acb9ae 281 DoConvertToPng();
0d2a2b60
RR
282}
283
1dd989e1 284size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const
0d2a2b60 285{
1dd989e1
RR
286 if (format != wxDF_BITMAP) return 0;
287
288 return m_pngSize;
0d2a2b60
RR
289}
290
1dd989e1 291bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
0d2a2b60 292{
1dd989e1
RR
293 if (format != wxDF_BITMAP) return FALSE;
294
295 if (m_pngSize > 0)
296 {
297 memcpy(buf, m_pngData, m_pngSize);
298 return TRUE;
299 }
300
301 return FALSE;
0d2a2b60
RR
302}
303
1dd989e1 304bool wxBitmapDataObject::SetData(const wxDataFormat& format, const void *buf)
0d2a2b60 305{
1dd989e1
RR
306 if (m_pngData) delete[] m_pngData;
307 m_pngData = (void*) NULL;
308 m_pngSize = 0;
309 m_bitmap = wxNullBitmap;
310
311 return FALSE;
e2acb9ae
RR
312}
313
1dd989e1 314void wxBitmapDataObject::SetPngData(const void *buf, size_t size)
e2acb9ae
RR
315{
316 if (m_pngData) delete[] m_pngData;
1dd989e1
RR
317 m_pngSize = size;
318 m_pngData = (void*) new char[m_pngSize];
e2acb9ae 319
1dd989e1
RR
320 memcpy( m_pngData, buf, m_pngSize );
321
322 wxMemoryInputStream mstream( (char*) m_pngData, m_pngSize );
e2acb9ae
RR
323 wxImage image;
324 wxPNGHandler handler;
325 handler.LoadFile( &image, mstream );
326 m_bitmap = image.ConvertToBitmap();
327}
328
329void wxBitmapDataObject::DoConvertToPng()
330{
1dd989e1 331 if (!m_bitmap.Ok()) return;
e2acb9ae
RR
332
333 wxImage image( m_bitmap );
334 wxPNGHandler handler;
335
336 wxCountingOutputStream count;
337 handler.SaveFile( &image, count );
338 m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
1dd989e1 339 m_pngData = (void*) new char[m_pngSize];
e2acb9ae 340
1dd989e1 341 wxMemoryOutputStream mstream( (char*) m_pngData, m_pngSize );
e2acb9ae 342 handler.SaveFile( &image, mstream );
0d2a2b60 343}
3f480da3 344
8b53e5a2
RR
345// ----------------------------------------------------------------------------
346// wxPrivateDataObject
347// ----------------------------------------------------------------------------
348
1dd989e1 349IMPLEMENT_CLASS( wxPrivateDataObject, wxDataObject )
8b53e5a2 350
3f480da3
VZ
351void wxPrivateDataObject::Free()
352{
353 if ( m_data )
354 free(m_data);
0d2a2b60
RR
355}
356
3f480da3 357wxPrivateDataObject::wxPrivateDataObject()
8b53e5a2 358{
223d09f6 359 wxString id = wxT("application/");
3f480da3 360 id += wxTheApp->GetAppName();
8b53e5a2 361
3f480da3
VZ
362 m_format.SetId( id );
363
364 m_size = 0;
365 m_data = (void *)NULL;
8b53e5a2
RR
366}
367
3f480da3 368void wxPrivateDataObject::SetData( const void *data, size_t size )
0d2a2b60 369{
3f480da3
VZ
370 Free();
371
372 m_size = size;
373 m_data = malloc(size);
374
375 memcpy( m_data, data, size );
0d2a2b60
RR
376}
377
378void wxPrivateDataObject::WriteData( void *dest ) const
379{
380 WriteData( m_data, dest );
381}
382
383size_t wxPrivateDataObject::GetSize() const
384{
3f480da3 385 return m_size;
0d2a2b60
RR
386}
387
3f480da3 388void wxPrivateDataObject::WriteData( const void *data, void *dest ) const
0d2a2b60
RR
389{
390 memcpy( dest, data, GetSize() );
391}
392