]> git.saurik.com Git - wxWidgets.git/blob - src/x11/dataobj.cpp
Increase the number of visible OS X combo items.
[wxWidgets.git] / src / x11 / dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/dataobj.cpp
3 // Purpose: wxDataObject class
4 // Author: Julian Smart
5 // Copyright: (c) 1998 Julian Smart
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
8
9 // for compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11
12 #if wxUSE_DATAOBJ
13
14 #include "wx/dataobj.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/log.h"
18 #include "wx/app.h"
19 #include "wx/utils.h"
20 #include "wx/image.h"
21 #endif
22
23 #include "wx/mstream.h"
24
25 #include "wx/x11/private.h"
26
27 //-------------------------------------------------------------------------
28 // global data
29 //-------------------------------------------------------------------------
30
31 Atom g_textAtom = 0;
32 Atom g_pngAtom = 0;
33 Atom g_fileAtom = 0;
34
35 //-------------------------------------------------------------------------
36 // wxDataFormat
37 //-------------------------------------------------------------------------
38
39 wxDataFormat::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
53 wxDataFormat::wxDataFormat( wxDataFormatId type )
54 {
55 PrepareFormats();
56 SetType( type );
57 }
58
59 wxDataFormat::wxDataFormat( const wxString &id )
60 {
61 PrepareFormats();
62 SetId( id );
63 }
64
65 wxDataFormat::wxDataFormat( NativeFormat format )
66 {
67 PrepareFormats();
68 SetId( format );
69 }
70
71 void wxDataFormat::SetType( wxDataFormatId type )
72 {
73 PrepareFormats();
74 m_type = type;
75
76 if (m_type == wxDF_TEXT || m_type == wxDF_UNICODETEXT)
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
90 wxDataFormatId wxDataFormat::GetType() const
91 {
92 return m_type;
93 }
94
95 wxString wxDataFormat::GetId() const
96 {
97 #if wxUSE_NANOX
98 return wxEmptyString;
99 #else
100 char *t = XGetAtomName ((Display*) wxGetDisplay(), m_format);
101 wxString ret = wxString::FromAscii( t );
102 if (t)
103 XFree( t );
104 return ret;
105 #endif
106 }
107
108 void 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
125 void wxDataFormat::SetId( const wxString& id )
126 {
127 #if !wxUSE_NANOX
128 PrepareFormats();
129 m_type = wxDF_PRIVATE;
130 m_format = XInternAtom( (Display*) wxGetDisplay(), id.ToAscii(), FALSE );
131 #endif
132 }
133
134 void wxDataFormat::PrepareFormats()
135 {
136 #if !wxUSE_NANOX
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)
142 g_fileAtom = XInternAtom( (Display*) wxGetDisplay(), "text/uri-list", FALSE );
143 #endif
144 }
145
146 //-------------------------------------------------------------------------
147 // wxDataObject
148 //-------------------------------------------------------------------------
149
150 wxDataObject::wxDataObject()
151 {
152 }
153
154 bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
155 {
156 size_t nFormatCount = GetFormatCount(dir);
157 if ( nFormatCount == 1 )
158 {
159 return format == GetPreferredFormat();
160 }
161 else
162 {
163 wxDataFormat *formats = new wxDataFormat[nFormatCount];
164 GetAllFormats(formats,dir);
165
166 size_t n;
167 for ( n = 0; n < nFormatCount; n++ )
168 {
169 if ( formats[n] == format )
170 break;
171 }
172
173 delete [] formats;
174
175 // found?
176 return n < nFormatCount;
177 }
178 }
179
180 // ----------------------------------------------------------------------------
181 // wxFileDataObject
182 // ----------------------------------------------------------------------------
183
184 bool 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
194 memcpy( buf, filenames.mbc_str(), filenames.length() + 1 );
195
196 return true;
197 }
198
199 size_t wxFileDataObject::GetDataSize() const
200 {
201 size_t res = 0;
202
203 for (size_t i = 0; i < m_filenames.GetCount(); i++)
204 {
205 res += m_filenames[i].length();
206 res += 1;
207 }
208
209 return res + 1;
210 }
211
212 bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf)
213 {
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 );
227 pos += file.length()+1;
228 filenames += file.length()+1;
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:")
245 if ( filename.Left(lenPrefix).MakeLower() == wxT("file:") )
246 {
247 // sometimes the syntax is "file:filename", sometimes it's
248 // URL-like: "file://filename" - deal with both
249 if ( filename[lenPrefix] == wxT('/') &&
250 filename[lenPrefix + 1] == wxT('/') )
251 {
252 // skip the slashes
253 lenPrefix += 2;
254 }
255
256 AddFile(filename.c_str() + lenPrefix);
257 filename.Empty();
258 }
259 else
260 {
261 wxLogDebug(wxT("Unsupported URI '%s' in wxFileDataObject"),
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
277
278 return true;
279 }
280
281 void wxFileDataObject::AddFile( const wxString &filename )
282 {
283 m_filenames.Add( filename );
284 }
285
286 // ----------------------------------------------------------------------------
287 // wxBitmapDataObject
288 // ----------------------------------------------------------------------------
289
290 wxBitmapDataObject::wxBitmapDataObject()
291 {
292 Init();
293 }
294
295 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
296 : wxBitmapDataObjectBase(bitmap)
297 {
298 Init();
299
300 DoConvertToPng();
301 }
302
303 wxBitmapDataObject::~wxBitmapDataObject()
304 {
305 Clear();
306 }
307
308 void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
309 {
310 ClearAll();
311
312 wxBitmapDataObjectBase::SetBitmap(bitmap);
313
314 DoConvertToPng();
315 }
316
317 bool wxBitmapDataObject::GetDataHere(void *buf) const
318 {
319 if ( !m_pngSize )
320 {
321 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
322
323 return false;
324 }
325
326 memcpy(buf, m_pngData, m_pngSize);
327
328 return true;
329 }
330
331 bool 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 {
346 return false;
347 }
348
349 m_bitmap = image;
350
351 return m_bitmap.IsOk();
352 #else
353 return false;
354 #endif
355 }
356
357 void wxBitmapDataObject::DoConvertToPng()
358 {
359 #if wxUSE_LIBPNG
360 if (!m_bitmap.IsOk())
361 return;
362
363 wxImage image = m_bitmap.ConvertToImage();
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 }
376
377 #endif // wxUSE_DATAOBJ