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('/') )
283 wxURL::ConvertFromURI
285 (filename
.c_str() + lenPrefix
));
290 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
309 void wxFileDataObject::AddFile( const wxString
&filename
)
311 m_filenames
.Add( filename
);
314 // ----------------------------------------------------------------------------
315 // wxBitmapDataObject
316 // ----------------------------------------------------------------------------
318 wxBitmapDataObject::wxBitmapDataObject()
323 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
324 : wxBitmapDataObjectBase(bitmap
)
331 wxBitmapDataObject::~wxBitmapDataObject()
336 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
340 wxBitmapDataObjectBase::SetBitmap(bitmap
);
345 bool wxBitmapDataObject::GetDataHere(void *buf
) const
349 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
354 memcpy(buf
, m_pngData
, m_pngSize
);
359 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
363 wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG
) != NULL
,
364 FALSE
, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
367 m_pngData
= malloc(m_pngSize
);
369 memcpy(m_pngData
, buf
, m_pngSize
);
371 wxMemoryInputStream
mstream((char*) m_pngData
, m_pngSize
);
373 if ( !image
.LoadFile( mstream
, wxBITMAP_TYPE_PNG
) )
378 m_bitmap
= wxBitmap(image
);
380 return m_bitmap
.Ok();
383 void wxBitmapDataObject::DoConvertToPng()
385 if ( !m_bitmap
.Ok() )
388 wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG
) != NULL
,
389 wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
391 wxImage image
= m_bitmap
.ConvertToImage();
393 wxCountingOutputStream count
;
394 image
.SaveFile(count
, wxBITMAP_TYPE_PNG
);
396 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
397 m_pngData
= malloc(m_pngSize
);
399 wxMemoryOutputStream
mstream((char*) m_pngData
, m_pngSize
);
400 image
.SaveFile(mstream
, wxBITMAP_TYPE_PNG
);