1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok (pbartok@novell.com)
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.ComponentModel;
35 using System.Runtime.InteropServices;
37 namespace System.Windows.Forms {
38 [ClassInterface(ClassInterfaceType.None)]
39 public class DataObject : IDataObject, System.Runtime.InteropServices.ComTypes.IDataObject
41 #region DataObject.Entry Class
43 #region Local Variables
46 private bool autoconvert;
48 #endregion // Local Variables
51 internal Entry(string type, object data, bool autoconvert) {
54 this.autoconvert = autoconvert;
56 #endregion // Constructors
68 public bool AutoConvert {
76 #endregion // Properties
79 public static int Count(Entry entries) {
84 while (entries != null) {
86 entries = entries.next;
92 public static Entry Find (Entry entries, string type) {
93 return Find (entries, type, false);
96 public static Entry Find(Entry entries, string type, bool only_convertible) {
97 while (entries != null) {
98 bool available = true;
99 if (only_convertible && !entries.autoconvert)
101 if (available && String.Compare (entries.type, type, true) == 0) {
104 entries = entries.next;
110 public static Entry FindConvertible(Entry entries, string type) {
113 e = Find(entries, type);
118 // map to *any* other text format if needed
119 if (type == DataFormats.StringFormat || type == DataFormats.Text || type == DataFormats.UnicodeText) {
122 if (e.type == DataFormats.StringFormat || e.type == DataFormats.Text || e.type == DataFormats.UnicodeText)
132 public static string[] Entries(Entry entries, bool convertible) {
137 // Initially store into something that we can grow easily
138 list = new ArrayList(Entry.Count(entries));
142 // Add the convertibles
143 Entry text_entry = Entry.Find (entries, DataFormats.Text);
144 Entry utext_entry = Entry.Find (entries, DataFormats.UnicodeText);
145 Entry string_entry = Entry.Find (entries, DataFormats.StringFormat);
146 bool text_convertible = text_entry != null && text_entry.AutoConvert;
147 bool utext_convertible = utext_entry != null && utext_entry.AutoConvert;
148 bool string_convertible = string_entry != null && string_entry.AutoConvert;
150 if (text_convertible || utext_convertible || string_convertible) {
151 list.Add (DataFormats.StringFormat);
152 list.Add (DataFormats.UnicodeText);
153 list.Add (DataFormats.Text);
158 if (!list.Contains (e.type))
163 // Copy the results into a string array
164 result = new string[list.Count];
165 for (int i = 0; i < list.Count; i++) {
166 result[i] = (string)list[i];
171 #endregion // Methods
173 #endregion // DataObject.Entry class
175 #region Local Variables
176 private Entry entries;
177 #endregion // Local Variables
179 #region Public Constructors
180 public DataObject() {
184 public DataObject(object data) {
188 public DataObject(string format, object data) {
189 SetData(format, data);
191 #endregion // Public Constructors
193 #region Public Instance Methods
194 public virtual bool ContainsAudio ()
196 return GetDataPresent (DataFormats.WaveAudio, true);
199 public virtual bool ContainsFileDropList ()
201 return GetDataPresent (DataFormats.FileDrop, true);
204 public virtual bool ContainsImage ()
206 return GetDataPresent (DataFormats.Bitmap, true);
209 public virtual bool ContainsText ()
211 return GetDataPresent (DataFormats.UnicodeText, true);
214 public virtual bool ContainsText (TextDataFormat format)
216 if (!Enum.IsDefined (typeof (TextDataFormat), format))
217 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for TextDataFormat", format));
219 return GetDataPresent (TextFormatToDataFormat (format), true);
222 public virtual Stream GetAudioStream ()
224 return (Stream)GetData (DataFormats.WaveAudio, true);
227 public virtual object GetData(string format) {
228 return GetData(format, true);
231 public virtual object GetData(string format, bool autoConvert) {
234 e = Entry.FindConvertible(entries, format);
236 e = Entry.Find(entries, format);
243 public virtual object GetData(Type format) {
244 return GetData(format.FullName, true);
247 public virtual bool GetDataPresent(string format) {
248 return GetDataPresent(format, true);
251 public virtual bool GetDataPresent(string format, bool autoConvert) {
253 return Entry.FindConvertible(entries, format) != null;
255 return Entry.Find(entries, format) != null;
259 public virtual bool GetDataPresent(Type format) {
260 return GetDataPresent(format.FullName, true);
263 public virtual StringCollection GetFileDropList ()
265 return (StringCollection)GetData (DataFormats.FileDrop, true);
267 public virtual string[] GetFormats() {
268 return GetFormats(true);
271 public virtual string[] GetFormats(bool autoConvert) {
272 return Entry.Entries(entries, autoConvert);
275 public virtual Image GetImage ()
277 return (Image)GetData (DataFormats.Bitmap, true);
280 public virtual string GetText ()
282 return (string)GetData (DataFormats.UnicodeText, true);
285 public virtual string GetText (TextDataFormat format)
287 if (!Enum.IsDefined (typeof (TextDataFormat), format))
288 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for TextDataFormat", format));
290 return (string)GetData (TextFormatToDataFormat (format), false);
293 public virtual void SetAudio (byte[] audioBytes)
295 if (audioBytes == null)
296 throw new ArgumentNullException ("audioBytes");
298 MemoryStream ms = new MemoryStream (audioBytes);
303 public virtual void SetAudio (Stream audioStream)
305 if (audioStream == null)
306 throw new ArgumentNullException ("audioStream");
308 SetData (DataFormats.WaveAudio, audioStream);
311 public virtual void SetData(object data) {
312 SetData(data.GetType(), data);
315 public virtual void SetData(string format, bool autoConvert, object data) {
319 entry = Entry.Find(entries, format);
322 entry = new DataObject.Entry(format, data, autoConvert);
329 if (entries == null) {
332 // Insert into the list of known/defined formats
335 while (e.next != null) {
343 public virtual void SetData(string format, object data) {
344 SetData(format, true, data);
347 public virtual void SetData(Type format, object data) {
348 SetData(EnsureFormat(format), true, data);
351 [MonoInternalNote ("Needs additional checks for valid paths, see MSDN")]
352 public virtual void SetFileDropList (StringCollection filePaths)
354 if (filePaths == null)
355 throw new ArgumentNullException ("filePaths");
357 SetData (DataFormats.FileDrop, filePaths);
360 public virtual void SetImage (Image image)
363 throw new ArgumentNullException ("image");
365 SetData (DataFormats.Bitmap, image);
368 public virtual void SetText (string textData)
370 if (string.IsNullOrEmpty (textData))
371 throw new ArgumentNullException ("text");
373 SetData (DataFormats.UnicodeText, textData);
376 public virtual void SetText (string textData, TextDataFormat format)
378 if (string.IsNullOrEmpty (textData))
379 throw new ArgumentNullException ("text");
380 if (!Enum.IsDefined (typeof (TextDataFormat), format))
381 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for TextDataFormat", format));
384 case TextDataFormat.Text:
385 SetData (DataFormats.Text, textData);
387 case TextDataFormat.UnicodeText:
388 SetData (DataFormats.UnicodeText, textData);
390 case TextDataFormat.Rtf:
391 SetData (DataFormats.Rtf, textData);
393 case TextDataFormat.Html:
394 SetData (DataFormats.Html, textData);
396 case TextDataFormat.CommaSeparatedValue:
397 SetData (DataFormats.CommaSeparatedValue, textData);
401 #endregion // Public Instance Methods
403 #region Private Methods
404 internal string EnsureFormat(string name) {
405 DataFormats.Format f;
407 f = DataFormats.Format.Find(name);
409 // Register the format
410 f = DataFormats.Format.Add(name);
416 internal string EnsureFormat(Type type) {
417 return EnsureFormat(type.FullName);
420 private string TextFormatToDataFormat (TextDataFormat format)
423 case TextDataFormat.Text:
425 return DataFormats.Text;
426 case TextDataFormat.UnicodeText:
427 return DataFormats.UnicodeText;
428 case TextDataFormat.Rtf:
429 return DataFormats.Rtf;
430 case TextDataFormat.Html:
431 return DataFormats.Html;
432 case TextDataFormat.CommaSeparatedValue:
433 return DataFormats.CommaSeparatedValue;
436 #endregion // Private Methods
438 #region IDataObject Members
439 int System.Runtime.InteropServices.ComTypes.IDataObject.DAdvise (ref System.Runtime.InteropServices.ComTypes.FORMATETC pFormatetc, System.Runtime.InteropServices.ComTypes.ADVF advf, System.Runtime.InteropServices.ComTypes.IAdviseSink adviseSink, out int connection)
441 throw new NotImplementedException ();
444 void System.Runtime.InteropServices.ComTypes.IDataObject.DUnadvise (int connection)
446 throw new NotImplementedException ();
449 int System.Runtime.InteropServices.ComTypes.IDataObject.EnumDAdvise (out System.Runtime.InteropServices.ComTypes.IEnumSTATDATA enumAdvise)
451 throw new NotImplementedException ();
454 System.Runtime.InteropServices.ComTypes.IEnumFORMATETC System.Runtime.InteropServices.ComTypes.IDataObject.EnumFormatEtc (System.Runtime.InteropServices.ComTypes.DATADIR direction)
456 throw new NotImplementedException ();
459 int System.Runtime.InteropServices.ComTypes.IDataObject.GetCanonicalFormatEtc (ref System.Runtime.InteropServices.ComTypes.FORMATETC formatIn, out System.Runtime.InteropServices.ComTypes.FORMATETC formatOut)
461 throw new NotImplementedException ();
464 void System.Runtime.InteropServices.ComTypes.IDataObject.GetData (ref System.Runtime.InteropServices.ComTypes.FORMATETC format, out System.Runtime.InteropServices.ComTypes.STGMEDIUM medium)
466 throw new NotImplementedException ();
469 void System.Runtime.InteropServices.ComTypes.IDataObject.GetDataHere (ref System.Runtime.InteropServices.ComTypes.FORMATETC format, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium)
471 throw new NotImplementedException ();
474 int System.Runtime.InteropServices.ComTypes.IDataObject.QueryGetData (ref System.Runtime.InteropServices.ComTypes.FORMATETC format)
476 throw new NotImplementedException ();
479 void System.Runtime.InteropServices.ComTypes.IDataObject.SetData (ref System.Runtime.InteropServices.ComTypes.FORMATETC formatIn, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium, bool release)
481 throw new NotImplementedException ();