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