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
61 public static IDataObject GetDataObject ()
63 return GetDataObject (false);
66 internal static IDataObject GetDataObject (bool primary_selection)
69 IntPtr clipboard_handle;
71 DataFormats.Format item_format;
72 object managed_clipboard_item;
73 XplatUI.ClipboardToObject converter;
75 converter = new XplatUI.ClipboardToObject(ConvertFromClipboardData);
77 clipboard_handle = XplatUI.ClipboardOpen (primary_selection);
78 native_formats = XplatUI.ClipboardAvailableFormats(clipboard_handle);
79 if (native_formats == null) {
80 return null; // Clipboard empty
83 // Build the IDataObject
84 clipboard = new DataObject();
85 for (int i = 0; i < native_formats.Length; i++) {
86 // We might get a format we don't understand or know
87 item_format = DataFormats.GetFormat(native_formats[i]);
89 if (item_format != null) {
90 managed_clipboard_item = XplatUI.ClipboardRetrieve(clipboard_handle, native_formats[i], converter);
92 if (managed_clipboard_item != null) {
93 clipboard.SetData(item_format.Name, managed_clipboard_item);
94 // We don't handle 'bitmap' since it involves handles, so we'll equate it to dib
95 if (item_format.Name == DataFormats.Dib) {
96 clipboard.SetData(DataFormats.Bitmap, managed_clipboard_item);
102 XplatUI.ClipboardClose(clipboard_handle);
107 public static void SetDataObject(object data) {
108 SetDataObject(data, true);
112 public static void SetDataObject(object data, bool copy) {
113 IntPtr clipboard_handle;
114 XplatUI.ObjectToClipboard converter;
116 DataFormats.Format item_format;
118 converter = new XplatUI.ObjectToClipboard(ConvertToClipboardData);
120 clipboard_handle = XplatUI.ClipboardOpen(false);
121 XplatUI.ClipboardStore(clipboard_handle, null, 0, null); // Empty clipboard
125 if (data is IDataObject) {
128 formats = ((IDataObject)data).GetFormats();
129 for (int i = 0; i < formats.Length; i++) {
130 item_format = DataFormats.GetFormat(formats[i]);
131 if ((item_format != null) && (item_format.Name != DataFormats.StringFormat)) {
132 native_format = item_format.Id;
135 XplatUI.ClipboardStore(clipboard_handle, ((IDataObject)data).GetData(formats[i]), native_format, converter);
138 item_format = DataFormats.Format.Find(data.GetType().FullName);
139 if ((item_format != null) && (item_format.Name != DataFormats.StringFormat)) {
140 native_format = item_format.Id;
143 XplatUI.ClipboardStore(clipboard_handle, data, native_format, converter);
145 XplatUI.ClipboardClose(clipboard_handle);
147 #endregion // Public Static Methods