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