1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataObject class
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "dataobj.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
17 #include "wx/dataobj.h"
20 #include "wx/mstream.h"
27 //-------------------------------------------------------------------------
29 //-------------------------------------------------------------------------
31 GdkAtom g_textAtom
= 0;
32 GdkAtom g_altTextAtom
= 0;
33 GdkAtom g_pngAtom
= 0;
34 GdkAtom g_fileAtom
= 0;
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
;
51 m_format
= (GdkAtom
) 0;
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
)
85 if (m_type
== wxDF_UNICODETEXT
)
86 m_format
= g_textAtom
;
87 else if (m_type
== wxDF_TEXT
)
88 m_format
= g_altTextAtom
;
90 if (m_type
== wxDF_TEXT
|| m_type
== wxDF_UNICODETEXT
)
91 m_format
= g_textAtom
;
94 if (m_type
== wxDF_BITMAP
)
97 if (m_type
== wxDF_FILENAME
)
98 m_format
= g_fileAtom
;
101 wxFAIL_MSG( wxT("invalid dataformat") );
105 wxDataFormatId
wxDataFormat::GetType() const
110 wxString
wxDataFormat::GetId() const
112 wxString ret
= wxString::FromAscii( gdk_atom_name( m_format
) );
116 void wxDataFormat::SetId( NativeFormat format
)
121 if (m_format
== g_textAtom
)
123 m_type
= wxDF_UNICODETEXT
;
128 if (m_format
== g_altTextAtom
)
131 if (m_format
== g_pngAtom
)
132 m_type
= wxDF_BITMAP
;
134 if (m_format
== g_fileAtom
)
135 m_type
= wxDF_FILENAME
;
137 m_type
= wxDF_PRIVATE
;
140 void wxDataFormat::SetId( const wxChar
*id
)
143 m_type
= wxDF_PRIVATE
;
145 m_format
= gdk_atom_intern( (const char*) tmp
.ToAscii(), FALSE
);
148 void wxDataFormat::PrepareFormats()
150 // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the
151 // atoms STRING and file:ALL as the old code was, but normal X apps
152 // use STRING for text selection when transfering the data via
153 // clipboard, for example, so do use STRING for now (GNOME apps will
154 // probably support STRING as well for compatibility anyhow), but use
155 // text/uri-list for file dnd because compatibility is not important
159 g_textAtom
= gdk_atom_intern( "UTF8_STRING", FALSE
);
160 g_altTextAtom
= gdk_atom_intern( "STRING", FALSE
);
162 g_textAtom
= gdk_atom_intern( "STRING" /* "text/plain" */, FALSE
);
165 g_pngAtom
= gdk_atom_intern( "image/png", FALSE
);
167 g_fileAtom
= gdk_atom_intern( "text/uri-list", FALSE
);
170 //-------------------------------------------------------------------------
172 //-------------------------------------------------------------------------
174 wxDataObject::wxDataObject()
178 wxDataObject::~wxDataObject()
180 // dtor is empty but needed for Darwin and AIX -- otherwise it doesn't link
183 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
185 size_t nFormatCount
= GetFormatCount(dir
);
186 if ( nFormatCount
== 1 )
188 return format
== GetPreferredFormat();
192 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
193 GetAllFormats(formats
,dir
);
196 for ( n
= 0; n
< nFormatCount
; n
++ )
198 if ( formats
[n
] == format
)
205 return n
< nFormatCount
;
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
213 #if defined(__WXGTK20__) && wxUSE_UNICODE
214 void wxTextDataObject::GetAllFormats(wxDataFormat
*formats
, wxDataObjectBase::Direction dir
) const
216 *formats
++ = GetPreferredFormat();
217 *formats
= g_altTextAtom
;
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
225 bool wxFileDataObject::GetDataHere(void *buf
) const
229 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
231 filenames
+= wxT("file:");
232 filenames
+= m_filenames
[i
];
233 filenames
+= wxT("\r\n");
236 memcpy( buf
, filenames
.mbc_str(), filenames
.Len() + 1 );
241 size_t wxFileDataObject::GetDataSize() const
245 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
247 // This is junk in UTF-8
248 res
+= m_filenames
[i
].Len();
249 res
+= 5 + 2; // "file:" (5) + "\r\n" (2)
255 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
259 // we get data in the text/uri-list format, i.e. as a sequence of URIs
260 // (filenames prefixed by "file:") delimited by "\r\n"
262 for ( const char *p
= (const char *)buf
; ; p
++ )
264 // some broken programs (testdnd GTK+ sample!) omit the trailing
265 // "\r\n", so check for '\0' explicitly here instead of doing it in
266 // the loop statement to account for it
267 if ( (*p
== '\r' && *(p
+1) == '\n') || !*p
)
269 size_t lenPrefix
= 5; // strlen("file:")
270 if ( filename
.Left(lenPrefix
).MakeLower() == _T("file:") )
272 // sometimes the syntax is "file:filename", sometimes it's
273 // URL-like: "file://filename" - deal with both
274 if ( filename
[lenPrefix
] == _T('/') &&
275 filename
[lenPrefix
+ 1] == _T('/') )
281 AddFile(wxURI::Unescape(filename
.c_str() + lenPrefix
));
286 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
305 void wxFileDataObject::AddFile( const wxString
&filename
)
307 m_filenames
.Add( filename
);
310 // ----------------------------------------------------------------------------
311 // wxBitmapDataObject
312 // ----------------------------------------------------------------------------
314 wxBitmapDataObject::wxBitmapDataObject()
319 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
320 : wxBitmapDataObjectBase(bitmap
)
327 wxBitmapDataObject::~wxBitmapDataObject()
332 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
336 wxBitmapDataObjectBase::SetBitmap(bitmap
);
341 bool wxBitmapDataObject::GetDataHere(void *buf
) const
345 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
350 memcpy(buf
, m_pngData
, m_pngSize
);
355 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
359 wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG
) != NULL
,
360 FALSE
, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
363 m_pngData
= malloc(m_pngSize
);
365 memcpy(m_pngData
, buf
, m_pngSize
);
367 wxMemoryInputStream
mstream((char*) m_pngData
, m_pngSize
);
369 if ( !image
.LoadFile( mstream
, wxBITMAP_TYPE_PNG
) )
374 m_bitmap
= wxBitmap(image
);
376 return m_bitmap
.Ok();
379 void wxBitmapDataObject::DoConvertToPng()
381 if ( !m_bitmap
.Ok() )
384 wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG
) != NULL
,
385 wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
387 wxImage image
= m_bitmap
.ConvertToImage();
389 wxCountingOutputStream count
;
390 image
.SaveFile(count
, wxBITMAP_TYPE_PNG
);
392 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
393 m_pngData
= malloc(m_pngSize
);
395 wxMemoryOutputStream
mstream((char*) m_pngData
, m_pngSize
);
396 image
.SaveFile(mstream
, wxBITMAP_TYPE_PNG
);