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