#if wxUSE_DATAOBJ guard src/gtk/dataobj.cpp
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "dataobj.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #include "wx/dataobj.h"
18
19 #if wxUSE_DATAOBJ
20
21 #include "wx/app.h"
22 #include "wx/debug.h"
23 #include "wx/mstream.h"
24 #include "wx/image.h"
25 #include "wx/log.h"
26 #include "wx/uri.h"
27
28 #include <gdk/gdk.h>
29
30 //-------------------------------------------------------------------------
31 // global data
32 //-------------------------------------------------------------------------
33
34 GdkAtom g_textAtom = 0;
35 GdkAtom g_altTextAtom = 0;
36 GdkAtom g_pngAtom = 0;
37 GdkAtom g_fileAtom = 0;
38
39 //-------------------------------------------------------------------------
40 // wxDataFormat
41 //-------------------------------------------------------------------------
42
43 wxDataFormat::wxDataFormat()
44 {
45 // do *not* call PrepareFormats() from here for 2 reasons:
46 //
47 // 1. we will have time to do it later because some other Set function
48 // must be called before we really need them
49 //
50 // 2. doing so prevents us from declaring global wxDataFormats because
51 // calling PrepareFormats (and thus gdk_atom_intern) before GDK is
52 // initialised will result in a crash
53 m_type = wxDF_INVALID;
54 m_format = (GdkAtom) 0;
55 }
56
57 wxDataFormat::wxDataFormat( wxDataFormatId type )
58 {
59 PrepareFormats();
60 SetType( type );
61 }
62
63 wxDataFormat::wxDataFormat( const wxChar *id )
64 {
65 PrepareFormats();
66 SetId( id );
67 }
68
69 wxDataFormat::wxDataFormat( const wxString &id )
70 {
71 PrepareFormats();
72 SetId( id );
73 }
74
75 wxDataFormat::wxDataFormat( NativeFormat format )
76 {
77 PrepareFormats();
78 SetId( format );
79 }
80
81 void wxDataFormat::SetType( wxDataFormatId type )
82 {
83 PrepareFormats();
84
85 m_type = type;
86
87 #if wxUSE_UNICODE
88 if (m_type == wxDF_UNICODETEXT)
89 m_format = g_textAtom;
90 else if (m_type == wxDF_TEXT)
91 m_format = g_altTextAtom;
92 #else
93 if (m_type == wxDF_TEXT || m_type == wxDF_UNICODETEXT)
94 m_format = g_textAtom;
95 #endif
96 else
97 if (m_type == wxDF_BITMAP)
98 m_format = g_pngAtom;
99 else
100 if (m_type == wxDF_FILENAME)
101 m_format = g_fileAtom;
102 else
103 {
104 wxFAIL_MSG( wxT("invalid dataformat") );
105 }
106 }
107
108 wxDataFormatId wxDataFormat::GetType() const
109 {
110 return m_type;
111 }
112
113 wxString wxDataFormat::GetId() const
114 {
115 wxString ret = wxString::FromAscii( gdk_atom_name( m_format ) );
116 return ret;
117 }
118
119 void wxDataFormat::SetId( NativeFormat format )
120 {
121 PrepareFormats();
122 m_format = format;
123
124 if (m_format == g_textAtom)
125 #if wxUSE_UNICODE
126 m_type = wxDF_UNICODETEXT;
127 #else
128 m_type = wxDF_TEXT;
129 #endif
130 else
131 if (m_format == g_altTextAtom)
132 m_type = wxDF_TEXT;
133 else
134 if (m_format == g_pngAtom)
135 m_type = wxDF_BITMAP;
136 else
137 if (m_format == g_fileAtom)
138 m_type = wxDF_FILENAME;
139 else
140 m_type = wxDF_PRIVATE;
141 }
142
143 void wxDataFormat::SetId( const wxChar *id )
144 {
145 PrepareFormats();
146 m_type = wxDF_PRIVATE;
147 wxString tmp( id );
148 m_format = gdk_atom_intern( (const char*) tmp.ToAscii(), FALSE );
149 }
150
151 void wxDataFormat::PrepareFormats()
152 {
153 // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the
154 // atoms STRING and file:ALL as the old code was, but normal X apps
155 // use STRING for text selection when transfering the data via
156 // clipboard, for example, so do use STRING for now (GNOME apps will
157 // probably support STRING as well for compatibility anyhow), but use
158 // text/uri-list for file dnd because compatibility is not important
159 // here (with whom?)
160 if (!g_textAtom)
161 #if wxUSE_UNICODE
162 g_textAtom = gdk_atom_intern( "UTF8_STRING", FALSE );
163 g_altTextAtom = gdk_atom_intern( "STRING", FALSE );
164 #else
165 g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE );
166 #endif
167 if (!g_pngAtom)
168 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
169 if (!g_fileAtom)
170 g_fileAtom = gdk_atom_intern( "text/uri-list", FALSE );
171 }
172
173 //-------------------------------------------------------------------------
174 // wxDataObject
175 //-------------------------------------------------------------------------
176
177 wxDataObject::wxDataObject()
178 {
179 }
180
181 wxDataObject::~wxDataObject()
182 {
183 // dtor is empty but needed for Darwin and AIX -- otherwise it doesn't link
184 }
185
186 bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
187 {
188 size_t nFormatCount = GetFormatCount(dir);
189 if ( nFormatCount == 1 )
190 {
191 return format == GetPreferredFormat();
192 }
193 else
194 {
195 wxDataFormat *formats = new wxDataFormat[nFormatCount];
196 GetAllFormats(formats,dir);
197
198 size_t n;
199 for ( n = 0; n < nFormatCount; n++ )
200 {
201 if ( formats[n] == format )
202 break;
203 }
204
205 delete [] formats;
206
207 // found?
208 return n < nFormatCount;
209 }
210 }
211
212 // ----------------------------------------------------------------------------
213 // wxTextDataObject
214 // ----------------------------------------------------------------------------
215
216 #if defined(__WXGTK20__) && wxUSE_UNICODE
217 void wxTextDataObject::GetAllFormats(wxDataFormat *formats, wxDataObjectBase::Direction dir) const
218 {
219 *formats++ = GetPreferredFormat();
220 *formats = g_altTextAtom;
221 }
222 #endif
223
224 // ----------------------------------------------------------------------------
225 // wxFileDataObject
226 // ----------------------------------------------------------------------------
227
228 bool wxFileDataObject::GetDataHere(void *buf) const
229 {
230 wxString filenames;
231
232 for (size_t i = 0; i < m_filenames.GetCount(); i++)
233 {
234 filenames += wxT("file:");
235 filenames += m_filenames[i];
236 filenames += wxT("\r\n");
237 }
238
239 memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 );
240
241 return TRUE;
242 }
243
244 size_t wxFileDataObject::GetDataSize() const
245 {
246 size_t res = 0;
247
248 for (size_t i = 0; i < m_filenames.GetCount(); i++)
249 {
250 // This is junk in UTF-8
251 res += m_filenames[i].Len();
252 res += 5 + 2; // "file:" (5) + "\r\n" (2)
253 }
254
255 return res + 1;
256 }
257
258 bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf)
259 {
260 m_filenames.Empty();
261
262 // we get data in the text/uri-list format, i.e. as a sequence of URIs
263 // (filenames prefixed by "file:") delimited by "\r\n"
264 wxString filename;
265 for ( const char *p = (const char *)buf; ; p++ )
266 {
267 // some broken programs (testdnd GTK+ sample!) omit the trailing
268 // "\r\n", so check for '\0' explicitly here instead of doing it in
269 // the loop statement to account for it
270 if ( (*p == '\r' && *(p+1) == '\n') || !*p )
271 {
272 size_t lenPrefix = 5; // strlen("file:")
273 if ( filename.Left(lenPrefix).MakeLower() == _T("file:") )
274 {
275 // sometimes the syntax is "file:filename", sometimes it's
276 // URL-like: "file://filename" - deal with both
277 if ( filename[lenPrefix] == _T('/') &&
278 filename[lenPrefix + 1] == _T('/') )
279 {
280 // skip the slashes
281 lenPrefix += 2;
282 }
283
284 AddFile(wxURI::Unescape(filename.c_str() + lenPrefix));
285 filename.Empty();
286 }
287 else
288 {
289 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
290 filename.c_str());
291 }
292
293 if ( !*p )
294 break;
295
296 // skip '\r'
297 p++;
298 }
299 else
300 {
301 filename += *p;
302 }
303 }
304
305 return TRUE;
306 }
307
308 void wxFileDataObject::AddFile( const wxString &filename )
309 {
310 m_filenames.Add( filename );
311 }
312
313 // ----------------------------------------------------------------------------
314 // wxBitmapDataObject
315 // ----------------------------------------------------------------------------
316
317 wxBitmapDataObject::wxBitmapDataObject()
318 {
319 Init();
320 }
321
322 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
323 : wxBitmapDataObjectBase(bitmap)
324 {
325 Init();
326
327 DoConvertToPng();
328 }
329
330 wxBitmapDataObject::~wxBitmapDataObject()
331 {
332 Clear();
333 }
334
335 void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
336 {
337 ClearAll();
338
339 wxBitmapDataObjectBase::SetBitmap(bitmap);
340
341 DoConvertToPng();
342 }
343
344 bool wxBitmapDataObject::GetDataHere(void *buf) const
345 {
346 if ( !m_pngSize )
347 {
348 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
349
350 return FALSE;
351 }
352
353 memcpy(buf, m_pngData, m_pngSize);
354
355 return TRUE;
356 }
357
358 bool wxBitmapDataObject::SetData(size_t size, const void *buf)
359 {
360 Clear();
361
362 wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL,
363 FALSE, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
364
365 m_pngSize = size;
366 m_pngData = malloc(m_pngSize);
367
368 memcpy(m_pngData, buf, m_pngSize);
369
370 wxMemoryInputStream mstream((char*) m_pngData, m_pngSize);
371 wxImage image;
372 if ( !image.LoadFile( mstream, wxBITMAP_TYPE_PNG ) )
373 {
374 return FALSE;
375 }
376
377 m_bitmap = wxBitmap(image);
378
379 return m_bitmap.Ok();
380 }
381
382 void wxBitmapDataObject::DoConvertToPng()
383 {
384 if ( !m_bitmap.Ok() )
385 return;
386
387 wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL,
388 wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
389
390 wxImage image = m_bitmap.ConvertToImage();
391
392 wxCountingOutputStream count;
393 image.SaveFile(count, wxBITMAP_TYPE_PNG);
394
395 m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
396 m_pngData = malloc(m_pngSize);
397
398 wxMemoryOutputStream mstream((char*) m_pngData, m_pngSize);
399 image.SaveFile(mstream, wxBITMAP_TYPE_PNG);
400 }
401
402 #endif // wxUSE_DATAOBJ