1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataObject class
4 // Author: Julian Smart
6 // Copyright: (c) 1998 Julian Smart
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "dataobj.h"
16 #include "wx/dataobj.h"
17 #include "wx/mstream.h"
22 #include "wx/x11/private.h"
24 //-------------------------------------------------------------------------
26 //-------------------------------------------------------------------------
32 //-------------------------------------------------------------------------
34 //-------------------------------------------------------------------------
36 wxDataFormat::wxDataFormat()
38 // do *not* call PrepareFormats() from here for 2 reasons:
40 // 1. we will have time to do it later because some other Set function
41 // must be called before we really need them
43 // 2. doing so prevents us from declaring global wxDataFormats because
44 // calling PrepareFormats (and thus gdk_atom_intern) before GDK is
45 // initialised will result in a crash
46 m_type
= wxDF_INVALID
;
50 wxDataFormat::wxDataFormat( wxDataFormatId type
)
56 wxDataFormat::wxDataFormat( const wxChar
*id
)
62 wxDataFormat::wxDataFormat( const wxString
&id
)
68 wxDataFormat::wxDataFormat( NativeFormat format
)
74 void wxDataFormat::SetType( wxDataFormatId type
)
79 if (m_type
== wxDF_TEXT
)
80 m_format
= g_textAtom
;
82 if (m_type
== wxDF_BITMAP
)
85 if (m_type
== wxDF_FILENAME
)
86 m_format
= g_fileAtom
;
89 wxFAIL_MSG( wxT("invalid dataformat") );
93 wxDataFormatId
wxDataFormat::GetType() const
98 wxString
wxDataFormat::GetId() const
101 return wxEmptyString
;
103 char *t
= XGetAtomName ((Display
*) wxGetDisplay(), m_format
);
104 wxString
ret( t
); // this will convert from ascii to Unicode
111 void wxDataFormat::SetId( NativeFormat format
)
116 if (m_format
== g_textAtom
)
119 if (m_format
== g_pngAtom
)
120 m_type
= wxDF_BITMAP
;
122 if (m_format
== g_fileAtom
)
123 m_type
= wxDF_FILENAME
;
125 m_type
= wxDF_PRIVATE
;
128 void wxDataFormat::SetId( const wxChar
*id
)
132 m_type
= wxDF_PRIVATE
;
134 m_format
= XInternAtom( (Display
*) wxGetDisplay(), wxMBSTRINGCAST tmp
.mbc_str(), FALSE
); // what is the string cast for?
138 void wxDataFormat::PrepareFormats()
142 g_textAtom
= XInternAtom( (Display
*) wxGetDisplay(), "STRING", FALSE
);
144 g_pngAtom
= XInternAtom( (Display
*) wxGetDisplay(), "image/png", FALSE
);
146 g_fileAtom
= XInternAtom( (Display
*) wxGetDisplay(), "text/uri-list", FALSE
);
150 //-------------------------------------------------------------------------
152 //-------------------------------------------------------------------------
154 wxDataObject::wxDataObject()
158 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
160 size_t nFormatCount
= GetFormatCount(dir
);
161 if ( nFormatCount
== 1 )
163 return format
== GetPreferredFormat();
167 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
168 GetAllFormats(formats
,dir
);
171 for ( n
= 0; n
< nFormatCount
; n
++ )
173 if ( formats
[n
] == format
)
180 return n
< nFormatCount
;
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 bool wxFileDataObject::GetDataHere(void *buf
) const
192 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
194 filenames
+= m_filenames
[i
];
195 filenames
+= (wxChar
) 0;
198 memcpy( buf
, filenames
.mbc_str(), filenames
.Len() + 1 );
203 size_t wxFileDataObject::GetDataSize() const
207 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
209 res
+= m_filenames
[i
].Len();
216 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
220 // filenames are stores as a string with #0 as deliminators
221 const char *filenames
= (const char*) buf
;
225 if (filenames
[0] == 0)
229 wxString
file( filenames
); // this returns the first file
232 filenames
+= file
.Len()+1;
237 // the text/uri-list format is a sequence of URIs (filenames prefixed by
238 // "file:" as far as I see) delimited by "\r\n" of total length size
239 // (I wonder what happens if the file has '\n' in its filename??)
241 for ( const char *p
= (const char *)buf
; ; p
++ )
243 // some broken programs (testdnd GTK+ sample!) omit the trailing
244 // "\r\n", so check for '\0' explicitly here instead of doing it in
245 // the loop statement to account for it
246 if ( (*p
== '\r' && *(p
+1) == '\n') || !*p
)
248 size_t lenPrefix
= 5; // strlen("file:")
249 if ( filename
.Left(lenPrefix
).MakeLower() == _T("file:") )
251 // sometimes the syntax is "file:filename", sometimes it's
252 // URL-like: "file://filename" - deal with both
253 if ( filename
[lenPrefix
] == _T('/') &&
254 filename
[lenPrefix
+ 1] == _T('/') )
260 AddFile(filename
.c_str() + lenPrefix
);
265 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
285 void wxFileDataObject::AddFile( const wxString
&filename
)
287 m_filenames
.Add( filename
);
290 // ----------------------------------------------------------------------------
291 // wxBitmapDataObject
292 // ----------------------------------------------------------------------------
294 wxBitmapDataObject::wxBitmapDataObject()
299 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
300 : wxBitmapDataObjectBase(bitmap
)
307 wxBitmapDataObject::~wxBitmapDataObject()
312 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
316 wxBitmapDataObjectBase::SetBitmap(bitmap
);
321 bool wxBitmapDataObject::GetDataHere(void *buf
) const
325 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
330 memcpy(buf
, m_pngData
, m_pngSize
);
335 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
341 m_pngData
= malloc(m_pngSize
);
343 memcpy( m_pngData
, buf
, m_pngSize
);
345 wxMemoryInputStream
mstream( (char*) m_pngData
, m_pngSize
);
347 wxPNGHandler handler
;
348 if ( !handler
.LoadFile( &image
, mstream
) )
353 m_bitmap
= image
.ConvertToBitmap();
355 return m_bitmap
.Ok();
361 void wxBitmapDataObject::DoConvertToPng()
367 wxImage
image( m_bitmap
);
368 wxPNGHandler handler
;
370 wxCountingOutputStream count
;
371 handler
.SaveFile( &image
, count
);
373 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
374 m_pngData
= malloc(m_pngSize
);
376 wxMemoryOutputStream
mstream( (char*) m_pngData
, m_pngSize
);
377 handler
.SaveFile( &image
, mstream
);