]>
Commit | Line | Data |
---|---|---|
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" | |
0d2a2b60 RR |
19 | |
20 | #include "gdk/gdk.h" | |
21 | ||
d6086ea6 RR |
22 | //------------------------------------------------------------------------- |
23 | // global data | |
24 | //------------------------------------------------------------------------- | |
25 | ||
26 | GdkAtom g_textAtom = 0; | |
e2acb9ae | 27 | GdkAtom g_pngAtom = 0; |
1dd989e1 | 28 | GdkAtom g_fileAtom = 0; |
d6086ea6 | 29 | |
0d2a2b60 RR |
30 | //------------------------------------------------------------------------- |
31 | // wxDataFormat | |
32 | //------------------------------------------------------------------------- | |
33 | ||
cd5bf2a6 | 34 | wxDataFormat::wxDataFormat() |
0d2a2b60 | 35 | { |
4b3d29db VZ |
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 | |
cd5bf2a6 | 44 | m_type = wxDF_INVALID; |
1dd989e1 | 45 | m_format = (GdkAtom) 0; |
cd5bf2a6 RR |
46 | } |
47 | ||
3f480da3 | 48 | wxDataFormat::wxDataFormat( wxDataFormatId type ) |
cd5bf2a6 | 49 | { |
e2acb9ae | 50 | PrepareFormats(); |
cd5bf2a6 | 51 | SetType( type ); |
0d2a2b60 RR |
52 | } |
53 | ||
b5be07d4 | 54 | wxDataFormat::wxDataFormat( const wxChar *id ) |
8a126fcc | 55 | { |
e2acb9ae | 56 | PrepareFormats(); |
8a126fcc RR |
57 | SetId( id ); |
58 | } | |
59 | ||
0d2a2b60 RR |
60 | wxDataFormat::wxDataFormat( const wxString &id ) |
61 | { | |
e2acb9ae | 62 | PrepareFormats(); |
cd5bf2a6 | 63 | SetId( id ); |
0d2a2b60 RR |
64 | } |
65 | ||
1dd989e1 | 66 | wxDataFormat::wxDataFormat( NativeFormat format ) |
0d2a2b60 | 67 | { |
e2acb9ae | 68 | PrepareFormats(); |
1dd989e1 | 69 | SetId( format ); |
0d2a2b60 RR |
70 | } |
71 | ||
3f480da3 | 72 | void wxDataFormat::SetType( wxDataFormatId type ) |
cd5bf2a6 | 73 | { |
4b3d29db | 74 | PrepareFormats(); |
cd5bf2a6 | 75 | m_type = type; |
3f480da3 | 76 | |
cd5bf2a6 | 77 | if (m_type == wxDF_TEXT) |
1dd989e1 | 78 | m_format = g_textAtom; |
cd5bf2a6 RR |
79 | else |
80 | if (m_type == wxDF_BITMAP) | |
1dd989e1 | 81 | m_format = g_pngAtom; |
cd5bf2a6 RR |
82 | else |
83 | if (m_type == wxDF_FILENAME) | |
1dd989e1 | 84 | m_format = g_fileAtom; |
cd5bf2a6 RR |
85 | else |
86 | { | |
223d09f6 | 87 | wxFAIL_MSG( wxT("invalid dataformat") ); |
cd5bf2a6 | 88 | } |
cd5bf2a6 | 89 | } |
3f480da3 VZ |
90 | |
91 | wxDataFormatId wxDataFormat::GetType() const | |
0d2a2b60 RR |
92 | { |
93 | return m_type; | |
94 | } | |
95 | ||
96 | wxString wxDataFormat::GetId() const | |
97 | { | |
1dd989e1 RR |
98 | wxString ret( gdk_atom_name( m_format ) ); // this will convert from ascii to Unicode |
99 | return ret; | |
0d2a2b60 | 100 | } |
3f480da3 | 101 | |
1dd989e1 | 102 | void wxDataFormat::SetId( NativeFormat format ) |
3f480da3 | 103 | { |
4b3d29db | 104 | PrepareFormats(); |
1dd989e1 | 105 | m_format = format; |
3f480da3 | 106 | |
1dd989e1 RR |
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; | |
0d2a2b60 | 117 | } |
3f480da3 | 118 | |
1dd989e1 | 119 | void wxDataFormat::SetId( const wxChar *id ) |
0d2a2b60 | 120 | { |
4b3d29db | 121 | PrepareFormats(); |
1dd989e1 RR |
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? | |
0d2a2b60 | 125 | } |
3f480da3 | 126 | |
1dd989e1 | 127 | void wxDataFormat::PrepareFormats() |
0d2a2b60 | 128 | { |
e1ee679c | 129 | if (!g_textAtom) |
1dd989e1 | 130 | g_textAtom = gdk_atom_intern( "STRING", FALSE ); |
e1ee679c | 131 | if (!g_pngAtom) |
1dd989e1 | 132 | g_pngAtom = gdk_atom_intern( "image/png", FALSE ); |
e1ee679c | 133 | if (!g_fileAtom) |
1dd989e1 | 134 | g_fileAtom = gdk_atom_intern( "file:ALL", FALSE ); |
0d2a2b60 | 135 | } |
8b53e5a2 RR |
136 | |
137 | //------------------------------------------------------------------------- | |
138 | // wxDataObject | |
139 | //------------------------------------------------------------------------- | |
140 | ||
0d2a2b60 RR |
141 | wxDataObject::wxDataObject() |
142 | { | |
0d2a2b60 | 143 | } |
3f480da3 | 144 | |
b068c4e8 | 145 | bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const |
0d2a2b60 | 146 | { |
b068c4e8 | 147 | size_t nFormatCount = GetFormatCount(dir); |
1dd989e1 RR |
148 | if ( nFormatCount == 1 ) { |
149 | return format == GetPreferredFormat(); | |
150 | } | |
151 | else { | |
152 | wxDataFormat *formats = new wxDataFormat[nFormatCount]; | |
b068c4e8 | 153 | GetAllFormats(formats,dir); |
1dd989e1 RR |
154 | |
155 | size_t n; | |
156 | for ( n = 0; n < nFormatCount; n++ ) { | |
157 | if ( formats[n] == format ) | |
158 | break; | |
159 | } | |
0d2a2b60 | 160 | |
1dd989e1 | 161 | delete [] formats; |
cd5bf2a6 | 162 | |
1dd989e1 RR |
163 | // found? |
164 | return n < nFormatCount; | |
165 | } | |
0d2a2b60 RR |
166 | } |
167 | ||
8b53e5a2 RR |
168 | // ---------------------------------------------------------------------------- |
169 | // wxFileDataObject | |
170 | // ---------------------------------------------------------------------------- | |
171 | ||
e1ee679c | 172 | bool wxFileDataObject::GetDataHere(void *buf) const |
0d2a2b60 | 173 | { |
b068c4e8 | 174 | wxString filenames; |
4b3d29db | 175 | |
b068c4e8 RR |
176 | for (size_t i = 0; i < m_filenames.GetCount(); i++) |
177 | { | |
178 | filenames += m_filenames[i]; | |
4b3d29db | 179 | filenames += (wxChar) 0; |
b068c4e8 | 180 | } |
4b3d29db | 181 | |
e1ee679c | 182 | memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 ); |
0d2a2b60 | 183 | |
e1ee679c | 184 | return TRUE; |
0d2a2b60 | 185 | } |
3f480da3 | 186 | |
e1ee679c | 187 | size_t wxFileDataObject::GetDataSize() const |
0d2a2b60 | 188 | { |
b068c4e8 | 189 | size_t res = 0; |
4b3d29db | 190 | |
b068c4e8 RR |
191 | for (size_t i = 0; i < m_filenames.GetCount(); i++) |
192 | { | |
193 | res += m_filenames[i].Len(); | |
4b3d29db | 194 | res += 1; |
b068c4e8 | 195 | } |
4b3d29db | 196 | |
b068c4e8 | 197 | return res + 1; |
0d2a2b60 | 198 | } |
3f480da3 | 199 | |
2d68e1b4 | 200 | bool wxFileDataObject::SetData(size_t size, const void *buf) |
0d2a2b60 | 201 | { |
2d68e1b4 | 202 | // filenames are stores as a string with #0 as deliminators |
4b3d29db | 203 | |
2d68e1b4 RR |
204 | const char *filenames = (const char*) buf; |
205 | size_t pos = 0; | |
206 | for(;;) | |
207 | { | |
208 | if (filenames[0] == 0) | |
209 | break; | |
210 | if (pos >= size) | |
211 | break; | |
212 | wxString file( filenames ); // this returns the first file | |
213 | AddFile( file ); | |
214 | pos += file.Len()+1; | |
215 | filenames += file.Len()+1; | |
216 | } | |
3f480da3 | 217 | |
1dd989e1 RR |
218 | return TRUE; |
219 | } | |
220 | ||
b068c4e8 RR |
221 | void wxFileDataObject::AddFile( const wxString &filename ) |
222 | { | |
223 | m_filenames.Add( filename ); | |
224 | } | |
225 | ||
8b53e5a2 RR |
226 | // ---------------------------------------------------------------------------- |
227 | // wxBitmapDataObject | |
228 | // ---------------------------------------------------------------------------- | |
229 | ||
0d2a2b60 RR |
230 | wxBitmapDataObject::wxBitmapDataObject() |
231 | { | |
e1ee679c | 232 | Init(); |
0d2a2b60 RR |
233 | } |
234 | ||
235 | wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap ) | |
e1ee679c | 236 | : wxBitmapDataObjectBase(bitmap) |
0d2a2b60 | 237 | { |
e1ee679c VZ |
238 | Init(); |
239 | ||
e2acb9ae RR |
240 | DoConvertToPng(); |
241 | } | |
242 | ||
243 | wxBitmapDataObject::~wxBitmapDataObject() | |
244 | { | |
e1ee679c | 245 | Clear(); |
0d2a2b60 RR |
246 | } |
247 | ||
248 | void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap ) | |
249 | { | |
e1ee679c | 250 | ClearAll(); |
0d2a2b60 | 251 | |
e1ee679c VZ |
252 | wxBitmapDataObjectBase::SetBitmap(bitmap); |
253 | ||
254 | DoConvertToPng(); | |
0d2a2b60 RR |
255 | } |
256 | ||
e1ee679c | 257 | bool wxBitmapDataObject::GetDataHere(void *buf) const |
0d2a2b60 | 258 | { |
e1ee679c | 259 | if ( !m_pngSize ) |
1dd989e1 | 260 | { |
e1ee679c VZ |
261 | wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") ); |
262 | ||
263 | return FALSE; | |
1dd989e1 | 264 | } |
0d2a2b60 | 265 | |
e1ee679c VZ |
266 | memcpy(buf, m_pngData, m_pngSize); |
267 | ||
268 | return TRUE; | |
e2acb9ae RR |
269 | } |
270 | ||
e1ee679c | 271 | bool wxBitmapDataObject::SetData(size_t size, const void *buf) |
e2acb9ae | 272 | { |
e1ee679c VZ |
273 | Clear(); |
274 | ||
1dd989e1 | 275 | m_pngSize = size; |
e1ee679c VZ |
276 | m_pngData = malloc(m_pngSize); |
277 | ||
1dd989e1 | 278 | memcpy( m_pngData, buf, m_pngSize ); |
e1ee679c | 279 | |
1dd989e1 | 280 | wxMemoryInputStream mstream( (char*) m_pngData, m_pngSize ); |
e2acb9ae RR |
281 | wxImage image; |
282 | wxPNGHandler handler; | |
e1ee679c VZ |
283 | if ( !handler.LoadFile( &image, mstream ) ) |
284 | { | |
285 | return FALSE; | |
286 | } | |
287 | ||
e2acb9ae | 288 | m_bitmap = image.ConvertToBitmap(); |
4b3d29db | 289 | |
b068c4e8 | 290 | return m_bitmap.Ok(); |
e2acb9ae RR |
291 | } |
292 | ||
293 | void wxBitmapDataObject::DoConvertToPng() | |
294 | { | |
e1ee679c VZ |
295 | if (!m_bitmap.Ok()) |
296 | return; | |
297 | ||
e2acb9ae RR |
298 | wxImage image( m_bitmap ); |
299 | wxPNGHandler handler; | |
e1ee679c | 300 | |
e2acb9ae RR |
301 | wxCountingOutputStream count; |
302 | handler.SaveFile( &image, count ); | |
e1ee679c | 303 | |
e2acb9ae | 304 | m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ??? |
e1ee679c VZ |
305 | m_pngData = malloc(m_pngSize); |
306 | ||
1dd989e1 | 307 | wxMemoryOutputStream mstream( (char*) m_pngData, m_pngSize ); |
e2acb9ae | 308 | handler.SaveFile( &image, mstream ); |
0d2a2b60 | 309 | } |
3f480da3 | 310 | |
0d2a2b60 | 311 |