1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        src/gtk/dataobj.cpp
3 // Purpose:     wxDataObject class
4 // Author:      Robert Roebling
5 // Copyright:   (c) 1998 Robert Roebling
6 // Licence:     wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
8 
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11 
12 #if wxUSE_DATAOBJ
13 
14 #include "wx/dataobj.h"
15 
16 #ifndef WX_PRECOMP
17     #include "wx/log.h"
18     #include "wx/app.h"
19     #include "wx/image.h"
20 #endif
21 
22 #include "wx/mstream.h"
23 #include "wx/uri.h"
24 
25 #include "wx/gtk/private.h"
26 
27 //-------------------------------------------------------------------------
28 // global data
29 //-------------------------------------------------------------------------
30 
31 GdkAtom  g_textAtom        = 0;
32 GdkAtom  g_altTextAtom     = 0;
33 GdkAtom  g_pngAtom         = 0;
34 GdkAtom  g_fileAtom        = 0;
35 GdkAtom  g_htmlAtom        = 0;
36 
37 //-------------------------------------------------------------------------
38 // wxDataFormat
39 //-------------------------------------------------------------------------
40 
wxDataFormat()41 wxDataFormat::wxDataFormat()
42 {
43     // do *not* call PrepareFormats() from here for 2 reasons:
44     //
45     // 1. we will have time to do it later because some other Set function
46     //    must be called before we really need them
47     //
48     // 2. doing so prevents us from declaring global wxDataFormats because
49     //    calling PrepareFormats (and thus gdk_atom_intern) before GDK is
50     //    initialised will result in a crash
51     m_type = wxDF_INVALID;
52     m_format = (GdkAtom) 0;
53 }
54 
wxDataFormat(wxDataFormatId type)55 wxDataFormat::wxDataFormat( wxDataFormatId type )
56 {
57     PrepareFormats();
58     SetType( type );
59 }
60 
InitFromString(const wxString & id)61 void wxDataFormat::InitFromString( const wxString &id )
62 {
63     PrepareFormats();
64     SetId( id );
65 }
66 
wxDataFormat(NativeFormat format)67 wxDataFormat::wxDataFormat( NativeFormat format )
68 {
69     PrepareFormats();
70     SetId( format );
71 }
72 
SetType(wxDataFormatId type)73 void wxDataFormat::SetType( wxDataFormatId type )
74 {
75     PrepareFormats();
76 
77     m_type = type;
78 
79 #if wxUSE_UNICODE
80     if (m_type == wxDF_UNICODETEXT)
81         m_format = g_textAtom;
82     else if (m_type == wxDF_TEXT)
83         m_format = g_altTextAtom;
84 #else // !wxUSE_UNICODE
85     // notice that we don't map wxDF_UNICODETEXT to g_textAtom here, this
86     // would lead the code elsewhere to treat data objects with this format as
87     // containing UTF-8 data which is not true
88     if (m_type == wxDF_TEXT)
89         m_format = g_textAtom;
90 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
91     else
92     if (m_type == wxDF_BITMAP)
93         m_format = g_pngAtom;
94     else
95     if (m_type == wxDF_FILENAME)
96         m_format = g_fileAtom;
97     else
98     if (m_type == wxDF_HTML)
99         m_format = g_htmlAtom;
100     else
101     {
102        wxFAIL_MSG( wxT("invalid dataformat") );
103     }
104 }
105 
GetType() const106 wxDataFormatId wxDataFormat::GetType() const
107 {
108     return m_type;
109 }
110 
GetId() const111 wxString wxDataFormat::GetId() const
112 {
113     wxGtkString atom_name(gdk_atom_name(m_format));
114     return wxString::FromAscii(atom_name);
115 }
116 
SetId(NativeFormat format)117 void wxDataFormat::SetId( NativeFormat format )
118 {
119     PrepareFormats();
120     m_format = format;
121 
122     if (m_format == g_textAtom)
123 #if wxUSE_UNICODE
124         m_type = wxDF_UNICODETEXT;
125 #else
126         m_type = wxDF_TEXT;
127 #endif
128     else
129     if (m_format == g_altTextAtom)
130         m_type = wxDF_TEXT;
131     else
132     if (m_format == g_pngAtom)
133         m_type = wxDF_BITMAP;
134     else
135     if (m_format == g_fileAtom)
136         m_type = wxDF_FILENAME;
137     else
138     if (m_format == g_htmlAtom)
139         m_type = wxDF_HTML;
140     else
141         m_type = wxDF_PRIVATE;
142 }
143 
SetId(const wxString & id)144 void wxDataFormat::SetId( const wxString& id )
145 {
146     PrepareFormats();
147     m_type = wxDF_PRIVATE;
148     m_format = gdk_atom_intern( id.ToAscii(), FALSE );
149 }
150 
PrepareFormats()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 transferring 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     {
162 #if wxUSE_UNICODE
163         g_textAtom = gdk_atom_intern( "UTF8_STRING", FALSE );
164         g_altTextAtom = gdk_atom_intern( "STRING", FALSE );
165 #else
166         g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE );
167 #endif
168     }
169     if (!g_pngAtom)
170         g_pngAtom = gdk_atom_intern( "image/png", FALSE );
171     if (!g_fileAtom)
172         g_fileAtom = gdk_atom_intern( "text/uri-list", FALSE );
173     if (!g_htmlAtom)
174         g_htmlAtom = gdk_atom_intern( "text/html", FALSE );
175 }
176 
177 //-------------------------------------------------------------------------
178 // wxDataObject
179 //-------------------------------------------------------------------------
180 
wxDataObject()181 wxDataObject::wxDataObject()
182 {
183 }
184 
~wxDataObject()185 wxDataObject::~wxDataObject()
186 {
187     // dtor is empty but needed for Darwin and AIX -- otherwise it doesn't link
188 }
189 
IsSupportedFormat(const wxDataFormat & format,Direction dir) const190 bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
191 {
192     size_t nFormatCount = GetFormatCount(dir);
193     if ( nFormatCount == 1 )
194     {
195         return format == GetPreferredFormat();
196     }
197     else
198     {
199         wxDataFormat *formats = new wxDataFormat[nFormatCount];
200         GetAllFormats(formats,dir);
201 
202         size_t n;
203         for ( n = 0; n < nFormatCount; n++ )
204         {
205             if ( formats[n] == format )
206                 break;
207         }
208 
209         delete [] formats;
210 
211         // found?
212         return n < nFormatCount;
213     }
214 }
215 
216 // ----------------------------------------------------------------------------
217 // wxTextDataObject
218 // ----------------------------------------------------------------------------
219 
220 #if wxUSE_UNICODE
221 
222 void
GetAllFormats(wxDataFormat * formats,wxDataObjectBase::Direction WXUNUSED (dir)) const223 wxTextDataObject::GetAllFormats(wxDataFormat *formats,
224                                 wxDataObjectBase::Direction WXUNUSED(dir)) const
225 {
226     *formats++ = GetPreferredFormat();
227     *formats = g_altTextAtom;
228 }
229 
230 #endif // wxUSE_UNICODE
231 
232 // ----------------------------------------------------------------------------
233 // wxFileDataObject
234 // ----------------------------------------------------------------------------
235 
GetDataHere(void * buf) const236 bool wxFileDataObject::GetDataHere(void *buf) const
237 {
238     char* out = static_cast<char*>(buf);
239 
240     for (size_t i = 0; i < m_filenames.GetCount(); i++)
241     {
242         char* uri = g_filename_to_uri(m_filenames[i].mbc_str(), 0, 0);
243         if (uri)
244         {
245             size_t const len = strlen(uri);
246             memcpy(out, uri, len);
247             out += len;
248             *(out++) = '\r';
249             *(out++) = '\n';
250             g_free(uri);
251         }
252     }
253     *out = 0;
254 
255     return true;
256 }
257 
GetDataSize() const258 size_t wxFileDataObject::GetDataSize() const
259 {
260     size_t res = 0;
261 
262     for (size_t i = 0; i < m_filenames.GetCount(); i++)
263     {
264         char* uri = g_filename_to_uri(m_filenames[i].mbc_str(), 0, 0);
265         if (uri) {
266             res += strlen(uri) + 2; // Including "\r\n"
267             g_free(uri);
268         }
269     }
270 
271     return res + 1;
272 }
273 
SetData(size_t WXUNUSED (size),const void * buf)274 bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf)
275 {
276     // we get data in the text/uri-list format, i.e. as a sequence of URIs
277     // (filenames prefixed by "file:") delimited by "\r\n". size includes
278     // the trailing zero (in theory, not for Nautilus in early GNOME
279     // versions).
280 
281     m_filenames.Empty();
282 
283     const gchar *nexttemp = (const gchar*) buf;
284     for ( ; ; )
285     {
286         int len = 0;
287         const gchar *temp = nexttemp;
288         for (;;)
289         {
290             if (temp[len] == 0)
291             {
292                 if (len > 0)
293                 {
294                     // if an app omits '\r''\n'
295                     nexttemp = temp+len;
296                     break;
297                 }
298 
299                 return true;
300             }
301             if (temp[len] == '\r')
302             {
303                 if (temp[len+1] == '\n')
304                     nexttemp = temp+len+2;
305                 else
306                     nexttemp = temp+len+1;
307                 break;
308             }
309             len++;
310         }
311 
312         if (len == 0)
313             break;
314 
315         // required to give it a trailing zero
316         gchar *uri = g_strndup( temp, len );
317 
318         gchar *fn = g_filename_from_uri( uri, NULL, NULL );
319 
320         g_free( uri );
321 
322         if (fn)
323         {
324             AddFile( wxConvFileName->cMB2WX( fn ) );
325             g_free( fn );
326         }
327     }
328 
329     return true;
330 }
331 
AddFile(const wxString & filename)332 void wxFileDataObject::AddFile( const wxString &filename )
333 {
334    m_filenames.Add( filename );
335 }
336 
337 // ----------------------------------------------------------------------------
338 // wxBitmapDataObject
339 // ----------------------------------------------------------------------------
340 
wxBitmapDataObject()341 wxBitmapDataObject::wxBitmapDataObject()
342 {
343     Init();
344 }
345 
wxBitmapDataObject(const wxBitmap & bitmap)346 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
347                   : wxBitmapDataObjectBase(bitmap)
348 {
349     Init();
350 
351     DoConvertToPng();
352 }
353 
~wxBitmapDataObject()354 wxBitmapDataObject::~wxBitmapDataObject()
355 {
356     Clear();
357 }
358 
SetBitmap(const wxBitmap & bitmap)359 void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
360 {
361     ClearAll();
362 
363     wxBitmapDataObjectBase::SetBitmap(bitmap);
364 
365     DoConvertToPng();
366 }
367 
GetDataHere(void * buf) const368 bool wxBitmapDataObject::GetDataHere(void *buf) const
369 {
370     if ( !m_pngSize )
371     {
372         wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
373 
374         return false;
375     }
376 
377     memcpy(buf, m_pngData, m_pngSize);
378 
379     return true;
380 }
381 
SetData(size_t size,const void * buf)382 bool wxBitmapDataObject::SetData(size_t size, const void *buf)
383 {
384     Clear();
385 
386     wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL,
387                  false, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
388 
389     m_pngSize = size;
390     m_pngData = malloc(m_pngSize);
391 
392     memcpy(m_pngData, buf, m_pngSize);
393 
394     wxMemoryInputStream mstream((char*) m_pngData, m_pngSize);
395     wxImage image;
396     if ( !image.LoadFile( mstream, wxBITMAP_TYPE_PNG ) )
397     {
398         return false;
399     }
400 
401     m_bitmap = wxBitmap(image);
402 
403     return m_bitmap.IsOk();
404 }
405 
DoConvertToPng()406 void wxBitmapDataObject::DoConvertToPng()
407 {
408     if ( !m_bitmap.IsOk() )
409         return;
410 
411     wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL,
412                  wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
413 
414     wxImage image = m_bitmap.ConvertToImage();
415 
416     wxCountingOutputStream count;
417     image.SaveFile(count, wxBITMAP_TYPE_PNG);
418 
419     m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
420     m_pngData = malloc(m_pngSize);
421 
422     wxMemoryOutputStream mstream((char*) m_pngData, m_pngSize);
423     image.SaveFile(mstream, wxBITMAP_TYPE_PNG);
424 }
425 
426 // ----------------------------------------------------------------------------
427 // wxURLDataObject
428 // ----------------------------------------------------------------------------
429 
430 class wxTextURIListDataObject : public wxDataObjectSimple
431 {
432 public:
wxTextURIListDataObject(const wxString & url)433     wxTextURIListDataObject(const wxString& url)
434         : wxDataObjectSimple(wxDataFormat(g_fileAtom)),
435           m_url(url)
436     {
437     }
438 
GetURL() const439     const wxString& GetURL() const { return m_url; }
SetURL(const wxString & url)440     void SetURL(const wxString& url) { m_url = url; }
441 
442 
GetDataSize() const443     virtual size_t GetDataSize() const
444     {
445         // It is not totally clear whether we should include "\r\n" at the end
446         // of the string if there is only one URL or not, but not doing it
447         // doesn't seem to create any problems, so keep things simple.
448         return strlen(m_url.utf8_str()) + 1;
449     }
450 
GetDataHere(void * buf) const451     virtual bool GetDataHere(void *buf) const
452     {
453         char* const dst = static_cast<char*>(buf);
454 
455         strcpy(dst, m_url.utf8_str());
456 
457         return true;
458     }
459 
SetData(size_t len,const void * buf)460     virtual bool SetData(size_t len, const void *buf)
461     {
462         const char* const src = static_cast<const char*>(buf);
463 
464         // The string might be "\r\n"-terminated but this is not necessarily
465         // the case (e.g. when dragging an URL from Firefox, it isn't).
466         if ( len > 1 && src[len - 1] == '\n' )
467         {
468             if ( len > 2 && src[len - 2] == '\r' )
469                 len--;
470 
471             len--;
472         }
473 
474         m_url = wxString::FromUTF8(src, len);
475 
476         return true;
477     }
478 
479     // Must provide overloads to avoid hiding them (and warnings about it)
GetDataSize(const wxDataFormat &) const480     virtual size_t GetDataSize(const wxDataFormat&) const
481     {
482         return GetDataSize();
483     }
GetDataHere(const wxDataFormat &,void * buf) const484     virtual bool GetDataHere(const wxDataFormat&, void *buf) const
485     {
486         return GetDataHere(buf);
487     }
SetData(const wxDataFormat &,size_t len,const void * buf)488     virtual bool SetData(const wxDataFormat&, size_t len, const void *buf)
489     {
490         return SetData(len, buf);
491     }
492 
493 private:
494     wxString m_url;
495 };
496 
wxURLDataObject(const wxString & url)497 wxURLDataObject::wxURLDataObject(const wxString& url) :
498     m_dobjURIList(new wxTextURIListDataObject(url)),
499     m_dobjText(new wxTextDataObject(url))
500 {
501     // Use both URL-specific format and a plain text one to ensure that URLs
502     // can be pasted into any application.
503     Add(m_dobjURIList, true /* preferred */);
504     Add(m_dobjText);
505 }
506 
SetURL(const wxString & url)507 void wxURLDataObject::SetURL(const wxString& url)
508 {
509     m_dobjURIList->SetURL(url);
510     m_dobjText->SetText(url);
511 }
512 
GetURL() const513 wxString wxURLDataObject::GetURL() const
514 {
515     if ( GetReceivedFormat() == g_fileAtom )
516     {
517         // If we received the URL as an URI, use it.
518         return m_dobjURIList->GetURL();
519     }
520     else // Otherwise we either got it as text or didn't get anything yet.
521     {
522         // In either case using the text format should be fine.
523         return m_dobjText->GetText();
524     }
525 }
526 
527 #endif // wxUSE_DATAOBJ
528