]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/dataobj.cpp
no message
[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{
e2acb9ae 36 PrepareFormats();
cd5bf2a6 37 m_type = wxDF_INVALID;
1dd989e1 38 m_format = (GdkAtom) 0;
cd5bf2a6
RR
39}
40
3f480da3 41wxDataFormat::wxDataFormat( wxDataFormatId type )
cd5bf2a6 42{
e2acb9ae 43 PrepareFormats();
cd5bf2a6 44 SetType( type );
0d2a2b60
RR
45}
46
b5be07d4 47wxDataFormat::wxDataFormat( const wxChar *id )
8a126fcc 48{
e2acb9ae 49 PrepareFormats();
8a126fcc
RR
50 SetId( id );
51}
52
0d2a2b60
RR
53wxDataFormat::wxDataFormat( const wxString &id )
54{
e2acb9ae 55 PrepareFormats();
cd5bf2a6 56 SetId( id );
0d2a2b60
RR
57}
58
1dd989e1 59wxDataFormat::wxDataFormat( NativeFormat format )
0d2a2b60 60{
e2acb9ae 61 PrepareFormats();
1dd989e1 62 SetId( format );
0d2a2b60
RR
63}
64
3f480da3 65void wxDataFormat::SetType( wxDataFormatId type )
cd5bf2a6
RR
66{
67 m_type = type;
3f480da3 68
cd5bf2a6 69 if (m_type == wxDF_TEXT)
1dd989e1 70 m_format = g_textAtom;
cd5bf2a6
RR
71 else
72 if (m_type == wxDF_BITMAP)
1dd989e1 73 m_format = g_pngAtom;
cd5bf2a6
RR
74 else
75 if (m_type == wxDF_FILENAME)
1dd989e1 76 m_format = g_fileAtom;
cd5bf2a6
RR
77 else
78 {
223d09f6 79 wxFAIL_MSG( wxT("invalid dataformat") );
cd5bf2a6 80 }
cd5bf2a6 81}
3f480da3
VZ
82
83wxDataFormatId wxDataFormat::GetType() const
0d2a2b60
RR
84{
85 return m_type;
86}
87
88wxString wxDataFormat::GetId() const
89{
1dd989e1
RR
90 wxString ret( gdk_atom_name( m_format ) ); // this will convert from ascii to Unicode
91 return ret;
0d2a2b60 92}
3f480da3 93
1dd989e1 94void wxDataFormat::SetId( NativeFormat format )
3f480da3 95{
1dd989e1 96 m_format = format;
3f480da3 97
1dd989e1
RR
98 if (m_format == g_textAtom)
99 m_type = wxDF_TEXT;
100 else
101 if (m_format == g_pngAtom)
102 m_type = wxDF_BITMAP;
103 else
104 if (m_format == g_fileAtom)
105 m_type = wxDF_FILENAME;
106 else
107 m_type = wxDF_PRIVATE;
0d2a2b60 108}
3f480da3 109
1dd989e1 110void wxDataFormat::SetId( const wxChar *id )
0d2a2b60 111{
1dd989e1
RR
112 m_type = wxDF_PRIVATE;
113 wxString tmp( id );
114 m_format = gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE ); // what is the string cast for?
0d2a2b60 115}
3f480da3 116
1dd989e1 117void wxDataFormat::PrepareFormats()
0d2a2b60 118{
e1ee679c 119 if (!g_textAtom)
1dd989e1 120 g_textAtom = gdk_atom_intern( "STRING", FALSE );
e1ee679c 121 if (!g_pngAtom)
1dd989e1 122 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
e1ee679c 123 if (!g_fileAtom)
1dd989e1 124 g_fileAtom = gdk_atom_intern( "file:ALL", FALSE );
0d2a2b60 125}
8b53e5a2
RR
126
127//-------------------------------------------------------------------------
128// wxDataObject
129//-------------------------------------------------------------------------
130
0d2a2b60
RR
131wxDataObject::wxDataObject()
132{
0d2a2b60 133}
3f480da3 134
b068c4e8 135bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
0d2a2b60 136{
b068c4e8 137 size_t nFormatCount = GetFormatCount(dir);
1dd989e1
RR
138 if ( nFormatCount == 1 ) {
139 return format == GetPreferredFormat();
140 }
141 else {
142 wxDataFormat *formats = new wxDataFormat[nFormatCount];
b068c4e8 143 GetAllFormats(formats,dir);
1dd989e1
RR
144
145 size_t n;
146 for ( n = 0; n < nFormatCount; n++ ) {
147 if ( formats[n] == format )
148 break;
149 }
0d2a2b60 150
1dd989e1 151 delete [] formats;
cd5bf2a6 152
1dd989e1
RR
153 // found?
154 return n < nFormatCount;
155 }
0d2a2b60
RR
156}
157
8b53e5a2
RR
158// ----------------------------------------------------------------------------
159// wxFileDataObject
160// ----------------------------------------------------------------------------
161
e1ee679c 162bool wxFileDataObject::GetDataHere(void *buf) const
0d2a2b60 163{
b068c4e8
RR
164 wxString filenames;
165
166 for (size_t i = 0; i < m_filenames.GetCount(); i++)
167 {
168 filenames += m_filenames[i];
169 filenames += (wxChar) 0;
170 }
171
e1ee679c 172 memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 );
0d2a2b60 173
e1ee679c 174 return TRUE;
0d2a2b60 175}
3f480da3 176
e1ee679c 177size_t wxFileDataObject::GetDataSize() const
0d2a2b60 178{
b068c4e8
RR
179 size_t res = 0;
180
181 for (size_t i = 0; i < m_filenames.GetCount(); i++)
182 {
183 res += m_filenames[i].Len();
184 res += 1;
185 }
186
187 return res + 1;
0d2a2b60 188}
3f480da3 189
b068c4e8 190bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf)
0d2a2b60 191{
b068c4e8
RR
192 /* TODO */
193
194 wxString file( (const char *)buf ); /* char, not wxChar */
195
196 AddFile( file );
3f480da3 197
1dd989e1
RR
198 return TRUE;
199}
200
b068c4e8
RR
201void wxFileDataObject::AddFile( const wxString &filename )
202{
203 m_filenames.Add( filename );
204}
205
8b53e5a2
RR
206// ----------------------------------------------------------------------------
207// wxBitmapDataObject
208// ----------------------------------------------------------------------------
209
0d2a2b60
RR
210wxBitmapDataObject::wxBitmapDataObject()
211{
e1ee679c 212 Init();
0d2a2b60
RR
213}
214
215wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
e1ee679c 216 : wxBitmapDataObjectBase(bitmap)
0d2a2b60 217{
e1ee679c
VZ
218 Init();
219
e2acb9ae
RR
220 DoConvertToPng();
221}
222
223wxBitmapDataObject::~wxBitmapDataObject()
224{
e1ee679c 225 Clear();
0d2a2b60
RR
226}
227
228void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
229{
e1ee679c 230 ClearAll();
0d2a2b60 231
e1ee679c
VZ
232 wxBitmapDataObjectBase::SetBitmap(bitmap);
233
234 DoConvertToPng();
0d2a2b60
RR
235}
236
e1ee679c 237bool wxBitmapDataObject::GetDataHere(void *buf) const
0d2a2b60 238{
e1ee679c 239 if ( !m_pngSize )
1dd989e1 240 {
e1ee679c
VZ
241 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
242
243 return FALSE;
1dd989e1 244 }
0d2a2b60 245
e1ee679c
VZ
246 memcpy(buf, m_pngData, m_pngSize);
247
248 return TRUE;
e2acb9ae
RR
249}
250
e1ee679c 251bool wxBitmapDataObject::SetData(size_t size, const void *buf)
e2acb9ae 252{
e1ee679c
VZ
253 Clear();
254
1dd989e1 255 m_pngSize = size;
e1ee679c
VZ
256 m_pngData = malloc(m_pngSize);
257
1dd989e1 258 memcpy( m_pngData, buf, m_pngSize );
e1ee679c 259
1dd989e1 260 wxMemoryInputStream mstream( (char*) m_pngData, m_pngSize );
e2acb9ae
RR
261 wxImage image;
262 wxPNGHandler handler;
e1ee679c
VZ
263 if ( !handler.LoadFile( &image, mstream ) )
264 {
265 return FALSE;
266 }
267
e2acb9ae 268 m_bitmap = image.ConvertToBitmap();
b068c4e8
RR
269
270 return m_bitmap.Ok();
e2acb9ae
RR
271}
272
273void wxBitmapDataObject::DoConvertToPng()
274{
e1ee679c
VZ
275 if (!m_bitmap.Ok())
276 return;
277
e2acb9ae
RR
278 wxImage image( m_bitmap );
279 wxPNGHandler handler;
e1ee679c 280
e2acb9ae
RR
281 wxCountingOutputStream count;
282 handler.SaveFile( &image, count );
e1ee679c 283
e2acb9ae 284 m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
e1ee679c
VZ
285 m_pngData = malloc(m_pngSize);
286
1dd989e1 287 wxMemoryOutputStream mstream( (char*) m_pngData, m_pngSize );
e2acb9ae 288 handler.SaveFile( &image, mstream );
0d2a2b60 289}
3f480da3 290
0d2a2b60 291