1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/dataobj.cpp
3 // Purpose: wxDataObject class
4 // Author: Julian Smart
6 // Copyright: (c) 1998 Julian Smart
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // for compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/dataobj.h"
24 #include "wx/mstream.h"
26 #include "wx/x11/private.h"
28 //-------------------------------------------------------------------------
30 //-------------------------------------------------------------------------
36 //-------------------------------------------------------------------------
38 //-------------------------------------------------------------------------
40 wxDataFormat::wxDataFormat()
42 // do *not* call PrepareFormats() from here for 2 reasons:
44 // 1. we will have time to do it later because some other Set function
45 // must be called before we really need them
47 // 2. doing so prevents us from declaring global wxDataFormats because
48 // calling PrepareFormats (and thus gdk_atom_intern) before GDK is
49 // initialised will result in a crash
50 m_type
= wxDF_INVALID
;
54 wxDataFormat::wxDataFormat( wxDataFormatId type
)
60 wxDataFormat::wxDataFormat( const wxString
&id
)
66 wxDataFormat::wxDataFormat( NativeFormat format
)
72 void wxDataFormat::SetType( wxDataFormatId type
)
77 if (m_type
== wxDF_TEXT
|| m_type
== wxDF_UNICODETEXT
)
78 m_format
= g_textAtom
;
80 if (m_type
== wxDF_BITMAP
)
83 if (m_type
== wxDF_FILENAME
)
84 m_format
= g_fileAtom
;
87 wxFAIL_MSG( wxT("invalid dataformat") );
91 wxDataFormatId
wxDataFormat::GetType() const
96 wxString
wxDataFormat::GetId() const
101 char *t
= XGetAtomName ((Display
*) wxGetDisplay(), m_format
);
102 wxString ret
= wxString::FromAscii( t
);
109 void wxDataFormat::SetId( NativeFormat format
)
114 if (m_format
== g_textAtom
)
117 if (m_format
== g_pngAtom
)
118 m_type
= wxDF_BITMAP
;
120 if (m_format
== g_fileAtom
)
121 m_type
= wxDF_FILENAME
;
123 m_type
= wxDF_PRIVATE
;
126 void wxDataFormat::SetId( const wxString
& id
)
130 m_type
= wxDF_PRIVATE
;
131 m_format
= XInternAtom( (Display
*) wxGetDisplay(), id
.ToAscii(), FALSE
);
135 void wxDataFormat::PrepareFormats()
139 g_textAtom
= XInternAtom( (Display
*) wxGetDisplay(), "STRING", FALSE
);
141 g_pngAtom
= XInternAtom( (Display
*) wxGetDisplay(), "image/png", FALSE
);
143 g_fileAtom
= XInternAtom( (Display
*) wxGetDisplay(), "text/uri-list", FALSE
);
147 //-------------------------------------------------------------------------
149 //-------------------------------------------------------------------------
151 wxDataObject::wxDataObject()
155 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
157 size_t nFormatCount
= GetFormatCount(dir
);
158 if ( nFormatCount
== 1 )
160 return format
== GetPreferredFormat();
164 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
165 GetAllFormats(formats
,dir
);
168 for ( n
= 0; n
< nFormatCount
; n
++ )
170 if ( formats
[n
] == format
)
177 return n
< nFormatCount
;
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 bool wxFileDataObject::GetDataHere(void *buf
) const
189 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
191 filenames
+= m_filenames
[i
];
192 filenames
+= (wxChar
) 0;
195 memcpy( buf
, filenames
.mbc_str(), filenames
.length() + 1 );
200 size_t wxFileDataObject::GetDataSize() const
204 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
206 res
+= m_filenames
[i
].length();
213 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
217 // filenames are stores as a string with #0 as deliminators
218 const char *filenames
= (const char*) buf
;
222 if (filenames
[0] == 0)
226 wxString
file( filenames
); // this returns the first file
228 pos
+= file
.length()+1;
229 filenames
+= file
.length()+1;
234 // the text/uri-list format is a sequence of URIs (filenames prefixed by
235 // "file:" as far as I see) delimited by "\r\n" of total length size
236 // (I wonder what happens if the file has '\n' in its filename??)
238 for ( const char *p
= (const char *)buf
; ; p
++ )
240 // some broken programs (testdnd GTK+ sample!) omit the trailing
241 // "\r\n", so check for '\0' explicitly here instead of doing it in
242 // the loop statement to account for it
243 if ( (*p
== '\r' && *(p
+1) == '\n') || !*p
)
245 size_t lenPrefix
= 5; // strlen("file:")
246 if ( filename
.Left(lenPrefix
).MakeLower() == wxT("file:") )
248 // sometimes the syntax is "file:filename", sometimes it's
249 // URL-like: "file://filename" - deal with both
250 if ( filename
[lenPrefix
] == wxT('/') &&
251 filename
[lenPrefix
+ 1] == wxT('/') )
257 AddFile(filename
.c_str() + lenPrefix
);
262 wxLogDebug(wxT("Unsupported URI '%s' in wxFileDataObject"),
282 void wxFileDataObject::AddFile( const wxString
&filename
)
284 m_filenames
.Add( filename
);
287 // ----------------------------------------------------------------------------
288 // wxBitmapDataObject
289 // ----------------------------------------------------------------------------
291 wxBitmapDataObject::wxBitmapDataObject()
296 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
297 : wxBitmapDataObjectBase(bitmap
)
304 wxBitmapDataObject::~wxBitmapDataObject()
309 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
313 wxBitmapDataObjectBase::SetBitmap(bitmap
);
318 bool wxBitmapDataObject::GetDataHere(void *buf
) const
322 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
327 memcpy(buf
, m_pngData
, m_pngSize
);
332 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
338 m_pngData
= malloc(m_pngSize
);
340 memcpy( m_pngData
, buf
, m_pngSize
);
342 wxMemoryInputStream
mstream( (char*) m_pngData
, m_pngSize
);
344 wxPNGHandler handler
;
345 if ( !handler
.LoadFile( &image
, mstream
) )
352 return m_bitmap
.Ok();
358 void wxBitmapDataObject::DoConvertToPng()
364 wxImage image
= m_bitmap
.ConvertToImage();
365 wxPNGHandler handler
;
367 wxCountingOutputStream count
;
368 handler
.SaveFile( &image
, count
);
370 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
371 m_pngData
= malloc(m_pngSize
);
373 wxMemoryOutputStream
mstream( (char*) m_pngData
, m_pngSize
);
374 handler
.SaveFile( &image
, mstream
);
378 #endif // wxUSE_DATAOBJ