]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dataobj.cpp
*** empty log message ***
[wxWidgets.git] / src / gtk1 / dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: dataobj.cpp
3 // Purpose: wxDataObject class
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "dataobj.h"
12 #endif
13
14 #include "wx/dataobj.h"
15 #include "wx/app.h"
16 #include "wx/debug.h"
17 #include "wx/mstream.h"
18 #include "wx/image.h"
19
20 #include "gdk/gdk.h"
21
22 //-------------------------------------------------------------------------
23 // global data
24 //-------------------------------------------------------------------------
25
26 GdkAtom g_textAtom = 0;
27 GdkAtom g_pngAtom = 0;
28 GdkAtom g_fileAtom = 0;
29
30 //-------------------------------------------------------------------------
31 // wxDataFormat
32 //-------------------------------------------------------------------------
33
34 wxDataFormat::wxDataFormat()
35 {
36 PrepareFormats();
37 m_type = wxDF_INVALID;
38 m_format = (GdkAtom) 0;
39 }
40
41 wxDataFormat::wxDataFormat( wxDataFormatId type )
42 {
43 PrepareFormats();
44 SetType( type );
45 }
46
47 wxDataFormat::wxDataFormat( const wxChar *id )
48 {
49 PrepareFormats();
50 SetId( id );
51 }
52
53 wxDataFormat::wxDataFormat( const wxString &id )
54 {
55 PrepareFormats();
56 SetId( id );
57 }
58
59 wxDataFormat::wxDataFormat( NativeFormat format )
60 {
61 PrepareFormats();
62 SetId( format );
63 }
64
65 void wxDataFormat::SetType( wxDataFormatId type )
66 {
67 m_type = type;
68
69 if (m_type == wxDF_TEXT)
70 m_format = g_textAtom;
71 else
72 if (m_type == wxDF_BITMAP)
73 m_format = g_pngAtom;
74 else
75 if (m_type == wxDF_FILENAME)
76 m_format = g_fileAtom;
77 else
78 {
79 wxFAIL_MSG( wxT("invalid dataformat") );
80 }
81 }
82
83 wxDataFormatId wxDataFormat::GetType() const
84 {
85 return m_type;
86 }
87
88 wxString wxDataFormat::GetId() const
89 {
90 wxString ret( gdk_atom_name( m_format ) ); // this will convert from ascii to Unicode
91 return ret;
92 }
93
94 void wxDataFormat::SetId( NativeFormat format )
95 {
96 m_format = format;
97
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;
108 }
109
110 void wxDataFormat::SetId( const wxChar *id )
111 {
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?
115 }
116
117 void wxDataFormat::PrepareFormats()
118 {
119 if (!g_textAtom)
120 g_textAtom = gdk_atom_intern( "STRING", FALSE );
121 if (!g_pngAtom)
122 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
123 if (!g_fileAtom)
124 g_fileAtom = gdk_atom_intern( "file:ALL", FALSE );
125 }
126
127 //-------------------------------------------------------------------------
128 // wxDataObject
129 //-------------------------------------------------------------------------
130
131 IMPLEMENT_ABSTRACT_CLASS( wxDataObject, wxObject )
132
133 wxDataObject::wxDataObject()
134 {
135 }
136
137 wxDataObject::~wxDataObject()
138 {
139 }
140
141 bool wxDataObject::IsSupportedFormat(const wxDataFormat& format) const
142 {
143 size_t nFormatCount = GetFormatCount();
144 if ( nFormatCount == 1 ) {
145 return format == GetPreferredFormat();
146 }
147 else {
148 wxDataFormat *formats = new wxDataFormat[nFormatCount];
149 GetAllFormats(formats);
150
151 size_t n;
152 for ( n = 0; n < nFormatCount; n++ ) {
153 if ( formats[n] == format )
154 break;
155 }
156
157 delete [] formats;
158
159 // found?
160 return n < nFormatCount;
161 }
162 }
163
164 // ----------------------------------------------------------------------------
165 // wxFileDataObject
166 // ----------------------------------------------------------------------------
167
168 bool wxFileDataObject::GetDataHere(void *buf) const
169 {
170 const wxString& filenames = GetFilenames();
171 memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 );
172
173 return TRUE;
174 }
175
176 size_t wxFileDataObject::GetDataSize() const
177 {
178 return GetFilenames().Len() + 1;
179 }
180
181 bool wxFileDataObject::SetData(const void *buf)
182 {
183 SetFilenames((const wxChar *)buf);
184
185 return TRUE;
186 }
187
188 // ----------------------------------------------------------------------------
189 // wxBitmapDataObject
190 // ----------------------------------------------------------------------------
191
192 wxBitmapDataObject::wxBitmapDataObject()
193 {
194 Init();
195 }
196
197 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
198 : wxBitmapDataObjectBase(bitmap)
199 {
200 Init();
201
202 DoConvertToPng();
203 }
204
205 wxBitmapDataObject::~wxBitmapDataObject()
206 {
207 Clear();
208 }
209
210 void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
211 {
212 ClearAll();
213
214 wxBitmapDataObjectBase::SetBitmap(bitmap);
215
216 DoConvertToPng();
217 }
218
219 bool wxBitmapDataObject::GetDataHere(void *buf) const
220 {
221 if ( !m_pngSize )
222 {
223 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
224
225 return FALSE;
226 }
227
228 memcpy(buf, m_pngData, m_pngSize);
229
230 return TRUE;
231 }
232
233 bool wxBitmapDataObject::SetData(size_t size, const void *buf)
234 {
235 Clear();
236
237 m_pngSize = size;
238 m_pngData = malloc(m_pngSize);
239
240 memcpy( m_pngData, buf, m_pngSize );
241
242 wxMemoryInputStream mstream( (char*) m_pngData, m_pngSize );
243 wxImage image;
244 wxPNGHandler handler;
245 if ( !handler.LoadFile( &image, mstream ) )
246 {
247 return FALSE;
248 }
249
250 m_bitmap = image.ConvertToBitmap();
251 }
252
253 void wxBitmapDataObject::DoConvertToPng()
254 {
255 if (!m_bitmap.Ok())
256 return;
257
258 wxImage image( m_bitmap );
259 wxPNGHandler handler;
260
261 wxCountingOutputStream count;
262 handler.SaveFile( &image, count );
263
264 m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
265 m_pngData = malloc(m_pngSize);
266
267 wxMemoryOutputStream mstream( (char*) m_pngData, m_pngSize );
268 handler.SaveFile( &image, mstream );
269 }
270
271 // ----------------------------------------------------------------------------
272 // wxPrivateDataObject
273 // ----------------------------------------------------------------------------
274
275 IMPLEMENT_CLASS( wxPrivateDataObject, wxDataObject )
276
277 void wxPrivateDataObject::Free()
278 {
279 if ( m_data )
280 free(m_data);
281 }
282
283 wxPrivateDataObject::wxPrivateDataObject()
284 {
285 wxString id = wxT("application/");
286 id += wxTheApp->GetAppName();
287
288 m_format.SetId( id );
289
290 m_size = 0;
291 m_data = (void *)NULL;
292 }
293
294 void wxPrivateDataObject::SetData( const void *data, size_t size )
295 {
296 Free();
297
298 m_size = size;
299 m_data = malloc(size);
300
301 memcpy( m_data, data, size );
302 }
303
304 void wxPrivateDataObject::WriteData( void *dest ) const
305 {
306 WriteData( m_data, dest );
307 }
308
309 size_t wxPrivateDataObject::GetSize() const
310 {
311 return m_size;
312 }
313
314 void wxPrivateDataObject::WriteData( const void *data, void *dest ) const
315 {
316 memcpy( dest, data, GetSize() );
317 }
318