]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dataobj.cpp
final sweep over docs - replace & with \&
[wxWidgets.git] / src / gtk1 / dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "dataobj.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #include "wx/dataobj.h"
18 #include "wx/app.h"
19 #include "wx/debug.h"
20 #include "wx/mstream.h"
21 #include "wx/image.h"
22 #include "wx/log.h"
23 #include "wx/url.h"
24
25 #include <gdk/gdk.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
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 = (GdkAtom) 0;
52 }
53
54 wxDataFormat::wxDataFormat( wxDataFormatId type )
55 {
56 PrepareFormats();
57 SetType( type );
58 }
59
60 wxDataFormat::wxDataFormat( const wxChar *id )
61 {
62 PrepareFormats();
63 SetId( id );
64 }
65
66 wxDataFormat::wxDataFormat( const wxString &id )
67 {
68 PrepareFormats();
69 SetId( id );
70 }
71
72 wxDataFormat::wxDataFormat( NativeFormat format )
73 {
74 PrepareFormats();
75 SetId( format );
76 }
77
78 void wxDataFormat::SetType( wxDataFormatId type )
79 {
80 PrepareFormats();
81
82 m_type = type;
83
84 #if wxUSE_UNICODE
85 if (m_type == wxDF_UNICODETEXT)
86 m_format = g_textAtom;
87 else if (m_type == wxDF_TEXT)
88 m_format = g_altTextAtom;
89 #else
90 if (m_type == wxDF_TEXT || m_type == wxDF_UNICODETEXT)
91 m_format = g_textAtom;
92 #endif
93 else
94 if (m_type == wxDF_BITMAP)
95 m_format = g_pngAtom;
96 else
97 if (m_type == wxDF_FILENAME)
98 m_format = g_fileAtom;
99 else
100 {
101 wxFAIL_MSG( wxT("invalid dataformat") );
102 }
103 }
104
105 wxDataFormatId wxDataFormat::GetType() const
106 {
107 return m_type;
108 }
109
110 wxString wxDataFormat::GetId() const
111 {
112 wxString ret = wxString::FromAscii( gdk_atom_name( m_format ) );
113 return ret;
114 }
115
116 void wxDataFormat::SetId( NativeFormat format )
117 {
118 PrepareFormats();
119 m_format = format;
120
121 if (m_format == g_textAtom)
122 #if wxUSE_UNICODE
123 m_type = wxDF_UNICODETEXT;
124 #else
125 m_type = wxDF_TEXT;
126 #endif
127 else
128 if (m_format == g_altTextAtom)
129 m_type = wxDF_TEXT;
130 else
131 if (m_format == g_pngAtom)
132 m_type = wxDF_BITMAP;
133 else
134 if (m_format == g_fileAtom)
135 m_type = wxDF_FILENAME;
136 else
137 m_type = wxDF_PRIVATE;
138 }
139
140 void wxDataFormat::SetId( const wxChar *id )
141 {
142 PrepareFormats();
143 m_type = wxDF_PRIVATE;
144 wxString tmp( id );
145 m_format = gdk_atom_intern( (const char*) tmp.ToAscii(), FALSE );
146 }
147
148 void wxDataFormat::PrepareFormats()
149 {
150 // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the
151 // atoms STRING and file:ALL as the old code was, but normal X apps
152 // use STRING for text selection when transfering the data via
153 // clipboard, for example, so do use STRING for now (GNOME apps will
154 // probably support STRING as well for compatibility anyhow), but use
155 // text/uri-list for file dnd because compatibility is not important
156 // here (with whom?)
157 if (!g_textAtom)
158 #if wxUSE_UNICODE
159 g_textAtom = gdk_atom_intern( "UTF8_STRING", FALSE );
160 g_altTextAtom = gdk_atom_intern( "STRING", FALSE );
161 #else
162 g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE );
163 #endif
164 if (!g_pngAtom)
165 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
166 if (!g_fileAtom)
167 g_fileAtom = gdk_atom_intern( "text/uri-list", FALSE );
168 }
169
170 //-------------------------------------------------------------------------
171 // wxDataObject
172 //-------------------------------------------------------------------------
173
174 wxDataObject::wxDataObject()
175 {
176 }
177
178 wxDataObject::~wxDataObject()
179 {
180 // dtor is empty but needed for Darwin and AIX -- otherwise it doesn't link
181 }
182
183 bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
184 {
185 size_t nFormatCount = GetFormatCount(dir);
186 if ( nFormatCount == 1 )
187 {
188 return format == GetPreferredFormat();
189 }
190 else
191 {
192 wxDataFormat *formats = new wxDataFormat[nFormatCount];
193 GetAllFormats(formats,dir);
194
195 size_t n;
196 for ( n = 0; n < nFormatCount; n++ )
197 {
198 if ( formats[n] == format )
199 break;
200 }
201
202 delete [] formats;
203
204 // found?
205 return n < nFormatCount;
206 }
207 }
208
209 // ----------------------------------------------------------------------------
210 // wxTextDataObject
211 // ----------------------------------------------------------------------------
212
213 #if defined(__WXGTK20__) && wxUSE_UNICODE
214 void wxTextDataObject::GetAllFormats(wxDataFormat *formats, wxDataObjectBase::Direction dir) const
215 {
216 *formats++ = GetPreferredFormat();
217 *formats = g_altTextAtom;
218 }
219 #endif
220
221 // ----------------------------------------------------------------------------
222 // wxFileDataObject
223 // ----------------------------------------------------------------------------
224
225 bool wxFileDataObject::GetDataHere(void *buf) const
226 {
227 wxString filenames;
228
229 for (size_t i = 0; i < m_filenames.GetCount(); i++)
230 {
231 filenames += wxT("file:");
232 filenames += m_filenames[i];
233 filenames += wxT("\r\n");
234 }
235
236 memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 );
237
238 return TRUE;
239 }
240
241 size_t wxFileDataObject::GetDataSize() const
242 {
243 size_t res = 0;
244
245 for (size_t i = 0; i < m_filenames.GetCount(); i++)
246 {
247 // This is junk in UTF-8
248 res += m_filenames[i].Len();
249 res += 5 + 2; // "file:" (5) + "\r\n" (2)
250 }
251
252 return res + 1;
253 }
254
255 bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf)
256 {
257 m_filenames.Empty();
258
259 // we get data in the text/uri-list format, i.e. as a sequence of URIs
260 // (filenames prefixed by "file:") delimited by "\r\n"
261 wxString filename;
262 for ( const char *p = (const char *)buf; ; p++ )
263 {
264 // some broken programs (testdnd GTK+ sample!) omit the trailing
265 // "\r\n", so check for '\0' explicitly here instead of doing it in
266 // the loop statement to account for it
267 if ( (*p == '\r' && *(p+1) == '\n') || !*p )
268 {
269 size_t lenPrefix = 5; // strlen("file:")
270 if ( filename.Left(lenPrefix).MakeLower() == _T("file:") )
271 {
272 // sometimes the syntax is "file:filename", sometimes it's
273 // URL-like: "file://filename" - deal with both
274 if ( filename[lenPrefix] == _T('/') &&
275 filename[lenPrefix + 1] == _T('/') )
276 {
277 // skip the slashes
278 lenPrefix += 2;
279 }
280
281 AddFile(
282 #if wxUSE_URL
283 wxURL::ConvertFromURI
284 #endif // wxUSE_URL
285 (filename.c_str() + lenPrefix));
286 filename.Empty();
287 }
288 else
289 {
290 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
291 filename.c_str());
292 }
293
294 if ( !*p )
295 break;
296
297 // skip '\r'
298 p++;
299 }
300 else
301 {
302 filename += *p;
303 }
304 }
305
306 return TRUE;
307 }
308
309 void wxFileDataObject::AddFile( const wxString &filename )
310 {
311 m_filenames.Add( filename );
312 }
313
314 // ----------------------------------------------------------------------------
315 // wxBitmapDataObject
316 // ----------------------------------------------------------------------------
317
318 wxBitmapDataObject::wxBitmapDataObject()
319 {
320 Init();
321 }
322
323 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
324 : wxBitmapDataObjectBase(bitmap)
325 {
326 Init();
327
328 DoConvertToPng();
329 }
330
331 wxBitmapDataObject::~wxBitmapDataObject()
332 {
333 Clear();
334 }
335
336 void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
337 {
338 ClearAll();
339
340 wxBitmapDataObjectBase::SetBitmap(bitmap);
341
342 DoConvertToPng();
343 }
344
345 bool wxBitmapDataObject::GetDataHere(void *buf) const
346 {
347 if ( !m_pngSize )
348 {
349 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
350
351 return FALSE;
352 }
353
354 memcpy(buf, m_pngData, m_pngSize);
355
356 return TRUE;
357 }
358
359 bool wxBitmapDataObject::SetData(size_t size, const void *buf)
360 {
361 Clear();
362
363 wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL,
364 FALSE, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
365
366 m_pngSize = size;
367 m_pngData = malloc(m_pngSize);
368
369 memcpy(m_pngData, buf, m_pngSize);
370
371 wxMemoryInputStream mstream((char*) m_pngData, m_pngSize);
372 wxImage image;
373 if ( !image.LoadFile( mstream, wxBITMAP_TYPE_PNG ) )
374 {
375 return FALSE;
376 }
377
378 m_bitmap = wxBitmap(image);
379
380 return m_bitmap.Ok();
381 }
382
383 void wxBitmapDataObject::DoConvertToPng()
384 {
385 if ( !m_bitmap.Ok() )
386 return;
387
388 wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL,
389 wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
390
391 wxImage image = m_bitmap.ConvertToImage();
392
393 wxCountingOutputStream count;
394 image.SaveFile(count, wxBITMAP_TYPE_PNG);
395
396 m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
397 m_pngData = malloc(m_pngSize);
398
399 wxMemoryOutputStream mstream((char*) m_pngData, m_pngSize);
400 image.SaveFile(mstream, wxBITMAP_TYPE_PNG);
401 }
402
403