| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: os2/dataobj.cpp |
| 3 | // Purpose: implementation of wx[I]DataObject class |
| 4 | // Author: David Webster |
| 5 | // Modified by: |
| 6 | // Created: 10/21/99 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1999 David Webster |
| 9 | // Licence: wxWindows license |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #ifdef __GNUG__ |
| 21 | #pragma implementation "dataobj.h" |
| 22 | #endif |
| 23 | |
| 24 | // For compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | #ifndef WX_PRECOMP |
| 28 | #include "wx/intl.h" |
| 29 | #endif |
| 30 | #include "wx/defs.h" |
| 31 | |
| 32 | #include "wx/log.h" |
| 33 | #include "wx/dataobj.h" |
| 34 | #include "wx/mstream.h" |
| 35 | #include "wx/image.h" |
| 36 | |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | // functions |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | |
| 41 | // ---------------------------------------------------------------------------- |
| 42 | // wxDataFormat |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | |
| 45 | wxDataFormat::wxDataFormat() |
| 46 | { |
| 47 | m_type = wxDF_INVALID; |
| 48 | m_format = 0; |
| 49 | } |
| 50 | |
| 51 | wxDataFormat::wxDataFormat( wxDataFormatId vType ) |
| 52 | { |
| 53 | SetType(vType); |
| 54 | } |
| 55 | |
| 56 | wxDataFormat::wxDataFormat( const wxChar* zId) |
| 57 | { |
| 58 | SetId(zId); |
| 59 | } |
| 60 | |
| 61 | wxDataFormat::wxDataFormat( const wxString& rId) |
| 62 | { |
| 63 | SetId(rId); |
| 64 | } |
| 65 | |
| 66 | wxDataFormat::wxDataFormat( NativeFormat vFormat) |
| 67 | { |
| 68 | SetId(vFormat); |
| 69 | } |
| 70 | |
| 71 | void wxDataFormat::SetType( wxDataFormatId Type ) |
| 72 | { |
| 73 | m_type = Type; |
| 74 | |
| 75 | if (m_type == wxDF_TEXT) |
| 76 | m_format = 'TEXT'; |
| 77 | else if (m_type == wxDF_BITMAP || m_type == wxDF_METAFILE ) |
| 78 | m_format = 'PICT'; |
| 79 | else if (m_type == wxDF_FILENAME) |
| 80 | m_format = 'SPEC'; |
| 81 | else |
| 82 | { |
| 83 | wxFAIL_MSG( wxT("invalid dataformat") ); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | wxDataFormatId wxDataFormat::GetType() const |
| 88 | { |
| 89 | return m_type; |
| 90 | } |
| 91 | |
| 92 | wxString wxDataFormat::GetId() const |
| 93 | { |
| 94 | wxString sRet(""); // TODO: to name of ( m_format ) ); |
| 95 | return sRet; |
| 96 | } |
| 97 | |
| 98 | void wxDataFormat::SetId( NativeFormat format ) |
| 99 | { |
| 100 | m_format = format; |
| 101 | |
| 102 | if (m_format == 'TEXT') |
| 103 | m_type = wxDF_TEXT; |
| 104 | else |
| 105 | if (m_format == 'PICT') |
| 106 | m_type = wxDF_BITMAP; |
| 107 | else |
| 108 | if (m_format == 'SPEC') |
| 109 | m_type = wxDF_FILENAME; |
| 110 | else |
| 111 | m_type = wxDF_PRIVATE; |
| 112 | } |
| 113 | |
| 114 | void wxDataFormat::SetId( const wxChar* zId ) |
| 115 | { |
| 116 | wxString tmp(zId); |
| 117 | |
| 118 | m_type = wxDF_PRIVATE; |
| 119 | m_format = 0;// TODO: get the format gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE ); |
| 120 | } |
| 121 | |
| 122 | //------------------------------------------------------------------------- |
| 123 | // wxDataObject |
| 124 | //------------------------------------------------------------------------- |
| 125 | |
| 126 | wxDataObject::wxDataObject() |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | bool wxDataObject::IsSupportedFormat( |
| 131 | const wxDataFormat& rFormat |
| 132 | , Direction vDir |
| 133 | ) const |
| 134 | { |
| 135 | size_t nFormatCount = GetFormatCount(vDir); |
| 136 | |
| 137 | if (nFormatCount == 1) |
| 138 | { |
| 139 | return rFormat == GetPreferredFormat(); |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | wxDataFormat* pFormats = new wxDataFormat[nFormatCount]; |
| 144 | GetAllFormats( pFormats |
| 145 | ,vDir |
| 146 | ); |
| 147 | |
| 148 | size_t n; |
| 149 | |
| 150 | for (n = 0; n < nFormatCount; n++) |
| 151 | { |
| 152 | if (pFormats[n] == rFormat) |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | delete [] pFormats; |
| 157 | |
| 158 | // found? |
| 159 | return n < nFormatCount; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // ---------------------------------------------------------------------------- |
| 164 | // wxFileDataObject |
| 165 | // ---------------------------------------------------------------------------- |
| 166 | |
| 167 | bool wxFileDataObject::GetDataHere( |
| 168 | void* pBuf |
| 169 | ) const |
| 170 | { |
| 171 | wxString sFilenames; |
| 172 | |
| 173 | for (size_t i = 0; i < m_filenames.GetCount(); i++) |
| 174 | { |
| 175 | sFilenames += m_filenames[i]; |
| 176 | sFilenames += (wxChar)0; |
| 177 | } |
| 178 | |
| 179 | memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1); |
| 180 | return TRUE; |
| 181 | } |
| 182 | |
| 183 | size_t wxFileDataObject::GetDataSize() const |
| 184 | { |
| 185 | size_t nRes = 0; |
| 186 | |
| 187 | for (size_t i = 0; i < m_filenames.GetCount(); i++) |
| 188 | { |
| 189 | nRes += m_filenames[i].Len(); |
| 190 | nRes += 1; |
| 191 | } |
| 192 | |
| 193 | return nRes + 1; |
| 194 | } |
| 195 | |
| 196 | bool wxFileDataObject::SetData( |
| 197 | size_t WXUNUSED(nSize) |
| 198 | , const void* pBuf |
| 199 | ) |
| 200 | { |
| 201 | /* TODO */ |
| 202 | |
| 203 | wxString sFile( (const char *)pBuf); /* char, not wxChar */ |
| 204 | |
| 205 | AddFile(sFile); |
| 206 | |
| 207 | return TRUE; |
| 208 | } |
| 209 | |
| 210 | void wxFileDataObject::AddFile( |
| 211 | const wxString& rFilename |
| 212 | ) |
| 213 | { |
| 214 | m_filenames.Add(rFilename); |
| 215 | } |
| 216 | |
| 217 | // ---------------------------------------------------------------------------- |
| 218 | // wxBitmapDataObject |
| 219 | // ---------------------------------------------------------------------------- |
| 220 | |
| 221 | wxBitmapDataObject::wxBitmapDataObject() |
| 222 | { |
| 223 | Init(); |
| 224 | } |
| 225 | |
| 226 | wxBitmapDataObject::wxBitmapDataObject( |
| 227 | const wxBitmap& rBitmap |
| 228 | ) |
| 229 | : wxBitmapDataObjectBase(rBitmap) |
| 230 | { |
| 231 | Init(); |
| 232 | |
| 233 | DoConvertToPng(); |
| 234 | } |
| 235 | |
| 236 | wxBitmapDataObject::~wxBitmapDataObject() |
| 237 | { |
| 238 | Clear(); |
| 239 | } |
| 240 | |
| 241 | void wxBitmapDataObject::SetBitmap( |
| 242 | const wxBitmap& rBitmap |
| 243 | ) |
| 244 | { |
| 245 | ClearAll(); |
| 246 | wxBitmapDataObjectBase::SetBitmap(rBitmap); |
| 247 | DoConvertToPng(); |
| 248 | } |
| 249 | |
| 250 | bool wxBitmapDataObject::GetDataHere( |
| 251 | void* pBuf |
| 252 | ) const |
| 253 | { |
| 254 | if (!m_pngSize) |
| 255 | { |
| 256 | wxFAIL_MSG(wxT("attempt to copy empty bitmap failed")); |
| 257 | return FALSE; |
| 258 | } |
| 259 | memcpy(pBuf, m_pngData, m_pngSize); |
| 260 | return TRUE; |
| 261 | } |
| 262 | |
| 263 | bool wxBitmapDataObject::SetData( |
| 264 | size_t nSize |
| 265 | , const void* pBuf |
| 266 | ) |
| 267 | { |
| 268 | Clear(); |
| 269 | m_pngSize = nSize; |
| 270 | m_pngData = malloc(m_pngSize); |
| 271 | |
| 272 | memcpy(m_pngData, pBuf, m_pngSize); |
| 273 | |
| 274 | wxMemoryInputStream vMstream((char*)m_pngData, m_pngSize); |
| 275 | wxImage vImage; |
| 276 | wxPNGHandler vHandler; |
| 277 | |
| 278 | if (!vHandler.LoadFile(&vImage, vMstream)) |
| 279 | { |
| 280 | return FALSE; |
| 281 | } |
| 282 | |
| 283 | m_bitmap = vImage.ConvertToBitmap(); |
| 284 | return m_bitmap.Ok(); |
| 285 | } |
| 286 | |
| 287 | void wxBitmapDataObject::DoConvertToPng() |
| 288 | { |
| 289 | if (!m_bitmap.Ok()) |
| 290 | return; |
| 291 | |
| 292 | wxImage vImage(m_bitmap); |
| 293 | wxPNGHandler vHandler; |
| 294 | wxCountingOutputStream vCount; |
| 295 | |
| 296 | vHandler.SaveFile(&vImage, vCount); |
| 297 | |
| 298 | m_pngSize = vCount.GetSize() + 100; // sometimes the size seems to vary ??? |
| 299 | m_pngData = malloc(m_pngSize); |
| 300 | |
| 301 | wxMemoryOutputStream vMstream((char*) m_pngData, m_pngSize); |
| 302 | |
| 303 | vHandler.SaveFile(&vImage, vMstream ); |
| 304 | } |
| 305 | |