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