]> git.saurik.com Git - wxWidgets.git/blob - src/common/dobjcmn.cpp
f806dcdec6b798a940f9e2fe36090606b2c29644
[wxWidgets.git] / src / common / dobjcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/dobjcmn.cpp
3 // Purpose: implementation of data object methods common to all platforms
4 // Author: Vadim Zeitlin, Robert Roebling
5 // Modified by:
6 // Created: 19.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows Team
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "dataobjbase.h"
22 #endif
23
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #ifndef WX_PRECOMP
31 #include "wx/app.h"
32 #include "wx/debug.h"
33 #endif // WX_PRECOMP
34
35 #include "wx/dataobj.h"
36
37 // ----------------------------------------------------------------------------
38 // lists
39 // ----------------------------------------------------------------------------
40
41 #include "wx/listimpl.cpp"
42
43 WX_DEFINE_LIST(wxSimpleDataObjectList);
44
45 // ============================================================================
46 // implementation
47 // ============================================================================
48
49 // ----------------------------------------------------------------------------
50 // wxDataObjectBase
51 // ----------------------------------------------------------------------------
52
53 wxDataObjectBase::~wxDataObjectBase()
54 {
55 }
56
57 // ----------------------------------------------------------------------------
58 // wxDataObjectComposite
59 // ----------------------------------------------------------------------------
60
61 wxDataObjectSimple *
62 wxDataObjectComposite::GetObject(const wxDataFormat& format) const
63 {
64 wxSimpleDataObjectList::Node *node = m_dataObjects.GetFirst();
65 while ( node )
66 {
67 wxDataObjectSimple *dataObj = node->GetData();
68
69 if ( dataObj->GetFormat() == format )
70 {
71 return dataObj;
72 }
73
74 node = node->GetNext();
75 }
76
77 return (wxDataObjectSimple *)NULL;
78 }
79
80 void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred)
81 {
82 if ( preferred )
83 m_preferred = m_dataObjects.GetCount();
84
85 m_dataObjects.Append( dataObject );
86 }
87
88 wxDataFormat
89 wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const
90 {
91 wxSimpleDataObjectList::Node *node = m_dataObjects.Item( m_preferred );
92
93 wxCHECK_MSG( node, wxDF_INVALID, wxT("no preferred format") );
94
95 wxDataObjectSimple* dataObj = node->GetData();
96
97 return dataObj->GetFormat();
98 }
99
100 size_t wxDataObjectComposite::GetFormatCount(Direction WXUNUSED(dir)) const
101 {
102 // TODO what about the Get/Set only formats?
103 return m_dataObjects.GetCount();
104 }
105
106 void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats,
107 Direction WXUNUSED(dir)) const
108 {
109 size_t n = 0;
110 wxSimpleDataObjectList::Node *node;
111 for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() )
112 {
113 // TODO if ( !outputOnlyToo ) && this one counts ...
114 formats[n++] = node->GetData()->GetFormat();
115 }
116 }
117
118 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat& format) const
119 {
120 wxDataObjectSimple *dataObj = GetObject(format);
121
122 wxCHECK_MSG( dataObj, 0,
123 wxT("unsupported format in wxDataObjectComposite"));
124
125 return dataObj->GetDataSize();
126 }
127
128 bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format,
129 void *buf) const
130 {
131 wxDataObjectSimple *dataObj = GetObject(format);
132
133 wxCHECK_MSG( dataObj, FALSE,
134 wxT("unsupported format in wxDataObjectComposite"));
135
136 return dataObj->GetDataHere(buf);
137 }
138
139 bool wxDataObjectComposite::SetData(const wxDataFormat& format,
140 size_t len,
141 const void *buf)
142 {
143 wxDataObjectSimple *dataObj = GetObject(format);
144
145 wxCHECK_MSG( dataObj, FALSE,
146 wxT("unsupported format in wxDataObjectComposite"));
147
148 return dataObj->SetData(len, buf);
149 }
150
151 // ----------------------------------------------------------------------------
152 // wxTextDataObject
153 // ----------------------------------------------------------------------------
154
155 size_t wxTextDataObject::GetDataSize() const
156 {
157 return GetTextLength();
158 }
159
160 bool wxTextDataObject::GetDataHere(void *buf) const
161 {
162 strcpy((char *)buf, GetText().mb_str());
163
164 return TRUE;
165 }
166
167 bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
168 {
169 SetText(wxString((const char *)buf));
170
171 return TRUE;
172 }
173
174 // ----------------------------------------------------------------------------
175 // wxFileDataObjectBase
176 // ----------------------------------------------------------------------------
177
178 wxString wxFileDataObjectBase::GetFilenames() const
179 {
180 wxString str;
181 size_t count = m_filenames.GetCount();
182 for ( size_t n = 0; n < count; n++ )
183 {
184 str << m_filenames[n] << wxT('\0');
185 }
186
187 return str;
188 }
189
190 void wxFileDataObjectBase::SetFilenames(const wxChar* filenames)
191 {
192 m_filenames.Empty();
193
194 wxString current;
195 for ( const wxChar *pc = filenames; ; pc++ )
196 {
197 if ( *pc )
198 {
199 current += *pc;
200 }
201 else
202 {
203 if ( !current )
204 {
205 // 2 consecutive NULs - this is the end of the string
206 break;
207 }
208
209 m_filenames.Add(current);
210 current.Empty();
211 }
212 }
213 }
214
215 // ----------------------------------------------------------------------------
216 // wxCustomDataObject
217 // ----------------------------------------------------------------------------
218
219 wxCustomDataObject::~wxCustomDataObject()
220 {
221 Free();
222 }
223
224 void wxCustomDataObject::TakeData(size_t size, void *data)
225 {
226 Free();
227
228 m_size = size;
229 m_data = data;
230 }
231
232 void *wxCustomDataObject::Alloc(size_t size)
233 {
234 return (void *)new char[size];
235 }
236
237 void wxCustomDataObject::Free()
238 {
239 delete [] m_data;
240 m_size = 0;
241 m_data = (void *)NULL;
242 }
243
244 size_t wxCustomDataObject::GetDataSize() const
245 {
246 return GetSize();
247 }
248
249 bool wxCustomDataObject::GetDataHere(void *buf) const
250 {
251 void *data = GetData();
252 if ( !data )
253 return FALSE;
254
255 memcpy(buf, data, GetSize());
256
257 return TRUE;
258 }
259
260 bool wxCustomDataObject::SetData(size_t len, const void *buf)
261 {
262 Free();
263
264 m_data = Alloc(size);
265 if ( !m_data )
266 return FALSE;
267
268 memcpy(m_data, buf, m_size = len);
269
270 return TRUE;
271 }
272