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