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) {
49 //this.autoconvert = autoconvert;
51 #endregion // Constructors
63 #endregion // Properties
66 public static int Count(Entry entries) {
71 while (entries != null) {
73 entries = entries.next;
79 public static Entry Find(Entry entries, string type) {
80 while (entries != null) {
81 if (entries.type.Equals(type)) {
84 entries = entries.next;
90 public static Entry FindConvertible(Entry entries, string type) {
93 e = Find(entries, type);
100 if (type == DataFormats.Text) {
101 if (e.type == DataFormats.UnicodeText) {
104 } else if (type == DataFormats.UnicodeText) {
105 if (e.type == DataFormats.Text) {
108 } else if (type == DataFormats.StringFormat) {
109 if (e.type == DataFormats.Text) {
111 } else if (e.type == DataFormats.UnicodeText) {
121 public static string[] Entries(Entry entries, bool convertible) {
126 // Initially store into something that we can grow easily
127 list = new ArrayList(Entry.Count(entries));
136 // Add the convertibles
137 if ((Entry.Find(entries, DataFormats.Text) != null) && (Entry.Find(entries, DataFormats.UnicodeText) == null)) {
138 list.Add(DataFormats.UnicodeText);
141 if ((Entry.Find(entries, DataFormats.Text) == null) && (Entry.Find(entries, DataFormats.UnicodeText) != null)) {
142 list.Add(DataFormats.Text);
145 if (((Entry.Find(entries, DataFormats.Text) != null) || (Entry.Find(entries, DataFormats.UnicodeText) != null)) && (Entry.Find(entries, DataFormats.StringFormat) == null)) {
146 list.Add(DataFormats.StringFormat);
150 // Copy the results into a string array
151 result = new string[list.Count];
152 for (int i = 0; i < list.Count; i++) {
153 result[i] = (string)list[i];
158 #endregion // Methods
160 #endregion // DataObject.Entry class
162 #region Local Variables
163 private Entry entries;
164 #endregion // Local Variables
166 #region Public Constructors
167 public DataObject() {
171 public DataObject(object data) {
175 public DataObject(string format, object data) {
176 SetData(format, data);
178 #endregion // Public Constructors
180 #region Public Instance Properties
181 public virtual object GetData(string format) {
182 return GetData(format, true);
185 public virtual object GetData(string format, bool autoConvert) {
188 e = Entry.FindConvertible(entries, format);
190 e = Entry.Find(entries, format);
197 public virtual object GetData(Type format) {
198 return GetData(format.FullName, true);
201 public virtual bool GetDataPresent(string format) {
202 return GetDataPresent(format, true);
205 public virtual bool GetDataPresent(string format, bool autoConvert) {
207 return Entry.FindConvertible(entries, format) != null;
209 return Entry.Find(entries, format) != null;
213 public virtual bool GetDataPresent(Type format) {
214 return GetDataPresent(format.FullName, true);
217 public virtual string[] GetFormats() {
218 return GetFormats(true);
221 public virtual string[] GetFormats(bool autoConvert) {
222 return Entry.Entries(entries, autoConvert);
225 public virtual void SetData(object data) {
226 SetData(data.GetType(), data);
229 public virtual void SetData(string format, bool autoConvert, object data) {
233 entry = Entry.Find(entries, format);
236 entry = new DataObject.Entry(format, data, autoConvert);
243 if (entries == null) {
246 // Insert into the list of known/defined formats
249 while (e.next != null) {
257 public virtual void SetData(string format, object data) {
258 SetData(format, true, data);
261 public virtual void SetData(Type format, object data) {
262 SetData(EnsureFormat(format), true, data);
264 #endregion // Public Instance Properties
266 #region Public Instance Methods
267 #endregion // Public Instance Methods
269 #region Private Methods
270 internal string EnsureFormat(string name) {
271 DataFormats.Format f;
273 f = DataFormats.Format.Find(name);
275 // Register the format
276 f = DataFormats.Format.Add(name);
282 internal string EnsureFormat(Type type) {
283 return EnsureFormat(type.FullName);
286 #endregion // Private Methods