wxFileDataObject supports GNOME file dnd now
[wxWidgets.git] / src / gtk1 / dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: dataobj.cpp
3 // Purpose: wxDataObject class
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "dataobj.h"
12 #endif
13
14 #include "wx/dataobj.h"
15 #include "wx/app.h"
16 #include "wx/debug.h"
17 #include "wx/mstream.h"
18 #include "wx/image.h"
19
20 #include "gdk/gdk.h"
21
22 //-------------------------------------------------------------------------
23 // global data
24 //-------------------------------------------------------------------------
25
26 GdkAtom g_textAtom = 0;
27 GdkAtom g_pngAtom = 0;
28 GdkAtom g_fileAtom = 0;
29
30 //-------------------------------------------------------------------------
31 // wxDataFormat
32 //-------------------------------------------------------------------------
33
34 wxDataFormat::wxDataFormat()
35 {
36 // do *not* call PrepareFormats() from here for 2 reasons:
37 //
38 // 1. we will have time to do it later because some other Set function
39 // must be called before we really need them
40 //
41 // 2. doing so prevents us from declaring global wxDataFormats because
42 // calling PrepareFormats (and thus gdk_atom_intern) before GDK is
43 // initialised will result in a crash
44 m_type = wxDF_INVALID;
45 m_format = (GdkAtom) 0;
46 }
47
48 wxDataFormat::wxDataFormat( wxDataFormatId type )
49 {
50 PrepareFormats();
51 SetType( type );
52 }
53
54 wxDataFormat::wxDataFormat( const wxChar *id )
55 {
56 PrepareFormats();
57 SetId( id );
58 }
59
60 wxDataFormat::wxDataFormat( const wxString &id )
61 {
62 PrepareFormats();
63 SetId( id );
64 }
65
66 wxDataFormat::wxDataFormat( NativeFormat format )
67 {
68 PrepareFormats();
69 SetId( format );
70 }
71
72 void wxDataFormat::SetType( wxDataFormatId type )
73 {
74 PrepareFormats();
75 m_type = type;
76
77 if (m_type == wxDF_TEXT)
78 m_format = g_textAtom;
79 else
80 if (m_type == wxDF_BITMAP)
81 m_format = g_pngAtom;
82 else
83 if (m_type == wxDF_FILENAME)
84 m_format = g_fileAtom;
85 else
86 {
87 wxFAIL_MSG( wxT("invalid dataformat") );
88 }
89 }
90
91 wxDataFormatId wxDataFormat::GetType() const
92 {
93 return m_type;
94 }
95
96 wxString wxDataFormat::GetId() const
97 {
98 wxString ret( gdk_atom_name( m_format ) ); // this will convert from ascii to Unicode
99 return ret;
100 }
101
102 void wxDataFormat::SetId( NativeFormat format )
103 {
104 PrepareFormats();
105 m_format = format;
106
107 if (m_format == g_textAtom)
108 m_type = wxDF_TEXT;
109 else
110 if (m_format == g_pngAtom)
111 m_type = wxDF_BITMAP;
112 else
113 if (m_format == g_fileAtom)
114 m_type = wxDF_FILENAME;
115 else
116 m_type = wxDF_PRIVATE;
117 }
118
119 void wxDataFormat::SetId( const wxChar *id )
120 {
121 PrepareFormats();
122 m_type = wxDF_PRIVATE;
123 wxString tmp( id );
124 m_format = gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE ); // what is the string cast for?
125 }
126
127 void wxDataFormat::PrepareFormats()
128 {
129 // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the
130 // atoms STRING and file:ALL as the old code was
131 if (!g_textAtom)
132 g_textAtom = gdk_atom_intern( "text/plain", FALSE );
133 if (!g_pngAtom)
134 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
135 if (!g_fileAtom)
136 g_fileAtom = gdk_atom_intern( "text/uri-list", FALSE );
137 }
138
139 //-------------------------------------------------------------------------
140 // wxDataObject
141 //-------------------------------------------------------------------------
142
143 wxDataObject::wxDataObject()
144 {
145 }
146
147 bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
148 {
149 size_t nFormatCount = GetFormatCount(dir);
150 if ( nFormatCount == 1 ) {
151 return format == GetPreferredFormat();
152 }
153 else {
154 wxDataFormat *formats = new wxDataFormat[nFormatCount];
155 GetAllFormats(formats,dir);
156
157 size_t n;
158 for ( n = 0; n < nFormatCount; n++ ) {
159 if ( formats[n] == format )
160 break;
161 }
162
163 delete [] formats;
164
165 // found?
166 return n < nFormatCount;
167 }
168 }
169
170 // ----------------------------------------------------------------------------
171 // wxFileDataObject
172 // ----------------------------------------------------------------------------
173
174 bool wxFileDataObject::GetDataHere(void *buf) const
175 {
176 wxString filenames;
177
178 for (size_t i = 0; i < m_filenames.GetCount(); i++)
179 {
180 filenames += m_filenames[i];
181 filenames += (wxChar) 0;
182 }
183
184 memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 );
185
186 return TRUE;
187 }
188
189 size_t wxFileDataObject::GetDataSize() const
190 {
191 size_t res = 0;
192
193 for (size_t i = 0; i < m_filenames.GetCount(); i++)
194 {
195 res += m_filenames[i].Len();
196 res += 1;
197 }
198
199 return res + 1;
200 }
201
202 bool wxFileDataObject::SetData(size_t size, const void *buf)
203 {
204 // VZ: old format
205 #if 0
206 // filenames are stores as a string with #0 as deliminators
207 const char *filenames = (const char*) buf;
208 size_t pos = 0;
209 for(;;)
210 {
211 if (filenames[0] == 0)
212 break;
213 if (pos >= size)
214 break;
215 wxString file( filenames ); // this returns the first file
216 AddFile( file );
217 pos += file.Len()+1;
218 filenames += file.Len()+1;
219 }
220 #else // 1
221 m_filenames.Empty();
222
223 // the text/uri-list format is a sequence of URIs (filenames prefixed by
224 // "file:" as far as I see) delimited by "\r\n" of total length size
225 // (I wonder what happens if the file has '\n' in its filename??)
226 wxString filename;
227 for ( const char *p = (const char *)buf; *p; p++ )
228 {
229 if ( *p == '\r' && *(p+1) == '\n' )
230 {
231 static const int lenPrefix = 5; // strlen("file:")
232 if ( filename.Left(lenPrefix).MakeLower() == _T("file:") )
233 {
234 filename.erase(0, lenPrefix);
235 }
236
237 AddFile(filename);
238 filename.Empty();
239
240 // skip '\r'
241 p++;
242 }
243 else
244 {
245 filename += *p;
246 }
247 }
248 #endif // 0/1
249
250 return TRUE;
251 }
252
253 void wxFileDataObject::AddFile( const wxString &filename )
254 {
255 m_filenames.Add( filename );
256 }
257
258 // ----------------------------------------------------------------------------
259 // wxBitmapDataObject
260 // ----------------------------------------------------------------------------
261
262 wxBitmapDataObject::wxBitmapDataObject()
263 {
264 Init();
265 }
266
267 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
268 : wxBitmapDataObjectBase(bitmap)
269 {
270 Init();
271
272 DoConvertToPng();
273 }
274
275 wxBitmapDataObject::~wxBitmapDataObject()
276 {
277 Clear();
278 }
279
280 void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
281 {
282 ClearAll();
283
284 wxBitmapDataObjectBase::SetBitmap(bitmap);
285
286 DoConvertToPng();
287 }
288
289 bool wxBitmapDataObject::GetDataHere(void *buf) const
290 {
291 if ( !m_pngSize )
292 {
293 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
294
295 return FALSE;
296 }
297
298 memcpy(buf, m_pngData, m_pngSize);
299
300 return TRUE;
301 }
302
303 bool wxBitmapDataObject::SetData(size_t size, const void *buf)
304 {
305 Clear();
306
307 m_pngSize = size;
308 m_pngData = malloc(m_pngSize);
309
310 memcpy( m_pngData, buf, m_pngSize );
311
312 wxMemoryInputStream mstream( (char*) m_pngData, m_pngSize );
313 wxImage image;
314 wxPNGHandler handler;
315 if ( !handler.LoadFile( &image, mstream ) )
316 {
317 return FALSE;
318 }
319
320 m_bitmap = image.ConvertToBitmap();
321
322 return m_bitmap.Ok();
323 }
324
325 void wxBitmapDataObject::DoConvertToPng()
326 {
327 if (!m_bitmap.Ok())
328 return;
329
330 wxImage image( m_bitmap );
331 wxPNGHandler handler;
332
333 wxCountingOutputStream count;
334 handler.SaveFile( &image, count );
335
336 m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
337 m_pngData = malloc(m_pngSize);
338
339 wxMemoryOutputStream mstream( (char*) m_pngData, m_pngSize );
340 handler.SaveFile( &image, mstream );
341 }
342
343