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"
23 //-------------------------------------------------------------------------
25 //-------------------------------------------------------------------------
27 GdkAtom g_textAtom
= 0;
28 GdkAtom g_pngAtom
= 0;
29 GdkAtom g_fileAtom
= 0;
31 //-------------------------------------------------------------------------
33 //-------------------------------------------------------------------------
35 wxDataFormat::wxDataFormat()
37 // do *not* call PrepareFormats() from here for 2 reasons:
39 // 1. we will have time to do it later because some other Set function
40 // must be called before we really need them
42 // 2. doing so prevents us from declaring global wxDataFormats because
43 // calling PrepareFormats (and thus gdk_atom_intern) before GDK is
44 // initialised will result in a crash
45 m_type
= wxDF_INVALID
;
46 m_format
= (GdkAtom
) 0;
49 wxDataFormat::wxDataFormat( wxDataFormatId type
)
55 wxDataFormat::wxDataFormat( const wxChar
*id
)
61 wxDataFormat::wxDataFormat( const wxString
&id
)
67 wxDataFormat::wxDataFormat( NativeFormat format
)
73 void wxDataFormat::SetType( wxDataFormatId type
)
77 if (type
== wxDF_UNICODETEXT
)
82 if (m_type
== wxDF_TEXT
)
83 m_format
= g_textAtom
;
85 if (m_type
== wxDF_BITMAP
)
88 if (m_type
== wxDF_FILENAME
)
89 m_format
= g_fileAtom
;
92 wxFAIL_MSG( wxT("invalid dataformat") );
96 wxDataFormatId
wxDataFormat::GetType() const
101 wxString
wxDataFormat::GetId() const
103 wxString ret
= wxString::FromAscii( gdk_atom_name( m_format
) );
107 void wxDataFormat::SetId( NativeFormat format
)
112 if (m_format
== g_textAtom
)
115 if (m_format
== g_pngAtom
)
116 m_type
= wxDF_BITMAP
;
118 if (m_format
== g_fileAtom
)
119 m_type
= wxDF_FILENAME
;
121 m_type
= wxDF_PRIVATE
;
124 void wxDataFormat::SetId( const wxChar
*id
)
127 m_type
= wxDF_PRIVATE
;
129 m_format
= gdk_atom_intern( (const char*) tmp
.ToAscii(), FALSE
);
132 void wxDataFormat::PrepareFormats()
134 // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the
135 // atoms STRING and file:ALL as the old code was, but normal X apps
136 // use STRING for text selection when transfering the data via
137 // clipboard, for example, so do use STRING for now (GNOME apps will
138 // probably support STRING as well for compatibility anyhow), but use
139 // text/uri-list for file dnd because compatibility is not important
143 g_textAtom
= gdk_atom_intern( "UTF8_STRING", FALSE
);
145 g_textAtom
= gdk_atom_intern( "STRING" /* "text/plain" */, FALSE
);
148 g_pngAtom
= gdk_atom_intern( "image/png", FALSE
);
150 g_fileAtom
= gdk_atom_intern( "text/uri-list", FALSE
);
153 //-------------------------------------------------------------------------
155 //-------------------------------------------------------------------------
157 wxDataObject::wxDataObject()
161 wxDataObject::~wxDataObject()
163 // dtor is empty but needed for Darwin and AIX -- otherwise it doesn't link
166 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
168 size_t nFormatCount
= GetFormatCount(dir
);
169 if ( nFormatCount
== 1 )
171 return format
== GetPreferredFormat();
175 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
176 GetAllFormats(formats
,dir
);
179 for ( n
= 0; n
< nFormatCount
; n
++ )
181 if ( formats
[n
] == format
)
188 return n
< nFormatCount
;
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 bool wxFileDataObject::GetDataHere(void *buf
) const
200 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
202 filenames
+= m_filenames
[i
];
203 filenames
+= (wxChar
) 0;
206 memcpy( buf
, filenames
.mbc_str(), filenames
.Len() + 1 );
211 size_t wxFileDataObject::GetDataSize() const
215 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
217 res
+= m_filenames
[i
].Len();
224 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
228 // filenames are stores as a string with #0 as deliminators
229 const char *filenames
= (const char*) buf
;
233 if (filenames
[0] == 0)
237 wxString
file( filenames
); // this returns the first file
240 filenames
+= file
.Len()+1;
245 // the text/uri-list format is a sequence of URIs (filenames prefixed by
246 // "file:" as far as I see) delimited by "\r\n" of total length size
247 // (I wonder what happens if the file has '\n' in its filename??)
249 for ( const char *p
= (const char *)buf
; ; p
++ )
251 // some broken programs (testdnd GTK+ sample!) omit the trailing
252 // "\r\n", so check for '\0' explicitly here instead of doing it in
253 // the loop statement to account for it
254 if ( (*p
== '\r' && *(p
+1) == '\n') || !*p
)
256 size_t lenPrefix
= 5; // strlen("file:")
257 if ( filename
.Left(lenPrefix
).MakeLower() == _T("file:") )
259 // sometimes the syntax is "file:filename", sometimes it's
260 // URL-like: "file://filename" - deal with both
261 if ( filename
[lenPrefix
] == _T('/') &&
262 filename
[lenPrefix
+ 1] == _T('/') )
268 AddFile(filename
.c_str() + lenPrefix
);
273 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
293 void wxFileDataObject::AddFile( const wxString
&filename
)
295 m_filenames
.Add( filename
);
298 // ----------------------------------------------------------------------------
299 // wxBitmapDataObject
300 // ----------------------------------------------------------------------------
302 wxBitmapDataObject::wxBitmapDataObject()
307 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
308 : wxBitmapDataObjectBase(bitmap
)
315 wxBitmapDataObject::~wxBitmapDataObject()
320 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
324 wxBitmapDataObjectBase::SetBitmap(bitmap
);
329 bool wxBitmapDataObject::GetDataHere(void *buf
) const
333 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
338 memcpy(buf
, m_pngData
, m_pngSize
);
343 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
347 wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG
) != NULL
,
348 FALSE
, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
351 m_pngData
= malloc(m_pngSize
);
353 memcpy(m_pngData
, buf
, m_pngSize
);
355 wxMemoryInputStream
mstream((char*) m_pngData
, m_pngSize
);
357 if ( !image
.LoadFile( mstream
, wxBITMAP_TYPE_PNG
) )
362 m_bitmap
= wxBitmap(image
);
364 return m_bitmap
.Ok();
367 void wxBitmapDataObject::DoConvertToPng()
369 if ( !m_bitmap
.Ok() )
372 wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG
) != NULL
,
373 wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
375 wxImage image
= m_bitmap
.ConvertToImage();
377 wxCountingOutputStream count
;
378 image
.SaveFile(count
, wxBITMAP_TYPE_PNG
);
380 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
381 m_pngData
= malloc(m_pngSize
);
383 wxMemoryOutputStream
mstream((char*) m_pngData
, m_pngSize
);
384 image
.SaveFile(mstream
, wxBITMAP_TYPE_PNG
);