1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/dataobj.cpp
3 // Purpose: wxDataObject class
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
14 #include "wx/dataobj.h"
22 #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 void wxDataFormat::InitFromString( const wxString
&id
)
66 wxDataFormat::wxDataFormat( NativeFormat format
)
72 void wxDataFormat::SetType( wxDataFormatId type
)
79 if (m_type
== wxDF_UNICODETEXT
)
80 m_format
= g_textAtom
;
81 else if (m_type
== wxDF_TEXT
)
82 m_format
= g_altTextAtom
;
84 if (m_type
== wxDF_TEXT
|| m_type
== wxDF_UNICODETEXT
)
85 m_format
= g_textAtom
;
88 if (m_type
== wxDF_BITMAP
)
91 if (m_type
== wxDF_FILENAME
)
92 m_format
= g_fileAtom
;
95 wxFAIL_MSG( wxT("invalid dataformat") );
99 wxDataFormatId
wxDataFormat::GetType() const
104 wxString
wxDataFormat::GetId() const
106 gchar
* atom_name
= gdk_atom_name( m_format
);
107 wxString ret
= wxString::FromAscii( atom_name
);
112 void wxDataFormat::SetId( NativeFormat format
)
117 if (m_format
== g_textAtom
)
119 m_type
= wxDF_UNICODETEXT
;
124 if (m_format
== g_altTextAtom
)
127 if (m_format
== g_pngAtom
)
128 m_type
= wxDF_BITMAP
;
130 if (m_format
== g_fileAtom
)
131 m_type
= wxDF_FILENAME
;
133 m_type
= wxDF_PRIVATE
;
136 void wxDataFormat::SetId( const wxString
& id
)
139 m_type
= wxDF_PRIVATE
;
140 m_format
= gdk_atom_intern( id
.ToAscii(), FALSE
);
143 void wxDataFormat::PrepareFormats()
145 // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the
146 // atoms STRING and file:ALL as the old code was, but normal X apps
147 // use STRING for text selection when transfering the data via
148 // clipboard, for example, so do use STRING for now (GNOME apps will
149 // probably support STRING as well for compatibility anyhow), but use
150 // text/uri-list for file dnd because compatibility is not important
154 g_textAtom
= gdk_atom_intern( "UTF8_STRING", FALSE
);
155 g_altTextAtom
= gdk_atom_intern( "STRING", FALSE
);
157 g_textAtom
= gdk_atom_intern( "STRING" /* "text/plain" */, FALSE
);
160 g_pngAtom
= gdk_atom_intern( "image/png", FALSE
);
162 g_fileAtom
= gdk_atom_intern( "text/uri-list", FALSE
);
165 //-------------------------------------------------------------------------
167 //-------------------------------------------------------------------------
169 wxDataObject::wxDataObject()
173 wxDataObject::~wxDataObject()
175 // dtor is empty but needed for Darwin and AIX -- otherwise it doesn't link
178 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
180 size_t nFormatCount
= GetFormatCount(dir
);
181 if ( nFormatCount
== 1 )
183 return format
== GetPreferredFormat();
187 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
188 GetAllFormats(formats
,dir
);
191 for ( n
= 0; n
< nFormatCount
; n
++ )
193 if ( formats
[n
] == format
)
200 return n
< nFormatCount
;
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 bool wxFileDataObject::GetDataHere(void *buf
) const
212 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
214 filenames
+= wxT("file:");
215 filenames
+= m_filenames
[i
];
216 filenames
+= wxT("\r\n");
219 memcpy( buf
, filenames
.mbc_str(), filenames
.length() + 1 );
224 size_t wxFileDataObject::GetDataSize() const
228 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
230 // This is junk in UTF-8
231 res
+= m_filenames
[i
].length();
232 res
+= 5 + 2; // "file:" (5) + "\r\n" (2)
238 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
242 // we get data in the text/uri-list format, i.e. as a sequence of URIs
243 // (filenames prefixed by "file:") delimited by "\r\n"
245 for ( const char *p
= (const char *)buf
; ; p
++ )
247 // some broken programs (testdnd GTK+ sample!) omit the trailing
248 // "\r\n", so check for '\0' explicitly here instead of doing it in
249 // the loop statement to account for it
250 if ( (*p
== '\r' && *(p
+1) == '\n') || !*p
)
252 size_t lenPrefix
= 5; // strlen("file:")
253 if ( filename
.Left(lenPrefix
).MakeLower() == wxT("file:") )
255 // sometimes the syntax is "file:filename", sometimes it's
256 // URL-like: "file://filename" - deal with both
257 if ( filename
[lenPrefix
] == wxT('/') &&
258 filename
[lenPrefix
+ 1] == wxT('/') )
264 AddFile(wxURI::Unescape(filename
.c_str() + lenPrefix
));
269 wxLogDebug(wxT("Unsupported URI '%s' in wxFileDataObject"),
288 void wxFileDataObject::AddFile( const wxString
&filename
)
290 m_filenames
.Add( filename
);
293 // ----------------------------------------------------------------------------
294 // wxBitmapDataObject
295 // ----------------------------------------------------------------------------
297 wxBitmapDataObject::wxBitmapDataObject()
302 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
303 : wxBitmapDataObjectBase(bitmap
)
310 wxBitmapDataObject::~wxBitmapDataObject()
315 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
319 wxBitmapDataObjectBase::SetBitmap(bitmap
);
324 bool wxBitmapDataObject::GetDataHere(void *buf
) const
328 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
333 memcpy(buf
, m_pngData
, m_pngSize
);
338 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
342 wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG
) != NULL
,
343 false, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
346 m_pngData
= malloc(m_pngSize
);
348 memcpy(m_pngData
, buf
, m_pngSize
);
350 wxMemoryInputStream
mstream((char*) m_pngData
, m_pngSize
);
352 if ( !image
.LoadFile( mstream
, wxBITMAP_TYPE_PNG
) )
357 m_bitmap
= wxBitmap(image
);
359 return m_bitmap
.IsOk();
362 void wxBitmapDataObject::DoConvertToPng()
364 if ( !m_bitmap
.IsOk() )
367 wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG
) != NULL
,
368 wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
370 wxImage image
= m_bitmap
.ConvertToImage();
372 wxCountingOutputStream count
;
373 image
.SaveFile(count
, wxBITMAP_TYPE_PNG
);
375 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
376 m_pngData
= malloc(m_pngSize
);
378 wxMemoryOutputStream
mstream((char*) m_pngData
, m_pngSize
);
379 image
.SaveFile(mstream
, wxBITMAP_TYPE_PNG
);
382 #endif // wxUSE_DATAOBJ