1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataObject class
4 // Author: Julian Smart
6 // Copyright: (c) 1998 Julian Smart
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
14 #include "wx/dataobj.h"
15 #include "wx/mstream.h"
21 #include "wx/x11/private.h"
23 //-------------------------------------------------------------------------
25 //-------------------------------------------------------------------------
31 //-------------------------------------------------------------------------
33 //-------------------------------------------------------------------------
35 wxDataFormat::wxDataFormat()
37 // do *not* call PrepareFormats() from here for 2 reasons:
39 // 1. we will have time to do it later because some other Set function
40 // must be called before we really need them
42 // 2. doing so prevents us from declaring global wxDataFormats because
43 // calling PrepareFormats (and thus gdk_atom_intern) before GDK is
44 // initialised will result in a crash
45 m_type
= wxDF_INVALID
;
49 wxDataFormat::wxDataFormat( wxDataFormatId type
)
55 wxDataFormat::wxDataFormat( const wxChar
*id
)
61 wxDataFormat::wxDataFormat( const wxString
&id
)
67 wxDataFormat::wxDataFormat( NativeFormat format
)
73 void wxDataFormat::SetType( wxDataFormatId type
)
78 if (m_type
== wxDF_TEXT
)
79 m_format
= g_textAtom
;
81 if (m_type
== wxDF_BITMAP
)
84 if (m_type
== wxDF_FILENAME
)
85 m_format
= g_fileAtom
;
88 wxFAIL_MSG( wxT("invalid dataformat") );
92 wxDataFormatId
wxDataFormat::GetType() const
97 wxString
wxDataFormat::GetId() const
100 return wxEmptyString
;
102 char *t
= XGetAtomName ((Display
*) wxGetDisplay(), m_format
);
103 wxString ret
= wxString::FromAscii( t
);
110 void wxDataFormat::SetId( NativeFormat format
)
115 if (m_format
== g_textAtom
)
118 if (m_format
== g_pngAtom
)
119 m_type
= wxDF_BITMAP
;
121 if (m_format
== g_fileAtom
)
122 m_type
= wxDF_FILENAME
;
124 m_type
= wxDF_PRIVATE
;
127 void wxDataFormat::SetId( const wxChar
*id
)
131 m_type
= wxDF_PRIVATE
;
133 m_format
= XInternAtom( (Display
*) wxGetDisplay(), tmp
.ToAscii(), FALSE
);
137 void wxDataFormat::PrepareFormats()
141 g_textAtom
= XInternAtom( (Display
*) wxGetDisplay(), "STRING", FALSE
);
143 g_pngAtom
= XInternAtom( (Display
*) wxGetDisplay(), "image/png", FALSE
);
145 g_fileAtom
= XInternAtom( (Display
*) wxGetDisplay(), "text/uri-list", FALSE
);
149 //-------------------------------------------------------------------------
151 //-------------------------------------------------------------------------
153 wxDataObject::wxDataObject()
157 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
159 size_t nFormatCount
= GetFormatCount(dir
);
160 if ( nFormatCount
== 1 )
162 return format
== GetPreferredFormat();
166 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
167 GetAllFormats(formats
,dir
);
170 for ( n
= 0; n
< nFormatCount
; n
++ )
172 if ( formats
[n
] == format
)
179 return n
< nFormatCount
;
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 bool wxFileDataObject::GetDataHere(void *buf
) const
191 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
193 filenames
+= m_filenames
[i
];
194 filenames
+= (wxChar
) 0;
197 memcpy( buf
, filenames
.mbc_str(), filenames
.Len() + 1 );
202 size_t wxFileDataObject::GetDataSize() const
206 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
208 res
+= m_filenames
[i
].Len();
215 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
219 // filenames are stores as a string with #0 as deliminators
220 const char *filenames
= (const char*) buf
;
224 if (filenames
[0] == 0)
228 wxString
file( filenames
); // this returns the first file
231 filenames
+= file
.Len()+1;
236 // the text/uri-list format is a sequence of URIs (filenames prefixed by
237 // "file:" as far as I see) delimited by "\r\n" of total length size
238 // (I wonder what happens if the file has '\n' in its filename??)
240 for ( const char *p
= (const char *)buf
; ; p
++ )
242 // some broken programs (testdnd GTK+ sample!) omit the trailing
243 // "\r\n", so check for '\0' explicitly here instead of doing it in
244 // the loop statement to account for it
245 if ( (*p
== '\r' && *(p
+1) == '\n') || !*p
)
247 size_t lenPrefix
= 5; // strlen("file:")
248 if ( filename
.Left(lenPrefix
).MakeLower() == _T("file:") )
250 // sometimes the syntax is "file:filename", sometimes it's
251 // URL-like: "file://filename" - deal with both
252 if ( filename
[lenPrefix
] == _T('/') &&
253 filename
[lenPrefix
+ 1] == _T('/') )
259 AddFile(filename
.c_str() + lenPrefix
);
264 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
284 void wxFileDataObject::AddFile( const wxString
&filename
)
286 m_filenames
.Add( filename
);
289 // ----------------------------------------------------------------------------
290 // wxBitmapDataObject
291 // ----------------------------------------------------------------------------
293 wxBitmapDataObject::wxBitmapDataObject()
298 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
299 : wxBitmapDataObjectBase(bitmap
)
306 wxBitmapDataObject::~wxBitmapDataObject()
311 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
315 wxBitmapDataObjectBase::SetBitmap(bitmap
);
320 bool wxBitmapDataObject::GetDataHere(void *buf
) const
324 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
329 memcpy(buf
, m_pngData
, m_pngSize
);
334 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
340 m_pngData
= malloc(m_pngSize
);
342 memcpy( m_pngData
, buf
, m_pngSize
);
344 wxMemoryInputStream
mstream( (char*) m_pngData
, m_pngSize
);
346 wxPNGHandler handler
;
347 if ( !handler
.LoadFile( &image
, mstream
) )
354 return m_bitmap
.Ok();
360 void wxBitmapDataObject::DoConvertToPng()
366 wxImage image
= m_bitmap
.ConvertToImage();
367 wxPNGHandler handler
;
369 wxCountingOutputStream count
;
370 handler
.SaveFile( &image
, count
);
372 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
373 m_pngData
= malloc(m_pngSize
);
375 wxMemoryOutputStream
mstream( (char*) m_pngData
, m_pngSize
);
376 handler
.SaveFile( &image
, mstream
);
380 #endif // wxUSE_DATAOBJ