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