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