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
)
77 if (m_type
== wxDF_TEXT
)
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
98 wxString ret
= wxString::FromAscii( gdk_atom_name( m_format
) );
102 void wxDataFormat::SetId( NativeFormat format
)
107 if (m_format
== g_textAtom
)
110 if (m_format
== g_pngAtom
)
111 m_type
= wxDF_BITMAP
;
113 if (m_format
== g_fileAtom
)
114 m_type
= wxDF_FILENAME
;
116 m_type
= wxDF_PRIVATE
;
119 void wxDataFormat::SetId( const wxChar
*id
)
122 m_type
= wxDF_PRIVATE
;
124 m_format
= gdk_atom_intern( (const char*) tmp
.ToAscii(), FALSE
);
127 void wxDataFormat::PrepareFormats()
129 // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the
130 // atoms STRING and file:ALL as the old code was, but normal X apps
131 // use STRING for text selection when transfering the data via
132 // clipboard, for example, so do use STRING for now (GNOME apps will
133 // probably support STRING as well for compatibility anyhow), but use
134 // text/uri-list for file dnd because compatibility is not important
138 g_textAtom
= gdk_atom_intern( "text/utf8", FALSE
);
140 g_textAtom
= gdk_atom_intern( "STRING" /* "text/plain" */, FALSE
);
143 g_pngAtom
= gdk_atom_intern( "image/png", FALSE
);
145 g_fileAtom
= gdk_atom_intern( "text/uri-list", FALSE
);
148 //-------------------------------------------------------------------------
150 //-------------------------------------------------------------------------
152 wxDataObject::wxDataObject()
156 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
158 size_t nFormatCount
= GetFormatCount(dir
);
159 if ( nFormatCount
== 1 )
161 return format
== GetPreferredFormat();
165 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
166 GetAllFormats(formats
,dir
);
169 for ( n
= 0; n
< nFormatCount
; n
++ )
171 if ( formats
[n
] == format
)
178 return n
< nFormatCount
;
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 bool wxFileDataObject::GetDataHere(void *buf
) const
190 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
192 filenames
+= m_filenames
[i
];
193 filenames
+= (wxChar
) 0;
196 memcpy( buf
, filenames
.mbc_str(), filenames
.Len() + 1 );
201 size_t wxFileDataObject::GetDataSize() const
205 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
207 res
+= m_filenames
[i
].Len();
214 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
218 // filenames are stores as a string with #0 as deliminators
219 const char *filenames
= (const char*) buf
;
223 if (filenames
[0] == 0)
227 wxString
file( filenames
); // this returns the first file
230 filenames
+= file
.Len()+1;
235 // the text/uri-list format is a sequence of URIs (filenames prefixed by
236 // "file:" as far as I see) delimited by "\r\n" of total length size
237 // (I wonder what happens if the file has '\n' in its filename??)
239 for ( const char *p
= (const char *)buf
; ; p
++ )
241 // some broken programs (testdnd GTK+ sample!) omit the trailing
242 // "\r\n", so check for '\0' explicitly here instead of doing it in
243 // the loop statement to account for it
244 if ( (*p
== '\r' && *(p
+1) == '\n') || !*p
)
246 size_t lenPrefix
= 5; // strlen("file:")
247 if ( filename
.Left(lenPrefix
).MakeLower() == _T("file:") )
249 // sometimes the syntax is "file:filename", sometimes it's
250 // URL-like: "file://filename" - deal with both
251 if ( filename
[lenPrefix
] == _T('/') &&
252 filename
[lenPrefix
+ 1] == _T('/') )
258 AddFile(filename
.c_str() + lenPrefix
);
263 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
283 void wxFileDataObject::AddFile( const wxString
&filename
)
285 m_filenames
.Add( filename
);
288 // ----------------------------------------------------------------------------
289 // wxBitmapDataObject
290 // ----------------------------------------------------------------------------
292 wxBitmapDataObject::wxBitmapDataObject()
297 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
298 : wxBitmapDataObjectBase(bitmap
)
305 wxBitmapDataObject::~wxBitmapDataObject()
310 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
314 wxBitmapDataObjectBase::SetBitmap(bitmap
);
319 bool wxBitmapDataObject::GetDataHere(void *buf
) const
323 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
328 memcpy(buf
, m_pngData
, m_pngSize
);
333 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
337 wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG
) != NULL
,
338 FALSE
, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
341 m_pngData
= malloc(m_pngSize
);
343 memcpy(m_pngData
, buf
, m_pngSize
);
345 wxMemoryInputStream
mstream((char*) m_pngData
, m_pngSize
);
347 if ( !image
.LoadFile( mstream
, wxBITMAP_TYPE_PNG
) )
352 m_bitmap
= wxBitmap(image
);
354 return m_bitmap
.Ok();
357 void wxBitmapDataObject::DoConvertToPng()
359 if ( !m_bitmap
.Ok() )
362 wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG
) != NULL
,
363 wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
365 wxImage image
= m_bitmap
.ConvertToImage();
367 wxCountingOutputStream count
;
368 image
.SaveFile(count
, wxBITMAP_TYPE_PNG
);
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 image
.SaveFile(mstream
, wxBITMAP_TYPE_PNG
);