]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/dataobj.cpp
some fixes and speedups
[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
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
RR
19
20#include "gdk/gdk.h"
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
VZ
129 // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the
130 // atoms STRING and file:ALL as the old code was
e1ee679c 131 if (!g_textAtom)
810b5e1f 132 g_textAtom = gdk_atom_intern( "text/plain", FALSE );
e1ee679c 133 if (!g_pngAtom)
1dd989e1 134 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
e1ee679c 135 if (!g_fileAtom)
810b5e1f 136 g_fileAtom = gdk_atom_intern( "text/uri-list", FALSE );
0d2a2b60 137}
8b53e5a2
RR
138
139//-------------------------------------------------------------------------
140// wxDataObject
141//-------------------------------------------------------------------------
142
0d2a2b60
RR
143wxDataObject::wxDataObject()
144{
0d2a2b60 145}
3f480da3 146
b068c4e8 147bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
0d2a2b60 148{
b068c4e8 149 size_t nFormatCount = GetFormatCount(dir);
1dd989e1
RR
150 if ( nFormatCount == 1 ) {
151 return format == GetPreferredFormat();
152 }
153 else {
154 wxDataFormat *formats = new wxDataFormat[nFormatCount];
b068c4e8 155 GetAllFormats(formats,dir);
1dd989e1
RR
156
157 size_t n;
158 for ( n = 0; n < nFormatCount; n++ ) {
159 if ( formats[n] == format )
160 break;
161 }
0d2a2b60 162
1dd989e1 163 delete [] formats;
cd5bf2a6 164
1dd989e1
RR
165 // found?
166 return n < nFormatCount;
167 }
0d2a2b60
RR
168}
169
8b53e5a2
RR
170// ----------------------------------------------------------------------------
171// wxFileDataObject
172// ----------------------------------------------------------------------------
173
e1ee679c 174bool wxFileDataObject::GetDataHere(void *buf) const
0d2a2b60 175{
b068c4e8 176 wxString filenames;
4b3d29db 177
b068c4e8
RR
178 for (size_t i = 0; i < m_filenames.GetCount(); i++)
179 {
180 filenames += m_filenames[i];
4b3d29db 181 filenames += (wxChar) 0;
b068c4e8 182 }
4b3d29db 183
e1ee679c 184 memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 );
0d2a2b60 185
e1ee679c 186 return TRUE;
0d2a2b60 187}
3f480da3 188
e1ee679c 189size_t wxFileDataObject::GetDataSize() const
0d2a2b60 190{
b068c4e8 191 size_t res = 0;
4b3d29db 192
b068c4e8
RR
193 for (size_t i = 0; i < m_filenames.GetCount(); i++)
194 {
195 res += m_filenames[i].Len();
4b3d29db 196 res += 1;
b068c4e8 197 }
4b3d29db 198
b068c4e8 199 return res + 1;
0d2a2b60 200}
3f480da3 201
2d68e1b4 202bool wxFileDataObject::SetData(size_t size, const void *buf)
0d2a2b60 203{
810b5e1f
VZ
204 // VZ: old format
205#if 0
2d68e1b4 206 // filenames are stores as a string with #0 as deliminators
2d68e1b4
RR
207 const char *filenames = (const char*) buf;
208 size_t pos = 0;
209 for(;;)
210 {
211 if (filenames[0] == 0)
810b5e1f
VZ
212 break;
213 if (pos >= size)
214 break;
2d68e1b4
RR
215 wxString file( filenames ); // this returns the first file
216 AddFile( file );
810b5e1f
VZ
217 pos += file.Len()+1;
218 filenames += file.Len()+1;
219 }
220#else // 1
221 m_filenames.Empty();
222
223 // the text/uri-list format is a sequence of URIs (filenames prefixed by
224 // "file:" as far as I see) delimited by "\r\n" of total length size
225 // (I wonder what happens if the file has '\n' in its filename??)
226 wxString filename;
227 for ( const char *p = (const char *)buf; *p; p++ )
228 {
229 if ( *p == '\r' && *(p+1) == '\n' )
230 {
231 static const int lenPrefix = 5; // strlen("file:")
232 if ( filename.Left(lenPrefix).MakeLower() == _T("file:") )
233 {
234 filename.erase(0, lenPrefix);
235 }
236
237 AddFile(filename);
238 filename.Empty();
239
240 // skip '\r'
241 p++;
242 }
243 else
244 {
245 filename += *p;
246 }
2d68e1b4 247 }
810b5e1f 248#endif // 0/1
3f480da3 249
1dd989e1
RR
250 return TRUE;
251}
252
b068c4e8
RR
253void wxFileDataObject::AddFile( const wxString &filename )
254{
255 m_filenames.Add( filename );
256}
257
8b53e5a2
RR
258// ----------------------------------------------------------------------------
259// wxBitmapDataObject
260// ----------------------------------------------------------------------------
261
0d2a2b60
RR
262wxBitmapDataObject::wxBitmapDataObject()
263{
e1ee679c 264 Init();
0d2a2b60
RR
265}
266
267wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
e1ee679c 268 : wxBitmapDataObjectBase(bitmap)
0d2a2b60 269{
e1ee679c
VZ
270 Init();
271
e2acb9ae
RR
272 DoConvertToPng();
273}
274
275wxBitmapDataObject::~wxBitmapDataObject()
276{
e1ee679c 277 Clear();
0d2a2b60
RR
278}
279
280void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
281{
e1ee679c 282 ClearAll();
0d2a2b60 283
e1ee679c
VZ
284 wxBitmapDataObjectBase::SetBitmap(bitmap);
285
286 DoConvertToPng();
0d2a2b60
RR
287}
288
e1ee679c 289bool wxBitmapDataObject::GetDataHere(void *buf) const
0d2a2b60 290{
e1ee679c 291 if ( !m_pngSize )
1dd989e1 292 {
e1ee679c
VZ
293 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
294
295 return FALSE;
1dd989e1 296 }
0d2a2b60 297
e1ee679c
VZ
298 memcpy(buf, m_pngData, m_pngSize);
299
300 return TRUE;
e2acb9ae
RR
301}
302
e1ee679c 303bool wxBitmapDataObject::SetData(size_t size, const void *buf)
e2acb9ae 304{
e1ee679c
VZ
305 Clear();
306
1dd989e1 307 m_pngSize = size;
e1ee679c
VZ
308 m_pngData = malloc(m_pngSize);
309
1dd989e1 310 memcpy( m_pngData, buf, m_pngSize );
e1ee679c 311
1dd989e1 312 wxMemoryInputStream mstream( (char*) m_pngData, m_pngSize );
e2acb9ae
RR
313 wxImage image;
314 wxPNGHandler handler;
e1ee679c
VZ
315 if ( !handler.LoadFile( &image, mstream ) )
316 {
317 return FALSE;
318 }
319
e2acb9ae 320 m_bitmap = image.ConvertToBitmap();
4b3d29db 321
b068c4e8 322 return m_bitmap.Ok();
e2acb9ae
RR
323}
324
325void wxBitmapDataObject::DoConvertToPng()
326{
e1ee679c
VZ
327 if (!m_bitmap.Ok())
328 return;
329
e2acb9ae
RR
330 wxImage image( m_bitmap );
331 wxPNGHandler handler;
e1ee679c 332
e2acb9ae
RR
333 wxCountingOutputStream count;
334 handler.SaveFile( &image, count );
e1ee679c 335
e2acb9ae 336 m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
e1ee679c
VZ
337 m_pngData = malloc(m_pngSize);
338
1dd989e1 339 wxMemoryOutputStream mstream( (char*) m_pngData, m_pngSize );
e2acb9ae 340 handler.SaveFile( &image, mstream );
0d2a2b60 341}
3f480da3 342
0d2a2b60 343