]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/dataobj.cpp
wxSlider should now display int values,
[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 PrepareFormats();
37 m_type = wxDF_INVALID;
38 m_format = (GdkAtom) 0;
39}
40
41wxDataFormat::wxDataFormat( wxDataFormatId type )
42{
43 PrepareFormats();
44 SetType( type );
45}
46
47wxDataFormat::wxDataFormat( const wxChar *id )
48{
49 PrepareFormats();
50 SetId( id );
51}
52
53wxDataFormat::wxDataFormat( const wxString &id )
54{
55 PrepareFormats();
56 SetId( id );
57}
58
59wxDataFormat::wxDataFormat( NativeFormat format )
60{
61 PrepareFormats();
62 SetId( format );
63}
64
65void wxDataFormat::SetType( wxDataFormatId type )
66{
67 m_type = type;
68
69 if (m_type == wxDF_TEXT)
70 m_format = g_textAtom;
71 else
72 if (m_type == wxDF_BITMAP)
73 m_format = g_pngAtom;
74 else
75 if (m_type == wxDF_FILENAME)
76 m_format = g_fileAtom;
77 else
78 {
79 wxFAIL_MSG( wxT("invalid dataformat") );
80 }
81}
82
83wxDataFormatId wxDataFormat::GetType() const
84{
85 return m_type;
86}
87
88wxString wxDataFormat::GetId() const
89{
90 wxString ret( gdk_atom_name( m_format ) ); // this will convert from ascii to Unicode
91 return ret;
92}
93
94void wxDataFormat::SetId( NativeFormat format )
95{
96 m_format = format;
97
98 if (m_format == g_textAtom)
99 m_type = wxDF_TEXT;
100 else
101 if (m_format == g_pngAtom)
102 m_type = wxDF_BITMAP;
103 else
104 if (m_format == g_fileAtom)
105 m_type = wxDF_FILENAME;
106 else
107 m_type = wxDF_PRIVATE;
108}
109
110void wxDataFormat::SetId( const wxChar *id )
111{
112 m_type = wxDF_PRIVATE;
113 wxString tmp( id );
114 m_format = gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE ); // what is the string cast for?
115}
116
117void wxDataFormat::PrepareFormats()
118{
119 if (!g_textAtom)
120 g_textAtom = gdk_atom_intern( "STRING", FALSE );
121 if (!g_pngAtom)
122 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
123 if (!g_fileAtom)
124 g_fileAtom = gdk_atom_intern( "file:ALL", FALSE );
125}
126
127//-------------------------------------------------------------------------
128// wxDataObject
129//-------------------------------------------------------------------------
130
131wxDataObject::wxDataObject()
132{
133}
134
135bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
136{
137 size_t nFormatCount = GetFormatCount(dir);
138 if ( nFormatCount == 1 ) {
139 return format == GetPreferredFormat();
140 }
141 else {
142 wxDataFormat *formats = new wxDataFormat[nFormatCount];
143 GetAllFormats(formats,dir);
144
145 size_t n;
146 for ( n = 0; n < nFormatCount; n++ ) {
147 if ( formats[n] == format )
148 break;
149 }
150
151 delete [] formats;
152
153 // found?
154 return n < nFormatCount;
155 }
156}
157
158// ----------------------------------------------------------------------------
159// wxFileDataObject
160// ----------------------------------------------------------------------------
161
162bool wxFileDataObject::GetDataHere(void *buf) const
163{
164 wxString filenames;
165
166 for (size_t i = 0; i < m_filenames.GetCount(); i++)
167 {
168 filenames += m_filenames[i];
169 filenames += (wxChar) 0;
170 }
171
172 memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 );
173
174 return TRUE;
175}
176
177size_t wxFileDataObject::GetDataSize() const
178{
179 size_t res = 0;
180
181 for (size_t i = 0; i < m_filenames.GetCount(); i++)
182 {
183 res += m_filenames[i].Len();
184 res += 1;
185 }
186
187 return res + 1;
188}
189
190bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf)
191{
192 /* TODO */
193
194 wxString file( (const char *)buf ); /* char, not wxChar */
195
196 AddFile( file );
197
198 return TRUE;
199}
200
201void wxFileDataObject::AddFile( const wxString &filename )
202{
203 m_filenames.Add( filename );
204}
205
206// ----------------------------------------------------------------------------
207// wxBitmapDataObject
208// ----------------------------------------------------------------------------
209
210wxBitmapDataObject::wxBitmapDataObject()
211{
212 Init();
213}
214
215wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
216 : wxBitmapDataObjectBase(bitmap)
217{
218 Init();
219
220 DoConvertToPng();
221}
222
223wxBitmapDataObject::~wxBitmapDataObject()
224{
225 Clear();
226}
227
228void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
229{
230 ClearAll();
231
232 wxBitmapDataObjectBase::SetBitmap(bitmap);
233
234 DoConvertToPng();
235}
236
237bool wxBitmapDataObject::GetDataHere(void *buf) const
238{
239 if ( !m_pngSize )
240 {
241 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
242
243 return FALSE;
244 }
245
246 memcpy(buf, m_pngData, m_pngSize);
247
248 return TRUE;
249}
250
251bool wxBitmapDataObject::SetData(size_t size, const void *buf)
252{
253 Clear();
254
255 m_pngSize = size;
256 m_pngData = malloc(m_pngSize);
257
258 memcpy( m_pngData, buf, m_pngSize );
259
260 wxMemoryInputStream mstream( (char*) m_pngData, m_pngSize );
261 wxImage image;
262 wxPNGHandler handler;
263 if ( !handler.LoadFile( &image, mstream ) )
264 {
265 return FALSE;
266 }
267
268 m_bitmap = image.ConvertToBitmap();
269
270 return m_bitmap.Ok();
271}
272
273void wxBitmapDataObject::DoConvertToPng()
274{
275 if (!m_bitmap.Ok())
276 return;
277
278 wxImage image( m_bitmap );
279 wxPNGHandler handler;
280
281 wxCountingOutputStream count;
282 handler.SaveFile( &image, count );
283
284 m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
285 m_pngData = malloc(m_pngSize);
286
287 wxMemoryOutputStream mstream( (char*) m_pngData, m_pngSize );
288 handler.SaveFile( &image, mstream );
289}
290
291