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