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