1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataObject class
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "dataobj.h"
14 #include "wx/dataobj.h"
17 #include "wx/mstream.h"
22 //-------------------------------------------------------------------------
24 //-------------------------------------------------------------------------
26 GdkAtom g_textAtom
= 0;
27 GdkAtom g_pngAtom
= 0;
28 GdkAtom g_fileAtom
= 0;
30 //-------------------------------------------------------------------------
32 //-------------------------------------------------------------------------
34 wxDataFormat::wxDataFormat()
36 // do *not* call PrepareFormats() from here for 2 reasons:
38 // 1. we will have time to do it later because some other Set function
39 // must be called before we really need them
41 // 2. doing so prevents us from declaring global wxDataFormats because
42 // calling PrepareFormats (and thus gdk_atom_intern) before GDK is
43 // initialised will result in a crash
44 m_type
= wxDF_INVALID
;
45 m_format
= (GdkAtom
) 0;
48 wxDataFormat::wxDataFormat( wxDataFormatId type
)
54 wxDataFormat::wxDataFormat( const wxChar
*id
)
60 wxDataFormat::wxDataFormat( const wxString
&id
)
66 wxDataFormat::wxDataFormat( NativeFormat format
)
72 void wxDataFormat::SetType( wxDataFormatId type
)
76 if (type
== wxDF_UNICODETEXT
)
81 if (m_type
== wxDF_TEXT
)
82 m_format
= g_textAtom
;
84 if (m_type
== wxDF_BITMAP
)
87 if (m_type
== wxDF_FILENAME
)
88 m_format
= g_fileAtom
;
91 wxFAIL_MSG( wxT("invalid dataformat") );
95 wxDataFormatId
wxDataFormat::GetType() const
100 wxString
wxDataFormat::GetId() const
102 wxString ret
= wxString::FromAscii( gdk_atom_name( m_format
) );
106 void wxDataFormat::SetId( NativeFormat format
)
111 if (m_format
== g_textAtom
)
114 if (m_format
== g_pngAtom
)
115 m_type
= wxDF_BITMAP
;
117 if (m_format
== g_fileAtom
)
118 m_type
= wxDF_FILENAME
;
120 m_type
= wxDF_PRIVATE
;
123 void wxDataFormat::SetId( const wxChar
*id
)
126 m_type
= wxDF_PRIVATE
;
128 m_format
= gdk_atom_intern( (const char*) tmp
.ToAscii(), FALSE
);
131 void wxDataFormat::PrepareFormats()
133 // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the
134 // atoms STRING and file:ALL as the old code was, but normal X apps
135 // use STRING for text selection when transfering the data via
136 // clipboard, for example, so do use STRING for now (GNOME apps will
137 // probably support STRING as well for compatibility anyhow), but use
138 // text/uri-list for file dnd because compatibility is not important
142 g_textAtom
= gdk_atom_intern( "UTF8_STRING", FALSE
);
144 g_textAtom
= gdk_atom_intern( "STRING" /* "text/plain" */, FALSE
);
147 g_pngAtom
= gdk_atom_intern( "image/png", FALSE
);
149 g_fileAtom
= gdk_atom_intern( "text/uri-list", FALSE
);
152 //-------------------------------------------------------------------------
154 //-------------------------------------------------------------------------
156 wxDataObject::wxDataObject()
160 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
162 size_t nFormatCount
= GetFormatCount(dir
);
163 if ( nFormatCount
== 1 )
165 return format
== GetPreferredFormat();
169 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
170 GetAllFormats(formats
,dir
);
173 for ( n
= 0; n
< nFormatCount
; n
++ )
175 if ( formats
[n
] == format
)
182 return n
< nFormatCount
;
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 bool wxFileDataObject::GetDataHere(void *buf
) const
194 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
196 filenames
+= m_filenames
[i
];
197 filenames
+= (wxChar
) 0;
200 memcpy( buf
, filenames
.mbc_str(), filenames
.Len() + 1 );
205 size_t wxFileDataObject::GetDataSize() const
209 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
211 res
+= m_filenames
[i
].Len();
218 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
222 // filenames are stores as a string with #0 as deliminators
223 const char *filenames
= (const char*) buf
;
227 if (filenames
[0] == 0)
231 wxString
file( filenames
); // this returns the first file
234 filenames
+= file
.Len()+1;
239 // the text/uri-list format is a sequence of URIs (filenames prefixed by
240 // "file:" as far as I see) delimited by "\r\n" of total length size
241 // (I wonder what happens if the file has '\n' in its filename??)
243 for ( const char *p
= (const char *)buf
; ; p
++ )
245 // some broken programs (testdnd GTK+ sample!) omit the trailing
246 // "\r\n", so check for '\0' explicitly here instead of doing it in
247 // the loop statement to account for it
248 if ( (*p
== '\r' && *(p
+1) == '\n') || !*p
)
250 size_t lenPrefix
= 5; // strlen("file:")
251 if ( filename
.Left(lenPrefix
).MakeLower() == _T("file:") )
253 // sometimes the syntax is "file:filename", sometimes it's
254 // URL-like: "file://filename" - deal with both
255 if ( filename
[lenPrefix
] == _T('/') &&
256 filename
[lenPrefix
+ 1] == _T('/') )
262 AddFile(filename
.c_str() + lenPrefix
);
267 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
287 void wxFileDataObject::AddFile( const wxString
&filename
)
289 m_filenames
.Add( filename
);
292 // ----------------------------------------------------------------------------
293 // wxBitmapDataObject
294 // ----------------------------------------------------------------------------
296 wxBitmapDataObject::wxBitmapDataObject()
301 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
302 : wxBitmapDataObjectBase(bitmap
)
309 wxBitmapDataObject::~wxBitmapDataObject()
314 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
318 wxBitmapDataObjectBase::SetBitmap(bitmap
);
323 bool wxBitmapDataObject::GetDataHere(void *buf
) const
327 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
332 memcpy(buf
, m_pngData
, m_pngSize
);
337 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
341 wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG
) != NULL
,
342 FALSE
, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
345 m_pngData
= malloc(m_pngSize
);
347 memcpy(m_pngData
, buf
, m_pngSize
);
349 wxMemoryInputStream
mstream((char*) m_pngData
, m_pngSize
);
351 if ( !image
.LoadFile( mstream
, wxBITMAP_TYPE_PNG
) )
356 m_bitmap
= wxBitmap(image
);
358 return m_bitmap
.Ok();
361 void wxBitmapDataObject::DoConvertToPng()
363 if ( !m_bitmap
.Ok() )
366 wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG
) != NULL
,
367 wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
369 wxImage image
= m_bitmap
.ConvertToImage();
371 wxCountingOutputStream count
;
372 image
.SaveFile(count
, wxBITMAP_TYPE_PNG
);
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 image
.SaveFile(mstream
, wxBITMAP_TYPE_PNG
);