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.Runtime.InteropServices;
34 namespace System.Windows.Forms {
35 public sealed class Clipboard {
36 #region Local Variables
37 #endregion // Local Variables
42 #endregion // Constructors
44 #region Private Methods
45 private static bool ConvertToClipboardData(ref int type, object obj, out byte[] data) {
50 private static bool ConvertFromClipboardData(int type, IntPtr data, out object obj) {
52 if (data == IntPtr.Zero) {
57 #endregion // Private Methods
59 #region Public Static Methods
60 public static IDataObject GetDataObject() {
62 IntPtr clipboard_handle;
64 DataFormats.Format item_format;
65 object managed_clipboard_item;
66 XplatUI.ClipboardToObject converter;
68 converter = new XplatUI.ClipboardToObject(ConvertFromClipboardData);
70 clipboard_handle = XplatUI.ClipboardOpen();
71 native_formats = XplatUI.ClipboardAvailableFormats(clipboard_handle);
72 if (native_formats == null) {
73 return null; // Clipboard empty
76 // Build the IDataObject
77 clipboard = new DataObject();
78 for (int i = 0; i < native_formats.Length; i++) {
79 // We might get a format we don't understand or know
80 item_format = DataFormats.GetFormat(native_formats[i]);
81 if (item_format != null) {
82 managed_clipboard_item = XplatUI.ClipboardRetrieve(clipboard_handle, native_formats[i], converter);
84 if (managed_clipboard_item != null) {
85 clipboard.SetData(item_format.Name, managed_clipboard_item);
86 // We don't handle 'bitmap' since it involves handles, so we'll equate it to dib
87 if (item_format.Name == DataFormats.Dib) {
88 clipboard.SetData(DataFormats.Bitmap, managed_clipboard_item);
94 XplatUI.ClipboardClose(clipboard_handle);
99 public static void SetDataObject(object data) {
100 SetDataObject(data, true);
104 public static void SetDataObject(object data, bool copy) {
105 IntPtr clipboard_handle;
106 XplatUI.ObjectToClipboard converter;
108 DataFormats.Format item_format;
110 converter = new XplatUI.ObjectToClipboard(ConvertToClipboardData);
112 clipboard_handle = XplatUI.ClipboardOpen();
116 item_format = DataFormats.Format.Find(data.GetType().FullName);
117 if ((item_format != null) && (item_format.Name != DataFormats.StringFormat)) {
118 native_format = item_format.Id;
121 XplatUI.ClipboardStore(clipboard_handle, data, native_format, converter);
122 XplatUI.ClipboardClose(clipboard_handle);
124 #endregion // Public Static Methods