]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/dataobj.cpp
VTK wrapper of vtkRenderWindow for wxPython. Tested on MSW so far.
[wxWidgets.git] / src / gtk1 / dataobj.cpp
... / ...
CommitLineData
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
26GdkAtom g_textAtom = 0;
27GdkAtom g_pngAtom = 0;
28GdkAtom g_fileAtom = 0;
29
30//-------------------------------------------------------------------------
31// wxDataFormat
32//-------------------------------------------------------------------------
33
34wxDataFormat::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
48wxDataFormat::wxDataFormat( wxDataFormatId type )
49{
50 PrepareFormats();
51 SetType( type );
52}
53
54wxDataFormat::wxDataFormat( const wxChar *id )
55{
56 PrepareFormats();
57 SetId( id );
58}
59
60wxDataFormat::wxDataFormat( const wxString &id )
61{
62 PrepareFormats();
63 SetId( id );
64}
65
66wxDataFormat::wxDataFormat( NativeFormat format )
67{
68 PrepareFormats();
69 SetId( format );
70}
71
72void 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
91wxDataFormatId wxDataFormat::GetType() const
92{
93 return m_type;
94}
95
96wxString wxDataFormat::GetId() const
97{
98 wxString ret( gdk_atom_name( m_format ) ); // this will convert from ascii to Unicode
99 return ret;
100}
101
102void 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
119void wxDataFormat::SetId( const wxChar *id )
120{
121 PrepareFormats();
122 m_type = wxDF_PRIVATE;
123 wxString tmp( id );
124 m_format = gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE ); // what is the string cast for?
125}
126
127void 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 g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE );
138 if (!g_pngAtom)
139 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
140 if (!g_fileAtom)
141 g_fileAtom = gdk_atom_intern( "text/uri-list", FALSE );
142}
143
144//-------------------------------------------------------------------------
145// wxDataObject
146//-------------------------------------------------------------------------
147
148wxDataObject::wxDataObject()
149{
150}
151
152bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
153{
154 size_t nFormatCount = GetFormatCount(dir);
155 if ( nFormatCount == 1 ) {
156 return format == GetPreferredFormat();
157 }
158 else {
159 wxDataFormat *formats = new wxDataFormat[nFormatCount];
160 GetAllFormats(formats,dir);
161
162 size_t n;
163 for ( n = 0; n < nFormatCount; n++ ) {
164 if ( formats[n] == format )
165 break;
166 }
167
168 delete [] formats;
169
170 // found?
171 return n < nFormatCount;
172 }
173}
174
175// ----------------------------------------------------------------------------
176// wxFileDataObject
177// ----------------------------------------------------------------------------
178
179bool wxFileDataObject::GetDataHere(void *buf) const
180{
181 wxString filenames;
182
183 for (size_t i = 0; i < m_filenames.GetCount(); i++)
184 {
185 filenames += m_filenames[i];
186 filenames += (wxChar) 0;
187 }
188
189 memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 );
190
191 return TRUE;
192}
193
194size_t wxFileDataObject::GetDataSize() const
195{
196 size_t res = 0;
197
198 for (size_t i = 0; i < m_filenames.GetCount(); i++)
199 {
200 res += m_filenames[i].Len();
201 res += 1;
202 }
203
204 return res + 1;
205}
206
207bool wxFileDataObject::SetData(size_t size, const void *buf)
208{
209 // VZ: old format
210#if 0
211 // filenames are stores as a string with #0 as deliminators
212 const char *filenames = (const char*) buf;
213 size_t pos = 0;
214 for(;;)
215 {
216 if (filenames[0] == 0)
217 break;
218 if (pos >= size)
219 break;
220 wxString file( filenames ); // this returns the first file
221 AddFile( file );
222 pos += file.Len()+1;
223 filenames += file.Len()+1;
224 }
225#else // 1
226 m_filenames.Empty();
227
228 // the text/uri-list format is a sequence of URIs (filenames prefixed by
229 // "file:" as far as I see) delimited by "\r\n" of total length size
230 // (I wonder what happens if the file has '\n' in its filename??)
231 wxString filename;
232 for ( const char *p = (const char *)buf; ; p++ )
233 {
234 // some broken programs (testdnd GTK+ sample!) omit the trailing
235 // "\r\n", so check for '\0' explicitly here instead of doing it in
236 // the loop statement to account for it
237 if ( (*p == '\r' && *(p+1) == '\n') || !*p )
238 {
239 size_t lenPrefix = 5; // strlen("file:")
240 if ( filename.Left(lenPrefix).MakeLower() == _T("file:") )
241 {
242 // sometimes the syntax is "file:filename", sometimes it's
243 // URL-like: "file://filename" - deal with both
244 if ( filename[lenPrefix] == _T('/') &&
245 filename[lenPrefix + 1] == _T('/') )
246 {
247 // skip the slashes
248 lenPrefix += 2;
249 }
250
251 AddFile(filename.c_str() + lenPrefix);
252 filename.Empty();
253 }
254 else
255 {
256 wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"),
257 filename.c_str());
258 }
259
260 if ( !*p )
261 break;
262
263 // skip '\r'
264 p++;
265 }
266 else
267 {
268 filename += *p;
269 }
270 }
271#endif // 0/1
272
273 return TRUE;
274}
275
276void wxFileDataObject::AddFile( const wxString &filename )
277{
278 m_filenames.Add( filename );
279}
280
281// ----------------------------------------------------------------------------
282// wxBitmapDataObject
283// ----------------------------------------------------------------------------
284
285wxBitmapDataObject::wxBitmapDataObject()
286{
287 Init();
288}
289
290wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
291 : wxBitmapDataObjectBase(bitmap)
292{
293 Init();
294
295 DoConvertToPng();
296}
297
298wxBitmapDataObject::~wxBitmapDataObject()
299{
300 Clear();
301}
302
303void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
304{
305 ClearAll();
306
307 wxBitmapDataObjectBase::SetBitmap(bitmap);
308
309 DoConvertToPng();
310}
311
312bool wxBitmapDataObject::GetDataHere(void *buf) const
313{
314 if ( !m_pngSize )
315 {
316 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
317
318 return FALSE;
319 }
320
321 memcpy(buf, m_pngData, m_pngSize);
322
323 return TRUE;
324}
325
326bool wxBitmapDataObject::SetData(size_t size, const void *buf)
327{
328 Clear();
329
330 m_pngSize = size;
331 m_pngData = malloc(m_pngSize);
332
333 memcpy( m_pngData, buf, m_pngSize );
334
335 wxMemoryInputStream mstream( (char*) m_pngData, m_pngSize );
336 wxImage image;
337 wxPNGHandler handler;
338 if ( !handler.LoadFile( &image, mstream ) )
339 {
340 return FALSE;
341 }
342
343 m_bitmap = image.ConvertToBitmap();
344
345 return m_bitmap.Ok();
346}
347
348void wxBitmapDataObject::DoConvertToPng()
349{
350 if (!m_bitmap.Ok())
351 return;
352
353 wxImage image( m_bitmap );
354 wxPNGHandler handler;
355
356 wxCountingOutputStream count;
357 handler.SaveFile( &image, count );
358
359 m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
360 m_pngData = malloc(m_pngSize);
361
362 wxMemoryOutputStream mstream( (char*) m_pngData, m_pngSize );
363 handler.SaveFile( &image, mstream );
364}
365
366