]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
83df96d6 JS |
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 | ||
daad5a23 | 77 | if (m_type == wxDF_TEXT || m_type == wxDF_UNICODETEXT) |
83df96d6 JS |
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 | { | |
70b8ab77 JS |
98 | #if wxUSE_NANOX |
99 | return wxEmptyString; | |
100 | #else | |
83df96d6 | 101 | char *t = XGetAtomName ((Display*) wxGetDisplay(), m_format); |
2b5f62a0 | 102 | wxString ret = wxString::FromAscii( t ); |
7520f3da | 103 | if (t) |
83df96d6 JS |
104 | XFree( t ); |
105 | return ret; | |
70b8ab77 | 106 | #endif |
83df96d6 JS |
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 | ||
a1eb65c2 | 126 | void wxDataFormat::SetId( const wxString& id ) |
83df96d6 | 127 | { |
70b8ab77 | 128 | #if !wxUSE_NANOX |
83df96d6 JS |
129 | PrepareFormats(); |
130 | m_type = wxDF_PRIVATE; | |
a1eb65c2 | 131 | m_format = XInternAtom( (Display*) wxGetDisplay(), id.ToAscii(), FALSE ); |
70b8ab77 | 132 | #endif |
83df96d6 JS |
133 | } |
134 | ||
135 | void wxDataFormat::PrepareFormats() | |
136 | { | |
70b8ab77 | 137 | #if !wxUSE_NANOX |
83df96d6 JS |
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) | |
9691c806 | 143 | g_fileAtom = XInternAtom( (Display*) wxGetDisplay(), "text/uri-list", FALSE ); |
70b8ab77 | 144 | #endif |
83df96d6 JS |
145 | } |
146 | ||
9691c806 RR |
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); | |
7520f3da | 158 | if ( nFormatCount == 1 ) |
9691c806 RR |
159 | { |
160 | return format == GetPreferredFormat(); | |
161 | } | |
7520f3da | 162 | else |
9691c806 RR |
163 | { |
164 | wxDataFormat *formats = new wxDataFormat[nFormatCount]; | |
165 | GetAllFormats(formats,dir); | |
166 | ||
167 | size_t n; | |
7520f3da | 168 | for ( n = 0; n < nFormatCount; n++ ) |
9691c806 RR |
169 | { |
170 | if ( formats[n] == format ) | |
171 | break; | |
172 | } | |
173 | ||
174 | delete [] formats; | |
175 | ||
176 | // found? | |
177 | return n < nFormatCount; | |
178 | } | |
179 | } | |
83df96d6 JS |
180 | |
181 | // ---------------------------------------------------------------------------- | |
9691c806 | 182 | // wxFileDataObject |
83df96d6 JS |
183 | // ---------------------------------------------------------------------------- |
184 | ||
9691c806 RR |
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 | ||
155ecd4c | 195 | memcpy( buf, filenames.mbc_str(), filenames.length() + 1 ); |
9691c806 | 196 | |
7520f3da | 197 | return true; |
9691c806 | 198 | } |
83df96d6 | 199 | |
9691c806 | 200 | size_t wxFileDataObject::GetDataSize() const |
83df96d6 | 201 | { |
9691c806 RR |
202 | size_t res = 0; |
203 | ||
204 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
205 | { | |
155ecd4c | 206 | res += m_filenames[i].length(); |
9691c806 RR |
207 | res += 1; |
208 | } | |
209 | ||
210 | return res + 1; | |
83df96d6 JS |
211 | } |
212 | ||
9691c806 | 213 | bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf) |
83df96d6 | 214 | { |
9691c806 RR |
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 ); | |
155ecd4c WS |
228 | pos += file.length()+1; |
229 | filenames += file.length()+1; | |
9691c806 RR |
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:") | |
9a83f860 | 246 | if ( filename.Left(lenPrefix).MakeLower() == wxT("file:") ) |
9691c806 RR |
247 | { |
248 | // sometimes the syntax is "file:filename", sometimes it's | |
249 | // URL-like: "file://filename" - deal with both | |
9a83f860 VZ |
250 | if ( filename[lenPrefix] == wxT('/') && |
251 | filename[lenPrefix + 1] == wxT('/') ) | |
9691c806 RR |
252 | { |
253 | // skip the slashes | |
254 | lenPrefix += 2; | |
255 | } | |
256 | ||
257 | AddFile(filename.c_str() + lenPrefix); | |
258 | filename.Empty(); | |
259 | } | |
260 | else | |
261 | { | |
9a83f860 | 262 | wxLogDebug(wxT("Unsupported URI '%s' in wxFileDataObject"), |
9691c806 RR |
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 | |
83df96d6 | 278 | |
7520f3da | 279 | return true; |
9691c806 | 280 | } |
83df96d6 | 281 | |
9691c806 RR |
282 | void wxFileDataObject::AddFile( const wxString &filename ) |
283 | { | |
284 | m_filenames.Add( filename ); | |
83df96d6 JS |
285 | } |
286 | ||
9691c806 RR |
287 | // ---------------------------------------------------------------------------- |
288 | // wxBitmapDataObject | |
289 | // ---------------------------------------------------------------------------- | |
290 | ||
291 | wxBitmapDataObject::wxBitmapDataObject() | |
83df96d6 | 292 | { |
9691c806 RR |
293 | Init(); |
294 | } | |
83df96d6 | 295 | |
9691c806 RR |
296 | wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap ) |
297 | : wxBitmapDataObjectBase(bitmap) | |
298 | { | |
299 | Init(); | |
83df96d6 | 300 | |
9691c806 | 301 | DoConvertToPng(); |
83df96d6 JS |
302 | } |
303 | ||
9691c806 | 304 | wxBitmapDataObject::~wxBitmapDataObject() |
83df96d6 | 305 | { |
9691c806 | 306 | Clear(); |
83df96d6 JS |
307 | } |
308 | ||
9691c806 | 309 | void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap ) |
83df96d6 | 310 | { |
9691c806 RR |
311 | ClearAll(); |
312 | ||
313 | wxBitmapDataObjectBase::SetBitmap(bitmap); | |
314 | ||
315 | DoConvertToPng(); | |
83df96d6 JS |
316 | } |
317 | ||
9691c806 | 318 | bool wxBitmapDataObject::GetDataHere(void *buf) const |
83df96d6 | 319 | { |
9691c806 RR |
320 | if ( !m_pngSize ) |
321 | { | |
322 | wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") ); | |
323 | ||
7520f3da | 324 | return false; |
9691c806 RR |
325 | } |
326 | ||
327 | memcpy(buf, m_pngData, m_pngSize); | |
328 | ||
7520f3da | 329 | return true; |
83df96d6 JS |
330 | } |
331 | ||
9691c806 RR |
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 | { | |
7520f3da | 347 | return false; |
9691c806 RR |
348 | } |
349 | ||
2b5f62a0 | 350 | m_bitmap = image; |
9691c806 RR |
351 | |
352 | return m_bitmap.Ok(); | |
353 | #else | |
7520f3da | 354 | return false; |
9691c806 RR |
355 | #endif |
356 | } | |
357 | ||
358 | void wxBitmapDataObject::DoConvertToPng() | |
359 | { | |
360 | #if wxUSE_LIBPNG | |
361 | if (!m_bitmap.Ok()) | |
362 | return; | |
363 | ||
2b5f62a0 | 364 | wxImage image = m_bitmap.ConvertToImage(); |
9691c806 RR |
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 | } | |
83df96d6 | 377 | |
55b5ddad | 378 | #endif // wxUSE_DATAOBJ |