PageRenderTime 1001ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataObject.cs

https://bitbucket.org/danipen/mono
C# | 485 lines | 370 code | 83 blank | 32 comment | 72 complexity | 468c5a1db4bfaea27e4515bff5f6df79 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  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:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  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.
  19. //
  20. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok (pbartok@novell.com)
  24. //
  25. //
  26. // COMPLETE
  27. using System;
  28. using System.IO;
  29. using System.Drawing;
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.ComponentModel;
  33. using System.Runtime.InteropServices;
  34. namespace System.Windows.Forms {
  35. [ClassInterface(ClassInterfaceType.None)]
  36. public class DataObject : IDataObject, System.Runtime.InteropServices.ComTypes.IDataObject
  37. {
  38. #region DataObject.Entry Class
  39. private class Entry {
  40. #region Local Variables
  41. private string type;
  42. private object data;
  43. private bool autoconvert;
  44. internal Entry next;
  45. #endregion // Local Variables
  46. #region Constructors
  47. internal Entry(string type, object data, bool autoconvert) {
  48. this.type = type;
  49. this.data = data;
  50. this.autoconvert = autoconvert;
  51. }
  52. #endregion // Constructors
  53. #region Properties
  54. public object Data {
  55. get {
  56. return data;
  57. }
  58. set {
  59. data = value;
  60. }
  61. }
  62. public bool AutoConvert {
  63. get {
  64. return autoconvert;
  65. }
  66. set {
  67. autoconvert = value;
  68. }
  69. }
  70. #endregion // Properties
  71. #region Methods
  72. public static int Count(Entry entries) {
  73. int result;
  74. result = 0;
  75. while (entries != null) {
  76. result++;
  77. entries = entries.next;
  78. }
  79. return result;
  80. }
  81. public static Entry Find (Entry entries, string type) {
  82. return Find (entries, type, false);
  83. }
  84. public static Entry Find(Entry entries, string type, bool only_convertible) {
  85. while (entries != null) {
  86. bool available = true;
  87. if (only_convertible && !entries.autoconvert)
  88. available = false;
  89. if (available && String.Compare (entries.type, type, true) == 0) {
  90. return entries;
  91. }
  92. entries = entries.next;
  93. }
  94. return null;
  95. }
  96. public static Entry FindConvertible(Entry entries, string type) {
  97. Entry e;
  98. e = Find(entries, type);
  99. if (e != null) {
  100. return e;
  101. }
  102. // map to *any* other text format if needed
  103. if (type == DataFormats.StringFormat || type == DataFormats.Text || type == DataFormats.UnicodeText) {
  104. e = entries;
  105. while (e != null) {
  106. if (e.type == DataFormats.StringFormat || e.type == DataFormats.Text || e.type == DataFormats.UnicodeText)
  107. return e;
  108. e = e.next;
  109. }
  110. }
  111. return null;
  112. }
  113. public static string[] Entries(Entry entries, bool convertible) {
  114. Entry e;
  115. ArrayList list;
  116. string[] result;
  117. // Initially store into something that we can grow easily
  118. list = new ArrayList(Entry.Count(entries));
  119. e = entries;
  120. if (convertible) {
  121. // Add the convertibles
  122. Entry text_entry = Entry.Find (entries, DataFormats.Text);
  123. Entry utext_entry = Entry.Find (entries, DataFormats.UnicodeText);
  124. Entry string_entry = Entry.Find (entries, DataFormats.StringFormat);
  125. bool text_convertible = text_entry != null && text_entry.AutoConvert;
  126. bool utext_convertible = utext_entry != null && utext_entry.AutoConvert;
  127. bool string_convertible = string_entry != null && string_entry.AutoConvert;
  128. if (text_convertible || utext_convertible || string_convertible) {
  129. list.Add (DataFormats.StringFormat);
  130. list.Add (DataFormats.UnicodeText);
  131. list.Add (DataFormats.Text);
  132. }
  133. }
  134. while (e != null) {
  135. if (!list.Contains (e.type))
  136. list.Add (e.type);
  137. e = e.next;
  138. }
  139. // Copy the results into a string array
  140. result = new string[list.Count];
  141. for (int i = 0; i < list.Count; i++) {
  142. result[i] = (string)list[i];
  143. }
  144. return result;
  145. }
  146. #endregion // Methods
  147. }
  148. #endregion // DataObject.Entry class
  149. #region Local Variables
  150. private Entry entries;
  151. #endregion // Local Variables
  152. #region Public Constructors
  153. public DataObject() {
  154. entries = null;
  155. }
  156. public DataObject(object data) {
  157. SetData(data);
  158. }
  159. public DataObject(string format, object data) {
  160. SetData(format, data);
  161. }
  162. #endregion // Public Constructors
  163. #region Public Instance Methods
  164. public virtual bool ContainsAudio ()
  165. {
  166. return GetDataPresent (DataFormats.WaveAudio, true);
  167. }
  168. public virtual bool ContainsFileDropList ()
  169. {
  170. return GetDataPresent (DataFormats.FileDrop, true);
  171. }
  172. public virtual bool ContainsImage ()
  173. {
  174. return GetDataPresent (DataFormats.Bitmap, true);
  175. }
  176. public virtual bool ContainsText ()
  177. {
  178. return GetDataPresent (DataFormats.UnicodeText, true);
  179. }
  180. public virtual bool ContainsText (TextDataFormat format)
  181. {
  182. if (!Enum.IsDefined (typeof (TextDataFormat), format))
  183. throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for TextDataFormat", format));
  184. return GetDataPresent (TextFormatToDataFormat (format), true);
  185. }
  186. public virtual Stream GetAudioStream ()
  187. {
  188. return (Stream)GetData (DataFormats.WaveAudio, true);
  189. }
  190. public virtual object GetData(string format) {
  191. return GetData(format, true);
  192. }
  193. public virtual object GetData(string format, bool autoConvert) {
  194. Entry e;
  195. if (autoConvert) {
  196. e = Entry.FindConvertible(entries, format);
  197. } else {
  198. e = Entry.Find(entries, format);
  199. }
  200. if (e == null)
  201. return null;
  202. return e.Data;
  203. }
  204. public virtual object GetData(Type format) {
  205. return GetData(format.FullName, true);
  206. }
  207. public virtual bool GetDataPresent(string format) {
  208. return GetDataPresent(format, true);
  209. }
  210. public virtual bool GetDataPresent(string format, bool autoConvert) {
  211. if (autoConvert) {
  212. return Entry.FindConvertible(entries, format) != null;
  213. } else {
  214. return Entry.Find(entries, format) != null;
  215. }
  216. }
  217. public virtual bool GetDataPresent(Type format) {
  218. return GetDataPresent(format.FullName, true);
  219. }
  220. public virtual StringCollection GetFileDropList ()
  221. {
  222. return (StringCollection)GetData (DataFormats.FileDrop, true);
  223. }
  224. public virtual string[] GetFormats() {
  225. return GetFormats(true);
  226. }
  227. public virtual string[] GetFormats(bool autoConvert) {
  228. return Entry.Entries(entries, autoConvert);
  229. }
  230. public virtual Image GetImage ()
  231. {
  232. return (Image)GetData (DataFormats.Bitmap, true);
  233. }
  234. public virtual string GetText ()
  235. {
  236. return (string)GetData (DataFormats.UnicodeText, true);
  237. }
  238. public virtual string GetText (TextDataFormat format)
  239. {
  240. if (!Enum.IsDefined (typeof (TextDataFormat), format))
  241. throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for TextDataFormat", format));
  242. return (string)GetData (TextFormatToDataFormat (format), false);
  243. }
  244. public virtual void SetAudio (byte[] audioBytes)
  245. {
  246. if (audioBytes == null)
  247. throw new ArgumentNullException ("audioBytes");
  248. MemoryStream ms = new MemoryStream (audioBytes);
  249. SetAudio (ms);
  250. }
  251. public virtual void SetAudio (Stream audioStream)
  252. {
  253. if (audioStream == null)
  254. throw new ArgumentNullException ("audioStream");
  255. SetData (DataFormats.WaveAudio, audioStream);
  256. }
  257. public virtual void SetData(object data) {
  258. SetData(data.GetType(), data);
  259. }
  260. public virtual void SetData(string format, bool autoConvert, object data) {
  261. Entry entry;
  262. Entry e;
  263. entry = Entry.Find(entries, format);
  264. if (entry == null) {
  265. entry = new DataObject.Entry(format, data, autoConvert);
  266. } else {
  267. entry.Data = data;
  268. return;
  269. }
  270. lock (this) {
  271. if (entries == null) {
  272. entries = entry;
  273. } else {
  274. // Insert into the list of known/defined formats
  275. e = entries;
  276. while (e.next != null) {
  277. e = e.next;
  278. }
  279. e.next = entry;
  280. }
  281. }
  282. }
  283. public virtual void SetData(string format, object data) {
  284. SetData(format, true, data);
  285. }
  286. public virtual void SetData(Type format, object data) {
  287. SetData(EnsureFormat(format), true, data);
  288. }
  289. [MonoInternalNote ("Needs additional checks for valid paths, see MSDN")]
  290. public virtual void SetFileDropList (StringCollection filePaths)
  291. {
  292. if (filePaths == null)
  293. throw new ArgumentNullException ("filePaths");
  294. SetData (DataFormats.FileDrop, filePaths);
  295. }
  296. public virtual void SetImage (Image image)
  297. {
  298. if (image == null)
  299. throw new ArgumentNullException ("image");
  300. SetData (DataFormats.Bitmap, image);
  301. }
  302. public virtual void SetText (string textData)
  303. {
  304. if (string.IsNullOrEmpty (textData))
  305. throw new ArgumentNullException ("text");
  306. SetData (DataFormats.UnicodeText, textData);
  307. }
  308. public virtual void SetText (string textData, TextDataFormat format)
  309. {
  310. if (string.IsNullOrEmpty (textData))
  311. throw new ArgumentNullException ("text");
  312. if (!Enum.IsDefined (typeof (TextDataFormat), format))
  313. throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for TextDataFormat", format));
  314. switch (format) {
  315. case TextDataFormat.Text:
  316. SetData (DataFormats.Text, textData);
  317. break;
  318. case TextDataFormat.UnicodeText:
  319. SetData (DataFormats.UnicodeText, textData);
  320. break;
  321. case TextDataFormat.Rtf:
  322. SetData (DataFormats.Rtf, textData);
  323. break;
  324. case TextDataFormat.Html:
  325. SetData (DataFormats.Html, textData);
  326. break;
  327. case TextDataFormat.CommaSeparatedValue:
  328. SetData (DataFormats.CommaSeparatedValue, textData);
  329. break;
  330. }
  331. }
  332. #endregion // Public Instance Methods
  333. #region Private Methods
  334. internal string EnsureFormat(string name) {
  335. DataFormats.Format f;
  336. f = DataFormats.Format.Find(name);
  337. if (f == null) {
  338. // Register the format
  339. f = DataFormats.Format.Add(name);
  340. }
  341. return f.Name;
  342. }
  343. internal string EnsureFormat(Type type) {
  344. return EnsureFormat(type.FullName);
  345. }
  346. private string TextFormatToDataFormat (TextDataFormat format)
  347. {
  348. switch (format) {
  349. case TextDataFormat.Text:
  350. default:
  351. return DataFormats.Text;
  352. case TextDataFormat.UnicodeText:
  353. return DataFormats.UnicodeText;
  354. case TextDataFormat.Rtf:
  355. return DataFormats.Rtf;
  356. case TextDataFormat.Html:
  357. return DataFormats.Html;
  358. case TextDataFormat.CommaSeparatedValue:
  359. return DataFormats.CommaSeparatedValue;
  360. }
  361. }
  362. #endregion // Private Methods
  363. #region IDataObject Members
  364. 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)
  365. {
  366. throw new NotImplementedException ();
  367. }
  368. void System.Runtime.InteropServices.ComTypes.IDataObject.DUnadvise (int connection)
  369. {
  370. throw new NotImplementedException ();
  371. }
  372. int System.Runtime.InteropServices.ComTypes.IDataObject.EnumDAdvise (out System.Runtime.InteropServices.ComTypes.IEnumSTATDATA enumAdvise)
  373. {
  374. throw new NotImplementedException ();
  375. }
  376. System.Runtime.InteropServices.ComTypes.IEnumFORMATETC System.Runtime.InteropServices.ComTypes.IDataObject.EnumFormatEtc (System.Runtime.InteropServices.ComTypes.DATADIR direction)
  377. {
  378. throw new NotImplementedException ();
  379. }
  380. int System.Runtime.InteropServices.ComTypes.IDataObject.GetCanonicalFormatEtc (ref System.Runtime.InteropServices.ComTypes.FORMATETC formatIn, out System.Runtime.InteropServices.ComTypes.FORMATETC formatOut)
  381. {
  382. throw new NotImplementedException ();
  383. }
  384. void System.Runtime.InteropServices.ComTypes.IDataObject.GetData (ref System.Runtime.InteropServices.ComTypes.FORMATETC format, out System.Runtime.InteropServices.ComTypes.STGMEDIUM medium)
  385. {
  386. throw new NotImplementedException ();
  387. }
  388. void System.Runtime.InteropServices.ComTypes.IDataObject.GetDataHere (ref System.Runtime.InteropServices.ComTypes.FORMATETC format, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium)
  389. {
  390. throw new NotImplementedException ();
  391. }
  392. int System.Runtime.InteropServices.ComTypes.IDataObject.QueryGetData (ref System.Runtime.InteropServices.ComTypes.FORMATETC format)
  393. {
  394. throw new NotImplementedException ();
  395. }
  396. void System.Runtime.InteropServices.ComTypes.IDataObject.SetData (ref System.Runtime.InteropServices.ComTypes.FORMATETC formatIn, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium, bool release)
  397. {
  398. throw new NotImplementedException ();
  399. }
  400. #endregion
  401. }
  402. }