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