1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dataobj.cpp
3 // Purpose: wxDataObject class
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/dataobj.h"
23 #include "wx/mstream.h"
26 #include "wx/gtk/private.h"
28 //-------------------------------------------------------------------------
30 //-------------------------------------------------------------------------
32 GdkAtom g_textAtom
= 0;
33 GdkAtom g_altTextAtom
= 0;
34 GdkAtom g_pngAtom
= 0;
35 GdkAtom g_fileAtom
= 0;
36 GdkAtom g_htmlAtom
= 0;
38 //-------------------------------------------------------------------------
40 //-------------------------------------------------------------------------
42 wxDataFormat::wxDataFormat()
44 // do *not* call PrepareFormats() from here for 2 reasons:
46 // 1. we will have time to do it later because some other Set function
47 // must be called before we really need them
49 // 2. doing so prevents us from declaring global wxDataFormats because
50 // calling PrepareFormats (and thus gdk_atom_intern) before GDK is
51 // initialised will result in a crash
52 m_type
= wxDF_INVALID
;
53 m_format
= (GdkAtom
) 0;
56 wxDataFormat::wxDataFormat( wxDataFormatId type
)
62 void wxDataFormat::InitFromString( const wxString
&id
)
68 wxDataFormat::wxDataFormat( NativeFormat format
)
74 void wxDataFormat::SetType( wxDataFormatId type
)
81 if (m_type
== wxDF_UNICODETEXT
)
82 m_format
= g_textAtom
;
83 else if (m_type
== wxDF_TEXT
)
84 m_format
= g_altTextAtom
;
85 #else // !wxUSE_UNICODE
86 // notice that we don't map wxDF_UNICODETEXT to g_textAtom here, this
87 // would lead the code elsewhere to treat data objects with this format as
88 // containing UTF-8 data which is not true
89 if (m_type
== wxDF_TEXT
)
90 m_format
= g_textAtom
;
91 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
93 if (m_type
== wxDF_BITMAP
)
96 if (m_type
== wxDF_FILENAME
)
97 m_format
= g_fileAtom
;
99 if (m_type
== wxDF_HTML
)
100 m_format
= g_htmlAtom
;
103 wxFAIL_MSG( wxT("invalid dataformat") );
107 wxDataFormatId
wxDataFormat::GetType() const
112 wxString
wxDataFormat::GetId() const
114 wxGtkString
atom_name(gdk_atom_name(m_format
));
115 return wxString::FromAscii(atom_name
);
118 void wxDataFormat::SetId( NativeFormat format
)
123 if (m_format
== g_textAtom
)
125 m_type
= wxDF_UNICODETEXT
;
130 if (m_format
== g_altTextAtom
)
133 if (m_format
== g_pngAtom
)
134 m_type
= wxDF_BITMAP
;
136 if (m_format
== g_fileAtom
)
137 m_type
= wxDF_FILENAME
;
139 if (m_format
== g_htmlAtom
)
142 m_type
= wxDF_PRIVATE
;
145 void wxDataFormat::SetId( const wxString
& id
)
148 m_type
= wxDF_PRIVATE
;
149 m_format
= gdk_atom_intern( id
.ToAscii(), FALSE
);
152 void wxDataFormat::PrepareFormats()
154 // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the
155 // atoms STRING and file:ALL as the old code was, but normal X apps
156 // use STRING for text selection when transferring the data via
157 // clipboard, for example, so do use STRING for now (GNOME apps will
158 // probably support STRING as well for compatibility anyhow), but use
159 // text/uri-list for file dnd because compatibility is not important
164 g_textAtom
= gdk_atom_intern( "UTF8_STRING", FALSE
);
165 g_altTextAtom
= gdk_atom_intern( "STRING", FALSE
);
167 g_textAtom
= gdk_atom_intern( "STRING" /* "text/plain" */, FALSE
);
171 g_pngAtom
= gdk_atom_intern( "image/png", FALSE
);
173 g_fileAtom
= gdk_atom_intern( "text/uri-list", FALSE
);
175 g_htmlAtom
= gdk_atom_intern( "text/html", FALSE
);
178 //-------------------------------------------------------------------------
180 //-------------------------------------------------------------------------
182 wxDataObject::wxDataObject()
186 wxDataObject::~wxDataObject()
188 // dtor is empty but needed for Darwin and AIX -- otherwise it doesn't link
191 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
193 size_t nFormatCount
= GetFormatCount(dir
);
194 if ( nFormatCount
== 1 )
196 return format
== GetPreferredFormat();
200 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
201 GetAllFormats(formats
,dir
);
204 for ( n
= 0; n
< nFormatCount
; n
++ )
206 if ( formats
[n
] == format
)
213 return n
< nFormatCount
;
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
224 wxTextDataObject::GetAllFormats(wxDataFormat
*formats
,
225 wxDataObjectBase::Direction
WXUNUSED(dir
)) const
227 *formats
++ = GetPreferredFormat();
228 *formats
= g_altTextAtom
;
231 #endif // wxUSE_UNICODE
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 bool wxFileDataObject::GetDataHere(void *buf
) const
241 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
243 filenames
+= wxT("file:");
244 filenames
+= m_filenames
[i
];
245 filenames
+= wxT("\r\n");
248 memcpy( buf
, filenames
.mbc_str(), filenames
.length() + 1 );
253 size_t wxFileDataObject::GetDataSize() const
257 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
259 // This is junk in UTF-8
260 res
+= m_filenames
[i
].length();
261 res
+= 5 + 2; // "file:" (5) + "\r\n" (2)
267 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
269 // we get data in the text/uri-list format, i.e. as a sequence of URIs
270 // (filenames prefixed by "file:") delimited by "\r\n". size includes
271 // the trailing zero (in theory, not for Nautilus in early GNOME
276 const gchar
*nexttemp
= (const gchar
*) buf
;
280 const gchar
*temp
= nexttemp
;
287 // if an app omits '\r''\n'
294 if (temp
[len
] == '\r')
296 if (temp
[len
+1] == '\n')
297 nexttemp
= temp
+len
+2;
299 nexttemp
= temp
+len
+1;
308 // required to give it a trailing zero
309 gchar
*uri
= g_strndup( temp
, len
);
311 gchar
*fn
= g_filename_from_uri( uri
, NULL
, NULL
);
317 AddFile( wxConvFileName
->cMB2WX( fn
) );
325 void wxFileDataObject::AddFile( const wxString
&filename
)
327 m_filenames
.Add( filename
);
330 // ----------------------------------------------------------------------------
331 // wxBitmapDataObject
332 // ----------------------------------------------------------------------------
334 wxBitmapDataObject::wxBitmapDataObject()
339 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
340 : wxBitmapDataObjectBase(bitmap
)
347 wxBitmapDataObject::~wxBitmapDataObject()
352 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
356 wxBitmapDataObjectBase::SetBitmap(bitmap
);
361 bool wxBitmapDataObject::GetDataHere(void *buf
) const
365 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
370 memcpy(buf
, m_pngData
, m_pngSize
);
375 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
379 wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG
) != NULL
,
380 false, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
383 m_pngData
= malloc(m_pngSize
);
385 memcpy(m_pngData
, buf
, m_pngSize
);
387 wxMemoryInputStream
mstream((char*) m_pngData
, m_pngSize
);
389 if ( !image
.LoadFile( mstream
, wxBITMAP_TYPE_PNG
) )
394 m_bitmap
= wxBitmap(image
);
396 return m_bitmap
.IsOk();
399 void wxBitmapDataObject::DoConvertToPng()
401 if ( !m_bitmap
.IsOk() )
404 wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG
) != NULL
,
405 wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
407 wxImage image
= m_bitmap
.ConvertToImage();
409 wxCountingOutputStream count
;
410 image
.SaveFile(count
, wxBITMAP_TYPE_PNG
);
412 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
413 m_pngData
= malloc(m_pngSize
);
415 wxMemoryOutputStream
mstream((char*) m_pngData
, m_pngSize
);
416 image
.SaveFile(mstream
, wxBITMAP_TYPE_PNG
);
419 // ----------------------------------------------------------------------------
421 // ----------------------------------------------------------------------------
423 wxURLDataObject::wxURLDataObject(const wxString
& url
) :
424 wxDataObjectSimple( wxDataFormat( g_fileAtom
) )
429 size_t wxURLDataObject::GetDataSize() const
431 return strlen(m_url
.utf8_str()) + 1;
434 bool wxURLDataObject::GetDataHere(void *buf
) const
436 strcpy(static_cast<char*>(buf
), m_url
.utf8_str());
441 bool wxURLDataObject::SetData(size_t len
, const void *buf
)
443 m_url
= wxString::FromUTF8(static_cast<const char*>(buf
), len
);
449 #endif // wxUSE_DATAOBJ