1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataObject class
4 // Author: Julian Smart
6 // Copyright: (c) 1998 Julian Smart
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "dataobj.h"
18 #include "wx/dataobj.h"
19 #include "wx/mstream.h"
25 #include "wx/x11/private.h"
27 //-------------------------------------------------------------------------
29 //-------------------------------------------------------------------------
35 //-------------------------------------------------------------------------
37 //-------------------------------------------------------------------------
39 wxDataFormat::wxDataFormat()
41 // do *not* call PrepareFormats() from here for 2 reasons:
43 // 1. we will have time to do it later because some other Set function
44 // must be called before we really need them
46 // 2. doing so prevents us from declaring global wxDataFormats because
47 // calling PrepareFormats (and thus gdk_atom_intern) before GDK is
48 // initialised will result in a crash
49 m_type
= wxDF_INVALID
;
53 wxDataFormat::wxDataFormat( wxDataFormatId type
)
59 wxDataFormat::wxDataFormat( const wxChar
*id
)
65 wxDataFormat::wxDataFormat( const wxString
&id
)
71 wxDataFormat::wxDataFormat( NativeFormat format
)
77 void wxDataFormat::SetType( wxDataFormatId type
)
82 if (m_type
== wxDF_TEXT
)
83 m_format
= g_textAtom
;
85 if (m_type
== wxDF_BITMAP
)
88 if (m_type
== wxDF_FILENAME
)
89 m_format
= g_fileAtom
;
92 wxFAIL_MSG( wxT("invalid dataformat") );
96 wxDataFormatId
wxDataFormat::GetType() const
101 wxString
wxDataFormat::GetId() const
104 return wxEmptyString
;
106 char *t
= XGetAtomName ((Display
*) wxGetDisplay(), m_format
);
107 wxString ret
= wxString::FromAscii( t
);
114 void wxDataFormat::SetId( NativeFormat format
)
119 if (m_format
== g_textAtom
)
122 if (m_format
== g_pngAtom
)
123 m_type
= wxDF_BITMAP
;
125 if (m_format
== g_fileAtom
)
126 m_type
= wxDF_FILENAME
;
128 m_type
= wxDF_PRIVATE
;
131 void wxDataFormat::SetId( const wxChar
*id
)
135 m_type
= wxDF_PRIVATE
;
137 m_format
= XInternAtom( (Display
*) wxGetDisplay(), tmp
.ToAscii(), FALSE
);
141 void wxDataFormat::PrepareFormats()
145 g_textAtom
= XInternAtom( (Display
*) wxGetDisplay(), "STRING", FALSE
);
147 g_pngAtom
= XInternAtom( (Display
*) wxGetDisplay(), "image/png", FALSE
);
149 g_fileAtom
= XInternAtom( (Display
*) wxGetDisplay(), "text/uri-list", FALSE
);
153 //-------------------------------------------------------------------------
155 //-------------------------------------------------------------------------
157 wxDataObject::wxDataObject()
161 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
163 size_t nFormatCount
= GetFormatCount(dir
);
164 if ( nFormatCount
== 1 )
166 return format
== GetPreferredFormat();
170 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
171 GetAllFormats(formats
,dir
);
174 for ( n
= 0; n
< nFormatCount
; n
++ )
176 if ( formats
[n
] == format
)
183 return n
< nFormatCount
;
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
191 bool wxFileDataObject::GetDataHere(void *buf
) const
195 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
197 filenames
+= m_filenames
[i
];
198 filenames
+= (wxChar
) 0;
201 memcpy( buf
, filenames
.mbc_str(), filenames
.Len() + 1 );
206 size_t wxFileDataObject::GetDataSize() const
210 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
212 res
+= m_filenames
[i
].Len();
219 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
223 // filenames are stores as a string with #0 as deliminators
224 const char *filenames
= (const char*) buf
;
228 if (filenames
[0] == 0)
232 wxString
file( filenames
); // this returns the first file
235 filenames
+= file
.Len()+1;
240 // the text/uri-list format is a sequence of URIs (filenames prefixed by
241 // "file:" as far as I see) delimited by "\r\n" of total length size
242 // (I wonder what happens if the file has '\n' in its filename??)
244 for ( const char *p
= (const char *)buf
; ; p
++ )
246 // some broken programs (testdnd GTK+ sample!) omit the trailing
247 // "\r\n", so check for '\0' explicitly here instead of doing it in
248 // the loop statement to account for it
249 if ( (*p
== '\r' && *(p
+1) == '\n') || !*p
)
251 size_t lenPrefix
= 5; // strlen("file:")
252 if ( filename
.Left(lenPrefix
).MakeLower() == _T("file:") )
254 // sometimes the syntax is "file:filename", sometimes it's
255 // URL-like: "file://filename" - deal with both
256 if ( filename
[lenPrefix
] == _T('/') &&
257 filename
[lenPrefix
+ 1] == _T('/') )
263 AddFile(filename
.c_str() + lenPrefix
);
268 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
288 void wxFileDataObject::AddFile( const wxString
&filename
)
290 m_filenames
.Add( filename
);
293 // ----------------------------------------------------------------------------
294 // wxBitmapDataObject
295 // ----------------------------------------------------------------------------
297 wxBitmapDataObject::wxBitmapDataObject()
302 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
303 : wxBitmapDataObjectBase(bitmap
)
310 wxBitmapDataObject::~wxBitmapDataObject()
315 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
319 wxBitmapDataObjectBase::SetBitmap(bitmap
);
324 bool wxBitmapDataObject::GetDataHere(void *buf
) const
328 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
333 memcpy(buf
, m_pngData
, m_pngSize
);
338 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
344 m_pngData
= malloc(m_pngSize
);
346 memcpy( m_pngData
, buf
, m_pngSize
);
348 wxMemoryInputStream
mstream( (char*) m_pngData
, m_pngSize
);
350 wxPNGHandler handler
;
351 if ( !handler
.LoadFile( &image
, mstream
) )
358 return m_bitmap
.Ok();
364 void wxBitmapDataObject::DoConvertToPng()
370 wxImage image
= m_bitmap
.ConvertToImage();
371 wxPNGHandler handler
;
373 wxCountingOutputStream count
;
374 handler
.SaveFile( &image
, count
);
376 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
377 m_pngData
= malloc(m_pngSize
);
379 wxMemoryOutputStream
mstream( (char*) m_pngData
, m_pngSize
);
380 handler
.SaveFile( &image
, mstream
);
384 #endif // wxUSE_DATAOBJ