]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dataobj.cpp
fix for CW non unicode builds
[wxWidgets.git] / src / gtk / dataobj.cpp
CommitLineData
8b53e5a2
RR
1///////////////////////////////////////////////////////////////////////////////
2// Name: dataobj.cpp
3// Purpose: wxDataObject class
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
3f480da3 7// Licence: wxWindows licence
8b53e5a2
RR
8///////////////////////////////////////////////////////////////////////////////
9
14f355c2 10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
3f480da3 11 #pragma implementation "dataobj.h"
8b53e5a2
RR
12#endif
13
14f355c2
VS
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
8b53e5a2 17#include "wx/dataobj.h"
ab8884ac 18#include "wx/app.h"
0d2a2b60 19#include "wx/debug.h"
e2acb9ae
RR
20#include "wx/mstream.h"
21#include "wx/image.h"
1fb35ade 22#include "wx/log.h"
0d2a2b60 23
071a2d78 24#include <gdk/gdk.h>
0d2a2b60 25
d6086ea6
RR
26//-------------------------------------------------------------------------
27// global data
28//-------------------------------------------------------------------------
29
30GdkAtom g_textAtom = 0;
e2acb9ae 31GdkAtom g_pngAtom = 0;
1dd989e1 32GdkAtom g_fileAtom = 0;
d6086ea6 33
0d2a2b60
RR
34//-------------------------------------------------------------------------
35// wxDataFormat
36//-------------------------------------------------------------------------
37
cd5bf2a6 38wxDataFormat::wxDataFormat()
0d2a2b60 39{
4b3d29db
VZ
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
cd5bf2a6 48 m_type = wxDF_INVALID;
1dd989e1 49 m_format = (GdkAtom) 0;
cd5bf2a6
RR
50}
51
3f480da3 52wxDataFormat::wxDataFormat( wxDataFormatId type )
cd5bf2a6 53{
e2acb9ae 54 PrepareFormats();
cd5bf2a6 55 SetType( type );
0d2a2b60
RR
56}
57
b5be07d4 58wxDataFormat::wxDataFormat( const wxChar *id )
8a126fcc 59{
e2acb9ae 60 PrepareFormats();
8a126fcc
RR
61 SetId( id );
62}
63
0d2a2b60
RR
64wxDataFormat::wxDataFormat( const wxString &id )
65{
e2acb9ae 66 PrepareFormats();
cd5bf2a6 67 SetId( id );
0d2a2b60
RR
68}
69
1dd989e1 70wxDataFormat::wxDataFormat( NativeFormat format )
0d2a2b60 71{
e2acb9ae 72 PrepareFormats();
1dd989e1 73 SetId( format );
0d2a2b60
RR
74}
75
3f480da3 76void wxDataFormat::SetType( wxDataFormatId type )
cd5bf2a6 77{
4b3d29db 78 PrepareFormats();
ca11abde
RR
79
80 if (type == wxDF_UNICODETEXT)
81 type = wxDF_TEXT;
3f480da3 82
ca11abde
RR
83 m_type = type;
84
cd5bf2a6 85 if (m_type == wxDF_TEXT)
1dd989e1 86 m_format = g_textAtom;
cd5bf2a6
RR
87 else
88 if (m_type == wxDF_BITMAP)
1dd989e1 89 m_format = g_pngAtom;
cd5bf2a6
RR
90 else
91 if (m_type == wxDF_FILENAME)
1dd989e1 92 m_format = g_fileAtom;
cd5bf2a6
RR
93 else
94 {
223d09f6 95 wxFAIL_MSG( wxT("invalid dataformat") );
cd5bf2a6 96 }
cd5bf2a6 97}
3f480da3
VZ
98
99wxDataFormatId wxDataFormat::GetType() const
0d2a2b60
RR
100{
101 return m_type;
102}
103
104wxString wxDataFormat::GetId() const
105{
2e1d7104 106 wxString ret = wxString::FromAscii( gdk_atom_name( m_format ) );
1dd989e1 107 return ret;
0d2a2b60 108}
3f480da3 109
1dd989e1 110void wxDataFormat::SetId( NativeFormat format )
3f480da3 111{
4b3d29db 112 PrepareFormats();
1dd989e1 113 m_format = format;
3f480da3 114
1dd989e1
RR
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;
0d2a2b60 125}
3f480da3 126
1dd989e1 127void wxDataFormat::SetId( const wxChar *id )
0d2a2b60 128{
4b3d29db 129 PrepareFormats();
1dd989e1
RR
130 m_type = wxDF_PRIVATE;
131 wxString tmp( id );
2e1d7104 132 m_format = gdk_atom_intern( (const char*) tmp.ToAscii(), FALSE );
0d2a2b60 133}
3f480da3 134
1dd989e1 135void wxDataFormat::PrepareFormats()
0d2a2b60 136{
810b5e1f 137 // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the
74d38ad8
VZ
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?)
e1ee679c 144 if (!g_textAtom)
2e1d7104 145#if wxUSE_UNICODE
ca11abde 146 g_textAtom = gdk_atom_intern( "UTF8_STRING", FALSE );
2e1d7104 147#else
74d38ad8 148 g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE );
2e1d7104 149#endif
e1ee679c 150 if (!g_pngAtom)
1dd989e1 151 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
e1ee679c 152 if (!g_fileAtom)
810b5e1f 153 g_fileAtom = gdk_atom_intern( "text/uri-list", FALSE );
0d2a2b60 154}
8b53e5a2
RR
155
156//-------------------------------------------------------------------------
157// wxDataObject
158//-------------------------------------------------------------------------
159
0d2a2b60
RR
160wxDataObject::wxDataObject()
161{
0d2a2b60 162}
3f480da3 163
2b5f62a0
VZ
164wxDataObject::~wxDataObject()
165{
166 // dtor is empty but needed for Darwin and AIX -- otherwise it doesn't link
167}
168
b068c4e8 169bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
0d2a2b60 170{
b068c4e8 171 size_t nFormatCount = GetFormatCount(dir);
f2593d0d
RR
172 if ( nFormatCount == 1 )
173 {
1dd989e1
RR
174 return format == GetPreferredFormat();
175 }
f2593d0d
RR
176 else
177 {
1dd989e1 178 wxDataFormat *formats = new wxDataFormat[nFormatCount];
b068c4e8 179 GetAllFormats(formats,dir);
1dd989e1
RR
180
181 size_t n;
f2593d0d
RR
182 for ( n = 0; n < nFormatCount; n++ )
183 {
1dd989e1
RR
184 if ( formats[n] == format )
185 break;
186 }
0d2a2b60 187
1dd989e1 188 delete [] formats;
cd5bf2a6 189
1dd989e1
RR
190 // found?
191 return n < nFormatCount;
192 }
0d2a2b60
RR
193}
194
8b53e5a2
RR
195// ----------------------------------------------------------------------------
196// wxFileDataObject
197// ----------------------------------------------------------------------------
198
e1ee679c 199bool wxFileDataObject::GetDataHere(void *buf) const
0d2a2b60 200{
b068c4e8 201 wxString filenames;
4b3d29db 202
b068c4e8
RR
203 for (size_t i = 0; i < m_filenames.GetCount(); i++)
204 {
205 filenames += m_filenames[i];
4b3d29db 206 filenames += (wxChar) 0;
b068c4e8 207 }
4b3d29db 208
e1ee679c 209 memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 );
0d2a2b60 210
e1ee679c 211 return TRUE;
0d2a2b60 212}
3f480da3 213
e1ee679c 214size_t wxFileDataObject::GetDataSize() const
0d2a2b60 215{
b068c4e8 216 size_t res = 0;
4b3d29db 217
b068c4e8
RR
218 for (size_t i = 0; i < m_filenames.GetCount(); i++)
219 {
220 res += m_filenames[i].Len();
4b3d29db 221 res += 1;
b068c4e8 222 }
4b3d29db 223
b068c4e8 224 return res + 1;
0d2a2b60 225}
3f480da3 226
7941ba11 227bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf)
0d2a2b60 228{
810b5e1f
VZ
229 // VZ: old format
230#if 0
2d68e1b4 231 // filenames are stores as a string with #0 as deliminators
2d68e1b4
RR
232 const char *filenames = (const char*) buf;
233 size_t pos = 0;
234 for(;;)
235 {
236 if (filenames[0] == 0)
810b5e1f
VZ
237 break;
238 if (pos >= size)
239 break;
2d68e1b4
RR
240 wxString file( filenames ); // this returns the first file
241 AddFile( file );
810b5e1f
VZ
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;
74d38ad8 252 for ( const char *p = (const char *)buf; ; p++ )
810b5e1f 253 {
74d38ad8
VZ
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 )
810b5e1f 258 {
74d38ad8 259 size_t lenPrefix = 5; // strlen("file:")
810b5e1f
VZ
260 if ( filename.Left(lenPrefix).MakeLower() == _T("file:") )
261 {
74d38ad8
VZ
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());
810b5e1f
VZ
278 }
279
74d38ad8
VZ
280 if ( !*p )
281 break;
810b5e1f
VZ
282
283 // skip '\r'
284 p++;
285 }
286 else
287 {
288 filename += *p;
289 }
2d68e1b4 290 }
810b5e1f 291#endif // 0/1
3f480da3 292
1dd989e1
RR
293 return TRUE;
294}
295
b068c4e8
RR
296void wxFileDataObject::AddFile( const wxString &filename )
297{
298 m_filenames.Add( filename );
299}
300
8b53e5a2
RR
301// ----------------------------------------------------------------------------
302// wxBitmapDataObject
303// ----------------------------------------------------------------------------
304
0d2a2b60
RR
305wxBitmapDataObject::wxBitmapDataObject()
306{
e1ee679c 307 Init();
0d2a2b60
RR
308}
309
310wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
e1ee679c 311 : wxBitmapDataObjectBase(bitmap)
0d2a2b60 312{
e1ee679c
VZ
313 Init();
314
e2acb9ae
RR
315 DoConvertToPng();
316}
317
318wxBitmapDataObject::~wxBitmapDataObject()
319{
e1ee679c 320 Clear();
0d2a2b60
RR
321}
322
323void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
324{
e1ee679c 325 ClearAll();
0d2a2b60 326
e1ee679c
VZ
327 wxBitmapDataObjectBase::SetBitmap(bitmap);
328
329 DoConvertToPng();
0d2a2b60
RR
330}
331
e1ee679c 332bool wxBitmapDataObject::GetDataHere(void *buf) const
0d2a2b60 333{
e1ee679c 334 if ( !m_pngSize )
1dd989e1 335 {
e1ee679c
VZ
336 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
337
338 return FALSE;
1dd989e1 339 }
0d2a2b60 340
e1ee679c
VZ
341 memcpy(buf, m_pngData, m_pngSize);
342
343 return TRUE;
e2acb9ae
RR
344}
345
e1ee679c 346bool wxBitmapDataObject::SetData(size_t size, const void *buf)
e2acb9ae 347{
e1ee679c
VZ
348 Clear();
349
bd3b7e09
VS
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
1dd989e1 353 m_pngSize = size;
e1ee679c
VZ
354 m_pngData = malloc(m_pngSize);
355
bd3b7e09 356 memcpy(m_pngData, buf, m_pngSize);
e1ee679c 357
bd3b7e09 358 wxMemoryInputStream mstream((char*) m_pngData, m_pngSize);
e2acb9ae 359 wxImage image;
bd3b7e09 360 if ( !image.LoadFile( mstream, wxBITMAP_TYPE_PNG ) )
e1ee679c
VZ
361 {
362 return FALSE;
363 }
364
bd3b7e09 365 m_bitmap = wxBitmap(image);
4b3d29db 366
b068c4e8 367 return m_bitmap.Ok();
e2acb9ae
RR
368}
369
370void wxBitmapDataObject::DoConvertToPng()
371{
bd3b7e09 372 if ( !m_bitmap.Ok() )
e1ee679c
VZ
373 return;
374
bd3b7e09
VS
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
368d59f0 378 wxImage image = m_bitmap.ConvertToImage();
e1ee679c 379
e2acb9ae 380 wxCountingOutputStream count;
bd3b7e09 381 image.SaveFile(count, wxBITMAP_TYPE_PNG);
e1ee679c 382
e2acb9ae 383 m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
e1ee679c
VZ
384 m_pngData = malloc(m_pngSize);
385
bd3b7e09
VS
386 wxMemoryOutputStream mstream((char*) m_pngData, m_pngSize);
387 image.SaveFile(mstream, wxBITMAP_TYPE_PNG);
0d2a2b60 388}
3f480da3 389
0d2a2b60 390