]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dataobj.cpp
iconv() really never set the trailing 0.
[wxWidgets.git] / src / gtk / 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 #ifdef __GNUG__
11 #pragma implementation "dataobj.h"
12 #endif
13
14 #include "wx/dataobj.h"
15 #include "wx/app.h"
16 #include "wx/debug.h"
17 #include "wx/mstream.h"
18 #include "wx/image.h"
19
20 #include <gdk/gdk.h>
21
22 //-------------------------------------------------------------------------
23 // global data
24 //-------------------------------------------------------------------------
25
26 GdkAtom g_textAtom = 0;
27 GdkAtom g_pngAtom = 0;
28 GdkAtom g_fileAtom = 0;
29
30 //-------------------------------------------------------------------------
31 // wxDataFormat
32 //-------------------------------------------------------------------------
33
34 wxDataFormat::wxDataFormat()
35 {
36 // do *not* call PrepareFormats() from here for 2 reasons:
37 //
38 // 1. we will have time to do it later because some other Set function
39 // must be called before we really need them
40 //
41 // 2. doing so prevents us from declaring global wxDataFormats because
42 // calling PrepareFormats (and thus gdk_atom_intern) before GDK is
43 // initialised will result in a crash
44 m_type = wxDF_INVALID;
45 m_format = (GdkAtom) 0;
46 }
47
48 wxDataFormat::wxDataFormat( wxDataFormatId type )
49 {
50 PrepareFormats();
51 SetType( type );
52 }
53
54 wxDataFormat::wxDataFormat( const wxChar *id )
55 {
56 PrepareFormats();
57 SetId( id );
58 }
59
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
77 if (m_type == wxDF_TEXT)
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 {
98 wxString ret = wxString::FromAscii( gdk_atom_name( m_format ) );
99 return ret;
100 }
101
102 void wxDataFormat::SetId( NativeFormat format )
103 {
104 PrepareFormats();
105 m_format = format;
106
107 if (m_format == g_textAtom)
108 m_type = wxDF_TEXT;
109 else
110 if (m_format == g_pngAtom)
111 m_type = wxDF_BITMAP;
112 else
113 if (m_format == g_fileAtom)
114 m_type = wxDF_FILENAME;
115 else
116 m_type = wxDF_PRIVATE;
117 }
118
119 void wxDataFormat::SetId( const wxChar *id )
120 {
121 PrepareFormats();
122 m_type = wxDF_PRIVATE;
123 wxString tmp( id );
124 m_format = gdk_atom_intern( (const char*) tmp.ToAscii(), FALSE );
125 }
126
127 void wxDataFormat::PrepareFormats()
128 {
129 // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the
130 // atoms STRING and file:ALL as the old code was, but normal X apps
131 // use STRING for text selection when transfering the data via
132 // clipboard, for example, so do use STRING for now (GNOME apps will
133 // probably support STRING as well for compatibility anyhow), but use
134 // text/uri-list for file dnd because compatibility is not important
135 // here (with whom?)
136 if (!g_textAtom)
137 #if wxUSE_UNICODE
138 g_textAtom = gdk_atom_intern( "text/utf8", FALSE );
139 #else
140 g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE );
141 #endif
142 if (!g_pngAtom)
143 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
144 if (!g_fileAtom)
145 g_fileAtom = gdk_atom_intern( "text/uri-list", FALSE );
146 }
147
148 //-------------------------------------------------------------------------
149 // wxDataObject
150 //-------------------------------------------------------------------------
151
152 wxDataObject::wxDataObject()
153 {
154 }
155
156 bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
157 {
158 size_t nFormatCount = GetFormatCount(dir);
159 if ( nFormatCount == 1 )
160 {
161 return format == GetPreferredFormat();
162 }
163 else
164 {
165 wxDataFormat *formats = new wxDataFormat[nFormatCount];
166 GetAllFormats(formats,dir);
167
168 size_t n;
169 for ( n = 0; n < nFormatCount; n++ )
170 {
171 if ( formats[n] == format )
172 break;
173 }
174
175 delete [] formats;
176
177 // found?
178 return n < nFormatCount;
179 }
180 }
181
182 // ----------------------------------------------------------------------------
183 // wxFileDataObject
184 // ----------------------------------------------------------------------------
185
186 bool wxFileDataObject::GetDataHere(void *buf) const
187 {
188 wxString filenames;
189
190 for (size_t i = 0; i < m_filenames.GetCount(); i++)
191 {
192 filenames += m_filenames[i];
193 filenames += (wxChar) 0;
194 }
195
196 memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 );
197
198 return TRUE;
199 }
200
201 size_t wxFileDataObject::GetDataSize() const
202 {
203 size_t res = 0;
204
205 for (size_t i = 0; i < m_filenames.GetCount(); i++)
206 {
207 res += m_filenames[i].Len();
208 res += 1;
209 }
210
211 return res + 1;
212 }
213
214 bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf)
215 {
216 // VZ: old format
217 #if 0
218 // filenames are stores as a string with #0 as deliminators
219 const char *filenames = (const char*) buf;
220 size_t pos = 0;
221 for(;;)
222 {
223 if (filenames[0] == 0)
224 break;
225 if (pos >= size)
226 break;
227 wxString file( filenames ); // this returns the first file
228 AddFile( file );
229 pos += file.Len()+1;
230 filenames += file.Len()+1;
231 }
232 #else // 1
233 m_filenames.Empty();
234
235 // the text/uri-list format is a sequence of URIs (filenames prefixed by
236 // "file:" as far as I see) delimited by "\r\n" of total length size
237 // (I wonder what happens if the file has '\n' in its filename??)
238 wxString filename;
239 for ( const char *p = (const char *)buf; ; p++ )
240 {
241 // some broken programs (testdnd GTK+ sample!) omit the trailing
242 // "\r\n", so check for '\0' explicitly here instead of doing it in
243 // the loop statement to account for it
244 if ( (*p == '\r' && *(p+1) == '\n') || !*p )
245 {
246 size_t lenPrefix = 5; // strlen("file:")
247 if ( filename.Left(lenPrefix).MakeLower() == _T("file:") )
248 {
249 // sometimes the syntax is "file:filename", sometimes it's
250 // URL-like: "file://filename" - deal with both
251 if ( filename[lenPrefix] == _T('/') &&
252 filename[lenPrefix + 1] == _T('/') )
253 {
254 // skip the slashes
255 lenPrefix += 2;
256 }
257
258 AddFile(filename.c_str() + lenPrefix);
259 filename.Empty();
260 }
261 else
262 {
263 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
264 filename.c_str());
265 }
266
267 if ( !*p )
268 break;
269
270 // skip '\r'
271 p++;
272 }
273 else
274 {
275 filename += *p;
276 }
277 }
278 #endif // 0/1
279
280 return TRUE;
281 }
282
283 void wxFileDataObject::AddFile( const wxString &filename )
284 {
285 m_filenames.Add( filename );
286 }
287
288 // ----------------------------------------------------------------------------
289 // wxBitmapDataObject
290 // ----------------------------------------------------------------------------
291
292 wxBitmapDataObject::wxBitmapDataObject()
293 {
294 Init();
295 }
296
297 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
298 : wxBitmapDataObjectBase(bitmap)
299 {
300 Init();
301
302 DoConvertToPng();
303 }
304
305 wxBitmapDataObject::~wxBitmapDataObject()
306 {
307 Clear();
308 }
309
310 void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
311 {
312 ClearAll();
313
314 wxBitmapDataObjectBase::SetBitmap(bitmap);
315
316 DoConvertToPng();
317 }
318
319 bool wxBitmapDataObject::GetDataHere(void *buf) const
320 {
321 if ( !m_pngSize )
322 {
323 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
324
325 return FALSE;
326 }
327
328 memcpy(buf, m_pngData, m_pngSize);
329
330 return TRUE;
331 }
332
333 bool wxBitmapDataObject::SetData(size_t size, const void *buf)
334 {
335 Clear();
336
337 wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL,
338 FALSE, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
339
340 m_pngSize = size;
341 m_pngData = malloc(m_pngSize);
342
343 memcpy(m_pngData, buf, m_pngSize);
344
345 wxMemoryInputStream mstream((char*) m_pngData, m_pngSize);
346 wxImage image;
347 if ( !image.LoadFile( mstream, wxBITMAP_TYPE_PNG ) )
348 {
349 return FALSE;
350 }
351
352 m_bitmap = wxBitmap(image);
353
354 return m_bitmap.Ok();
355 }
356
357 void wxBitmapDataObject::DoConvertToPng()
358 {
359 if ( !m_bitmap.Ok() )
360 return;
361
362 wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL,
363 wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
364
365 wxImage image = m_bitmap.ConvertToImage();
366
367 wxCountingOutputStream count;
368 image.SaveFile(count, wxBITMAP_TYPE_PNG);
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 image.SaveFile(mstream, wxBITMAP_TYPE_PNG);
375 }
376
377