]> git.saurik.com Git - wxWidgets.git/blame - src/x11/dataobj.cpp
Minor header cleaning.
[wxWidgets.git] / src / x11 / dataobj.cpp
CommitLineData
83df96d6 1///////////////////////////////////////////////////////////////////////////////
7520f3da 2// Name: src/x11/dataobj.cpp
83df96d6
JS
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
7520f3da
WS
10// for compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
83df96d6 12
55b5ddad
VZ
13#if wxUSE_DATAOBJ
14
83df96d6 15#include "wx/dataobj.h"
e4db172a
WS
16
17#ifndef WX_PRECOMP
18 #include "wx/log.h"
670f9935 19 #include "wx/app.h"
de6185e2 20 #include "wx/utils.h"
155ecd4c 21 #include "wx/image.h"
e4db172a
WS
22#endif
23
9691c806 24#include "wx/mstream.h"
83df96d6 25
7266b672 26#include "wx/x11/private.h"
83df96d6
JS
27
28//-------------------------------------------------------------------------
29// global data
30//-------------------------------------------------------------------------
31
32Atom g_textAtom = 0;
33Atom g_pngAtom = 0;
34Atom g_fileAtom = 0;
35
36//-------------------------------------------------------------------------
37// wxDataFormat
38//-------------------------------------------------------------------------
39
40wxDataFormat::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
54wxDataFormat::wxDataFormat( wxDataFormatId type )
55{
56 PrepareFormats();
57 SetType( type );
58}
59
60wxDataFormat::wxDataFormat( const wxChar *id )
61{
62 PrepareFormats();
63 SetId( id );
64}
65
66wxDataFormat::wxDataFormat( const wxString &id )
67{
68 PrepareFormats();
69 SetId( id );
70}
71
72wxDataFormat::wxDataFormat( NativeFormat format )
73{
74 PrepareFormats();
75 SetId( format );
76}
77
78void wxDataFormat::SetType( wxDataFormatId type )
79{
80 PrepareFormats();
81 m_type = type;
82
daad5a23 83 if (m_type == wxDF_TEXT || m_type == wxDF_UNICODETEXT)
83df96d6
JS
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
97wxDataFormatId wxDataFormat::GetType() const
98{
99 return m_type;
100}
101
102wxString wxDataFormat::GetId() const
103{
70b8ab77
JS
104#if wxUSE_NANOX
105 return wxEmptyString;
106#else
83df96d6 107 char *t = XGetAtomName ((Display*) wxGetDisplay(), m_format);
2b5f62a0 108 wxString ret = wxString::FromAscii( t );
7520f3da 109 if (t)
83df96d6
JS
110 XFree( t );
111 return ret;
70b8ab77 112#endif
83df96d6
JS
113}
114
115void 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
132void wxDataFormat::SetId( const wxChar *id )
133{
70b8ab77 134#if !wxUSE_NANOX
83df96d6
JS
135 PrepareFormats();
136 m_type = wxDF_PRIVATE;
137 wxString tmp( id );
7520f3da 138 m_format = XInternAtom( (Display*) wxGetDisplay(), tmp.ToAscii(), FALSE );
70b8ab77 139#endif
83df96d6
JS
140}
141
142void wxDataFormat::PrepareFormats()
143{
70b8ab77 144#if !wxUSE_NANOX
83df96d6
JS
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)
9691c806 150 g_fileAtom = XInternAtom( (Display*) wxGetDisplay(), "text/uri-list", FALSE );
70b8ab77 151#endif
83df96d6
JS
152}
153
9691c806
RR
154//-------------------------------------------------------------------------
155// wxDataObject
156//-------------------------------------------------------------------------
157
158wxDataObject::wxDataObject()
159{
160}
161
162bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
163{
164 size_t nFormatCount = GetFormatCount(dir);
7520f3da 165 if ( nFormatCount == 1 )
9691c806
RR
166 {
167 return format == GetPreferredFormat();
168 }
7520f3da 169 else
9691c806
RR
170 {
171 wxDataFormat *formats = new wxDataFormat[nFormatCount];
172 GetAllFormats(formats,dir);
173
174 size_t n;
7520f3da 175 for ( n = 0; n < nFormatCount; n++ )
9691c806
RR
176 {
177 if ( formats[n] == format )
178 break;
179 }
180
181 delete [] formats;
182
183 // found?
184 return n < nFormatCount;
185 }
186}
83df96d6
JS
187
188// ----------------------------------------------------------------------------
9691c806 189// wxFileDataObject
83df96d6
JS
190// ----------------------------------------------------------------------------
191
9691c806
RR
192bool 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
155ecd4c 202 memcpy( buf, filenames.mbc_str(), filenames.length() + 1 );
9691c806 203
7520f3da 204 return true;
9691c806 205}
83df96d6 206
9691c806 207size_t wxFileDataObject::GetDataSize() const
83df96d6 208{
9691c806
RR
209 size_t res = 0;
210
211 for (size_t i = 0; i < m_filenames.GetCount(); i++)
212 {
155ecd4c 213 res += m_filenames[i].length();
9691c806
RR
214 res += 1;
215 }
216
217 return res + 1;
83df96d6
JS
218}
219
9691c806 220bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf)
83df96d6 221{
9691c806
RR
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 );
155ecd4c
WS
235 pos += file.length()+1;
236 filenames += file.length()+1;
9691c806
RR
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
83df96d6 285
7520f3da 286 return true;
9691c806 287}
83df96d6 288
9691c806
RR
289void wxFileDataObject::AddFile( const wxString &filename )
290{
291 m_filenames.Add( filename );
83df96d6
JS
292}
293
9691c806
RR
294// ----------------------------------------------------------------------------
295// wxBitmapDataObject
296// ----------------------------------------------------------------------------
297
298wxBitmapDataObject::wxBitmapDataObject()
83df96d6 299{
9691c806
RR
300 Init();
301}
83df96d6 302
9691c806
RR
303wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
304 : wxBitmapDataObjectBase(bitmap)
305{
306 Init();
83df96d6 307
9691c806 308 DoConvertToPng();
83df96d6
JS
309}
310
9691c806 311wxBitmapDataObject::~wxBitmapDataObject()
83df96d6 312{
9691c806 313 Clear();
83df96d6
JS
314}
315
9691c806 316void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
83df96d6 317{
9691c806
RR
318 ClearAll();
319
320 wxBitmapDataObjectBase::SetBitmap(bitmap);
321
322 DoConvertToPng();
83df96d6
JS
323}
324
9691c806 325bool wxBitmapDataObject::GetDataHere(void *buf) const
83df96d6 326{
9691c806
RR
327 if ( !m_pngSize )
328 {
329 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
330
7520f3da 331 return false;
9691c806
RR
332 }
333
334 memcpy(buf, m_pngData, m_pngSize);
335
7520f3da 336 return true;
83df96d6
JS
337}
338
9691c806
RR
339bool 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 {
7520f3da 354 return false;
9691c806
RR
355 }
356
2b5f62a0 357 m_bitmap = image;
9691c806
RR
358
359 return m_bitmap.Ok();
360#else
7520f3da 361 return false;
9691c806
RR
362#endif
363}
364
365void wxBitmapDataObject::DoConvertToPng()
366{
367#if wxUSE_LIBPNG
368 if (!m_bitmap.Ok())
369 return;
370
2b5f62a0 371 wxImage image = m_bitmap.ConvertToImage();
9691c806
RR
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}
83df96d6 384
55b5ddad 385#endif // wxUSE_DATAOBJ