]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dataobj.cpp
added (and documented) msw.xp-tab-ok option
[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/uri.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(wxURI::Unescape(filename.c_str() + lenPrefix));
282 filename.Empty();
283 }
284 else
285 {
286 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
287 filename.c_str());
288 }
289
290 if ( !*p )
291 break;
292
293 // skip '\r'
294 p++;
295 }
296 else
297 {
298 filename += *p;
299 }
300 }
301
302 return TRUE;
303 }
304
305 void wxFileDataObject::AddFile( const wxString &filename )
306 {
307 m_filenames.Add( filename );
308 }
309
310 // ----------------------------------------------------------------------------
311 // wxBitmapDataObject
312 // ----------------------------------------------------------------------------
313
314 wxBitmapDataObject::wxBitmapDataObject()
315 {
316 Init();
317 }
318
319 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
320 : wxBitmapDataObjectBase(bitmap)
321 {
322 Init();
323
324 DoConvertToPng();
325 }
326
327 wxBitmapDataObject::~wxBitmapDataObject()
328 {
329 Clear();
330 }
331
332 void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
333 {
334 ClearAll();
335
336 wxBitmapDataObjectBase::SetBitmap(bitmap);
337
338 DoConvertToPng();
339 }
340
341 bool wxBitmapDataObject::GetDataHere(void *buf) const
342 {
343 if ( !m_pngSize )
344 {
345 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
346
347 return FALSE;
348 }
349
350 memcpy(buf, m_pngData, m_pngSize);
351
352 return TRUE;
353 }
354
355 bool wxBitmapDataObject::SetData(size_t size, const void *buf)
356 {
357 Clear();
358
359 wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL,
360 FALSE, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
361
362 m_pngSize = size;
363 m_pngData = malloc(m_pngSize);
364
365 memcpy(m_pngData, buf, m_pngSize);
366
367 wxMemoryInputStream mstream((char*) m_pngData, m_pngSize);
368 wxImage image;
369 if ( !image.LoadFile( mstream, wxBITMAP_TYPE_PNG ) )
370 {
371 return FALSE;
372 }
373
374 m_bitmap = wxBitmap(image);
375
376 return m_bitmap.Ok();
377 }
378
379 void wxBitmapDataObject::DoConvertToPng()
380 {
381 if ( !m_bitmap.Ok() )
382 return;
383
384 wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL,
385 wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
386
387 wxImage image = m_bitmap.ConvertToImage();
388
389 wxCountingOutputStream count;
390 image.SaveFile(count, wxBITMAP_TYPE_PNG);
391
392 m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
393 m_pngData = malloc(m_pngSize);
394
395 wxMemoryOutputStream mstream((char*) m_pngData, m_pngSize);
396 image.SaveFile(mstream, wxBITMAP_TYPE_PNG);
397 }
398
399