1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataObject class
4 // Author: Julian Smart
6 // Copyright: (c) 1998 Julian Smart
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "dataobj.h"
16 #include "wx/dataobj.h"
17 #include "wx/mstream.h"
23 #include "wx/x11/private.h"
25 //-------------------------------------------------------------------------
27 //-------------------------------------------------------------------------
33 //-------------------------------------------------------------------------
35 //-------------------------------------------------------------------------
37 wxDataFormat::wxDataFormat()
39 // do *not* call PrepareFormats() from here for 2 reasons:
41 // 1. we will have time to do it later because some other Set function
42 // must be called before we really need them
44 // 2. doing so prevents us from declaring global wxDataFormats because
45 // calling PrepareFormats (and thus gdk_atom_intern) before GDK is
46 // initialised will result in a crash
47 m_type
= wxDF_INVALID
;
51 wxDataFormat::wxDataFormat( wxDataFormatId type
)
57 wxDataFormat::wxDataFormat( const wxChar
*id
)
63 wxDataFormat::wxDataFormat( const wxString
&id
)
69 wxDataFormat::wxDataFormat( NativeFormat format
)
75 void wxDataFormat::SetType( wxDataFormatId type
)
80 if (m_type
== wxDF_TEXT
)
81 m_format
= g_textAtom
;
83 if (m_type
== wxDF_BITMAP
)
86 if (m_type
== wxDF_FILENAME
)
87 m_format
= g_fileAtom
;
90 wxFAIL_MSG( wxT("invalid dataformat") );
94 wxDataFormatId
wxDataFormat::GetType() const
99 wxString
wxDataFormat::GetId() const
102 return wxEmptyString
;
104 char *t
= XGetAtomName ((Display
*) wxGetDisplay(), m_format
);
105 wxString ret
= wxString::FromAscii( t
);
112 void wxDataFormat::SetId( NativeFormat format
)
117 if (m_format
== g_textAtom
)
120 if (m_format
== g_pngAtom
)
121 m_type
= wxDF_BITMAP
;
123 if (m_format
== g_fileAtom
)
124 m_type
= wxDF_FILENAME
;
126 m_type
= wxDF_PRIVATE
;
129 void wxDataFormat::SetId( const wxChar
*id
)
133 m_type
= wxDF_PRIVATE
;
135 m_format
= XInternAtom( (Display
*) wxGetDisplay(), tmp
.ToAscii(), FALSE
);
139 void wxDataFormat::PrepareFormats()
143 g_textAtom
= XInternAtom( (Display
*) wxGetDisplay(), "STRING", FALSE
);
145 g_pngAtom
= XInternAtom( (Display
*) wxGetDisplay(), "image/png", FALSE
);
147 g_fileAtom
= XInternAtom( (Display
*) wxGetDisplay(), "text/uri-list", FALSE
);
151 //-------------------------------------------------------------------------
153 //-------------------------------------------------------------------------
155 wxDataObject::wxDataObject()
159 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
161 size_t nFormatCount
= GetFormatCount(dir
);
162 if ( nFormatCount
== 1 )
164 return format
== GetPreferredFormat();
168 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
169 GetAllFormats(formats
,dir
);
172 for ( n
= 0; n
< nFormatCount
; n
++ )
174 if ( formats
[n
] == format
)
181 return n
< nFormatCount
;
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 bool wxFileDataObject::GetDataHere(void *buf
) const
193 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
195 filenames
+= m_filenames
[i
];
196 filenames
+= (wxChar
) 0;
199 memcpy( buf
, filenames
.mbc_str(), filenames
.Len() + 1 );
204 size_t wxFileDataObject::GetDataSize() const
208 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
210 res
+= m_filenames
[i
].Len();
217 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
221 // filenames are stores as a string with #0 as deliminators
222 const char *filenames
= (const char*) buf
;
226 if (filenames
[0] == 0)
230 wxString
file( filenames
); // this returns the first file
233 filenames
+= file
.Len()+1;
238 // the text/uri-list format is a sequence of URIs (filenames prefixed by
239 // "file:" as far as I see) delimited by "\r\n" of total length size
240 // (I wonder what happens if the file has '\n' in its filename??)
242 for ( const char *p
= (const char *)buf
; ; p
++ )
244 // some broken programs (testdnd GTK+ sample!) omit the trailing
245 // "\r\n", so check for '\0' explicitly here instead of doing it in
246 // the loop statement to account for it
247 if ( (*p
== '\r' && *(p
+1) == '\n') || !*p
)
249 size_t lenPrefix
= 5; // strlen("file:")
250 if ( filename
.Left(lenPrefix
).MakeLower() == _T("file:") )
252 // sometimes the syntax is "file:filename", sometimes it's
253 // URL-like: "file://filename" - deal with both
254 if ( filename
[lenPrefix
] == _T('/') &&
255 filename
[lenPrefix
+ 1] == _T('/') )
261 AddFile(filename
.c_str() + lenPrefix
);
266 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
286 void wxFileDataObject::AddFile( const wxString
&filename
)
288 m_filenames
.Add( filename
);
291 // ----------------------------------------------------------------------------
292 // wxBitmapDataObject
293 // ----------------------------------------------------------------------------
295 wxBitmapDataObject::wxBitmapDataObject()
300 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
301 : wxBitmapDataObjectBase(bitmap
)
308 wxBitmapDataObject::~wxBitmapDataObject()
313 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
317 wxBitmapDataObjectBase::SetBitmap(bitmap
);
322 bool wxBitmapDataObject::GetDataHere(void *buf
) const
326 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
331 memcpy(buf
, m_pngData
, m_pngSize
);
336 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
342 m_pngData
= malloc(m_pngSize
);
344 memcpy( m_pngData
, buf
, m_pngSize
);
346 wxMemoryInputStream
mstream( (char*) m_pngData
, m_pngSize
);
348 wxPNGHandler handler
;
349 if ( !handler
.LoadFile( &image
, mstream
) )
356 return m_bitmap
.Ok();
362 void wxBitmapDataObject::DoConvertToPng()
368 wxImage image
= m_bitmap
.ConvertToImage();
369 wxPNGHandler handler
;
371 wxCountingOutputStream count
;
372 handler
.SaveFile( &image
, count
);
374 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
375 m_pngData
= malloc(m_pngSize
);
377 wxMemoryOutputStream
mstream( (char*) m_pngData
, m_pngSize
);
378 handler
.SaveFile( &image
, mstream
);