1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/dataobj.cpp
3 // Purpose: wxDataObject class
4 // Author: Julian Smart
5 // Copyright: (c) 1998 Julian Smart
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
9 // for compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
14 #include "wx/dataobj.h"
23 #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 wxString
&id
)
65 wxDataFormat::wxDataFormat( NativeFormat format
)
71 void wxDataFormat::SetType( wxDataFormatId type
)
76 if (m_type
== wxDF_TEXT
|| m_type
== wxDF_UNICODETEXT
)
77 m_format
= g_textAtom
;
79 if (m_type
== wxDF_BITMAP
)
82 if (m_type
== wxDF_FILENAME
)
83 m_format
= g_fileAtom
;
86 wxFAIL_MSG( wxT("invalid dataformat") );
90 wxDataFormatId
wxDataFormat::GetType() const
95 wxString
wxDataFormat::GetId() const
100 char *t
= XGetAtomName ((Display
*) wxGetDisplay(), m_format
);
101 wxString ret
= wxString::FromAscii( t
);
108 void wxDataFormat::SetId( NativeFormat format
)
113 if (m_format
== g_textAtom
)
116 if (m_format
== g_pngAtom
)
117 m_type
= wxDF_BITMAP
;
119 if (m_format
== g_fileAtom
)
120 m_type
= wxDF_FILENAME
;
122 m_type
= wxDF_PRIVATE
;
125 void wxDataFormat::SetId( const wxString
& id
)
129 m_type
= wxDF_PRIVATE
;
130 m_format
= XInternAtom( (Display
*) wxGetDisplay(), id
.ToAscii(), FALSE
);
134 void wxDataFormat::PrepareFormats()
138 g_textAtom
= XInternAtom( (Display
*) wxGetDisplay(), "STRING", FALSE
);
140 g_pngAtom
= XInternAtom( (Display
*) wxGetDisplay(), "image/png", FALSE
);
142 g_fileAtom
= XInternAtom( (Display
*) wxGetDisplay(), "text/uri-list", FALSE
);
146 //-------------------------------------------------------------------------
148 //-------------------------------------------------------------------------
150 wxDataObject::wxDataObject()
154 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
156 size_t nFormatCount
= GetFormatCount(dir
);
157 if ( nFormatCount
== 1 )
159 return format
== GetPreferredFormat();
163 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
164 GetAllFormats(formats
,dir
);
167 for ( n
= 0; n
< nFormatCount
; n
++ )
169 if ( formats
[n
] == format
)
176 return n
< nFormatCount
;
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 bool wxFileDataObject::GetDataHere(void *buf
) const
188 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
190 filenames
+= m_filenames
[i
];
191 filenames
+= (wxChar
) 0;
194 memcpy( buf
, filenames
.mbc_str(), filenames
.length() + 1 );
199 size_t wxFileDataObject::GetDataSize() const
203 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
205 res
+= m_filenames
[i
].length();
212 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
216 // filenames are stores as a string with #0 as deliminators
217 const char *filenames
= (const char*) buf
;
221 if (filenames
[0] == 0)
225 wxString
file( filenames
); // this returns the first file
227 pos
+= file
.length()+1;
228 filenames
+= file
.length()+1;
233 // the text/uri-list format is a sequence of URIs (filenames prefixed by
234 // "file:" as far as I see) delimited by "\r\n" of total length size
235 // (I wonder what happens if the file has '\n' in its filename??)
237 for ( const char *p
= (const char *)buf
; ; p
++ )
239 // some broken programs (testdnd GTK+ sample!) omit the trailing
240 // "\r\n", so check for '\0' explicitly here instead of doing it in
241 // the loop statement to account for it
242 if ( (*p
== '\r' && *(p
+1) == '\n') || !*p
)
244 size_t lenPrefix
= 5; // strlen("file:")
245 if ( filename
.Left(lenPrefix
).MakeLower() == wxT("file:") )
247 // sometimes the syntax is "file:filename", sometimes it's
248 // URL-like: "file://filename" - deal with both
249 if ( filename
[lenPrefix
] == wxT('/') &&
250 filename
[lenPrefix
+ 1] == wxT('/') )
256 AddFile(filename
.c_str() + lenPrefix
);
261 wxLogDebug(wxT("Unsupported URI '%s' in wxFileDataObject"),
281 void wxFileDataObject::AddFile( const wxString
&filename
)
283 m_filenames
.Add( filename
);
286 // ----------------------------------------------------------------------------
287 // wxBitmapDataObject
288 // ----------------------------------------------------------------------------
290 wxBitmapDataObject::wxBitmapDataObject()
295 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
296 : wxBitmapDataObjectBase(bitmap
)
303 wxBitmapDataObject::~wxBitmapDataObject()
308 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
312 wxBitmapDataObjectBase::SetBitmap(bitmap
);
317 bool wxBitmapDataObject::GetDataHere(void *buf
) const
321 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
326 memcpy(buf
, m_pngData
, m_pngSize
);
331 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
337 m_pngData
= malloc(m_pngSize
);
339 memcpy( m_pngData
, buf
, m_pngSize
);
341 wxMemoryInputStream
mstream( (char*) m_pngData
, m_pngSize
);
343 wxPNGHandler handler
;
344 if ( !handler
.LoadFile( &image
, mstream
) )
351 return m_bitmap
.IsOk();
357 void wxBitmapDataObject::DoConvertToPng()
360 if (!m_bitmap
.IsOk())
363 wxImage image
= m_bitmap
.ConvertToImage();
364 wxPNGHandler handler
;
366 wxCountingOutputStream count
;
367 handler
.SaveFile( &image
, count
);
369 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
370 m_pngData
= malloc(m_pngSize
);
372 wxMemoryOutputStream
mstream( (char*) m_pngData
, m_pngSize
);
373 handler
.SaveFile( &image
, mstream
);
377 #endif // wxUSE_DATAOBJ