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