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)
30 using System.Collections;
31 using System.Runtime.InteropServices;
33 namespace System.Windows.Forms {
34 [ClassInterface(ClassInterfaceType.None)]
35 public class DataObject : IDataObject {
36 #region DataObject.Entry Class
38 #region Local Variables
41 private bool autoconvert;
43 #endregion // Local Variables
46 internal Entry(string type, object data, bool autoconvert) {
51 this.autoconvert = autoconvert;
53 #endregion // Constructors
61 #endregion // Properties
64 public static int Count(Entry entries) {
69 while (entries != null) {
71 entries = entries.next;
77 public static Entry Find(Entry entries, string type) {
78 while (entries != null) {
79 if (entries.type.Equals(type)) {
82 entries = entries.next;
88 public static Entry FindConvertible(Entry entries, string type) {
91 e = Find(entries, type);
98 if (type == DataFormats.Text) {
99 if (e.type == DataFormats.UnicodeText) {
102 } else if (type == DataFormats.UnicodeText) {
103 if (e.type == DataFormats.Text) {
106 } else if (type == DataFormats.StringFormat) {
107 if (e.type == DataFormats.Text) {
109 } else if (e.type == DataFormats.UnicodeText) {
119 public static string[] Entries(Entry entries, bool convertible) {
124 // Initially store into something that we can grow easily
125 list = new ArrayList(Entry.Count(entries));
134 // Add the convertibles
135 if ((Entry.Find(entries, DataFormats.Text) != null) && (Entry.Find(entries, DataFormats.UnicodeText) == null)) {
136 list.Add(DataFormats.UnicodeText);
139 if ((Entry.Find(entries, DataFormats.Text) == null) && (Entry.Find(entries, DataFormats.UnicodeText) != null)) {
140 list.Add(DataFormats.Text);
143 if (((Entry.Find(entries, DataFormats.Text) != null) || (Entry.Find(entries, DataFormats.UnicodeText) != null)) && (Entry.Find(entries, DataFormats.StringFormat) == null)) {
144 list.Add(DataFormats.StringFormat);
148 // Copy the results into a string array
149 result = new string[list.Count];
150 for (int i = 0; i < list.Count; i++) {
151 result[i] = (string)list[i];
156 #endregion // Methods
158 #endregion // DataObject.Entry class
160 #region Local Variables
161 private Entry entries;
162 #endregion // Local Variables
164 #region Public Constructors
165 public DataObject() {
169 public DataObject(object data) {
173 public DataObject(string format, object data) {
174 SetData(format, data);
176 #endregion // Public Constructors
178 #region Public Instance Properties
179 public virtual object GetData(string format) {
180 return GetData(format, true);
183 public virtual object GetData(string format, bool autoConvert) {
185 return Entry.FindConvertible(entries, format).Data;
187 return Entry.Find(entries, format).Data;
191 public virtual object GetData(Type format) {
192 return GetData(format.FullName, true);
195 public virtual bool GetDataPresent(string format) {
196 return GetDataPresent(format, true);
199 public virtual bool GetDataPresent(string format, bool autoConvert) {
201 return Entry.FindConvertible(entries, format) != null;
203 return Entry.Find(entries, format) != null;
207 public virtual bool GetDataPresent(Type format) {
208 return GetDataPresent(format.FullName, true);
211 public virtual string[] GetFormats() {
212 return GetFormats(true);
215 public virtual string[] GetFormats(bool autoConvert) {
216 return Entry.Entries(entries, autoConvert);
219 public virtual void SetData(object data) {
220 SetData(data.GetType(), data);
223 public virtual void SetData(string format, bool autoConvert, object data) {
227 entry = new DataObject.Entry(format, data, autoConvert);
230 if (entries == null) {
233 // Insert into the list of known/defined formats
236 while (e.next != null) {
244 public virtual void SetData(string format, object data) {
245 SetData(format, true, data);
248 public virtual void SetData(Type format, object data) {
249 SetData(EnsureFormat(format), true, data);
251 #endregion // Public Instance Properties
253 #region Public Instance Methods
254 #endregion // Public Instance Methods
256 #region Private Methods
257 internal string EnsureFormat(string name) {
258 DataFormats.Format f;
260 f = DataFormats.Format.Find(name);
262 // Register the format
263 f = DataFormats.Format.Add(name);
269 internal string EnsureFormat(Type type) {
270 return EnsureFormat(type.FullName);
273 #endregion // Private Methods