]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dataobj.cpp
use GTK-specific method to delete selection
[wxWidgets.git] / src / gtk / dataobj.cpp
CommitLineData
8b53e5a2 1///////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/gtk/dataobj.cpp
8b53e5a2
RR
3// Purpose: wxDataObject class
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
8b53e5a2
RR
8///////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
e4db172a
WS
13#if wxUSE_DATAOBJ
14
8b53e5a2 15#include "wx/dataobj.h"
14631f7f 16
e4db172a
WS
17#ifndef WX_PRECOMP
18 #include "wx/log.h"
670f9935 19 #include "wx/app.h"
155ecd4c 20 #include "wx/image.h"
e4db172a 21#endif
14631f7f 22
e2acb9ae 23#include "wx/mstream.h"
174f2229 24#include "wx/uri.h"
0d2a2b60 25
e808cf8a 26#include "wx/gtk/private.h"
0d2a2b60 27
d6086ea6
RR
28//-------------------------------------------------------------------------
29// global data
30//-------------------------------------------------------------------------
31
32GdkAtom g_textAtom = 0;
c7d6d883 33GdkAtom g_altTextAtom = 0;
e2acb9ae 34GdkAtom g_pngAtom = 0;
1dd989e1 35GdkAtom g_fileAtom = 0;
b8acf11e 36GdkAtom g_htmlAtom = 0;
d6086ea6 37
0d2a2b60
RR
38//-------------------------------------------------------------------------
39// wxDataFormat
40//-------------------------------------------------------------------------
41
cd5bf2a6 42wxDataFormat::wxDataFormat()
0d2a2b60 43{
4b3d29db
VZ
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
cd5bf2a6 52 m_type = wxDF_INVALID;
1dd989e1 53 m_format = (GdkAtom) 0;
cd5bf2a6
RR
54}
55
3f480da3 56wxDataFormat::wxDataFormat( wxDataFormatId type )
cd5bf2a6 57{
e2acb9ae 58 PrepareFormats();
cd5bf2a6 59 SetType( type );
0d2a2b60
RR
60}
61
45344b38 62void wxDataFormat::InitFromString( const wxString &id )
0d2a2b60 63{
e2acb9ae 64 PrepareFormats();
cd5bf2a6 65 SetId( id );
0d2a2b60
RR
66}
67
1dd989e1 68wxDataFormat::wxDataFormat( NativeFormat format )
0d2a2b60 69{
e2acb9ae 70 PrepareFormats();
1dd989e1 71 SetId( format );
0d2a2b60
RR
72}
73
3f480da3 74void wxDataFormat::SetType( wxDataFormatId type )
cd5bf2a6 75{
4b3d29db 76 PrepareFormats();
3d257b8d 77
ca11abde 78 m_type = type;
3d257b8d 79
810324c8
VS
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;
73597f8a
VZ
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)
1dd989e1 90 m_format = g_textAtom;
73597f8a 91#endif // wxUSE_UNICODE/!wxUSE_UNICODE
cd5bf2a6
RR
92 else
93 if (m_type == wxDF_BITMAP)
1dd989e1 94 m_format = g_pngAtom;
cd5bf2a6
RR
95 else
96 if (m_type == wxDF_FILENAME)
1dd989e1 97 m_format = g_fileAtom;
cd5bf2a6 98 else
b8acf11e
RD
99 if (m_type == wxDF_HTML)
100 m_format = g_htmlAtom;
101 else
cd5bf2a6 102 {
223d09f6 103 wxFAIL_MSG( wxT("invalid dataformat") );
cd5bf2a6 104 }
cd5bf2a6 105}
3f480da3
VZ
106
107wxDataFormatId wxDataFormat::GetType() const
0d2a2b60
RR
108{
109 return m_type;
110}
111
112wxString wxDataFormat::GetId() const
113{
e808cf8a
PC
114 wxGtkString atom_name(gdk_atom_name(m_format));
115 return wxString::FromAscii(atom_name);
0d2a2b60 116}
3f480da3 117
1dd989e1 118void wxDataFormat::SetId( NativeFormat format )
3f480da3 119{
4b3d29db 120 PrepareFormats();
1dd989e1 121 m_format = format;
3f480da3 122
1dd989e1 123 if (m_format == g_textAtom)
810324c8
VS
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)
1dd989e1
RR
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;
b8acf11e
RD
138 else
139 if (m_format == g_htmlAtom)
140 m_type = wxDF_HTML;
1dd989e1
RR
141 else
142 m_type = wxDF_PRIVATE;
0d2a2b60 143}
3f480da3 144
a1eb65c2 145void wxDataFormat::SetId( const wxString& id )
0d2a2b60 146{
4b3d29db 147 PrepareFormats();
1dd989e1 148 m_type = wxDF_PRIVATE;
a1eb65c2 149 m_format = gdk_atom_intern( id.ToAscii(), FALSE );
0d2a2b60 150}
3f480da3 151
1dd989e1 152void wxDataFormat::PrepareFormats()
0d2a2b60 153{
810b5e1f 154 // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the
74d38ad8 155 // atoms STRING and file:ALL as the old code was, but normal X apps
27781f63 156 // use STRING for text selection when transferring the data via
74d38ad8
VZ
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?)
e1ee679c 161 if (!g_textAtom)
782158c2 162 {
2e1d7104 163#if wxUSE_UNICODE
ca11abde 164 g_textAtom = gdk_atom_intern( "UTF8_STRING", FALSE );
c7d6d883 165 g_altTextAtom = gdk_atom_intern( "STRING", FALSE );
2e1d7104 166#else
74d38ad8 167 g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE );
2e1d7104 168#endif
782158c2 169 }
e1ee679c 170 if (!g_pngAtom)
1dd989e1 171 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
e1ee679c 172 if (!g_fileAtom)
810b5e1f 173 g_fileAtom = gdk_atom_intern( "text/uri-list", FALSE );
b8acf11e
RD
174 if (!g_htmlAtom)
175 g_htmlAtom = gdk_atom_intern( "text/html", FALSE );
0d2a2b60 176}
8b53e5a2
RR
177
178//-------------------------------------------------------------------------
179// wxDataObject
180//-------------------------------------------------------------------------
181
0d2a2b60
RR
182wxDataObject::wxDataObject()
183{
0d2a2b60 184}
3f480da3 185
2b5f62a0
VZ
186wxDataObject::~wxDataObject()
187{
188 // dtor is empty but needed for Darwin and AIX -- otherwise it doesn't link
189}
190
b068c4e8 191bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
0d2a2b60 192{
b068c4e8 193 size_t nFormatCount = GetFormatCount(dir);
3d257b8d 194 if ( nFormatCount == 1 )
f2593d0d 195 {
1dd989e1
RR
196 return format == GetPreferredFormat();
197 }
3d257b8d 198 else
f2593d0d 199 {
1dd989e1 200 wxDataFormat *formats = new wxDataFormat[nFormatCount];
b068c4e8 201 GetAllFormats(formats,dir);
1dd989e1
RR
202
203 size_t n;
3d257b8d 204 for ( n = 0; n < nFormatCount; n++ )
f2593d0d 205 {
1dd989e1
RR
206 if ( formats[n] == format )
207 break;
208 }
0d2a2b60 209
1dd989e1 210 delete [] formats;
cd5bf2a6 211
1dd989e1
RR
212 // found?
213 return n < nFormatCount;
214 }
0d2a2b60
RR
215}
216
c7d6d883
RR
217// ----------------------------------------------------------------------------
218// wxTextDataObject
219// ----------------------------------------------------------------------------
220
ff654490
VZ
221#if wxUSE_UNICODE
222
e4161a2a
VZ
223void
224wxTextDataObject::GetAllFormats(wxDataFormat *formats,
225 wxDataObjectBase::Direction WXUNUSED(dir)) const
c7d6d883
RR
226{
227 *formats++ = GetPreferredFormat();
228 *formats = g_altTextAtom;
229}
ff654490
VZ
230
231#endif // wxUSE_UNICODE
c7d6d883 232
8b53e5a2
RR
233// ----------------------------------------------------------------------------
234// wxFileDataObject
235// ----------------------------------------------------------------------------
236
e1ee679c 237bool wxFileDataObject::GetDataHere(void *buf) const
0d2a2b60 238{
b068c4e8 239 wxString filenames;
4b3d29db 240
b068c4e8
RR
241 for (size_t i = 0; i < m_filenames.GetCount(); i++)
242 {
a1696b86 243 filenames += wxT("file:");
b068c4e8 244 filenames += m_filenames[i];
a1696b86 245 filenames += wxT("\r\n");
b068c4e8 246 }
4b3d29db 247
155ecd4c 248 memcpy( buf, filenames.mbc_str(), filenames.length() + 1 );
0d2a2b60 249
670f9935 250 return true;
0d2a2b60 251}
3f480da3 252
e1ee679c 253size_t wxFileDataObject::GetDataSize() const
0d2a2b60 254{
b068c4e8 255 size_t res = 0;
4b3d29db 256
b068c4e8
RR
257 for (size_t i = 0; i < m_filenames.GetCount(); i++)
258 {
a1696b86 259 // This is junk in UTF-8
155ecd4c 260 res += m_filenames[i].length();
a1696b86 261 res += 5 + 2; // "file:" (5) + "\r\n" (2)
b068c4e8 262 }
4b3d29db 263
b068c4e8 264 return res + 1;
0d2a2b60 265}
3f480da3 266
7941ba11 267bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf)
0d2a2b60 268{
ead90b5f
RR
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).
27781f63 273
810b5e1f
VZ
274 m_filenames.Empty();
275
ead90b5f
RR
276 const gchar *nexttemp = (const gchar*) buf;
277 for ( ; ; )
810b5e1f 278 {
ead90b5f
RR
279 int len = 0;
280 const gchar *temp = nexttemp;
281 for (;;)
810b5e1f 282 {
ead90b5f 283 if (temp[len] == 0)
810b5e1f 284 {
ead90b5f 285 if (len > 0)
74d38ad8 286 {
ead90b5f
RR
287 // if an app omits '\r''\n'
288 nexttemp = temp+len;
289 break;
74d38ad8 290 }
27781f63 291
ead90b5f 292 return true;
74d38ad8 293 }
ead90b5f 294 if (temp[len] == '\r')
74d38ad8 295 {
ead90b5f
RR
296 if (temp[len+1] == '\n')
297 nexttemp = temp+len+2;
298 else
299 nexttemp = temp+len+1;
74d38ad8 300 break;
ead90b5f
RR
301 }
302 len++;
810b5e1f 303 }
27781f63 304
ead90b5f
RR
305 if (len == 0)
306 break;
27781f63 307
ead90b5f
RR
308 // required to give it a trailing zero
309 gchar *uri = g_strndup( temp, len );
27781f63 310
ead90b5f 311 gchar *fn = g_filename_from_uri( uri, NULL, NULL );
27781f63 312
ead90b5f 313 g_free( uri );
27781f63 314
ead90b5f 315 if (fn)
810b5e1f 316 {
ead90b5f
RR
317 AddFile( wxConvFileName->cMB2WX( fn ) );
318 g_free( fn );
810b5e1f 319 }
2d68e1b4 320 }
3f480da3 321
670f9935 322 return true;
1dd989e1
RR
323}
324
b068c4e8
RR
325void wxFileDataObject::AddFile( const wxString &filename )
326{
327 m_filenames.Add( filename );
328}
329
8b53e5a2
RR
330// ----------------------------------------------------------------------------
331// wxBitmapDataObject
332// ----------------------------------------------------------------------------
333
0d2a2b60
RR
334wxBitmapDataObject::wxBitmapDataObject()
335{
e1ee679c 336 Init();
0d2a2b60
RR
337}
338
339wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
e1ee679c 340 : wxBitmapDataObjectBase(bitmap)
0d2a2b60 341{
e1ee679c
VZ
342 Init();
343
e2acb9ae
RR
344 DoConvertToPng();
345}
346
347wxBitmapDataObject::~wxBitmapDataObject()
348{
e1ee679c 349 Clear();
0d2a2b60
RR
350}
351
352void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
353{
e1ee679c 354 ClearAll();
0d2a2b60 355
e1ee679c
VZ
356 wxBitmapDataObjectBase::SetBitmap(bitmap);
357
358 DoConvertToPng();
0d2a2b60
RR
359}
360
e1ee679c 361bool wxBitmapDataObject::GetDataHere(void *buf) const
0d2a2b60 362{
e1ee679c 363 if ( !m_pngSize )
1dd989e1 364 {
e1ee679c
VZ
365 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
366
670f9935 367 return false;
1dd989e1 368 }
0d2a2b60 369
e1ee679c
VZ
370 memcpy(buf, m_pngData, m_pngSize);
371
670f9935 372 return true;
e2acb9ae
RR
373}
374
e1ee679c 375bool wxBitmapDataObject::SetData(size_t size, const void *buf)
e2acb9ae 376{
e1ee679c
VZ
377 Clear();
378
bd3b7e09 379 wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL,
670f9935 380 false, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
bd3b7e09 381
1dd989e1 382 m_pngSize = size;
e1ee679c
VZ
383 m_pngData = malloc(m_pngSize);
384
bd3b7e09 385 memcpy(m_pngData, buf, m_pngSize);
e1ee679c 386
bd3b7e09 387 wxMemoryInputStream mstream((char*) m_pngData, m_pngSize);
e2acb9ae 388 wxImage image;
bd3b7e09 389 if ( !image.LoadFile( mstream, wxBITMAP_TYPE_PNG ) )
e1ee679c 390 {
670f9935 391 return false;
e1ee679c
VZ
392 }
393
bd3b7e09 394 m_bitmap = wxBitmap(image);
4b3d29db 395
a1b806b9 396 return m_bitmap.IsOk();
e2acb9ae
RR
397}
398
399void wxBitmapDataObject::DoConvertToPng()
400{
a1b806b9 401 if ( !m_bitmap.IsOk() )
e1ee679c
VZ
402 return;
403
bd3b7e09
VS
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
368d59f0 407 wxImage image = m_bitmap.ConvertToImage();
e1ee679c 408
e2acb9ae 409 wxCountingOutputStream count;
bd3b7e09 410 image.SaveFile(count, wxBITMAP_TYPE_PNG);
e1ee679c 411
e2acb9ae 412 m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
e1ee679c
VZ
413 m_pngData = malloc(m_pngSize);
414
bd3b7e09
VS
415 wxMemoryOutputStream mstream((char*) m_pngData, m_pngSize);
416 image.SaveFile(mstream, wxBITMAP_TYPE_PNG);
0d2a2b60 417}
3f480da3 418
d613be55
RR
419// ----------------------------------------------------------------------------
420// wxURLDataObject
421// ----------------------------------------------------------------------------
422
c56fd7a3 423class wxTextURIListDataObject : public wxDataObjectSimple
d613be55 424{
c56fd7a3
VZ
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);
d613be55 468
c56fd7a3
VZ
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))
27781f63 493{
c56fd7a3
VZ
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);
d613be55
RR
498}
499
c56fd7a3 500void wxURLDataObject::SetURL(const wxString& url)
27781f63 501{
c56fd7a3
VZ
502 m_dobjURIList->SetURL(url);
503 m_dobjText->SetText(url);
d613be55
RR
504}
505
c56fd7a3 506wxString wxURLDataObject::GetURL() const
27781f63 507{
c56fd7a3
VZ
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 }
d613be55
RR
518}
519
14631f7f 520#endif // wxUSE_DATAOBJ