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 wxChar
*id
)
66 wxDataFormat::wxDataFormat( const wxString
&id
)
72 wxDataFormat::wxDataFormat( NativeFormat format
)
78 void wxDataFormat::SetType( wxDataFormatId type
)
83 if (m_type
== wxDF_TEXT
)
84 m_format
= g_textAtom
;
86 if (m_type
== wxDF_BITMAP
)
89 if (m_type
== wxDF_FILENAME
)
90 m_format
= g_fileAtom
;
93 wxFAIL_MSG( wxT("invalid dataformat") );
97 wxDataFormatId
wxDataFormat::GetType() const
102 wxString
wxDataFormat::GetId() const
105 return wxEmptyString
;
107 char *t
= XGetAtomName ((Display
*) wxGetDisplay(), m_format
);
108 wxString ret
= wxString::FromAscii( t
);
115 void wxDataFormat::SetId( NativeFormat format
)
120 if (m_format
== g_textAtom
)
123 if (m_format
== g_pngAtom
)
124 m_type
= wxDF_BITMAP
;
126 if (m_format
== g_fileAtom
)
127 m_type
= wxDF_FILENAME
;
129 m_type
= wxDF_PRIVATE
;
132 void wxDataFormat::SetId( const wxChar
*id
)
136 m_type
= wxDF_PRIVATE
;
138 m_format
= XInternAtom( (Display
*) wxGetDisplay(), tmp
.ToAscii(), FALSE
);
142 void wxDataFormat::PrepareFormats()
146 g_textAtom
= XInternAtom( (Display
*) wxGetDisplay(), "STRING", FALSE
);
148 g_pngAtom
= XInternAtom( (Display
*) wxGetDisplay(), "image/png", FALSE
);
150 g_fileAtom
= XInternAtom( (Display
*) wxGetDisplay(), "text/uri-list", FALSE
);
154 //-------------------------------------------------------------------------
156 //-------------------------------------------------------------------------
158 wxDataObject::wxDataObject()
162 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
164 size_t nFormatCount
= GetFormatCount(dir
);
165 if ( nFormatCount
== 1 )
167 return format
== GetPreferredFormat();
171 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
172 GetAllFormats(formats
,dir
);
175 for ( n
= 0; n
< nFormatCount
; n
++ )
177 if ( formats
[n
] == format
)
184 return n
< nFormatCount
;
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 bool wxFileDataObject::GetDataHere(void *buf
) const
196 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
198 filenames
+= m_filenames
[i
];
199 filenames
+= (wxChar
) 0;
202 memcpy( buf
, filenames
.mbc_str(), filenames
.length() + 1 );
207 size_t wxFileDataObject::GetDataSize() const
211 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
213 res
+= m_filenames
[i
].length();
220 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
224 // filenames are stores as a string with #0 as deliminators
225 const char *filenames
= (const char*) buf
;
229 if (filenames
[0] == 0)
233 wxString
file( filenames
); // this returns the first file
235 pos
+= file
.length()+1;
236 filenames
+= file
.length()+1;
241 // the text/uri-list format is a sequence of URIs (filenames prefixed by
242 // "file:" as far as I see) delimited by "\r\n" of total length size
243 // (I wonder what happens if the file has '\n' in its filename??)
245 for ( const char *p
= (const char *)buf
; ; p
++ )
247 // some broken programs (testdnd GTK+ sample!) omit the trailing
248 // "\r\n", so check for '\0' explicitly here instead of doing it in
249 // the loop statement to account for it
250 if ( (*p
== '\r' && *(p
+1) == '\n') || !*p
)
252 size_t lenPrefix
= 5; // strlen("file:")
253 if ( filename
.Left(lenPrefix
).MakeLower() == _T("file:") )
255 // sometimes the syntax is "file:filename", sometimes it's
256 // URL-like: "file://filename" - deal with both
257 if ( filename
[lenPrefix
] == _T('/') &&
258 filename
[lenPrefix
+ 1] == _T('/') )
264 AddFile(filename
.c_str() + lenPrefix
);
269 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
289 void wxFileDataObject::AddFile( const wxString
&filename
)
291 m_filenames
.Add( filename
);
294 // ----------------------------------------------------------------------------
295 // wxBitmapDataObject
296 // ----------------------------------------------------------------------------
298 wxBitmapDataObject::wxBitmapDataObject()
303 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
304 : wxBitmapDataObjectBase(bitmap
)
311 wxBitmapDataObject::~wxBitmapDataObject()
316 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
320 wxBitmapDataObjectBase::SetBitmap(bitmap
);
325 bool wxBitmapDataObject::GetDataHere(void *buf
) const
329 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
334 memcpy(buf
, m_pngData
, m_pngSize
);
339 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
345 m_pngData
= malloc(m_pngSize
);
347 memcpy( m_pngData
, buf
, m_pngSize
);
349 wxMemoryInputStream
mstream( (char*) m_pngData
, m_pngSize
);
351 wxPNGHandler handler
;
352 if ( !handler
.LoadFile( &image
, mstream
) )
359 return m_bitmap
.Ok();
365 void wxBitmapDataObject::DoConvertToPng()
371 wxImage image
= m_bitmap
.ConvertToImage();
372 wxPNGHandler handler
;
374 wxCountingOutputStream count
;
375 handler
.SaveFile( &image
, count
);
377 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
378 m_pngData
= malloc(m_pngSize
);
380 wxMemoryOutputStream
mstream( (char*) m_pngData
, m_pngSize
);
381 handler
.SaveFile( &image
, mstream
);
385 #endif // wxUSE_DATAOBJ