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
100 char *t
= XGetAtomName ((Display
*) wxGetDisplay(), m_format
);
101 wxString
ret( t
); // this will convert from ascii to Unicode
107 void wxDataFormat::SetId( NativeFormat format
)
112 if (m_format
== g_textAtom
)
115 if (m_format
== g_pngAtom
)
116 m_type
= wxDF_BITMAP
;
118 if (m_format
== g_fileAtom
)
119 m_type
= wxDF_FILENAME
;
121 m_type
= wxDF_PRIVATE
;
124 void wxDataFormat::SetId( const wxChar
*id
)
127 m_type
= wxDF_PRIVATE
;
129 m_format
= XInternAtom( (Display
*) wxGetDisplay(), wxMBSTRINGCAST tmp
.mbc_str(), FALSE
); // what is the string cast for?
132 void wxDataFormat::PrepareFormats()
135 g_textAtom
= XInternAtom( (Display
*) wxGetDisplay(), "STRING", FALSE
);
137 g_pngAtom
= XInternAtom( (Display
*) wxGetDisplay(), "image/png", FALSE
);
139 g_fileAtom
= XInternAtom( (Display
*) wxGetDisplay(), "text/uri-list", FALSE
);
142 //-------------------------------------------------------------------------
144 //-------------------------------------------------------------------------
146 wxDataObject::wxDataObject()
150 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
152 size_t nFormatCount
= GetFormatCount(dir
);
153 if ( nFormatCount
== 1 )
155 return format
== GetPreferredFormat();
159 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
160 GetAllFormats(formats
,dir
);
163 for ( n
= 0; n
< nFormatCount
; n
++ )
165 if ( formats
[n
] == format
)
172 return n
< nFormatCount
;
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 bool wxFileDataObject::GetDataHere(void *buf
) const
184 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
186 filenames
+= m_filenames
[i
];
187 filenames
+= (wxChar
) 0;
190 memcpy( buf
, filenames
.mbc_str(), filenames
.Len() + 1 );
195 size_t wxFileDataObject::GetDataSize() const
199 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
201 res
+= m_filenames
[i
].Len();
208 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
212 // filenames are stores as a string with #0 as deliminators
213 const char *filenames
= (const char*) buf
;
217 if (filenames
[0] == 0)
221 wxString
file( filenames
); // this returns the first file
224 filenames
+= file
.Len()+1;
229 // the text/uri-list format is a sequence of URIs (filenames prefixed by
230 // "file:" as far as I see) delimited by "\r\n" of total length size
231 // (I wonder what happens if the file has '\n' in its filename??)
233 for ( const char *p
= (const char *)buf
; ; p
++ )
235 // some broken programs (testdnd GTK+ sample!) omit the trailing
236 // "\r\n", so check for '\0' explicitly here instead of doing it in
237 // the loop statement to account for it
238 if ( (*p
== '\r' && *(p
+1) == '\n') || !*p
)
240 size_t lenPrefix
= 5; // strlen("file:")
241 if ( filename
.Left(lenPrefix
).MakeLower() == _T("file:") )
243 // sometimes the syntax is "file:filename", sometimes it's
244 // URL-like: "file://filename" - deal with both
245 if ( filename
[lenPrefix
] == _T('/') &&
246 filename
[lenPrefix
+ 1] == _T('/') )
252 AddFile(filename
.c_str() + lenPrefix
);
257 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
277 void wxFileDataObject::AddFile( const wxString
&filename
)
279 m_filenames
.Add( filename
);
282 // ----------------------------------------------------------------------------
283 // wxBitmapDataObject
284 // ----------------------------------------------------------------------------
286 wxBitmapDataObject::wxBitmapDataObject()
291 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
292 : wxBitmapDataObjectBase(bitmap
)
299 wxBitmapDataObject::~wxBitmapDataObject()
304 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
308 wxBitmapDataObjectBase::SetBitmap(bitmap
);
313 bool wxBitmapDataObject::GetDataHere(void *buf
) const
317 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
322 memcpy(buf
, m_pngData
, m_pngSize
);
327 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
333 m_pngData
= malloc(m_pngSize
);
335 memcpy( m_pngData
, buf
, m_pngSize
);
337 wxMemoryInputStream
mstream( (char*) m_pngData
, m_pngSize
);
339 wxPNGHandler handler
;
340 if ( !handler
.LoadFile( &image
, mstream
) )
345 m_bitmap
= image
.ConvertToBitmap();
347 return m_bitmap
.Ok();
353 void wxBitmapDataObject::DoConvertToPng()
359 wxImage
image( m_bitmap
);
360 wxPNGHandler handler
;
362 wxCountingOutputStream count
;
363 handler
.SaveFile( &image
, count
);
365 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
366 m_pngData
= malloc(m_pngSize
);
368 wxMemoryOutputStream
mstream( (char*) m_pngData
, m_pngSize
);
369 handler
.SaveFile( &image
, mstream
);