]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/dataobj.cpp | |
3 | // Purpose: wxDataObject class | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
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/image.h" | |
21 | #endif | |
22 | ||
23 | #include "wx/mstream.h" | |
24 | #include "wx/uri.h" | |
25 | ||
26 | #include "wx/gtk/private.h" | |
27 | ||
28 | //------------------------------------------------------------------------- | |
29 | // global data | |
30 | //------------------------------------------------------------------------- | |
31 | ||
32 | GdkAtom g_textAtom = 0; | |
33 | GdkAtom g_altTextAtom = 0; | |
34 | GdkAtom g_pngAtom = 0; | |
35 | GdkAtom g_fileAtom = 0; | |
36 | GdkAtom g_htmlAtom = 0; | |
37 | ||
38 | //------------------------------------------------------------------------- | |
39 | // wxDataFormat | |
40 | //------------------------------------------------------------------------- | |
41 | ||
42 | wxDataFormat::wxDataFormat() | |
43 | { | |
44 | // do *not* call PrepareFormats() from here for 2 reasons: | |
45 | // | |
46 | // 1. we will have time to do it later because some other Set function | |
47 | // must be called before we really need them | |
48 | // | |
49 | // 2. doing so prevents us from declaring global wxDataFormats because | |
50 | // calling PrepareFormats (and thus gdk_atom_intern) before GDK is | |
51 | // initialised will result in a crash | |
52 | m_type = wxDF_INVALID; | |
53 | m_format = (GdkAtom) 0; | |
54 | } | |
55 | ||
56 | wxDataFormat::wxDataFormat( wxDataFormatId type ) | |
57 | { | |
58 | PrepareFormats(); | |
59 | SetType( type ); | |
60 | } | |
61 | ||
62 | void wxDataFormat::InitFromString( const wxString &id ) | |
63 | { | |
64 | PrepareFormats(); | |
65 | SetId( id ); | |
66 | } | |
67 | ||
68 | wxDataFormat::wxDataFormat( NativeFormat format ) | |
69 | { | |
70 | PrepareFormats(); | |
71 | SetId( format ); | |
72 | } | |
73 | ||
74 | void wxDataFormat::SetType( wxDataFormatId type ) | |
75 | { | |
76 | PrepareFormats(); | |
77 | ||
78 | m_type = type; | |
79 | ||
80 | #if wxUSE_UNICODE | |
81 | if (m_type == wxDF_UNICODETEXT) | |
82 | m_format = g_textAtom; | |
83 | else if (m_type == wxDF_TEXT) | |
84 | m_format = g_altTextAtom; | |
85 | #else // !wxUSE_UNICODE | |
86 | // notice that we don't map wxDF_UNICODETEXT to g_textAtom here, this | |
87 | // would lead the code elsewhere to treat data objects with this format as | |
88 | // containing UTF-8 data which is not true | |
89 | if (m_type == wxDF_TEXT) | |
90 | m_format = g_textAtom; | |
91 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE | |
92 | else | |
93 | if (m_type == wxDF_BITMAP) | |
94 | m_format = g_pngAtom; | |
95 | else | |
96 | if (m_type == wxDF_FILENAME) | |
97 | m_format = g_fileAtom; | |
98 | else | |
99 | if (m_type == wxDF_HTML) | |
100 | m_format = g_htmlAtom; | |
101 | else | |
102 | { | |
103 | wxFAIL_MSG( wxT("invalid dataformat") ); | |
104 | } | |
105 | } | |
106 | ||
107 | wxDataFormatId wxDataFormat::GetType() const | |
108 | { | |
109 | return m_type; | |
110 | } | |
111 | ||
112 | wxString wxDataFormat::GetId() const | |
113 | { | |
114 | wxGtkString atom_name(gdk_atom_name(m_format)); | |
115 | return wxString::FromAscii(atom_name); | |
116 | } | |
117 | ||
118 | void wxDataFormat::SetId( NativeFormat format ) | |
119 | { | |
120 | PrepareFormats(); | |
121 | m_format = format; | |
122 | ||
123 | if (m_format == g_textAtom) | |
124 | #if wxUSE_UNICODE | |
125 | m_type = wxDF_UNICODETEXT; | |
126 | #else | |
127 | m_type = wxDF_TEXT; | |
128 | #endif | |
129 | else | |
130 | if (m_format == g_altTextAtom) | |
131 | m_type = wxDF_TEXT; | |
132 | else | |
133 | if (m_format == g_pngAtom) | |
134 | m_type = wxDF_BITMAP; | |
135 | else | |
136 | if (m_format == g_fileAtom) | |
137 | m_type = wxDF_FILENAME; | |
138 | else | |
139 | if (m_format == g_htmlAtom) | |
140 | m_type = wxDF_HTML; | |
141 | else | |
142 | m_type = wxDF_PRIVATE; | |
143 | } | |
144 | ||
145 | void wxDataFormat::SetId( const wxString& id ) | |
146 | { | |
147 | PrepareFormats(); | |
148 | m_type = wxDF_PRIVATE; | |
149 | m_format = gdk_atom_intern( id.ToAscii(), FALSE ); | |
150 | } | |
151 | ||
152 | void wxDataFormat::PrepareFormats() | |
153 | { | |
154 | // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the | |
155 | // atoms STRING and file:ALL as the old code was, but normal X apps | |
156 | // use STRING for text selection when transferring the data via | |
157 | // clipboard, for example, so do use STRING for now (GNOME apps will | |
158 | // probably support STRING as well for compatibility anyhow), but use | |
159 | // text/uri-list for file dnd because compatibility is not important | |
160 | // here (with whom?) | |
161 | if (!g_textAtom) | |
162 | { | |
163 | #if wxUSE_UNICODE | |
164 | g_textAtom = gdk_atom_intern( "UTF8_STRING", FALSE ); | |
165 | g_altTextAtom = gdk_atom_intern( "STRING", FALSE ); | |
166 | #else | |
167 | g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE ); | |
168 | #endif | |
169 | } | |
170 | if (!g_pngAtom) | |
171 | g_pngAtom = gdk_atom_intern( "image/png", FALSE ); | |
172 | if (!g_fileAtom) | |
173 | g_fileAtom = gdk_atom_intern( "text/uri-list", FALSE ); | |
174 | if (!g_htmlAtom) | |
175 | g_htmlAtom = gdk_atom_intern( "text/html", FALSE ); | |
176 | } | |
177 | ||
178 | //------------------------------------------------------------------------- | |
179 | // wxDataObject | |
180 | //------------------------------------------------------------------------- | |
181 | ||
182 | wxDataObject::wxDataObject() | |
183 | { | |
184 | } | |
185 | ||
186 | wxDataObject::~wxDataObject() | |
187 | { | |
188 | // dtor is empty but needed for Darwin and AIX -- otherwise it doesn't link | |
189 | } | |
190 | ||
191 | bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const | |
192 | { | |
193 | size_t nFormatCount = GetFormatCount(dir); | |
194 | if ( nFormatCount == 1 ) | |
195 | { | |
196 | return format == GetPreferredFormat(); | |
197 | } | |
198 | else | |
199 | { | |
200 | wxDataFormat *formats = new wxDataFormat[nFormatCount]; | |
201 | GetAllFormats(formats,dir); | |
202 | ||
203 | size_t n; | |
204 | for ( n = 0; n < nFormatCount; n++ ) | |
205 | { | |
206 | if ( formats[n] == format ) | |
207 | break; | |
208 | } | |
209 | ||
210 | delete [] formats; | |
211 | ||
212 | // found? | |
213 | return n < nFormatCount; | |
214 | } | |
215 | } | |
216 | ||
217 | // ---------------------------------------------------------------------------- | |
218 | // wxTextDataObject | |
219 | // ---------------------------------------------------------------------------- | |
220 | ||
221 | #if wxUSE_UNICODE | |
222 | ||
223 | void | |
224 | wxTextDataObject::GetAllFormats(wxDataFormat *formats, | |
225 | wxDataObjectBase::Direction WXUNUSED(dir)) const | |
226 | { | |
227 | *formats++ = GetPreferredFormat(); | |
228 | *formats = g_altTextAtom; | |
229 | } | |
230 | ||
231 | #endif // wxUSE_UNICODE | |
232 | ||
233 | // ---------------------------------------------------------------------------- | |
234 | // wxFileDataObject | |
235 | // ---------------------------------------------------------------------------- | |
236 | ||
237 | bool wxFileDataObject::GetDataHere(void *buf) const | |
238 | { | |
239 | wxString filenames; | |
240 | ||
241 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
242 | { | |
243 | filenames += wxT("file:"); | |
244 | filenames += m_filenames[i]; | |
245 | filenames += wxT("\r\n"); | |
246 | } | |
247 | ||
248 | memcpy( buf, filenames.mbc_str(), filenames.length() + 1 ); | |
249 | ||
250 | return true; | |
251 | } | |
252 | ||
253 | size_t wxFileDataObject::GetDataSize() const | |
254 | { | |
255 | size_t res = 0; | |
256 | ||
257 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
258 | { | |
259 | // This is junk in UTF-8 | |
260 | res += m_filenames[i].length(); | |
261 | res += 5 + 2; // "file:" (5) + "\r\n" (2) | |
262 | } | |
263 | ||
264 | return res + 1; | |
265 | } | |
266 | ||
267 | bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf) | |
268 | { | |
269 | // we get data in the text/uri-list format, i.e. as a sequence of URIs | |
270 | // (filenames prefixed by "file:") delimited by "\r\n". size includes | |
271 | // the trailing zero (in theory, not for Nautilus in early GNOME | |
272 | // versions). | |
273 | ||
274 | m_filenames.Empty(); | |
275 | ||
276 | const gchar *nexttemp = (const gchar*) buf; | |
277 | for ( ; ; ) | |
278 | { | |
279 | int len = 0; | |
280 | const gchar *temp = nexttemp; | |
281 | for (;;) | |
282 | { | |
283 | if (temp[len] == 0) | |
284 | { | |
285 | if (len > 0) | |
286 | { | |
287 | // if an app omits '\r''\n' | |
288 | nexttemp = temp+len; | |
289 | break; | |
290 | } | |
291 | ||
292 | return true; | |
293 | } | |
294 | if (temp[len] == '\r') | |
295 | { | |
296 | if (temp[len+1] == '\n') | |
297 | nexttemp = temp+len+2; | |
298 | else | |
299 | nexttemp = temp+len+1; | |
300 | break; | |
301 | } | |
302 | len++; | |
303 | } | |
304 | ||
305 | if (len == 0) | |
306 | break; | |
307 | ||
308 | // required to give it a trailing zero | |
309 | gchar *uri = g_strndup( temp, len ); | |
310 | ||
311 | gchar *fn = g_filename_from_uri( uri, NULL, NULL ); | |
312 | ||
313 | g_free( uri ); | |
314 | ||
315 | if (fn) | |
316 | { | |
317 | AddFile( wxConvFileName->cMB2WX( fn ) ); | |
318 | g_free( fn ); | |
319 | } | |
320 | } | |
321 | ||
322 | return true; | |
323 | } | |
324 | ||
325 | void wxFileDataObject::AddFile( const wxString &filename ) | |
326 | { | |
327 | m_filenames.Add( filename ); | |
328 | } | |
329 | ||
330 | // ---------------------------------------------------------------------------- | |
331 | // wxBitmapDataObject | |
332 | // ---------------------------------------------------------------------------- | |
333 | ||
334 | wxBitmapDataObject::wxBitmapDataObject() | |
335 | { | |
336 | Init(); | |
337 | } | |
338 | ||
339 | wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap ) | |
340 | : wxBitmapDataObjectBase(bitmap) | |
341 | { | |
342 | Init(); | |
343 | ||
344 | DoConvertToPng(); | |
345 | } | |
346 | ||
347 | wxBitmapDataObject::~wxBitmapDataObject() | |
348 | { | |
349 | Clear(); | |
350 | } | |
351 | ||
352 | void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap ) | |
353 | { | |
354 | ClearAll(); | |
355 | ||
356 | wxBitmapDataObjectBase::SetBitmap(bitmap); | |
357 | ||
358 | DoConvertToPng(); | |
359 | } | |
360 | ||
361 | bool wxBitmapDataObject::GetDataHere(void *buf) const | |
362 | { | |
363 | if ( !m_pngSize ) | |
364 | { | |
365 | wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") ); | |
366 | ||
367 | return false; | |
368 | } | |
369 | ||
370 | memcpy(buf, m_pngData, m_pngSize); | |
371 | ||
372 | return true; | |
373 | } | |
374 | ||
375 | bool wxBitmapDataObject::SetData(size_t size, const void *buf) | |
376 | { | |
377 | Clear(); | |
378 | ||
379 | wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL, | |
380 | false, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") ); | |
381 | ||
382 | m_pngSize = size; | |
383 | m_pngData = malloc(m_pngSize); | |
384 | ||
385 | memcpy(m_pngData, buf, m_pngSize); | |
386 | ||
387 | wxMemoryInputStream mstream((char*) m_pngData, m_pngSize); | |
388 | wxImage image; | |
389 | if ( !image.LoadFile( mstream, wxBITMAP_TYPE_PNG ) ) | |
390 | { | |
391 | return false; | |
392 | } | |
393 | ||
394 | m_bitmap = wxBitmap(image); | |
395 | ||
396 | return m_bitmap.IsOk(); | |
397 | } | |
398 | ||
399 | void wxBitmapDataObject::DoConvertToPng() | |
400 | { | |
401 | if ( !m_bitmap.IsOk() ) | |
402 | return; | |
403 | ||
404 | wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL, | |
405 | wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") ); | |
406 | ||
407 | wxImage image = m_bitmap.ConvertToImage(); | |
408 | ||
409 | wxCountingOutputStream count; | |
410 | image.SaveFile(count, wxBITMAP_TYPE_PNG); | |
411 | ||
412 | m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ??? | |
413 | m_pngData = malloc(m_pngSize); | |
414 | ||
415 | wxMemoryOutputStream mstream((char*) m_pngData, m_pngSize); | |
416 | image.SaveFile(mstream, wxBITMAP_TYPE_PNG); | |
417 | } | |
418 | ||
419 | // ---------------------------------------------------------------------------- | |
420 | // wxURLDataObject | |
421 | // ---------------------------------------------------------------------------- | |
422 | ||
423 | wxURLDataObject::wxURLDataObject(const wxString& url) : | |
424 | wxDataObjectSimple( wxDataFormat( gdk_atom_intern("text/x-moz-url",FALSE) ) ) | |
425 | { | |
426 | m_url = url; | |
427 | } | |
428 | ||
429 | size_t wxURLDataObject::GetDataSize() const | |
430 | { | |
431 | if (m_url.empty()) | |
432 | return 0; | |
433 | ||
434 | return 2*m_url.Len()+2; | |
435 | } | |
436 | ||
437 | bool wxURLDataObject::GetDataHere(void *buf) const | |
438 | { | |
439 | if (m_url.empty()) | |
440 | return false; | |
441 | ||
442 | wxCSConv conv( "UCS2" ); | |
443 | conv.FromWChar( (char*) buf, 2*m_url.Len()+2, m_url.wc_str() ); | |
444 | ||
445 | return true; | |
446 | } | |
447 | ||
448 | // copy data from buffer to our data | |
449 | bool wxURLDataObject::SetData(size_t len, const void *buf) | |
450 | { | |
451 | if (len == 0) | |
452 | { | |
453 | m_url = wxEmptyString; | |
454 | return false; | |
455 | } | |
456 | ||
457 | wxCSConv conv( "UCS2" ); | |
458 | wxWCharBuffer res = conv.cMB2WC( (const char*) buf ); | |
459 | m_url = res; | |
460 | int pos = m_url.Find( '\n' ); | |
461 | if (pos != wxNOT_FOUND) | |
462 | m_url.Remove( pos, m_url.Len() - pos ); | |
463 | ||
464 | return true; | |
465 | } | |
466 | ||
467 | ||
468 | #endif // wxUSE_DATAOBJ |