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