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