]>
Commit | Line | Data |
---|---|---|
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 | ||
26 | GdkAtom g_textAtom = 0; | |
27 | GdkAtom g_pngAtom = 0; | |
28 | GdkAtom g_fileAtom = 0; | |
29 | ||
30 | //------------------------------------------------------------------------- | |
31 | // wxDataFormat | |
32 | //------------------------------------------------------------------------- | |
33 | ||
34 | wxDataFormat::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 | ||
48 | wxDataFormat::wxDataFormat( wxDataFormatId type ) | |
49 | { | |
50 | PrepareFormats(); | |
51 | SetType( type ); | |
52 | } | |
53 | ||
54 | wxDataFormat::wxDataFormat( const wxChar *id ) | |
55 | { | |
56 | PrepareFormats(); | |
57 | SetId( id ); | |
58 | } | |
59 | ||
60 | wxDataFormat::wxDataFormat( const wxString &id ) | |
61 | { | |
62 | PrepareFormats(); | |
63 | SetId( id ); | |
64 | } | |
65 | ||
66 | wxDataFormat::wxDataFormat( NativeFormat format ) | |
67 | { | |
68 | PrepareFormats(); | |
69 | SetId( format ); | |
70 | } | |
71 | ||
72 | void 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 | ||
91 | wxDataFormatId wxDataFormat::GetType() const | |
92 | { | |
93 | return m_type; | |
94 | } | |
95 | ||
96 | wxString wxDataFormat::GetId() const | |
97 | { | |
98 | wxString ret( gdk_atom_name( m_format ) ); // this will convert from ascii to Unicode | |
99 | return ret; | |
100 | } | |
101 | ||
102 | void 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 | ||
119 | void 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 | ||
127 | void wxDataFormat::PrepareFormats() | |
128 | { | |
129 | if (!g_textAtom) | |
130 | g_textAtom = gdk_atom_intern( "STRING", FALSE ); | |
131 | if (!g_pngAtom) | |
132 | g_pngAtom = gdk_atom_intern( "image/png", FALSE ); | |
133 | if (!g_fileAtom) | |
134 | g_fileAtom = gdk_atom_intern( "file:ALL", FALSE ); | |
135 | } | |
136 | ||
137 | //------------------------------------------------------------------------- | |
138 | // wxDataObject | |
139 | //------------------------------------------------------------------------- | |
140 | ||
141 | wxDataObject::wxDataObject() | |
142 | { | |
143 | } | |
144 | ||
145 | bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const | |
146 | { | |
147 | size_t nFormatCount = GetFormatCount(dir); | |
148 | if ( nFormatCount == 1 ) { | |
149 | return format == GetPreferredFormat(); | |
150 | } | |
151 | else { | |
152 | wxDataFormat *formats = new wxDataFormat[nFormatCount]; | |
153 | GetAllFormats(formats,dir); | |
154 | ||
155 | size_t n; | |
156 | for ( n = 0; n < nFormatCount; n++ ) { | |
157 | if ( formats[n] == format ) | |
158 | break; | |
159 | } | |
160 | ||
161 | delete [] formats; | |
162 | ||
163 | // found? | |
164 | return n < nFormatCount; | |
165 | } | |
166 | } | |
167 | ||
168 | // ---------------------------------------------------------------------------- | |
169 | // wxFileDataObject | |
170 | // ---------------------------------------------------------------------------- | |
171 | ||
172 | bool wxFileDataObject::GetDataHere(void *buf) const | |
173 | { | |
174 | wxString filenames; | |
175 | ||
176 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
177 | { | |
178 | filenames += m_filenames[i]; | |
179 | filenames += (wxChar) 0; | |
180 | } | |
181 | ||
182 | memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 ); | |
183 | ||
184 | return TRUE; | |
185 | } | |
186 | ||
187 | size_t wxFileDataObject::GetDataSize() const | |
188 | { | |
189 | size_t res = 0; | |
190 | ||
191 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
192 | { | |
193 | res += m_filenames[i].Len(); | |
194 | res += 1; | |
195 | } | |
196 | ||
197 | return res + 1; | |
198 | } | |
199 | ||
200 | bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf) | |
201 | { | |
202 | /* TODO */ | |
203 | ||
204 | wxString file( (const char *)buf ); /* char, not wxChar */ | |
205 | ||
206 | AddFile( file ); | |
207 | ||
208 | return TRUE; | |
209 | } | |
210 | ||
211 | void wxFileDataObject::AddFile( const wxString &filename ) | |
212 | { | |
213 | m_filenames.Add( filename ); | |
214 | } | |
215 | ||
216 | // ---------------------------------------------------------------------------- | |
217 | // wxBitmapDataObject | |
218 | // ---------------------------------------------------------------------------- | |
219 | ||
220 | wxBitmapDataObject::wxBitmapDataObject() | |
221 | { | |
222 | Init(); | |
223 | } | |
224 | ||
225 | wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap ) | |
226 | : wxBitmapDataObjectBase(bitmap) | |
227 | { | |
228 | Init(); | |
229 | ||
230 | DoConvertToPng(); | |
231 | } | |
232 | ||
233 | wxBitmapDataObject::~wxBitmapDataObject() | |
234 | { | |
235 | Clear(); | |
236 | } | |
237 | ||
238 | void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap ) | |
239 | { | |
240 | ClearAll(); | |
241 | ||
242 | wxBitmapDataObjectBase::SetBitmap(bitmap); | |
243 | ||
244 | DoConvertToPng(); | |
245 | } | |
246 | ||
247 | bool wxBitmapDataObject::GetDataHere(void *buf) const | |
248 | { | |
249 | if ( !m_pngSize ) | |
250 | { | |
251 | wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") ); | |
252 | ||
253 | return FALSE; | |
254 | } | |
255 | ||
256 | memcpy(buf, m_pngData, m_pngSize); | |
257 | ||
258 | return TRUE; | |
259 | } | |
260 | ||
261 | bool wxBitmapDataObject::SetData(size_t size, const void *buf) | |
262 | { | |
263 | Clear(); | |
264 | ||
265 | m_pngSize = size; | |
266 | m_pngData = malloc(m_pngSize); | |
267 | ||
268 | memcpy( m_pngData, buf, m_pngSize ); | |
269 | ||
270 | wxMemoryInputStream mstream( (char*) m_pngData, m_pngSize ); | |
271 | wxImage image; | |
272 | wxPNGHandler handler; | |
273 | if ( !handler.LoadFile( &image, mstream ) ) | |
274 | { | |
275 | return FALSE; | |
276 | } | |
277 | ||
278 | m_bitmap = image.ConvertToBitmap(); | |
279 | ||
280 | return m_bitmap.Ok(); | |
281 | } | |
282 | ||
283 | void wxBitmapDataObject::DoConvertToPng() | |
284 | { | |
285 | if (!m_bitmap.Ok()) | |
286 | return; | |
287 | ||
288 | wxImage image( m_bitmap ); | |
289 | wxPNGHandler handler; | |
290 | ||
291 | wxCountingOutputStream count; | |
292 | handler.SaveFile( &image, count ); | |
293 | ||
294 | m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ??? | |
295 | m_pngData = malloc(m_pngSize); | |
296 | ||
297 | wxMemoryOutputStream mstream( (char*) m_pngData, m_pngSize ); | |
298 | handler.SaveFile( &image, mstream ); | |
299 | } | |
300 | ||
301 |