]> git.saurik.com Git - wxWidgets.git/blob - src/x11/dataobj.cpp
wxStrdup(NULL) doesn't work
[wxWidgets.git] / src / x11 / dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: dataobj.cpp
3 // Purpose: wxDataObject class
4 // Author: Julian Smart
5 // Id: $Id$
6 // Copyright: (c) 1998 Julian Smart
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "dataobj.h"
12 #endif
13
14 #include "wx/defs.h"
15
16 #include "wx/dataobj.h"
17 #include "wx/mstream.h"
18 #include "wx/app.h"
19 #include "wx/image.h"
20 #include "wx/log.h"
21
22 #include "wx/utils.h"
23 #include "wx/x11/private.h"
24
25 //-------------------------------------------------------------------------
26 // global data
27 //-------------------------------------------------------------------------
28
29 Atom g_textAtom = 0;
30 Atom g_pngAtom = 0;
31 Atom g_fileAtom = 0;
32
33 //-------------------------------------------------------------------------
34 // wxDataFormat
35 //-------------------------------------------------------------------------
36
37 wxDataFormat::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
51 wxDataFormat::wxDataFormat( wxDataFormatId type )
52 {
53 PrepareFormats();
54 SetType( type );
55 }
56
57 wxDataFormat::wxDataFormat( const wxChar *id )
58 {
59 PrepareFormats();
60 SetId( id );
61 }
62
63 wxDataFormat::wxDataFormat( const wxString &id )
64 {
65 PrepareFormats();
66 SetId( id );
67 }
68
69 wxDataFormat::wxDataFormat( NativeFormat format )
70 {
71 PrepareFormats();
72 SetId( format );
73 }
74
75 void 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
94 wxDataFormatId wxDataFormat::GetType() const
95 {
96 return m_type;
97 }
98
99 wxString wxDataFormat::GetId() const
100 {
101 #if wxUSE_NANOX
102 return wxEmptyString;
103 #else
104 char *t = XGetAtomName ((Display*) wxGetDisplay(), m_format);
105 wxString ret = wxString::FromAscii( t );
106 if (t)
107 XFree( t );
108 return ret;
109 #endif
110 }
111
112 void 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
129 void wxDataFormat::SetId( const wxChar *id )
130 {
131 #if !wxUSE_NANOX
132 PrepareFormats();
133 m_type = wxDF_PRIVATE;
134 wxString tmp( id );
135 m_format = XInternAtom( (Display*) wxGetDisplay(), tmp.ToAscii(), FALSE );
136 #endif
137 }
138
139 void wxDataFormat::PrepareFormats()
140 {
141 #if !wxUSE_NANOX
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)
147 g_fileAtom = XInternAtom( (Display*) wxGetDisplay(), "text/uri-list", FALSE );
148 #endif
149 }
150
151 //-------------------------------------------------------------------------
152 // wxDataObject
153 //-------------------------------------------------------------------------
154
155 wxDataObject::wxDataObject()
156 {
157 }
158
159 bool 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 }
184
185 // ----------------------------------------------------------------------------
186 // wxFileDataObject
187 // ----------------------------------------------------------------------------
188
189 bool 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 }
203
204 size_t wxFileDataObject::GetDataSize() const
205 {
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;
215 }
216
217 bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf)
218 {
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
282
283 return TRUE;
284 }
285
286 void wxFileDataObject::AddFile( const wxString &filename )
287 {
288 m_filenames.Add( filename );
289 }
290
291 // ----------------------------------------------------------------------------
292 // wxBitmapDataObject
293 // ----------------------------------------------------------------------------
294
295 wxBitmapDataObject::wxBitmapDataObject()
296 {
297 Init();
298 }
299
300 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
301 : wxBitmapDataObjectBase(bitmap)
302 {
303 Init();
304
305 DoConvertToPng();
306 }
307
308 wxBitmapDataObject::~wxBitmapDataObject()
309 {
310 Clear();
311 }
312
313 void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
314 {
315 ClearAll();
316
317 wxBitmapDataObjectBase::SetBitmap(bitmap);
318
319 DoConvertToPng();
320 }
321
322 bool wxBitmapDataObject::GetDataHere(void *buf) const
323 {
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;
334 }
335
336 bool 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
354 m_bitmap = image;
355
356 return m_bitmap.Ok();
357 #else
358 return FALSE;
359 #endif
360 }
361
362 void wxBitmapDataObject::DoConvertToPng()
363 {
364 #if wxUSE_LIBPNG
365 if (!m_bitmap.Ok())
366 return;
367
368 wxImage image = m_bitmap.ConvertToImage();
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 }
381