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